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