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