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 * Bring the atime in the XFS inode uptodate.
63 * Used before logging the inode to disk or when the Linux inode goes away.
66 xfs_synchronize_atime(
71 vp = XFS_ITOV_NULL(ip);
73 struct inode *inode = &vp->v_inode;
74 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
75 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
80 * Change the requested timestamp in the given inode.
81 * We don't lock across timestamp updates, and we don't log them but
82 * we do record the fact that there is dirty information in core.
84 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
85 * with XFS_ICHGTIME_ACC to be sure that access time
86 * update will take. Calling first with XFS_ICHGTIME_ACC
87 * and then XFS_ICHGTIME_MOD may fail to modify the access
88 * timestamp if the filesystem is mounted noacctm.
95 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
99 if (flags & XFS_ICHGTIME_MOD) {
101 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
102 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
104 if (flags & XFS_ICHGTIME_ACC) {
106 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
107 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
109 if (flags & XFS_ICHGTIME_CHG) {
111 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
112 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
116 * We update the i_update_core field _after_ changing
117 * the timestamps in order to coordinate properly with
118 * xfs_iflush() so that we don't lose timestamp updates.
119 * This keeps us from having to hold the inode lock
120 * while doing this. We use the SYNCHRONIZE macro to
121 * ensure that the compiler does not reorder the update
122 * of i_update_core above the timestamp updates above.
125 ip->i_update_core = 1;
126 if (!(inode->i_state & I_LOCK))
127 mark_inode_dirty_sync(inode);
131 * Variant on the above which avoids querying the system clock
132 * in situations where we know the Linux inode timestamps have
133 * just been updated (and so we can update our inode cheaply).
144 * Atime updates for read() & friends are handled lazily now, and
145 * explicit updates must go through xfs_ichgtime()
147 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
150 * We're not supposed to change timestamps in readonly-mounted
151 * filesystems. Throw it away if anyone asks us.
153 if (unlikely(IS_RDONLY(inode)))
156 if (flags & XFS_ICHGTIME_MOD) {
157 tvp = &inode->i_mtime;
158 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
159 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
161 if (flags & XFS_ICHGTIME_CHG) {
162 tvp = &inode->i_ctime;
163 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
164 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
168 * We update the i_update_core field _after_ changing
169 * the timestamps in order to coordinate properly with
170 * xfs_iflush() so that we don't lose timestamp updates.
171 * This keeps us from having to hold the inode lock
172 * while doing this. We use the SYNCHRONIZE macro to
173 * ensure that the compiler does not reorder the update
174 * of i_update_core above the timestamp updates above.
177 ip->i_update_core = 1;
178 if (!(inode->i_state & I_LOCK))
179 mark_inode_dirty_sync(inode);
184 * Pull the link count and size up from the xfs inode to the linux inode
190 vnode_t *vp = LINVFS_GET_VP(ip);
194 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
195 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
196 if (likely(!error)) {
197 ip->i_nlink = va.va_nlink;
198 ip->i_blocks = va.va_nblocks;
200 /* we're under i_mutex so i_size can't change under us */
201 if (i_size_read(ip) != va.va_size)
202 i_size_write(ip, va.va_size);
207 * Hook in SELinux. This is not quite correct yet, what we really need
208 * here (as we do for default ACLs) is a mechanism by which creation of
209 * these attrs can be journalled at inode creation time (along with the
210 * inode, of course, such that log replay can't cause these to be lost).
213 linvfs_init_security(
217 struct inode *ip = LINVFS_GET_IP(vp);
223 error = security_inode_init_security(ip, dir, &name, &value, &length);
225 if (error == -EOPNOTSUPP)
230 VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
240 * Determine whether a process has a valid fs_struct (kernel daemons
241 * like knfsd don't have an fs_struct).
243 * XXX(hch): nfsd is broken, better fix it instead.
246 has_fs_struct(struct task_struct *task)
248 return (task->fs != init_task.fs);
254 struct dentry *dentry,
260 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
261 xfs_acl_t *default_acl = NULL;
262 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
266 * Irix uses Missed'em'V split, but doesn't want to see
267 * the upper 5 bits of (14bit) major.
269 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
272 if (test_default_acl && test_default_acl(dvp)) {
273 if (!_ACL_ALLOC(default_acl))
275 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
276 _ACL_FREE(default_acl);
281 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
282 mode &= ~current->fs->umask;
284 memset(&va, 0, sizeof(va));
285 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
288 switch (mode & S_IFMT) {
289 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
290 va.va_rdev = sysv_encode_dev(rdev);
291 va.va_mask |= XFS_AT_RDEV;
294 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
297 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
305 error = linvfs_init_security(vp, dir);
309 error = _ACL_INHERIT(vp, &va, default_acl);
313 struct dentry teardown = {};
317 * If we can't add the ACL we must back out.
318 * ENOSPC can hit here, among other things.
320 teardown.d_inode = ip = LINVFS_GET_IP(vp);
321 teardown.d_name = dentry->d_name;
326 VOP_RMDIR(dvp, &teardown, NULL, err2);
328 VOP_REMOVE(dvp, &teardown, NULL, err2);
332 _ACL_FREE(default_acl);
337 ip = LINVFS_GET_IP(vp);
339 if (S_ISCHR(mode) || S_ISBLK(mode))
341 else if (S_ISDIR(mode))
343 d_instantiate(dentry, ip);
344 validate_fields(dir);
352 struct dentry *dentry,
354 struct nameidata *nd)
356 return linvfs_mknod(dir, dentry, mode, 0);
362 struct dentry *dentry,
365 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
368 STATIC struct dentry *
371 struct dentry *dentry,
372 struct nameidata *nd)
374 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
377 if (dentry->d_name.len >= MAXNAMELEN)
378 return ERR_PTR(-ENAMETOOLONG);
380 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
382 if (unlikely(error != ENOENT))
383 return ERR_PTR(-error);
388 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
393 struct dentry *old_dentry,
395 struct dentry *dentry)
397 struct inode *ip; /* inode of guy being linked to */
398 vnode_t *tdvp; /* target directory for new name/link */
399 vnode_t *vp; /* vp of name being linked */
402 ip = old_dentry->d_inode; /* inode being linked to */
403 if (S_ISDIR(ip->i_mode))
406 tdvp = LINVFS_GET_VP(dir);
407 vp = LINVFS_GET_VP(ip);
409 VOP_LINK(tdvp, vp, dentry, NULL, error);
414 d_instantiate(dentry, ip);
422 struct dentry *dentry)
425 vnode_t *dvp; /* directory containing name to remove */
428 inode = dentry->d_inode;
429 dvp = LINVFS_GET_VP(dir);
431 VOP_REMOVE(dvp, dentry, NULL, error);
433 validate_fields(dir); /* For size only */
434 validate_fields(inode);
443 struct dentry *dentry,
448 vnode_t *dvp; /* directory containing name of symlink */
449 vnode_t *cvp; /* used to lookup symlink to put in dentry */
452 dvp = LINVFS_GET_VP(dir);
455 memset(&va, 0, sizeof(va));
456 va.va_mode = S_IFLNK |
457 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
458 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
461 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
463 ip = LINVFS_GET_IP(cvp);
464 d_instantiate(dentry, ip);
465 validate_fields(dir);
466 validate_fields(ip); /* size needs update */
474 struct dentry *dentry)
476 struct inode *inode = dentry->d_inode;
477 vnode_t *dvp = LINVFS_GET_VP(dir);
480 VOP_RMDIR(dvp, dentry, NULL, error);
482 validate_fields(inode);
483 validate_fields(dir);
491 struct dentry *odentry,
493 struct dentry *ndentry)
495 struct inode *new_inode = ndentry->d_inode;
496 vnode_t *fvp; /* from directory */
497 vnode_t *tvp; /* target directory */
500 fvp = LINVFS_GET_VP(odir);
501 tvp = LINVFS_GET_VP(ndir);
503 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
508 validate_fields(new_inode);
510 validate_fields(odir);
512 validate_fields(ndir);
517 * careful here - this function can get called recursively, so
518 * we need to be very careful about how much stack we use.
519 * uio is kmalloced for this reason...
523 struct dentry *dentry,
524 struct nameidata *nd)
535 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
537 nd_set_link(nd, ERR_PTR(-ENOMEM));
541 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
544 nd_set_link(nd, ERR_PTR(-ENOMEM));
548 vp = LINVFS_GET_VP(dentry->d_inode);
551 iov.iov_len = MAXNAMELEN;
555 uio->uio_segflg = UIO_SYSSPACE;
556 uio->uio_resid = MAXNAMELEN;
559 VOP_READLINK(vp, uio, 0, NULL, error);
562 link = ERR_PTR(-error);
564 link[MAXNAMELEN - uio->uio_resid] = '\0';
568 nd_set_link(nd, link);
574 struct dentry *dentry,
575 struct nameidata *nd,
578 char *s = nd_get_link(nd);
584 #ifdef CONFIG_XFS_POSIX_ACL
589 struct nameidata *nd)
591 vnode_t *vp = LINVFS_GET_VP(inode);
594 mode <<= 6; /* convert from linux to vnode access bits */
595 VOP_ACCESS(vp, mode, NULL, error);
599 #define linvfs_permission NULL
604 struct vfsmount *mnt,
605 struct dentry *dentry,
608 struct inode *inode = dentry->d_inode;
609 vnode_t *vp = LINVFS_GET_VP(inode);
612 if (unlikely(vp->v_flag & VMODIFIED))
613 error = vn_revalidate(vp);
615 generic_fillattr(inode, stat);
621 struct dentry *dentry,
624 struct inode *inode = dentry->d_inode;
625 unsigned int ia_valid = attr->ia_valid;
626 vnode_t *vp = LINVFS_GET_VP(inode);
631 memset(&vattr, 0, sizeof(vattr_t));
632 if (ia_valid & ATTR_UID) {
633 vattr.va_mask |= XFS_AT_UID;
634 vattr.va_uid = attr->ia_uid;
636 if (ia_valid & ATTR_GID) {
637 vattr.va_mask |= XFS_AT_GID;
638 vattr.va_gid = attr->ia_gid;
640 if (ia_valid & ATTR_SIZE) {
641 vattr.va_mask |= XFS_AT_SIZE;
642 vattr.va_size = attr->ia_size;
644 if (ia_valid & ATTR_ATIME) {
645 vattr.va_mask |= XFS_AT_ATIME;
646 vattr.va_atime = attr->ia_atime;
648 if (ia_valid & ATTR_MTIME) {
649 vattr.va_mask |= XFS_AT_MTIME;
650 vattr.va_mtime = attr->ia_mtime;
652 if (ia_valid & ATTR_CTIME) {
653 vattr.va_mask |= XFS_AT_CTIME;
654 vattr.va_ctime = attr->ia_ctime;
656 if (ia_valid & ATTR_MODE) {
657 vattr.va_mask |= XFS_AT_MODE;
658 vattr.va_mode = attr->ia_mode;
659 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
660 inode->i_mode &= ~S_ISGID;
663 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
666 if ((ia_valid & ATTR_NO_BLOCK))
667 flags |= ATTR_NONBLOCK;
670 VOP_SETATTR(vp, &vattr, flags, NULL, error);
681 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
686 struct dentry *dentry,
692 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
693 char *attr = (char *)name;
698 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
701 attr += namesp->attr_namelen;
702 error = namesp->attr_capable(vp, NULL);
706 /* Convert Linux syscall to XFS internal ATTR flags */
707 if (flags & XATTR_CREATE)
708 xflags |= ATTR_CREATE;
709 if (flags & XATTR_REPLACE)
710 xflags |= ATTR_REPLACE;
711 xflags |= namesp->attr_flag;
712 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
717 struct dentry *dentry,
722 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
723 char *attr = (char *)name;
728 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
731 attr += namesp->attr_namelen;
732 error = namesp->attr_capable(vp, NULL);
736 /* Convert Linux syscall to XFS internal ATTR flags */
738 xflags |= ATTR_KERNOVAL;
741 xflags |= namesp->attr_flag;
742 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
747 struct dentry *dentry,
751 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
752 int error, xflags = ATTR_KERNAMELS;
756 xflags |= ATTR_KERNOVAL;
757 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
759 error = attr_generic_list(vp, data, size, xflags, &result);
767 struct dentry *dentry,
770 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
771 char *attr = (char *)name;
776 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
779 attr += namesp->attr_namelen;
780 error = namesp->attr_capable(vp, NULL);
783 xflags |= namesp->attr_flag;
784 return namesp->attr_remove(vp, attr, xflags);
788 struct inode_operations linvfs_file_inode_operations = {
789 .permission = linvfs_permission,
790 .truncate = linvfs_truncate,
791 .getattr = linvfs_getattr,
792 .setattr = linvfs_setattr,
793 .setxattr = linvfs_setxattr,
794 .getxattr = linvfs_getxattr,
795 .listxattr = linvfs_listxattr,
796 .removexattr = linvfs_removexattr,
799 struct inode_operations linvfs_dir_inode_operations = {
800 .create = linvfs_create,
801 .lookup = linvfs_lookup,
803 .unlink = linvfs_unlink,
804 .symlink = linvfs_symlink,
805 .mkdir = linvfs_mkdir,
806 .rmdir = linvfs_rmdir,
807 .mknod = linvfs_mknod,
808 .rename = linvfs_rename,
809 .permission = linvfs_permission,
810 .getattr = linvfs_getattr,
811 .setattr = linvfs_setattr,
812 .setxattr = linvfs_setxattr,
813 .getxattr = linvfs_getxattr,
814 .listxattr = linvfs_listxattr,
815 .removexattr = linvfs_removexattr,
818 struct inode_operations linvfs_symlink_inode_operations = {
819 .readlink = generic_readlink,
820 .follow_link = linvfs_follow_link,
821 .put_link = linvfs_put_link,
822 .permission = linvfs_permission,
823 .getattr = linvfs_getattr,
824 .setattr = linvfs_setattr,
825 .setxattr = linvfs_setxattr,
826 .getxattr = linvfs_getxattr,
827 .listxattr = linvfs_listxattr,
828 .removexattr = linvfs_removexattr,