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