Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 LT |
6 | #include "xfs.h" |
7 | #include "xfs_fs.h" | |
70a9883c | 8 | #include "xfs_shared.h" |
239880ef DC |
9 | #include "xfs_format.h" |
10 | #include "xfs_log_format.h" | |
11 | #include "xfs_trans_resv.h" | |
1da177e4 | 12 | #include "xfs_mount.h" |
1da177e4 | 13 | #include "xfs_inode.h" |
239880ef | 14 | #include "xfs_acl.h" |
239880ef | 15 | #include "xfs_quota.h" |
fd920008 AH |
16 | #include "xfs_da_format.h" |
17 | #include "xfs_da_btree.h" | |
1da177e4 | 18 | #include "xfs_attr.h" |
239880ef | 19 | #include "xfs_trans.h" |
d0489451 ZY |
20 | #include "xfs_trans_space.h" |
21 | #include "xfs_bmap_btree.h" | |
0b1b213f | 22 | #include "xfs_trace.h" |
27b52867 | 23 | #include "xfs_icache.h" |
c24b5dfa | 24 | #include "xfs_symlink.h" |
1b767ee3 | 25 | #include "xfs_dir2.h" |
68a9f5e7 | 26 | #include "xfs_iomap.h" |
a5155b87 | 27 | #include "xfs_error.h" |
9fefd5db | 28 | #include "xfs_ioctl.h" |
efc2efeb | 29 | #include "xfs_xattr.h" |
00acb28d | 30 | #include "xfs_file.h" |
acdddbe1 | 31 | #include "xfs_bmap.h" |
058dd70c | 32 | #include "xfs_zone_alloc.h" |
1da177e4 | 33 | |
ef14f0c1 | 34 | #include <linux/posix_acl.h> |
446ada4a | 35 | #include <linux/security.h> |
c3b1b131 | 36 | #include <linux/iversion.h> |
10c5db28 | 37 | #include <linux/fiemap.h> |
1da177e4 | 38 | |
93a8614e | 39 | /* |
c1e8d7c6 | 40 | * Directories have different lock order w.r.t. mmap_lock compared to regular |
93a8614e DC |
41 | * files. This is due to readdir potentially triggering page faults on a user |
42 | * buffer inside filldir(), and this happens with the ilock on the directory | |
43 | * held. For regular files, the lock order is the other way around - the | |
c1e8d7c6 | 44 | * mmap_lock is taken during the page fault, and then we lock the ilock to do |
93a8614e | 45 | * block mapping. Hence we need a different class for the directory ilock so |
bb6cdd55 DW |
46 | * that lockdep can tell them apart. Directories in the metadata directory |
47 | * tree get a separate class so that lockdep reports will warn us if someone | |
48 | * ever tries to lock regular directories after locking metadata directories. | |
93a8614e DC |
49 | */ |
50 | static struct lock_class_key xfs_nondir_ilock_class; | |
51 | static struct lock_class_key xfs_dir_ilock_class; | |
52 | ||
8d2a5e6e DC |
53 | static int |
54 | xfs_initxattrs( | |
55 | struct inode *inode, | |
56 | const struct xattr *xattr_array, | |
57 | void *fs_info) | |
9d8f13ba | 58 | { |
8d2a5e6e DC |
59 | const struct xattr *xattr; |
60 | struct xfs_inode *ip = XFS_I(inode); | |
61 | int error = 0; | |
9d8f13ba MZ |
62 | |
63 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { | |
a2544622 CH |
64 | struct xfs_da_args args = { |
65 | .dp = ip, | |
d5f0f49a | 66 | .attr_filter = XFS_ATTR_SECURE, |
a2544622 CH |
67 | .name = xattr->name, |
68 | .namelen = strlen(xattr->name), | |
69 | .value = xattr->value, | |
70 | .valuelen = xattr->value_len, | |
71 | }; | |
c27411d4 | 72 | error = xfs_attr_change(&args, XFS_ATTRUPDATE_UPSERT); |
9d8f13ba MZ |
73 | if (error < 0) |
74 | break; | |
75 | } | |
76 | return error; | |
77 | } | |
78 | ||
446ada4a NS |
79 | /* |
80 | * Hook in SELinux. This is not quite correct yet, what we really need | |
81 | * here (as we do for default ACLs) is a mechanism by which creation of | |
82 | * these attrs can be journalled at inode creation time (along with the | |
83 | * inode, of course, such that log replay can't cause these to be lost). | |
84 | */ | |
70b589a3 ES |
85 | int |
86 | xfs_inode_init_security( | |
af048193 | 87 | struct inode *inode, |
2a7dba39 EP |
88 | struct inode *dir, |
89 | const struct qstr *qstr) | |
446ada4a | 90 | { |
2451337d | 91 | return security_inode_init_security(inode, dir, qstr, |
a5a14de2 | 92 | &xfs_initxattrs, NULL); |
446ada4a NS |
93 | } |
94 | ||
556b8b16 BN |
95 | static void |
96 | xfs_dentry_to_name( | |
fab8eef8 AG |
97 | struct xfs_name *namep, |
98 | struct dentry *dentry) | |
99 | { | |
100 | namep->name = dentry->d_name.name; | |
101 | namep->len = dentry->d_name.len; | |
102 | namep->type = XFS_DIR3_FT_UNKNOWN; | |
103 | } | |
104 | ||
105 | static int | |
106 | xfs_dentry_mode_to_name( | |
556b8b16 | 107 | struct xfs_name *namep, |
0cb97766 DC |
108 | struct dentry *dentry, |
109 | int mode) | |
556b8b16 BN |
110 | { |
111 | namep->name = dentry->d_name.name; | |
112 | namep->len = dentry->d_name.len; | |
1fc4d33f | 113 | namep->type = xfs_mode_to_ftype(mode); |
fab8eef8 AG |
114 | |
115 | if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN)) | |
116 | return -EFSCORRUPTED; | |
117 | ||
118 | return 0; | |
556b8b16 BN |
119 | } |
120 | ||
7989cb8e | 121 | STATIC void |
416c6d5b | 122 | xfs_cleanup_inode( |
739bfb2a | 123 | struct inode *dir, |
af048193 | 124 | struct inode *inode, |
8f112e3b | 125 | struct dentry *dentry) |
3a69c7dc | 126 | { |
556b8b16 | 127 | struct xfs_name teardown; |
3a69c7dc YL |
128 | |
129 | /* Oh, the horror. | |
220b5284 | 130 | * If we can't add the ACL or we fail in |
70b589a3 | 131 | * xfs_inode_init_security we must back out. |
3a69c7dc YL |
132 | * ENOSPC can hit here, among other things. |
133 | */ | |
fab8eef8 | 134 | xfs_dentry_to_name(&teardown, dentry); |
3a69c7dc | 135 | |
8f112e3b | 136 | xfs_remove(XFS_I(dir), &teardown, XFS_I(inode)); |
3a69c7dc YL |
137 | } |
138 | ||
e6a688c3 DC |
139 | /* |
140 | * Check to see if we are likely to need an extended attribute to be added to | |
141 | * the inode we are about to allocate. This allows the attribute fork to be | |
142 | * created during the inode allocation, reducing the number of transactions we | |
143 | * need to do in this fast path. | |
144 | * | |
145 | * The security checks are optimistic, but not guaranteed. The two LSMs that | |
146 | * require xattrs to be added here (selinux and smack) are also the only two | |
147 | * LSMs that add a sb->s_security structure to the superblock. Hence if security | |
148 | * is enabled and sb->s_security is set, we have a pretty good idea that we are | |
149 | * going to be asked to add a security xattr immediately after allocating the | |
150 | * xfs inode and instantiating the VFS inode. | |
151 | */ | |
152 | static inline bool | |
153 | xfs_create_need_xattr( | |
154 | struct inode *dir, | |
155 | struct posix_acl *default_acl, | |
156 | struct posix_acl *acl) | |
157 | { | |
158 | if (acl) | |
159 | return true; | |
160 | if (default_acl) | |
161 | return true; | |
162 | #if IS_ENABLED(CONFIG_SECURITY) | |
163 | if (dir->i_sb->s_security) | |
164 | return true; | |
165 | #endif | |
166 | return false; | |
167 | } | |
168 | ||
169 | ||
1da177e4 | 170 | STATIC int |
d540e43b | 171 | xfs_generic_create( |
f2d40141 CB |
172 | struct mnt_idmap *idmap, |
173 | struct inode *dir, | |
174 | struct dentry *dentry, | |
175 | umode_t mode, | |
176 | dev_t rdev, | |
177 | struct file *tmpfile) /* unnamed file */ | |
1da177e4 | 178 | { |
dfaf8842 DW |
179 | struct xfs_icreate_args args = { |
180 | .idmap = idmap, | |
181 | .pip = XFS_I(dir), | |
182 | .rdev = rdev, | |
183 | .mode = mode, | |
184 | }; | |
185 | struct inode *inode; | |
186 | struct xfs_inode *ip = NULL; | |
187 | struct posix_acl *default_acl, *acl; | |
188 | struct xfs_name name; | |
189 | int error; | |
1da177e4 LT |
190 | |
191 | /* | |
192 | * Irix uses Missed'em'V split, but doesn't want to see | |
193 | * the upper 5 bits of (14bit) major. | |
194 | */ | |
dfaf8842 DW |
195 | if (S_ISCHR(args.mode) || S_ISBLK(args.mode)) { |
196 | if (unlikely(!sysv_valid_dev(args.rdev) || | |
197 | MAJOR(args.rdev) & ~0x1ff)) | |
517b5e8c | 198 | return -EINVAL; |
517b5e8c | 199 | } else { |
dfaf8842 | 200 | args.rdev = 0; |
517b5e8c | 201 | } |
1da177e4 | 202 | |
dfaf8842 | 203 | error = posix_acl_create(dir, &args.mode, &default_acl, &acl); |
2401dc29 CH |
204 | if (error) |
205 | return error; | |
1da177e4 | 206 | |
fab8eef8 | 207 | /* Verify mode is valid also for tmpfile case */ |
dfaf8842 | 208 | error = xfs_dentry_mode_to_name(&name, dentry, args.mode); |
fab8eef8 AG |
209 | if (unlikely(error)) |
210 | goto out_free_acl; | |
211 | ||
d540e43b | 212 | if (!tmpfile) { |
dfaf8842 DW |
213 | if (xfs_create_need_xattr(dir, default_acl, acl)) |
214 | args.flags |= XFS_ICREATE_INIT_XATTRS; | |
215 | ||
216 | error = xfs_create(&args, &name, &ip); | |
d540e43b | 217 | } else { |
dfaf8842 | 218 | args.flags |= XFS_ICREATE_TMPFILE; |
b7c62d90 AH |
219 | |
220 | /* | |
b11b11e3 DW |
221 | * If this temporary file will not be linkable, don't bother |
222 | * creating an attr fork to receive a parent pointer. | |
b7c62d90 | 223 | */ |
b11b11e3 DW |
224 | if (tmpfile->f_flags & O_EXCL) |
225 | args.flags |= XFS_ICREATE_UNLINKABLE; | |
b7c62d90 | 226 | |
dfaf8842 | 227 | error = xfs_create_tmpfile(&args, &ip); |
d540e43b | 228 | } |
db0bb7ba CH |
229 | if (unlikely(error)) |
230 | goto out_free_acl; | |
446ada4a | 231 | |
01651646 | 232 | inode = VFS_I(ip); |
979ebab1 | 233 | |
70b589a3 | 234 | error = xfs_inode_init_security(inode, dir, &dentry->d_name); |
db0bb7ba CH |
235 | if (unlikely(error)) |
236 | goto out_cleanup_inode; | |
237 | ||
238 | if (default_acl) { | |
8ba35875 | 239 | error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); |
2401dc29 | 240 | if (error) |
db0bb7ba | 241 | goto out_cleanup_inode; |
1da177e4 | 242 | } |
2401dc29 | 243 | if (acl) { |
8ba35875 | 244 | error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS); |
2401dc29 CH |
245 | if (error) |
246 | goto out_cleanup_inode; | |
247 | } | |
1da177e4 | 248 | |
2b3d1d41 CH |
249 | xfs_setup_iops(ip); |
250 | ||
c4a6bf7f DW |
251 | if (tmpfile) { |
252 | /* | |
253 | * The VFS requires that any inode fed to d_tmpfile must have | |
254 | * nlink == 1 so that it can decrement the nlink in d_tmpfile. | |
255 | * However, we created the temp file with nlink == 0 because | |
256 | * we're not allowed to put an inode with nlink > 0 on the | |
257 | * unlinked list. Therefore we have to set nlink to 1 so that | |
258 | * d_tmpfile can immediately set it back to zero. | |
259 | */ | |
260 | set_nlink(inode, 1); | |
863f144f | 261 | d_tmpfile(tmpfile, inode); |
c4a6bf7f | 262 | } else |
d540e43b BF |
263 | d_instantiate(dentry, inode); |
264 | ||
58c90473 DC |
265 | xfs_finish_inode_setup(ip); |
266 | ||
2401dc29 | 267 | out_free_acl: |
88269b88 KX |
268 | posix_acl_release(default_acl); |
269 | posix_acl_release(acl); | |
2451337d | 270 | return error; |
db0bb7ba CH |
271 | |
272 | out_cleanup_inode: | |
58c90473 | 273 | xfs_finish_inode_setup(ip); |
d540e43b BF |
274 | if (!tmpfile) |
275 | xfs_cleanup_inode(dir, inode, dentry); | |
44a8736b | 276 | xfs_irele(ip); |
2401dc29 | 277 | goto out_free_acl; |
1da177e4 LT |
278 | } |
279 | ||
d540e43b BF |
280 | STATIC int |
281 | xfs_vn_mknod( | |
5ebb29be | 282 | struct mnt_idmap *idmap, |
549c7297 CB |
283 | struct inode *dir, |
284 | struct dentry *dentry, | |
285 | umode_t mode, | |
286 | dev_t rdev) | |
d540e43b | 287 | { |
f2d40141 | 288 | return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL); |
d540e43b BF |
289 | } |
290 | ||
1da177e4 | 291 | STATIC int |
416c6d5b | 292 | xfs_vn_create( |
6c960e68 | 293 | struct mnt_idmap *idmap, |
549c7297 CB |
294 | struct inode *dir, |
295 | struct dentry *dentry, | |
296 | umode_t mode, | |
297 | bool flags) | |
1da177e4 | 298 | { |
f2d40141 | 299 | return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL); |
1da177e4 LT |
300 | } |
301 | ||
88d5baf6 | 302 | STATIC struct dentry * |
416c6d5b | 303 | xfs_vn_mkdir( |
c54bd91e | 304 | struct mnt_idmap *idmap, |
549c7297 CB |
305 | struct inode *dir, |
306 | struct dentry *dentry, | |
307 | umode_t mode) | |
1da177e4 | 308 | { |
88d5baf6 | 309 | return ERR_PTR(xfs_generic_create(idmap, dir, dentry, mode | S_IFDIR, 0, NULL)); |
1da177e4 LT |
310 | } |
311 | ||
312 | STATIC struct dentry * | |
416c6d5b | 313 | xfs_vn_lookup( |
1da177e4 LT |
314 | struct inode *dir, |
315 | struct dentry *dentry, | |
00cd8dd3 | 316 | unsigned int flags) |
1da177e4 | 317 | { |
b113a6d3 | 318 | struct inode *inode; |
ef1f5e7a | 319 | struct xfs_inode *cip; |
556b8b16 | 320 | struct xfs_name name; |
1da177e4 LT |
321 | int error; |
322 | ||
323 | if (dentry->d_name.len >= MAXNAMELEN) | |
324 | return ERR_PTR(-ENAMETOOLONG); | |
325 | ||
fab8eef8 | 326 | xfs_dentry_to_name(&name, dentry); |
384f3ced | 327 | error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); |
b113a6d3 AV |
328 | if (likely(!error)) |
329 | inode = VFS_I(cip); | |
330 | else if (likely(error == -ENOENT)) | |
331 | inode = NULL; | |
332 | else | |
333 | inode = ERR_PTR(error); | |
334 | return d_splice_alias(inode, dentry); | |
1da177e4 LT |
335 | } |
336 | ||
384f3ced BN |
337 | STATIC struct dentry * |
338 | xfs_vn_ci_lookup( | |
339 | struct inode *dir, | |
340 | struct dentry *dentry, | |
00cd8dd3 | 341 | unsigned int flags) |
384f3ced BN |
342 | { |
343 | struct xfs_inode *ip; | |
344 | struct xfs_name xname; | |
345 | struct xfs_name ci_name; | |
346 | struct qstr dname; | |
347 | int error; | |
348 | ||
349 | if (dentry->d_name.len >= MAXNAMELEN) | |
350 | return ERR_PTR(-ENAMETOOLONG); | |
351 | ||
fab8eef8 | 352 | xfs_dentry_to_name(&xname, dentry); |
384f3ced BN |
353 | error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name); |
354 | if (unlikely(error)) { | |
2451337d DC |
355 | if (unlikely(error != -ENOENT)) |
356 | return ERR_PTR(error); | |
866d5dc9 BN |
357 | /* |
358 | * call d_add(dentry, NULL) here when d_drop_negative_children | |
359 | * is called in xfs_vn_mknod (ie. allow negative dentries | |
360 | * with CI filesystems). | |
361 | */ | |
384f3ced BN |
362 | return NULL; |
363 | } | |
364 | ||
365 | /* if exact match, just splice and exit */ | |
366 | if (!ci_name.name) | |
01651646 | 367 | return d_splice_alias(VFS_I(ip), dentry); |
384f3ced BN |
368 | |
369 | /* else case-insensitive match... */ | |
370 | dname.name = ci_name.name; | |
371 | dname.len = ci_name.len; | |
e45b590b | 372 | dentry = d_add_ci(dentry, VFS_I(ip), &dname); |
d4c75a1b | 373 | kfree(ci_name.name); |
384f3ced BN |
374 | return dentry; |
375 | } | |
376 | ||
1da177e4 | 377 | STATIC int |
416c6d5b | 378 | xfs_vn_link( |
1da177e4 LT |
379 | struct dentry *old_dentry, |
380 | struct inode *dir, | |
381 | struct dentry *dentry) | |
382 | { | |
2b0143b5 | 383 | struct inode *inode = d_inode(old_dentry); |
556b8b16 | 384 | struct xfs_name name; |
1da177e4 LT |
385 | int error; |
386 | ||
fab8eef8 AG |
387 | error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode); |
388 | if (unlikely(error)) | |
389 | return error; | |
1da177e4 | 390 | |
cab23a42 DW |
391 | if (IS_PRIVATE(inode)) |
392 | return -EPERM; | |
393 | ||
556b8b16 | 394 | error = xfs_link(XFS_I(dir), XFS_I(inode), &name); |
d9424b3c | 395 | if (unlikely(error)) |
2451337d | 396 | return error; |
a3da7896 | 397 | |
7de9c6ee | 398 | ihold(inode); |
a3da7896 CH |
399 | d_instantiate(dentry, inode); |
400 | return 0; | |
1da177e4 LT |
401 | } |
402 | ||
403 | STATIC int | |
416c6d5b | 404 | xfs_vn_unlink( |
1da177e4 LT |
405 | struct inode *dir, |
406 | struct dentry *dentry) | |
407 | { | |
556b8b16 | 408 | struct xfs_name name; |
1da177e4 LT |
409 | int error; |
410 | ||
fab8eef8 | 411 | xfs_dentry_to_name(&name, dentry); |
1da177e4 | 412 | |
2b0143b5 | 413 | error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry))); |
e5700704 CH |
414 | if (error) |
415 | return error; | |
416 | ||
417 | /* | |
418 | * With unlink, the VFS makes the dentry "negative": no inode, | |
419 | * but still hashed. This is incompatible with case-insensitive | |
420 | * mode, so invalidate (unhash) the dentry in CI-mode. | |
421 | */ | |
38c26bfd | 422 | if (xfs_has_asciici(XFS_M(dir->i_sb))) |
e5700704 CH |
423 | d_invalidate(dentry); |
424 | return 0; | |
1da177e4 LT |
425 | } |
426 | ||
427 | STATIC int | |
416c6d5b | 428 | xfs_vn_symlink( |
7a77db95 | 429 | struct mnt_idmap *idmap, |
549c7297 CB |
430 | struct inode *dir, |
431 | struct dentry *dentry, | |
432 | const char *symname) | |
1da177e4 | 433 | { |
3937be5b CH |
434 | struct inode *inode; |
435 | struct xfs_inode *cip = NULL; | |
556b8b16 | 436 | struct xfs_name name; |
1da177e4 | 437 | int error; |
576b1d67 | 438 | umode_t mode; |
1da177e4 | 439 | |
3e5daf05 | 440 | mode = S_IFLNK | |
ce3b0f8d | 441 | (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO); |
fab8eef8 AG |
442 | error = xfs_dentry_mode_to_name(&name, dentry, mode); |
443 | if (unlikely(error)) | |
444 | goto out; | |
1da177e4 | 445 | |
f2d40141 | 446 | error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip); |
3937be5b CH |
447 | if (unlikely(error)) |
448 | goto out; | |
449 | ||
01651646 | 450 | inode = VFS_I(cip); |
3937be5b | 451 | |
70b589a3 | 452 | error = xfs_inode_init_security(inode, dir, &dentry->d_name); |
3937be5b CH |
453 | if (unlikely(error)) |
454 | goto out_cleanup_inode; | |
455 | ||
2b3d1d41 CH |
456 | xfs_setup_iops(cip); |
457 | ||
3937be5b | 458 | d_instantiate(dentry, inode); |
58c90473 | 459 | xfs_finish_inode_setup(cip); |
3937be5b CH |
460 | return 0; |
461 | ||
462 | out_cleanup_inode: | |
58c90473 | 463 | xfs_finish_inode_setup(cip); |
8f112e3b | 464 | xfs_cleanup_inode(dir, inode, dentry); |
44a8736b | 465 | xfs_irele(cip); |
3937be5b | 466 | out: |
2451337d | 467 | return error; |
1da177e4 LT |
468 | } |
469 | ||
1da177e4 | 470 | STATIC int |
416c6d5b | 471 | xfs_vn_rename( |
e18275ae | 472 | struct mnt_idmap *idmap, |
549c7297 CB |
473 | struct inode *odir, |
474 | struct dentry *odentry, | |
475 | struct inode *ndir, | |
476 | struct dentry *ndentry, | |
477 | unsigned int flags) | |
1da177e4 | 478 | { |
2b0143b5 | 479 | struct inode *new_inode = d_inode(ndentry); |
d31a1825 | 480 | int omode = 0; |
fab8eef8 | 481 | int error; |
556b8b16 BN |
482 | struct xfs_name oname; |
483 | struct xfs_name nname; | |
1da177e4 | 484 | |
7dcf5c3e | 485 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
dbe1b5ca CM |
486 | return -EINVAL; |
487 | ||
d31a1825 CM |
488 | /* if we are exchanging files, we need to set i_mode of both files */ |
489 | if (flags & RENAME_EXCHANGE) | |
2b0143b5 | 490 | omode = d_inode(ndentry)->i_mode; |
d31a1825 | 491 | |
fab8eef8 AG |
492 | error = xfs_dentry_mode_to_name(&oname, odentry, omode); |
493 | if (omode && unlikely(error)) | |
494 | return error; | |
495 | ||
496 | error = xfs_dentry_mode_to_name(&nname, ndentry, | |
497 | d_inode(odentry)->i_mode); | |
498 | if (unlikely(error)) | |
499 | return error; | |
556b8b16 | 500 | |
f2d40141 | 501 | return xfs_rename(idmap, XFS_I(odir), &oname, |
f736d93d | 502 | XFS_I(d_inode(odentry)), XFS_I(ndir), &nname, |
d31a1825 | 503 | new_inode ? XFS_I(new_inode) : NULL, flags); |
1da177e4 LT |
504 | } |
505 | ||
506 | /* | |
507 | * careful here - this function can get called recursively, so | |
508 | * we need to be very careful about how much stack we use. | |
509 | * uio is kmalloced for this reason... | |
510 | */ | |
680baacb | 511 | STATIC const char * |
6b255391 | 512 | xfs_vn_get_link( |
1da177e4 | 513 | struct dentry *dentry, |
6b255391 | 514 | struct inode *inode, |
fceef393 | 515 | struct delayed_call *done) |
1da177e4 | 516 | { |
1da177e4 | 517 | char *link; |
804c83c3 | 518 | int error = -ENOMEM; |
1da177e4 | 519 | |
6b255391 AV |
520 | if (!dentry) |
521 | return ERR_PTR(-ECHILD); | |
522 | ||
6eb0b8df | 523 | link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL); |
804c83c3 CH |
524 | if (!link) |
525 | goto out_err; | |
1da177e4 | 526 | |
2b0143b5 | 527 | error = xfs_readlink(XFS_I(d_inode(dentry)), link); |
804c83c3 CH |
528 | if (unlikely(error)) |
529 | goto out_kfree; | |
1da177e4 | 530 | |
fceef393 AV |
531 | set_delayed_call(done, kfree_link, link); |
532 | return link; | |
804c83c3 CH |
533 | |
534 | out_kfree: | |
535 | kfree(link); | |
536 | out_err: | |
680baacb | 537 | return ERR_PTR(error); |
1da177e4 LT |
538 | } |
539 | ||
dd2d535e CH |
540 | static uint32_t |
541 | xfs_stat_blksize( | |
542 | struct xfs_inode *ip) | |
543 | { | |
544 | struct xfs_mount *mp = ip->i_mount; | |
545 | ||
546 | /* | |
547 | * If the file blocks are being allocated from a realtime volume, then | |
548 | * always return the realtime extent size. | |
549 | */ | |
550 | if (XFS_IS_REALTIME_INODE(ip)) | |
6a94b1ac | 551 | return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip) ? : 1); |
dd2d535e CH |
552 | |
553 | /* | |
554 | * Allow large block sizes to be reported to userspace programs if the | |
555 | * "largeio" mount option is used. | |
556 | * | |
557 | * If compatibility mode is specified, simply return the basic unit of | |
558 | * caching so that we don't get inefficient read/modify/write I/O from | |
559 | * user apps. Otherwise.... | |
560 | * | |
561 | * If the underlying volume is a stripe, then return the stripe width in | |
562 | * bytes as the recommended I/O size. It is not a stripe and we've set a | |
563 | * default buffered I/O size, return that, otherwise return the compat | |
564 | * default. | |
565 | */ | |
0560f31a | 566 | if (xfs_has_large_iosize(mp)) { |
dd2d535e | 567 | if (mp->m_swidth) |
a7bcb147 | 568 | return XFS_FSB_TO_B(mp, mp->m_swidth); |
0560f31a | 569 | if (xfs_has_allocsize(mp)) |
5da8a07c | 570 | return 1U << mp->m_allocsize_log; |
dd2d535e CH |
571 | } |
572 | ||
79012cfa | 573 | return max_t(uint32_t, PAGE_SIZE, mp->m_sb.sb_blocksize); |
dd2d535e CH |
574 | } |
575 | ||
6432c6e7 | 576 | static void |
7e17483c | 577 | xfs_report_dioalign( |
6432c6e7 | 578 | struct xfs_inode *ip, |
7e17483c | 579 | struct kstat *stat) |
6432c6e7 | 580 | { |
7e17483c CH |
581 | struct xfs_buftarg *target = xfs_inode_buftarg(ip); |
582 | struct block_device *bdev = target->bt_bdev; | |
6432c6e7 | 583 | |
7422bbd0 | 584 | stat->result_mask |= STATX_DIOALIGN | STATX_DIO_READ_ALIGN; |
7e17483c | 585 | stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; |
7422bbd0 CH |
586 | |
587 | /* | |
588 | * For COW inodes, we can only perform out of place writes of entire | |
589 | * allocation units (blocks or RT extents). | |
590 | * For writes smaller than the allocation unit, we must fall back to | |
591 | * buffered I/O to perform read-modify-write cycles. At best this is | |
592 | * highly inefficient; at worst it leads to page cache invalidation | |
593 | * races. Tell applications to avoid this by reporting the larger write | |
594 | * alignment in dio_offset_align, and the smaller read alignment in | |
595 | * dio_read_offset_align. | |
596 | */ | |
597 | stat->dio_read_offset_align = bdev_logical_block_size(bdev); | |
598 | if (xfs_is_cow_inode(ip)) | |
599 | stat->dio_offset_align = xfs_inode_alloc_unitsize(ip); | |
600 | else | |
601 | stat->dio_offset_align = stat->dio_read_offset_align; | |
7e17483c CH |
602 | } |
603 | ||
0ea88ed4 JG |
604 | unsigned int |
605 | xfs_get_atomic_write_min( | |
606 | struct xfs_inode *ip) | |
607 | { | |
9dffc58f JG |
608 | struct xfs_mount *mp = ip->i_mount; |
609 | ||
610 | /* | |
611 | * If we can complete an atomic write via atomic out of place writes, | |
612 | * then advertise a minimum size of one fsblock. Without this | |
613 | * mechanism, we can only guarantee atomic writes up to a single LBA. | |
614 | * | |
615 | * If out of place writes are not available, we can guarantee an atomic | |
616 | * write of exactly one single fsblock if the bdev will make that | |
617 | * guarantee for us. | |
618 | */ | |
619 | if (xfs_inode_can_hw_atomic_write(ip) || xfs_can_sw_atomic_write(mp)) | |
620 | return mp->m_sb.sb_blocksize; | |
0ea88ed4 | 621 | |
9dffc58f | 622 | return 0; |
0ea88ed4 JG |
623 | } |
624 | ||
625 | unsigned int | |
626 | xfs_get_atomic_write_max( | |
627 | struct xfs_inode *ip) | |
628 | { | |
9dffc58f JG |
629 | struct xfs_mount *mp = ip->i_mount; |
630 | ||
631 | /* | |
632 | * If out of place writes are not available, we can guarantee an atomic | |
633 | * write of exactly one single fsblock if the bdev will make that | |
634 | * guarantee for us. | |
635 | */ | |
636 | if (!xfs_can_sw_atomic_write(mp)) { | |
637 | if (xfs_inode_can_hw_atomic_write(ip)) | |
638 | return mp->m_sb.sb_blocksize; | |
0ea88ed4 | 639 | return 0; |
9dffc58f | 640 | } |
0ea88ed4 | 641 | |
9dffc58f JG |
642 | /* |
643 | * If we can complete an atomic write via atomic out of place writes, | |
644 | * then advertise a maximum size of whatever we can complete through | |
645 | * that means. Hardware support is reported via max_opt, not here. | |
646 | */ | |
647 | if (XFS_IS_REALTIME_INODE(ip)) | |
648 | return XFS_FSB_TO_B(mp, mp->m_groups[XG_TYPE_RTG].awu_max); | |
649 | return XFS_FSB_TO_B(mp, mp->m_groups[XG_TYPE_AG].awu_max); | |
0ea88ed4 JG |
650 | } |
651 | ||
652 | unsigned int | |
653 | xfs_get_atomic_write_max_opt( | |
654 | struct xfs_inode *ip) | |
655 | { | |
9dffc58f JG |
656 | unsigned int awu_max = xfs_get_atomic_write_max(ip); |
657 | ||
658 | /* if the max is 1x block, then just keep behaviour that opt is 0 */ | |
659 | if (awu_max <= ip->i_mount->m_sb.sb_blocksize) | |
660 | return 0; | |
661 | ||
662 | /* | |
663 | * Advertise the maximum size of an atomic write that we can tell the | |
664 | * block device to perform for us. In general the bdev limit will be | |
665 | * less than our out of place write limit, but we don't want to exceed | |
666 | * the awu_max. | |
667 | */ | |
668 | return min(awu_max, xfs_inode_buftarg(ip)->bt_bdev_awu_max); | |
0ea88ed4 JG |
669 | } |
670 | ||
7e17483c CH |
671 | static void |
672 | xfs_report_atomic_write( | |
673 | struct xfs_inode *ip, | |
674 | struct kstat *stat) | |
675 | { | |
0ea88ed4 JG |
676 | generic_fill_statx_atomic_writes(stat, |
677 | xfs_get_atomic_write_min(ip), | |
678 | xfs_get_atomic_write_max(ip), | |
679 | xfs_get_atomic_write_max_opt(ip)); | |
6432c6e7 JG |
680 | } |
681 | ||
1da177e4 | 682 | STATIC int |
416c6d5b | 683 | xfs_vn_getattr( |
b74d24f7 | 684 | struct mnt_idmap *idmap, |
a528d35e DH |
685 | const struct path *path, |
686 | struct kstat *stat, | |
687 | u32 request_mask, | |
688 | unsigned int query_flags) | |
1da177e4 | 689 | { |
a528d35e | 690 | struct inode *inode = d_inode(path->dentry); |
c43f4087 CH |
691 | struct xfs_inode *ip = XFS_I(inode); |
692 | struct xfs_mount *mp = ip->i_mount; | |
e67fe633 CB |
693 | vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); |
694 | vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); | |
c43f4087 | 695 | |
cca28fb8 | 696 | trace_xfs_getattr(ip); |
c43f4087 | 697 | |
75c8c50f | 698 | if (xfs_is_shutdown(mp)) |
b474c7ae | 699 | return -EIO; |
c43f4087 CH |
700 | |
701 | stat->size = XFS_ISIZE(ip); | |
702 | stat->dev = inode->i_sb->s_dev; | |
c19b3b05 | 703 | stat->mode = inode->i_mode; |
54d7b5c1 | 704 | stat->nlink = inode->i_nlink; |
42b7cc11 CB |
705 | stat->uid = vfsuid_into_kuid(vfsuid); |
706 | stat->gid = vfsgid_into_kgid(vfsgid); | |
c43f4087 | 707 | stat->ino = ip->i_ino; |
75d1e312 | 708 | stat->atime = inode_get_atime(inode); |
1cf7e834 JL |
709 | |
710 | fill_mg_cmtime(stat, request_mask, inode); | |
711 | ||
6e73a545 | 712 | stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks); |
c43f4087 | 713 | |
38c26bfd | 714 | if (xfs_has_v3inodes(mp)) { |
5f955f26 DW |
715 | if (request_mask & STATX_BTIME) { |
716 | stat->result_mask |= STATX_BTIME; | |
e98d5e88 | 717 | stat->btime = ip->i_crtime; |
5f955f26 DW |
718 | } |
719 | } | |
720 | ||
1b9598c8 LR |
721 | /* |
722 | * Note: If you add another clause to set an attribute flag, please | |
723 | * update attributes_mask below. | |
724 | */ | |
db07349d | 725 | if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE) |
5f955f26 | 726 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
db07349d | 727 | if (ip->i_diflags & XFS_DIFLAG_APPEND) |
5f955f26 | 728 | stat->attributes |= STATX_ATTR_APPEND; |
db07349d | 729 | if (ip->i_diflags & XFS_DIFLAG_NODUMP) |
5f955f26 | 730 | stat->attributes |= STATX_ATTR_NODUMP; |
c43f4087 | 731 | |
1b9598c8 LR |
732 | stat->attributes_mask |= (STATX_ATTR_IMMUTABLE | |
733 | STATX_ATTR_APPEND | | |
734 | STATX_ATTR_NODUMP); | |
735 | ||
c43f4087 CH |
736 | switch (inode->i_mode & S_IFMT) { |
737 | case S_IFBLK: | |
738 | case S_IFCHR: | |
739 | stat->blksize = BLKDEV_IOSIZE; | |
66f36464 | 740 | stat->rdev = inode->i_rdev; |
c43f4087 | 741 | break; |
61a223df | 742 | case S_IFREG: |
7422bbd0 | 743 | if (request_mask & (STATX_DIOALIGN | STATX_DIO_READ_ALIGN)) |
7e17483c CH |
744 | xfs_report_dioalign(ip, stat); |
745 | if (request_mask & STATX_WRITE_ATOMIC) | |
746 | xfs_report_atomic_write(ip, stat); | |
61a223df | 747 | fallthrough; |
c43f4087 | 748 | default: |
dd2d535e | 749 | stat->blksize = xfs_stat_blksize(ip); |
c43f4087 CH |
750 | stat->rdev = 0; |
751 | break; | |
69e23b9a | 752 | } |
c43f4087 CH |
753 | |
754 | return 0; | |
1da177e4 LT |
755 | } |
756 | ||
69bca807 JK |
757 | static int |
758 | xfs_vn_change_ok( | |
c1632a0f | 759 | struct mnt_idmap *idmap, |
f736d93d CH |
760 | struct dentry *dentry, |
761 | struct iattr *iattr) | |
69bca807 | 762 | { |
31051c85 | 763 | struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount; |
69bca807 | 764 | |
2e973b2c | 765 | if (xfs_is_readonly(mp)) |
69bca807 JK |
766 | return -EROFS; |
767 | ||
75c8c50f | 768 | if (xfs_is_shutdown(mp)) |
69bca807 JK |
769 | return -EIO; |
770 | ||
c1632a0f | 771 | return setattr_prepare(idmap, dentry, iattr); |
69bca807 JK |
772 | } |
773 | ||
774 | /* | |
775 | * Set non-size attributes of an inode. | |
776 | * | |
777 | * Caution: The caller of this function is responsible for calling | |
31051c85 | 778 | * setattr_prepare() or otherwise verifying the change is fine. |
69bca807 | 779 | */ |
5d24ec4c | 780 | static int |
c4ed4243 | 781 | xfs_setattr_nonsize( |
c1632a0f | 782 | struct mnt_idmap *idmap, |
138060ba | 783 | struct dentry *dentry, |
c4ed4243 | 784 | struct xfs_inode *ip, |
5d24ec4c | 785 | struct iattr *iattr) |
c4ed4243 CH |
786 | { |
787 | xfs_mount_t *mp = ip->i_mount; | |
788 | struct inode *inode = VFS_I(ip); | |
789 | int mask = iattr->ia_valid; | |
790 | xfs_trans_t *tp; | |
791 | int error; | |
dd3b015d DW |
792 | kuid_t uid = GLOBAL_ROOT_UID; |
793 | kgid_t gid = GLOBAL_ROOT_GID; | |
c4ed4243 | 794 | struct xfs_dquot *udqp = NULL, *gdqp = NULL; |
dd3b015d | 795 | struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL; |
c4ed4243 | 796 | |
c4ed4243 CH |
797 | ASSERT((mask & ATTR_SIZE) == 0); |
798 | ||
799 | /* | |
800 | * If disk quotas is on, we make sure that the dquots do exist on disk, | |
801 | * before we start any other transactions. Trying to do this later | |
802 | * is messy. We don't care to take a readlock to look at the ids | |
803 | * in inode here, because we can't hold it across the trans_reserve. | |
804 | * If the IDs do change before we take the ilock, we're covered | |
805 | * because the i_*dquot fields will get updated anyway. | |
806 | */ | |
807 | if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) { | |
808 | uint qflags = 0; | |
809 | ||
810 | if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) { | |
4d7ca409 | 811 | uid = from_vfsuid(idmap, i_user_ns(inode), |
b27c82e1 | 812 | iattr->ia_vfsuid); |
c4ed4243 CH |
813 | qflags |= XFS_QMOPT_UQUOTA; |
814 | } else { | |
7aab1b28 | 815 | uid = inode->i_uid; |
c4ed4243 CH |
816 | } |
817 | if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) { | |
4d7ca409 | 818 | gid = from_vfsgid(idmap, i_user_ns(inode), |
b27c82e1 | 819 | iattr->ia_vfsgid); |
c4ed4243 CH |
820 | qflags |= XFS_QMOPT_GQUOTA; |
821 | } else { | |
7aab1b28 | 822 | gid = inode->i_gid; |
c4ed4243 CH |
823 | } |
824 | ||
825 | /* | |
826 | * We take a reference when we initialize udqp and gdqp, | |
827 | * so it is important that we never blindly double trip on | |
828 | * the same variable. See xfs_create() for an example. | |
829 | */ | |
830 | ASSERT(udqp == NULL); | |
831 | ASSERT(gdqp == NULL); | |
ceaf603c | 832 | error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid, |
7aab1b28 | 833 | qflags, &udqp, &gdqp, NULL); |
c4ed4243 CH |
834 | if (error) |
835 | return error; | |
836 | } | |
837 | ||
7317a03d | 838 | error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL, |
eba0549b | 839 | has_capability_noaudit(current, CAP_FOWNER), &tp); |
c4ed4243 | 840 | if (error) |
253f4911 | 841 | goto out_dqrele; |
c4ed4243 | 842 | |
c4ed4243 | 843 | /* |
dd3b015d DW |
844 | * Register quota modifications in the transaction. Must be the owner |
845 | * or privileged. These IDs could have changed since we last looked at | |
846 | * them. But, we're assured that if the ownership did change while we | |
847 | * didn't have the inode locked, inode's dquot(s) would have changed | |
848 | * also. | |
c4ed4243 | 849 | */ |
35faf310 | 850 | if (XFS_IS_UQUOTA_ON(mp) && |
0dbe12f2 | 851 | i_uid_needs_update(idmap, iattr, inode)) { |
dd3b015d DW |
852 | ASSERT(udqp); |
853 | old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp); | |
854 | } | |
35faf310 | 855 | if (XFS_IS_GQUOTA_ON(mp) && |
0dbe12f2 | 856 | i_gid_needs_update(idmap, iattr, inode)) { |
dd3b015d DW |
857 | ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp)); |
858 | ASSERT(gdqp); | |
859 | old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp); | |
c4ed4243 CH |
860 | } |
861 | ||
c1632a0f | 862 | setattr_copy(idmap, inode, iattr); |
c4ed4243 CH |
863 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
864 | ||
ff6d6af2 | 865 | XFS_STATS_INC(mp, xs_ig_attrchg); |
c4ed4243 | 866 | |
0560f31a | 867 | if (xfs_has_wsync(mp)) |
c4ed4243 | 868 | xfs_trans_set_sync(tp); |
70393313 | 869 | error = xfs_trans_commit(tp); |
c4ed4243 | 870 | |
c4ed4243 CH |
871 | /* |
872 | * Release any dquot(s) the inode had kept before chown. | |
873 | */ | |
dd3b015d DW |
874 | xfs_qm_dqrele(old_udqp); |
875 | xfs_qm_dqrele(old_gdqp); | |
c4ed4243 CH |
876 | xfs_qm_dqrele(udqp); |
877 | xfs_qm_dqrele(gdqp); | |
878 | ||
879 | if (error) | |
b474c7ae | 880 | return error; |
c4ed4243 CH |
881 | |
882 | /* | |
883 | * XXX(hch): Updating the ACL entries is not atomic vs the i_mode | |
884 | * update. We could avoid this with linked transactions | |
885 | * and passing down the transaction pointer all the way | |
886 | * to attr_set. No previous user of the generic | |
887 | * Posix ACL code seems to care about this issue either. | |
888 | */ | |
5d24ec4c | 889 | if (mask & ATTR_MODE) { |
13e83a49 | 890 | error = posix_acl_chmod(idmap, dentry, inode->i_mode); |
c4ed4243 | 891 | if (error) |
b474c7ae | 892 | return error; |
c4ed4243 CH |
893 | } |
894 | ||
895 | return 0; | |
896 | ||
253f4911 | 897 | out_dqrele: |
c4ed4243 CH |
898 | xfs_qm_dqrele(udqp); |
899 | xfs_qm_dqrele(gdqp); | |
900 | return error; | |
901 | } | |
902 | ||
903 | /* | |
904 | * Truncate file. Must have write permission and not be a directory. | |
69bca807 JK |
905 | * |
906 | * Caution: The caller of this function is responsible for calling | |
31051c85 | 907 | * setattr_prepare() or otherwise verifying the change is fine. |
c4ed4243 | 908 | */ |
7bf7a193 | 909 | STATIC int |
c4ed4243 | 910 | xfs_setattr_size( |
c1632a0f | 911 | struct mnt_idmap *idmap, |
138060ba | 912 | struct dentry *dentry, |
c4ed4243 | 913 | struct xfs_inode *ip, |
76ca4c23 | 914 | struct iattr *iattr) |
c4ed4243 CH |
915 | { |
916 | struct xfs_mount *mp = ip->i_mount; | |
917 | struct inode *inode = VFS_I(ip); | |
673e8e59 | 918 | xfs_off_t oldsize, newsize; |
c4ed4243 CH |
919 | struct xfs_trans *tp; |
920 | int error; | |
f38996f5 | 921 | uint lock_flags = 0; |
d0489451 | 922 | uint resblks = 0; |
5885ebda | 923 | bool did_zeroing = false; |
058dd70c | 924 | struct xfs_zone_alloc_ctx ac = { }; |
c4ed4243 | 925 | |
3fed24ff | 926 | xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL); |
c19b3b05 | 927 | ASSERT(S_ISREG(inode->i_mode)); |
fe60a8a0 | 928 | ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET| |
88a9e03b | 929 | ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0); |
c4ed4243 | 930 | |
ce7ae151 | 931 | oldsize = inode->i_size; |
673e8e59 CH |
932 | newsize = iattr->ia_size; |
933 | ||
c4ed4243 CH |
934 | /* |
935 | * Short circuit the truncate case for zero length files. | |
936 | */ | |
daf83964 | 937 | if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) { |
fe60a8a0 | 938 | if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME))) |
76ca4c23 | 939 | return 0; |
681b1200 CH |
940 | |
941 | /* | |
942 | * Use the regular setattr path to update the timestamps. | |
943 | */ | |
681b1200 | 944 | iattr->ia_valid &= ~ATTR_SIZE; |
c1632a0f | 945 | return xfs_setattr_nonsize(idmap, dentry, ip, iattr); |
c4ed4243 CH |
946 | } |
947 | ||
948 | /* | |
949 | * Make sure that the dquots are attached to the inode. | |
950 | */ | |
c14cfcca | 951 | error = xfs_qm_dqattach(ip); |
c4ed4243 | 952 | if (error) |
76ca4c23 | 953 | return error; |
c4ed4243 | 954 | |
f0c6bcba CH |
955 | /* |
956 | * Wait for all direct I/O to complete. | |
957 | */ | |
958 | inode_dio_wait(inode); | |
959 | ||
058dd70c CH |
960 | /* |
961 | * Normally xfs_zoned_space_reserve is supposed to be called outside the | |
962 | * IOLOCK. For truncate we can't do that since ->setattr is called with | |
963 | * it already held by the VFS. So for now chicken out and try to | |
964 | * allocate space under it. | |
965 | * | |
966 | * To avoid deadlocks this means we can't block waiting for space, which | |
967 | * can lead to spurious -ENOSPC if there are no directly available | |
968 | * blocks. We mitigate this a bit by allowing zeroing to dip into the | |
969 | * reserved pool, but eventually the VFS calling convention needs to | |
970 | * change. | |
971 | */ | |
972 | if (xfs_is_zoned_inode(ip)) { | |
973 | error = xfs_zoned_space_reserve(ip, 1, | |
974 | XFS_ZR_NOWAIT | XFS_ZR_RESERVED, &ac); | |
975 | if (error) { | |
976 | if (error == -EAGAIN) | |
977 | return -ENOSPC; | |
978 | return error; | |
979 | } | |
980 | } | |
981 | ||
c4ed4243 | 982 | /* |
5885ebda DC |
983 | * File data changes must be complete before we start the transaction to |
984 | * modify the inode. This needs to be done before joining the inode to | |
985 | * the transaction because the inode cannot be unlocked once it is a | |
986 | * part of the transaction. | |
987 | * | |
f0c6bcba CH |
988 | * Start with zeroing any data beyond EOF that we may expose on file |
989 | * extension, or zeroing out the rest of the block on a downward | |
990 | * truncate. | |
c4ed4243 | 991 | */ |
673e8e59 | 992 | if (newsize > oldsize) { |
f5c54717 | 993 | trace_xfs_zero_eof(ip, oldsize, newsize - oldsize); |
f1ba5faf | 994 | error = xfs_zero_range(ip, oldsize, newsize - oldsize, |
058dd70c | 995 | &ac, &did_zeroing); |
f0c6bcba | 996 | } else { |
058dd70c | 997 | error = xfs_truncate_page(ip, newsize, &ac, &did_zeroing); |
c4ed4243 | 998 | } |
c4ed4243 | 999 | |
058dd70c CH |
1000 | if (xfs_is_zoned_inode(ip)) |
1001 | xfs_zoned_space_unreserve(ip, &ac); | |
1002 | ||
f0c6bcba CH |
1003 | if (error) |
1004 | return error; | |
1005 | ||
49abc3a8 | 1006 | /* |
0f9160b4 DC |
1007 | * We've already locked out new page faults, so now we can safely remove |
1008 | * pages from the page cache knowing they won't get refaulted until we | |
1009 | * drop the XFS_MMAP_EXCL lock after the extent manipulations are | |
1010 | * complete. The truncate_setsize() call also cleans partial EOF page | |
1011 | * PTEs on extending truncates and hence ensures sub-page block size | |
1012 | * filesystems are correctly handled, too. | |
49abc3a8 | 1013 | * |
0f9160b4 DC |
1014 | * We have to do all the page cache truncate work outside the |
1015 | * transaction context as the "lock" order is page lock->log space | |
1016 | * reservation as defined by extent allocation in the writeback path. | |
253f4911 | 1017 | * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but |
0f9160b4 DC |
1018 | * having already truncated the in-memory version of the file (i.e. made |
1019 | * user visible changes). There's not much we can do about this, except | |
1020 | * to hope that the caller sees ENOMEM and retries the truncate | |
1021 | * operation. | |
350976ae EG |
1022 | * |
1023 | * And we update in-core i_size and truncate page cache beyond newsize | |
13d2c10b CH |
1024 | * before writeback the [i_disk_size, newsize] range, so we're |
1025 | * guaranteed not to write stale data past the new EOF on truncate down. | |
49abc3a8 | 1026 | */ |
49abc3a8 | 1027 | truncate_setsize(inode, newsize); |
c4ed4243 | 1028 | |
350976ae EG |
1029 | /* |
1030 | * We are going to log the inode size change in this transaction so | |
1031 | * any previous writes that are beyond the on disk EOF and the new | |
1032 | * EOF that have not been written out need to be written here. If we | |
1033 | * do not write the data out, we expose ourselves to the null files | |
1034 | * problem. Note that this includes any block zeroing we did above; | |
1035 | * otherwise those blocks may not be zeroed after a crash. | |
1036 | */ | |
1037 | if (did_zeroing || | |
13d2c10b | 1038 | (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) { |
350976ae | 1039 | error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, |
13d2c10b | 1040 | ip->i_disk_size, newsize - 1); |
350976ae EG |
1041 | if (error) |
1042 | return error; | |
1043 | } | |
1044 | ||
d0489451 ZY |
1045 | /* |
1046 | * For realtime inode with more than one block rtextsize, we need the | |
1047 | * block reservation for bmap btree block allocations/splits that can | |
1048 | * happen since it could split the tail written extent and convert the | |
1049 | * right beyond EOF one to unwritten. | |
1050 | */ | |
1051 | if (xfs_inode_has_bigrtalloc(ip)) | |
1052 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); | |
1053 | ||
1054 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks, | |
1055 | 0, 0, &tp); | |
c4ed4243 | 1056 | if (error) |
253f4911 | 1057 | return error; |
c4ed4243 | 1058 | |
c4ed4243 | 1059 | lock_flags |= XFS_ILOCK_EXCL; |
c4ed4243 | 1060 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
ddc3415a | 1061 | xfs_trans_ijoin(tp, ip, 0); |
c4ed4243 CH |
1062 | |
1063 | /* | |
1064 | * Only change the c/mtime if we are changing the size or we are | |
1065 | * explicitly asked to change it. This handles the semantic difference | |
1066 | * between truncate() and ftruncate() as implemented in the VFS. | |
1067 | * | |
1068 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
1069 | * special case where we need to update the times despite not having | |
1070 | * these flags set. For all other operations the VFS set these flags | |
1071 | * explicitly if it wants a timestamp update. | |
1072 | */ | |
fe60a8a0 CH |
1073 | if (newsize != oldsize && |
1074 | !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) { | |
c4ed4243 | 1075 | iattr->ia_ctime = iattr->ia_mtime = |
f798accd | 1076 | current_time(inode); |
fe60a8a0 | 1077 | iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME; |
c4ed4243 CH |
1078 | } |
1079 | ||
673e8e59 CH |
1080 | /* |
1081 | * The first thing we do is set the size to new_size permanently on | |
1082 | * disk. This way we don't have to worry about anyone ever being able | |
1083 | * to look at the data being freed even in the face of a crash. | |
1084 | * What we're getting around here is the case where we free a block, it | |
1085 | * is allocated to another file, it is written to, and then we crash. | |
1086 | * If the new data gets written to the file but the log buffers | |
1087 | * containing the free and reallocation don't, then we'd end up with | |
1088 | * garbage in the blocks being freed. As long as we make the new size | |
1089 | * permanent before actually freeing any blocks it doesn't matter if | |
1090 | * they get written to. | |
1091 | */ | |
13d2c10b | 1092 | ip->i_disk_size = newsize; |
673e8e59 CH |
1093 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
1094 | ||
1095 | if (newsize <= oldsize) { | |
1096 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize); | |
c4ed4243 | 1097 | if (error) |
4906e215 | 1098 | goto out_trans_cancel; |
c4ed4243 CH |
1099 | |
1100 | /* | |
1101 | * Truncated "down", so we're removing references to old data | |
1102 | * here - if we delay flushing for a long time, we expose | |
1103 | * ourselves unduly to the notorious NULL files problem. So, | |
1104 | * we mark this inode and flush it when the file is closed, | |
1105 | * and do not wait the usual (long) time for writeout. | |
1106 | */ | |
1107 | xfs_iflags_set(ip, XFS_ITRUNCATED); | |
27b52867 BF |
1108 | |
1109 | /* A truncate down always removes post-EOF blocks. */ | |
1110 | xfs_inode_clear_eofblocks_tag(ip); | |
c4ed4243 CH |
1111 | } |
1112 | ||
e014f37d | 1113 | ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID))); |
c1632a0f | 1114 | setattr_copy(idmap, inode, iattr); |
c4ed4243 CH |
1115 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
1116 | ||
ff6d6af2 | 1117 | XFS_STATS_INC(mp, xs_ig_attrchg); |
c4ed4243 | 1118 | |
0560f31a | 1119 | if (xfs_has_wsync(mp)) |
c4ed4243 CH |
1120 | xfs_trans_set_sync(tp); |
1121 | ||
70393313 | 1122 | error = xfs_trans_commit(tp); |
c4ed4243 CH |
1123 | out_unlock: |
1124 | if (lock_flags) | |
1125 | xfs_iunlock(ip, lock_flags); | |
1126 | return error; | |
1127 | ||
c4ed4243 | 1128 | out_trans_cancel: |
4906e215 | 1129 | xfs_trans_cancel(tp); |
c4ed4243 CH |
1130 | goto out_unlock; |
1131 | } | |
1132 | ||
69bca807 JK |
1133 | int |
1134 | xfs_vn_setattr_size( | |
c1632a0f | 1135 | struct mnt_idmap *idmap, |
69bca807 JK |
1136 | struct dentry *dentry, |
1137 | struct iattr *iattr) | |
1138 | { | |
1139 | struct xfs_inode *ip = XFS_I(d_inode(dentry)); | |
1140 | int error; | |
1141 | ||
1142 | trace_xfs_setattr(ip); | |
1143 | ||
c1632a0f | 1144 | error = xfs_vn_change_ok(idmap, dentry, iattr); |
69bca807 JK |
1145 | if (error) |
1146 | return error; | |
c1632a0f | 1147 | return xfs_setattr_size(idmap, dentry, ip, iattr); |
69bca807 JK |
1148 | } |
1149 | ||
1da177e4 | 1150 | STATIC int |
416c6d5b | 1151 | xfs_vn_setattr( |
c1632a0f | 1152 | struct mnt_idmap *idmap, |
76ca4c23 CH |
1153 | struct dentry *dentry, |
1154 | struct iattr *iattr) | |
1da177e4 | 1155 | { |
26f88363 CH |
1156 | struct inode *inode = d_inode(dentry); |
1157 | struct xfs_inode *ip = XFS_I(inode); | |
76ca4c23 CH |
1158 | int error; |
1159 | ||
1160 | if (iattr->ia_valid & ATTR_SIZE) { | |
c63a8eae | 1161 | uint iolock; |
781355c6 | 1162 | |
c63a8eae DW |
1163 | xfs_ilock(ip, XFS_MMAPLOCK_EXCL); |
1164 | iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL; | |
781355c6 | 1165 | |
69eb5fa1 | 1166 | error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP); |
c63a8eae DW |
1167 | if (error) { |
1168 | xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); | |
65523218 | 1169 | return error; |
c63a8eae | 1170 | } |
e8e9ad42 | 1171 | |
c1632a0f | 1172 | error = xfs_vn_setattr_size(idmap, dentry, iattr); |
65523218 | 1173 | xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); |
76ca4c23 | 1174 | } else { |
26f88363 CH |
1175 | trace_xfs_setattr(ip); |
1176 | ||
c1632a0f | 1177 | error = xfs_vn_change_ok(idmap, dentry, iattr); |
26f88363 | 1178 | if (!error) |
c1632a0f | 1179 | error = xfs_setattr_nonsize(idmap, dentry, ip, iattr); |
76ca4c23 CH |
1180 | } |
1181 | ||
2451337d | 1182 | return error; |
1da177e4 LT |
1183 | } |
1184 | ||
69ff2826 CH |
1185 | STATIC int |
1186 | xfs_vn_update_time( | |
1187 | struct inode *inode, | |
69ff2826 CH |
1188 | int flags) |
1189 | { | |
1190 | struct xfs_inode *ip = XFS_I(inode); | |
1191 | struct xfs_mount *mp = ip->i_mount; | |
c3b1b131 | 1192 | int log_flags = XFS_ILOG_TIMESTAMP; |
69ff2826 CH |
1193 | struct xfs_trans *tp; |
1194 | int error; | |
51b0f3eb | 1195 | struct timespec64 now; |
69ff2826 CH |
1196 | |
1197 | trace_xfs_update_time(ip); | |
1198 | ||
c3b1b131 CH |
1199 | if (inode->i_sb->s_flags & SB_LAZYTIME) { |
1200 | if (!((flags & S_VERSION) && | |
541d4c79 JL |
1201 | inode_maybe_inc_iversion(inode, false))) { |
1202 | generic_update_time(inode, flags); | |
1203 | return 0; | |
1204 | } | |
c3b1b131 CH |
1205 | |
1206 | /* Capture the iversion update that just occurred */ | |
1207 | log_flags |= XFS_ILOG_CORE; | |
1208 | } | |
1209 | ||
253f4911 CH |
1210 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); |
1211 | if (error) | |
2451337d | 1212 | return error; |
69ff2826 CH |
1213 | |
1214 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
51b0f3eb JL |
1215 | if (flags & (S_CTIME|S_MTIME)) |
1216 | now = inode_set_ctime_current(inode); | |
1217 | else | |
1218 | now = current_time(inode); | |
1219 | ||
3987848c | 1220 | if (flags & S_MTIME) |
75d1e312 | 1221 | inode_set_mtime_to_ts(inode, now); |
3987848c | 1222 | if (flags & S_ATIME) |
75d1e312 | 1223 | inode_set_atime_to_ts(inode, now); |
3987848c | 1224 | |
69ff2826 | 1225 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
c3b1b131 | 1226 | xfs_trans_log_inode(tp, ip, log_flags); |
70393313 | 1227 | return xfs_trans_commit(tp); |
69ff2826 CH |
1228 | } |
1229 | ||
f35642e2 ES |
1230 | STATIC int |
1231 | xfs_vn_fiemap( | |
1232 | struct inode *inode, | |
1233 | struct fiemap_extent_info *fieinfo, | |
1234 | u64 start, | |
1235 | u64 length) | |
1236 | { | |
f35642e2 ES |
1237 | int error; |
1238 | ||
d2bb140e | 1239 | xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED); |
1d4795e7 CH |
1240 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
1241 | fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; | |
1242 | error = iomap_fiemap(inode, fieinfo, start, length, | |
1243 | &xfs_xattr_iomap_ops); | |
1244 | } else { | |
1245 | error = iomap_fiemap(inode, fieinfo, start, length, | |
690c2a38 | 1246 | &xfs_read_iomap_ops); |
1d4795e7 | 1247 | } |
d2bb140e | 1248 | xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED); |
f35642e2 | 1249 | |
d2bb140e | 1250 | return error; |
f35642e2 ES |
1251 | } |
1252 | ||
99b6436b ZYW |
1253 | STATIC int |
1254 | xfs_vn_tmpfile( | |
011e2b71 | 1255 | struct mnt_idmap *idmap, |
549c7297 | 1256 | struct inode *dir, |
863f144f | 1257 | struct file *file, |
549c7297 | 1258 | umode_t mode) |
99b6436b | 1259 | { |
f2d40141 | 1260 | int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file); |
863f144f MS |
1261 | |
1262 | return finish_open_simple(file, err); | |
99b6436b ZYW |
1263 | } |
1264 | ||
41be8bed | 1265 | static const struct inode_operations xfs_inode_operations = { |
cac2f8b8 | 1266 | .get_inode_acl = xfs_get_acl, |
2401dc29 | 1267 | .set_acl = xfs_set_acl, |
416c6d5b NS |
1268 | .getattr = xfs_vn_getattr, |
1269 | .setattr = xfs_vn_setattr, | |
416c6d5b | 1270 | .listxattr = xfs_vn_listxattr, |
f35642e2 | 1271 | .fiemap = xfs_vn_fiemap, |
69ff2826 | 1272 | .update_time = xfs_vn_update_time, |
9fefd5db MS |
1273 | .fileattr_get = xfs_fileattr_get, |
1274 | .fileattr_set = xfs_fileattr_set, | |
1da177e4 LT |
1275 | }; |
1276 | ||
41be8bed | 1277 | static const struct inode_operations xfs_dir_inode_operations = { |
416c6d5b NS |
1278 | .create = xfs_vn_create, |
1279 | .lookup = xfs_vn_lookup, | |
1280 | .link = xfs_vn_link, | |
1281 | .unlink = xfs_vn_unlink, | |
1282 | .symlink = xfs_vn_symlink, | |
1283 | .mkdir = xfs_vn_mkdir, | |
8f112e3b CH |
1284 | /* |
1285 | * Yes, XFS uses the same method for rmdir and unlink. | |
1286 | * | |
1287 | * There are some subtile differences deeper in the code, | |
1288 | * but we use S_ISDIR to check for those. | |
1289 | */ | |
1290 | .rmdir = xfs_vn_unlink, | |
416c6d5b | 1291 | .mknod = xfs_vn_mknod, |
2773bf00 | 1292 | .rename = xfs_vn_rename, |
cac2f8b8 | 1293 | .get_inode_acl = xfs_get_acl, |
2401dc29 | 1294 | .set_acl = xfs_set_acl, |
416c6d5b NS |
1295 | .getattr = xfs_vn_getattr, |
1296 | .setattr = xfs_vn_setattr, | |
416c6d5b | 1297 | .listxattr = xfs_vn_listxattr, |
69ff2826 | 1298 | .update_time = xfs_vn_update_time, |
99b6436b | 1299 | .tmpfile = xfs_vn_tmpfile, |
9fefd5db MS |
1300 | .fileattr_get = xfs_fileattr_get, |
1301 | .fileattr_set = xfs_fileattr_set, | |
1da177e4 LT |
1302 | }; |
1303 | ||
41be8bed | 1304 | static const struct inode_operations xfs_dir_ci_inode_operations = { |
384f3ced BN |
1305 | .create = xfs_vn_create, |
1306 | .lookup = xfs_vn_ci_lookup, | |
1307 | .link = xfs_vn_link, | |
1308 | .unlink = xfs_vn_unlink, | |
1309 | .symlink = xfs_vn_symlink, | |
1310 | .mkdir = xfs_vn_mkdir, | |
8f112e3b CH |
1311 | /* |
1312 | * Yes, XFS uses the same method for rmdir and unlink. | |
1313 | * | |
1314 | * There are some subtile differences deeper in the code, | |
1315 | * but we use S_ISDIR to check for those. | |
1316 | */ | |
1317 | .rmdir = xfs_vn_unlink, | |
384f3ced | 1318 | .mknod = xfs_vn_mknod, |
2773bf00 | 1319 | .rename = xfs_vn_rename, |
cac2f8b8 | 1320 | .get_inode_acl = xfs_get_acl, |
2401dc29 | 1321 | .set_acl = xfs_set_acl, |
384f3ced BN |
1322 | .getattr = xfs_vn_getattr, |
1323 | .setattr = xfs_vn_setattr, | |
384f3ced | 1324 | .listxattr = xfs_vn_listxattr, |
69ff2826 | 1325 | .update_time = xfs_vn_update_time, |
99b6436b | 1326 | .tmpfile = xfs_vn_tmpfile, |
9fefd5db MS |
1327 | .fileattr_get = xfs_fileattr_get, |
1328 | .fileattr_set = xfs_fileattr_set, | |
384f3ced BN |
1329 | }; |
1330 | ||
41be8bed | 1331 | static const struct inode_operations xfs_symlink_inode_operations = { |
6b255391 | 1332 | .get_link = xfs_vn_get_link, |
416c6d5b NS |
1333 | .getattr = xfs_vn_getattr, |
1334 | .setattr = xfs_vn_setattr, | |
416c6d5b | 1335 | .listxattr = xfs_vn_listxattr, |
69ff2826 | 1336 | .update_time = xfs_vn_update_time, |
1da177e4 | 1337 | }; |
41be8bed | 1338 | |
ba23cba9 DW |
1339 | /* Figure out if this file actually supports DAX. */ |
1340 | static bool | |
1341 | xfs_inode_supports_dax( | |
1342 | struct xfs_inode *ip) | |
1343 | { | |
1344 | struct xfs_mount *mp = ip->i_mount; | |
1345 | ||
32dbc565 IW |
1346 | /* Only supported on regular files. */ |
1347 | if (!S_ISREG(VFS_I(ip)->i_mode)) | |
ba23cba9 DW |
1348 | return false; |
1349 | ||
ba23cba9 DW |
1350 | /* Block size must match page size */ |
1351 | if (mp->m_sb.sb_blocksize != PAGE_SIZE) | |
1352 | return false; | |
1353 | ||
1354 | /* Device has to support DAX too. */ | |
30fa529e | 1355 | return xfs_inode_buftarg(ip)->bt_daxdev != NULL; |
ba23cba9 DW |
1356 | } |
1357 | ||
32dbc565 IW |
1358 | static bool |
1359 | xfs_inode_should_enable_dax( | |
1360 | struct xfs_inode *ip) | |
1361 | { | |
1362 | if (!IS_ENABLED(CONFIG_FS_DAX)) | |
1363 | return false; | |
0560f31a | 1364 | if (xfs_has_dax_never(ip->i_mount)) |
32dbc565 IW |
1365 | return false; |
1366 | if (!xfs_inode_supports_dax(ip)) | |
1367 | return false; | |
0560f31a | 1368 | if (xfs_has_dax_always(ip->i_mount)) |
32dbc565 | 1369 | return true; |
3e09ab8f | 1370 | if (ip->i_diflags2 & XFS_DIFLAG2_DAX) |
32dbc565 IW |
1371 | return true; |
1372 | return false; | |
1373 | } | |
1374 | ||
840d493d | 1375 | void |
41be8bed | 1376 | xfs_diflags_to_iflags( |
840d493d IW |
1377 | struct xfs_inode *ip, |
1378 | bool init) | |
41be8bed | 1379 | { |
840d493d IW |
1380 | struct inode *inode = VFS_I(ip); |
1381 | unsigned int xflags = xfs_ip2xflags(ip); | |
1382 | unsigned int flags = 0; | |
1383 | ||
1384 | ASSERT(!(IS_DAX(inode) && init)); | |
1385 | ||
1386 | if (xflags & FS_XFLAG_IMMUTABLE) | |
1387 | flags |= S_IMMUTABLE; | |
1388 | if (xflags & FS_XFLAG_APPEND) | |
1389 | flags |= S_APPEND; | |
1390 | if (xflags & FS_XFLAG_SYNC) | |
1391 | flags |= S_SYNC; | |
1392 | if (xflags & FS_XFLAG_NOATIME) | |
1393 | flags |= S_NOATIME; | |
1394 | if (init && xfs_inode_should_enable_dax(ip)) | |
1395 | flags |= S_DAX; | |
1396 | ||
1397 | /* | |
1398 | * S_DAX can only be set during inode initialization and is never set by | |
1399 | * the VFS, so we cannot mask off S_DAX in i_flags. | |
1400 | */ | |
1401 | inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME); | |
1402 | inode->i_flags |= flags; | |
41be8bed CH |
1403 | } |
1404 | ||
1405 | /* | |
2b3d1d41 | 1406 | * Initialize the Linux inode. |
bf904248 | 1407 | * |
58c90473 | 1408 | * When reading existing inodes from disk this is called directly from xfs_iget, |
132c460e YX |
1409 | * when creating a new inode it is called from xfs_init_new_inode after setting |
1410 | * up the inode. These callers have different criteria for clearing XFS_INEW, so | |
1411 | * leave it up to the caller to deal with unlocking the inode appropriately. | |
41be8bed CH |
1412 | */ |
1413 | void | |
1414 | xfs_setup_inode( | |
1415 | struct xfs_inode *ip) | |
1416 | { | |
bf904248 | 1417 | struct inode *inode = &ip->i_vnode; |
ad22c7a0 | 1418 | gfp_t gfp_mask; |
bb6cdd55 | 1419 | bool is_meta = xfs_is_internal_inode(ip); |
bf904248 DC |
1420 | |
1421 | inode->i_ino = ip->i_ino; | |
f38a032b | 1422 | inode->i_state |= I_NEW; |
646ec461 CH |
1423 | |
1424 | inode_sb_list_add(inode); | |
c6f6cd06 | 1425 | /* make the inode look hashed for the writeback code */ |
5bef9151 | 1426 | inode_fake_hash(inode); |
41be8bed | 1427 | |
13d2c10b | 1428 | i_size_write(inode, ip->i_disk_size); |
840d493d | 1429 | xfs_diflags_to_iflags(ip, true); |
41be8bed | 1430 | |
bb6cdd55 DW |
1431 | /* |
1432 | * Mark our metadata files as private so that LSMs and the ACL code | |
1433 | * don't try to add their own metadata or reason about these files, | |
1434 | * and users cannot ever obtain file handles to them. | |
1435 | */ | |
1436 | if (is_meta) { | |
1437 | inode->i_flags |= S_PRIVATE; | |
1438 | inode->i_opflags &= ~IOP_XATTR; | |
1439 | } | |
1440 | ||
2b3d1d41 | 1441 | if (S_ISDIR(inode->i_mode)) { |
ef215e39 DC |
1442 | /* |
1443 | * We set the i_rwsem class here to avoid potential races with | |
1444 | * lockdep_annotate_inode_mutex_key() reinitialising the lock | |
1445 | * after a filehandle lookup has already found the inode in | |
1446 | * cache before it has been unlocked via unlock_new_inode(). | |
1447 | */ | |
1448 | lockdep_set_class(&inode->i_rwsem, | |
1449 | &inode->i_sb->s_type->i_mutex_dir_key); | |
785dd131 | 1450 | lockdep_set_class(&ip->i_lock, &xfs_dir_ilock_class); |
2b3d1d41 | 1451 | } else { |
785dd131 | 1452 | lockdep_set_class(&ip->i_lock, &xfs_nondir_ilock_class); |
41be8bed CH |
1453 | } |
1454 | ||
ad22c7a0 DC |
1455 | /* |
1456 | * Ensure all page cache allocations are done from GFP_NOFS context to | |
1457 | * prevent direct reclaim recursion back into the filesystem and blowing | |
1458 | * stacks or deadlocking. | |
1459 | */ | |
1460 | gfp_mask = mapping_gfp_mask(inode->i_mapping); | |
1461 | mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS))); | |
1462 | ||
9c041384 CH |
1463 | /* |
1464 | * For real-time inodes update the stable write flags to that of the RT | |
1465 | * device instead of the data device. | |
1466 | */ | |
1467 | if (S_ISREG(inode->i_mode) && XFS_IS_REALTIME_INODE(ip)) | |
1468 | xfs_update_stable_writes(ip); | |
1469 | ||
510792ee CH |
1470 | /* |
1471 | * If there is no attribute fork no ACL can exist on this inode, | |
1472 | * and it can't have any file capabilities attached to it either. | |
1473 | */ | |
932b42c6 | 1474 | if (!xfs_inode_has_attr_fork(ip)) { |
510792ee | 1475 | inode_has_no_xattr(inode); |
6311b108 | 1476 | cache_no_acl(inode); |
510792ee | 1477 | } |
41be8bed | 1478 | } |
2b3d1d41 CH |
1479 | |
1480 | void | |
1481 | xfs_setup_iops( | |
1482 | struct xfs_inode *ip) | |
1483 | { | |
1484 | struct inode *inode = &ip->i_vnode; | |
1485 | ||
41be8bed CH |
1486 | switch (inode->i_mode & S_IFMT) { |
1487 | case S_IFREG: | |
1488 | inode->i_op = &xfs_inode_operations; | |
1489 | inode->i_fop = &xfs_file_operations; | |
6e2608df DW |
1490 | if (IS_DAX(inode)) |
1491 | inode->i_mapping->a_ops = &xfs_dax_aops; | |
1492 | else | |
1493 | inode->i_mapping->a_ops = &xfs_address_space_operations; | |
41be8bed CH |
1494 | break; |
1495 | case S_IFDIR: | |
38c26bfd | 1496 | if (xfs_has_asciici(XFS_M(inode->i_sb))) |
41be8bed CH |
1497 | inode->i_op = &xfs_dir_ci_inode_operations; |
1498 | else | |
1499 | inode->i_op = &xfs_dir_inode_operations; | |
1500 | inode->i_fop = &xfs_dir_file_operations; | |
1501 | break; | |
1502 | case S_IFLNK: | |
7b7820b8 | 1503 | inode->i_op = &xfs_symlink_inode_operations; |
41be8bed CH |
1504 | break; |
1505 | default: | |
1506 | inode->i_op = &xfs_inode_operations; | |
1507 | init_special_inode(inode, inode->i_mode, inode->i_rdev); | |
1508 | break; | |
1509 | } | |
41be8bed | 1510 | } |