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 | 157 | |
4aeb664c NS |
158 | if (flags & XFS_ICHGTIME_MOD) { |
159 | tvp = &inode->i_mtime; | |
160 | ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec; | |
161 | ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec; | |
162 | } | |
4aeb664c NS |
163 | if (flags & XFS_ICHGTIME_CHG) { |
164 | tvp = &inode->i_ctime; | |
165 | ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec; | |
166 | ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec; | |
167 | } | |
168 | ||
169 | /* | |
170 | * We update the i_update_core field _after_ changing | |
171 | * the timestamps in order to coordinate properly with | |
172 | * xfs_iflush() so that we don't lose timestamp updates. | |
173 | * This keeps us from having to hold the inode lock | |
174 | * while doing this. We use the SYNCHRONIZE macro to | |
175 | * ensure that the compiler does not reorder the update | |
176 | * of i_update_core above the timestamp updates above. | |
177 | */ | |
178 | SYNCHRONIZE(); | |
179 | ip->i_update_core = 1; | |
cf10e82b | 180 | if (!(inode->i_state & I_NEW)) |
4aeb664c NS |
181 | mark_inode_dirty_sync(inode); |
182 | } | |
183 | ||
1da177e4 LT |
184 | |
185 | /* | |
186 | * Pull the link count and size up from the xfs inode to the linux inode | |
187 | */ | |
188 | STATIC void | |
416c6d5b | 189 | xfs_validate_fields( |
6572bc28 | 190 | struct inode *inode) |
1da177e4 | 191 | { |
6572bc28 CH |
192 | struct xfs_inode *ip = XFS_I(inode); |
193 | loff_t size; | |
194 | ||
6572bc28 CH |
195 | /* we're under i_sem so i_size can't change under us */ |
196 | size = XFS_ISIZE(ip); | |
197 | if (i_size_read(inode) != size) | |
198 | i_size_write(inode, size); | |
1da177e4 LT |
199 | } |
200 | ||
446ada4a NS |
201 | /* |
202 | * Hook in SELinux. This is not quite correct yet, what we really need | |
203 | * here (as we do for default ACLs) is a mechanism by which creation of | |
204 | * these attrs can be journalled at inode creation time (along with the | |
205 | * inode, of course, such that log replay can't cause these to be lost). | |
206 | */ | |
207 | STATIC int | |
416c6d5b | 208 | xfs_init_security( |
af048193 | 209 | struct inode *inode, |
446ada4a NS |
210 | struct inode *dir) |
211 | { | |
af048193 | 212 | struct xfs_inode *ip = XFS_I(inode); |
446ada4a NS |
213 | size_t length; |
214 | void *value; | |
215 | char *name; | |
216 | int error; | |
217 | ||
af048193 CH |
218 | error = security_inode_init_security(inode, dir, &name, |
219 | &value, &length); | |
446ada4a NS |
220 | if (error) { |
221 | if (error == -EOPNOTSUPP) | |
222 | return 0; | |
223 | return -error; | |
224 | } | |
225 | ||
af048193 | 226 | error = xfs_attr_set(ip, name, value, length, ATTR_SECURE); |
446ada4a | 227 | if (!error) |
af048193 | 228 | xfs_iflags_set(ip, XFS_IMODIFIED); |
446ada4a NS |
229 | |
230 | kfree(name); | |
231 | kfree(value); | |
232 | return error; | |
233 | } | |
234 | ||
556b8b16 BN |
235 | static void |
236 | xfs_dentry_to_name( | |
237 | struct xfs_name *namep, | |
238 | struct dentry *dentry) | |
239 | { | |
240 | namep->name = dentry->d_name.name; | |
241 | namep->len = dentry->d_name.len; | |
242 | } | |
243 | ||
7989cb8e | 244 | STATIC void |
416c6d5b | 245 | xfs_cleanup_inode( |
739bfb2a | 246 | struct inode *dir, |
af048193 | 247 | struct inode *inode, |
8f112e3b | 248 | struct dentry *dentry) |
3a69c7dc | 249 | { |
556b8b16 | 250 | struct xfs_name teardown; |
3a69c7dc YL |
251 | |
252 | /* Oh, the horror. | |
220b5284 | 253 | * If we can't add the ACL or we fail in |
416c6d5b | 254 | * xfs_init_security we must back out. |
3a69c7dc YL |
255 | * ENOSPC can hit here, among other things. |
256 | */ | |
556b8b16 | 257 | xfs_dentry_to_name(&teardown, dentry); |
3a69c7dc | 258 | |
8f112e3b | 259 | xfs_remove(XFS_I(dir), &teardown, XFS_I(inode)); |
af048193 | 260 | iput(inode); |
3a69c7dc YL |
261 | } |
262 | ||
1da177e4 | 263 | STATIC int |
416c6d5b | 264 | xfs_vn_mknod( |
1da177e4 LT |
265 | struct inode *dir, |
266 | struct dentry *dentry, | |
267 | int mode, | |
268 | dev_t rdev) | |
269 | { | |
db0bb7ba | 270 | struct inode *inode; |
979ebab1 | 271 | struct xfs_inode *ip = NULL; |
1da177e4 | 272 | xfs_acl_t *default_acl = NULL; |
556b8b16 | 273 | struct xfs_name name; |
0ec58516 | 274 | int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS; |
1da177e4 LT |
275 | int error; |
276 | ||
277 | /* | |
278 | * Irix uses Missed'em'V split, but doesn't want to see | |
279 | * the upper 5 bits of (14bit) major. | |
280 | */ | |
220b5284 | 281 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
1da177e4 LT |
282 | return -EINVAL; |
283 | ||
979ebab1 | 284 | if (test_default_acl && test_default_acl(dir)) { |
220b5284 | 285 | if (!_ACL_ALLOC(default_acl)) { |
1da177e4 | 286 | return -ENOMEM; |
220b5284 | 287 | } |
979ebab1 | 288 | if (!_ACL_GET_DEFAULT(dir, default_acl)) { |
1da177e4 LT |
289 | _ACL_FREE(default_acl); |
290 | default_acl = NULL; | |
291 | } | |
292 | } | |
293 | ||
556b8b16 BN |
294 | xfs_dentry_to_name(&name, dentry); |
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: |
556b8b16 | 306 | error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL); |
1da177e4 LT |
307 | break; |
308 | case S_IFDIR: | |
556b8b16 | 309 | error = xfs_mkdir(XFS_I(dir), &name, 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: | |
8f112e3b | 341 | xfs_cleanup_inode(dir, inode, dentry); |
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; |
556b8b16 | 374 | struct xfs_name name; |
1da177e4 LT |
375 | int error; |
376 | ||
377 | if (dentry->d_name.len >= MAXNAMELEN) | |
378 | return ERR_PTR(-ENAMETOOLONG); | |
379 | ||
556b8b16 | 380 | xfs_dentry_to_name(&name, dentry); |
384f3ced | 381 | error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); |
67fcaa73 | 382 | if (unlikely(error)) { |
1da177e4 LT |
383 | if (unlikely(error != ENOENT)) |
384 | return ERR_PTR(-error); | |
385 | d_add(dentry, NULL); | |
386 | return NULL; | |
387 | } | |
388 | ||
ef1f5e7a | 389 | return d_splice_alias(cip->i_vnode, dentry); |
1da177e4 LT |
390 | } |
391 | ||
384f3ced BN |
392 | STATIC struct dentry * |
393 | xfs_vn_ci_lookup( | |
394 | struct inode *dir, | |
395 | struct dentry *dentry, | |
396 | struct nameidata *nd) | |
397 | { | |
398 | struct xfs_inode *ip; | |
399 | struct xfs_name xname; | |
400 | struct xfs_name ci_name; | |
401 | struct qstr dname; | |
402 | int error; | |
403 | ||
404 | if (dentry->d_name.len >= MAXNAMELEN) | |
405 | return ERR_PTR(-ENAMETOOLONG); | |
406 | ||
407 | xfs_dentry_to_name(&xname, dentry); | |
408 | error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name); | |
409 | if (unlikely(error)) { | |
410 | if (unlikely(error != ENOENT)) | |
411 | return ERR_PTR(-error); | |
866d5dc9 BN |
412 | /* |
413 | * call d_add(dentry, NULL) here when d_drop_negative_children | |
414 | * is called in xfs_vn_mknod (ie. allow negative dentries | |
415 | * with CI filesystems). | |
416 | */ | |
384f3ced BN |
417 | return NULL; |
418 | } | |
419 | ||
420 | /* if exact match, just splice and exit */ | |
421 | if (!ci_name.name) | |
422 | return d_splice_alias(ip->i_vnode, dentry); | |
423 | ||
424 | /* else case-insensitive match... */ | |
425 | dname.name = ci_name.name; | |
426 | dname.len = ci_name.len; | |
427 | dentry = d_add_ci(ip->i_vnode, dentry, &dname); | |
428 | kmem_free(ci_name.name); | |
429 | return dentry; | |
430 | } | |
431 | ||
1da177e4 | 432 | STATIC int |
416c6d5b | 433 | xfs_vn_link( |
1da177e4 LT |
434 | struct dentry *old_dentry, |
435 | struct inode *dir, | |
436 | struct dentry *dentry) | |
437 | { | |
a3da7896 | 438 | struct inode *inode; /* inode of guy being linked to */ |
556b8b16 | 439 | struct xfs_name name; |
1da177e4 LT |
440 | int error; |
441 | ||
a3da7896 | 442 | inode = old_dentry->d_inode; |
556b8b16 | 443 | xfs_dentry_to_name(&name, dentry); |
1da177e4 | 444 | |
a3da7896 | 445 | igrab(inode); |
556b8b16 | 446 | error = xfs_link(XFS_I(dir), XFS_I(inode), &name); |
97dfd70c | 447 | if (unlikely(error)) { |
a3da7896 CH |
448 | iput(inode); |
449 | return -error; | |
1da177e4 | 450 | } |
a3da7896 CH |
451 | |
452 | xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED); | |
453 | xfs_validate_fields(inode); | |
454 | d_instantiate(dentry, inode); | |
455 | return 0; | |
1da177e4 LT |
456 | } |
457 | ||
458 | STATIC int | |
416c6d5b | 459 | xfs_vn_unlink( |
1da177e4 LT |
460 | struct inode *dir, |
461 | struct dentry *dentry) | |
462 | { | |
463 | struct inode *inode; | |
556b8b16 | 464 | struct xfs_name name; |
1da177e4 LT |
465 | int error; |
466 | ||
467 | inode = dentry->d_inode; | |
556b8b16 | 468 | xfs_dentry_to_name(&name, dentry); |
1da177e4 | 469 | |
556b8b16 | 470 | error = xfs_remove(XFS_I(dir), &name, XFS_I(inode)); |
220b5284 | 471 | if (likely(!error)) { |
6572bc28 CH |
472 | xfs_validate_fields(dir); /* size needs update */ |
473 | xfs_validate_fields(inode); | |
d532506c BN |
474 | /* |
475 | * With unlink, the VFS makes the dentry "negative": no inode, | |
476 | * but still hashed. This is incompatible with case-insensitive | |
477 | * mode, so invalidate (unhash) the dentry in CI-mode. | |
478 | */ | |
479 | if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb)) | |
480 | d_invalidate(dentry); | |
1da177e4 | 481 | } |
1da177e4 LT |
482 | return -error; |
483 | } | |
484 | ||
485 | STATIC int | |
416c6d5b | 486 | xfs_vn_symlink( |
1da177e4 LT |
487 | struct inode *dir, |
488 | struct dentry *dentry, | |
489 | const char *symname) | |
490 | { | |
3937be5b CH |
491 | struct inode *inode; |
492 | struct xfs_inode *cip = NULL; | |
556b8b16 | 493 | struct xfs_name name; |
1da177e4 | 494 | int error; |
3e5daf05 | 495 | mode_t mode; |
1da177e4 | 496 | |
3e5daf05 | 497 | mode = S_IFLNK | |
0432dab2 | 498 | (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO); |
556b8b16 | 499 | xfs_dentry_to_name(&name, dentry); |
1da177e4 | 500 | |
556b8b16 | 501 | error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL); |
3937be5b CH |
502 | if (unlikely(error)) |
503 | goto out; | |
504 | ||
505 | inode = cip->i_vnode; | |
506 | ||
507 | error = xfs_init_security(inode, dir); | |
508 | if (unlikely(error)) | |
509 | goto out_cleanup_inode; | |
510 | ||
511 | d_instantiate(dentry, inode); | |
512 | xfs_validate_fields(dir); | |
513 | xfs_validate_fields(inode); | |
514 | return 0; | |
515 | ||
516 | out_cleanup_inode: | |
8f112e3b | 517 | xfs_cleanup_inode(dir, inode, dentry); |
3937be5b | 518 | out: |
1da177e4 LT |
519 | return -error; |
520 | } | |
521 | ||
1da177e4 | 522 | STATIC int |
416c6d5b | 523 | xfs_vn_rename( |
1da177e4 LT |
524 | struct inode *odir, |
525 | struct dentry *odentry, | |
526 | struct inode *ndir, | |
527 | struct dentry *ndentry) | |
528 | { | |
529 | struct inode *new_inode = ndentry->d_inode; | |
556b8b16 BN |
530 | struct xfs_name oname; |
531 | struct xfs_name nname; | |
1da177e4 LT |
532 | int error; |
533 | ||
556b8b16 BN |
534 | xfs_dentry_to_name(&oname, odentry); |
535 | xfs_dentry_to_name(&nname, ndentry); | |
536 | ||
537 | error = xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode), | |
cfa853e4 CH |
538 | XFS_I(ndir), &nname, new_inode ? |
539 | XFS_I(new_inode) : NULL); | |
220b5284 NS |
540 | if (likely(!error)) { |
541 | if (new_inode) | |
6572bc28 CH |
542 | xfs_validate_fields(new_inode); |
543 | xfs_validate_fields(odir); | |
220b5284 | 544 | if (ndir != odir) |
6572bc28 | 545 | xfs_validate_fields(ndir); |
220b5284 | 546 | } |
220b5284 | 547 | return -error; |
1da177e4 LT |
548 | } |
549 | ||
550 | /* | |
551 | * careful here - this function can get called recursively, so | |
552 | * we need to be very careful about how much stack we use. | |
553 | * uio is kmalloced for this reason... | |
554 | */ | |
008b150a | 555 | STATIC void * |
416c6d5b | 556 | xfs_vn_follow_link( |
1da177e4 LT |
557 | struct dentry *dentry, |
558 | struct nameidata *nd) | |
559 | { | |
1da177e4 | 560 | char *link; |
804c83c3 | 561 | int error = -ENOMEM; |
1da177e4 | 562 | |
f52720ca | 563 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
804c83c3 CH |
564 | if (!link) |
565 | goto out_err; | |
1da177e4 | 566 | |
739bfb2a | 567 | error = -xfs_readlink(XFS_I(dentry->d_inode), link); |
804c83c3 CH |
568 | if (unlikely(error)) |
569 | goto out_kfree; | |
1da177e4 LT |
570 | |
571 | nd_set_link(nd, link); | |
008b150a | 572 | return NULL; |
804c83c3 CH |
573 | |
574 | out_kfree: | |
575 | kfree(link); | |
576 | out_err: | |
577 | nd_set_link(nd, ERR_PTR(error)); | |
578 | return NULL; | |
1da177e4 LT |
579 | } |
580 | ||
cde410a9 | 581 | STATIC void |
416c6d5b | 582 | xfs_vn_put_link( |
cde410a9 NS |
583 | struct dentry *dentry, |
584 | struct nameidata *nd, | |
585 | void *p) | |
1da177e4 | 586 | { |
cde410a9 NS |
587 | char *s = nd_get_link(nd); |
588 | ||
1da177e4 LT |
589 | if (!IS_ERR(s)) |
590 | kfree(s); | |
591 | } | |
592 | ||
593 | #ifdef CONFIG_XFS_POSIX_ACL | |
4576758d CH |
594 | STATIC int |
595 | xfs_check_acl( | |
596 | struct inode *inode, | |
597 | int mask) | |
598 | { | |
599 | struct xfs_inode *ip = XFS_I(inode); | |
600 | int error; | |
601 | ||
602 | xfs_itrace_entry(ip); | |
603 | ||
604 | if (XFS_IFORK_Q(ip)) { | |
605 | error = xfs_acl_iaccess(ip, mask, NULL); | |
606 | if (error != -1) | |
607 | return -error; | |
608 | } | |
609 | ||
610 | return -EAGAIN; | |
611 | } | |
612 | ||
1da177e4 | 613 | STATIC int |
416c6d5b | 614 | xfs_vn_permission( |
4576758d | 615 | struct inode *inode, |
e6305c43 | 616 | int mask) |
1da177e4 | 617 | { |
4576758d | 618 | return generic_permission(inode, mask, xfs_check_acl); |
1da177e4 LT |
619 | } |
620 | #else | |
416c6d5b | 621 | #define xfs_vn_permission NULL |
1da177e4 LT |
622 | #endif |
623 | ||
624 | STATIC int | |
416c6d5b | 625 | xfs_vn_getattr( |
c43f4087 CH |
626 | struct vfsmount *mnt, |
627 | struct dentry *dentry, | |
628 | struct kstat *stat) | |
1da177e4 | 629 | { |
c43f4087 CH |
630 | struct inode *inode = dentry->d_inode; |
631 | struct xfs_inode *ip = XFS_I(inode); | |
632 | struct xfs_mount *mp = ip->i_mount; | |
633 | ||
634 | xfs_itrace_entry(ip); | |
635 | ||
636 | if (XFS_FORCED_SHUTDOWN(mp)) | |
637 | return XFS_ERROR(EIO); | |
638 | ||
639 | stat->size = XFS_ISIZE(ip); | |
640 | stat->dev = inode->i_sb->s_dev; | |
641 | stat->mode = ip->i_d.di_mode; | |
642 | stat->nlink = ip->i_d.di_nlink; | |
643 | stat->uid = ip->i_d.di_uid; | |
644 | stat->gid = ip->i_d.di_gid; | |
645 | stat->ino = ip->i_ino; | |
646 | #if XFS_BIG_INUMS | |
647 | stat->ino += mp->m_inoadd; | |
648 | #endif | |
649 | stat->atime = inode->i_atime; | |
650 | stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec; | |
651 | stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; | |
652 | stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec; | |
653 | stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; | |
654 | stat->blocks = | |
655 | XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); | |
656 | ||
657 | ||
658 | switch (inode->i_mode & S_IFMT) { | |
659 | case S_IFBLK: | |
660 | case S_IFCHR: | |
661 | stat->blksize = BLKDEV_IOSIZE; | |
662 | stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, | |
663 | sysv_minor(ip->i_df.if_u2.if_rdev)); | |
664 | break; | |
665 | default: | |
71ddabb9 | 666 | if (XFS_IS_REALTIME_INODE(ip)) { |
c43f4087 CH |
667 | /* |
668 | * If the file blocks are being allocated from a | |
669 | * realtime volume, then return the inode's realtime | |
670 | * extent size or the realtime volume's extent size. | |
671 | */ | |
672 | stat->blksize = | |
673 | xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; | |
674 | } else | |
675 | stat->blksize = xfs_preferred_iosize(mp); | |
676 | stat->rdev = 0; | |
677 | break; | |
69e23b9a | 678 | } |
c43f4087 CH |
679 | |
680 | return 0; | |
1da177e4 LT |
681 | } |
682 | ||
683 | STATIC int | |
416c6d5b | 684 | xfs_vn_setattr( |
1da177e4 LT |
685 | struct dentry *dentry, |
686 | struct iattr *attr) | |
687 | { | |
688 | struct inode *inode = dentry->d_inode; | |
689 | unsigned int ia_valid = attr->ia_valid; | |
8285fb58 | 690 | bhv_vattr_t vattr = { 0 }; |
1da177e4 LT |
691 | int flags = 0; |
692 | int error; | |
693 | ||
1da177e4 LT |
694 | if (ia_valid & ATTR_UID) { |
695 | vattr.va_mask |= XFS_AT_UID; | |
696 | vattr.va_uid = attr->ia_uid; | |
697 | } | |
698 | if (ia_valid & ATTR_GID) { | |
699 | vattr.va_mask |= XFS_AT_GID; | |
700 | vattr.va_gid = attr->ia_gid; | |
701 | } | |
702 | if (ia_valid & ATTR_SIZE) { | |
703 | vattr.va_mask |= XFS_AT_SIZE; | |
704 | vattr.va_size = attr->ia_size; | |
705 | } | |
706 | if (ia_valid & ATTR_ATIME) { | |
707 | vattr.va_mask |= XFS_AT_ATIME; | |
708 | vattr.va_atime = attr->ia_atime; | |
8c0b5113 | 709 | inode->i_atime = attr->ia_atime; |
1da177e4 LT |
710 | } |
711 | if (ia_valid & ATTR_MTIME) { | |
712 | vattr.va_mask |= XFS_AT_MTIME; | |
713 | vattr.va_mtime = attr->ia_mtime; | |
714 | } | |
715 | if (ia_valid & ATTR_CTIME) { | |
716 | vattr.va_mask |= XFS_AT_CTIME; | |
717 | vattr.va_ctime = attr->ia_ctime; | |
718 | } | |
719 | if (ia_valid & ATTR_MODE) { | |
720 | vattr.va_mask |= XFS_AT_MODE; | |
721 | vattr.va_mode = attr->ia_mode; | |
722 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | |
723 | inode->i_mode &= ~S_ISGID; | |
724 | } | |
725 | ||
726 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) | |
727 | flags |= ATTR_UTIME; | |
728 | #ifdef ATTR_NO_BLOCK | |
729 | if ((ia_valid & ATTR_NO_BLOCK)) | |
730 | flags |= ATTR_NONBLOCK; | |
731 | #endif | |
732 | ||
739bfb2a | 733 | error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL); |
220b5284 | 734 | if (likely(!error)) |
21a62542 | 735 | vn_revalidate(vn_from_inode(inode)); |
220b5284 | 736 | return -error; |
1da177e4 LT |
737 | } |
738 | ||
d87dd636 DC |
739 | /* |
740 | * block_truncate_page can return an error, but we can't propagate it | |
741 | * at all here. Leave a complaint + stack trace in the syslog because | |
742 | * this could be bad. If it is bad, we need to propagate the error further. | |
743 | */ | |
1da177e4 | 744 | STATIC void |
416c6d5b | 745 | xfs_vn_truncate( |
1da177e4 LT |
746 | struct inode *inode) |
747 | { | |
d87dd636 DC |
748 | int error; |
749 | error = block_truncate_page(inode->i_mapping, inode->i_size, | |
750 | xfs_get_blocks); | |
751 | WARN_ON(error); | |
1da177e4 LT |
752 | } |
753 | ||
3ed65264 DC |
754 | STATIC long |
755 | xfs_vn_fallocate( | |
756 | struct inode *inode, | |
757 | int mode, | |
758 | loff_t offset, | |
759 | loff_t len) | |
760 | { | |
761 | long error; | |
762 | loff_t new_size = 0; | |
763 | xfs_flock64_t bf; | |
764 | xfs_inode_t *ip = XFS_I(inode); | |
765 | ||
766 | /* preallocation on directories not yet supported */ | |
767 | error = -ENODEV; | |
768 | if (S_ISDIR(inode->i_mode)) | |
769 | goto out_error; | |
770 | ||
771 | bf.l_whence = 0; | |
772 | bf.l_start = offset; | |
773 | bf.l_len = len; | |
774 | ||
775 | xfs_ilock(ip, XFS_IOLOCK_EXCL); | |
776 | error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf, | |
777 | 0, NULL, ATTR_NOLOCK); | |
778 | if (!error && !(mode & FALLOC_FL_KEEP_SIZE) && | |
779 | offset + len > i_size_read(inode)) | |
780 | new_size = offset + len; | |
781 | ||
782 | /* Change file size if needed */ | |
783 | if (new_size) { | |
784 | bhv_vattr_t va; | |
785 | ||
786 | va.va_mask = XFS_AT_SIZE; | |
787 | va.va_size = new_size; | |
788 | error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL); | |
789 | } | |
790 | ||
791 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); | |
792 | out_error: | |
793 | return error; | |
794 | } | |
1da177e4 | 795 | |
c5ef1c42 | 796 | const struct inode_operations xfs_inode_operations = { |
416c6d5b NS |
797 | .permission = xfs_vn_permission, |
798 | .truncate = xfs_vn_truncate, | |
799 | .getattr = xfs_vn_getattr, | |
800 | .setattr = xfs_vn_setattr, | |
0ec58516 LM |
801 | .setxattr = generic_setxattr, |
802 | .getxattr = generic_getxattr, | |
803 | .removexattr = generic_removexattr, | |
416c6d5b | 804 | .listxattr = xfs_vn_listxattr, |
3ed65264 | 805 | .fallocate = xfs_vn_fallocate, |
1da177e4 LT |
806 | }; |
807 | ||
c5ef1c42 | 808 | const struct inode_operations xfs_dir_inode_operations = { |
416c6d5b NS |
809 | .create = xfs_vn_create, |
810 | .lookup = xfs_vn_lookup, | |
811 | .link = xfs_vn_link, | |
812 | .unlink = xfs_vn_unlink, | |
813 | .symlink = xfs_vn_symlink, | |
814 | .mkdir = xfs_vn_mkdir, | |
8f112e3b CH |
815 | /* |
816 | * Yes, XFS uses the same method for rmdir and unlink. | |
817 | * | |
818 | * There are some subtile differences deeper in the code, | |
819 | * but we use S_ISDIR to check for those. | |
820 | */ | |
821 | .rmdir = xfs_vn_unlink, | |
416c6d5b NS |
822 | .mknod = xfs_vn_mknod, |
823 | .rename = xfs_vn_rename, | |
824 | .permission = xfs_vn_permission, | |
825 | .getattr = xfs_vn_getattr, | |
826 | .setattr = xfs_vn_setattr, | |
0ec58516 LM |
827 | .setxattr = generic_setxattr, |
828 | .getxattr = generic_getxattr, | |
829 | .removexattr = generic_removexattr, | |
416c6d5b | 830 | .listxattr = xfs_vn_listxattr, |
1da177e4 LT |
831 | }; |
832 | ||
384f3ced BN |
833 | const struct inode_operations xfs_dir_ci_inode_operations = { |
834 | .create = xfs_vn_create, | |
835 | .lookup = xfs_vn_ci_lookup, | |
836 | .link = xfs_vn_link, | |
837 | .unlink = xfs_vn_unlink, | |
838 | .symlink = xfs_vn_symlink, | |
839 | .mkdir = xfs_vn_mkdir, | |
8f112e3b CH |
840 | /* |
841 | * Yes, XFS uses the same method for rmdir and unlink. | |
842 | * | |
843 | * There are some subtile differences deeper in the code, | |
844 | * but we use S_ISDIR to check for those. | |
845 | */ | |
846 | .rmdir = xfs_vn_unlink, | |
384f3ced BN |
847 | .mknod = xfs_vn_mknod, |
848 | .rename = xfs_vn_rename, | |
849 | .permission = xfs_vn_permission, | |
850 | .getattr = xfs_vn_getattr, | |
851 | .setattr = xfs_vn_setattr, | |
0ec58516 LM |
852 | .setxattr = generic_setxattr, |
853 | .getxattr = generic_getxattr, | |
854 | .removexattr = generic_removexattr, | |
384f3ced | 855 | .listxattr = xfs_vn_listxattr, |
384f3ced BN |
856 | }; |
857 | ||
c5ef1c42 | 858 | const struct inode_operations xfs_symlink_inode_operations = { |
1da177e4 | 859 | .readlink = generic_readlink, |
416c6d5b NS |
860 | .follow_link = xfs_vn_follow_link, |
861 | .put_link = xfs_vn_put_link, | |
862 | .permission = xfs_vn_permission, | |
863 | .getattr = xfs_vn_getattr, | |
864 | .setattr = xfs_vn_setattr, | |
0ec58516 LM |
865 | .setxattr = generic_setxattr, |
866 | .getxattr = generic_getxattr, | |
867 | .removexattr = generic_removexattr, | |
416c6d5b | 868 | .listxattr = xfs_vn_listxattr, |
1da177e4 | 869 | }; |