1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
4 * Portions Copyright (C) Christoph Hellwig, 2001-2002
8 #include <linux/namei.h>
9 #include <linux/ctype.h>
10 #include <linux/quotaops.h>
11 #include <linux/exportfs.h>
12 #include "jfs_incore.h"
13 #include "jfs_superblock.h"
14 #include "jfs_inode.h"
15 #include "jfs_dinode.h"
17 #include "jfs_unicode.h"
18 #include "jfs_metapage.h"
19 #include "jfs_xattr.h"
21 #include "jfs_debug.h"
26 const struct dentry_operations jfs_ci_dentry_operations
;
28 static s64
commitZeroLink(tid_t
, struct inode
*);
31 * NAME: free_ea_wmap(inode)
33 * FUNCTION: free uncommitted extended attributes from working map
36 static inline void free_ea_wmap(struct inode
*inode
)
38 dxd_t
*ea
= &JFS_IP(inode
)->ea
;
40 if (ea
->flag
& DXD_EXTENT
) {
41 /* free EA pages from cache */
42 invalidate_dxd_metapages(inode
, *ea
);
43 dbFree(inode
, addressDXD(ea
), lengthDXD(ea
));
49 * NAME: jfs_create(dip, dentry, mode)
51 * FUNCTION: create a regular file in the parent directory <dip>
52 * with name = <from dentry> and mode = <mode>
54 * PARAMETER: dip - parent directory vnode
55 * dentry - dentry of new file
56 * mode - create mode (rwxrwxrwx).
59 * RETURN: Errors from subroutines
62 static int jfs_create(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
,
66 tid_t tid
; /* transaction id */
67 struct inode
*ip
= NULL
; /* child directory inode */
69 struct component_name dname
; /* child directory name */
70 struct btstack btstack
;
71 struct inode
*iplist
[2];
74 jfs_info("jfs_create: dip:0x%p name:%pd", dip
, dentry
);
76 rc
= dquot_initialize(dip
);
81 * search parent directory for entry/freespace
82 * (dtSearch() returns parent directory page pinned)
84 if ((rc
= get_UCSname(&dname
, dentry
)))
88 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
89 * block there while holding dtree page, so we allocate the inode &
90 * begin the transaction before we search the directory.
92 ip
= ialloc(dip
, mode
);
98 tid
= txBegin(dip
->i_sb
, 0);
100 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
101 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
103 rc
= jfs_init_acl(tid
, ip
, dip
);
107 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
113 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
114 jfs_err("jfs_create: dtSearch returned %d", rc
);
119 tblk
= tid_to_tblock(tid
);
120 tblk
->xflag
|= COMMIT_CREATE
;
121 tblk
->ino
= ip
->i_ino
;
122 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
128 * initialize the child XAD tree root in-line in inode
133 * create entry in parent directory for child directory
134 * (dtInsert() releases parent directory page)
137 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
139 jfs_err("jfs_create: dtInsert returned -EIO");
140 txAbort(tid
, 1); /* Marks Filesystem dirty */
142 txAbort(tid
, 0); /* Filesystem full */
146 ip
->i_op
= &jfs_file_inode_operations
;
147 ip
->i_fop
= &jfs_file_operations
;
148 ip
->i_mapping
->a_ops
= &jfs_aops
;
150 mark_inode_dirty(ip
);
152 dip
->i_ctime
= dip
->i_mtime
= current_time(dip
);
154 mark_inode_dirty(dip
);
156 rc
= txCommit(tid
, 2, &iplist
[0], 0);
160 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
161 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
165 discard_new_inode(ip
);
167 d_instantiate_new(dentry
, ip
);
171 free_UCSname(&dname
);
175 jfs_info("jfs_create: rc:%d", rc
);
181 * NAME: jfs_mkdir(dip, dentry, mode)
183 * FUNCTION: create a child directory in the parent directory <dip>
184 * with name = <from dentry> and mode = <mode>
186 * PARAMETER: dip - parent directory vnode
187 * dentry - dentry of child directory
188 * mode - create mode (rwxrwxrwx).
190 * RETURN: Errors from subroutines
193 * EACCES: user needs search+write permission on the parent directory
195 static int jfs_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
198 tid_t tid
; /* transaction id */
199 struct inode
*ip
= NULL
; /* child directory inode */
201 struct component_name dname
; /* child directory name */
202 struct btstack btstack
;
203 struct inode
*iplist
[2];
206 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip
, dentry
);
208 rc
= dquot_initialize(dip
);
213 * search parent directory for entry/freespace
214 * (dtSearch() returns parent directory page pinned)
216 if ((rc
= get_UCSname(&dname
, dentry
)))
220 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
221 * block there while holding dtree page, so we allocate the inode &
222 * begin the transaction before we search the directory.
224 ip
= ialloc(dip
, S_IFDIR
| mode
);
230 tid
= txBegin(dip
->i_sb
, 0);
232 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
233 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
235 rc
= jfs_init_acl(tid
, ip
, dip
);
239 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
245 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
246 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
251 tblk
= tid_to_tblock(tid
);
252 tblk
->xflag
|= COMMIT_CREATE
;
253 tblk
->ino
= ip
->i_ino
;
254 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
260 * initialize the child directory in-line in inode
262 dtInitRoot(tid
, ip
, dip
->i_ino
);
265 * create entry in parent directory for child directory
266 * (dtInsert() releases parent directory page)
269 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
271 jfs_err("jfs_mkdir: dtInsert returned -EIO");
272 txAbort(tid
, 1); /* Marks Filesystem dirty */
274 txAbort(tid
, 0); /* Filesystem full */
278 set_nlink(ip
, 2); /* for '.' */
279 ip
->i_op
= &jfs_dir_inode_operations
;
280 ip
->i_fop
= &jfs_dir_operations
;
282 mark_inode_dirty(ip
);
284 /* update parent directory inode */
285 inc_nlink(dip
); /* for '..' from child directory */
286 dip
->i_ctime
= dip
->i_mtime
= current_time(dip
);
287 mark_inode_dirty(dip
);
289 rc
= txCommit(tid
, 2, &iplist
[0], 0);
293 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
294 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
298 discard_new_inode(ip
);
300 d_instantiate_new(dentry
, ip
);
304 free_UCSname(&dname
);
309 jfs_info("jfs_mkdir: rc:%d", rc
);
314 * NAME: jfs_rmdir(dip, dentry)
316 * FUNCTION: remove a link to child directory
318 * PARAMETER: dip - parent inode
319 * dentry - child directory dentry
321 * RETURN: -EINVAL - if name is . or ..
322 * -EINVAL - if . or .. exist but are invalid.
323 * errors from subroutines
326 * if other threads have the directory open when the last link
327 * is removed, the "." and ".." entries, if present, are removed before
328 * rmdir() returns and no new entries may be created in the directory,
329 * but the directory is not removed until the last reference to
330 * the directory is released (cf.unlink() of regular file).
332 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
335 tid_t tid
; /* transaction id */
336 struct inode
*ip
= d_inode(dentry
);
338 struct component_name dname
;
339 struct inode
*iplist
[2];
342 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip
, dentry
);
344 /* Init inode for quota operations. */
345 rc
= dquot_initialize(dip
);
348 rc
= dquot_initialize(ip
);
352 /* directory must be empty to be removed */
358 if ((rc
= get_UCSname(&dname
, dentry
))) {
362 tid
= txBegin(dip
->i_sb
, 0);
364 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
365 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
370 tblk
= tid_to_tblock(tid
);
371 tblk
->xflag
|= COMMIT_DELETE
;
375 * delete the entry of target directory from parent directory
378 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
379 jfs_err("jfs_rmdir: dtDelete returned %d", rc
);
383 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
384 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
389 /* update parent directory's link count corresponding
390 * to ".." entry of the target directory deleted
392 dip
->i_ctime
= dip
->i_mtime
= current_time(dip
);
393 inode_dec_link_count(dip
);
396 * OS/2 could have created EA and/or ACL
398 /* free EA from both persistent and working map */
399 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
401 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
403 JFS_IP(ip
)->ea
.flag
= 0;
405 /* free ACL from both persistent and working map */
406 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
408 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
410 JFS_IP(ip
)->acl
.flag
= 0;
412 /* mark the target directory as deleted */
414 mark_inode_dirty(ip
);
416 rc
= txCommit(tid
, 2, &iplist
[0], 0);
420 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
421 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
424 * Truncating the directory index table is not guaranteed. It
425 * may need to be done iteratively
427 if (test_cflag(COMMIT_Stale
, dip
)) {
429 jfs_truncate_nolock(dip
, 0);
431 clear_cflag(COMMIT_Stale
, dip
);
435 free_UCSname(&dname
);
438 jfs_info("jfs_rmdir: rc:%d", rc
);
443 * NAME: jfs_unlink(dip, dentry)
445 * FUNCTION: remove a link to object <vp> named by <name>
446 * from parent directory <dvp>
448 * PARAMETER: dip - inode of parent directory
449 * dentry - dentry of object to be removed
451 * RETURN: errors from subroutines
454 * temporary file: if one or more processes have the file open
455 * when the last link is removed, the link will be removed before
456 * unlink() returns, but the removal of the file contents will be
457 * postponed until all references to the files are closed.
459 * JFS does NOT support unlink() on directories.
462 static int jfs_unlink(struct inode
*dip
, struct dentry
*dentry
)
465 tid_t tid
; /* transaction id */
466 struct inode
*ip
= d_inode(dentry
);
468 struct component_name dname
; /* object name */
469 struct inode
*iplist
[2];
474 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip
, dentry
);
476 /* Init inode for quota operations. */
477 rc
= dquot_initialize(dip
);
480 rc
= dquot_initialize(ip
);
484 if ((rc
= get_UCSname(&dname
, dentry
)))
487 IWRITE_LOCK(ip
, RDWRLOCK_NORMAL
);
489 tid
= txBegin(dip
->i_sb
, 0);
491 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
492 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
498 * delete the entry of target file from parent directory
501 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
502 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
504 txAbort(tid
, 1); /* Marks FS Dirty */
506 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
507 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
514 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= current_time(ip
);
515 mark_inode_dirty(dip
);
517 /* update target's inode */
518 inode_dec_link_count(ip
);
521 * commit zero link count object
523 if (ip
->i_nlink
== 0) {
524 assert(!test_cflag(COMMIT_Nolink
, ip
));
525 /* free block resources */
526 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
527 txAbort(tid
, 1); /* Marks FS Dirty */
529 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
530 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
535 tblk
= tid_to_tblock(tid
);
536 tblk
->xflag
|= COMMIT_DELETE
;
541 * Incomplete truncate of file data can
542 * result in timing problems unless we synchronously commit the
546 commit_flag
= COMMIT_SYNC
;
551 * If xtTruncate was incomplete, commit synchronously to avoid
552 * timing complications
554 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
558 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
559 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
561 while (new_size
&& (rc
== 0)) {
562 tid
= txBegin(dip
->i_sb
, 0);
563 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
564 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
566 txAbort(tid
, 1); /* Marks FS Dirty */
569 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
571 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
574 if (ip
->i_nlink
== 0)
575 set_cflag(COMMIT_Nolink
, ip
);
580 * Truncating the directory index table is not guaranteed. It
581 * may need to be done iteratively
583 if (test_cflag(COMMIT_Stale
, dip
)) {
585 jfs_truncate_nolock(dip
, 0);
587 clear_cflag(COMMIT_Stale
, dip
);
591 free_UCSname(&dname
);
593 jfs_info("jfs_unlink: rc:%d", rc
);
598 * NAME: commitZeroLink()
600 * FUNCTION: for non-directory, called by jfs_remove(),
601 * truncate a regular file, directory or symbolic
602 * link to zero length. return 0 if type is not
605 * if the file is currently associated with a VM segment
606 * only permanent disk and inode map resources are freed,
607 * and neither the inode nor indirect blocks are modified
608 * so that the resources can be later freed in the work
610 * if there is no VM segment on entry, the resources are
611 * freed in both work and permanent map.
612 * (? for temporary file - memory object is cached even
613 * after no reference:
614 * reference count > 0 - )
616 * PARAMETERS: cd - pointer to commit data structure.
617 * current inode is the one to truncate.
619 * RETURN: Errors from subroutines
621 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
626 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
628 filetype
= ip
->i_mode
& S_IFMT
;
633 /* fast symbolic link */
634 if (ip
->i_size
< IDATASIZE
) {
640 assert(filetype
!= S_IFDIR
);
644 set_cflag(COMMIT_Freewmap
, ip
);
646 /* mark transaction of block map update type */
647 tblk
= tid_to_tblock(tid
);
648 tblk
->xflag
|= COMMIT_PMAP
;
653 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
654 /* acquire maplock on EA to be freed from block map */
655 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
660 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
661 /* acquire maplock on EA to be freed from block map */
662 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
665 * free xtree/data (truncate to zero length):
666 * free xtree/data pages from cache if COMMIT_PWMAP,
667 * free xtree/data blocks from persistent block map, and
668 * free xtree/data blocks from working block map if COMMIT_PWMAP;
671 return xtTruncate_pmap(tid
, ip
, 0);
678 * NAME: jfs_free_zero_link()
680 * FUNCTION: for non-directory, called by iClose(),
681 * free resources of a file from cache and WORKING map
682 * for a file previously committed with zero link count
683 * while associated with a pager object,
685 * PARAMETER: ip - pointer to inode of file.
687 void jfs_free_zero_link(struct inode
*ip
)
691 jfs_info("jfs_free_zero_link: ip = 0x%p", ip
);
693 /* return if not reg or symbolic link or if size is
696 type
= ip
->i_mode
& S_IFMT
;
702 /* if its contained in inode nothing to do */
703 if (ip
->i_size
< IDATASIZE
)
713 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
714 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
715 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
716 struct maplock maplock
; /* maplock for COMMIT_WMAP */
717 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
719 /* free EA pages from cache */
720 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
722 /* free EA extent from working block map */
724 pxdlock
= (struct pxd_lock
*) & maplock
;
725 pxdlock
->flag
= mlckFREEPXD
;
726 PXDaddress(&pxdlock
->pxd
, xaddr
);
727 PXDlength(&pxdlock
->pxd
, xlen
);
728 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
734 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
735 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
736 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
737 struct maplock maplock
; /* maplock for COMMIT_WMAP */
738 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
740 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
742 /* free ACL extent from working block map */
744 pxdlock
= (struct pxd_lock
*) & maplock
;
745 pxdlock
->flag
= mlckFREEPXD
;
746 PXDaddress(&pxdlock
->pxd
, xaddr
);
747 PXDlength(&pxdlock
->pxd
, xlen
);
748 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
752 * free xtree/data (truncate to zero length):
753 * free xtree/data pages from cache, and
754 * free xtree/data blocks from working block map;
757 xtTruncate(0, ip
, 0, COMMIT_WMAP
);
761 * NAME: jfs_link(vp, dvp, name, crp)
763 * FUNCTION: create a link to <vp> by the name = <name>
764 * in the parent directory <dvp>
766 * PARAMETER: vp - target object
767 * dvp - parent directory of new link
768 * name - name of new link to target object
771 * RETURN: Errors from subroutines
774 * JFS does NOT support link() on directories (to prevent circular
775 * path in the directory hierarchy);
776 * EPERM: the target object is a directory, and either the caller
777 * does not have appropriate privileges or the implementation prohibits
778 * using link() on directories [XPG4.2].
780 * JFS does NOT support links between file systems:
781 * EXDEV: target object and new link are on different file systems and
782 * implementation does not support links between file systems [XPG4.2].
784 static int jfs_link(struct dentry
*old_dentry
,
785 struct inode
*dir
, struct dentry
*dentry
)
789 struct inode
*ip
= d_inode(old_dentry
);
791 struct component_name dname
;
792 struct btstack btstack
;
793 struct inode
*iplist
[2];
795 jfs_info("jfs_link: %pd %pd", old_dentry
, dentry
);
797 rc
= dquot_initialize(dir
);
801 tid
= txBegin(ip
->i_sb
, 0);
803 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
804 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
807 * scan parent directory for entry/freespace
809 if ((rc
= get_UCSname(&dname
, dentry
)))
812 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
816 * create entry for new link in parent directory
819 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
822 /* update object inode */
823 inc_nlink(ip
); /* for new link */
824 ip
->i_ctime
= current_time(ip
);
825 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
826 mark_inode_dirty(dir
);
831 rc
= txCommit(tid
, 2, &iplist
[0], 0);
834 drop_nlink(ip
); /* never instantiated */
837 d_instantiate(dentry
, ip
);
840 free_UCSname(&dname
);
845 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
846 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
849 jfs_info("jfs_link: rc:%d", rc
);
854 * NAME: jfs_symlink(dip, dentry, name)
856 * FUNCTION: creates a symbolic link to <symlink> by name <name>
859 * PARAMETER: dip - parent directory vnode
860 * dentry - dentry of symbolic link
861 * name - the path name of the existing object
862 * that will be the source of the link
864 * RETURN: errors from subroutines
867 * ENAMETOOLONG: pathname resolution of a symbolic link produced
868 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
871 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
877 struct component_name dname
;
878 int ssize
; /* source pathname size */
879 struct btstack btstack
;
880 struct inode
*ip
= d_inode(dentry
);
882 int bmask
= 0, xsize
;
885 struct super_block
*sb
;
888 struct inode
*iplist
[2];
890 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
892 rc
= dquot_initialize(dip
);
896 ssize
= strlen(name
) + 1;
899 * search parent directory for entry/freespace
900 * (dtSearch() returns parent directory page pinned)
903 if ((rc
= get_UCSname(&dname
, dentry
)))
907 * allocate on-disk/in-memory inode for symbolic link:
908 * (iAlloc() returns new, locked inode)
910 ip
= ialloc(dip
, S_IFLNK
| 0777);
916 tid
= txBegin(dip
->i_sb
, 0);
918 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
919 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
921 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
925 tblk
= tid_to_tblock(tid
);
926 tblk
->xflag
|= COMMIT_CREATE
;
927 tblk
->ino
= ip
->i_ino
;
928 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
930 /* fix symlink access permission
931 * (dir_create() ANDs in the u.u_cmask,
932 * but symlinks really need to be 777 access)
937 * write symbolic link target path name
942 * write source path name inline in on-disk inode (fast symbolic link)
945 if (ssize
<= IDATASIZE
) {
946 ip
->i_op
= &jfs_fast_symlink_inode_operations
;
948 ip
->i_link
= JFS_IP(ip
)->i_inline
;
949 memcpy(ip
->i_link
, name
, ssize
);
950 ip
->i_size
= ssize
- 1;
953 * if symlink is > 128 bytes, we don't have the space to
954 * store inline extended attributes
956 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
957 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
959 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
963 * write source path name in a single extent
966 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
968 ip
->i_op
= &jfs_symlink_inode_operations
;
970 ip
->i_mapping
->a_ops
= &jfs_aops
;
973 * even though the data of symlink object (source
974 * path name) is treated as non-journaled user data,
975 * it is read/written thru buffer cache for performance.
978 bmask
= JFS_SBI(sb
)->bsize
- 1;
979 xsize
= (ssize
+ bmask
) & ~bmask
;
981 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
982 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
986 ip
->i_size
= ssize
- 1;
988 /* This is kind of silly since PATH_MAX == 4K */
989 int copy_size
= min(ssize
, PSIZE
);
991 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
994 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
999 memcpy(mp
->data
, name
, copy_size
);
1003 xaddr
+= JFS_SBI(sb
)->nbperpage
;
1008 * create entry for symbolic link in parent directory
1010 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
1013 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
1017 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1019 /* discard new inode */
1023 mark_inode_dirty(ip
);
1025 dip
->i_ctime
= dip
->i_mtime
= current_time(dip
);
1026 mark_inode_dirty(dip
);
1028 * commit update of parent directory and link object
1033 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1037 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1038 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
1042 discard_new_inode(ip
);
1044 d_instantiate_new(dentry
, ip
);
1048 free_UCSname(&dname
);
1051 jfs_info("jfs_symlink: rc:%d", rc
);
1059 * FUNCTION: rename a file or directory
1061 static int jfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1062 struct inode
*new_dir
, struct dentry
*new_dentry
,
1065 struct btstack btstack
;
1067 struct component_name new_dname
;
1068 struct inode
*new_ip
;
1069 struct component_name old_dname
;
1070 struct inode
*old_ip
;
1074 struct dt_lock
*dtlck
;
1077 struct inode
*iplist
[4];
1078 struct tblock
*tblk
;
1082 if (flags
& ~RENAME_NOREPLACE
)
1085 jfs_info("jfs_rename: %pd %pd", old_dentry
, new_dentry
);
1087 rc
= dquot_initialize(old_dir
);
1090 rc
= dquot_initialize(new_dir
);
1094 old_ip
= d_inode(old_dentry
);
1095 new_ip
= d_inode(new_dentry
);
1097 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1100 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1104 * Make sure source inode number is what we think it is
1106 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1107 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1113 * Make sure dest inode number (if any) is what we think it is
1115 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1117 if ((!new_ip
) || (ino
!= new_ip
->i_ino
)) {
1121 } else if (rc
!= -ENOENT
)
1124 /* no entry exists, but one was expected */
1129 if (S_ISDIR(old_ip
->i_mode
)) {
1131 if (!dtEmpty(new_ip
)) {
1136 } else if (new_ip
) {
1137 IWRITE_LOCK(new_ip
, RDWRLOCK_NORMAL
);
1138 /* Init inode for quota operations. */
1139 rc
= dquot_initialize(new_ip
);
1145 * The real work starts here
1147 tid
= txBegin(new_dir
->i_sb
, 0);
1150 * How do we know the locking is safe from deadlocks?
1151 * The vfs does the hard part for us. Any time we are taking nested
1152 * commit_mutexes, the vfs already has i_mutex held on the parent.
1153 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1155 mutex_lock_nested(&JFS_IP(new_dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1156 mutex_lock_nested(&JFS_IP(old_ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1157 if (old_dir
!= new_dir
)
1158 mutex_lock_nested(&JFS_IP(old_dir
)->commit_mutex
,
1159 COMMIT_MUTEX_SECOND_PARENT
);
1162 mutex_lock_nested(&JFS_IP(new_ip
)->commit_mutex
,
1163 COMMIT_MUTEX_VICTIM
);
1165 * Change existing directory entry to new inode number
1167 ino
= new_ip
->i_ino
;
1168 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1169 old_ip
->i_ino
, JFS_RENAME
);
1173 if (S_ISDIR(new_ip
->i_mode
)) {
1175 if (new_ip
->i_nlink
) {
1176 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1177 if (old_dir
!= new_dir
)
1178 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1179 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1180 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1181 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1182 IWRITE_UNLOCK(new_ip
);
1183 jfs_error(new_ip
->i_sb
,
1184 "new_ip->i_nlink != 0\n");
1187 tblk
= tid_to_tblock(tid
);
1188 tblk
->xflag
|= COMMIT_DELETE
;
1189 tblk
->u
.ip
= new_ip
;
1190 } else if (new_ip
->i_nlink
== 0) {
1191 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1192 /* free block resources */
1193 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1194 txAbort(tid
, 1); /* Marks FS Dirty */
1198 tblk
= tid_to_tblock(tid
);
1199 tblk
->xflag
|= COMMIT_DELETE
;
1200 tblk
->u
.ip
= new_ip
;
1202 new_ip
->i_ctime
= current_time(new_ip
);
1203 mark_inode_dirty(new_ip
);
1207 * Add new directory entry
1209 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1212 jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1217 ino
= old_ip
->i_ino
;
1218 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1221 jfs_err("jfs_rename: dtInsert returned -EIO");
1224 if (S_ISDIR(old_ip
->i_mode
))
1228 * Remove old directory entry
1231 ino
= old_ip
->i_ino
;
1232 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1234 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1236 txAbort(tid
, 1); /* Marks Filesystem dirty */
1239 if (S_ISDIR(old_ip
->i_mode
)) {
1240 drop_nlink(old_dir
);
1241 if (old_dir
!= new_dir
) {
1243 * Change inode number of parent for moved directory
1246 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1247 cpu_to_le32(new_dir
->i_ino
);
1249 /* Linelock header of dtree */
1250 tlck
= txLock(tid
, old_ip
,
1251 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1252 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1253 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1254 ASSERT(dtlck
->index
== 0);
1255 lv
= & dtlck
->lv
[0];
1263 * Update ctime on changed/moved inodes & mark dirty
1265 old_ip
->i_ctime
= current_time(old_ip
);
1266 mark_inode_dirty(old_ip
);
1268 new_dir
->i_ctime
= new_dir
->i_mtime
= current_time(new_dir
);
1269 mark_inode_dirty(new_dir
);
1271 /* Build list of inodes modified by this transaction */
1273 iplist
[ipcount
++] = old_ip
;
1275 iplist
[ipcount
++] = new_ip
;
1276 iplist
[ipcount
++] = old_dir
;
1278 if (old_dir
!= new_dir
) {
1279 iplist
[ipcount
++] = new_dir
;
1280 old_dir
->i_ctime
= old_dir
->i_mtime
= current_time(old_dir
);
1281 mark_inode_dirty(old_dir
);
1285 * Incomplete truncate of file data can
1286 * result in timing problems unless we synchronously commit the
1290 commit_flag
= COMMIT_SYNC
;
1294 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1299 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1300 if (old_dir
!= new_dir
)
1301 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1302 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1303 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1305 while (new_size
&& (rc
== 0)) {
1306 tid
= txBegin(new_ip
->i_sb
, 0);
1307 mutex_lock(&JFS_IP(new_ip
)->commit_mutex
);
1308 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1313 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1315 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1317 if (new_ip
&& (new_ip
->i_nlink
== 0))
1318 set_cflag(COMMIT_Nolink
, new_ip
);
1320 * Truncating the directory index table is not guaranteed. It
1321 * may need to be done iteratively
1323 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1324 if (old_dir
->i_size
> 1)
1325 jfs_truncate_nolock(old_dir
, 0);
1327 clear_cflag(COMMIT_Stale
, old_dir
);
1330 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1331 IWRITE_UNLOCK(new_ip
);
1333 free_UCSname(&new_dname
);
1335 free_UCSname(&old_dname
);
1337 jfs_info("jfs_rename: returning %d", rc
);
1345 * FUNCTION: Create a special file (device)
1347 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1348 umode_t mode
, dev_t rdev
)
1350 struct jfs_inode_info
*jfs_ip
;
1351 struct btstack btstack
;
1352 struct component_name dname
;
1355 struct inode
*iplist
[2];
1358 struct tblock
*tblk
;
1360 jfs_info("jfs_mknod: %pd", dentry
);
1362 rc
= dquot_initialize(dir
);
1366 if ((rc
= get_UCSname(&dname
, dentry
)))
1369 ip
= ialloc(dir
, mode
);
1374 jfs_ip
= JFS_IP(ip
);
1376 tid
= txBegin(dir
->i_sb
, 0);
1378 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1379 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1381 rc
= jfs_init_acl(tid
, ip
, dir
);
1385 rc
= jfs_init_security(tid
, ip
, dir
, &dentry
->d_name
);
1391 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
1396 tblk
= tid_to_tblock(tid
);
1397 tblk
->xflag
|= COMMIT_CREATE
;
1398 tblk
->ino
= ip
->i_ino
;
1399 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1402 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
))) {
1407 ip
->i_op
= &jfs_file_inode_operations
;
1408 jfs_ip
->dev
= new_encode_dev(rdev
);
1409 init_special_inode(ip
, ip
->i_mode
, rdev
);
1411 mark_inode_dirty(ip
);
1413 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
1415 mark_inode_dirty(dir
);
1419 rc
= txCommit(tid
, 2, iplist
, 0);
1423 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1424 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
1428 discard_new_inode(ip
);
1430 d_instantiate_new(dentry
, ip
);
1434 free_UCSname(&dname
);
1437 jfs_info("jfs_mknod: returning %d", rc
);
1441 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
1443 struct btstack btstack
;
1446 struct component_name key
;
1449 jfs_info("jfs_lookup: name = %pd", dentry
);
1451 if ((rc
= get_UCSname(&key
, dentry
)))
1453 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1455 if (rc
== -ENOENT
) {
1458 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1461 ip
= jfs_iget(dip
->i_sb
, inum
);
1463 jfs_err("jfs_lookup: iget failed on inum %d", (uint
)inum
);
1466 return d_splice_alias(ip
, dentry
);
1469 static struct inode
*jfs_nfs_get_inode(struct super_block
*sb
,
1470 u64 ino
, u32 generation
)
1472 struct inode
*inode
;
1475 return ERR_PTR(-ESTALE
);
1476 inode
= jfs_iget(sb
, ino
);
1478 return ERR_CAST(inode
);
1480 if (generation
&& inode
->i_generation
!= generation
) {
1482 return ERR_PTR(-ESTALE
);
1488 struct dentry
*jfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
1489 int fh_len
, int fh_type
)
1491 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
1495 struct dentry
*jfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
1496 int fh_len
, int fh_type
)
1498 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
1502 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1504 unsigned long parent_ino
;
1507 le32_to_cpu(JFS_IP(d_inode(dentry
))->i_dtroot
.header
.idotdot
);
1509 return d_obtain_alias(jfs_iget(dentry
->d_sb
, parent_ino
));
1512 const struct inode_operations jfs_dir_inode_operations
= {
1513 .create
= jfs_create
,
1514 .lookup
= jfs_lookup
,
1516 .unlink
= jfs_unlink
,
1517 .symlink
= jfs_symlink
,
1521 .rename
= jfs_rename
,
1522 .listxattr
= jfs_listxattr
,
1523 .setattr
= jfs_setattr
,
1524 #ifdef CONFIG_JFS_POSIX_ACL
1525 .get_acl
= jfs_get_acl
,
1526 .set_acl
= jfs_set_acl
,
1530 const struct file_operations jfs_dir_operations
= {
1531 .read
= generic_read_dir
,
1532 .iterate
= jfs_readdir
,
1534 .unlocked_ioctl
= jfs_ioctl
,
1535 #ifdef CONFIG_COMPAT
1536 .compat_ioctl
= jfs_compat_ioctl
,
1538 .llseek
= generic_file_llseek
,
1541 static int jfs_ci_hash(const struct dentry
*dir
, struct qstr
*this)
1546 hash
= init_name_hash(dir
);
1547 for (i
=0; i
< this->len
; i
++)
1548 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1549 this->hash
= end_name_hash(hash
);
1554 static int jfs_ci_compare(const struct dentry
*dentry
,
1555 unsigned int len
, const char *str
, const struct qstr
*name
)
1559 if (len
!= name
->len
)
1561 for (i
=0; i
< len
; i
++) {
1562 if (tolower(str
[i
]) != tolower(name
->name
[i
]))
1570 static int jfs_ci_revalidate(struct dentry
*dentry
, unsigned int flags
)
1573 * This is not negative dentry. Always valid.
1575 * Note, rename() to existing directory entry will have ->d_inode,
1576 * and will use existing name which isn't specified name by user.
1578 * We may be able to drop this positive dentry here. But dropping
1579 * positive dentry isn't good idea. So it's unsupported like
1580 * rename("filename", "FILENAME") for now.
1582 if (d_really_is_positive(dentry
))
1586 * This may be nfsd (or something), anyway, we can't see the
1587 * intent of this. So, since this can be for creation, drop it.
1593 * Drop the negative dentry, in order to make sure to use the
1594 * case sensitive name which is specified by user if this is
1597 if (flags
& (LOOKUP_CREATE
| LOOKUP_RENAME_TARGET
))
1602 const struct dentry_operations jfs_ci_dentry_operations
=
1604 .d_hash
= jfs_ci_hash
,
1605 .d_compare
= jfs_ci_compare
,
1606 .d_revalidate
= jfs_ci_revalidate
,