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 * Bring the atime in the XFS inode uptodate.
62 * Used before logging the inode to disk or when the Linux inode goes away.
65 xfs_synchronize_atime(
70 vp = XFS_ITOV_NULL(ip);
72 struct inode *inode = &vp->v_inode;
73 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
74 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
79 * Change the requested timestamp in the given inode.
80 * We don't lock across timestamp updates, and we don't log them but
81 * we do record the fact that there is dirty information in core.
83 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
84 * with XFS_ICHGTIME_ACC to be sure that access time
85 * update will take. Calling first with XFS_ICHGTIME_ACC
86 * and then XFS_ICHGTIME_MOD may fail to modify the access
87 * timestamp if the filesystem is mounted noacctm.
94 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
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).
143 * Atime updates for read() & friends are handled lazily now, and
144 * explicit updates must go through xfs_ichgtime()
146 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
149 * We're not supposed to change timestamps in readonly-mounted
150 * filesystems. Throw it away if anyone asks us.
152 if (unlikely(IS_RDONLY(inode)))
155 if (flags & XFS_ICHGTIME_MOD) {
156 tvp = &inode->i_mtime;
157 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
158 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
160 if (flags & XFS_ICHGTIME_CHG) {
161 tvp = &inode->i_ctime;
162 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
163 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
167 * We update the i_update_core field _after_ changing
168 * the timestamps in order to coordinate properly with
169 * xfs_iflush() so that we don't lose timestamp updates.
170 * This keeps us from having to hold the inode lock
171 * while doing this. We use the SYNCHRONIZE macro to
172 * ensure that the compiler does not reorder the update
173 * of i_update_core above the timestamp updates above.
176 ip->i_update_core = 1;
177 if (!(inode->i_state & I_LOCK))
178 mark_inode_dirty_sync(inode);
183 * Pull the link count and size up from the xfs inode to the linux inode
189 vnode_t *vp = LINVFS_GET_VP(ip);
193 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
194 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
195 if (likely(!error)) {
196 ip->i_nlink = va.va_nlink;
197 ip->i_blocks = va.va_nblocks;
199 /* we're under i_mutex so i_size can't change under us */
200 if (i_size_read(ip) != va.va_size)
201 i_size_write(ip, va.va_size);
206 * Determine whether a process has a valid fs_struct (kernel daemons
207 * like knfsd don't have an fs_struct).
209 * XXX(hch): nfsd is broken, better fix it instead.
212 has_fs_struct(struct task_struct *task)
214 return (task->fs != init_task.fs);
220 struct dentry *dentry,
226 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
227 xfs_acl_t *default_acl = NULL;
228 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
232 * Irix uses Missed'em'V split, but doesn't want to see
233 * the upper 5 bits of (14bit) major.
235 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
238 if (test_default_acl && test_default_acl(dvp)) {
239 if (!_ACL_ALLOC(default_acl))
241 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
242 _ACL_FREE(default_acl);
247 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
248 mode &= ~current->fs->umask;
250 memset(&va, 0, sizeof(va));
251 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
254 switch (mode & S_IFMT) {
255 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
256 va.va_rdev = sysv_encode_dev(rdev);
257 va.va_mask |= XFS_AT_RDEV;
260 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
263 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
272 error = _ACL_INHERIT(vp, &va, default_acl);
276 struct dentry teardown = {};
280 * If we can't add the ACL we must back out.
281 * ENOSPC can hit here, among other things.
283 teardown.d_inode = ip = LINVFS_GET_IP(vp);
284 teardown.d_name = dentry->d_name;
289 VOP_RMDIR(dvp, &teardown, NULL, err2);
291 VOP_REMOVE(dvp, &teardown, NULL, err2);
295 _ACL_FREE(default_acl);
300 ip = LINVFS_GET_IP(vp);
302 if (S_ISCHR(mode) || S_ISBLK(mode))
304 else if (S_ISDIR(mode))
306 d_instantiate(dentry, ip);
307 validate_fields(dir);
315 struct dentry *dentry,
317 struct nameidata *nd)
319 return linvfs_mknod(dir, dentry, mode, 0);
325 struct dentry *dentry,
328 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
331 STATIC struct dentry *
334 struct dentry *dentry,
335 struct nameidata *nd)
337 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
340 if (dentry->d_name.len >= MAXNAMELEN)
341 return ERR_PTR(-ENAMETOOLONG);
343 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
345 if (unlikely(error != ENOENT))
346 return ERR_PTR(-error);
351 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
356 struct dentry *old_dentry,
358 struct dentry *dentry)
360 struct inode *ip; /* inode of guy being linked to */
361 vnode_t *tdvp; /* target directory for new name/link */
362 vnode_t *vp; /* vp of name being linked */
365 ip = old_dentry->d_inode; /* inode being linked to */
366 if (S_ISDIR(ip->i_mode))
369 tdvp = LINVFS_GET_VP(dir);
370 vp = LINVFS_GET_VP(ip);
372 VOP_LINK(tdvp, vp, dentry, NULL, error);
377 d_instantiate(dentry, ip);
385 struct dentry *dentry)
388 vnode_t *dvp; /* directory containing name to remove */
391 inode = dentry->d_inode;
392 dvp = LINVFS_GET_VP(dir);
394 VOP_REMOVE(dvp, dentry, NULL, error);
396 validate_fields(dir); /* For size only */
397 validate_fields(inode);
406 struct dentry *dentry,
411 vnode_t *dvp; /* directory containing name of symlink */
412 vnode_t *cvp; /* used to lookup symlink to put in dentry */
415 dvp = LINVFS_GET_VP(dir);
418 memset(&va, 0, sizeof(va));
419 va.va_mode = S_IFLNK |
420 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
421 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
424 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
426 ip = LINVFS_GET_IP(cvp);
427 d_instantiate(dentry, ip);
428 validate_fields(dir);
429 validate_fields(ip); /* size needs update */
437 struct dentry *dentry)
439 struct inode *inode = dentry->d_inode;
440 vnode_t *dvp = LINVFS_GET_VP(dir);
443 VOP_RMDIR(dvp, dentry, NULL, error);
445 validate_fields(inode);
446 validate_fields(dir);
454 struct dentry *odentry,
456 struct dentry *ndentry)
458 struct inode *new_inode = ndentry->d_inode;
459 vnode_t *fvp; /* from directory */
460 vnode_t *tvp; /* target directory */
463 fvp = LINVFS_GET_VP(odir);
464 tvp = LINVFS_GET_VP(ndir);
466 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
471 validate_fields(new_inode);
473 validate_fields(odir);
475 validate_fields(ndir);
480 * careful here - this function can get called recursively, so
481 * we need to be very careful about how much stack we use.
482 * uio is kmalloced for this reason...
486 struct dentry *dentry,
487 struct nameidata *nd)
498 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
500 nd_set_link(nd, ERR_PTR(-ENOMEM));
504 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
507 nd_set_link(nd, ERR_PTR(-ENOMEM));
511 vp = LINVFS_GET_VP(dentry->d_inode);
514 iov.iov_len = MAXNAMELEN;
518 uio->uio_segflg = UIO_SYSSPACE;
519 uio->uio_resid = MAXNAMELEN;
522 VOP_READLINK(vp, uio, 0, NULL, error);
525 link = ERR_PTR(-error);
527 link[MAXNAMELEN - uio->uio_resid] = '\0';
531 nd_set_link(nd, link);
537 struct dentry *dentry,
538 struct nameidata *nd,
541 char *s = nd_get_link(nd);
547 #ifdef CONFIG_XFS_POSIX_ACL
552 struct nameidata *nd)
554 vnode_t *vp = LINVFS_GET_VP(inode);
557 mode <<= 6; /* convert from linux to vnode access bits */
558 VOP_ACCESS(vp, mode, NULL, error);
562 #define linvfs_permission NULL
567 struct vfsmount *mnt,
568 struct dentry *dentry,
571 struct inode *inode = dentry->d_inode;
572 vnode_t *vp = LINVFS_GET_VP(inode);
575 if (unlikely(vp->v_flag & VMODIFIED))
576 error = vn_revalidate(vp);
578 generic_fillattr(inode, stat);
584 struct dentry *dentry,
587 struct inode *inode = dentry->d_inode;
588 unsigned int ia_valid = attr->ia_valid;
589 vnode_t *vp = LINVFS_GET_VP(inode);
594 memset(&vattr, 0, sizeof(vattr_t));
595 if (ia_valid & ATTR_UID) {
596 vattr.va_mask |= XFS_AT_UID;
597 vattr.va_uid = attr->ia_uid;
599 if (ia_valid & ATTR_GID) {
600 vattr.va_mask |= XFS_AT_GID;
601 vattr.va_gid = attr->ia_gid;
603 if (ia_valid & ATTR_SIZE) {
604 vattr.va_mask |= XFS_AT_SIZE;
605 vattr.va_size = attr->ia_size;
607 if (ia_valid & ATTR_ATIME) {
608 vattr.va_mask |= XFS_AT_ATIME;
609 vattr.va_atime = attr->ia_atime;
611 if (ia_valid & ATTR_MTIME) {
612 vattr.va_mask |= XFS_AT_MTIME;
613 vattr.va_mtime = attr->ia_mtime;
615 if (ia_valid & ATTR_CTIME) {
616 vattr.va_mask |= XFS_AT_CTIME;
617 vattr.va_ctime = attr->ia_ctime;
619 if (ia_valid & ATTR_MODE) {
620 vattr.va_mask |= XFS_AT_MODE;
621 vattr.va_mode = attr->ia_mode;
622 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
623 inode->i_mode &= ~S_ISGID;
626 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
629 if ((ia_valid & ATTR_NO_BLOCK))
630 flags |= ATTR_NONBLOCK;
633 VOP_SETATTR(vp, &vattr, flags, NULL, error);
644 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
649 struct dentry *dentry,
655 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
656 char *attr = (char *)name;
661 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
664 attr += namesp->attr_namelen;
665 error = namesp->attr_capable(vp, NULL);
669 /* Convert Linux syscall to XFS internal ATTR flags */
670 if (flags & XATTR_CREATE)
671 xflags |= ATTR_CREATE;
672 if (flags & XATTR_REPLACE)
673 xflags |= ATTR_REPLACE;
674 xflags |= namesp->attr_flag;
675 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
680 struct dentry *dentry,
685 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
686 char *attr = (char *)name;
691 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
694 attr += namesp->attr_namelen;
695 error = namesp->attr_capable(vp, NULL);
699 /* Convert Linux syscall to XFS internal ATTR flags */
701 xflags |= ATTR_KERNOVAL;
704 xflags |= namesp->attr_flag;
705 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
710 struct dentry *dentry,
714 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
715 int error, xflags = ATTR_KERNAMELS;
719 xflags |= ATTR_KERNOVAL;
720 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
722 error = attr_generic_list(vp, data, size, xflags, &result);
730 struct dentry *dentry,
733 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
734 char *attr = (char *)name;
739 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
742 attr += namesp->attr_namelen;
743 error = namesp->attr_capable(vp, NULL);
746 xflags |= namesp->attr_flag;
747 return namesp->attr_remove(vp, attr, xflags);
751 struct inode_operations linvfs_file_inode_operations = {
752 .permission = linvfs_permission,
753 .truncate = linvfs_truncate,
754 .getattr = linvfs_getattr,
755 .setattr = linvfs_setattr,
756 .setxattr = linvfs_setxattr,
757 .getxattr = linvfs_getxattr,
758 .listxattr = linvfs_listxattr,
759 .removexattr = linvfs_removexattr,
762 struct inode_operations linvfs_dir_inode_operations = {
763 .create = linvfs_create,
764 .lookup = linvfs_lookup,
766 .unlink = linvfs_unlink,
767 .symlink = linvfs_symlink,
768 .mkdir = linvfs_mkdir,
769 .rmdir = linvfs_rmdir,
770 .mknod = linvfs_mknod,
771 .rename = linvfs_rename,
772 .permission = linvfs_permission,
773 .getattr = linvfs_getattr,
774 .setattr = linvfs_setattr,
775 .setxattr = linvfs_setxattr,
776 .getxattr = linvfs_getxattr,
777 .listxattr = linvfs_listxattr,
778 .removexattr = linvfs_removexattr,
781 struct inode_operations linvfs_symlink_inode_operations = {
782 .readlink = generic_readlink,
783 .follow_link = linvfs_follow_link,
784 .put_link = linvfs_put_link,
785 .permission = linvfs_permission,
786 .getattr = linvfs_getattr,
787 .setattr = linvfs_setattr,
788 .setxattr = linvfs_setxattr,
789 .getxattr = linvfs_getxattr,
790 .listxattr = linvfs_listxattr,
791 .removexattr = linvfs_removexattr,