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/ctype.h>
22 #include <linux/quotaops.h>
23 #include <linux/exportfs.h>
24 #include "jfs_incore.h"
25 #include "jfs_superblock.h"
26 #include "jfs_inode.h"
27 #include "jfs_dinode.h"
29 #include "jfs_unicode.h"
30 #include "jfs_metapage.h"
31 #include "jfs_xattr.h"
33 #include "jfs_debug.h"
38 const struct dentry_operations jfs_ci_dentry_operations
;
40 static s64
commitZeroLink(tid_t
, struct inode
*);
43 * NAME: free_ea_wmap(inode)
45 * FUNCTION: free uncommitted extended attributes from working map
48 static inline void free_ea_wmap(struct inode
*inode
)
50 dxd_t
*ea
= &JFS_IP(inode
)->ea
;
52 if (ea
->flag
& DXD_EXTENT
) {
53 /* free EA pages from cache */
54 invalidate_dxd_metapages(inode
, *ea
);
55 dbFree(inode
, addressDXD(ea
), lengthDXD(ea
));
61 * NAME: jfs_create(dip, dentry, mode)
63 * FUNCTION: create a regular file in the parent directory <dip>
64 * with name = <from dentry> and mode = <mode>
66 * PARAMETER: dip - parent directory vnode
67 * dentry - dentry of new file
68 * mode - create mode (rwxrwxrwx).
71 * RETURN: Errors from subroutines
74 static int jfs_create(struct inode
*dip
, struct dentry
*dentry
, int mode
,
78 tid_t tid
; /* transaction id */
79 struct inode
*ip
= NULL
; /* child directory inode */
81 struct component_name dname
; /* child directory name */
82 struct btstack btstack
;
83 struct inode
*iplist
[2];
86 jfs_info("jfs_create: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
88 dquot_initialize(dip
);
91 * search parent directory for entry/freespace
92 * (dtSearch() returns parent directory page pinned)
94 if ((rc
= get_UCSname(&dname
, dentry
)))
98 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
99 * block there while holding dtree page, so we allocate the inode &
100 * begin the transaction before we search the directory.
102 ip
= ialloc(dip
, mode
);
108 tid
= txBegin(dip
->i_sb
, 0);
110 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
111 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
113 rc
= jfs_init_acl(tid
, ip
, dip
);
117 rc
= jfs_init_security(tid
, ip
, dip
);
123 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
124 jfs_err("jfs_create: dtSearch returned %d", rc
);
129 tblk
= tid_to_tblock(tid
);
130 tblk
->xflag
|= COMMIT_CREATE
;
131 tblk
->ino
= ip
->i_ino
;
132 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
138 * initialize the child XAD tree root in-line in inode
143 * create entry in parent directory for child directory
144 * (dtInsert() releases parent directory page)
147 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
149 jfs_err("jfs_create: dtInsert returned -EIO");
150 txAbort(tid
, 1); /* Marks Filesystem dirty */
152 txAbort(tid
, 0); /* Filesystem full */
156 ip
->i_op
= &jfs_file_inode_operations
;
157 ip
->i_fop
= &jfs_file_operations
;
158 ip
->i_mapping
->a_ops
= &jfs_aops
;
160 mark_inode_dirty(ip
);
162 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
164 mark_inode_dirty(dip
);
166 rc
= txCommit(tid
, 2, &iplist
[0], 0);
170 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
171 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
175 unlock_new_inode(ip
);
178 d_instantiate(dentry
, ip
);
179 unlock_new_inode(ip
);
183 free_UCSname(&dname
);
187 jfs_info("jfs_create: rc:%d", rc
);
193 * NAME: jfs_mkdir(dip, dentry, mode)
195 * FUNCTION: create a child directory in the parent directory <dip>
196 * with name = <from dentry> and mode = <mode>
198 * PARAMETER: dip - parent directory vnode
199 * dentry - dentry of child directory
200 * mode - create mode (rwxrwxrwx).
202 * RETURN: Errors from subroutines
205 * EACCESS: user needs search+write permission on the parent directory
207 static int jfs_mkdir(struct inode
*dip
, struct dentry
*dentry
, int mode
)
210 tid_t tid
; /* transaction id */
211 struct inode
*ip
= NULL
; /* child directory inode */
213 struct component_name dname
; /* child directory name */
214 struct btstack btstack
;
215 struct inode
*iplist
[2];
218 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
220 dquot_initialize(dip
);
222 /* link count overflow on parent directory ? */
223 if (dip
->i_nlink
== JFS_LINK_MAX
) {
229 * search parent directory for entry/freespace
230 * (dtSearch() returns parent directory page pinned)
232 if ((rc
= get_UCSname(&dname
, dentry
)))
236 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
237 * block there while holding dtree page, so we allocate the inode &
238 * begin the transaction before we search the directory.
240 ip
= ialloc(dip
, S_IFDIR
| mode
);
246 tid
= txBegin(dip
->i_sb
, 0);
248 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
249 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
251 rc
= jfs_init_acl(tid
, ip
, dip
);
255 rc
= jfs_init_security(tid
, ip
, dip
);
261 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
262 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
267 tblk
= tid_to_tblock(tid
);
268 tblk
->xflag
|= COMMIT_CREATE
;
269 tblk
->ino
= ip
->i_ino
;
270 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
276 * initialize the child directory in-line in inode
278 dtInitRoot(tid
, ip
, dip
->i_ino
);
281 * create entry in parent directory for child directory
282 * (dtInsert() releases parent directory page)
285 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
287 jfs_err("jfs_mkdir: dtInsert returned -EIO");
288 txAbort(tid
, 1); /* Marks Filesystem dirty */
290 txAbort(tid
, 0); /* Filesystem full */
294 ip
->i_nlink
= 2; /* for '.' */
295 ip
->i_op
= &jfs_dir_inode_operations
;
296 ip
->i_fop
= &jfs_dir_operations
;
298 mark_inode_dirty(ip
);
300 /* update parent directory inode */
301 inc_nlink(dip
); /* for '..' from child directory */
302 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
303 mark_inode_dirty(dip
);
305 rc
= txCommit(tid
, 2, &iplist
[0], 0);
309 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
310 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
314 unlock_new_inode(ip
);
317 d_instantiate(dentry
, ip
);
318 unlock_new_inode(ip
);
322 free_UCSname(&dname
);
327 jfs_info("jfs_mkdir: rc:%d", rc
);
332 * NAME: jfs_rmdir(dip, dentry)
334 * FUNCTION: remove a link to child directory
336 * PARAMETER: dip - parent inode
337 * dentry - child directory dentry
339 * RETURN: -EINVAL - if name is . or ..
340 * -EINVAL - if . or .. exist but are invalid.
341 * errors from subroutines
344 * if other threads have the directory open when the last link
345 * is removed, the "." and ".." entries, if present, are removed before
346 * rmdir() returns and no new entries may be created in the directory,
347 * but the directory is not removed until the last reference to
348 * the directory is released (cf.unlink() of regular file).
350 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
353 tid_t tid
; /* transaction id */
354 struct inode
*ip
= dentry
->d_inode
;
356 struct component_name dname
;
357 struct inode
*iplist
[2];
360 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
362 /* Init inode for quota operations. */
363 dquot_initialize(dip
);
364 dquot_initialize(ip
);
366 /* directory must be empty to be removed */
372 if ((rc
= get_UCSname(&dname
, dentry
))) {
376 tid
= txBegin(dip
->i_sb
, 0);
378 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
379 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
384 tblk
= tid_to_tblock(tid
);
385 tblk
->xflag
|= COMMIT_DELETE
;
389 * delete the entry of target directory from parent directory
392 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
393 jfs_err("jfs_rmdir: dtDelete returned %d", rc
);
397 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
398 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
403 /* update parent directory's link count corresponding
404 * to ".." entry of the target directory deleted
406 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
407 inode_dec_link_count(dip
);
410 * OS/2 could have created EA and/or ACL
412 /* free EA from both persistent and working map */
413 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
415 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
417 JFS_IP(ip
)->ea
.flag
= 0;
419 /* free ACL from both persistent and working map */
420 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
422 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
424 JFS_IP(ip
)->acl
.flag
= 0;
426 /* mark the target directory as deleted */
428 mark_inode_dirty(ip
);
430 rc
= txCommit(tid
, 2, &iplist
[0], 0);
434 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
435 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
438 * Truncating the directory index table is not guaranteed. It
439 * may need to be done iteratively
441 if (test_cflag(COMMIT_Stale
, dip
)) {
443 jfs_truncate_nolock(dip
, 0);
445 clear_cflag(COMMIT_Stale
, dip
);
449 free_UCSname(&dname
);
452 jfs_info("jfs_rmdir: rc:%d", rc
);
457 * NAME: jfs_unlink(dip, dentry)
459 * FUNCTION: remove a link to object <vp> named by <name>
460 * from parent directory <dvp>
462 * PARAMETER: dip - inode of parent directory
463 * dentry - dentry of object to be removed
465 * RETURN: errors from subroutines
468 * temporary file: if one or more processes have the file open
469 * when the last link is removed, the link will be removed before
470 * unlink() returns, but the removal of the file contents will be
471 * postponed until all references to the files are closed.
473 * JFS does NOT support unlink() on directories.
476 static int jfs_unlink(struct inode
*dip
, struct dentry
*dentry
)
479 tid_t tid
; /* transaction id */
480 struct inode
*ip
= dentry
->d_inode
;
482 struct component_name dname
; /* object name */
483 struct inode
*iplist
[2];
488 jfs_info("jfs_unlink: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
490 /* Init inode for quota operations. */
491 dquot_initialize(dip
);
492 dquot_initialize(ip
);
494 if ((rc
= get_UCSname(&dname
, dentry
)))
497 IWRITE_LOCK(ip
, RDWRLOCK_NORMAL
);
499 tid
= txBegin(dip
->i_sb
, 0);
501 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
502 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
508 * delete the entry of target file from parent directory
511 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
512 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
514 txAbort(tid
, 1); /* Marks FS Dirty */
516 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
517 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
524 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
525 mark_inode_dirty(dip
);
527 /* update target's inode */
528 inode_dec_link_count(ip
);
531 * commit zero link count object
533 if (ip
->i_nlink
== 0) {
534 assert(!test_cflag(COMMIT_Nolink
, ip
));
535 /* free block resources */
536 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
537 txAbort(tid
, 1); /* Marks FS Dirty */
539 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
540 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
545 tblk
= tid_to_tblock(tid
);
546 tblk
->xflag
|= COMMIT_DELETE
;
551 * Incomplete truncate of file data can
552 * result in timing problems unless we synchronously commit the
556 commit_flag
= COMMIT_SYNC
;
561 * If xtTruncate was incomplete, commit synchronously to avoid
562 * timing complications
564 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
568 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
569 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
571 while (new_size
&& (rc
== 0)) {
572 tid
= txBegin(dip
->i_sb
, 0);
573 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
574 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
576 txAbort(tid
, 1); /* Marks FS Dirty */
579 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
581 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
584 if (ip
->i_nlink
== 0)
585 set_cflag(COMMIT_Nolink
, ip
);
590 * Truncating the directory index table is not guaranteed. It
591 * may need to be done iteratively
593 if (test_cflag(COMMIT_Stale
, dip
)) {
595 jfs_truncate_nolock(dip
, 0);
597 clear_cflag(COMMIT_Stale
, dip
);
601 free_UCSname(&dname
);
603 jfs_info("jfs_unlink: rc:%d", rc
);
608 * NAME: commitZeroLink()
610 * FUNCTION: for non-directory, called by jfs_remove(),
611 * truncate a regular file, directory or symbolic
612 * link to zero length. return 0 if type is not
615 * if the file is currently associated with a VM segment
616 * only permanent disk and inode map resources are freed,
617 * and neither the inode nor indirect blocks are modified
618 * so that the resources can be later freed in the work
620 * if there is no VM segment on entry, the resources are
621 * freed in both work and permanent map.
622 * (? for temporary file - memory object is cached even
623 * after no reference:
624 * reference count > 0 - )
626 * PARAMETERS: cd - pointer to commit data structure.
627 * current inode is the one to truncate.
629 * RETURN: Errors from subroutines
631 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
636 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
638 filetype
= ip
->i_mode
& S_IFMT
;
643 /* fast symbolic link */
644 if (ip
->i_size
< IDATASIZE
) {
650 assert(filetype
!= S_IFDIR
);
654 set_cflag(COMMIT_Freewmap
, ip
);
656 /* mark transaction of block map update type */
657 tblk
= tid_to_tblock(tid
);
658 tblk
->xflag
|= COMMIT_PMAP
;
663 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
664 /* acquire maplock on EA to be freed from block map */
665 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
670 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
671 /* acquire maplock on EA to be freed from block map */
672 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
675 * free xtree/data (truncate to zero length):
676 * free xtree/data pages from cache if COMMIT_PWMAP,
677 * free xtree/data blocks from persistent block map, and
678 * free xtree/data blocks from working block map if COMMIT_PWMAP;
681 return xtTruncate_pmap(tid
, ip
, 0);
688 * NAME: jfs_free_zero_link()
690 * FUNCTION: for non-directory, called by iClose(),
691 * free resources of a file from cache and WORKING map
692 * for a file previously committed with zero link count
693 * while associated with a pager object,
695 * PARAMETER: ip - pointer to inode of file.
697 void jfs_free_zero_link(struct inode
*ip
)
701 jfs_info("jfs_free_zero_link: ip = 0x%p", ip
);
703 /* return if not reg or symbolic link or if size is
706 type
= ip
->i_mode
& S_IFMT
;
712 /* if its contained in inode nothing to do */
713 if (ip
->i_size
< IDATASIZE
)
723 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
724 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
725 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
726 struct maplock maplock
; /* maplock for COMMIT_WMAP */
727 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
729 /* free EA pages from cache */
730 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
732 /* free EA extent from working block map */
734 pxdlock
= (struct pxd_lock
*) & maplock
;
735 pxdlock
->flag
= mlckFREEPXD
;
736 PXDaddress(&pxdlock
->pxd
, xaddr
);
737 PXDlength(&pxdlock
->pxd
, xlen
);
738 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
744 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
745 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
746 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
747 struct maplock maplock
; /* maplock for COMMIT_WMAP */
748 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
750 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
752 /* free ACL extent from working block map */
754 pxdlock
= (struct pxd_lock
*) & maplock
;
755 pxdlock
->flag
= mlckFREEPXD
;
756 PXDaddress(&pxdlock
->pxd
, xaddr
);
757 PXDlength(&pxdlock
->pxd
, xlen
);
758 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
762 * free xtree/data (truncate to zero length):
763 * free xtree/data pages from cache, and
764 * free xtree/data blocks from working block map;
767 xtTruncate(0, ip
, 0, COMMIT_WMAP
);
771 * NAME: jfs_link(vp, dvp, name, crp)
773 * FUNCTION: create a link to <vp> by the name = <name>
774 * in the parent directory <dvp>
776 * PARAMETER: vp - target object
777 * dvp - parent directory of new link
778 * name - name of new link to target object
781 * RETURN: Errors from subroutines
784 * JFS does NOT support link() on directories (to prevent circular
785 * path in the directory hierarchy);
786 * EPERM: the target object is a directory, and either the caller
787 * does not have appropriate privileges or the implementation prohibits
788 * using link() on directories [XPG4.2].
790 * JFS does NOT support links between file systems:
791 * EXDEV: target object and new link are on different file systems and
792 * implementation does not support links between file systems [XPG4.2].
794 static int jfs_link(struct dentry
*old_dentry
,
795 struct inode
*dir
, struct dentry
*dentry
)
799 struct inode
*ip
= old_dentry
->d_inode
;
801 struct component_name dname
;
802 struct btstack btstack
;
803 struct inode
*iplist
[2];
805 jfs_info("jfs_link: %s %s", old_dentry
->d_name
.name
,
806 dentry
->d_name
.name
);
808 if (ip
->i_nlink
== JFS_LINK_MAX
)
811 if (ip
->i_nlink
== 0)
814 dquot_initialize(dir
);
816 tid
= txBegin(ip
->i_sb
, 0);
818 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
819 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
822 * scan parent directory for entry/freespace
824 if ((rc
= get_UCSname(&dname
, dentry
)))
827 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
831 * create entry for new link in parent directory
834 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
837 /* update object inode */
838 inc_nlink(ip
); /* for new link */
839 ip
->i_ctime
= CURRENT_TIME
;
840 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
841 mark_inode_dirty(dir
);
842 atomic_inc(&ip
->i_count
);
846 rc
= txCommit(tid
, 2, &iplist
[0], 0);
849 ip
->i_nlink
--; /* never instantiated */
852 d_instantiate(dentry
, ip
);
855 free_UCSname(&dname
);
860 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
861 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
863 jfs_info("jfs_link: rc:%d", rc
);
868 * NAME: jfs_symlink(dip, dentry, name)
870 * FUNCTION: creates a symbolic link to <symlink> by name <name>
873 * PARAMETER: dip - parent directory vnode
874 * dentry - dentry of symbolic link
875 * name - the path name of the existing object
876 * that will be the source of the link
878 * RETURN: errors from subroutines
881 * ENAMETOOLONG: pathname resolution of a symbolic link produced
882 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
885 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
891 struct component_name dname
;
892 int ssize
; /* source pathname size */
893 struct btstack btstack
;
894 struct inode
*ip
= dentry
->d_inode
;
895 unchar
*i_fastsymlink
;
897 int bmask
= 0, xsize
;
898 s64 extent
= 0, xaddr
;
900 struct super_block
*sb
;
903 struct inode
*iplist
[2];
905 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
907 dquot_initialize(dip
);
909 ssize
= strlen(name
) + 1;
912 * search parent directory for entry/freespace
913 * (dtSearch() returns parent directory page pinned)
916 if ((rc
= get_UCSname(&dname
, dentry
)))
920 * allocate on-disk/in-memory inode for symbolic link:
921 * (iAlloc() returns new, locked inode)
923 ip
= ialloc(dip
, S_IFLNK
| 0777);
929 tid
= txBegin(dip
->i_sb
, 0);
931 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
932 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
934 rc
= jfs_init_security(tid
, ip
, dip
);
938 tblk
= tid_to_tblock(tid
);
939 tblk
->xflag
|= COMMIT_CREATE
;
940 tblk
->ino
= ip
->i_ino
;
941 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
943 /* fix symlink access permission
944 * (dir_create() ANDs in the u.u_cmask,
945 * but symlinks really need to be 777 access)
950 * write symbolic link target path name
955 * write source path name inline in on-disk inode (fast symbolic link)
958 if (ssize
<= IDATASIZE
) {
959 ip
->i_op
= &jfs_fast_symlink_inode_operations
;
961 i_fastsymlink
= JFS_IP(ip
)->i_inline
;
962 memcpy(i_fastsymlink
, name
, ssize
);
963 ip
->i_size
= ssize
- 1;
966 * if symlink is > 128 bytes, we don't have the space to
967 * store inline extended attributes
969 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
970 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
972 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
976 * write source path name in a single extent
979 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
981 ip
->i_op
= &jfs_symlink_inode_operations
;
982 ip
->i_mapping
->a_ops
= &jfs_aops
;
985 * even though the data of symlink object (source
986 * path name) is treated as non-journaled user data,
987 * it is read/written thru buffer cache for performance.
990 bmask
= JFS_SBI(sb
)->bsize
- 1;
991 xsize
= (ssize
+ bmask
) & ~bmask
;
993 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
994 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
999 ip
->i_size
= ssize
- 1;
1001 /* This is kind of silly since PATH_MAX == 4K */
1002 int copy_size
= min(ssize
, PSIZE
);
1004 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
1007 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1012 memcpy(mp
->data
, name
, copy_size
);
1016 xaddr
+= JFS_SBI(sb
)->nbperpage
;
1021 * create entry for symbolic link in parent directory
1023 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
1026 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
1030 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1032 /* discard new inode */
1036 mark_inode_dirty(ip
);
1038 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
1039 mark_inode_dirty(dip
);
1041 * commit update of parent directory and link object
1046 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1050 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1051 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
1055 unlock_new_inode(ip
);
1058 d_instantiate(dentry
, ip
);
1059 unlock_new_inode(ip
);
1063 free_UCSname(&dname
);
1066 jfs_info("jfs_symlink: rc:%d", rc
);
1074 * FUNCTION: rename a file or directory
1076 static int jfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1077 struct inode
*new_dir
, struct dentry
*new_dentry
)
1079 struct btstack btstack
;
1081 struct component_name new_dname
;
1082 struct inode
*new_ip
;
1083 struct component_name old_dname
;
1084 struct inode
*old_ip
;
1088 struct dt_lock
*dtlck
;
1091 struct inode
*iplist
[4];
1092 struct tblock
*tblk
;
1097 jfs_info("jfs_rename: %s %s", old_dentry
->d_name
.name
,
1098 new_dentry
->d_name
.name
);
1100 dquot_initialize(old_dir
);
1101 dquot_initialize(new_dir
);
1103 old_ip
= old_dentry
->d_inode
;
1104 new_ip
= new_dentry
->d_inode
;
1106 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1109 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1113 * Make sure source inode number is what we think it is
1115 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1116 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1122 * Make sure dest inode number (if any) is what we think it is
1124 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1126 if ((!new_ip
) || (ino
!= new_ip
->i_ino
)) {
1130 } else if (rc
!= -ENOENT
)
1133 /* no entry exists, but one was expected */
1138 if (S_ISDIR(old_ip
->i_mode
)) {
1140 if (!dtEmpty(new_ip
)) {
1144 } else if ((new_dir
!= old_dir
) &&
1145 (new_dir
->i_nlink
== JFS_LINK_MAX
)) {
1149 } else if (new_ip
) {
1150 IWRITE_LOCK(new_ip
, RDWRLOCK_NORMAL
);
1151 /* Init inode for quota operations. */
1152 dquot_initialize(new_ip
);
1156 * The real work starts here
1158 tid
= txBegin(new_dir
->i_sb
, 0);
1161 * How do we know the locking is safe from deadlocks?
1162 * The vfs does the hard part for us. Any time we are taking nested
1163 * commit_mutexes, the vfs already has i_mutex held on the parent.
1164 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1166 mutex_lock_nested(&JFS_IP(new_dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1167 mutex_lock_nested(&JFS_IP(old_ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1168 if (old_dir
!= new_dir
)
1169 mutex_lock_nested(&JFS_IP(old_dir
)->commit_mutex
,
1170 COMMIT_MUTEX_SECOND_PARENT
);
1173 mutex_lock_nested(&JFS_IP(new_ip
)->commit_mutex
,
1174 COMMIT_MUTEX_VICTIM
);
1176 * Change existing directory entry to new inode number
1178 ino
= new_ip
->i_ino
;
1179 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1180 old_ip
->i_ino
, JFS_RENAME
);
1184 if (S_ISDIR(new_ip
->i_mode
)) {
1186 if (new_ip
->i_nlink
) {
1187 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1188 if (old_dir
!= new_dir
)
1189 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1190 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1191 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1192 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1193 IWRITE_UNLOCK(new_ip
);
1194 jfs_error(new_ip
->i_sb
,
1195 "jfs_rename: new_ip->i_nlink != 0");
1198 tblk
= tid_to_tblock(tid
);
1199 tblk
->xflag
|= COMMIT_DELETE
;
1200 tblk
->u
.ip
= new_ip
;
1201 } else if (new_ip
->i_nlink
== 0) {
1202 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1203 /* free block resources */
1204 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1205 txAbort(tid
, 1); /* Marks FS Dirty */
1209 tblk
= tid_to_tblock(tid
);
1210 tblk
->xflag
|= COMMIT_DELETE
;
1211 tblk
->u
.ip
= new_ip
;
1213 new_ip
->i_ctime
= CURRENT_TIME
;
1214 mark_inode_dirty(new_ip
);
1218 * Add new directory entry
1220 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1223 jfs_err("jfs_rename didn't expect dtSearch to fail "
1228 ino
= old_ip
->i_ino
;
1229 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1232 jfs_err("jfs_rename: dtInsert returned -EIO");
1235 if (S_ISDIR(old_ip
->i_mode
))
1239 * Remove old directory entry
1242 ino
= old_ip
->i_ino
;
1243 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1245 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1247 txAbort(tid
, 1); /* Marks Filesystem dirty */
1250 if (S_ISDIR(old_ip
->i_mode
)) {
1251 drop_nlink(old_dir
);
1252 if (old_dir
!= new_dir
) {
1254 * Change inode number of parent for moved directory
1257 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1258 cpu_to_le32(new_dir
->i_ino
);
1260 /* Linelock header of dtree */
1261 tlck
= txLock(tid
, old_ip
,
1262 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1263 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1264 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1265 ASSERT(dtlck
->index
== 0);
1266 lv
= & dtlck
->lv
[0];
1274 * Update ctime on changed/moved inodes & mark dirty
1276 old_ip
->i_ctime
= CURRENT_TIME
;
1277 mark_inode_dirty(old_ip
);
1279 new_dir
->i_ctime
= new_dir
->i_mtime
= current_fs_time(new_dir
->i_sb
);
1280 mark_inode_dirty(new_dir
);
1282 /* Build list of inodes modified by this transaction */
1284 iplist
[ipcount
++] = old_ip
;
1286 iplist
[ipcount
++] = new_ip
;
1287 iplist
[ipcount
++] = old_dir
;
1289 if (old_dir
!= new_dir
) {
1290 iplist
[ipcount
++] = new_dir
;
1291 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1292 mark_inode_dirty(old_dir
);
1296 * Incomplete truncate of file data can
1297 * result in timing problems unless we synchronously commit the
1301 commit_flag
= COMMIT_SYNC
;
1305 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1310 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1311 if (old_dir
!= new_dir
)
1312 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1313 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1314 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1316 while (new_size
&& (rc
== 0)) {
1317 tid
= txBegin(new_ip
->i_sb
, 0);
1318 mutex_lock(&JFS_IP(new_ip
)->commit_mutex
);
1319 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1324 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1326 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1328 if (new_ip
&& (new_ip
->i_nlink
== 0))
1329 set_cflag(COMMIT_Nolink
, new_ip
);
1331 free_UCSname(&new_dname
);
1333 free_UCSname(&old_dname
);
1335 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1336 IWRITE_UNLOCK(new_ip
);
1338 * Truncating the directory index table is not guaranteed. It
1339 * may need to be done iteratively
1341 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1342 if (old_dir
->i_size
> 1)
1343 jfs_truncate_nolock(old_dir
, 0);
1345 clear_cflag(COMMIT_Stale
, old_dir
);
1348 jfs_info("jfs_rename: returning %d", rc
);
1356 * FUNCTION: Create a special file (device)
1358 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1359 int mode
, dev_t rdev
)
1361 struct jfs_inode_info
*jfs_ip
;
1362 struct btstack btstack
;
1363 struct component_name dname
;
1366 struct inode
*iplist
[2];
1369 struct tblock
*tblk
;
1371 if (!new_valid_dev(rdev
))
1374 jfs_info("jfs_mknod: %s", dentry
->d_name
.name
);
1376 dquot_initialize(dir
);
1378 if ((rc
= get_UCSname(&dname
, dentry
)))
1381 ip
= ialloc(dir
, mode
);
1386 jfs_ip
= JFS_IP(ip
);
1388 tid
= txBegin(dir
->i_sb
, 0);
1390 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1391 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1393 rc
= jfs_init_acl(tid
, ip
, dir
);
1397 rc
= jfs_init_security(tid
, ip
, dir
);
1403 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
1408 tblk
= tid_to_tblock(tid
);
1409 tblk
->xflag
|= COMMIT_CREATE
;
1410 tblk
->ino
= ip
->i_ino
;
1411 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1414 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
))) {
1419 ip
->i_op
= &jfs_file_inode_operations
;
1420 jfs_ip
->dev
= new_encode_dev(rdev
);
1421 init_special_inode(ip
, ip
->i_mode
, rdev
);
1423 mark_inode_dirty(ip
);
1425 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
1427 mark_inode_dirty(dir
);
1431 rc
= txCommit(tid
, 2, iplist
, 0);
1435 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1436 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
1440 unlock_new_inode(ip
);
1443 d_instantiate(dentry
, ip
);
1444 unlock_new_inode(ip
);
1448 free_UCSname(&dname
);
1451 jfs_info("jfs_mknod: returning %d", rc
);
1455 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
1457 struct btstack btstack
;
1460 struct component_name key
;
1461 const char *name
= dentry
->d_name
.name
;
1462 int len
= dentry
->d_name
.len
;
1465 jfs_info("jfs_lookup: name = %s", name
);
1467 if (JFS_SBI(dip
->i_sb
)->mntflag
& JFS_OS2
)
1468 dentry
->d_op
= &jfs_ci_dentry_operations
;
1470 if ((name
[0] == '.') && (len
== 1))
1472 else if (strcmp(name
, "..") == 0)
1475 if ((rc
= get_UCSname(&key
, dentry
)))
1477 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1479 if (rc
== -ENOENT
) {
1480 d_add(dentry
, NULL
);
1483 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1488 ip
= jfs_iget(dip
->i_sb
, inum
);
1490 jfs_err("jfs_lookup: iget failed on inum %d", (uint
) inum
);
1491 return ERR_CAST(ip
);
1494 dentry
= d_splice_alias(ip
, dentry
);
1496 if (dentry
&& (JFS_SBI(dip
->i_sb
)->mntflag
& JFS_OS2
))
1497 dentry
->d_op
= &jfs_ci_dentry_operations
;
1502 static struct inode
*jfs_nfs_get_inode(struct super_block
*sb
,
1503 u64 ino
, u32 generation
)
1505 struct inode
*inode
;
1508 return ERR_PTR(-ESTALE
);
1509 inode
= jfs_iget(sb
, ino
);
1511 return ERR_CAST(inode
);
1513 if (generation
&& inode
->i_generation
!= generation
) {
1515 return ERR_PTR(-ESTALE
);
1521 struct dentry
*jfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
1522 int fh_len
, int fh_type
)
1524 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
1528 struct dentry
*jfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
1529 int fh_len
, int fh_type
)
1531 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
1535 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1537 unsigned long parent_ino
;
1540 le32_to_cpu(JFS_IP(dentry
->d_inode
)->i_dtroot
.header
.idotdot
);
1542 return d_obtain_alias(jfs_iget(dentry
->d_inode
->i_sb
, parent_ino
));
1545 const struct inode_operations jfs_dir_inode_operations
= {
1546 .create
= jfs_create
,
1547 .lookup
= jfs_lookup
,
1549 .unlink
= jfs_unlink
,
1550 .symlink
= jfs_symlink
,
1554 .rename
= jfs_rename
,
1555 .setxattr
= jfs_setxattr
,
1556 .getxattr
= jfs_getxattr
,
1557 .listxattr
= jfs_listxattr
,
1558 .removexattr
= jfs_removexattr
,
1559 .setattr
= jfs_setattr
,
1560 #ifdef CONFIG_JFS_POSIX_ACL
1561 .check_acl
= jfs_check_acl
,
1565 const struct file_operations jfs_dir_operations
= {
1566 .read
= generic_read_dir
,
1567 .readdir
= jfs_readdir
,
1569 .unlocked_ioctl
= jfs_ioctl
,
1570 #ifdef CONFIG_COMPAT
1571 .compat_ioctl
= jfs_compat_ioctl
,
1573 .llseek
= generic_file_llseek
,
1576 static int jfs_ci_hash(struct dentry
*dir
, struct qstr
*this)
1581 hash
= init_name_hash();
1582 for (i
=0; i
< this->len
; i
++)
1583 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1584 this->hash
= end_name_hash(hash
);
1589 static int jfs_ci_compare(struct dentry
*dir
, struct qstr
*a
, struct qstr
*b
)
1593 if (a
->len
!= b
->len
)
1595 for (i
=0; i
< a
->len
; i
++) {
1596 if (tolower(a
->name
[i
]) != tolower(b
->name
[i
]))
1602 * We want creates to preserve case. A negative dentry, a, that
1603 * has a different case than b may cause a new entry to be created
1604 * with the wrong case. Since we can't tell if a comes from a negative
1605 * dentry, we blindly replace it with b. This should be harmless if
1606 * a is not a negative dentry.
1608 memcpy((unsigned char *)a
->name
, b
->name
, a
->len
);
1613 const struct dentry_operations jfs_ci_dentry_operations
=
1615 .d_hash
= jfs_ci_hash
,
1616 .d_compare
= jfs_ci_compare
,