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