1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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"
15 #include "xfs_quota.h"
17 #include "xfs_trans.h"
18 #include "xfs_trace.h"
19 #include "xfs_icache.h"
20 #include "xfs_symlink.h"
22 #include "xfs_iomap.h"
23 #include "xfs_error.h"
24 #include "xfs_ioctl.h"
26 #include <linux/posix_acl.h>
27 #include <linux/security.h>
28 #include <linux/iversion.h>
29 #include <linux/fiemap.h>
32 * Directories have different lock order w.r.t. mmap_lock compared to regular
33 * files. This is due to readdir potentially triggering page faults on a user
34 * buffer inside filldir(), and this happens with the ilock on the directory
35 * held. For regular files, the lock order is the other way around - the
36 * mmap_lock is taken during the page fault, and then we lock the ilock to do
37 * block mapping. Hence we need a different class for the directory ilock so
38 * that lockdep can tell them apart.
40 static struct lock_class_key xfs_nondir_ilock_class;
41 static struct lock_class_key xfs_dir_ilock_class;
46 const struct xattr *xattr_array,
49 const struct xattr *xattr;
50 struct xfs_inode *ip = XFS_I(inode);
53 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
54 struct xfs_da_args args = {
56 .attr_filter = XFS_ATTR_SECURE,
58 .namelen = strlen(xattr->name),
59 .value = xattr->value,
60 .valuelen = xattr->value_len,
62 error = xfs_attr_set(&args);
70 * Hook in SELinux. This is not quite correct yet, what we really need
71 * here (as we do for default ACLs) is a mechanism by which creation of
72 * these attrs can be journalled at inode creation time (along with the
73 * inode, of course, such that log replay can't cause these to be lost).
80 const struct qstr *qstr)
82 return security_inode_init_security(inode, dir, qstr,
83 &xfs_initxattrs, NULL);
88 struct xfs_name *namep,
89 struct dentry *dentry)
91 namep->name = dentry->d_name.name;
92 namep->len = dentry->d_name.len;
93 namep->type = XFS_DIR3_FT_UNKNOWN;
97 xfs_dentry_mode_to_name(
98 struct xfs_name *namep,
99 struct dentry *dentry,
102 namep->name = dentry->d_name.name;
103 namep->len = dentry->d_name.len;
104 namep->type = xfs_mode_to_ftype(mode);
106 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
107 return -EFSCORRUPTED;
116 struct dentry *dentry)
118 struct xfs_name teardown;
121 * If we can't add the ACL or we fail in
122 * xfs_init_security we must back out.
123 * ENOSPC can hit here, among other things.
125 xfs_dentry_to_name(&teardown, dentry);
127 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
132 struct user_namespace *mnt_userns,
134 struct dentry *dentry,
137 bool tmpfile) /* unnamed file */
140 struct xfs_inode *ip = NULL;
141 struct posix_acl *default_acl, *acl;
142 struct xfs_name name;
146 * Irix uses Missed'em'V split, but doesn't want to see
147 * the upper 5 bits of (14bit) major.
149 if (S_ISCHR(mode) || S_ISBLK(mode)) {
150 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
156 error = posix_acl_create(dir, &mode, &default_acl, &acl);
160 /* Verify mode is valid also for tmpfile case */
161 error = xfs_dentry_mode_to_name(&name, dentry, mode);
166 error = xfs_create(mnt_userns, XFS_I(dir), &name, mode, rdev,
169 error = xfs_create_tmpfile(mnt_userns, XFS_I(dir), mode, &ip);
176 error = xfs_init_security(inode, dir, &dentry->d_name);
178 goto out_cleanup_inode;
180 #ifdef CONFIG_XFS_POSIX_ACL
182 error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
184 goto out_cleanup_inode;
187 error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
189 goto out_cleanup_inode;
197 * The VFS requires that any inode fed to d_tmpfile must have
198 * nlink == 1 so that it can decrement the nlink in d_tmpfile.
199 * However, we created the temp file with nlink == 0 because
200 * we're not allowed to put an inode with nlink > 0 on the
201 * unlinked list. Therefore we have to set nlink to 1 so that
202 * d_tmpfile can immediately set it back to zero.
205 d_tmpfile(dentry, inode);
207 d_instantiate(dentry, inode);
209 xfs_finish_inode_setup(ip);
212 posix_acl_release(default_acl);
213 posix_acl_release(acl);
217 xfs_finish_inode_setup(ip);
219 xfs_cleanup_inode(dir, inode, dentry);
226 struct user_namespace *mnt_userns,
228 struct dentry *dentry,
232 return xfs_generic_create(mnt_userns, dir, dentry, mode, rdev, false);
237 struct user_namespace *mnt_userns,
239 struct dentry *dentry,
243 return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, false);
248 struct user_namespace *mnt_userns,
250 struct dentry *dentry,
253 return xfs_generic_create(mnt_userns, dir, dentry, mode | S_IFDIR, 0,
257 STATIC struct dentry *
260 struct dentry *dentry,
264 struct xfs_inode *cip;
265 struct xfs_name name;
268 if (dentry->d_name.len >= MAXNAMELEN)
269 return ERR_PTR(-ENAMETOOLONG);
271 xfs_dentry_to_name(&name, dentry);
272 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
275 else if (likely(error == -ENOENT))
278 inode = ERR_PTR(error);
279 return d_splice_alias(inode, dentry);
282 STATIC struct dentry *
285 struct dentry *dentry,
288 struct xfs_inode *ip;
289 struct xfs_name xname;
290 struct xfs_name ci_name;
294 if (dentry->d_name.len >= MAXNAMELEN)
295 return ERR_PTR(-ENAMETOOLONG);
297 xfs_dentry_to_name(&xname, dentry);
298 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
299 if (unlikely(error)) {
300 if (unlikely(error != -ENOENT))
301 return ERR_PTR(error);
303 * call d_add(dentry, NULL) here when d_drop_negative_children
304 * is called in xfs_vn_mknod (ie. allow negative dentries
305 * with CI filesystems).
310 /* if exact match, just splice and exit */
312 return d_splice_alias(VFS_I(ip), dentry);
314 /* else case-insensitive match... */
315 dname.name = ci_name.name;
316 dname.len = ci_name.len;
317 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
318 kmem_free(ci_name.name);
324 struct dentry *old_dentry,
326 struct dentry *dentry)
328 struct inode *inode = d_inode(old_dentry);
329 struct xfs_name name;
332 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
336 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
341 d_instantiate(dentry, inode);
348 struct dentry *dentry)
350 struct xfs_name name;
353 xfs_dentry_to_name(&name, dentry);
355 error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
360 * With unlink, the VFS makes the dentry "negative": no inode,
361 * but still hashed. This is incompatible with case-insensitive
362 * mode, so invalidate (unhash) the dentry in CI-mode.
364 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
365 d_invalidate(dentry);
371 struct user_namespace *mnt_userns,
373 struct dentry *dentry,
377 struct xfs_inode *cip = NULL;
378 struct xfs_name name;
383 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
384 error = xfs_dentry_mode_to_name(&name, dentry, mode);
388 error = xfs_symlink(mnt_userns, XFS_I(dir), &name, symname, mode, &cip);
394 error = xfs_init_security(inode, dir, &dentry->d_name);
396 goto out_cleanup_inode;
400 d_instantiate(dentry, inode);
401 xfs_finish_inode_setup(cip);
405 xfs_finish_inode_setup(cip);
406 xfs_cleanup_inode(dir, inode, dentry);
414 struct user_namespace *mnt_userns,
416 struct dentry *odentry,
418 struct dentry *ndentry,
421 struct inode *new_inode = d_inode(ndentry);
424 struct xfs_name oname;
425 struct xfs_name nname;
427 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
430 /* if we are exchanging files, we need to set i_mode of both files */
431 if (flags & RENAME_EXCHANGE)
432 omode = d_inode(ndentry)->i_mode;
434 error = xfs_dentry_mode_to_name(&oname, odentry, omode);
435 if (omode && unlikely(error))
438 error = xfs_dentry_mode_to_name(&nname, ndentry,
439 d_inode(odentry)->i_mode);
443 return xfs_rename(mnt_userns, XFS_I(odir), &oname,
444 XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
445 new_inode ? XFS_I(new_inode) : NULL, flags);
449 * careful here - this function can get called recursively, so
450 * we need to be very careful about how much stack we use.
451 * uio is kmalloced for this reason...
455 struct dentry *dentry,
457 struct delayed_call *done)
463 return ERR_PTR(-ECHILD);
465 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
469 error = xfs_readlink(XFS_I(d_inode(dentry)), link);
473 set_delayed_call(done, kfree_link, link);
479 return ERR_PTR(error);
483 xfs_vn_get_link_inline(
484 struct dentry *dentry,
486 struct delayed_call *done)
488 struct xfs_inode *ip = XFS_I(inode);
491 ASSERT(ip->i_df.if_flags & XFS_IFINLINE);
494 * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED if
497 link = ip->i_df.if_u1.if_data;
498 if (XFS_IS_CORRUPT(ip->i_mount, !link))
499 return ERR_PTR(-EFSCORRUPTED);
505 struct xfs_inode *ip)
507 struct xfs_mount *mp = ip->i_mount;
510 * If the file blocks are being allocated from a realtime volume, then
511 * always return the realtime extent size.
513 if (XFS_IS_REALTIME_INODE(ip))
514 return xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
517 * Allow large block sizes to be reported to userspace programs if the
518 * "largeio" mount option is used.
520 * If compatibility mode is specified, simply return the basic unit of
521 * caching so that we don't get inefficient read/modify/write I/O from
522 * user apps. Otherwise....
524 * If the underlying volume is a stripe, then return the stripe width in
525 * bytes as the recommended I/O size. It is not a stripe and we've set a
526 * default buffered I/O size, return that, otherwise return the compat
529 if (mp->m_flags & XFS_MOUNT_LARGEIO) {
531 return mp->m_swidth << mp->m_sb.sb_blocklog;
532 if (mp->m_flags & XFS_MOUNT_ALLOCSIZE)
533 return 1U << mp->m_allocsize_log;
541 struct user_namespace *mnt_userns,
542 const struct path *path,
545 unsigned int query_flags)
547 struct inode *inode = d_inode(path->dentry);
548 struct xfs_inode *ip = XFS_I(inode);
549 struct xfs_mount *mp = ip->i_mount;
551 trace_xfs_getattr(ip);
553 if (XFS_FORCED_SHUTDOWN(mp))
556 stat->size = XFS_ISIZE(ip);
557 stat->dev = inode->i_sb->s_dev;
558 stat->mode = inode->i_mode;
559 stat->nlink = inode->i_nlink;
560 stat->uid = i_uid_into_mnt(mnt_userns, inode);
561 stat->gid = i_gid_into_mnt(mnt_userns, inode);
562 stat->ino = ip->i_ino;
563 stat->atime = inode->i_atime;
564 stat->mtime = inode->i_mtime;
565 stat->ctime = inode->i_ctime;
567 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
569 if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
570 if (request_mask & STATX_BTIME) {
571 stat->result_mask |= STATX_BTIME;
572 stat->btime = ip->i_d.di_crtime;
577 * Note: If you add another clause to set an attribute flag, please
578 * update attributes_mask below.
580 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
581 stat->attributes |= STATX_ATTR_IMMUTABLE;
582 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
583 stat->attributes |= STATX_ATTR_APPEND;
584 if (ip->i_d.di_flags & XFS_DIFLAG_NODUMP)
585 stat->attributes |= STATX_ATTR_NODUMP;
587 stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
591 switch (inode->i_mode & S_IFMT) {
594 stat->blksize = BLKDEV_IOSIZE;
595 stat->rdev = inode->i_rdev;
598 stat->blksize = xfs_stat_blksize(ip);
608 struct xfs_inode *ip,
611 struct inode *inode = VFS_I(ip);
612 umode_t mode = iattr->ia_mode;
614 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
616 inode->i_mode &= S_IFMT;
617 inode->i_mode |= mode & ~S_IFMT;
622 struct xfs_inode *ip,
625 struct inode *inode = VFS_I(ip);
627 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
629 if (iattr->ia_valid & ATTR_ATIME)
630 inode->i_atime = iattr->ia_atime;
631 if (iattr->ia_valid & ATTR_CTIME)
632 inode->i_ctime = iattr->ia_ctime;
633 if (iattr->ia_valid & ATTR_MTIME)
634 inode->i_mtime = iattr->ia_mtime;
639 struct user_namespace *mnt_userns,
640 struct dentry *dentry,
643 struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
645 if (mp->m_flags & XFS_MOUNT_RDONLY)
648 if (XFS_FORCED_SHUTDOWN(mp))
651 return setattr_prepare(mnt_userns, dentry, iattr);
655 * Set non-size attributes of an inode.
657 * Caution: The caller of this function is responsible for calling
658 * setattr_prepare() or otherwise verifying the change is fine.
662 struct user_namespace *mnt_userns,
663 struct xfs_inode *ip,
666 xfs_mount_t *mp = ip->i_mount;
667 struct inode *inode = VFS_I(ip);
668 int mask = iattr->ia_valid;
671 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
672 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
673 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
674 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
676 ASSERT((mask & ATTR_SIZE) == 0);
679 * If disk quotas is on, we make sure that the dquots do exist on disk,
680 * before we start any other transactions. Trying to do this later
681 * is messy. We don't care to take a readlock to look at the ids
682 * in inode here, because we can't hold it across the trans_reserve.
683 * If the IDs do change before we take the ilock, we're covered
684 * because the i_*dquot fields will get updated anyway.
686 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
689 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
691 qflags |= XFS_QMOPT_UQUOTA;
695 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
697 qflags |= XFS_QMOPT_GQUOTA;
703 * We take a reference when we initialize udqp and gdqp,
704 * so it is important that we never blindly double trip on
705 * the same variable. See xfs_create() for an example.
707 ASSERT(udqp == NULL);
708 ASSERT(gdqp == NULL);
709 error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
710 qflags, &udqp, &gdqp, NULL);
715 error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
716 capable(CAP_FOWNER), &tp);
721 * Change file ownership. Must be the owner or privileged.
723 if (mask & (ATTR_UID|ATTR_GID)) {
725 * These IDs could have changed since we last looked at them.
726 * But, we're assured that if the ownership did change
727 * while we didn't have the inode locked, inode's dquot(s)
728 * would have changed also.
732 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
733 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
736 * CAP_FSETID overrides the following restrictions:
738 * The set-user-ID and set-group-ID bits of a file will be
739 * cleared upon successful return from chown()
741 if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
742 !capable(CAP_FSETID))
743 inode->i_mode &= ~(S_ISUID|S_ISGID);
746 * Change the ownerships and register quota modifications
747 * in the transaction.
749 if (!uid_eq(iuid, uid)) {
750 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
751 ASSERT(mask & ATTR_UID);
753 olddquot1 = xfs_qm_vop_chown(tp, ip,
754 &ip->i_udquot, udqp);
758 if (!gid_eq(igid, gid)) {
759 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
760 ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
761 !XFS_IS_PQUOTA_ON(mp));
762 ASSERT(mask & ATTR_GID);
764 olddquot2 = xfs_qm_vop_chown(tp, ip,
765 &ip->i_gdquot, gdqp);
771 if (mask & ATTR_MODE)
772 xfs_setattr_mode(ip, iattr);
773 if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
774 xfs_setattr_time(ip, iattr);
776 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
778 XFS_STATS_INC(mp, xs_ig_attrchg);
780 if (mp->m_flags & XFS_MOUNT_WSYNC)
781 xfs_trans_set_sync(tp);
782 error = xfs_trans_commit(tp);
785 * Release any dquot(s) the inode had kept before chown.
787 xfs_qm_dqrele(olddquot1);
788 xfs_qm_dqrele(olddquot2);
796 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
797 * update. We could avoid this with linked transactions
798 * and passing down the transaction pointer all the way
799 * to attr_set. No previous user of the generic
800 * Posix ACL code seems to care about this issue either.
802 if (mask & ATTR_MODE) {
803 error = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
817 * Truncate file. Must have write permission and not be a directory.
819 * Caution: The caller of this function is responsible for calling
820 * setattr_prepare() or otherwise verifying the change is fine.
824 struct user_namespace *mnt_userns,
825 struct xfs_inode *ip,
828 struct xfs_mount *mp = ip->i_mount;
829 struct inode *inode = VFS_I(ip);
830 xfs_off_t oldsize, newsize;
831 struct xfs_trans *tp;
834 bool did_zeroing = false;
836 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
837 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
838 ASSERT(S_ISREG(inode->i_mode));
839 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
840 ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
842 oldsize = inode->i_size;
843 newsize = iattr->ia_size;
846 * Short circuit the truncate case for zero length files.
848 if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
849 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
853 * Use the regular setattr path to update the timestamps.
855 iattr->ia_valid &= ~ATTR_SIZE;
856 return xfs_setattr_nonsize(mnt_userns, ip, iattr);
860 * Make sure that the dquots are attached to the inode.
862 error = xfs_qm_dqattach(ip);
867 * Wait for all direct I/O to complete.
869 inode_dio_wait(inode);
872 * File data changes must be complete before we start the transaction to
873 * modify the inode. This needs to be done before joining the inode to
874 * the transaction because the inode cannot be unlocked once it is a
875 * part of the transaction.
877 * Start with zeroing any data beyond EOF that we may expose on file
878 * extension, or zeroing out the rest of the block on a downward
881 if (newsize > oldsize) {
882 trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
883 error = iomap_zero_range(inode, oldsize, newsize - oldsize,
884 &did_zeroing, &xfs_buffered_write_iomap_ops);
887 * iomap won't detect a dirty page over an unwritten block (or a
888 * cow block over a hole) and subsequently skips zeroing the
889 * newly post-EOF portion of the page. Flush the new EOF to
890 * convert the block before the pagecache truncate.
892 error = filemap_write_and_wait_range(inode->i_mapping, newsize,
896 error = iomap_truncate_page(inode, newsize, &did_zeroing,
897 &xfs_buffered_write_iomap_ops);
904 * We've already locked out new page faults, so now we can safely remove
905 * pages from the page cache knowing they won't get refaulted until we
906 * drop the XFS_MMAP_EXCL lock after the extent manipulations are
907 * complete. The truncate_setsize() call also cleans partial EOF page
908 * PTEs on extending truncates and hence ensures sub-page block size
909 * filesystems are correctly handled, too.
911 * We have to do all the page cache truncate work outside the
912 * transaction context as the "lock" order is page lock->log space
913 * reservation as defined by extent allocation in the writeback path.
914 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
915 * having already truncated the in-memory version of the file (i.e. made
916 * user visible changes). There's not much we can do about this, except
917 * to hope that the caller sees ENOMEM and retries the truncate
920 * And we update in-core i_size and truncate page cache beyond newsize
921 * before writeback the [di_size, newsize] range, so we're guaranteed
922 * not to write stale data past the new EOF on truncate down.
924 truncate_setsize(inode, newsize);
927 * We are going to log the inode size change in this transaction so
928 * any previous writes that are beyond the on disk EOF and the new
929 * EOF that have not been written out need to be written here. If we
930 * do not write the data out, we expose ourselves to the null files
931 * problem. Note that this includes any block zeroing we did above;
932 * otherwise those blocks may not be zeroed after a crash.
935 (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
936 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
937 ip->i_d.di_size, newsize - 1);
942 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
946 lock_flags |= XFS_ILOCK_EXCL;
947 xfs_ilock(ip, XFS_ILOCK_EXCL);
948 xfs_trans_ijoin(tp, ip, 0);
951 * Only change the c/mtime if we are changing the size or we are
952 * explicitly asked to change it. This handles the semantic difference
953 * between truncate() and ftruncate() as implemented in the VFS.
955 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
956 * special case where we need to update the times despite not having
957 * these flags set. For all other operations the VFS set these flags
958 * explicitly if it wants a timestamp update.
960 if (newsize != oldsize &&
961 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
962 iattr->ia_ctime = iattr->ia_mtime =
964 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
968 * The first thing we do is set the size to new_size permanently on
969 * disk. This way we don't have to worry about anyone ever being able
970 * to look at the data being freed even in the face of a crash.
971 * What we're getting around here is the case where we free a block, it
972 * is allocated to another file, it is written to, and then we crash.
973 * If the new data gets written to the file but the log buffers
974 * containing the free and reallocation don't, then we'd end up with
975 * garbage in the blocks being freed. As long as we make the new size
976 * permanent before actually freeing any blocks it doesn't matter if
977 * they get written to.
979 ip->i_d.di_size = newsize;
980 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
982 if (newsize <= oldsize) {
983 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
985 goto out_trans_cancel;
988 * Truncated "down", so we're removing references to old data
989 * here - if we delay flushing for a long time, we expose
990 * ourselves unduly to the notorious NULL files problem. So,
991 * we mark this inode and flush it when the file is closed,
992 * and do not wait the usual (long) time for writeout.
994 xfs_iflags_set(ip, XFS_ITRUNCATED);
996 /* A truncate down always removes post-EOF blocks. */
997 xfs_inode_clear_eofblocks_tag(ip);
1000 if (iattr->ia_valid & ATTR_MODE)
1001 xfs_setattr_mode(ip, iattr);
1002 if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
1003 xfs_setattr_time(ip, iattr);
1005 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1007 XFS_STATS_INC(mp, xs_ig_attrchg);
1009 if (mp->m_flags & XFS_MOUNT_WSYNC)
1010 xfs_trans_set_sync(tp);
1012 error = xfs_trans_commit(tp);
1015 xfs_iunlock(ip, lock_flags);
1019 xfs_trans_cancel(tp);
1024 xfs_vn_setattr_size(
1025 struct user_namespace *mnt_userns,
1026 struct dentry *dentry,
1027 struct iattr *iattr)
1029 struct xfs_inode *ip = XFS_I(d_inode(dentry));
1032 trace_xfs_setattr(ip);
1034 error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
1037 return xfs_setattr_size(mnt_userns, ip, iattr);
1042 struct user_namespace *mnt_userns,
1043 struct dentry *dentry,
1044 struct iattr *iattr)
1046 struct inode *inode = d_inode(dentry);
1047 struct xfs_inode *ip = XFS_I(inode);
1050 if (iattr->ia_valid & ATTR_SIZE) {
1053 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
1054 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
1056 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
1058 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1062 error = xfs_vn_setattr_size(mnt_userns, dentry, iattr);
1063 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1065 trace_xfs_setattr(ip);
1067 error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
1069 error = xfs_setattr_nonsize(mnt_userns, ip, iattr);
1077 struct inode *inode,
1078 struct timespec64 *now,
1081 struct xfs_inode *ip = XFS_I(inode);
1082 struct xfs_mount *mp = ip->i_mount;
1083 int log_flags = XFS_ILOG_TIMESTAMP;
1084 struct xfs_trans *tp;
1087 trace_xfs_update_time(ip);
1089 if (inode->i_sb->s_flags & SB_LAZYTIME) {
1090 if (!((flags & S_VERSION) &&
1091 inode_maybe_inc_iversion(inode, false)))
1092 return generic_update_time(inode, now, flags);
1094 /* Capture the iversion update that just occurred */
1095 log_flags |= XFS_ILOG_CORE;
1098 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1102 xfs_ilock(ip, XFS_ILOCK_EXCL);
1103 if (flags & S_CTIME)
1104 inode->i_ctime = *now;
1105 if (flags & S_MTIME)
1106 inode->i_mtime = *now;
1107 if (flags & S_ATIME)
1108 inode->i_atime = *now;
1110 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1111 xfs_trans_log_inode(tp, ip, log_flags);
1112 return xfs_trans_commit(tp);
1117 struct inode *inode,
1118 struct fiemap_extent_info *fieinfo,
1124 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
1125 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1126 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
1127 error = iomap_fiemap(inode, fieinfo, start, length,
1128 &xfs_xattr_iomap_ops);
1130 error = iomap_fiemap(inode, fieinfo, start, length,
1131 &xfs_read_iomap_ops);
1133 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
1140 struct user_namespace *mnt_userns,
1142 struct dentry *dentry,
1145 return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, true);
1148 static const struct inode_operations xfs_inode_operations = {
1149 .get_acl = xfs_get_acl,
1150 .set_acl = xfs_set_acl,
1151 .getattr = xfs_vn_getattr,
1152 .setattr = xfs_vn_setattr,
1153 .listxattr = xfs_vn_listxattr,
1154 .fiemap = xfs_vn_fiemap,
1155 .update_time = xfs_vn_update_time,
1156 .fileattr_get = xfs_fileattr_get,
1157 .fileattr_set = xfs_fileattr_set,
1160 static const struct inode_operations xfs_dir_inode_operations = {
1161 .create = xfs_vn_create,
1162 .lookup = xfs_vn_lookup,
1163 .link = xfs_vn_link,
1164 .unlink = xfs_vn_unlink,
1165 .symlink = xfs_vn_symlink,
1166 .mkdir = xfs_vn_mkdir,
1168 * Yes, XFS uses the same method for rmdir and unlink.
1170 * There are some subtile differences deeper in the code,
1171 * but we use S_ISDIR to check for those.
1173 .rmdir = xfs_vn_unlink,
1174 .mknod = xfs_vn_mknod,
1175 .rename = xfs_vn_rename,
1176 .get_acl = xfs_get_acl,
1177 .set_acl = xfs_set_acl,
1178 .getattr = xfs_vn_getattr,
1179 .setattr = xfs_vn_setattr,
1180 .listxattr = xfs_vn_listxattr,
1181 .update_time = xfs_vn_update_time,
1182 .tmpfile = xfs_vn_tmpfile,
1183 .fileattr_get = xfs_fileattr_get,
1184 .fileattr_set = xfs_fileattr_set,
1187 static const struct inode_operations xfs_dir_ci_inode_operations = {
1188 .create = xfs_vn_create,
1189 .lookup = xfs_vn_ci_lookup,
1190 .link = xfs_vn_link,
1191 .unlink = xfs_vn_unlink,
1192 .symlink = xfs_vn_symlink,
1193 .mkdir = xfs_vn_mkdir,
1195 * Yes, XFS uses the same method for rmdir and unlink.
1197 * There are some subtile differences deeper in the code,
1198 * but we use S_ISDIR to check for those.
1200 .rmdir = xfs_vn_unlink,
1201 .mknod = xfs_vn_mknod,
1202 .rename = xfs_vn_rename,
1203 .get_acl = xfs_get_acl,
1204 .set_acl = xfs_set_acl,
1205 .getattr = xfs_vn_getattr,
1206 .setattr = xfs_vn_setattr,
1207 .listxattr = xfs_vn_listxattr,
1208 .update_time = xfs_vn_update_time,
1209 .tmpfile = xfs_vn_tmpfile,
1210 .fileattr_get = xfs_fileattr_get,
1211 .fileattr_set = xfs_fileattr_set,
1214 static const struct inode_operations xfs_symlink_inode_operations = {
1215 .get_link = xfs_vn_get_link,
1216 .getattr = xfs_vn_getattr,
1217 .setattr = xfs_vn_setattr,
1218 .listxattr = xfs_vn_listxattr,
1219 .update_time = xfs_vn_update_time,
1222 static const struct inode_operations xfs_inline_symlink_inode_operations = {
1223 .get_link = xfs_vn_get_link_inline,
1224 .getattr = xfs_vn_getattr,
1225 .setattr = xfs_vn_setattr,
1226 .listxattr = xfs_vn_listxattr,
1227 .update_time = xfs_vn_update_time,
1230 /* Figure out if this file actually supports DAX. */
1232 xfs_inode_supports_dax(
1233 struct xfs_inode *ip)
1235 struct xfs_mount *mp = ip->i_mount;
1237 /* Only supported on regular files. */
1238 if (!S_ISREG(VFS_I(ip)->i_mode))
1241 /* Only supported on non-reflinked files. */
1242 if (xfs_is_reflink_inode(ip))
1245 /* Block size must match page size */
1246 if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1249 /* Device has to support DAX too. */
1250 return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
1254 xfs_inode_should_enable_dax(
1255 struct xfs_inode *ip)
1257 if (!IS_ENABLED(CONFIG_FS_DAX))
1259 if (ip->i_mount->m_flags & XFS_MOUNT_DAX_NEVER)
1261 if (!xfs_inode_supports_dax(ip))
1263 if (ip->i_mount->m_flags & XFS_MOUNT_DAX_ALWAYS)
1265 if (ip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
1271 xfs_diflags_to_iflags(
1272 struct xfs_inode *ip,
1275 struct inode *inode = VFS_I(ip);
1276 unsigned int xflags = xfs_ip2xflags(ip);
1277 unsigned int flags = 0;
1279 ASSERT(!(IS_DAX(inode) && init));
1281 if (xflags & FS_XFLAG_IMMUTABLE)
1282 flags |= S_IMMUTABLE;
1283 if (xflags & FS_XFLAG_APPEND)
1285 if (xflags & FS_XFLAG_SYNC)
1287 if (xflags & FS_XFLAG_NOATIME)
1289 if (init && xfs_inode_should_enable_dax(ip))
1293 * S_DAX can only be set during inode initialization and is never set by
1294 * the VFS, so we cannot mask off S_DAX in i_flags.
1296 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
1297 inode->i_flags |= flags;
1301 * Initialize the Linux inode.
1303 * When reading existing inodes from disk this is called directly from xfs_iget,
1304 * when creating a new inode it is called from xfs_ialloc after setting up the
1305 * inode. These callers have different criteria for clearing XFS_INEW, so leave
1306 * it up to the caller to deal with unlocking the inode appropriately.
1310 struct xfs_inode *ip)
1312 struct inode *inode = &ip->i_vnode;
1315 inode->i_ino = ip->i_ino;
1316 inode->i_state = I_NEW;
1318 inode_sb_list_add(inode);
1319 /* make the inode look hashed for the writeback code */
1320 inode_fake_hash(inode);
1322 i_size_write(inode, ip->i_d.di_size);
1323 xfs_diflags_to_iflags(ip, true);
1325 if (S_ISDIR(inode->i_mode)) {
1327 * We set the i_rwsem class here to avoid potential races with
1328 * lockdep_annotate_inode_mutex_key() reinitialising the lock
1329 * after a filehandle lookup has already found the inode in
1330 * cache before it has been unlocked via unlock_new_inode().
1332 lockdep_set_class(&inode->i_rwsem,
1333 &inode->i_sb->s_type->i_mutex_dir_key);
1334 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
1336 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
1340 * Ensure all page cache allocations are done from GFP_NOFS context to
1341 * prevent direct reclaim recursion back into the filesystem and blowing
1342 * stacks or deadlocking.
1344 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1345 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1348 * If there is no attribute fork no ACL can exist on this inode,
1349 * and it can't have any file capabilities attached to it either.
1351 if (!XFS_IFORK_Q(ip)) {
1352 inode_has_no_xattr(inode);
1353 cache_no_acl(inode);
1359 struct xfs_inode *ip)
1361 struct inode *inode = &ip->i_vnode;
1363 switch (inode->i_mode & S_IFMT) {
1365 inode->i_op = &xfs_inode_operations;
1366 inode->i_fop = &xfs_file_operations;
1368 inode->i_mapping->a_ops = &xfs_dax_aops;
1370 inode->i_mapping->a_ops = &xfs_address_space_operations;
1373 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1374 inode->i_op = &xfs_dir_ci_inode_operations;
1376 inode->i_op = &xfs_dir_inode_operations;
1377 inode->i_fop = &xfs_dir_file_operations;
1380 if (ip->i_df.if_flags & XFS_IFINLINE)
1381 inode->i_op = &xfs_inline_symlink_inode_operations;
1383 inode->i_op = &xfs_symlink_inode_operations;
1386 inode->i_op = &xfs_inode_operations;
1387 init_special_inode(inode, inode->i_mode, inode->i_rdev);