ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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         d_instantiate(dentry, ip);
139
140         dip->i_ctime = dip->i_mtime = CURRENT_TIME;
141
142         mark_inode_dirty(dip);
143
144         rc = txCommit(tid, 2, &iplist[0], 0);
145
146       out3:
147         txEnd(tid);
148         up(&JFS_IP(dip)->commit_sem);
149         up(&JFS_IP(ip)->commit_sem);
150         if (rc) {
151                 ip->i_nlink = 0;
152                 iput(ip);
153         }
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         d_instantiate(dentry, ip);
269
270         /* update parent directory inode */
271         dip->i_nlink++;         /* for '..' from child directory */
272         dip->i_ctime = dip->i_mtime = CURRENT_TIME;
273         mark_inode_dirty(dip);
274
275         rc = txCommit(tid, 2, &iplist[0], 0);
276
277       out3:
278         txEnd(tid);
279         up(&JFS_IP(dip)->commit_sem);
280         up(&JFS_IP(ip)->commit_sem);
281         if (rc) {
282                 ip->i_nlink = 0;
283                 iput(ip);
284         }
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         d_instantiate(dentry, ip);
810
811         iplist[0] = ip;
812         iplist[1] = dir;
813         rc = txCommit(tid, 2, &iplist[0], 0);
814
815       free_dname:
816         free_UCSname(&dname);
817
818       out:
819         txEnd(tid);
820
821         up(&JFS_IP(dir)->commit_sem);
822         up(&JFS_IP(ip)->commit_sem);
823
824         jfs_info("jfs_link: rc:%d", rc);
825         return rc;
826 }
827
828 /*
829  * NAME:        jfs_symlink(dip, dentry, name)
830  *
831  * FUNCTION:    creates a symbolic link to <symlink> by name <name>
832  *                      in directory <dip>
833  *
834  * PARAMETER:   dip         - parent directory vnode
835  *                      dentry  - dentry of symbolic link
836  *                      name    - the path name of the existing object 
837  *                                    that will be the source of the link
838  *
839  * RETURN:      errors from subroutines
840  *
841  * note:
842  * ENAMETOOLONG: pathname resolution of a symbolic link produced
843  * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
844 */
845
846 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
847                 const char *name)
848 {
849         int rc;
850         tid_t tid;
851         ino_t ino = 0;
852         struct component_name dname;
853         int ssize;              /* source pathname size */
854         struct btstack btstack;
855         struct inode *ip = dentry->d_inode;
856         unchar *i_fastsymlink;
857         s64 xlen = 0;
858         int bmask = 0, xsize;
859         s64 xaddr;
860         struct metapage *mp;
861         struct super_block *sb;
862         struct tblock *tblk;
863
864         struct inode *iplist[2];
865
866         jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
867
868         ssize = strlen(name) + 1;
869
870         /*
871          * search parent directory for entry/freespace
872          * (dtSearch() returns parent directory page pinned)
873          */
874
875         if ((rc = get_UCSname(&dname, dentry)))
876                 goto out1;
877
878         /*
879          * allocate on-disk/in-memory inode for symbolic link:
880          * (iAlloc() returns new, locked inode)
881          */
882         ip = ialloc(dip, S_IFLNK | 0777);
883         if (ip == NULL) {
884                 rc = -ENOSPC;
885                 goto out2;
886         }
887
888         tid = txBegin(dip->i_sb, 0);
889
890         down(&JFS_IP(dip)->commit_sem);
891         down(&JFS_IP(ip)->commit_sem);
892
893         if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE)))
894                 goto out3;
895
896         tblk = tid_to_tblock(tid);
897         tblk->xflag |= COMMIT_CREATE;
898         tblk->ino = ip->i_ino;
899         tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
900
901         /*
902          * create entry for symbolic link in parent directory
903          */
904
905         ino = ip->i_ino;
906
907
908
909         if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
910                 jfs_err("jfs_symlink: dtInsert returned %d", rc);
911                 /* discard ne inode */
912                 goto out3;
913
914         }
915
916         /* fix symlink access permission
917          * (dir_create() ANDs in the u.u_cmask, 
918          * but symlinks really need to be 777 access)
919          */
920         ip->i_mode |= 0777;
921
922         /*
923            *       write symbolic link target path name
924          */
925         xtInitRoot(tid, ip);
926
927         /*
928          * write source path name inline in on-disk inode (fast symbolic link)
929          */
930
931         if (ssize <= IDATASIZE) {
932                 ip->i_op = &jfs_symlink_inode_operations;
933
934                 i_fastsymlink = JFS_IP(ip)->i_inline;
935                 memcpy(i_fastsymlink, name, ssize);
936                 ip->i_size = ssize - 1;
937
938                 /*
939                  * if symlink is > 128 bytes, we don't have the space to
940                  * store inline extended attributes
941                  */
942                 if (ssize > sizeof (JFS_IP(ip)->i_inline))
943                         JFS_IP(ip)->mode2 &= ~INLINEEA;
944
945                 jfs_info("jfs_symlink: fast symlink added  ssize:%d name:%s ",
946                          ssize, name);
947         }
948         /*
949          * write source path name in a single extent
950          */
951         else {
952                 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
953
954                 ip->i_op = &page_symlink_inode_operations;
955                 ip->i_mapping->a_ops = &jfs_aops;
956
957                 /*
958                  * even though the data of symlink object (source 
959                  * path name) is treated as non-journaled user data,
960                  * it is read/written thru buffer cache for performance.
961                  */
962                 sb = ip->i_sb;
963                 bmask = JFS_SBI(sb)->bsize - 1;
964                 xsize = (ssize + bmask) & ~bmask;
965                 xaddr = 0;
966                 xlen = xsize >> JFS_SBI(sb)->l2bsize;
967                 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0)) == 0) {
968                         ip->i_size = ssize - 1;
969                         while (ssize) {
970                                 int copy_size = min(ssize, PSIZE);
971
972                                 mp = get_metapage(ip, xaddr, PSIZE, 1);
973
974                                 if (mp == NULL) {
975                                         dtDelete(tid, dip, &dname, &ino,
976                                                  JFS_REMOVE);
977                                         rc = -EIO;
978                                         goto out3;
979                                 }
980                                 memcpy(mp->data, name, copy_size);
981                                 flush_metapage(mp);
982 #if 0
983                                 set_buffer_uptodate(bp);
984                                 mark_buffer_dirty(bp, 1);
985                                 if (IS_SYNC(dip))
986                                         sync_dirty_buffer(bp);
987                                 brelse(bp);
988 #endif                          /* 0 */
989                                 ssize -= copy_size;
990                                 xaddr += JFS_SBI(sb)->nbperpage;
991                         }
992                         ip->i_blocks = LBLK2PBLK(sb, xlen);
993                 } else {
994                         dtDelete(tid, dip, &dname, &ino, JFS_REMOVE);
995                         rc = -ENOSPC;
996                         goto out3;
997                 }
998         }
999
1000         insert_inode_hash(ip);
1001         mark_inode_dirty(ip);
1002         d_instantiate(dentry, ip);
1003
1004         /*
1005          * commit update of parent directory and link object
1006          *
1007          * if extent allocation failed (ENOSPC),
1008          * the parent inode is committed regardless to avoid
1009          * backing out parent directory update (by dtInsert())
1010          * and subsequent dtDelete() which is harmless wrt 
1011          * integrity concern.  
1012          * the symlink inode will be freed by iput() at exit
1013          * as it has a zero link count (by dtDelete()) and 
1014          * no permanant resources. 
1015          */
1016
1017         iplist[0] = dip;
1018         if (rc == 0) {
1019                 iplist[1] = ip;
1020                 rc = txCommit(tid, 2, &iplist[0], 0);
1021         } else
1022                 rc = txCommit(tid, 1, &iplist[0], 0);
1023
1024       out3:
1025         txEnd(tid);
1026         up(&JFS_IP(dip)->commit_sem);
1027         up(&JFS_IP(ip)->commit_sem);
1028         if (rc) {
1029                 ip->i_nlink = 0;
1030                 iput(ip);
1031         }
1032
1033       out2:
1034         free_UCSname(&dname);
1035
1036 #ifdef CONFIG_JFS_POSIX_ACL
1037         if (rc == 0)
1038                 jfs_init_acl(ip, dip);
1039 #endif
1040
1041       out1:
1042         jfs_info("jfs_symlink: rc:%d", rc);
1043         return rc;
1044 }
1045
1046
1047 /*
1048  * NAME:        jfs_rename
1049  *
1050  * FUNCTION:    rename a file or directory
1051  */
1052 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1053                struct inode *new_dir, struct dentry *new_dentry)
1054 {
1055         struct btstack btstack;
1056         ino_t ino;
1057         struct component_name new_dname;
1058         struct inode *new_ip;
1059         struct component_name old_dname;
1060         struct inode *old_ip;
1061         int rc;
1062         tid_t tid;
1063         struct tlock *tlck;
1064         struct dt_lock *dtlck;
1065         struct lv *lv;
1066         int ipcount;
1067         struct inode *iplist[4];
1068         struct tblock *tblk;
1069         s64 new_size = 0;
1070         int commit_flag;
1071
1072
1073         jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1074                  new_dentry->d_name.name);
1075
1076         old_ip = old_dentry->d_inode;
1077         new_ip = new_dentry->d_inode;
1078
1079         if ((rc = get_UCSname(&old_dname, old_dentry)))
1080                 goto out1;
1081
1082         if ((rc = get_UCSname(&new_dname, new_dentry)))
1083                 goto out2;
1084
1085         /*
1086          * Make sure source inode number is what we think it is
1087          */
1088         rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1089         if (rc || (ino != old_ip->i_ino)) {
1090                 rc = -ENOENT;
1091                 goto out3;
1092         }
1093
1094         /*
1095          * Make sure dest inode number (if any) is what we think it is
1096          */
1097         rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1098         if (rc == 0) {
1099                 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1100                         rc = -ESTALE;
1101                         goto out3;
1102                 }
1103         } else if (rc != -ENOENT)
1104                 goto out3;
1105         else if (new_ip) {
1106                 /* no entry exists, but one was expected */
1107                 rc = -ESTALE;
1108                 goto out3;
1109         }
1110
1111         if (S_ISDIR(old_ip->i_mode)) {
1112                 if (new_ip) {
1113                         if (!dtEmpty(new_ip)) {
1114                                 rc = -ENOTEMPTY;
1115                                 goto out3;
1116                         }
1117                 } else if ((new_dir != old_dir) &&
1118                            (new_dir->i_nlink == JFS_LINK_MAX)) {
1119                         rc = -EMLINK;
1120                         goto out3;
1121                 }
1122         } else if (new_ip)
1123                 IWRITE_LOCK(new_ip);
1124
1125         /*
1126          * The real work starts here
1127          */
1128         tid = txBegin(new_dir->i_sb, 0);
1129
1130         down(&JFS_IP(new_dir)->commit_sem);
1131         down(&JFS_IP(old_ip)->commit_sem);
1132         if (old_dir != new_dir)
1133                 down(&JFS_IP(old_dir)->commit_sem);
1134
1135         if (new_ip) {
1136                 down(&JFS_IP(new_ip)->commit_sem);
1137                 /*
1138                  * Change existing directory entry to new inode number
1139                  */
1140                 ino = new_ip->i_ino;
1141                 rc = dtModify(tid, new_dir, &new_dname, &ino,
1142                               old_ip->i_ino, JFS_RENAME);
1143                 if (rc)
1144                         goto out4;
1145                 new_ip->i_nlink--;
1146                 if (S_ISDIR(new_ip->i_mode)) {
1147                         new_ip->i_nlink--;
1148                         if (new_ip->i_nlink) {
1149                                 up(&JFS_IP(new_dir)->commit_sem);
1150                                 up(&JFS_IP(old_ip)->commit_sem);
1151                                 if (old_dir != new_dir)
1152                                         up(&JFS_IP(old_dir)->commit_sem);
1153                                 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1154                                         IWRITE_UNLOCK(new_ip);
1155                                 jfs_error(new_ip->i_sb,
1156                                           "jfs_rename: new_ip->i_nlink != 0");
1157                                 return -EIO;
1158                         }
1159                         tblk = tid_to_tblock(tid);
1160                         tblk->xflag |= COMMIT_DELETE;
1161                         tblk->u.ip = new_ip;
1162                 } else if (new_ip->i_nlink == 0) {
1163                         assert(!test_cflag(COMMIT_Nolink, new_ip));
1164                         /* free block resources */
1165                         if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1166                                 txAbort(tid, 1);        /* Marks FS Dirty */
1167                                 rc = new_size;          
1168                                 goto out4;
1169                         }
1170                         tblk = tid_to_tblock(tid);
1171                         tblk->xflag |= COMMIT_DELETE;
1172                         tblk->u.ip = new_ip;
1173                 } else {
1174                         new_ip->i_ctime = CURRENT_TIME;
1175                         mark_inode_dirty(new_ip);
1176                 }
1177         } else {
1178                 /*
1179                  * Add new directory entry
1180                  */
1181                 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1182                               JFS_CREATE);
1183                 if (rc) {
1184                         jfs_err("jfs_rename didn't expect dtSearch to fail "
1185                                 "w/rc = %d", rc);
1186                         goto out4;
1187                 }
1188
1189                 ino = old_ip->i_ino;
1190                 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1191                 if (rc) {
1192                         jfs_err("jfs_rename: dtInsert failed w/rc = %d",
1193                                 rc);
1194                         goto out4;
1195                 }
1196                 if (S_ISDIR(old_ip->i_mode))
1197                         new_dir->i_nlink++;
1198         }
1199         /*
1200          * Remove old directory entry
1201          */
1202
1203         ino = old_ip->i_ino;
1204         rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1205         if (rc) {
1206                 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1207                         rc);
1208                 txAbort(tid, 1);        /* Marks Filesystem dirty */
1209                 goto out4;
1210         }
1211         if (S_ISDIR(old_ip->i_mode)) {
1212                 old_dir->i_nlink--;
1213                 if (old_dir != new_dir) {
1214                         /*
1215                          * Change inode number of parent for moved directory
1216                          */
1217
1218                         JFS_IP(old_ip)->i_dtroot.header.idotdot =
1219                                 cpu_to_le32(new_dir->i_ino);
1220
1221                         /* Linelock header of dtree */
1222                         tlck = txLock(tid, old_ip,
1223                                     (struct metapage *) &JFS_IP(old_ip)->bxflag,
1224                                       tlckDTREE | tlckBTROOT);
1225                         dtlck = (struct dt_lock *) & tlck->lock;
1226                         ASSERT(dtlck->index == 0);
1227                         lv = & dtlck->lv[0];
1228                         lv->offset = 0;
1229                         lv->length = 1;
1230                         dtlck->index++;
1231                 }
1232         }
1233
1234         /*
1235          * Update ctime on changed/moved inodes & mark dirty
1236          */
1237         old_ip->i_ctime = CURRENT_TIME;
1238         mark_inode_dirty(old_ip);
1239
1240         new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME;
1241         mark_inode_dirty(new_dir);
1242
1243         /* Build list of inodes modified by this transaction */
1244         ipcount = 0;
1245         iplist[ipcount++] = old_ip;
1246         if (new_ip)
1247                 iplist[ipcount++] = new_ip;
1248         iplist[ipcount++] = old_dir;
1249
1250         if (old_dir != new_dir) {
1251                 iplist[ipcount++] = new_dir;
1252                 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1253                 mark_inode_dirty(old_dir);
1254         }
1255
1256         /*
1257          * Incomplete truncate of file data can
1258          * result in timing problems unless we synchronously commit the
1259          * transaction.
1260          */
1261         if (new_size)
1262                 commit_flag = COMMIT_SYNC;
1263         else
1264                 commit_flag = 0;
1265
1266         rc = txCommit(tid, ipcount, iplist, commit_flag);
1267
1268       out4:
1269         txEnd(tid);
1270
1271         up(&JFS_IP(new_dir)->commit_sem);
1272         up(&JFS_IP(old_ip)->commit_sem);
1273         if (old_dir != new_dir)
1274                 up(&JFS_IP(old_dir)->commit_sem);
1275         if (new_ip)
1276                 up(&JFS_IP(new_ip)->commit_sem);
1277
1278         while (new_size && (rc == 0)) {
1279                 tid = txBegin(new_ip->i_sb, 0);
1280                 down(&JFS_IP(new_ip)->commit_sem);
1281                 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1282                 if (new_size < 0) {
1283                         txAbort(tid, 1);
1284                         rc = new_size;          
1285                 } else
1286                         rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1287                 txEnd(tid);
1288                 up(&JFS_IP(new_ip)->commit_sem);
1289         }
1290         if (new_ip && (new_ip->i_nlink == 0))
1291                 set_cflag(COMMIT_Nolink, new_ip);
1292       out3:
1293         free_UCSname(&new_dname);
1294       out2:
1295         free_UCSname(&old_dname);
1296       out1:
1297         if (new_ip && !S_ISDIR(new_ip->i_mode))
1298                 IWRITE_UNLOCK(new_ip);
1299         /*
1300          * Truncating the directory index table is not guaranteed.  It
1301          * may need to be done iteratively
1302          */
1303         if (test_cflag(COMMIT_Stale, old_dir)) {
1304                 if (old_dir->i_size > 1)
1305                         jfs_truncate_nolock(old_dir, 0);
1306
1307                 clear_cflag(COMMIT_Stale, old_dir);
1308         }
1309
1310         jfs_info("jfs_rename: returning %d", rc);
1311         return rc;
1312 }
1313
1314
1315 /*
1316  * NAME:        jfs_mknod
1317  *
1318  * FUNCTION:    Create a special file (device)
1319  */
1320 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1321                 int mode, dev_t rdev)
1322 {
1323         struct jfs_inode_info *jfs_ip;
1324         struct btstack btstack;
1325         struct component_name dname;
1326         ino_t ino;
1327         struct inode *ip;
1328         struct inode *iplist[2];
1329         int rc;
1330         tid_t tid;
1331         struct tblock *tblk;
1332
1333         if (!new_valid_dev(rdev))
1334                 return -EINVAL;
1335
1336         jfs_info("jfs_mknod: %s", dentry->d_name.name);
1337
1338         if ((rc = get_UCSname(&dname, dentry)))
1339                 goto out;
1340
1341         ip = ialloc(dir, mode);
1342         if (ip == NULL) {
1343                 rc = -ENOSPC;
1344                 goto out1;
1345         }
1346         jfs_ip = JFS_IP(ip);
1347
1348         tid = txBegin(dir->i_sb, 0);
1349
1350         down(&JFS_IP(dir)->commit_sem);
1351         down(&JFS_IP(ip)->commit_sem);
1352
1353         if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
1354                 goto out3;
1355
1356         tblk = tid_to_tblock(tid);
1357         tblk->xflag |= COMMIT_CREATE;
1358         tblk->ino = ip->i_ino;
1359         tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1360
1361         ino = ip->i_ino;
1362         if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
1363                 goto out3;
1364
1365         ip->i_op = &jfs_file_inode_operations;
1366         jfs_ip->dev = new_encode_dev(rdev);
1367         init_special_inode(ip, ip->i_mode, rdev);
1368
1369         insert_inode_hash(ip);
1370         mark_inode_dirty(ip);
1371         d_instantiate(dentry, ip);
1372
1373         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1374
1375         mark_inode_dirty(dir);
1376
1377         iplist[0] = dir;
1378         iplist[1] = ip;
1379         rc = txCommit(tid, 2, iplist, 0);
1380
1381       out3:
1382         txEnd(tid);
1383         up(&JFS_IP(ip)->commit_sem);
1384         up(&JFS_IP(dir)->commit_sem);
1385         if (rc) {
1386                 ip->i_nlink = 0;
1387                 iput(ip);
1388         }
1389
1390       out1:
1391         free_UCSname(&dname);
1392
1393 #ifdef CONFIG_JFS_POSIX_ACL
1394         if (rc == 0)
1395                 jfs_init_acl(ip, dir);
1396 #endif
1397
1398       out:
1399         jfs_info("jfs_mknod: returning %d", rc);
1400         return rc;
1401 }
1402
1403 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1404 {
1405         struct btstack btstack;
1406         ino_t inum;
1407         struct inode *ip;
1408         struct component_name key;
1409         const char *name = dentry->d_name.name;
1410         int len = dentry->d_name.len;
1411         int rc;
1412
1413         jfs_info("jfs_lookup: name = %s", name);
1414
1415
1416         if ((name[0] == '.') && (len == 1))
1417                 inum = dip->i_ino;
1418         else if (strcmp(name, "..") == 0)
1419                 inum = PARENT(dip);
1420         else {
1421                 if ((rc = get_UCSname(&key, dentry)))
1422                         return ERR_PTR(rc);
1423                 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1424                 free_UCSname(&key);
1425                 if (rc == -ENOENT) {
1426                         d_add(dentry, NULL);
1427                         return ERR_PTR(0);
1428                 } else if (rc) {
1429                         jfs_err("jfs_lookup: dtSearch returned %d", rc);
1430                         return ERR_PTR(rc);
1431                 }
1432         }
1433
1434         ip = iget(dip->i_sb, inum);
1435         if (ip == NULL || is_bad_inode(ip)) {
1436                 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1437                 if (ip)
1438                         iput(ip);
1439                 return ERR_PTR(-EACCES);
1440         }
1441
1442         return d_splice_alias(ip, dentry);
1443 }
1444
1445 struct dentry *jfs_get_parent(struct dentry *dentry)
1446 {
1447         struct super_block *sb = dentry->d_inode->i_sb;
1448         struct dentry *parent = ERR_PTR(-ENOENT);
1449         struct inode *inode;
1450         unsigned long parent_ino;
1451
1452         parent_ino =
1453                 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1454         inode = iget(sb, parent_ino);
1455         if (inode) {
1456                 if (is_bad_inode(inode)) {
1457                         iput(inode);
1458                         parent = ERR_PTR(-EACCES);
1459                 } else {
1460                         parent = d_alloc_anon(inode);
1461                         if (!parent) {
1462                                 parent = ERR_PTR(-ENOMEM);
1463                                 iput(inode);
1464                         }
1465                 }
1466         }
1467
1468         return parent;
1469 }
1470
1471 struct inode_operations jfs_dir_inode_operations = {
1472         .create         = jfs_create,
1473         .lookup         = jfs_lookup,
1474         .link           = jfs_link,
1475         .unlink         = jfs_unlink,
1476         .symlink        = jfs_symlink,
1477         .mkdir          = jfs_mkdir,
1478         .rmdir          = jfs_rmdir,
1479         .mknod          = jfs_mknod,
1480         .rename         = jfs_rename,
1481         .setxattr       = jfs_setxattr,
1482         .getxattr       = jfs_getxattr,
1483         .listxattr      = jfs_listxattr,
1484         .removexattr    = jfs_removexattr,
1485 #ifdef CONFIG_JFS_POSIX_ACL
1486         .setattr        = jfs_setattr,
1487         .permission     = jfs_permission,
1488 #endif
1489 };
1490
1491 struct file_operations jfs_dir_operations = {
1492         .read           = generic_read_dir,
1493         .readdir        = jfs_readdir,
1494         .fsync          = jfs_fsync,
1495 };