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(dip
);
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 unlock_new_inode(ip
);
182 d_instantiate(dentry
, ip
);
186 free_UCSname(&dname
);
190 jfs_info("jfs_create: rc:%d", rc
);
196 * NAME: jfs_mkdir(dip, dentry, mode)
198 * FUNCTION: create a child directory in the parent directory <dip>
199 * with name = <from dentry> and mode = <mode>
201 * PARAMETER: dip - parent directory vnode
202 * dentry - dentry of child directory
203 * mode - create mode (rwxrwxrwx).
205 * RETURN: Errors from subroutines
208 * EACCESS: user needs search+write permission on the parent directory
210 static int jfs_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
213 tid_t tid
; /* transaction id */
214 struct inode
*ip
= NULL
; /* child directory inode */
216 struct component_name dname
; /* child directory name */
217 struct btstack btstack
;
218 struct inode
*iplist
[2];
221 jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip
, dentry
);
223 rc
= dquot_initialize(dip
);
228 * search parent directory for entry/freespace
229 * (dtSearch() returns parent directory page pinned)
231 if ((rc
= get_UCSname(&dname
, dentry
)))
235 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
236 * block there while holding dtree page, so we allocate the inode &
237 * begin the transaction before we search the directory.
239 ip
= ialloc(dip
, S_IFDIR
| mode
);
245 tid
= txBegin(dip
->i_sb
, 0);
247 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
248 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
250 rc
= jfs_init_acl(tid
, ip
, dip
);
254 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
260 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
261 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
266 tblk
= tid_to_tblock(tid
);
267 tblk
->xflag
|= COMMIT_CREATE
;
268 tblk
->ino
= ip
->i_ino
;
269 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
275 * initialize the child directory in-line in inode
277 dtInitRoot(tid
, ip
, dip
->i_ino
);
280 * create entry in parent directory for child directory
281 * (dtInsert() releases parent directory page)
284 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
286 jfs_err("jfs_mkdir: dtInsert returned -EIO");
287 txAbort(tid
, 1); /* Marks Filesystem dirty */
289 txAbort(tid
, 0); /* Filesystem full */
293 set_nlink(ip
, 2); /* for '.' */
294 ip
->i_op
= &jfs_dir_inode_operations
;
295 ip
->i_fop
= &jfs_dir_operations
;
297 mark_inode_dirty(ip
);
299 /* update parent directory inode */
300 inc_nlink(dip
); /* for '..' from child directory */
301 dip
->i_ctime
= dip
->i_mtime
= current_time(dip
);
302 mark_inode_dirty(dip
);
304 rc
= txCommit(tid
, 2, &iplist
[0], 0);
308 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
309 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
313 unlock_new_inode(ip
);
316 unlock_new_inode(ip
);
317 d_instantiate(dentry
, ip
);
321 free_UCSname(&dname
);
326 jfs_info("jfs_mkdir: rc:%d", rc
);
331 * NAME: jfs_rmdir(dip, dentry)
333 * FUNCTION: remove a link to child directory
335 * PARAMETER: dip - parent inode
336 * dentry - child directory dentry
338 * RETURN: -EINVAL - if name is . or ..
339 * -EINVAL - if . or .. exist but are invalid.
340 * errors from subroutines
343 * if other threads have the directory open when the last link
344 * is removed, the "." and ".." entries, if present, are removed before
345 * rmdir() returns and no new entries may be created in the directory,
346 * but the directory is not removed until the last reference to
347 * the directory is released (cf.unlink() of regular file).
349 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
352 tid_t tid
; /* transaction id */
353 struct inode
*ip
= d_inode(dentry
);
355 struct component_name dname
;
356 struct inode
*iplist
[2];
359 jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip
, dentry
);
361 /* Init inode for quota operations. */
362 rc
= dquot_initialize(dip
);
365 rc
= dquot_initialize(ip
);
369 /* directory must be empty to be removed */
375 if ((rc
= get_UCSname(&dname
, dentry
))) {
379 tid
= txBegin(dip
->i_sb
, 0);
381 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
382 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
387 tblk
= tid_to_tblock(tid
);
388 tblk
->xflag
|= COMMIT_DELETE
;
392 * delete the entry of target directory from parent directory
395 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
396 jfs_err("jfs_rmdir: dtDelete returned %d", rc
);
400 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
401 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
406 /* update parent directory's link count corresponding
407 * to ".." entry of the target directory deleted
409 dip
->i_ctime
= dip
->i_mtime
= current_time(dip
);
410 inode_dec_link_count(dip
);
413 * OS/2 could have created EA and/or ACL
415 /* free EA from both persistent and working map */
416 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
418 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
420 JFS_IP(ip
)->ea
.flag
= 0;
422 /* free ACL from both persistent and working map */
423 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
425 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
427 JFS_IP(ip
)->acl
.flag
= 0;
429 /* mark the target directory as deleted */
431 mark_inode_dirty(ip
);
433 rc
= txCommit(tid
, 2, &iplist
[0], 0);
437 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
438 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
441 * Truncating the directory index table is not guaranteed. It
442 * may need to be done iteratively
444 if (test_cflag(COMMIT_Stale
, dip
)) {
446 jfs_truncate_nolock(dip
, 0);
448 clear_cflag(COMMIT_Stale
, dip
);
452 free_UCSname(&dname
);
455 jfs_info("jfs_rmdir: rc:%d", rc
);
460 * NAME: jfs_unlink(dip, dentry)
462 * FUNCTION: remove a link to object <vp> named by <name>
463 * from parent directory <dvp>
465 * PARAMETER: dip - inode of parent directory
466 * dentry - dentry of object to be removed
468 * RETURN: errors from subroutines
471 * temporary file: if one or more processes have the file open
472 * when the last link is removed, the link will be removed before
473 * unlink() returns, but the removal of the file contents will be
474 * postponed until all references to the files are closed.
476 * JFS does NOT support unlink() on directories.
479 static int jfs_unlink(struct inode
*dip
, struct dentry
*dentry
)
482 tid_t tid
; /* transaction id */
483 struct inode
*ip
= d_inode(dentry
);
485 struct component_name dname
; /* object name */
486 struct inode
*iplist
[2];
491 jfs_info("jfs_unlink: dip:0x%p name:%pd", dip
, dentry
);
493 /* Init inode for quota operations. */
494 rc
= dquot_initialize(dip
);
497 rc
= dquot_initialize(ip
);
501 if ((rc
= get_UCSname(&dname
, dentry
)))
504 IWRITE_LOCK(ip
, RDWRLOCK_NORMAL
);
506 tid
= txBegin(dip
->i_sb
, 0);
508 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
509 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
515 * delete the entry of target file from parent directory
518 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
519 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
521 txAbort(tid
, 1); /* Marks FS Dirty */
523 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
524 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
531 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= current_time(ip
);
532 mark_inode_dirty(dip
);
534 /* update target's inode */
535 inode_dec_link_count(ip
);
538 * commit zero link count object
540 if (ip
->i_nlink
== 0) {
541 assert(!test_cflag(COMMIT_Nolink
, ip
));
542 /* free block resources */
543 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
544 txAbort(tid
, 1); /* Marks FS Dirty */
546 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
547 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
552 tblk
= tid_to_tblock(tid
);
553 tblk
->xflag
|= COMMIT_DELETE
;
558 * Incomplete truncate of file data can
559 * result in timing problems unless we synchronously commit the
563 commit_flag
= COMMIT_SYNC
;
568 * If xtTruncate was incomplete, commit synchronously to avoid
569 * timing complications
571 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
575 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
576 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
578 while (new_size
&& (rc
== 0)) {
579 tid
= txBegin(dip
->i_sb
, 0);
580 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
581 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
583 txAbort(tid
, 1); /* Marks FS Dirty */
586 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
588 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
591 if (ip
->i_nlink
== 0)
592 set_cflag(COMMIT_Nolink
, ip
);
597 * Truncating the directory index table is not guaranteed. It
598 * may need to be done iteratively
600 if (test_cflag(COMMIT_Stale
, dip
)) {
602 jfs_truncate_nolock(dip
, 0);
604 clear_cflag(COMMIT_Stale
, dip
);
608 free_UCSname(&dname
);
610 jfs_info("jfs_unlink: rc:%d", rc
);
615 * NAME: commitZeroLink()
617 * FUNCTION: for non-directory, called by jfs_remove(),
618 * truncate a regular file, directory or symbolic
619 * link to zero length. return 0 if type is not
622 * if the file is currently associated with a VM segment
623 * only permanent disk and inode map resources are freed,
624 * and neither the inode nor indirect blocks are modified
625 * so that the resources can be later freed in the work
627 * if there is no VM segment on entry, the resources are
628 * freed in both work and permanent map.
629 * (? for temporary file - memory object is cached even
630 * after no reference:
631 * reference count > 0 - )
633 * PARAMETERS: cd - pointer to commit data structure.
634 * current inode is the one to truncate.
636 * RETURN: Errors from subroutines
638 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
643 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
645 filetype
= ip
->i_mode
& S_IFMT
;
650 /* fast symbolic link */
651 if (ip
->i_size
< IDATASIZE
) {
657 assert(filetype
!= S_IFDIR
);
661 set_cflag(COMMIT_Freewmap
, ip
);
663 /* mark transaction of block map update type */
664 tblk
= tid_to_tblock(tid
);
665 tblk
->xflag
|= COMMIT_PMAP
;
670 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
671 /* acquire maplock on EA to be freed from block map */
672 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
677 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
678 /* acquire maplock on EA to be freed from block map */
679 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
682 * free xtree/data (truncate to zero length):
683 * free xtree/data pages from cache if COMMIT_PWMAP,
684 * free xtree/data blocks from persistent block map, and
685 * free xtree/data blocks from working block map if COMMIT_PWMAP;
688 return xtTruncate_pmap(tid
, ip
, 0);
695 * NAME: jfs_free_zero_link()
697 * FUNCTION: for non-directory, called by iClose(),
698 * free resources of a file from cache and WORKING map
699 * for a file previously committed with zero link count
700 * while associated with a pager object,
702 * PARAMETER: ip - pointer to inode of file.
704 void jfs_free_zero_link(struct inode
*ip
)
708 jfs_info("jfs_free_zero_link: ip = 0x%p", ip
);
710 /* return if not reg or symbolic link or if size is
713 type
= ip
->i_mode
& S_IFMT
;
719 /* if its contained in inode nothing to do */
720 if (ip
->i_size
< IDATASIZE
)
730 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
731 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
732 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
733 struct maplock maplock
; /* maplock for COMMIT_WMAP */
734 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
736 /* free EA pages from cache */
737 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
739 /* free EA extent from working block map */
741 pxdlock
= (struct pxd_lock
*) & maplock
;
742 pxdlock
->flag
= mlckFREEPXD
;
743 PXDaddress(&pxdlock
->pxd
, xaddr
);
744 PXDlength(&pxdlock
->pxd
, xlen
);
745 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
751 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
752 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
753 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
754 struct maplock maplock
; /* maplock for COMMIT_WMAP */
755 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
757 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
759 /* free ACL extent from working block map */
761 pxdlock
= (struct pxd_lock
*) & maplock
;
762 pxdlock
->flag
= mlckFREEPXD
;
763 PXDaddress(&pxdlock
->pxd
, xaddr
);
764 PXDlength(&pxdlock
->pxd
, xlen
);
765 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
769 * free xtree/data (truncate to zero length):
770 * free xtree/data pages from cache, and
771 * free xtree/data blocks from working block map;
774 xtTruncate(0, ip
, 0, COMMIT_WMAP
);
778 * NAME: jfs_link(vp, dvp, name, crp)
780 * FUNCTION: create a link to <vp> by the name = <name>
781 * in the parent directory <dvp>
783 * PARAMETER: vp - target object
784 * dvp - parent directory of new link
785 * name - name of new link to target object
788 * RETURN: Errors from subroutines
791 * JFS does NOT support link() on directories (to prevent circular
792 * path in the directory hierarchy);
793 * EPERM: the target object is a directory, and either the caller
794 * does not have appropriate privileges or the implementation prohibits
795 * using link() on directories [XPG4.2].
797 * JFS does NOT support links between file systems:
798 * EXDEV: target object and new link are on different file systems and
799 * implementation does not support links between file systems [XPG4.2].
801 static int jfs_link(struct dentry
*old_dentry
,
802 struct inode
*dir
, struct dentry
*dentry
)
806 struct inode
*ip
= d_inode(old_dentry
);
808 struct component_name dname
;
809 struct btstack btstack
;
810 struct inode
*iplist
[2];
812 jfs_info("jfs_link: %pd %pd", old_dentry
, dentry
);
814 rc
= dquot_initialize(dir
);
818 tid
= txBegin(ip
->i_sb
, 0);
820 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
821 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
824 * scan parent directory for entry/freespace
826 if ((rc
= get_UCSname(&dname
, dentry
)))
829 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
833 * create entry for new link in parent directory
836 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
839 /* update object inode */
840 inc_nlink(ip
); /* for new link */
841 ip
->i_ctime
= current_time(ip
);
842 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
843 mark_inode_dirty(dir
);
848 rc
= txCommit(tid
, 2, &iplist
[0], 0);
851 drop_nlink(ip
); /* never instantiated */
854 d_instantiate(dentry
, ip
);
857 free_UCSname(&dname
);
862 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
863 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
866 jfs_info("jfs_link: rc:%d", rc
);
871 * NAME: jfs_symlink(dip, dentry, name)
873 * FUNCTION: creates a symbolic link to <symlink> by name <name>
876 * PARAMETER: dip - parent directory vnode
877 * dentry - dentry of symbolic link
878 * name - the path name of the existing object
879 * that will be the source of the link
881 * RETURN: errors from subroutines
884 * ENAMETOOLONG: pathname resolution of a symbolic link produced
885 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
888 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
894 struct component_name dname
;
895 int ssize
; /* source pathname size */
896 struct btstack btstack
;
897 struct inode
*ip
= d_inode(dentry
);
899 int bmask
= 0, xsize
;
902 struct super_block
*sb
;
905 struct inode
*iplist
[2];
907 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
909 rc
= dquot_initialize(dip
);
913 ssize
= strlen(name
) + 1;
916 * search parent directory for entry/freespace
917 * (dtSearch() returns parent directory page pinned)
920 if ((rc
= get_UCSname(&dname
, dentry
)))
924 * allocate on-disk/in-memory inode for symbolic link:
925 * (iAlloc() returns new, locked inode)
927 ip
= ialloc(dip
, S_IFLNK
| 0777);
933 tid
= txBegin(dip
->i_sb
, 0);
935 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
936 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
938 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
942 tblk
= tid_to_tblock(tid
);
943 tblk
->xflag
|= COMMIT_CREATE
;
944 tblk
->ino
= ip
->i_ino
;
945 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
947 /* fix symlink access permission
948 * (dir_create() ANDs in the u.u_cmask,
949 * but symlinks really need to be 777 access)
954 * write symbolic link target path name
959 * write source path name inline in on-disk inode (fast symbolic link)
962 if (ssize
<= IDATASIZE
) {
963 ip
->i_op
= &jfs_fast_symlink_inode_operations
;
965 ip
->i_link
= JFS_IP(ip
)->i_inline
;
966 memcpy(ip
->i_link
, name
, ssize
);
967 ip
->i_size
= ssize
- 1;
970 * if symlink is > 128 bytes, we don't have the space to
971 * store inline extended attributes
973 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
974 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
976 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
980 * write source path name in a single extent
983 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
985 ip
->i_op
= &jfs_symlink_inode_operations
;
987 ip
->i_mapping
->a_ops
= &jfs_aops
;
990 * even though the data of symlink object (source
991 * path name) is treated as non-journaled user data,
992 * it is read/written thru buffer cache for performance.
995 bmask
= JFS_SBI(sb
)->bsize
- 1;
996 xsize
= (ssize
+ bmask
) & ~bmask
;
998 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
999 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
1003 ip
->i_size
= ssize
- 1;
1005 /* This is kind of silly since PATH_MAX == 4K */
1006 int copy_size
= min(ssize
, PSIZE
);
1008 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
1011 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1016 memcpy(mp
->data
, name
, copy_size
);
1020 xaddr
+= JFS_SBI(sb
)->nbperpage
;
1025 * create entry for symbolic link in parent directory
1027 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
1030 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
1034 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1036 /* discard new inode */
1040 mark_inode_dirty(ip
);
1042 dip
->i_ctime
= dip
->i_mtime
= current_time(dip
);
1043 mark_inode_dirty(dip
);
1045 * commit update of parent directory and link object
1050 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1054 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1055 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
1059 unlock_new_inode(ip
);
1062 unlock_new_inode(ip
);
1063 d_instantiate(dentry
, ip
);
1067 free_UCSname(&dname
);
1070 jfs_info("jfs_symlink: rc:%d", rc
);
1078 * FUNCTION: rename a file or directory
1080 static int jfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1081 struct inode
*new_dir
, struct dentry
*new_dentry
,
1084 struct btstack btstack
;
1086 struct component_name new_dname
;
1087 struct inode
*new_ip
;
1088 struct component_name old_dname
;
1089 struct inode
*old_ip
;
1093 struct dt_lock
*dtlck
;
1096 struct inode
*iplist
[4];
1097 struct tblock
*tblk
;
1101 if (flags
& ~RENAME_NOREPLACE
)
1104 jfs_info("jfs_rename: %pd %pd", old_dentry
, new_dentry
);
1106 rc
= dquot_initialize(old_dir
);
1109 rc
= dquot_initialize(new_dir
);
1113 old_ip
= d_inode(old_dentry
);
1114 new_ip
= d_inode(new_dentry
);
1116 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1119 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1123 * Make sure source inode number is what we think it is
1125 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1126 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1132 * Make sure dest inode number (if any) is what we think it is
1134 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1136 if ((!new_ip
) || (ino
!= new_ip
->i_ino
)) {
1140 } else if (rc
!= -ENOENT
)
1143 /* no entry exists, but one was expected */
1148 if (S_ISDIR(old_ip
->i_mode
)) {
1150 if (!dtEmpty(new_ip
)) {
1155 } else if (new_ip
) {
1156 IWRITE_LOCK(new_ip
, RDWRLOCK_NORMAL
);
1157 /* Init inode for quota operations. */
1158 rc
= dquot_initialize(new_ip
);
1164 * The real work starts here
1166 tid
= txBegin(new_dir
->i_sb
, 0);
1169 * How do we know the locking is safe from deadlocks?
1170 * The vfs does the hard part for us. Any time we are taking nested
1171 * commit_mutexes, the vfs already has i_mutex held on the parent.
1172 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1174 mutex_lock_nested(&JFS_IP(new_dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1175 mutex_lock_nested(&JFS_IP(old_ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1176 if (old_dir
!= new_dir
)
1177 mutex_lock_nested(&JFS_IP(old_dir
)->commit_mutex
,
1178 COMMIT_MUTEX_SECOND_PARENT
);
1181 mutex_lock_nested(&JFS_IP(new_ip
)->commit_mutex
,
1182 COMMIT_MUTEX_VICTIM
);
1184 * Change existing directory entry to new inode number
1186 ino
= new_ip
->i_ino
;
1187 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1188 old_ip
->i_ino
, JFS_RENAME
);
1192 if (S_ISDIR(new_ip
->i_mode
)) {
1194 if (new_ip
->i_nlink
) {
1195 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1196 if (old_dir
!= new_dir
)
1197 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1198 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1199 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1200 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1201 IWRITE_UNLOCK(new_ip
);
1202 jfs_error(new_ip
->i_sb
,
1203 "new_ip->i_nlink != 0\n");
1206 tblk
= tid_to_tblock(tid
);
1207 tblk
->xflag
|= COMMIT_DELETE
;
1208 tblk
->u
.ip
= new_ip
;
1209 } else if (new_ip
->i_nlink
== 0) {
1210 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1211 /* free block resources */
1212 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1213 txAbort(tid
, 1); /* Marks FS Dirty */
1217 tblk
= tid_to_tblock(tid
);
1218 tblk
->xflag
|= COMMIT_DELETE
;
1219 tblk
->u
.ip
= new_ip
;
1221 new_ip
->i_ctime
= current_time(new_ip
);
1222 mark_inode_dirty(new_ip
);
1226 * Add new directory entry
1228 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1231 jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1236 ino
= old_ip
->i_ino
;
1237 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1240 jfs_err("jfs_rename: dtInsert returned -EIO");
1243 if (S_ISDIR(old_ip
->i_mode
))
1247 * Remove old directory entry
1250 ino
= old_ip
->i_ino
;
1251 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1253 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1255 txAbort(tid
, 1); /* Marks Filesystem dirty */
1258 if (S_ISDIR(old_ip
->i_mode
)) {
1259 drop_nlink(old_dir
);
1260 if (old_dir
!= new_dir
) {
1262 * Change inode number of parent for moved directory
1265 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1266 cpu_to_le32(new_dir
->i_ino
);
1268 /* Linelock header of dtree */
1269 tlck
= txLock(tid
, old_ip
,
1270 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1271 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1272 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1273 ASSERT(dtlck
->index
== 0);
1274 lv
= & dtlck
->lv
[0];
1282 * Update ctime on changed/moved inodes & mark dirty
1284 old_ip
->i_ctime
= current_time(old_ip
);
1285 mark_inode_dirty(old_ip
);
1287 new_dir
->i_ctime
= new_dir
->i_mtime
= current_time(new_dir
);
1288 mark_inode_dirty(new_dir
);
1290 /* Build list of inodes modified by this transaction */
1292 iplist
[ipcount
++] = old_ip
;
1294 iplist
[ipcount
++] = new_ip
;
1295 iplist
[ipcount
++] = old_dir
;
1297 if (old_dir
!= new_dir
) {
1298 iplist
[ipcount
++] = new_dir
;
1299 old_dir
->i_ctime
= old_dir
->i_mtime
= current_time(old_dir
);
1300 mark_inode_dirty(old_dir
);
1304 * Incomplete truncate of file data can
1305 * result in timing problems unless we synchronously commit the
1309 commit_flag
= COMMIT_SYNC
;
1313 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1318 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1319 if (old_dir
!= new_dir
)
1320 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1321 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1322 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1324 while (new_size
&& (rc
== 0)) {
1325 tid
= txBegin(new_ip
->i_sb
, 0);
1326 mutex_lock(&JFS_IP(new_ip
)->commit_mutex
);
1327 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1332 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1334 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1336 if (new_ip
&& (new_ip
->i_nlink
== 0))
1337 set_cflag(COMMIT_Nolink
, new_ip
);
1339 * Truncating the directory index table is not guaranteed. It
1340 * may need to be done iteratively
1342 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1343 if (old_dir
->i_size
> 1)
1344 jfs_truncate_nolock(old_dir
, 0);
1346 clear_cflag(COMMIT_Stale
, old_dir
);
1349 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1350 IWRITE_UNLOCK(new_ip
);
1352 free_UCSname(&new_dname
);
1354 free_UCSname(&old_dname
);
1356 jfs_info("jfs_rename: returning %d", rc
);
1364 * FUNCTION: Create a special file (device)
1366 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1367 umode_t mode
, dev_t rdev
)
1369 struct jfs_inode_info
*jfs_ip
;
1370 struct btstack btstack
;
1371 struct component_name dname
;
1374 struct inode
*iplist
[2];
1377 struct tblock
*tblk
;
1379 jfs_info("jfs_mknod: %pd", dentry
);
1381 rc
= dquot_initialize(dir
);
1385 if ((rc
= get_UCSname(&dname
, dentry
)))
1388 ip
= ialloc(dir
, mode
);
1393 jfs_ip
= JFS_IP(ip
);
1395 tid
= txBegin(dir
->i_sb
, 0);
1397 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1398 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1400 rc
= jfs_init_acl(tid
, ip
, dir
);
1404 rc
= jfs_init_security(tid
, ip
, dir
, &dentry
->d_name
);
1410 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
1415 tblk
= tid_to_tblock(tid
);
1416 tblk
->xflag
|= COMMIT_CREATE
;
1417 tblk
->ino
= ip
->i_ino
;
1418 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1421 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
))) {
1426 ip
->i_op
= &jfs_file_inode_operations
;
1427 jfs_ip
->dev
= new_encode_dev(rdev
);
1428 init_special_inode(ip
, ip
->i_mode
, rdev
);
1430 mark_inode_dirty(ip
);
1432 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
1434 mark_inode_dirty(dir
);
1438 rc
= txCommit(tid
, 2, iplist
, 0);
1442 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1443 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
1447 unlock_new_inode(ip
);
1450 unlock_new_inode(ip
);
1451 d_instantiate(dentry
, ip
);
1455 free_UCSname(&dname
);
1458 jfs_info("jfs_mknod: returning %d", rc
);
1462 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
1464 struct btstack btstack
;
1467 struct component_name key
;
1470 jfs_info("jfs_lookup: name = %pd", dentry
);
1472 if ((rc
= get_UCSname(&key
, dentry
)))
1474 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1476 if (rc
== -ENOENT
) {
1479 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1482 ip
= jfs_iget(dip
->i_sb
, inum
);
1484 jfs_err("jfs_lookup: iget failed on inum %d", (uint
)inum
);
1487 return d_splice_alias(ip
, dentry
);
1490 static struct inode
*jfs_nfs_get_inode(struct super_block
*sb
,
1491 u64 ino
, u32 generation
)
1493 struct inode
*inode
;
1496 return ERR_PTR(-ESTALE
);
1497 inode
= jfs_iget(sb
, ino
);
1499 return ERR_CAST(inode
);
1501 if (generation
&& inode
->i_generation
!= generation
) {
1503 return ERR_PTR(-ESTALE
);
1509 struct dentry
*jfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
1510 int fh_len
, int fh_type
)
1512 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
1516 struct dentry
*jfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
1517 int fh_len
, int fh_type
)
1519 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
1523 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1525 unsigned long parent_ino
;
1528 le32_to_cpu(JFS_IP(d_inode(dentry
))->i_dtroot
.header
.idotdot
);
1530 return d_obtain_alias(jfs_iget(dentry
->d_sb
, parent_ino
));
1533 const struct inode_operations jfs_dir_inode_operations
= {
1534 .create
= jfs_create
,
1535 .lookup
= jfs_lookup
,
1537 .unlink
= jfs_unlink
,
1538 .symlink
= jfs_symlink
,
1542 .rename
= jfs_rename
,
1543 .listxattr
= jfs_listxattr
,
1544 .setattr
= jfs_setattr
,
1545 #ifdef CONFIG_JFS_POSIX_ACL
1546 .get_acl
= jfs_get_acl
,
1547 .set_acl
= jfs_set_acl
,
1551 const struct file_operations jfs_dir_operations
= {
1552 .read
= generic_read_dir
,
1553 .iterate
= jfs_readdir
,
1555 .unlocked_ioctl
= jfs_ioctl
,
1556 #ifdef CONFIG_COMPAT
1557 .compat_ioctl
= jfs_compat_ioctl
,
1559 .llseek
= generic_file_llseek
,
1562 static int jfs_ci_hash(const struct dentry
*dir
, struct qstr
*this)
1567 hash
= init_name_hash(dir
);
1568 for (i
=0; i
< this->len
; i
++)
1569 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1570 this->hash
= end_name_hash(hash
);
1575 static int jfs_ci_compare(const struct dentry
*dentry
,
1576 unsigned int len
, const char *str
, const struct qstr
*name
)
1580 if (len
!= name
->len
)
1582 for (i
=0; i
< len
; i
++) {
1583 if (tolower(str
[i
]) != tolower(name
->name
[i
]))
1591 static int jfs_ci_revalidate(struct dentry
*dentry
, unsigned int flags
)
1594 * This is not negative dentry. Always valid.
1596 * Note, rename() to existing directory entry will have ->d_inode,
1597 * and will use existing name which isn't specified name by user.
1599 * We may be able to drop this positive dentry here. But dropping
1600 * positive dentry isn't good idea. So it's unsupported like
1601 * rename("filename", "FILENAME") for now.
1603 if (d_really_is_positive(dentry
))
1607 * This may be nfsd (or something), anyway, we can't see the
1608 * intent of this. So, since this can be for creation, drop it.
1614 * Drop the negative dentry, in order to make sure to use the
1615 * case sensitive name which is specified by user if this is
1618 if (flags
& (LOOKUP_CREATE
| LOOKUP_RENAME_TARGET
))
1623 const struct dentry_operations jfs_ci_dentry_operations
=
1625 .d_hash
= jfs_ci_hash
,
1626 .d_compare
= jfs_ci_compare
,
1627 .d_revalidate
= jfs_ci_revalidate
,