initial commit with v2.6.9
[linux-2.6.9-moxart.git] / fs / jfs / namei.c
blobe089615adb6b12aab37de82cb567d67a9cdd12dc
1 /*
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.
9 *
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
20 #include <linux/fs.h>
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"
27 #include "jfs_dmap.h"
28 #include "jfs_unicode.h"
29 #include "jfs_metapage.h"
30 #include "jfs_xattr.h"
31 #include "jfs_acl.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 *);
44 * forward references
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).
61 * nd- nd struct
63 * RETURN: Errors from subroutines
66 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
67 struct nameidata *nd)
69 int rc = 0;
70 tid_t tid; /* transaction id */
71 struct inode *ip = NULL; /* child directory inode */
72 ino_t ino;
73 struct component_name dname; /* child directory name */
74 struct btstack btstack;
75 struct inode *iplist[2];
76 struct tblock *tblk;
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)))
85 goto out1;
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);
93 if (ip == NULL) {
94 rc = -ENOSPC;
95 goto out2;
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);
105 goto out3;
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;
113 iplist[0] = dip;
114 iplist[1] = ip;
117 * initialize the child XAD tree root in-line in inode
119 xtInitRoot(tid, ip);
122 * create entry in parent directory for child directory
123 * (dtInsert() releases parent directory page)
125 ino = ip->i_ino;
126 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
127 if (rc == -EIO) {
128 jfs_err("jfs_create: dtInsert returned -EIO");
129 txAbort(tid, 1); /* Marks Filesystem dirty */
130 } else
131 txAbort(tid, 0); /* Filesystem full */
132 goto out3;
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);
148 out3:
149 txEnd(tid);
150 up(&JFS_IP(dip)->commit_sem);
151 up(&JFS_IP(ip)->commit_sem);
152 if (rc) {
153 ip->i_nlink = 0;
154 iput(ip);
155 } else
156 d_instantiate(dentry, ip);
158 out2:
159 free_UCSname(&dname);
161 #ifdef CONFIG_JFS_POSIX_ACL
162 if (rc == 0)
163 jfs_init_acl(ip, dip);
164 #endif
166 out1:
168 jfs_info("jfs_create: rc:%d", rc);
169 return 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
185 * note:
186 * EACCESS: user needs search+write permission on the parent directory
188 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
190 int rc = 0;
191 tid_t tid; /* transaction id */
192 struct inode *ip = NULL; /* child directory inode */
193 ino_t ino;
194 struct component_name dname; /* child directory name */
195 struct btstack btstack;
196 struct inode *iplist[2];
197 struct tblock *tblk;
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) {
203 rc = -EMLINK;
204 goto out1;
208 * search parent directory for entry/freespace
209 * (dtSearch() returns parent directory page pinned)
211 if ((rc = get_UCSname(&dname, dentry)))
212 goto out1;
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);
220 if (ip == NULL) {
221 rc = -ENOSPC;
222 goto out2;
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);
232 goto out3;
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;
240 iplist[0] = dip;
241 iplist[1] = ip;
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)
252 ino = ip->i_ino;
253 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
254 if (rc == -EIO) {
255 jfs_err("jfs_mkdir: dtInsert returned -EIO");
256 txAbort(tid, 1); /* Marks Filesystem dirty */
257 } else
258 txAbort(tid, 0); /* Filesystem full */
259 goto out3;
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);
278 out3:
279 txEnd(tid);
280 up(&JFS_IP(dip)->commit_sem);
281 up(&JFS_IP(ip)->commit_sem);
282 if (rc) {
283 ip->i_nlink = 0;
284 iput(ip);
285 } else
286 d_instantiate(dentry, ip);
288 out2:
289 free_UCSname(&dname);
291 #ifdef CONFIG_JFS_POSIX_ACL
292 if (rc == 0)
293 jfs_init_acl(ip, dip);
294 #endif
296 out1:
298 jfs_info("jfs_mkdir: rc:%d", rc);
299 return 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
314 * note:
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)
323 int rc;
324 tid_t tid; /* transaction id */
325 struct inode *ip = dentry->d_inode;
326 ino_t ino;
327 struct component_name dname;
328 struct inode *iplist[2];
329 struct tblock *tblk;
331 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
333 /* Init inode for quota operations. */
334 DQUOT_INIT(ip);
336 /* directory must be empty to be removed */
337 if (!dtEmpty(ip)) {
338 rc = -ENOTEMPTY;
339 goto out;
342 if ((rc = get_UCSname(&dname, dentry))) {
343 goto out;
346 tid = txBegin(dip->i_sb, 0);
348 down(&JFS_IP(dip)->commit_sem);
349 down(&JFS_IP(ip)->commit_sem);
351 iplist[0] = dip;
352 iplist[1] = ip;
354 tblk = tid_to_tblock(tid);
355 tblk->xflag |= COMMIT_DELETE;
356 tblk->u.ip = ip;
359 * delete the entry of target directory from parent directory
361 ino = ip->i_ino;
362 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
363 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
364 if (rc == -EIO)
365 txAbort(tid, 1);
366 txEnd(tid);
367 up(&JFS_IP(dip)->commit_sem);
368 up(&JFS_IP(ip)->commit_sem);
370 goto out2;
373 /* update parent directory's link count corresponding
374 * to ".." entry of the target directory deleted
376 dip->i_nlink--;
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) {
385 /* free EA pages */
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) {
392 /* free ACL pages */
393 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
395 JFS_IP(ip)->acl.flag = 0;
397 /* mark the target directory as deleted */
398 ip->i_nlink = 0;
399 mark_inode_dirty(ip);
401 rc = txCommit(tid, 2, &iplist[0], 0);
403 txEnd(tid);
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)) {
413 if (dip->i_size > 1)
414 jfs_truncate_nolock(dip, 0);
416 clear_cflag(COMMIT_Stale, dip);
419 out2:
420 free_UCSname(&dname);
422 out:
423 jfs_info("jfs_rmdir: rc:%d", rc);
424 return 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
438 * note:
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)
449 int rc;
450 tid_t tid; /* transaction id */
451 struct inode *ip = dentry->d_inode;
452 ino_t ino;
453 struct component_name dname; /* object name */
454 struct inode *iplist[2];
455 struct tblock *tblk;
456 s64 new_size = 0;
457 int commit_flag;
459 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
461 /* Init inode for quota operations. */
462 DQUOT_INIT(ip);
464 if ((rc = get_UCSname(&dname, dentry)))
465 goto out;
467 IWRITE_LOCK(ip);
469 tid = txBegin(dip->i_sb, 0);
471 down(&JFS_IP(dip)->commit_sem);
472 down(&JFS_IP(ip)->commit_sem);
474 iplist[0] = dip;
475 iplist[1] = ip;
478 * delete the entry of target file from parent directory
480 ino = ip->i_ino;
481 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
482 jfs_err("jfs_unlink: dtDelete returned %d", rc);
483 if (rc == -EIO)
484 txAbort(tid, 1); /* Marks FS Dirty */
485 txEnd(tid);
486 up(&JFS_IP(dip)->commit_sem);
487 up(&JFS_IP(ip)->commit_sem);
488 IWRITE_UNLOCK(ip);
489 goto out1;
492 ASSERT(ip->i_nlink);
494 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
495 mark_inode_dirty(dip);
497 /* update target's inode */
498 ip->i_nlink--;
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 */
509 txEnd(tid);
510 up(&JFS_IP(dip)->commit_sem);
511 up(&JFS_IP(ip)->commit_sem);
512 IWRITE_UNLOCK(ip);
513 rc = new_size;
514 goto out1;
516 tblk = tid_to_tblock(tid);
517 tblk->xflag |= COMMIT_DELETE;
518 tblk->u.ip = ip;
522 * Incomplete truncate of file data can
523 * result in timing problems unless we synchronously commit the
524 * transaction.
526 if (new_size)
527 commit_flag = COMMIT_SYNC;
528 else
529 commit_flag = 0;
532 * If xtTruncate was incomplete, commit synchronously to avoid
533 * timing complications
535 rc = txCommit(tid, 2, &iplist[0], commit_flag);
537 txEnd(tid);
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);
547 if (new_size < 0) {
548 txAbort(tid, 1); /* Marks FS Dirty */
549 rc = new_size;
550 } else
551 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
552 txEnd(tid);
553 up(&JFS_IP(ip)->commit_sem);
556 if (ip->i_nlink == 0)
557 set_cflag(COMMIT_Nolink, ip);
559 IWRITE_UNLOCK(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)) {
566 if (dip->i_size > 1)
567 jfs_truncate_nolock(dip, 0);
569 clear_cflag(COMMIT_Stale, dip);
572 out1:
573 free_UCSname(&dname);
574 out:
575 jfs_info("jfs_unlink: rc:%d", rc);
576 return 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
585 * one of these.
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
591 * map by ctrunc1.
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)
605 int filetype;
606 struct tblock *tblk;
608 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
610 filetype = ip->i_mode & S_IFMT;
611 switch (filetype) {
612 case S_IFREG:
613 break;
614 case S_IFLNK:
615 /* fast symbolic link */
616 if (ip->i_size < IDATASIZE) {
617 ip->i_size = 0;
618 return 0;
620 break;
621 default:
622 assert(filetype != S_IFDIR);
623 return 0;
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;
633 * free EA
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);
640 * free ACL
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;
652 if (ip->i_size)
653 return xtTruncate_pmap(tid, ip, 0);
655 return 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.
669 * RETURN: 0 -ok
671 int freeZeroLink(struct inode *ip)
673 int rc = 0;
674 int type;
676 jfs_info("freeZeroLink: ip = 0x%p", ip);
678 /* return if not reg or symbolic link or if size is
679 * already ok.
681 type = ip->i_mode & S_IFMT;
683 switch (type) {
684 case S_IFREG:
685 break;
686 case S_IFLNK:
687 /* if its contained in inode nothing to do */
688 if (ip->i_size < IDATASIZE)
689 return 0;
690 break;
691 default:
692 return 0;
696 * free EA
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 */
708 maplock.index = 1;
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);
717 * free ACL
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 */
728 maplock.index = 1;
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;
741 if (ip->i_size)
742 rc = xtTruncate(0, ip, 0, COMMIT_WMAP);
744 return rc;
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
756 * crp - credential
758 * RETURN: Errors from subroutines
760 * note:
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)
774 int rc;
775 tid_t tid;
776 struct inode *ip = old_dentry->d_inode;
777 ino_t ino;
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)
786 return -EMLINK;
788 if (ip->i_nlink == 0)
789 return -ENOENT;
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)))
800 goto out;
802 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
803 goto free_dname;
806 * create entry for new link in parent directory
808 ino = ip->i_ino;
809 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
810 goto free_dname;
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);
818 iplist[0] = ip;
819 iplist[1] = dir;
820 rc = txCommit(tid, 2, &iplist[0], 0);
822 if (rc) {
823 ip->i_nlink--;
824 iput(ip);
825 } else
826 d_instantiate(dentry, ip);
828 free_dname:
829 free_UCSname(&dname);
831 out:
832 txEnd(tid);
834 up(&JFS_IP(dir)->commit_sem);
835 up(&JFS_IP(ip)->commit_sem);
837 jfs_info("jfs_link: rc:%d", rc);
838 return rc;
842 * NAME: jfs_symlink(dip, dentry, name)
844 * FUNCTION: creates a symbolic link to <symlink> by name <name>
845 * in directory <dip>
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
854 * note:
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,
860 const char *name)
862 int rc;
863 tid_t tid;
864 ino_t ino = 0;
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;
870 s64 xlen = 0;
871 int bmask = 0, xsize;
872 s64 extent = 0, xaddr;
873 struct metapage *mp;
874 struct super_block *sb;
875 struct tblock *tblk;
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)))
889 goto out1;
892 * allocate on-disk/in-memory inode for symbolic link:
893 * (iAlloc() returns new, locked inode)
895 ip = ialloc(dip, S_IFLNK | 0777);
896 if (ip == NULL) {
897 rc = -ENOSPC;
898 goto out2;
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)
915 ip->i_mode |= 0777;
918 * write symbolic link target path name
920 xtInitRoot(tid, ip);
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 ",
941 ssize, name);
944 * write source path name in a single extent
946 else {
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.
957 sb = ip->i_sb;
958 bmask = JFS_SBI(sb)->bsize - 1;
959 xsize = (ssize + bmask) & ~bmask;
960 xaddr = 0;
961 xlen = xsize >> JFS_SBI(sb)->l2bsize;
962 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
963 txAbort(tid, 0);
964 rc = -ENOSPC;
965 goto out3;
967 extent = xaddr;
968 ip->i_size = ssize - 1;
969 while (ssize) {
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);
975 if (mp == NULL) {
976 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
977 rc = -EIO;
978 txAbort(tid, 0);
979 goto out3;
981 memcpy(mp->data, name, copy_size);
982 flush_metapage(mp);
983 ssize -= copy_size;
984 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);
993 if (rc == 0) {
994 ino = ip->i_ino;
995 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
997 if (rc) {
998 if (xlen)
999 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1000 txAbort(tid, 0);
1001 /* discard new inode */
1002 goto out3;
1005 insert_inode_hash(ip);
1006 mark_inode_dirty(ip);
1009 * commit update of parent directory and link object
1012 iplist[0] = dip;
1013 iplist[1] = ip;
1014 rc = txCommit(tid, 2, &iplist[0], 0);
1016 out3:
1017 txEnd(tid);
1018 up(&JFS_IP(dip)->commit_sem);
1019 up(&JFS_IP(ip)->commit_sem);
1020 if (rc) {
1021 ip->i_nlink = 0;
1022 iput(ip);
1023 } else
1024 d_instantiate(dentry, ip);
1026 out2:
1027 free_UCSname(&dname);
1029 #ifdef CONFIG_JFS_POSIX_ACL
1030 if (rc == 0)
1031 jfs_init_acl(ip, dip);
1032 #endif
1034 out1:
1035 jfs_info("jfs_symlink: rc:%d", rc);
1036 return rc;
1041 * NAME: jfs_rename
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;
1049 ino_t ino;
1050 struct component_name new_dname;
1051 struct inode *new_ip;
1052 struct component_name old_dname;
1053 struct inode *old_ip;
1054 int rc;
1055 tid_t tid;
1056 struct tlock *tlck;
1057 struct dt_lock *dtlck;
1058 struct lv *lv;
1059 int ipcount;
1060 struct inode *iplist[4];
1061 struct tblock *tblk;
1062 s64 new_size = 0;
1063 int commit_flag;
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)))
1073 goto out1;
1075 if ((rc = get_UCSname(&new_dname, new_dentry)))
1076 goto out2;
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)) {
1083 rc = -ENOENT;
1084 goto out3;
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);
1091 if (rc == 0) {
1092 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1093 rc = -ESTALE;
1094 goto out3;
1096 } else if (rc != -ENOENT)
1097 goto out3;
1098 else if (new_ip) {
1099 /* no entry exists, but one was expected */
1100 rc = -ESTALE;
1101 goto out3;
1104 if (S_ISDIR(old_ip->i_mode)) {
1105 if (new_ip) {
1106 if (!dtEmpty(new_ip)) {
1107 rc = -ENOTEMPTY;
1108 goto out3;
1110 } else if ((new_dir != old_dir) &&
1111 (new_dir->i_nlink == JFS_LINK_MAX)) {
1112 rc = -EMLINK;
1113 goto out3;
1115 } else if (new_ip) {
1116 IWRITE_LOCK(new_ip);
1117 /* Init inode for quota operations. */
1118 DQUOT_INIT(new_ip);
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);
1131 if (new_ip) {
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);
1139 if (rc)
1140 goto out4;
1141 new_ip->i_nlink--;
1142 if (S_ISDIR(new_ip->i_mode)) {
1143 new_ip->i_nlink--;
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");
1153 return -EIO;
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 */
1163 rc = new_size;
1164 goto out4;
1166 tblk = tid_to_tblock(tid);
1167 tblk->xflag |= COMMIT_DELETE;
1168 tblk->u.ip = new_ip;
1169 } else {
1170 new_ip->i_ctime = CURRENT_TIME;
1171 mark_inode_dirty(new_ip);
1173 } else {
1175 * Add new directory entry
1177 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1178 JFS_CREATE);
1179 if (rc) {
1180 jfs_err("jfs_rename didn't expect dtSearch to fail "
1181 "w/rc = %d", rc);
1182 goto out4;
1185 ino = old_ip->i_ino;
1186 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1187 if (rc) {
1188 if (rc == -EIO)
1189 jfs_err("jfs_rename: dtInsert returned -EIO");
1190 goto out4;
1192 if (S_ISDIR(old_ip->i_mode))
1193 new_dir->i_nlink++;
1196 * Remove old directory entry
1199 ino = old_ip->i_ino;
1200 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1201 if (rc) {
1202 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1203 rc);
1204 txAbort(tid, 1); /* Marks Filesystem dirty */
1205 goto out4;
1207 if (S_ISDIR(old_ip->i_mode)) {
1208 old_dir->i_nlink--;
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];
1224 lv->offset = 0;
1225 lv->length = 1;
1226 dtlck->index++;
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 */
1240 ipcount = 0;
1241 iplist[ipcount++] = old_ip;
1242 if (new_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
1255 * transaction.
1257 if (new_size)
1258 commit_flag = COMMIT_SYNC;
1259 else
1260 commit_flag = 0;
1262 rc = txCommit(tid, ipcount, iplist, commit_flag);
1264 out4:
1265 txEnd(tid);
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);
1271 if (new_ip)
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);
1278 if (new_size < 0) {
1279 txAbort(tid, 1);
1280 rc = new_size;
1281 } else
1282 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1283 txEnd(tid);
1284 up(&JFS_IP(new_ip)->commit_sem);
1286 if (new_ip && (new_ip->i_nlink == 0))
1287 set_cflag(COMMIT_Nolink, new_ip);
1288 out3:
1289 free_UCSname(&new_dname);
1290 out2:
1291 free_UCSname(&old_dname);
1292 out1:
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);
1307 return rc;
1312 * NAME: jfs_mknod
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;
1322 ino_t ino;
1323 struct inode *ip;
1324 struct inode *iplist[2];
1325 int rc;
1326 tid_t tid;
1327 struct tblock *tblk;
1329 if (!new_valid_dev(rdev))
1330 return -EINVAL;
1332 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1334 if ((rc = get_UCSname(&dname, dentry)))
1335 goto out;
1337 ip = ialloc(dir, mode);
1338 if (ip == NULL) {
1339 rc = -ENOSPC;
1340 goto out1;
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)))
1350 goto out3;
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;
1357 ino = ip->i_ino;
1358 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
1359 goto out3;
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);
1372 iplist[0] = dir;
1373 iplist[1] = ip;
1374 rc = txCommit(tid, 2, iplist, 0);
1376 out3:
1377 txEnd(tid);
1378 up(&JFS_IP(ip)->commit_sem);
1379 up(&JFS_IP(dir)->commit_sem);
1380 if (rc) {
1381 ip->i_nlink = 0;
1382 iput(ip);
1383 } else
1384 d_instantiate(dentry, ip);
1386 out1:
1387 free_UCSname(&dname);
1389 #ifdef CONFIG_JFS_POSIX_ACL
1390 if (rc == 0)
1391 jfs_init_acl(ip, dir);
1392 #endif
1394 out:
1395 jfs_info("jfs_mknod: returning %d", rc);
1396 return rc;
1399 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1401 struct btstack btstack;
1402 ino_t inum;
1403 struct inode *ip;
1404 struct component_name key;
1405 const char *name = dentry->d_name.name;
1406 int len = dentry->d_name.len;
1407 int rc;
1409 jfs_info("jfs_lookup: name = %s", name);
1412 if ((name[0] == '.') && (len == 1))
1413 inum = dip->i_ino;
1414 else if (strcmp(name, "..") == 0)
1415 inum = PARENT(dip);
1416 else {
1417 if ((rc = get_UCSname(&key, dentry)))
1418 return ERR_PTR(rc);
1419 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1420 free_UCSname(&key);
1421 if (rc == -ENOENT) {
1422 d_add(dentry, NULL);
1423 return ERR_PTR(0);
1424 } else if (rc) {
1425 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1426 return ERR_PTR(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);
1433 if (ip)
1434 iput(ip);
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;
1446 return dentry;
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;
1456 parent_ino =
1457 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1458 inode = iget(sb, parent_ino);
1459 if (inode) {
1460 if (is_bad_inode(inode)) {
1461 iput(inode);
1462 parent = ERR_PTR(-EACCES);
1463 } else {
1464 parent = d_alloc_anon(inode);
1465 if (!parent) {
1466 parent = ERR_PTR(-ENOMEM);
1467 iput(inode);
1472 return parent;
1475 struct inode_operations jfs_dir_inode_operations = {
1476 .create = jfs_create,
1477 .lookup = jfs_lookup,
1478 .link = jfs_link,
1479 .unlink = jfs_unlink,
1480 .symlink = jfs_symlink,
1481 .mkdir = jfs_mkdir,
1482 .rmdir = jfs_rmdir,
1483 .mknod = jfs_mknod,
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,
1492 #endif
1495 struct file_operations jfs_dir_operations = {
1496 .read = generic_read_dir,
1497 .readdir = jfs_readdir,
1498 .fsync = jfs_fsync,
1501 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1503 unsigned long hash;
1504 int i;
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);
1511 return 0;
1514 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1516 int i, result = 1;
1518 if (a->len != b->len)
1519 goto out;
1520 for (i=0; i < a->len; i++) {
1521 if (tolower(a->name[i]) != tolower(b->name[i]))
1522 goto out;
1524 result = 0;
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);
1534 out:
1535 return result;
1538 struct dentry_operations jfs_ci_dentry_operations =
1540 .d_hash = jfs_ci_hash,
1541 .d_compare = jfs_ci_compare,