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