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