Merge branch 'upstream'
[linux-drm-fsl-dcu.git] / fs / jfs / namei.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2004
3  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or 
8  *   (at your option) any later version.
9  * 
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software 
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 #include <linux/fs.h>
21 #include <linux/ctype.h>
22 #include <linux/quotaops.h>
23 #include "jfs_incore.h"
24 #include "jfs_superblock.h"
25 #include "jfs_inode.h"
26 #include "jfs_dinode.h"
27 #include "jfs_dmap.h"
28 #include "jfs_unicode.h"
29 #include "jfs_metapage.h"
30 #include "jfs_xattr.h"
31 #include "jfs_acl.h"
32 #include "jfs_debug.h"
33
34 /*
35  * forward references
36  */
37 struct dentry_operations jfs_ci_dentry_operations;
38
39 static s64 commitZeroLink(tid_t, struct inode *);
40
41 /*
42  * NAME:        free_ea_wmap(inode)
43  *
44  * FUNCTION:    free uncommitted extended attributes from working map 
45  *
46  */
47 static inline void free_ea_wmap(struct inode *inode)
48 {
49         dxd_t *ea = &JFS_IP(inode)->ea;
50
51         if (ea->flag & DXD_EXTENT) {
52                 /* free EA pages from cache */
53                 invalidate_dxd_metapages(inode, *ea);
54                 dbFree(inode, addressDXD(ea), lengthDXD(ea));
55         }
56         ea->flag = 0;
57 }
58
59 /*
60  * NAME:        jfs_create(dip, dentry, mode)
61  *
62  * FUNCTION:    create a regular file in the parent directory <dip>
63  *              with name = <from dentry> and mode = <mode>
64  *
65  * PARAMETER:   dip     - parent directory vnode
66  *              dentry  - dentry of new file
67  *              mode    - create mode (rwxrwxrwx).
68  *              nd- nd struct
69  *
70  * RETURN:      Errors from subroutines
71  *
72  */
73 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
74                 struct nameidata *nd)
75 {
76         int rc = 0;
77         tid_t tid;              /* transaction id */
78         struct inode *ip = NULL;        /* child directory inode */
79         ino_t ino;
80         struct component_name dname;    /* child directory name */
81         struct btstack btstack;
82         struct inode *iplist[2];
83         struct tblock *tblk;
84
85         jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
86
87         /*
88          * search parent directory for entry/freespace
89          * (dtSearch() returns parent directory page pinned)
90          */
91         if ((rc = get_UCSname(&dname, dentry)))
92                 goto out1;
93
94         /*
95          * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
96          * block there while holding dtree page, so we allocate the inode &
97          * begin the transaction before we search the directory.
98          */
99         ip = ialloc(dip, mode);
100         if (ip == NULL) {
101                 rc = -ENOSPC;
102                 goto out2;
103         }
104
105         tid = txBegin(dip->i_sb, 0);
106
107         mutex_lock(&JFS_IP(dip)->commit_mutex);
108         mutex_lock(&JFS_IP(ip)->commit_mutex);
109
110         rc = jfs_init_acl(tid, ip, dip);
111         if (rc)
112                 goto out3;
113
114         rc = jfs_init_security(tid, ip, dip);
115         if (rc) {
116                 txAbort(tid, 0);
117                 goto out3;
118         }
119
120         if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
121                 jfs_err("jfs_create: dtSearch returned %d", rc);
122                 txAbort(tid, 0);
123                 goto out3;
124         }
125
126         tblk = tid_to_tblock(tid);
127         tblk->xflag |= COMMIT_CREATE;
128         tblk->ino = ip->i_ino;
129         tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
130
131         iplist[0] = dip;
132         iplist[1] = ip;
133
134         /*
135          * initialize the child XAD tree root in-line in inode
136          */
137         xtInitRoot(tid, ip);
138
139         /*
140          * create entry in parent directory for child directory
141          * (dtInsert() releases parent directory page)
142          */
143         ino = ip->i_ino;
144         if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
145                 if (rc == -EIO) {
146                         jfs_err("jfs_create: dtInsert returned -EIO");
147                         txAbort(tid, 1);        /* Marks Filesystem dirty */
148                 } else
149                         txAbort(tid, 0);        /* Filesystem full */
150                 goto out3;
151         }
152
153         ip->i_op = &jfs_file_inode_operations;
154         ip->i_fop = &jfs_file_operations;
155         ip->i_mapping->a_ops = &jfs_aops;
156
157         insert_inode_hash(ip);
158         mark_inode_dirty(ip);
159
160         dip->i_ctime = dip->i_mtime = CURRENT_TIME;
161
162         mark_inode_dirty(dip);
163
164         rc = txCommit(tid, 2, &iplist[0], 0);
165
166       out3:
167         txEnd(tid);
168         mutex_unlock(&JFS_IP(dip)->commit_mutex);
169         mutex_unlock(&JFS_IP(ip)->commit_mutex);
170         if (rc) {
171                 free_ea_wmap(ip);
172                 ip->i_nlink = 0;
173                 iput(ip);
174         } else
175                 d_instantiate(dentry, ip);
176
177       out2:
178         free_UCSname(&dname);
179
180       out1:
181
182         jfs_info("jfs_create: rc:%d", rc);
183         return rc;
184 }
185
186
187 /*
188  * NAME:        jfs_mkdir(dip, dentry, mode)
189  *
190  * FUNCTION:    create a child directory in the parent directory <dip>
191  *              with name = <from dentry> and mode = <mode>
192  *
193  * PARAMETER:   dip     - parent directory vnode
194  *              dentry  - dentry of child directory
195  *              mode    - create mode (rwxrwxrwx).
196  *
197  * RETURN:      Errors from subroutines
198  *
199  * note:
200  * EACCESS: user needs search+write permission on the parent directory
201  */
202 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
203 {
204         int rc = 0;
205         tid_t tid;              /* transaction id */
206         struct inode *ip = NULL;        /* child directory inode */
207         ino_t ino;
208         struct component_name dname;    /* child directory name */
209         struct btstack btstack;
210         struct inode *iplist[2];
211         struct tblock *tblk;
212
213         jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
214
215         /* link count overflow on parent directory ? */
216         if (dip->i_nlink == JFS_LINK_MAX) {
217                 rc = -EMLINK;
218                 goto out1;
219         }
220
221         /*
222          * search parent directory for entry/freespace
223          * (dtSearch() returns parent directory page pinned)
224          */
225         if ((rc = get_UCSname(&dname, dentry)))
226                 goto out1;
227
228         /*
229          * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
230          * block there while holding dtree page, so we allocate the inode &
231          * begin the transaction before we search the directory.
232          */
233         ip = ialloc(dip, S_IFDIR | mode);
234         if (ip == NULL) {
235                 rc = -ENOSPC;
236                 goto out2;
237         }
238
239         tid = txBegin(dip->i_sb, 0);
240
241         mutex_lock(&JFS_IP(dip)->commit_mutex);
242         mutex_lock(&JFS_IP(ip)->commit_mutex);
243
244         rc = jfs_init_acl(tid, ip, dip);
245         if (rc)
246                 goto out3;
247
248         rc = jfs_init_security(tid, ip, dip);
249         if (rc) {
250                 txAbort(tid, 0);
251                 goto out3;
252         }
253
254         if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
255                 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
256                 txAbort(tid, 0);
257                 goto out3;
258         }
259
260         tblk = tid_to_tblock(tid);
261         tblk->xflag |= COMMIT_CREATE;
262         tblk->ino = ip->i_ino;
263         tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
264
265         iplist[0] = dip;
266         iplist[1] = ip;
267
268         /*
269          * initialize the child directory in-line in inode
270          */
271         dtInitRoot(tid, ip, dip->i_ino);
272
273         /*
274          * create entry in parent directory for child directory
275          * (dtInsert() releases parent directory page)
276          */
277         ino = ip->i_ino;
278         if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
279                 if (rc == -EIO) {
280                         jfs_err("jfs_mkdir: dtInsert returned -EIO");
281                         txAbort(tid, 1);        /* Marks Filesystem dirty */
282                 } else
283                         txAbort(tid, 0);        /* Filesystem full */
284                 goto out3;
285         }
286
287         ip->i_nlink = 2;        /* for '.' */
288         ip->i_op = &jfs_dir_inode_operations;
289         ip->i_fop = &jfs_dir_operations;
290
291         insert_inode_hash(ip);
292         mark_inode_dirty(ip);
293
294         /* update parent directory inode */
295         dip->i_nlink++;         /* for '..' from child directory */
296         dip->i_ctime = dip->i_mtime = CURRENT_TIME;
297         mark_inode_dirty(dip);
298
299         rc = txCommit(tid, 2, &iplist[0], 0);
300
301       out3:
302         txEnd(tid);
303         mutex_unlock(&JFS_IP(dip)->commit_mutex);
304         mutex_unlock(&JFS_IP(ip)->commit_mutex);
305         if (rc) {
306                 free_ea_wmap(ip);
307                 ip->i_nlink = 0;
308                 iput(ip);
309         } else
310                 d_instantiate(dentry, ip);
311
312       out2:
313         free_UCSname(&dname);
314
315
316       out1:
317
318         jfs_info("jfs_mkdir: rc:%d", rc);
319         return rc;
320 }
321
322 /*
323  * NAME:        jfs_rmdir(dip, dentry)
324  *
325  * FUNCTION:    remove a link to child directory
326  *
327  * PARAMETER:   dip     - parent inode
328  *              dentry  - child directory dentry
329  *
330  * RETURN:      -EINVAL - if name is . or ..
331  *              -EINVAL  - if . or .. exist but are invalid.
332  *              errors from subroutines
333  *
334  * note:
335  * if other threads have the directory open when the last link 
336  * is removed, the "." and ".." entries, if present, are removed before 
337  * rmdir() returns and no new entries may be created in the directory, 
338  * but the directory is not removed until the last reference to 
339  * the directory is released (cf.unlink() of regular file).
340  */
341 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
342 {
343         int rc;
344         tid_t tid;              /* transaction id */
345         struct inode *ip = dentry->d_inode;
346         ino_t ino;
347         struct component_name dname;
348         struct inode *iplist[2];
349         struct tblock *tblk;
350
351         jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
352
353         /* Init inode for quota operations. */
354         DQUOT_INIT(ip);
355
356         /* directory must be empty to be removed */
357         if (!dtEmpty(ip)) {
358                 rc = -ENOTEMPTY;
359                 goto out;
360         }
361
362         if ((rc = get_UCSname(&dname, dentry))) {
363                 goto out;
364         }
365
366         tid = txBegin(dip->i_sb, 0);
367
368         mutex_lock(&JFS_IP(dip)->commit_mutex);
369         mutex_lock(&JFS_IP(ip)->commit_mutex);
370
371         iplist[0] = dip;
372         iplist[1] = ip;
373
374         tblk = tid_to_tblock(tid);
375         tblk->xflag |= COMMIT_DELETE;
376         tblk->u.ip = ip;
377
378         /*
379          * delete the entry of target directory from parent directory
380          */
381         ino = ip->i_ino;
382         if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
383                 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
384                 if (rc == -EIO)
385                         txAbort(tid, 1);
386                 txEnd(tid);
387                 mutex_unlock(&JFS_IP(dip)->commit_mutex);
388                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
389
390                 goto out2;
391         }
392
393         /* update parent directory's link count corresponding
394          * to ".." entry of the target directory deleted
395          */
396         dip->i_nlink--;
397         dip->i_ctime = dip->i_mtime = CURRENT_TIME;
398         mark_inode_dirty(dip);
399
400         /*
401          * OS/2 could have created EA and/or ACL
402          */
403         /* free EA from both persistent and working map */
404         if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
405                 /* free EA pages */
406                 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
407         }
408         JFS_IP(ip)->ea.flag = 0;
409
410         /* free ACL from both persistent and working map */
411         if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
412                 /* free ACL pages */
413                 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
414         }
415         JFS_IP(ip)->acl.flag = 0;
416
417         /* mark the target directory as deleted */
418         ip->i_nlink = 0;
419         mark_inode_dirty(ip);
420
421         rc = txCommit(tid, 2, &iplist[0], 0);
422
423         txEnd(tid);
424
425         mutex_unlock(&JFS_IP(dip)->commit_mutex);
426         mutex_unlock(&JFS_IP(ip)->commit_mutex);
427
428         /*
429          * Truncating the directory index table is not guaranteed.  It
430          * may need to be done iteratively
431          */
432         if (test_cflag(COMMIT_Stale, dip)) {
433                 if (dip->i_size > 1)
434                         jfs_truncate_nolock(dip, 0);
435
436                 clear_cflag(COMMIT_Stale, dip);
437         }
438
439       out2:
440         free_UCSname(&dname);
441
442       out:
443         jfs_info("jfs_rmdir: rc:%d", rc);
444         return rc;
445 }
446
447 /*
448  * NAME:        jfs_unlink(dip, dentry)
449  *
450  * FUNCTION:    remove a link to object <vp> named by <name> 
451  *              from parent directory <dvp>
452  *
453  * PARAMETER:   dip     - inode of parent directory
454  *              dentry  - dentry of object to be removed
455  *
456  * RETURN:      errors from subroutines
457  *
458  * note:
459  * temporary file: if one or more processes have the file open
460  * when the last link is removed, the link will be removed before
461  * unlink() returns, but the removal of the file contents will be
462  * postponed until all references to the files are closed.
463  *
464  * JFS does NOT support unlink() on directories.
465  *
466  */
467 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
468 {
469         int rc;
470         tid_t tid;              /* transaction id */
471         struct inode *ip = dentry->d_inode;
472         ino_t ino;
473         struct component_name dname;    /* object name */
474         struct inode *iplist[2];
475         struct tblock *tblk;
476         s64 new_size = 0;
477         int commit_flag;
478
479         jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
480
481         /* Init inode for quota operations. */
482         DQUOT_INIT(ip);
483
484         if ((rc = get_UCSname(&dname, dentry)))
485                 goto out;
486
487         IWRITE_LOCK(ip);
488
489         tid = txBegin(dip->i_sb, 0);
490
491         mutex_lock(&JFS_IP(dip)->commit_mutex);
492         mutex_lock(&JFS_IP(ip)->commit_mutex);
493
494         iplist[0] = dip;
495         iplist[1] = ip;
496
497         /*
498          * delete the entry of target file from parent directory
499          */
500         ino = ip->i_ino;
501         if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
502                 jfs_err("jfs_unlink: dtDelete returned %d", rc);
503                 if (rc == -EIO)
504                         txAbort(tid, 1);        /* Marks FS Dirty */
505                 txEnd(tid);
506                 mutex_unlock(&JFS_IP(dip)->commit_mutex);
507                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
508                 IWRITE_UNLOCK(ip);
509                 goto out1;
510         }
511
512         ASSERT(ip->i_nlink);
513
514         ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
515         mark_inode_dirty(dip);
516
517         /* update target's inode */
518         ip->i_nlink--;
519         mark_inode_dirty(ip);
520
521         /*
522          *      commit zero link count object
523          */
524         if (ip->i_nlink == 0) {
525                 assert(!test_cflag(COMMIT_Nolink, ip));
526                 /* free block resources */
527                 if ((new_size = commitZeroLink(tid, ip)) < 0) {
528                         txAbort(tid, 1);        /* Marks FS Dirty */
529                         txEnd(tid);
530                         mutex_unlock(&JFS_IP(dip)->commit_mutex);
531                         mutex_unlock(&JFS_IP(ip)->commit_mutex);
532                         IWRITE_UNLOCK(ip);
533                         rc = new_size;
534                         goto out1;
535                 }
536                 tblk = tid_to_tblock(tid);
537                 tblk->xflag |= COMMIT_DELETE;
538                 tblk->u.ip = ip;
539         }
540
541         /*
542          * Incomplete truncate of file data can
543          * result in timing problems unless we synchronously commit the
544          * transaction.
545          */
546         if (new_size)
547                 commit_flag = COMMIT_SYNC;
548         else
549                 commit_flag = 0;
550
551         /*
552          * If xtTruncate was incomplete, commit synchronously to avoid
553          * timing complications
554          */
555         rc = txCommit(tid, 2, &iplist[0], commit_flag);
556
557         txEnd(tid);
558
559         mutex_unlock(&JFS_IP(dip)->commit_mutex);
560         mutex_unlock(&JFS_IP(ip)->commit_mutex);
561
562
563         while (new_size && (rc == 0)) {
564                 tid = txBegin(dip->i_sb, 0);
565                 mutex_lock(&JFS_IP(ip)->commit_mutex);
566                 new_size = xtTruncate_pmap(tid, ip, new_size);
567                 if (new_size < 0) {
568                         txAbort(tid, 1);        /* Marks FS Dirty */
569                         rc = new_size;
570                 } else
571                         rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
572                 txEnd(tid);
573                 mutex_unlock(&JFS_IP(ip)->commit_mutex);
574         }
575
576         if (ip->i_nlink == 0)
577                 set_cflag(COMMIT_Nolink, ip);
578
579         IWRITE_UNLOCK(ip);
580
581         /*
582          * Truncating the directory index table is not guaranteed.  It
583          * may need to be done iteratively
584          */
585         if (test_cflag(COMMIT_Stale, dip)) {
586                 if (dip->i_size > 1)
587                         jfs_truncate_nolock(dip, 0);
588
589                 clear_cflag(COMMIT_Stale, dip);
590         }
591
592       out1:
593         free_UCSname(&dname);
594       out:
595         jfs_info("jfs_unlink: rc:%d", rc);
596         return rc;
597 }
598
599 /*
600  * NAME:        commitZeroLink()
601  *
602  * FUNCTION:    for non-directory, called by jfs_remove(),
603  *              truncate a regular file, directory or symbolic
604  *              link to zero length. return 0 if type is not 
605  *              one of these.
606  *
607  *              if the file is currently associated with a VM segment
608  *              only permanent disk and inode map resources are freed,
609  *              and neither the inode nor indirect blocks are modified
610  *              so that the resources can be later freed in the work
611  *              map by ctrunc1.
612  *              if there is no VM segment on entry, the resources are
613  *              freed in both work and permanent map.
614  *              (? for temporary file - memory object is cached even 
615  *              after no reference:
616  *              reference count > 0 -   )
617  *
618  * PARAMETERS:  cd      - pointer to commit data structure.
619  *                        current inode is the one to truncate.
620  *
621  * RETURN:      Errors from subroutines
622  */
623 static s64 commitZeroLink(tid_t tid, struct inode *ip)
624 {
625         int filetype;
626         struct tblock *tblk;
627
628         jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
629
630         filetype = ip->i_mode & S_IFMT;
631         switch (filetype) {
632         case S_IFREG:
633                 break;
634         case S_IFLNK:
635                 /* fast symbolic link */
636                 if (ip->i_size < IDATASIZE) {
637                         ip->i_size = 0;
638                         return 0;
639                 }
640                 break;
641         default:
642                 assert(filetype != S_IFDIR);
643                 return 0;
644         }
645
646         set_cflag(COMMIT_Freewmap, ip);
647
648         /* mark transaction of block map update type */
649         tblk = tid_to_tblock(tid);
650         tblk->xflag |= COMMIT_PMAP;
651
652         /*
653          * free EA
654          */
655         if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
656                 /* acquire maplock on EA to be freed from block map */
657                 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
658
659         /*
660          * free ACL
661          */
662         if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
663                 /* acquire maplock on EA to be freed from block map */
664                 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
665
666         /*
667          * free xtree/data (truncate to zero length):
668          * free xtree/data pages from cache if COMMIT_PWMAP, 
669          * free xtree/data blocks from persistent block map, and
670          * free xtree/data blocks from working block map if COMMIT_PWMAP;
671          */
672         if (ip->i_size)
673                 return xtTruncate_pmap(tid, ip, 0);
674
675         return 0;
676 }
677
678
679 /*
680  * NAME:        jfs_free_zero_link()
681  *
682  * FUNCTION:    for non-directory, called by iClose(),
683  *              free resources of a file from cache and WORKING map 
684  *              for a file previously committed with zero link count
685  *              while associated with a pager object,
686  *
687  * PARAMETER:   ip      - pointer to inode of file.
688  */
689 void jfs_free_zero_link(struct inode *ip)
690 {
691         int type;
692
693         jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
694
695         /* return if not reg or symbolic link or if size is
696          * already ok.
697          */
698         type = ip->i_mode & S_IFMT;
699
700         switch (type) {
701         case S_IFREG:
702                 break;
703         case S_IFLNK:
704                 /* if its contained in inode nothing to do */
705                 if (ip->i_size < IDATASIZE)
706                         return;
707                 break;
708         default:
709                 return;
710         }
711
712         /*
713          * free EA
714          */
715         if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
716                 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
717                 int xlen = lengthDXD(&JFS_IP(ip)->ea);
718                 struct maplock maplock; /* maplock for COMMIT_WMAP */
719                 struct pxd_lock *pxdlock;       /* maplock for COMMIT_WMAP */
720
721                 /* free EA pages from cache */
722                 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
723
724                 /* free EA extent from working block map */
725                 maplock.index = 1;
726                 pxdlock = (struct pxd_lock *) & maplock;
727                 pxdlock->flag = mlckFREEPXD;
728                 PXDaddress(&pxdlock->pxd, xaddr);
729                 PXDlength(&pxdlock->pxd, xlen);
730                 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
731         }
732
733         /*
734          * free ACL
735          */
736         if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
737                 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
738                 int xlen = lengthDXD(&JFS_IP(ip)->acl);
739                 struct maplock maplock; /* maplock for COMMIT_WMAP */
740                 struct pxd_lock *pxdlock;       /* maplock for COMMIT_WMAP */
741
742                 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
743
744                 /* free ACL extent from working block map */
745                 maplock.index = 1;
746                 pxdlock = (struct pxd_lock *) & maplock;
747                 pxdlock->flag = mlckFREEPXD;
748                 PXDaddress(&pxdlock->pxd, xaddr);
749                 PXDlength(&pxdlock->pxd, xlen);
750                 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
751         }
752
753         /*
754          * free xtree/data (truncate to zero length):
755          * free xtree/data pages from cache, and
756          * free xtree/data blocks from working block map;
757          */
758         if (ip->i_size)
759                 xtTruncate(0, ip, 0, COMMIT_WMAP);
760 }
761
762 /*
763  * NAME:        jfs_link(vp, dvp, name, crp)
764  *
765  * FUNCTION:    create a link to <vp> by the name = <name>
766  *              in the parent directory <dvp>
767  *
768  * PARAMETER:   vp      - target object
769  *              dvp     - parent directory of new link
770  *              name    - name of new link to target object
771  *              crp     - credential
772  *
773  * RETURN:      Errors from subroutines
774  *
775  * note:
776  * JFS does NOT support link() on directories (to prevent circular
777  * path in the directory hierarchy);
778  * EPERM: the target object is a directory, and either the caller
779  * does not have appropriate privileges or the implementation prohibits
780  * using link() on directories [XPG4.2].
781  *
782  * JFS does NOT support links between file systems:
783  * EXDEV: target object and new link are on different file systems and
784  * implementation does not support links between file systems [XPG4.2].
785  */
786 static int jfs_link(struct dentry *old_dentry,
787              struct inode *dir, struct dentry *dentry)
788 {
789         int rc;
790         tid_t tid;
791         struct inode *ip = old_dentry->d_inode;
792         ino_t ino;
793         struct component_name dname;
794         struct btstack btstack;
795         struct inode *iplist[2];
796
797         jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
798                  dentry->d_name.name);
799
800         if (ip->i_nlink == JFS_LINK_MAX)
801                 return -EMLINK;
802
803         if (ip->i_nlink == 0)
804                 return -ENOENT;
805
806         tid = txBegin(ip->i_sb, 0);
807
808         mutex_lock(&JFS_IP(dir)->commit_mutex);
809         mutex_lock(&JFS_IP(ip)->commit_mutex);
810
811         /*
812          * scan parent directory for entry/freespace
813          */
814         if ((rc = get_UCSname(&dname, dentry)))
815                 goto out;
816
817         if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
818                 goto free_dname;
819
820         /*
821          * create entry for new link in parent directory
822          */
823         ino = ip->i_ino;
824         if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
825                 goto free_dname;
826
827         /* update object inode */
828         ip->i_nlink++;          /* for new link */
829         ip->i_ctime = CURRENT_TIME;
830         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
831         mark_inode_dirty(dir);
832         atomic_inc(&ip->i_count);
833
834         iplist[0] = ip;
835         iplist[1] = dir;
836         rc = txCommit(tid, 2, &iplist[0], 0);
837
838         if (rc) {
839                 ip->i_nlink--;
840                 iput(ip);
841         } else
842                 d_instantiate(dentry, ip);
843
844       free_dname:
845         free_UCSname(&dname);
846
847       out:
848         txEnd(tid);
849
850         mutex_unlock(&JFS_IP(dir)->commit_mutex);
851         mutex_unlock(&JFS_IP(ip)->commit_mutex);
852
853         jfs_info("jfs_link: rc:%d", rc);
854         return rc;
855 }
856
857 /*
858  * NAME:        jfs_symlink(dip, dentry, name)
859  *
860  * FUNCTION:    creates a symbolic link to <symlink> by name <name>
861  *                      in directory <dip>
862  *
863  * PARAMETER:   dip         - parent directory vnode
864  *                      dentry  - dentry of symbolic link
865  *                      name    - the path name of the existing object 
866  *                                    that will be the source of the link
867  *
868  * RETURN:      errors from subroutines
869  *
870  * note:
871  * ENAMETOOLONG: pathname resolution of a symbolic link produced
872  * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
873 */
874
875 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
876                 const char *name)
877 {
878         int rc;
879         tid_t tid;
880         ino_t ino = 0;
881         struct component_name dname;
882         int ssize;              /* source pathname size */
883         struct btstack btstack;
884         struct inode *ip = dentry->d_inode;
885         unchar *i_fastsymlink;
886         s64 xlen = 0;
887         int bmask = 0, xsize;
888         s64 extent = 0, xaddr;
889         struct metapage *mp;
890         struct super_block *sb;
891         struct tblock *tblk;
892
893         struct inode *iplist[2];
894
895         jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
896
897         ssize = strlen(name) + 1;
898
899         /*
900          * search parent directory for entry/freespace
901          * (dtSearch() returns parent directory page pinned)
902          */
903
904         if ((rc = get_UCSname(&dname, dentry)))
905                 goto out1;
906
907         /*
908          * allocate on-disk/in-memory inode for symbolic link:
909          * (iAlloc() returns new, locked inode)
910          */
911         ip = ialloc(dip, S_IFLNK | 0777);
912         if (ip == NULL) {
913                 rc = -ENOSPC;
914                 goto out2;
915         }
916
917         tid = txBegin(dip->i_sb, 0);
918
919         mutex_lock(&JFS_IP(dip)->commit_mutex);
920         mutex_lock(&JFS_IP(ip)->commit_mutex);
921
922         rc = jfs_init_security(tid, ip, dip);
923         if (rc)
924                 goto out3;
925
926         tblk = tid_to_tblock(tid);
927         tblk->xflag |= COMMIT_CREATE;
928         tblk->ino = ip->i_ino;
929         tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
930
931         /* fix symlink access permission
932          * (dir_create() ANDs in the u.u_cmask, 
933          * but symlinks really need to be 777 access)
934          */
935         ip->i_mode |= 0777;
936
937         /*
938          * write symbolic link target path name
939          */
940         xtInitRoot(tid, ip);
941
942         /*
943          * write source path name inline in on-disk inode (fast symbolic link)
944          */
945
946         if (ssize <= IDATASIZE) {
947                 ip->i_op = &jfs_symlink_inode_operations;
948
949                 i_fastsymlink = JFS_IP(ip)->i_inline;
950                 memcpy(i_fastsymlink, name, ssize);
951                 ip->i_size = ssize - 1;
952
953                 /*
954                  * if symlink is > 128 bytes, we don't have the space to
955                  * store inline extended attributes
956                  */
957                 if (ssize > sizeof (JFS_IP(ip)->i_inline))
958                         JFS_IP(ip)->mode2 &= ~INLINEEA;
959
960                 jfs_info("jfs_symlink: fast symlink added  ssize:%d name:%s ",
961                          ssize, name);
962         }
963         /*
964          * write source path name in a single extent
965          */
966         else {
967                 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
968
969                 ip->i_op = &page_symlink_inode_operations;
970                 ip->i_mapping->a_ops = &jfs_aops;
971
972                 /*
973                  * even though the data of symlink object (source 
974                  * path name) is treated as non-journaled user data,
975                  * it is read/written thru buffer cache for performance.
976                  */
977                 sb = ip->i_sb;
978                 bmask = JFS_SBI(sb)->bsize - 1;
979                 xsize = (ssize + bmask) & ~bmask;
980                 xaddr = 0;
981                 xlen = xsize >> JFS_SBI(sb)->l2bsize;
982                 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
983                         txAbort(tid, 0);
984                         rc = -ENOSPC;
985                         goto out3;
986                 }
987                 extent = xaddr;
988                 ip->i_size = ssize - 1;
989                 while (ssize) {
990                         /* This is kind of silly since PATH_MAX == 4K */
991                         int copy_size = min(ssize, PSIZE);
992
993                         mp = get_metapage(ip, xaddr, PSIZE, 1);
994
995                         if (mp == NULL) {
996                                 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
997                                 rc = -EIO;
998                                 txAbort(tid, 0);
999                                 goto out3;
1000                         }
1001                         memcpy(mp->data, name, copy_size);
1002                         flush_metapage(mp);
1003                         ssize -= copy_size;
1004                         name += copy_size;
1005                         xaddr += JFS_SBI(sb)->nbperpage;
1006                 }
1007         }
1008
1009         /*
1010          * create entry for symbolic link in parent directory
1011          */
1012         rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1013         if (rc == 0) {
1014                 ino = ip->i_ino;
1015                 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1016         }
1017         if (rc) {
1018                 if (xlen)
1019                         xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1020                 txAbort(tid, 0);
1021                 /* discard new inode */
1022                 goto out3;
1023         }
1024
1025         insert_inode_hash(ip);
1026         mark_inode_dirty(ip);
1027
1028         dip->i_ctime = dip->i_mtime = CURRENT_TIME;
1029         mark_inode_dirty(dip);
1030         /*
1031          * commit update of parent directory and link object
1032          */
1033
1034         iplist[0] = dip;
1035         iplist[1] = ip;
1036         rc = txCommit(tid, 2, &iplist[0], 0);
1037
1038       out3:
1039         txEnd(tid);
1040         mutex_unlock(&JFS_IP(dip)->commit_mutex);
1041         mutex_unlock(&JFS_IP(ip)->commit_mutex);
1042         if (rc) {
1043                 free_ea_wmap(ip);
1044                 ip->i_nlink = 0;
1045                 iput(ip);
1046         } else
1047                 d_instantiate(dentry, ip);
1048
1049       out2:
1050         free_UCSname(&dname);
1051
1052       out1:
1053         jfs_info("jfs_symlink: rc:%d", rc);
1054         return rc;
1055 }
1056
1057
1058 /*
1059  * NAME:        jfs_rename
1060  *
1061  * FUNCTION:    rename a file or directory
1062  */
1063 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1064                struct inode *new_dir, struct dentry *new_dentry)
1065 {
1066         struct btstack btstack;
1067         ino_t ino;
1068         struct component_name new_dname;
1069         struct inode *new_ip;
1070         struct component_name old_dname;
1071         struct inode *old_ip;
1072         int rc;
1073         tid_t tid;
1074         struct tlock *tlck;
1075         struct dt_lock *dtlck;
1076         struct lv *lv;
1077         int ipcount;
1078         struct inode *iplist[4];
1079         struct tblock *tblk;
1080         s64 new_size = 0;
1081         int commit_flag;
1082
1083
1084         jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1085                  new_dentry->d_name.name);
1086
1087         old_ip = old_dentry->d_inode;
1088         new_ip = new_dentry->d_inode;
1089
1090         if ((rc = get_UCSname(&old_dname, old_dentry)))
1091                 goto out1;
1092
1093         if ((rc = get_UCSname(&new_dname, new_dentry)))
1094                 goto out2;
1095
1096         /*
1097          * Make sure source inode number is what we think it is
1098          */
1099         rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1100         if (rc || (ino != old_ip->i_ino)) {
1101                 rc = -ENOENT;
1102                 goto out3;
1103         }
1104
1105         /*
1106          * Make sure dest inode number (if any) is what we think it is
1107          */
1108         rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1109         if (rc == 0) {
1110                 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1111                         rc = -ESTALE;
1112                         goto out3;
1113                 }
1114         } else if (rc != -ENOENT)
1115                 goto out3;
1116         else if (new_ip) {
1117                 /* no entry exists, but one was expected */
1118                 rc = -ESTALE;
1119                 goto out3;
1120         }
1121
1122         if (S_ISDIR(old_ip->i_mode)) {
1123                 if (new_ip) {
1124                         if (!dtEmpty(new_ip)) {
1125                                 rc = -ENOTEMPTY;
1126                                 goto out3;
1127                         }
1128                 } else if ((new_dir != old_dir) &&
1129                            (new_dir->i_nlink == JFS_LINK_MAX)) {
1130                         rc = -EMLINK;
1131                         goto out3;
1132                 }
1133         } else if (new_ip) {
1134                 IWRITE_LOCK(new_ip);
1135                 /* Init inode for quota operations. */
1136                 DQUOT_INIT(new_ip);
1137         }
1138
1139         /*
1140          * The real work starts here
1141          */
1142         tid = txBegin(new_dir->i_sb, 0);
1143
1144         mutex_lock(&JFS_IP(new_dir)->commit_mutex);
1145         mutex_lock(&JFS_IP(old_ip)->commit_mutex);
1146         if (old_dir != new_dir)
1147                 mutex_lock(&JFS_IP(old_dir)->commit_mutex);
1148
1149         if (new_ip) {
1150                 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1151                 /*
1152                  * Change existing directory entry to new inode number
1153                  */
1154                 ino = new_ip->i_ino;
1155                 rc = dtModify(tid, new_dir, &new_dname, &ino,
1156                               old_ip->i_ino, JFS_RENAME);
1157                 if (rc)
1158                         goto out4;
1159                 new_ip->i_nlink--;
1160                 if (S_ISDIR(new_ip->i_mode)) {
1161                         new_ip->i_nlink--;
1162                         if (new_ip->i_nlink) {
1163                                 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1164                                 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1165                                 if (old_dir != new_dir)
1166                                         mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1167                                 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1168                                         IWRITE_UNLOCK(new_ip);
1169                                 jfs_error(new_ip->i_sb,
1170                                           "jfs_rename: new_ip->i_nlink != 0");
1171                                 return -EIO;
1172                         }
1173                         tblk = tid_to_tblock(tid);
1174                         tblk->xflag |= COMMIT_DELETE;
1175                         tblk->u.ip = new_ip;
1176                 } else if (new_ip->i_nlink == 0) {
1177                         assert(!test_cflag(COMMIT_Nolink, new_ip));
1178                         /* free block resources */
1179                         if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1180                                 txAbort(tid, 1);        /* Marks FS Dirty */
1181                                 rc = new_size;          
1182                                 goto out4;
1183                         }
1184                         tblk = tid_to_tblock(tid);
1185                         tblk->xflag |= COMMIT_DELETE;
1186                         tblk->u.ip = new_ip;
1187                 } else {
1188                         new_ip->i_ctime = CURRENT_TIME;
1189                         mark_inode_dirty(new_ip);
1190                 }
1191         } else {
1192                 /*
1193                  * Add new directory entry
1194                  */
1195                 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1196                               JFS_CREATE);
1197                 if (rc) {
1198                         jfs_err("jfs_rename didn't expect dtSearch to fail "
1199                                 "w/rc = %d", rc);
1200                         goto out4;
1201                 }
1202
1203                 ino = old_ip->i_ino;
1204                 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1205                 if (rc) {
1206                         if (rc == -EIO)
1207                                 jfs_err("jfs_rename: dtInsert returned -EIO");
1208                         goto out4;
1209                 }
1210                 if (S_ISDIR(old_ip->i_mode))
1211                         new_dir->i_nlink++;
1212         }
1213         /*
1214          * Remove old directory entry
1215          */
1216
1217         ino = old_ip->i_ino;
1218         rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1219         if (rc) {
1220                 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1221                         rc);
1222                 txAbort(tid, 1);        /* Marks Filesystem dirty */
1223                 goto out4;
1224         }
1225         if (S_ISDIR(old_ip->i_mode)) {
1226                 old_dir->i_nlink--;
1227                 if (old_dir != new_dir) {
1228                         /*
1229                          * Change inode number of parent for moved directory
1230                          */
1231
1232                         JFS_IP(old_ip)->i_dtroot.header.idotdot =
1233                                 cpu_to_le32(new_dir->i_ino);
1234
1235                         /* Linelock header of dtree */
1236                         tlck = txLock(tid, old_ip,
1237                                     (struct metapage *) &JFS_IP(old_ip)->bxflag,
1238                                       tlckDTREE | tlckBTROOT | tlckRELINK);
1239                         dtlck = (struct dt_lock *) & tlck->lock;
1240                         ASSERT(dtlck->index == 0);
1241                         lv = & dtlck->lv[0];
1242                         lv->offset = 0;
1243                         lv->length = 1;
1244                         dtlck->index++;
1245                 }
1246         }
1247
1248         /*
1249          * Update ctime on changed/moved inodes & mark dirty
1250          */
1251         old_ip->i_ctime = CURRENT_TIME;
1252         mark_inode_dirty(old_ip);
1253
1254         new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1255         mark_inode_dirty(new_dir);
1256
1257         /* Build list of inodes modified by this transaction */
1258         ipcount = 0;
1259         iplist[ipcount++] = old_ip;
1260         if (new_ip)
1261                 iplist[ipcount++] = new_ip;
1262         iplist[ipcount++] = old_dir;
1263
1264         if (old_dir != new_dir) {
1265                 iplist[ipcount++] = new_dir;
1266                 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1267                 mark_inode_dirty(old_dir);
1268         }
1269
1270         /*
1271          * Incomplete truncate of file data can
1272          * result in timing problems unless we synchronously commit the
1273          * transaction.
1274          */
1275         if (new_size)
1276                 commit_flag = COMMIT_SYNC;
1277         else
1278                 commit_flag = 0;
1279
1280         rc = txCommit(tid, ipcount, iplist, commit_flag);
1281
1282       out4:
1283         txEnd(tid);
1284
1285         mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1286         mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1287         if (old_dir != new_dir)
1288                 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1289         if (new_ip)
1290                 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1291
1292         while (new_size && (rc == 0)) {
1293                 tid = txBegin(new_ip->i_sb, 0);
1294                 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1295                 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1296                 if (new_size < 0) {
1297                         txAbort(tid, 1);
1298                         rc = new_size;          
1299                 } else
1300                         rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1301                 txEnd(tid);
1302                 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1303         }
1304         if (new_ip && (new_ip->i_nlink == 0))
1305                 set_cflag(COMMIT_Nolink, new_ip);
1306       out3:
1307         free_UCSname(&new_dname);
1308       out2:
1309         free_UCSname(&old_dname);
1310       out1:
1311         if (new_ip && !S_ISDIR(new_ip->i_mode))
1312                 IWRITE_UNLOCK(new_ip);
1313         /*
1314          * Truncating the directory index table is not guaranteed.  It
1315          * may need to be done iteratively
1316          */
1317         if (test_cflag(COMMIT_Stale, old_dir)) {
1318                 if (old_dir->i_size > 1)
1319                         jfs_truncate_nolock(old_dir, 0);
1320
1321                 clear_cflag(COMMIT_Stale, old_dir);
1322         }
1323
1324         jfs_info("jfs_rename: returning %d", rc);
1325         return rc;
1326 }
1327
1328
1329 /*
1330  * NAME:        jfs_mknod
1331  *
1332  * FUNCTION:    Create a special file (device)
1333  */
1334 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1335                 int mode, dev_t rdev)
1336 {
1337         struct jfs_inode_info *jfs_ip;
1338         struct btstack btstack;
1339         struct component_name dname;
1340         ino_t ino;
1341         struct inode *ip;
1342         struct inode *iplist[2];
1343         int rc;
1344         tid_t tid;
1345         struct tblock *tblk;
1346
1347         if (!new_valid_dev(rdev))
1348                 return -EINVAL;
1349
1350         jfs_info("jfs_mknod: %s", dentry->d_name.name);
1351
1352         if ((rc = get_UCSname(&dname, dentry)))
1353                 goto out;
1354
1355         ip = ialloc(dir, mode);
1356         if (ip == NULL) {
1357                 rc = -ENOSPC;
1358                 goto out1;
1359         }
1360         jfs_ip = JFS_IP(ip);
1361
1362         tid = txBegin(dir->i_sb, 0);
1363
1364         mutex_lock(&JFS_IP(dir)->commit_mutex);
1365         mutex_lock(&JFS_IP(ip)->commit_mutex);
1366
1367         rc = jfs_init_acl(tid, ip, dir);
1368         if (rc)
1369                 goto out3;
1370
1371         rc = jfs_init_security(tid, ip, dir);
1372         if (rc) {
1373                 txAbort(tid, 0);
1374                 goto out3;
1375         }
1376
1377         if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1378                 txAbort(tid, 0);
1379                 goto out3;
1380         }
1381
1382         tblk = tid_to_tblock(tid);
1383         tblk->xflag |= COMMIT_CREATE;
1384         tblk->ino = ip->i_ino;
1385         tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1386
1387         ino = ip->i_ino;
1388         if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1389                 txAbort(tid, 0);
1390                 goto out3;
1391         }
1392
1393         ip->i_op = &jfs_file_inode_operations;
1394         jfs_ip->dev = new_encode_dev(rdev);
1395         init_special_inode(ip, ip->i_mode, rdev);
1396
1397         insert_inode_hash(ip);
1398         mark_inode_dirty(ip);
1399
1400         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1401
1402         mark_inode_dirty(dir);
1403
1404         iplist[0] = dir;
1405         iplist[1] = ip;
1406         rc = txCommit(tid, 2, iplist, 0);
1407
1408       out3:
1409         txEnd(tid);
1410         mutex_unlock(&JFS_IP(ip)->commit_mutex);
1411         mutex_unlock(&JFS_IP(dir)->commit_mutex);
1412         if (rc) {
1413                 free_ea_wmap(ip);
1414                 ip->i_nlink = 0;
1415                 iput(ip);
1416         } else
1417                 d_instantiate(dentry, ip);
1418
1419       out1:
1420         free_UCSname(&dname);
1421
1422       out:
1423         jfs_info("jfs_mknod: returning %d", rc);
1424         return rc;
1425 }
1426
1427 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1428 {
1429         struct btstack btstack;
1430         ino_t inum;
1431         struct inode *ip;
1432         struct component_name key;
1433         const char *name = dentry->d_name.name;
1434         int len = dentry->d_name.len;
1435         int rc;
1436
1437         jfs_info("jfs_lookup: name = %s", name);
1438
1439         if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1440                 dentry->d_op = &jfs_ci_dentry_operations;
1441
1442         if ((name[0] == '.') && (len == 1))
1443                 inum = dip->i_ino;
1444         else if (strcmp(name, "..") == 0)
1445                 inum = PARENT(dip);
1446         else {
1447                 if ((rc = get_UCSname(&key, dentry)))
1448                         return ERR_PTR(rc);
1449                 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1450                 free_UCSname(&key);
1451                 if (rc == -ENOENT) {
1452                         d_add(dentry, NULL);
1453                         return ERR_PTR(0);
1454                 } else if (rc) {
1455                         jfs_err("jfs_lookup: dtSearch returned %d", rc);
1456                         return ERR_PTR(rc);
1457                 }
1458         }
1459
1460         ip = iget(dip->i_sb, inum);
1461         if (ip == NULL || is_bad_inode(ip)) {
1462                 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1463                 if (ip)
1464                         iput(ip);
1465                 return ERR_PTR(-EACCES);
1466         }
1467
1468         dentry = d_splice_alias(ip, dentry);
1469
1470         if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1471                 dentry->d_op = &jfs_ci_dentry_operations;
1472
1473         return dentry;
1474 }
1475
1476 struct dentry *jfs_get_parent(struct dentry *dentry)
1477 {
1478         struct super_block *sb = dentry->d_inode->i_sb;
1479         struct dentry *parent = ERR_PTR(-ENOENT);
1480         struct inode *inode;
1481         unsigned long parent_ino;
1482
1483         parent_ino =
1484                 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1485         inode = iget(sb, parent_ino);
1486         if (inode) {
1487                 if (is_bad_inode(inode)) {
1488                         iput(inode);
1489                         parent = ERR_PTR(-EACCES);
1490                 } else {
1491                         parent = d_alloc_anon(inode);
1492                         if (!parent) {
1493                                 parent = ERR_PTR(-ENOMEM);
1494                                 iput(inode);
1495                         }
1496                 }
1497         }
1498
1499         return parent;
1500 }
1501
1502 struct inode_operations jfs_dir_inode_operations = {
1503         .create         = jfs_create,
1504         .lookup         = jfs_lookup,
1505         .link           = jfs_link,
1506         .unlink         = jfs_unlink,
1507         .symlink        = jfs_symlink,
1508         .mkdir          = jfs_mkdir,
1509         .rmdir          = jfs_rmdir,
1510         .mknod          = jfs_mknod,
1511         .rename         = jfs_rename,
1512         .setxattr       = jfs_setxattr,
1513         .getxattr       = jfs_getxattr,
1514         .listxattr      = jfs_listxattr,
1515         .removexattr    = jfs_removexattr,
1516 #ifdef CONFIG_JFS_POSIX_ACL
1517         .setattr        = jfs_setattr,
1518         .permission     = jfs_permission,
1519 #endif
1520 };
1521
1522 const struct file_operations jfs_dir_operations = {
1523         .read           = generic_read_dir,
1524         .readdir        = jfs_readdir,
1525         .fsync          = jfs_fsync,
1526         .ioctl          = jfs_ioctl,
1527 };
1528
1529 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1530 {
1531         unsigned long hash;
1532         int i;
1533
1534         hash = init_name_hash();
1535         for (i=0; i < this->len; i++)
1536                 hash = partial_name_hash(tolower(this->name[i]), hash);
1537         this->hash = end_name_hash(hash);
1538
1539         return 0;
1540 }
1541
1542 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1543 {
1544         int i, result = 1;
1545
1546         if (a->len != b->len)
1547                 goto out;
1548         for (i=0; i < a->len; i++) {
1549                 if (tolower(a->name[i]) != tolower(b->name[i]))
1550                         goto out;
1551         }
1552         result = 0;
1553
1554         /*
1555          * We want creates to preserve case.  A negative dentry, a, that
1556          * has a different case than b may cause a new entry to be created
1557          * with the wrong case.  Since we can't tell if a comes from a negative
1558          * dentry, we blindly replace it with b.  This should be harmless if
1559          * a is not a negative dentry.
1560          */
1561         memcpy((unsigned char *)a->name, b->name, a->len);
1562 out:
1563         return result;
1564 }
1565
1566 struct dentry_operations jfs_ci_dentry_operations =
1567 {
1568         .d_hash = jfs_ci_hash,
1569         .d_compare = jfs_ci_compare,
1570 };