2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
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.
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.
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
21 #include <linux/ctype.h>
22 #include "jfs_incore.h"
23 #include "jfs_superblock.h"
24 #include "jfs_inode.h"
25 #include "jfs_dinode.h"
27 #include "jfs_unicode.h"
28 #include "jfs_metapage.h"
29 #include "jfs_xattr.h"
31 #include "jfs_debug.h"
33 extern struct inode_operations jfs_file_inode_operations;
34 extern struct inode_operations jfs_symlink_inode_operations;
35 extern struct file_operations jfs_file_operations;
36 extern struct address_space_operations jfs_aops;
38 extern int jfs_fsync(struct file *, struct dentry *, int);
39 extern void jfs_truncate_nolock(struct inode *, loff_t);
40 extern int jfs_init_acl(struct inode *, struct inode *);
45 struct inode_operations jfs_dir_inode_operations;
46 struct file_operations jfs_dir_operations;
47 struct dentry_operations jfs_ci_dentry_operations;
49 static s64 commitZeroLink(tid_t, struct inode *);
52 * NAME: jfs_create(dip, dentry, mode)
54 * FUNCTION: create a regular file in the parent directory <dip>
55 * with name = <from dentry> and mode = <mode>
57 * PARAMETER: dip - parent directory vnode
58 * dentry - dentry of new file
59 * mode - create mode (rwxrwxrwx).
62 * RETURN: Errors from subroutines
65 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
69 tid_t tid; /* transaction id */
70 struct inode *ip = NULL; /* child directory inode */
72 struct component_name dname; /* child directory name */
73 struct btstack btstack;
74 struct inode *iplist[2];
77 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
80 * search parent directory for entry/freespace
81 * (dtSearch() returns parent directory page pinned)
83 if ((rc = get_UCSname(&dname, dentry)))
87 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
88 * block there while holding dtree page, so we allocate the inode &
89 * begin the transaction before we search the directory.
91 ip = ialloc(dip, mode);
97 tid = txBegin(dip->i_sb, 0);
99 down(&JFS_IP(dip)->commit_sem);
100 down(&JFS_IP(ip)->commit_sem);
102 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
103 jfs_err("jfs_create: dtSearch returned %d", rc);
107 tblk = tid_to_tblock(tid);
108 tblk->xflag |= COMMIT_CREATE;
109 tblk->ino = ip->i_ino;
110 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
116 * initialize the child XAD tree root in-line in inode
121 * create entry in parent directory for child directory
122 * (dtInsert() releases parent directory page)
125 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
126 jfs_err("jfs_create: dtInsert returned %d", rc);
128 txAbort(tid, 1); /* Marks Filesystem dirty */
130 txAbort(tid, 0); /* Filesystem full */
134 ip->i_op = &jfs_file_inode_operations;
135 ip->i_fop = &jfs_file_operations;
136 ip->i_mapping->a_ops = &jfs_aops;
138 insert_inode_hash(ip);
139 mark_inode_dirty(ip);
141 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
143 mark_inode_dirty(dip);
145 rc = txCommit(tid, 2, &iplist[0], 0);
149 up(&JFS_IP(dip)->commit_sem);
150 up(&JFS_IP(ip)->commit_sem);
155 d_instantiate(dentry, ip);
158 free_UCSname(&dname);
160 #ifdef CONFIG_JFS_POSIX_ACL
162 jfs_init_acl(ip, dip);
167 jfs_info("jfs_create: rc:%d", rc);
173 * NAME: jfs_mkdir(dip, dentry, mode)
175 * FUNCTION: create a child directory in the parent directory <dip>
176 * with name = <from dentry> and mode = <mode>
178 * PARAMETER: dip - parent directory vnode
179 * dentry - dentry of child directory
180 * mode - create mode (rwxrwxrwx).
182 * RETURN: Errors from subroutines
185 * EACCESS: user needs search+write permission on the parent directory
187 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
190 tid_t tid; /* transaction id */
191 struct inode *ip = NULL; /* child directory inode */
193 struct component_name dname; /* child directory name */
194 struct btstack btstack;
195 struct inode *iplist[2];
198 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
200 /* link count overflow on parent directory ? */
201 if (dip->i_nlink == JFS_LINK_MAX) {
207 * search parent directory for entry/freespace
208 * (dtSearch() returns parent directory page pinned)
210 if ((rc = get_UCSname(&dname, dentry)))
214 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
215 * block there while holding dtree page, so we allocate the inode &
216 * begin the transaction before we search the directory.
218 ip = ialloc(dip, S_IFDIR | mode);
224 tid = txBegin(dip->i_sb, 0);
226 down(&JFS_IP(dip)->commit_sem);
227 down(&JFS_IP(ip)->commit_sem);
229 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
230 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
234 tblk = tid_to_tblock(tid);
235 tblk->xflag |= COMMIT_CREATE;
236 tblk->ino = ip->i_ino;
237 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
243 * initialize the child directory in-line in inode
245 dtInitRoot(tid, ip, dip->i_ino);
248 * create entry in parent directory for child directory
249 * (dtInsert() releases parent directory page)
252 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
253 jfs_err("jfs_mkdir: dtInsert returned %d", rc);
256 txAbort(tid, 1); /* Marks Filesystem dirty */
258 txAbort(tid, 0); /* Filesystem full */
262 ip->i_nlink = 2; /* for '.' */
263 ip->i_op = &jfs_dir_inode_operations;
264 ip->i_fop = &jfs_dir_operations;
265 ip->i_mapping->a_ops = &jfs_aops;
266 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
268 insert_inode_hash(ip);
269 mark_inode_dirty(ip);
271 /* update parent directory inode */
272 dip->i_nlink++; /* for '..' from child directory */
273 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
274 mark_inode_dirty(dip);
276 rc = txCommit(tid, 2, &iplist[0], 0);
280 up(&JFS_IP(dip)->commit_sem);
281 up(&JFS_IP(ip)->commit_sem);
286 d_instantiate(dentry, ip);
289 free_UCSname(&dname);
291 #ifdef CONFIG_JFS_POSIX_ACL
293 jfs_init_acl(ip, dip);
298 jfs_info("jfs_mkdir: rc:%d", rc);
303 * NAME: jfs_rmdir(dip, dentry)
305 * FUNCTION: remove a link to child directory
307 * PARAMETER: dip - parent inode
308 * dentry - child directory dentry
310 * RETURN: -EINVAL - if name is . or ..
311 * -EINVAL - if . or .. exist but are invalid.
312 * errors from subroutines
315 * if other threads have the directory open when the last link
316 * is removed, the "." and ".." entries, if present, are removed before
317 * rmdir() returns and no new entries may be created in the directory,
318 * but the directory is not removed until the last reference to
319 * the directory is released (cf.unlink() of regular file).
321 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
324 tid_t tid; /* transaction id */
325 struct inode *ip = dentry->d_inode;
327 struct component_name dname;
328 struct inode *iplist[2];
331 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
333 /* directory must be empty to be removed */
339 if ((rc = get_UCSname(&dname, dentry))) {
343 tid = txBegin(dip->i_sb, 0);
345 down(&JFS_IP(dip)->commit_sem);
346 down(&JFS_IP(ip)->commit_sem);
351 tblk = tid_to_tblock(tid);
352 tblk->xflag |= COMMIT_DELETE;
356 * delete the entry of target directory from parent directory
359 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
360 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
364 up(&JFS_IP(dip)->commit_sem);
365 up(&JFS_IP(ip)->commit_sem);
370 /* update parent directory's link count corresponding
371 * to ".." entry of the target directory deleted
374 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
375 mark_inode_dirty(dip);
378 * OS/2 could have created EA and/or ACL
380 /* free EA from both persistent and working map */
381 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
383 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
385 JFS_IP(ip)->ea.flag = 0;
387 /* free ACL from both persistent and working map */
388 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
390 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
392 JFS_IP(ip)->acl.flag = 0;
394 /* mark the target directory as deleted */
396 mark_inode_dirty(ip);
398 rc = txCommit(tid, 2, &iplist[0], 0);
402 up(&JFS_IP(dip)->commit_sem);
403 up(&JFS_IP(ip)->commit_sem);
406 * Truncating the directory index table is not guaranteed. It
407 * may need to be done iteratively
409 if (test_cflag(COMMIT_Stale, dip)) {
411 jfs_truncate_nolock(dip, 0);
413 clear_cflag(COMMIT_Stale, dip);
417 free_UCSname(&dname);
420 jfs_info("jfs_rmdir: rc:%d", rc);
425 * NAME: jfs_unlink(dip, dentry)
427 * FUNCTION: remove a link to object <vp> named by <name>
428 * from parent directory <dvp>
430 * PARAMETER: dip - inode of parent directory
431 * dentry - dentry of object to be removed
433 * RETURN: errors from subroutines
436 * temporary file: if one or more processes have the file open
437 * when the last link is removed, the link will be removed before
438 * unlink() returns, but the removal of the file contents will be
439 * postponed until all references to the files are closed.
441 * JFS does NOT support unlink() on directories.
444 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
447 tid_t tid; /* transaction id */
448 struct inode *ip = dentry->d_inode;
450 struct component_name dname; /* object name */
451 struct inode *iplist[2];
456 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
458 if ((rc = get_UCSname(&dname, dentry)))
463 tid = txBegin(dip->i_sb, 0);
465 down(&JFS_IP(dip)->commit_sem);
466 down(&JFS_IP(ip)->commit_sem);
472 * delete the entry of target file from parent directory
475 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
476 jfs_err("jfs_unlink: dtDelete returned %d", rc);
478 txAbort(tid, 1); /* Marks FS Dirty */
480 up(&JFS_IP(dip)->commit_sem);
481 up(&JFS_IP(ip)->commit_sem);
488 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
489 mark_inode_dirty(dip);
491 /* update target's inode */
493 mark_inode_dirty(ip);
496 * commit zero link count object
498 if (ip->i_nlink == 0) {
499 assert(!test_cflag(COMMIT_Nolink, ip));
500 /* free block resources */
501 if ((new_size = commitZeroLink(tid, ip)) < 0) {
502 txAbort(tid, 1); /* Marks FS Dirty */
504 up(&JFS_IP(dip)->commit_sem);
505 up(&JFS_IP(ip)->commit_sem);
510 tblk = tid_to_tblock(tid);
511 tblk->xflag |= COMMIT_DELETE;
516 * Incomplete truncate of file data can
517 * result in timing problems unless we synchronously commit the
521 commit_flag = COMMIT_SYNC;
526 * If xtTruncate was incomplete, commit synchronously to avoid
527 * timing complications
529 rc = txCommit(tid, 2, &iplist[0], commit_flag);
533 up(&JFS_IP(dip)->commit_sem);
534 up(&JFS_IP(ip)->commit_sem);
537 while (new_size && (rc == 0)) {
538 tid = txBegin(dip->i_sb, 0);
539 down(&JFS_IP(ip)->commit_sem);
540 new_size = xtTruncate_pmap(tid, ip, new_size);
542 txAbort(tid, 1); /* Marks FS Dirty */
545 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
547 up(&JFS_IP(ip)->commit_sem);
550 if (ip->i_nlink == 0)
551 set_cflag(COMMIT_Nolink, ip);
556 * Truncating the directory index table is not guaranteed. It
557 * may need to be done iteratively
559 if (test_cflag(COMMIT_Stale, dip)) {
561 jfs_truncate_nolock(dip, 0);
563 clear_cflag(COMMIT_Stale, dip);
567 free_UCSname(&dname);
569 jfs_info("jfs_unlink: rc:%d", rc);
574 * NAME: commitZeroLink()
576 * FUNCTION: for non-directory, called by jfs_remove(),
577 * truncate a regular file, directory or symbolic
578 * link to zero length. return 0 if type is not
581 * if the file is currently associated with a VM segment
582 * only permanent disk and inode map resources are freed,
583 * and neither the inode nor indirect blocks are modified
584 * so that the resources can be later freed in the work
586 * if there is no VM segment on entry, the resources are
587 * freed in both work and permanent map.
588 * (? for temporary file - memory object is cached even
589 * after no reference:
590 * reference count > 0 - )
592 * PARAMETERS: cd - pointer to commit data structure.
593 * current inode is the one to truncate.
595 * RETURN: Errors from subroutines
597 static s64 commitZeroLink(tid_t tid, struct inode *ip)
602 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
604 filetype = ip->i_mode & S_IFMT;
609 /* fast symbolic link */
610 if (ip->i_size < IDATASIZE) {
616 assert(filetype != S_IFDIR);
620 set_cflag(COMMIT_Freewmap, ip);
622 /* mark transaction of block map update type */
623 tblk = tid_to_tblock(tid);
624 tblk->xflag |= COMMIT_PMAP;
629 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
630 /* acquire maplock on EA to be freed from block map */
631 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
636 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
637 /* acquire maplock on EA to be freed from block map */
638 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
641 * free xtree/data (truncate to zero length):
642 * free xtree/data pages from cache if COMMIT_PWMAP,
643 * free xtree/data blocks from persistent block map, and
644 * free xtree/data blocks from working block map if COMMIT_PWMAP;
647 return xtTruncate_pmap(tid, ip, 0);
654 * NAME: freeZeroLink()
656 * FUNCTION: for non-directory, called by iClose(),
657 * free resources of a file from cache and WORKING map
658 * for a file previously committed with zero link count
659 * while associated with a pager object,
661 * PARAMETER: ip - pointer to inode of file.
665 int freeZeroLink(struct inode *ip)
670 jfs_info("freeZeroLink: ip = 0x%p", ip);
672 /* return if not reg or symbolic link or if size is
675 type = ip->i_mode & S_IFMT;
681 /* if its contained in inode nothing to do */
682 if (ip->i_size < IDATASIZE)
692 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
693 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
694 int xlen = lengthDXD(&JFS_IP(ip)->ea);
695 struct maplock maplock; /* maplock for COMMIT_WMAP */
696 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
698 /* free EA pages from cache */
699 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
701 /* free EA extent from working block map */
703 pxdlock = (struct pxd_lock *) & maplock;
704 pxdlock->flag = mlckFREEPXD;
705 PXDaddress(&pxdlock->pxd, xaddr);
706 PXDlength(&pxdlock->pxd, xlen);
707 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
713 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
714 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
715 int xlen = lengthDXD(&JFS_IP(ip)->acl);
716 struct maplock maplock; /* maplock for COMMIT_WMAP */
717 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
719 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
721 /* free ACL extent from working block map */
723 pxdlock = (struct pxd_lock *) & maplock;
724 pxdlock->flag = mlckFREEPXD;
725 PXDaddress(&pxdlock->pxd, xaddr);
726 PXDlength(&pxdlock->pxd, xlen);
727 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
731 * free xtree/data (truncate to zero length):
732 * free xtree/data pages from cache, and
733 * free xtree/data blocks from working block map;
736 rc = xtTruncate(0, ip, 0, COMMIT_WMAP);
742 * NAME: jfs_link(vp, dvp, name, crp)
744 * FUNCTION: create a link to <vp> by the name = <name>
745 * in the parent directory <dvp>
747 * PARAMETER: vp - target object
748 * dvp - parent directory of new link
749 * name - name of new link to target object
752 * RETURN: Errors from subroutines
755 * JFS does NOT support link() on directories (to prevent circular
756 * path in the directory hierarchy);
757 * EPERM: the target object is a directory, and either the caller
758 * does not have appropriate privileges or the implementation prohibits
759 * using link() on directories [XPG4.2].
761 * JFS does NOT support links between file systems:
762 * EXDEV: target object and new link are on different file systems and
763 * implementation does not support links between file systems [XPG4.2].
765 static int jfs_link(struct dentry *old_dentry,
766 struct inode *dir, struct dentry *dentry)
770 struct inode *ip = old_dentry->d_inode;
772 struct component_name dname;
773 struct btstack btstack;
774 struct inode *iplist[2];
776 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
777 dentry->d_name.name);
779 if (ip->i_nlink == JFS_LINK_MAX)
782 if (ip->i_nlink == 0)
785 tid = txBegin(ip->i_sb, 0);
787 down(&JFS_IP(dir)->commit_sem);
788 down(&JFS_IP(ip)->commit_sem);
791 * scan parent directory for entry/freespace
793 if ((rc = get_UCSname(&dname, dentry)))
796 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
800 * create entry for new link in parent directory
803 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
806 /* update object inode */
807 ip->i_nlink++; /* for new link */
808 ip->i_ctime = CURRENT_TIME;
809 mark_inode_dirty(dir);
810 atomic_inc(&ip->i_count);
814 rc = txCommit(tid, 2, &iplist[0], 0);
817 d_instantiate(dentry, ip);
820 free_UCSname(&dname);
825 up(&JFS_IP(dir)->commit_sem);
826 up(&JFS_IP(ip)->commit_sem);
828 jfs_info("jfs_link: rc:%d", rc);
833 * NAME: jfs_symlink(dip, dentry, name)
835 * FUNCTION: creates a symbolic link to <symlink> by name <name>
838 * PARAMETER: dip - parent directory vnode
839 * dentry - dentry of symbolic link
840 * name - the path name of the existing object
841 * that will be the source of the link
843 * RETURN: errors from subroutines
846 * ENAMETOOLONG: pathname resolution of a symbolic link produced
847 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
850 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
856 struct component_name dname;
857 int ssize; /* source pathname size */
858 struct btstack btstack;
859 struct inode *ip = dentry->d_inode;
860 unchar *i_fastsymlink;
862 int bmask = 0, xsize;
863 s64 extent = 0, xaddr;
865 struct super_block *sb;
868 struct inode *iplist[2];
870 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
872 ssize = strlen(name) + 1;
875 * search parent directory for entry/freespace
876 * (dtSearch() returns parent directory page pinned)
879 if ((rc = get_UCSname(&dname, dentry)))
883 * allocate on-disk/in-memory inode for symbolic link:
884 * (iAlloc() returns new, locked inode)
886 ip = ialloc(dip, S_IFLNK | 0777);
892 tid = txBegin(dip->i_sb, 0);
894 down(&JFS_IP(dip)->commit_sem);
895 down(&JFS_IP(ip)->commit_sem);
897 tblk = tid_to_tblock(tid);
898 tblk->xflag |= COMMIT_CREATE;
899 tblk->ino = ip->i_ino;
900 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
902 /* fix symlink access permission
903 * (dir_create() ANDs in the u.u_cmask,
904 * but symlinks really need to be 777 access)
909 * write symbolic link target path name
914 * write source path name inline in on-disk inode (fast symbolic link)
917 if (ssize <= IDATASIZE) {
918 ip->i_op = &jfs_symlink_inode_operations;
920 i_fastsymlink = JFS_IP(ip)->i_inline;
921 memcpy(i_fastsymlink, name, ssize);
922 ip->i_size = ssize - 1;
925 * if symlink is > 128 bytes, we don't have the space to
926 * store inline extended attributes
928 if (ssize > sizeof (JFS_IP(ip)->i_inline))
929 JFS_IP(ip)->mode2 &= ~INLINEEA;
931 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
935 * write source path name in a single extent
938 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
940 ip->i_op = &page_symlink_inode_operations;
941 ip->i_mapping->a_ops = &jfs_aops;
944 * even though the data of symlink object (source
945 * path name) is treated as non-journaled user data,
946 * it is read/written thru buffer cache for performance.
949 bmask = JFS_SBI(sb)->bsize - 1;
950 xsize = (ssize + bmask) & ~bmask;
952 xlen = xsize >> JFS_SBI(sb)->l2bsize;
953 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
959 ip->i_size = ssize - 1;
961 /* This is kind of silly since PATH_MAX == 4K */
962 int copy_size = min(ssize, PSIZE);
964 mp = get_metapage(ip, xaddr, PSIZE, 1);
967 dbFree(ip, extent, xlen);
972 memcpy(mp->data, name, copy_size);
976 xaddr += JFS_SBI(sb)->nbperpage;
978 ip->i_blocks = LBLK2PBLK(sb, xlen);
982 * create entry for symbolic link in parent directory
984 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
987 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
991 dbFree(ip, extent, xlen);
993 /* discard new inode */
997 insert_inode_hash(ip);
998 mark_inode_dirty(ip);
1001 * commit update of parent directory and link object
1006 rc = txCommit(tid, 2, &iplist[0], 0);
1010 up(&JFS_IP(dip)->commit_sem);
1011 up(&JFS_IP(ip)->commit_sem);
1016 d_instantiate(dentry, ip);
1019 free_UCSname(&dname);
1021 #ifdef CONFIG_JFS_POSIX_ACL
1023 jfs_init_acl(ip, dip);
1027 jfs_info("jfs_symlink: rc:%d", rc);
1035 * FUNCTION: rename a file or directory
1037 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1038 struct inode *new_dir, struct dentry *new_dentry)
1040 struct btstack btstack;
1042 struct component_name new_dname;
1043 struct inode *new_ip;
1044 struct component_name old_dname;
1045 struct inode *old_ip;
1049 struct dt_lock *dtlck;
1052 struct inode *iplist[4];
1053 struct tblock *tblk;
1058 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1059 new_dentry->d_name.name);
1061 old_ip = old_dentry->d_inode;
1062 new_ip = new_dentry->d_inode;
1064 if ((rc = get_UCSname(&old_dname, old_dentry)))
1067 if ((rc = get_UCSname(&new_dname, new_dentry)))
1071 * Make sure source inode number is what we think it is
1073 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1074 if (rc || (ino != old_ip->i_ino)) {
1080 * Make sure dest inode number (if any) is what we think it is
1082 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1084 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1088 } else if (rc != -ENOENT)
1091 /* no entry exists, but one was expected */
1096 if (S_ISDIR(old_ip->i_mode)) {
1098 if (!dtEmpty(new_ip)) {
1102 } else if ((new_dir != old_dir) &&
1103 (new_dir->i_nlink == JFS_LINK_MAX)) {
1108 IWRITE_LOCK(new_ip);
1111 * The real work starts here
1113 tid = txBegin(new_dir->i_sb, 0);
1115 down(&JFS_IP(new_dir)->commit_sem);
1116 down(&JFS_IP(old_ip)->commit_sem);
1117 if (old_dir != new_dir)
1118 down(&JFS_IP(old_dir)->commit_sem);
1121 down(&JFS_IP(new_ip)->commit_sem);
1123 * Change existing directory entry to new inode number
1125 ino = new_ip->i_ino;
1126 rc = dtModify(tid, new_dir, &new_dname, &ino,
1127 old_ip->i_ino, JFS_RENAME);
1131 if (S_ISDIR(new_ip->i_mode)) {
1133 if (new_ip->i_nlink) {
1134 up(&JFS_IP(new_dir)->commit_sem);
1135 up(&JFS_IP(old_ip)->commit_sem);
1136 if (old_dir != new_dir)
1137 up(&JFS_IP(old_dir)->commit_sem);
1138 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1139 IWRITE_UNLOCK(new_ip);
1140 jfs_error(new_ip->i_sb,
1141 "jfs_rename: new_ip->i_nlink != 0");
1144 tblk = tid_to_tblock(tid);
1145 tblk->xflag |= COMMIT_DELETE;
1146 tblk->u.ip = new_ip;
1147 } else if (new_ip->i_nlink == 0) {
1148 assert(!test_cflag(COMMIT_Nolink, new_ip));
1149 /* free block resources */
1150 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1151 txAbort(tid, 1); /* Marks FS Dirty */
1155 tblk = tid_to_tblock(tid);
1156 tblk->xflag |= COMMIT_DELETE;
1157 tblk->u.ip = new_ip;
1159 new_ip->i_ctime = CURRENT_TIME;
1160 mark_inode_dirty(new_ip);
1164 * Add new directory entry
1166 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1169 jfs_err("jfs_rename didn't expect dtSearch to fail "
1174 ino = old_ip->i_ino;
1175 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1177 jfs_err("jfs_rename: dtInsert failed w/rc = %d",
1181 if (S_ISDIR(old_ip->i_mode))
1185 * Remove old directory entry
1188 ino = old_ip->i_ino;
1189 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1191 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1193 txAbort(tid, 1); /* Marks Filesystem dirty */
1196 if (S_ISDIR(old_ip->i_mode)) {
1198 if (old_dir != new_dir) {
1200 * Change inode number of parent for moved directory
1203 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1204 cpu_to_le32(new_dir->i_ino);
1206 /* Linelock header of dtree */
1207 tlck = txLock(tid, old_ip,
1208 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1209 tlckDTREE | tlckBTROOT | tlckRELINK);
1210 dtlck = (struct dt_lock *) & tlck->lock;
1211 ASSERT(dtlck->index == 0);
1212 lv = & dtlck->lv[0];
1220 * Update ctime on changed/moved inodes & mark dirty
1222 old_ip->i_ctime = CURRENT_TIME;
1223 mark_inode_dirty(old_ip);
1225 new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME;
1226 mark_inode_dirty(new_dir);
1228 /* Build list of inodes modified by this transaction */
1230 iplist[ipcount++] = old_ip;
1232 iplist[ipcount++] = new_ip;
1233 iplist[ipcount++] = old_dir;
1235 if (old_dir != new_dir) {
1236 iplist[ipcount++] = new_dir;
1237 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1238 mark_inode_dirty(old_dir);
1242 * Incomplete truncate of file data can
1243 * result in timing problems unless we synchronously commit the
1247 commit_flag = COMMIT_SYNC;
1251 rc = txCommit(tid, ipcount, iplist, commit_flag);
1256 up(&JFS_IP(new_dir)->commit_sem);
1257 up(&JFS_IP(old_ip)->commit_sem);
1258 if (old_dir != new_dir)
1259 up(&JFS_IP(old_dir)->commit_sem);
1261 up(&JFS_IP(new_ip)->commit_sem);
1263 while (new_size && (rc == 0)) {
1264 tid = txBegin(new_ip->i_sb, 0);
1265 down(&JFS_IP(new_ip)->commit_sem);
1266 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1271 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1273 up(&JFS_IP(new_ip)->commit_sem);
1275 if (new_ip && (new_ip->i_nlink == 0))
1276 set_cflag(COMMIT_Nolink, new_ip);
1278 free_UCSname(&new_dname);
1280 free_UCSname(&old_dname);
1282 if (new_ip && !S_ISDIR(new_ip->i_mode))
1283 IWRITE_UNLOCK(new_ip);
1285 * Truncating the directory index table is not guaranteed. It
1286 * may need to be done iteratively
1288 if (test_cflag(COMMIT_Stale, old_dir)) {
1289 if (old_dir->i_size > 1)
1290 jfs_truncate_nolock(old_dir, 0);
1292 clear_cflag(COMMIT_Stale, old_dir);
1295 jfs_info("jfs_rename: returning %d", rc);
1303 * FUNCTION: Create a special file (device)
1305 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1306 int mode, dev_t rdev)
1308 struct jfs_inode_info *jfs_ip;
1309 struct btstack btstack;
1310 struct component_name dname;
1313 struct inode *iplist[2];
1316 struct tblock *tblk;
1318 if (!new_valid_dev(rdev))
1321 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1323 if ((rc = get_UCSname(&dname, dentry)))
1326 ip = ialloc(dir, mode);
1331 jfs_ip = JFS_IP(ip);
1333 tid = txBegin(dir->i_sb, 0);
1335 down(&JFS_IP(dir)->commit_sem);
1336 down(&JFS_IP(ip)->commit_sem);
1338 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
1341 tblk = tid_to_tblock(tid);
1342 tblk->xflag |= COMMIT_CREATE;
1343 tblk->ino = ip->i_ino;
1344 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1347 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
1350 ip->i_op = &jfs_file_inode_operations;
1351 jfs_ip->dev = new_encode_dev(rdev);
1352 init_special_inode(ip, ip->i_mode, rdev);
1354 insert_inode_hash(ip);
1355 mark_inode_dirty(ip);
1357 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1359 mark_inode_dirty(dir);
1363 rc = txCommit(tid, 2, iplist, 0);
1367 up(&JFS_IP(ip)->commit_sem);
1368 up(&JFS_IP(dir)->commit_sem);
1373 d_instantiate(dentry, ip);
1376 free_UCSname(&dname);
1378 #ifdef CONFIG_JFS_POSIX_ACL
1380 jfs_init_acl(ip, dir);
1384 jfs_info("jfs_mknod: returning %d", rc);
1388 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1390 struct btstack btstack;
1393 struct component_name key;
1394 const char *name = dentry->d_name.name;
1395 int len = dentry->d_name.len;
1398 jfs_info("jfs_lookup: name = %s", name);
1401 if ((name[0] == '.') && (len == 1))
1403 else if (strcmp(name, "..") == 0)
1406 if ((rc = get_UCSname(&key, dentry)))
1408 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1410 if (rc == -ENOENT) {
1411 d_add(dentry, NULL);
1414 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1419 ip = iget(dip->i_sb, inum);
1420 if (ip == NULL || is_bad_inode(ip)) {
1421 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1424 return ERR_PTR(-EACCES);
1427 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1428 dentry->d_op = &jfs_ci_dentry_operations;
1430 dentry = d_splice_alias(ip, dentry);
1432 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1433 dentry->d_op = &jfs_ci_dentry_operations;
1438 struct dentry *jfs_get_parent(struct dentry *dentry)
1440 struct super_block *sb = dentry->d_inode->i_sb;
1441 struct dentry *parent = ERR_PTR(-ENOENT);
1442 struct inode *inode;
1443 unsigned long parent_ino;
1446 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1447 inode = iget(sb, parent_ino);
1449 if (is_bad_inode(inode)) {
1451 parent = ERR_PTR(-EACCES);
1453 parent = d_alloc_anon(inode);
1455 parent = ERR_PTR(-ENOMEM);
1464 struct inode_operations jfs_dir_inode_operations = {
1465 .create = jfs_create,
1466 .lookup = jfs_lookup,
1468 .unlink = jfs_unlink,
1469 .symlink = jfs_symlink,
1473 .rename = jfs_rename,
1474 .setxattr = jfs_setxattr,
1475 .getxattr = jfs_getxattr,
1476 .listxattr = jfs_listxattr,
1477 .removexattr = jfs_removexattr,
1478 #ifdef CONFIG_JFS_POSIX_ACL
1479 .setattr = jfs_setattr,
1480 .permission = jfs_permission,
1484 struct file_operations jfs_dir_operations = {
1485 .read = generic_read_dir,
1486 .readdir = jfs_readdir,
1490 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1495 hash = init_name_hash();
1496 for (i=0; i < this->len; i++)
1497 hash = partial_name_hash(tolower(this->name[i]), hash);
1498 this->hash = end_name_hash(hash);
1503 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1507 if (a->len != b->len)
1509 for (i=0; i < a->len; i++) {
1510 if (tolower(a->name[i]) != tolower(b->name[i]))
1516 * We want creates to preserve case. A negative dentry, a, that
1517 * has a different case than b may cause a new entry to be created
1518 * with the wrong case. Since we can't tell if a comes from a negative
1519 * dentry, we blindly replace it with b. This should be harmless if
1520 * a is not a negative dentry.
1522 memcpy((unsigned char *)a->name, b->name, a->len);
1527 struct dentry_operations jfs_ci_dentry_operations =
1529 .d_hash = jfs_ci_hash,
1530 .d_compare = jfs_ci_compare,