2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
37 #include "xfs_trans.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_utils.h"
69 #include <linux/xattr.h>
70 #include <linux/namei.h>
73 * Change the requested timestamp in the given inode.
74 * We don't lock across timestamp updates, and we don't log them but
75 * we do record the fact that there is dirty information in core.
77 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
78 * with XFS_ICHGTIME_ACC to be sure that access time
79 * update will take. Calling first with XFS_ICHGTIME_ACC
80 * and then XFS_ICHGTIME_MOD may fail to modify the access
81 * timestamp if the filesystem is mounted noacctm.
88 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
92 * We're not supposed to change timestamps in readonly-mounted
93 * filesystems. Throw it away if anyone asks us.
95 if (unlikely(IS_RDONLY(inode)))
99 * Don't update access timestamps on reads if mounted "noatime".
100 * Throw it away if anyone asks us.
103 (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
104 (flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
109 if (flags & XFS_ICHGTIME_MOD) {
111 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
112 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
114 if (flags & XFS_ICHGTIME_ACC) {
116 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
117 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
119 if (flags & XFS_ICHGTIME_CHG) {
121 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
122 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
126 * We update the i_update_core field _after_ changing
127 * the timestamps in order to coordinate properly with
128 * xfs_iflush() so that we don't lose timestamp updates.
129 * This keeps us from having to hold the inode lock
130 * while doing this. We use the SYNCHRONIZE macro to
131 * ensure that the compiler does not reorder the update
132 * of i_update_core above the timestamp updates above.
135 ip->i_update_core = 1;
136 if (!(inode->i_state & I_LOCK))
137 mark_inode_dirty_sync(inode);
141 * Variant on the above which avoids querying the system clock
142 * in situations where we know the Linux inode timestamps have
143 * just been updated (and so we can update our inode cheaply).
144 * We also skip the readonly and noatime checks here, they are
145 * also catered for already.
156 * We're not supposed to change timestamps in readonly-mounted
157 * filesystems. Throw it away if anyone asks us.
159 if (unlikely(IS_RDONLY(inode)))
163 * Don't update access timestamps on reads if mounted "noatime".
164 * Throw it away if anyone asks us.
167 (ip->i_mount->m_flags & XFS_MOUNT_NOATIME || IS_NOATIME(inode)) &&
168 ((flags & (XFS_ICHGTIME_ACC|XFS_ICHGTIME_MOD|XFS_ICHGTIME_CHG)) ==
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_ACC) {
178 tvp = &inode->i_atime;
179 ip->i_d.di_atime.t_sec = (__int32_t)tvp->tv_sec;
180 ip->i_d.di_atime.t_nsec = (__int32_t)tvp->tv_nsec;
182 if (flags & XFS_ICHGTIME_CHG) {
183 tvp = &inode->i_ctime;
184 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
185 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
189 * We update the i_update_core field _after_ changing
190 * the timestamps in order to coordinate properly with
191 * xfs_iflush() so that we don't lose timestamp updates.
192 * This keeps us from having to hold the inode lock
193 * while doing this. We use the SYNCHRONIZE macro to
194 * ensure that the compiler does not reorder the update
195 * of i_update_core above the timestamp updates above.
198 ip->i_update_core = 1;
199 if (!(inode->i_state & I_LOCK))
200 mark_inode_dirty_sync(inode);
205 * Pull the link count and size up from the xfs inode to the linux inode
211 vnode_t *vp = LINVFS_GET_VP(ip);
215 va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
216 VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
217 if (likely(!error)) {
218 ip->i_nlink = va.va_nlink;
219 ip->i_blocks = va.va_nblocks;
221 /* we're under i_sem so i_size can't change under us */
222 if (i_size_read(ip) != va.va_size)
223 i_size_write(ip, va.va_size);
228 * Determine whether a process has a valid fs_struct (kernel daemons
229 * like knfsd don't have an fs_struct).
231 * XXX(hch): nfsd is broken, better fix it instead.
234 has_fs_struct(struct task_struct *task)
236 return (task->fs != init_task.fs);
242 struct dentry *dentry,
248 vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
249 xfs_acl_t *default_acl = NULL;
250 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
254 * Irix uses Missed'em'V split, but doesn't want to see
255 * the upper 5 bits of (14bit) major.
257 if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
260 if (test_default_acl && test_default_acl(dvp)) {
261 if (!_ACL_ALLOC(default_acl))
263 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
264 _ACL_FREE(default_acl);
269 if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
270 mode &= ~current->fs->umask;
272 memset(&va, 0, sizeof(va));
273 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
276 switch (mode & S_IFMT) {
277 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
278 va.va_rdev = sysv_encode_dev(rdev);
279 va.va_mask |= XFS_AT_RDEV;
282 VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
285 VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
294 error = _ACL_INHERIT(vp, &va, default_acl);
298 struct dentry teardown = {};
302 * If we can't add the ACL we must back out.
303 * ENOSPC can hit here, among other things.
305 teardown.d_inode = ip = LINVFS_GET_IP(vp);
306 teardown.d_name = dentry->d_name;
311 VOP_RMDIR(dvp, &teardown, NULL, err2);
313 VOP_REMOVE(dvp, &teardown, NULL, err2);
317 _ACL_FREE(default_acl);
322 ip = LINVFS_GET_IP(vp);
324 if (S_ISCHR(mode) || S_ISBLK(mode))
326 else if (S_ISDIR(mode))
328 d_instantiate(dentry, ip);
329 validate_fields(dir);
337 struct dentry *dentry,
339 struct nameidata *nd)
341 return linvfs_mknod(dir, dentry, mode, 0);
347 struct dentry *dentry,
350 return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
353 STATIC struct dentry *
356 struct dentry *dentry,
357 struct nameidata *nd)
359 struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
362 if (dentry->d_name.len >= MAXNAMELEN)
363 return ERR_PTR(-ENAMETOOLONG);
365 VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
367 if (unlikely(error != ENOENT))
368 return ERR_PTR(-error);
373 return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
378 struct dentry *old_dentry,
380 struct dentry *dentry)
382 struct inode *ip; /* inode of guy being linked to */
383 vnode_t *tdvp; /* target directory for new name/link */
384 vnode_t *vp; /* vp of name being linked */
387 ip = old_dentry->d_inode; /* inode being linked to */
388 if (S_ISDIR(ip->i_mode))
391 tdvp = LINVFS_GET_VP(dir);
392 vp = LINVFS_GET_VP(ip);
394 VOP_LINK(tdvp, vp, dentry, NULL, error);
399 d_instantiate(dentry, ip);
407 struct dentry *dentry)
410 vnode_t *dvp; /* directory containing name to remove */
413 inode = dentry->d_inode;
414 dvp = LINVFS_GET_VP(dir);
416 VOP_REMOVE(dvp, dentry, NULL, error);
418 validate_fields(dir); /* For size only */
419 validate_fields(inode);
428 struct dentry *dentry,
433 vnode_t *dvp; /* directory containing name of symlink */
434 vnode_t *cvp; /* used to lookup symlink to put in dentry */
437 dvp = LINVFS_GET_VP(dir);
440 memset(&va, 0, sizeof(va));
441 va.va_mode = S_IFLNK |
442 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
443 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
446 VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
448 ip = LINVFS_GET_IP(cvp);
449 d_instantiate(dentry, ip);
450 validate_fields(dir);
451 validate_fields(ip); /* size needs update */
459 struct dentry *dentry)
461 struct inode *inode = dentry->d_inode;
462 vnode_t *dvp = LINVFS_GET_VP(dir);
465 VOP_RMDIR(dvp, dentry, NULL, error);
467 validate_fields(inode);
468 validate_fields(dir);
476 struct dentry *odentry,
478 struct dentry *ndentry)
480 struct inode *new_inode = ndentry->d_inode;
481 vnode_t *fvp; /* from directory */
482 vnode_t *tvp; /* target directory */
485 fvp = LINVFS_GET_VP(odir);
486 tvp = LINVFS_GET_VP(ndir);
488 VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
493 validate_fields(new_inode);
495 validate_fields(odir);
497 validate_fields(ndir);
502 * careful here - this function can get called recursively, so
503 * we need to be very careful about how much stack we use.
504 * uio is kmalloced for this reason...
508 struct dentry *dentry,
509 struct nameidata *nd)
520 link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
522 nd_set_link(nd, ERR_PTR(-ENOMEM));
526 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
529 nd_set_link(nd, ERR_PTR(-ENOMEM));
533 vp = LINVFS_GET_VP(dentry->d_inode);
536 iov.iov_len = MAXNAMELEN;
540 uio->uio_segflg = UIO_SYSSPACE;
541 uio->uio_resid = MAXNAMELEN;
544 VOP_READLINK(vp, uio, 0, NULL, error);
547 link = ERR_PTR(-error);
549 link[MAXNAMELEN - uio->uio_resid] = '\0';
553 nd_set_link(nd, link);
559 struct dentry *dentry,
560 struct nameidata *nd,
563 char *s = nd_get_link(nd);
569 #ifdef CONFIG_XFS_POSIX_ACL
574 struct nameidata *nd)
576 vnode_t *vp = LINVFS_GET_VP(inode);
579 mode <<= 6; /* convert from linux to vnode access bits */
580 VOP_ACCESS(vp, mode, NULL, error);
584 #define linvfs_permission NULL
589 struct vfsmount *mnt,
590 struct dentry *dentry,
593 struct inode *inode = dentry->d_inode;
594 vnode_t *vp = LINVFS_GET_VP(inode);
597 if (unlikely(vp->v_flag & VMODIFIED))
598 error = vn_revalidate(vp);
600 generic_fillattr(inode, stat);
606 struct dentry *dentry,
609 struct inode *inode = dentry->d_inode;
610 unsigned int ia_valid = attr->ia_valid;
611 vnode_t *vp = LINVFS_GET_VP(inode);
616 memset(&vattr, 0, sizeof(vattr_t));
617 if (ia_valid & ATTR_UID) {
618 vattr.va_mask |= XFS_AT_UID;
619 vattr.va_uid = attr->ia_uid;
621 if (ia_valid & ATTR_GID) {
622 vattr.va_mask |= XFS_AT_GID;
623 vattr.va_gid = attr->ia_gid;
625 if (ia_valid & ATTR_SIZE) {
626 vattr.va_mask |= XFS_AT_SIZE;
627 vattr.va_size = attr->ia_size;
629 if (ia_valid & ATTR_ATIME) {
630 vattr.va_mask |= XFS_AT_ATIME;
631 vattr.va_atime = attr->ia_atime;
633 if (ia_valid & ATTR_MTIME) {
634 vattr.va_mask |= XFS_AT_MTIME;
635 vattr.va_mtime = attr->ia_mtime;
637 if (ia_valid & ATTR_CTIME) {
638 vattr.va_mask |= XFS_AT_CTIME;
639 vattr.va_ctime = attr->ia_ctime;
641 if (ia_valid & ATTR_MODE) {
642 vattr.va_mask |= XFS_AT_MODE;
643 vattr.va_mode = attr->ia_mode;
644 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
645 inode->i_mode &= ~S_ISGID;
648 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
651 if ((ia_valid & ATTR_NO_BLOCK))
652 flags |= ATTR_NONBLOCK;
655 VOP_SETATTR(vp, &vattr, flags, NULL, error);
666 block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
671 struct dentry *dentry,
677 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
678 char *attr = (char *)name;
683 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
686 attr += namesp->attr_namelen;
687 error = namesp->attr_capable(vp, NULL);
691 /* Convert Linux syscall to XFS internal ATTR flags */
692 if (flags & XATTR_CREATE)
693 xflags |= ATTR_CREATE;
694 if (flags & XATTR_REPLACE)
695 xflags |= ATTR_REPLACE;
696 xflags |= namesp->attr_flag;
697 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
702 struct dentry *dentry,
707 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
708 char *attr = (char *)name;
713 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
716 attr += namesp->attr_namelen;
717 error = namesp->attr_capable(vp, NULL);
721 /* Convert Linux syscall to XFS internal ATTR flags */
723 xflags |= ATTR_KERNOVAL;
726 xflags |= namesp->attr_flag;
727 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
732 struct dentry *dentry,
736 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
737 int error, xflags = ATTR_KERNAMELS;
741 xflags |= ATTR_KERNOVAL;
742 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
744 error = attr_generic_list(vp, data, size, xflags, &result);
752 struct dentry *dentry,
755 vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
756 char *attr = (char *)name;
761 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
764 attr += namesp->attr_namelen;
765 error = namesp->attr_capable(vp, NULL);
768 xflags |= namesp->attr_flag;
769 return namesp->attr_remove(vp, attr, xflags);
773 struct inode_operations linvfs_file_inode_operations = {
774 .permission = linvfs_permission,
775 .truncate = linvfs_truncate,
776 .getattr = linvfs_getattr,
777 .setattr = linvfs_setattr,
778 .setxattr = linvfs_setxattr,
779 .getxattr = linvfs_getxattr,
780 .listxattr = linvfs_listxattr,
781 .removexattr = linvfs_removexattr,
784 struct inode_operations linvfs_dir_inode_operations = {
785 .create = linvfs_create,
786 .lookup = linvfs_lookup,
788 .unlink = linvfs_unlink,
789 .symlink = linvfs_symlink,
790 .mkdir = linvfs_mkdir,
791 .rmdir = linvfs_rmdir,
792 .mknod = linvfs_mknod,
793 .rename = linvfs_rename,
794 .permission = linvfs_permission,
795 .getattr = linvfs_getattr,
796 .setattr = linvfs_setattr,
797 .setxattr = linvfs_setxattr,
798 .getxattr = linvfs_getxattr,
799 .listxattr = linvfs_listxattr,
800 .removexattr = linvfs_removexattr,
803 struct inode_operations linvfs_symlink_inode_operations = {
804 .readlink = generic_readlink,
805 .follow_link = linvfs_follow_link,
806 .put_link = linvfs_put_link,
807 .permission = linvfs_permission,
808 .getattr = linvfs_getattr,
809 .setattr = linvfs_setattr,
810 .setxattr = linvfs_setxattr,
811 .getxattr = linvfs_getxattr,
812 .listxattr = linvfs_listxattr,
813 .removexattr = linvfs_removexattr,