2 * Copyright (C) International Business Machines Corp., 2000-2003
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 "jfs_incore.h"
22 #include "jfs_superblock.h"
23 #include "jfs_inode.h"
24 #include "jfs_dinode.h"
26 #include "jfs_unicode.h"
27 #include "jfs_metapage.h"
28 #include "jfs_xattr.h"
30 #include "jfs_debug.h"
32 extern struct inode_operations jfs_file_inode_operations;
33 extern struct inode_operations jfs_symlink_inode_operations;
34 extern struct file_operations jfs_file_operations;
35 extern struct address_space_operations jfs_aops;
37 extern int jfs_fsync(struct file *, struct dentry *, int);
38 extern void jfs_truncate_nolock(struct inode *, loff_t);
39 extern int jfs_init_acl(struct inode *, struct inode *);
44 struct inode_operations jfs_dir_inode_operations;
45 struct file_operations jfs_dir_operations;
47 s64 commitZeroLink(tid_t, struct inode *);
50 * NAME: jfs_create(dip, dentry, mode)
52 * FUNCTION: create a regular file in the parent directory <dip>
53 * with name = <from dentry> and mode = <mode>
55 * PARAMETER: dip - parent directory vnode
56 * dentry - dentry of new file
57 * mode - create mode (rwxrwxrwx).
60 * RETURN: Errors from subroutines
63 int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
67 tid_t tid; /* transaction id */
68 struct inode *ip = NULL; /* child directory inode */
70 struct component_name dname; /* child directory name */
71 struct btstack btstack;
72 struct inode *iplist[2];
75 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
78 * search parent directory for entry/freespace
79 * (dtSearch() returns parent directory page pinned)
81 if ((rc = get_UCSname(&dname, dentry)))
85 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
86 * block there while holding dtree page, so we allocate the inode &
87 * begin the transaction before we search the directory.
89 ip = ialloc(dip, mode);
95 tid = txBegin(dip->i_sb, 0);
97 down(&JFS_IP(dip)->commit_sem);
98 down(&JFS_IP(ip)->commit_sem);
100 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
101 jfs_err("jfs_create: dtSearch returned %d", rc);
105 tblk = tid_to_tblock(tid);
106 tblk->xflag |= COMMIT_CREATE;
113 * initialize the child XAD tree root in-line in inode
118 * create entry in parent directory for child directory
119 * (dtInsert() releases parent directory page)
122 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
123 jfs_err("jfs_create: dtInsert returned %d", rc);
125 txAbort(tid, 1); /* Marks Filesystem dirty */
127 txAbort(tid, 0); /* Filesystem full */
131 ip->i_op = &jfs_file_inode_operations;
132 ip->i_fop = &jfs_file_operations;
133 ip->i_mapping->a_ops = &jfs_aops;
135 insert_inode_hash(ip);
136 mark_inode_dirty(ip);
137 d_instantiate(dentry, ip);
139 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
141 mark_inode_dirty(dip);
143 rc = txCommit(tid, 2, &iplist[0], 0);
147 up(&JFS_IP(dip)->commit_sem);
148 up(&JFS_IP(ip)->commit_sem);
155 free_UCSname(&dname);
157 #ifdef CONFIG_JFS_POSIX_ACL
159 jfs_init_acl(ip, dip);
164 jfs_info("jfs_create: rc:%d", rc);
170 * NAME: jfs_mkdir(dip, dentry, mode)
172 * FUNCTION: create a child directory in the parent directory <dip>
173 * with name = <from dentry> and mode = <mode>
175 * PARAMETER: dip - parent directory vnode
176 * dentry - dentry of child directory
177 * mode - create mode (rwxrwxrwx).
179 * RETURN: Errors from subroutines
182 * EACCESS: user needs search+write permission on the parent directory
184 int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
187 tid_t tid; /* transaction id */
188 struct inode *ip = NULL; /* child directory inode */
190 struct component_name dname; /* child directory name */
191 struct btstack btstack;
192 struct inode *iplist[2];
195 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
197 /* link count overflow on parent directory ? */
198 if (dip->i_nlink == JFS_LINK_MAX) {
204 * search parent directory for entry/freespace
205 * (dtSearch() returns parent directory page pinned)
207 if ((rc = get_UCSname(&dname, dentry)))
211 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
212 * block there while holding dtree page, so we allocate the inode &
213 * begin the transaction before we search the directory.
215 ip = ialloc(dip, S_IFDIR | mode);
221 tid = txBegin(dip->i_sb, 0);
223 down(&JFS_IP(dip)->commit_sem);
224 down(&JFS_IP(ip)->commit_sem);
226 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
227 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
231 tblk = tid_to_tblock(tid);
232 tblk->xflag |= COMMIT_CREATE;
239 * initialize the child directory in-line in inode
241 dtInitRoot(tid, ip, dip->i_ino);
244 * create entry in parent directory for child directory
245 * (dtInsert() releases parent directory page)
248 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
249 jfs_err("jfs_mkdir: dtInsert returned %d", rc);
252 txAbort(tid, 1); /* Marks Filesystem dirty */
254 txAbort(tid, 0); /* Filesystem full */
258 ip->i_nlink = 2; /* for '.' */
259 ip->i_op = &jfs_dir_inode_operations;
260 ip->i_fop = &jfs_dir_operations;
261 ip->i_mapping->a_ops = &jfs_aops;
262 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
264 insert_inode_hash(ip);
265 mark_inode_dirty(ip);
266 d_instantiate(dentry, ip);
268 /* update parent directory inode */
269 dip->i_nlink++; /* for '..' from child directory */
270 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
271 mark_inode_dirty(dip);
273 rc = txCommit(tid, 2, &iplist[0], 0);
277 up(&JFS_IP(dip)->commit_sem);
278 up(&JFS_IP(ip)->commit_sem);
285 free_UCSname(&dname);
287 #ifdef CONFIG_JFS_POSIX_ACL
289 jfs_init_acl(ip, dip);
294 jfs_info("jfs_mkdir: rc:%d", rc);
299 * NAME: jfs_rmdir(dip, dentry)
301 * FUNCTION: remove a link to child directory
303 * PARAMETER: dip - parent inode
304 * dentry - child directory dentry
306 * RETURN: -EINVAL - if name is . or ..
307 * -EINVAL - if . or .. exist but are invalid.
308 * errors from subroutines
311 * if other threads have the directory open when the last link
312 * is removed, the "." and ".." entries, if present, are removed before
313 * rmdir() returns and no new entries may be created in the directory,
314 * but the directory is not removed until the last reference to
315 * the directory is released (cf.unlink() of regular file).
317 int jfs_rmdir(struct inode *dip, struct dentry *dentry)
320 tid_t tid; /* transaction id */
321 struct inode *ip = dentry->d_inode;
323 struct component_name dname;
324 struct inode *iplist[2];
327 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
329 /* directory must be empty to be removed */
335 if ((rc = get_UCSname(&dname, dentry))) {
339 tid = txBegin(dip->i_sb, 0);
341 down(&JFS_IP(dip)->commit_sem);
342 down(&JFS_IP(ip)->commit_sem);
347 tblk = tid_to_tblock(tid);
348 tblk->xflag |= COMMIT_DELETE;
352 * delete the entry of target directory from parent directory
355 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
356 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
360 up(&JFS_IP(dip)->commit_sem);
361 up(&JFS_IP(ip)->commit_sem);
366 /* update parent directory's link count corresponding
367 * to ".." entry of the target directory deleted
370 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
371 mark_inode_dirty(dip);
374 * OS/2 could have created EA and/or ACL
376 /* free EA from both persistent and working map */
377 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
379 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
381 JFS_IP(ip)->ea.flag = 0;
383 /* free ACL from both persistent and working map */
384 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
386 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
388 JFS_IP(ip)->acl.flag = 0;
390 /* mark the target directory as deleted */
392 mark_inode_dirty(ip);
394 rc = txCommit(tid, 2, &iplist[0], 0);
398 up(&JFS_IP(dip)->commit_sem);
399 up(&JFS_IP(ip)->commit_sem);
402 * Truncating the directory index table is not guaranteed. It
403 * may need to be done iteratively
405 if (test_cflag(COMMIT_Stale, dip)) {
407 jfs_truncate_nolock(dip, 0);
409 clear_cflag(COMMIT_Stale, dip);
413 free_UCSname(&dname);
416 jfs_info("jfs_rmdir: rc:%d", rc);
421 * NAME: jfs_unlink(dip, dentry)
423 * FUNCTION: remove a link to object <vp> named by <name>
424 * from parent directory <dvp>
426 * PARAMETER: dip - inode of parent directory
427 * dentry - dentry of object to be removed
429 * RETURN: errors from subroutines
432 * temporary file: if one or more processes have the file open
433 * when the last link is removed, the link will be removed before
434 * unlink() returns, but the removal of the file contents will be
435 * postponed until all references to the files are closed.
437 * JFS does NOT support unlink() on directories.
440 int jfs_unlink(struct inode *dip, struct dentry *dentry)
443 tid_t tid; /* transaction id */
444 struct inode *ip = dentry->d_inode;
446 struct component_name dname; /* object name */
447 struct inode *iplist[2];
452 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
454 if ((rc = get_UCSname(&dname, dentry)))
459 tid = txBegin(dip->i_sb, 0);
461 down(&JFS_IP(dip)->commit_sem);
462 down(&JFS_IP(ip)->commit_sem);
468 * delete the entry of target file from parent directory
471 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
472 jfs_err("jfs_unlink: dtDelete returned %d", rc);
474 txAbort(tid, 1); /* Marks FS Dirty */
476 up(&JFS_IP(dip)->commit_sem);
477 up(&JFS_IP(ip)->commit_sem);
484 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
485 mark_inode_dirty(dip);
487 /* update target's inode */
489 mark_inode_dirty(ip);
492 * commit zero link count object
494 if (ip->i_nlink == 0) {
495 assert(!test_cflag(COMMIT_Nolink, ip));
496 /* free block resources */
497 if ((new_size = commitZeroLink(tid, ip)) < 0) {
498 txAbort(tid, 1); /* Marks FS Dirty */
500 up(&JFS_IP(dip)->commit_sem);
501 up(&JFS_IP(ip)->commit_sem);
506 tblk = tid_to_tblock(tid);
507 tblk->xflag |= COMMIT_DELETE;
512 * Incomplete truncate of file data can
513 * result in timing problems unless we synchronously commit the
517 commit_flag = COMMIT_SYNC;
522 * If xtTruncate was incomplete, commit synchronously to avoid
523 * timing complications
525 rc = txCommit(tid, 2, &iplist[0], commit_flag);
529 up(&JFS_IP(dip)->commit_sem);
530 up(&JFS_IP(ip)->commit_sem);
533 while (new_size && (rc == 0)) {
534 tid = txBegin(dip->i_sb, 0);
535 down(&JFS_IP(ip)->commit_sem);
536 new_size = xtTruncate_pmap(tid, ip, new_size);
538 txAbort(tid, 1); /* Marks FS Dirty */
541 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
543 up(&JFS_IP(ip)->commit_sem);
546 if (ip->i_nlink == 0)
547 set_cflag(COMMIT_Nolink, ip);
552 * Truncating the directory index table is not guaranteed. It
553 * may need to be done iteratively
555 if (test_cflag(COMMIT_Stale, dip)) {
557 jfs_truncate_nolock(dip, 0);
559 clear_cflag(COMMIT_Stale, dip);
563 free_UCSname(&dname);
565 jfs_info("jfs_unlink: rc:%d", rc);
570 * NAME: commitZeroLink()
572 * FUNCTION: for non-directory, called by jfs_remove(),
573 * truncate a regular file, directory or symbolic
574 * link to zero length. return 0 if type is not
577 * if the file is currently associated with a VM segment
578 * only permanent disk and inode map resources are freed,
579 * and neither the inode nor indirect blocks are modified
580 * so that the resources can be later freed in the work
582 * if there is no VM segment on entry, the resources are
583 * freed in both work and permanent map.
584 * (? for temporary file - memory object is cached even
585 * after no reference:
586 * reference count > 0 - )
588 * PARAMETERS: cd - pointer to commit data structure.
589 * current inode is the one to truncate.
591 * RETURN: Errors from subroutines
593 s64 commitZeroLink(tid_t tid, struct inode *ip)
598 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
600 filetype = ip->i_mode & S_IFMT;
605 /* fast symbolic link */
606 if (ip->i_size < IDATASIZE) {
612 assert(filetype != S_IFDIR);
616 set_cflag(COMMIT_Freewmap, ip);
618 /* mark transaction of block map update type */
619 tblk = tid_to_tblock(tid);
620 tblk->xflag |= COMMIT_PMAP;
625 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
626 /* acquire maplock on EA to be freed from block map */
627 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
632 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
633 /* acquire maplock on EA to be freed from block map */
634 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
637 * free xtree/data (truncate to zero length):
638 * free xtree/data pages from cache if COMMIT_PWMAP,
639 * free xtree/data blocks from persistent block map, and
640 * free xtree/data blocks from working block map if COMMIT_PWMAP;
643 return xtTruncate_pmap(tid, ip, 0);
650 * NAME: freeZeroLink()
652 * FUNCTION: for non-directory, called by iClose(),
653 * free resources of a file from cache and WORKING map
654 * for a file previously committed with zero link count
655 * while associated with a pager object,
657 * PARAMETER: ip - pointer to inode of file.
661 int freeZeroLink(struct inode *ip)
666 jfs_info("freeZeroLink: ip = 0x%p", ip);
668 /* return if not reg or symbolic link or if size is
671 type = ip->i_mode & S_IFMT;
677 /* if its contained in inode nothing to do */
678 if (ip->i_size < IDATASIZE)
688 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
689 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
690 int xlen = lengthDXD(&JFS_IP(ip)->ea);
691 struct maplock maplock; /* maplock for COMMIT_WMAP */
692 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
694 /* free EA pages from cache */
695 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
697 /* free EA extent from working block map */
699 pxdlock = (struct pxd_lock *) & maplock;
700 pxdlock->flag = mlckFREEPXD;
701 PXDaddress(&pxdlock->pxd, xaddr);
702 PXDlength(&pxdlock->pxd, xlen);
703 txFreeMap(ip, pxdlock, 0, COMMIT_WMAP);
709 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
710 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
711 int xlen = lengthDXD(&JFS_IP(ip)->acl);
712 struct maplock maplock; /* maplock for COMMIT_WMAP */
713 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
715 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
717 /* free ACL extent from working block map */
719 pxdlock = (struct pxd_lock *) & maplock;
720 pxdlock->flag = mlckFREEPXD;
721 PXDaddress(&pxdlock->pxd, xaddr);
722 PXDlength(&pxdlock->pxd, xlen);
723 txFreeMap(ip, pxdlock, 0, COMMIT_WMAP);
727 * free xtree/data (truncate to zero length):
728 * free xtree/data pages from cache, and
729 * free xtree/data blocks from working block map;
732 rc = xtTruncate(0, ip, 0, COMMIT_WMAP);
738 * NAME: jfs_link(vp, dvp, name, crp)
740 * FUNCTION: create a link to <vp> by the name = <name>
741 * in the parent directory <dvp>
743 * PARAMETER: vp - target object
744 * dvp - parent directory of new link
745 * name - name of new link to target object
748 * RETURN: Errors from subroutines
751 * JFS does NOT support link() on directories (to prevent circular
752 * path in the directory hierarchy);
753 * EPERM: the target object is a directory, and either the caller
754 * does not have appropriate privileges or the implementation prohibits
755 * using link() on directories [XPG4.2].
757 * JFS does NOT support links between file systems:
758 * EXDEV: target object and new link are on different file systems and
759 * implementation does not support links between file systems [XPG4.2].
761 int jfs_link(struct dentry *old_dentry,
762 struct inode *dir, struct dentry *dentry)
766 struct inode *ip = old_dentry->d_inode;
768 struct component_name dname;
769 struct btstack btstack;
770 struct inode *iplist[2];
772 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
773 dentry->d_name.name);
775 if (ip->i_nlink == JFS_LINK_MAX)
778 if (ip->i_nlink == 0)
781 tid = txBegin(ip->i_sb, 0);
783 down(&JFS_IP(dir)->commit_sem);
784 down(&JFS_IP(ip)->commit_sem);
787 * scan parent directory for entry/freespace
789 if ((rc = get_UCSname(&dname, dentry)))
792 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
796 * create entry for new link in parent directory
799 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
802 /* update object inode */
803 ip->i_nlink++; /* for new link */
804 ip->i_ctime = CURRENT_TIME;
805 mark_inode_dirty(dir);
806 atomic_inc(&ip->i_count);
807 d_instantiate(dentry, ip);
811 rc = txCommit(tid, 2, &iplist[0], 0);
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 int jfs_symlink(struct inode *dip, struct dentry *dentry, const char *name)
846 struct component_name dname;
847 int ssize; /* source pathname size */
848 struct btstack btstack;
849 struct inode *ip = dentry->d_inode;
850 unchar *i_fastsymlink;
852 int bmask = 0, xsize;
855 struct super_block *sb;
858 struct inode *iplist[2];
860 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
862 ssize = strlen(name) + 1;
865 * search parent directory for entry/freespace
866 * (dtSearch() returns parent directory page pinned)
869 if ((rc = get_UCSname(&dname, dentry)))
873 * allocate on-disk/in-memory inode for symbolic link:
874 * (iAlloc() returns new, locked inode)
876 ip = ialloc(dip, S_IFLNK | 0777);
882 tid = txBegin(dip->i_sb, 0);
884 down(&JFS_IP(dip)->commit_sem);
885 down(&JFS_IP(ip)->commit_sem);
887 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE)))
890 tblk = tid_to_tblock(tid);
891 tblk->xflag |= COMMIT_CREATE;
895 * create entry for symbolic link in parent directory
902 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
903 jfs_err("jfs_symlink: dtInsert returned %d", rc);
904 /* discard ne inode */
909 /* fix symlink access permission
910 * (dir_create() ANDs in the u.u_cmask,
911 * but symlinks really need to be 777 access)
916 * write symbolic link target path name
921 * write source path name inline in on-disk inode (fast symbolic link)
924 if (ssize <= IDATASIZE) {
925 ip->i_op = &jfs_symlink_inode_operations;
927 i_fastsymlink = JFS_IP(ip)->i_inline;
928 memcpy(i_fastsymlink, name, ssize);
929 ip->i_size = ssize - 1;
932 * if symlink is > 128 bytes, we don't have the space to
933 * store inline extended attributes
935 if (ssize > sizeof (JFS_IP(ip)->i_inline))
936 JFS_IP(ip)->mode2 &= ~INLINEEA;
938 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
942 * write source path name in a single extent
945 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
947 ip->i_op = &page_symlink_inode_operations;
948 ip->i_mapping->a_ops = &jfs_aops;
951 * even though the data of symlink object (source
952 * path name) is treated as non-journaled user data,
953 * it is read/written thru buffer cache for performance.
956 bmask = JFS_SBI(sb)->bsize - 1;
957 xsize = (ssize + bmask) & ~bmask;
959 xlen = xsize >> JFS_SBI(sb)->l2bsize;
960 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0)) == 0) {
961 ip->i_size = ssize - 1;
963 int copy_size = min(ssize, PSIZE);
965 mp = get_metapage(ip, xaddr, PSIZE, 1);
968 dtDelete(tid, dip, &dname, &ino,
973 memcpy(mp->data, name, copy_size);
976 set_buffer_uptodate(bp);
977 mark_buffer_dirty(bp, 1);
979 sync_dirty_buffer(bp);
983 xaddr += JFS_SBI(sb)->nbperpage;
985 ip->i_blocks = LBLK2PBLK(sb, xlen);
987 dtDelete(tid, dip, &dname, &ino, JFS_REMOVE);
993 insert_inode_hash(ip);
994 mark_inode_dirty(ip);
995 d_instantiate(dentry, ip);
998 * commit update of parent directory and link object
1000 * if extent allocation failed (ENOSPC),
1001 * the parent inode is committed regardless to avoid
1002 * backing out parent directory update (by dtInsert())
1003 * and subsequent dtDelete() which is harmless wrt
1004 * integrity concern.
1005 * the symlink inode will be freed by iput() at exit
1006 * as it has a zero link count (by dtDelete()) and
1007 * no permanant resources.
1013 rc = txCommit(tid, 2, &iplist[0], 0);
1015 rc = txCommit(tid, 1, &iplist[0], 0);
1019 up(&JFS_IP(dip)->commit_sem);
1020 up(&JFS_IP(ip)->commit_sem);
1027 free_UCSname(&dname);
1029 #ifdef CONFIG_JFS_POSIX_ACL
1031 jfs_init_acl(ip, dip);
1035 jfs_info("jfs_symlink: rc:%d", rc);
1043 * FUNCTION: rename a file or directory
1045 int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1046 struct inode *new_dir, struct dentry *new_dentry)
1048 struct btstack btstack;
1050 struct component_name new_dname;
1051 struct inode *new_ip;
1052 struct component_name old_dname;
1053 struct inode *old_ip;
1057 struct dt_lock *dtlck;
1060 struct inode *iplist[4];
1061 struct tblock *tblk;
1066 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1067 new_dentry->d_name.name);
1069 old_ip = old_dentry->d_inode;
1070 new_ip = new_dentry->d_inode;
1072 if ((rc = get_UCSname(&old_dname, old_dentry)))
1075 if ((rc = get_UCSname(&new_dname, new_dentry)))
1079 * Make sure source inode number is what we think it is
1081 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1082 if (rc || (ino != old_ip->i_ino)) {
1088 * Make sure dest inode number (if any) is what we think it is
1090 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1092 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1096 } else if (rc != -ENOENT)
1099 /* no entry exists, but one was expected */
1104 if (S_ISDIR(old_ip->i_mode)) {
1106 if (!dtEmpty(new_ip)) {
1110 } else if ((new_dir != old_dir) &&
1111 (new_dir->i_nlink == JFS_LINK_MAX)) {
1116 IWRITE_LOCK(new_ip);
1119 * The real work starts here
1121 tid = txBegin(new_dir->i_sb, 0);
1123 down(&JFS_IP(new_dir)->commit_sem);
1124 down(&JFS_IP(old_ip)->commit_sem);
1125 if (old_dir != new_dir)
1126 down(&JFS_IP(old_dir)->commit_sem);
1129 down(&JFS_IP(new_ip)->commit_sem);
1131 * Change existing directory entry to new inode number
1133 ino = new_ip->i_ino;
1134 rc = dtModify(tid, new_dir, &new_dname, &ino,
1135 old_ip->i_ino, JFS_RENAME);
1139 if (S_ISDIR(new_ip->i_mode)) {
1141 if (new_ip->i_nlink) {
1142 up(&JFS_IP(new_dir)->commit_sem);
1143 up(&JFS_IP(old_ip)->commit_sem);
1144 if (old_dir != new_dir)
1145 up(&JFS_IP(old_dir)->commit_sem);
1146 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1147 IWRITE_UNLOCK(new_ip);
1148 jfs_error(new_ip->i_sb,
1149 "jfs_rename: new_ip->i_nlink != 0");
1152 tblk = tid_to_tblock(tid);
1153 tblk->xflag |= COMMIT_DELETE;
1155 } else if (new_ip->i_nlink == 0) {
1156 assert(!test_cflag(COMMIT_Nolink, new_ip));
1157 /* free block resources */
1158 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1159 txAbort(tid, 1); /* Marks FS Dirty */
1163 tblk = tid_to_tblock(tid);
1164 tblk->xflag |= COMMIT_DELETE;
1167 new_ip->i_ctime = CURRENT_TIME;
1168 mark_inode_dirty(new_ip);
1172 * Add new directory entry
1174 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1177 jfs_err("jfs_rename didn't expect dtSearch to fail "
1182 ino = old_ip->i_ino;
1183 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1185 jfs_err("jfs_rename: dtInsert failed w/rc = %d",
1189 if (S_ISDIR(old_ip->i_mode))
1193 * Remove old directory entry
1196 ino = old_ip->i_ino;
1197 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1199 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1201 txAbort(tid, 1); /* Marks Filesystem dirty */
1204 if (S_ISDIR(old_ip->i_mode)) {
1206 if (old_dir != new_dir) {
1208 * Change inode number of parent for moved directory
1211 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1212 cpu_to_le32(new_dir->i_ino);
1214 /* Linelock header of dtree */
1215 tlck = txLock(tid, old_ip,
1216 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1217 tlckDTREE | tlckBTROOT);
1218 dtlck = (struct dt_lock *) & tlck->lock;
1219 ASSERT(dtlck->index == 0);
1220 lv = & dtlck->lv[0];
1228 * Update ctime on changed/moved inodes & mark dirty
1230 old_ip->i_ctime = CURRENT_TIME;
1231 mark_inode_dirty(old_ip);
1233 new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME;
1234 mark_inode_dirty(new_dir);
1236 /* Build list of inodes modified by this transaction */
1238 iplist[ipcount++] = old_ip;
1240 iplist[ipcount++] = new_ip;
1241 iplist[ipcount++] = old_dir;
1243 if (old_dir != new_dir) {
1244 iplist[ipcount++] = new_dir;
1245 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1246 mark_inode_dirty(old_dir);
1250 * Incomplete truncate of file data can
1251 * result in timing problems unless we synchronously commit the
1255 commit_flag = COMMIT_SYNC;
1259 rc = txCommit(tid, ipcount, iplist, commit_flag);
1264 up(&JFS_IP(new_dir)->commit_sem);
1265 up(&JFS_IP(old_ip)->commit_sem);
1266 if (old_dir != new_dir)
1267 up(&JFS_IP(old_dir)->commit_sem);
1269 up(&JFS_IP(new_ip)->commit_sem);
1271 while (new_size && (rc == 0)) {
1272 tid = txBegin(new_ip->i_sb, 0);
1273 down(&JFS_IP(new_ip)->commit_sem);
1274 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1279 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1281 up(&JFS_IP(new_ip)->commit_sem);
1283 if (new_ip && (new_ip->i_nlink == 0))
1284 set_cflag(COMMIT_Nolink, new_ip);
1286 free_UCSname(&new_dname);
1288 free_UCSname(&old_dname);
1290 if (new_ip && !S_ISDIR(new_ip->i_mode))
1291 IWRITE_UNLOCK(new_ip);
1293 * Truncating the directory index table is not guaranteed. It
1294 * may need to be done iteratively
1296 if (test_cflag(COMMIT_Stale, old_dir)) {
1297 if (old_dir->i_size > 1)
1298 jfs_truncate_nolock(old_dir, 0);
1300 clear_cflag(COMMIT_Stale, old_dir);
1303 jfs_info("jfs_rename: returning %d", rc);
1311 * FUNCTION: Create a special file (device)
1313 int jfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1315 struct jfs_inode_info *jfs_ip;
1316 struct btstack btstack;
1317 struct component_name dname;
1320 struct inode *iplist[2];
1323 struct tblock *tblk;
1325 if (!new_valid_dev(rdev))
1328 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1330 if ((rc = get_UCSname(&dname, dentry)))
1333 ip = ialloc(dir, mode);
1338 jfs_ip = JFS_IP(ip);
1340 tid = txBegin(dir->i_sb, 0);
1342 down(&JFS_IP(dir)->commit_sem);
1343 down(&JFS_IP(ip)->commit_sem);
1345 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
1348 tblk = tid_to_tblock(tid);
1349 tblk->xflag |= COMMIT_CREATE;
1353 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
1356 ip->i_op = &jfs_file_inode_operations;
1357 jfs_ip->dev = new_encode_dev(rdev);
1358 init_special_inode(ip, ip->i_mode, rdev);
1360 insert_inode_hash(ip);
1361 mark_inode_dirty(ip);
1362 d_instantiate(dentry, ip);
1364 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1366 mark_inode_dirty(dir);
1370 rc = txCommit(tid, 2, iplist, 0);
1374 up(&JFS_IP(ip)->commit_sem);
1375 up(&JFS_IP(dir)->commit_sem);
1382 free_UCSname(&dname);
1384 #ifdef CONFIG_JFS_POSIX_ACL
1386 jfs_init_acl(ip, dir);
1390 jfs_info("jfs_mknod: returning %d", rc);
1394 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1396 struct btstack btstack;
1399 struct component_name key;
1400 const char *name = dentry->d_name.name;
1401 int len = dentry->d_name.len;
1404 jfs_info("jfs_lookup: name = %s", name);
1407 if ((name[0] == '.') && (len == 1))
1409 else if (strcmp(name, "..") == 0)
1412 if ((rc = get_UCSname(&key, dentry)))
1414 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1416 if (rc == -ENOENT) {
1417 d_add(dentry, NULL);
1420 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1425 ip = iget(dip->i_sb, inum);
1426 if (ip == NULL || is_bad_inode(ip)) {
1427 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1430 return ERR_PTR(-EACCES);
1433 return d_splice_alias(ip, dentry);
1436 struct dentry *jfs_get_parent(struct dentry *dentry)
1438 struct super_block *sb = dentry->d_inode->i_sb;
1439 struct dentry *parent = ERR_PTR(-ENOENT);
1440 struct inode *inode;
1441 unsigned long parent_ino;
1444 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1445 inode = iget(sb, parent_ino);
1447 if (is_bad_inode(inode)) {
1449 parent = ERR_PTR(-EACCES);
1451 parent = d_alloc_anon(inode);
1453 parent = ERR_PTR(-ENOMEM);
1462 struct inode_operations jfs_dir_inode_operations = {
1463 .create = jfs_create,
1464 .lookup = jfs_lookup,
1466 .unlink = jfs_unlink,
1467 .symlink = jfs_symlink,
1471 .rename = jfs_rename,
1472 .setxattr = jfs_setxattr,
1473 .getxattr = jfs_getxattr,
1474 .listxattr = jfs_listxattr,
1475 .removexattr = jfs_removexattr,
1476 #ifdef CONFIG_JFS_POSIX_ACL
1477 .setattr = jfs_setattr,
1478 .permission = jfs_permission,
1482 struct file_operations jfs_dir_operations = {
1483 .read = generic_read_dir,
1484 .readdir = jfs_readdir,