JFS: Implement jfs_init_security
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jfs / namei.c
blob1abe7343f920494451b3bb5e5165f31af471eac6
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 rc = jfs_init_security(tid, ip, dip);
115 if (rc) {
116 txAbort(tid, 0);
117 goto out3;
120 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
121 jfs_err("jfs_create: dtSearch returned %d", rc);
122 txAbort(tid, 0);
123 goto out3;
126 tblk = tid_to_tblock(tid);
127 tblk->xflag |= COMMIT_CREATE;
128 tblk->ino = ip->i_ino;
129 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
131 iplist[0] = dip;
132 iplist[1] = ip;
135 * initialize the child XAD tree root in-line in inode
137 xtInitRoot(tid, ip);
140 * create entry in parent directory for child directory
141 * (dtInsert() releases parent directory page)
143 ino = ip->i_ino;
144 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
145 if (rc == -EIO) {
146 jfs_err("jfs_create: dtInsert returned -EIO");
147 txAbort(tid, 1); /* Marks Filesystem dirty */
148 } else
149 txAbort(tid, 0); /* Filesystem full */
150 goto out3;
153 ip->i_op = &jfs_file_inode_operations;
154 ip->i_fop = &jfs_file_operations;
155 ip->i_mapping->a_ops = &jfs_aops;
157 insert_inode_hash(ip);
158 mark_inode_dirty(ip);
160 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
162 mark_inode_dirty(dip);
164 rc = txCommit(tid, 2, &iplist[0], 0);
166 out3:
167 txEnd(tid);
168 up(&JFS_IP(dip)->commit_sem);
169 up(&JFS_IP(ip)->commit_sem);
170 if (rc) {
171 free_ea_wmap(ip);
172 ip->i_nlink = 0;
173 iput(ip);
174 } else
175 d_instantiate(dentry, ip);
177 out2:
178 free_UCSname(&dname);
180 out1:
182 jfs_info("jfs_create: rc:%d", rc);
183 return rc;
188 * NAME: jfs_mkdir(dip, dentry, mode)
190 * FUNCTION: create a child directory in the parent directory <dip>
191 * with name = <from dentry> and mode = <mode>
193 * PARAMETER: dip - parent directory vnode
194 * dentry - dentry of child directory
195 * mode - create mode (rwxrwxrwx).
197 * RETURN: Errors from subroutines
199 * note:
200 * EACCESS: user needs search+write permission on the parent directory
202 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
204 int rc = 0;
205 tid_t tid; /* transaction id */
206 struct inode *ip = NULL; /* child directory inode */
207 ino_t ino;
208 struct component_name dname; /* child directory name */
209 struct btstack btstack;
210 struct inode *iplist[2];
211 struct tblock *tblk;
213 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
215 /* link count overflow on parent directory ? */
216 if (dip->i_nlink == JFS_LINK_MAX) {
217 rc = -EMLINK;
218 goto out1;
222 * search parent directory for entry/freespace
223 * (dtSearch() returns parent directory page pinned)
225 if ((rc = get_UCSname(&dname, dentry)))
226 goto out1;
229 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
230 * block there while holding dtree page, so we allocate the inode &
231 * begin the transaction before we search the directory.
233 ip = ialloc(dip, S_IFDIR | mode);
234 if (ip == NULL) {
235 rc = -ENOSPC;
236 goto out2;
239 tid = txBegin(dip->i_sb, 0);
241 down(&JFS_IP(dip)->commit_sem);
242 down(&JFS_IP(ip)->commit_sem);
244 rc = jfs_init_acl(tid, ip, dip);
245 if (rc)
246 goto out3;
248 rc = jfs_init_security(tid, ip, dip);
249 if (rc) {
250 txAbort(tid, 0);
251 goto out3;
254 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
255 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
256 txAbort(tid, 0);
257 goto out3;
260 tblk = tid_to_tblock(tid);
261 tblk->xflag |= COMMIT_CREATE;
262 tblk->ino = ip->i_ino;
263 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
265 iplist[0] = dip;
266 iplist[1] = ip;
269 * initialize the child directory in-line in inode
271 dtInitRoot(tid, ip, dip->i_ino);
274 * create entry in parent directory for child directory
275 * (dtInsert() releases parent directory page)
277 ino = ip->i_ino;
278 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
279 if (rc == -EIO) {
280 jfs_err("jfs_mkdir: dtInsert returned -EIO");
281 txAbort(tid, 1); /* Marks Filesystem dirty */
282 } else
283 txAbort(tid, 0); /* Filesystem full */
284 goto out3;
287 ip->i_nlink = 2; /* for '.' */
288 ip->i_op = &jfs_dir_inode_operations;
289 ip->i_fop = &jfs_dir_operations;
291 insert_inode_hash(ip);
292 mark_inode_dirty(ip);
294 /* update parent directory inode */
295 dip->i_nlink++; /* for '..' from child directory */
296 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
297 mark_inode_dirty(dip);
299 rc = txCommit(tid, 2, &iplist[0], 0);
301 out3:
302 txEnd(tid);
303 up(&JFS_IP(dip)->commit_sem);
304 up(&JFS_IP(ip)->commit_sem);
305 if (rc) {
306 free_ea_wmap(ip);
307 ip->i_nlink = 0;
308 iput(ip);
309 } else
310 d_instantiate(dentry, ip);
312 out2:
313 free_UCSname(&dname);
316 out1:
318 jfs_info("jfs_mkdir: rc:%d", rc);
319 return rc;
323 * NAME: jfs_rmdir(dip, dentry)
325 * FUNCTION: remove a link to child directory
327 * PARAMETER: dip - parent inode
328 * dentry - child directory dentry
330 * RETURN: -EINVAL - if name is . or ..
331 * -EINVAL - if . or .. exist but are invalid.
332 * errors from subroutines
334 * note:
335 * if other threads have the directory open when the last link
336 * is removed, the "." and ".." entries, if present, are removed before
337 * rmdir() returns and no new entries may be created in the directory,
338 * but the directory is not removed until the last reference to
339 * the directory is released (cf.unlink() of regular file).
341 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
343 int rc;
344 tid_t tid; /* transaction id */
345 struct inode *ip = dentry->d_inode;
346 ino_t ino;
347 struct component_name dname;
348 struct inode *iplist[2];
349 struct tblock *tblk;
351 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
353 /* Init inode for quota operations. */
354 DQUOT_INIT(ip);
356 /* directory must be empty to be removed */
357 if (!dtEmpty(ip)) {
358 rc = -ENOTEMPTY;
359 goto out;
362 if ((rc = get_UCSname(&dname, dentry))) {
363 goto out;
366 tid = txBegin(dip->i_sb, 0);
368 down(&JFS_IP(dip)->commit_sem);
369 down(&JFS_IP(ip)->commit_sem);
371 iplist[0] = dip;
372 iplist[1] = ip;
374 tblk = tid_to_tblock(tid);
375 tblk->xflag |= COMMIT_DELETE;
376 tblk->u.ip = ip;
379 * delete the entry of target directory from parent directory
381 ino = ip->i_ino;
382 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
383 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
384 if (rc == -EIO)
385 txAbort(tid, 1);
386 txEnd(tid);
387 up(&JFS_IP(dip)->commit_sem);
388 up(&JFS_IP(ip)->commit_sem);
390 goto out2;
393 /* update parent directory's link count corresponding
394 * to ".." entry of the target directory deleted
396 dip->i_nlink--;
397 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
398 mark_inode_dirty(dip);
401 * OS/2 could have created EA and/or ACL
403 /* free EA from both persistent and working map */
404 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
405 /* free EA pages */
406 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
408 JFS_IP(ip)->ea.flag = 0;
410 /* free ACL from both persistent and working map */
411 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
412 /* free ACL pages */
413 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
415 JFS_IP(ip)->acl.flag = 0;
417 /* mark the target directory as deleted */
418 ip->i_nlink = 0;
419 mark_inode_dirty(ip);
421 rc = txCommit(tid, 2, &iplist[0], 0);
423 txEnd(tid);
425 up(&JFS_IP(dip)->commit_sem);
426 up(&JFS_IP(ip)->commit_sem);
429 * Truncating the directory index table is not guaranteed. It
430 * may need to be done iteratively
432 if (test_cflag(COMMIT_Stale, dip)) {
433 if (dip->i_size > 1)
434 jfs_truncate_nolock(dip, 0);
436 clear_cflag(COMMIT_Stale, dip);
439 out2:
440 free_UCSname(&dname);
442 out:
443 jfs_info("jfs_rmdir: rc:%d", rc);
444 return rc;
448 * NAME: jfs_unlink(dip, dentry)
450 * FUNCTION: remove a link to object <vp> named by <name>
451 * from parent directory <dvp>
453 * PARAMETER: dip - inode of parent directory
454 * dentry - dentry of object to be removed
456 * RETURN: errors from subroutines
458 * note:
459 * temporary file: if one or more processes have the file open
460 * when the last link is removed, the link will be removed before
461 * unlink() returns, but the removal of the file contents will be
462 * postponed until all references to the files are closed.
464 * JFS does NOT support unlink() on directories.
467 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
469 int rc;
470 tid_t tid; /* transaction id */
471 struct inode *ip = dentry->d_inode;
472 ino_t ino;
473 struct component_name dname; /* object name */
474 struct inode *iplist[2];
475 struct tblock *tblk;
476 s64 new_size = 0;
477 int commit_flag;
479 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
481 /* Init inode for quota operations. */
482 DQUOT_INIT(ip);
484 if ((rc = get_UCSname(&dname, dentry)))
485 goto out;
487 IWRITE_LOCK(ip);
489 tid = txBegin(dip->i_sb, 0);
491 down(&JFS_IP(dip)->commit_sem);
492 down(&JFS_IP(ip)->commit_sem);
494 iplist[0] = dip;
495 iplist[1] = ip;
498 * delete the entry of target file from parent directory
500 ino = ip->i_ino;
501 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
502 jfs_err("jfs_unlink: dtDelete returned %d", rc);
503 if (rc == -EIO)
504 txAbort(tid, 1); /* Marks FS Dirty */
505 txEnd(tid);
506 up(&JFS_IP(dip)->commit_sem);
507 up(&JFS_IP(ip)->commit_sem);
508 IWRITE_UNLOCK(ip);
509 goto out1;
512 ASSERT(ip->i_nlink);
514 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
515 mark_inode_dirty(dip);
517 /* update target's inode */
518 ip->i_nlink--;
519 mark_inode_dirty(ip);
522 * commit zero link count object
524 if (ip->i_nlink == 0) {
525 assert(!test_cflag(COMMIT_Nolink, ip));
526 /* free block resources */
527 if ((new_size = commitZeroLink(tid, ip)) < 0) {
528 txAbort(tid, 1); /* Marks FS Dirty */
529 txEnd(tid);
530 up(&JFS_IP(dip)->commit_sem);
531 up(&JFS_IP(ip)->commit_sem);
532 IWRITE_UNLOCK(ip);
533 rc = new_size;
534 goto out1;
536 tblk = tid_to_tblock(tid);
537 tblk->xflag |= COMMIT_DELETE;
538 tblk->u.ip = ip;
542 * Incomplete truncate of file data can
543 * result in timing problems unless we synchronously commit the
544 * transaction.
546 if (new_size)
547 commit_flag = COMMIT_SYNC;
548 else
549 commit_flag = 0;
552 * If xtTruncate was incomplete, commit synchronously to avoid
553 * timing complications
555 rc = txCommit(tid, 2, &iplist[0], commit_flag);
557 txEnd(tid);
559 up(&JFS_IP(dip)->commit_sem);
560 up(&JFS_IP(ip)->commit_sem);
563 while (new_size && (rc == 0)) {
564 tid = txBegin(dip->i_sb, 0);
565 down(&JFS_IP(ip)->commit_sem);
566 new_size = xtTruncate_pmap(tid, ip, new_size);
567 if (new_size < 0) {
568 txAbort(tid, 1); /* Marks FS Dirty */
569 rc = new_size;
570 } else
571 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
572 txEnd(tid);
573 up(&JFS_IP(ip)->commit_sem);
576 if (ip->i_nlink == 0)
577 set_cflag(COMMIT_Nolink, ip);
579 IWRITE_UNLOCK(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)) {
586 if (dip->i_size > 1)
587 jfs_truncate_nolock(dip, 0);
589 clear_cflag(COMMIT_Stale, dip);
592 out1:
593 free_UCSname(&dname);
594 out:
595 jfs_info("jfs_unlink: rc:%d", rc);
596 return 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
605 * one of these.
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
611 * map by ctrunc1.
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)
625 int filetype;
626 struct tblock *tblk;
628 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
630 filetype = ip->i_mode & S_IFMT;
631 switch (filetype) {
632 case S_IFREG:
633 break;
634 case S_IFLNK:
635 /* fast symbolic link */
636 if (ip->i_size < IDATASIZE) {
637 ip->i_size = 0;
638 return 0;
640 break;
641 default:
642 assert(filetype != S_IFDIR);
643 return 0;
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;
653 * free EA
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);
660 * free ACL
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;
672 if (ip->i_size)
673 return xtTruncate_pmap(tid, ip, 0);
675 return 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)
691 int type;
693 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
695 /* return if not reg or symbolic link or if size is
696 * already ok.
698 type = ip->i_mode & S_IFMT;
700 switch (type) {
701 case S_IFREG:
702 break;
703 case S_IFLNK:
704 /* if its contained in inode nothing to do */
705 if (ip->i_size < IDATASIZE)
706 return;
707 break;
708 default:
709 return;
713 * free EA
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 */
725 maplock.index = 1;
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);
734 * free ACL
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 */
745 maplock.index = 1;
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;
758 if (ip->i_size)
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
771 * crp - credential
773 * RETURN: Errors from subroutines
775 * note:
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)
789 int rc;
790 tid_t tid;
791 struct inode *ip = old_dentry->d_inode;
792 ino_t ino;
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)
801 return -EMLINK;
803 if (ip->i_nlink == 0)
804 return -ENOENT;
806 tid = txBegin(ip->i_sb, 0);
808 down(&JFS_IP(dir)->commit_sem);
809 down(&JFS_IP(ip)->commit_sem);
812 * scan parent directory for entry/freespace
814 if ((rc = get_UCSname(&dname, dentry)))
815 goto out;
817 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
818 goto free_dname;
821 * create entry for new link in parent directory
823 ino = ip->i_ino;
824 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
825 goto free_dname;
827 /* update object inode */
828 ip->i_nlink++; /* for new link */
829 ip->i_ctime = CURRENT_TIME;
830 mark_inode_dirty(dir);
831 atomic_inc(&ip->i_count);
833 iplist[0] = ip;
834 iplist[1] = dir;
835 rc = txCommit(tid, 2, &iplist[0], 0);
837 if (rc) {
838 ip->i_nlink--;
839 iput(ip);
840 } else
841 d_instantiate(dentry, ip);
843 free_dname:
844 free_UCSname(&dname);
846 out:
847 txEnd(tid);
849 up(&JFS_IP(dir)->commit_sem);
850 up(&JFS_IP(ip)->commit_sem);
852 jfs_info("jfs_link: rc:%d", rc);
853 return rc;
857 * NAME: jfs_symlink(dip, dentry, name)
859 * FUNCTION: creates a symbolic link to <symlink> by name <name>
860 * in directory <dip>
862 * PARAMETER: dip - parent directory vnode
863 * dentry - dentry of symbolic link
864 * name - the path name of the existing object
865 * that will be the source of the link
867 * RETURN: errors from subroutines
869 * note:
870 * ENAMETOOLONG: pathname resolution of a symbolic link produced
871 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
874 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
875 const char *name)
877 int rc;
878 tid_t tid;
879 ino_t ino = 0;
880 struct component_name dname;
881 int ssize; /* source pathname size */
882 struct btstack btstack;
883 struct inode *ip = dentry->d_inode;
884 unchar *i_fastsymlink;
885 s64 xlen = 0;
886 int bmask = 0, xsize;
887 s64 extent = 0, xaddr;
888 struct metapage *mp;
889 struct super_block *sb;
890 struct tblock *tblk;
892 struct inode *iplist[2];
894 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
896 ssize = strlen(name) + 1;
899 * search parent directory for entry/freespace
900 * (dtSearch() returns parent directory page pinned)
903 if ((rc = get_UCSname(&dname, dentry)))
904 goto out1;
907 * allocate on-disk/in-memory inode for symbolic link:
908 * (iAlloc() returns new, locked inode)
910 ip = ialloc(dip, S_IFLNK | 0777);
911 if (ip == NULL) {
912 rc = -ENOSPC;
913 goto out2;
916 tid = txBegin(dip->i_sb, 0);
918 down(&JFS_IP(dip)->commit_sem);
919 down(&JFS_IP(ip)->commit_sem);
921 rc = jfs_init_security(tid, ip, dip);
922 if (rc)
923 goto out3;
925 tblk = tid_to_tblock(tid);
926 tblk->xflag |= COMMIT_CREATE;
927 tblk->ino = ip->i_ino;
928 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
930 /* fix symlink access permission
931 * (dir_create() ANDs in the u.u_cmask,
932 * but symlinks really need to be 777 access)
934 ip->i_mode |= 0777;
937 * write symbolic link target path name
939 xtInitRoot(tid, ip);
942 * write source path name inline in on-disk inode (fast symbolic link)
945 if (ssize <= IDATASIZE) {
946 ip->i_op = &jfs_symlink_inode_operations;
948 i_fastsymlink = JFS_IP(ip)->i_inline;
949 memcpy(i_fastsymlink, name, ssize);
950 ip->i_size = ssize - 1;
953 * if symlink is > 128 bytes, we don't have the space to
954 * store inline extended attributes
956 if (ssize > sizeof (JFS_IP(ip)->i_inline))
957 JFS_IP(ip)->mode2 &= ~INLINEEA;
959 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
960 ssize, name);
963 * write source path name in a single extent
965 else {
966 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
968 ip->i_op = &page_symlink_inode_operations;
969 ip->i_mapping->a_ops = &jfs_aops;
972 * even though the data of symlink object (source
973 * path name) is treated as non-journaled user data,
974 * it is read/written thru buffer cache for performance.
976 sb = ip->i_sb;
977 bmask = JFS_SBI(sb)->bsize - 1;
978 xsize = (ssize + bmask) & ~bmask;
979 xaddr = 0;
980 xlen = xsize >> JFS_SBI(sb)->l2bsize;
981 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
982 txAbort(tid, 0);
983 rc = -ENOSPC;
984 goto out3;
986 extent = xaddr;
987 ip->i_size = ssize - 1;
988 while (ssize) {
989 /* This is kind of silly since PATH_MAX == 4K */
990 int copy_size = min(ssize, PSIZE);
992 mp = get_metapage(ip, xaddr, PSIZE, 1);
994 if (mp == NULL) {
995 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
996 rc = -EIO;
997 txAbort(tid, 0);
998 goto out3;
1000 memcpy(mp->data, name, copy_size);
1001 flush_metapage(mp);
1002 ssize -= copy_size;
1003 name += copy_size;
1004 xaddr += JFS_SBI(sb)->nbperpage;
1009 * create entry for symbolic link in parent directory
1011 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1012 if (rc == 0) {
1013 ino = ip->i_ino;
1014 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1016 if (rc) {
1017 if (xlen)
1018 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1019 txAbort(tid, 0);
1020 /* discard new inode */
1021 goto out3;
1024 insert_inode_hash(ip);
1025 mark_inode_dirty(ip);
1028 * commit update of parent directory and link object
1031 iplist[0] = dip;
1032 iplist[1] = ip;
1033 rc = txCommit(tid, 2, &iplist[0], 0);
1035 out3:
1036 txEnd(tid);
1037 up(&JFS_IP(dip)->commit_sem);
1038 up(&JFS_IP(ip)->commit_sem);
1039 if (rc) {
1040 free_ea_wmap(ip);
1041 ip->i_nlink = 0;
1042 iput(ip);
1043 } else
1044 d_instantiate(dentry, ip);
1046 out2:
1047 free_UCSname(&dname);
1049 out1:
1050 jfs_info("jfs_symlink: rc:%d", rc);
1051 return rc;
1056 * NAME: jfs_rename
1058 * FUNCTION: rename a file or directory
1060 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1061 struct inode *new_dir, struct dentry *new_dentry)
1063 struct btstack btstack;
1064 ino_t ino;
1065 struct component_name new_dname;
1066 struct inode *new_ip;
1067 struct component_name old_dname;
1068 struct inode *old_ip;
1069 int rc;
1070 tid_t tid;
1071 struct tlock *tlck;
1072 struct dt_lock *dtlck;
1073 struct lv *lv;
1074 int ipcount;
1075 struct inode *iplist[4];
1076 struct tblock *tblk;
1077 s64 new_size = 0;
1078 int commit_flag;
1081 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1082 new_dentry->d_name.name);
1084 old_ip = old_dentry->d_inode;
1085 new_ip = new_dentry->d_inode;
1087 if ((rc = get_UCSname(&old_dname, old_dentry)))
1088 goto out1;
1090 if ((rc = get_UCSname(&new_dname, new_dentry)))
1091 goto out2;
1094 * Make sure source inode number is what we think it is
1096 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1097 if (rc || (ino != old_ip->i_ino)) {
1098 rc = -ENOENT;
1099 goto out3;
1103 * Make sure dest inode number (if any) is what we think it is
1105 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1106 if (rc == 0) {
1107 if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1108 rc = -ESTALE;
1109 goto out3;
1111 } else if (rc != -ENOENT)
1112 goto out3;
1113 else if (new_ip) {
1114 /* no entry exists, but one was expected */
1115 rc = -ESTALE;
1116 goto out3;
1119 if (S_ISDIR(old_ip->i_mode)) {
1120 if (new_ip) {
1121 if (!dtEmpty(new_ip)) {
1122 rc = -ENOTEMPTY;
1123 goto out3;
1125 } else if ((new_dir != old_dir) &&
1126 (new_dir->i_nlink == JFS_LINK_MAX)) {
1127 rc = -EMLINK;
1128 goto out3;
1130 } else if (new_ip) {
1131 IWRITE_LOCK(new_ip);
1132 /* Init inode for quota operations. */
1133 DQUOT_INIT(new_ip);
1137 * The real work starts here
1139 tid = txBegin(new_dir->i_sb, 0);
1141 down(&JFS_IP(new_dir)->commit_sem);
1142 down(&JFS_IP(old_ip)->commit_sem);
1143 if (old_dir != new_dir)
1144 down(&JFS_IP(old_dir)->commit_sem);
1146 if (new_ip) {
1147 down(&JFS_IP(new_ip)->commit_sem);
1149 * Change existing directory entry to new inode number
1151 ino = new_ip->i_ino;
1152 rc = dtModify(tid, new_dir, &new_dname, &ino,
1153 old_ip->i_ino, JFS_RENAME);
1154 if (rc)
1155 goto out4;
1156 new_ip->i_nlink--;
1157 if (S_ISDIR(new_ip->i_mode)) {
1158 new_ip->i_nlink--;
1159 if (new_ip->i_nlink) {
1160 up(&JFS_IP(new_dir)->commit_sem);
1161 up(&JFS_IP(old_ip)->commit_sem);
1162 if (old_dir != new_dir)
1163 up(&JFS_IP(old_dir)->commit_sem);
1164 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1165 IWRITE_UNLOCK(new_ip);
1166 jfs_error(new_ip->i_sb,
1167 "jfs_rename: new_ip->i_nlink != 0");
1168 return -EIO;
1170 tblk = tid_to_tblock(tid);
1171 tblk->xflag |= COMMIT_DELETE;
1172 tblk->u.ip = new_ip;
1173 } else if (new_ip->i_nlink == 0) {
1174 assert(!test_cflag(COMMIT_Nolink, new_ip));
1175 /* free block resources */
1176 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1177 txAbort(tid, 1); /* Marks FS Dirty */
1178 rc = new_size;
1179 goto out4;
1181 tblk = tid_to_tblock(tid);
1182 tblk->xflag |= COMMIT_DELETE;
1183 tblk->u.ip = new_ip;
1184 } else {
1185 new_ip->i_ctime = CURRENT_TIME;
1186 mark_inode_dirty(new_ip);
1188 } else {
1190 * Add new directory entry
1192 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1193 JFS_CREATE);
1194 if (rc) {
1195 jfs_err("jfs_rename didn't expect dtSearch to fail "
1196 "w/rc = %d", rc);
1197 goto out4;
1200 ino = old_ip->i_ino;
1201 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1202 if (rc) {
1203 if (rc == -EIO)
1204 jfs_err("jfs_rename: dtInsert returned -EIO");
1205 goto out4;
1207 if (S_ISDIR(old_ip->i_mode))
1208 new_dir->i_nlink++;
1211 * Remove old directory entry
1214 ino = old_ip->i_ino;
1215 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1216 if (rc) {
1217 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1218 rc);
1219 txAbort(tid, 1); /* Marks Filesystem dirty */
1220 goto out4;
1222 if (S_ISDIR(old_ip->i_mode)) {
1223 old_dir->i_nlink--;
1224 if (old_dir != new_dir) {
1226 * Change inode number of parent for moved directory
1229 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1230 cpu_to_le32(new_dir->i_ino);
1232 /* Linelock header of dtree */
1233 tlck = txLock(tid, old_ip,
1234 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1235 tlckDTREE | tlckBTROOT | tlckRELINK);
1236 dtlck = (struct dt_lock *) & tlck->lock;
1237 ASSERT(dtlck->index == 0);
1238 lv = & dtlck->lv[0];
1239 lv->offset = 0;
1240 lv->length = 1;
1241 dtlck->index++;
1246 * Update ctime on changed/moved inodes & mark dirty
1248 old_ip->i_ctime = CURRENT_TIME;
1249 mark_inode_dirty(old_ip);
1251 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1252 mark_inode_dirty(new_dir);
1254 /* Build list of inodes modified by this transaction */
1255 ipcount = 0;
1256 iplist[ipcount++] = old_ip;
1257 if (new_ip)
1258 iplist[ipcount++] = new_ip;
1259 iplist[ipcount++] = old_dir;
1261 if (old_dir != new_dir) {
1262 iplist[ipcount++] = new_dir;
1263 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1264 mark_inode_dirty(old_dir);
1268 * Incomplete truncate of file data can
1269 * result in timing problems unless we synchronously commit the
1270 * transaction.
1272 if (new_size)
1273 commit_flag = COMMIT_SYNC;
1274 else
1275 commit_flag = 0;
1277 rc = txCommit(tid, ipcount, iplist, commit_flag);
1279 out4:
1280 txEnd(tid);
1282 up(&JFS_IP(new_dir)->commit_sem);
1283 up(&JFS_IP(old_ip)->commit_sem);
1284 if (old_dir != new_dir)
1285 up(&JFS_IP(old_dir)->commit_sem);
1286 if (new_ip)
1287 up(&JFS_IP(new_ip)->commit_sem);
1289 while (new_size && (rc == 0)) {
1290 tid = txBegin(new_ip->i_sb, 0);
1291 down(&JFS_IP(new_ip)->commit_sem);
1292 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1293 if (new_size < 0) {
1294 txAbort(tid, 1);
1295 rc = new_size;
1296 } else
1297 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1298 txEnd(tid);
1299 up(&JFS_IP(new_ip)->commit_sem);
1301 if (new_ip && (new_ip->i_nlink == 0))
1302 set_cflag(COMMIT_Nolink, new_ip);
1303 out3:
1304 free_UCSname(&new_dname);
1305 out2:
1306 free_UCSname(&old_dname);
1307 out1:
1308 if (new_ip && !S_ISDIR(new_ip->i_mode))
1309 IWRITE_UNLOCK(new_ip);
1311 * Truncating the directory index table is not guaranteed. It
1312 * may need to be done iteratively
1314 if (test_cflag(COMMIT_Stale, old_dir)) {
1315 if (old_dir->i_size > 1)
1316 jfs_truncate_nolock(old_dir, 0);
1318 clear_cflag(COMMIT_Stale, old_dir);
1321 jfs_info("jfs_rename: returning %d", rc);
1322 return rc;
1327 * NAME: jfs_mknod
1329 * FUNCTION: Create a special file (device)
1331 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1332 int mode, dev_t rdev)
1334 struct jfs_inode_info *jfs_ip;
1335 struct btstack btstack;
1336 struct component_name dname;
1337 ino_t ino;
1338 struct inode *ip;
1339 struct inode *iplist[2];
1340 int rc;
1341 tid_t tid;
1342 struct tblock *tblk;
1344 if (!new_valid_dev(rdev))
1345 return -EINVAL;
1347 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1349 if ((rc = get_UCSname(&dname, dentry)))
1350 goto out;
1352 ip = ialloc(dir, mode);
1353 if (ip == NULL) {
1354 rc = -ENOSPC;
1355 goto out1;
1357 jfs_ip = JFS_IP(ip);
1359 tid = txBegin(dir->i_sb, 0);
1361 down(&JFS_IP(dir)->commit_sem);
1362 down(&JFS_IP(ip)->commit_sem);
1364 rc = jfs_init_acl(tid, ip, dir);
1365 if (rc)
1366 goto out3;
1368 rc = jfs_init_security(tid, ip, dir);
1369 if (rc) {
1370 txAbort(tid, 0);
1371 goto out3;
1374 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1375 txAbort(tid, 0);
1376 goto out3;
1379 tblk = tid_to_tblock(tid);
1380 tblk->xflag |= COMMIT_CREATE;
1381 tblk->ino = ip->i_ino;
1382 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1384 ino = ip->i_ino;
1385 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1386 txAbort(tid, 0);
1387 goto out3;
1390 ip->i_op = &jfs_file_inode_operations;
1391 jfs_ip->dev = new_encode_dev(rdev);
1392 init_special_inode(ip, ip->i_mode, rdev);
1394 insert_inode_hash(ip);
1395 mark_inode_dirty(ip);
1397 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1399 mark_inode_dirty(dir);
1401 iplist[0] = dir;
1402 iplist[1] = ip;
1403 rc = txCommit(tid, 2, iplist, 0);
1405 out3:
1406 txEnd(tid);
1407 up(&JFS_IP(ip)->commit_sem);
1408 up(&JFS_IP(dir)->commit_sem);
1409 if (rc) {
1410 free_ea_wmap(ip);
1411 ip->i_nlink = 0;
1412 iput(ip);
1413 } else
1414 d_instantiate(dentry, ip);
1416 out1:
1417 free_UCSname(&dname);
1419 out:
1420 jfs_info("jfs_mknod: returning %d", rc);
1421 return rc;
1424 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1426 struct btstack btstack;
1427 ino_t inum;
1428 struct inode *ip;
1429 struct component_name key;
1430 const char *name = dentry->d_name.name;
1431 int len = dentry->d_name.len;
1432 int rc;
1434 jfs_info("jfs_lookup: name = %s", name);
1436 if (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2)
1437 dentry->d_op = &jfs_ci_dentry_operations;
1439 if ((name[0] == '.') && (len == 1))
1440 inum = dip->i_ino;
1441 else if (strcmp(name, "..") == 0)
1442 inum = PARENT(dip);
1443 else {
1444 if ((rc = get_UCSname(&key, dentry)))
1445 return ERR_PTR(rc);
1446 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1447 free_UCSname(&key);
1448 if (rc == -ENOENT) {
1449 d_add(dentry, NULL);
1450 return ERR_PTR(0);
1451 } else if (rc) {
1452 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1453 return ERR_PTR(rc);
1457 ip = iget(dip->i_sb, inum);
1458 if (ip == NULL || is_bad_inode(ip)) {
1459 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1460 if (ip)
1461 iput(ip);
1462 return ERR_PTR(-EACCES);
1465 dentry = d_splice_alias(ip, dentry);
1467 if (dentry && (JFS_SBI(dip->i_sb)->mntflag & JFS_OS2))
1468 dentry->d_op = &jfs_ci_dentry_operations;
1470 return dentry;
1473 struct dentry *jfs_get_parent(struct dentry *dentry)
1475 struct super_block *sb = dentry->d_inode->i_sb;
1476 struct dentry *parent = ERR_PTR(-ENOENT);
1477 struct inode *inode;
1478 unsigned long parent_ino;
1480 parent_ino =
1481 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1482 inode = iget(sb, parent_ino);
1483 if (inode) {
1484 if (is_bad_inode(inode)) {
1485 iput(inode);
1486 parent = ERR_PTR(-EACCES);
1487 } else {
1488 parent = d_alloc_anon(inode);
1489 if (!parent) {
1490 parent = ERR_PTR(-ENOMEM);
1491 iput(inode);
1496 return parent;
1499 struct inode_operations jfs_dir_inode_operations = {
1500 .create = jfs_create,
1501 .lookup = jfs_lookup,
1502 .link = jfs_link,
1503 .unlink = jfs_unlink,
1504 .symlink = jfs_symlink,
1505 .mkdir = jfs_mkdir,
1506 .rmdir = jfs_rmdir,
1507 .mknod = jfs_mknod,
1508 .rename = jfs_rename,
1509 .setxattr = jfs_setxattr,
1510 .getxattr = jfs_getxattr,
1511 .listxattr = jfs_listxattr,
1512 .removexattr = jfs_removexattr,
1513 #ifdef CONFIG_JFS_POSIX_ACL
1514 .setattr = jfs_setattr,
1515 .permission = jfs_permission,
1516 #endif
1519 struct file_operations jfs_dir_operations = {
1520 .read = generic_read_dir,
1521 .readdir = jfs_readdir,
1522 .fsync = jfs_fsync,
1525 static int jfs_ci_hash(struct dentry *dir, struct qstr *this)
1527 unsigned long hash;
1528 int i;
1530 hash = init_name_hash();
1531 for (i=0; i < this->len; i++)
1532 hash = partial_name_hash(tolower(this->name[i]), hash);
1533 this->hash = end_name_hash(hash);
1535 return 0;
1538 static int jfs_ci_compare(struct dentry *dir, struct qstr *a, struct qstr *b)
1540 int i, result = 1;
1542 if (a->len != b->len)
1543 goto out;
1544 for (i=0; i < a->len; i++) {
1545 if (tolower(a->name[i]) != tolower(b->name[i]))
1546 goto out;
1548 result = 0;
1551 * We want creates to preserve case. A negative dentry, a, that
1552 * has a different case than b may cause a new entry to be created
1553 * with the wrong case. Since we can't tell if a comes from a negative
1554 * dentry, we blindly replace it with b. This should be harmless if
1555 * a is not a negative dentry.
1557 memcpy((unsigned char *)a->name, b->name, a->len);
1558 out:
1559 return result;
1562 struct dentry_operations jfs_ci_dentry_operations =
1564 .d_hash = jfs_ci_hash,
1565 .d_compare = jfs_ci_compare,