2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "xfs_trans.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
45 #include "xfs_itable.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_utils.h"
54 #include <linux/capability.h>
55 #include <linux/xattr.h>
56 #include <linux/namei.h>
57 #include <linux/security.h>
59 #define IS_NOATIME(inode) ((inode->i_sb->s_flags & MS_NOATIME) || \
60 (S_ISDIR(inode->i_mode) && inode->i_sb->s_flags & MS_NODIRATIME))
63 * Get a XFS inode from a given vnode.
71 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
72 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
73 if (unlikely(bdp == NULL))
75 return XFS_BHVTOI(bdp);
79 * Bring the atime in the XFS inode uptodate.
80 * Used before logging the inode to disk or when the Linux inode goes away.
83 xfs_synchronize_atime(
88 vp = XFS_ITOV_NULL(ip);
90 struct inode *inode = &vp->v_inode;
91 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
92 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
97 * Change the requested timestamp in the given inode.
98 * We don't lock across timestamp updates, and we don't log them but
99 * we do record the fact that there is dirty information in core.
101 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
102 * with XFS_ICHGTIME_ACC to be sure that access time
103 * update will take. Calling first with XFS_ICHGTIME_ACC
104 * and then XFS_ICHGTIME_MOD may fail to modify the access
105 * timestamp if the filesystem is mounted noacctm.
112 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
116 if (flags & XFS_ICHGTIME_MOD) {
118 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
119 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
121 if (flags & XFS_ICHGTIME_ACC) {
123 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
124 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
126 if (flags & XFS_ICHGTIME_CHG) {
128 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
129 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
133 * We update the i_update_core field _after_ changing
134 * the timestamps in order to coordinate properly with
135 * xfs_iflush() so that we don't lose timestamp updates.
136 * This keeps us from having to hold the inode lock
137 * while doing this. We use the SYNCHRONIZE macro to
138 * ensure that the compiler does not reorder the update
139 * of i_update_core above the timestamp updates above.
142 ip->i_update_core = 1;
143 if (!(inode->i_state & I_LOCK))
144 mark_inode_dirty_sync(inode);
148 * Variant on the above which avoids querying the system clock
149 * in situations where we know the Linux inode timestamps have
150 * just been updated (and so we can update our inode cheaply).
161 * Atime updates for read() & friends are handled lazily now, and
162 * explicit updates must go through xfs_ichgtime()
164 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
167 * We're not supposed to change timestamps in readonly-mounted
168 * filesystems. Throw it away if anyone asks us.
170 if (unlikely(IS_RDONLY(inode)))
173 if (flags & XFS_ICHGTIME_MOD) {
174 tvp = &inode->i_mtime;
175 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
176 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
178 if (flags & XFS_ICHGTIME_CHG) {
179 tvp = &inode->i_ctime;
180 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
181 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
185 * We update the i_update_core field _after_ changing
186 * the timestamps in order to coordinate properly with
187 * xfs_iflush() so that we don't lose timestamp updates.
188 * This keeps us from having to hold the inode lock
189 * while doing this. We use the SYNCHRONIZE macro to
190 * ensure that the compiler does not reorder the update
191 * of i_update_core above the timestamp updates above.
194 ip->i_update_core = 1;
195 if (!(inode->i_state & I_LOCK))
196 mark_inode_dirty_sync(inode);
201 * Pull the link count and size up from the xfs inode to the linux inode
207 vnode_t *vp = LINVFS_GET_VP(ip);
211 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
212 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
213 if (likely(!error)) {
214 ip->i_nlink = va.va_nlink;
215 ip->i_blocks = va.va_nblocks;
217 /* we're under i_mutex so i_size can't change under us */
218 if (i_size_read(ip) != va.va_size)
219 i_size_write(ip, va.va_size);
224 * Hook in SELinux. This is not quite correct yet, what we really need
225 * here (as we do for default ACLs) is a mechanism by which creation of
226 * these attrs can be journalled at inode creation time (along with the
227 * inode, of course, such that log replay can't cause these to be lost).
230 linvfs_init_security(
234 struct inode *ip = LINVFS_GET_IP(vp);
240 error = security_inode_init_security(ip, dir, &name, &value, &length);
242 if (error == -EOPNOTSUPP)
247 VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
257 * Determine whether a process has a valid fs_struct (kernel daemons
258 * like knfsd don't have an fs_struct).
260 * XXX(hch): nfsd is broken, better fix it instead.
263 has_fs_struct(struct task_struct *task)
265 return (task->fs != init_task.fs);
271 struct dentry *dentry,
277 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
278 xfs_acl_t *default_acl = NULL;
279 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
283 * Irix uses Missed'em'V split, but doesn't want to see
284 * the upper 5 bits of (14bit) major.
286 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
289 if (test_default_acl && test_default_acl(dvp)) {
290 if (!_ACL_ALLOC(default_acl))
292 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
293 _ACL_FREE(default_acl);
298 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
299 mode &= ~current->fs->umask;
301 memset(&va, 0, sizeof(va));
302 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
305 switch (mode & S_IFMT) {
306 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
307 va.va_rdev = sysv_encode_dev(rdev);
308 va.va_mask |= XFS_AT_RDEV;
311 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
314 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
322 error = linvfs_init_security(vp, dir);
326 error = _ACL_INHERIT(vp, &va, default_acl);
330 struct dentry teardown = {};
334 * If we can't add the ACL we must back out.
335 * ENOSPC can hit here, among other things.
337 teardown.d_inode = ip = LINVFS_GET_IP(vp);
338 teardown.d_name = dentry->d_name;
341 VOP_RMDIR(dvp, &teardown, NULL, err2);
343 VOP_REMOVE(dvp, &teardown, NULL, err2);
347 _ACL_FREE(default_acl);
352 ip = LINVFS_GET_IP(vp);
354 if (S_ISCHR(mode) || S_ISBLK(mode))
356 else if (S_ISDIR(mode))
358 d_instantiate(dentry, ip);
359 validate_fields(dir);
367 struct dentry *dentry,
369 struct nameidata *nd)
371 return linvfs_mknod(dir, dentry, mode, 0);
377 struct dentry *dentry,
380 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
383 STATIC struct dentry *
386 struct dentry *dentry,
387 struct nameidata *nd)
389 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
392 if (dentry->d_name.len >= MAXNAMELEN)
393 return ERR_PTR(-ENAMETOOLONG);
395 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
397 if (unlikely(error != ENOENT))
398 return ERR_PTR(-error);
403 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
408 struct dentry *old_dentry,
410 struct dentry *dentry)
412 struct inode *ip; /* inode of guy being linked to */
413 vnode_t *tdvp; /* target directory for new name/link */
414 vnode_t *vp; /* vp of name being linked */
417 ip = old_dentry->d_inode; /* inode being linked to */
418 if (S_ISDIR(ip->i_mode))
421 tdvp = LINVFS_GET_VP(dir);
422 vp = LINVFS_GET_VP(ip);
424 VOP_LINK(tdvp, vp, dentry, NULL, error);
429 d_instantiate(dentry, ip);
437 struct dentry *dentry)
440 vnode_t *dvp; /* directory containing name to remove */
443 inode = dentry->d_inode;
444 dvp = LINVFS_GET_VP(dir);
446 VOP_REMOVE(dvp, dentry, NULL, error);
448 validate_fields(dir); /* For size only */
449 validate_fields(inode);
458 struct dentry *dentry,
463 vnode_t *dvp; /* directory containing name of symlink */
464 vnode_t *cvp; /* used to lookup symlink to put in dentry */
467 dvp = LINVFS_GET_VP(dir);
470 memset(&va, 0, sizeof(va));
471 va.va_mode = S_IFLNK |
472 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
473 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
476 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
478 ip = LINVFS_GET_IP(cvp);
479 d_instantiate(dentry, ip);
480 validate_fields(dir);
481 validate_fields(ip); /* size needs update */
489 struct dentry *dentry)
491 struct inode *inode = dentry->d_inode;
492 vnode_t *dvp = LINVFS_GET_VP(dir);
495 VOP_RMDIR(dvp, dentry, NULL, error);
497 validate_fields(inode);
498 validate_fields(dir);
506 struct dentry *odentry,
508 struct dentry *ndentry)
510 struct inode *new_inode = ndentry->d_inode;
511 vnode_t *fvp; /* from directory */
512 vnode_t *tvp; /* target directory */
515 fvp = LINVFS_GET_VP(odir);
516 tvp = LINVFS_GET_VP(ndir);
518 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
523 validate_fields(new_inode);
525 validate_fields(odir);
527 validate_fields(ndir);
532 * careful here - this function can get called recursively, so
533 * we need to be very careful about how much stack we use.
534 * uio is kmalloced for this reason...
538 struct dentry *dentry,
539 struct nameidata *nd)
550 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
552 nd_set_link(nd, ERR_PTR(-ENOMEM));
556 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
559 nd_set_link(nd, ERR_PTR(-ENOMEM));
563 vp = LINVFS_GET_VP(dentry->d_inode);
566 iov.iov_len = MAXPATHLEN;
570 uio->uio_segflg = UIO_SYSSPACE;
571 uio->uio_resid = MAXPATHLEN;
574 VOP_READLINK(vp, uio, 0, NULL, error);
577 link = ERR_PTR(-error);
579 link[MAXPATHLEN - uio->uio_resid] = '\0';
583 nd_set_link(nd, link);
589 struct dentry *dentry,
590 struct nameidata *nd,
593 char *s = nd_get_link(nd);
599 #ifdef CONFIG_XFS_POSIX_ACL
604 struct nameidata *nd)
606 vnode_t *vp = LINVFS_GET_VP(inode);
609 mode <<= 6; /* convert from linux to vnode access bits */
610 VOP_ACCESS(vp, mode, NULL, error);
614 #define linvfs_permission NULL
619 struct vfsmount *mnt,
620 struct dentry *dentry,
623 struct inode *inode = dentry->d_inode;
624 vnode_t *vp = LINVFS_GET_VP(inode);
627 if (unlikely(vp->v_flag & VMODIFIED))
628 error = vn_revalidate(vp);
630 generic_fillattr(inode, stat);
636 struct dentry *dentry,
639 struct inode *inode = dentry->d_inode;
640 unsigned int ia_valid = attr->ia_valid;
641 vnode_t *vp = LINVFS_GET_VP(inode);
646 memset(&vattr, 0, sizeof(vattr_t));
647 if (ia_valid & ATTR_UID) {
648 vattr.va_mask |= XFS_AT_UID;
649 vattr.va_uid = attr->ia_uid;
651 if (ia_valid & ATTR_GID) {
652 vattr.va_mask |= XFS_AT_GID;
653 vattr.va_gid = attr->ia_gid;
655 if (ia_valid & ATTR_SIZE) {
656 vattr.va_mask |= XFS_AT_SIZE;
657 vattr.va_size = attr->ia_size;
659 if (ia_valid & ATTR_ATIME) {
660 vattr.va_mask |= XFS_AT_ATIME;
661 vattr.va_atime = attr->ia_atime;
663 if (ia_valid & ATTR_MTIME) {
664 vattr.va_mask |= XFS_AT_MTIME;
665 vattr.va_mtime = attr->ia_mtime;
667 if (ia_valid & ATTR_CTIME) {
668 vattr.va_mask |= XFS_AT_CTIME;
669 vattr.va_ctime = attr->ia_ctime;
671 if (ia_valid & ATTR_MODE) {
672 vattr.va_mask |= XFS_AT_MODE;
673 vattr.va_mode = attr->ia_mode;
674 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
675 inode->i_mode &= ~S_ISGID;
678 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
681 if ((ia_valid & ATTR_NO_BLOCK))
682 flags |= ATTR_NONBLOCK;
685 VOP_SETATTR(vp, &vattr, flags, NULL, error);
696 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
701 struct dentry *dentry,
707 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
708 char *attr = (char *)name;
713 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
716 attr += namesp->attr_namelen;
717 error = namesp->attr_capable(vp, NULL);
721 /* Convert Linux syscall to XFS internal ATTR flags */
722 if (flags & XATTR_CREATE)
723 xflags |= ATTR_CREATE;
724 if (flags & XATTR_REPLACE)
725 xflags |= ATTR_REPLACE;
726 xflags |= namesp->attr_flag;
727 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
732 struct dentry *dentry,
737 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
738 char *attr = (char *)name;
743 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
746 attr += namesp->attr_namelen;
747 error = namesp->attr_capable(vp, NULL);
751 /* Convert Linux syscall to XFS internal ATTR flags */
753 xflags |= ATTR_KERNOVAL;
756 xflags |= namesp->attr_flag;
757 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
762 struct dentry *dentry,
766 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
767 int error, xflags = ATTR_KERNAMELS;
771 xflags |= ATTR_KERNOVAL;
772 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
774 error = attr_generic_list(vp, data, size, xflags, &result);
782 struct dentry *dentry,
785 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
786 char *attr = (char *)name;
791 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
794 attr += namesp->attr_namelen;
795 error = namesp->attr_capable(vp, NULL);
798 xflags |= namesp->attr_flag;
799 return namesp->attr_remove(vp, attr, xflags);
803 struct inode_operations linvfs_file_inode_operations = {
804 .permission = linvfs_permission,
805 .truncate = linvfs_truncate,
806 .getattr = linvfs_getattr,
807 .setattr = linvfs_setattr,
808 .setxattr = linvfs_setxattr,
809 .getxattr = linvfs_getxattr,
810 .listxattr = linvfs_listxattr,
811 .removexattr = linvfs_removexattr,
814 struct inode_operations linvfs_dir_inode_operations = {
815 .create = linvfs_create,
816 .lookup = linvfs_lookup,
818 .unlink = linvfs_unlink,
819 .symlink = linvfs_symlink,
820 .mkdir = linvfs_mkdir,
821 .rmdir = linvfs_rmdir,
822 .mknod = linvfs_mknod,
823 .rename = linvfs_rename,
824 .permission = linvfs_permission,
825 .getattr = linvfs_getattr,
826 .setattr = linvfs_setattr,
827 .setxattr = linvfs_setxattr,
828 .getxattr = linvfs_getxattr,
829 .listxattr = linvfs_listxattr,
830 .removexattr = linvfs_removexattr,
833 struct inode_operations linvfs_symlink_inode_operations = {
834 .readlink = generic_readlink,
835 .follow_link = linvfs_follow_link,
836 .put_link = linvfs_put_link,
837 .permission = linvfs_permission,
838 .getattr = linvfs_getattr,
839 .setattr = linvfs_setattr,
840 .setxattr = linvfs_setxattr,
841 .getxattr = linvfs_getxattr,
842 .listxattr = linvfs_listxattr,
843 .removexattr = linvfs_removexattr,