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 static 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 static 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;
107 tblk->ino = ip->i_ino;
108 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
114 * initialize the child XAD tree root in-line in inode
119 * create entry in parent directory for child directory
120 * (dtInsert() releases parent directory page)
123 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
124 jfs_err("jfs_create: dtInsert returned %d", rc);
126 txAbort(tid, 1); /* Marks Filesystem dirty */
128 txAbort(tid, 0); /* Filesystem full */
132 ip->i_op = &jfs_file_inode_operations;
133 ip->i_fop = &jfs_file_operations;
134 ip->i_mapping->a_ops = &jfs_aops;
136 insert_inode_hash(ip);
137 mark_inode_dirty(ip);
138 d_instantiate(dentry, ip);
140 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
142 mark_inode_dirty(dip);
144 rc = txCommit(tid, 2, &iplist[0], 0);
148 up(&JFS_IP(dip)->commit_sem);
149 up(&JFS_IP(ip)->commit_sem);
156 free_UCSname(&dname);
158 #ifdef CONFIG_JFS_POSIX_ACL
160 jfs_init_acl(ip, dip);
165 jfs_info("jfs_create: rc:%d", rc);
171 * NAME: jfs_mkdir(dip, dentry, mode)
173 * FUNCTION: create a child directory in the parent directory <dip>
174 * with name = <from dentry> and mode = <mode>
176 * PARAMETER: dip - parent directory vnode
177 * dentry - dentry of child directory
178 * mode - create mode (rwxrwxrwx).
180 * RETURN: Errors from subroutines
183 * EACCESS: user needs search+write permission on the parent directory
185 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
188 tid_t tid; /* transaction id */
189 struct inode *ip = NULL; /* child directory inode */
191 struct component_name dname; /* child directory name */
192 struct btstack btstack;
193 struct inode *iplist[2];
196 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
198 /* link count overflow on parent directory ? */
199 if (dip->i_nlink == JFS_LINK_MAX) {
205 * search parent directory for entry/freespace
206 * (dtSearch() returns parent directory page pinned)
208 if ((rc = get_UCSname(&dname, dentry)))
212 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
213 * block there while holding dtree page, so we allocate the inode &
214 * begin the transaction before we search the directory.
216 ip = ialloc(dip, S_IFDIR | mode);
222 tid = txBegin(dip->i_sb, 0);
224 down(&JFS_IP(dip)->commit_sem);
225 down(&JFS_IP(ip)->commit_sem);
227 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
228 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
232 tblk = tid_to_tblock(tid);
233 tblk->xflag |= COMMIT_CREATE;
234 tblk->ino = ip->i_ino;
235 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
241 * initialize the child directory in-line in inode
243 dtInitRoot(tid, ip, dip->i_ino);
246 * create entry in parent directory for child directory
247 * (dtInsert() releases parent directory page)
250 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
251 jfs_err("jfs_mkdir: dtInsert returned %d", rc);
254 txAbort(tid, 1); /* Marks Filesystem dirty */
256 txAbort(tid, 0); /* Filesystem full */
260 ip->i_nlink = 2; /* for '.' */
261 ip->i_op = &jfs_dir_inode_operations;
262 ip->i_fop = &jfs_dir_operations;
263 ip->i_mapping->a_ops = &jfs_aops;
264 mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
266 insert_inode_hash(ip);
267 mark_inode_dirty(ip);
268 d_instantiate(dentry, ip);
270 /* update parent directory inode */
271 dip->i_nlink++; /* for '..' from child directory */
272 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
273 mark_inode_dirty(dip);
275 rc = txCommit(tid, 2, &iplist[0], 0);
279 up(&JFS_IP(dip)->commit_sem);
280 up(&JFS_IP(ip)->commit_sem);
287 free_UCSname(&dname);
289 #ifdef CONFIG_JFS_POSIX_ACL
291 jfs_init_acl(ip, dip);
296 jfs_info("jfs_mkdir: rc:%d", rc);
301 * NAME: jfs_rmdir(dip, dentry)
303 * FUNCTION: remove a link to child directory
305 * PARAMETER: dip - parent inode
306 * dentry - child directory dentry
308 * RETURN: -EINVAL - if name is . or ..
309 * -EINVAL - if . or .. exist but are invalid.
310 * errors from subroutines
313 * if other threads have the directory open when the last link
314 * is removed, the "." and ".." entries, if present, are removed before
315 * rmdir() returns and no new entries may be created in the directory,
316 * but the directory is not removed until the last reference to
317 * the directory is released (cf.unlink() of regular file).
319 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
322 tid_t tid; /* transaction id */
323 struct inode *ip = dentry->d_inode;
325 struct component_name dname;
326 struct inode *iplist[2];
329 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
331 /* directory must be empty to be removed */
337 if ((rc = get_UCSname(&dname, dentry))) {
341 tid = txBegin(dip->i_sb, 0);
343 down(&JFS_IP(dip)->commit_sem);
344 down(&JFS_IP(ip)->commit_sem);
349 tblk = tid_to_tblock(tid);
350 tblk->xflag |= COMMIT_DELETE;
354 * delete the entry of target directory from parent directory
357 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
358 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
362 up(&JFS_IP(dip)->commit_sem);
363 up(&JFS_IP(ip)->commit_sem);
368 /* update parent directory's link count corresponding
369 * to ".." entry of the target directory deleted
372 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
373 mark_inode_dirty(dip);
376 * OS/2 could have created EA and/or ACL
378 /* free EA from both persistent and working map */
379 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
381 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
383 JFS_IP(ip)->ea.flag = 0;
385 /* free ACL from both persistent and working map */
386 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
388 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
390 JFS_IP(ip)->acl.flag = 0;
392 /* mark the target directory as deleted */
394 mark_inode_dirty(ip);
396 rc = txCommit(tid, 2, &iplist[0], 0);
400 up(&JFS_IP(dip)->commit_sem);
401 up(&JFS_IP(ip)->commit_sem);
404 * Truncating the directory index table is not guaranteed. It
405 * may need to be done iteratively
407 if (test_cflag(COMMIT_Stale, dip)) {
409 jfs_truncate_nolock(dip, 0);
411 clear_cflag(COMMIT_Stale, dip);
415 free_UCSname(&dname);
418 jfs_info("jfs_rmdir: rc:%d", rc);
423 * NAME: jfs_unlink(dip, dentry)
425 * FUNCTION: remove a link to object <vp> named by <name>
426 * from parent directory <dvp>
428 * PARAMETER: dip - inode of parent directory
429 * dentry - dentry of object to be removed
431 * RETURN: errors from subroutines
434 * temporary file: if one or more processes have the file open
435 * when the last link is removed, the link will be removed before
436 * unlink() returns, but the removal of the file contents will be
437 * postponed until all references to the files are closed.
439 * JFS does NOT support unlink() on directories.
442 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
445 tid_t tid; /* transaction id */
446 struct inode *ip = dentry->d_inode;
448 struct component_name dname; /* object name */
449 struct inode *iplist[2];
454 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
456 if ((rc = get_UCSname(&dname, dentry)))
461 tid = txBegin(dip->i_sb, 0);
463 down(&JFS_IP(dip)->commit_sem);
464 down(&JFS_IP(ip)->commit_sem);
470 * delete the entry of target file from parent directory
473 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
474 jfs_err("jfs_unlink: dtDelete returned %d", rc);
476 txAbort(tid, 1); /* Marks FS Dirty */
478 up(&JFS_IP(dip)->commit_sem);
479 up(&JFS_IP(ip)->commit_sem);
486 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
487 mark_inode_dirty(dip);
489 /* update target's inode */
491 mark_inode_dirty(ip);
494 * commit zero link count object
496 if (ip->i_nlink == 0) {
497 assert(!test_cflag(COMMIT_Nolink, ip));
498 /* free block resources */
499 if ((new_size = commitZeroLink(tid, ip)) < 0) {
500 txAbort(tid, 1); /* Marks FS Dirty */
502 up(&JFS_IP(dip)->commit_sem);
503 up(&JFS_IP(ip)->commit_sem);
508 tblk = tid_to_tblock(tid);
509 tblk->xflag |= COMMIT_DELETE;
514 * Incomplete truncate of file data can
515 * result in timing problems unless we synchronously commit the
519 commit_flag = COMMIT_SYNC;
524 * If xtTruncate was incomplete, commit synchronously to avoid
525 * timing complications
527 rc = txCommit(tid, 2, &iplist[0], commit_flag);
531 up(&JFS_IP(dip)->commit_sem);
532 up(&JFS_IP(ip)->commit_sem);
535 while (new_size && (rc == 0)) {
536 tid = txBegin(dip->i_sb, 0);
537 down(&JFS_IP(ip)->commit_sem);
538 new_size = xtTruncate_pmap(tid, ip, new_size);
540 txAbort(tid, 1); /* Marks FS Dirty */
543 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
545 up(&JFS_IP(ip)->commit_sem);
548 if (ip->i_nlink == 0)
549 set_cflag(COMMIT_Nolink, ip);
554 * Truncating the directory index table is not guaranteed. It
555 * may need to be done iteratively
557 if (test_cflag(COMMIT_Stale, dip)) {
559 jfs_truncate_nolock(dip, 0);
561 clear_cflag(COMMIT_Stale, dip);
565 free_UCSname(&dname);
567 jfs_info("jfs_unlink: rc:%d", rc);
572 * NAME: commitZeroLink()
574 * FUNCTION: for non-directory, called by jfs_remove(),
575 * truncate a regular file, directory or symbolic
576 * link to zero length. return 0 if type is not
579 * if the file is currently associated with a VM segment
580 * only permanent disk and inode map resources are freed,
581 * and neither the inode nor indirect blocks are modified
582 * so that the resources can be later freed in the work
584 * if there is no VM segment on entry, the resources are
585 * freed in both work and permanent map.
586 * (? for temporary file - memory object is cached even
587 * after no reference:
588 * reference count > 0 - )
590 * PARAMETERS: cd - pointer to commit data structure.
591 * current inode is the one to truncate.
593 * RETURN: Errors from subroutines
595 static s64 commitZeroLink(tid_t tid, struct inode *ip)
600 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
602 filetype = ip->i_mode & S_IFMT;
607 /* fast symbolic link */
608 if (ip->i_size < IDATASIZE) {
614 assert(filetype != S_IFDIR);
618 set_cflag(COMMIT_Freewmap, ip);
620 /* mark transaction of block map update type */
621 tblk = tid_to_tblock(tid);
622 tblk->xflag |= COMMIT_PMAP;
627 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
628 /* acquire maplock on EA to be freed from block map */
629 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
634 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
635 /* acquire maplock on EA to be freed from block map */
636 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
639 * free xtree/data (truncate to zero length):
640 * free xtree/data pages from cache if COMMIT_PWMAP,
641 * free xtree/data blocks from persistent block map, and
642 * free xtree/data blocks from working block map if COMMIT_PWMAP;
645 return xtTruncate_pmap(tid, ip, 0);
652 * NAME: freeZeroLink()
654 * FUNCTION: for non-directory, called by iClose(),
655 * free resources of a file from cache and WORKING map
656 * for a file previously committed with zero link count
657 * while associated with a pager object,
659 * PARAMETER: ip - pointer to inode of file.
663 int freeZeroLink(struct inode *ip)
668 jfs_info("freeZeroLink: ip = 0x%p", ip);
670 /* return if not reg or symbolic link or if size is
673 type = ip->i_mode & S_IFMT;
679 /* if its contained in inode nothing to do */
680 if (ip->i_size < IDATASIZE)
690 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
691 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
692 int xlen = lengthDXD(&JFS_IP(ip)->ea);
693 struct maplock maplock; /* maplock for COMMIT_WMAP */
694 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
696 /* free EA pages from cache */
697 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
699 /* free EA extent from working block map */
701 pxdlock = (struct pxd_lock *) & maplock;
702 pxdlock->flag = mlckFREEPXD;
703 PXDaddress(&pxdlock->pxd, xaddr);
704 PXDlength(&pxdlock->pxd, xlen);
705 txFreeMap(ip, pxdlock, 0, COMMIT_WMAP);
711 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
712 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
713 int xlen = lengthDXD(&JFS_IP(ip)->acl);
714 struct maplock maplock; /* maplock for COMMIT_WMAP */
715 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
717 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
719 /* free ACL extent from working block map */
721 pxdlock = (struct pxd_lock *) & maplock;
722 pxdlock->flag = mlckFREEPXD;
723 PXDaddress(&pxdlock->pxd, xaddr);
724 PXDlength(&pxdlock->pxd, xlen);
725 txFreeMap(ip, pxdlock, 0, COMMIT_WMAP);
729 * free xtree/data (truncate to zero length):
730 * free xtree/data pages from cache, and
731 * free xtree/data blocks from working block map;
734 rc = xtTruncate(0, ip, 0, COMMIT_WMAP);
740 * NAME: jfs_link(vp, dvp, name, crp)
742 * FUNCTION: create a link to <vp> by the name = <name>
743 * in the parent directory <dvp>
745 * PARAMETER: vp - target object
746 * dvp - parent directory of new link
747 * name - name of new link to target object
750 * RETURN: Errors from subroutines
753 * JFS does NOT support link() on directories (to prevent circular
754 * path in the directory hierarchy);
755 * EPERM: the target object is a directory, and either the caller
756 * does not have appropriate privileges or the implementation prohibits
757 * using link() on directories [XPG4.2].
759 * JFS does NOT support links between file systems:
760 * EXDEV: target object and new link are on different file systems and
761 * implementation does not support links between file systems [XPG4.2].
763 static int jfs_link(struct dentry *old_dentry,
764 struct inode *dir, struct dentry *dentry)
768 struct inode *ip = old_dentry->d_inode;
770 struct component_name dname;
771 struct btstack btstack;
772 struct inode *iplist[2];
774 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
775 dentry->d_name.name);
777 if (ip->i_nlink == JFS_LINK_MAX)
780 if (ip->i_nlink == 0)
783 tid = txBegin(ip->i_sb, 0);
785 down(&JFS_IP(dir)->commit_sem);
786 down(&JFS_IP(ip)->commit_sem);
789 * scan parent directory for entry/freespace
791 if ((rc = get_UCSname(&dname, dentry)))
794 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
798 * create entry for new link in parent directory
801 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
804 /* update object inode */
805 ip->i_nlink++; /* for new link */
806 ip->i_ctime = CURRENT_TIME;
807 mark_inode_dirty(dir);
808 atomic_inc(&ip->i_count);
809 d_instantiate(dentry, ip);
813 rc = txCommit(tid, 2, &iplist[0], 0);
816 free_UCSname(&dname);
821 up(&JFS_IP(dir)->commit_sem);
822 up(&JFS_IP(ip)->commit_sem);
824 jfs_info("jfs_link: rc:%d", rc);
829 * NAME: jfs_symlink(dip, dentry, name)
831 * FUNCTION: creates a symbolic link to <symlink> by name <name>
834 * PARAMETER: dip - parent directory vnode
835 * dentry - dentry of symbolic link
836 * name - the path name of the existing object
837 * that will be the source of the link
839 * RETURN: errors from subroutines
842 * ENAMETOOLONG: pathname resolution of a symbolic link produced
843 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
846 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
852 struct component_name dname;
853 int ssize; /* source pathname size */
854 struct btstack btstack;
855 struct inode *ip = dentry->d_inode;
856 unchar *i_fastsymlink;
858 int bmask = 0, xsize;
861 struct super_block *sb;
864 struct inode *iplist[2];
866 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
868 ssize = strlen(name) + 1;
871 * search parent directory for entry/freespace
872 * (dtSearch() returns parent directory page pinned)
875 if ((rc = get_UCSname(&dname, dentry)))
879 * allocate on-disk/in-memory inode for symbolic link:
880 * (iAlloc() returns new, locked inode)
882 ip = ialloc(dip, S_IFLNK | 0777);
888 tid = txBegin(dip->i_sb, 0);
890 down(&JFS_IP(dip)->commit_sem);
891 down(&JFS_IP(ip)->commit_sem);
893 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE)))
896 tblk = tid_to_tblock(tid);
897 tblk->xflag |= COMMIT_CREATE;
898 tblk->ino = ip->i_ino;
899 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
902 * create entry for symbolic link in parent directory
909 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
910 jfs_err("jfs_symlink: dtInsert returned %d", rc);
911 /* discard ne inode */
916 /* fix symlink access permission
917 * (dir_create() ANDs in the u.u_cmask,
918 * but symlinks really need to be 777 access)
923 * write symbolic link target path name
928 * write source path name inline in on-disk inode (fast symbolic link)
931 if (ssize <= IDATASIZE) {
932 ip->i_op = &jfs_symlink_inode_operations;
934 i_fastsymlink = JFS_IP(ip)->i_inline;
935 memcpy(i_fastsymlink, name, ssize);
936 ip->i_size = ssize - 1;
939 * if symlink is > 128 bytes, we don't have the space to
940 * store inline extended attributes
942 if (ssize > sizeof (JFS_IP(ip)->i_inline))
943 JFS_IP(ip)->mode2 &= ~INLINEEA;
945 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
949 * write source path name in a single extent
952 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
954 ip->i_op = &page_symlink_inode_operations;
955 ip->i_mapping->a_ops = &jfs_aops;
958 * even though the data of symlink object (source
959 * path name) is treated as non-journaled user data,
960 * it is read/written thru buffer cache for performance.
963 bmask = JFS_SBI(sb)->bsize - 1;
964 xsize = (ssize + bmask) & ~bmask;
966 xlen = xsize >> JFS_SBI(sb)->l2bsize;
967 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0)) == 0) {
968 ip->i_size = ssize - 1;
970 int copy_size = min(ssize, PSIZE);
972 mp = get_metapage(ip, xaddr, PSIZE, 1);
975 dtDelete(tid, dip, &dname, &ino,
980 memcpy(mp->data, name, copy_size);
983 set_buffer_uptodate(bp);
984 mark_buffer_dirty(bp, 1);
986 sync_dirty_buffer(bp);
990 xaddr += JFS_SBI(sb)->nbperpage;
992 ip->i_blocks = LBLK2PBLK(sb, xlen);
994 dtDelete(tid, dip, &dname, &ino, JFS_REMOVE);
1000 insert_inode_hash(ip);
1001 mark_inode_dirty(ip);
1002 d_instantiate(dentry, ip);
1005 * commit update of parent directory and link object
1007 * if extent allocation failed (ENOSPC),
1008 * the parent inode is committed regardless to avoid
1009 * backing out parent directory update (by dtInsert())
1010 * and subsequent dtDelete() which is harmless wrt
1011 * integrity concern.
1012 * the symlink inode will be freed by iput() at exit
1013 * as it has a zero link count (by dtDelete()) and
1014 * no permanant resources.
1020 rc = txCommit(tid, 2, &iplist[0], 0);
1022 rc = txCommit(tid, 1, &iplist[0], 0);
1026 up(&JFS_IP(dip)->commit_sem);
1027 up(&JFS_IP(ip)->commit_sem);
1034 free_UCSname(&dname);
1036 #ifdef CONFIG_JFS_POSIX_ACL
1038 jfs_init_acl(ip, dip);
1042 jfs_info("jfs_symlink: rc:%d", rc);
1050 * FUNCTION: rename a file or directory
1052 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1053 struct inode *new_dir, struct dentry *new_dentry)
1055 struct btstack btstack;
1057 struct component_name new_dname;
1058 struct inode *new_ip;
1059 struct component_name old_dname;
1060 struct inode *old_ip;
1064 struct dt_lock *dtlck;
1067 struct inode *iplist[4];
1068 struct tblock *tblk;
1073 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1074 new_dentry->d_name.name);
1076 old_ip = old_dentry->d_inode;
1077 new_ip = new_dentry->d_inode;
1079 if ((rc = get_UCSname(&old_dname, old_dentry)))
1082 if ((rc = get_UCSname(&new_dname, new_dentry)))
1086 * Make sure source inode number is what we think it is
1088 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1089 if (rc || (ino != old_ip->i_ino)) {
1095 * Make sure dest inode number (if any) is what we think it is
1097 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1099 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1103 } else if (rc != -ENOENT)
1106 /* no entry exists, but one was expected */
1111 if (S_ISDIR(old_ip->i_mode)) {
1113 if (!dtEmpty(new_ip)) {
1117 } else if ((new_dir != old_dir) &&
1118 (new_dir->i_nlink == JFS_LINK_MAX)) {
1123 IWRITE_LOCK(new_ip);
1126 * The real work starts here
1128 tid = txBegin(new_dir->i_sb, 0);
1130 down(&JFS_IP(new_dir)->commit_sem);
1131 down(&JFS_IP(old_ip)->commit_sem);
1132 if (old_dir != new_dir)
1133 down(&JFS_IP(old_dir)->commit_sem);
1136 down(&JFS_IP(new_ip)->commit_sem);
1138 * Change existing directory entry to new inode number
1140 ino = new_ip->i_ino;
1141 rc = dtModify(tid, new_dir, &new_dname, &ino,
1142 old_ip->i_ino, JFS_RENAME);
1146 if (S_ISDIR(new_ip->i_mode)) {
1148 if (new_ip->i_nlink) {
1149 up(&JFS_IP(new_dir)->commit_sem);
1150 up(&JFS_IP(old_ip)->commit_sem);
1151 if (old_dir != new_dir)
1152 up(&JFS_IP(old_dir)->commit_sem);
1153 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1154 IWRITE_UNLOCK(new_ip);
1155 jfs_error(new_ip->i_sb,
1156 "jfs_rename: new_ip->i_nlink != 0");
1159 tblk = tid_to_tblock(tid);
1160 tblk->xflag |= COMMIT_DELETE;
1161 tblk->u.ip = new_ip;
1162 } else if (new_ip->i_nlink == 0) {
1163 assert(!test_cflag(COMMIT_Nolink, new_ip));
1164 /* free block resources */
1165 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1166 txAbort(tid, 1); /* Marks FS Dirty */
1170 tblk = tid_to_tblock(tid);
1171 tblk->xflag |= COMMIT_DELETE;
1172 tblk->u.ip = new_ip;
1174 new_ip->i_ctime = CURRENT_TIME;
1175 mark_inode_dirty(new_ip);
1179 * Add new directory entry
1181 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1184 jfs_err("jfs_rename didn't expect dtSearch to fail "
1189 ino = old_ip->i_ino;
1190 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1192 jfs_err("jfs_rename: dtInsert failed w/rc = %d",
1196 if (S_ISDIR(old_ip->i_mode))
1200 * Remove old directory entry
1203 ino = old_ip->i_ino;
1204 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1206 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1208 txAbort(tid, 1); /* Marks Filesystem dirty */
1211 if (S_ISDIR(old_ip->i_mode)) {
1213 if (old_dir != new_dir) {
1215 * Change inode number of parent for moved directory
1218 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1219 cpu_to_le32(new_dir->i_ino);
1221 /* Linelock header of dtree */
1222 tlck = txLock(tid, old_ip,
1223 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1224 tlckDTREE | tlckBTROOT);
1225 dtlck = (struct dt_lock *) & tlck->lock;
1226 ASSERT(dtlck->index == 0);
1227 lv = & dtlck->lv[0];
1235 * Update ctime on changed/moved inodes & mark dirty
1237 old_ip->i_ctime = CURRENT_TIME;
1238 mark_inode_dirty(old_ip);
1240 new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME;
1241 mark_inode_dirty(new_dir);
1243 /* Build list of inodes modified by this transaction */
1245 iplist[ipcount++] = old_ip;
1247 iplist[ipcount++] = new_ip;
1248 iplist[ipcount++] = old_dir;
1250 if (old_dir != new_dir) {
1251 iplist[ipcount++] = new_dir;
1252 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1253 mark_inode_dirty(old_dir);
1257 * Incomplete truncate of file data can
1258 * result in timing problems unless we synchronously commit the
1262 commit_flag = COMMIT_SYNC;
1266 rc = txCommit(tid, ipcount, iplist, commit_flag);
1271 up(&JFS_IP(new_dir)->commit_sem);
1272 up(&JFS_IP(old_ip)->commit_sem);
1273 if (old_dir != new_dir)
1274 up(&JFS_IP(old_dir)->commit_sem);
1276 up(&JFS_IP(new_ip)->commit_sem);
1278 while (new_size && (rc == 0)) {
1279 tid = txBegin(new_ip->i_sb, 0);
1280 down(&JFS_IP(new_ip)->commit_sem);
1281 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1286 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1288 up(&JFS_IP(new_ip)->commit_sem);
1290 if (new_ip && (new_ip->i_nlink == 0))
1291 set_cflag(COMMIT_Nolink, new_ip);
1293 free_UCSname(&new_dname);
1295 free_UCSname(&old_dname);
1297 if (new_ip && !S_ISDIR(new_ip->i_mode))
1298 IWRITE_UNLOCK(new_ip);
1300 * Truncating the directory index table is not guaranteed. It
1301 * may need to be done iteratively
1303 if (test_cflag(COMMIT_Stale, old_dir)) {
1304 if (old_dir->i_size > 1)
1305 jfs_truncate_nolock(old_dir, 0);
1307 clear_cflag(COMMIT_Stale, old_dir);
1310 jfs_info("jfs_rename: returning %d", rc);
1318 * FUNCTION: Create a special file (device)
1320 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1321 int mode, dev_t rdev)
1323 struct jfs_inode_info *jfs_ip;
1324 struct btstack btstack;
1325 struct component_name dname;
1328 struct inode *iplist[2];
1331 struct tblock *tblk;
1333 if (!new_valid_dev(rdev))
1336 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1338 if ((rc = get_UCSname(&dname, dentry)))
1341 ip = ialloc(dir, mode);
1346 jfs_ip = JFS_IP(ip);
1348 tid = txBegin(dir->i_sb, 0);
1350 down(&JFS_IP(dir)->commit_sem);
1351 down(&JFS_IP(ip)->commit_sem);
1353 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
1356 tblk = tid_to_tblock(tid);
1357 tblk->xflag |= COMMIT_CREATE;
1358 tblk->ino = ip->i_ino;
1359 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1362 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
1365 ip->i_op = &jfs_file_inode_operations;
1366 jfs_ip->dev = new_encode_dev(rdev);
1367 init_special_inode(ip, ip->i_mode, rdev);
1369 insert_inode_hash(ip);
1370 mark_inode_dirty(ip);
1371 d_instantiate(dentry, ip);
1373 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1375 mark_inode_dirty(dir);
1379 rc = txCommit(tid, 2, iplist, 0);
1383 up(&JFS_IP(ip)->commit_sem);
1384 up(&JFS_IP(dir)->commit_sem);
1391 free_UCSname(&dname);
1393 #ifdef CONFIG_JFS_POSIX_ACL
1395 jfs_init_acl(ip, dir);
1399 jfs_info("jfs_mknod: returning %d", rc);
1403 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1405 struct btstack btstack;
1408 struct component_name key;
1409 const char *name = dentry->d_name.name;
1410 int len = dentry->d_name.len;
1413 jfs_info("jfs_lookup: name = %s", name);
1416 if ((name[0] == '.') && (len == 1))
1418 else if (strcmp(name, "..") == 0)
1421 if ((rc = get_UCSname(&key, dentry)))
1423 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1425 if (rc == -ENOENT) {
1426 d_add(dentry, NULL);
1429 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1434 ip = iget(dip->i_sb, inum);
1435 if (ip == NULL || is_bad_inode(ip)) {
1436 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1439 return ERR_PTR(-EACCES);
1442 return d_splice_alias(ip, dentry);
1445 struct dentry *jfs_get_parent(struct dentry *dentry)
1447 struct super_block *sb = dentry->d_inode->i_sb;
1448 struct dentry *parent = ERR_PTR(-ENOENT);
1449 struct inode *inode;
1450 unsigned long parent_ino;
1453 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1454 inode = iget(sb, parent_ino);
1456 if (is_bad_inode(inode)) {
1458 parent = ERR_PTR(-EACCES);
1460 parent = d_alloc_anon(inode);
1462 parent = ERR_PTR(-ENOMEM);
1471 struct inode_operations jfs_dir_inode_operations = {
1472 .create = jfs_create,
1473 .lookup = jfs_lookup,
1475 .unlink = jfs_unlink,
1476 .symlink = jfs_symlink,
1480 .rename = jfs_rename,
1481 .setxattr = jfs_setxattr,
1482 .getxattr = jfs_getxattr,
1483 .listxattr = jfs_listxattr,
1484 .removexattr = jfs_removexattr,
1485 #ifdef CONFIG_JFS_POSIX_ACL
1486 .setattr = jfs_setattr,
1487 .permission = jfs_permission,
1491 struct file_operations jfs_dir_operations = {
1492 .read = generic_read_dir,
1493 .readdir = jfs_readdir,