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