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: jfs_create(dip, dentry, mode)
44 * FUNCTION: create a regular file in the parent directory <dip>
45 * with name = <from dentry> and mode = <mode>
47 * PARAMETER: dip - parent directory vnode
48 * dentry - dentry of new file
49 * mode - create mode (rwxrwxrwx).
52 * RETURN: Errors from subroutines
55 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
59 tid_t tid; /* transaction id */
60 struct inode *ip = NULL; /* child directory inode */
62 struct component_name dname; /* child directory name */
63 struct btstack btstack;
64 struct inode *iplist[2];
67 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
70 * search parent directory for entry/freespace
71 * (dtSearch() returns parent directory page pinned)
73 if ((rc = get_UCSname(&dname, dentry)))
77 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
78 * block there while holding dtree page, so we allocate the inode &
79 * begin the transaction before we search the directory.
81 ip = ialloc(dip, mode);
87 tid = txBegin(dip->i_sb, 0);
89 down(&JFS_IP(dip)->commit_sem);
90 down(&JFS_IP(ip)->commit_sem);
92 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
93 jfs_err("jfs_create: dtSearch returned %d", rc);
97 tblk = tid_to_tblock(tid);
98 tblk->xflag |= COMMIT_CREATE;
99 tblk->ino = ip->i_ino;
100 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
106 * initialize the child XAD tree root in-line in inode
111 * create entry in parent directory for child directory
112 * (dtInsert() releases parent directory page)
115 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
117 jfs_err("jfs_create: dtInsert returned -EIO");
118 txAbort(tid, 1); /* Marks Filesystem dirty */
120 txAbort(tid, 0); /* Filesystem full */
124 ip->i_op = &jfs_file_inode_operations;
125 ip->i_fop = &jfs_file_operations;
126 ip->i_mapping->a_ops = &jfs_aops;
128 insert_inode_hash(ip);
129 mark_inode_dirty(ip);
131 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
133 mark_inode_dirty(dip);
135 rc = txCommit(tid, 2, &iplist[0], 0);
139 up(&JFS_IP(dip)->commit_sem);
140 up(&JFS_IP(ip)->commit_sem);
145 d_instantiate(dentry, ip);
148 free_UCSname(&dname);
150 #ifdef CONFIG_JFS_POSIX_ACL
152 jfs_init_acl(ip, dip);
157 jfs_info("jfs_create: rc:%d", rc);
163 * NAME: jfs_mkdir(dip, dentry, mode)
165 * FUNCTION: create a child directory in the parent directory <dip>
166 * with name = <from dentry> and mode = <mode>
168 * PARAMETER: dip - parent directory vnode
169 * dentry - dentry of child directory
170 * mode - create mode (rwxrwxrwx).
172 * RETURN: Errors from subroutines
175 * EACCESS: user needs search+write permission on the parent directory
177 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
180 tid_t tid; /* transaction id */
181 struct inode *ip = NULL; /* child directory inode */
183 struct component_name dname; /* child directory name */
184 struct btstack btstack;
185 struct inode *iplist[2];
188 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
190 /* link count overflow on parent directory ? */
191 if (dip->i_nlink == JFS_LINK_MAX) {
197 * search parent directory for entry/freespace
198 * (dtSearch() returns parent directory page pinned)
200 if ((rc = get_UCSname(&dname, dentry)))
204 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
205 * block there while holding dtree page, so we allocate the inode &
206 * begin the transaction before we search the directory.
208 ip = ialloc(dip, S_IFDIR | mode);
214 tid = txBegin(dip->i_sb, 0);
216 down(&JFS_IP(dip)->commit_sem);
217 down(&JFS_IP(ip)->commit_sem);
219 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
220 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
224 tblk = tid_to_tblock(tid);
225 tblk->xflag |= COMMIT_CREATE;
226 tblk->ino = ip->i_ino;
227 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
233 * initialize the child directory in-line in inode
235 dtInitRoot(tid, ip, dip->i_ino);
238 * create entry in parent directory for child directory
239 * (dtInsert() releases parent directory page)
242 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
244 jfs_err("jfs_mkdir: dtInsert returned -EIO");
245 txAbort(tid, 1); /* Marks Filesystem dirty */
247 txAbort(tid, 0); /* Filesystem full */
251 ip->i_nlink = 2; /* for '.' */
252 ip->i_op = &jfs_dir_inode_operations;
253 ip->i_fop = &jfs_dir_operations;
255 insert_inode_hash(ip);
256 mark_inode_dirty(ip);
258 /* update parent directory inode */
259 dip->i_nlink++; /* for '..' from child directory */
260 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
261 mark_inode_dirty(dip);
263 rc = txCommit(tid, 2, &iplist[0], 0);
267 up(&JFS_IP(dip)->commit_sem);
268 up(&JFS_IP(ip)->commit_sem);
273 d_instantiate(dentry, ip);
276 free_UCSname(&dname);
278 #ifdef CONFIG_JFS_POSIX_ACL
280 jfs_init_acl(ip, dip);
285 jfs_info("jfs_mkdir: rc:%d", rc);
290 * NAME: jfs_rmdir(dip, dentry)
292 * FUNCTION: remove a link to child directory
294 * PARAMETER: dip - parent inode
295 * dentry - child directory dentry
297 * RETURN: -EINVAL - if name is . or ..
298 * -EINVAL - if . or .. exist but are invalid.
299 * errors from subroutines
302 * if other threads have the directory open when the last link
303 * is removed, the "." and ".." entries, if present, are removed before
304 * rmdir() returns and no new entries may be created in the directory,
305 * but the directory is not removed until the last reference to
306 * the directory is released (cf.unlink() of regular file).
308 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
311 tid_t tid; /* transaction id */
312 struct inode *ip = dentry->d_inode;
314 struct component_name dname;
315 struct inode *iplist[2];
318 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
320 /* Init inode for quota operations. */
323 /* directory must be empty to be removed */
329 if ((rc = get_UCSname(&dname, dentry))) {
333 tid = txBegin(dip->i_sb, 0);
335 down(&JFS_IP(dip)->commit_sem);
336 down(&JFS_IP(ip)->commit_sem);
341 tblk = tid_to_tblock(tid);
342 tblk->xflag |= COMMIT_DELETE;
346 * delete the entry of target directory from parent directory
349 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
350 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
354 up(&JFS_IP(dip)->commit_sem);
355 up(&JFS_IP(ip)->commit_sem);
360 /* update parent directory's link count corresponding
361 * to ".." entry of the target directory deleted
364 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
365 mark_inode_dirty(dip);
368 * OS/2 could have created EA and/or ACL
370 /* free EA from both persistent and working map */
371 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
373 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
375 JFS_IP(ip)->ea.flag = 0;
377 /* free ACL from both persistent and working map */
378 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
380 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
382 JFS_IP(ip)->acl.flag = 0;
384 /* mark the target directory as deleted */
386 mark_inode_dirty(ip);
388 rc = txCommit(tid, 2, &iplist[0], 0);
392 up(&JFS_IP(dip)->commit_sem);
393 up(&JFS_IP(ip)->commit_sem);
396 * Truncating the directory index table is not guaranteed. It
397 * may need to be done iteratively
399 if (test_cflag(COMMIT_Stale, dip)) {
401 jfs_truncate_nolock(dip, 0);
403 clear_cflag(COMMIT_Stale, dip);
407 free_UCSname(&dname);
410 jfs_info("jfs_rmdir: rc:%d", rc);
415 * NAME: jfs_unlink(dip, dentry)
417 * FUNCTION: remove a link to object <vp> named by <name>
418 * from parent directory <dvp>
420 * PARAMETER: dip - inode of parent directory
421 * dentry - dentry of object to be removed
423 * RETURN: errors from subroutines
426 * temporary file: if one or more processes have the file open
427 * when the last link is removed, the link will be removed before
428 * unlink() returns, but the removal of the file contents will be
429 * postponed until all references to the files are closed.
431 * JFS does NOT support unlink() on directories.
434 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
437 tid_t tid; /* transaction id */
438 struct inode *ip = dentry->d_inode;
440 struct component_name dname; /* object name */
441 struct inode *iplist[2];
446 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
448 /* Init inode for quota operations. */
451 if ((rc = get_UCSname(&dname, dentry)))
456 tid = txBegin(dip->i_sb, 0);
458 down(&JFS_IP(dip)->commit_sem);
459 down(&JFS_IP(ip)->commit_sem);
465 * delete the entry of target file from parent directory
468 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
469 jfs_err("jfs_unlink: dtDelete returned %d", rc);
471 txAbort(tid, 1); /* Marks FS Dirty */
473 up(&JFS_IP(dip)->commit_sem);
474 up(&JFS_IP(ip)->commit_sem);
481 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
482 mark_inode_dirty(dip);
484 /* update target's inode */
486 mark_inode_dirty(ip);
489 * commit zero link count object
491 if (ip->i_nlink == 0) {
492 assert(!test_cflag(COMMIT_Nolink, ip));
493 /* free block resources */
494 if ((new_size = commitZeroLink(tid, ip)) < 0) {
495 txAbort(tid, 1); /* Marks FS Dirty */
497 up(&JFS_IP(dip)->commit_sem);
498 up(&JFS_IP(ip)->commit_sem);
503 tblk = tid_to_tblock(tid);
504 tblk->xflag |= COMMIT_DELETE;
509 * Incomplete truncate of file data can
510 * result in timing problems unless we synchronously commit the
514 commit_flag = COMMIT_SYNC;
519 * If xtTruncate was incomplete, commit synchronously to avoid
520 * timing complications
522 rc = txCommit(tid, 2, &iplist[0], commit_flag);
526 up(&JFS_IP(dip)->commit_sem);
527 up(&JFS_IP(ip)->commit_sem);
530 while (new_size && (rc == 0)) {
531 tid = txBegin(dip->i_sb, 0);
532 down(&JFS_IP(ip)->commit_sem);
533 new_size = xtTruncate_pmap(tid, ip, new_size);
535 txAbort(tid, 1); /* Marks FS Dirty */
538 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
540 up(&JFS_IP(ip)->commit_sem);
543 if (ip->i_nlink == 0)
544 set_cflag(COMMIT_Nolink, ip);
549 * Truncating the directory index table is not guaranteed. It
550 * may need to be done iteratively
552 if (test_cflag(COMMIT_Stale, dip)) {
554 jfs_truncate_nolock(dip, 0);
556 clear_cflag(COMMIT_Stale, dip);
560 free_UCSname(&dname);
562 jfs_info("jfs_unlink: rc:%d", rc);
567 * NAME: commitZeroLink()
569 * FUNCTION: for non-directory, called by jfs_remove(),
570 * truncate a regular file, directory or symbolic
571 * link to zero length. return 0 if type is not
574 * if the file is currently associated with a VM segment
575 * only permanent disk and inode map resources are freed,
576 * and neither the inode nor indirect blocks are modified
577 * so that the resources can be later freed in the work
579 * if there is no VM segment on entry, the resources are
580 * freed in both work and permanent map.
581 * (? for temporary file - memory object is cached even
582 * after no reference:
583 * reference count > 0 - )
585 * PARAMETERS: cd - pointer to commit data structure.
586 * current inode is the one to truncate.
588 * RETURN: Errors from subroutines
590 static s64 commitZeroLink(tid_t tid, struct inode *ip)
595 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
597 filetype = ip->i_mode & S_IFMT;
602 /* fast symbolic link */
603 if (ip->i_size < IDATASIZE) {
609 assert(filetype != S_IFDIR);
613 set_cflag(COMMIT_Freewmap, ip);
615 /* mark transaction of block map update type */
616 tblk = tid_to_tblock(tid);
617 tblk->xflag |= COMMIT_PMAP;
622 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
623 /* acquire maplock on EA to be freed from block map */
624 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
629 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
630 /* acquire maplock on EA to be freed from block map */
631 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
634 * free xtree/data (truncate to zero length):
635 * free xtree/data pages from cache if COMMIT_PWMAP,
636 * free xtree/data blocks from persistent block map, and
637 * free xtree/data blocks from working block map if COMMIT_PWMAP;
640 return xtTruncate_pmap(tid, ip, 0);
647 * NAME: jfs_free_zero_link()
649 * FUNCTION: for non-directory, called by iClose(),
650 * free resources of a file from cache and WORKING map
651 * for a file previously committed with zero link count
652 * while associated with a pager object,
654 * PARAMETER: ip - pointer to inode of file.
656 void jfs_free_zero_link(struct inode *ip)
660 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
662 /* return if not reg or symbolic link or if size is
665 type = ip->i_mode & S_IFMT;
671 /* if its contained in inode nothing to do */
672 if (ip->i_size < IDATASIZE)
682 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
683 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
684 int xlen = lengthDXD(&JFS_IP(ip)->ea);
685 struct maplock maplock; /* maplock for COMMIT_WMAP */
686 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
688 /* free EA pages from cache */
689 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
691 /* free EA extent from working block map */
693 pxdlock = (struct pxd_lock *) & maplock;
694 pxdlock->flag = mlckFREEPXD;
695 PXDaddress(&pxdlock->pxd, xaddr);
696 PXDlength(&pxdlock->pxd, xlen);
697 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
703 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
704 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
705 int xlen = lengthDXD(&JFS_IP(ip)->acl);
706 struct maplock maplock; /* maplock for COMMIT_WMAP */
707 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
709 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
711 /* free ACL extent from working block map */
713 pxdlock = (struct pxd_lock *) & maplock;
714 pxdlock->flag = mlckFREEPXD;
715 PXDaddress(&pxdlock->pxd, xaddr);
716 PXDlength(&pxdlock->pxd, xlen);
717 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
721 * free xtree/data (truncate to zero length):
722 * free xtree/data pages from cache, and
723 * free xtree/data blocks from working block map;
726 xtTruncate(0, ip, 0, COMMIT_WMAP);
730 * NAME: jfs_link(vp, dvp, name, crp)
732 * FUNCTION: create a link to <vp> by the name = <name>
733 * in the parent directory <dvp>
735 * PARAMETER: vp - target object
736 * dvp - parent directory of new link
737 * name - name of new link to target object
740 * RETURN: Errors from subroutines
743 * JFS does NOT support link() on directories (to prevent circular
744 * path in the directory hierarchy);
745 * EPERM: the target object is a directory, and either the caller
746 * does not have appropriate privileges or the implementation prohibits
747 * using link() on directories [XPG4.2].
749 * JFS does NOT support links between file systems:
750 * EXDEV: target object and new link are on different file systems and
751 * implementation does not support links between file systems [XPG4.2].
753 static int jfs_link(struct dentry *old_dentry,
754 struct inode *dir, struct dentry *dentry)
758 struct inode *ip = old_dentry->d_inode;
760 struct component_name dname;
761 struct btstack btstack;
762 struct inode *iplist[2];
764 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
765 dentry->d_name.name);
767 if (ip->i_nlink == JFS_LINK_MAX)
770 if (ip->i_nlink == 0)
773 tid = txBegin(ip->i_sb, 0);
775 down(&JFS_IP(dir)->commit_sem);
776 down(&JFS_IP(ip)->commit_sem);
779 * scan parent directory for entry/freespace
781 if ((rc = get_UCSname(&dname, dentry)))
784 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
788 * create entry for new link in parent directory
791 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
794 /* update object inode */
795 ip->i_nlink++; /* for new link */
796 ip->i_ctime = CURRENT_TIME;
797 mark_inode_dirty(dir);
798 atomic_inc(&ip->i_count);
802 rc = txCommit(tid, 2, &iplist[0], 0);
808 d_instantiate(dentry, ip);
811 free_UCSname(&dname);
816 up(&JFS_IP(dir)->commit_sem);
817 up(&JFS_IP(ip)->commit_sem);
819 jfs_info("jfs_link: rc:%d", rc);
824 * NAME: jfs_symlink(dip, dentry, name)
826 * FUNCTION: creates a symbolic link to <symlink> by name <name>
829 * PARAMETER: dip - parent directory vnode
830 * dentry - dentry of symbolic link
831 * name - the path name of the existing object
832 * that will be the source of the link
834 * RETURN: errors from subroutines
837 * ENAMETOOLONG: pathname resolution of a symbolic link produced
838 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
841 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
847 struct component_name dname;
848 int ssize; /* source pathname size */
849 struct btstack btstack;
850 struct inode *ip = dentry->d_inode;
851 unchar *i_fastsymlink;
853 int bmask = 0, xsize;
854 s64 extent = 0, xaddr;
856 struct super_block *sb;
859 struct inode *iplist[2];
861 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
863 ssize = strlen(name) + 1;
866 * search parent directory for entry/freespace
867 * (dtSearch() returns parent directory page pinned)
870 if ((rc = get_UCSname(&dname, dentry)))
874 * allocate on-disk/in-memory inode for symbolic link:
875 * (iAlloc() returns new, locked inode)
877 ip = ialloc(dip, S_IFLNK | 0777);
883 tid = txBegin(dip->i_sb, 0);
885 down(&JFS_IP(dip)->commit_sem);
886 down(&JFS_IP(ip)->commit_sem);
888 tblk = tid_to_tblock(tid);
889 tblk->xflag |= COMMIT_CREATE;
890 tblk->ino = ip->i_ino;
891 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
893 /* fix symlink access permission
894 * (dir_create() ANDs in the u.u_cmask,
895 * but symlinks really need to be 777 access)
900 * write symbolic link target path name
905 * write source path name inline in on-disk inode (fast symbolic link)
908 if (ssize <= IDATASIZE) {
909 ip->i_op = &jfs_symlink_inode_operations;
911 i_fastsymlink = JFS_IP(ip)->i_inline;
912 memcpy(i_fastsymlink, name, ssize);
913 ip->i_size = ssize - 1;
916 * if symlink is > 128 bytes, we don't have the space to
917 * store inline extended attributes
919 if (ssize > sizeof (JFS_IP(ip)->i_inline))
920 JFS_IP(ip)->mode2 &= ~INLINEEA;
922 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
926 * write source path name in a single extent
929 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
931 ip->i_op = &page_symlink_inode_operations;
932 ip->i_mapping->a_ops = &jfs_aops;
935 * even though the data of symlink object (source
936 * path name) is treated as non-journaled user data,
937 * it is read/written thru buffer cache for performance.
940 bmask = JFS_SBI(sb)->bsize - 1;
941 xsize = (ssize + bmask) & ~bmask;
943 xlen = xsize >> JFS_SBI(sb)->l2bsize;
944 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
950 ip->i_size = ssize - 1;
952 /* This is kind of silly since PATH_MAX == 4K */
953 int copy_size = min(ssize, PSIZE);
955 mp = get_metapage(ip, xaddr, PSIZE, 1);
958 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
963 memcpy(mp->data, name, copy_size);
967 xaddr += JFS_SBI(sb)->nbperpage;
972 * create entry for symbolic link in parent directory
974 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
977 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
981 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
983 /* discard new inode */
987 insert_inode_hash(ip);
988 mark_inode_dirty(ip);
991 * commit update of parent directory and link object
996 rc = txCommit(tid, 2, &iplist[0], 0);
1000 up(&JFS_IP(dip)->commit_sem);
1001 up(&JFS_IP(ip)->commit_sem);
1006 d_instantiate(dentry, ip);
1009 free_UCSname(&dname);
1011 #ifdef CONFIG_JFS_POSIX_ACL
1013 jfs_init_acl(ip, dip);
1017 jfs_info("jfs_symlink: rc:%d", rc);
1025 * FUNCTION: rename a file or directory
1027 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1028 struct inode *new_dir, struct dentry *new_dentry)
1030 struct btstack btstack;
1032 struct component_name new_dname;
1033 struct inode *new_ip;
1034 struct component_name old_dname;
1035 struct inode *old_ip;
1039 struct dt_lock *dtlck;
1042 struct inode *iplist[4];
1043 struct tblock *tblk;
1048 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1049 new_dentry->d_name.name);
1051 old_ip = old_dentry->d_inode;
1052 new_ip = new_dentry->d_inode;
1054 if ((rc = get_UCSname(&old_dname, old_dentry)))
1057 if ((rc = get_UCSname(&new_dname, new_dentry)))
1061 * Make sure source inode number is what we think it is
1063 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1064 if (rc || (ino != old_ip->i_ino)) {
1070 * Make sure dest inode number (if any) is what we think it is
1072 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1074 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1078 } else if (rc != -ENOENT)
1081 /* no entry exists, but one was expected */
1086 if (S_ISDIR(old_ip->i_mode)) {
1088 if (!dtEmpty(new_ip)) {
1092 } else if ((new_dir != old_dir) &&
1093 (new_dir->i_nlink == JFS_LINK_MAX)) {
1097 } else if (new_ip) {
1098 IWRITE_LOCK(new_ip);
1099 /* Init inode for quota operations. */
1104 * The real work starts here
1106 tid = txBegin(new_dir->i_sb, 0);
1108 down(&JFS_IP(new_dir)->commit_sem);
1109 down(&JFS_IP(old_ip)->commit_sem);
1110 if (old_dir != new_dir)
1111 down(&JFS_IP(old_dir)->commit_sem);
1114 down(&JFS_IP(new_ip)->commit_sem);
1116 * Change existing directory entry to new inode number
1118 ino = new_ip->i_ino;
1119 rc = dtModify(tid, new_dir, &new_dname, &ino,
1120 old_ip->i_ino, JFS_RENAME);
1124 if (S_ISDIR(new_ip->i_mode)) {
1126 if (new_ip->i_nlink) {
1127 up(&JFS_IP(new_dir)->commit_sem);
1128 up(&JFS_IP(old_ip)->commit_sem);
1129 if (old_dir != new_dir)
1130 up(&JFS_IP(old_dir)->commit_sem);
1131 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1132 IWRITE_UNLOCK(new_ip);
1133 jfs_error(new_ip->i_sb,
1134 "jfs_rename: new_ip->i_nlink != 0");
1137 tblk = tid_to_tblock(tid);
1138 tblk->xflag |= COMMIT_DELETE;
1139 tblk->u.ip = new_ip;
1140 } else if (new_ip->i_nlink == 0) {
1141 assert(!test_cflag(COMMIT_Nolink, new_ip));
1142 /* free block resources */
1143 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1144 txAbort(tid, 1); /* Marks FS Dirty */
1148 tblk = tid_to_tblock(tid);
1149 tblk->xflag |= COMMIT_DELETE;
1150 tblk->u.ip = new_ip;
1152 new_ip->i_ctime = CURRENT_TIME;
1153 mark_inode_dirty(new_ip);
1157 * Add new directory entry
1159 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1162 jfs_err("jfs_rename didn't expect dtSearch to fail "
1167 ino = old_ip->i_ino;
1168 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1171 jfs_err("jfs_rename: dtInsert returned -EIO");
1174 if (S_ISDIR(old_ip->i_mode))
1178 * Remove old directory entry
1181 ino = old_ip->i_ino;
1182 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1184 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1186 txAbort(tid, 1); /* Marks Filesystem dirty */
1189 if (S_ISDIR(old_ip->i_mode)) {
1191 if (old_dir != new_dir) {
1193 * Change inode number of parent for moved directory
1196 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1197 cpu_to_le32(new_dir->i_ino);
1199 /* Linelock header of dtree */
1200 tlck = txLock(tid, old_ip,
1201 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1202 tlckDTREE | tlckBTROOT | tlckRELINK);
1203 dtlck = (struct dt_lock *) & tlck->lock;
1204 ASSERT(dtlck->index == 0);
1205 lv = & dtlck->lv[0];
1213 * Update ctime on changed/moved inodes & mark dirty
1215 old_ip->i_ctime = CURRENT_TIME;
1216 mark_inode_dirty(old_ip);
1218 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1219 mark_inode_dirty(new_dir);
1221 /* Build list of inodes modified by this transaction */
1223 iplist[ipcount++] = old_ip;
1225 iplist[ipcount++] = new_ip;
1226 iplist[ipcount++] = old_dir;
1228 if (old_dir != new_dir) {
1229 iplist[ipcount++] = new_dir;
1230 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1231 mark_inode_dirty(old_dir);
1235 * Incomplete truncate of file data can
1236 * result in timing problems unless we synchronously commit the
1240 commit_flag = COMMIT_SYNC;
1244 rc = txCommit(tid, ipcount, iplist, commit_flag);
1249 up(&JFS_IP(new_dir)->commit_sem);
1250 up(&JFS_IP(old_ip)->commit_sem);
1251 if (old_dir != new_dir)
1252 up(&JFS_IP(old_dir)->commit_sem);
1254 up(&JFS_IP(new_ip)->commit_sem);
1256 while (new_size && (rc == 0)) {
1257 tid = txBegin(new_ip->i_sb, 0);
1258 down(&JFS_IP(new_ip)->commit_sem);
1259 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1264 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1266 up(&JFS_IP(new_ip)->commit_sem);
1268 if (new_ip && (new_ip->i_nlink == 0))
1269 set_cflag(COMMIT_Nolink, new_ip);
1271 free_UCSname(&new_dname);
1273 free_UCSname(&old_dname);
1275 if (new_ip && !S_ISDIR(new_ip->i_mode))
1276 IWRITE_UNLOCK(new_ip);
1278 * Truncating the directory index table is not guaranteed. It
1279 * may need to be done iteratively
1281 if (test_cflag(COMMIT_Stale, old_dir)) {
1282 if (old_dir->i_size > 1)
1283 jfs_truncate_nolock(old_dir, 0);
1285 clear_cflag(COMMIT_Stale, old_dir);
1288 jfs_info("jfs_rename: returning %d", rc);
1296 * FUNCTION: Create a special file (device)
1298 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1299 int mode, dev_t rdev)
1301 struct jfs_inode_info *jfs_ip;
1302 struct btstack btstack;
1303 struct component_name dname;
1306 struct inode *iplist[2];
1309 struct tblock *tblk;
1311 if (!new_valid_dev(rdev))
1314 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1316 if ((rc = get_UCSname(&dname, dentry)))
1319 ip = ialloc(dir, mode);
1324 jfs_ip = JFS_IP(ip);
1326 tid = txBegin(dir->i_sb, 0);
1328 down(&JFS_IP(dir)->commit_sem);
1329 down(&JFS_IP(ip)->commit_sem);
1331 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
1334 tblk = tid_to_tblock(tid);
1335 tblk->xflag |= COMMIT_CREATE;
1336 tblk->ino = ip->i_ino;
1337 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1340 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
1343 ip->i_op = &jfs_file_inode_operations;
1344 jfs_ip->dev = new_encode_dev(rdev);
1345 init_special_inode(ip, ip->i_mode, rdev);
1347 insert_inode_hash(ip);
1348 mark_inode_dirty(ip);
1350 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1352 mark_inode_dirty(dir);
1356 rc = txCommit(tid, 2, iplist, 0);
1360 up(&JFS_IP(ip)->commit_sem);
1361 up(&JFS_IP(dir)->commit_sem);
1366 d_instantiate(dentry, ip);
1369 free_UCSname(&dname);
1371 #ifdef CONFIG_JFS_POSIX_ACL
1373 jfs_init_acl(ip, dir);
1377 jfs_info("jfs_mknod: returning %d", rc);
1381 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1383 struct btstack btstack;
1386 struct component_name key;
1387 const char *name = dentry->d_name.name;
1388 int len = dentry->d_name.len;
1391 jfs_info("jfs_lookup: name = %s", name);
1393 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1394 dentry->d_op = &jfs_ci_dentry_operations;
1396 if ((name[0] == '.') && (len == 1))
1398 else if (strcmp(name, "..") == 0)
1401 if ((rc = get_UCSname(&key, dentry)))
1403 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1405 if (rc == -ENOENT) {
1406 d_add(dentry, NULL);
1409 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1414 ip = iget(dip->i_sb, inum);
1415 if (ip == NULL || is_bad_inode(ip)) {
1416 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1419 return ERR_PTR(-EACCES);
1422 dentry = d_splice_alias(ip, dentry);
1424 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1425 dentry->d_op = &jfs_ci_dentry_operations;
1430 struct dentry *jfs_get_parent(struct dentry *dentry)
1432 struct super_block *sb = dentry->d_inode->i_sb;
1433 struct dentry *parent = ERR_PTR(-ENOENT);
1434 struct inode *inode;
1435 unsigned long parent_ino;
1438 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1439 inode = iget(sb, parent_ino);
1441 if (is_bad_inode(inode)) {
1443 parent = ERR_PTR(-EACCES);
1445 parent = d_alloc_anon(inode);
1447 parent = ERR_PTR(-ENOMEM);
1456 struct inode_operations jfs_dir_inode_operations = {
1457 .create = jfs_create,
1458 .lookup = jfs_lookup,
1460 .unlink = jfs_unlink,
1461 .symlink = jfs_symlink,
1465 .rename = jfs_rename,
1466 .setxattr = jfs_setxattr,
1467 .getxattr = jfs_getxattr,
1468 .listxattr = jfs_listxattr,
1469 .removexattr = jfs_removexattr,
1470 #ifdef CONFIG_JFS_POSIX_ACL
1471 .setattr = jfs_setattr,
1472 .permission = jfs_permission,
1476 struct file_operations jfs_dir_operations = {
1477 .read = generic_read_dir,
1478 .readdir = jfs_readdir,
1482 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1487 hash = init_name_hash();
1488 for (i=0; i < this->len; i++)
1489 hash = partial_name_hash(tolower(this->name[i]), hash);
1490 this->hash = end_name_hash(hash);
1495 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1499 if (a->len != b->len)
1501 for (i=0; i < a->len; i++) {
1502 if (tolower(a->name[i]) != tolower(b->name[i]))
1508 * We want creates to preserve case. A negative dentry, a, that
1509 * has a different case than b may cause a new entry to be created
1510 * with the wrong case. Since we can't tell if a comes from a negative
1511 * dentry, we blindly replace it with b. This should be harmless if
1512 * a is not a negative dentry.
1514 memcpy((unsigned char *)a->name, b->name, a->len);
1519 struct dentry_operations jfs_ci_dentry_operations =
1521 .d_hash = jfs_ci_hash,
1522 .d_compare = jfs_ci_compare,