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/namei.h>
22 #include <linux/ctype.h>
23 #include <linux/quotaops.h>
24 #include <linux/exportfs.h>
25 #include "jfs_incore.h"
26 #include "jfs_superblock.h"
27 #include "jfs_inode.h"
28 #include "jfs_dinode.h"
30 #include "jfs_unicode.h"
31 #include "jfs_metapage.h"
32 #include "jfs_xattr.h"
34 #include "jfs_debug.h"
39 const struct dentry_operations jfs_ci_dentry_operations;
41 static s64 commitZeroLink(tid_t, struct inode *);
44 * NAME: free_ea_wmap(inode)
46 * FUNCTION: free uncommitted extended attributes from working map
49 static inline void free_ea_wmap(struct inode *inode)
51 dxd_t *ea = &JFS_IP(inode)->ea;
53 if (ea->flag & DXD_EXTENT) {
54 /* free EA pages from cache */
55 invalidate_dxd_metapages(inode, *ea);
56 dbFree(inode, addressDXD(ea), lengthDXD(ea));
62 * NAME: jfs_create(dip, dentry, mode)
64 * FUNCTION: create a regular file in the parent directory <dip>
65 * with name = <from dentry> and mode = <mode>
67 * PARAMETER: dip - parent directory vnode
68 * dentry - dentry of new file
69 * mode - create mode (rwxrwxrwx).
72 * RETURN: Errors from subroutines
75 static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode,
79 tid_t tid; /* transaction id */
80 struct inode *ip = NULL; /* child directory inode */
82 struct component_name dname; /* child directory name */
83 struct btstack btstack;
84 struct inode *iplist[2];
87 jfs_info("jfs_create: dip:0x%p name:%pd", dip, dentry);
89 rc = dquot_initialize(dip);
94 * search parent directory for entry/freespace
95 * (dtSearch() returns parent directory page pinned)
97 if ((rc = get_UCSname(&dname, dentry)))
101 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
102 * block there while holding dtree page, so we allocate the inode &
103 * begin the transaction before we search the directory.
105 ip = ialloc(dip, mode);
111 tid = txBegin(dip->i_sb, 0);
113 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
114 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
116 rc = jfs_init_acl(tid, ip, dip);
120 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
126 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
127 jfs_err("jfs_create: dtSearch returned %d", rc);
132 tblk = tid_to_tblock(tid);
133 tblk->xflag |= COMMIT_CREATE;
134 tblk->ino = ip->i_ino;
135 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
141 * initialize the child XAD tree root in-line in inode
146 * create entry in parent directory for child directory
147 * (dtInsert() releases parent directory page)
150 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
152 jfs_err("jfs_create: dtInsert returned -EIO");
153 txAbort(tid, 1); /* Marks Filesystem dirty */
155 txAbort(tid, 0); /* Filesystem full */
159 ip->i_op = &jfs_file_inode_operations;
160 ip->i_fop = &jfs_file_operations;
161 ip->i_mapping->a_ops = &jfs_aops;
163 mark_inode_dirty(ip);
165 dip->i_ctime = dip->i_mtime = current_time(dip);
167 mark_inode_dirty(dip);
169 rc = txCommit(tid, 2, &iplist[0], 0);
173 mutex_unlock(&JFS_IP(ip)->commit_mutex);
174 mutex_unlock(&JFS_IP(dip)->commit_mutex);
178 discard_new_inode(ip);
180 d_instantiate_new(dentry, ip);
184 free_UCSname(&dname);
188 jfs_info("jfs_create: rc:%d", rc);
194 * NAME: jfs_mkdir(dip, dentry, mode)
196 * FUNCTION: create a child directory in the parent directory <dip>
197 * with name = <from dentry> and mode = <mode>
199 * PARAMETER: dip - parent directory vnode
200 * dentry - dentry of child directory
201 * mode - create mode (rwxrwxrwx).
203 * RETURN: Errors from subroutines
206 * EACCESS: user needs search+write permission on the parent directory
208 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
211 tid_t tid; /* transaction id */
212 struct inode *ip = NULL; /* child directory inode */
214 struct component_name dname; /* child directory name */
215 struct btstack btstack;
216 struct inode *iplist[2];
219 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
221 rc = dquot_initialize(dip);
226 * search parent directory for entry/freespace
227 * (dtSearch() returns parent directory page pinned)
229 if ((rc = get_UCSname(&dname, dentry)))
233 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
234 * block there while holding dtree page, so we allocate the inode &
235 * begin the transaction before we search the directory.
237 ip = ialloc(dip, S_IFDIR | mode);
243 tid = txBegin(dip->i_sb, 0);
245 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
246 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
248 rc = jfs_init_acl(tid, ip, dip);
252 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
258 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
259 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
264 tblk = tid_to_tblock(tid);
265 tblk->xflag |= COMMIT_CREATE;
266 tblk->ino = ip->i_ino;
267 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
273 * initialize the child directory in-line in inode
275 dtInitRoot(tid, ip, dip->i_ino);
278 * create entry in parent directory for child directory
279 * (dtInsert() releases parent directory page)
282 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
284 jfs_err("jfs_mkdir: dtInsert returned -EIO");
285 txAbort(tid, 1); /* Marks Filesystem dirty */
287 txAbort(tid, 0); /* Filesystem full */
291 set_nlink(ip, 2); /* for '.' */
292 ip->i_op = &jfs_dir_inode_operations;
293 ip->i_fop = &jfs_dir_operations;
295 mark_inode_dirty(ip);
297 /* update parent directory inode */
298 inc_nlink(dip); /* for '..' from child directory */
299 dip->i_ctime = dip->i_mtime = current_time(dip);
300 mark_inode_dirty(dip);
302 rc = txCommit(tid, 2, &iplist[0], 0);
306 mutex_unlock(&JFS_IP(ip)->commit_mutex);
307 mutex_unlock(&JFS_IP(dip)->commit_mutex);
311 discard_new_inode(ip);
313 d_instantiate_new(dentry, ip);
317 free_UCSname(&dname);
322 jfs_info("jfs_mkdir: rc:%d", rc);
327 * NAME: jfs_rmdir(dip, dentry)
329 * FUNCTION: remove a link to child directory
331 * PARAMETER: dip - parent inode
332 * dentry - child directory dentry
334 * RETURN: -EINVAL - if name is . or ..
335 * -EINVAL - if . or .. exist but are invalid.
336 * errors from subroutines
339 * if other threads have the directory open when the last link
340 * is removed, the "." and ".." entries, if present, are removed before
341 * rmdir() returns and no new entries may be created in the directory,
342 * but the directory is not removed until the last reference to
343 * the directory is released (cf.unlink() of regular file).
345 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
348 tid_t tid; /* transaction id */
349 struct inode *ip = d_inode(dentry);
351 struct component_name dname;
352 struct inode *iplist[2];
355 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
357 /* Init inode for quota operations. */
358 rc = dquot_initialize(dip);
361 rc = dquot_initialize(ip);
365 /* directory must be empty to be removed */
371 if ((rc = get_UCSname(&dname, dentry))) {
375 tid = txBegin(dip->i_sb, 0);
377 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
378 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
383 tblk = tid_to_tblock(tid);
384 tblk->xflag |= COMMIT_DELETE;
388 * delete the entry of target directory from parent directory
391 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
392 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
396 mutex_unlock(&JFS_IP(ip)->commit_mutex);
397 mutex_unlock(&JFS_IP(dip)->commit_mutex);
402 /* update parent directory's link count corresponding
403 * to ".." entry of the target directory deleted
405 dip->i_ctime = dip->i_mtime = current_time(dip);
406 inode_dec_link_count(dip);
409 * OS/2 could have created EA and/or ACL
411 /* free EA from both persistent and working map */
412 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
414 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
416 JFS_IP(ip)->ea.flag = 0;
418 /* free ACL from both persistent and working map */
419 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
421 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
423 JFS_IP(ip)->acl.flag = 0;
425 /* mark the target directory as deleted */
427 mark_inode_dirty(ip);
429 rc = txCommit(tid, 2, &iplist[0], 0);
433 mutex_unlock(&JFS_IP(ip)->commit_mutex);
434 mutex_unlock(&JFS_IP(dip)->commit_mutex);
437 * Truncating the directory index table is not guaranteed. It
438 * may need to be done iteratively
440 if (test_cflag(COMMIT_Stale, dip)) {
442 jfs_truncate_nolock(dip, 0);
444 clear_cflag(COMMIT_Stale, dip);
448 free_UCSname(&dname);
451 jfs_info("jfs_rmdir: rc:%d", rc);
456 * NAME: jfs_unlink(dip, dentry)
458 * FUNCTION: remove a link to object <vp> named by <name>
459 * from parent directory <dvp>
461 * PARAMETER: dip - inode of parent directory
462 * dentry - dentry of object to be removed
464 * RETURN: errors from subroutines
467 * temporary file: if one or more processes have the file open
468 * when the last link is removed, the link will be removed before
469 * unlink() returns, but the removal of the file contents will be
470 * postponed until all references to the files are closed.
472 * JFS does NOT support unlink() on directories.
475 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
478 tid_t tid; /* transaction id */
479 struct inode *ip = d_inode(dentry);
481 struct component_name dname; /* object name */
482 struct inode *iplist[2];
487 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
489 /* Init inode for quota operations. */
490 rc = dquot_initialize(dip);
493 rc = dquot_initialize(ip);
497 if ((rc = get_UCSname(&dname, dentry)))
500 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
502 tid = txBegin(dip->i_sb, 0);
504 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
505 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
511 * delete the entry of target file from parent directory
514 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
515 jfs_err("jfs_unlink: dtDelete returned %d", rc);
517 txAbort(tid, 1); /* Marks FS Dirty */
519 mutex_unlock(&JFS_IP(ip)->commit_mutex);
520 mutex_unlock(&JFS_IP(dip)->commit_mutex);
527 ip->i_ctime = dip->i_ctime = dip->i_mtime = current_time(ip);
528 mark_inode_dirty(dip);
530 /* update target's inode */
531 inode_dec_link_count(ip);
534 * commit zero link count object
536 if (ip->i_nlink == 0) {
537 assert(!test_cflag(COMMIT_Nolink, ip));
538 /* free block resources */
539 if ((new_size = commitZeroLink(tid, ip)) < 0) {
540 txAbort(tid, 1); /* Marks FS Dirty */
542 mutex_unlock(&JFS_IP(ip)->commit_mutex);
543 mutex_unlock(&JFS_IP(dip)->commit_mutex);
548 tblk = tid_to_tblock(tid);
549 tblk->xflag |= COMMIT_DELETE;
554 * Incomplete truncate of file data can
555 * result in timing problems unless we synchronously commit the
559 commit_flag = COMMIT_SYNC;
564 * If xtTruncate was incomplete, commit synchronously to avoid
565 * timing complications
567 rc = txCommit(tid, 2, &iplist[0], commit_flag);
571 mutex_unlock(&JFS_IP(ip)->commit_mutex);
572 mutex_unlock(&JFS_IP(dip)->commit_mutex);
574 while (new_size && (rc == 0)) {
575 tid = txBegin(dip->i_sb, 0);
576 mutex_lock(&JFS_IP(ip)->commit_mutex);
577 new_size = xtTruncate_pmap(tid, ip, new_size);
579 txAbort(tid, 1); /* Marks FS Dirty */
582 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
584 mutex_unlock(&JFS_IP(ip)->commit_mutex);
587 if (ip->i_nlink == 0)
588 set_cflag(COMMIT_Nolink, ip);
593 * Truncating the directory index table is not guaranteed. It
594 * may need to be done iteratively
596 if (test_cflag(COMMIT_Stale, dip)) {
598 jfs_truncate_nolock(dip, 0);
600 clear_cflag(COMMIT_Stale, dip);
604 free_UCSname(&dname);
606 jfs_info("jfs_unlink: rc:%d", rc);
611 * NAME: commitZeroLink()
613 * FUNCTION: for non-directory, called by jfs_remove(),
614 * truncate a regular file, directory or symbolic
615 * link to zero length. return 0 if type is not
618 * if the file is currently associated with a VM segment
619 * only permanent disk and inode map resources are freed,
620 * and neither the inode nor indirect blocks are modified
621 * so that the resources can be later freed in the work
623 * if there is no VM segment on entry, the resources are
624 * freed in both work and permanent map.
625 * (? for temporary file - memory object is cached even
626 * after no reference:
627 * reference count > 0 - )
629 * PARAMETERS: cd - pointer to commit data structure.
630 * current inode is the one to truncate.
632 * RETURN: Errors from subroutines
634 static s64 commitZeroLink(tid_t tid, struct inode *ip)
639 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
641 filetype = ip->i_mode & S_IFMT;
646 /* fast symbolic link */
647 if (ip->i_size < IDATASIZE) {
653 assert(filetype != S_IFDIR);
657 set_cflag(COMMIT_Freewmap, ip);
659 /* mark transaction of block map update type */
660 tblk = tid_to_tblock(tid);
661 tblk->xflag |= COMMIT_PMAP;
666 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
667 /* acquire maplock on EA to be freed from block map */
668 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
673 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
674 /* acquire maplock on EA to be freed from block map */
675 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
678 * free xtree/data (truncate to zero length):
679 * free xtree/data pages from cache if COMMIT_PWMAP,
680 * free xtree/data blocks from persistent block map, and
681 * free xtree/data blocks from working block map if COMMIT_PWMAP;
684 return xtTruncate_pmap(tid, ip, 0);
691 * NAME: jfs_free_zero_link()
693 * FUNCTION: for non-directory, called by iClose(),
694 * free resources of a file from cache and WORKING map
695 * for a file previously committed with zero link count
696 * while associated with a pager object,
698 * PARAMETER: ip - pointer to inode of file.
700 void jfs_free_zero_link(struct inode *ip)
704 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
706 /* return if not reg or symbolic link or if size is
709 type = ip->i_mode & S_IFMT;
715 /* if its contained in inode nothing to do */
716 if (ip->i_size < IDATASIZE)
726 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
727 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
728 int xlen = lengthDXD(&JFS_IP(ip)->ea);
729 struct maplock maplock; /* maplock for COMMIT_WMAP */
730 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
732 /* free EA pages from cache */
733 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
735 /* free EA extent from working block map */
737 pxdlock = (struct pxd_lock *) & maplock;
738 pxdlock->flag = mlckFREEPXD;
739 PXDaddress(&pxdlock->pxd, xaddr);
740 PXDlength(&pxdlock->pxd, xlen);
741 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
747 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
748 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
749 int xlen = lengthDXD(&JFS_IP(ip)->acl);
750 struct maplock maplock; /* maplock for COMMIT_WMAP */
751 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
753 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
755 /* free ACL extent from working block map */
757 pxdlock = (struct pxd_lock *) & maplock;
758 pxdlock->flag = mlckFREEPXD;
759 PXDaddress(&pxdlock->pxd, xaddr);
760 PXDlength(&pxdlock->pxd, xlen);
761 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
765 * free xtree/data (truncate to zero length):
766 * free xtree/data pages from cache, and
767 * free xtree/data blocks from working block map;
770 xtTruncate(0, ip, 0, COMMIT_WMAP);
774 * NAME: jfs_link(vp, dvp, name, crp)
776 * FUNCTION: create a link to <vp> by the name = <name>
777 * in the parent directory <dvp>
779 * PARAMETER: vp - target object
780 * dvp - parent directory of new link
781 * name - name of new link to target object
784 * RETURN: Errors from subroutines
787 * JFS does NOT support link() on directories (to prevent circular
788 * path in the directory hierarchy);
789 * EPERM: the target object is a directory, and either the caller
790 * does not have appropriate privileges or the implementation prohibits
791 * using link() on directories [XPG4.2].
793 * JFS does NOT support links between file systems:
794 * EXDEV: target object and new link are on different file systems and
795 * implementation does not support links between file systems [XPG4.2].
797 static int jfs_link(struct dentry *old_dentry,
798 struct inode *dir, struct dentry *dentry)
802 struct inode *ip = d_inode(old_dentry);
804 struct component_name dname;
805 struct btstack btstack;
806 struct inode *iplist[2];
808 jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
810 rc = dquot_initialize(dir);
814 tid = txBegin(ip->i_sb, 0);
816 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
817 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
820 * scan parent directory for entry/freespace
822 if ((rc = get_UCSname(&dname, dentry)))
825 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
829 * create entry for new link in parent directory
832 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
835 /* update object inode */
836 inc_nlink(ip); /* for new link */
837 ip->i_ctime = current_time(ip);
838 dir->i_ctime = dir->i_mtime = current_time(dir);
839 mark_inode_dirty(dir);
844 rc = txCommit(tid, 2, &iplist[0], 0);
847 drop_nlink(ip); /* never instantiated */
850 d_instantiate(dentry, ip);
853 free_UCSname(&dname);
858 mutex_unlock(&JFS_IP(ip)->commit_mutex);
859 mutex_unlock(&JFS_IP(dir)->commit_mutex);
862 jfs_info("jfs_link: rc:%d", rc);
867 * NAME: jfs_symlink(dip, dentry, name)
869 * FUNCTION: creates a symbolic link to <symlink> by name <name>
872 * PARAMETER: dip - parent directory vnode
873 * dentry - dentry of symbolic link
874 * name - the path name of the existing object
875 * that will be the source of the link
877 * RETURN: errors from subroutines
880 * ENAMETOOLONG: pathname resolution of a symbolic link produced
881 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
884 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
890 struct component_name dname;
891 int ssize; /* source pathname size */
892 struct btstack btstack;
893 struct inode *ip = d_inode(dentry);
895 int bmask = 0, xsize;
898 struct super_block *sb;
901 struct inode *iplist[2];
903 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
905 rc = dquot_initialize(dip);
909 ssize = strlen(name) + 1;
912 * search parent directory for entry/freespace
913 * (dtSearch() returns parent directory page pinned)
916 if ((rc = get_UCSname(&dname, dentry)))
920 * allocate on-disk/in-memory inode for symbolic link:
921 * (iAlloc() returns new, locked inode)
923 ip = ialloc(dip, S_IFLNK | 0777);
929 tid = txBegin(dip->i_sb, 0);
931 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
932 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
934 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
938 tblk = tid_to_tblock(tid);
939 tblk->xflag |= COMMIT_CREATE;
940 tblk->ino = ip->i_ino;
941 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
943 /* fix symlink access permission
944 * (dir_create() ANDs in the u.u_cmask,
945 * but symlinks really need to be 777 access)
950 * write symbolic link target path name
955 * write source path name inline in on-disk inode (fast symbolic link)
958 if (ssize <= IDATASIZE) {
959 ip->i_op = &jfs_fast_symlink_inode_operations;
961 ip->i_link = JFS_IP(ip)->i_inline;
962 memcpy(ip->i_link, name, ssize);
963 ip->i_size = ssize - 1;
966 * if symlink is > 128 bytes, we don't have the space to
967 * store inline extended attributes
969 if (ssize > sizeof (JFS_IP(ip)->i_inline))
970 JFS_IP(ip)->mode2 &= ~INLINEEA;
972 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
976 * write source path name in a single extent
979 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
981 ip->i_op = &jfs_symlink_inode_operations;
983 ip->i_mapping->a_ops = &jfs_aops;
986 * even though the data of symlink object (source
987 * path name) is treated as non-journaled user data,
988 * it is read/written thru buffer cache for performance.
991 bmask = JFS_SBI(sb)->bsize - 1;
992 xsize = (ssize + bmask) & ~bmask;
994 xlen = xsize >> JFS_SBI(sb)->l2bsize;
995 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
999 ip->i_size = ssize - 1;
1001 /* This is kind of silly since PATH_MAX == 4K */
1002 int copy_size = min(ssize, PSIZE);
1004 mp = get_metapage(ip, xaddr, PSIZE, 1);
1007 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1012 memcpy(mp->data, name, copy_size);
1016 xaddr += JFS_SBI(sb)->nbperpage;
1021 * create entry for symbolic link in parent directory
1023 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1026 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1030 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1032 /* discard new inode */
1036 mark_inode_dirty(ip);
1038 dip->i_ctime = dip->i_mtime = current_time(dip);
1039 mark_inode_dirty(dip);
1041 * commit update of parent directory and link object
1046 rc = txCommit(tid, 2, &iplist[0], 0);
1050 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1051 mutex_unlock(&JFS_IP(dip)->commit_mutex);
1055 discard_new_inode(ip);
1057 d_instantiate_new(dentry, ip);
1061 free_UCSname(&dname);
1064 jfs_info("jfs_symlink: rc:%d", rc);
1072 * FUNCTION: rename a file or directory
1074 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1075 struct inode *new_dir, struct dentry *new_dentry,
1078 struct btstack btstack;
1080 struct component_name new_dname;
1081 struct inode *new_ip;
1082 struct component_name old_dname;
1083 struct inode *old_ip;
1087 struct dt_lock *dtlck;
1090 struct inode *iplist[4];
1091 struct tblock *tblk;
1095 if (flags & ~RENAME_NOREPLACE)
1098 jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
1100 rc = dquot_initialize(old_dir);
1103 rc = dquot_initialize(new_dir);
1107 old_ip = d_inode(old_dentry);
1108 new_ip = d_inode(new_dentry);
1110 if ((rc = get_UCSname(&old_dname, old_dentry)))
1113 if ((rc = get_UCSname(&new_dname, new_dentry)))
1117 * Make sure source inode number is what we think it is
1119 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1120 if (rc || (ino != old_ip->i_ino)) {
1126 * Make sure dest inode number (if any) is what we think it is
1128 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1130 if ((!new_ip) || (ino != new_ip->i_ino)) {
1134 } else if (rc != -ENOENT)
1137 /* no entry exists, but one was expected */
1142 if (S_ISDIR(old_ip->i_mode)) {
1144 if (!dtEmpty(new_ip)) {
1149 } else if (new_ip) {
1150 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
1151 /* Init inode for quota operations. */
1152 rc = dquot_initialize(new_ip);
1158 * The real work starts here
1160 tid = txBegin(new_dir->i_sb, 0);
1163 * How do we know the locking is safe from deadlocks?
1164 * The vfs does the hard part for us. Any time we are taking nested
1165 * commit_mutexes, the vfs already has i_mutex held on the parent.
1166 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1168 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1169 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1170 if (old_dir != new_dir)
1171 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1172 COMMIT_MUTEX_SECOND_PARENT);
1175 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1176 COMMIT_MUTEX_VICTIM);
1178 * Change existing directory entry to new inode number
1180 ino = new_ip->i_ino;
1181 rc = dtModify(tid, new_dir, &new_dname, &ino,
1182 old_ip->i_ino, JFS_RENAME);
1186 if (S_ISDIR(new_ip->i_mode)) {
1188 if (new_ip->i_nlink) {
1189 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1190 if (old_dir != new_dir)
1191 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1192 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1193 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1194 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1195 IWRITE_UNLOCK(new_ip);
1196 jfs_error(new_ip->i_sb,
1197 "new_ip->i_nlink != 0\n");
1200 tblk = tid_to_tblock(tid);
1201 tblk->xflag |= COMMIT_DELETE;
1202 tblk->u.ip = new_ip;
1203 } else if (new_ip->i_nlink == 0) {
1204 assert(!test_cflag(COMMIT_Nolink, new_ip));
1205 /* free block resources */
1206 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1207 txAbort(tid, 1); /* Marks FS Dirty */
1211 tblk = tid_to_tblock(tid);
1212 tblk->xflag |= COMMIT_DELETE;
1213 tblk->u.ip = new_ip;
1215 new_ip->i_ctime = current_time(new_ip);
1216 mark_inode_dirty(new_ip);
1220 * Add new directory entry
1222 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1225 jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1230 ino = old_ip->i_ino;
1231 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1234 jfs_err("jfs_rename: dtInsert returned -EIO");
1237 if (S_ISDIR(old_ip->i_mode))
1241 * Remove old directory entry
1244 ino = old_ip->i_ino;
1245 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1247 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1249 txAbort(tid, 1); /* Marks Filesystem dirty */
1252 if (S_ISDIR(old_ip->i_mode)) {
1253 drop_nlink(old_dir);
1254 if (old_dir != new_dir) {
1256 * Change inode number of parent for moved directory
1259 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1260 cpu_to_le32(new_dir->i_ino);
1262 /* Linelock header of dtree */
1263 tlck = txLock(tid, old_ip,
1264 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1265 tlckDTREE | tlckBTROOT | tlckRELINK);
1266 dtlck = (struct dt_lock *) & tlck->lock;
1267 ASSERT(dtlck->index == 0);
1268 lv = & dtlck->lv[0];
1276 * Update ctime on changed/moved inodes & mark dirty
1278 old_ip->i_ctime = current_time(old_ip);
1279 mark_inode_dirty(old_ip);
1281 new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
1282 mark_inode_dirty(new_dir);
1284 /* Build list of inodes modified by this transaction */
1286 iplist[ipcount++] = old_ip;
1288 iplist[ipcount++] = new_ip;
1289 iplist[ipcount++] = old_dir;
1291 if (old_dir != new_dir) {
1292 iplist[ipcount++] = new_dir;
1293 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1294 mark_inode_dirty(old_dir);
1298 * Incomplete truncate of file data can
1299 * result in timing problems unless we synchronously commit the
1303 commit_flag = COMMIT_SYNC;
1307 rc = txCommit(tid, ipcount, iplist, commit_flag);
1312 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1313 if (old_dir != new_dir)
1314 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1315 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1316 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1318 while (new_size && (rc == 0)) {
1319 tid = txBegin(new_ip->i_sb, 0);
1320 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1321 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1326 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1328 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1330 if (new_ip && (new_ip->i_nlink == 0))
1331 set_cflag(COMMIT_Nolink, new_ip);
1333 * Truncating the directory index table is not guaranteed. It
1334 * may need to be done iteratively
1336 if (test_cflag(COMMIT_Stale, old_dir)) {
1337 if (old_dir->i_size > 1)
1338 jfs_truncate_nolock(old_dir, 0);
1340 clear_cflag(COMMIT_Stale, old_dir);
1343 if (new_ip && !S_ISDIR(new_ip->i_mode))
1344 IWRITE_UNLOCK(new_ip);
1346 free_UCSname(&new_dname);
1348 free_UCSname(&old_dname);
1350 jfs_info("jfs_rename: returning %d", rc);
1358 * FUNCTION: Create a special file (device)
1360 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1361 umode_t mode, dev_t rdev)
1363 struct jfs_inode_info *jfs_ip;
1364 struct btstack btstack;
1365 struct component_name dname;
1368 struct inode *iplist[2];
1371 struct tblock *tblk;
1373 jfs_info("jfs_mknod: %pd", dentry);
1375 rc = dquot_initialize(dir);
1379 if ((rc = get_UCSname(&dname, dentry)))
1382 ip = ialloc(dir, mode);
1387 jfs_ip = JFS_IP(ip);
1389 tid = txBegin(dir->i_sb, 0);
1391 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1392 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1394 rc = jfs_init_acl(tid, ip, dir);
1398 rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
1404 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1409 tblk = tid_to_tblock(tid);
1410 tblk->xflag |= COMMIT_CREATE;
1411 tblk->ino = ip->i_ino;
1412 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1415 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1420 ip->i_op = &jfs_file_inode_operations;
1421 jfs_ip->dev = new_encode_dev(rdev);
1422 init_special_inode(ip, ip->i_mode, rdev);
1424 mark_inode_dirty(ip);
1426 dir->i_ctime = dir->i_mtime = current_time(dir);
1428 mark_inode_dirty(dir);
1432 rc = txCommit(tid, 2, iplist, 0);
1436 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1437 mutex_unlock(&JFS_IP(dir)->commit_mutex);
1441 discard_new_inode(ip);
1443 d_instantiate_new(dentry, ip);
1447 free_UCSname(&dname);
1450 jfs_info("jfs_mknod: returning %d", rc);
1454 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
1456 struct btstack btstack;
1459 struct component_name key;
1462 jfs_info("jfs_lookup: name = %pd", dentry);
1464 if ((rc = get_UCSname(&key, dentry)))
1466 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1468 if (rc == -ENOENT) {
1471 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1474 ip = jfs_iget(dip->i_sb, inum);
1476 jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
1479 return d_splice_alias(ip, dentry);
1482 static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1483 u64 ino, u32 generation)
1485 struct inode *inode;
1488 return ERR_PTR(-ESTALE);
1489 inode = jfs_iget(sb, ino);
1491 return ERR_CAST(inode);
1493 if (generation && inode->i_generation != generation) {
1495 return ERR_PTR(-ESTALE);
1501 struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1502 int fh_len, int fh_type)
1504 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1508 struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1509 int fh_len, int fh_type)
1511 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1515 struct dentry *jfs_get_parent(struct dentry *dentry)
1517 unsigned long parent_ino;
1520 le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
1522 return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
1525 const struct inode_operations jfs_dir_inode_operations = {
1526 .create = jfs_create,
1527 .lookup = jfs_lookup,
1529 .unlink = jfs_unlink,
1530 .symlink = jfs_symlink,
1534 .rename = jfs_rename,
1535 .listxattr = jfs_listxattr,
1536 .setattr = jfs_setattr,
1537 #ifdef CONFIG_JFS_POSIX_ACL
1538 .get_acl = jfs_get_acl,
1539 .set_acl = jfs_set_acl,
1543 const struct file_operations jfs_dir_operations = {
1544 .read = generic_read_dir,
1545 .iterate = jfs_readdir,
1547 .unlocked_ioctl = jfs_ioctl,
1548 #ifdef CONFIG_COMPAT
1549 .compat_ioctl = jfs_compat_ioctl,
1551 .llseek = generic_file_llseek,
1554 static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
1559 hash = init_name_hash(dir);
1560 for (i=0; i < this->len; i++)
1561 hash = partial_name_hash(tolower(this->name[i]), hash);
1562 this->hash = end_name_hash(hash);
1567 static int jfs_ci_compare(const struct dentry *dentry,
1568 unsigned int len, const char *str, const struct qstr *name)
1572 if (len != name->len)
1574 for (i=0; i < len; i++) {
1575 if (tolower(str[i]) != tolower(name->name[i]))
1583 static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
1586 * This is not negative dentry. Always valid.
1588 * Note, rename() to existing directory entry will have ->d_inode,
1589 * and will use existing name which isn't specified name by user.
1591 * We may be able to drop this positive dentry here. But dropping
1592 * positive dentry isn't good idea. So it's unsupported like
1593 * rename("filename", "FILENAME") for now.
1595 if (d_really_is_positive(dentry))
1599 * This may be nfsd (or something), anyway, we can't see the
1600 * intent of this. So, since this can be for creation, drop it.
1606 * Drop the negative dentry, in order to make sure to use the
1607 * case sensitive name which is specified by user if this is
1610 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1615 const struct dentry_operations jfs_ci_dentry_operations =
1617 .d_hash = jfs_ci_hash,
1618 .d_compare = jfs_ci_compare,
1619 .d_revalidate = jfs_ci_revalidate,