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 <linux/quotaops.h>
23 #include "jfs_incore.h"
24 #include "jfs_superblock.h"
25 #include "jfs_inode.h"
26 #include "jfs_dinode.h"
28 #include "jfs_unicode.h"
29 #include "jfs_metapage.h"
30 #include "jfs_xattr.h"
32 #include "jfs_debug.h"
37 struct dentry_operations jfs_ci_dentry_operations;
39 static s64 commitZeroLink(tid_t, struct inode *);
42 * NAME: free_ea_wmap(inode)
44 * FUNCTION: free uncommitted extended attributes from working map
47 static inline void free_ea_wmap(struct inode *inode)
49 dxd_t *ea = &JFS_IP(inode)->ea;
51 if (ea->flag & DXD_EXTENT) {
52 /* free EA pages from cache */
53 invalidate_dxd_metapages(inode, *ea);
54 dbFree(inode, addressDXD(ea), lengthDXD(ea));
60 * NAME: jfs_create(dip, dentry, mode)
62 * FUNCTION: create a regular file in the parent directory <dip>
63 * with name = <from dentry> and mode = <mode>
65 * PARAMETER: dip - parent directory vnode
66 * dentry - dentry of new file
67 * mode - create mode (rwxrwxrwx).
70 * RETURN: Errors from subroutines
73 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
77 tid_t tid; /* transaction id */
78 struct inode *ip = NULL; /* child directory inode */
80 struct component_name dname; /* child directory name */
81 struct btstack btstack;
82 struct inode *iplist[2];
85 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
88 * search parent directory for entry/freespace
89 * (dtSearch() returns parent directory page pinned)
91 if ((rc = get_UCSname(&dname, dentry)))
95 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
96 * block there while holding dtree page, so we allocate the inode &
97 * begin the transaction before we search the directory.
99 ip = ialloc(dip, mode);
105 tid = txBegin(dip->i_sb, 0);
107 down(&JFS_IP(dip)->commit_sem);
108 down(&JFS_IP(ip)->commit_sem);
110 rc = jfs_init_acl(tid, ip, dip);
114 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
115 jfs_err("jfs_create: dtSearch returned %d", rc);
120 tblk = tid_to_tblock(tid);
121 tblk->xflag |= COMMIT_CREATE;
122 tblk->ino = ip->i_ino;
123 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
129 * initialize the child XAD tree root in-line in inode
134 * create entry in parent directory for child directory
135 * (dtInsert() releases parent directory page)
138 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
140 jfs_err("jfs_create: dtInsert returned -EIO");
141 txAbort(tid, 1); /* Marks Filesystem dirty */
143 txAbort(tid, 0); /* Filesystem full */
147 ip->i_op = &jfs_file_inode_operations;
148 ip->i_fop = &jfs_file_operations;
149 ip->i_mapping->a_ops = &jfs_aops;
151 insert_inode_hash(ip);
152 mark_inode_dirty(ip);
154 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
156 mark_inode_dirty(dip);
158 rc = txCommit(tid, 2, &iplist[0], 0);
162 up(&JFS_IP(dip)->commit_sem);
163 up(&JFS_IP(ip)->commit_sem);
169 d_instantiate(dentry, ip);
172 free_UCSname(&dname);
176 jfs_info("jfs_create: rc:%d", rc);
182 * NAME: jfs_mkdir(dip, dentry, mode)
184 * FUNCTION: create a child directory in the parent directory <dip>
185 * with name = <from dentry> and mode = <mode>
187 * PARAMETER: dip - parent directory vnode
188 * dentry - dentry of child directory
189 * mode - create mode (rwxrwxrwx).
191 * RETURN: Errors from subroutines
194 * EACCESS: user needs search+write permission on the parent directory
196 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
199 tid_t tid; /* transaction id */
200 struct inode *ip = NULL; /* child directory inode */
202 struct component_name dname; /* child directory name */
203 struct btstack btstack;
204 struct inode *iplist[2];
207 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
209 /* link count overflow on parent directory ? */
210 if (dip->i_nlink == JFS_LINK_MAX) {
216 * search parent directory for entry/freespace
217 * (dtSearch() returns parent directory page pinned)
219 if ((rc = get_UCSname(&dname, dentry)))
223 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
224 * block there while holding dtree page, so we allocate the inode &
225 * begin the transaction before we search the directory.
227 ip = ialloc(dip, S_IFDIR | mode);
233 tid = txBegin(dip->i_sb, 0);
235 down(&JFS_IP(dip)->commit_sem);
236 down(&JFS_IP(ip)->commit_sem);
238 rc = jfs_init_acl(tid, ip, dip);
242 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
243 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
248 tblk = tid_to_tblock(tid);
249 tblk->xflag |= COMMIT_CREATE;
250 tblk->ino = ip->i_ino;
251 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
257 * initialize the child directory in-line in inode
259 dtInitRoot(tid, ip, dip->i_ino);
262 * create entry in parent directory for child directory
263 * (dtInsert() releases parent directory page)
266 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
268 jfs_err("jfs_mkdir: dtInsert returned -EIO");
269 txAbort(tid, 1); /* Marks Filesystem dirty */
271 txAbort(tid, 0); /* Filesystem full */
275 ip->i_nlink = 2; /* for '.' */
276 ip->i_op = &jfs_dir_inode_operations;
277 ip->i_fop = &jfs_dir_operations;
279 insert_inode_hash(ip);
280 mark_inode_dirty(ip);
282 /* update parent directory inode */
283 dip->i_nlink++; /* for '..' from child directory */
284 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
285 mark_inode_dirty(dip);
287 rc = txCommit(tid, 2, &iplist[0], 0);
291 up(&JFS_IP(dip)->commit_sem);
292 up(&JFS_IP(ip)->commit_sem);
298 d_instantiate(dentry, ip);
301 free_UCSname(&dname);
306 jfs_info("jfs_mkdir: rc:%d", rc);
311 * NAME: jfs_rmdir(dip, dentry)
313 * FUNCTION: remove a link to child directory
315 * PARAMETER: dip - parent inode
316 * dentry - child directory dentry
318 * RETURN: -EINVAL - if name is . or ..
319 * -EINVAL - if . or .. exist but are invalid.
320 * errors from subroutines
323 * if other threads have the directory open when the last link
324 * is removed, the "." and ".." entries, if present, are removed before
325 * rmdir() returns and no new entries may be created in the directory,
326 * but the directory is not removed until the last reference to
327 * the directory is released (cf.unlink() of regular file).
329 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
332 tid_t tid; /* transaction id */
333 struct inode *ip = dentry->d_inode;
335 struct component_name dname;
336 struct inode *iplist[2];
339 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
341 /* Init inode for quota operations. */
344 /* directory must be empty to be removed */
350 if ((rc = get_UCSname(&dname, dentry))) {
354 tid = txBegin(dip->i_sb, 0);
356 down(&JFS_IP(dip)->commit_sem);
357 down(&JFS_IP(ip)->commit_sem);
362 tblk = tid_to_tblock(tid);
363 tblk->xflag |= COMMIT_DELETE;
367 * delete the entry of target directory from parent directory
370 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
371 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
375 up(&JFS_IP(dip)->commit_sem);
376 up(&JFS_IP(ip)->commit_sem);
381 /* update parent directory's link count corresponding
382 * to ".." entry of the target directory deleted
385 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
386 mark_inode_dirty(dip);
389 * OS/2 could have created EA and/or ACL
391 /* free EA from both persistent and working map */
392 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
394 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
396 JFS_IP(ip)->ea.flag = 0;
398 /* free ACL from both persistent and working map */
399 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
401 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
403 JFS_IP(ip)->acl.flag = 0;
405 /* mark the target directory as deleted */
407 mark_inode_dirty(ip);
409 rc = txCommit(tid, 2, &iplist[0], 0);
413 up(&JFS_IP(dip)->commit_sem);
414 up(&JFS_IP(ip)->commit_sem);
417 * Truncating the directory index table is not guaranteed. It
418 * may need to be done iteratively
420 if (test_cflag(COMMIT_Stale, dip)) {
422 jfs_truncate_nolock(dip, 0);
424 clear_cflag(COMMIT_Stale, dip);
428 free_UCSname(&dname);
431 jfs_info("jfs_rmdir: rc:%d", rc);
436 * NAME: jfs_unlink(dip, dentry)
438 * FUNCTION: remove a link to object <vp> named by <name>
439 * from parent directory <dvp>
441 * PARAMETER: dip - inode of parent directory
442 * dentry - dentry of object to be removed
444 * RETURN: errors from subroutines
447 * temporary file: if one or more processes have the file open
448 * when the last link is removed, the link will be removed before
449 * unlink() returns, but the removal of the file contents will be
450 * postponed until all references to the files are closed.
452 * JFS does NOT support unlink() on directories.
455 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
458 tid_t tid; /* transaction id */
459 struct inode *ip = dentry->d_inode;
461 struct component_name dname; /* object name */
462 struct inode *iplist[2];
467 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
469 /* Init inode for quota operations. */
472 if ((rc = get_UCSname(&dname, dentry)))
477 tid = txBegin(dip->i_sb, 0);
479 down(&JFS_IP(dip)->commit_sem);
480 down(&JFS_IP(ip)->commit_sem);
486 * delete the entry of target file from parent directory
489 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
490 jfs_err("jfs_unlink: dtDelete returned %d", rc);
492 txAbort(tid, 1); /* Marks FS Dirty */
494 up(&JFS_IP(dip)->commit_sem);
495 up(&JFS_IP(ip)->commit_sem);
502 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
503 mark_inode_dirty(dip);
505 /* update target's inode */
507 mark_inode_dirty(ip);
510 * commit zero link count object
512 if (ip->i_nlink == 0) {
513 assert(!test_cflag(COMMIT_Nolink, ip));
514 /* free block resources */
515 if ((new_size = commitZeroLink(tid, ip)) < 0) {
516 txAbort(tid, 1); /* Marks FS Dirty */
518 up(&JFS_IP(dip)->commit_sem);
519 up(&JFS_IP(ip)->commit_sem);
524 tblk = tid_to_tblock(tid);
525 tblk->xflag |= COMMIT_DELETE;
530 * Incomplete truncate of file data can
531 * result in timing problems unless we synchronously commit the
535 commit_flag = COMMIT_SYNC;
540 * If xtTruncate was incomplete, commit synchronously to avoid
541 * timing complications
543 rc = txCommit(tid, 2, &iplist[0], commit_flag);
547 up(&JFS_IP(dip)->commit_sem);
548 up(&JFS_IP(ip)->commit_sem);
551 while (new_size && (rc == 0)) {
552 tid = txBegin(dip->i_sb, 0);
553 down(&JFS_IP(ip)->commit_sem);
554 new_size = xtTruncate_pmap(tid, ip, new_size);
556 txAbort(tid, 1); /* Marks FS Dirty */
559 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
561 up(&JFS_IP(ip)->commit_sem);
564 if (ip->i_nlink == 0)
565 set_cflag(COMMIT_Nolink, ip);
570 * Truncating the directory index table is not guaranteed. It
571 * may need to be done iteratively
573 if (test_cflag(COMMIT_Stale, dip)) {
575 jfs_truncate_nolock(dip, 0);
577 clear_cflag(COMMIT_Stale, dip);
581 free_UCSname(&dname);
583 jfs_info("jfs_unlink: rc:%d", rc);
588 * NAME: commitZeroLink()
590 * FUNCTION: for non-directory, called by jfs_remove(),
591 * truncate a regular file, directory or symbolic
592 * link to zero length. return 0 if type is not
595 * if the file is currently associated with a VM segment
596 * only permanent disk and inode map resources are freed,
597 * and neither the inode nor indirect blocks are modified
598 * so that the resources can be later freed in the work
600 * if there is no VM segment on entry, the resources are
601 * freed in both work and permanent map.
602 * (? for temporary file - memory object is cached even
603 * after no reference:
604 * reference count > 0 - )
606 * PARAMETERS: cd - pointer to commit data structure.
607 * current inode is the one to truncate.
609 * RETURN: Errors from subroutines
611 static s64 commitZeroLink(tid_t tid, struct inode *ip)
616 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
618 filetype = ip->i_mode & S_IFMT;
623 /* fast symbolic link */
624 if (ip->i_size < IDATASIZE) {
630 assert(filetype != S_IFDIR);
634 set_cflag(COMMIT_Freewmap, ip);
636 /* mark transaction of block map update type */
637 tblk = tid_to_tblock(tid);
638 tblk->xflag |= COMMIT_PMAP;
643 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
644 /* acquire maplock on EA to be freed from block map */
645 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
650 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
651 /* acquire maplock on EA to be freed from block map */
652 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
655 * free xtree/data (truncate to zero length):
656 * free xtree/data pages from cache if COMMIT_PWMAP,
657 * free xtree/data blocks from persistent block map, and
658 * free xtree/data blocks from working block map if COMMIT_PWMAP;
661 return xtTruncate_pmap(tid, ip, 0);
668 * NAME: jfs_free_zero_link()
670 * FUNCTION: for non-directory, called by iClose(),
671 * free resources of a file from cache and WORKING map
672 * for a file previously committed with zero link count
673 * while associated with a pager object,
675 * PARAMETER: ip - pointer to inode of file.
677 void jfs_free_zero_link(struct inode *ip)
681 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
683 /* return if not reg or symbolic link or if size is
686 type = ip->i_mode & S_IFMT;
692 /* if its contained in inode nothing to do */
693 if (ip->i_size < IDATASIZE)
703 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
704 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
705 int xlen = lengthDXD(&JFS_IP(ip)->ea);
706 struct maplock maplock; /* maplock for COMMIT_WMAP */
707 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
709 /* free EA pages from cache */
710 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
712 /* free EA extent from working block map */
714 pxdlock = (struct pxd_lock *) & maplock;
715 pxdlock->flag = mlckFREEPXD;
716 PXDaddress(&pxdlock->pxd, xaddr);
717 PXDlength(&pxdlock->pxd, xlen);
718 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
724 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
725 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
726 int xlen = lengthDXD(&JFS_IP(ip)->acl);
727 struct maplock maplock; /* maplock for COMMIT_WMAP */
728 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
730 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
732 /* free ACL extent from working block map */
734 pxdlock = (struct pxd_lock *) & maplock;
735 pxdlock->flag = mlckFREEPXD;
736 PXDaddress(&pxdlock->pxd, xaddr);
737 PXDlength(&pxdlock->pxd, xlen);
738 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
742 * free xtree/data (truncate to zero length):
743 * free xtree/data pages from cache, and
744 * free xtree/data blocks from working block map;
747 xtTruncate(0, ip, 0, COMMIT_WMAP);
751 * NAME: jfs_link(vp, dvp, name, crp)
753 * FUNCTION: create a link to <vp> by the name = <name>
754 * in the parent directory <dvp>
756 * PARAMETER: vp - target object
757 * dvp - parent directory of new link
758 * name - name of new link to target object
761 * RETURN: Errors from subroutines
764 * JFS does NOT support link() on directories (to prevent circular
765 * path in the directory hierarchy);
766 * EPERM: the target object is a directory, and either the caller
767 * does not have appropriate privileges or the implementation prohibits
768 * using link() on directories [XPG4.2].
770 * JFS does NOT support links between file systems:
771 * EXDEV: target object and new link are on different file systems and
772 * implementation does not support links between file systems [XPG4.2].
774 static int jfs_link(struct dentry *old_dentry,
775 struct inode *dir, struct dentry *dentry)
779 struct inode *ip = old_dentry->d_inode;
781 struct component_name dname;
782 struct btstack btstack;
783 struct inode *iplist[2];
785 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
786 dentry->d_name.name);
788 if (ip->i_nlink == JFS_LINK_MAX)
791 if (ip->i_nlink == 0)
794 tid = txBegin(ip->i_sb, 0);
796 down(&JFS_IP(dir)->commit_sem);
797 down(&JFS_IP(ip)->commit_sem);
800 * scan parent directory for entry/freespace
802 if ((rc = get_UCSname(&dname, dentry)))
805 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
809 * create entry for new link in parent directory
812 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
815 /* update object inode */
816 ip->i_nlink++; /* for new link */
817 ip->i_ctime = CURRENT_TIME;
818 mark_inode_dirty(dir);
819 atomic_inc(&ip->i_count);
823 rc = txCommit(tid, 2, &iplist[0], 0);
829 d_instantiate(dentry, ip);
832 free_UCSname(&dname);
837 up(&JFS_IP(dir)->commit_sem);
838 up(&JFS_IP(ip)->commit_sem);
840 jfs_info("jfs_link: rc:%d", rc);
845 * NAME: jfs_symlink(dip, dentry, name)
847 * FUNCTION: creates a symbolic link to <symlink> by name <name>
850 * PARAMETER: dip - parent directory vnode
851 * dentry - dentry of symbolic link
852 * name - the path name of the existing object
853 * that will be the source of the link
855 * RETURN: errors from subroutines
858 * ENAMETOOLONG: pathname resolution of a symbolic link produced
859 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
862 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
868 struct component_name dname;
869 int ssize; /* source pathname size */
870 struct btstack btstack;
871 struct inode *ip = dentry->d_inode;
872 unchar *i_fastsymlink;
874 int bmask = 0, xsize;
875 s64 extent = 0, xaddr;
877 struct super_block *sb;
880 struct inode *iplist[2];
882 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
884 ssize = strlen(name) + 1;
887 * search parent directory for entry/freespace
888 * (dtSearch() returns parent directory page pinned)
891 if ((rc = get_UCSname(&dname, dentry)))
895 * allocate on-disk/in-memory inode for symbolic link:
896 * (iAlloc() returns new, locked inode)
898 ip = ialloc(dip, S_IFLNK | 0777);
904 tid = txBegin(dip->i_sb, 0);
906 down(&JFS_IP(dip)->commit_sem);
907 down(&JFS_IP(ip)->commit_sem);
909 tblk = tid_to_tblock(tid);
910 tblk->xflag |= COMMIT_CREATE;
911 tblk->ino = ip->i_ino;
912 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
914 /* fix symlink access permission
915 * (dir_create() ANDs in the u.u_cmask,
916 * but symlinks really need to be 777 access)
921 * write symbolic link target path name
926 * write source path name inline in on-disk inode (fast symbolic link)
929 if (ssize <= IDATASIZE) {
930 ip->i_op = &jfs_symlink_inode_operations;
932 i_fastsymlink = JFS_IP(ip)->i_inline;
933 memcpy(i_fastsymlink, name, ssize);
934 ip->i_size = ssize - 1;
937 * if symlink is > 128 bytes, we don't have the space to
938 * store inline extended attributes
940 if (ssize > sizeof (JFS_IP(ip)->i_inline))
941 JFS_IP(ip)->mode2 &= ~INLINEEA;
943 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
947 * write source path name in a single extent
950 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
952 ip->i_op = &page_symlink_inode_operations;
953 ip->i_mapping->a_ops = &jfs_aops;
956 * even though the data of symlink object (source
957 * path name) is treated as non-journaled user data,
958 * it is read/written thru buffer cache for performance.
961 bmask = JFS_SBI(sb)->bsize - 1;
962 xsize = (ssize + bmask) & ~bmask;
964 xlen = xsize >> JFS_SBI(sb)->l2bsize;
965 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
971 ip->i_size = ssize - 1;
973 /* This is kind of silly since PATH_MAX == 4K */
974 int copy_size = min(ssize, PSIZE);
976 mp = get_metapage(ip, xaddr, PSIZE, 1);
979 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
984 memcpy(mp->data, name, copy_size);
988 xaddr += JFS_SBI(sb)->nbperpage;
993 * create entry for symbolic link in parent directory
995 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
998 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1002 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1004 /* discard new inode */
1008 insert_inode_hash(ip);
1009 mark_inode_dirty(ip);
1012 * commit update of parent directory and link object
1017 rc = txCommit(tid, 2, &iplist[0], 0);
1021 up(&JFS_IP(dip)->commit_sem);
1022 up(&JFS_IP(ip)->commit_sem);
1028 d_instantiate(dentry, ip);
1031 free_UCSname(&dname);
1034 jfs_info("jfs_symlink: rc:%d", rc);
1042 * FUNCTION: rename a file or directory
1044 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1045 struct inode *new_dir, struct dentry *new_dentry)
1047 struct btstack btstack;
1049 struct component_name new_dname;
1050 struct inode *new_ip;
1051 struct component_name old_dname;
1052 struct inode *old_ip;
1056 struct dt_lock *dtlck;
1059 struct inode *iplist[4];
1060 struct tblock *tblk;
1065 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1066 new_dentry->d_name.name);
1068 old_ip = old_dentry->d_inode;
1069 new_ip = new_dentry->d_inode;
1071 if ((rc = get_UCSname(&old_dname, old_dentry)))
1074 if ((rc = get_UCSname(&new_dname, new_dentry)))
1078 * Make sure source inode number is what we think it is
1080 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1081 if (rc || (ino != old_ip->i_ino)) {
1087 * Make sure dest inode number (if any) is what we think it is
1089 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1091 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1095 } else if (rc != -ENOENT)
1098 /* no entry exists, but one was expected */
1103 if (S_ISDIR(old_ip->i_mode)) {
1105 if (!dtEmpty(new_ip)) {
1109 } else if ((new_dir != old_dir) &&
1110 (new_dir->i_nlink == JFS_LINK_MAX)) {
1114 } else if (new_ip) {
1115 IWRITE_LOCK(new_ip);
1116 /* Init inode for quota operations. */
1121 * The real work starts here
1123 tid = txBegin(new_dir->i_sb, 0);
1125 down(&JFS_IP(new_dir)->commit_sem);
1126 down(&JFS_IP(old_ip)->commit_sem);
1127 if (old_dir != new_dir)
1128 down(&JFS_IP(old_dir)->commit_sem);
1131 down(&JFS_IP(new_ip)->commit_sem);
1133 * Change existing directory entry to new inode number
1135 ino = new_ip->i_ino;
1136 rc = dtModify(tid, new_dir, &new_dname, &ino,
1137 old_ip->i_ino, JFS_RENAME);
1141 if (S_ISDIR(new_ip->i_mode)) {
1143 if (new_ip->i_nlink) {
1144 up(&JFS_IP(new_dir)->commit_sem);
1145 up(&JFS_IP(old_ip)->commit_sem);
1146 if (old_dir != new_dir)
1147 up(&JFS_IP(old_dir)->commit_sem);
1148 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1149 IWRITE_UNLOCK(new_ip);
1150 jfs_error(new_ip->i_sb,
1151 "jfs_rename: new_ip->i_nlink != 0");
1154 tblk = tid_to_tblock(tid);
1155 tblk->xflag |= COMMIT_DELETE;
1156 tblk->u.ip = new_ip;
1157 } else if (new_ip->i_nlink == 0) {
1158 assert(!test_cflag(COMMIT_Nolink, new_ip));
1159 /* free block resources */
1160 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1161 txAbort(tid, 1); /* Marks FS Dirty */
1165 tblk = tid_to_tblock(tid);
1166 tblk->xflag |= COMMIT_DELETE;
1167 tblk->u.ip = new_ip;
1169 new_ip->i_ctime = CURRENT_TIME;
1170 mark_inode_dirty(new_ip);
1174 * Add new directory entry
1176 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1179 jfs_err("jfs_rename didn't expect dtSearch to fail "
1184 ino = old_ip->i_ino;
1185 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1188 jfs_err("jfs_rename: dtInsert returned -EIO");
1191 if (S_ISDIR(old_ip->i_mode))
1195 * Remove old directory entry
1198 ino = old_ip->i_ino;
1199 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1201 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1203 txAbort(tid, 1); /* Marks Filesystem dirty */
1206 if (S_ISDIR(old_ip->i_mode)) {
1208 if (old_dir != new_dir) {
1210 * Change inode number of parent for moved directory
1213 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1214 cpu_to_le32(new_dir->i_ino);
1216 /* Linelock header of dtree */
1217 tlck = txLock(tid, old_ip,
1218 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1219 tlckDTREE | tlckBTROOT | tlckRELINK);
1220 dtlck = (struct dt_lock *) & tlck->lock;
1221 ASSERT(dtlck->index == 0);
1222 lv = & dtlck->lv[0];
1230 * Update ctime on changed/moved inodes & mark dirty
1232 old_ip->i_ctime = CURRENT_TIME;
1233 mark_inode_dirty(old_ip);
1235 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1236 mark_inode_dirty(new_dir);
1238 /* Build list of inodes modified by this transaction */
1240 iplist[ipcount++] = old_ip;
1242 iplist[ipcount++] = new_ip;
1243 iplist[ipcount++] = old_dir;
1245 if (old_dir != new_dir) {
1246 iplist[ipcount++] = new_dir;
1247 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1248 mark_inode_dirty(old_dir);
1252 * Incomplete truncate of file data can
1253 * result in timing problems unless we synchronously commit the
1257 commit_flag = COMMIT_SYNC;
1261 rc = txCommit(tid, ipcount, iplist, commit_flag);
1266 up(&JFS_IP(new_dir)->commit_sem);
1267 up(&JFS_IP(old_ip)->commit_sem);
1268 if (old_dir != new_dir)
1269 up(&JFS_IP(old_dir)->commit_sem);
1271 up(&JFS_IP(new_ip)->commit_sem);
1273 while (new_size && (rc == 0)) {
1274 tid = txBegin(new_ip->i_sb, 0);
1275 down(&JFS_IP(new_ip)->commit_sem);
1276 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1281 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1283 up(&JFS_IP(new_ip)->commit_sem);
1285 if (new_ip && (new_ip->i_nlink == 0))
1286 set_cflag(COMMIT_Nolink, new_ip);
1288 free_UCSname(&new_dname);
1290 free_UCSname(&old_dname);
1292 if (new_ip && !S_ISDIR(new_ip->i_mode))
1293 IWRITE_UNLOCK(new_ip);
1295 * Truncating the directory index table is not guaranteed. It
1296 * may need to be done iteratively
1298 if (test_cflag(COMMIT_Stale, old_dir)) {
1299 if (old_dir->i_size > 1)
1300 jfs_truncate_nolock(old_dir, 0);
1302 clear_cflag(COMMIT_Stale, old_dir);
1305 jfs_info("jfs_rename: returning %d", rc);
1313 * FUNCTION: Create a special file (device)
1315 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1316 int mode, dev_t rdev)
1318 struct jfs_inode_info *jfs_ip;
1319 struct btstack btstack;
1320 struct component_name dname;
1323 struct inode *iplist[2];
1326 struct tblock *tblk;
1328 if (!new_valid_dev(rdev))
1331 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1333 if ((rc = get_UCSname(&dname, dentry)))
1336 ip = ialloc(dir, mode);
1341 jfs_ip = JFS_IP(ip);
1343 tid = txBegin(dir->i_sb, 0);
1345 down(&JFS_IP(dir)->commit_sem);
1346 down(&JFS_IP(ip)->commit_sem);
1348 rc = jfs_init_acl(tid, ip, dir);
1352 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1357 tblk = tid_to_tblock(tid);
1358 tblk->xflag |= COMMIT_CREATE;
1359 tblk->ino = ip->i_ino;
1360 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1363 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1368 ip->i_op = &jfs_file_inode_operations;
1369 jfs_ip->dev = new_encode_dev(rdev);
1370 init_special_inode(ip, ip->i_mode, rdev);
1372 insert_inode_hash(ip);
1373 mark_inode_dirty(ip);
1375 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1377 mark_inode_dirty(dir);
1381 rc = txCommit(tid, 2, iplist, 0);
1385 up(&JFS_IP(ip)->commit_sem);
1386 up(&JFS_IP(dir)->commit_sem);
1392 d_instantiate(dentry, ip);
1395 free_UCSname(&dname);
1398 jfs_info("jfs_mknod: returning %d", rc);
1402 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1404 struct btstack btstack;
1407 struct component_name key;
1408 const char *name = dentry->d_name.name;
1409 int len = dentry->d_name.len;
1412 jfs_info("jfs_lookup: name = %s", name);
1414 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1415 dentry->d_op = &jfs_ci_dentry_operations;
1417 if ((name[0] == '.') && (len == 1))
1419 else if (strcmp(name, "..") == 0)
1422 if ((rc = get_UCSname(&key, dentry)))
1424 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1426 if (rc == -ENOENT) {
1427 d_add(dentry, NULL);
1430 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1435 ip = iget(dip->i_sb, inum);
1436 if (ip == NULL || is_bad_inode(ip)) {
1437 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1440 return ERR_PTR(-EACCES);
1443 dentry = d_splice_alias(ip, dentry);
1445 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1446 dentry->d_op = &jfs_ci_dentry_operations;
1451 struct dentry *jfs_get_parent(struct dentry *dentry)
1453 struct super_block *sb = dentry->d_inode->i_sb;
1454 struct dentry *parent = ERR_PTR(-ENOENT);
1455 struct inode *inode;
1456 unsigned long parent_ino;
1459 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1460 inode = iget(sb, parent_ino);
1462 if (is_bad_inode(inode)) {
1464 parent = ERR_PTR(-EACCES);
1466 parent = d_alloc_anon(inode);
1468 parent = ERR_PTR(-ENOMEM);
1477 struct inode_operations jfs_dir_inode_operations = {
1478 .create = jfs_create,
1479 .lookup = jfs_lookup,
1481 .unlink = jfs_unlink,
1482 .symlink = jfs_symlink,
1486 .rename = jfs_rename,
1487 .setxattr = jfs_setxattr,
1488 .getxattr = jfs_getxattr,
1489 .listxattr = jfs_listxattr,
1490 .removexattr = jfs_removexattr,
1491 #ifdef CONFIG_JFS_POSIX_ACL
1492 .setattr = jfs_setattr,
1493 .permission = jfs_permission,
1497 struct file_operations jfs_dir_operations = {
1498 .read = generic_read_dir,
1499 .readdir = jfs_readdir,
1503 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1508 hash = init_name_hash();
1509 for (i=0; i < this->len; i++)
1510 hash = partial_name_hash(tolower(this->name[i]), hash);
1511 this->hash = end_name_hash(hash);
1516 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1520 if (a->len != b->len)
1522 for (i=0; i < a->len; i++) {
1523 if (tolower(a->name[i]) != tolower(b->name[i]))
1529 * We want creates to preserve case. A negative dentry, a, that
1530 * has a different case than b may cause a new entry to be created
1531 * with the wrong case. Since we can't tell if a comes from a negative
1532 * dentry, we blindly replace it with b. This should be harmless if
1533 * a is not a negative dentry.
1535 memcpy((unsigned char *)a->name, b->name, a->len);
1540 struct dentry_operations jfs_ci_dentry_operations =
1542 .d_hash = jfs_ci_hash,
1543 .d_compare = jfs_ci_compare,