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 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 65 | bhv_vnode_t *vp; |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 66 | |
| 67 | vp = XFS_ITOV_NULL(ip); |
| 68 | if (vp) { |
Christoph Hellwig | 0a74cd1 | 2007-08-29 11:53:12 +1000 | [diff] [blame] | 69 | ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec; |
| 70 | ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec; |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /* |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 75 | * If the linux inode exists, mark it dirty. |
| 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 | { |
| 83 | bhv_vnode_t *vp; |
| 84 | |
| 85 | vp = XFS_ITOV_NULL(ip); |
| 86 | if (vp) |
| 87 | mark_inode_dirty_sync(vn_to_inode(vp)); |
| 88 | } |
| 89 | |
| 90 | /* |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 91 | * Change the requested timestamp in the given inode. |
| 92 | * We don't lock across timestamp updates, and we don't log them but |
| 93 | * we do record the fact that there is dirty information in core. |
| 94 | * |
| 95 | * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG |
| 96 | * with XFS_ICHGTIME_ACC to be sure that access time |
| 97 | * update will take. Calling first with XFS_ICHGTIME_ACC |
| 98 | * and then XFS_ICHGTIME_MOD may fail to modify the access |
| 99 | * timestamp if the filesystem is mounted noacctm. |
| 100 | */ |
| 101 | void |
| 102 | xfs_ichgtime( |
| 103 | xfs_inode_t *ip, |
| 104 | int flags) |
| 105 | { |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 106 | struct inode *inode = vn_to_inode(XFS_ITOV(ip)); |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 107 | timespec_t tv; |
| 108 | |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 109 | nanotime(&tv); |
| 110 | if (flags & XFS_ICHGTIME_MOD) { |
| 111 | inode->i_mtime = tv; |
| 112 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; |
| 113 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; |
| 114 | } |
| 115 | if (flags & XFS_ICHGTIME_ACC) { |
| 116 | inode->i_atime = tv; |
| 117 | ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec; |
| 118 | ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec; |
| 119 | } |
| 120 | if (flags & XFS_ICHGTIME_CHG) { |
| 121 | inode->i_ctime = tv; |
| 122 | ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec; |
| 123 | ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * We update the i_update_core field _after_ changing |
| 128 | * the timestamps in order to coordinate properly with |
| 129 | * xfs_iflush() so that we don't lose timestamp updates. |
| 130 | * This keeps us from having to hold the inode lock |
| 131 | * while doing this. We use the SYNCHRONIZE macro to |
| 132 | * ensure that the compiler does not reorder the update |
| 133 | * of i_update_core above the timestamp updates above. |
| 134 | */ |
| 135 | SYNCHRONIZE(); |
| 136 | ip->i_update_core = 1; |
David Chinner | cf10e82 | 2007-12-07 14:09:11 +1100 | [diff] [blame] | 137 | if (!(inode->i_state & I_NEW)) |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 138 | mark_inode_dirty_sync(inode); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Variant on the above which avoids querying the system clock |
| 143 | * in situations where we know the Linux inode timestamps have |
| 144 | * just been updated (and so we can update our inode cheaply). |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 145 | */ |
| 146 | void |
| 147 | xfs_ichgtime_fast( |
| 148 | xfs_inode_t *ip, |
| 149 | struct inode *inode, |
| 150 | int flags) |
| 151 | { |
| 152 | timespec_t *tvp; |
| 153 | |
| 154 | /* |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 155 | * Atime updates for read() & friends are handled lazily now, and |
| 156 | * explicit updates must go through xfs_ichgtime() |
| 157 | */ |
| 158 | ASSERT((flags & XFS_ICHGTIME_ACC) == 0); |
| 159 | |
| 160 | /* |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 161 | * We're not supposed to change timestamps in readonly-mounted |
| 162 | * filesystems. Throw it away if anyone asks us. |
| 163 | */ |
| 164 | if (unlikely(IS_RDONLY(inode))) |
| 165 | return; |
| 166 | |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 167 | if (flags & XFS_ICHGTIME_MOD) { |
| 168 | tvp = &inode->i_mtime; |
| 169 | ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec; |
| 170 | ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec; |
| 171 | } |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 172 | if (flags & XFS_ICHGTIME_CHG) { |
| 173 | tvp = &inode->i_ctime; |
| 174 | ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec; |
| 175 | ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * We update the i_update_core field _after_ changing |
| 180 | * the timestamps in order to coordinate properly with |
| 181 | * xfs_iflush() so that we don't lose timestamp updates. |
| 182 | * This keeps us from having to hold the inode lock |
| 183 | * while doing this. We use the SYNCHRONIZE macro to |
| 184 | * ensure that the compiler does not reorder the update |
| 185 | * of i_update_core above the timestamp updates above. |
| 186 | */ |
| 187 | SYNCHRONIZE(); |
| 188 | ip->i_update_core = 1; |
David Chinner | cf10e82 | 2007-12-07 14:09:11 +1100 | [diff] [blame] | 189 | if (!(inode->i_state & I_NEW)) |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 190 | mark_inode_dirty_sync(inode); |
| 191 | } |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Pull the link count and size up from the xfs inode to the linux inode |
| 196 | */ |
| 197 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 198 | xfs_validate_fields( |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 199 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 201 | struct xfs_inode *ip = XFS_I(inode); |
| 202 | loff_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 204 | inode->i_nlink = ip->i_d.di_nlink; |
| 205 | inode->i_blocks = |
| 206 | XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks + |
| 207 | ip->i_delayed_blks); |
| 208 | /* we're under i_sem so i_size can't change under us */ |
| 209 | size = XFS_ISIZE(ip); |
| 210 | if (i_size_read(inode) != size) |
| 211 | i_size_write(inode, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 215 | * Hook in SELinux. This is not quite correct yet, what we really need |
| 216 | * here (as we do for default ACLs) is a mechanism by which creation of |
| 217 | * these attrs can be journalled at inode creation time (along with the |
| 218 | * inode, of course, such that log replay can't cause these to be lost). |
| 219 | */ |
| 220 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 221 | xfs_init_security( |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 222 | bhv_vnode_t *vp, |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 223 | struct inode *dir) |
| 224 | { |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 225 | struct inode *ip = vn_to_inode(vp); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 226 | size_t length; |
| 227 | void *value; |
| 228 | char *name; |
| 229 | int error; |
| 230 | |
| 231 | error = security_inode_init_security(ip, dir, &name, &value, &length); |
| 232 | if (error) { |
| 233 | if (error == -EOPNOTSUPP) |
| 234 | return 0; |
| 235 | return -error; |
| 236 | } |
| 237 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 238 | error = xfs_attr_set(XFS_I(ip), name, value, |
| 239 | length, ATTR_SECURE); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 240 | if (!error) |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 241 | xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 242 | |
| 243 | kfree(name); |
| 244 | kfree(value); |
| 245 | return error; |
| 246 | } |
| 247 | |
| 248 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | * Determine whether a process has a valid fs_struct (kernel daemons |
| 250 | * like knfsd don't have an fs_struct). |
| 251 | * |
| 252 | * XXX(hch): nfsd is broken, better fix it instead. |
| 253 | */ |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 254 | STATIC_INLINE int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 255 | xfs_has_fs_struct(struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
| 257 | return (task->fs != init_task.fs); |
| 258 | } |
| 259 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 260 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 261 | xfs_cleanup_inode( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 262 | struct inode *dir, |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 263 | bhv_vnode_t *vp, |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 264 | struct dentry *dentry, |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 265 | int mode) |
| 266 | { |
| 267 | struct dentry teardown = {}; |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 268 | |
| 269 | /* Oh, the horror. |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 270 | * If we can't add the ACL or we fail in |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 271 | * xfs_init_security we must back out. |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 272 | * ENOSPC can hit here, among other things. |
| 273 | */ |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 274 | teardown.d_inode = vn_to_inode(vp); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 275 | teardown.d_name = dentry->d_name; |
| 276 | |
| 277 | if (S_ISDIR(mode)) |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 278 | xfs_rmdir(XFS_I(dir), &teardown); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 279 | else |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 280 | xfs_remove(XFS_I(dir), &teardown); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 281 | VN_RELE(vp); |
| 282 | } |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 285 | xfs_vn_mknod( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | struct inode *dir, |
| 287 | struct dentry *dentry, |
| 288 | int mode, |
| 289 | dev_t rdev) |
| 290 | { |
| 291 | struct inode *ip; |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 292 | bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | xfs_acl_t *default_acl = NULL; |
| 294 | attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS; |
| 295 | int error; |
| 296 | |
| 297 | /* |
| 298 | * Irix uses Missed'em'V split, but doesn't want to see |
| 299 | * the upper 5 bits of (14bit) major. |
| 300 | */ |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 301 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return -EINVAL; |
| 303 | |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 304 | if (unlikely(test_default_acl && test_default_acl(dvp))) { |
| 305 | if (!_ACL_ALLOC(default_acl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return -ENOMEM; |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | if (!_ACL_GET_DEFAULT(dvp, default_acl)) { |
| 309 | _ACL_FREE(default_acl); |
| 310 | default_acl = NULL; |
| 311 | } |
| 312 | } |
| 313 | |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 314 | if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | mode &= ~current->fs->umask; |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | switch (mode & S_IFMT) { |
| 318 | case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK: |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 319 | rdev = sysv_encode_dev(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | case S_IFREG: |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 321 | error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | break; |
| 323 | case S_IFDIR: |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 324 | error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | break; |
| 326 | default: |
| 327 | error = EINVAL; |
| 328 | break; |
| 329 | } |
| 330 | |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 331 | if (unlikely(!error)) { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 332 | error = xfs_init_security(vp, dir); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 333 | if (error) |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 334 | xfs_cleanup_inode(dir, vp, dentry, mode); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 335 | } |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 336 | |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 337 | if (unlikely(default_acl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (!error) { |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 339 | error = _ACL_INHERIT(vp, mode, default_acl); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 340 | if (!error) |
Christoph Hellwig | 0a74cd1 | 2007-08-29 11:53:12 +1000 | [diff] [blame] | 341 | xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 342 | else |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 343 | xfs_cleanup_inode(dir, vp, dentry, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | _ACL_FREE(default_acl); |
| 346 | } |
| 347 | |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 348 | if (likely(!error)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | ASSERT(vp); |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 350 | ip = vn_to_inode(vp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Christoph Hellwig | bad60fd | 2007-12-21 10:58:56 +1100 | [diff] [blame] | 352 | if (S_ISDIR(mode)) |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 353 | xfs_validate_fields(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | d_instantiate(dentry, ip); |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 355 | xfs_validate_fields(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | return -error; |
| 358 | } |
| 359 | |
| 360 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 361 | xfs_vn_create( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | struct inode *dir, |
| 363 | struct dentry *dentry, |
| 364 | int mode, |
| 365 | struct nameidata *nd) |
| 366 | { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 367 | return xfs_vn_mknod(dir, dentry, mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 371 | xfs_vn_mkdir( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | struct inode *dir, |
| 373 | struct dentry *dentry, |
| 374 | int mode) |
| 375 | { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 376 | return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | STATIC struct dentry * |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 380 | xfs_vn_lookup( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | struct inode *dir, |
| 382 | struct dentry *dentry, |
| 383 | struct nameidata *nd) |
| 384 | { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 385 | bhv_vnode_t *cvp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | int error; |
| 387 | |
| 388 | if (dentry->d_name.len >= MAXNAMELEN) |
| 389 | return ERR_PTR(-ENAMETOOLONG); |
| 390 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 391 | error = xfs_lookup(XFS_I(dir), dentry, &cvp); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 392 | if (unlikely(error)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (unlikely(error != ENOENT)) |
| 394 | return ERR_PTR(-error); |
| 395 | d_add(dentry, NULL); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 399 | return d_splice_alias(vn_to_inode(cvp), dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 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 | { |
| 408 | struct inode *ip; /* inode of guy being linked to */ |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 409 | bhv_vnode_t *vp; /* vp of name being linked */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | int error; |
| 411 | |
| 412 | ip = old_dentry->d_inode; /* inode being linked to */ |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 413 | vp = vn_from_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Nathan Scott | 97dfd70 | 2006-06-27 16:13:46 +1000 | [diff] [blame] | 415 | VN_HOLD(vp); |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 416 | error = xfs_link(XFS_I(dir), vp, dentry); |
Nathan Scott | 97dfd70 | 2006-06-27 16:13:46 +1000 | [diff] [blame] | 417 | if (unlikely(error)) { |
| 418 | VN_RELE(vp); |
| 419 | } else { |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 420 | xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED); |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 421 | xfs_validate_fields(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | d_instantiate(dentry, ip); |
| 423 | } |
| 424 | return -error; |
| 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 | { |
| 432 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | int error; |
| 434 | |
| 435 | inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 437 | error = xfs_remove(XFS_I(dir), dentry); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 438 | if (likely(!error)) { |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 439 | xfs_validate_fields(dir); /* size needs update */ |
| 440 | xfs_validate_fields(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
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_symlink( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | struct inode *dir, |
| 448 | struct dentry *dentry, |
| 449 | const char *symname) |
| 450 | { |
| 451 | struct inode *ip; |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 452 | bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | int error; |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 454 | mode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | cvp = NULL; |
| 457 | |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 458 | mode = S_IFLNK | |
Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 459 | (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 461 | error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode, |
| 462 | &cvp, NULL); |
Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 463 | if (likely(!error && cvp)) { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 464 | error = xfs_init_security(cvp, dir); |
Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 465 | if (likely(!error)) { |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 466 | ip = vn_to_inode(cvp); |
Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 467 | d_instantiate(dentry, ip); |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 468 | xfs_validate_fields(dir); |
| 469 | xfs_validate_fields(ip); |
Nathan Scott | ce9d37c | 2006-03-17 17:26:34 +1100 | [diff] [blame] | 470 | } else { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 471 | xfs_cleanup_inode(dir, cvp, dentry, 0); |
Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | return -error; |
| 475 | } |
| 476 | |
| 477 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 478 | xfs_vn_rmdir( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | struct inode *dir, |
| 480 | struct dentry *dentry) |
| 481 | { |
| 482 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | int error; |
| 484 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 485 | error = xfs_rmdir(XFS_I(dir), dentry); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 486 | if (likely(!error)) { |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 487 | xfs_validate_fields(inode); |
| 488 | xfs_validate_fields(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | return -error; |
| 491 | } |
| 492 | |
| 493 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 494 | xfs_vn_rename( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | struct inode *odir, |
| 496 | struct dentry *odentry, |
| 497 | struct inode *ndir, |
| 498 | struct dentry *ndentry) |
| 499 | { |
| 500 | struct inode *new_inode = ndentry->d_inode; |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 501 | bhv_vnode_t *tvp; /* target directory */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | int error; |
| 503 | |
Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 504 | tvp = vn_from_inode(ndir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 506 | error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 507 | if (likely(!error)) { |
| 508 | if (new_inode) |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 509 | xfs_validate_fields(new_inode); |
| 510 | xfs_validate_fields(odir); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 511 | if (ndir != odir) |
Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 512 | xfs_validate_fields(ndir); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 513 | } |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 514 | return -error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* |
| 518 | * careful here - this function can get called recursively, so |
| 519 | * we need to be very careful about how much stack we use. |
| 520 | * uio is kmalloced for this reason... |
| 521 | */ |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 522 | STATIC void * |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 523 | xfs_vn_follow_link( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | struct dentry *dentry, |
| 525 | struct nameidata *nd) |
| 526 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | char *link; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 528 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 530 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 531 | if (!link) |
| 532 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 534 | error = -xfs_readlink(XFS_I(dentry->d_inode), link); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 535 | if (unlikely(error)) |
| 536 | goto out_kfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | nd_set_link(nd, link); |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 539 | return NULL; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 540 | |
| 541 | out_kfree: |
| 542 | kfree(link); |
| 543 | out_err: |
| 544 | nd_set_link(nd, ERR_PTR(error)); |
| 545 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 548 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 549 | xfs_vn_put_link( |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 550 | struct dentry *dentry, |
| 551 | struct nameidata *nd, |
| 552 | void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 554 | char *s = nd_get_link(nd); |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (!IS_ERR(s)) |
| 557 | kfree(s); |
| 558 | } |
| 559 | |
| 560 | #ifdef CONFIG_XFS_POSIX_ACL |
| 561 | STATIC int |
Christoph Hellwig | 4576758 | 2008-02-05 12:13:24 +1100 | [diff] [blame^] | 562 | xfs_check_acl( |
| 563 | struct inode *inode, |
| 564 | int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
Christoph Hellwig | 4576758 | 2008-02-05 12:13:24 +1100 | [diff] [blame^] | 566 | struct xfs_inode *ip = XFS_I(inode); |
| 567 | int error; |
| 568 | |
| 569 | xfs_itrace_entry(ip); |
| 570 | |
| 571 | if (XFS_IFORK_Q(ip)) { |
| 572 | error = xfs_acl_iaccess(ip, mask, NULL); |
| 573 | if (error != -1) |
| 574 | return -error; |
| 575 | } |
| 576 | |
| 577 | return -EAGAIN; |
| 578 | } |
| 579 | |
| 580 | STATIC int |
| 581 | xfs_vn_permission( |
| 582 | struct inode *inode, |
| 583 | int mask, |
| 584 | struct nameidata *nd) |
| 585 | { |
| 586 | return generic_permission(inode, mask, xfs_check_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | #else |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 589 | #define xfs_vn_permission NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | #endif |
| 591 | |
| 592 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 593 | xfs_vn_getattr( |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 594 | struct vfsmount *mnt, |
| 595 | struct dentry *dentry, |
| 596 | struct kstat *stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 598 | struct inode *inode = dentry->d_inode; |
| 599 | struct xfs_inode *ip = XFS_I(inode); |
| 600 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 602 | xfs_itrace_entry(ip); |
| 603 | |
| 604 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 605 | return XFS_ERROR(EIO); |
| 606 | |
| 607 | stat->size = XFS_ISIZE(ip); |
| 608 | stat->dev = inode->i_sb->s_dev; |
| 609 | stat->mode = ip->i_d.di_mode; |
| 610 | stat->nlink = ip->i_d.di_nlink; |
| 611 | stat->uid = ip->i_d.di_uid; |
| 612 | stat->gid = ip->i_d.di_gid; |
| 613 | stat->ino = ip->i_ino; |
| 614 | #if XFS_BIG_INUMS |
| 615 | stat->ino += mp->m_inoadd; |
| 616 | #endif |
| 617 | stat->atime = inode->i_atime; |
| 618 | stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec; |
| 619 | stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; |
| 620 | stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec; |
| 621 | stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; |
| 622 | stat->blocks = |
| 623 | XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); |
| 624 | |
| 625 | |
| 626 | switch (inode->i_mode & S_IFMT) { |
| 627 | case S_IFBLK: |
| 628 | case S_IFCHR: |
| 629 | stat->blksize = BLKDEV_IOSIZE; |
| 630 | stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, |
| 631 | sysv_minor(ip->i_df.if_u2.if_rdev)); |
| 632 | break; |
| 633 | default: |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 634 | if (XFS_IS_REALTIME_INODE(ip)) { |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 635 | /* |
| 636 | * If the file blocks are being allocated from a |
| 637 | * realtime volume, then return the inode's realtime |
| 638 | * extent size or the realtime volume's extent size. |
| 639 | */ |
| 640 | stat->blksize = |
| 641 | xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; |
| 642 | } else |
| 643 | stat->blksize = xfs_preferred_iosize(mp); |
| 644 | stat->rdev = 0; |
| 645 | break; |
Nathan Scott | 69e23b9 | 2006-09-28 11:01:22 +1000 | [diff] [blame] | 646 | } |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 647 | |
| 648 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 652 | xfs_vn_setattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | struct dentry *dentry, |
| 654 | struct iattr *attr) |
| 655 | { |
| 656 | struct inode *inode = dentry->d_inode; |
| 657 | unsigned int ia_valid = attr->ia_valid; |
Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 658 | bhv_vattr_t vattr = { 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | int flags = 0; |
| 660 | int error; |
| 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (ia_valid & ATTR_UID) { |
| 663 | vattr.va_mask |= XFS_AT_UID; |
| 664 | vattr.va_uid = attr->ia_uid; |
| 665 | } |
| 666 | if (ia_valid & ATTR_GID) { |
| 667 | vattr.va_mask |= XFS_AT_GID; |
| 668 | vattr.va_gid = attr->ia_gid; |
| 669 | } |
| 670 | if (ia_valid & ATTR_SIZE) { |
| 671 | vattr.va_mask |= XFS_AT_SIZE; |
| 672 | vattr.va_size = attr->ia_size; |
| 673 | } |
| 674 | if (ia_valid & ATTR_ATIME) { |
| 675 | vattr.va_mask |= XFS_AT_ATIME; |
| 676 | vattr.va_atime = attr->ia_atime; |
Nathan Scott | 8c0b511 | 2006-04-11 15:12:45 +1000 | [diff] [blame] | 677 | inode->i_atime = attr->ia_atime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
| 679 | if (ia_valid & ATTR_MTIME) { |
| 680 | vattr.va_mask |= XFS_AT_MTIME; |
| 681 | vattr.va_mtime = attr->ia_mtime; |
| 682 | } |
| 683 | if (ia_valid & ATTR_CTIME) { |
| 684 | vattr.va_mask |= XFS_AT_CTIME; |
| 685 | vattr.va_ctime = attr->ia_ctime; |
| 686 | } |
| 687 | if (ia_valid & ATTR_MODE) { |
| 688 | vattr.va_mask |= XFS_AT_MODE; |
| 689 | vattr.va_mode = attr->ia_mode; |
| 690 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 691 | inode->i_mode &= ~S_ISGID; |
| 692 | } |
| 693 | |
| 694 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) |
| 695 | flags |= ATTR_UTIME; |
| 696 | #ifdef ATTR_NO_BLOCK |
| 697 | if ((ia_valid & ATTR_NO_BLOCK)) |
| 698 | flags |= ATTR_NONBLOCK; |
| 699 | #endif |
| 700 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 701 | error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 702 | if (likely(!error)) |
Christoph Hellwig | 21a6254 | 2007-09-19 15:27:49 +1000 | [diff] [blame] | 703 | vn_revalidate(vn_from_inode(inode)); |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 704 | return -error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 708 | xfs_vn_truncate( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | struct inode *inode) |
| 710 | { |
Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 711 | block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 715 | xfs_vn_setxattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | struct dentry *dentry, |
| 717 | const char *name, |
| 718 | const void *data, |
| 719 | size_t size, |
| 720 | int flags) |
| 721 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 722 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | char *attr = (char *)name; |
| 724 | attrnames_t *namesp; |
| 725 | int xflags = 0; |
| 726 | int error; |
| 727 | |
| 728 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); |
| 729 | if (!namesp) |
| 730 | return -EOPNOTSUPP; |
| 731 | attr += namesp->attr_namelen; |
| 732 | error = namesp->attr_capable(vp, NULL); |
| 733 | if (error) |
| 734 | return error; |
| 735 | |
| 736 | /* Convert Linux syscall to XFS internal ATTR flags */ |
| 737 | if (flags & XATTR_CREATE) |
| 738 | xflags |= ATTR_CREATE; |
| 739 | if (flags & XATTR_REPLACE) |
| 740 | xflags |= ATTR_REPLACE; |
| 741 | xflags |= namesp->attr_flag; |
| 742 | return namesp->attr_set(vp, attr, (void *)data, size, xflags); |
| 743 | } |
| 744 | |
| 745 | STATIC ssize_t |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 746 | xfs_vn_getxattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | struct dentry *dentry, |
| 748 | const char *name, |
| 749 | void *data, |
| 750 | size_t size) |
| 751 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 752 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | char *attr = (char *)name; |
| 754 | attrnames_t *namesp; |
| 755 | int xflags = 0; |
| 756 | ssize_t error; |
| 757 | |
| 758 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); |
| 759 | if (!namesp) |
| 760 | return -EOPNOTSUPP; |
| 761 | attr += namesp->attr_namelen; |
| 762 | error = namesp->attr_capable(vp, NULL); |
| 763 | if (error) |
| 764 | return error; |
| 765 | |
| 766 | /* Convert Linux syscall to XFS internal ATTR flags */ |
| 767 | if (!size) { |
| 768 | xflags |= ATTR_KERNOVAL; |
| 769 | data = NULL; |
| 770 | } |
| 771 | xflags |= namesp->attr_flag; |
| 772 | return namesp->attr_get(vp, attr, (void *)data, size, xflags); |
| 773 | } |
| 774 | |
| 775 | STATIC ssize_t |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 776 | xfs_vn_listxattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | struct dentry *dentry, |
| 778 | char *data, |
| 779 | size_t size) |
| 780 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 781 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | int error, xflags = ATTR_KERNAMELS; |
| 783 | ssize_t result; |
| 784 | |
| 785 | if (!size) |
| 786 | xflags |= ATTR_KERNOVAL; |
| 787 | xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS; |
| 788 | |
| 789 | error = attr_generic_list(vp, data, size, xflags, &result); |
| 790 | if (error < 0) |
| 791 | return error; |
| 792 | return result; |
| 793 | } |
| 794 | |
| 795 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 796 | xfs_vn_removexattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | struct dentry *dentry, |
| 798 | const char *name) |
| 799 | { |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 800 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | char *attr = (char *)name; |
| 802 | attrnames_t *namesp; |
| 803 | int xflags = 0; |
| 804 | int error; |
| 805 | |
| 806 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); |
| 807 | if (!namesp) |
| 808 | return -EOPNOTSUPP; |
| 809 | attr += namesp->attr_namelen; |
| 810 | error = namesp->attr_capable(vp, NULL); |
| 811 | if (error) |
| 812 | return error; |
| 813 | xflags |= namesp->attr_flag; |
| 814 | return namesp->attr_remove(vp, attr, xflags); |
| 815 | } |
| 816 | |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 817 | STATIC long |
| 818 | xfs_vn_fallocate( |
| 819 | struct inode *inode, |
| 820 | int mode, |
| 821 | loff_t offset, |
| 822 | loff_t len) |
| 823 | { |
| 824 | long error; |
| 825 | loff_t new_size = 0; |
| 826 | xfs_flock64_t bf; |
| 827 | xfs_inode_t *ip = XFS_I(inode); |
| 828 | |
| 829 | /* preallocation on directories not yet supported */ |
| 830 | error = -ENODEV; |
| 831 | if (S_ISDIR(inode->i_mode)) |
| 832 | goto out_error; |
| 833 | |
| 834 | bf.l_whence = 0; |
| 835 | bf.l_start = offset; |
| 836 | bf.l_len = len; |
| 837 | |
| 838 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 839 | error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf, |
| 840 | 0, NULL, ATTR_NOLOCK); |
| 841 | if (!error && !(mode & FALLOC_FL_KEEP_SIZE) && |
| 842 | offset + len > i_size_read(inode)) |
| 843 | new_size = offset + len; |
| 844 | |
| 845 | /* Change file size if needed */ |
| 846 | if (new_size) { |
| 847 | bhv_vattr_t va; |
| 848 | |
| 849 | va.va_mask = XFS_AT_SIZE; |
| 850 | va.va_size = new_size; |
| 851 | error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL); |
| 852 | } |
| 853 | |
| 854 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 855 | out_error: |
| 856 | return error; |
| 857 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 859 | const struct inode_operations xfs_inode_operations = { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 860 | .permission = xfs_vn_permission, |
| 861 | .truncate = xfs_vn_truncate, |
| 862 | .getattr = xfs_vn_getattr, |
| 863 | .setattr = xfs_vn_setattr, |
| 864 | .setxattr = xfs_vn_setxattr, |
| 865 | .getxattr = xfs_vn_getxattr, |
| 866 | .listxattr = xfs_vn_listxattr, |
| 867 | .removexattr = xfs_vn_removexattr, |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 868 | .fallocate = xfs_vn_fallocate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | }; |
| 870 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 871 | const struct inode_operations xfs_dir_inode_operations = { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 872 | .create = xfs_vn_create, |
| 873 | .lookup = xfs_vn_lookup, |
| 874 | .link = xfs_vn_link, |
| 875 | .unlink = xfs_vn_unlink, |
| 876 | .symlink = xfs_vn_symlink, |
| 877 | .mkdir = xfs_vn_mkdir, |
| 878 | .rmdir = xfs_vn_rmdir, |
| 879 | .mknod = xfs_vn_mknod, |
| 880 | .rename = xfs_vn_rename, |
| 881 | .permission = xfs_vn_permission, |
| 882 | .getattr = xfs_vn_getattr, |
| 883 | .setattr = xfs_vn_setattr, |
| 884 | .setxattr = xfs_vn_setxattr, |
| 885 | .getxattr = xfs_vn_getxattr, |
| 886 | .listxattr = xfs_vn_listxattr, |
| 887 | .removexattr = xfs_vn_removexattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | }; |
| 889 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 890 | const struct inode_operations xfs_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | .readlink = generic_readlink, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 892 | .follow_link = xfs_vn_follow_link, |
| 893 | .put_link = xfs_vn_put_link, |
| 894 | .permission = xfs_vn_permission, |
| 895 | .getattr = xfs_vn_getattr, |
| 896 | .setattr = xfs_vn_setattr, |
| 897 | .setxattr = xfs_vn_setxattr, |
| 898 | .getxattr = xfs_vn_getxattr, |
| 899 | .listxattr = xfs_vn_listxattr, |
| 900 | .removexattr = xfs_vn_removexattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | }; |