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