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>
57 #define IS_NOATIME(inode) ((inode->i_sb->s_flags & MS_NOATIME) || \
58 (S_ISDIR(inode->i_mode) && inode->i_sb->s_flags & MS_NODIRATIME))
61 * Change the requested timestamp in the given inode.
62 * We don't lock across timestamp updates, and we don't log them but
63 * we do record the fact that there is dirty information in core.
65 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
66 * with XFS_ICHGTIME_ACC to be sure that access time
67 * update will take. Calling first with XFS_ICHGTIME_ACC
68 * and then XFS_ICHGTIME_MOD may fail to modify the access
69 * timestamp if the filesystem is mounted noacctm.
76 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
80 * We're not supposed to change timestamps in readonly-mounted
81 * filesystems. Throw it away if anyone asks us.
83 if (unlikely(IS_RDONLY(inode)))
87 * Don't update access timestamps on reads if mounted "noatime".
88 * Throw it away if anyone asks us.
91 (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
92 (flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
97 if (flags & XFS_ICHGTIME_MOD) {
99 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
100 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
102 if (flags & XFS_ICHGTIME_ACC) {
104 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
105 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
107 if (flags & XFS_ICHGTIME_CHG) {
109 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
110 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
114 * We update the i_update_core field _after_ changing
115 * the timestamps in order to coordinate properly with
116 * xfs_iflush() so that we don't lose timestamp updates.
117 * This keeps us from having to hold the inode lock
118 * while doing this. We use the SYNCHRONIZE macro to
119 * ensure that the compiler does not reorder the update
120 * of i_update_core above the timestamp updates above.
123 ip->i_update_core = 1;
124 if (!(inode->i_state & I_LOCK))
125 mark_inode_dirty_sync(inode);
129 * Variant on the above which avoids querying the system clock
130 * in situations where we know the Linux inode timestamps have
131 * just been updated (and so we can update our inode cheaply).
132 * We also skip the readonly and noatime checks here, they are
133 * also catered for already.
144 * We're not supposed to change timestamps in readonly-mounted
145 * filesystems. Throw it away if anyone asks us.
147 if (unlikely(IS_RDONLY(inode)))
151 * Don't update access timestamps on reads if mounted "noatime".
152 * Throw it away if anyone asks us.
155 (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
156 ((flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
160 if (flags & XFS_ICHGTIME_MOD) {
161 tvp = &inode->i_mtime;
162 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
163 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
165 if (flags & XFS_ICHGTIME_ACC) {
166 tvp = &inode->i_atime;
167 ip->i_d.di_atime.t_sec = (__int32_t)tvp->tv_sec;
168 ip->i_d.di_atime.t_nsec = (__int32_t)tvp->tv_nsec;
170 if (flags & XFS_ICHGTIME_CHG) {
171 tvp = &inode->i_ctime;
172 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
173 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
177 * We update the i_update_core field _after_ changing
178 * the timestamps in order to coordinate properly with
179 * xfs_iflush() so that we don't lose timestamp updates.
180 * This keeps us from having to hold the inode lock
181 * while doing this. We use the SYNCHRONIZE macro to
182 * ensure that the compiler does not reorder the update
183 * of i_update_core above the timestamp updates above.
186 ip->i_update_core = 1;
187 if (!(inode->i_state & I_LOCK))
188 mark_inode_dirty_sync(inode);
193 * Pull the link count and size up from the xfs inode to the linux inode
199 vnode_t *vp = LINVFS_GET_VP(ip);
203 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
204 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
205 if (likely(!error)) {
206 ip->i_nlink = va.va_nlink;
207 ip->i_blocks = va.va_nblocks;
209 /* we're under i_mutex so i_size can't change under us */
210 if (i_size_read(ip) != va.va_size)
211 i_size_write(ip, va.va_size);
216 * Determine whether a process has a valid fs_struct (kernel daemons
217 * like knfsd don't have an fs_struct).
219 * XXX(hch): nfsd is broken, better fix it instead.
222 has_fs_struct(struct task_struct *task)
224 return (task->fs != init_task.fs);
230 struct dentry *dentry,
236 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
237 xfs_acl_t *default_acl = NULL;
238 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
242 * Irix uses Missed'em'V split, but doesn't want to see
243 * the upper 5 bits of (14bit) major.
245 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
248 if (test_default_acl && test_default_acl(dvp)) {
249 if (!_ACL_ALLOC(default_acl))
251 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
252 _ACL_FREE(default_acl);
257 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
258 mode &= ~current->fs->umask;
260 memset(&va, 0, sizeof(va));
261 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
264 switch (mode & S_IFMT) {
265 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
266 va.va_rdev = sysv_encode_dev(rdev);
267 va.va_mask |= XFS_AT_RDEV;
270 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
273 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
282 error = _ACL_INHERIT(vp, &va, default_acl);
286 struct dentry teardown = {};
290 * If we can't add the ACL we must back out.
291 * ENOSPC can hit here, among other things.
293 teardown.d_inode = ip = LINVFS_GET_IP(vp);
294 teardown.d_name = dentry->d_name;
299 VOP_RMDIR(dvp, &teardown, NULL, err2);
301 VOP_REMOVE(dvp, &teardown, NULL, err2);
305 _ACL_FREE(default_acl);
310 ip = LINVFS_GET_IP(vp);
312 if (S_ISCHR(mode) || S_ISBLK(mode))
314 else if (S_ISDIR(mode))
316 d_instantiate(dentry, ip);
317 validate_fields(dir);
325 struct dentry *dentry,
327 struct nameidata *nd)
329 return linvfs_mknod(dir, dentry, mode, 0);
335 struct dentry *dentry,
338 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
341 STATIC struct dentry *
344 struct dentry *dentry,
345 struct nameidata *nd)
347 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
350 if (dentry->d_name.len >= MAXNAMELEN)
351 return ERR_PTR(-ENAMETOOLONG);
353 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
355 if (unlikely(error != ENOENT))
356 return ERR_PTR(-error);
361 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
366 struct dentry *old_dentry,
368 struct dentry *dentry)
370 struct inode *ip; /* inode of guy being linked to */
371 vnode_t *tdvp; /* target directory for new name/link */
372 vnode_t *vp; /* vp of name being linked */
375 ip = old_dentry->d_inode; /* inode being linked to */
376 if (S_ISDIR(ip->i_mode))
379 tdvp = LINVFS_GET_VP(dir);
380 vp = LINVFS_GET_VP(ip);
382 VOP_LINK(tdvp, vp, dentry, NULL, error);
387 d_instantiate(dentry, ip);
395 struct dentry *dentry)
398 vnode_t *dvp; /* directory containing name to remove */
401 inode = dentry->d_inode;
402 dvp = LINVFS_GET_VP(dir);
404 VOP_REMOVE(dvp, dentry, NULL, error);
406 validate_fields(dir); /* For size only */
407 validate_fields(inode);
416 struct dentry *dentry,
421 vnode_t *dvp; /* directory containing name of symlink */
422 vnode_t *cvp; /* used to lookup symlink to put in dentry */
425 dvp = LINVFS_GET_VP(dir);
428 memset(&va, 0, sizeof(va));
429 va.va_mode = S_IFLNK |
430 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
431 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
434 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
436 ip = LINVFS_GET_IP(cvp);
437 d_instantiate(dentry, ip);
438 validate_fields(dir);
439 validate_fields(ip); /* size needs update */
447 struct dentry *dentry)
449 struct inode *inode = dentry->d_inode;
450 vnode_t *dvp = LINVFS_GET_VP(dir);
453 VOP_RMDIR(dvp, dentry, NULL, error);
455 validate_fields(inode);
456 validate_fields(dir);
464 struct dentry *odentry,
466 struct dentry *ndentry)
468 struct inode *new_inode = ndentry->d_inode;
469 vnode_t *fvp; /* from directory */
470 vnode_t *tvp; /* target directory */
473 fvp = LINVFS_GET_VP(odir);
474 tvp = LINVFS_GET_VP(ndir);
476 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
481 validate_fields(new_inode);
483 validate_fields(odir);
485 validate_fields(ndir);
490 * careful here - this function can get called recursively, so
491 * we need to be very careful about how much stack we use.
492 * uio is kmalloced for this reason...
496 struct dentry *dentry,
497 struct nameidata *nd)
508 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
510 nd_set_link(nd, ERR_PTR(-ENOMEM));
514 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
517 nd_set_link(nd, ERR_PTR(-ENOMEM));
521 vp = LINVFS_GET_VP(dentry->d_inode);
524 iov.iov_len = MAXNAMELEN;
528 uio->uio_segflg = UIO_SYSSPACE;
529 uio->uio_resid = MAXNAMELEN;
532 VOP_READLINK(vp, uio, 0, NULL, error);
535 link = ERR_PTR(-error);
537 link[MAXNAMELEN - uio->uio_resid] = '\0';
541 nd_set_link(nd, link);
547 struct dentry *dentry,
548 struct nameidata *nd,
551 char *s = nd_get_link(nd);
557 #ifdef CONFIG_XFS_POSIX_ACL
562 struct nameidata *nd)
564 vnode_t *vp = LINVFS_GET_VP(inode);
567 mode <<= 6; /* convert from linux to vnode access bits */
568 VOP_ACCESS(vp, mode, NULL, error);
572 #define linvfs_permission NULL
577 struct vfsmount *mnt,
578 struct dentry *dentry,
581 struct inode *inode = dentry->d_inode;
582 vnode_t *vp = LINVFS_GET_VP(inode);
585 if (unlikely(vp->v_flag & VMODIFIED))
586 error = vn_revalidate(vp);
588 generic_fillattr(inode, stat);
594 struct dentry *dentry,
597 struct inode *inode = dentry->d_inode;
598 unsigned int ia_valid = attr->ia_valid;
599 vnode_t *vp = LINVFS_GET_VP(inode);
604 memset(&vattr, 0, sizeof(vattr_t));
605 if (ia_valid & ATTR_UID) {
606 vattr.va_mask |= XFS_AT_UID;
607 vattr.va_uid = attr->ia_uid;
609 if (ia_valid & ATTR_GID) {
610 vattr.va_mask |= XFS_AT_GID;
611 vattr.va_gid = attr->ia_gid;
613 if (ia_valid & ATTR_SIZE) {
614 vattr.va_mask |= XFS_AT_SIZE;
615 vattr.va_size = attr->ia_size;
617 if (ia_valid & ATTR_ATIME) {
618 vattr.va_mask |= XFS_AT_ATIME;
619 vattr.va_atime = attr->ia_atime;
621 if (ia_valid & ATTR_MTIME) {
622 vattr.va_mask |= XFS_AT_MTIME;
623 vattr.va_mtime = attr->ia_mtime;
625 if (ia_valid & ATTR_CTIME) {
626 vattr.va_mask |= XFS_AT_CTIME;
627 vattr.va_ctime = attr->ia_ctime;
629 if (ia_valid & ATTR_MODE) {
630 vattr.va_mask |= XFS_AT_MODE;
631 vattr.va_mode = attr->ia_mode;
632 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
633 inode->i_mode &= ~S_ISGID;
636 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
639 if ((ia_valid & ATTR_NO_BLOCK))
640 flags |= ATTR_NONBLOCK;
643 VOP_SETATTR(vp, &vattr, flags, NULL, error);
654 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
659 struct dentry *dentry,
665 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
666 char *attr = (char *)name;
671 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
674 attr += namesp->attr_namelen;
675 error = namesp->attr_capable(vp, NULL);
679 /* Convert Linux syscall to XFS internal ATTR flags */
680 if (flags & XATTR_CREATE)
681 xflags |= ATTR_CREATE;
682 if (flags & XATTR_REPLACE)
683 xflags |= ATTR_REPLACE;
684 xflags |= namesp->attr_flag;
685 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
690 struct dentry *dentry,
695 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
696 char *attr = (char *)name;
701 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
704 attr += namesp->attr_namelen;
705 error = namesp->attr_capable(vp, NULL);
709 /* Convert Linux syscall to XFS internal ATTR flags */
711 xflags |= ATTR_KERNOVAL;
714 xflags |= namesp->attr_flag;
715 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
720 struct dentry *dentry,
724 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
725 int error, xflags = ATTR_KERNAMELS;
729 xflags |= ATTR_KERNOVAL;
730 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
732 error = attr_generic_list(vp, data, size, xflags, &result);
740 struct dentry *dentry,
743 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
744 char *attr = (char *)name;
749 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
752 attr += namesp->attr_namelen;
753 error = namesp->attr_capable(vp, NULL);
756 xflags |= namesp->attr_flag;
757 return namesp->attr_remove(vp, attr, xflags);
761 struct inode_operations linvfs_file_inode_operations = {
762 .permission = linvfs_permission,
763 .truncate = linvfs_truncate,
764 .getattr = linvfs_getattr,
765 .setattr = linvfs_setattr,
766 .setxattr = linvfs_setxattr,
767 .getxattr = linvfs_getxattr,
768 .listxattr = linvfs_listxattr,
769 .removexattr = linvfs_removexattr,
772 struct inode_operations linvfs_dir_inode_operations = {
773 .create = linvfs_create,
774 .lookup = linvfs_lookup,
776 .unlink = linvfs_unlink,
777 .symlink = linvfs_symlink,
778 .mkdir = linvfs_mkdir,
779 .rmdir = linvfs_rmdir,
780 .mknod = linvfs_mknod,
781 .rename = linvfs_rename,
782 .permission = linvfs_permission,
783 .getattr = linvfs_getattr,
784 .setattr = linvfs_setattr,
785 .setxattr = linvfs_setxattr,
786 .getxattr = linvfs_getxattr,
787 .listxattr = linvfs_listxattr,
788 .removexattr = linvfs_removexattr,
791 struct inode_operations linvfs_symlink_inode_operations = {
792 .readlink = generic_readlink,
793 .follow_link = linvfs_follow_link,
794 .put_link = linvfs_put_link,
795 .permission = linvfs_permission,
796 .getattr = linvfs_getattr,
797 .setattr = linvfs_setattr,
798 .setxattr = linvfs_setxattr,
799 .getxattr = linvfs_getxattr,
800 .listxattr = linvfs_listxattr,
801 .removexattr = linvfs_removexattr,