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/vserver/xid.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(&JFS_IP(dip)->commit_mutex);
109 mutex_lock(&JFS_IP(ip)->commit_mutex);
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 insert_inode_hash(ip);
159 mark_inode_dirty(ip);
161 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
163 mark_inode_dirty(dip);
165 rc = txCommit(tid, 2, &iplist[0], 0);
169 mutex_unlock(&JFS_IP(ip)->commit_mutex);
170 mutex_unlock(&JFS_IP(dip)->commit_mutex);
176 d_instantiate(dentry, ip);
179 free_UCSname(&dname);
183 jfs_info("jfs_create: rc:%d", rc);
189 * NAME: jfs_mkdir(dip, dentry, mode)
191 * FUNCTION: create a child directory in the parent directory <dip>
192 * with name = <from dentry> and mode = <mode>
194 * PARAMETER: dip - parent directory vnode
195 * dentry - dentry of child directory
196 * mode - create mode (rwxrwxrwx).
198 * RETURN: Errors from subroutines
201 * EACCESS: user needs search+write permission on the parent directory
203 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
206 tid_t tid; /* transaction id */
207 struct inode *ip = NULL; /* child directory inode */
209 struct component_name dname; /* child directory name */
210 struct btstack btstack;
211 struct inode *iplist[2];
214 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
216 /* link count overflow on parent directory ? */
217 if (dip->i_nlink == JFS_LINK_MAX) {
223 * search parent directory for entry/freespace
224 * (dtSearch() returns parent directory page pinned)
226 if ((rc = get_UCSname(&dname, dentry)))
230 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
231 * block there while holding dtree page, so we allocate the inode &
232 * begin the transaction before we search the directory.
234 ip = ialloc(dip, S_IFDIR | mode);
240 tid = txBegin(dip->i_sb, 0);
242 mutex_lock(&JFS_IP(dip)->commit_mutex);
243 mutex_lock(&JFS_IP(ip)->commit_mutex);
245 rc = jfs_init_acl(tid, ip, dip);
249 rc = jfs_init_security(tid, ip, dip);
255 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
256 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
261 tblk = tid_to_tblock(tid);
262 tblk->xflag |= COMMIT_CREATE;
263 tblk->ino = ip->i_ino;
264 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
270 * initialize the child directory in-line in inode
272 dtInitRoot(tid, ip, dip->i_ino);
275 * create entry in parent directory for child directory
276 * (dtInsert() releases parent directory page)
279 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
281 jfs_err("jfs_mkdir: dtInsert returned -EIO");
282 txAbort(tid, 1); /* Marks Filesystem dirty */
284 txAbort(tid, 0); /* Filesystem full */
288 ip->i_nlink = 2; /* for '.' */
289 ip->i_op = &jfs_dir_inode_operations;
290 ip->i_fop = &jfs_dir_operations;
292 insert_inode_hash(ip);
293 mark_inode_dirty(ip);
295 /* update parent directory inode */
296 dip->i_nlink++; /* for '..' from child directory */
297 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
298 mark_inode_dirty(dip);
300 rc = txCommit(tid, 2, &iplist[0], 0);
304 mutex_unlock(&JFS_IP(ip)->commit_mutex);
305 mutex_unlock(&JFS_IP(dip)->commit_mutex);
311 d_instantiate(dentry, ip);
314 free_UCSname(&dname);
319 jfs_info("jfs_mkdir: rc:%d", rc);
324 * NAME: jfs_rmdir(dip, dentry)
326 * FUNCTION: remove a link to child directory
328 * PARAMETER: dip - parent inode
329 * dentry - child directory dentry
331 * RETURN: -EINVAL - if name is . or ..
332 * -EINVAL - if . or .. exist but are invalid.
333 * errors from subroutines
336 * if other threads have the directory open when the last link
337 * is removed, the "." and ".." entries, if present, are removed before
338 * rmdir() returns and no new entries may be created in the directory,
339 * but the directory is not removed until the last reference to
340 * the directory is released (cf.unlink() of regular file).
342 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
345 tid_t tid; /* transaction id */
346 struct inode *ip = dentry->d_inode;
348 struct component_name dname;
349 struct inode *iplist[2];
352 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
354 /* Init inode for quota operations. */
357 /* directory must be empty to be removed */
363 if ((rc = get_UCSname(&dname, dentry))) {
367 tid = txBegin(dip->i_sb, 0);
369 mutex_lock(&JFS_IP(dip)->commit_mutex);
370 mutex_lock(&JFS_IP(ip)->commit_mutex);
375 tblk = tid_to_tblock(tid);
376 tblk->xflag |= COMMIT_DELETE;
380 * delete the entry of target directory from parent directory
383 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
384 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
388 mutex_unlock(&JFS_IP(ip)->commit_mutex);
389 mutex_unlock(&JFS_IP(dip)->commit_mutex);
394 /* update parent directory's link count corresponding
395 * to ".." entry of the target directory deleted
398 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
399 mark_inode_dirty(dip);
402 * OS/2 could have created EA and/or ACL
404 /* free EA from both persistent and working map */
405 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
407 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
409 JFS_IP(ip)->ea.flag = 0;
411 /* free ACL from both persistent and working map */
412 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
414 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
416 JFS_IP(ip)->acl.flag = 0;
418 /* mark the target directory as deleted */
420 mark_inode_dirty(ip);
422 rc = txCommit(tid, 2, &iplist[0], 0);
426 mutex_unlock(&JFS_IP(ip)->commit_mutex);
427 mutex_unlock(&JFS_IP(dip)->commit_mutex);
430 * Truncating the directory index table is not guaranteed. It
431 * may need to be done iteratively
433 if (test_cflag(COMMIT_Stale, dip)) {
435 jfs_truncate_nolock(dip, 0);
437 clear_cflag(COMMIT_Stale, dip);
441 free_UCSname(&dname);
444 jfs_info("jfs_rmdir: rc:%d", rc);
449 * NAME: jfs_unlink(dip, dentry)
451 * FUNCTION: remove a link to object <vp> named by <name>
452 * from parent directory <dvp>
454 * PARAMETER: dip - inode of parent directory
455 * dentry - dentry of object to be removed
457 * RETURN: errors from subroutines
460 * temporary file: if one or more processes have the file open
461 * when the last link is removed, the link will be removed before
462 * unlink() returns, but the removal of the file contents will be
463 * postponed until all references to the files are closed.
465 * JFS does NOT support unlink() on directories.
468 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
471 tid_t tid; /* transaction id */
472 struct inode *ip = dentry->d_inode;
474 struct component_name dname; /* object name */
475 struct inode *iplist[2];
480 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
482 /* Init inode for quota operations. */
485 if ((rc = get_UCSname(&dname, dentry)))
490 tid = txBegin(dip->i_sb, 0);
492 mutex_lock(&JFS_IP(dip)->commit_mutex);
493 mutex_lock(&JFS_IP(ip)->commit_mutex);
499 * delete the entry of target file from parent directory
502 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
503 jfs_err("jfs_unlink: dtDelete returned %d", rc);
505 txAbort(tid, 1); /* Marks FS Dirty */
507 mutex_unlock(&JFS_IP(ip)->commit_mutex);
508 mutex_unlock(&JFS_IP(dip)->commit_mutex);
515 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
516 mark_inode_dirty(dip);
518 /* update target's inode */
520 mark_inode_dirty(ip);
523 * commit zero link count object
525 if (ip->i_nlink == 0) {
526 assert(!test_cflag(COMMIT_Nolink, ip));
527 /* free block resources */
528 if ((new_size = commitZeroLink(tid, ip)) < 0) {
529 txAbort(tid, 1); /* Marks FS Dirty */
531 mutex_unlock(&JFS_IP(ip)->commit_mutex);
532 mutex_unlock(&JFS_IP(dip)->commit_mutex);
537 tblk = tid_to_tblock(tid);
538 tblk->xflag |= COMMIT_DELETE;
543 * Incomplete truncate of file data can
544 * result in timing problems unless we synchronously commit the
548 commit_flag = COMMIT_SYNC;
553 * If xtTruncate was incomplete, commit synchronously to avoid
554 * timing complications
556 rc = txCommit(tid, 2, &iplist[0], commit_flag);
560 mutex_unlock(&JFS_IP(ip)->commit_mutex);
561 mutex_unlock(&JFS_IP(dip)->commit_mutex);
563 while (new_size && (rc == 0)) {
564 tid = txBegin(dip->i_sb, 0);
565 mutex_lock(&JFS_IP(ip)->commit_mutex);
566 new_size = xtTruncate_pmap(tid, ip, new_size);
568 txAbort(tid, 1); /* Marks FS Dirty */
571 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
573 mutex_unlock(&JFS_IP(ip)->commit_mutex);
576 if (ip->i_nlink == 0)
577 set_cflag(COMMIT_Nolink, ip);
582 * Truncating the directory index table is not guaranteed. It
583 * may need to be done iteratively
585 if (test_cflag(COMMIT_Stale, dip)) {
587 jfs_truncate_nolock(dip, 0);
589 clear_cflag(COMMIT_Stale, dip);
593 free_UCSname(&dname);
595 jfs_info("jfs_unlink: rc:%d", rc);
600 * NAME: commitZeroLink()
602 * FUNCTION: for non-directory, called by jfs_remove(),
603 * truncate a regular file, directory or symbolic
604 * link to zero length. return 0 if type is not
607 * if the file is currently associated with a VM segment
608 * only permanent disk and inode map resources are freed,
609 * and neither the inode nor indirect blocks are modified
610 * so that the resources can be later freed in the work
612 * if there is no VM segment on entry, the resources are
613 * freed in both work and permanent map.
614 * (? for temporary file - memory object is cached even
615 * after no reference:
616 * reference count > 0 - )
618 * PARAMETERS: cd - pointer to commit data structure.
619 * current inode is the one to truncate.
621 * RETURN: Errors from subroutines
623 static s64 commitZeroLink(tid_t tid, struct inode *ip)
628 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
630 filetype = ip->i_mode & S_IFMT;
635 /* fast symbolic link */
636 if (ip->i_size < IDATASIZE) {
642 assert(filetype != S_IFDIR);
646 set_cflag(COMMIT_Freewmap, ip);
648 /* mark transaction of block map update type */
649 tblk = tid_to_tblock(tid);
650 tblk->xflag |= COMMIT_PMAP;
655 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
656 /* acquire maplock on EA to be freed from block map */
657 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
662 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
663 /* acquire maplock on EA to be freed from block map */
664 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
667 * free xtree/data (truncate to zero length):
668 * free xtree/data pages from cache if COMMIT_PWMAP,
669 * free xtree/data blocks from persistent block map, and
670 * free xtree/data blocks from working block map if COMMIT_PWMAP;
673 return xtTruncate_pmap(tid, ip, 0);
680 * NAME: jfs_free_zero_link()
682 * FUNCTION: for non-directory, called by iClose(),
683 * free resources of a file from cache and WORKING map
684 * for a file previously committed with zero link count
685 * while associated with a pager object,
687 * PARAMETER: ip - pointer to inode of file.
689 void jfs_free_zero_link(struct inode *ip)
693 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
695 /* return if not reg or symbolic link or if size is
698 type = ip->i_mode & S_IFMT;
704 /* if its contained in inode nothing to do */
705 if (ip->i_size < IDATASIZE)
715 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
716 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
717 int xlen = lengthDXD(&JFS_IP(ip)->ea);
718 struct maplock maplock; /* maplock for COMMIT_WMAP */
719 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
721 /* free EA pages from cache */
722 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
724 /* free EA extent from working block map */
726 pxdlock = (struct pxd_lock *) & maplock;
727 pxdlock->flag = mlckFREEPXD;
728 PXDaddress(&pxdlock->pxd, xaddr);
729 PXDlength(&pxdlock->pxd, xlen);
730 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
736 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
737 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
738 int xlen = lengthDXD(&JFS_IP(ip)->acl);
739 struct maplock maplock; /* maplock for COMMIT_WMAP */
740 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
742 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
744 /* free ACL extent from working block map */
746 pxdlock = (struct pxd_lock *) & maplock;
747 pxdlock->flag = mlckFREEPXD;
748 PXDaddress(&pxdlock->pxd, xaddr);
749 PXDlength(&pxdlock->pxd, xlen);
750 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
754 * free xtree/data (truncate to zero length):
755 * free xtree/data pages from cache, and
756 * free xtree/data blocks from working block map;
759 xtTruncate(0, ip, 0, COMMIT_WMAP);
763 * NAME: jfs_link(vp, dvp, name, crp)
765 * FUNCTION: create a link to <vp> by the name = <name>
766 * in the parent directory <dvp>
768 * PARAMETER: vp - target object
769 * dvp - parent directory of new link
770 * name - name of new link to target object
773 * RETURN: Errors from subroutines
776 * JFS does NOT support link() on directories (to prevent circular
777 * path in the directory hierarchy);
778 * EPERM: the target object is a directory, and either the caller
779 * does not have appropriate privileges or the implementation prohibits
780 * using link() on directories [XPG4.2].
782 * JFS does NOT support links between file systems:
783 * EXDEV: target object and new link are on different file systems and
784 * implementation does not support links between file systems [XPG4.2].
786 static int jfs_link(struct dentry *old_dentry,
787 struct inode *dir, struct dentry *dentry)
791 struct inode *ip = old_dentry->d_inode;
793 struct component_name dname;
794 struct btstack btstack;
795 struct inode *iplist[2];
797 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
798 dentry->d_name.name);
800 if (ip->i_nlink == JFS_LINK_MAX)
803 if (ip->i_nlink == 0)
806 tid = txBegin(ip->i_sb, 0);
808 mutex_lock(&JFS_IP(dir)->commit_mutex);
809 mutex_lock(&JFS_IP(ip)->commit_mutex);
812 * scan parent directory for entry/freespace
814 if ((rc = get_UCSname(&dname, dentry)))
817 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
821 * create entry for new link in parent directory
824 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
827 /* update object inode */
828 ip->i_nlink++; /* for new link */
829 ip->i_ctime = CURRENT_TIME;
830 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
831 mark_inode_dirty(dir);
832 atomic_inc(&ip->i_count);
836 rc = txCommit(tid, 2, &iplist[0], 0);
842 d_instantiate(dentry, ip);
845 free_UCSname(&dname);
850 mutex_unlock(&JFS_IP(ip)->commit_mutex);
851 mutex_unlock(&JFS_IP(dir)->commit_mutex);
853 jfs_info("jfs_link: rc:%d", rc);
858 * NAME: jfs_symlink(dip, dentry, name)
860 * FUNCTION: creates a symbolic link to <symlink> by name <name>
863 * PARAMETER: dip - parent directory vnode
864 * dentry - dentry of symbolic link
865 * name - the path name of the existing object
866 * that will be the source of the link
868 * RETURN: errors from subroutines
871 * ENAMETOOLONG: pathname resolution of a symbolic link produced
872 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
875 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
881 struct component_name dname;
882 int ssize; /* source pathname size */
883 struct btstack btstack;
884 struct inode *ip = dentry->d_inode;
885 unchar *i_fastsymlink;
887 int bmask = 0, xsize;
888 s64 extent = 0, xaddr;
890 struct super_block *sb;
893 struct inode *iplist[2];
895 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
897 ssize = strlen(name) + 1;
900 * search parent directory for entry/freespace
901 * (dtSearch() returns parent directory page pinned)
904 if ((rc = get_UCSname(&dname, dentry)))
908 * allocate on-disk/in-memory inode for symbolic link:
909 * (iAlloc() returns new, locked inode)
911 ip = ialloc(dip, S_IFLNK | 0777);
917 tid = txBegin(dip->i_sb, 0);
919 mutex_lock(&JFS_IP(dip)->commit_mutex);
920 mutex_lock(&JFS_IP(ip)->commit_mutex);
922 rc = jfs_init_security(tid, ip, dip);
926 tblk = tid_to_tblock(tid);
927 tblk->xflag |= COMMIT_CREATE;
928 tblk->ino = ip->i_ino;
929 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
931 /* fix symlink access permission
932 * (dir_create() ANDs in the u.u_cmask,
933 * but symlinks really need to be 777 access)
938 * write symbolic link target path name
943 * write source path name inline in on-disk inode (fast symbolic link)
946 if (ssize <= IDATASIZE) {
947 ip->i_op = &jfs_symlink_inode_operations;
949 i_fastsymlink = JFS_IP(ip)->i_inline;
950 memcpy(i_fastsymlink, name, ssize);
951 ip->i_size = ssize - 1;
954 * if symlink is > 128 bytes, we don't have the space to
955 * store inline extended attributes
957 if (ssize > sizeof (JFS_IP(ip)->i_inline))
958 JFS_IP(ip)->mode2 &= ~INLINEEA;
960 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
964 * write source path name in a single extent
967 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
969 ip->i_op = &page_symlink_inode_operations;
970 ip->i_mapping->a_ops = &jfs_aops;
973 * even though the data of symlink object (source
974 * path name) is treated as non-journaled user data,
975 * it is read/written thru buffer cache for performance.
978 bmask = JFS_SBI(sb)->bsize - 1;
979 xsize = (ssize + bmask) & ~bmask;
981 xlen = xsize >> JFS_SBI(sb)->l2bsize;
982 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
988 ip->i_size = ssize - 1;
990 /* This is kind of silly since PATH_MAX == 4K */
991 int copy_size = min(ssize, PSIZE);
993 mp = get_metapage(ip, xaddr, PSIZE, 1);
996 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1001 memcpy(mp->data, name, copy_size);
1005 xaddr += JFS_SBI(sb)->nbperpage;
1010 * create entry for symbolic link in parent directory
1012 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1015 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1019 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1021 /* discard new inode */
1025 insert_inode_hash(ip);
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);
1047 d_instantiate(dentry, ip);
1050 free_UCSname(&dname);
1053 jfs_info("jfs_symlink: rc:%d", rc);
1061 * FUNCTION: rename a file or directory
1063 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1064 struct inode *new_dir, struct dentry *new_dentry)
1066 struct btstack btstack;
1068 struct component_name new_dname;
1069 struct inode *new_ip;
1070 struct component_name old_dname;
1071 struct inode *old_ip;
1075 struct dt_lock *dtlck;
1078 struct inode *iplist[4];
1079 struct tblock *tblk;
1084 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1085 new_dentry->d_name.name);
1087 old_ip = old_dentry->d_inode;
1088 new_ip = new_dentry->d_inode;
1090 if ((rc = get_UCSname(&old_dname, old_dentry)))
1093 if ((rc = get_UCSname(&new_dname, new_dentry)))
1097 * Make sure source inode number is what we think it is
1099 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1100 if (rc || (ino != old_ip->i_ino)) {
1106 * Make sure dest inode number (if any) is what we think it is
1108 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1110 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1114 } else if (rc != -ENOENT)
1117 /* no entry exists, but one was expected */
1122 if (S_ISDIR(old_ip->i_mode)) {
1124 if (!dtEmpty(new_ip)) {
1128 } else if ((new_dir != old_dir) &&
1129 (new_dir->i_nlink == JFS_LINK_MAX)) {
1133 } else if (new_ip) {
1134 IWRITE_LOCK(new_ip);
1135 /* Init inode for quota operations. */
1140 * The real work starts here
1142 tid = txBegin(new_dir->i_sb, 0);
1144 mutex_lock(&JFS_IP(new_dir)->commit_mutex);
1145 mutex_lock(&JFS_IP(old_ip)->commit_mutex);
1146 if (old_dir != new_dir)
1147 mutex_lock(&JFS_IP(old_dir)->commit_mutex);
1150 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1152 * Change existing directory entry to new inode number
1154 ino = new_ip->i_ino;
1155 rc = dtModify(tid, new_dir, &new_dname, &ino,
1156 old_ip->i_ino, JFS_RENAME);
1160 if (S_ISDIR(new_ip->i_mode)) {
1162 if (new_ip->i_nlink) {
1163 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1164 if (old_dir != new_dir)
1165 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1166 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1167 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1168 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1169 IWRITE_UNLOCK(new_ip);
1170 jfs_error(new_ip->i_sb,
1171 "jfs_rename: new_ip->i_nlink != 0");
1174 tblk = tid_to_tblock(tid);
1175 tblk->xflag |= COMMIT_DELETE;
1176 tblk->u.ip = new_ip;
1177 } else if (new_ip->i_nlink == 0) {
1178 assert(!test_cflag(COMMIT_Nolink, new_ip));
1179 /* free block resources */
1180 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1181 txAbort(tid, 1); /* Marks FS Dirty */
1185 tblk = tid_to_tblock(tid);
1186 tblk->xflag |= COMMIT_DELETE;
1187 tblk->u.ip = new_ip;
1189 new_ip->i_ctime = CURRENT_TIME;
1190 mark_inode_dirty(new_ip);
1194 * Add new directory entry
1196 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1199 jfs_err("jfs_rename didn't expect dtSearch to fail "
1204 ino = old_ip->i_ino;
1205 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1208 jfs_err("jfs_rename: dtInsert returned -EIO");
1211 if (S_ISDIR(old_ip->i_mode))
1215 * Remove old directory entry
1218 ino = old_ip->i_ino;
1219 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1221 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1223 txAbort(tid, 1); /* Marks Filesystem dirty */
1226 if (S_ISDIR(old_ip->i_mode)) {
1228 if (old_dir != new_dir) {
1230 * Change inode number of parent for moved directory
1233 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1234 cpu_to_le32(new_dir->i_ino);
1236 /* Linelock header of dtree */
1237 tlck = txLock(tid, old_ip,
1238 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1239 tlckDTREE | tlckBTROOT | tlckRELINK);
1240 dtlck = (struct dt_lock *) & tlck->lock;
1241 ASSERT(dtlck->index == 0);
1242 lv = & dtlck->lv[0];
1250 * Update ctime on changed/moved inodes & mark dirty
1252 old_ip->i_ctime = CURRENT_TIME;
1253 mark_inode_dirty(old_ip);
1255 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1256 mark_inode_dirty(new_dir);
1258 /* Build list of inodes modified by this transaction */
1260 iplist[ipcount++] = old_ip;
1262 iplist[ipcount++] = new_ip;
1263 iplist[ipcount++] = old_dir;
1265 if (old_dir != new_dir) {
1266 iplist[ipcount++] = new_dir;
1267 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1268 mark_inode_dirty(old_dir);
1272 * Incomplete truncate of file data can
1273 * result in timing problems unless we synchronously commit the
1277 commit_flag = COMMIT_SYNC;
1281 rc = txCommit(tid, ipcount, iplist, commit_flag);
1286 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1287 if (old_dir != new_dir)
1288 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1289 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1290 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1292 while (new_size && (rc == 0)) {
1293 tid = txBegin(new_ip->i_sb, 0);
1294 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1295 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1300 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1302 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1304 if (new_ip && (new_ip->i_nlink == 0))
1305 set_cflag(COMMIT_Nolink, new_ip);
1307 free_UCSname(&new_dname);
1309 free_UCSname(&old_dname);
1311 if (new_ip && !S_ISDIR(new_ip->i_mode))
1312 IWRITE_UNLOCK(new_ip);
1314 * Truncating the directory index table is not guaranteed. It
1315 * may need to be done iteratively
1317 if (test_cflag(COMMIT_Stale, old_dir)) {
1318 if (old_dir->i_size > 1)
1319 jfs_truncate_nolock(old_dir, 0);
1321 clear_cflag(COMMIT_Stale, old_dir);
1324 jfs_info("jfs_rename: returning %d", rc);
1332 * FUNCTION: Create a special file (device)
1334 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1335 int mode, dev_t rdev)
1337 struct jfs_inode_info *jfs_ip;
1338 struct btstack btstack;
1339 struct component_name dname;
1342 struct inode *iplist[2];
1345 struct tblock *tblk;
1347 if (!new_valid_dev(rdev))
1350 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1352 if ((rc = get_UCSname(&dname, dentry)))
1355 ip = ialloc(dir, mode);
1360 jfs_ip = JFS_IP(ip);
1362 tid = txBegin(dir->i_sb, 0);
1364 mutex_lock(&JFS_IP(dir)->commit_mutex);
1365 mutex_lock(&JFS_IP(ip)->commit_mutex);
1367 rc = jfs_init_acl(tid, ip, dir);
1371 rc = jfs_init_security(tid, ip, dir);
1377 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1382 tblk = tid_to_tblock(tid);
1383 tblk->xflag |= COMMIT_CREATE;
1384 tblk->ino = ip->i_ino;
1385 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1388 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1393 ip->i_op = &jfs_file_inode_operations;
1394 jfs_ip->dev = new_encode_dev(rdev);
1395 init_special_inode(ip, ip->i_mode, rdev);
1397 insert_inode_hash(ip);
1398 mark_inode_dirty(ip);
1400 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1402 mark_inode_dirty(dir);
1406 rc = txCommit(tid, 2, iplist, 0);
1410 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1411 mutex_unlock(&JFS_IP(dir)->commit_mutex);
1417 d_instantiate(dentry, ip);
1420 free_UCSname(&dname);
1423 jfs_info("jfs_mknod: returning %d", rc);
1427 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1429 struct btstack btstack;
1432 struct component_name key;
1433 const char *name = dentry->d_name.name;
1434 int len = dentry->d_name.len;
1437 jfs_info("jfs_lookup: name = %s", name);
1439 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1440 dentry->d_op = &jfs_ci_dentry_operations;
1442 if ((name[0] == '.') && (len == 1))
1444 else if (strcmp(name, "..") == 0)
1447 if ((rc = get_UCSname(&key, dentry)))
1449 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1451 if (rc == -ENOENT) {
1452 d_add(dentry, NULL);
1455 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1460 ip = iget(dip->i_sb, inum);
1461 if (ip == NULL || is_bad_inode(ip)) {
1462 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1465 return ERR_PTR(-EACCES);
1468 vx_propagate_xid(nd, ip);
1469 dentry = d_splice_alias(ip, dentry);
1471 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1472 dentry->d_op = &jfs_ci_dentry_operations;
1477 struct dentry *jfs_get_parent(struct dentry *dentry)
1479 struct super_block *sb = dentry->d_inode->i_sb;
1480 struct dentry *parent = ERR_PTR(-ENOENT);
1481 struct inode *inode;
1482 unsigned long parent_ino;
1485 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1486 inode = iget(sb, parent_ino);
1488 if (is_bad_inode(inode)) {
1490 parent = ERR_PTR(-EACCES);
1492 parent = d_alloc_anon(inode);
1494 parent = ERR_PTR(-ENOMEM);
1503 struct inode_operations jfs_dir_inode_operations = {
1504 .create = jfs_create,
1505 .lookup = jfs_lookup,
1507 .unlink = jfs_unlink,
1508 .symlink = jfs_symlink,
1512 .rename = jfs_rename,
1513 .setxattr = jfs_setxattr,
1514 .getxattr = jfs_getxattr,
1515 .listxattr = jfs_listxattr,
1516 .removexattr = jfs_removexattr,
1517 #ifdef CONFIG_JFS_POSIX_ACL
1518 .setattr = jfs_setattr,
1519 .permission = jfs_permission,
1521 .sync_flags = jfs_sync_flags,
1524 const struct file_operations jfs_dir_operations = {
1525 .read = generic_read_dir,
1526 .readdir = jfs_readdir,
1531 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1536 hash = init_name_hash();
1537 for (i=0; i < this->len; i++)
1538 hash = partial_name_hash(tolower(this->name[i]), hash);
1539 this->hash = end_name_hash(hash);
1544 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1548 if (a->len != b->len)
1550 for (i=0; i < a->len; i++) {
1551 if (tolower(a->name[i]) != tolower(b->name[i]))
1557 * We want creates to preserve case. A negative dentry, a, that
1558 * has a different case than b may cause a new entry to be created
1559 * with the wrong case. Since we can't tell if a comes from a negative
1560 * dentry, we blindly replace it with b. This should be harmless if
1561 * a is not a negative dentry.
1563 memcpy((unsigned char *)a->name, b->name, a->len);
1568 struct dentry_operations jfs_ci_dentry_operations =
1570 .d_hash = jfs_ci_hash,
1571 .d_compare = jfs_ci_compare,