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/xattr.h>
55 #include <linux/namei.h>
56 #include <linux/security.h>
58 #define IS_NOATIME(inode) ((inode->i_sb->s_flags & MS_NOATIME) || \
59 (S_ISDIR(inode->i_mode) && inode->i_sb->s_flags & MS_NODIRATIME))
62 * Get a XFS inode from a given vnode.
70 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
71 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
72 if (unlikely(bdp == NULL))
74 return XFS_BHVTOI(bdp);
78 * Bring the atime in the XFS inode uptodate.
79 * Used before logging the inode to disk or when the Linux inode goes away.
82 xfs_synchronize_atime(
87 vp = XFS_ITOV_NULL(ip);
89 struct inode *inode = &vp->v_inode;
90 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
91 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
96 * Change the requested timestamp in the given inode.
97 * We don't lock across timestamp updates, and we don't log them but
98 * we do record the fact that there is dirty information in core.
100 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
101 * with XFS_ICHGTIME_ACC to be sure that access time
102 * update will take. Calling first with XFS_ICHGTIME_ACC
103 * and then XFS_ICHGTIME_MOD may fail to modify the access
104 * timestamp if the filesystem is mounted noacctm.
111 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
115 if (flags & XFS_ICHGTIME_MOD) {
117 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
118 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
120 if (flags & XFS_ICHGTIME_ACC) {
122 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
123 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
125 if (flags & XFS_ICHGTIME_CHG) {
127 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
128 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
132 * We update the i_update_core field _after_ changing
133 * the timestamps in order to coordinate properly with
134 * xfs_iflush() so that we don't lose timestamp updates.
135 * This keeps us from having to hold the inode lock
136 * while doing this. We use the SYNCHRONIZE macro to
137 * ensure that the compiler does not reorder the update
138 * of i_update_core above the timestamp updates above.
141 ip->i_update_core = 1;
142 if (!(inode->i_state & I_LOCK))
143 mark_inode_dirty_sync(inode);
147 * Variant on the above which avoids querying the system clock
148 * in situations where we know the Linux inode timestamps have
149 * just been updated (and so we can update our inode cheaply).
160 * Atime updates for read() & friends are handled lazily now, and
161 * explicit updates must go through xfs_ichgtime()
163 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
166 * We're not supposed to change timestamps in readonly-mounted
167 * filesystems. Throw it away if anyone asks us.
169 if (unlikely(IS_RDONLY(inode)))
172 if (flags & XFS_ICHGTIME_MOD) {
173 tvp = &inode->i_mtime;
174 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
175 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
177 if (flags & XFS_ICHGTIME_CHG) {
178 tvp = &inode->i_ctime;
179 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
180 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
184 * We update the i_update_core field _after_ changing
185 * the timestamps in order to coordinate properly with
186 * xfs_iflush() so that we don't lose timestamp updates.
187 * This keeps us from having to hold the inode lock
188 * while doing this. We use the SYNCHRONIZE macro to
189 * ensure that the compiler does not reorder the update
190 * of i_update_core above the timestamp updates above.
193 ip->i_update_core = 1;
194 if (!(inode->i_state & I_LOCK))
195 mark_inode_dirty_sync(inode);
200 * Pull the link count and size up from the xfs inode to the linux inode
206 vnode_t *vp = LINVFS_GET_VP(ip);
210 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
211 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
212 if (likely(!error)) {
213 ip->i_nlink = va.va_nlink;
214 ip->i_blocks = va.va_nblocks;
216 /* we're under i_mutex so i_size can't change under us */
217 if (i_size_read(ip) != va.va_size)
218 i_size_write(ip, va.va_size);
223 * Hook in SELinux. This is not quite correct yet, what we really need
224 * here (as we do for default ACLs) is a mechanism by which creation of
225 * these attrs can be journalled at inode creation time (along with the
226 * inode, of course, such that log replay can't cause these to be lost).
229 linvfs_init_security(
233 struct inode *ip = LINVFS_GET_IP(vp);
239 error = security_inode_init_security(ip, dir, &name, &value, &length);
241 if (error == -EOPNOTSUPP)
246 VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
256 * Determine whether a process has a valid fs_struct (kernel daemons
257 * like knfsd don't have an fs_struct).
259 * XXX(hch): nfsd is broken, better fix it instead.
262 has_fs_struct(struct task_struct *task)
264 return (task->fs != init_task.fs);
270 struct dentry *dentry,
276 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
277 xfs_acl_t *default_acl = NULL;
278 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
282 * Irix uses Missed'em'V split, but doesn't want to see
283 * the upper 5 bits of (14bit) major.
285 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
288 if (test_default_acl && test_default_acl(dvp)) {
289 if (!_ACL_ALLOC(default_acl))
291 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
292 _ACL_FREE(default_acl);
297 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
298 mode &= ~current->fs->umask;
300 memset(&va, 0, sizeof(va));
301 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
304 switch (mode & S_IFMT) {
305 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
306 va.va_rdev = sysv_encode_dev(rdev);
307 va.va_mask |= XFS_AT_RDEV;
310 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
313 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
321 error = linvfs_init_security(vp, dir);
325 error = _ACL_INHERIT(vp, &va, default_acl);
329 struct dentry teardown = {};
333 * If we can't add the ACL we must back out.
334 * ENOSPC can hit here, among other things.
336 teardown.d_inode = ip = LINVFS_GET_IP(vp);
337 teardown.d_name = dentry->d_name;
340 VOP_RMDIR(dvp, &teardown, NULL, err2);
342 VOP_REMOVE(dvp, &teardown, NULL, err2);
346 _ACL_FREE(default_acl);
351 ip = LINVFS_GET_IP(vp);
353 if (S_ISCHR(mode) || S_ISBLK(mode))
355 else if (S_ISDIR(mode))
357 d_instantiate(dentry, ip);
358 validate_fields(dir);
366 struct dentry *dentry,
368 struct nameidata *nd)
370 return linvfs_mknod(dir, dentry, mode, 0);
376 struct dentry *dentry,
379 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
382 STATIC struct dentry *
385 struct dentry *dentry,
386 struct nameidata *nd)
388 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
391 if (dentry->d_name.len >= MAXNAMELEN)
392 return ERR_PTR(-ENAMETOOLONG);
394 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
396 if (unlikely(error != ENOENT))
397 return ERR_PTR(-error);
402 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
407 struct dentry *old_dentry,
409 struct dentry *dentry)
411 struct inode *ip; /* inode of guy being linked to */
412 vnode_t *tdvp; /* target directory for new name/link */
413 vnode_t *vp; /* vp of name being linked */
416 ip = old_dentry->d_inode; /* inode being linked to */
417 if (S_ISDIR(ip->i_mode))
420 tdvp = LINVFS_GET_VP(dir);
421 vp = LINVFS_GET_VP(ip);
423 VOP_LINK(tdvp, vp, dentry, NULL, error);
428 d_instantiate(dentry, ip);
436 struct dentry *dentry)
439 vnode_t *dvp; /* directory containing name to remove */
442 inode = dentry->d_inode;
443 dvp = LINVFS_GET_VP(dir);
445 VOP_REMOVE(dvp, dentry, NULL, error);
447 validate_fields(dir); /* For size only */
448 validate_fields(inode);
457 struct dentry *dentry,
462 vnode_t *dvp; /* directory containing name of symlink */
463 vnode_t *cvp; /* used to lookup symlink to put in dentry */
466 dvp = LINVFS_GET_VP(dir);
469 memset(&va, 0, sizeof(va));
470 va.va_mode = S_IFLNK |
471 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
472 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
475 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
477 ip = LINVFS_GET_IP(cvp);
478 d_instantiate(dentry, ip);
479 validate_fields(dir);
480 validate_fields(ip); /* size needs update */
488 struct dentry *dentry)
490 struct inode *inode = dentry->d_inode;
491 vnode_t *dvp = LINVFS_GET_VP(dir);
494 VOP_RMDIR(dvp, dentry, NULL, error);
496 validate_fields(inode);
497 validate_fields(dir);
505 struct dentry *odentry,
507 struct dentry *ndentry)
509 struct inode *new_inode = ndentry->d_inode;
510 vnode_t *fvp; /* from directory */
511 vnode_t *tvp; /* target directory */
514 fvp = LINVFS_GET_VP(odir);
515 tvp = LINVFS_GET_VP(ndir);
517 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
522 validate_fields(new_inode);
524 validate_fields(odir);
526 validate_fields(ndir);
531 * careful here - this function can get called recursively, so
532 * we need to be very careful about how much stack we use.
533 * uio is kmalloced for this reason...
537 struct dentry *dentry,
538 struct nameidata *nd)
549 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
551 nd_set_link(nd, ERR_PTR(-ENOMEM));
555 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
558 nd_set_link(nd, ERR_PTR(-ENOMEM));
562 vp = LINVFS_GET_VP(dentry->d_inode);
565 iov.iov_len = MAXNAMELEN;
569 uio->uio_segflg = UIO_SYSSPACE;
570 uio->uio_resid = MAXNAMELEN;
573 VOP_READLINK(vp, uio, 0, NULL, error);
576 link = ERR_PTR(-error);
578 link[MAXNAMELEN - uio->uio_resid] = '\0';
582 nd_set_link(nd, link);
588 struct dentry *dentry,
589 struct nameidata *nd,
592 char *s = nd_get_link(nd);
598 #ifdef CONFIG_XFS_POSIX_ACL
603 struct nameidata *nd)
605 vnode_t *vp = LINVFS_GET_VP(inode);
608 mode <<= 6; /* convert from linux to vnode access bits */
609 VOP_ACCESS(vp, mode, NULL, error);
613 #define linvfs_permission NULL
618 struct vfsmount *mnt,
619 struct dentry *dentry,
622 struct inode *inode = dentry->d_inode;
623 vnode_t *vp = LINVFS_GET_VP(inode);
626 if (unlikely(vp->v_flag & VMODIFIED))
627 error = vn_revalidate(vp);
629 generic_fillattr(inode, stat);
635 struct dentry *dentry,
638 struct inode *inode = dentry->d_inode;
639 unsigned int ia_valid = attr->ia_valid;
640 vnode_t *vp = LINVFS_GET_VP(inode);
645 memset(&vattr, 0, sizeof(vattr_t));
646 if (ia_valid & ATTR_UID) {
647 vattr.va_mask |= XFS_AT_UID;
648 vattr.va_uid = attr->ia_uid;
650 if (ia_valid & ATTR_GID) {
651 vattr.va_mask |= XFS_AT_GID;
652 vattr.va_gid = attr->ia_gid;
654 if (ia_valid & ATTR_SIZE) {
655 vattr.va_mask |= XFS_AT_SIZE;
656 vattr.va_size = attr->ia_size;
658 if (ia_valid & ATTR_ATIME) {
659 vattr.va_mask |= XFS_AT_ATIME;
660 vattr.va_atime = attr->ia_atime;
662 if (ia_valid & ATTR_MTIME) {
663 vattr.va_mask |= XFS_AT_MTIME;
664 vattr.va_mtime = attr->ia_mtime;
666 if (ia_valid & ATTR_CTIME) {
667 vattr.va_mask |= XFS_AT_CTIME;
668 vattr.va_ctime = attr->ia_ctime;
670 if (ia_valid & ATTR_MODE) {
671 vattr.va_mask |= XFS_AT_MODE;
672 vattr.va_mode = attr->ia_mode;
673 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
674 inode->i_mode &= ~S_ISGID;
677 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
680 if ((ia_valid & ATTR_NO_BLOCK))
681 flags |= ATTR_NONBLOCK;
684 VOP_SETATTR(vp, &vattr, flags, NULL, error);
695 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
700 struct dentry *dentry,
706 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
707 char *attr = (char *)name;
712 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
715 attr += namesp->attr_namelen;
716 error = namesp->attr_capable(vp, NULL);
720 /* Convert Linux syscall to XFS internal ATTR flags */
721 if (flags & XATTR_CREATE)
722 xflags |= ATTR_CREATE;
723 if (flags & XATTR_REPLACE)
724 xflags |= ATTR_REPLACE;
725 xflags |= namesp->attr_flag;
726 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
731 struct dentry *dentry,
736 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
737 char *attr = (char *)name;
742 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
745 attr += namesp->attr_namelen;
746 error = namesp->attr_capable(vp, NULL);
750 /* Convert Linux syscall to XFS internal ATTR flags */
752 xflags |= ATTR_KERNOVAL;
755 xflags |= namesp->attr_flag;
756 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
761 struct dentry *dentry,
765 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
766 int error, xflags = ATTR_KERNAMELS;
770 xflags |= ATTR_KERNOVAL;
771 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
773 error = attr_generic_list(vp, data, size, xflags, &result);
781 struct dentry *dentry,
784 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
785 char *attr = (char *)name;
790 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
793 attr += namesp->attr_namelen;
794 error = namesp->attr_capable(vp, NULL);
797 xflags |= namesp->attr_flag;
798 return namesp->attr_remove(vp, attr, xflags);
802 struct inode_operations linvfs_file_inode_operations = {
803 .permission = linvfs_permission,
804 .truncate = linvfs_truncate,
805 .getattr = linvfs_getattr,
806 .setattr = linvfs_setattr,
807 .setxattr = linvfs_setxattr,
808 .getxattr = linvfs_getxattr,
809 .listxattr = linvfs_listxattr,
810 .removexattr = linvfs_removexattr,
813 struct inode_operations linvfs_dir_inode_operations = {
814 .create = linvfs_create,
815 .lookup = linvfs_lookup,
817 .unlink = linvfs_unlink,
818 .symlink = linvfs_symlink,
819 .mkdir = linvfs_mkdir,
820 .rmdir = linvfs_rmdir,
821 .mknod = linvfs_mknod,
822 .rename = linvfs_rename,
823 .permission = linvfs_permission,
824 .getattr = linvfs_getattr,
825 .setattr = linvfs_setattr,
826 .setxattr = linvfs_setxattr,
827 .getxattr = linvfs_getxattr,
828 .listxattr = linvfs_listxattr,
829 .removexattr = linvfs_removexattr,
832 struct inode_operations linvfs_symlink_inode_operations = {
833 .readlink = generic_readlink,
834 .follow_link = linvfs_follow_link,
835 .put_link = linvfs_put_link,
836 .permission = linvfs_permission,
837 .getattr = linvfs_getattr,
838 .setattr = linvfs_setattr,
839 .setxattr = linvfs_setxattr,
840 .getxattr = linvfs_getxattr,
841 .listxattr = linvfs_listxattr,
842 .removexattr = linvfs_removexattr,