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>
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 * Change the requested timestamp in the given inode.
63 * We don't lock across timestamp updates, and we don't log them but
64 * we do record the fact that there is dirty information in core.
66 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
67 * with XFS_ICHGTIME_ACC to be sure that access time
68 * update will take. Calling first with XFS_ICHGTIME_ACC
69 * and then XFS_ICHGTIME_MOD may fail to modify the access
70 * timestamp if the filesystem is mounted noacctm.
77 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
81 * We're not supposed to change timestamps in readonly-mounted
82 * filesystems. Throw it away if anyone asks us.
84 if (unlikely(IS_RDONLY(inode)))
88 * Don't update access timestamps on reads if mounted "noatime".
89 * Throw it away if anyone asks us.
92 (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
93 (flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
98 if (flags & XFS_ICHGTIME_MOD) {
100 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
101 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
103 if (flags & XFS_ICHGTIME_ACC) {
105 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
106 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
108 if (flags & XFS_ICHGTIME_CHG) {
110 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
111 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
115 * We update the i_update_core field _after_ changing
116 * the timestamps in order to coordinate properly with
117 * xfs_iflush() so that we don't lose timestamp updates.
118 * This keeps us from having to hold the inode lock
119 * while doing this. We use the SYNCHRONIZE macro to
120 * ensure that the compiler does not reorder the update
121 * of i_update_core above the timestamp updates above.
124 ip->i_update_core = 1;
125 if (!(inode->i_state & I_LOCK))
126 mark_inode_dirty_sync(inode);
130 * Variant on the above which avoids querying the system clock
131 * in situations where we know the Linux inode timestamps have
132 * just been updated (and so we can update our inode cheaply).
133 * We also skip the readonly and noatime checks here, they are
134 * also catered for already.
145 * We're not supposed to change timestamps in readonly-mounted
146 * filesystems. Throw it away if anyone asks us.
148 if (unlikely(IS_RDONLY(inode)))
152 * Don't update access timestamps on reads if mounted "noatime".
153 * Throw it away if anyone asks us.
156 (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
157 ((flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
161 if (flags & XFS_ICHGTIME_MOD) {
162 tvp = &inode->i_mtime;
163 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
164 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
166 if (flags & XFS_ICHGTIME_ACC) {
167 tvp = &inode->i_atime;
168 ip->i_d.di_atime.t_sec = (__int32_t)tvp->tv_sec;
169 ip->i_d.di_atime.t_nsec = (__int32_t)tvp->tv_nsec;
171 if (flags & XFS_ICHGTIME_CHG) {
172 tvp = &inode->i_ctime;
173 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
174 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
178 * We update the i_update_core field _after_ changing
179 * the timestamps in order to coordinate properly with
180 * xfs_iflush() so that we don't lose timestamp updates.
181 * This keeps us from having to hold the inode lock
182 * while doing this. We use the SYNCHRONIZE macro to
183 * ensure that the compiler does not reorder the update
184 * of i_update_core above the timestamp updates above.
187 ip->i_update_core = 1;
188 if (!(inode->i_state & I_LOCK))
189 mark_inode_dirty_sync(inode);
194 * Pull the link count and size up from the xfs inode to the linux inode
200 vnode_t *vp = LINVFS_GET_VP(ip);
204 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
205 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
206 if (likely(!error)) {
207 ip->i_nlink = va.va_nlink;
208 ip->i_blocks = va.va_nblocks;
210 /* we're under i_mutex so i_size can't change under us */
211 if (i_size_read(ip) != va.va_size)
212 i_size_write(ip, va.va_size);
217 * Determine whether a process has a valid fs_struct (kernel daemons
218 * like knfsd don't have an fs_struct).
220 * XXX(hch): nfsd is broken, better fix it instead.
223 has_fs_struct(struct task_struct *task)
225 return (task->fs != init_task.fs);
231 struct dentry *dentry,
237 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
238 xfs_acl_t *default_acl = NULL;
239 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
243 * Irix uses Missed'em'V split, but doesn't want to see
244 * the upper 5 bits of (14bit) major.
246 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
249 if (test_default_acl && test_default_acl(dvp)) {
250 if (!_ACL_ALLOC(default_acl))
252 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
253 _ACL_FREE(default_acl);
258 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
259 mode &= ~current->fs->umask;
261 memset(&va, 0, sizeof(va));
262 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
265 switch (mode & S_IFMT) {
266 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
267 va.va_rdev = sysv_encode_dev(rdev);
268 va.va_mask |= XFS_AT_RDEV;
271 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
274 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
283 error = _ACL_INHERIT(vp, &va, default_acl);
287 struct dentry teardown = {};
291 * If we can't add the ACL we must back out.
292 * ENOSPC can hit here, among other things.
294 teardown.d_inode = ip = LINVFS_GET_IP(vp);
295 teardown.d_name = dentry->d_name;
300 VOP_RMDIR(dvp, &teardown, NULL, err2);
302 VOP_REMOVE(dvp, &teardown, NULL, err2);
306 _ACL_FREE(default_acl);
311 ip = LINVFS_GET_IP(vp);
313 if (S_ISCHR(mode) || S_ISBLK(mode))
315 else if (S_ISDIR(mode))
317 d_instantiate(dentry, ip);
318 validate_fields(dir);
326 struct dentry *dentry,
328 struct nameidata *nd)
330 return linvfs_mknod(dir, dentry, mode, 0);
336 struct dentry *dentry,
339 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
342 STATIC struct dentry *
345 struct dentry *dentry,
346 struct nameidata *nd)
348 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
351 if (dentry->d_name.len >= MAXNAMELEN)
352 return ERR_PTR(-ENAMETOOLONG);
354 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
356 if (unlikely(error != ENOENT))
357 return ERR_PTR(-error);
362 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
367 struct dentry *old_dentry,
369 struct dentry *dentry)
371 struct inode *ip; /* inode of guy being linked to */
372 vnode_t *tdvp; /* target directory for new name/link */
373 vnode_t *vp; /* vp of name being linked */
376 ip = old_dentry->d_inode; /* inode being linked to */
377 if (S_ISDIR(ip->i_mode))
380 tdvp = LINVFS_GET_VP(dir);
381 vp = LINVFS_GET_VP(ip);
383 VOP_LINK(tdvp, vp, dentry, NULL, error);
388 d_instantiate(dentry, ip);
396 struct dentry *dentry)
399 vnode_t *dvp; /* directory containing name to remove */
402 inode = dentry->d_inode;
403 dvp = LINVFS_GET_VP(dir);
405 VOP_REMOVE(dvp, dentry, NULL, error);
407 validate_fields(dir); /* For size only */
408 validate_fields(inode);
417 struct dentry *dentry,
422 vnode_t *dvp; /* directory containing name of symlink */
423 vnode_t *cvp; /* used to lookup symlink to put in dentry */
426 dvp = LINVFS_GET_VP(dir);
429 memset(&va, 0, sizeof(va));
430 va.va_mode = S_IFLNK |
431 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
432 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
435 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
437 ip = LINVFS_GET_IP(cvp);
438 d_instantiate(dentry, ip);
439 validate_fields(dir);
440 validate_fields(ip); /* size needs update */
448 struct dentry *dentry)
450 struct inode *inode = dentry->d_inode;
451 vnode_t *dvp = LINVFS_GET_VP(dir);
454 VOP_RMDIR(dvp, dentry, NULL, error);
456 validate_fields(inode);
457 validate_fields(dir);
465 struct dentry *odentry,
467 struct dentry *ndentry)
469 struct inode *new_inode = ndentry->d_inode;
470 vnode_t *fvp; /* from directory */
471 vnode_t *tvp; /* target directory */
474 fvp = LINVFS_GET_VP(odir);
475 tvp = LINVFS_GET_VP(ndir);
477 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
482 validate_fields(new_inode);
484 validate_fields(odir);
486 validate_fields(ndir);
491 * careful here - this function can get called recursively, so
492 * we need to be very careful about how much stack we use.
493 * uio is kmalloced for this reason...
497 struct dentry *dentry,
498 struct nameidata *nd)
509 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
511 nd_set_link(nd, ERR_PTR(-ENOMEM));
515 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
518 nd_set_link(nd, ERR_PTR(-ENOMEM));
522 vp = LINVFS_GET_VP(dentry->d_inode);
525 iov.iov_len = MAXNAMELEN;
529 uio->uio_segflg = UIO_SYSSPACE;
530 uio->uio_resid = MAXNAMELEN;
533 VOP_READLINK(vp, uio, 0, NULL, error);
536 link = ERR_PTR(-error);
538 link[MAXNAMELEN - uio->uio_resid] = '\0';
542 nd_set_link(nd, link);
548 struct dentry *dentry,
549 struct nameidata *nd,
552 char *s = nd_get_link(nd);
558 #ifdef CONFIG_XFS_POSIX_ACL
563 struct nameidata *nd)
565 vnode_t *vp = LINVFS_GET_VP(inode);
568 mode <<= 6; /* convert from linux to vnode access bits */
569 VOP_ACCESS(vp, mode, NULL, error);
573 #define linvfs_permission NULL
578 struct vfsmount *mnt,
579 struct dentry *dentry,
582 struct inode *inode = dentry->d_inode;
583 vnode_t *vp = LINVFS_GET_VP(inode);
586 if (unlikely(vp->v_flag & VMODIFIED))
587 error = vn_revalidate(vp);
589 generic_fillattr(inode, stat);
595 struct dentry *dentry,
598 struct inode *inode = dentry->d_inode;
599 unsigned int ia_valid = attr->ia_valid;
600 vnode_t *vp = LINVFS_GET_VP(inode);
605 memset(&vattr, 0, sizeof(vattr_t));
606 if (ia_valid & ATTR_UID) {
607 vattr.va_mask |= XFS_AT_UID;
608 vattr.va_uid = attr->ia_uid;
610 if (ia_valid & ATTR_GID) {
611 vattr.va_mask |= XFS_AT_GID;
612 vattr.va_gid = attr->ia_gid;
614 if (ia_valid & ATTR_SIZE) {
615 vattr.va_mask |= XFS_AT_SIZE;
616 vattr.va_size = attr->ia_size;
618 if (ia_valid & ATTR_ATIME) {
619 vattr.va_mask |= XFS_AT_ATIME;
620 vattr.va_atime = attr->ia_atime;
622 if (ia_valid & ATTR_MTIME) {
623 vattr.va_mask |= XFS_AT_MTIME;
624 vattr.va_mtime = attr->ia_mtime;
626 if (ia_valid & ATTR_CTIME) {
627 vattr.va_mask |= XFS_AT_CTIME;
628 vattr.va_ctime = attr->ia_ctime;
630 if (ia_valid & ATTR_MODE) {
631 vattr.va_mask |= XFS_AT_MODE;
632 vattr.va_mode = attr->ia_mode;
633 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
634 inode->i_mode &= ~S_ISGID;
637 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
640 if ((ia_valid & ATTR_NO_BLOCK))
641 flags |= ATTR_NONBLOCK;
644 VOP_SETATTR(vp, &vattr, flags, NULL, error);
655 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
660 struct dentry *dentry,
666 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
667 char *attr = (char *)name;
672 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
675 attr += namesp->attr_namelen;
676 error = namesp->attr_capable(vp, NULL);
680 /* Convert Linux syscall to XFS internal ATTR flags */
681 if (flags & XATTR_CREATE)
682 xflags |= ATTR_CREATE;
683 if (flags & XATTR_REPLACE)
684 xflags |= ATTR_REPLACE;
685 xflags |= namesp->attr_flag;
686 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
691 struct dentry *dentry,
696 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
697 char *attr = (char *)name;
702 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
705 attr += namesp->attr_namelen;
706 error = namesp->attr_capable(vp, NULL);
710 /* Convert Linux syscall to XFS internal ATTR flags */
712 xflags |= ATTR_KERNOVAL;
715 xflags |= namesp->attr_flag;
716 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
721 struct dentry *dentry,
725 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
726 int error, xflags = ATTR_KERNAMELS;
730 xflags |= ATTR_KERNOVAL;
731 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
733 error = attr_generic_list(vp, data, size, xflags, &result);
741 struct dentry *dentry,
744 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
745 char *attr = (char *)name;
750 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
753 attr += namesp->attr_namelen;
754 error = namesp->attr_capable(vp, NULL);
757 xflags |= namesp->attr_flag;
758 return namesp->attr_remove(vp, attr, xflags);
762 struct inode_operations linvfs_file_inode_operations = {
763 .permission = linvfs_permission,
764 .truncate = linvfs_truncate,
765 .getattr = linvfs_getattr,
766 .setattr = linvfs_setattr,
767 .setxattr = linvfs_setxattr,
768 .getxattr = linvfs_getxattr,
769 .listxattr = linvfs_listxattr,
770 .removexattr = linvfs_removexattr,
773 struct inode_operations linvfs_dir_inode_operations = {
774 .create = linvfs_create,
775 .lookup = linvfs_lookup,
777 .unlink = linvfs_unlink,
778 .symlink = linvfs_symlink,
779 .mkdir = linvfs_mkdir,
780 .rmdir = linvfs_rmdir,
781 .mknod = linvfs_mknod,
782 .rename = linvfs_rename,
783 .permission = linvfs_permission,
784 .getattr = linvfs_getattr,
785 .setattr = linvfs_setattr,
786 .setxattr = linvfs_setxattr,
787 .getxattr = linvfs_getxattr,
788 .listxattr = linvfs_listxattr,
789 .removexattr = linvfs_removexattr,
792 struct inode_operations linvfs_symlink_inode_operations = {
793 .readlink = generic_readlink,
794 .follow_link = linvfs_follow_link,
795 .put_link = linvfs_put_link,
796 .permission = linvfs_permission,
797 .getattr = linvfs_getattr,
798 .setattr = linvfs_setattr,
799 .setxattr = linvfs_setxattr,
800 .getxattr = linvfs_getxattr,
801 .listxattr = linvfs_listxattr,
802 .removexattr = linvfs_removexattr,