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 "jfs_incore.h"
24 #include "jfs_superblock.h"
25 #include "jfs_inode.h"
26 #include "jfs_dinode.h"
28 #include "jfs_unicode.h"
29 #include "jfs_metapage.h"
30 #include "jfs_xattr.h"
32 #include "jfs_debug.h"
34 extern struct inode_operations jfs_file_inode_operations
;
35 extern struct inode_operations jfs_symlink_inode_operations
;
36 extern struct file_operations jfs_file_operations
;
37 extern struct address_space_operations jfs_aops
;
39 extern int jfs_fsync(struct file
*, struct dentry
*, int);
40 extern void jfs_truncate_nolock(struct inode
*, loff_t
);
41 extern int jfs_init_acl(struct inode
*, struct inode
*);
46 struct inode_operations jfs_dir_inode_operations
;
47 struct file_operations jfs_dir_operations
;
48 struct dentry_operations jfs_ci_dentry_operations
;
50 static s64
commitZeroLink(tid_t
, struct inode
*);
53 * NAME: jfs_create(dip, dentry, mode)
55 * FUNCTION: create a regular file in the parent directory <dip>
56 * with name = <from dentry> and mode = <mode>
58 * PARAMETER: dip - parent directory vnode
59 * dentry - dentry of new file
60 * mode - create mode (rwxrwxrwx).
63 * RETURN: Errors from subroutines
66 static int jfs_create(struct inode
*dip
, struct dentry
*dentry
, int mode
,
70 tid_t tid
; /* transaction id */
71 struct inode
*ip
= NULL
; /* child directory inode */
73 struct component_name dname
; /* child directory name */
74 struct btstack btstack
;
75 struct inode
*iplist
[2];
78 jfs_info("jfs_create: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
81 * search parent directory for entry/freespace
82 * (dtSearch() returns parent directory page pinned)
84 if ((rc
= get_UCSname(&dname
, dentry
)))
88 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
89 * block there while holding dtree page, so we allocate the inode &
90 * begin the transaction before we search the directory.
92 ip
= ialloc(dip
, mode
);
98 tid
= txBegin(dip
->i_sb
, 0);
100 down(&JFS_IP(dip
)->commit_sem
);
101 down(&JFS_IP(ip
)->commit_sem
);
103 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
104 jfs_err("jfs_create: dtSearch returned %d", rc
);
108 tblk
= tid_to_tblock(tid
);
109 tblk
->xflag
|= COMMIT_CREATE
;
110 tblk
->ino
= ip
->i_ino
;
111 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
117 * initialize the child XAD tree root in-line in inode
122 * create entry in parent directory for child directory
123 * (dtInsert() releases parent directory page)
126 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
128 jfs_err("jfs_create: dtInsert returned -EIO");
129 txAbort(tid
, 1); /* Marks Filesystem dirty */
131 txAbort(tid
, 0); /* Filesystem full */
135 ip
->i_op
= &jfs_file_inode_operations
;
136 ip
->i_fop
= &jfs_file_operations
;
137 ip
->i_mapping
->a_ops
= &jfs_aops
;
139 insert_inode_hash(ip
);
140 mark_inode_dirty(ip
);
142 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
144 mark_inode_dirty(dip
);
146 rc
= txCommit(tid
, 2, &iplist
[0], 0);
150 up(&JFS_IP(dip
)->commit_sem
);
151 up(&JFS_IP(ip
)->commit_sem
);
156 d_instantiate(dentry
, ip
);
159 free_UCSname(&dname
);
161 #ifdef CONFIG_JFS_POSIX_ACL
163 jfs_init_acl(ip
, dip
);
168 jfs_info("jfs_create: rc:%d", rc
);
174 * NAME: jfs_mkdir(dip, dentry, mode)
176 * FUNCTION: create a child directory in the parent directory <dip>
177 * with name = <from dentry> and mode = <mode>
179 * PARAMETER: dip - parent directory vnode
180 * dentry - dentry of child directory
181 * mode - create mode (rwxrwxrwx).
183 * RETURN: Errors from subroutines
186 * EACCESS: user needs search+write permission on the parent directory
188 static int jfs_mkdir(struct inode
*dip
, struct dentry
*dentry
, int mode
)
191 tid_t tid
; /* transaction id */
192 struct inode
*ip
= NULL
; /* child directory inode */
194 struct component_name dname
; /* child directory name */
195 struct btstack btstack
;
196 struct inode
*iplist
[2];
199 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
201 /* link count overflow on parent directory ? */
202 if (dip
->i_nlink
== JFS_LINK_MAX
) {
208 * search parent directory for entry/freespace
209 * (dtSearch() returns parent directory page pinned)
211 if ((rc
= get_UCSname(&dname
, dentry
)))
215 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
216 * block there while holding dtree page, so we allocate the inode &
217 * begin the transaction before we search the directory.
219 ip
= ialloc(dip
, S_IFDIR
| mode
);
225 tid
= txBegin(dip
->i_sb
, 0);
227 down(&JFS_IP(dip
)->commit_sem
);
228 down(&JFS_IP(ip
)->commit_sem
);
230 if ((rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
))) {
231 jfs_err("jfs_mkdir: dtSearch returned %d", rc
);
235 tblk
= tid_to_tblock(tid
);
236 tblk
->xflag
|= COMMIT_CREATE
;
237 tblk
->ino
= ip
->i_ino
;
238 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
244 * initialize the child directory in-line in inode
246 dtInitRoot(tid
, ip
, dip
->i_ino
);
249 * create entry in parent directory for child directory
250 * (dtInsert() releases parent directory page)
253 if ((rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
))) {
255 jfs_err("jfs_mkdir: dtInsert returned -EIO");
256 txAbort(tid
, 1); /* Marks Filesystem dirty */
258 txAbort(tid
, 0); /* Filesystem full */
262 ip
->i_nlink
= 2; /* for '.' */
263 ip
->i_op
= &jfs_dir_inode_operations
;
264 ip
->i_fop
= &jfs_dir_operations
;
265 ip
->i_mapping
->a_ops
= &jfs_aops
;
266 mapping_set_gfp_mask(ip
->i_mapping
, GFP_NOFS
);
268 insert_inode_hash(ip
);
269 mark_inode_dirty(ip
);
271 /* update parent directory inode */
272 dip
->i_nlink
++; /* for '..' from child directory */
273 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
274 mark_inode_dirty(dip
);
276 rc
= txCommit(tid
, 2, &iplist
[0], 0);
280 up(&JFS_IP(dip
)->commit_sem
);
281 up(&JFS_IP(ip
)->commit_sem
);
286 d_instantiate(dentry
, ip
);
289 free_UCSname(&dname
);
291 #ifdef CONFIG_JFS_POSIX_ACL
293 jfs_init_acl(ip
, dip
);
298 jfs_info("jfs_mkdir: rc:%d", rc
);
303 * NAME: jfs_rmdir(dip, dentry)
305 * FUNCTION: remove a link to child directory
307 * PARAMETER: dip - parent inode
308 * dentry - child directory dentry
310 * RETURN: -EINVAL - if name is . or ..
311 * -EINVAL - if . or .. exist but are invalid.
312 * errors from subroutines
315 * if other threads have the directory open when the last link
316 * is removed, the "." and ".." entries, if present, are removed before
317 * rmdir() returns and no new entries may be created in the directory,
318 * but the directory is not removed until the last reference to
319 * the directory is released (cf.unlink() of regular file).
321 static int jfs_rmdir(struct inode
*dip
, struct dentry
*dentry
)
324 tid_t tid
; /* transaction id */
325 struct inode
*ip
= dentry
->d_inode
;
327 struct component_name dname
;
328 struct inode
*iplist
[2];
331 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
333 /* Init inode for quota operations. */
336 /* directory must be empty to be removed */
342 if ((rc
= get_UCSname(&dname
, dentry
))) {
346 tid
= txBegin(dip
->i_sb
, 0);
348 down(&JFS_IP(dip
)->commit_sem
);
349 down(&JFS_IP(ip
)->commit_sem
);
354 tblk
= tid_to_tblock(tid
);
355 tblk
->xflag
|= COMMIT_DELETE
;
359 * delete the entry of target directory from parent directory
362 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
363 jfs_err("jfs_rmdir: dtDelete returned %d", rc
);
367 up(&JFS_IP(dip
)->commit_sem
);
368 up(&JFS_IP(ip
)->commit_sem
);
373 /* update parent directory's link count corresponding
374 * to ".." entry of the target directory deleted
377 dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
378 mark_inode_dirty(dip
);
381 * OS/2 could have created EA and/or ACL
383 /* free EA from both persistent and working map */
384 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
386 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
388 JFS_IP(ip
)->ea
.flag
= 0;
390 /* free ACL from both persistent and working map */
391 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
393 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
395 JFS_IP(ip
)->acl
.flag
= 0;
397 /* mark the target directory as deleted */
399 mark_inode_dirty(ip
);
401 rc
= txCommit(tid
, 2, &iplist
[0], 0);
405 up(&JFS_IP(dip
)->commit_sem
);
406 up(&JFS_IP(ip
)->commit_sem
);
409 * Truncating the directory index table is not guaranteed. It
410 * may need to be done iteratively
412 if (test_cflag(COMMIT_Stale
, dip
)) {
414 jfs_truncate_nolock(dip
, 0);
416 clear_cflag(COMMIT_Stale
, dip
);
420 free_UCSname(&dname
);
423 jfs_info("jfs_rmdir: rc:%d", rc
);
428 * NAME: jfs_unlink(dip, dentry)
430 * FUNCTION: remove a link to object <vp> named by <name>
431 * from parent directory <dvp>
433 * PARAMETER: dip - inode of parent directory
434 * dentry - dentry of object to be removed
436 * RETURN: errors from subroutines
439 * temporary file: if one or more processes have the file open
440 * when the last link is removed, the link will be removed before
441 * unlink() returns, but the removal of the file contents will be
442 * postponed until all references to the files are closed.
444 * JFS does NOT support unlink() on directories.
447 static int jfs_unlink(struct inode
*dip
, struct dentry
*dentry
)
450 tid_t tid
; /* transaction id */
451 struct inode
*ip
= dentry
->d_inode
;
453 struct component_name dname
; /* object name */
454 struct inode
*iplist
[2];
459 jfs_info("jfs_unlink: dip:0x%p name:%s", dip
, dentry
->d_name
.name
);
461 /* Init inode for quota operations. */
464 if ((rc
= get_UCSname(&dname
, dentry
)))
469 tid
= txBegin(dip
->i_sb
, 0);
471 down(&JFS_IP(dip
)->commit_sem
);
472 down(&JFS_IP(ip
)->commit_sem
);
478 * delete the entry of target file from parent directory
481 if ((rc
= dtDelete(tid
, dip
, &dname
, &ino
, JFS_REMOVE
))) {
482 jfs_err("jfs_unlink: dtDelete returned %d", rc
);
484 txAbort(tid
, 1); /* Marks FS Dirty */
486 up(&JFS_IP(dip
)->commit_sem
);
487 up(&JFS_IP(ip
)->commit_sem
);
494 ip
->i_ctime
= dip
->i_ctime
= dip
->i_mtime
= CURRENT_TIME
;
495 mark_inode_dirty(dip
);
497 /* update target's inode */
499 mark_inode_dirty(ip
);
502 * commit zero link count object
504 if (ip
->i_nlink
== 0) {
505 assert(!test_cflag(COMMIT_Nolink
, ip
));
506 /* free block resources */
507 if ((new_size
= commitZeroLink(tid
, ip
)) < 0) {
508 txAbort(tid
, 1); /* Marks FS Dirty */
510 up(&JFS_IP(dip
)->commit_sem
);
511 up(&JFS_IP(ip
)->commit_sem
);
516 tblk
= tid_to_tblock(tid
);
517 tblk
->xflag
|= COMMIT_DELETE
;
522 * Incomplete truncate of file data can
523 * result in timing problems unless we synchronously commit the
527 commit_flag
= COMMIT_SYNC
;
532 * If xtTruncate was incomplete, commit synchronously to avoid
533 * timing complications
535 rc
= txCommit(tid
, 2, &iplist
[0], commit_flag
);
539 up(&JFS_IP(dip
)->commit_sem
);
540 up(&JFS_IP(ip
)->commit_sem
);
543 while (new_size
&& (rc
== 0)) {
544 tid
= txBegin(dip
->i_sb
, 0);
545 down(&JFS_IP(ip
)->commit_sem
);
546 new_size
= xtTruncate_pmap(tid
, ip
, new_size
);
548 txAbort(tid
, 1); /* Marks FS Dirty */
551 rc
= txCommit(tid
, 2, &iplist
[0], COMMIT_SYNC
);
553 up(&JFS_IP(ip
)->commit_sem
);
556 if (ip
->i_nlink
== 0)
557 set_cflag(COMMIT_Nolink
, ip
);
562 * Truncating the directory index table is not guaranteed. It
563 * may need to be done iteratively
565 if (test_cflag(COMMIT_Stale
, dip
)) {
567 jfs_truncate_nolock(dip
, 0);
569 clear_cflag(COMMIT_Stale
, dip
);
573 free_UCSname(&dname
);
575 jfs_info("jfs_unlink: rc:%d", rc
);
580 * NAME: commitZeroLink()
582 * FUNCTION: for non-directory, called by jfs_remove(),
583 * truncate a regular file, directory or symbolic
584 * link to zero length. return 0 if type is not
587 * if the file is currently associated with a VM segment
588 * only permanent disk and inode map resources are freed,
589 * and neither the inode nor indirect blocks are modified
590 * so that the resources can be later freed in the work
592 * if there is no VM segment on entry, the resources are
593 * freed in both work and permanent map.
594 * (? for temporary file - memory object is cached even
595 * after no reference:
596 * reference count > 0 - )
598 * PARAMETERS: cd - pointer to commit data structure.
599 * current inode is the one to truncate.
601 * RETURN: Errors from subroutines
603 static s64
commitZeroLink(tid_t tid
, struct inode
*ip
)
608 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid
, ip
);
610 filetype
= ip
->i_mode
& S_IFMT
;
615 /* fast symbolic link */
616 if (ip
->i_size
< IDATASIZE
) {
622 assert(filetype
!= S_IFDIR
);
626 set_cflag(COMMIT_Freewmap
, ip
);
628 /* mark transaction of block map update type */
629 tblk
= tid_to_tblock(tid
);
630 tblk
->xflag
|= COMMIT_PMAP
;
635 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
)
636 /* acquire maplock on EA to be freed from block map */
637 txEA(tid
, ip
, &JFS_IP(ip
)->ea
, NULL
);
642 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
)
643 /* acquire maplock on EA to be freed from block map */
644 txEA(tid
, ip
, &JFS_IP(ip
)->acl
, NULL
);
647 * free xtree/data (truncate to zero length):
648 * free xtree/data pages from cache if COMMIT_PWMAP,
649 * free xtree/data blocks from persistent block map, and
650 * free xtree/data blocks from working block map if COMMIT_PWMAP;
653 return xtTruncate_pmap(tid
, ip
, 0);
660 * NAME: freeZeroLink()
662 * FUNCTION: for non-directory, called by iClose(),
663 * free resources of a file from cache and WORKING map
664 * for a file previously committed with zero link count
665 * while associated with a pager object,
667 * PARAMETER: ip - pointer to inode of file.
671 int freeZeroLink(struct inode
*ip
)
676 jfs_info("freeZeroLink: ip = 0x%p", ip
);
678 /* return if not reg or symbolic link or if size is
681 type
= ip
->i_mode
& S_IFMT
;
687 /* if its contained in inode nothing to do */
688 if (ip
->i_size
< IDATASIZE
)
698 if (JFS_IP(ip
)->ea
.flag
& DXD_EXTENT
) {
699 s64 xaddr
= addressDXD(&JFS_IP(ip
)->ea
);
700 int xlen
= lengthDXD(&JFS_IP(ip
)->ea
);
701 struct maplock maplock
; /* maplock for COMMIT_WMAP */
702 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
704 /* free EA pages from cache */
705 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->ea
);
707 /* free EA extent from working block map */
709 pxdlock
= (struct pxd_lock
*) & maplock
;
710 pxdlock
->flag
= mlckFREEPXD
;
711 PXDaddress(&pxdlock
->pxd
, xaddr
);
712 PXDlength(&pxdlock
->pxd
, xlen
);
713 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
719 if (JFS_IP(ip
)->acl
.flag
& DXD_EXTENT
) {
720 s64 xaddr
= addressDXD(&JFS_IP(ip
)->acl
);
721 int xlen
= lengthDXD(&JFS_IP(ip
)->acl
);
722 struct maplock maplock
; /* maplock for COMMIT_WMAP */
723 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
725 invalidate_dxd_metapages(ip
, JFS_IP(ip
)->acl
);
727 /* free ACL 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
);
737 * free xtree/data (truncate to zero length):
738 * free xtree/data pages from cache, and
739 * free xtree/data blocks from working block map;
742 rc
= xtTruncate(0, ip
, 0, COMMIT_WMAP
);
748 * NAME: jfs_link(vp, dvp, name, crp)
750 * FUNCTION: create a link to <vp> by the name = <name>
751 * in the parent directory <dvp>
753 * PARAMETER: vp - target object
754 * dvp - parent directory of new link
755 * name - name of new link to target object
758 * RETURN: Errors from subroutines
761 * JFS does NOT support link() on directories (to prevent circular
762 * path in the directory hierarchy);
763 * EPERM: the target object is a directory, and either the caller
764 * does not have appropriate privileges or the implementation prohibits
765 * using link() on directories [XPG4.2].
767 * JFS does NOT support links between file systems:
768 * EXDEV: target object and new link are on different file systems and
769 * implementation does not support links between file systems [XPG4.2].
771 static int jfs_link(struct dentry
*old_dentry
,
772 struct inode
*dir
, struct dentry
*dentry
)
776 struct inode
*ip
= old_dentry
->d_inode
;
778 struct component_name dname
;
779 struct btstack btstack
;
780 struct inode
*iplist
[2];
782 jfs_info("jfs_link: %s %s", old_dentry
->d_name
.name
,
783 dentry
->d_name
.name
);
785 if (ip
->i_nlink
== JFS_LINK_MAX
)
788 if (ip
->i_nlink
== 0)
791 tid
= txBegin(ip
->i_sb
, 0);
793 down(&JFS_IP(dir
)->commit_sem
);
794 down(&JFS_IP(ip
)->commit_sem
);
797 * scan parent directory for entry/freespace
799 if ((rc
= get_UCSname(&dname
, dentry
)))
802 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
806 * create entry for new link in parent directory
809 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
812 /* update object inode */
813 ip
->i_nlink
++; /* for new link */
814 ip
->i_ctime
= CURRENT_TIME
;
815 mark_inode_dirty(dir
);
816 atomic_inc(&ip
->i_count
);
820 rc
= txCommit(tid
, 2, &iplist
[0], 0);
826 d_instantiate(dentry
, ip
);
829 free_UCSname(&dname
);
834 up(&JFS_IP(dir
)->commit_sem
);
835 up(&JFS_IP(ip
)->commit_sem
);
837 jfs_info("jfs_link: rc:%d", rc
);
842 * NAME: jfs_symlink(dip, dentry, name)
844 * FUNCTION: creates a symbolic link to <symlink> by name <name>
847 * PARAMETER: dip - parent directory vnode
848 * dentry - dentry of symbolic link
849 * name - the path name of the existing object
850 * that will be the source of the link
852 * RETURN: errors from subroutines
855 * ENAMETOOLONG: pathname resolution of a symbolic link produced
856 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
859 static int jfs_symlink(struct inode
*dip
, struct dentry
*dentry
,
865 struct component_name dname
;
866 int ssize
; /* source pathname size */
867 struct btstack btstack
;
868 struct inode
*ip
= dentry
->d_inode
;
869 unchar
*i_fastsymlink
;
871 int bmask
= 0, xsize
;
872 s64 extent
= 0, xaddr
;
874 struct super_block
*sb
;
877 struct inode
*iplist
[2];
879 jfs_info("jfs_symlink: dip:0x%p name:%s", dip
, name
);
881 ssize
= strlen(name
) + 1;
884 * search parent directory for entry/freespace
885 * (dtSearch() returns parent directory page pinned)
888 if ((rc
= get_UCSname(&dname
, dentry
)))
892 * allocate on-disk/in-memory inode for symbolic link:
893 * (iAlloc() returns new, locked inode)
895 ip
= ialloc(dip
, S_IFLNK
| 0777);
901 tid
= txBegin(dip
->i_sb
, 0);
903 down(&JFS_IP(dip
)->commit_sem
);
904 down(&JFS_IP(ip
)->commit_sem
);
906 tblk
= tid_to_tblock(tid
);
907 tblk
->xflag
|= COMMIT_CREATE
;
908 tblk
->ino
= ip
->i_ino
;
909 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
911 /* fix symlink access permission
912 * (dir_create() ANDs in the u.u_cmask,
913 * but symlinks really need to be 777 access)
918 * write symbolic link target path name
923 * write source path name inline in on-disk inode (fast symbolic link)
926 if (ssize
<= IDATASIZE
) {
927 ip
->i_op
= &jfs_symlink_inode_operations
;
929 i_fastsymlink
= JFS_IP(ip
)->i_inline
;
930 memcpy(i_fastsymlink
, name
, ssize
);
931 ip
->i_size
= ssize
- 1;
934 * if symlink is > 128 bytes, we don't have the space to
935 * store inline extended attributes
937 if (ssize
> sizeof (JFS_IP(ip
)->i_inline
))
938 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
940 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
944 * write source path name in a single extent
947 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip
);
949 ip
->i_op
= &page_symlink_inode_operations
;
950 ip
->i_mapping
->a_ops
= &jfs_aops
;
953 * even though the data of symlink object (source
954 * path name) is treated as non-journaled user data,
955 * it is read/written thru buffer cache for performance.
958 bmask
= JFS_SBI(sb
)->bsize
- 1;
959 xsize
= (ssize
+ bmask
) & ~bmask
;
961 xlen
= xsize
>> JFS_SBI(sb
)->l2bsize
;
962 if ((rc
= xtInsert(tid
, ip
, 0, 0, xlen
, &xaddr
, 0))) {
968 ip
->i_size
= ssize
- 1;
970 /* This is kind of silly since PATH_MAX == 4K */
971 int copy_size
= min(ssize
, PSIZE
);
973 mp
= get_metapage(ip
, xaddr
, PSIZE
, 1);
976 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
981 memcpy(mp
->data
, name
, copy_size
);
985 xaddr
+= JFS_SBI(sb
)->nbperpage
;
990 * create entry for symbolic link in parent directory
992 rc
= dtSearch(dip
, &dname
, &ino
, &btstack
, JFS_CREATE
);
995 rc
= dtInsert(tid
, dip
, &dname
, &ino
, &btstack
);
999 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
1001 /* discard new inode */
1005 insert_inode_hash(ip
);
1006 mark_inode_dirty(ip
);
1009 * commit update of parent directory and link object
1014 rc
= txCommit(tid
, 2, &iplist
[0], 0);
1018 up(&JFS_IP(dip
)->commit_sem
);
1019 up(&JFS_IP(ip
)->commit_sem
);
1024 d_instantiate(dentry
, ip
);
1027 free_UCSname(&dname
);
1029 #ifdef CONFIG_JFS_POSIX_ACL
1031 jfs_init_acl(ip
, dip
);
1035 jfs_info("jfs_symlink: rc:%d", rc
);
1043 * FUNCTION: rename a file or directory
1045 static int jfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1046 struct inode
*new_dir
, struct dentry
*new_dentry
)
1048 struct btstack btstack
;
1050 struct component_name new_dname
;
1051 struct inode
*new_ip
;
1052 struct component_name old_dname
;
1053 struct inode
*old_ip
;
1057 struct dt_lock
*dtlck
;
1060 struct inode
*iplist
[4];
1061 struct tblock
*tblk
;
1066 jfs_info("jfs_rename: %s %s", old_dentry
->d_name
.name
,
1067 new_dentry
->d_name
.name
);
1069 old_ip
= old_dentry
->d_inode
;
1070 new_ip
= new_dentry
->d_inode
;
1072 if ((rc
= get_UCSname(&old_dname
, old_dentry
)))
1075 if ((rc
= get_UCSname(&new_dname
, new_dentry
)))
1079 * Make sure source inode number is what we think it is
1081 rc
= dtSearch(old_dir
, &old_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1082 if (rc
|| (ino
!= old_ip
->i_ino
)) {
1088 * Make sure dest inode number (if any) is what we think it is
1090 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
, JFS_LOOKUP
);
1092 if ((new_ip
== 0) || (ino
!= new_ip
->i_ino
)) {
1096 } else if (rc
!= -ENOENT
)
1099 /* no entry exists, but one was expected */
1104 if (S_ISDIR(old_ip
->i_mode
)) {
1106 if (!dtEmpty(new_ip
)) {
1110 } else if ((new_dir
!= old_dir
) &&
1111 (new_dir
->i_nlink
== JFS_LINK_MAX
)) {
1115 } else if (new_ip
) {
1116 IWRITE_LOCK(new_ip
);
1117 /* Init inode for quota operations. */
1122 * The real work starts here
1124 tid
= txBegin(new_dir
->i_sb
, 0);
1126 down(&JFS_IP(new_dir
)->commit_sem
);
1127 down(&JFS_IP(old_ip
)->commit_sem
);
1128 if (old_dir
!= new_dir
)
1129 down(&JFS_IP(old_dir
)->commit_sem
);
1132 down(&JFS_IP(new_ip
)->commit_sem
);
1134 * Change existing directory entry to new inode number
1136 ino
= new_ip
->i_ino
;
1137 rc
= dtModify(tid
, new_dir
, &new_dname
, &ino
,
1138 old_ip
->i_ino
, JFS_RENAME
);
1142 if (S_ISDIR(new_ip
->i_mode
)) {
1144 if (new_ip
->i_nlink
) {
1145 up(&JFS_IP(new_dir
)->commit_sem
);
1146 up(&JFS_IP(old_ip
)->commit_sem
);
1147 if (old_dir
!= new_dir
)
1148 up(&JFS_IP(old_dir
)->commit_sem
);
1149 if (!S_ISDIR(old_ip
->i_mode
) && new_ip
)
1150 IWRITE_UNLOCK(new_ip
);
1151 jfs_error(new_ip
->i_sb
,
1152 "jfs_rename: new_ip->i_nlink != 0");
1155 tblk
= tid_to_tblock(tid
);
1156 tblk
->xflag
|= COMMIT_DELETE
;
1157 tblk
->u
.ip
= new_ip
;
1158 } else if (new_ip
->i_nlink
== 0) {
1159 assert(!test_cflag(COMMIT_Nolink
, new_ip
));
1160 /* free block resources */
1161 if ((new_size
= commitZeroLink(tid
, new_ip
)) < 0) {
1162 txAbort(tid
, 1); /* Marks FS Dirty */
1166 tblk
= tid_to_tblock(tid
);
1167 tblk
->xflag
|= COMMIT_DELETE
;
1168 tblk
->u
.ip
= new_ip
;
1170 new_ip
->i_ctime
= CURRENT_TIME
;
1171 mark_inode_dirty(new_ip
);
1175 * Add new directory entry
1177 rc
= dtSearch(new_dir
, &new_dname
, &ino
, &btstack
,
1180 jfs_err("jfs_rename didn't expect dtSearch to fail "
1185 ino
= old_ip
->i_ino
;
1186 rc
= dtInsert(tid
, new_dir
, &new_dname
, &ino
, &btstack
);
1189 jfs_err("jfs_rename: dtInsert returned -EIO");
1192 if (S_ISDIR(old_ip
->i_mode
))
1196 * Remove old directory entry
1199 ino
= old_ip
->i_ino
;
1200 rc
= dtDelete(tid
, old_dir
, &old_dname
, &ino
, JFS_REMOVE
);
1202 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1204 txAbort(tid
, 1); /* Marks Filesystem dirty */
1207 if (S_ISDIR(old_ip
->i_mode
)) {
1209 if (old_dir
!= new_dir
) {
1211 * Change inode number of parent for moved directory
1214 JFS_IP(old_ip
)->i_dtroot
.header
.idotdot
=
1215 cpu_to_le32(new_dir
->i_ino
);
1217 /* Linelock header of dtree */
1218 tlck
= txLock(tid
, old_ip
,
1219 (struct metapage
*) &JFS_IP(old_ip
)->bxflag
,
1220 tlckDTREE
| tlckBTROOT
| tlckRELINK
);
1221 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1222 ASSERT(dtlck
->index
== 0);
1223 lv
= & dtlck
->lv
[0];
1231 * Update ctime on changed/moved inodes & mark dirty
1233 old_ip
->i_ctime
= CURRENT_TIME
;
1234 mark_inode_dirty(old_ip
);
1236 new_dir
->i_ctime
= new_dir
->i_mtime
= CURRENT_TIME
;
1237 mark_inode_dirty(new_dir
);
1239 /* Build list of inodes modified by this transaction */
1241 iplist
[ipcount
++] = old_ip
;
1243 iplist
[ipcount
++] = new_ip
;
1244 iplist
[ipcount
++] = old_dir
;
1246 if (old_dir
!= new_dir
) {
1247 iplist
[ipcount
++] = new_dir
;
1248 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1249 mark_inode_dirty(old_dir
);
1253 * Incomplete truncate of file data can
1254 * result in timing problems unless we synchronously commit the
1258 commit_flag
= COMMIT_SYNC
;
1262 rc
= txCommit(tid
, ipcount
, iplist
, commit_flag
);
1267 up(&JFS_IP(new_dir
)->commit_sem
);
1268 up(&JFS_IP(old_ip
)->commit_sem
);
1269 if (old_dir
!= new_dir
)
1270 up(&JFS_IP(old_dir
)->commit_sem
);
1272 up(&JFS_IP(new_ip
)->commit_sem
);
1274 while (new_size
&& (rc
== 0)) {
1275 tid
= txBegin(new_ip
->i_sb
, 0);
1276 down(&JFS_IP(new_ip
)->commit_sem
);
1277 new_size
= xtTruncate_pmap(tid
, new_ip
, new_size
);
1282 rc
= txCommit(tid
, 1, &new_ip
, COMMIT_SYNC
);
1284 up(&JFS_IP(new_ip
)->commit_sem
);
1286 if (new_ip
&& (new_ip
->i_nlink
== 0))
1287 set_cflag(COMMIT_Nolink
, new_ip
);
1289 free_UCSname(&new_dname
);
1291 free_UCSname(&old_dname
);
1293 if (new_ip
&& !S_ISDIR(new_ip
->i_mode
))
1294 IWRITE_UNLOCK(new_ip
);
1296 * Truncating the directory index table is not guaranteed. It
1297 * may need to be done iteratively
1299 if (test_cflag(COMMIT_Stale
, old_dir
)) {
1300 if (old_dir
->i_size
> 1)
1301 jfs_truncate_nolock(old_dir
, 0);
1303 clear_cflag(COMMIT_Stale
, old_dir
);
1306 jfs_info("jfs_rename: returning %d", rc
);
1314 * FUNCTION: Create a special file (device)
1316 static int jfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1317 int mode
, dev_t rdev
)
1319 struct jfs_inode_info
*jfs_ip
;
1320 struct btstack btstack
;
1321 struct component_name dname
;
1324 struct inode
*iplist
[2];
1327 struct tblock
*tblk
;
1329 if (!new_valid_dev(rdev
))
1332 jfs_info("jfs_mknod: %s", dentry
->d_name
.name
);
1334 if ((rc
= get_UCSname(&dname
, dentry
)))
1337 ip
= ialloc(dir
, mode
);
1342 jfs_ip
= JFS_IP(ip
);
1344 tid
= txBegin(dir
->i_sb
, 0);
1346 down(&JFS_IP(dir
)->commit_sem
);
1347 down(&JFS_IP(ip
)->commit_sem
);
1349 if ((rc
= dtSearch(dir
, &dname
, &ino
, &btstack
, JFS_CREATE
)))
1352 tblk
= tid_to_tblock(tid
);
1353 tblk
->xflag
|= COMMIT_CREATE
;
1354 tblk
->ino
= ip
->i_ino
;
1355 tblk
->u
.ixpxd
= JFS_IP(ip
)->ixpxd
;
1358 if ((rc
= dtInsert(tid
, dir
, &dname
, &ino
, &btstack
)))
1361 ip
->i_op
= &jfs_file_inode_operations
;
1362 jfs_ip
->dev
= new_encode_dev(rdev
);
1363 init_special_inode(ip
, ip
->i_mode
, rdev
);
1365 insert_inode_hash(ip
);
1366 mark_inode_dirty(ip
);
1368 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
1370 mark_inode_dirty(dir
);
1374 rc
= txCommit(tid
, 2, iplist
, 0);
1378 up(&JFS_IP(ip
)->commit_sem
);
1379 up(&JFS_IP(dir
)->commit_sem
);
1384 d_instantiate(dentry
, ip
);
1387 free_UCSname(&dname
);
1389 #ifdef CONFIG_JFS_POSIX_ACL
1391 jfs_init_acl(ip
, dir
);
1395 jfs_info("jfs_mknod: returning %d", rc
);
1399 static struct dentry
*jfs_lookup(struct inode
*dip
, struct dentry
*dentry
, struct nameidata
*nd
)
1401 struct btstack btstack
;
1404 struct component_name key
;
1405 const char *name
= dentry
->d_name
.name
;
1406 int len
= dentry
->d_name
.len
;
1409 jfs_info("jfs_lookup: name = %s", name
);
1412 if ((name
[0] == '.') && (len
== 1))
1414 else if (strcmp(name
, "..") == 0)
1417 if ((rc
= get_UCSname(&key
, dentry
)))
1419 rc
= dtSearch(dip
, &key
, &inum
, &btstack
, JFS_LOOKUP
);
1421 if (rc
== -ENOENT
) {
1422 d_add(dentry
, NULL
);
1425 jfs_err("jfs_lookup: dtSearch returned %d", rc
);
1430 ip
= iget(dip
->i_sb
, inum
);
1431 if (ip
== NULL
|| is_bad_inode(ip
)) {
1432 jfs_err("jfs_lookup: iget failed on inum %d", (uint
) inum
);
1435 return ERR_PTR(-EACCES
);
1438 if (JFS_SBI(dip
->i_sb
)->mntflag
& JFS_OS2
)
1439 dentry
->d_op
= &jfs_ci_dentry_operations
;
1441 dentry
= d_splice_alias(ip
, dentry
);
1443 if (dentry
&& (JFS_SBI(dip
->i_sb
)->mntflag
& JFS_OS2
))
1444 dentry
->d_op
= &jfs_ci_dentry_operations
;
1449 struct dentry
*jfs_get_parent(struct dentry
*dentry
)
1451 struct super_block
*sb
= dentry
->d_inode
->i_sb
;
1452 struct dentry
*parent
= ERR_PTR(-ENOENT
);
1453 struct inode
*inode
;
1454 unsigned long parent_ino
;
1457 le32_to_cpu(JFS_IP(dentry
->d_inode
)->i_dtroot
.header
.idotdot
);
1458 inode
= iget(sb
, parent_ino
);
1460 if (is_bad_inode(inode
)) {
1462 parent
= ERR_PTR(-EACCES
);
1464 parent
= d_alloc_anon(inode
);
1466 parent
= ERR_PTR(-ENOMEM
);
1475 struct inode_operations jfs_dir_inode_operations
= {
1476 .create
= jfs_create
,
1477 .lookup
= jfs_lookup
,
1479 .unlink
= jfs_unlink
,
1480 .symlink
= jfs_symlink
,
1484 .rename
= jfs_rename
,
1485 .setxattr
= jfs_setxattr
,
1486 .getxattr
= jfs_getxattr
,
1487 .listxattr
= jfs_listxattr
,
1488 .removexattr
= jfs_removexattr
,
1489 #ifdef CONFIG_JFS_POSIX_ACL
1490 .setattr
= jfs_setattr
,
1491 .permission
= jfs_permission
,
1495 struct file_operations jfs_dir_operations
= {
1496 .read
= generic_read_dir
,
1497 .readdir
= jfs_readdir
,
1501 static int jfs_ci_hash(struct dentry
*dir
, struct qstr
*this)
1506 hash
= init_name_hash();
1507 for (i
=0; i
< this->len
; i
++)
1508 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
1509 this->hash
= end_name_hash(hash
);
1514 static int jfs_ci_compare(struct dentry
*dir
, struct qstr
*a
, struct qstr
*b
)
1518 if (a
->len
!= b
->len
)
1520 for (i
=0; i
< a
->len
; i
++) {
1521 if (tolower(a
->name
[i
]) != tolower(b
->name
[i
]))
1527 * We want creates to preserve case. A negative dentry, a, that
1528 * has a different case than b may cause a new entry to be created
1529 * with the wrong case. Since we can't tell if a comes from a negative
1530 * dentry, we blindly replace it with b. This should be harmless if
1531 * a is not a negative dentry.
1533 memcpy((unsigned char *)a
->name
, b
->name
, a
->len
);
1538 struct dentry_operations jfs_ci_dentry_operations
=
1540 .d_hash
= jfs_ci_hash
,
1541 .d_compare
= jfs_ci_compare
,