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:%s", dip
, dentry
->d_name
.name
);
89 dquot_initialize(dip
);
92 * search parent directory for entry/freespace
93 * (dtSearch() returns parent directory page pinned)
95 if ((rc
= get_UCSname(&dname
, dentry
)))
99 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
100 * block there while holding dtree page, so we allocate the inode &
101 * begin the transaction before we search the directory.
103 ip
= ialloc(dip
, mode
);
109 tid
= txBegin(dip
->i_sb
, 0);
111 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
112 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
114 rc
= jfs_init_acl(tid
, ip
, dip
);
118 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
124 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
125 jfs_err("jfs_create: dtSearch returned %d", rc
);
130 tblk
= tid_to_tblock(tid
);
131 tblk
->xflag
|= COMMIT_CREATE
;
132 tblk
->ino
= ip
->i_ino
;
133 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
139 * initialize the child XAD tree root in-line in inode
144 * create entry in parent directory for child directory
145 * (dtInsert() releases parent directory page)
148 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
150 jfs_err("jfs_create: dtInsert returned -EIO");
151 txAbort(tid
, 1); /* Marks Filesystem dirty */
153 txAbort(tid
, 0); /* Filesystem full */
157 ip
->i_op
= &jfs_file_inode_operations
;
158 ip
->i_fop
= &jfs_file_operations
;
159 ip
->i_mapping
->a_ops
= &jfs_aops
;
161 mark_inode_dirty(ip
);
163 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
165 mark_inode_dirty(dip
);
167 rc
= txCommit(tid
, 2, &iplist
[0], 0);
171 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
172 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
176 unlock_new_inode(ip
);
179 unlock_new_inode(ip
);
180 d_instantiate(dentry
, ip
);
184 free_UCSname(&dname
);
188 jfs_info("jfs_create: rc:%d", rc
);
194 * NAME: jfs_mkdir(dip, dentry, mode)
196 * FUNCTION: create a child directory in the parent directory <dip>
197 * with name = <from dentry> and mode = <mode>
199 * PARAMETER: dip - parent directory vnode
200 * dentry - dentry of child directory
201 * mode - create mode (rwxrwxrwx).
203 * RETURN: Errors from subroutines
206 * EACCESS: user needs search+write permission on the parent directory
208 static int jfs_mkdir(struct inode
*dip
, struct dentry
*dentry
, umode_t mode
)
211 tid_t tid
; /* transaction id */
212 struct inode
*ip
= NULL
; /* child directory inode */
214 struct component_name dname
; /* child directory name */
215 struct btstack btstack
;
216 struct inode
*iplist
[2];
219 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
221 dquot_initialize(dip
);
224 * search parent directory for entry/freespace
225 * (dtSearch() returns parent directory page pinned)
227 if ((rc
= get_UCSname(&dname
, dentry
)))
231 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
232 * block there while holding dtree page, so we allocate the inode &
233 * begin the transaction before we search the directory.
235 ip
= ialloc(dip
, S_IFDIR
| mode
);
241 tid
= txBegin(dip
->i_sb
, 0);
243 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
244 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
246 rc
= jfs_init_acl(tid
, ip
, dip
);
250 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
256 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
257 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
262 tblk
= tid_to_tblock(tid
);
263 tblk
->xflag
|= COMMIT_CREATE
;
264 tblk
->ino
= ip
->i_ino
;
265 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
271 * initialize the child directory in-line in inode
273 dtInitRoot(tid
, ip
, dip
->i_ino
);
276 * create entry in parent directory for child directory
277 * (dtInsert() releases parent directory page)
280 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
282 jfs_err("jfs_mkdir: dtInsert returned -EIO");
283 txAbort(tid
, 1); /* Marks Filesystem dirty */
285 txAbort(tid
, 0); /* Filesystem full */
289 set_nlink(ip
, 2); /* for '.' */
290 ip
->i_op
= &jfs_dir_inode_operations
;
291 ip
->i_fop
= &jfs_dir_operations
;
293 mark_inode_dirty(ip
);
295 /* update parent directory inode */
296 inc_nlink(dip
); /* for '..' from child directory */
297 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
298 mark_inode_dirty(dip
);
300 rc
= txCommit(tid
, 2, &iplist
[0], 0);
304 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
305 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
309 unlock_new_inode(ip
);
312 unlock_new_inode(ip
);
313 d_instantiate(dentry
, ip
);
317 free_UCSname(&dname
);
322 jfs_info("jfs_mkdir: rc:%d", rc
);
327 * NAME: jfs_rmdir(dip, dentry)
329 * FUNCTION: remove a link to child directory
331 * PARAMETER: dip - parent inode
332 * dentry - child directory dentry
334 * RETURN: -EINVAL - if name is . or ..
335 * -EINVAL - if . or .. exist but are invalid.
336 * errors from subroutines
339 * if other threads have the directory open when the last link
340 * is removed, the "." and ".." entries, if present, are removed before
341 * rmdir() returns and no new entries may be created in the directory,
342 * but the directory is not removed until the last reference to
343 * the directory is released (cf.unlink() of regular file).
345 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
348 tid_t tid
; /* transaction id */
349 struct inode
*ip
= dentry
->d_inode
;
351 struct component_name dname
;
352 struct inode
*iplist
[2];
355 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
357 /* Init inode for quota operations. */
358 dquot_initialize(dip
);
359 dquot_initialize(ip
);
361 /* directory must be empty to be removed */
367 if ((rc
= get_UCSname(&dname
, dentry
))) {
371 tid
= txBegin(dip
->i_sb
, 0);
373 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
374 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
379 tblk
= tid_to_tblock(tid
);
380 tblk
->xflag
|= COMMIT_DELETE
;
384 * delete the entry of target directory from parent directory
387 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
388 jfs_err("jfs_rmdir: dtDelete returned %d", rc
);
392 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
393 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
398 /* update parent directory's link count corresponding
399 * to ".." entry of the target directory deleted
401 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
402 inode_dec_link_count(dip
);
405 * OS/2 could have created EA and/or ACL
407 /* free EA from both persistent and working map */
408 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
410 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
412 JFS_IP(ip
)->ea
.flag
= 0;
414 /* free ACL from both persistent and working map */
415 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
417 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
419 JFS_IP(ip
)->acl
.flag
= 0;
421 /* mark the target directory as deleted */
423 mark_inode_dirty(ip
);
425 rc
= txCommit(tid
, 2, &iplist
[0], 0);
429 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
430 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
433 * Truncating the directory index table is not guaranteed. It
434 * may need to be done iteratively
436 if (test_cflag(COMMIT_Stale
, dip
)) {
438 jfs_truncate_nolock(dip
, 0);
440 clear_cflag(COMMIT_Stale
, dip
);
444 free_UCSname(&dname
);
447 jfs_info("jfs_rmdir: rc:%d", rc
);
452 * NAME: jfs_unlink(dip, dentry)
454 * FUNCTION: remove a link to object <vp> named by <name>
455 * from parent directory <dvp>
457 * PARAMETER: dip - inode of parent directory
458 * dentry - dentry of object to be removed
460 * RETURN: errors from subroutines
463 * temporary file: if one or more processes have the file open
464 * when the last link is removed, the link will be removed before
465 * unlink() returns, but the removal of the file contents will be
466 * postponed until all references to the files are closed.
468 * JFS does NOT support unlink() on directories.
471 static int jfs_unlink(struct inode
*dip
, struct dentry
*dentry
)
474 tid_t tid
; /* transaction id */
475 struct inode
*ip
= dentry
->d_inode
;
477 struct component_name dname
; /* object name */
478 struct inode
*iplist
[2];
483 jfs_info("jfs_unlink: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
485 /* Init inode for quota operations. */
486 dquot_initialize(dip
);
487 dquot_initialize(ip
);
489 if ((rc
= get_UCSname(&dname
, dentry
)))
492 IWRITE_LOCK(ip
, RDWRLOCK_NORMAL
);
494 tid
= txBegin(dip
->i_sb
, 0);
496 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
497 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
503 * delete the entry of target file from parent directory
506 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
507 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
509 txAbort(tid
, 1); /* Marks FS Dirty */
511 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
512 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
519 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
520 mark_inode_dirty(dip
);
522 /* update target's inode */
523 inode_dec_link_count(ip
);
526 * commit zero link count object
528 if (ip
->i_nlink
== 0) {
529 assert(!test_cflag(COMMIT_Nolink
, ip
));
530 /* free block resources */
531 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
532 txAbort(tid
, 1); /* Marks FS Dirty */
534 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
535 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
540 tblk
= tid_to_tblock(tid
);
541 tblk
->xflag
|= COMMIT_DELETE
;
546 * Incomplete truncate of file data can
547 * result in timing problems unless we synchronously commit the
551 commit_flag
= COMMIT_SYNC
;
556 * If xtTruncate was incomplete, commit synchronously to avoid
557 * timing complications
559 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
563 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
564 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
566 while (new_size
&& (rc
== 0)) {
567 tid
= txBegin(dip
->i_sb
, 0);
568 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
569 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
571 txAbort(tid
, 1); /* Marks FS Dirty */
574 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
576 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
579 if (ip
->i_nlink
== 0)
580 set_cflag(COMMIT_Nolink
, ip
);
585 * Truncating the directory index table is not guaranteed. It
586 * may need to be done iteratively
588 if (test_cflag(COMMIT_Stale
, dip
)) {
590 jfs_truncate_nolock(dip
, 0);
592 clear_cflag(COMMIT_Stale
, dip
);
596 free_UCSname(&dname
);
598 jfs_info("jfs_unlink: rc:%d", rc
);
603 * NAME: commitZeroLink()
605 * FUNCTION: for non-directory, called by jfs_remove(),
606 * truncate a regular file, directory or symbolic
607 * link to zero length. return 0 if type is not
610 * if the file is currently associated with a VM segment
611 * only permanent disk and inode map resources are freed,
612 * and neither the inode nor indirect blocks are modified
613 * so that the resources can be later freed in the work
615 * if there is no VM segment on entry, the resources are
616 * freed in both work and permanent map.
617 * (? for temporary file - memory object is cached even
618 * after no reference:
619 * reference count > 0 - )
621 * PARAMETERS: cd - pointer to commit data structure.
622 * current inode is the one to truncate.
624 * RETURN: Errors from subroutines
626 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
631 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
633 filetype
= ip
->i_mode
& S_IFMT
;
638 /* fast symbolic link */
639 if (ip
->i_size
< IDATASIZE
) {
645 assert(filetype
!= S_IFDIR
);
649 set_cflag(COMMIT_Freewmap
, ip
);
651 /* mark transaction of block map update type */
652 tblk
= tid_to_tblock(tid
);
653 tblk
->xflag
|= COMMIT_PMAP
;
658 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
659 /* acquire maplock on EA to be freed from block map */
660 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
665 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
666 /* acquire maplock on EA to be freed from block map */
667 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
670 * free xtree/data (truncate to zero length):
671 * free xtree/data pages from cache if COMMIT_PWMAP,
672 * free xtree/data blocks from persistent block map, and
673 * free xtree/data blocks from working block map if COMMIT_PWMAP;
676 return xtTruncate_pmap(tid
, ip
, 0);
683 * NAME: jfs_free_zero_link()
685 * FUNCTION: for non-directory, called by iClose(),
686 * free resources of a file from cache and WORKING map
687 * for a file previously committed with zero link count
688 * while associated with a pager object,
690 * PARAMETER: ip - pointer to inode of file.
692 void jfs_free_zero_link(struct inode
*ip
)
696 jfs_info("jfs_free_zero_link: ip = 0x%p", ip
);
698 /* return if not reg or symbolic link or if size is
701 type
= ip
->i_mode
& S_IFMT
;
707 /* if its contained in inode nothing to do */
708 if (ip
->i_size
< IDATASIZE
)
718 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
719 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
720 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
721 struct maplock maplock
; /* maplock for COMMIT_WMAP */
722 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
724 /* free EA pages from cache */
725 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
727 /* free EA extent from working block map */
729 pxdlock
= (struct pxd_lock
*) & maplock
;
730 pxdlock
->flag
= mlckFREEPXD
;
731 PXDaddress(&pxdlock
->pxd
, xaddr
);
732 PXDlength(&pxdlock
->pxd
, xlen
);
733 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
739 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
740 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
741 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
742 struct maplock maplock
; /* maplock for COMMIT_WMAP */
743 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
745 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
747 /* free ACL extent from working block map */
749 pxdlock
= (struct pxd_lock
*) & maplock
;
750 pxdlock
->flag
= mlckFREEPXD
;
751 PXDaddress(&pxdlock
->pxd
, xaddr
);
752 PXDlength(&pxdlock
->pxd
, xlen
);
753 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
757 * free xtree/data (truncate to zero length):
758 * free xtree/data pages from cache, and
759 * free xtree/data blocks from working block map;
762 xtTruncate(0, ip
, 0, COMMIT_WMAP
);
766 * NAME: jfs_link(vp, dvp, name, crp)
768 * FUNCTION: create a link to <vp> by the name = <name>
769 * in the parent directory <dvp>
771 * PARAMETER: vp - target object
772 * dvp - parent directory of new link
773 * name - name of new link to target object
776 * RETURN: Errors from subroutines
779 * JFS does NOT support link() on directories (to prevent circular
780 * path in the directory hierarchy);
781 * EPERM: the target object is a directory, and either the caller
782 * does not have appropriate privileges or the implementation prohibits
783 * using link() on directories [XPG4.2].
785 * JFS does NOT support links between file systems:
786 * EXDEV: target object and new link are on different file systems and
787 * implementation does not support links between file systems [XPG4.2].
789 static int jfs_link(struct dentry
*old_dentry
,
790 struct inode
*dir
, struct dentry
*dentry
)
794 struct inode
*ip
= old_dentry
->d_inode
;
796 struct component_name dname
;
797 struct btstack btstack
;
798 struct inode
*iplist
[2];
800 jfs_info("jfs_link: %s %s", old_dentry
->d_name
.name
,
801 dentry
->d_name
.name
);
803 dquot_initialize(dir
);
805 tid
= txBegin(ip
->i_sb
, 0);
807 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
808 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
811 * scan parent directory for entry/freespace
813 if ((rc
= get_UCSname(&dname
, dentry
)))
816 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
820 * create entry for new link in parent directory
823 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
826 /* update object inode */
827 inc_nlink(ip
); /* for new link */
828 ip
->i_ctime
= CURRENT_TIME
;
829 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
830 mark_inode_dirty(dir
);
835 rc
= txCommit(tid
, 2, &iplist
[0], 0);
838 drop_nlink(ip
); /* never instantiated */
841 d_instantiate(dentry
, ip
);
844 free_UCSname(&dname
);
849 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
850 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
852 jfs_info("jfs_link: rc:%d", rc
);
857 * NAME: jfs_symlink(dip, dentry, name)
859 * FUNCTION: creates a symbolic link to <symlink> by name <name>
862 * PARAMETER: dip - parent directory vnode
863 * dentry - dentry of symbolic link
864 * name - the path name of the existing object
865 * that will be the source of the link
867 * RETURN: errors from subroutines
870 * ENAMETOOLONG: pathname resolution of a symbolic link produced
871 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
874 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
880 struct component_name dname
;
881 int ssize
; /* source pathname size */
882 struct btstack btstack
;
883 struct inode
*ip
= dentry
->d_inode
;
884 unchar
*i_fastsymlink
;
886 int bmask
= 0, xsize
;
889 struct super_block
*sb
;
892 struct inode
*iplist
[2];
894 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
896 dquot_initialize(dip
);
898 ssize
= strlen(name
) + 1;
901 * search parent directory for entry/freespace
902 * (dtSearch() returns parent directory page pinned)
905 if ((rc
= get_UCSname(&dname
, dentry
)))
909 * allocate on-disk/in-memory inode for symbolic link:
910 * (iAlloc() returns new, locked inode)
912 ip
= ialloc(dip
, S_IFLNK
| 0777);
918 tid
= txBegin(dip
->i_sb
, 0);
920 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
921 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
923 rc
= jfs_init_security(tid
, ip
, dip
, &dentry
->d_name
);
927 tblk
= tid_to_tblock(tid
);
928 tblk
->xflag
|= COMMIT_CREATE
;
929 tblk
->ino
= ip
->i_ino
;
930 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
932 /* fix symlink access permission
933 * (dir_create() ANDs in the u.u_cmask,
934 * but symlinks really need to be 777 access)
939 * write symbolic link target path name
944 * write source path name inline in on-disk inode (fast symbolic link)
947 if (ssize
<= IDATASIZE
) {
948 ip
->i_op
= &jfs_fast_symlink_inode_operations
;
950 i_fastsymlink
= JFS_IP(ip
)->i_inline
;
951 memcpy(i_fastsymlink
, name
, ssize
);
952 ip
->i_size
= ssize
- 1;
955 * if symlink is > 128 bytes, we don't have the space to
956 * store inline extended attributes
958 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
959 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
961 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
965 * write source path name in a single extent
968 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
970 ip
->i_op
= &jfs_symlink_inode_operations
;
971 ip
->i_mapping
->a_ops
= &jfs_aops
;
974 * even though the data of symlink object (source
975 * path name) is treated as non-journaled user data,
976 * it is read/written thru buffer cache for performance.
979 bmask
= JFS_SBI(sb
)->bsize
- 1;
980 xsize
= (ssize
+ bmask
) & ~bmask
;
982 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
983 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
987 ip
->i_size
= ssize
- 1;
989 /* This is kind of silly since PATH_MAX == 4K */
990 int copy_size
= min(ssize
, PSIZE
);
992 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
995 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1000 memcpy(mp
->data
, name
, copy_size
);
1004 xaddr
+= JFS_SBI(sb
)->nbperpage
;
1009 * create entry for symbolic link in parent directory
1011 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
1014 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
1018 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1020 /* discard new inode */
1024 mark_inode_dirty(ip
);
1026 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
1027 mark_inode_dirty(dip
);
1029 * commit update of parent directory and link object
1034 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1038 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1039 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
1043 unlock_new_inode(ip
);
1046 unlock_new_inode(ip
);
1047 d_instantiate(dentry
, ip
);
1051 free_UCSname(&dname
);
1054 jfs_info("jfs_symlink: rc:%d", rc
);
1062 * FUNCTION: rename a file or directory
1064 static int jfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1065 struct inode
*new_dir
, struct dentry
*new_dentry
)
1067 struct btstack btstack
;
1069 struct component_name new_dname
;
1070 struct inode
*new_ip
;
1071 struct component_name old_dname
;
1072 struct inode
*old_ip
;
1076 struct dt_lock
*dtlck
;
1079 struct inode
*iplist
[4];
1080 struct tblock
*tblk
;
1085 jfs_info("jfs_rename: %s %s", old_dentry
->d_name
.name
,
1086 new_dentry
->d_name
.name
);
1088 dquot_initialize(old_dir
);
1089 dquot_initialize(new_dir
);
1091 old_ip
= old_dentry
->d_inode
;
1092 new_ip
= new_dentry
->d_inode
;
1094 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1097 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1101 * Make sure source inode number is what we think it is
1103 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1104 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1110 * Make sure dest inode number (if any) is what we think it is
1112 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1114 if ((!new_ip
) || (ino
!= new_ip
->i_ino
)) {
1118 } else if (rc
!= -ENOENT
)
1121 /* no entry exists, but one was expected */
1126 if (S_ISDIR(old_ip
->i_mode
)) {
1128 if (!dtEmpty(new_ip
)) {
1133 } else if (new_ip
) {
1134 IWRITE_LOCK(new_ip
, RDWRLOCK_NORMAL
);
1135 /* Init inode for quota operations. */
1136 dquot_initialize(new_ip
);
1140 * The real work starts here
1142 tid
= txBegin(new_dir
->i_sb
, 0);
1145 * How do we know the locking is safe from deadlocks?
1146 * The vfs does the hard part for us. Any time we are taking nested
1147 * commit_mutexes, the vfs already has i_mutex held on the parent.
1148 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1150 mutex_lock_nested(&JFS_IP(new_dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1151 mutex_lock_nested(&JFS_IP(old_ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1152 if (old_dir
!= new_dir
)
1153 mutex_lock_nested(&JFS_IP(old_dir
)->commit_mutex
,
1154 COMMIT_MUTEX_SECOND_PARENT
);
1157 mutex_lock_nested(&JFS_IP(new_ip
)->commit_mutex
,
1158 COMMIT_MUTEX_VICTIM
);
1160 * Change existing directory entry to new inode number
1162 ino
= new_ip
->i_ino
;
1163 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1164 old_ip
->i_ino
, JFS_RENAME
);
1168 if (S_ISDIR(new_ip
->i_mode
)) {
1170 if (new_ip
->i_nlink
) {
1171 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1172 if (old_dir
!= new_dir
)
1173 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1174 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1175 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1176 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1177 IWRITE_UNLOCK(new_ip
);
1178 jfs_error(new_ip
->i_sb
,
1179 "new_ip->i_nlink != 0\n");
1182 tblk
= tid_to_tblock(tid
);
1183 tblk
->xflag
|= COMMIT_DELETE
;
1184 tblk
->u
.ip
= new_ip
;
1185 } else if (new_ip
->i_nlink
== 0) {
1186 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1187 /* free block resources */
1188 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1189 txAbort(tid
, 1); /* Marks FS Dirty */
1193 tblk
= tid_to_tblock(tid
);
1194 tblk
->xflag
|= COMMIT_DELETE
;
1195 tblk
->u
.ip
= new_ip
;
1197 new_ip
->i_ctime
= CURRENT_TIME
;
1198 mark_inode_dirty(new_ip
);
1202 * Add new directory entry
1204 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1207 jfs_err("jfs_rename didn't expect dtSearch to fail "
1212 ino
= old_ip
->i_ino
;
1213 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1216 jfs_err("jfs_rename: dtInsert returned -EIO");
1219 if (S_ISDIR(old_ip
->i_mode
))
1223 * Remove old directory entry
1226 ino
= old_ip
->i_ino
;
1227 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1229 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1231 txAbort(tid
, 1); /* Marks Filesystem dirty */
1234 if (S_ISDIR(old_ip
->i_mode
)) {
1235 drop_nlink(old_dir
);
1236 if (old_dir
!= new_dir
) {
1238 * Change inode number of parent for moved directory
1241 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1242 cpu_to_le32(new_dir
->i_ino
);
1244 /* Linelock header of dtree */
1245 tlck
= txLock(tid
, old_ip
,
1246 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1247 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1248 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1249 ASSERT(dtlck
->index
== 0);
1250 lv
= & dtlck
->lv
[0];
1258 * Update ctime on changed/moved inodes & mark dirty
1260 old_ip
->i_ctime
= CURRENT_TIME
;
1261 mark_inode_dirty(old_ip
);
1263 new_dir
->i_ctime
= new_dir
->i_mtime
= current_fs_time(new_dir
->i_sb
);
1264 mark_inode_dirty(new_dir
);
1266 /* Build list of inodes modified by this transaction */
1268 iplist
[ipcount
++] = old_ip
;
1270 iplist
[ipcount
++] = new_ip
;
1271 iplist
[ipcount
++] = old_dir
;
1273 if (old_dir
!= new_dir
) {
1274 iplist
[ipcount
++] = new_dir
;
1275 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1276 mark_inode_dirty(old_dir
);
1280 * Incomplete truncate of file data can
1281 * result in timing problems unless we synchronously commit the
1285 commit_flag
= COMMIT_SYNC
;
1289 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1294 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1295 if (old_dir
!= new_dir
)
1296 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1297 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1298 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1300 while (new_size
&& (rc
== 0)) {
1301 tid
= txBegin(new_ip
->i_sb
, 0);
1302 mutex_lock(&JFS_IP(new_ip
)->commit_mutex
);
1303 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1308 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1310 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1312 if (new_ip
&& (new_ip
->i_nlink
== 0))
1313 set_cflag(COMMIT_Nolink
, new_ip
);
1315 free_UCSname(&new_dname
);
1317 free_UCSname(&old_dname
);
1319 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1320 IWRITE_UNLOCK(new_ip
);
1322 * Truncating the directory index table is not guaranteed. It
1323 * may need to be done iteratively
1325 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1326 if (old_dir
->i_size
> 1)
1327 jfs_truncate_nolock(old_dir
, 0);
1329 clear_cflag(COMMIT_Stale
, old_dir
);
1332 jfs_info("jfs_rename: returning %d", rc
);
1340 * FUNCTION: Create a special file (device)
1342 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1343 umode_t mode
, dev_t rdev
)
1345 struct jfs_inode_info
*jfs_ip
;
1346 struct btstack btstack
;
1347 struct component_name dname
;
1350 struct inode
*iplist
[2];
1353 struct tblock
*tblk
;
1355 if (!new_valid_dev(rdev
))
1358 jfs_info("jfs_mknod: %s", dentry
->d_name
.name
);
1360 dquot_initialize(dir
);
1362 if ((rc
= get_UCSname(&dname
, dentry
)))
1365 ip
= ialloc(dir
, mode
);
1370 jfs_ip
= JFS_IP(ip
);
1372 tid
= txBegin(dir
->i_sb
, 0);
1374 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1375 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1377 rc
= jfs_init_acl(tid
, ip
, dir
);
1381 rc
= jfs_init_security(tid
, ip
, dir
, &dentry
->d_name
);
1387 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
1392 tblk
= tid_to_tblock(tid
);
1393 tblk
->xflag
|= COMMIT_CREATE
;
1394 tblk
->ino
= ip
->i_ino
;
1395 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1398 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
))) {
1403 ip
->i_op
= &jfs_file_inode_operations
;
1404 jfs_ip
->dev
= new_encode_dev(rdev
);
1405 init_special_inode(ip
, ip
->i_mode
, rdev
);
1407 mark_inode_dirty(ip
);
1409 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
1411 mark_inode_dirty(dir
);
1415 rc
= txCommit(tid
, 2, iplist
, 0);
1419 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1420 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
1424 unlock_new_inode(ip
);
1427 unlock_new_inode(ip
);
1428 d_instantiate(dentry
, ip
);
1432 free_UCSname(&dname
);
1435 jfs_info("jfs_mknod: returning %d", rc
);
1439 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, unsigned int flags
)
1441 struct btstack btstack
;
1444 struct component_name key
;
1447 jfs_info("jfs_lookup: name = %s", dentry
->d_name
.name
);
1449 if ((rc
= get_UCSname(&key
, dentry
)))
1451 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1453 if (rc
== -ENOENT
) {
1456 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1459 ip
= jfs_iget(dip
->i_sb
, inum
);
1461 jfs_err("jfs_lookup: iget failed on inum %d", (uint
)inum
);
1464 return d_splice_alias(ip
, dentry
);
1467 static struct inode
*jfs_nfs_get_inode(struct super_block
*sb
,
1468 u64 ino
, u32 generation
)
1470 struct inode
*inode
;
1473 return ERR_PTR(-ESTALE
);
1474 inode
= jfs_iget(sb
, ino
);
1476 return ERR_CAST(inode
);
1478 if (generation
&& inode
->i_generation
!= generation
) {
1480 return ERR_PTR(-ESTALE
);
1486 struct dentry
*jfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
1487 int fh_len
, int fh_type
)
1489 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
1493 struct dentry
*jfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
1494 int fh_len
, int fh_type
)
1496 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
1500 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1502 unsigned long parent_ino
;
1505 le32_to_cpu(JFS_IP(dentry
->d_inode
)->i_dtroot
.header
.idotdot
);
1507 return d_obtain_alias(jfs_iget(dentry
->d_inode
->i_sb
, parent_ino
));
1510 const struct inode_operations jfs_dir_inode_operations
= {
1511 .create
= jfs_create
,
1512 .lookup
= jfs_lookup
,
1514 .unlink
= jfs_unlink
,
1515 .symlink
= jfs_symlink
,
1519 .rename
= jfs_rename
,
1520 .setxattr
= jfs_setxattr
,
1521 .getxattr
= jfs_getxattr
,
1522 .listxattr
= jfs_listxattr
,
1523 .removexattr
= jfs_removexattr
,
1524 .setattr
= jfs_setattr
,
1525 #ifdef CONFIG_JFS_POSIX_ACL
1526 .get_acl
= jfs_get_acl
,
1527 .set_acl
= jfs_set_acl
,
1531 const struct file_operations jfs_dir_operations
= {
1532 .read
= generic_read_dir
,
1533 .iterate
= jfs_readdir
,
1535 .unlocked_ioctl
= jfs_ioctl
,
1536 #ifdef CONFIG_COMPAT
1537 .compat_ioctl
= jfs_compat_ioctl
,
1539 .llseek
= generic_file_llseek
,
1542 static int jfs_ci_hash(const struct dentry
*dir
, struct qstr
*this)
1547 hash
= init_name_hash();
1548 for (i
=0; i
< this->len
; i
++)
1549 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1550 this->hash
= end_name_hash(hash
);
1555 static int jfs_ci_compare(const struct dentry
*parent
, const struct dentry
*dentry
,
1556 unsigned int len
, const char *str
, const struct qstr
*name
)
1560 if (len
!= name
->len
)
1562 for (i
=0; i
< len
; i
++) {
1563 if (tolower(str
[i
]) != tolower(name
->name
[i
]))
1571 static int jfs_ci_revalidate(struct dentry
*dentry
, unsigned int flags
)
1574 * This is not negative dentry. Always valid.
1576 * Note, rename() to existing directory entry will have ->d_inode,
1577 * and will use existing name which isn't specified name by user.
1579 * We may be able to drop this positive dentry here. But dropping
1580 * positive dentry isn't good idea. So it's unsupported like
1581 * rename("filename", "FILENAME") for now.
1583 if (dentry
->d_inode
)
1587 * This may be nfsd (or something), anyway, we can't see the
1588 * intent of this. So, since this can be for creation, drop it.
1594 * Drop the negative dentry, in order to make sure to use the
1595 * case sensitive name which is specified by user if this is
1598 if (flags
& (LOOKUP_CREATE
| LOOKUP_RENAME_TARGET
))
1603 const struct dentry_operations jfs_ci_dentry_operations
=
1605 .d_hash
= jfs_ci_hash
,
1606 .d_compare
= jfs_ci_compare
,
1607 .d_revalidate
= jfs_ci_revalidate
,