JFS: allow extended attributes to be set within a existing transaction
[linux-2.6/linux-mips.git] / fs / jfs / namei.c
blobf23f9c2aa5259b37eae271e87a65e74123f00e6c
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"
35 * forward references
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));
56 ea->flag = 0;
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).
68 * nd- nd struct
70 * RETURN: Errors from subroutines
73 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
74 struct nameidata *nd)
76 int rc = 0;
77 tid_t tid; /* transaction id */
78 struct inode *ip = NULL; /* child directory inode */
79 ino_t ino;
80 struct component_name dname; /* child directory name */
81 struct btstack btstack;
82 struct inode *iplist[2];
83 struct tblock *tblk;
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)))
92 goto out1;
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);
100 if (ip == NULL) {
101 rc = -ENOSPC;
102 goto out2;
105 tid = txBegin(dip->i_sb, 0);
107 down(&JFS_IP(dip)->commit_sem);
108 down(&JFS_IP(ip)->commit_sem);
110 rc = jfs_init_acl(tid, ip, dip);
111 if (rc)
112 goto out3;
114 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
115 jfs_err("jfs_create: dtSearch returned %d", rc);
116 txAbort(tid, 0);
117 goto out3;
120 tblk = tid_to_tblock(tid);
121 tblk->xflag |= COMMIT_CREATE;
122 tblk->ino = ip->i_ino;
123 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
125 iplist[0] = dip;
126 iplist[1] = ip;
129 * initialize the child XAD tree root in-line in inode
131 xtInitRoot(tid, ip);
134 * create entry in parent directory for child directory
135 * (dtInsert() releases parent directory page)
137 ino = ip->i_ino;
138 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
139 if (rc == -EIO) {
140 jfs_err("jfs_create: dtInsert returned -EIO");
141 txAbort(tid, 1); /* Marks Filesystem dirty */
142 } else
143 txAbort(tid, 0); /* Filesystem full */
144 goto out3;
147 ip->i_op = &jfs_file_inode_operations;
148 ip->i_fop = &jfs_file_operations;
149 ip->i_mapping->a_ops = &jfs_aops;
151 insert_inode_hash(ip);
152 mark_inode_dirty(ip);
154 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
156 mark_inode_dirty(dip);
158 rc = txCommit(tid, 2, &iplist[0], 0);
160 out3:
161 txEnd(tid);
162 up(&JFS_IP(dip)->commit_sem);
163 up(&JFS_IP(ip)->commit_sem);
164 if (rc) {
165 free_ea_wmap(ip);
166 ip->i_nlink = 0;
167 iput(ip);
168 } else
169 d_instantiate(dentry, ip);
171 out2:
172 free_UCSname(&dname);
174 out1:
176 jfs_info("jfs_create: rc:%d", rc);
177 return rc;
182 * NAME: jfs_mkdir(dip, dentry, mode)
184 * FUNCTION: create a child directory in the parent directory <dip>
185 * with name = <from dentry> and mode = <mode>
187 * PARAMETER: dip - parent directory vnode
188 * dentry - dentry of child directory
189 * mode - create mode (rwxrwxrwx).
191 * RETURN: Errors from subroutines
193 * note:
194 * EACCESS: user needs search+write permission on the parent directory
196 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
198 int rc = 0;
199 tid_t tid; /* transaction id */
200 struct inode *ip = NULL; /* child directory inode */
201 ino_t ino;
202 struct component_name dname; /* child directory name */
203 struct btstack btstack;
204 struct inode *iplist[2];
205 struct tblock *tblk;
207 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
209 /* link count overflow on parent directory ? */
210 if (dip->i_nlink == JFS_LINK_MAX) {
211 rc = -EMLINK;
212 goto out1;
216 * search parent directory for entry/freespace
217 * (dtSearch() returns parent directory page pinned)
219 if ((rc = get_UCSname(&dname, dentry)))
220 goto out1;
223 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
224 * block there while holding dtree page, so we allocate the inode &
225 * begin the transaction before we search the directory.
227 ip = ialloc(dip, S_IFDIR | mode);
228 if (ip == NULL) {
229 rc = -ENOSPC;
230 goto out2;
233 tid = txBegin(dip->i_sb, 0);
235 down(&JFS_IP(dip)->commit_sem);
236 down(&JFS_IP(ip)->commit_sem);
238 rc = jfs_init_acl(tid, ip, dip);
239 if (rc)
240 goto out3;
242 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
243 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
244 txAbort(tid, 0);
245 goto out3;
248 tblk = tid_to_tblock(tid);
249 tblk->xflag |= COMMIT_CREATE;
250 tblk->ino = ip->i_ino;
251 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
253 iplist[0] = dip;
254 iplist[1] = ip;
257 * initialize the child directory in-line in inode
259 dtInitRoot(tid, ip, dip->i_ino);
262 * create entry in parent directory for child directory
263 * (dtInsert() releases parent directory page)
265 ino = ip->i_ino;
266 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
267 if (rc == -EIO) {
268 jfs_err("jfs_mkdir: dtInsert returned -EIO");
269 txAbort(tid, 1); /* Marks Filesystem dirty */
270 } else
271 txAbort(tid, 0); /* Filesystem full */
272 goto out3;
275 ip->i_nlink = 2; /* for '.' */
276 ip->i_op = &jfs_dir_inode_operations;
277 ip->i_fop = &jfs_dir_operations;
279 insert_inode_hash(ip);
280 mark_inode_dirty(ip);
282 /* update parent directory inode */
283 dip->i_nlink++; /* for '..' from child directory */
284 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
285 mark_inode_dirty(dip);
287 rc = txCommit(tid, 2, &iplist[0], 0);
289 out3:
290 txEnd(tid);
291 up(&JFS_IP(dip)->commit_sem);
292 up(&JFS_IP(ip)->commit_sem);
293 if (rc) {
294 free_ea_wmap(ip);
295 ip->i_nlink = 0;
296 iput(ip);
297 } else
298 d_instantiate(dentry, ip);
300 out2:
301 free_UCSname(&dname);
304 out1:
306 jfs_info("jfs_mkdir: rc:%d", rc);
307 return rc;
311 * NAME: jfs_rmdir(dip, dentry)
313 * FUNCTION: remove a link to child directory
315 * PARAMETER: dip - parent inode
316 * dentry - child directory dentry
318 * RETURN: -EINVAL - if name is . or ..
319 * -EINVAL - if . or .. exist but are invalid.
320 * errors from subroutines
322 * note:
323 * if other threads have the directory open when the last link
324 * is removed, the "." and ".." entries, if present, are removed before
325 * rmdir() returns and no new entries may be created in the directory,
326 * but the directory is not removed until the last reference to
327 * the directory is released (cf.unlink() of regular file).
329 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
331 int rc;
332 tid_t tid; /* transaction id */
333 struct inode *ip = dentry->d_inode;
334 ino_t ino;
335 struct component_name dname;
336 struct inode *iplist[2];
337 struct tblock *tblk;
339 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
341 /* Init inode for quota operations. */
342 DQUOT_INIT(ip);
344 /* directory must be empty to be removed */
345 if (!dtEmpty(ip)) {
346 rc = -ENOTEMPTY;
347 goto out;
350 if ((rc = get_UCSname(&dname, dentry))) {
351 goto out;
354 tid = txBegin(dip->i_sb, 0);
356 down(&JFS_IP(dip)->commit_sem);
357 down(&JFS_IP(ip)->commit_sem);
359 iplist[0] = dip;
360 iplist[1] = ip;
362 tblk = tid_to_tblock(tid);
363 tblk->xflag |= COMMIT_DELETE;
364 tblk->u.ip = ip;
367 * delete the entry of target directory from parent directory
369 ino = ip->i_ino;
370 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
371 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
372 if (rc == -EIO)
373 txAbort(tid, 1);
374 txEnd(tid);
375 up(&JFS_IP(dip)->commit_sem);
376 up(&JFS_IP(ip)->commit_sem);
378 goto out2;
381 /* update parent directory's link count corresponding
382 * to ".." entry of the target directory deleted
384 dip->i_nlink--;
385 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
386 mark_inode_dirty(dip);
389 * OS/2 could have created EA and/or ACL
391 /* free EA from both persistent and working map */
392 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
393 /* free EA pages */
394 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
396 JFS_IP(ip)->ea.flag = 0;
398 /* free ACL from both persistent and working map */
399 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
400 /* free ACL pages */
401 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
403 JFS_IP(ip)->acl.flag = 0;
405 /* mark the target directory as deleted */
406 ip->i_nlink = 0;
407 mark_inode_dirty(ip);
409 rc = txCommit(tid, 2, &iplist[0], 0);
411 txEnd(tid);
413 up(&JFS_IP(dip)->commit_sem);
414 up(&JFS_IP(ip)->commit_sem);
417 * Truncating the directory index table is not guaranteed. It
418 * may need to be done iteratively
420 if (test_cflag(COMMIT_Stale, dip)) {
421 if (dip->i_size > 1)
422 jfs_truncate_nolock(dip, 0);
424 clear_cflag(COMMIT_Stale, dip);
427 out2:
428 free_UCSname(&dname);
430 out:
431 jfs_info("jfs_rmdir: rc:%d", rc);
432 return rc;
436 * NAME: jfs_unlink(dip, dentry)
438 * FUNCTION: remove a link to object <vp> named by <name>
439 * from parent directory <dvp>
441 * PARAMETER: dip - inode of parent directory
442 * dentry - dentry of object to be removed
444 * RETURN: errors from subroutines
446 * note:
447 * temporary file: if one or more processes have the file open
448 * when the last link is removed, the link will be removed before
449 * unlink() returns, but the removal of the file contents will be
450 * postponed until all references to the files are closed.
452 * JFS does NOT support unlink() on directories.
455 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
457 int rc;
458 tid_t tid; /* transaction id */
459 struct inode *ip = dentry->d_inode;
460 ino_t ino;
461 struct component_name dname; /* object name */
462 struct inode *iplist[2];
463 struct tblock *tblk;
464 s64 new_size = 0;
465 int commit_flag;
467 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
469 /* Init inode for quota operations. */
470 DQUOT_INIT(ip);
472 if ((rc = get_UCSname(&dname, dentry)))
473 goto out;
475 IWRITE_LOCK(ip);
477 tid = txBegin(dip->i_sb, 0);
479 down(&JFS_IP(dip)->commit_sem);
480 down(&JFS_IP(ip)->commit_sem);
482 iplist[0] = dip;
483 iplist[1] = ip;
486 * delete the entry of target file from parent directory
488 ino = ip->i_ino;
489 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
490 jfs_err("jfs_unlink: dtDelete returned %d", rc);
491 if (rc == -EIO)
492 txAbort(tid, 1); /* Marks FS Dirty */
493 txEnd(tid);
494 up(&JFS_IP(dip)->commit_sem);
495 up(&JFS_IP(ip)->commit_sem);
496 IWRITE_UNLOCK(ip);
497 goto out1;
500 ASSERT(ip->i_nlink);
502 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
503 mark_inode_dirty(dip);
505 /* update target's inode */
506 ip->i_nlink--;
507 mark_inode_dirty(ip);
510 * commit zero link count object
512 if (ip->i_nlink == 0) {
513 assert(!test_cflag(COMMIT_Nolink, ip));
514 /* free block resources */
515 if ((new_size = commitZeroLink(tid, ip)) < 0) {
516 txAbort(tid, 1); /* Marks FS Dirty */
517 txEnd(tid);
518 up(&JFS_IP(dip)->commit_sem);
519 up(&JFS_IP(ip)->commit_sem);
520 IWRITE_UNLOCK(ip);
521 rc = new_size;
522 goto out1;
524 tblk = tid_to_tblock(tid);
525 tblk->xflag |= COMMIT_DELETE;
526 tblk->u.ip = ip;
530 * Incomplete truncate of file data can
531 * result in timing problems unless we synchronously commit the
532 * transaction.
534 if (new_size)
535 commit_flag = COMMIT_SYNC;
536 else
537 commit_flag = 0;
540 * If xtTruncate was incomplete, commit synchronously to avoid
541 * timing complications
543 rc = txCommit(tid, 2, &iplist[0], commit_flag);
545 txEnd(tid);
547 up(&JFS_IP(dip)->commit_sem);
548 up(&JFS_IP(ip)->commit_sem);
551 while (new_size && (rc == 0)) {
552 tid = txBegin(dip->i_sb, 0);
553 down(&JFS_IP(ip)->commit_sem);
554 new_size = xtTruncate_pmap(tid, ip, new_size);
555 if (new_size < 0) {
556 txAbort(tid, 1); /* Marks FS Dirty */
557 rc = new_size;
558 } else
559 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
560 txEnd(tid);
561 up(&JFS_IP(ip)->commit_sem);
564 if (ip->i_nlink == 0)
565 set_cflag(COMMIT_Nolink, ip);
567 IWRITE_UNLOCK(ip);
570 * Truncating the directory index table is not guaranteed. It
571 * may need to be done iteratively
573 if (test_cflag(COMMIT_Stale, dip)) {
574 if (dip->i_size > 1)
575 jfs_truncate_nolock(dip, 0);
577 clear_cflag(COMMIT_Stale, dip);
580 out1:
581 free_UCSname(&dname);
582 out:
583 jfs_info("jfs_unlink: rc:%d", rc);
584 return rc;
588 * NAME: commitZeroLink()
590 * FUNCTION: for non-directory, called by jfs_remove(),
591 * truncate a regular file, directory or symbolic
592 * link to zero length. return 0 if type is not
593 * one of these.
595 * if the file is currently associated with a VM segment
596 * only permanent disk and inode map resources are freed,
597 * and neither the inode nor indirect blocks are modified
598 * so that the resources can be later freed in the work
599 * map by ctrunc1.
600 * if there is no VM segment on entry, the resources are
601 * freed in both work and permanent map.
602 * (? for temporary file - memory object is cached even
603 * after no reference:
604 * reference count > 0 - )
606 * PARAMETERS: cd - pointer to commit data structure.
607 * current inode is the one to truncate.
609 * RETURN: Errors from subroutines
611 static s64 commitZeroLink(tid_t tid, struct inode *ip)
613 int filetype;
614 struct tblock *tblk;
616 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
618 filetype = ip->i_mode & S_IFMT;
619 switch (filetype) {
620 case S_IFREG:
621 break;
622 case S_IFLNK:
623 /* fast symbolic link */
624 if (ip->i_size < IDATASIZE) {
625 ip->i_size = 0;
626 return 0;
628 break;
629 default:
630 assert(filetype != S_IFDIR);
631 return 0;
634 set_cflag(COMMIT_Freewmap, ip);
636 /* mark transaction of block map update type */
637 tblk = tid_to_tblock(tid);
638 tblk->xflag |= COMMIT_PMAP;
641 * free EA
643 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
644 /* acquire maplock on EA to be freed from block map */
645 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
648 * free ACL
650 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
651 /* acquire maplock on EA to be freed from block map */
652 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
655 * free xtree/data (truncate to zero length):
656 * free xtree/data pages from cache if COMMIT_PWMAP,
657 * free xtree/data blocks from persistent block map, and
658 * free xtree/data blocks from working block map if COMMIT_PWMAP;
660 if (ip->i_size)
661 return xtTruncate_pmap(tid, ip, 0);
663 return 0;
668 * NAME: jfs_free_zero_link()
670 * FUNCTION: for non-directory, called by iClose(),
671 * free resources of a file from cache and WORKING map
672 * for a file previously committed with zero link count
673 * while associated with a pager object,
675 * PARAMETER: ip - pointer to inode of file.
677 void jfs_free_zero_link(struct inode *ip)
679 int type;
681 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
683 /* return if not reg or symbolic link or if size is
684 * already ok.
686 type = ip->i_mode & S_IFMT;
688 switch (type) {
689 case S_IFREG:
690 break;
691 case S_IFLNK:
692 /* if its contained in inode nothing to do */
693 if (ip->i_size < IDATASIZE)
694 return;
695 break;
696 default:
697 return;
701 * free EA
703 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
704 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
705 int xlen = lengthDXD(&JFS_IP(ip)->ea);
706 struct maplock maplock; /* maplock for COMMIT_WMAP */
707 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
709 /* free EA pages from cache */
710 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
712 /* free EA extent from working block map */
713 maplock.index = 1;
714 pxdlock = (struct pxd_lock *) & maplock;
715 pxdlock->flag = mlckFREEPXD;
716 PXDaddress(&pxdlock->pxd, xaddr);
717 PXDlength(&pxdlock->pxd, xlen);
718 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
722 * free ACL
724 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
725 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
726 int xlen = lengthDXD(&JFS_IP(ip)->acl);
727 struct maplock maplock; /* maplock for COMMIT_WMAP */
728 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
730 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
732 /* free ACL extent from working block map */
733 maplock.index = 1;
734 pxdlock = (struct pxd_lock *) & maplock;
735 pxdlock->flag = mlckFREEPXD;
736 PXDaddress(&pxdlock->pxd, xaddr);
737 PXDlength(&pxdlock->pxd, xlen);
738 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
742 * free xtree/data (truncate to zero length):
743 * free xtree/data pages from cache, and
744 * free xtree/data blocks from working block map;
746 if (ip->i_size)
747 xtTruncate(0, ip, 0, COMMIT_WMAP);
751 * NAME: jfs_link(vp, dvp, name, crp)
753 * FUNCTION: create a link to <vp> by the name = <name>
754 * in the parent directory <dvp>
756 * PARAMETER: vp - target object
757 * dvp - parent directory of new link
758 * name - name of new link to target object
759 * crp - credential
761 * RETURN: Errors from subroutines
763 * note:
764 * JFS does NOT support link() on directories (to prevent circular
765 * path in the directory hierarchy);
766 * EPERM: the target object is a directory, and either the caller
767 * does not have appropriate privileges or the implementation prohibits
768 * using link() on directories [XPG4.2].
770 * JFS does NOT support links between file systems:
771 * EXDEV: target object and new link are on different file systems and
772 * implementation does not support links between file systems [XPG4.2].
774 static int jfs_link(struct dentry *old_dentry,
775 struct inode *dir, struct dentry *dentry)
777 int rc;
778 tid_t tid;
779 struct inode *ip = old_dentry->d_inode;
780 ino_t ino;
781 struct component_name dname;
782 struct btstack btstack;
783 struct inode *iplist[2];
785 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
786 dentry->d_name.name);
788 if (ip->i_nlink == JFS_LINK_MAX)
789 return -EMLINK;
791 if (ip->i_nlink == 0)
792 return -ENOENT;
794 tid = txBegin(ip->i_sb, 0);
796 down(&JFS_IP(dir)->commit_sem);
797 down(&JFS_IP(ip)->commit_sem);
800 * scan parent directory for entry/freespace
802 if ((rc = get_UCSname(&dname, dentry)))
803 goto out;
805 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
806 goto free_dname;
809 * create entry for new link in parent directory
811 ino = ip->i_ino;
812 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
813 goto free_dname;
815 /* update object inode */
816 ip->i_nlink++; /* for new link */
817 ip->i_ctime = CURRENT_TIME;
818 mark_inode_dirty(dir);
819 atomic_inc(&ip->i_count);
821 iplist[0] = ip;
822 iplist[1] = dir;
823 rc = txCommit(tid, 2, &iplist[0], 0);
825 if (rc) {
826 ip->i_nlink--;
827 iput(ip);
828 } else
829 d_instantiate(dentry, ip);
831 free_dname:
832 free_UCSname(&dname);
834 out:
835 txEnd(tid);
837 up(&JFS_IP(dir)->commit_sem);
838 up(&JFS_IP(ip)->commit_sem);
840 jfs_info("jfs_link: rc:%d", rc);
841 return rc;
845 * NAME: jfs_symlink(dip, dentry, name)
847 * FUNCTION: creates a symbolic link to <symlink> by name <name>
848 * in directory <dip>
850 * PARAMETER: dip - parent directory vnode
851 * dentry - dentry of symbolic link
852 * name - the path name of the existing object
853 * that will be the source of the link
855 * RETURN: errors from subroutines
857 * note:
858 * ENAMETOOLONG: pathname resolution of a symbolic link produced
859 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
862 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
863 const char *name)
865 int rc;
866 tid_t tid;
867 ino_t ino = 0;
868 struct component_name dname;
869 int ssize; /* source pathname size */
870 struct btstack btstack;
871 struct inode *ip = dentry->d_inode;
872 unchar *i_fastsymlink;
873 s64 xlen = 0;
874 int bmask = 0, xsize;
875 s64 extent = 0, xaddr;
876 struct metapage *mp;
877 struct super_block *sb;
878 struct tblock *tblk;
880 struct inode *iplist[2];
882 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
884 ssize = strlen(name) + 1;
887 * search parent directory for entry/freespace
888 * (dtSearch() returns parent directory page pinned)
891 if ((rc = get_UCSname(&dname, dentry)))
892 goto out1;
895 * allocate on-disk/in-memory inode for symbolic link:
896 * (iAlloc() returns new, locked inode)
898 ip = ialloc(dip, S_IFLNK | 0777);
899 if (ip == NULL) {
900 rc = -ENOSPC;
901 goto out2;
904 tid = txBegin(dip->i_sb, 0);
906 down(&JFS_IP(dip)->commit_sem);
907 down(&JFS_IP(ip)->commit_sem);
909 tblk = tid_to_tblock(tid);
910 tblk->xflag |= COMMIT_CREATE;
911 tblk->ino = ip->i_ino;
912 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
914 /* fix symlink access permission
915 * (dir_create() ANDs in the u.u_cmask,
916 * but symlinks really need to be 777 access)
918 ip->i_mode |= 0777;
921 * write symbolic link target path name
923 xtInitRoot(tid, ip);
926 * write source path name inline in on-disk inode (fast symbolic link)
929 if (ssize <= IDATASIZE) {
930 ip->i_op = &jfs_symlink_inode_operations;
932 i_fastsymlink = JFS_IP(ip)->i_inline;
933 memcpy(i_fastsymlink, name, ssize);
934 ip->i_size = ssize - 1;
937 * if symlink is > 128 bytes, we don't have the space to
938 * store inline extended attributes
940 if (ssize > sizeof (JFS_IP(ip)->i_inline))
941 JFS_IP(ip)->mode2 &= ~INLINEEA;
943 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
944 ssize, name);
947 * write source path name in a single extent
949 else {
950 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
952 ip->i_op = &page_symlink_inode_operations;
953 ip->i_mapping->a_ops = &jfs_aops;
956 * even though the data of symlink object (source
957 * path name) is treated as non-journaled user data,
958 * it is read/written thru buffer cache for performance.
960 sb = ip->i_sb;
961 bmask = JFS_SBI(sb)->bsize - 1;
962 xsize = (ssize + bmask) & ~bmask;
963 xaddr = 0;
964 xlen = xsize >> JFS_SBI(sb)->l2bsize;
965 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
966 txAbort(tid, 0);
967 rc = -ENOSPC;
968 goto out3;
970 extent = xaddr;
971 ip->i_size = ssize - 1;
972 while (ssize) {
973 /* This is kind of silly since PATH_MAX == 4K */
974 int copy_size = min(ssize, PSIZE);
976 mp = get_metapage(ip, xaddr, PSIZE, 1);
978 if (mp == NULL) {
979 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
980 rc = -EIO;
981 txAbort(tid, 0);
982 goto out3;
984 memcpy(mp->data, name, copy_size);
985 flush_metapage(mp);
986 ssize -= copy_size;
987 name += copy_size;
988 xaddr += JFS_SBI(sb)->nbperpage;
993 * create entry for symbolic link in parent directory
995 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
996 if (rc == 0) {
997 ino = ip->i_ino;
998 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1000 if (rc) {
1001 if (xlen)
1002 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1003 txAbort(tid, 0);
1004 /* discard new inode */
1005 goto out3;
1008 insert_inode_hash(ip);
1009 mark_inode_dirty(ip);
1012 * commit update of parent directory and link object
1015 iplist[0] = dip;
1016 iplist[1] = ip;
1017 rc = txCommit(tid, 2, &iplist[0], 0);
1019 out3:
1020 txEnd(tid);
1021 up(&JFS_IP(dip)->commit_sem);
1022 up(&JFS_IP(ip)->commit_sem);
1023 if (rc) {
1024 free_ea_wmap(ip);
1025 ip->i_nlink = 0;
1026 iput(ip);
1027 } else
1028 d_instantiate(dentry, ip);
1030 out2:
1031 free_UCSname(&dname);
1033 out1:
1034 jfs_info("jfs_symlink: rc:%d", rc);
1035 return rc;
1040 * NAME: jfs_rename
1042 * FUNCTION: rename a file or directory
1044 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1045 struct inode *new_dir, struct dentry *new_dentry)
1047 struct btstack btstack;
1048 ino_t ino;
1049 struct component_name new_dname;
1050 struct inode *new_ip;
1051 struct component_name old_dname;
1052 struct inode *old_ip;
1053 int rc;
1054 tid_t tid;
1055 struct tlock *tlck;
1056 struct dt_lock *dtlck;
1057 struct lv *lv;
1058 int ipcount;
1059 struct inode *iplist[4];
1060 struct tblock *tblk;
1061 s64 new_size = 0;
1062 int commit_flag;
1065 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1066 new_dentry->d_name.name);
1068 old_ip = old_dentry->d_inode;
1069 new_ip = new_dentry->d_inode;
1071 if ((rc = get_UCSname(&old_dname, old_dentry)))
1072 goto out1;
1074 if ((rc = get_UCSname(&new_dname, new_dentry)))
1075 goto out2;
1078 * Make sure source inode number is what we think it is
1080 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1081 if (rc || (ino != old_ip->i_ino)) {
1082 rc = -ENOENT;
1083 goto out3;
1087 * Make sure dest inode number (if any) is what we think it is
1089 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1090 if (rc == 0) {
1091 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1092 rc = -ESTALE;
1093 goto out3;
1095 } else if (rc != -ENOENT)
1096 goto out3;
1097 else if (new_ip) {
1098 /* no entry exists, but one was expected */
1099 rc = -ESTALE;
1100 goto out3;
1103 if (S_ISDIR(old_ip->i_mode)) {
1104 if (new_ip) {
1105 if (!dtEmpty(new_ip)) {
1106 rc = -ENOTEMPTY;
1107 goto out3;
1109 } else if ((new_dir != old_dir) &&
1110 (new_dir->i_nlink == JFS_LINK_MAX)) {
1111 rc = -EMLINK;
1112 goto out3;
1114 } else if (new_ip) {
1115 IWRITE_LOCK(new_ip);
1116 /* Init inode for quota operations. */
1117 DQUOT_INIT(new_ip);
1121 * The real work starts here
1123 tid = txBegin(new_dir->i_sb, 0);
1125 down(&JFS_IP(new_dir)->commit_sem);
1126 down(&JFS_IP(old_ip)->commit_sem);
1127 if (old_dir != new_dir)
1128 down(&JFS_IP(old_dir)->commit_sem);
1130 if (new_ip) {
1131 down(&JFS_IP(new_ip)->commit_sem);
1133 * Change existing directory entry to new inode number
1135 ino = new_ip->i_ino;
1136 rc = dtModify(tid, new_dir, &new_dname, &ino,
1137 old_ip->i_ino, JFS_RENAME);
1138 if (rc)
1139 goto out4;
1140 new_ip->i_nlink--;
1141 if (S_ISDIR(new_ip->i_mode)) {
1142 new_ip->i_nlink--;
1143 if (new_ip->i_nlink) {
1144 up(&JFS_IP(new_dir)->commit_sem);
1145 up(&JFS_IP(old_ip)->commit_sem);
1146 if (old_dir != new_dir)
1147 up(&JFS_IP(old_dir)->commit_sem);
1148 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1149 IWRITE_UNLOCK(new_ip);
1150 jfs_error(new_ip->i_sb,
1151 "jfs_rename: new_ip->i_nlink != 0");
1152 return -EIO;
1154 tblk = tid_to_tblock(tid);
1155 tblk->xflag |= COMMIT_DELETE;
1156 tblk->u.ip = new_ip;
1157 } else if (new_ip->i_nlink == 0) {
1158 assert(!test_cflag(COMMIT_Nolink, new_ip));
1159 /* free block resources */
1160 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1161 txAbort(tid, 1); /* Marks FS Dirty */
1162 rc = new_size;
1163 goto out4;
1165 tblk = tid_to_tblock(tid);
1166 tblk->xflag |= COMMIT_DELETE;
1167 tblk->u.ip = new_ip;
1168 } else {
1169 new_ip->i_ctime = CURRENT_TIME;
1170 mark_inode_dirty(new_ip);
1172 } else {
1174 * Add new directory entry
1176 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1177 JFS_CREATE);
1178 if (rc) {
1179 jfs_err("jfs_rename didn't expect dtSearch to fail "
1180 "w/rc = %d", rc);
1181 goto out4;
1184 ino = old_ip->i_ino;
1185 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1186 if (rc) {
1187 if (rc == -EIO)
1188 jfs_err("jfs_rename: dtInsert returned -EIO");
1189 goto out4;
1191 if (S_ISDIR(old_ip->i_mode))
1192 new_dir->i_nlink++;
1195 * Remove old directory entry
1198 ino = old_ip->i_ino;
1199 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1200 if (rc) {
1201 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1202 rc);
1203 txAbort(tid, 1); /* Marks Filesystem dirty */
1204 goto out4;
1206 if (S_ISDIR(old_ip->i_mode)) {
1207 old_dir->i_nlink--;
1208 if (old_dir != new_dir) {
1210 * Change inode number of parent for moved directory
1213 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1214 cpu_to_le32(new_dir->i_ino);
1216 /* Linelock header of dtree */
1217 tlck = txLock(tid, old_ip,
1218 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1219 tlckDTREE | tlckBTROOT | tlckRELINK);
1220 dtlck = (struct dt_lock *) & tlck->lock;
1221 ASSERT(dtlck->index == 0);
1222 lv = & dtlck->lv[0];
1223 lv->offset = 0;
1224 lv->length = 1;
1225 dtlck->index++;
1230 * Update ctime on changed/moved inodes & mark dirty
1232 old_ip->i_ctime = CURRENT_TIME;
1233 mark_inode_dirty(old_ip);
1235 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1236 mark_inode_dirty(new_dir);
1238 /* Build list of inodes modified by this transaction */
1239 ipcount = 0;
1240 iplist[ipcount++] = old_ip;
1241 if (new_ip)
1242 iplist[ipcount++] = new_ip;
1243 iplist[ipcount++] = old_dir;
1245 if (old_dir != new_dir) {
1246 iplist[ipcount++] = new_dir;
1247 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1248 mark_inode_dirty(old_dir);
1252 * Incomplete truncate of file data can
1253 * result in timing problems unless we synchronously commit the
1254 * transaction.
1256 if (new_size)
1257 commit_flag = COMMIT_SYNC;
1258 else
1259 commit_flag = 0;
1261 rc = txCommit(tid, ipcount, iplist, commit_flag);
1263 out4:
1264 txEnd(tid);
1266 up(&JFS_IP(new_dir)->commit_sem);
1267 up(&JFS_IP(old_ip)->commit_sem);
1268 if (old_dir != new_dir)
1269 up(&JFS_IP(old_dir)->commit_sem);
1270 if (new_ip)
1271 up(&JFS_IP(new_ip)->commit_sem);
1273 while (new_size && (rc == 0)) {
1274 tid = txBegin(new_ip->i_sb, 0);
1275 down(&JFS_IP(new_ip)->commit_sem);
1276 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1277 if (new_size < 0) {
1278 txAbort(tid, 1);
1279 rc = new_size;
1280 } else
1281 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1282 txEnd(tid);
1283 up(&JFS_IP(new_ip)->commit_sem);
1285 if (new_ip && (new_ip->i_nlink == 0))
1286 set_cflag(COMMIT_Nolink, new_ip);
1287 out3:
1288 free_UCSname(&new_dname);
1289 out2:
1290 free_UCSname(&old_dname);
1291 out1:
1292 if (new_ip && !S_ISDIR(new_ip->i_mode))
1293 IWRITE_UNLOCK(new_ip);
1295 * Truncating the directory index table is not guaranteed. It
1296 * may need to be done iteratively
1298 if (test_cflag(COMMIT_Stale, old_dir)) {
1299 if (old_dir->i_size > 1)
1300 jfs_truncate_nolock(old_dir, 0);
1302 clear_cflag(COMMIT_Stale, old_dir);
1305 jfs_info("jfs_rename: returning %d", rc);
1306 return rc;
1311 * NAME: jfs_mknod
1313 * FUNCTION: Create a special file (device)
1315 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1316 int mode, dev_t rdev)
1318 struct jfs_inode_info *jfs_ip;
1319 struct btstack btstack;
1320 struct component_name dname;
1321 ino_t ino;
1322 struct inode *ip;
1323 struct inode *iplist[2];
1324 int rc;
1325 tid_t tid;
1326 struct tblock *tblk;
1328 if (!new_valid_dev(rdev))
1329 return -EINVAL;
1331 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1333 if ((rc = get_UCSname(&dname, dentry)))
1334 goto out;
1336 ip = ialloc(dir, mode);
1337 if (ip == NULL) {
1338 rc = -ENOSPC;
1339 goto out1;
1341 jfs_ip = JFS_IP(ip);
1343 tid = txBegin(dir->i_sb, 0);
1345 down(&JFS_IP(dir)->commit_sem);
1346 down(&JFS_IP(ip)->commit_sem);
1348 rc = jfs_init_acl(tid, ip, dir);
1349 if (rc)
1350 goto out3;
1352 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1353 txAbort(tid, 0);
1354 goto out3;
1357 tblk = tid_to_tblock(tid);
1358 tblk->xflag |= COMMIT_CREATE;
1359 tblk->ino = ip->i_ino;
1360 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1362 ino = ip->i_ino;
1363 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1364 txAbort(tid, 0);
1365 goto out3;
1368 ip->i_op = &jfs_file_inode_operations;
1369 jfs_ip->dev = new_encode_dev(rdev);
1370 init_special_inode(ip, ip->i_mode, rdev);
1372 insert_inode_hash(ip);
1373 mark_inode_dirty(ip);
1375 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1377 mark_inode_dirty(dir);
1379 iplist[0] = dir;
1380 iplist[1] = ip;
1381 rc = txCommit(tid, 2, iplist, 0);
1383 out3:
1384 txEnd(tid);
1385 up(&JFS_IP(ip)->commit_sem);
1386 up(&JFS_IP(dir)->commit_sem);
1387 if (rc) {
1388 free_ea_wmap(ip);
1389 ip->i_nlink = 0;
1390 iput(ip);
1391 } else
1392 d_instantiate(dentry, ip);
1394 out1:
1395 free_UCSname(&dname);
1397 out:
1398 jfs_info("jfs_mknod: returning %d", rc);
1399 return rc;
1402 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1404 struct btstack btstack;
1405 ino_t inum;
1406 struct inode *ip;
1407 struct component_name key;
1408 const char *name = dentry->d_name.name;
1409 int len = dentry->d_name.len;
1410 int rc;
1412 jfs_info("jfs_lookup: name = %s", name);
1414 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1415 dentry->d_op = &jfs_ci_dentry_operations;
1417 if ((name[0] == '.') && (len == 1))
1418 inum = dip->i_ino;
1419 else if (strcmp(name, "..") == 0)
1420 inum = PARENT(dip);
1421 else {
1422 if ((rc = get_UCSname(&key, dentry)))
1423 return ERR_PTR(rc);
1424 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1425 free_UCSname(&key);
1426 if (rc == -ENOENT) {
1427 d_add(dentry, NULL);
1428 return ERR_PTR(0);
1429 } else if (rc) {
1430 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1431 return ERR_PTR(rc);
1435 ip = iget(dip->i_sb, inum);
1436 if (ip == NULL || is_bad_inode(ip)) {
1437 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1438 if (ip)
1439 iput(ip);
1440 return ERR_PTR(-EACCES);
1443 dentry = d_splice_alias(ip, dentry);
1445 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1446 dentry->d_op = &jfs_ci_dentry_operations;
1448 return dentry;
1451 struct dentry *jfs_get_parent(struct dentry *dentry)
1453 struct super_block *sb = dentry->d_inode->i_sb;
1454 struct dentry *parent = ERR_PTR(-ENOENT);
1455 struct inode *inode;
1456 unsigned long parent_ino;
1458 parent_ino =
1459 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1460 inode = iget(sb, parent_ino);
1461 if (inode) {
1462 if (is_bad_inode(inode)) {
1463 iput(inode);
1464 parent = ERR_PTR(-EACCES);
1465 } else {
1466 parent = d_alloc_anon(inode);
1467 if (!parent) {
1468 parent = ERR_PTR(-ENOMEM);
1469 iput(inode);
1474 return parent;
1477 struct inode_operations jfs_dir_inode_operations = {
1478 .create = jfs_create,
1479 .lookup = jfs_lookup,
1480 .link = jfs_link,
1481 .unlink = jfs_unlink,
1482 .symlink = jfs_symlink,
1483 .mkdir = jfs_mkdir,
1484 .rmdir = jfs_rmdir,
1485 .mknod = jfs_mknod,
1486 .rename = jfs_rename,
1487 .setxattr = jfs_setxattr,
1488 .getxattr = jfs_getxattr,
1489 .listxattr = jfs_listxattr,
1490 .removexattr = jfs_removexattr,
1491 #ifdef CONFIG_JFS_POSIX_ACL
1492 .setattr = jfs_setattr,
1493 .permission = jfs_permission,
1494 #endif
1497 struct file_operations jfs_dir_operations = {
1498 .read = generic_read_dir,
1499 .readdir = jfs_readdir,
1500 .fsync = jfs_fsync,
1503 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1505 unsigned long hash;
1506 int i;
1508 hash = init_name_hash();
1509 for (i=0; i < this->len; i++)
1510 hash = partial_name_hash(tolower(this->name[i]), hash);
1511 this->hash = end_name_hash(hash);
1513 return 0;
1516 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1518 int i, result = 1;
1520 if (a->len != b->len)
1521 goto out;
1522 for (i=0; i < a->len; i++) {
1523 if (tolower(a->name[i]) != tolower(b->name[i]))
1524 goto out;
1526 result = 0;
1529 * We want creates to preserve case. A negative dentry, a, that
1530 * has a different case than b may cause a new entry to be created
1531 * with the wrong case. Since we can't tell if a comes from a negative
1532 * dentry, we blindly replace it with b. This should be harmless if
1533 * a is not a negative dentry.
1535 memcpy((unsigned char *)a->name, b->name, a->len);
1536 out:
1537 return result;
1540 struct dentry_operations jfs_ci_dentry_operations =
1542 .d_hash = jfs_ci_hash,
1543 .d_compare = jfs_ci_compare,