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"
37 struct dentry_operations jfs_ci_dentry_operations;
39 static s64 commitZeroLink(tid_t, struct inode *);
42 * NAME: free_ea_wmap(inode)
44 * FUNCTION: free uncommitted extended attributes from working map
47 static inline void free_ea_wmap(struct inode *inode)
49 dxd_t *ea = &JFS_IP(inode)->ea;
51 if (ea->flag & DXD_EXTENT) {
52 /* free EA pages from cache */
53 invalidate_dxd_metapages(inode, *ea);
54 dbFree(inode, addressDXD(ea), lengthDXD(ea));
60 * NAME: jfs_create(dip, dentry, mode)
62 * FUNCTION: create a regular file in the parent directory <dip>
63 * with name = <from dentry> and mode = <mode>
65 * PARAMETER: dip - parent directory vnode
66 * dentry - dentry of new file
67 * mode - create mode (rwxrwxrwx).
70 * RETURN: Errors from subroutines
73 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
77 tid_t tid; /* transaction id */
78 struct inode *ip = NULL; /* child directory inode */
80 struct component_name dname; /* child directory name */
81 struct btstack btstack;
82 struct inode *iplist[2];
85 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
88 * search parent directory for entry/freespace
89 * (dtSearch() returns parent directory page pinned)
91 if ((rc = get_UCSname(&dname, dentry)))
95 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
96 * block there while holding dtree page, so we allocate the inode &
97 * begin the transaction before we search the directory.
99 ip = ialloc(dip, mode);
105 tid = txBegin(dip->i_sb, 0);
107 mutex_lock(&JFS_IP(dip)->commit_mutex);
108 mutex_lock(&JFS_IP(ip)->commit_mutex);
110 rc = jfs_init_acl(tid, ip, dip);
114 rc = jfs_init_security(tid, ip, dip);
120 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
121 jfs_err("jfs_create: dtSearch returned %d", rc);
126 tblk = tid_to_tblock(tid);
127 tblk->xflag |= COMMIT_CREATE;
128 tblk->ino = ip->i_ino;
129 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
135 * initialize the child XAD tree root in-line in inode
140 * create entry in parent directory for child directory
141 * (dtInsert() releases parent directory page)
144 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
146 jfs_err("jfs_create: dtInsert returned -EIO");
147 txAbort(tid, 1); /* Marks Filesystem dirty */
149 txAbort(tid, 0); /* Filesystem full */
153 ip->i_op = &jfs_file_inode_operations;
154 ip->i_fop = &jfs_file_operations;
155 ip->i_mapping->a_ops = &jfs_aops;
157 insert_inode_hash(ip);
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);
175 d_instantiate(dentry, ip);
178 free_UCSname(&dname);
182 jfs_info("jfs_create: rc:%d", rc);
188 * NAME: jfs_mkdir(dip, dentry, mode)
190 * FUNCTION: create a child directory in the parent directory <dip>
191 * with name = <from dentry> and mode = <mode>
193 * PARAMETER: dip - parent directory vnode
194 * dentry - dentry of child directory
195 * mode - create mode (rwxrwxrwx).
197 * RETURN: Errors from subroutines
200 * EACCESS: user needs search+write permission on the parent directory
202 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
205 tid_t tid; /* transaction id */
206 struct inode *ip = NULL; /* child directory inode */
208 struct component_name dname; /* child directory name */
209 struct btstack btstack;
210 struct inode *iplist[2];
213 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
215 /* link count overflow on parent directory ? */
216 if (dip->i_nlink == JFS_LINK_MAX) {
222 * search parent directory for entry/freespace
223 * (dtSearch() returns parent directory page pinned)
225 if ((rc = get_UCSname(&dname, dentry)))
229 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
230 * block there while holding dtree page, so we allocate the inode &
231 * begin the transaction before we search the directory.
233 ip = ialloc(dip, S_IFDIR | mode);
239 tid = txBegin(dip->i_sb, 0);
241 mutex_lock(&JFS_IP(dip)->commit_mutex);
242 mutex_lock(&JFS_IP(ip)->commit_mutex);
244 rc = jfs_init_acl(tid, ip, dip);
248 rc = jfs_init_security(tid, ip, dip);
254 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
255 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
260 tblk = tid_to_tblock(tid);
261 tblk->xflag |= COMMIT_CREATE;
262 tblk->ino = ip->i_ino;
263 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
269 * initialize the child directory in-line in inode
271 dtInitRoot(tid, ip, dip->i_ino);
274 * create entry in parent directory for child directory
275 * (dtInsert() releases parent directory page)
278 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
280 jfs_err("jfs_mkdir: dtInsert returned -EIO");
281 txAbort(tid, 1); /* Marks Filesystem dirty */
283 txAbort(tid, 0); /* Filesystem full */
287 ip->i_nlink = 2; /* for '.' */
288 ip->i_op = &jfs_dir_inode_operations;
289 ip->i_fop = &jfs_dir_operations;
291 insert_inode_hash(ip);
292 mark_inode_dirty(ip);
294 /* update parent directory inode */
295 inc_nlink(dip); /* for '..' from child directory */
296 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
297 mark_inode_dirty(dip);
299 rc = txCommit(tid, 2, &iplist[0], 0);
303 mutex_unlock(&JFS_IP(ip)->commit_mutex);
304 mutex_unlock(&JFS_IP(dip)->commit_mutex);
310 d_instantiate(dentry, ip);
313 free_UCSname(&dname);
318 jfs_info("jfs_mkdir: rc:%d", rc);
323 * NAME: jfs_rmdir(dip, dentry)
325 * FUNCTION: remove a link to child directory
327 * PARAMETER: dip - parent inode
328 * dentry - child directory dentry
330 * RETURN: -EINVAL - if name is . or ..
331 * -EINVAL - if . or .. exist but are invalid.
332 * errors from subroutines
335 * if other threads have the directory open when the last link
336 * is removed, the "." and ".." entries, if present, are removed before
337 * rmdir() returns and no new entries may be created in the directory,
338 * but the directory is not removed until the last reference to
339 * the directory is released (cf.unlink() of regular file).
341 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
344 tid_t tid; /* transaction id */
345 struct inode *ip = dentry->d_inode;
347 struct component_name dname;
348 struct inode *iplist[2];
351 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
353 /* Init inode for quota operations. */
356 /* directory must be empty to be removed */
362 if ((rc = get_UCSname(&dname, dentry))) {
366 tid = txBegin(dip->i_sb, 0);
368 mutex_lock(&JFS_IP(dip)->commit_mutex);
369 mutex_lock(&JFS_IP(ip)->commit_mutex);
374 tblk = tid_to_tblock(tid);
375 tblk->xflag |= COMMIT_DELETE;
379 * delete the entry of target directory from parent directory
382 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
383 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
387 mutex_unlock(&JFS_IP(ip)->commit_mutex);
388 mutex_unlock(&JFS_IP(dip)->commit_mutex);
393 /* update parent directory's link count corresponding
394 * to ".." entry of the target directory deleted
396 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
397 inode_dec_link_count(dip);
400 * OS/2 could have created EA and/or ACL
402 /* free EA from both persistent and working map */
403 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
405 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
407 JFS_IP(ip)->ea.flag = 0;
409 /* free ACL from both persistent and working map */
410 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
412 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
414 JFS_IP(ip)->acl.flag = 0;
416 /* mark the target directory as deleted */
418 mark_inode_dirty(ip);
420 rc = txCommit(tid, 2, &iplist[0], 0);
424 mutex_unlock(&JFS_IP(ip)->commit_mutex);
425 mutex_unlock(&JFS_IP(dip)->commit_mutex);
428 * Truncating the directory index table is not guaranteed. It
429 * may need to be done iteratively
431 if (test_cflag(COMMIT_Stale, dip)) {
433 jfs_truncate_nolock(dip, 0);
435 clear_cflag(COMMIT_Stale, dip);
439 free_UCSname(&dname);
442 jfs_info("jfs_rmdir: rc:%d", rc);
447 * NAME: jfs_unlink(dip, dentry)
449 * FUNCTION: remove a link to object <vp> named by <name>
450 * from parent directory <dvp>
452 * PARAMETER: dip - inode of parent directory
453 * dentry - dentry of object to be removed
455 * RETURN: errors from subroutines
458 * temporary file: if one or more processes have the file open
459 * when the last link is removed, the link will be removed before
460 * unlink() returns, but the removal of the file contents will be
461 * postponed until all references to the files are closed.
463 * JFS does NOT support unlink() on directories.
466 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
469 tid_t tid; /* transaction id */
470 struct inode *ip = dentry->d_inode;
472 struct component_name dname; /* object name */
473 struct inode *iplist[2];
478 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
480 /* Init inode for quota operations. */
483 if ((rc = get_UCSname(&dname, dentry)))
488 tid = txBegin(dip->i_sb, 0);
490 mutex_lock(&JFS_IP(dip)->commit_mutex);
491 mutex_lock(&JFS_IP(ip)->commit_mutex);
497 * delete the entry of target file from parent directory
500 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
501 jfs_err("jfs_unlink: dtDelete returned %d", rc);
503 txAbort(tid, 1); /* Marks FS Dirty */
505 mutex_unlock(&JFS_IP(ip)->commit_mutex);
506 mutex_unlock(&JFS_IP(dip)->commit_mutex);
513 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
514 mark_inode_dirty(dip);
516 /* update target's inode */
517 inode_dec_link_count(ip);
520 * commit zero link count object
522 if (ip->i_nlink == 0) {
523 assert(!test_cflag(COMMIT_Nolink, ip));
524 /* free block resources */
525 if ((new_size = commitZeroLink(tid, ip)) < 0) {
526 txAbort(tid, 1); /* Marks FS Dirty */
528 mutex_unlock(&JFS_IP(ip)->commit_mutex);
529 mutex_unlock(&JFS_IP(dip)->commit_mutex);
534 tblk = tid_to_tblock(tid);
535 tblk->xflag |= COMMIT_DELETE;
540 * Incomplete truncate of file data can
541 * result in timing problems unless we synchronously commit the
545 commit_flag = COMMIT_SYNC;
550 * If xtTruncate was incomplete, commit synchronously to avoid
551 * timing complications
553 rc = txCommit(tid, 2, &iplist[0], commit_flag);
557 mutex_unlock(&JFS_IP(ip)->commit_mutex);
558 mutex_unlock(&JFS_IP(dip)->commit_mutex);
560 while (new_size && (rc == 0)) {
561 tid = txBegin(dip->i_sb, 0);
562 mutex_lock(&JFS_IP(ip)->commit_mutex);
563 new_size = xtTruncate_pmap(tid, ip, new_size);
565 txAbort(tid, 1); /* Marks FS Dirty */
568 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
570 mutex_unlock(&JFS_IP(ip)->commit_mutex);
573 if (ip->i_nlink == 0)
574 set_cflag(COMMIT_Nolink, ip);
579 * Truncating the directory index table is not guaranteed. It
580 * may need to be done iteratively
582 if (test_cflag(COMMIT_Stale, dip)) {
584 jfs_truncate_nolock(dip, 0);
586 clear_cflag(COMMIT_Stale, dip);
590 free_UCSname(&dname);
592 jfs_info("jfs_unlink: rc:%d", rc);
597 * NAME: commitZeroLink()
599 * FUNCTION: for non-directory, called by jfs_remove(),
600 * truncate a regular file, directory or symbolic
601 * link to zero length. return 0 if type is not
604 * if the file is currently associated with a VM segment
605 * only permanent disk and inode map resources are freed,
606 * and neither the inode nor indirect blocks are modified
607 * so that the resources can be later freed in the work
609 * if there is no VM segment on entry, the resources are
610 * freed in both work and permanent map.
611 * (? for temporary file - memory object is cached even
612 * after no reference:
613 * reference count > 0 - )
615 * PARAMETERS: cd - pointer to commit data structure.
616 * current inode is the one to truncate.
618 * RETURN: Errors from subroutines
620 static s64 commitZeroLink(tid_t tid, struct inode *ip)
625 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
627 filetype = ip->i_mode & S_IFMT;
632 /* fast symbolic link */
633 if (ip->i_size < IDATASIZE) {
639 assert(filetype != S_IFDIR);
643 set_cflag(COMMIT_Freewmap, ip);
645 /* mark transaction of block map update type */
646 tblk = tid_to_tblock(tid);
647 tblk->xflag |= COMMIT_PMAP;
652 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
653 /* acquire maplock on EA to be freed from block map */
654 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
659 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
660 /* acquire maplock on EA to be freed from block map */
661 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
664 * free xtree/data (truncate to zero length):
665 * free xtree/data pages from cache if COMMIT_PWMAP,
666 * free xtree/data blocks from persistent block map, and
667 * free xtree/data blocks from working block map if COMMIT_PWMAP;
670 return xtTruncate_pmap(tid, ip, 0);
677 * NAME: jfs_free_zero_link()
679 * FUNCTION: for non-directory, called by iClose(),
680 * free resources of a file from cache and WORKING map
681 * for a file previously committed with zero link count
682 * while associated with a pager object,
684 * PARAMETER: ip - pointer to inode of file.
686 void jfs_free_zero_link(struct inode *ip)
690 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
692 /* return if not reg or symbolic link or if size is
695 type = ip->i_mode & S_IFMT;
701 /* if its contained in inode nothing to do */
702 if (ip->i_size < IDATASIZE)
712 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
713 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
714 int xlen = lengthDXD(&JFS_IP(ip)->ea);
715 struct maplock maplock; /* maplock for COMMIT_WMAP */
716 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
718 /* free EA pages from cache */
719 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
721 /* free EA extent from working block map */
723 pxdlock = (struct pxd_lock *) & maplock;
724 pxdlock->flag = mlckFREEPXD;
725 PXDaddress(&pxdlock->pxd, xaddr);
726 PXDlength(&pxdlock->pxd, xlen);
727 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
733 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
734 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
735 int xlen = lengthDXD(&JFS_IP(ip)->acl);
736 struct maplock maplock; /* maplock for COMMIT_WMAP */
737 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
739 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
741 /* free ACL extent from working block map */
743 pxdlock = (struct pxd_lock *) & maplock;
744 pxdlock->flag = mlckFREEPXD;
745 PXDaddress(&pxdlock->pxd, xaddr);
746 PXDlength(&pxdlock->pxd, xlen);
747 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
751 * free xtree/data (truncate to zero length):
752 * free xtree/data pages from cache, and
753 * free xtree/data blocks from working block map;
756 xtTruncate(0, ip, 0, COMMIT_WMAP);
760 * NAME: jfs_link(vp, dvp, name, crp)
762 * FUNCTION: create a link to <vp> by the name = <name>
763 * in the parent directory <dvp>
765 * PARAMETER: vp - target object
766 * dvp - parent directory of new link
767 * name - name of new link to target object
770 * RETURN: Errors from subroutines
773 * JFS does NOT support link() on directories (to prevent circular
774 * path in the directory hierarchy);
775 * EPERM: the target object is a directory, and either the caller
776 * does not have appropriate privileges or the implementation prohibits
777 * using link() on directories [XPG4.2].
779 * JFS does NOT support links between file systems:
780 * EXDEV: target object and new link are on different file systems and
781 * implementation does not support links between file systems [XPG4.2].
783 static int jfs_link(struct dentry *old_dentry,
784 struct inode *dir, struct dentry *dentry)
788 struct inode *ip = old_dentry->d_inode;
790 struct component_name dname;
791 struct btstack btstack;
792 struct inode *iplist[2];
794 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
795 dentry->d_name.name);
797 if (ip->i_nlink == JFS_LINK_MAX)
800 if (ip->i_nlink == 0)
803 tid = txBegin(ip->i_sb, 0);
805 mutex_lock(&JFS_IP(dir)->commit_mutex);
806 mutex_lock(&JFS_IP(ip)->commit_mutex);
809 * scan parent directory for entry/freespace
811 if ((rc = get_UCSname(&dname, dentry)))
814 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
818 * create entry for new link in parent directory
821 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
824 /* update object inode */
825 inc_nlink(ip); /* for new link */
826 ip->i_ctime = CURRENT_TIME;
827 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
828 mark_inode_dirty(dir);
829 atomic_inc(&ip->i_count);
833 rc = txCommit(tid, 2, &iplist[0], 0);
836 ip->i_nlink--; /* never instantiated */
839 d_instantiate(dentry, ip);
842 free_UCSname(&dname);
847 mutex_unlock(&JFS_IP(ip)->commit_mutex);
848 mutex_unlock(&JFS_IP(dir)->commit_mutex);
850 jfs_info("jfs_link: rc:%d", rc);
855 * NAME: jfs_symlink(dip, dentry, name)
857 * FUNCTION: creates a symbolic link to <symlink> by name <name>
860 * PARAMETER: dip - parent directory vnode
861 * dentry - dentry of symbolic link
862 * name - the path name of the existing object
863 * that will be the source of the link
865 * RETURN: errors from subroutines
868 * ENAMETOOLONG: pathname resolution of a symbolic link produced
869 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
872 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
878 struct component_name dname;
879 int ssize; /* source pathname size */
880 struct btstack btstack;
881 struct inode *ip = dentry->d_inode;
882 unchar *i_fastsymlink;
884 int bmask = 0, xsize;
885 s64 extent = 0, xaddr;
887 struct super_block *sb;
890 struct inode *iplist[2];
892 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
894 ssize = strlen(name) + 1;
897 * search parent directory for entry/freespace
898 * (dtSearch() returns parent directory page pinned)
901 if ((rc = get_UCSname(&dname, dentry)))
905 * allocate on-disk/in-memory inode for symbolic link:
906 * (iAlloc() returns new, locked inode)
908 ip = ialloc(dip, S_IFLNK | 0777);
914 tid = txBegin(dip->i_sb, 0);
916 mutex_lock(&JFS_IP(dip)->commit_mutex);
917 mutex_lock(&JFS_IP(ip)->commit_mutex);
919 rc = jfs_init_security(tid, ip, dip);
923 tblk = tid_to_tblock(tid);
924 tblk->xflag |= COMMIT_CREATE;
925 tblk->ino = ip->i_ino;
926 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
928 /* fix symlink access permission
929 * (dir_create() ANDs in the u.u_cmask,
930 * but symlinks really need to be 777 access)
935 * write symbolic link target path name
940 * write source path name inline in on-disk inode (fast symbolic link)
943 if (ssize <= IDATASIZE) {
944 ip->i_op = &jfs_symlink_inode_operations;
946 i_fastsymlink = JFS_IP(ip)->i_inline;
947 memcpy(i_fastsymlink, name, ssize);
948 ip->i_size = ssize - 1;
951 * if symlink is > 128 bytes, we don't have the space to
952 * store inline extended attributes
954 if (ssize > sizeof (JFS_IP(ip)->i_inline))
955 JFS_IP(ip)->mode2 &= ~INLINEEA;
957 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
961 * write source path name in a single extent
964 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
966 ip->i_op = &page_symlink_inode_operations;
967 ip->i_mapping->a_ops = &jfs_aops;
970 * even though the data of symlink object (source
971 * path name) is treated as non-journaled user data,
972 * it is read/written thru buffer cache for performance.
975 bmask = JFS_SBI(sb)->bsize - 1;
976 xsize = (ssize + bmask) & ~bmask;
978 xlen = xsize >> JFS_SBI(sb)->l2bsize;
979 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
984 ip->i_size = ssize - 1;
986 /* This is kind of silly since PATH_MAX == 4K */
987 int copy_size = min(ssize, PSIZE);
989 mp = get_metapage(ip, xaddr, PSIZE, 1);
992 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
997 memcpy(mp->data, name, copy_size);
1001 xaddr += JFS_SBI(sb)->nbperpage;
1006 * create entry for symbolic link in parent directory
1008 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1011 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1015 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1017 /* discard new inode */
1021 insert_inode_hash(ip);
1022 mark_inode_dirty(ip);
1024 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
1025 mark_inode_dirty(dip);
1027 * commit update of parent directory and link object
1032 rc = txCommit(tid, 2, &iplist[0], 0);
1036 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1037 mutex_unlock(&JFS_IP(dip)->commit_mutex);
1043 d_instantiate(dentry, ip);
1046 free_UCSname(&dname);
1049 jfs_info("jfs_symlink: rc:%d", rc);
1057 * FUNCTION: rename a file or directory
1059 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1060 struct inode *new_dir, struct dentry *new_dentry)
1062 struct btstack btstack;
1064 struct component_name new_dname;
1065 struct inode *new_ip;
1066 struct component_name old_dname;
1067 struct inode *old_ip;
1071 struct dt_lock *dtlck;
1074 struct inode *iplist[4];
1075 struct tblock *tblk;
1080 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1081 new_dentry->d_name.name);
1083 old_ip = old_dentry->d_inode;
1084 new_ip = new_dentry->d_inode;
1086 if ((rc = get_UCSname(&old_dname, old_dentry)))
1089 if ((rc = get_UCSname(&new_dname, new_dentry)))
1093 * Make sure source inode number is what we think it is
1095 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1096 if (rc || (ino != old_ip->i_ino)) {
1102 * Make sure dest inode number (if any) is what we think it is
1104 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1106 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1110 } else if (rc != -ENOENT)
1113 /* no entry exists, but one was expected */
1118 if (S_ISDIR(old_ip->i_mode)) {
1120 if (!dtEmpty(new_ip)) {
1124 } else if ((new_dir != old_dir) &&
1125 (new_dir->i_nlink == JFS_LINK_MAX)) {
1129 } else if (new_ip) {
1130 IWRITE_LOCK(new_ip);
1131 /* Init inode for quota operations. */
1136 * The real work starts here
1138 tid = txBegin(new_dir->i_sb, 0);
1140 mutex_lock(&JFS_IP(new_dir)->commit_mutex);
1141 mutex_lock(&JFS_IP(old_ip)->commit_mutex);
1142 if (old_dir != new_dir)
1143 mutex_lock(&JFS_IP(old_dir)->commit_mutex);
1146 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1148 * Change existing directory entry to new inode number
1150 ino = new_ip->i_ino;
1151 rc = dtModify(tid, new_dir, &new_dname, &ino,
1152 old_ip->i_ino, JFS_RENAME);
1156 if (S_ISDIR(new_ip->i_mode)) {
1158 if (new_ip->i_nlink) {
1159 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1160 if (old_dir != new_dir)
1161 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1162 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1163 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1164 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1165 IWRITE_UNLOCK(new_ip);
1166 jfs_error(new_ip->i_sb,
1167 "jfs_rename: new_ip->i_nlink != 0");
1170 tblk = tid_to_tblock(tid);
1171 tblk->xflag |= COMMIT_DELETE;
1172 tblk->u.ip = new_ip;
1173 } else if (new_ip->i_nlink == 0) {
1174 assert(!test_cflag(COMMIT_Nolink, new_ip));
1175 /* free block resources */
1176 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1177 txAbort(tid, 1); /* Marks FS Dirty */
1181 tblk = tid_to_tblock(tid);
1182 tblk->xflag |= COMMIT_DELETE;
1183 tblk->u.ip = new_ip;
1185 new_ip->i_ctime = CURRENT_TIME;
1186 mark_inode_dirty(new_ip);
1190 * Add new directory entry
1192 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1195 jfs_err("jfs_rename didn't expect dtSearch to fail "
1200 ino = old_ip->i_ino;
1201 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1204 jfs_err("jfs_rename: dtInsert returned -EIO");
1207 if (S_ISDIR(old_ip->i_mode))
1211 * Remove old directory entry
1214 ino = old_ip->i_ino;
1215 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1217 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1219 txAbort(tid, 1); /* Marks Filesystem dirty */
1222 if (S_ISDIR(old_ip->i_mode)) {
1223 drop_nlink(old_dir);
1224 if (old_dir != new_dir) {
1226 * Change inode number of parent for moved directory
1229 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1230 cpu_to_le32(new_dir->i_ino);
1232 /* Linelock header of dtree */
1233 tlck = txLock(tid, old_ip,
1234 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1235 tlckDTREE | tlckBTROOT | tlckRELINK);
1236 dtlck = (struct dt_lock *) & tlck->lock;
1237 ASSERT(dtlck->index == 0);
1238 lv = & dtlck->lv[0];
1246 * Update ctime on changed/moved inodes & mark dirty
1248 old_ip->i_ctime = CURRENT_TIME;
1249 mark_inode_dirty(old_ip);
1251 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1252 mark_inode_dirty(new_dir);
1254 /* Build list of inodes modified by this transaction */
1256 iplist[ipcount++] = old_ip;
1258 iplist[ipcount++] = new_ip;
1259 iplist[ipcount++] = old_dir;
1261 if (old_dir != new_dir) {
1262 iplist[ipcount++] = new_dir;
1263 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1264 mark_inode_dirty(old_dir);
1268 * Incomplete truncate of file data can
1269 * result in timing problems unless we synchronously commit the
1273 commit_flag = COMMIT_SYNC;
1277 rc = txCommit(tid, ipcount, iplist, commit_flag);
1282 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1283 if (old_dir != new_dir)
1284 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1285 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1286 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1288 while (new_size && (rc == 0)) {
1289 tid = txBegin(new_ip->i_sb, 0);
1290 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1291 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1296 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1298 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1300 if (new_ip && (new_ip->i_nlink == 0))
1301 set_cflag(COMMIT_Nolink, new_ip);
1303 free_UCSname(&new_dname);
1305 free_UCSname(&old_dname);
1307 if (new_ip && !S_ISDIR(new_ip->i_mode))
1308 IWRITE_UNLOCK(new_ip);
1310 * Truncating the directory index table is not guaranteed. It
1311 * may need to be done iteratively
1313 if (test_cflag(COMMIT_Stale, old_dir)) {
1314 if (old_dir->i_size > 1)
1315 jfs_truncate_nolock(old_dir, 0);
1317 clear_cflag(COMMIT_Stale, old_dir);
1320 jfs_info("jfs_rename: returning %d", rc);
1328 * FUNCTION: Create a special file (device)
1330 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1331 int mode, dev_t rdev)
1333 struct jfs_inode_info *jfs_ip;
1334 struct btstack btstack;
1335 struct component_name dname;
1338 struct inode *iplist[2];
1341 struct tblock *tblk;
1343 if (!new_valid_dev(rdev))
1346 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1348 if ((rc = get_UCSname(&dname, dentry)))
1351 ip = ialloc(dir, mode);
1356 jfs_ip = JFS_IP(ip);
1358 tid = txBegin(dir->i_sb, 0);
1360 mutex_lock(&JFS_IP(dir)->commit_mutex);
1361 mutex_lock(&JFS_IP(ip)->commit_mutex);
1363 rc = jfs_init_acl(tid, ip, dir);
1367 rc = jfs_init_security(tid, ip, dir);
1373 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1378 tblk = tid_to_tblock(tid);
1379 tblk->xflag |= COMMIT_CREATE;
1380 tblk->ino = ip->i_ino;
1381 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1384 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1389 ip->i_op = &jfs_file_inode_operations;
1390 jfs_ip->dev = new_encode_dev(rdev);
1391 init_special_inode(ip, ip->i_mode, rdev);
1393 insert_inode_hash(ip);
1394 mark_inode_dirty(ip);
1396 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1398 mark_inode_dirty(dir);
1402 rc = txCommit(tid, 2, iplist, 0);
1406 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1407 mutex_unlock(&JFS_IP(dir)->commit_mutex);
1413 d_instantiate(dentry, ip);
1416 free_UCSname(&dname);
1419 jfs_info("jfs_mknod: returning %d", rc);
1423 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1425 struct btstack btstack;
1428 struct component_name key;
1429 const char *name = dentry->d_name.name;
1430 int len = dentry->d_name.len;
1433 jfs_info("jfs_lookup: name = %s", name);
1435 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1436 dentry->d_op = &jfs_ci_dentry_operations;
1438 if ((name[0] == '.') && (len == 1))
1440 else if (strcmp(name, "..") == 0)
1443 if ((rc = get_UCSname(&key, dentry)))
1445 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1447 if (rc == -ENOENT) {
1448 d_add(dentry, NULL);
1451 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1456 ip = iget(dip->i_sb, inum);
1457 if (ip == NULL || is_bad_inode(ip)) {
1458 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1461 return ERR_PTR(-EACCES);
1464 dentry = d_splice_alias(ip, dentry);
1466 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1467 dentry->d_op = &jfs_ci_dentry_operations;
1472 struct dentry *jfs_get_parent(struct dentry *dentry)
1474 struct super_block *sb = dentry->d_inode->i_sb;
1475 struct dentry *parent = ERR_PTR(-ENOENT);
1476 struct inode *inode;
1477 unsigned long parent_ino;
1480 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1481 inode = iget(sb, parent_ino);
1483 if (is_bad_inode(inode)) {
1485 parent = ERR_PTR(-EACCES);
1487 parent = d_alloc_anon(inode);
1489 parent = ERR_PTR(-ENOMEM);
1498 struct inode_operations jfs_dir_inode_operations = {
1499 .create = jfs_create,
1500 .lookup = jfs_lookup,
1502 .unlink = jfs_unlink,
1503 .symlink = jfs_symlink,
1507 .rename = jfs_rename,
1508 .setxattr = jfs_setxattr,
1509 .getxattr = jfs_getxattr,
1510 .listxattr = jfs_listxattr,
1511 .removexattr = jfs_removexattr,
1512 #ifdef CONFIG_JFS_POSIX_ACL
1513 .setattr = jfs_setattr,
1514 .permission = jfs_permission,
1518 const struct file_operations jfs_dir_operations = {
1519 .read = generic_read_dir,
1520 .readdir = jfs_readdir,
1525 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1530 hash = init_name_hash();
1531 for (i=0; i < this->len; i++)
1532 hash = partial_name_hash(tolower(this->name[i]), hash);
1533 this->hash = end_name_hash(hash);
1538 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1542 if (a->len != b->len)
1544 for (i=0; i < a->len; i++) {
1545 if (tolower(a->name[i]) != tolower(b->name[i]))
1551 * We want creates to preserve case. A negative dentry, a, that
1552 * has a different case than b may cause a new entry to be created
1553 * with the wrong case. Since we can't tell if a comes from a negative
1554 * dentry, we blindly replace it with b. This should be harmless if
1555 * a is not a negative dentry.
1557 memcpy((unsigned char *)a->name, b->name, a->len);
1562 struct dentry_operations jfs_ci_dentry_operations =
1564 .d_hash = jfs_ci_hash,
1565 .d_compare = jfs_ci_compare,