2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/namei.h>
22 #include <linux/ctype.h>
23 #include <linux/quotaops.h>
24 #include <linux/exportfs.h>
25 #include "jfs_incore.h"
26 #include "jfs_superblock.h"
27 #include "jfs_inode.h"
28 #include "jfs_dinode.h"
30 #include "jfs_unicode.h"
31 #include "jfs_metapage.h"
32 #include "jfs_xattr.h"
34 #include "jfs_debug.h"
39 const struct dentry_operations jfs_ci_dentry_operations
;
41 static s64
commitZeroLink(tid_t
, struct inode
*);
44 * NAME: free_ea_wmap(inode)
46 * FUNCTION: free uncommitted extended attributes from working map
49 static inline void free_ea_wmap(struct inode
*inode
)
51 dxd_t
*ea
= &JFS_IP(inode
)->ea
;
53 if (ea
->flag
& DXD_EXTENT
) {
54 /* free EA pages from cache */
55 invalidate_dxd_metapages(inode
, *ea
);
56 dbFree(inode
, addressDXD(ea
), lengthDXD(ea
));
62 * NAME: jfs_create(dip, dentry, mode)
64 * FUNCTION: create a regular file in the parent directory <dip>
65 * with name = <from dentry> and mode = <mode>
67 * PARAMETER: dip - parent directory vnode
68 * dentry - dentry of new file
69 * mode - create mode (rwxrwxrwx).
72 * RETURN: Errors from subroutines
75 static int jfs_create(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
,
79 tid_t tid
; /* transaction id */
80 struct inode
*ip
= NULL
; /* child directory inode */
82 struct component_name dname
; /* child directory name */
83 struct btstack btstack
;
84 struct inode
*iplist
[2];
87 jfs_info("jfs_create: dip:0x%p name:%pd", dip
, dentry
);
89 rc
= dquot_initialize(dip
);
94 * search parent directory for entry/freespace
95 * (dtSearch() returns parent directory page pinned)
97 if ((rc
= get_UCSname(&dname
, dentry
)))
101 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
102 * block there while holding dtree page, so we allocate the inode &
103 * begin the transaction before we search the directory.
105 ip
= ialloc(dip
, mode
);
111 tid
= txBegin(dip
->i_sb
, 0);
113 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
114 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
116 rc
= jfs_init_acl(tid
, ip
, dip
);
120 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
126 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
127 jfs_err("jfs_create: dtSearch returned %d", rc
);
132 tblk
= tid_to_tblock(tid
);
133 tblk
->xflag
|= COMMIT_CREATE
;
134 tblk
->ino
= ip
->i_ino
;
135 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
141 * initialize the child XAD tree root in-line in inode
146 * create entry in parent directory for child directory
147 * (dtInsert() releases parent directory page)
150 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
152 jfs_err("jfs_create: dtInsert returned -EIO");
153 txAbort(tid
, 1); /* Marks Filesystem dirty */
155 txAbort(tid
, 0); /* Filesystem full */
159 ip
->i_op
= &jfs_file_inode_operations
;
160 ip
->i_fop
= &jfs_file_operations
;
161 ip
->i_mapping
->a_ops
= &jfs_aops
;
163 mark_inode_dirty(ip
);
165 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
167 mark_inode_dirty(dip
);
169 rc
= txCommit(tid
, 2, &iplist
[0], 0);
173 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
174 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
178 unlock_new_inode(ip
);
181 d_instantiate_new(dentry
, ip
);
185 free_UCSname(&dname
);
189 jfs_info("jfs_create: rc:%d", rc
);
195 * NAME: jfs_mkdir(dip, dentry, mode)
197 * FUNCTION: create a child directory in the parent directory <dip>
198 * with name = <from dentry> and mode = <mode>
200 * PARAMETER: dip - parent directory vnode
201 * dentry - dentry of child directory
202 * mode - create mode (rwxrwxrwx).
204 * RETURN: Errors from subroutines
207 * EACCESS: user needs search+write permission on the parent directory
209 static int jfs_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
212 tid_t tid
; /* transaction id */
213 struct inode
*ip
= NULL
; /* child directory inode */
215 struct component_name dname
; /* child directory name */
216 struct btstack btstack
;
217 struct inode
*iplist
[2];
220 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip
, dentry
);
222 rc
= dquot_initialize(dip
);
227 * search parent directory for entry/freespace
228 * (dtSearch() returns parent directory page pinned)
230 if ((rc
= get_UCSname(&dname
, dentry
)))
234 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
235 * block there while holding dtree page, so we allocate the inode &
236 * begin the transaction before we search the directory.
238 ip
= ialloc(dip
, S_IFDIR
| mode
);
244 tid
= txBegin(dip
->i_sb
, 0);
246 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
247 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
249 rc
= jfs_init_acl(tid
, ip
, dip
);
253 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
259 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
260 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
265 tblk
= tid_to_tblock(tid
);
266 tblk
->xflag
|= COMMIT_CREATE
;
267 tblk
->ino
= ip
->i_ino
;
268 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
274 * initialize the child directory in-line in inode
276 dtInitRoot(tid
, ip
, dip
->i_ino
);
279 * create entry in parent directory for child directory
280 * (dtInsert() releases parent directory page)
283 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
285 jfs_err("jfs_mkdir: dtInsert returned -EIO");
286 txAbort(tid
, 1); /* Marks Filesystem dirty */
288 txAbort(tid
, 0); /* Filesystem full */
292 set_nlink(ip
, 2); /* for '.' */
293 ip
->i_op
= &jfs_dir_inode_operations
;
294 ip
->i_fop
= &jfs_dir_operations
;
296 mark_inode_dirty(ip
);
298 /* update parent directory inode */
299 inc_nlink(dip
); /* for '..' from child directory */
300 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
301 mark_inode_dirty(dip
);
303 rc
= txCommit(tid
, 2, &iplist
[0], 0);
307 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
308 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
312 unlock_new_inode(ip
);
315 d_instantiate_new(dentry
, ip
);
319 free_UCSname(&dname
);
324 jfs_info("jfs_mkdir: rc:%d", rc
);
329 * NAME: jfs_rmdir(dip, dentry)
331 * FUNCTION: remove a link to child directory
333 * PARAMETER: dip - parent inode
334 * dentry - child directory dentry
336 * RETURN: -EINVAL - if name is . or ..
337 * -EINVAL - if . or .. exist but are invalid.
338 * errors from subroutines
341 * if other threads have the directory open when the last link
342 * is removed, the "." and ".." entries, if present, are removed before
343 * rmdir() returns and no new entries may be created in the directory,
344 * but the directory is not removed until the last reference to
345 * the directory is released (cf.unlink() of regular file).
347 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
350 tid_t tid
; /* transaction id */
351 struct inode
*ip
= d_inode(dentry
);
353 struct component_name dname
;
354 struct inode
*iplist
[2];
357 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip
, dentry
);
359 /* Init inode for quota operations. */
360 rc
= dquot_initialize(dip
);
363 rc
= 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
= d_inode(dentry
);
483 struct component_name dname
; /* object name */
484 struct inode
*iplist
[2];
489 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip
, dentry
);
491 /* Init inode for quota operations. */
492 rc
= dquot_initialize(dip
);
495 rc
= dquot_initialize(ip
);
499 if ((rc
= get_UCSname(&dname
, dentry
)))
502 IWRITE_LOCK(ip
, RDWRLOCK_NORMAL
);
504 tid
= txBegin(dip
->i_sb
, 0);
506 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
507 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
513 * delete the entry of target file from parent directory
516 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
517 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
519 txAbort(tid
, 1); /* Marks FS Dirty */
521 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
522 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
529 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
530 mark_inode_dirty(dip
);
532 /* update target's inode */
533 inode_dec_link_count(ip
);
536 * commit zero link count object
538 if (ip
->i_nlink
== 0) {
539 assert(!test_cflag(COMMIT_Nolink
, ip
));
540 /* free block resources */
541 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
542 txAbort(tid
, 1); /* Marks FS Dirty */
544 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
545 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
550 tblk
= tid_to_tblock(tid
);
551 tblk
->xflag
|= COMMIT_DELETE
;
556 * Incomplete truncate of file data can
557 * result in timing problems unless we synchronously commit the
561 commit_flag
= COMMIT_SYNC
;
566 * If xtTruncate was incomplete, commit synchronously to avoid
567 * timing complications
569 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
573 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
574 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
576 while (new_size
&& (rc
== 0)) {
577 tid
= txBegin(dip
->i_sb
, 0);
578 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
579 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
581 txAbort(tid
, 1); /* Marks FS Dirty */
584 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
586 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
589 if (ip
->i_nlink
== 0)
590 set_cflag(COMMIT_Nolink
, ip
);
595 * Truncating the directory index table is not guaranteed. It
596 * may need to be done iteratively
598 if (test_cflag(COMMIT_Stale
, dip
)) {
600 jfs_truncate_nolock(dip
, 0);
602 clear_cflag(COMMIT_Stale
, dip
);
606 free_UCSname(&dname
);
608 jfs_info("jfs_unlink: rc:%d", rc
);
613 * NAME: commitZeroLink()
615 * FUNCTION: for non-directory, called by jfs_remove(),
616 * truncate a regular file, directory or symbolic
617 * link to zero length. return 0 if type is not
620 * if the file is currently associated with a VM segment
621 * only permanent disk and inode map resources are freed,
622 * and neither the inode nor indirect blocks are modified
623 * so that the resources can be later freed in the work
625 * if there is no VM segment on entry, the resources are
626 * freed in both work and permanent map.
627 * (? for temporary file - memory object is cached even
628 * after no reference:
629 * reference count > 0 - )
631 * PARAMETERS: cd - pointer to commit data structure.
632 * current inode is the one to truncate.
634 * RETURN: Errors from subroutines
636 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
641 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
643 filetype
= ip
->i_mode
& S_IFMT
;
648 /* fast symbolic link */
649 if (ip
->i_size
< IDATASIZE
) {
655 assert(filetype
!= S_IFDIR
);
659 set_cflag(COMMIT_Freewmap
, ip
);
661 /* mark transaction of block map update type */
662 tblk
= tid_to_tblock(tid
);
663 tblk
->xflag
|= COMMIT_PMAP
;
668 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
669 /* acquire maplock on EA to be freed from block map */
670 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
675 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
676 /* acquire maplock on EA to be freed from block map */
677 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
680 * free xtree/data (truncate to zero length):
681 * free xtree/data pages from cache if COMMIT_PWMAP,
682 * free xtree/data blocks from persistent block map, and
683 * free xtree/data blocks from working block map if COMMIT_PWMAP;
686 return xtTruncate_pmap(tid
, ip
, 0);
693 * NAME: jfs_free_zero_link()
695 * FUNCTION: for non-directory, called by iClose(),
696 * free resources of a file from cache and WORKING map
697 * for a file previously committed with zero link count
698 * while associated with a pager object,
700 * PARAMETER: ip - pointer to inode of file.
702 void jfs_free_zero_link(struct inode
*ip
)
706 jfs_info("jfs_free_zero_link: ip = 0x%p", ip
);
708 /* return if not reg or symbolic link or if size is
711 type
= ip
->i_mode
& S_IFMT
;
717 /* if its contained in inode nothing to do */
718 if (ip
->i_size
< IDATASIZE
)
728 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
729 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
730 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
731 struct maplock maplock
; /* maplock for COMMIT_WMAP */
732 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
734 /* free EA pages from cache */
735 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
737 /* free EA extent from working block map */
739 pxdlock
= (struct pxd_lock
*) & maplock
;
740 pxdlock
->flag
= mlckFREEPXD
;
741 PXDaddress(&pxdlock
->pxd
, xaddr
);
742 PXDlength(&pxdlock
->pxd
, xlen
);
743 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
749 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
750 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
751 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
752 struct maplock maplock
; /* maplock for COMMIT_WMAP */
753 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
755 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
757 /* free ACL extent from working block map */
759 pxdlock
= (struct pxd_lock
*) & maplock
;
760 pxdlock
->flag
= mlckFREEPXD
;
761 PXDaddress(&pxdlock
->pxd
, xaddr
);
762 PXDlength(&pxdlock
->pxd
, xlen
);
763 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
767 * free xtree/data (truncate to zero length):
768 * free xtree/data pages from cache, and
769 * free xtree/data blocks from working block map;
772 xtTruncate(0, ip
, 0, COMMIT_WMAP
);
776 * NAME: jfs_link(vp, dvp, name, crp)
778 * FUNCTION: create a link to <vp> by the name = <name>
779 * in the parent directory <dvp>
781 * PARAMETER: vp - target object
782 * dvp - parent directory of new link
783 * name - name of new link to target object
786 * RETURN: Errors from subroutines
789 * JFS does NOT support link() on directories (to prevent circular
790 * path in the directory hierarchy);
791 * EPERM: the target object is a directory, and either the caller
792 * does not have appropriate privileges or the implementation prohibits
793 * using link() on directories [XPG4.2].
795 * JFS does NOT support links between file systems:
796 * EXDEV: target object and new link are on different file systems and
797 * implementation does not support links between file systems [XPG4.2].
799 static int jfs_link(struct dentry
*old_dentry
,
800 struct inode
*dir
, struct dentry
*dentry
)
804 struct inode
*ip
= d_inode(old_dentry
);
806 struct component_name dname
;
807 struct btstack btstack
;
808 struct inode
*iplist
[2];
810 jfs_info("jfs_link: %pd %pd", old_dentry
, dentry
);
812 rc
= dquot_initialize(dir
);
816 tid
= txBegin(ip
->i_sb
, 0);
818 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
819 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
822 * scan parent directory for entry/freespace
824 if ((rc
= get_UCSname(&dname
, dentry
)))
827 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
831 * create entry for new link in parent directory
834 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
837 /* update object inode */
838 inc_nlink(ip
); /* for new link */
839 ip
->i_ctime
= CURRENT_TIME
;
840 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
841 mark_inode_dirty(dir
);
846 rc
= txCommit(tid
, 2, &iplist
[0], 0);
849 drop_nlink(ip
); /* never instantiated */
852 d_instantiate(dentry
, ip
);
855 free_UCSname(&dname
);
860 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
861 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
864 jfs_info("jfs_link: rc:%d", rc
);
869 * NAME: jfs_symlink(dip, dentry, name)
871 * FUNCTION: creates a symbolic link to <symlink> by name <name>
874 * PARAMETER: dip - parent directory vnode
875 * dentry - dentry of symbolic link
876 * name - the path name of the existing object
877 * that will be the source of the link
879 * RETURN: errors from subroutines
882 * ENAMETOOLONG: pathname resolution of a symbolic link produced
883 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
886 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
892 struct component_name dname
;
893 int ssize
; /* source pathname size */
894 struct btstack btstack
;
895 struct inode
*ip
= d_inode(dentry
);
897 int bmask
= 0, xsize
;
900 struct super_block
*sb
;
903 struct inode
*iplist
[2];
905 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
907 rc
= dquot_initialize(dip
);
911 ssize
= strlen(name
) + 1;
914 * search parent directory for entry/freespace
915 * (dtSearch() returns parent directory page pinned)
918 if ((rc
= get_UCSname(&dname
, dentry
)))
922 * allocate on-disk/in-memory inode for symbolic link:
923 * (iAlloc() returns new, locked inode)
925 ip
= ialloc(dip
, S_IFLNK
| 0777);
931 tid
= txBegin(dip
->i_sb
, 0);
933 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
934 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
936 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
940 tblk
= tid_to_tblock(tid
);
941 tblk
->xflag
|= COMMIT_CREATE
;
942 tblk
->ino
= ip
->i_ino
;
943 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
945 /* fix symlink access permission
946 * (dir_create() ANDs in the u.u_cmask,
947 * but symlinks really need to be 777 access)
952 * write symbolic link target path name
957 * write source path name inline in on-disk inode (fast symbolic link)
960 if (ssize
<= IDATASIZE
) {
961 ip
->i_op
= &jfs_fast_symlink_inode_operations
;
963 ip
->i_link
= JFS_IP(ip
)->i_inline
;
964 memcpy(ip
->i_link
, name
, ssize
);
965 ip
->i_size
= ssize
- 1;
968 * if symlink is > 128 bytes, we don't have the space to
969 * store inline extended attributes
971 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
972 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
974 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
978 * write source path name in a single extent
981 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
983 ip
->i_op
= &jfs_symlink_inode_operations
;
984 ip
->i_mapping
->a_ops
= &jfs_aops
;
987 * even though the data of symlink object (source
988 * path name) is treated as non-journaled user data,
989 * it is read/written thru buffer cache for performance.
992 bmask
= JFS_SBI(sb
)->bsize
- 1;
993 xsize
= (ssize
+ bmask
) & ~bmask
;
995 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
996 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
1000 ip
->i_size
= ssize
- 1;
1002 /* This is kind of silly since PATH_MAX == 4K */
1003 int copy_size
= min(ssize
, PSIZE
);
1005 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
1008 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1013 memcpy(mp
->data
, name
, copy_size
);
1017 xaddr
+= JFS_SBI(sb
)->nbperpage
;
1022 * create entry for symbolic link in parent directory
1024 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
1027 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
1031 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1033 /* discard new inode */
1037 mark_inode_dirty(ip
);
1039 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
1040 mark_inode_dirty(dip
);
1042 * commit update of parent directory and link object
1047 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1051 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1052 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
1056 unlock_new_inode(ip
);
1059 d_instantiate_new(dentry
, ip
);
1063 free_UCSname(&dname
);
1066 jfs_info("jfs_symlink: rc:%d", rc
);
1074 * FUNCTION: rename a file or directory
1076 static int jfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1077 struct inode
*new_dir
, struct dentry
*new_dentry
)
1079 struct btstack btstack
;
1081 struct component_name new_dname
;
1082 struct inode
*new_ip
;
1083 struct component_name old_dname
;
1084 struct inode
*old_ip
;
1088 struct dt_lock
*dtlck
;
1091 struct inode
*iplist
[4];
1092 struct tblock
*tblk
;
1097 jfs_info("jfs_rename: %pd %pd", old_dentry
, new_dentry
);
1099 rc
= dquot_initialize(old_dir
);
1102 rc
= dquot_initialize(new_dir
);
1106 old_ip
= d_inode(old_dentry
);
1107 new_ip
= d_inode(new_dentry
);
1109 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1112 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1116 * Make sure source inode number is what we think it is
1118 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1119 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1125 * Make sure dest inode number (if any) is what we think it is
1127 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1129 if ((!new_ip
) || (ino
!= new_ip
->i_ino
)) {
1133 } else if (rc
!= -ENOENT
)
1136 /* no entry exists, but one was expected */
1141 if (S_ISDIR(old_ip
->i_mode
)) {
1143 if (!dtEmpty(new_ip
)) {
1148 } else if (new_ip
) {
1149 IWRITE_LOCK(new_ip
, RDWRLOCK_NORMAL
);
1150 /* Init inode for quota operations. */
1151 rc
= dquot_initialize(new_ip
);
1157 * The real work starts here
1159 tid
= txBegin(new_dir
->i_sb
, 0);
1162 * How do we know the locking is safe from deadlocks?
1163 * The vfs does the hard part for us. Any time we are taking nested
1164 * commit_mutexes, the vfs already has i_mutex held on the parent.
1165 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1167 mutex_lock_nested(&JFS_IP(new_dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1168 mutex_lock_nested(&JFS_IP(old_ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1169 if (old_dir
!= new_dir
)
1170 mutex_lock_nested(&JFS_IP(old_dir
)->commit_mutex
,
1171 COMMIT_MUTEX_SECOND_PARENT
);
1174 mutex_lock_nested(&JFS_IP(new_ip
)->commit_mutex
,
1175 COMMIT_MUTEX_VICTIM
);
1177 * Change existing directory entry to new inode number
1179 ino
= new_ip
->i_ino
;
1180 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1181 old_ip
->i_ino
, JFS_RENAME
);
1185 if (S_ISDIR(new_ip
->i_mode
)) {
1187 if (new_ip
->i_nlink
) {
1188 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1189 if (old_dir
!= new_dir
)
1190 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1191 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1192 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1193 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1194 IWRITE_UNLOCK(new_ip
);
1195 jfs_error(new_ip
->i_sb
,
1196 "new_ip->i_nlink != 0\n");
1199 tblk
= tid_to_tblock(tid
);
1200 tblk
->xflag
|= COMMIT_DELETE
;
1201 tblk
->u
.ip
= new_ip
;
1202 } else if (new_ip
->i_nlink
== 0) {
1203 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1204 /* free block resources */
1205 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1206 txAbort(tid
, 1); /* Marks FS Dirty */
1210 tblk
= tid_to_tblock(tid
);
1211 tblk
->xflag
|= COMMIT_DELETE
;
1212 tblk
->u
.ip
= new_ip
;
1214 new_ip
->i_ctime
= CURRENT_TIME
;
1215 mark_inode_dirty(new_ip
);
1219 * Add new directory entry
1221 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1224 jfs_err("jfs_rename didn't expect dtSearch to fail "
1229 ino
= old_ip
->i_ino
;
1230 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1233 jfs_err("jfs_rename: dtInsert returned -EIO");
1236 if (S_ISDIR(old_ip
->i_mode
))
1240 * Remove old directory entry
1243 ino
= old_ip
->i_ino
;
1244 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1246 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1248 txAbort(tid
, 1); /* Marks Filesystem dirty */
1251 if (S_ISDIR(old_ip
->i_mode
)) {
1252 drop_nlink(old_dir
);
1253 if (old_dir
!= new_dir
) {
1255 * Change inode number of parent for moved directory
1258 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1259 cpu_to_le32(new_dir
->i_ino
);
1261 /* Linelock header of dtree */
1262 tlck
= txLock(tid
, old_ip
,
1263 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1264 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1265 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1266 ASSERT(dtlck
->index
== 0);
1267 lv
= & dtlck
->lv
[0];
1275 * Update ctime on changed/moved inodes & mark dirty
1277 old_ip
->i_ctime
= CURRENT_TIME
;
1278 mark_inode_dirty(old_ip
);
1280 new_dir
->i_ctime
= new_dir
->i_mtime
= current_fs_time(new_dir
->i_sb
);
1281 mark_inode_dirty(new_dir
);
1283 /* Build list of inodes modified by this transaction */
1285 iplist
[ipcount
++] = old_ip
;
1287 iplist
[ipcount
++] = new_ip
;
1288 iplist
[ipcount
++] = old_dir
;
1290 if (old_dir
!= new_dir
) {
1291 iplist
[ipcount
++] = new_dir
;
1292 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1293 mark_inode_dirty(old_dir
);
1297 * Incomplete truncate of file data can
1298 * result in timing problems unless we synchronously commit the
1302 commit_flag
= COMMIT_SYNC
;
1306 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1311 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1312 if (old_dir
!= new_dir
)
1313 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1314 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1315 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1317 while (new_size
&& (rc
== 0)) {
1318 tid
= txBegin(new_ip
->i_sb
, 0);
1319 mutex_lock(&JFS_IP(new_ip
)->commit_mutex
);
1320 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1325 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1327 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1329 if (new_ip
&& (new_ip
->i_nlink
== 0))
1330 set_cflag(COMMIT_Nolink
, new_ip
);
1332 * Truncating the directory index table is not guaranteed. It
1333 * may need to be done iteratively
1335 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1336 if (old_dir
->i_size
> 1)
1337 jfs_truncate_nolock(old_dir
, 0);
1339 clear_cflag(COMMIT_Stale
, old_dir
);
1342 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1343 IWRITE_UNLOCK(new_ip
);
1345 free_UCSname(&new_dname
);
1347 free_UCSname(&old_dname
);
1349 jfs_info("jfs_rename: returning %d", rc
);
1357 * FUNCTION: Create a special file (device)
1359 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1360 umode_t mode
, dev_t rdev
)
1362 struct jfs_inode_info
*jfs_ip
;
1363 struct btstack btstack
;
1364 struct component_name dname
;
1367 struct inode
*iplist
[2];
1370 struct tblock
*tblk
;
1372 jfs_info("jfs_mknod: %pd", dentry
);
1374 rc
= dquot_initialize(dir
);
1378 if ((rc
= get_UCSname(&dname
, dentry
)))
1381 ip
= ialloc(dir
, mode
);
1386 jfs_ip
= JFS_IP(ip
);
1388 tid
= txBegin(dir
->i_sb
, 0);
1390 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1391 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1393 rc
= jfs_init_acl(tid
, ip
, dir
);
1397 rc
= jfs_init_security(tid
, ip
, dir
, &dentry
->d_name
);
1403 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
1408 tblk
= tid_to_tblock(tid
);
1409 tblk
->xflag
|= COMMIT_CREATE
;
1410 tblk
->ino
= ip
->i_ino
;
1411 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1414 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
))) {
1419 ip
->i_op
= &jfs_file_inode_operations
;
1420 jfs_ip
->dev
= new_encode_dev(rdev
);
1421 init_special_inode(ip
, ip
->i_mode
, rdev
);
1423 mark_inode_dirty(ip
);
1425 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
1427 mark_inode_dirty(dir
);
1431 rc
= txCommit(tid
, 2, iplist
, 0);
1435 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1436 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
1440 unlock_new_inode(ip
);
1443 d_instantiate_new(dentry
, ip
);
1447 free_UCSname(&dname
);
1450 jfs_info("jfs_mknod: returning %d", rc
);
1454 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
1456 struct btstack btstack
;
1459 struct component_name key
;
1462 jfs_info("jfs_lookup: name = %pd", dentry
);
1464 if ((rc
= get_UCSname(&key
, dentry
)))
1466 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1468 if (rc
== -ENOENT
) {
1471 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1474 ip
= jfs_iget(dip
->i_sb
, inum
);
1476 jfs_err("jfs_lookup: iget failed on inum %d", (uint
)inum
);
1479 return d_splice_alias(ip
, dentry
);
1482 static struct inode
*jfs_nfs_get_inode(struct super_block
*sb
,
1483 u64 ino
, u32 generation
)
1485 struct inode
*inode
;
1488 return ERR_PTR(-ESTALE
);
1489 inode
= jfs_iget(sb
, ino
);
1491 return ERR_CAST(inode
);
1493 if (generation
&& inode
->i_generation
!= generation
) {
1495 return ERR_PTR(-ESTALE
);
1501 struct dentry
*jfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
1502 int fh_len
, int fh_type
)
1504 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
1508 struct dentry
*jfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
1509 int fh_len
, int fh_type
)
1511 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
1515 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1517 unsigned long parent_ino
;
1520 le32_to_cpu(JFS_IP(d_inode(dentry
))->i_dtroot
.header
.idotdot
);
1522 return d_obtain_alias(jfs_iget(d_inode(dentry
)->i_sb
, parent_ino
));
1525 const struct inode_operations jfs_dir_inode_operations
= {
1526 .create
= jfs_create
,
1527 .lookup
= jfs_lookup
,
1529 .unlink
= jfs_unlink
,
1530 .symlink
= jfs_symlink
,
1534 .rename
= jfs_rename
,
1535 .setxattr
= jfs_setxattr
,
1536 .getxattr
= jfs_getxattr
,
1537 .listxattr
= jfs_listxattr
,
1538 .removexattr
= jfs_removexattr
,
1539 .setattr
= jfs_setattr
,
1540 #ifdef CONFIG_JFS_POSIX_ACL
1541 .get_acl
= jfs_get_acl
,
1542 .set_acl
= jfs_set_acl
,
1546 const struct file_operations jfs_dir_operations
= {
1547 .read
= generic_read_dir
,
1548 .iterate
= jfs_readdir
,
1550 .unlocked_ioctl
= jfs_ioctl
,
1551 #ifdef CONFIG_COMPAT
1552 .compat_ioctl
= jfs_compat_ioctl
,
1554 .llseek
= generic_file_llseek
,
1557 static int jfs_ci_hash(const struct dentry
*dir
, struct qstr
*this)
1562 hash
= init_name_hash();
1563 for (i
=0; i
< this->len
; i
++)
1564 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1565 this->hash
= end_name_hash(hash
);
1570 static int jfs_ci_compare(const struct dentry
*parent
, const struct dentry
*dentry
,
1571 unsigned int len
, const char *str
, const struct qstr
*name
)
1575 if (len
!= name
->len
)
1577 for (i
=0; i
< len
; i
++) {
1578 if (tolower(str
[i
]) != tolower(name
->name
[i
]))
1586 static int jfs_ci_revalidate(struct dentry
*dentry
, unsigned int flags
)
1589 * This is not negative dentry. Always valid.
1591 * Note, rename() to existing directory entry will have ->d_inode,
1592 * and will use existing name which isn't specified name by user.
1594 * We may be able to drop this positive dentry here. But dropping
1595 * positive dentry isn't good idea. So it's unsupported like
1596 * rename("filename", "FILENAME") for now.
1598 if (d_really_is_positive(dentry
))
1602 * This may be nfsd (or something), anyway, we can't see the
1603 * intent of this. So, since this can be for creation, drop it.
1609 * Drop the negative dentry, in order to make sure to use the
1610 * case sensitive name which is specified by user if this is
1613 if (flags
& (LOOKUP_CREATE
| LOOKUP_RENAME_TARGET
))
1618 const struct dentry_operations jfs_ci_dentry_operations
=
1620 .d_hash
= jfs_ci_hash
,
1621 .d_compare
= jfs_ci_compare
,
1622 .d_revalidate
= jfs_ci_revalidate
,