Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 | 17 | */ |
1da177e4 LT |
18 | #include "xfs.h" |
19 | #include "xfs_fs.h" | |
a844f451 | 20 | #include "xfs_bit.h" |
1da177e4 | 21 | #include "xfs_log.h" |
a844f451 | 22 | #include "xfs_inum.h" |
1da177e4 LT |
23 | #include "xfs_trans.h" |
24 | #include "xfs_sb.h" | |
25 | #include "xfs_ag.h" | |
1da177e4 LT |
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" | |
1da177e4 | 31 | #include "xfs_bmap_btree.h" |
a844f451 | 32 | #include "xfs_alloc_btree.h" |
1da177e4 | 33 | #include "xfs_ialloc_btree.h" |
1da177e4 | 34 | #include "xfs_dir2_sf.h" |
a844f451 | 35 | #include "xfs_attr_sf.h" |
1da177e4 LT |
36 | #include "xfs_dinode.h" |
37 | #include "xfs_inode.h" | |
38 | #include "xfs_bmap.h" | |
a844f451 NS |
39 | #include "xfs_btree.h" |
40 | #include "xfs_ialloc.h" | |
1da177e4 LT |
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" | |
46 | #include "xfs_cap.h" | |
47 | #include "xfs_mac.h" | |
48 | #include "xfs_attr.h" | |
49 | #include "xfs_buf_item.h" | |
50 | #include "xfs_utils.h" | |
51 | ||
16f7e0fe | 52 | #include <linux/capability.h> |
1da177e4 LT |
53 | #include <linux/xattr.h> |
54 | #include <linux/namei.h> | |
446ada4a | 55 | #include <linux/security.h> |
1da177e4 | 56 | |
75e17b3c CH |
57 | /* |
58 | * Get a XFS inode from a given vnode. | |
59 | */ | |
60 | xfs_inode_t * | |
61 | xfs_vtoi( | |
67fcaa73 | 62 | bhv_vnode_t *vp) |
75e17b3c CH |
63 | { |
64 | bhv_desc_t *bdp; | |
65 | ||
66 | bdp = bhv_lookup_range(VN_BHV_HEAD(vp), | |
67 | VNODE_POSITION_XFS, VNODE_POSITION_XFS); | |
68 | if (unlikely(bdp == NULL)) | |
69 | return NULL; | |
70 | return XFS_BHVTOI(bdp); | |
71 | } | |
72 | ||
42fe2b1f CH |
73 | /* |
74 | * Bring the atime in the XFS inode uptodate. | |
75 | * Used before logging the inode to disk or when the Linux inode goes away. | |
76 | */ | |
77 | void | |
78 | xfs_synchronize_atime( | |
79 | xfs_inode_t *ip) | |
80 | { | |
67fcaa73 | 81 | bhv_vnode_t *vp; |
42fe2b1f CH |
82 | |
83 | vp = XFS_ITOV_NULL(ip); | |
84 | if (vp) { | |
85 | struct inode *inode = &vp->v_inode; | |
86 | ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec; | |
87 | ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec; | |
88 | } | |
89 | } | |
90 | ||
4aeb664c NS |
91 | /* |
92 | * Change the requested timestamp in the given inode. | |
93 | * We don't lock across timestamp updates, and we don't log them but | |
94 | * we do record the fact that there is dirty information in core. | |
95 | * | |
96 | * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG | |
97 | * with XFS_ICHGTIME_ACC to be sure that access time | |
98 | * update will take. Calling first with XFS_ICHGTIME_ACC | |
99 | * and then XFS_ICHGTIME_MOD may fail to modify the access | |
100 | * timestamp if the filesystem is mounted noacctm. | |
101 | */ | |
102 | void | |
103 | xfs_ichgtime( | |
104 | xfs_inode_t *ip, | |
105 | int flags) | |
106 | { | |
ec86dc02 | 107 | struct inode *inode = vn_to_inode(XFS_ITOV(ip)); |
4aeb664c NS |
108 | timespec_t tv; |
109 | ||
4aeb664c NS |
110 | nanotime(&tv); |
111 | if (flags & XFS_ICHGTIME_MOD) { | |
112 | inode->i_mtime = tv; | |
113 | ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; | |
114 | ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; | |
115 | } | |
116 | if (flags & XFS_ICHGTIME_ACC) { | |
117 | inode->i_atime = tv; | |
118 | ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec; | |
119 | ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec; | |
120 | } | |
121 | if (flags & XFS_ICHGTIME_CHG) { | |
122 | inode->i_ctime = tv; | |
123 | ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec; | |
124 | ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec; | |
125 | } | |
126 | ||
127 | /* | |
128 | * We update the i_update_core field _after_ changing | |
129 | * the timestamps in order to coordinate properly with | |
130 | * xfs_iflush() so that we don't lose timestamp updates. | |
131 | * This keeps us from having to hold the inode lock | |
132 | * while doing this. We use the SYNCHRONIZE macro to | |
133 | * ensure that the compiler does not reorder the update | |
134 | * of i_update_core above the timestamp updates above. | |
135 | */ | |
136 | SYNCHRONIZE(); | |
137 | ip->i_update_core = 1; | |
138 | if (!(inode->i_state & I_LOCK)) | |
139 | mark_inode_dirty_sync(inode); | |
140 | } | |
141 | ||
142 | /* | |
143 | * Variant on the above which avoids querying the system clock | |
144 | * in situations where we know the Linux inode timestamps have | |
145 | * just been updated (and so we can update our inode cheaply). | |
4aeb664c NS |
146 | */ |
147 | void | |
148 | xfs_ichgtime_fast( | |
149 | xfs_inode_t *ip, | |
150 | struct inode *inode, | |
151 | int flags) | |
152 | { | |
153 | timespec_t *tvp; | |
154 | ||
155 | /* | |
42fe2b1f CH |
156 | * Atime updates for read() & friends are handled lazily now, and |
157 | * explicit updates must go through xfs_ichgtime() | |
4aeb664c | 158 | */ |
42fe2b1f | 159 | ASSERT((flags & XFS_ICHGTIME_ACC) == 0); |
4aeb664c NS |
160 | |
161 | /* | |
42fe2b1f CH |
162 | * We're not supposed to change timestamps in readonly-mounted |
163 | * filesystems. Throw it away if anyone asks us. | |
4aeb664c | 164 | */ |
42fe2b1f | 165 | if (unlikely(IS_RDONLY(inode))) |
4aeb664c NS |
166 | return; |
167 | ||
168 | if (flags & XFS_ICHGTIME_MOD) { | |
169 | tvp = &inode->i_mtime; | |
170 | ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec; | |
171 | ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec; | |
172 | } | |
4aeb664c NS |
173 | if (flags & XFS_ICHGTIME_CHG) { |
174 | tvp = &inode->i_ctime; | |
175 | ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec; | |
176 | ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec; | |
177 | } | |
178 | ||
179 | /* | |
180 | * We update the i_update_core field _after_ changing | |
181 | * the timestamps in order to coordinate properly with | |
182 | * xfs_iflush() so that we don't lose timestamp updates. | |
183 | * This keeps us from having to hold the inode lock | |
184 | * while doing this. We use the SYNCHRONIZE macro to | |
185 | * ensure that the compiler does not reorder the update | |
186 | * of i_update_core above the timestamp updates above. | |
187 | */ | |
188 | SYNCHRONIZE(); | |
189 | ip->i_update_core = 1; | |
190 | if (!(inode->i_state & I_LOCK)) | |
191 | mark_inode_dirty_sync(inode); | |
192 | } | |
193 | ||
1da177e4 LT |
194 | |
195 | /* | |
196 | * Pull the link count and size up from the xfs inode to the linux inode | |
197 | */ | |
198 | STATIC void | |
416c6d5b | 199 | xfs_validate_fields( |
220b5284 | 200 | struct inode *ip, |
8285fb58 | 201 | bhv_vattr_t *vattr) |
1da177e4 | 202 | { |
220b5284 | 203 | vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS; |
67fcaa73 | 204 | if (!bhv_vop_getattr(vn_from_inode(ip), vattr, ATTR_LAZY, NULL)) { |
220b5284 NS |
205 | ip->i_nlink = vattr->va_nlink; |
206 | ip->i_blocks = vattr->va_nblocks; | |
1da177e4 | 207 | |
220b5284 NS |
208 | /* we're under i_sem so i_size can't change under us */ |
209 | if (i_size_read(ip) != vattr->va_size) | |
210 | i_size_write(ip, vattr->va_size); | |
1da177e4 LT |
211 | } |
212 | } | |
213 | ||
446ada4a NS |
214 | /* |
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 | |
416c6d5b | 221 | xfs_init_security( |
67fcaa73 | 222 | bhv_vnode_t *vp, |
446ada4a NS |
223 | struct inode *dir) |
224 | { | |
ec86dc02 | 225 | struct inode *ip = vn_to_inode(vp); |
446ada4a NS |
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 | ||
67fcaa73 | 238 | error = bhv_vop_attr_set(vp, name, value, length, ATTR_SECURE, NULL); |
446ada4a NS |
239 | if (!error) |
240 | VMODIFY(vp); | |
241 | ||
242 | kfree(name); | |
243 | kfree(value); | |
244 | return error; | |
245 | } | |
246 | ||
1da177e4 LT |
247 | /* |
248 | * Determine whether a process has a valid fs_struct (kernel daemons | |
249 | * like knfsd don't have an fs_struct). | |
250 | * | |
251 | * XXX(hch): nfsd is broken, better fix it instead. | |
252 | */ | |
253 | STATIC inline int | |
416c6d5b | 254 | xfs_has_fs_struct(struct task_struct *task) |
1da177e4 LT |
255 | { |
256 | return (task->fs != init_task.fs); | |
257 | } | |
258 | ||
3a69c7dc | 259 | STATIC inline void |
416c6d5b | 260 | xfs_cleanup_inode( |
67fcaa73 NS |
261 | bhv_vnode_t *dvp, |
262 | bhv_vnode_t *vp, | |
220b5284 | 263 | struct dentry *dentry, |
3a69c7dc YL |
264 | int mode) |
265 | { | |
266 | struct dentry teardown = {}; | |
3a69c7dc YL |
267 | |
268 | /* Oh, the horror. | |
220b5284 | 269 | * If we can't add the ACL or we fail in |
416c6d5b | 270 | * xfs_init_security we must back out. |
3a69c7dc YL |
271 | * ENOSPC can hit here, among other things. |
272 | */ | |
ec86dc02 | 273 | teardown.d_inode = vn_to_inode(vp); |
3a69c7dc YL |
274 | teardown.d_name = dentry->d_name; |
275 | ||
276 | if (S_ISDIR(mode)) | |
67fcaa73 | 277 | bhv_vop_rmdir(dvp, &teardown, NULL); |
3a69c7dc | 278 | else |
67fcaa73 | 279 | bhv_vop_remove(dvp, &teardown, NULL); |
3a69c7dc YL |
280 | VN_RELE(vp); |
281 | } | |
282 | ||
1da177e4 | 283 | STATIC int |
416c6d5b | 284 | xfs_vn_mknod( |
1da177e4 LT |
285 | struct inode *dir, |
286 | struct dentry *dentry, | |
287 | int mode, | |
288 | dev_t rdev) | |
289 | { | |
290 | struct inode *ip; | |
8285fb58 | 291 | bhv_vattr_t vattr = { 0 }; |
67fcaa73 | 292 | bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir); |
1da177e4 LT |
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 | */ | |
220b5284 | 301 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
1da177e4 LT |
302 | return -EINVAL; |
303 | ||
220b5284 NS |
304 | if (unlikely(test_default_acl && test_default_acl(dvp))) { |
305 | if (!_ACL_ALLOC(default_acl)) { | |
1da177e4 | 306 | return -ENOMEM; |
220b5284 | 307 | } |
1da177e4 LT |
308 | if (!_ACL_GET_DEFAULT(dvp, default_acl)) { |
309 | _ACL_FREE(default_acl); | |
310 | default_acl = NULL; | |
311 | } | |
312 | } | |
313 | ||
416c6d5b | 314 | if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current)) |
1da177e4 LT |
315 | mode &= ~current->fs->umask; |
316 | ||
524fbf5d NS |
317 | vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE; |
318 | vattr.va_mode = mode; | |
1da177e4 LT |
319 | |
320 | switch (mode & S_IFMT) { | |
321 | case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK: | |
524fbf5d NS |
322 | vattr.va_rdev = sysv_encode_dev(rdev); |
323 | vattr.va_mask |= XFS_AT_RDEV; | |
1da177e4 LT |
324 | /*FALLTHROUGH*/ |
325 | case S_IFREG: | |
67fcaa73 | 326 | error = bhv_vop_create(dvp, dentry, &vattr, &vp, NULL); |
1da177e4 LT |
327 | break; |
328 | case S_IFDIR: | |
67fcaa73 | 329 | error = bhv_vop_mkdir(dvp, dentry, &vattr, &vp, NULL); |
1da177e4 LT |
330 | break; |
331 | default: | |
332 | error = EINVAL; | |
333 | break; | |
334 | } | |
335 | ||
220b5284 | 336 | if (unlikely(!error)) { |
416c6d5b | 337 | error = xfs_init_security(vp, dir); |
3a69c7dc | 338 | if (error) |
416c6d5b | 339 | xfs_cleanup_inode(dvp, vp, dentry, mode); |
3a69c7dc | 340 | } |
446ada4a | 341 | |
220b5284 | 342 | if (unlikely(default_acl)) { |
1da177e4 | 343 | if (!error) { |
524fbf5d | 344 | error = _ACL_INHERIT(vp, &vattr, default_acl); |
220b5284 | 345 | if (!error) |
1da177e4 | 346 | VMODIFY(vp); |
3a69c7dc | 347 | else |
416c6d5b | 348 | xfs_cleanup_inode(dvp, vp, dentry, mode); |
1da177e4 LT |
349 | } |
350 | _ACL_FREE(default_acl); | |
351 | } | |
352 | ||
220b5284 | 353 | if (likely(!error)) { |
1da177e4 | 354 | ASSERT(vp); |
ec86dc02 | 355 | ip = vn_to_inode(vp); |
1da177e4 LT |
356 | |
357 | if (S_ISCHR(mode) || S_ISBLK(mode)) | |
358 | ip->i_rdev = rdev; | |
359 | else if (S_ISDIR(mode)) | |
524fbf5d | 360 | xfs_validate_fields(ip, &vattr); |
1da177e4 | 361 | d_instantiate(dentry, ip); |
524fbf5d | 362 | xfs_validate_fields(dir, &vattr); |
1da177e4 LT |
363 | } |
364 | return -error; | |
365 | } | |
366 | ||
367 | STATIC int | |
416c6d5b | 368 | xfs_vn_create( |
1da177e4 LT |
369 | struct inode *dir, |
370 | struct dentry *dentry, | |
371 | int mode, | |
372 | struct nameidata *nd) | |
373 | { | |
416c6d5b | 374 | return xfs_vn_mknod(dir, dentry, mode, 0); |
1da177e4 LT |
375 | } |
376 | ||
377 | STATIC int | |
416c6d5b | 378 | xfs_vn_mkdir( |
1da177e4 LT |
379 | struct inode *dir, |
380 | struct dentry *dentry, | |
381 | int mode) | |
382 | { | |
416c6d5b | 383 | return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0); |
1da177e4 LT |
384 | } |
385 | ||
386 | STATIC struct dentry * | |
416c6d5b | 387 | xfs_vn_lookup( |
1da177e4 LT |
388 | struct inode *dir, |
389 | struct dentry *dentry, | |
390 | struct nameidata *nd) | |
391 | { | |
67fcaa73 | 392 | bhv_vnode_t *vp = vn_from_inode(dir), *cvp; |
1da177e4 LT |
393 | int error; |
394 | ||
395 | if (dentry->d_name.len >= MAXNAMELEN) | |
396 | return ERR_PTR(-ENAMETOOLONG); | |
397 | ||
67fcaa73 NS |
398 | error = bhv_vop_lookup(vp, dentry, &cvp, 0, NULL, NULL); |
399 | if (unlikely(error)) { | |
1da177e4 LT |
400 | if (unlikely(error != ENOENT)) |
401 | return ERR_PTR(-error); | |
402 | d_add(dentry, NULL); | |
403 | return NULL; | |
404 | } | |
405 | ||
ec86dc02 | 406 | return d_splice_alias(vn_to_inode(cvp), dentry); |
1da177e4 LT |
407 | } |
408 | ||
409 | STATIC int | |
416c6d5b | 410 | xfs_vn_link( |
1da177e4 LT |
411 | struct dentry *old_dentry, |
412 | struct inode *dir, | |
413 | struct dentry *dentry) | |
414 | { | |
415 | struct inode *ip; /* inode of guy being linked to */ | |
67fcaa73 NS |
416 | bhv_vnode_t *tdvp; /* target directory for new name/link */ |
417 | bhv_vnode_t *vp; /* vp of name being linked */ | |
8285fb58 | 418 | bhv_vattr_t vattr; |
1da177e4 LT |
419 | int error; |
420 | ||
421 | ip = old_dentry->d_inode; /* inode being linked to */ | |
ec86dc02 NS |
422 | tdvp = vn_from_inode(dir); |
423 | vp = vn_from_inode(ip); | |
1da177e4 | 424 | |
67fcaa73 | 425 | error = bhv_vop_link(tdvp, vp, dentry, NULL); |
220b5284 | 426 | if (likely(!error)) { |
1da177e4 LT |
427 | VMODIFY(tdvp); |
428 | VN_HOLD(vp); | |
524fbf5d | 429 | xfs_validate_fields(ip, &vattr); |
1da177e4 LT |
430 | d_instantiate(dentry, ip); |
431 | } | |
432 | return -error; | |
433 | } | |
434 | ||
435 | STATIC int | |
416c6d5b | 436 | xfs_vn_unlink( |
1da177e4 LT |
437 | struct inode *dir, |
438 | struct dentry *dentry) | |
439 | { | |
440 | struct inode *inode; | |
67fcaa73 | 441 | bhv_vnode_t *dvp; /* directory containing name to remove */ |
8285fb58 | 442 | bhv_vattr_t vattr; |
1da177e4 LT |
443 | int error; |
444 | ||
445 | inode = dentry->d_inode; | |
ec86dc02 | 446 | dvp = vn_from_inode(dir); |
1da177e4 | 447 | |
67fcaa73 | 448 | error = bhv_vop_remove(dvp, dentry, NULL); |
220b5284 | 449 | if (likely(!error)) { |
524fbf5d NS |
450 | xfs_validate_fields(dir, &vattr); /* size needs update */ |
451 | xfs_validate_fields(inode, &vattr); | |
1da177e4 | 452 | } |
1da177e4 LT |
453 | return -error; |
454 | } | |
455 | ||
456 | STATIC int | |
416c6d5b | 457 | xfs_vn_symlink( |
1da177e4 LT |
458 | struct inode *dir, |
459 | struct dentry *dentry, | |
460 | const char *symname) | |
461 | { | |
462 | struct inode *ip; | |
8285fb58 | 463 | bhv_vattr_t va = { 0 }; |
67fcaa73 NS |
464 | bhv_vnode_t *dvp; /* directory containing name of symlink */ |
465 | bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */ | |
1da177e4 LT |
466 | int error; |
467 | ||
ec86dc02 | 468 | dvp = vn_from_inode(dir); |
1da177e4 LT |
469 | cvp = NULL; |
470 | ||
67fcaa73 | 471 | va.va_mode = S_IFLNK | |
0432dab2 | 472 | (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO); |
67fcaa73 | 473 | va.va_mask = XFS_AT_TYPE|XFS_AT_MODE; |
1da177e4 | 474 | |
67fcaa73 | 475 | error = bhv_vop_symlink(dvp, dentry, &va, (char *)symname, &cvp, NULL); |
54245702 | 476 | if (likely(!error && cvp)) { |
416c6d5b | 477 | error = xfs_init_security(cvp, dir); |
54245702 | 478 | if (likely(!error)) { |
ec86dc02 | 479 | ip = vn_to_inode(cvp); |
54245702 | 480 | d_instantiate(dentry, ip); |
67fcaa73 NS |
481 | xfs_validate_fields(dir, &va); |
482 | xfs_validate_fields(ip, &va); | |
ce9d37c2 NS |
483 | } else { |
484 | xfs_cleanup_inode(dvp, cvp, dentry, 0); | |
54245702 | 485 | } |
1da177e4 LT |
486 | } |
487 | return -error; | |
488 | } | |
489 | ||
490 | STATIC int | |
416c6d5b | 491 | xfs_vn_rmdir( |
1da177e4 LT |
492 | struct inode *dir, |
493 | struct dentry *dentry) | |
494 | { | |
495 | struct inode *inode = dentry->d_inode; | |
67fcaa73 | 496 | bhv_vnode_t *dvp = vn_from_inode(dir); |
8285fb58 | 497 | bhv_vattr_t vattr; |
1da177e4 LT |
498 | int error; |
499 | ||
67fcaa73 | 500 | error = bhv_vop_rmdir(dvp, dentry, NULL); |
220b5284 | 501 | if (likely(!error)) { |
524fbf5d NS |
502 | xfs_validate_fields(inode, &vattr); |
503 | xfs_validate_fields(dir, &vattr); | |
1da177e4 LT |
504 | } |
505 | return -error; | |
506 | } | |
507 | ||
508 | STATIC int | |
416c6d5b | 509 | xfs_vn_rename( |
1da177e4 LT |
510 | struct inode *odir, |
511 | struct dentry *odentry, | |
512 | struct inode *ndir, | |
513 | struct dentry *ndentry) | |
514 | { | |
515 | struct inode *new_inode = ndentry->d_inode; | |
67fcaa73 NS |
516 | bhv_vnode_t *fvp; /* from directory */ |
517 | bhv_vnode_t *tvp; /* target directory */ | |
8285fb58 | 518 | bhv_vattr_t vattr; |
1da177e4 LT |
519 | int error; |
520 | ||
ec86dc02 NS |
521 | fvp = vn_from_inode(odir); |
522 | tvp = vn_from_inode(ndir); | |
1da177e4 | 523 | |
67fcaa73 | 524 | error = bhv_vop_rename(fvp, odentry, tvp, ndentry, NULL); |
220b5284 NS |
525 | if (likely(!error)) { |
526 | if (new_inode) | |
524fbf5d NS |
527 | xfs_validate_fields(new_inode, &vattr); |
528 | xfs_validate_fields(odir, &vattr); | |
220b5284 | 529 | if (ndir != odir) |
524fbf5d | 530 | xfs_validate_fields(ndir, &vattr); |
220b5284 | 531 | } |
220b5284 | 532 | return -error; |
1da177e4 LT |
533 | } |
534 | ||
535 | /* | |
536 | * careful here - this function can get called recursively, so | |
537 | * we need to be very careful about how much stack we use. | |
538 | * uio is kmalloced for this reason... | |
539 | */ | |
008b150a | 540 | STATIC void * |
416c6d5b | 541 | xfs_vn_follow_link( |
1da177e4 LT |
542 | struct dentry *dentry, |
543 | struct nameidata *nd) | |
544 | { | |
67fcaa73 | 545 | bhv_vnode_t *vp; |
1da177e4 LT |
546 | uio_t *uio; |
547 | iovec_t iov; | |
548 | int error; | |
549 | char *link; | |
550 | ||
551 | ASSERT(dentry); | |
552 | ASSERT(nd); | |
553 | ||
0d1335b3 | 554 | link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
1da177e4 LT |
555 | if (!link) { |
556 | nd_set_link(nd, ERR_PTR(-ENOMEM)); | |
008b150a | 557 | return NULL; |
1da177e4 LT |
558 | } |
559 | ||
560 | uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL); | |
561 | if (!uio) { | |
562 | kfree(link); | |
563 | nd_set_link(nd, ERR_PTR(-ENOMEM)); | |
008b150a | 564 | return NULL; |
1da177e4 LT |
565 | } |
566 | ||
ec86dc02 | 567 | vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
568 | |
569 | iov.iov_base = link; | |
0d1335b3 | 570 | iov.iov_len = MAXPATHLEN; |
1da177e4 LT |
571 | |
572 | uio->uio_iov = &iov; | |
573 | uio->uio_offset = 0; | |
574 | uio->uio_segflg = UIO_SYSSPACE; | |
0d1335b3 | 575 | uio->uio_resid = MAXPATHLEN; |
1da177e4 LT |
576 | uio->uio_iovcnt = 1; |
577 | ||
67fcaa73 NS |
578 | error = bhv_vop_readlink(vp, uio, 0, NULL); |
579 | if (unlikely(error)) { | |
1da177e4 LT |
580 | kfree(link); |
581 | link = ERR_PTR(-error); | |
582 | } else { | |
0d1335b3 | 583 | link[MAXPATHLEN - uio->uio_resid] = '\0'; |
1da177e4 LT |
584 | } |
585 | kfree(uio); | |
586 | ||
587 | nd_set_link(nd, link); | |
008b150a | 588 | return NULL; |
1da177e4 LT |
589 | } |
590 | ||
cde410a9 | 591 | STATIC void |
416c6d5b | 592 | xfs_vn_put_link( |
cde410a9 NS |
593 | struct dentry *dentry, |
594 | struct nameidata *nd, | |
595 | void *p) | |
1da177e4 | 596 | { |
cde410a9 NS |
597 | char *s = nd_get_link(nd); |
598 | ||
1da177e4 LT |
599 | if (!IS_ERR(s)) |
600 | kfree(s); | |
601 | } | |
602 | ||
603 | #ifdef CONFIG_XFS_POSIX_ACL | |
604 | STATIC int | |
416c6d5b | 605 | xfs_vn_permission( |
1da177e4 LT |
606 | struct inode *inode, |
607 | int mode, | |
608 | struct nameidata *nd) | |
609 | { | |
67fcaa73 | 610 | return -bhv_vop_access(vn_from_inode(inode), mode << 6, NULL); |
1da177e4 LT |
611 | } |
612 | #else | |
416c6d5b | 613 | #define xfs_vn_permission NULL |
1da177e4 LT |
614 | #endif |
615 | ||
616 | STATIC int | |
416c6d5b | 617 | xfs_vn_getattr( |
1da177e4 LT |
618 | struct vfsmount *mnt, |
619 | struct dentry *dentry, | |
620 | struct kstat *stat) | |
621 | { | |
622 | struct inode *inode = dentry->d_inode; | |
67fcaa73 | 623 | bhv_vnode_t *vp = vn_from_inode(inode); |
1da177e4 LT |
624 | int error = 0; |
625 | ||
626 | if (unlikely(vp->v_flag & VMODIFIED)) | |
627 | error = vn_revalidate(vp); | |
628 | if (!error) | |
629 | generic_fillattr(inode, stat); | |
b76963fa | 630 | return -error; |
1da177e4 LT |
631 | } |
632 | ||
633 | STATIC int | |
416c6d5b | 634 | xfs_vn_setattr( |
1da177e4 LT |
635 | struct dentry *dentry, |
636 | struct iattr *attr) | |
637 | { | |
638 | struct inode *inode = dentry->d_inode; | |
639 | unsigned int ia_valid = attr->ia_valid; | |
67fcaa73 | 640 | bhv_vnode_t *vp = vn_from_inode(inode); |
8285fb58 | 641 | bhv_vattr_t vattr = { 0 }; |
1da177e4 LT |
642 | int flags = 0; |
643 | int error; | |
644 | ||
1da177e4 LT |
645 | if (ia_valid & ATTR_UID) { |
646 | vattr.va_mask |= XFS_AT_UID; | |
647 | vattr.va_uid = attr->ia_uid; | |
648 | } | |
649 | if (ia_valid & ATTR_GID) { | |
650 | vattr.va_mask |= XFS_AT_GID; | |
651 | vattr.va_gid = attr->ia_gid; | |
652 | } | |
653 | if (ia_valid & ATTR_SIZE) { | |
654 | vattr.va_mask |= XFS_AT_SIZE; | |
655 | vattr.va_size = attr->ia_size; | |
656 | } | |
657 | if (ia_valid & ATTR_ATIME) { | |
658 | vattr.va_mask |= XFS_AT_ATIME; | |
659 | vattr.va_atime = attr->ia_atime; | |
8c0b5113 | 660 | inode->i_atime = attr->ia_atime; |
1da177e4 LT |
661 | } |
662 | if (ia_valid & ATTR_MTIME) { | |
663 | vattr.va_mask |= XFS_AT_MTIME; | |
664 | vattr.va_mtime = attr->ia_mtime; | |
665 | } | |
666 | if (ia_valid & ATTR_CTIME) { | |
667 | vattr.va_mask |= XFS_AT_CTIME; | |
668 | vattr.va_ctime = attr->ia_ctime; | |
669 | } | |
670 | if (ia_valid & ATTR_MODE) { | |
671 | vattr.va_mask |= XFS_AT_MODE; | |
672 | vattr.va_mode = attr->ia_mode; | |
673 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | |
674 | inode->i_mode &= ~S_ISGID; | |
675 | } | |
676 | ||
677 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) | |
678 | flags |= ATTR_UTIME; | |
679 | #ifdef ATTR_NO_BLOCK | |
680 | if ((ia_valid & ATTR_NO_BLOCK)) | |
681 | flags |= ATTR_NONBLOCK; | |
682 | #endif | |
683 | ||
67fcaa73 | 684 | error = bhv_vop_setattr(vp, &vattr, flags, NULL); |
220b5284 NS |
685 | if (likely(!error)) |
686 | __vn_revalidate(vp, &vattr); | |
687 | return -error; | |
1da177e4 LT |
688 | } |
689 | ||
690 | STATIC void | |
416c6d5b | 691 | xfs_vn_truncate( |
1da177e4 LT |
692 | struct inode *inode) |
693 | { | |
c2536668 | 694 | block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks); |
1da177e4 LT |
695 | } |
696 | ||
697 | STATIC int | |
416c6d5b | 698 | xfs_vn_setxattr( |
1da177e4 LT |
699 | struct dentry *dentry, |
700 | const char *name, | |
701 | const void *data, | |
702 | size_t size, | |
703 | int flags) | |
704 | { | |
67fcaa73 | 705 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
706 | char *attr = (char *)name; |
707 | attrnames_t *namesp; | |
708 | int xflags = 0; | |
709 | int error; | |
710 | ||
711 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
712 | if (!namesp) | |
713 | return -EOPNOTSUPP; | |
714 | attr += namesp->attr_namelen; | |
715 | error = namesp->attr_capable(vp, NULL); | |
716 | if (error) | |
717 | return error; | |
718 | ||
719 | /* Convert Linux syscall to XFS internal ATTR flags */ | |
720 | if (flags & XATTR_CREATE) | |
721 | xflags |= ATTR_CREATE; | |
722 | if (flags & XATTR_REPLACE) | |
723 | xflags |= ATTR_REPLACE; | |
724 | xflags |= namesp->attr_flag; | |
725 | return namesp->attr_set(vp, attr, (void *)data, size, xflags); | |
726 | } | |
727 | ||
728 | STATIC ssize_t | |
416c6d5b | 729 | xfs_vn_getxattr( |
1da177e4 LT |
730 | struct dentry *dentry, |
731 | const char *name, | |
732 | void *data, | |
733 | size_t size) | |
734 | { | |
67fcaa73 | 735 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
736 | char *attr = (char *)name; |
737 | attrnames_t *namesp; | |
738 | int xflags = 0; | |
739 | ssize_t error; | |
740 | ||
741 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
742 | if (!namesp) | |
743 | return -EOPNOTSUPP; | |
744 | attr += namesp->attr_namelen; | |
745 | error = namesp->attr_capable(vp, NULL); | |
746 | if (error) | |
747 | return error; | |
748 | ||
749 | /* Convert Linux syscall to XFS internal ATTR flags */ | |
750 | if (!size) { | |
751 | xflags |= ATTR_KERNOVAL; | |
752 | data = NULL; | |
753 | } | |
754 | xflags |= namesp->attr_flag; | |
755 | return namesp->attr_get(vp, attr, (void *)data, size, xflags); | |
756 | } | |
757 | ||
758 | STATIC ssize_t | |
416c6d5b | 759 | xfs_vn_listxattr( |
1da177e4 LT |
760 | struct dentry *dentry, |
761 | char *data, | |
762 | size_t size) | |
763 | { | |
67fcaa73 | 764 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
765 | int error, xflags = ATTR_KERNAMELS; |
766 | ssize_t result; | |
767 | ||
768 | if (!size) | |
769 | xflags |= ATTR_KERNOVAL; | |
770 | xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS; | |
771 | ||
772 | error = attr_generic_list(vp, data, size, xflags, &result); | |
773 | if (error < 0) | |
774 | return error; | |
775 | return result; | |
776 | } | |
777 | ||
778 | STATIC int | |
416c6d5b | 779 | xfs_vn_removexattr( |
1da177e4 LT |
780 | struct dentry *dentry, |
781 | const char *name) | |
782 | { | |
67fcaa73 | 783 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
784 | char *attr = (char *)name; |
785 | attrnames_t *namesp; | |
786 | int xflags = 0; | |
787 | int error; | |
788 | ||
789 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
790 | if (!namesp) | |
791 | return -EOPNOTSUPP; | |
792 | attr += namesp->attr_namelen; | |
793 | error = namesp->attr_capable(vp, NULL); | |
794 | if (error) | |
795 | return error; | |
796 | xflags |= namesp->attr_flag; | |
797 | return namesp->attr_remove(vp, attr, xflags); | |
798 | } | |
799 | ||
800 | ||
416c6d5b NS |
801 | struct inode_operations xfs_inode_operations = { |
802 | .permission = xfs_vn_permission, | |
803 | .truncate = xfs_vn_truncate, | |
804 | .getattr = xfs_vn_getattr, | |
805 | .setattr = xfs_vn_setattr, | |
806 | .setxattr = xfs_vn_setxattr, | |
807 | .getxattr = xfs_vn_getxattr, | |
808 | .listxattr = xfs_vn_listxattr, | |
809 | .removexattr = xfs_vn_removexattr, | |
1da177e4 LT |
810 | }; |
811 | ||
416c6d5b NS |
812 | struct inode_operations xfs_dir_inode_operations = { |
813 | .create = xfs_vn_create, | |
814 | .lookup = xfs_vn_lookup, | |
815 | .link = xfs_vn_link, | |
816 | .unlink = xfs_vn_unlink, | |
817 | .symlink = xfs_vn_symlink, | |
818 | .mkdir = xfs_vn_mkdir, | |
819 | .rmdir = xfs_vn_rmdir, | |
820 | .mknod = xfs_vn_mknod, | |
821 | .rename = xfs_vn_rename, | |
822 | .permission = xfs_vn_permission, | |
823 | .getattr = xfs_vn_getattr, | |
824 | .setattr = xfs_vn_setattr, | |
825 | .setxattr = xfs_vn_setxattr, | |
826 | .getxattr = xfs_vn_getxattr, | |
827 | .listxattr = xfs_vn_listxattr, | |
828 | .removexattr = xfs_vn_removexattr, | |
1da177e4 LT |
829 | }; |
830 | ||
416c6d5b | 831 | struct inode_operations xfs_symlink_inode_operations = { |
1da177e4 | 832 | .readlink = generic_readlink, |
416c6d5b NS |
833 | .follow_link = xfs_vn_follow_link, |
834 | .put_link = xfs_vn_put_link, | |
835 | .permission = xfs_vn_permission, | |
836 | .getattr = xfs_vn_getattr, | |
837 | .setattr = xfs_vn_setattr, | |
838 | .setxattr = xfs_vn_setxattr, | |
839 | .getxattr = xfs_vn_getxattr, | |
840 | .listxattr = xfs_vn_listxattr, | |
841 | .removexattr = xfs_vn_removexattr, | |
1da177e4 | 842 | }; |