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