Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
| 19 | #include "xfs_fs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_trans.h" |
| 24 | #include "xfs_sb.h" |
| 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_dir2.h" |
| 27 | #include "xfs_alloc.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_quota.h" |
| 30 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_dinode.h" |
| 37 | #include "xfs_inode.h" |
| 38 | #include "xfs_bmap.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_btree.h" |
| 40 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_rtalloc.h" |
| 42 | #include "xfs_error.h" |
| 43 | #include "xfs_itable.h" |
| 44 | #include "xfs_rw.h" |
| 45 | #include "xfs_acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include "xfs_attr.h" |
| 47 | #include "xfs_buf_item.h" |
| 48 | #include "xfs_utils.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 49 | #include "xfs_vnodeops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 51 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/xattr.h> |
| 53 | #include <linux/namei.h> |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 54 | #include <linux/security.h> |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 55 | #include <linux/falloc.h> |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 56 | #include <linux/fiemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 58 | /* |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 59 | * Bring the atime in the XFS inode uptodate. |
| 60 | * Used before logging the inode to disk or when the Linux inode goes away. |
| 61 | */ |
| 62 | void |
| 63 | xfs_synchronize_atime( |
| 64 | xfs_inode_t *ip) |
| 65 | { |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 66 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 67 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 68 | if (!(inode->i_state & I_CLEAR)) { |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 69 | ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec; |
| 70 | ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec; |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /* |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 75 | * If the linux inode is valid, mark it dirty. |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 76 | * Used when commiting a dirty inode into a transaction so that |
| 77 | * the inode will get written back by the linux code |
| 78 | */ |
| 79 | void |
| 80 | xfs_mark_inode_dirty_sync( |
| 81 | xfs_inode_t *ip) |
| 82 | { |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 83 | struct inode *inode = VFS_I(ip); |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 84 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 85 | if (!(inode->i_state & (I_WILL_FREE|I_FREEING|I_CLEAR))) |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 86 | mark_inode_dirty_sync(inode); |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 90 | * Change the requested timestamp in the given inode. |
| 91 | * We don't lock across timestamp updates, and we don't log them but |
| 92 | * we do record the fact that there is dirty information in core. |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 93 | */ |
| 94 | void |
| 95 | xfs_ichgtime( |
| 96 | xfs_inode_t *ip, |
| 97 | int flags) |
| 98 | { |
David Chinner | e4f7529 | 2008-08-13 16:00:45 +1000 | [diff] [blame] | 99 | struct inode *inode = VFS_I(ip); |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 100 | timespec_t tv; |
Christoph Hellwig | 8e5975c | 2008-08-13 16:45:13 +1000 | [diff] [blame] | 101 | int sync_it = 0; |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 102 | |
Christoph Hellwig | 8e5975c | 2008-08-13 16:45:13 +1000 | [diff] [blame] | 103 | tv = current_fs_time(inode->i_sb); |
| 104 | |
| 105 | if ((flags & XFS_ICHGTIME_MOD) && |
| 106 | !timespec_equal(&inode->i_mtime, &tv)) { |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 107 | inode->i_mtime = tv; |
| 108 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; |
| 109 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; |
Christoph Hellwig | 8e5975c | 2008-08-13 16:45:13 +1000 | [diff] [blame] | 110 | sync_it = 1; |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 111 | } |
Christoph Hellwig | 8e5975c | 2008-08-13 16:45:13 +1000 | [diff] [blame] | 112 | if ((flags & XFS_ICHGTIME_CHG) && |
| 113 | !timespec_equal(&inode->i_ctime, &tv)) { |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 114 | inode->i_ctime = tv; |
| 115 | ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec; |
| 116 | ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec; |
Christoph Hellwig | 8e5975c | 2008-08-13 16:45:13 +1000 | [diff] [blame] | 117 | sync_it = 1; |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * We update the i_update_core field _after_ changing |
| 122 | * the timestamps in order to coordinate properly with |
| 123 | * xfs_iflush() so that we don't lose timestamp updates. |
| 124 | * This keeps us from having to hold the inode lock |
| 125 | * while doing this. We use the SYNCHRONIZE macro to |
| 126 | * ensure that the compiler does not reorder the update |
| 127 | * of i_update_core above the timestamp updates above. |
| 128 | */ |
Christoph Hellwig | 8e5975c | 2008-08-13 16:45:13 +1000 | [diff] [blame] | 129 | if (sync_it) { |
| 130 | SYNCHRONIZE(); |
| 131 | ip->i_update_core = 1; |
David Chinner | 94b97e3 | 2008-10-30 17:21:30 +1100 | [diff] [blame] | 132 | xfs_mark_inode_dirty_sync(ip); |
Christoph Hellwig | 8e5975c | 2008-08-13 16:45:13 +1000 | [diff] [blame] | 133 | } |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 137 | * Hook in SELinux. This is not quite correct yet, what we really need |
| 138 | * here (as we do for default ACLs) is a mechanism by which creation of |
| 139 | * these attrs can be journalled at inode creation time (along with the |
| 140 | * inode, of course, such that log replay can't cause these to be lost). |
| 141 | */ |
| 142 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 143 | xfs_init_security( |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 144 | struct inode *inode, |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 145 | struct inode *dir) |
| 146 | { |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 147 | struct xfs_inode *ip = XFS_I(inode); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 148 | size_t length; |
| 149 | void *value; |
| 150 | char *name; |
| 151 | int error; |
| 152 | |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 153 | error = security_inode_init_security(inode, dir, &name, |
| 154 | &value, &length); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 155 | if (error) { |
| 156 | if (error == -EOPNOTSUPP) |
| 157 | return 0; |
| 158 | return -error; |
| 159 | } |
| 160 | |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 161 | error = xfs_attr_set(ip, name, value, length, ATTR_SECURE); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 162 | if (!error) |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 163 | xfs_iflags_set(ip, XFS_IMODIFIED); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 164 | |
| 165 | kfree(name); |
| 166 | kfree(value); |
| 167 | return error; |
| 168 | } |
| 169 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 170 | static void |
| 171 | xfs_dentry_to_name( |
| 172 | struct xfs_name *namep, |
| 173 | struct dentry *dentry) |
| 174 | { |
| 175 | namep->name = dentry->d_name.name; |
| 176 | namep->len = dentry->d_name.len; |
| 177 | } |
| 178 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 179 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 180 | xfs_cleanup_inode( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 181 | struct inode *dir, |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 182 | struct inode *inode, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 183 | struct dentry *dentry) |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 184 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 185 | struct xfs_name teardown; |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 186 | |
| 187 | /* Oh, the horror. |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 188 | * If we can't add the ACL or we fail in |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 189 | * xfs_init_security we must back out. |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 190 | * ENOSPC can hit here, among other things. |
| 191 | */ |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 192 | xfs_dentry_to_name(&teardown, dentry); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 193 | |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 194 | xfs_remove(XFS_I(dir), &teardown, XFS_I(inode)); |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 195 | iput(inode); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 199 | xfs_vn_mknod( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | struct inode *dir, |
| 201 | struct dentry *dentry, |
| 202 | int mode, |
| 203 | dev_t rdev) |
| 204 | { |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 205 | struct inode *inode; |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 206 | struct xfs_inode *ip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | xfs_acl_t *default_acl = NULL; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 208 | struct xfs_name name; |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 209 | int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | int error; |
| 211 | |
| 212 | /* |
| 213 | * Irix uses Missed'em'V split, but doesn't want to see |
| 214 | * the upper 5 bits of (14bit) major. |
| 215 | */ |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 216 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 219 | if (test_default_acl && test_default_acl(dir)) { |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 220 | if (!_ACL_ALLOC(default_acl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return -ENOMEM; |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 222 | } |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 223 | if (!_ACL_GET_DEFAULT(dir, default_acl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | _ACL_FREE(default_acl); |
| 225 | default_acl = NULL; |
| 226 | } |
| 227 | } |
| 228 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 229 | xfs_dentry_to_name(&name, dentry); |
| 230 | |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 231 | if (IS_POSIXACL(dir) && !default_acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | mode &= ~current->fs->umask; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | switch (mode & S_IFMT) { |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 235 | case S_IFCHR: |
| 236 | case S_IFBLK: |
| 237 | case S_IFIFO: |
| 238 | case S_IFSOCK: |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 239 | rdev = sysv_encode_dev(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | case S_IFREG: |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 241 | error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | break; |
| 243 | case S_IFDIR: |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 244 | error = xfs_mkdir(XFS_I(dir), &name, mode, &ip, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | break; |
| 246 | default: |
| 247 | error = EINVAL; |
| 248 | break; |
| 249 | } |
| 250 | |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 251 | if (unlikely(error)) |
| 252 | goto out_free_acl; |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 253 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 254 | inode = VFS_I(ip); |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 255 | |
| 256 | error = xfs_init_security(inode, dir); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 257 | if (unlikely(error)) |
| 258 | goto out_cleanup_inode; |
| 259 | |
| 260 | if (default_acl) { |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 261 | error = _ACL_INHERIT(inode, mode, default_acl); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 262 | if (unlikely(error)) |
| 263 | goto out_cleanup_inode; |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 264 | xfs_iflags_set(ip, XFS_IMODIFIED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | _ACL_FREE(default_acl); |
| 266 | } |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 269 | d_instantiate(dentry, inode); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 270 | return -error; |
| 271 | |
| 272 | out_cleanup_inode: |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 273 | xfs_cleanup_inode(dir, inode, dentry); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 274 | out_free_acl: |
| 275 | if (default_acl) |
| 276 | _ACL_FREE(default_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return -error; |
| 278 | } |
| 279 | |
| 280 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 281 | xfs_vn_create( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | struct inode *dir, |
| 283 | struct dentry *dentry, |
| 284 | int mode, |
| 285 | struct nameidata *nd) |
| 286 | { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 287 | return xfs_vn_mknod(dir, dentry, mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 291 | xfs_vn_mkdir( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | struct inode *dir, |
| 293 | struct dentry *dentry, |
| 294 | int mode) |
| 295 | { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 296 | return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | STATIC struct dentry * |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 300 | xfs_vn_lookup( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | struct inode *dir, |
| 302 | struct dentry *dentry, |
| 303 | struct nameidata *nd) |
| 304 | { |
Christoph Hellwig | ef1f5e7 | 2008-03-06 13:46:25 +1100 | [diff] [blame] | 305 | struct xfs_inode *cip; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 306 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | int error; |
| 308 | |
| 309 | if (dentry->d_name.len >= MAXNAMELEN) |
| 310 | return ERR_PTR(-ENAMETOOLONG); |
| 311 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 312 | xfs_dentry_to_name(&name, dentry); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 313 | error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 314 | if (unlikely(error)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | if (unlikely(error != ENOENT)) |
| 316 | return ERR_PTR(-error); |
| 317 | d_add(dentry, NULL); |
| 318 | return NULL; |
| 319 | } |
| 320 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 321 | return d_splice_alias(VFS_I(cip), dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 324 | STATIC struct dentry * |
| 325 | xfs_vn_ci_lookup( |
| 326 | struct inode *dir, |
| 327 | struct dentry *dentry, |
| 328 | struct nameidata *nd) |
| 329 | { |
| 330 | struct xfs_inode *ip; |
| 331 | struct xfs_name xname; |
| 332 | struct xfs_name ci_name; |
| 333 | struct qstr dname; |
| 334 | int error; |
| 335 | |
| 336 | if (dentry->d_name.len >= MAXNAMELEN) |
| 337 | return ERR_PTR(-ENAMETOOLONG); |
| 338 | |
| 339 | xfs_dentry_to_name(&xname, dentry); |
| 340 | error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name); |
| 341 | if (unlikely(error)) { |
| 342 | if (unlikely(error != ENOENT)) |
| 343 | return ERR_PTR(-error); |
Barry Naujok | 866d5dc | 2008-05-22 17:21:40 +1000 | [diff] [blame] | 344 | /* |
| 345 | * call d_add(dentry, NULL) here when d_drop_negative_children |
| 346 | * is called in xfs_vn_mknod (ie. allow negative dentries |
| 347 | * with CI filesystems). |
| 348 | */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | /* if exact match, just splice and exit */ |
| 353 | if (!ci_name.name) |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 354 | return d_splice_alias(VFS_I(ip), dentry); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 355 | |
| 356 | /* else case-insensitive match... */ |
| 357 | dname.name = ci_name.name; |
| 358 | dname.len = ci_name.len; |
Christoph Hellwig | e45b590 | 2008-08-07 23:49:07 +0200 | [diff] [blame] | 359 | dentry = d_add_ci(dentry, VFS_I(ip), &dname); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 360 | kmem_free(ci_name.name); |
| 361 | return dentry; |
| 362 | } |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 365 | xfs_vn_link( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | struct dentry *old_dentry, |
| 367 | struct inode *dir, |
| 368 | struct dentry *dentry) |
| 369 | { |
Christoph Hellwig | d9424b3 | 2008-12-03 12:20:27 +0100 | [diff] [blame^] | 370 | struct inode *inode = old_dentry->d_inode; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 371 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | int error; |
| 373 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 374 | xfs_dentry_to_name(&name, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 376 | error = xfs_link(XFS_I(dir), XFS_I(inode), &name); |
Christoph Hellwig | d9424b3 | 2008-12-03 12:20:27 +0100 | [diff] [blame^] | 377 | if (unlikely(error)) |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 378 | return -error; |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 379 | |
| 380 | xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED); |
Christoph Hellwig | d9424b3 | 2008-12-03 12:20:27 +0100 | [diff] [blame^] | 381 | atomic_inc(&inode->i_count); |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 382 | d_instantiate(dentry, inode); |
| 383 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 387 | xfs_vn_unlink( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | struct inode *dir, |
| 389 | struct dentry *dentry) |
| 390 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 391 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | int error; |
| 393 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 394 | xfs_dentry_to_name(&name, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Christoph Hellwig | e570070 | 2008-06-23 13:25:25 +1000 | [diff] [blame] | 396 | error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode)); |
| 397 | if (error) |
| 398 | return error; |
| 399 | |
| 400 | /* |
| 401 | * With unlink, the VFS makes the dentry "negative": no inode, |
| 402 | * but still hashed. This is incompatible with case-insensitive |
| 403 | * mode, so invalidate (unhash) the dentry in CI-mode. |
| 404 | */ |
| 405 | if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb)) |
| 406 | d_invalidate(dentry); |
| 407 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 411 | xfs_vn_symlink( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | struct inode *dir, |
| 413 | struct dentry *dentry, |
| 414 | const char *symname) |
| 415 | { |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 416 | struct inode *inode; |
| 417 | struct xfs_inode *cip = NULL; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 418 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | int error; |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 420 | mode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 422 | mode = S_IFLNK | |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 423 | (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 424 | xfs_dentry_to_name(&name, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 426 | error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 427 | if (unlikely(error)) |
| 428 | goto out; |
| 429 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 430 | inode = VFS_I(cip); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 431 | |
| 432 | error = xfs_init_security(inode, dir); |
| 433 | if (unlikely(error)) |
| 434 | goto out_cleanup_inode; |
| 435 | |
| 436 | d_instantiate(dentry, inode); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 437 | return 0; |
| 438 | |
| 439 | out_cleanup_inode: |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 440 | xfs_cleanup_inode(dir, inode, dentry); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 441 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return -error; |
| 443 | } |
| 444 | |
| 445 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 446 | xfs_vn_rename( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | struct inode *odir, |
| 448 | struct dentry *odentry, |
| 449 | struct inode *ndir, |
| 450 | struct dentry *ndentry) |
| 451 | { |
| 452 | struct inode *new_inode = ndentry->d_inode; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 453 | struct xfs_name oname; |
| 454 | struct xfs_name nname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 456 | xfs_dentry_to_name(&oname, odentry); |
| 457 | xfs_dentry_to_name(&nname, ndentry); |
| 458 | |
Christoph Hellwig | e570070 | 2008-06-23 13:25:25 +1000 | [diff] [blame] | 459 | return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode), |
Christoph Hellwig | cfa853e | 2008-04-22 17:34:06 +1000 | [diff] [blame] | 460 | XFS_I(ndir), &nname, new_inode ? |
| 461 | XFS_I(new_inode) : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* |
| 465 | * careful here - this function can get called recursively, so |
| 466 | * we need to be very careful about how much stack we use. |
| 467 | * uio is kmalloced for this reason... |
| 468 | */ |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 469 | STATIC void * |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 470 | xfs_vn_follow_link( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | struct dentry *dentry, |
| 472 | struct nameidata *nd) |
| 473 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | char *link; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 475 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 477 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 478 | if (!link) |
| 479 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 481 | error = -xfs_readlink(XFS_I(dentry->d_inode), link); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 482 | if (unlikely(error)) |
| 483 | goto out_kfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | nd_set_link(nd, link); |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 486 | return NULL; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 487 | |
| 488 | out_kfree: |
| 489 | kfree(link); |
| 490 | out_err: |
| 491 | nd_set_link(nd, ERR_PTR(error)); |
| 492 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 495 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 496 | xfs_vn_put_link( |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 497 | struct dentry *dentry, |
| 498 | struct nameidata *nd, |
| 499 | void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 501 | char *s = nd_get_link(nd); |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | if (!IS_ERR(s)) |
| 504 | kfree(s); |
| 505 | } |
| 506 | |
| 507 | #ifdef CONFIG_XFS_POSIX_ACL |
| 508 | STATIC int |
Christoph Hellwig | 4576758 | 2008-02-05 12:13:24 +1100 | [diff] [blame] | 509 | xfs_check_acl( |
| 510 | struct inode *inode, |
| 511 | int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
Christoph Hellwig | 4576758 | 2008-02-05 12:13:24 +1100 | [diff] [blame] | 513 | struct xfs_inode *ip = XFS_I(inode); |
| 514 | int error; |
| 515 | |
| 516 | xfs_itrace_entry(ip); |
| 517 | |
| 518 | if (XFS_IFORK_Q(ip)) { |
| 519 | error = xfs_acl_iaccess(ip, mask, NULL); |
| 520 | if (error != -1) |
| 521 | return -error; |
| 522 | } |
| 523 | |
| 524 | return -EAGAIN; |
| 525 | } |
| 526 | |
| 527 | STATIC int |
| 528 | xfs_vn_permission( |
| 529 | struct inode *inode, |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 530 | int mask) |
Christoph Hellwig | 4576758 | 2008-02-05 12:13:24 +1100 | [diff] [blame] | 531 | { |
| 532 | return generic_permission(inode, mask, xfs_check_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | #else |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 535 | #define xfs_vn_permission NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | #endif |
| 537 | |
| 538 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 539 | xfs_vn_getattr( |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 540 | struct vfsmount *mnt, |
| 541 | struct dentry *dentry, |
| 542 | struct kstat *stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 544 | struct inode *inode = dentry->d_inode; |
| 545 | struct xfs_inode *ip = XFS_I(inode); |
| 546 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 548 | xfs_itrace_entry(ip); |
| 549 | |
| 550 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 551 | return XFS_ERROR(EIO); |
| 552 | |
| 553 | stat->size = XFS_ISIZE(ip); |
| 554 | stat->dev = inode->i_sb->s_dev; |
| 555 | stat->mode = ip->i_d.di_mode; |
| 556 | stat->nlink = ip->i_d.di_nlink; |
| 557 | stat->uid = ip->i_d.di_uid; |
| 558 | stat->gid = ip->i_d.di_gid; |
| 559 | stat->ino = ip->i_ino; |
| 560 | #if XFS_BIG_INUMS |
| 561 | stat->ino += mp->m_inoadd; |
| 562 | #endif |
| 563 | stat->atime = inode->i_atime; |
| 564 | stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec; |
| 565 | stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; |
| 566 | stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec; |
| 567 | stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; |
| 568 | stat->blocks = |
| 569 | XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); |
| 570 | |
| 571 | |
| 572 | switch (inode->i_mode & S_IFMT) { |
| 573 | case S_IFBLK: |
| 574 | case S_IFCHR: |
| 575 | stat->blksize = BLKDEV_IOSIZE; |
| 576 | stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, |
| 577 | sysv_minor(ip->i_df.if_u2.if_rdev)); |
| 578 | break; |
| 579 | default: |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 580 | if (XFS_IS_REALTIME_INODE(ip)) { |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 581 | /* |
| 582 | * If the file blocks are being allocated from a |
| 583 | * realtime volume, then return the inode's realtime |
| 584 | * extent size or the realtime volume's extent size. |
| 585 | */ |
| 586 | stat->blksize = |
| 587 | xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; |
| 588 | } else |
| 589 | stat->blksize = xfs_preferred_iosize(mp); |
| 590 | stat->rdev = 0; |
| 591 | break; |
Nathan Scott | 69e23b9 | 2006-09-28 11:01:22 +1000 | [diff] [blame] | 592 | } |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 593 | |
| 594 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 598 | xfs_vn_setattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | struct dentry *dentry, |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 600 | struct iattr *iattr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Christoph Hellwig | ea5a3dc8 | 2008-10-30 18:27:48 +1100 | [diff] [blame] | 602 | return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
David Chinner | d87dd63 | 2008-04-10 12:21:46 +1000 | [diff] [blame] | 605 | /* |
| 606 | * block_truncate_page can return an error, but we can't propagate it |
| 607 | * at all here. Leave a complaint + stack trace in the syslog because |
| 608 | * this could be bad. If it is bad, we need to propagate the error further. |
| 609 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 611 | xfs_vn_truncate( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | struct inode *inode) |
| 613 | { |
David Chinner | d87dd63 | 2008-04-10 12:21:46 +1000 | [diff] [blame] | 614 | int error; |
| 615 | error = block_truncate_page(inode->i_mapping, inode->i_size, |
| 616 | xfs_get_blocks); |
| 617 | WARN_ON(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 620 | STATIC long |
| 621 | xfs_vn_fallocate( |
| 622 | struct inode *inode, |
| 623 | int mode, |
| 624 | loff_t offset, |
| 625 | loff_t len) |
| 626 | { |
| 627 | long error; |
| 628 | loff_t new_size = 0; |
| 629 | xfs_flock64_t bf; |
| 630 | xfs_inode_t *ip = XFS_I(inode); |
| 631 | |
| 632 | /* preallocation on directories not yet supported */ |
| 633 | error = -ENODEV; |
| 634 | if (S_ISDIR(inode->i_mode)) |
| 635 | goto out_error; |
| 636 | |
| 637 | bf.l_whence = 0; |
| 638 | bf.l_start = offset; |
| 639 | bf.l_len = len; |
| 640 | |
| 641 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 642 | error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf, |
Christoph Hellwig | ea5a3dc8 | 2008-10-30 18:27:48 +1100 | [diff] [blame] | 643 | 0, XFS_ATTR_NOLOCK); |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 644 | if (!error && !(mode & FALLOC_FL_KEEP_SIZE) && |
| 645 | offset + len > i_size_read(inode)) |
| 646 | new_size = offset + len; |
| 647 | |
| 648 | /* Change file size if needed */ |
| 649 | if (new_size) { |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 650 | struct iattr iattr; |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 651 | |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 652 | iattr.ia_valid = ATTR_SIZE; |
| 653 | iattr.ia_size = new_size; |
Christoph Hellwig | ea5a3dc8 | 2008-10-30 18:27:48 +1100 | [diff] [blame] | 654 | error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK); |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 658 | out_error: |
| 659 | return error; |
| 660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 662 | #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) |
| 663 | |
| 664 | /* |
| 665 | * Call fiemap helper to fill in user data. |
| 666 | * Returns positive errors to xfs_getbmap. |
| 667 | */ |
| 668 | STATIC int |
| 669 | xfs_fiemap_format( |
| 670 | void **arg, |
| 671 | struct getbmapx *bmv, |
| 672 | int *full) |
| 673 | { |
| 674 | int error; |
| 675 | struct fiemap_extent_info *fieinfo = *arg; |
| 676 | u32 fiemap_flags = 0; |
| 677 | u64 logical, physical, length; |
| 678 | |
| 679 | /* Do nothing for a hole */ |
| 680 | if (bmv->bmv_block == -1LL) |
| 681 | return 0; |
| 682 | |
| 683 | logical = BBTOB(bmv->bmv_offset); |
| 684 | physical = BBTOB(bmv->bmv_block); |
| 685 | length = BBTOB(bmv->bmv_length); |
| 686 | |
| 687 | if (bmv->bmv_oflags & BMV_OF_PREALLOC) |
| 688 | fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 689 | else if (bmv->bmv_oflags & BMV_OF_DELALLOC) { |
| 690 | fiemap_flags |= FIEMAP_EXTENT_DELALLOC; |
| 691 | physical = 0; /* no block yet */ |
| 692 | } |
| 693 | if (bmv->bmv_oflags & BMV_OF_LAST) |
| 694 | fiemap_flags |= FIEMAP_EXTENT_LAST; |
| 695 | |
| 696 | error = fiemap_fill_next_extent(fieinfo, logical, physical, |
| 697 | length, fiemap_flags); |
| 698 | if (error > 0) { |
| 699 | error = 0; |
| 700 | *full = 1; /* user array now full */ |
| 701 | } |
| 702 | |
| 703 | return -error; |
| 704 | } |
| 705 | |
| 706 | STATIC int |
| 707 | xfs_vn_fiemap( |
| 708 | struct inode *inode, |
| 709 | struct fiemap_extent_info *fieinfo, |
| 710 | u64 start, |
| 711 | u64 length) |
| 712 | { |
| 713 | xfs_inode_t *ip = XFS_I(inode); |
| 714 | struct getbmapx bm; |
| 715 | int error; |
| 716 | |
| 717 | error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS); |
| 718 | if (error) |
| 719 | return error; |
| 720 | |
| 721 | /* Set up bmap header for xfs internal routine */ |
| 722 | bm.bmv_offset = BTOBB(start); |
| 723 | /* Special case for whole file */ |
| 724 | if (length == FIEMAP_MAX_OFFSET) |
| 725 | bm.bmv_length = -1LL; |
| 726 | else |
| 727 | bm.bmv_length = BTOBB(length); |
| 728 | |
| 729 | /* our formatter will tell xfs_getbmap when to stop. */ |
| 730 | bm.bmv_count = MAXEXTNUM; |
| 731 | bm.bmv_iflags = BMV_IF_PREALLOC; |
| 732 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) |
| 733 | bm.bmv_iflags |= BMV_IF_ATTRFORK; |
| 734 | if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC)) |
| 735 | bm.bmv_iflags |= BMV_IF_DELALLOC; |
| 736 | |
| 737 | error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo); |
| 738 | if (error) |
| 739 | return -error; |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 744 | static const struct inode_operations xfs_inode_operations = { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 745 | .permission = xfs_vn_permission, |
| 746 | .truncate = xfs_vn_truncate, |
| 747 | .getattr = xfs_vn_getattr, |
| 748 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 749 | .setxattr = generic_setxattr, |
| 750 | .getxattr = generic_getxattr, |
| 751 | .removexattr = generic_removexattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 752 | .listxattr = xfs_vn_listxattr, |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 753 | .fallocate = xfs_vn_fallocate, |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 754 | .fiemap = xfs_vn_fiemap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | }; |
| 756 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 757 | static const struct inode_operations xfs_dir_inode_operations = { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 758 | .create = xfs_vn_create, |
| 759 | .lookup = xfs_vn_lookup, |
| 760 | .link = xfs_vn_link, |
| 761 | .unlink = xfs_vn_unlink, |
| 762 | .symlink = xfs_vn_symlink, |
| 763 | .mkdir = xfs_vn_mkdir, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 764 | /* |
| 765 | * Yes, XFS uses the same method for rmdir and unlink. |
| 766 | * |
| 767 | * There are some subtile differences deeper in the code, |
| 768 | * but we use S_ISDIR to check for those. |
| 769 | */ |
| 770 | .rmdir = xfs_vn_unlink, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 771 | .mknod = xfs_vn_mknod, |
| 772 | .rename = xfs_vn_rename, |
| 773 | .permission = xfs_vn_permission, |
| 774 | .getattr = xfs_vn_getattr, |
| 775 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 776 | .setxattr = generic_setxattr, |
| 777 | .getxattr = generic_getxattr, |
| 778 | .removexattr = generic_removexattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 779 | .listxattr = xfs_vn_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | }; |
| 781 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 782 | static const struct inode_operations xfs_dir_ci_inode_operations = { |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 783 | .create = xfs_vn_create, |
| 784 | .lookup = xfs_vn_ci_lookup, |
| 785 | .link = xfs_vn_link, |
| 786 | .unlink = xfs_vn_unlink, |
| 787 | .symlink = xfs_vn_symlink, |
| 788 | .mkdir = xfs_vn_mkdir, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 789 | /* |
| 790 | * Yes, XFS uses the same method for rmdir and unlink. |
| 791 | * |
| 792 | * There are some subtile differences deeper in the code, |
| 793 | * but we use S_ISDIR to check for those. |
| 794 | */ |
| 795 | .rmdir = xfs_vn_unlink, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 796 | .mknod = xfs_vn_mknod, |
| 797 | .rename = xfs_vn_rename, |
| 798 | .permission = xfs_vn_permission, |
| 799 | .getattr = xfs_vn_getattr, |
| 800 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 801 | .setxattr = generic_setxattr, |
| 802 | .getxattr = generic_getxattr, |
| 803 | .removexattr = generic_removexattr, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 804 | .listxattr = xfs_vn_listxattr, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 805 | }; |
| 806 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 807 | static const struct inode_operations xfs_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | .readlink = generic_readlink, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 809 | .follow_link = xfs_vn_follow_link, |
| 810 | .put_link = xfs_vn_put_link, |
| 811 | .permission = xfs_vn_permission, |
| 812 | .getattr = xfs_vn_getattr, |
| 813 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 814 | .setxattr = generic_setxattr, |
| 815 | .getxattr = generic_getxattr, |
| 816 | .removexattr = generic_removexattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 817 | .listxattr = xfs_vn_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | }; |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 819 | |
| 820 | STATIC void |
| 821 | xfs_diflags_to_iflags( |
| 822 | struct inode *inode, |
| 823 | struct xfs_inode *ip) |
| 824 | { |
| 825 | if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE) |
| 826 | inode->i_flags |= S_IMMUTABLE; |
| 827 | else |
| 828 | inode->i_flags &= ~S_IMMUTABLE; |
| 829 | if (ip->i_d.di_flags & XFS_DIFLAG_APPEND) |
| 830 | inode->i_flags |= S_APPEND; |
| 831 | else |
| 832 | inode->i_flags &= ~S_APPEND; |
| 833 | if (ip->i_d.di_flags & XFS_DIFLAG_SYNC) |
| 834 | inode->i_flags |= S_SYNC; |
| 835 | else |
| 836 | inode->i_flags &= ~S_SYNC; |
| 837 | if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME) |
| 838 | inode->i_flags |= S_NOATIME; |
| 839 | else |
| 840 | inode->i_flags &= ~S_NOATIME; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Initialize the Linux inode, set up the operation vectors and |
| 845 | * unlock the inode. |
| 846 | * |
| 847 | * When reading existing inodes from disk this is called directly |
| 848 | * from xfs_iget, when creating a new inode it is called from |
| 849 | * xfs_ialloc after setting up the inode. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 850 | * |
| 851 | * We are always called with an uninitialised linux inode here. |
| 852 | * We need to initialise the necessary fields and take a reference |
| 853 | * on it. |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 854 | */ |
| 855 | void |
| 856 | xfs_setup_inode( |
| 857 | struct xfs_inode *ip) |
| 858 | { |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 859 | struct inode *inode = &ip->i_vnode; |
| 860 | |
| 861 | inode->i_ino = ip->i_ino; |
| 862 | inode->i_state = I_NEW|I_LOCK; |
| 863 | inode_add_to_lists(ip->i_mount->m_super, inode); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 864 | |
| 865 | inode->i_mode = ip->i_d.di_mode; |
| 866 | inode->i_nlink = ip->i_d.di_nlink; |
| 867 | inode->i_uid = ip->i_d.di_uid; |
| 868 | inode->i_gid = ip->i_d.di_gid; |
| 869 | |
| 870 | switch (inode->i_mode & S_IFMT) { |
| 871 | case S_IFBLK: |
| 872 | case S_IFCHR: |
| 873 | inode->i_rdev = |
| 874 | MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, |
| 875 | sysv_minor(ip->i_df.if_u2.if_rdev)); |
| 876 | break; |
| 877 | default: |
| 878 | inode->i_rdev = 0; |
| 879 | break; |
| 880 | } |
| 881 | |
| 882 | inode->i_generation = ip->i_d.di_gen; |
| 883 | i_size_write(inode, ip->i_d.di_size); |
| 884 | inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec; |
| 885 | inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec; |
| 886 | inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec; |
| 887 | inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; |
| 888 | inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec; |
| 889 | inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; |
| 890 | xfs_diflags_to_iflags(inode, ip); |
| 891 | xfs_iflags_clear(ip, XFS_IMODIFIED); |
| 892 | |
| 893 | switch (inode->i_mode & S_IFMT) { |
| 894 | case S_IFREG: |
| 895 | inode->i_op = &xfs_inode_operations; |
| 896 | inode->i_fop = &xfs_file_operations; |
| 897 | inode->i_mapping->a_ops = &xfs_address_space_operations; |
| 898 | break; |
| 899 | case S_IFDIR: |
| 900 | if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb)) |
| 901 | inode->i_op = &xfs_dir_ci_inode_operations; |
| 902 | else |
| 903 | inode->i_op = &xfs_dir_inode_operations; |
| 904 | inode->i_fop = &xfs_dir_file_operations; |
| 905 | break; |
| 906 | case S_IFLNK: |
| 907 | inode->i_op = &xfs_symlink_inode_operations; |
| 908 | if (!(ip->i_df.if_flags & XFS_IFINLINE)) |
| 909 | inode->i_mapping->a_ops = &xfs_address_space_operations; |
| 910 | break; |
| 911 | default: |
| 912 | inode->i_op = &xfs_inode_operations; |
| 913 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | xfs_iflags_clear(ip, XFS_INEW); |
| 918 | barrier(); |
| 919 | |
| 920 | unlock_new_inode(inode); |
| 921 | } |