1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_acl.h"
15 #include "xfs_quota.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
18 #include "xfs_attr.h"
19 #include "xfs_trans.h"
20 #include "xfs_trans_space.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_trace.h"
23 #include "xfs_icache.h"
24 #include "xfs_symlink.h"
25 #include "xfs_dir2.h"
26 #include "xfs_iomap.h"
27 #include "xfs_error.h"
28 #include "xfs_ioctl.h"
29 #include "xfs_xattr.h"
30 #include "xfs_file.h"
31 #include "xfs_bmap.h"
32 #include "xfs_zone_alloc.h"
33
34 #include <linux/posix_acl.h>
35 #include <linux/security.h>
36 #include <linux/iversion.h>
37 #include <linux/fiemap.h>
38
39 /*
40 * Directories have different lock order w.r.t. mmap_lock compared to regular
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
44 * mmap_lock is taken during the page fault, and then we lock the ilock to do
45 * block mapping. Hence we need a different class for the directory ilock so
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.
49 */
50 static struct lock_class_key xfs_nondir_ilock_class;
51 static struct lock_class_key xfs_dir_ilock_class;
52
53 static int
xfs_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * fs_info)54 xfs_initxattrs(
55 struct inode *inode,
56 const struct xattr *xattr_array,
57 void *fs_info)
58 {
59 const struct xattr *xattr;
60 struct xfs_inode *ip = XFS_I(inode);
61 int error = 0;
62
63 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
64 struct xfs_da_args args = {
65 .dp = ip,
66 .attr_filter = XFS_ATTR_SECURE,
67 .name = xattr->name,
68 .namelen = strlen(xattr->name),
69 .value = xattr->value,
70 .valuelen = xattr->value_len,
71 };
72 error = xfs_attr_change(&args, XFS_ATTRUPDATE_UPSERT);
73 if (error < 0)
74 break;
75 }
76 return error;
77 }
78
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 */
85 int
xfs_inode_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr)86 xfs_inode_init_security(
87 struct inode *inode,
88 struct inode *dir,
89 const struct qstr *qstr)
90 {
91 return security_inode_init_security(inode, dir, qstr,
92 &xfs_initxattrs, NULL);
93 }
94
95 static void
xfs_dentry_to_name(struct xfs_name * namep,struct dentry * dentry)96 xfs_dentry_to_name(
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
xfs_dentry_mode_to_name(struct xfs_name * namep,struct dentry * dentry,int mode)106 xfs_dentry_mode_to_name(
107 struct xfs_name *namep,
108 struct dentry *dentry,
109 int mode)
110 {
111 namep->name = dentry->d_name.name;
112 namep->len = dentry->d_name.len;
113 namep->type = xfs_mode_to_ftype(mode);
114
115 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
116 return -EFSCORRUPTED;
117
118 return 0;
119 }
120
121 STATIC void
xfs_cleanup_inode(struct inode * dir,struct inode * inode,struct dentry * dentry)122 xfs_cleanup_inode(
123 struct inode *dir,
124 struct inode *inode,
125 struct dentry *dentry)
126 {
127 struct xfs_name teardown;
128
129 /* Oh, the horror.
130 * If we can't add the ACL or we fail in
131 * xfs_inode_init_security we must back out.
132 * ENOSPC can hit here, among other things.
133 */
134 xfs_dentry_to_name(&teardown, dentry);
135
136 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
137 }
138
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
xfs_create_need_xattr(struct inode * dir,struct posix_acl * default_acl,struct posix_acl * acl)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
170 STATIC int
xfs_generic_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev,struct file * tmpfile)171 xfs_generic_create(
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 */
178 {
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;
190
191 /*
192 * Irix uses Missed'em'V split, but doesn't want to see
193 * the upper 5 bits of (14bit) major.
194 */
195 if (S_ISCHR(args.mode) || S_ISBLK(args.mode)) {
196 if (unlikely(!sysv_valid_dev(args.rdev) ||
197 MAJOR(args.rdev) & ~0x1ff))
198 return -EINVAL;
199 } else {
200 args.rdev = 0;
201 }
202
203 error = posix_acl_create(dir, &args.mode, &default_acl, &acl);
204 if (error)
205 return error;
206
207 /* Verify mode is valid also for tmpfile case */
208 error = xfs_dentry_mode_to_name(&name, dentry, args.mode);
209 if (unlikely(error))
210 goto out_free_acl;
211
212 if (!tmpfile) {
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);
217 } else {
218 args.flags |= XFS_ICREATE_TMPFILE;
219
220 /*
221 * If this temporary file will not be linkable, don't bother
222 * creating an attr fork to receive a parent pointer.
223 */
224 if (tmpfile->f_flags & O_EXCL)
225 args.flags |= XFS_ICREATE_UNLINKABLE;
226
227 error = xfs_create_tmpfile(&args, &ip);
228 }
229 if (unlikely(error))
230 goto out_free_acl;
231
232 inode = VFS_I(ip);
233
234 error = xfs_inode_init_security(inode, dir, &dentry->d_name);
235 if (unlikely(error))
236 goto out_cleanup_inode;
237
238 if (default_acl) {
239 error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
240 if (error)
241 goto out_cleanup_inode;
242 }
243 if (acl) {
244 error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
245 if (error)
246 goto out_cleanup_inode;
247 }
248
249 xfs_setup_iops(ip);
250
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);
261 d_tmpfile(tmpfile, inode);
262 } else
263 d_instantiate(dentry, inode);
264
265 xfs_finish_inode_setup(ip);
266
267 out_free_acl:
268 posix_acl_release(default_acl);
269 posix_acl_release(acl);
270 return error;
271
272 out_cleanup_inode:
273 xfs_finish_inode_setup(ip);
274 if (!tmpfile)
275 xfs_cleanup_inode(dir, inode, dentry);
276 xfs_irele(ip);
277 goto out_free_acl;
278 }
279
280 STATIC int
xfs_vn_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)281 xfs_vn_mknod(
282 struct mnt_idmap *idmap,
283 struct inode *dir,
284 struct dentry *dentry,
285 umode_t mode,
286 dev_t rdev)
287 {
288 return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL);
289 }
290
291 STATIC int
xfs_vn_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool flags)292 xfs_vn_create(
293 struct mnt_idmap *idmap,
294 struct inode *dir,
295 struct dentry *dentry,
296 umode_t mode,
297 bool flags)
298 {
299 return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL);
300 }
301
302 STATIC struct dentry *
xfs_vn_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)303 xfs_vn_mkdir(
304 struct mnt_idmap *idmap,
305 struct inode *dir,
306 struct dentry *dentry,
307 umode_t mode)
308 {
309 return ERR_PTR(xfs_generic_create(idmap, dir, dentry, mode | S_IFDIR, 0, NULL));
310 }
311
312 STATIC struct dentry *
xfs_vn_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)313 xfs_vn_lookup(
314 struct inode *dir,
315 struct dentry *dentry,
316 unsigned int flags)
317 {
318 struct inode *inode;
319 struct xfs_inode *cip;
320 struct xfs_name name;
321 int error;
322
323 if (dentry->d_name.len >= MAXNAMELEN)
324 return ERR_PTR(-ENAMETOOLONG);
325
326 xfs_dentry_to_name(&name, dentry);
327 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
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);
335 }
336
337 STATIC struct dentry *
xfs_vn_ci_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)338 xfs_vn_ci_lookup(
339 struct inode *dir,
340 struct dentry *dentry,
341 unsigned int flags)
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
352 xfs_dentry_to_name(&xname, dentry);
353 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
354 if (unlikely(error)) {
355 if (unlikely(error != -ENOENT))
356 return ERR_PTR(error);
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 */
362 return NULL;
363 }
364
365 /* if exact match, just splice and exit */
366 if (!ci_name.name)
367 return d_splice_alias(VFS_I(ip), dentry);
368
369 /* else case-insensitive match... */
370 dname.name = ci_name.name;
371 dname.len = ci_name.len;
372 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
373 kfree(ci_name.name);
374 return dentry;
375 }
376
377 STATIC int
xfs_vn_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)378 xfs_vn_link(
379 struct dentry *old_dentry,
380 struct inode *dir,
381 struct dentry *dentry)
382 {
383 struct inode *inode = d_inode(old_dentry);
384 struct xfs_name name;
385 int error;
386
387 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
388 if (unlikely(error))
389 return error;
390
391 if (IS_PRIVATE(inode))
392 return -EPERM;
393
394 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
395 if (unlikely(error))
396 return error;
397
398 ihold(inode);
399 d_instantiate(dentry, inode);
400 return 0;
401 }
402
403 STATIC int
xfs_vn_unlink(struct inode * dir,struct dentry * dentry)404 xfs_vn_unlink(
405 struct inode *dir,
406 struct dentry *dentry)
407 {
408 struct xfs_name name;
409 int error;
410
411 xfs_dentry_to_name(&name, dentry);
412
413 error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
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 */
422 if (xfs_has_asciici(XFS_M(dir->i_sb)))
423 d_invalidate(dentry);
424 return 0;
425 }
426
427 STATIC int
xfs_vn_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)428 xfs_vn_symlink(
429 struct mnt_idmap *idmap,
430 struct inode *dir,
431 struct dentry *dentry,
432 const char *symname)
433 {
434 struct inode *inode;
435 struct xfs_inode *cip = NULL;
436 struct xfs_name name;
437 int error;
438 umode_t mode;
439
440 mode = S_IFLNK |
441 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
442 error = xfs_dentry_mode_to_name(&name, dentry, mode);
443 if (unlikely(error))
444 goto out;
445
446 error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip);
447 if (unlikely(error))
448 goto out;
449
450 inode = VFS_I(cip);
451
452 error = xfs_inode_init_security(inode, dir, &dentry->d_name);
453 if (unlikely(error))
454 goto out_cleanup_inode;
455
456 xfs_setup_iops(cip);
457
458 d_instantiate(dentry, inode);
459 xfs_finish_inode_setup(cip);
460 return 0;
461
462 out_cleanup_inode:
463 xfs_finish_inode_setup(cip);
464 xfs_cleanup_inode(dir, inode, dentry);
465 xfs_irele(cip);
466 out:
467 return error;
468 }
469
470 STATIC int
xfs_vn_rename(struct mnt_idmap * idmap,struct inode * odir,struct dentry * odentry,struct inode * ndir,struct dentry * ndentry,unsigned int flags)471 xfs_vn_rename(
472 struct mnt_idmap *idmap,
473 struct inode *odir,
474 struct dentry *odentry,
475 struct inode *ndir,
476 struct dentry *ndentry,
477 unsigned int flags)
478 {
479 struct inode *new_inode = d_inode(ndentry);
480 int omode = 0;
481 int error;
482 struct xfs_name oname;
483 struct xfs_name nname;
484
485 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
486 return -EINVAL;
487
488 /* if we are exchanging files, we need to set i_mode of both files */
489 if (flags & RENAME_EXCHANGE)
490 omode = d_inode(ndentry)->i_mode;
491
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;
500
501 return xfs_rename(idmap, XFS_I(odir), &oname,
502 XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
503 new_inode ? XFS_I(new_inode) : NULL, flags);
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 */
511 STATIC const char *
xfs_vn_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)512 xfs_vn_get_link(
513 struct dentry *dentry,
514 struct inode *inode,
515 struct delayed_call *done)
516 {
517 char *link;
518 int error = -ENOMEM;
519
520 if (!dentry)
521 return ERR_PTR(-ECHILD);
522
523 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
524 if (!link)
525 goto out_err;
526
527 error = xfs_readlink(XFS_I(d_inode(dentry)), link);
528 if (unlikely(error))
529 goto out_kfree;
530
531 set_delayed_call(done, kfree_link, link);
532 return link;
533
534 out_kfree:
535 kfree(link);
536 out_err:
537 return ERR_PTR(error);
538 }
539
540 static uint32_t
xfs_stat_blksize(struct xfs_inode * ip)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))
551 return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip) ? : 1);
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 */
566 if (xfs_has_large_iosize(mp)) {
567 if (mp->m_swidth)
568 return XFS_FSB_TO_B(mp, mp->m_swidth);
569 if (xfs_has_allocsize(mp))
570 return 1U << mp->m_allocsize_log;
571 }
572
573 return max_t(uint32_t, PAGE_SIZE, mp->m_sb.sb_blocksize);
574 }
575
576 static void
xfs_report_dioalign(struct xfs_inode * ip,struct kstat * stat)577 xfs_report_dioalign(
578 struct xfs_inode *ip,
579 struct kstat *stat)
580 {
581 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
582 struct block_device *bdev = target->bt_bdev;
583
584 stat->result_mask |= STATX_DIOALIGN | STATX_DIO_READ_ALIGN;
585 stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
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;
602 }
603
604 static void
xfs_report_atomic_write(struct xfs_inode * ip,struct kstat * stat)605 xfs_report_atomic_write(
606 struct xfs_inode *ip,
607 struct kstat *stat)
608 {
609 unsigned int unit_min = 0, unit_max = 0;
610
611 if (xfs_inode_can_atomicwrite(ip))
612 unit_min = unit_max = ip->i_mount->m_sb.sb_blocksize;
613 generic_fill_statx_atomic_writes(stat, unit_min, unit_max);
614 }
615
616 STATIC int
xfs_vn_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)617 xfs_vn_getattr(
618 struct mnt_idmap *idmap,
619 const struct path *path,
620 struct kstat *stat,
621 u32 request_mask,
622 unsigned int query_flags)
623 {
624 struct inode *inode = d_inode(path->dentry);
625 struct xfs_inode *ip = XFS_I(inode);
626 struct xfs_mount *mp = ip->i_mount;
627 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
628 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
629
630 trace_xfs_getattr(ip);
631
632 if (xfs_is_shutdown(mp))
633 return -EIO;
634
635 stat->size = XFS_ISIZE(ip);
636 stat->dev = inode->i_sb->s_dev;
637 stat->mode = inode->i_mode;
638 stat->nlink = inode->i_nlink;
639 stat->uid = vfsuid_into_kuid(vfsuid);
640 stat->gid = vfsgid_into_kgid(vfsgid);
641 stat->ino = ip->i_ino;
642 stat->atime = inode_get_atime(inode);
643
644 fill_mg_cmtime(stat, request_mask, inode);
645
646 stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
647
648 if (xfs_has_v3inodes(mp)) {
649 if (request_mask & STATX_BTIME) {
650 stat->result_mask |= STATX_BTIME;
651 stat->btime = ip->i_crtime;
652 }
653 }
654
655 /*
656 * Note: If you add another clause to set an attribute flag, please
657 * update attributes_mask below.
658 */
659 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
660 stat->attributes |= STATX_ATTR_IMMUTABLE;
661 if (ip->i_diflags & XFS_DIFLAG_APPEND)
662 stat->attributes |= STATX_ATTR_APPEND;
663 if (ip->i_diflags & XFS_DIFLAG_NODUMP)
664 stat->attributes |= STATX_ATTR_NODUMP;
665
666 stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
667 STATX_ATTR_APPEND |
668 STATX_ATTR_NODUMP);
669
670 switch (inode->i_mode & S_IFMT) {
671 case S_IFBLK:
672 case S_IFCHR:
673 stat->blksize = BLKDEV_IOSIZE;
674 stat->rdev = inode->i_rdev;
675 break;
676 case S_IFREG:
677 if (request_mask & (STATX_DIOALIGN | STATX_DIO_READ_ALIGN))
678 xfs_report_dioalign(ip, stat);
679 if (request_mask & STATX_WRITE_ATOMIC)
680 xfs_report_atomic_write(ip, stat);
681 fallthrough;
682 default:
683 stat->blksize = xfs_stat_blksize(ip);
684 stat->rdev = 0;
685 break;
686 }
687
688 return 0;
689 }
690
691 static int
xfs_vn_change_ok(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)692 xfs_vn_change_ok(
693 struct mnt_idmap *idmap,
694 struct dentry *dentry,
695 struct iattr *iattr)
696 {
697 struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
698
699 if (xfs_is_readonly(mp))
700 return -EROFS;
701
702 if (xfs_is_shutdown(mp))
703 return -EIO;
704
705 return setattr_prepare(idmap, dentry, iattr);
706 }
707
708 /*
709 * Set non-size attributes of an inode.
710 *
711 * Caution: The caller of this function is responsible for calling
712 * setattr_prepare() or otherwise verifying the change is fine.
713 */
714 static int
xfs_setattr_nonsize(struct mnt_idmap * idmap,struct dentry * dentry,struct xfs_inode * ip,struct iattr * iattr)715 xfs_setattr_nonsize(
716 struct mnt_idmap *idmap,
717 struct dentry *dentry,
718 struct xfs_inode *ip,
719 struct iattr *iattr)
720 {
721 xfs_mount_t *mp = ip->i_mount;
722 struct inode *inode = VFS_I(ip);
723 int mask = iattr->ia_valid;
724 xfs_trans_t *tp;
725 int error;
726 kuid_t uid = GLOBAL_ROOT_UID;
727 kgid_t gid = GLOBAL_ROOT_GID;
728 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
729 struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL;
730
731 ASSERT((mask & ATTR_SIZE) == 0);
732
733 /*
734 * If disk quotas is on, we make sure that the dquots do exist on disk,
735 * before we start any other transactions. Trying to do this later
736 * is messy. We don't care to take a readlock to look at the ids
737 * in inode here, because we can't hold it across the trans_reserve.
738 * If the IDs do change before we take the ilock, we're covered
739 * because the i_*dquot fields will get updated anyway.
740 */
741 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
742 uint qflags = 0;
743
744 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
745 uid = from_vfsuid(idmap, i_user_ns(inode),
746 iattr->ia_vfsuid);
747 qflags |= XFS_QMOPT_UQUOTA;
748 } else {
749 uid = inode->i_uid;
750 }
751 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
752 gid = from_vfsgid(idmap, i_user_ns(inode),
753 iattr->ia_vfsgid);
754 qflags |= XFS_QMOPT_GQUOTA;
755 } else {
756 gid = inode->i_gid;
757 }
758
759 /*
760 * We take a reference when we initialize udqp and gdqp,
761 * so it is important that we never blindly double trip on
762 * the same variable. See xfs_create() for an example.
763 */
764 ASSERT(udqp == NULL);
765 ASSERT(gdqp == NULL);
766 error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
767 qflags, &udqp, &gdqp, NULL);
768 if (error)
769 return error;
770 }
771
772 error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
773 has_capability_noaudit(current, CAP_FOWNER), &tp);
774 if (error)
775 goto out_dqrele;
776
777 /*
778 * Register quota modifications in the transaction. Must be the owner
779 * or privileged. These IDs could have changed since we last looked at
780 * them. But, we're assured that if the ownership did change while we
781 * didn't have the inode locked, inode's dquot(s) would have changed
782 * also.
783 */
784 if (XFS_IS_UQUOTA_ON(mp) &&
785 i_uid_needs_update(idmap, iattr, inode)) {
786 ASSERT(udqp);
787 old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp);
788 }
789 if (XFS_IS_GQUOTA_ON(mp) &&
790 i_gid_needs_update(idmap, iattr, inode)) {
791 ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp));
792 ASSERT(gdqp);
793 old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp);
794 }
795
796 setattr_copy(idmap, inode, iattr);
797 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
798
799 XFS_STATS_INC(mp, xs_ig_attrchg);
800
801 if (xfs_has_wsync(mp))
802 xfs_trans_set_sync(tp);
803 error = xfs_trans_commit(tp);
804
805 /*
806 * Release any dquot(s) the inode had kept before chown.
807 */
808 xfs_qm_dqrele(old_udqp);
809 xfs_qm_dqrele(old_gdqp);
810 xfs_qm_dqrele(udqp);
811 xfs_qm_dqrele(gdqp);
812
813 if (error)
814 return error;
815
816 /*
817 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
818 * update. We could avoid this with linked transactions
819 * and passing down the transaction pointer all the way
820 * to attr_set. No previous user of the generic
821 * Posix ACL code seems to care about this issue either.
822 */
823 if (mask & ATTR_MODE) {
824 error = posix_acl_chmod(idmap, dentry, inode->i_mode);
825 if (error)
826 return error;
827 }
828
829 return 0;
830
831 out_dqrele:
832 xfs_qm_dqrele(udqp);
833 xfs_qm_dqrele(gdqp);
834 return error;
835 }
836
837 /*
838 * Truncate file. Must have write permission and not be a directory.
839 *
840 * Caution: The caller of this function is responsible for calling
841 * setattr_prepare() or otherwise verifying the change is fine.
842 */
843 STATIC int
xfs_setattr_size(struct mnt_idmap * idmap,struct dentry * dentry,struct xfs_inode * ip,struct iattr * iattr)844 xfs_setattr_size(
845 struct mnt_idmap *idmap,
846 struct dentry *dentry,
847 struct xfs_inode *ip,
848 struct iattr *iattr)
849 {
850 struct xfs_mount *mp = ip->i_mount;
851 struct inode *inode = VFS_I(ip);
852 xfs_off_t oldsize, newsize;
853 struct xfs_trans *tp;
854 int error;
855 uint lock_flags = 0;
856 uint resblks = 0;
857 bool did_zeroing = false;
858 struct xfs_zone_alloc_ctx ac = { };
859
860 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
861 ASSERT(S_ISREG(inode->i_mode));
862 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
863 ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
864
865 oldsize = inode->i_size;
866 newsize = iattr->ia_size;
867
868 /*
869 * Short circuit the truncate case for zero length files.
870 */
871 if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
872 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
873 return 0;
874
875 /*
876 * Use the regular setattr path to update the timestamps.
877 */
878 iattr->ia_valid &= ~ATTR_SIZE;
879 return xfs_setattr_nonsize(idmap, dentry, ip, iattr);
880 }
881
882 /*
883 * Make sure that the dquots are attached to the inode.
884 */
885 error = xfs_qm_dqattach(ip);
886 if (error)
887 return error;
888
889 /*
890 * Wait for all direct I/O to complete.
891 */
892 inode_dio_wait(inode);
893
894 /*
895 * Normally xfs_zoned_space_reserve is supposed to be called outside the
896 * IOLOCK. For truncate we can't do that since ->setattr is called with
897 * it already held by the VFS. So for now chicken out and try to
898 * allocate space under it.
899 *
900 * To avoid deadlocks this means we can't block waiting for space, which
901 * can lead to spurious -ENOSPC if there are no directly available
902 * blocks. We mitigate this a bit by allowing zeroing to dip into the
903 * reserved pool, but eventually the VFS calling convention needs to
904 * change.
905 */
906 if (xfs_is_zoned_inode(ip)) {
907 error = xfs_zoned_space_reserve(ip, 1,
908 XFS_ZR_NOWAIT | XFS_ZR_RESERVED, &ac);
909 if (error) {
910 if (error == -EAGAIN)
911 return -ENOSPC;
912 return error;
913 }
914 }
915
916 /*
917 * File data changes must be complete before we start the transaction to
918 * modify the inode. This needs to be done before joining the inode to
919 * the transaction because the inode cannot be unlocked once it is a
920 * part of the transaction.
921 *
922 * Start with zeroing any data beyond EOF that we may expose on file
923 * extension, or zeroing out the rest of the block on a downward
924 * truncate.
925 */
926 if (newsize > oldsize) {
927 trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
928 error = xfs_zero_range(ip, oldsize, newsize - oldsize,
929 &ac, &did_zeroing);
930 } else {
931 error = xfs_truncate_page(ip, newsize, &ac, &did_zeroing);
932 }
933
934 if (xfs_is_zoned_inode(ip))
935 xfs_zoned_space_unreserve(ip, &ac);
936
937 if (error)
938 return error;
939
940 /*
941 * We've already locked out new page faults, so now we can safely remove
942 * pages from the page cache knowing they won't get refaulted until we
943 * drop the XFS_MMAP_EXCL lock after the extent manipulations are
944 * complete. The truncate_setsize() call also cleans partial EOF page
945 * PTEs on extending truncates and hence ensures sub-page block size
946 * filesystems are correctly handled, too.
947 *
948 * We have to do all the page cache truncate work outside the
949 * transaction context as the "lock" order is page lock->log space
950 * reservation as defined by extent allocation in the writeback path.
951 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
952 * having already truncated the in-memory version of the file (i.e. made
953 * user visible changes). There's not much we can do about this, except
954 * to hope that the caller sees ENOMEM and retries the truncate
955 * operation.
956 *
957 * And we update in-core i_size and truncate page cache beyond newsize
958 * before writeback the [i_disk_size, newsize] range, so we're
959 * guaranteed not to write stale data past the new EOF on truncate down.
960 */
961 truncate_setsize(inode, newsize);
962
963 /*
964 * We are going to log the inode size change in this transaction so
965 * any previous writes that are beyond the on disk EOF and the new
966 * EOF that have not been written out need to be written here. If we
967 * do not write the data out, we expose ourselves to the null files
968 * problem. Note that this includes any block zeroing we did above;
969 * otherwise those blocks may not be zeroed after a crash.
970 */
971 if (did_zeroing ||
972 (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
973 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
974 ip->i_disk_size, newsize - 1);
975 if (error)
976 return error;
977 }
978
979 /*
980 * For realtime inode with more than one block rtextsize, we need the
981 * block reservation for bmap btree block allocations/splits that can
982 * happen since it could split the tail written extent and convert the
983 * right beyond EOF one to unwritten.
984 */
985 if (xfs_inode_has_bigrtalloc(ip))
986 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
987
988 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks,
989 0, 0, &tp);
990 if (error)
991 return error;
992
993 lock_flags |= XFS_ILOCK_EXCL;
994 xfs_ilock(ip, XFS_ILOCK_EXCL);
995 xfs_trans_ijoin(tp, ip, 0);
996
997 /*
998 * Only change the c/mtime if we are changing the size or we are
999 * explicitly asked to change it. This handles the semantic difference
1000 * between truncate() and ftruncate() as implemented in the VFS.
1001 *
1002 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
1003 * special case where we need to update the times despite not having
1004 * these flags set. For all other operations the VFS set these flags
1005 * explicitly if it wants a timestamp update.
1006 */
1007 if (newsize != oldsize &&
1008 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
1009 iattr->ia_ctime = iattr->ia_mtime =
1010 current_time(inode);
1011 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
1012 }
1013
1014 /*
1015 * The first thing we do is set the size to new_size permanently on
1016 * disk. This way we don't have to worry about anyone ever being able
1017 * to look at the data being freed even in the face of a crash.
1018 * What we're getting around here is the case where we free a block, it
1019 * is allocated to another file, it is written to, and then we crash.
1020 * If the new data gets written to the file but the log buffers
1021 * containing the free and reallocation don't, then we'd end up with
1022 * garbage in the blocks being freed. As long as we make the new size
1023 * permanent before actually freeing any blocks it doesn't matter if
1024 * they get written to.
1025 */
1026 ip->i_disk_size = newsize;
1027 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1028
1029 if (newsize <= oldsize) {
1030 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
1031 if (error)
1032 goto out_trans_cancel;
1033
1034 /*
1035 * Truncated "down", so we're removing references to old data
1036 * here - if we delay flushing for a long time, we expose
1037 * ourselves unduly to the notorious NULL files problem. So,
1038 * we mark this inode and flush it when the file is closed,
1039 * and do not wait the usual (long) time for writeout.
1040 */
1041 xfs_iflags_set(ip, XFS_ITRUNCATED);
1042
1043 /* A truncate down always removes post-EOF blocks. */
1044 xfs_inode_clear_eofblocks_tag(ip);
1045 }
1046
1047 ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
1048 setattr_copy(idmap, inode, iattr);
1049 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1050
1051 XFS_STATS_INC(mp, xs_ig_attrchg);
1052
1053 if (xfs_has_wsync(mp))
1054 xfs_trans_set_sync(tp);
1055
1056 error = xfs_trans_commit(tp);
1057 out_unlock:
1058 if (lock_flags)
1059 xfs_iunlock(ip, lock_flags);
1060 return error;
1061
1062 out_trans_cancel:
1063 xfs_trans_cancel(tp);
1064 goto out_unlock;
1065 }
1066
1067 int
xfs_vn_setattr_size(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)1068 xfs_vn_setattr_size(
1069 struct mnt_idmap *idmap,
1070 struct dentry *dentry,
1071 struct iattr *iattr)
1072 {
1073 struct xfs_inode *ip = XFS_I(d_inode(dentry));
1074 int error;
1075
1076 trace_xfs_setattr(ip);
1077
1078 error = xfs_vn_change_ok(idmap, dentry, iattr);
1079 if (error)
1080 return error;
1081 return xfs_setattr_size(idmap, dentry, ip, iattr);
1082 }
1083
1084 STATIC int
xfs_vn_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)1085 xfs_vn_setattr(
1086 struct mnt_idmap *idmap,
1087 struct dentry *dentry,
1088 struct iattr *iattr)
1089 {
1090 struct inode *inode = d_inode(dentry);
1091 struct xfs_inode *ip = XFS_I(inode);
1092 int error;
1093
1094 if (iattr->ia_valid & ATTR_SIZE) {
1095 uint iolock;
1096
1097 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
1098 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
1099
1100 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
1101 if (error) {
1102 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1103 return error;
1104 }
1105
1106 error = xfs_vn_setattr_size(idmap, dentry, iattr);
1107 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1108 } else {
1109 trace_xfs_setattr(ip);
1110
1111 error = xfs_vn_change_ok(idmap, dentry, iattr);
1112 if (!error)
1113 error = xfs_setattr_nonsize(idmap, dentry, ip, iattr);
1114 }
1115
1116 return error;
1117 }
1118
1119 STATIC int
xfs_vn_update_time(struct inode * inode,int flags)1120 xfs_vn_update_time(
1121 struct inode *inode,
1122 int flags)
1123 {
1124 struct xfs_inode *ip = XFS_I(inode);
1125 struct xfs_mount *mp = ip->i_mount;
1126 int log_flags = XFS_ILOG_TIMESTAMP;
1127 struct xfs_trans *tp;
1128 int error;
1129 struct timespec64 now;
1130
1131 trace_xfs_update_time(ip);
1132
1133 if (inode->i_sb->s_flags & SB_LAZYTIME) {
1134 if (!((flags & S_VERSION) &&
1135 inode_maybe_inc_iversion(inode, false))) {
1136 generic_update_time(inode, flags);
1137 return 0;
1138 }
1139
1140 /* Capture the iversion update that just occurred */
1141 log_flags |= XFS_ILOG_CORE;
1142 }
1143
1144 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1145 if (error)
1146 return error;
1147
1148 xfs_ilock(ip, XFS_ILOCK_EXCL);
1149 if (flags & (S_CTIME|S_MTIME))
1150 now = inode_set_ctime_current(inode);
1151 else
1152 now = current_time(inode);
1153
1154 if (flags & S_MTIME)
1155 inode_set_mtime_to_ts(inode, now);
1156 if (flags & S_ATIME)
1157 inode_set_atime_to_ts(inode, now);
1158
1159 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1160 xfs_trans_log_inode(tp, ip, log_flags);
1161 return xfs_trans_commit(tp);
1162 }
1163
1164 STATIC int
xfs_vn_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 length)1165 xfs_vn_fiemap(
1166 struct inode *inode,
1167 struct fiemap_extent_info *fieinfo,
1168 u64 start,
1169 u64 length)
1170 {
1171 int error;
1172
1173 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
1174 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1175 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
1176 error = iomap_fiemap(inode, fieinfo, start, length,
1177 &xfs_xattr_iomap_ops);
1178 } else {
1179 error = iomap_fiemap(inode, fieinfo, start, length,
1180 &xfs_read_iomap_ops);
1181 }
1182 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
1183
1184 return error;
1185 }
1186
1187 STATIC int
xfs_vn_tmpfile(struct mnt_idmap * idmap,struct inode * dir,struct file * file,umode_t mode)1188 xfs_vn_tmpfile(
1189 struct mnt_idmap *idmap,
1190 struct inode *dir,
1191 struct file *file,
1192 umode_t mode)
1193 {
1194 int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file);
1195
1196 return finish_open_simple(file, err);
1197 }
1198
1199 static const struct inode_operations xfs_inode_operations = {
1200 .get_inode_acl = xfs_get_acl,
1201 .set_acl = xfs_set_acl,
1202 .getattr = xfs_vn_getattr,
1203 .setattr = xfs_vn_setattr,
1204 .listxattr = xfs_vn_listxattr,
1205 .fiemap = xfs_vn_fiemap,
1206 .update_time = xfs_vn_update_time,
1207 .fileattr_get = xfs_fileattr_get,
1208 .fileattr_set = xfs_fileattr_set,
1209 };
1210
1211 static const struct inode_operations xfs_dir_inode_operations = {
1212 .create = xfs_vn_create,
1213 .lookup = xfs_vn_lookup,
1214 .link = xfs_vn_link,
1215 .unlink = xfs_vn_unlink,
1216 .symlink = xfs_vn_symlink,
1217 .mkdir = xfs_vn_mkdir,
1218 /*
1219 * Yes, XFS uses the same method for rmdir and unlink.
1220 *
1221 * There are some subtile differences deeper in the code,
1222 * but we use S_ISDIR to check for those.
1223 */
1224 .rmdir = xfs_vn_unlink,
1225 .mknod = xfs_vn_mknod,
1226 .rename = xfs_vn_rename,
1227 .get_inode_acl = xfs_get_acl,
1228 .set_acl = xfs_set_acl,
1229 .getattr = xfs_vn_getattr,
1230 .setattr = xfs_vn_setattr,
1231 .listxattr = xfs_vn_listxattr,
1232 .update_time = xfs_vn_update_time,
1233 .tmpfile = xfs_vn_tmpfile,
1234 .fileattr_get = xfs_fileattr_get,
1235 .fileattr_set = xfs_fileattr_set,
1236 };
1237
1238 static const struct inode_operations xfs_dir_ci_inode_operations = {
1239 .create = xfs_vn_create,
1240 .lookup = xfs_vn_ci_lookup,
1241 .link = xfs_vn_link,
1242 .unlink = xfs_vn_unlink,
1243 .symlink = xfs_vn_symlink,
1244 .mkdir = xfs_vn_mkdir,
1245 /*
1246 * Yes, XFS uses the same method for rmdir and unlink.
1247 *
1248 * There are some subtile differences deeper in the code,
1249 * but we use S_ISDIR to check for those.
1250 */
1251 .rmdir = xfs_vn_unlink,
1252 .mknod = xfs_vn_mknod,
1253 .rename = xfs_vn_rename,
1254 .get_inode_acl = xfs_get_acl,
1255 .set_acl = xfs_set_acl,
1256 .getattr = xfs_vn_getattr,
1257 .setattr = xfs_vn_setattr,
1258 .listxattr = xfs_vn_listxattr,
1259 .update_time = xfs_vn_update_time,
1260 .tmpfile = xfs_vn_tmpfile,
1261 .fileattr_get = xfs_fileattr_get,
1262 .fileattr_set = xfs_fileattr_set,
1263 };
1264
1265 static const struct inode_operations xfs_symlink_inode_operations = {
1266 .get_link = xfs_vn_get_link,
1267 .getattr = xfs_vn_getattr,
1268 .setattr = xfs_vn_setattr,
1269 .listxattr = xfs_vn_listxattr,
1270 .update_time = xfs_vn_update_time,
1271 };
1272
1273 /* Figure out if this file actually supports DAX. */
1274 static bool
xfs_inode_supports_dax(struct xfs_inode * ip)1275 xfs_inode_supports_dax(
1276 struct xfs_inode *ip)
1277 {
1278 struct xfs_mount *mp = ip->i_mount;
1279
1280 /* Only supported on regular files. */
1281 if (!S_ISREG(VFS_I(ip)->i_mode))
1282 return false;
1283
1284 /* Block size must match page size */
1285 if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1286 return false;
1287
1288 /* Device has to support DAX too. */
1289 return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
1290 }
1291
1292 static bool
xfs_inode_should_enable_dax(struct xfs_inode * ip)1293 xfs_inode_should_enable_dax(
1294 struct xfs_inode *ip)
1295 {
1296 if (!IS_ENABLED(CONFIG_FS_DAX))
1297 return false;
1298 if (xfs_has_dax_never(ip->i_mount))
1299 return false;
1300 if (!xfs_inode_supports_dax(ip))
1301 return false;
1302 if (xfs_has_dax_always(ip->i_mount))
1303 return true;
1304 if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
1305 return true;
1306 return false;
1307 }
1308
1309 void
xfs_diflags_to_iflags(struct xfs_inode * ip,bool init)1310 xfs_diflags_to_iflags(
1311 struct xfs_inode *ip,
1312 bool init)
1313 {
1314 struct inode *inode = VFS_I(ip);
1315 unsigned int xflags = xfs_ip2xflags(ip);
1316 unsigned int flags = 0;
1317
1318 ASSERT(!(IS_DAX(inode) && init));
1319
1320 if (xflags & FS_XFLAG_IMMUTABLE)
1321 flags |= S_IMMUTABLE;
1322 if (xflags & FS_XFLAG_APPEND)
1323 flags |= S_APPEND;
1324 if (xflags & FS_XFLAG_SYNC)
1325 flags |= S_SYNC;
1326 if (xflags & FS_XFLAG_NOATIME)
1327 flags |= S_NOATIME;
1328 if (init && xfs_inode_should_enable_dax(ip))
1329 flags |= S_DAX;
1330
1331 /*
1332 * S_DAX can only be set during inode initialization and is never set by
1333 * the VFS, so we cannot mask off S_DAX in i_flags.
1334 */
1335 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
1336 inode->i_flags |= flags;
1337 }
1338
1339 /*
1340 * Initialize the Linux inode.
1341 *
1342 * When reading existing inodes from disk this is called directly from xfs_iget,
1343 * when creating a new inode it is called from xfs_init_new_inode after setting
1344 * up the inode. These callers have different criteria for clearing XFS_INEW, so
1345 * leave it up to the caller to deal with unlocking the inode appropriately.
1346 */
1347 void
xfs_setup_inode(struct xfs_inode * ip)1348 xfs_setup_inode(
1349 struct xfs_inode *ip)
1350 {
1351 struct inode *inode = &ip->i_vnode;
1352 gfp_t gfp_mask;
1353 bool is_meta = xfs_is_internal_inode(ip);
1354
1355 inode->i_ino = ip->i_ino;
1356 inode->i_state |= I_NEW;
1357
1358 inode_sb_list_add(inode);
1359 /* make the inode look hashed for the writeback code */
1360 inode_fake_hash(inode);
1361
1362 i_size_write(inode, ip->i_disk_size);
1363 xfs_diflags_to_iflags(ip, true);
1364
1365 /*
1366 * Mark our metadata files as private so that LSMs and the ACL code
1367 * don't try to add their own metadata or reason about these files,
1368 * and users cannot ever obtain file handles to them.
1369 */
1370 if (is_meta) {
1371 inode->i_flags |= S_PRIVATE;
1372 inode->i_opflags &= ~IOP_XATTR;
1373 }
1374
1375 if (S_ISDIR(inode->i_mode)) {
1376 /*
1377 * We set the i_rwsem class here to avoid potential races with
1378 * lockdep_annotate_inode_mutex_key() reinitialising the lock
1379 * after a filehandle lookup has already found the inode in
1380 * cache before it has been unlocked via unlock_new_inode().
1381 */
1382 lockdep_set_class(&inode->i_rwsem,
1383 &inode->i_sb->s_type->i_mutex_dir_key);
1384 lockdep_set_class(&ip->i_lock, &xfs_dir_ilock_class);
1385 } else {
1386 lockdep_set_class(&ip->i_lock, &xfs_nondir_ilock_class);
1387 }
1388
1389 /*
1390 * Ensure all page cache allocations are done from GFP_NOFS context to
1391 * prevent direct reclaim recursion back into the filesystem and blowing
1392 * stacks or deadlocking.
1393 */
1394 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1395 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1396
1397 /*
1398 * For real-time inodes update the stable write flags to that of the RT
1399 * device instead of the data device.
1400 */
1401 if (S_ISREG(inode->i_mode) && XFS_IS_REALTIME_INODE(ip))
1402 xfs_update_stable_writes(ip);
1403
1404 /*
1405 * If there is no attribute fork no ACL can exist on this inode,
1406 * and it can't have any file capabilities attached to it either.
1407 */
1408 if (!xfs_inode_has_attr_fork(ip)) {
1409 inode_has_no_xattr(inode);
1410 cache_no_acl(inode);
1411 }
1412 }
1413
1414 void
xfs_setup_iops(struct xfs_inode * ip)1415 xfs_setup_iops(
1416 struct xfs_inode *ip)
1417 {
1418 struct inode *inode = &ip->i_vnode;
1419
1420 switch (inode->i_mode & S_IFMT) {
1421 case S_IFREG:
1422 inode->i_op = &xfs_inode_operations;
1423 inode->i_fop = &xfs_file_operations;
1424 if (IS_DAX(inode))
1425 inode->i_mapping->a_ops = &xfs_dax_aops;
1426 else
1427 inode->i_mapping->a_ops = &xfs_address_space_operations;
1428 break;
1429 case S_IFDIR:
1430 if (xfs_has_asciici(XFS_M(inode->i_sb)))
1431 inode->i_op = &xfs_dir_ci_inode_operations;
1432 else
1433 inode->i_op = &xfs_dir_inode_operations;
1434 inode->i_fop = &xfs_dir_file_operations;
1435 break;
1436 case S_IFLNK:
1437 inode->i_op = &xfs_symlink_inode_operations;
1438 break;
1439 default:
1440 inode->i_op = &xfs_inode_operations;
1441 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1442 break;
1443 }
1444 }
1445