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 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
);
89 * search parent directory for entry/freespace
90 * (dtSearch() returns parent directory page pinned)
92 if ((rc
= get_UCSname(&dname
, dentry
)))
96 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
97 * block there while holding dtree page, so we allocate the inode &
98 * begin the transaction before we search the directory.
100 ip
= ialloc(dip
, mode
);
106 tid
= txBegin(dip
->i_sb
, 0);
108 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
109 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
111 rc
= jfs_init_acl(tid
, ip
, dip
);
115 rc
= jfs_init_security(tid
, ip
, dip
);
121 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
122 jfs_err("jfs_create: dtSearch returned %d", rc
);
127 tblk
= tid_to_tblock(tid
);
128 tblk
->xflag
|= COMMIT_CREATE
;
129 tblk
->ino
= ip
->i_ino
;
130 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
136 * initialize the child XAD tree root in-line in inode
141 * create entry in parent directory for child directory
142 * (dtInsert() releases parent directory page)
145 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
147 jfs_err("jfs_create: dtInsert returned -EIO");
148 txAbort(tid
, 1); /* Marks Filesystem dirty */
150 txAbort(tid
, 0); /* Filesystem full */
154 ip
->i_op
= &jfs_file_inode_operations
;
155 ip
->i_fop
= &jfs_file_operations
;
156 ip
->i_mapping
->a_ops
= &jfs_aops
;
158 mark_inode_dirty(ip
);
160 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
162 mark_inode_dirty(dip
);
164 rc
= txCommit(tid
, 2, &iplist
[0], 0);
168 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
169 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
173 unlock_new_inode(ip
);
176 d_instantiate(dentry
, ip
);
177 unlock_new_inode(ip
);
181 free_UCSname(&dname
);
185 jfs_info("jfs_create: rc:%d", rc
);
191 * NAME: jfs_mkdir(dip, dentry, mode)
193 * FUNCTION: create a child directory in the parent directory <dip>
194 * with name = <from dentry> and mode = <mode>
196 * PARAMETER: dip - parent directory vnode
197 * dentry - dentry of child directory
198 * mode - create mode (rwxrwxrwx).
200 * RETURN: Errors from subroutines
203 * EACCESS: user needs search+write permission on the parent directory
205 static int jfs_mkdir(struct inode
*dip
, struct dentry
*dentry
, int mode
)
208 tid_t tid
; /* transaction id */
209 struct inode
*ip
= NULL
; /* child directory inode */
211 struct component_name dname
; /* child directory name */
212 struct btstack btstack
;
213 struct inode
*iplist
[2];
216 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
218 /* link count overflow on parent directory ? */
219 if (dip
->i_nlink
== JFS_LINK_MAX
) {
225 * search parent directory for entry/freespace
226 * (dtSearch() returns parent directory page pinned)
228 if ((rc
= get_UCSname(&dname
, dentry
)))
232 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
233 * block there while holding dtree page, so we allocate the inode &
234 * begin the transaction before we search the directory.
236 ip
= ialloc(dip
, S_IFDIR
| mode
);
242 tid
= txBegin(dip
->i_sb
, 0);
244 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
245 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
247 rc
= jfs_init_acl(tid
, ip
, dip
);
251 rc
= jfs_init_security(tid
, ip
, dip
);
257 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
258 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
263 tblk
= tid_to_tblock(tid
);
264 tblk
->xflag
|= COMMIT_CREATE
;
265 tblk
->ino
= ip
->i_ino
;
266 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
272 * initialize the child directory in-line in inode
274 dtInitRoot(tid
, ip
, dip
->i_ino
);
277 * create entry in parent directory for child directory
278 * (dtInsert() releases parent directory page)
281 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
283 jfs_err("jfs_mkdir: dtInsert returned -EIO");
284 txAbort(tid
, 1); /* Marks Filesystem dirty */
286 txAbort(tid
, 0); /* Filesystem full */
290 ip
->i_nlink
= 2; /* for '.' */
291 ip
->i_op
= &jfs_dir_inode_operations
;
292 ip
->i_fop
= &jfs_dir_operations
;
294 mark_inode_dirty(ip
);
296 /* update parent directory inode */
297 inc_nlink(dip
); /* for '..' from child directory */
298 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
299 mark_inode_dirty(dip
);
301 rc
= txCommit(tid
, 2, &iplist
[0], 0);
305 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
306 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
310 unlock_new_inode(ip
);
313 d_instantiate(dentry
, ip
);
314 unlock_new_inode(ip
);
318 free_UCSname(&dname
);
323 jfs_info("jfs_mkdir: rc:%d", rc
);
328 * NAME: jfs_rmdir(dip, dentry)
330 * FUNCTION: remove a link to child directory
332 * PARAMETER: dip - parent inode
333 * dentry - child directory dentry
335 * RETURN: -EINVAL - if name is . or ..
336 * -EINVAL - if . or .. exist but are invalid.
337 * errors from subroutines
340 * if other threads have the directory open when the last link
341 * is removed, the "." and ".." entries, if present, are removed before
342 * rmdir() returns and no new entries may be created in the directory,
343 * but the directory is not removed until the last reference to
344 * the directory is released (cf.unlink() of regular file).
346 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
349 tid_t tid
; /* transaction id */
350 struct inode
*ip
= dentry
->d_inode
;
352 struct component_name dname
;
353 struct inode
*iplist
[2];
356 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
358 /* Init inode for quota operations. */
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. */
488 if ((rc
= get_UCSname(&dname
, dentry
)))
491 IWRITE_LOCK(ip
, RDWRLOCK_NORMAL
);
493 tid
= txBegin(dip
->i_sb
, 0);
495 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
496 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
502 * delete the entry of target file from parent directory
505 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
506 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
508 txAbort(tid
, 1); /* Marks FS Dirty */
510 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
511 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
518 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
519 mark_inode_dirty(dip
);
521 /* update target's inode */
522 inode_dec_link_count(ip
);
525 * commit zero link count object
527 if (ip
->i_nlink
== 0) {
528 assert(!test_cflag(COMMIT_Nolink
, ip
));
529 /* free block resources */
530 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
531 txAbort(tid
, 1); /* Marks FS Dirty */
533 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
534 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
539 tblk
= tid_to_tblock(tid
);
540 tblk
->xflag
|= COMMIT_DELETE
;
545 * Incomplete truncate of file data can
546 * result in timing problems unless we synchronously commit the
550 commit_flag
= COMMIT_SYNC
;
555 * If xtTruncate was incomplete, commit synchronously to avoid
556 * timing complications
558 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
562 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
563 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
565 while (new_size
&& (rc
== 0)) {
566 tid
= txBegin(dip
->i_sb
, 0);
567 mutex_lock(&JFS_IP(ip
)->commit_mutex
);
568 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
570 txAbort(tid
, 1); /* Marks FS Dirty */
573 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
575 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
578 if (ip
->i_nlink
== 0)
579 set_cflag(COMMIT_Nolink
, ip
);
584 * Truncating the directory index table is not guaranteed. It
585 * may need to be done iteratively
587 if (test_cflag(COMMIT_Stale
, dip
)) {
589 jfs_truncate_nolock(dip
, 0);
591 clear_cflag(COMMIT_Stale
, dip
);
595 free_UCSname(&dname
);
597 jfs_info("jfs_unlink: rc:%d", rc
);
602 * NAME: commitZeroLink()
604 * FUNCTION: for non-directory, called by jfs_remove(),
605 * truncate a regular file, directory or symbolic
606 * link to zero length. return 0 if type is not
609 * if the file is currently associated with a VM segment
610 * only permanent disk and inode map resources are freed,
611 * and neither the inode nor indirect blocks are modified
612 * so that the resources can be later freed in the work
614 * if there is no VM segment on entry, the resources are
615 * freed in both work and permanent map.
616 * (? for temporary file - memory object is cached even
617 * after no reference:
618 * reference count > 0 - )
620 * PARAMETERS: cd - pointer to commit data structure.
621 * current inode is the one to truncate.
623 * RETURN: Errors from subroutines
625 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
630 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
632 filetype
= ip
->i_mode
& S_IFMT
;
637 /* fast symbolic link */
638 if (ip
->i_size
< IDATASIZE
) {
644 assert(filetype
!= S_IFDIR
);
648 set_cflag(COMMIT_Freewmap
, ip
);
650 /* mark transaction of block map update type */
651 tblk
= tid_to_tblock(tid
);
652 tblk
->xflag
|= COMMIT_PMAP
;
657 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
658 /* acquire maplock on EA to be freed from block map */
659 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
664 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
665 /* acquire maplock on EA to be freed from block map */
666 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
669 * free xtree/data (truncate to zero length):
670 * free xtree/data pages from cache if COMMIT_PWMAP,
671 * free xtree/data blocks from persistent block map, and
672 * free xtree/data blocks from working block map if COMMIT_PWMAP;
675 return xtTruncate_pmap(tid
, ip
, 0);
682 * NAME: jfs_free_zero_link()
684 * FUNCTION: for non-directory, called by iClose(),
685 * free resources of a file from cache and WORKING map
686 * for a file previously committed with zero link count
687 * while associated with a pager object,
689 * PARAMETER: ip - pointer to inode of file.
691 void jfs_free_zero_link(struct inode
*ip
)
695 jfs_info("jfs_free_zero_link: ip = 0x%p", ip
);
697 /* return if not reg or symbolic link or if size is
700 type
= ip
->i_mode
& S_IFMT
;
706 /* if its contained in inode nothing to do */
707 if (ip
->i_size
< IDATASIZE
)
717 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
718 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
719 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
720 struct maplock maplock
; /* maplock for COMMIT_WMAP */
721 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
723 /* free EA pages from cache */
724 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
726 /* free EA extent from working block map */
728 pxdlock
= (struct pxd_lock
*) & maplock
;
729 pxdlock
->flag
= mlckFREEPXD
;
730 PXDaddress(&pxdlock
->pxd
, xaddr
);
731 PXDlength(&pxdlock
->pxd
, xlen
);
732 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
738 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
739 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
740 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
741 struct maplock maplock
; /* maplock for COMMIT_WMAP */
742 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
744 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
746 /* free ACL extent from working block map */
748 pxdlock
= (struct pxd_lock
*) & maplock
;
749 pxdlock
->flag
= mlckFREEPXD
;
750 PXDaddress(&pxdlock
->pxd
, xaddr
);
751 PXDlength(&pxdlock
->pxd
, xlen
);
752 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
756 * free xtree/data (truncate to zero length):
757 * free xtree/data pages from cache, and
758 * free xtree/data blocks from working block map;
761 xtTruncate(0, ip
, 0, COMMIT_WMAP
);
765 * NAME: jfs_link(vp, dvp, name, crp)
767 * FUNCTION: create a link to <vp> by the name = <name>
768 * in the parent directory <dvp>
770 * PARAMETER: vp - target object
771 * dvp - parent directory of new link
772 * name - name of new link to target object
775 * RETURN: Errors from subroutines
778 * JFS does NOT support link() on directories (to prevent circular
779 * path in the directory hierarchy);
780 * EPERM: the target object is a directory, and either the caller
781 * does not have appropriate privileges or the implementation prohibits
782 * using link() on directories [XPG4.2].
784 * JFS does NOT support links between file systems:
785 * EXDEV: target object and new link are on different file systems and
786 * implementation does not support links between file systems [XPG4.2].
788 static int jfs_link(struct dentry
*old_dentry
,
789 struct inode
*dir
, struct dentry
*dentry
)
793 struct inode
*ip
= old_dentry
->d_inode
;
795 struct component_name dname
;
796 struct btstack btstack
;
797 struct inode
*iplist
[2];
799 jfs_info("jfs_link: %s %s", old_dentry
->d_name
.name
,
800 dentry
->d_name
.name
);
802 if (ip
->i_nlink
== JFS_LINK_MAX
)
805 if (ip
->i_nlink
== 0)
808 tid
= txBegin(ip
->i_sb
, 0);
810 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
811 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
814 * scan parent directory for entry/freespace
816 if ((rc
= get_UCSname(&dname
, dentry
)))
819 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
823 * create entry for new link in parent directory
826 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
829 /* update object inode */
830 inc_nlink(ip
); /* for new link */
831 ip
->i_ctime
= CURRENT_TIME
;
832 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
833 mark_inode_dirty(dir
);
834 atomic_inc(&ip
->i_count
);
838 rc
= txCommit(tid
, 2, &iplist
[0], 0);
841 ip
->i_nlink
--; /* never instantiated */
844 d_instantiate(dentry
, ip
);
847 free_UCSname(&dname
);
852 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
853 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
855 jfs_info("jfs_link: rc:%d", rc
);
860 * NAME: jfs_symlink(dip, dentry, name)
862 * FUNCTION: creates a symbolic link to <symlink> by name <name>
865 * PARAMETER: dip - parent directory vnode
866 * dentry - dentry of symbolic link
867 * name - the path name of the existing object
868 * that will be the source of the link
870 * RETURN: errors from subroutines
873 * ENAMETOOLONG: pathname resolution of a symbolic link produced
874 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
877 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
883 struct component_name dname
;
884 int ssize
; /* source pathname size */
885 struct btstack btstack
;
886 struct inode
*ip
= dentry
->d_inode
;
887 unchar
*i_fastsymlink
;
889 int bmask
= 0, xsize
;
890 s64 extent
= 0, xaddr
;
892 struct super_block
*sb
;
895 struct inode
*iplist
[2];
897 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
899 ssize
= strlen(name
) + 1;
902 * search parent directory for entry/freespace
903 * (dtSearch() returns parent directory page pinned)
906 if ((rc
= get_UCSname(&dname
, dentry
)))
910 * allocate on-disk/in-memory inode for symbolic link:
911 * (iAlloc() returns new, locked inode)
913 ip
= ialloc(dip
, S_IFLNK
| 0777);
919 tid
= txBegin(dip
->i_sb
, 0);
921 mutex_lock_nested(&JFS_IP(dip
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
922 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
924 rc
= jfs_init_security(tid
, ip
, dip
);
928 tblk
= tid_to_tblock(tid
);
929 tblk
->xflag
|= COMMIT_CREATE
;
930 tblk
->ino
= ip
->i_ino
;
931 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
933 /* fix symlink access permission
934 * (dir_create() ANDs in the u.u_cmask,
935 * but symlinks really need to be 777 access)
940 * write symbolic link target path name
945 * write source path name inline in on-disk inode (fast symbolic link)
948 if (ssize
<= IDATASIZE
) {
949 ip
->i_op
= &jfs_symlink_inode_operations
;
951 i_fastsymlink
= JFS_IP(ip
)->i_inline
;
952 memcpy(i_fastsymlink
, name
, ssize
);
953 ip
->i_size
= ssize
- 1;
956 * if symlink is > 128 bytes, we don't have the space to
957 * store inline extended attributes
959 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
960 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
962 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
966 * write source path name in a single extent
969 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
971 ip
->i_op
= &page_symlink_inode_operations
;
972 ip
->i_mapping
->a_ops
= &jfs_aops
;
975 * even though the data of symlink object (source
976 * path name) is treated as non-journaled user data,
977 * it is read/written thru buffer cache for performance.
980 bmask
= JFS_SBI(sb
)->bsize
- 1;
981 xsize
= (ssize
+ bmask
) & ~bmask
;
983 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
984 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
989 ip
->i_size
= ssize
- 1;
991 /* This is kind of silly since PATH_MAX == 4K */
992 int copy_size
= min(ssize
, PSIZE
);
994 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
997 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1002 memcpy(mp
->data
, name
, copy_size
);
1006 xaddr
+= JFS_SBI(sb
)->nbperpage
;
1011 * create entry for symbolic link in parent directory
1013 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
1016 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
1020 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1022 /* discard new inode */
1026 mark_inode_dirty(ip
);
1028 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
1029 mark_inode_dirty(dip
);
1031 * commit update of parent directory and link object
1036 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1040 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1041 mutex_unlock(&JFS_IP(dip
)->commit_mutex
);
1045 unlock_new_inode(ip
);
1048 d_instantiate(dentry
, ip
);
1049 unlock_new_inode(ip
);
1053 free_UCSname(&dname
);
1056 jfs_info("jfs_symlink: rc:%d", rc
);
1064 * FUNCTION: rename a file or directory
1066 static int jfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1067 struct inode
*new_dir
, struct dentry
*new_dentry
)
1069 struct btstack btstack
;
1071 struct component_name new_dname
;
1072 struct inode
*new_ip
;
1073 struct component_name old_dname
;
1074 struct inode
*old_ip
;
1078 struct dt_lock
*dtlck
;
1081 struct inode
*iplist
[4];
1082 struct tblock
*tblk
;
1087 jfs_info("jfs_rename: %s %s", old_dentry
->d_name
.name
,
1088 new_dentry
->d_name
.name
);
1090 old_ip
= old_dentry
->d_inode
;
1091 new_ip
= new_dentry
->d_inode
;
1093 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1096 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1100 * Make sure source inode number is what we think it is
1102 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1103 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1109 * Make sure dest inode number (if any) is what we think it is
1111 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1113 if ((!new_ip
) || (ino
!= new_ip
->i_ino
)) {
1117 } else if (rc
!= -ENOENT
)
1120 /* no entry exists, but one was expected */
1125 if (S_ISDIR(old_ip
->i_mode
)) {
1127 if (!dtEmpty(new_ip
)) {
1131 } else if ((new_dir
!= old_dir
) &&
1132 (new_dir
->i_nlink
== JFS_LINK_MAX
)) {
1136 } else if (new_ip
) {
1137 IWRITE_LOCK(new_ip
, RDWRLOCK_NORMAL
);
1138 /* Init inode for quota operations. */
1143 * The real work starts here
1145 tid
= txBegin(new_dir
->i_sb
, 0);
1148 * How do we know the locking is safe from deadlocks?
1149 * The vfs does the hard part for us. Any time we are taking nested
1150 * commit_mutexes, the vfs already has i_mutex held on the parent.
1151 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1153 mutex_lock_nested(&JFS_IP(new_dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1154 mutex_lock_nested(&JFS_IP(old_ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1155 if (old_dir
!= new_dir
)
1156 mutex_lock_nested(&JFS_IP(old_dir
)->commit_mutex
,
1157 COMMIT_MUTEX_SECOND_PARENT
);
1160 mutex_lock_nested(&JFS_IP(new_ip
)->commit_mutex
,
1161 COMMIT_MUTEX_VICTIM
);
1163 * Change existing directory entry to new inode number
1165 ino
= new_ip
->i_ino
;
1166 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1167 old_ip
->i_ino
, JFS_RENAME
);
1171 if (S_ISDIR(new_ip
->i_mode
)) {
1173 if (new_ip
->i_nlink
) {
1174 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1175 if (old_dir
!= new_dir
)
1176 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1177 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1178 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1179 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1180 IWRITE_UNLOCK(new_ip
);
1181 jfs_error(new_ip
->i_sb
,
1182 "jfs_rename: new_ip->i_nlink != 0");
1185 tblk
= tid_to_tblock(tid
);
1186 tblk
->xflag
|= COMMIT_DELETE
;
1187 tblk
->u
.ip
= new_ip
;
1188 } else if (new_ip
->i_nlink
== 0) {
1189 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1190 /* free block resources */
1191 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1192 txAbort(tid
, 1); /* Marks FS Dirty */
1196 tblk
= tid_to_tblock(tid
);
1197 tblk
->xflag
|= COMMIT_DELETE
;
1198 tblk
->u
.ip
= new_ip
;
1200 new_ip
->i_ctime
= CURRENT_TIME
;
1201 mark_inode_dirty(new_ip
);
1205 * Add new directory entry
1207 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1210 jfs_err("jfs_rename didn't expect dtSearch to fail "
1215 ino
= old_ip
->i_ino
;
1216 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1219 jfs_err("jfs_rename: dtInsert returned -EIO");
1222 if (S_ISDIR(old_ip
->i_mode
))
1226 * Remove old directory entry
1229 ino
= old_ip
->i_ino
;
1230 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1232 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1234 txAbort(tid
, 1); /* Marks Filesystem dirty */
1237 if (S_ISDIR(old_ip
->i_mode
)) {
1238 drop_nlink(old_dir
);
1239 if (old_dir
!= new_dir
) {
1241 * Change inode number of parent for moved directory
1244 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1245 cpu_to_le32(new_dir
->i_ino
);
1247 /* Linelock header of dtree */
1248 tlck
= txLock(tid
, old_ip
,
1249 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1250 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1251 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1252 ASSERT(dtlck
->index
== 0);
1253 lv
= & dtlck
->lv
[0];
1261 * Update ctime on changed/moved inodes & mark dirty
1263 old_ip
->i_ctime
= CURRENT_TIME
;
1264 mark_inode_dirty(old_ip
);
1266 new_dir
->i_ctime
= new_dir
->i_mtime
= current_fs_time(new_dir
->i_sb
);
1267 mark_inode_dirty(new_dir
);
1269 /* Build list of inodes modified by this transaction */
1271 iplist
[ipcount
++] = old_ip
;
1273 iplist
[ipcount
++] = new_ip
;
1274 iplist
[ipcount
++] = old_dir
;
1276 if (old_dir
!= new_dir
) {
1277 iplist
[ipcount
++] = new_dir
;
1278 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1279 mark_inode_dirty(old_dir
);
1283 * Incomplete truncate of file data can
1284 * result in timing problems unless we synchronously commit the
1288 commit_flag
= COMMIT_SYNC
;
1292 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1297 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1298 if (old_dir
!= new_dir
)
1299 mutex_unlock(&JFS_IP(old_dir
)->commit_mutex
);
1300 mutex_unlock(&JFS_IP(old_ip
)->commit_mutex
);
1301 mutex_unlock(&JFS_IP(new_dir
)->commit_mutex
);
1303 while (new_size
&& (rc
== 0)) {
1304 tid
= txBegin(new_ip
->i_sb
, 0);
1305 mutex_lock(&JFS_IP(new_ip
)->commit_mutex
);
1306 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1311 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1313 mutex_unlock(&JFS_IP(new_ip
)->commit_mutex
);
1315 if (new_ip
&& (new_ip
->i_nlink
== 0))
1316 set_cflag(COMMIT_Nolink
, new_ip
);
1318 free_UCSname(&new_dname
);
1320 free_UCSname(&old_dname
);
1322 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1323 IWRITE_UNLOCK(new_ip
);
1325 * Truncating the directory index table is not guaranteed. It
1326 * may need to be done iteratively
1328 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1329 if (old_dir
->i_size
> 1)
1330 jfs_truncate_nolock(old_dir
, 0);
1332 clear_cflag(COMMIT_Stale
, old_dir
);
1335 jfs_info("jfs_rename: returning %d", rc
);
1343 * FUNCTION: Create a special file (device)
1345 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1346 int mode
, dev_t rdev
)
1348 struct jfs_inode_info
*jfs_ip
;
1349 struct btstack btstack
;
1350 struct component_name dname
;
1353 struct inode
*iplist
[2];
1356 struct tblock
*tblk
;
1358 if (!new_valid_dev(rdev
))
1361 jfs_info("jfs_mknod: %s", dentry
->d_name
.name
);
1363 if ((rc
= get_UCSname(&dname
, dentry
)))
1366 ip
= ialloc(dir
, mode
);
1371 jfs_ip
= JFS_IP(ip
);
1373 tid
= txBegin(dir
->i_sb
, 0);
1375 mutex_lock_nested(&JFS_IP(dir
)->commit_mutex
, COMMIT_MUTEX_PARENT
);
1376 mutex_lock_nested(&JFS_IP(ip
)->commit_mutex
, COMMIT_MUTEX_CHILD
);
1378 rc
= jfs_init_acl(tid
, ip
, dir
);
1382 rc
= jfs_init_security(tid
, ip
, dir
);
1388 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
1393 tblk
= tid_to_tblock(tid
);
1394 tblk
->xflag
|= COMMIT_CREATE
;
1395 tblk
->ino
= ip
->i_ino
;
1396 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1399 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
))) {
1404 ip
->i_op
= &jfs_file_inode_operations
;
1405 jfs_ip
->dev
= new_encode_dev(rdev
);
1406 init_special_inode(ip
, ip
->i_mode
, rdev
);
1408 mark_inode_dirty(ip
);
1410 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
1412 mark_inode_dirty(dir
);
1416 rc
= txCommit(tid
, 2, iplist
, 0);
1420 mutex_unlock(&JFS_IP(ip
)->commit_mutex
);
1421 mutex_unlock(&JFS_IP(dir
)->commit_mutex
);
1425 unlock_new_inode(ip
);
1428 d_instantiate(dentry
, ip
);
1429 unlock_new_inode(ip
);
1433 free_UCSname(&dname
);
1436 jfs_info("jfs_mknod: returning %d", rc
);
1440 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
1442 struct btstack btstack
;
1445 struct component_name key
;
1446 const char *name
= dentry
->d_name
.name
;
1447 int len
= dentry
->d_name
.len
;
1450 jfs_info("jfs_lookup: name = %s", name
);
1452 if (JFS_SBI(dip
->i_sb
)->mntflag
& JFS_OS2
)
1453 dentry
->d_op
= &jfs_ci_dentry_operations
;
1455 if ((name
[0] == '.') && (len
== 1))
1457 else if (strcmp(name
, "..") == 0)
1460 if ((rc
= get_UCSname(&key
, dentry
)))
1462 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1464 if (rc
== -ENOENT
) {
1465 d_add(dentry
, NULL
);
1468 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1473 ip
= jfs_iget(dip
->i_sb
, inum
);
1475 jfs_err("jfs_lookup: iget failed on inum %d", (uint
) inum
);
1476 return ERR_CAST(ip
);
1479 dentry
= d_splice_alias(ip
, dentry
);
1481 if (dentry
&& (JFS_SBI(dip
->i_sb
)->mntflag
& JFS_OS2
))
1482 dentry
->d_op
= &jfs_ci_dentry_operations
;
1487 static struct inode
*jfs_nfs_get_inode(struct super_block
*sb
,
1488 u64 ino
, u32 generation
)
1490 struct inode
*inode
;
1493 return ERR_PTR(-ESTALE
);
1494 inode
= jfs_iget(sb
, ino
);
1496 return ERR_CAST(inode
);
1498 if (generation
&& inode
->i_generation
!= generation
) {
1500 return ERR_PTR(-ESTALE
);
1506 struct dentry
*jfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fid
,
1507 int fh_len
, int fh_type
)
1509 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
1513 struct dentry
*jfs_fh_to_parent(struct super_block
*sb
, struct fid
*fid
,
1514 int fh_len
, int fh_type
)
1516 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
1520 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1522 unsigned long parent_ino
;
1525 le32_to_cpu(JFS_IP(dentry
->d_inode
)->i_dtroot
.header
.idotdot
);
1527 return d_obtain_alias(jfs_iget(dentry
->d_inode
->i_sb
, parent_ino
));
1530 const struct inode_operations jfs_dir_inode_operations
= {
1531 .create
= jfs_create
,
1532 .lookup
= jfs_lookup
,
1534 .unlink
= jfs_unlink
,
1535 .symlink
= jfs_symlink
,
1539 .rename
= jfs_rename
,
1540 .setxattr
= jfs_setxattr
,
1541 .getxattr
= jfs_getxattr
,
1542 .listxattr
= jfs_listxattr
,
1543 .removexattr
= jfs_removexattr
,
1544 #ifdef CONFIG_JFS_POSIX_ACL
1545 .setattr
= jfs_setattr
,
1546 .permission
= jfs_permission
,
1550 const struct file_operations jfs_dir_operations
= {
1551 .read
= generic_read_dir
,
1552 .readdir
= jfs_readdir
,
1554 .unlocked_ioctl
= jfs_ioctl
,
1555 #ifdef CONFIG_COMPAT
1556 .compat_ioctl
= jfs_compat_ioctl
,
1558 .llseek
= generic_file_llseek
,
1561 static int jfs_ci_hash(struct dentry
*dir
, struct qstr
*this)
1566 hash
= init_name_hash();
1567 for (i
=0; i
< this->len
; i
++)
1568 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1569 this->hash
= end_name_hash(hash
);
1574 static int jfs_ci_compare(struct dentry
*dir
, struct qstr
*a
, struct qstr
*b
)
1578 if (a
->len
!= b
->len
)
1580 for (i
=0; i
< a
->len
; i
++) {
1581 if (tolower(a
->name
[i
]) != tolower(b
->name
[i
]))
1587 * We want creates to preserve case. A negative dentry, a, that
1588 * has a different case than b may cause a new entry to be created
1589 * with the wrong case. Since we can't tell if a comes from a negative
1590 * dentry, we blindly replace it with b. This should be harmless if
1591 * a is not a negative dentry.
1593 memcpy((unsigned char *)a
->name
, b
->name
, a
->len
);
1598 struct dentry_operations jfs_ci_dentry_operations
=
1600 .d_hash
= jfs_ci_hash
,
1601 .d_compare
= jfs_ci_compare
,