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
, int 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:%s", dip
, dentry
->d_name
.name
);
89 dquot_initialize(dip
);
92 * search parent directory for entry/freespace
93 * (dtSearch() returns parent directory page pinned)
95 if ((rc
= get_UCSname(&dname
, dentry
)))
99 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
100 * block there while holding dtree page, so we allocate the inode &
101 * begin the transaction before we search the directory.
103 ip
= ialloc(dip
, mode
);
109 tid
= txBegin(dip
->i_sb
, 0);
111 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
112 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
114 rc
= jfs_init_acl(tid
, ip
, dip
);
118 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
124 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
125 jfs_err("jfs_create: dtSearch returned %d", rc
);
130 tblk
= tid_to_tblock(tid
);
131 tblk
->xflag
|= COMMIT_CREATE
;
132 tblk
->ino
= ip
->i_ino
;
133 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
139 * initialize the child XAD tree root in-line in inode
144 * create entry in parent directory for child directory
145 * (dtInsert() releases parent directory page)
148 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
150 jfs_err("jfs_create: dtInsert returned -EIO");
151 txAbort(tid
, 1); /* Marks Filesystem dirty */
153 txAbort(tid
, 0); /* Filesystem full */
157 ip
->i_op
= &jfs_file_inode_operations
;
158 ip
->i_fop
= &jfs_file_operations
;
159 ip
->i_mapping
->a_ops
= &jfs_aops
;
161 mark_inode_dirty(ip
);
163 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
165 mark_inode_dirty(dip
);
167 rc
= txCommit(tid
, 2, &iplist
[0], 0);
171 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
172 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
176 unlock_new_inode(ip
);
179 d_instantiate(dentry
, ip
);
180 unlock_new_inode(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
, int 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:%s", dip
, dentry
->d_name
.name
);
221 dquot_initialize(dip
);
223 /* link count overflow on parent directory ? */
224 if (dip
->i_nlink
== JFS_LINK_MAX
) {
230 * search parent directory for entry/freespace
231 * (dtSearch() returns parent directory page pinned)
233 if ((rc
= get_UCSname(&dname
, dentry
)))
237 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
238 * block there while holding dtree page, so we allocate the inode &
239 * begin the transaction before we search the directory.
241 ip
= ialloc(dip
, S_IFDIR
| mode
);
247 tid
= txBegin(dip
->i_sb
, 0);
249 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
250 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
252 rc
= jfs_init_acl(tid
, ip
, dip
);
256 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
262 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
263 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
268 tblk
= tid_to_tblock(tid
);
269 tblk
->xflag
|= COMMIT_CREATE
;
270 tblk
->ino
= ip
->i_ino
;
271 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
277 * initialize the child directory in-line in inode
279 dtInitRoot(tid
, ip
, dip
->i_ino
);
282 * create entry in parent directory for child directory
283 * (dtInsert() releases parent directory page)
286 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
288 jfs_err("jfs_mkdir: dtInsert returned -EIO");
289 txAbort(tid
, 1); /* Marks Filesystem dirty */
291 txAbort(tid
, 0); /* Filesystem full */
295 ip
->i_nlink
= 2; /* for '.' */
296 ip
->i_op
= &jfs_dir_inode_operations
;
297 ip
->i_fop
= &jfs_dir_operations
;
299 mark_inode_dirty(ip
);
301 /* update parent directory inode */
302 inc_nlink(dip
); /* for '..' from child directory */
303 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
304 mark_inode_dirty(dip
);
306 rc
= txCommit(tid
, 2, &iplist
[0], 0);
310 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
311 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
315 unlock_new_inode(ip
);
318 d_instantiate(dentry
, ip
);
319 unlock_new_inode(ip
);
323 free_UCSname(&dname
);
328 jfs_info("jfs_mkdir: rc:%d", rc
);
333 * NAME: jfs_rmdir(dip, dentry)
335 * FUNCTION: remove a link to child directory
337 * PARAMETER: dip - parent inode
338 * dentry - child directory dentry
340 * RETURN: -EINVAL - if name is . or ..
341 * -EINVAL - if . or .. exist but are invalid.
342 * errors from subroutines
345 * if other threads have the directory open when the last link
346 * is removed, the "." and ".." entries, if present, are removed before
347 * rmdir() returns and no new entries may be created in the directory,
348 * but the directory is not removed until the last reference to
349 * the directory is released (cf.unlink() of regular file).
351 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
354 tid_t tid
; /* transaction id */
355 struct inode
*ip
= dentry
->d_inode
;
357 struct component_name dname
;
358 struct inode
*iplist
[2];
361 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
363 /* Init inode for quota operations. */
364 dquot_initialize(dip
);
365 dquot_initialize(ip
);
367 /* directory must be empty to be removed */
373 if ((rc
= get_UCSname(&dname
, dentry
))) {
377 tid
= txBegin(dip
->i_sb
, 0);
379 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
380 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
385 tblk
= tid_to_tblock(tid
);
386 tblk
->xflag
|= COMMIT_DELETE
;
390 * delete the entry of target directory from parent directory
393 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
394 jfs_err("jfs_rmdir: dtDelete returned %d", rc
);
398 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
399 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
404 /* update parent directory's link count corresponding
405 * to ".." entry of the target directory deleted
407 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
408 inode_dec_link_count(dip
);
411 * OS/2 could have created EA and/or ACL
413 /* free EA from both persistent and working map */
414 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
416 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
418 JFS_IP(ip
)->ea
.flag
= 0;
420 /* free ACL from both persistent and working map */
421 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
423 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
425 JFS_IP(ip
)->acl
.flag
= 0;
427 /* mark the target directory as deleted */
429 mark_inode_dirty(ip
);
431 rc
= txCommit(tid
, 2, &iplist
[0], 0);
435 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
436 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
439 * Truncating the directory index table is not guaranteed. It
440 * may need to be done iteratively
442 if (test_cflag(COMMIT_Stale
, dip
)) {
444 jfs_truncate_nolock(dip
, 0);
446 clear_cflag(COMMIT_Stale
, dip
);
450 free_UCSname(&dname
);
453 jfs_info("jfs_rmdir: rc:%d", rc
);
458 * NAME: jfs_unlink(dip, dentry)
460 * FUNCTION: remove a link to object <vp> named by <name>
461 * from parent directory <dvp>
463 * PARAMETER: dip - inode of parent directory
464 * dentry - dentry of object to be removed
466 * RETURN: errors from subroutines
469 * temporary file: if one or more processes have the file open
470 * when the last link is removed, the link will be removed before
471 * unlink() returns, but the removal of the file contents will be
472 * postponed until all references to the files are closed.
474 * JFS does NOT support unlink() on directories.
477 static int jfs_unlink(struct inode
*dip
, struct dentry
*dentry
)
480 tid_t tid
; /* transaction id */
481 struct inode
*ip
= dentry
->d_inode
;
483 struct component_name dname
; /* object name */
484 struct inode
*iplist
[2];
489 jfs_info("jfs_unlink: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
491 /* Init inode for quota operations. */
492 dquot_initialize(dip
);
493 dquot_initialize(ip
);
495 if ((rc
= get_UCSname(&dname
, dentry
)))
498 IWRITE_LOCK(ip
, RDWRLOCK_NORMAL
);
500 tid
= txBegin(dip
->i_sb
, 0);
502 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
503 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
509 * delete the entry of target file from parent directory
512 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
513 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
515 txAbort(tid
, 1); /* Marks FS Dirty */
517 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
518 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
525 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
526 mark_inode_dirty(dip
);
528 /* update target's inode */
529 inode_dec_link_count(ip
);
532 * commit zero link count object
534 if (ip
->i_nlink
== 0) {
535 assert(!test_cflag(COMMIT_Nolink
, ip
));
536 /* free block resources */
537 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
538 txAbort(tid
, 1); /* Marks FS Dirty */
540 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
541 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
546 tblk
= tid_to_tblock(tid
);
547 tblk
->xflag
|= COMMIT_DELETE
;
552 * Incomplete truncate of file data can
553 * result in timing problems unless we synchronously commit the
557 commit_flag
= COMMIT_SYNC
;
562 * If xtTruncate was incomplete, commit synchronously to avoid
563 * timing complications
565 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
569 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
570 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
572 while (new_size
&& (rc
== 0)) {
573 tid
= txBegin(dip
->i_sb
, 0);
574 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
575 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
577 txAbort(tid
, 1); /* Marks FS Dirty */
580 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
582 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
585 if (ip
->i_nlink
== 0)
586 set_cflag(COMMIT_Nolink
, ip
);
591 * Truncating the directory index table is not guaranteed. It
592 * may need to be done iteratively
594 if (test_cflag(COMMIT_Stale
, dip
)) {
596 jfs_truncate_nolock(dip
, 0);
598 clear_cflag(COMMIT_Stale
, dip
);
602 free_UCSname(&dname
);
604 jfs_info("jfs_unlink: rc:%d", rc
);
609 * NAME: commitZeroLink()
611 * FUNCTION: for non-directory, called by jfs_remove(),
612 * truncate a regular file, directory or symbolic
613 * link to zero length. return 0 if type is not
616 * if the file is currently associated with a VM segment
617 * only permanent disk and inode map resources are freed,
618 * and neither the inode nor indirect blocks are modified
619 * so that the resources can be later freed in the work
621 * if there is no VM segment on entry, the resources are
622 * freed in both work and permanent map.
623 * (? for temporary file - memory object is cached even
624 * after no reference:
625 * reference count > 0 - )
627 * PARAMETERS: cd - pointer to commit data structure.
628 * current inode is the one to truncate.
630 * RETURN: Errors from subroutines
632 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
637 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
639 filetype
= ip
->i_mode
& S_IFMT
;
644 /* fast symbolic link */
645 if (ip
->i_size
< IDATASIZE
) {
651 assert(filetype
!= S_IFDIR
);
655 set_cflag(COMMIT_Freewmap
, ip
);
657 /* mark transaction of block map update type */
658 tblk
= tid_to_tblock(tid
);
659 tblk
->xflag
|= COMMIT_PMAP
;
664 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
665 /* acquire maplock on EA to be freed from block map */
666 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
671 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
672 /* acquire maplock on EA to be freed from block map */
673 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
676 * free xtree/data (truncate to zero length):
677 * free xtree/data pages from cache if COMMIT_PWMAP,
678 * free xtree/data blocks from persistent block map, and
679 * free xtree/data blocks from working block map if COMMIT_PWMAP;
682 return xtTruncate_pmap(tid
, ip
, 0);
689 * NAME: jfs_free_zero_link()
691 * FUNCTION: for non-directory, called by iClose(),
692 * free resources of a file from cache and WORKING map
693 * for a file previously committed with zero link count
694 * while associated with a pager object,
696 * PARAMETER: ip - pointer to inode of file.
698 void jfs_free_zero_link(struct inode
*ip
)
702 jfs_info("jfs_free_zero_link: ip = 0x%p", ip
);
704 /* return if not reg or symbolic link or if size is
707 type
= ip
->i_mode
& S_IFMT
;
713 /* if its contained in inode nothing to do */
714 if (ip
->i_size
< IDATASIZE
)
724 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
725 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
726 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
727 struct maplock maplock
; /* maplock for COMMIT_WMAP */
728 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
730 /* free EA pages from cache */
731 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
733 /* free EA extent from working block map */
735 pxdlock
= (struct pxd_lock
*) & maplock
;
736 pxdlock
->flag
= mlckFREEPXD
;
737 PXDaddress(&pxdlock
->pxd
, xaddr
);
738 PXDlength(&pxdlock
->pxd
, xlen
);
739 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
745 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
746 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
747 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
748 struct maplock maplock
; /* maplock for COMMIT_WMAP */
749 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
751 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
753 /* free ACL extent from working block map */
755 pxdlock
= (struct pxd_lock
*) & maplock
;
756 pxdlock
->flag
= mlckFREEPXD
;
757 PXDaddress(&pxdlock
->pxd
, xaddr
);
758 PXDlength(&pxdlock
->pxd
, xlen
);
759 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
763 * free xtree/data (truncate to zero length):
764 * free xtree/data pages from cache, and
765 * free xtree/data blocks from working block map;
768 xtTruncate(0, ip
, 0, COMMIT_WMAP
);
772 * NAME: jfs_link(vp, dvp, name, crp)
774 * FUNCTION: create a link to <vp> by the name = <name>
775 * in the parent directory <dvp>
777 * PARAMETER: vp - target object
778 * dvp - parent directory of new link
779 * name - name of new link to target object
782 * RETURN: Errors from subroutines
785 * JFS does NOT support link() on directories (to prevent circular
786 * path in the directory hierarchy);
787 * EPERM: the target object is a directory, and either the caller
788 * does not have appropriate privileges or the implementation prohibits
789 * using link() on directories [XPG4.2].
791 * JFS does NOT support links between file systems:
792 * EXDEV: target object and new link are on different file systems and
793 * implementation does not support links between file systems [XPG4.2].
795 static int jfs_link(struct dentry
*old_dentry
,
796 struct inode
*dir
, struct dentry
*dentry
)
800 struct inode
*ip
= old_dentry
->d_inode
;
802 struct component_name dname
;
803 struct btstack btstack
;
804 struct inode
*iplist
[2];
806 jfs_info("jfs_link: %s %s", old_dentry
->d_name
.name
,
807 dentry
->d_name
.name
);
809 if (ip
->i_nlink
== JFS_LINK_MAX
)
812 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
;
838 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
839 mark_inode_dirty(dir
);
844 rc
= txCommit(tid
, 2, &iplist
[0], 0);
847 ip
->i_nlink
--; /* 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
);
861 jfs_info("jfs_link: rc:%d", rc
);
866 * NAME: jfs_symlink(dip, dentry, name)
868 * FUNCTION: creates a symbolic link to <symlink> by name <name>
871 * PARAMETER: dip - parent directory vnode
872 * dentry - dentry of symbolic link
873 * name - the path name of the existing object
874 * that will be the source of the link
876 * RETURN: errors from subroutines
879 * ENAMETOOLONG: pathname resolution of a symbolic link produced
880 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
883 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
889 struct component_name dname
;
890 int ssize
; /* source pathname size */
891 struct btstack btstack
;
892 struct inode
*ip
= dentry
->d_inode
;
893 unchar
*i_fastsymlink
;
895 int bmask
= 0, xsize
;
896 s64 extent
= 0, xaddr
;
898 struct super_block
*sb
;
901 struct inode
*iplist
[2];
903 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
905 dquot_initialize(dip
);
907 ssize
= strlen(name
) + 1;
910 * search parent directory for entry/freespace
911 * (dtSearch() returns parent directory page pinned)
914 if ((rc
= get_UCSname(&dname
, dentry
)))
918 * allocate on-disk/in-memory inode for symbolic link:
919 * (iAlloc() returns new, locked inode)
921 ip
= ialloc(dip
, S_IFLNK
| 0777);
927 tid
= txBegin(dip
->i_sb
, 0);
929 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
930 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
932 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
936 tblk
= tid_to_tblock(tid
);
937 tblk
->xflag
|= COMMIT_CREATE
;
938 tblk
->ino
= ip
->i_ino
;
939 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
941 /* fix symlink access permission
942 * (dir_create() ANDs in the u.u_cmask,
943 * but symlinks really need to be 777 access)
948 * write symbolic link target path name
953 * write source path name inline in on-disk inode (fast symbolic link)
956 if (ssize
<= IDATASIZE
) {
957 ip
->i_op
= &jfs_fast_symlink_inode_operations
;
959 i_fastsymlink
= JFS_IP(ip
)->i_inline
;
960 memcpy(i_fastsymlink
, name
, ssize
);
961 ip
->i_size
= ssize
- 1;
964 * if symlink is > 128 bytes, we don't have the space to
965 * store inline extended attributes
967 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
968 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
970 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
974 * write source path name in a single extent
977 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
979 ip
->i_op
= &jfs_symlink_inode_operations
;
980 ip
->i_mapping
->a_ops
= &jfs_aops
;
983 * even though the data of symlink object (source
984 * path name) is treated as non-journaled user data,
985 * it is read/written thru buffer cache for performance.
988 bmask
= JFS_SBI(sb
)->bsize
- 1;
989 xsize
= (ssize
+ bmask
) & ~bmask
;
991 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
992 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
997 ip
->i_size
= ssize
- 1;
999 /* This is kind of silly since PATH_MAX == 4K */
1000 int copy_size
= min(ssize
, PSIZE
);
1002 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
1005 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1010 memcpy(mp
->data
, name
, copy_size
);
1014 xaddr
+= JFS_SBI(sb
)->nbperpage
;
1019 * create entry for symbolic link in parent directory
1021 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
1024 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
1028 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1030 /* discard new inode */
1034 mark_inode_dirty(ip
);
1036 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
1037 mark_inode_dirty(dip
);
1039 * commit update of parent directory and link object
1044 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1048 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1049 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
1053 unlock_new_inode(ip
);
1056 d_instantiate(dentry
, ip
);
1057 unlock_new_inode(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
)
1077 struct btstack btstack
;
1079 struct component_name new_dname
;
1080 struct inode
*new_ip
;
1081 struct component_name old_dname
;
1082 struct inode
*old_ip
;
1086 struct dt_lock
*dtlck
;
1089 struct inode
*iplist
[4];
1090 struct tblock
*tblk
;
1095 jfs_info("jfs_rename: %s %s", old_dentry
->d_name
.name
,
1096 new_dentry
->d_name
.name
);
1098 dquot_initialize(old_dir
);
1099 dquot_initialize(new_dir
);
1101 old_ip
= old_dentry
->d_inode
;
1102 new_ip
= new_dentry
->d_inode
;
1104 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1107 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1111 * Make sure source inode number is what we think it is
1113 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1114 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1120 * Make sure dest inode number (if any) is what we think it is
1122 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1124 if ((!new_ip
) || (ino
!= new_ip
->i_ino
)) {
1128 } else if (rc
!= -ENOENT
)
1131 /* no entry exists, but one was expected */
1136 if (S_ISDIR(old_ip
->i_mode
)) {
1138 if (!dtEmpty(new_ip
)) {
1142 } else if ((new_dir
!= old_dir
) &&
1143 (new_dir
->i_nlink
== JFS_LINK_MAX
)) {
1147 } else if (new_ip
) {
1148 IWRITE_LOCK(new_ip
, RDWRLOCK_NORMAL
);
1149 /* Init inode for quota operations. */
1150 dquot_initialize(new_ip
);
1154 * The real work starts here
1156 tid
= txBegin(new_dir
->i_sb
, 0);
1159 * How do we know the locking is safe from deadlocks?
1160 * The vfs does the hard part for us. Any time we are taking nested
1161 * commit_mutexes, the vfs already has i_mutex held on the parent.
1162 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1164 mutex_lock_nested(&JFS_IP(new_dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1165 mutex_lock_nested(&JFS_IP(old_ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1166 if (old_dir
!= new_dir
)
1167 mutex_lock_nested(&JFS_IP(old_dir
)->commit_mutex
,
1168 COMMIT_MUTEX_SECOND_PARENT
);
1171 mutex_lock_nested(&JFS_IP(new_ip
)->commit_mutex
,
1172 COMMIT_MUTEX_VICTIM
);
1174 * Change existing directory entry to new inode number
1176 ino
= new_ip
->i_ino
;
1177 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1178 old_ip
->i_ino
, JFS_RENAME
);
1182 if (S_ISDIR(new_ip
->i_mode
)) {
1184 if (new_ip
->i_nlink
) {
1185 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1186 if (old_dir
!= new_dir
)
1187 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1188 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1189 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1190 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1191 IWRITE_UNLOCK(new_ip
);
1192 jfs_error(new_ip
->i_sb
,
1193 "jfs_rename: new_ip->i_nlink != 0");
1196 tblk
= tid_to_tblock(tid
);
1197 tblk
->xflag
|= COMMIT_DELETE
;
1198 tblk
->u
.ip
= new_ip
;
1199 } else if (new_ip
->i_nlink
== 0) {
1200 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1201 /* free block resources */
1202 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1203 txAbort(tid
, 1); /* Marks FS Dirty */
1207 tblk
= tid_to_tblock(tid
);
1208 tblk
->xflag
|= COMMIT_DELETE
;
1209 tblk
->u
.ip
= new_ip
;
1211 new_ip
->i_ctime
= CURRENT_TIME
;
1212 mark_inode_dirty(new_ip
);
1216 * Add new directory entry
1218 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1221 jfs_err("jfs_rename didn't expect dtSearch to fail "
1226 ino
= old_ip
->i_ino
;
1227 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1230 jfs_err("jfs_rename: dtInsert returned -EIO");
1233 if (S_ISDIR(old_ip
->i_mode
))
1237 * Remove old directory entry
1240 ino
= old_ip
->i_ino
;
1241 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1243 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1245 txAbort(tid
, 1); /* Marks Filesystem dirty */
1248 if (S_ISDIR(old_ip
->i_mode
)) {
1249 drop_nlink(old_dir
);
1250 if (old_dir
!= new_dir
) {
1252 * Change inode number of parent for moved directory
1255 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1256 cpu_to_le32(new_dir
->i_ino
);
1258 /* Linelock header of dtree */
1259 tlck
= txLock(tid
, old_ip
,
1260 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1261 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1262 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1263 ASSERT(dtlck
->index
== 0);
1264 lv
= & dtlck
->lv
[0];
1272 * Update ctime on changed/moved inodes & mark dirty
1274 old_ip
->i_ctime
= CURRENT_TIME
;
1275 mark_inode_dirty(old_ip
);
1277 new_dir
->i_ctime
= new_dir
->i_mtime
= current_fs_time(new_dir
->i_sb
);
1278 mark_inode_dirty(new_dir
);
1280 /* Build list of inodes modified by this transaction */
1282 iplist
[ipcount
++] = old_ip
;
1284 iplist
[ipcount
++] = new_ip
;
1285 iplist
[ipcount
++] = old_dir
;
1287 if (old_dir
!= new_dir
) {
1288 iplist
[ipcount
++] = new_dir
;
1289 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1290 mark_inode_dirty(old_dir
);
1294 * Incomplete truncate of file data can
1295 * result in timing problems unless we synchronously commit the
1299 commit_flag
= COMMIT_SYNC
;
1303 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1308 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1309 if (old_dir
!= new_dir
)
1310 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1311 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1312 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1314 while (new_size
&& (rc
== 0)) {
1315 tid
= txBegin(new_ip
->i_sb
, 0);
1316 mutex_lock(&JFS_IP(new_ip
)->commit_mutex
);
1317 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1322 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1324 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1326 if (new_ip
&& (new_ip
->i_nlink
== 0))
1327 set_cflag(COMMIT_Nolink
, new_ip
);
1329 free_UCSname(&new_dname
);
1331 free_UCSname(&old_dname
);
1333 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1334 IWRITE_UNLOCK(new_ip
);
1336 * Truncating the directory index table is not guaranteed. It
1337 * may need to be done iteratively
1339 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1340 if (old_dir
->i_size
> 1)
1341 jfs_truncate_nolock(old_dir
, 0);
1343 clear_cflag(COMMIT_Stale
, old_dir
);
1346 jfs_info("jfs_rename: returning %d", rc
);
1354 * FUNCTION: Create a special file (device)
1356 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1357 int mode
, dev_t rdev
)
1359 struct jfs_inode_info
*jfs_ip
;
1360 struct btstack btstack
;
1361 struct component_name dname
;
1364 struct inode
*iplist
[2];
1367 struct tblock
*tblk
;
1369 if (!new_valid_dev(rdev
))
1372 jfs_info("jfs_mknod: %s", dentry
->d_name
.name
);
1374 dquot_initialize(dir
);
1376 if ((rc
= get_UCSname(&dname
, dentry
)))
1379 ip
= ialloc(dir
, mode
);
1384 jfs_ip
= JFS_IP(ip
);
1386 tid
= txBegin(dir
->i_sb
, 0);
1388 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1389 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1391 rc
= jfs_init_acl(tid
, ip
, dir
);
1395 rc
= jfs_init_security(tid
, ip
, dir
, &dentry
->d_name
);
1401 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
1406 tblk
= tid_to_tblock(tid
);
1407 tblk
->xflag
|= COMMIT_CREATE
;
1408 tblk
->ino
= ip
->i_ino
;
1409 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1412 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
))) {
1417 ip
->i_op
= &jfs_file_inode_operations
;
1418 jfs_ip
->dev
= new_encode_dev(rdev
);
1419 init_special_inode(ip
, ip
->i_mode
, rdev
);
1421 mark_inode_dirty(ip
);
1423 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
1425 mark_inode_dirty(dir
);
1429 rc
= txCommit(tid
, 2, iplist
, 0);
1433 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1434 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
1438 unlock_new_inode(ip
);
1441 d_instantiate(dentry
, ip
);
1442 unlock_new_inode(ip
);
1446 free_UCSname(&dname
);
1449 jfs_info("jfs_mknod: returning %d", rc
);
1453 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
1455 struct btstack btstack
;
1458 struct component_name key
;
1459 const char *name
= dentry
->d_name
.name
;
1460 int len
= dentry
->d_name
.len
;
1463 jfs_info("jfs_lookup: name = %s", name
);
1465 if ((name
[0] == '.') && (len
== 1))
1467 else if (strcmp(name
, "..") == 0)
1470 if ((rc
= get_UCSname(&key
, dentry
)))
1472 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1474 if (rc
== -ENOENT
) {
1475 d_add(dentry
, NULL
);
1478 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1483 ip
= jfs_iget(dip
->i_sb
, inum
);
1485 jfs_err("jfs_lookup: iget failed on inum %d", (uint
) inum
);
1486 return ERR_CAST(ip
);
1489 return d_splice_alias(ip
, dentry
);
1492 static struct inode
*jfs_nfs_get_inode(struct super_block
*sb
,
1493 u64 ino
, u32 generation
)
1495 struct inode
*inode
;
1498 return ERR_PTR(-ESTALE
);
1499 inode
= jfs_iget(sb
, ino
);
1501 return ERR_CAST(inode
);
1503 if (generation
&& inode
->i_generation
!= generation
) {
1505 return ERR_PTR(-ESTALE
);
1511 struct dentry
*jfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
1512 int fh_len
, int fh_type
)
1514 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
1518 struct dentry
*jfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
1519 int fh_len
, int fh_type
)
1521 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
1525 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1527 unsigned long parent_ino
;
1530 le32_to_cpu(JFS_IP(dentry
->d_inode
)->i_dtroot
.header
.idotdot
);
1532 return d_obtain_alias(jfs_iget(dentry
->d_inode
->i_sb
, parent_ino
));
1535 const struct inode_operations jfs_dir_inode_operations
= {
1536 .create
= jfs_create
,
1537 .lookup
= jfs_lookup
,
1539 .unlink
= jfs_unlink
,
1540 .symlink
= jfs_symlink
,
1544 .rename
= jfs_rename
,
1545 .setxattr
= jfs_setxattr
,
1546 .getxattr
= jfs_getxattr
,
1547 .listxattr
= jfs_listxattr
,
1548 .removexattr
= jfs_removexattr
,
1549 .setattr
= jfs_setattr
,
1550 #ifdef CONFIG_JFS_POSIX_ACL
1551 .check_acl
= jfs_check_acl
,
1555 const struct file_operations jfs_dir_operations
= {
1556 .read
= generic_read_dir
,
1557 .readdir
= jfs_readdir
,
1559 .unlocked_ioctl
= jfs_ioctl
,
1560 #ifdef CONFIG_COMPAT
1561 .compat_ioctl
= jfs_compat_ioctl
,
1563 .llseek
= generic_file_llseek
,
1566 static int jfs_ci_hash(const struct dentry
*dir
, const struct inode
*inode
,
1572 hash
= init_name_hash();
1573 for (i
=0; i
< this->len
; i
++)
1574 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1575 this->hash
= end_name_hash(hash
);
1580 static int jfs_ci_compare(const struct dentry
*parent
,
1581 const struct inode
*pinode
,
1582 const struct dentry
*dentry
, const struct inode
*inode
,
1583 unsigned int len
, const char *str
, const struct qstr
*name
)
1587 if (len
!= name
->len
)
1589 for (i
=0; i
< len
; i
++) {
1590 if (tolower(str
[i
]) != tolower(name
->name
[i
]))
1598 static int jfs_ci_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1600 if (nd
&& nd
->flags
& LOOKUP_RCU
)
1603 * This is not negative dentry. Always valid.
1605 * Note, rename() to existing directory entry will have ->d_inode,
1606 * and will use existing name which isn't specified name by user.
1608 * We may be able to drop this positive dentry here. But dropping
1609 * positive dentry isn't good idea. So it's unsupported like
1610 * rename("filename", "FILENAME") for now.
1612 if (dentry
->d_inode
)
1616 * This may be nfsd (or something), anyway, we can't see the
1617 * intent of this. So, since this can be for creation, drop it.
1623 * Drop the negative dentry, in order to make sure to use the
1624 * case sensitive name which is specified by user if this is
1627 if (!(nd
->flags
& (LOOKUP_CONTINUE
| LOOKUP_PARENT
))) {
1628 if (nd
->flags
& (LOOKUP_CREATE
| LOOKUP_RENAME_TARGET
))
1634 const struct dentry_operations jfs_ci_dentry_operations
=
1636 .d_hash
= jfs_ci_hash
,
1637 .d_compare
= jfs_ci_compare
,
1638 .d_revalidate
= jfs_ci_revalidate
,