10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
2c59d87c4SChristoph Hellwig /*
3c59d87c4SChristoph Hellwig * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4c59d87c4SChristoph Hellwig * All Rights Reserved.
5c59d87c4SChristoph Hellwig */
6c59d87c4SChristoph Hellwig #include "xfs.h"
7c59d87c4SChristoph Hellwig #include "xfs_fs.h"
870a9883cSDave Chinner #include "xfs_shared.h"
9239880efSDave Chinner #include "xfs_format.h"
10239880efSDave Chinner #include "xfs_log_format.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
12c59d87c4SChristoph Hellwig #include "xfs_mount.h"
13c59d87c4SChristoph Hellwig #include "xfs_inode.h"
14239880efSDave Chinner #include "xfs_acl.h"
15239880efSDave Chinner #include "xfs_quota.h"
16fd920008SAllison Henderson #include "xfs_da_format.h"
17fd920008SAllison Henderson #include "xfs_da_btree.h"
18c59d87c4SChristoph Hellwig #include "xfs_attr.h"
19239880efSDave Chinner #include "xfs_trans.h"
20c59d87c4SChristoph Hellwig #include "xfs_trace.h"
2127b52867SBrian Foster #include "xfs_icache.h"
22c24b5dfaSDave Chinner #include "xfs_symlink.h"
231b767ee3SDave Chinner #include "xfs_dir2.h"
2468a9f5e7SChristoph Hellwig #include "xfs_iomap.h"
25a5155b87SDarrick J. Wong #include "xfs_error.h"
269fefd5dbSMiklos Szeredi #include "xfs_ioctl.h"
27efc2efebSDarrick J. Wong #include "xfs_xattr.h"
28c59d87c4SChristoph Hellwig
29c59d87c4SChristoph Hellwig #include <linux/posix_acl.h>
30c59d87c4SChristoph Hellwig #include <linux/security.h>
31c3b1b131SChristoph Hellwig #include <linux/iversion.h>
3210c5db28SChristoph Hellwig #include <linux/fiemap.h>
33c59d87c4SChristoph Hellwig
3493a8614eSDave Chinner /*
35c1e8d7c6SMichel Lespinasse * Directories have different lock order w.r.t. mmap_lock compared to regular
3693a8614eSDave Chinner * files. This is due to readdir potentially triggering page faults on a user
3793a8614eSDave Chinner * buffer inside filldir(), and this happens with the ilock on the directory
3893a8614eSDave Chinner * held. For regular files, the lock order is the other way around - the
39c1e8d7c6SMichel Lespinasse * mmap_lock is taken during the page fault, and then we lock the ilock to do
4093a8614eSDave Chinner * block mapping. Hence we need a different class for the directory ilock so
4193a8614eSDave Chinner * that lockdep can tell them apart.
4293a8614eSDave Chinner */
4393a8614eSDave Chinner static struct lock_class_key xfs_nondir_ilock_class;
4493a8614eSDave Chinner static struct lock_class_key xfs_dir_ilock_class;
4593a8614eSDave Chinner
468d2a5e6eSDave Chinner static int
xfs_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * fs_info)478d2a5e6eSDave Chinner xfs_initxattrs(
488d2a5e6eSDave Chinner struct inode *inode,
498d2a5e6eSDave Chinner const struct xattr *xattr_array,
5036b8d186SLinus Torvalds void *fs_info)
5136b8d186SLinus Torvalds {
5236b8d186SLinus Torvalds const struct xattr *xattr;
5336b8d186SLinus Torvalds struct xfs_inode *ip = XFS_I(inode);
5436b8d186SLinus Torvalds int error = 0;
5536b8d186SLinus Torvalds
5636b8d186SLinus Torvalds for (xattr = xattr_array; xattr->name != NULL; xattr++) {
57a2544622SChristoph Hellwig struct xfs_da_args args = {
58a2544622SChristoph Hellwig .dp = ip,
59d5f0f49aSChristoph Hellwig .attr_filter = XFS_ATTR_SECURE,
60a2544622SChristoph Hellwig .name = xattr->name,
61a2544622SChristoph Hellwig .namelen = strlen(xattr->name),
62a2544622SChristoph Hellwig .value = xattr->value,
63a2544622SChristoph Hellwig .valuelen = xattr->value_len,
64a2544622SChristoph Hellwig };
65efc2efebSDarrick J. Wong error = xfs_attr_change(&args);
6636b8d186SLinus Torvalds if (error < 0)
6736b8d186SLinus Torvalds break;
6836b8d186SLinus Torvalds }
6936b8d186SLinus Torvalds return error;
7036b8d186SLinus Torvalds }
7136b8d186SLinus Torvalds
72c59d87c4SChristoph Hellwig /*
73c59d87c4SChristoph Hellwig * Hook in SELinux. This is not quite correct yet, what we really need
74c59d87c4SChristoph Hellwig * here (as we do for default ACLs) is a mechanism by which creation of
75c59d87c4SChristoph Hellwig * these attrs can be journalled at inode creation time (along with the
76c59d87c4SChristoph Hellwig * inode, of course, such that log replay can't cause these to be lost).
77c59d87c4SChristoph Hellwig */
7870b589a3SEric Sandeen int
xfs_inode_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr)7970b589a3SEric Sandeen xfs_inode_init_security(
80c59d87c4SChristoph Hellwig struct inode *inode,
81c59d87c4SChristoph Hellwig struct inode *dir,
82c59d87c4SChristoph Hellwig const struct qstr *qstr)
83c59d87c4SChristoph Hellwig {
842451337dSDave Chinner return security_inode_init_security(inode, dir, qstr,
8536b8d186SLinus Torvalds &xfs_initxattrs, NULL);
86c59d87c4SChristoph Hellwig }
87c59d87c4SChristoph Hellwig
88c59d87c4SChristoph Hellwig static void
xfs_dentry_to_name(struct xfs_name * namep,struct dentry * dentry)89c59d87c4SChristoph Hellwig xfs_dentry_to_name(
90c59d87c4SChristoph Hellwig struct xfs_name *namep,
91fab8eef8SAmir Goldstein struct dentry *dentry)
92fab8eef8SAmir Goldstein {
93fab8eef8SAmir Goldstein namep->name = dentry->d_name.name;
94fab8eef8SAmir Goldstein namep->len = dentry->d_name.len;
95fab8eef8SAmir Goldstein namep->type = XFS_DIR3_FT_UNKNOWN;
96fab8eef8SAmir Goldstein }
97fab8eef8SAmir Goldstein
98fab8eef8SAmir Goldstein static int
xfs_dentry_mode_to_name(struct xfs_name * namep,struct dentry * dentry,int mode)99fab8eef8SAmir Goldstein xfs_dentry_mode_to_name(
100fab8eef8SAmir Goldstein struct xfs_name *namep,
1010cb97766SDave Chinner struct dentry *dentry,
1020cb97766SDave Chinner int mode)
103c59d87c4SChristoph Hellwig {
104c59d87c4SChristoph Hellwig namep->name = dentry->d_name.name;
105c59d87c4SChristoph Hellwig namep->len = dentry->d_name.len;
1061fc4d33fSAmir Goldstein namep->type = xfs_mode_to_ftype(mode);
107fab8eef8SAmir Goldstein
108fab8eef8SAmir Goldstein if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
109fab8eef8SAmir Goldstein return -EFSCORRUPTED;
110fab8eef8SAmir Goldstein
111fab8eef8SAmir Goldstein return 0;
112c59d87c4SChristoph Hellwig }
113c59d87c4SChristoph Hellwig
114c59d87c4SChristoph Hellwig STATIC void
xfs_cleanup_inode(struct inode * dir,struct inode * inode,struct dentry * dentry)115c59d87c4SChristoph Hellwig xfs_cleanup_inode(
116c59d87c4SChristoph Hellwig struct inode *dir,
117c59d87c4SChristoph Hellwig struct inode *inode,
118c59d87c4SChristoph Hellwig struct dentry *dentry)
119c59d87c4SChristoph Hellwig {
120c59d87c4SChristoph Hellwig struct xfs_name teardown;
121c59d87c4SChristoph Hellwig
122c59d87c4SChristoph Hellwig /* Oh, the horror.
123c59d87c4SChristoph Hellwig * If we can't add the ACL or we fail in
12470b589a3SEric Sandeen * xfs_inode_init_security we must back out.
125c59d87c4SChristoph Hellwig * ENOSPC can hit here, among other things.
126c59d87c4SChristoph Hellwig */
127fab8eef8SAmir Goldstein xfs_dentry_to_name(&teardown, dentry);
128c59d87c4SChristoph Hellwig
129c59d87c4SChristoph Hellwig xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
130c59d87c4SChristoph Hellwig }
131c59d87c4SChristoph Hellwig
132e6a688c3SDave Chinner /*
133e6a688c3SDave Chinner * Check to see if we are likely to need an extended attribute to be added to
134e6a688c3SDave Chinner * the inode we are about to allocate. This allows the attribute fork to be
135e6a688c3SDave Chinner * created during the inode allocation, reducing the number of transactions we
136e6a688c3SDave Chinner * need to do in this fast path.
137e6a688c3SDave Chinner *
138e6a688c3SDave Chinner * The security checks are optimistic, but not guaranteed. The two LSMs that
139e6a688c3SDave Chinner * require xattrs to be added here (selinux and smack) are also the only two
140e6a688c3SDave Chinner * LSMs that add a sb->s_security structure to the superblock. Hence if security
141e6a688c3SDave Chinner * is enabled and sb->s_security is set, we have a pretty good idea that we are
142e6a688c3SDave Chinner * going to be asked to add a security xattr immediately after allocating the
143e6a688c3SDave Chinner * xfs inode and instantiating the VFS inode.
144e6a688c3SDave Chinner */
145e6a688c3SDave Chinner static inline bool
xfs_create_need_xattr(struct inode * dir,struct posix_acl * default_acl,struct posix_acl * acl)146e6a688c3SDave Chinner xfs_create_need_xattr(
147e6a688c3SDave Chinner struct inode *dir,
148e6a688c3SDave Chinner struct posix_acl *default_acl,
149e6a688c3SDave Chinner struct posix_acl *acl)
150e6a688c3SDave Chinner {
151e6a688c3SDave Chinner if (acl)
152e6a688c3SDave Chinner return true;
153e6a688c3SDave Chinner if (default_acl)
154e6a688c3SDave Chinner return true;
155e6a688c3SDave Chinner #if IS_ENABLED(CONFIG_SECURITY)
156e6a688c3SDave Chinner if (dir->i_sb->s_security)
157e6a688c3SDave Chinner return true;
158e6a688c3SDave Chinner #endif
159e6a688c3SDave Chinner return false;
160e6a688c3SDave Chinner }
161e6a688c3SDave Chinner
162e6a688c3SDave Chinner
163c59d87c4SChristoph Hellwig STATIC int
xfs_generic_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev,struct file * tmpfile)164d540e43bSBrian Foster xfs_generic_create(
165f2d40141SChristian Brauner struct mnt_idmap *idmap,
166c59d87c4SChristoph Hellwig struct inode *dir,
167c59d87c4SChristoph Hellwig struct dentry *dentry,
1681a67aafbSAl Viro umode_t mode,
169d540e43bSBrian Foster dev_t rdev,
170863f144fSMiklos Szeredi struct file *tmpfile) /* unnamed file */
171c59d87c4SChristoph Hellwig {
172c59d87c4SChristoph Hellwig struct inode *inode;
173c59d87c4SChristoph Hellwig struct xfs_inode *ip = NULL;
1742401dc29SChristoph Hellwig struct posix_acl *default_acl, *acl;
175c59d87c4SChristoph Hellwig struct xfs_name name;
176c59d87c4SChristoph Hellwig int error;
177c59d87c4SChristoph Hellwig
178c59d87c4SChristoph Hellwig /*
179c59d87c4SChristoph Hellwig * Irix uses Missed'em'V split, but doesn't want to see
180c59d87c4SChristoph Hellwig * the upper 5 bits of (14bit) major.
181c59d87c4SChristoph Hellwig */
182c59d87c4SChristoph Hellwig if (S_ISCHR(mode) || S_ISBLK(mode)) {
183c59d87c4SChristoph Hellwig if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
184c59d87c4SChristoph Hellwig return -EINVAL;
185c59d87c4SChristoph Hellwig } else {
186c59d87c4SChristoph Hellwig rdev = 0;
187c59d87c4SChristoph Hellwig }
188c59d87c4SChristoph Hellwig
1892401dc29SChristoph Hellwig error = posix_acl_create(dir, &mode, &default_acl, &acl);
1902401dc29SChristoph Hellwig if (error)
1912401dc29SChristoph Hellwig return error;
192c59d87c4SChristoph Hellwig
193fab8eef8SAmir Goldstein /* Verify mode is valid also for tmpfile case */
194fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&name, dentry, mode);
195fab8eef8SAmir Goldstein if (unlikely(error))
196fab8eef8SAmir Goldstein goto out_free_acl;
197fab8eef8SAmir Goldstein
198d540e43bSBrian Foster if (!tmpfile) {
199f2d40141SChristian Brauner error = xfs_create(idmap, XFS_I(dir), &name, mode, rdev,
200e6a688c3SDave Chinner xfs_create_need_xattr(dir, default_acl, acl),
201f736d93dSChristoph Hellwig &ip);
202d540e43bSBrian Foster } else {
203f2d40141SChristian Brauner error = xfs_create_tmpfile(idmap, XFS_I(dir), mode, &ip);
204d540e43bSBrian Foster }
205c59d87c4SChristoph Hellwig if (unlikely(error))
206c59d87c4SChristoph Hellwig goto out_free_acl;
207c59d87c4SChristoph Hellwig
208c59d87c4SChristoph Hellwig inode = VFS_I(ip);
209c59d87c4SChristoph Hellwig
21070b589a3SEric Sandeen error = xfs_inode_init_security(inode, dir, &dentry->d_name);
211c59d87c4SChristoph Hellwig if (unlikely(error))
212c59d87c4SChristoph Hellwig goto out_cleanup_inode;
213c59d87c4SChristoph Hellwig
214c59d87c4SChristoph Hellwig if (default_acl) {
2158ba35875SJan Kara error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
2162401dc29SChristoph Hellwig if (error)
217c59d87c4SChristoph Hellwig goto out_cleanup_inode;
218c59d87c4SChristoph Hellwig }
2192401dc29SChristoph Hellwig if (acl) {
2208ba35875SJan Kara error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
2212401dc29SChristoph Hellwig if (error)
2222401dc29SChristoph Hellwig goto out_cleanup_inode;
2232401dc29SChristoph Hellwig }
224c59d87c4SChristoph Hellwig
2252b3d1d41SChristoph Hellwig xfs_setup_iops(ip);
2262b3d1d41SChristoph Hellwig
227c4a6bf7fSDarrick J. Wong if (tmpfile) {
228c4a6bf7fSDarrick J. Wong /*
229c4a6bf7fSDarrick J. Wong * The VFS requires that any inode fed to d_tmpfile must have
230c4a6bf7fSDarrick J. Wong * nlink == 1 so that it can decrement the nlink in d_tmpfile.
231c4a6bf7fSDarrick J. Wong * However, we created the temp file with nlink == 0 because
232c4a6bf7fSDarrick J. Wong * we're not allowed to put an inode with nlink > 0 on the
233c4a6bf7fSDarrick J. Wong * unlinked list. Therefore we have to set nlink to 1 so that
234c4a6bf7fSDarrick J. Wong * d_tmpfile can immediately set it back to zero.
235c4a6bf7fSDarrick J. Wong */
236c4a6bf7fSDarrick J. Wong set_nlink(inode, 1);
237863f144fSMiklos Szeredi d_tmpfile(tmpfile, inode);
238c4a6bf7fSDarrick J. Wong } else
239c59d87c4SChristoph Hellwig d_instantiate(dentry, inode);
240d540e43bSBrian Foster
24158c90473SDave Chinner xfs_finish_inode_setup(ip);
24258c90473SDave Chinner
2432401dc29SChristoph Hellwig out_free_acl:
2442401dc29SChristoph Hellwig posix_acl_release(default_acl);
2452401dc29SChristoph Hellwig posix_acl_release(acl);
2462451337dSDave Chinner return error;
247c59d87c4SChristoph Hellwig
248c59d87c4SChristoph Hellwig out_cleanup_inode:
24958c90473SDave Chinner xfs_finish_inode_setup(ip);
250d540e43bSBrian Foster if (!tmpfile)
251c59d87c4SChristoph Hellwig xfs_cleanup_inode(dir, inode, dentry);
25244a8736bSDarrick J. Wong xfs_irele(ip);
2532401dc29SChristoph Hellwig goto out_free_acl;
254c59d87c4SChristoph Hellwig }
255c59d87c4SChristoph Hellwig
256c59d87c4SChristoph Hellwig STATIC int
xfs_vn_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)257d540e43bSBrian Foster xfs_vn_mknod(
2585ebb29beSChristian Brauner struct mnt_idmap *idmap,
259d540e43bSBrian Foster struct inode *dir,
260d540e43bSBrian Foster struct dentry *dentry,
261d540e43bSBrian Foster umode_t mode,
262d540e43bSBrian Foster dev_t rdev)
263d540e43bSBrian Foster {
264f2d40141SChristian Brauner return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL);
265d540e43bSBrian Foster }
266d540e43bSBrian Foster
267d540e43bSBrian Foster STATIC int
xfs_vn_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool flags)268c59d87c4SChristoph Hellwig xfs_vn_create(
2696c960e68SChristian Brauner struct mnt_idmap *idmap,
270c59d87c4SChristoph Hellwig struct inode *dir,
271c59d87c4SChristoph Hellwig struct dentry *dentry,
2724acdaf27SAl Viro umode_t mode,
273ebfc3b49SAl Viro bool flags)
274c59d87c4SChristoph Hellwig {
275f2d40141SChristian Brauner return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL);
276c59d87c4SChristoph Hellwig }
277c59d87c4SChristoph Hellwig
278c59d87c4SChristoph Hellwig STATIC int
xfs_vn_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)279c59d87c4SChristoph Hellwig xfs_vn_mkdir(
280c54bd91eSChristian Brauner struct mnt_idmap *idmap,
281c59d87c4SChristoph Hellwig struct inode *dir,
282c59d87c4SChristoph Hellwig struct dentry *dentry,
28318bb1db3SAl Viro umode_t mode)
284c59d87c4SChristoph Hellwig {
285f2d40141SChristian Brauner return xfs_generic_create(idmap, dir, dentry, mode | S_IFDIR, 0, NULL);
286c59d87c4SChristoph Hellwig }
287c59d87c4SChristoph Hellwig
288c59d87c4SChristoph Hellwig STATIC struct dentry *
xfs_vn_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)289c59d87c4SChristoph Hellwig xfs_vn_lookup(
290c59d87c4SChristoph Hellwig struct inode *dir,
291c59d87c4SChristoph Hellwig struct dentry *dentry,
29200cd8dd3SAl Viro unsigned int flags)
293c59d87c4SChristoph Hellwig {
294b113a6d3SAl Viro struct inode *inode;
295c59d87c4SChristoph Hellwig struct xfs_inode *cip;
296c59d87c4SChristoph Hellwig struct xfs_name name;
297c59d87c4SChristoph Hellwig int error;
298c59d87c4SChristoph Hellwig
299c59d87c4SChristoph Hellwig if (dentry->d_name.len >= MAXNAMELEN)
300c59d87c4SChristoph Hellwig return ERR_PTR(-ENAMETOOLONG);
301c59d87c4SChristoph Hellwig
302fab8eef8SAmir Goldstein xfs_dentry_to_name(&name, dentry);
303c59d87c4SChristoph Hellwig error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
304b113a6d3SAl Viro if (likely(!error))
305b113a6d3SAl Viro inode = VFS_I(cip);
306b113a6d3SAl Viro else if (likely(error == -ENOENT))
307b113a6d3SAl Viro inode = NULL;
308b113a6d3SAl Viro else
309b113a6d3SAl Viro inode = ERR_PTR(error);
310b113a6d3SAl Viro return d_splice_alias(inode, dentry);
311c59d87c4SChristoph Hellwig }
312c59d87c4SChristoph Hellwig
313c59d87c4SChristoph Hellwig STATIC struct dentry *
xfs_vn_ci_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)314c59d87c4SChristoph Hellwig xfs_vn_ci_lookup(
315c59d87c4SChristoph Hellwig struct inode *dir,
316c59d87c4SChristoph Hellwig struct dentry *dentry,
31700cd8dd3SAl Viro unsigned int flags)
318c59d87c4SChristoph Hellwig {
319c59d87c4SChristoph Hellwig struct xfs_inode *ip;
320c59d87c4SChristoph Hellwig struct xfs_name xname;
321c59d87c4SChristoph Hellwig struct xfs_name ci_name;
322c59d87c4SChristoph Hellwig struct qstr dname;
323c59d87c4SChristoph Hellwig int error;
324c59d87c4SChristoph Hellwig
325c59d87c4SChristoph Hellwig if (dentry->d_name.len >= MAXNAMELEN)
326c59d87c4SChristoph Hellwig return ERR_PTR(-ENAMETOOLONG);
327c59d87c4SChristoph Hellwig
328fab8eef8SAmir Goldstein xfs_dentry_to_name(&xname, dentry);
329c59d87c4SChristoph Hellwig error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
330c59d87c4SChristoph Hellwig if (unlikely(error)) {
3312451337dSDave Chinner if (unlikely(error != -ENOENT))
3322451337dSDave Chinner return ERR_PTR(error);
333c59d87c4SChristoph Hellwig /*
334c59d87c4SChristoph Hellwig * call d_add(dentry, NULL) here when d_drop_negative_children
335c59d87c4SChristoph Hellwig * is called in xfs_vn_mknod (ie. allow negative dentries
336c59d87c4SChristoph Hellwig * with CI filesystems).
337c59d87c4SChristoph Hellwig */
338c59d87c4SChristoph Hellwig return NULL;
339c59d87c4SChristoph Hellwig }
340c59d87c4SChristoph Hellwig
341c59d87c4SChristoph Hellwig /* if exact match, just splice and exit */
342c59d87c4SChristoph Hellwig if (!ci_name.name)
343c59d87c4SChristoph Hellwig return d_splice_alias(VFS_I(ip), dentry);
344c59d87c4SChristoph Hellwig
345c59d87c4SChristoph Hellwig /* else case-insensitive match... */
346c59d87c4SChristoph Hellwig dname.name = ci_name.name;
347c59d87c4SChristoph Hellwig dname.len = ci_name.len;
348c59d87c4SChristoph Hellwig dentry = d_add_ci(dentry, VFS_I(ip), &dname);
349c59d87c4SChristoph Hellwig kmem_free(ci_name.name);
350c59d87c4SChristoph Hellwig return dentry;
351c59d87c4SChristoph Hellwig }
352c59d87c4SChristoph Hellwig
353c59d87c4SChristoph Hellwig STATIC int
xfs_vn_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)354c59d87c4SChristoph Hellwig xfs_vn_link(
355c59d87c4SChristoph Hellwig struct dentry *old_dentry,
356c59d87c4SChristoph Hellwig struct inode *dir,
357c59d87c4SChristoph Hellwig struct dentry *dentry)
358c59d87c4SChristoph Hellwig {
3592b0143b5SDavid Howells struct inode *inode = d_inode(old_dentry);
360c59d87c4SChristoph Hellwig struct xfs_name name;
361c59d87c4SChristoph Hellwig int error;
362c59d87c4SChristoph Hellwig
363fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
364fab8eef8SAmir Goldstein if (unlikely(error))
365fab8eef8SAmir Goldstein return error;
366c59d87c4SChristoph Hellwig
367c59d87c4SChristoph Hellwig error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
368c59d87c4SChristoph Hellwig if (unlikely(error))
3692451337dSDave Chinner return error;
370c59d87c4SChristoph Hellwig
371c59d87c4SChristoph Hellwig ihold(inode);
372c59d87c4SChristoph Hellwig d_instantiate(dentry, inode);
373c59d87c4SChristoph Hellwig return 0;
374c59d87c4SChristoph Hellwig }
375c59d87c4SChristoph Hellwig
376c59d87c4SChristoph Hellwig STATIC int
xfs_vn_unlink(struct inode * dir,struct dentry * dentry)377c59d87c4SChristoph Hellwig xfs_vn_unlink(
378c59d87c4SChristoph Hellwig struct inode *dir,
379c59d87c4SChristoph Hellwig struct dentry *dentry)
380c59d87c4SChristoph Hellwig {
381c59d87c4SChristoph Hellwig struct xfs_name name;
382c59d87c4SChristoph Hellwig int error;
383c59d87c4SChristoph Hellwig
384fab8eef8SAmir Goldstein xfs_dentry_to_name(&name, dentry);
385c59d87c4SChristoph Hellwig
3862b0143b5SDavid Howells error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
387c59d87c4SChristoph Hellwig if (error)
388c59d87c4SChristoph Hellwig return error;
389c59d87c4SChristoph Hellwig
390c59d87c4SChristoph Hellwig /*
391c59d87c4SChristoph Hellwig * With unlink, the VFS makes the dentry "negative": no inode,
392c59d87c4SChristoph Hellwig * but still hashed. This is incompatible with case-insensitive
393c59d87c4SChristoph Hellwig * mode, so invalidate (unhash) the dentry in CI-mode.
394c59d87c4SChristoph Hellwig */
39538c26bfdSDave Chinner if (xfs_has_asciici(XFS_M(dir->i_sb)))
396c59d87c4SChristoph Hellwig d_invalidate(dentry);
397c59d87c4SChristoph Hellwig return 0;
398c59d87c4SChristoph Hellwig }
399c59d87c4SChristoph Hellwig
400c59d87c4SChristoph Hellwig STATIC int
xfs_vn_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)401c59d87c4SChristoph Hellwig xfs_vn_symlink(
4027a77db95SChristian Brauner struct mnt_idmap *idmap,
403c59d87c4SChristoph Hellwig struct inode *dir,
404c59d87c4SChristoph Hellwig struct dentry *dentry,
405c59d87c4SChristoph Hellwig const char *symname)
406c59d87c4SChristoph Hellwig {
407c59d87c4SChristoph Hellwig struct inode *inode;
408c59d87c4SChristoph Hellwig struct xfs_inode *cip = NULL;
409c59d87c4SChristoph Hellwig struct xfs_name name;
410c59d87c4SChristoph Hellwig int error;
411576b1d67SAl Viro umode_t mode;
412c59d87c4SChristoph Hellwig
413c59d87c4SChristoph Hellwig mode = S_IFLNK |
414c59d87c4SChristoph Hellwig (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
415fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&name, dentry, mode);
416fab8eef8SAmir Goldstein if (unlikely(error))
417fab8eef8SAmir Goldstein goto out;
418c59d87c4SChristoph Hellwig
419f2d40141SChristian Brauner error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip);
420c59d87c4SChristoph Hellwig if (unlikely(error))
421c59d87c4SChristoph Hellwig goto out;
422c59d87c4SChristoph Hellwig
423c59d87c4SChristoph Hellwig inode = VFS_I(cip);
424c59d87c4SChristoph Hellwig
42570b589a3SEric Sandeen error = xfs_inode_init_security(inode, dir, &dentry->d_name);
426c59d87c4SChristoph Hellwig if (unlikely(error))
427c59d87c4SChristoph Hellwig goto out_cleanup_inode;
428c59d87c4SChristoph Hellwig
4292b3d1d41SChristoph Hellwig xfs_setup_iops(cip);
4302b3d1d41SChristoph Hellwig
431c59d87c4SChristoph Hellwig d_instantiate(dentry, inode);
43258c90473SDave Chinner xfs_finish_inode_setup(cip);
433c59d87c4SChristoph Hellwig return 0;
434c59d87c4SChristoph Hellwig
435c59d87c4SChristoph Hellwig out_cleanup_inode:
43658c90473SDave Chinner xfs_finish_inode_setup(cip);
437c59d87c4SChristoph Hellwig xfs_cleanup_inode(dir, inode, dentry);
43844a8736bSDarrick J. Wong xfs_irele(cip);
439c59d87c4SChristoph Hellwig out:
4402451337dSDave Chinner return error;
441c59d87c4SChristoph Hellwig }
442c59d87c4SChristoph Hellwig
443c59d87c4SChristoph Hellwig STATIC int
xfs_vn_rename(struct mnt_idmap * idmap,struct inode * odir,struct dentry * odentry,struct inode * ndir,struct dentry * ndentry,unsigned int flags)444c59d87c4SChristoph Hellwig xfs_vn_rename(
445e18275aeSChristian Brauner struct mnt_idmap *idmap,
446c59d87c4SChristoph Hellwig struct inode *odir,
447c59d87c4SChristoph Hellwig struct dentry *odentry,
448c59d87c4SChristoph Hellwig struct inode *ndir,
449dbe1b5caSCarlos Maiolino struct dentry *ndentry,
450dbe1b5caSCarlos Maiolino unsigned int flags)
451c59d87c4SChristoph Hellwig {
4522b0143b5SDavid Howells struct inode *new_inode = d_inode(ndentry);
453d31a1825SCarlos Maiolino int omode = 0;
454fab8eef8SAmir Goldstein int error;
455c59d87c4SChristoph Hellwig struct xfs_name oname;
456c59d87c4SChristoph Hellwig struct xfs_name nname;
457c59d87c4SChristoph Hellwig
4587dcf5c3eSDave Chinner if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
459dbe1b5caSCarlos Maiolino return -EINVAL;
460dbe1b5caSCarlos Maiolino
461d31a1825SCarlos Maiolino /* if we are exchanging files, we need to set i_mode of both files */
462d31a1825SCarlos Maiolino if (flags & RENAME_EXCHANGE)
4632b0143b5SDavid Howells omode = d_inode(ndentry)->i_mode;
464d31a1825SCarlos Maiolino
465fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&oname, odentry, omode);
466fab8eef8SAmir Goldstein if (omode && unlikely(error))
467fab8eef8SAmir Goldstein return error;
468fab8eef8SAmir Goldstein
469fab8eef8SAmir Goldstein error = xfs_dentry_mode_to_name(&nname, ndentry,
470fab8eef8SAmir Goldstein d_inode(odentry)->i_mode);
471fab8eef8SAmir Goldstein if (unlikely(error))
472fab8eef8SAmir Goldstein return error;
473c59d87c4SChristoph Hellwig
474f2d40141SChristian Brauner return xfs_rename(idmap, XFS_I(odir), &oname,
475f736d93dSChristoph Hellwig XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
476d31a1825SCarlos Maiolino new_inode ? XFS_I(new_inode) : NULL, flags);
477c59d87c4SChristoph Hellwig }
478c59d87c4SChristoph Hellwig
479c59d87c4SChristoph Hellwig /*
480c59d87c4SChristoph Hellwig * careful here - this function can get called recursively, so
481c59d87c4SChristoph Hellwig * we need to be very careful about how much stack we use.
482c59d87c4SChristoph Hellwig * uio is kmalloced for this reason...
483c59d87c4SChristoph Hellwig */
484680baacbSAl Viro STATIC const char *
xfs_vn_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)4856b255391SAl Viro xfs_vn_get_link(
486c59d87c4SChristoph Hellwig struct dentry *dentry,
4876b255391SAl Viro struct inode *inode,
488fceef393SAl Viro struct delayed_call *done)
489c59d87c4SChristoph Hellwig {
490c59d87c4SChristoph Hellwig char *link;
491c59d87c4SChristoph Hellwig int error = -ENOMEM;
492c59d87c4SChristoph Hellwig
4936b255391SAl Viro if (!dentry)
4946b255391SAl Viro return ERR_PTR(-ECHILD);
4956b255391SAl Viro
4966eb0b8dfSDarrick J. Wong link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
497c59d87c4SChristoph Hellwig if (!link)
498c59d87c4SChristoph Hellwig goto out_err;
499c59d87c4SChristoph Hellwig
5002b0143b5SDavid Howells error = xfs_readlink(XFS_I(d_inode(dentry)), link);
501c59d87c4SChristoph Hellwig if (unlikely(error))
502c59d87c4SChristoph Hellwig goto out_kfree;
503c59d87c4SChristoph Hellwig
504fceef393SAl Viro set_delayed_call(done, kfree_link, link);
505fceef393SAl Viro return link;
506c59d87c4SChristoph Hellwig
507c59d87c4SChristoph Hellwig out_kfree:
508c59d87c4SChristoph Hellwig kfree(link);
509c59d87c4SChristoph Hellwig out_err:
510680baacbSAl Viro return ERR_PTR(error);
511c59d87c4SChristoph Hellwig }
512c59d87c4SChristoph Hellwig
513dd2d535eSChristoph Hellwig static uint32_t
xfs_stat_blksize(struct xfs_inode * ip)514dd2d535eSChristoph Hellwig xfs_stat_blksize(
515dd2d535eSChristoph Hellwig struct xfs_inode *ip)
516dd2d535eSChristoph Hellwig {
517dd2d535eSChristoph Hellwig struct xfs_mount *mp = ip->i_mount;
518dd2d535eSChristoph Hellwig
519dd2d535eSChristoph Hellwig /*
520dd2d535eSChristoph Hellwig * If the file blocks are being allocated from a realtime volume, then
521dd2d535eSChristoph Hellwig * always return the realtime extent size.
522dd2d535eSChristoph Hellwig */
523dd2d535eSChristoph Hellwig if (XFS_IS_REALTIME_INODE(ip))
524a7bcb147SDarrick J. Wong return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip));
525dd2d535eSChristoph Hellwig
526dd2d535eSChristoph Hellwig /*
527dd2d535eSChristoph Hellwig * Allow large block sizes to be reported to userspace programs if the
528dd2d535eSChristoph Hellwig * "largeio" mount option is used.
529dd2d535eSChristoph Hellwig *
530dd2d535eSChristoph Hellwig * If compatibility mode is specified, simply return the basic unit of
531dd2d535eSChristoph Hellwig * caching so that we don't get inefficient read/modify/write I/O from
532dd2d535eSChristoph Hellwig * user apps. Otherwise....
533dd2d535eSChristoph Hellwig *
534dd2d535eSChristoph Hellwig * If the underlying volume is a stripe, then return the stripe width in
535dd2d535eSChristoph Hellwig * bytes as the recommended I/O size. It is not a stripe and we've set a
536dd2d535eSChristoph Hellwig * default buffered I/O size, return that, otherwise return the compat
537dd2d535eSChristoph Hellwig * default.
538dd2d535eSChristoph Hellwig */
5390560f31aSDave Chinner if (xfs_has_large_iosize(mp)) {
540dd2d535eSChristoph Hellwig if (mp->m_swidth)
541a7bcb147SDarrick J. Wong return XFS_FSB_TO_B(mp, mp->m_swidth);
5420560f31aSDave Chinner if (xfs_has_allocsize(mp))
5435da8a07cSChristoph Hellwig return 1U << mp->m_allocsize_log;
544dd2d535eSChristoph Hellwig }
545dd2d535eSChristoph Hellwig
546dd2d535eSChristoph Hellwig return PAGE_SIZE;
547dd2d535eSChristoph Hellwig }
548dd2d535eSChristoph Hellwig
549c59d87c4SChristoph Hellwig STATIC int
xfs_vn_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)550c59d87c4SChristoph Hellwig xfs_vn_getattr(
551b74d24f7SChristian Brauner struct mnt_idmap *idmap,
552a528d35eSDavid Howells const struct path *path,
553a528d35eSDavid Howells struct kstat *stat,
554a528d35eSDavid Howells u32 request_mask,
555a528d35eSDavid Howells unsigned int query_flags)
556c59d87c4SChristoph Hellwig {
557a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry);
558c59d87c4SChristoph Hellwig struct xfs_inode *ip = XFS_I(inode);
559c59d87c4SChristoph Hellwig struct xfs_mount *mp = ip->i_mount;
560e67fe633SChristian Brauner vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
561e67fe633SChristian Brauner vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
562c59d87c4SChristoph Hellwig
563c59d87c4SChristoph Hellwig trace_xfs_getattr(ip);
564c59d87c4SChristoph Hellwig
56575c8c50fSDave Chinner if (xfs_is_shutdown(mp))
566b474c7aeSEric Sandeen return -EIO;
567c59d87c4SChristoph Hellwig
568c59d87c4SChristoph Hellwig stat->size = XFS_ISIZE(ip);
569c59d87c4SChristoph Hellwig stat->dev = inode->i_sb->s_dev;
570c19b3b05SDave Chinner stat->mode = inode->i_mode;
57154d7b5c1SDave Chinner stat->nlink = inode->i_nlink;
57242b7cc11SChristian Brauner stat->uid = vfsuid_into_kuid(vfsuid);
57342b7cc11SChristian Brauner stat->gid = vfsgid_into_kgid(vfsgid);
574c59d87c4SChristoph Hellwig stat->ino = ip->i_ino;
575c59d87c4SChristoph Hellwig stat->atime = inode->i_atime;
576f798accdSChristian Brauner stat->mtime = inode->i_mtime;
577f798accdSChristian Brauner stat->ctime = inode_get_ctime(inode);
5786e73a545SChristoph Hellwig stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
579c59d87c4SChristoph Hellwig
58038c26bfdSDave Chinner if (xfs_has_v3inodes(mp)) {
5815f955f26SDarrick J. Wong if (request_mask & STATX_BTIME) {
5825f955f26SDarrick J. Wong stat->result_mask |= STATX_BTIME;
583e98d5e88SChristoph Hellwig stat->btime = ip->i_crtime;
5845f955f26SDarrick J. Wong }
5855f955f26SDarrick J. Wong }
5865f955f26SDarrick J. Wong
587cbc06310SJeff Layton if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
588cbc06310SJeff Layton stat->change_cookie = inode_query_iversion(inode);
589cbc06310SJeff Layton stat->result_mask |= STATX_CHANGE_COOKIE;
590cbc06310SJeff Layton }
591cbc06310SJeff Layton
5921b9598c8SLuis R. Rodriguez /*
5931b9598c8SLuis R. Rodriguez * Note: If you add another clause to set an attribute flag, please
5941b9598c8SLuis R. Rodriguez * update attributes_mask below.
5951b9598c8SLuis R. Rodriguez */
596db07349dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
5975f955f26SDarrick J. Wong stat->attributes |= STATX_ATTR_IMMUTABLE;
598db07349dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_APPEND)
5995f955f26SDarrick J. Wong stat->attributes |= STATX_ATTR_APPEND;
600db07349dSChristoph Hellwig if (ip->i_diflags & XFS_DIFLAG_NODUMP)
6015f955f26SDarrick J. Wong stat->attributes |= STATX_ATTR_NODUMP;
602c59d87c4SChristoph Hellwig
6031b9598c8SLuis R. Rodriguez stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
6041b9598c8SLuis R. Rodriguez STATX_ATTR_APPEND |
6051b9598c8SLuis R. Rodriguez STATX_ATTR_NODUMP);
6061b9598c8SLuis R. Rodriguez
607c59d87c4SChristoph Hellwig switch (inode->i_mode & S_IFMT) {
608c59d87c4SChristoph Hellwig case S_IFBLK:
609c59d87c4SChristoph Hellwig case S_IFCHR:
610c59d87c4SChristoph Hellwig stat->blksize = BLKDEV_IOSIZE;
61166f36464SChristoph Hellwig stat->rdev = inode->i_rdev;
612c59d87c4SChristoph Hellwig break;
61361a223dfSEric Biggers case S_IFREG:
61461a223dfSEric Biggers if (request_mask & STATX_DIOALIGN) {
61561a223dfSEric Biggers struct xfs_buftarg *target = xfs_inode_buftarg(ip);
61661a223dfSEric Biggers struct block_device *bdev = target->bt_bdev;
61761a223dfSEric Biggers
61861a223dfSEric Biggers stat->result_mask |= STATX_DIOALIGN;
61961a223dfSEric Biggers stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
62061a223dfSEric Biggers stat->dio_offset_align = bdev_logical_block_size(bdev);
62161a223dfSEric Biggers }
62261a223dfSEric Biggers fallthrough;
623c59d87c4SChristoph Hellwig default:
624dd2d535eSChristoph Hellwig stat->blksize = xfs_stat_blksize(ip);
625c59d87c4SChristoph Hellwig stat->rdev = 0;
626c59d87c4SChristoph Hellwig break;
627c59d87c4SChristoph Hellwig }
628c59d87c4SChristoph Hellwig
629c59d87c4SChristoph Hellwig return 0;
630c59d87c4SChristoph Hellwig }
631c59d87c4SChristoph Hellwig
63269bca807SJan Kara static int
xfs_vn_change_ok(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)63369bca807SJan Kara xfs_vn_change_ok(
634c1632a0fSChristian Brauner struct mnt_idmap *idmap,
63569bca807SJan Kara struct dentry *dentry,
63669bca807SJan Kara struct iattr *iattr)
63769bca807SJan Kara {
63831051c85SJan Kara struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
63969bca807SJan Kara
6402e973b2cSDave Chinner if (xfs_is_readonly(mp))
64169bca807SJan Kara return -EROFS;
64269bca807SJan Kara
64375c8c50fSDave Chinner if (xfs_is_shutdown(mp))
64469bca807SJan Kara return -EIO;
64569bca807SJan Kara
646c1632a0fSChristian Brauner return setattr_prepare(idmap, dentry, iattr);
64769bca807SJan Kara }
64869bca807SJan Kara
64969bca807SJan Kara /*
65069bca807SJan Kara * Set non-size attributes of an inode.
65169bca807SJan Kara *
65269bca807SJan Kara * Caution: The caller of this function is responsible for calling
65331051c85SJan Kara * setattr_prepare() or otherwise verifying the change is fine.
65469bca807SJan Kara */
6555d24ec4cSChristoph Hellwig static int
xfs_setattr_nonsize(struct mnt_idmap * idmap,struct dentry * dentry,struct xfs_inode * ip,struct iattr * iattr)656c59d87c4SChristoph Hellwig xfs_setattr_nonsize(
657c1632a0fSChristian Brauner struct mnt_idmap *idmap,
658138060baSChristian Brauner struct dentry *dentry,
659c59d87c4SChristoph Hellwig struct xfs_inode *ip,
6605d24ec4cSChristoph Hellwig struct iattr *iattr)
661c59d87c4SChristoph Hellwig {
662c59d87c4SChristoph Hellwig xfs_mount_t *mp = ip->i_mount;
663c59d87c4SChristoph Hellwig struct inode *inode = VFS_I(ip);
664c59d87c4SChristoph Hellwig int mask = iattr->ia_valid;
665c59d87c4SChristoph Hellwig xfs_trans_t *tp;
666c59d87c4SChristoph Hellwig int error;
667dd3b015dSDarrick J. Wong kuid_t uid = GLOBAL_ROOT_UID;
668dd3b015dSDarrick J. Wong kgid_t gid = GLOBAL_ROOT_GID;
669c59d87c4SChristoph Hellwig struct xfs_dquot *udqp = NULL, *gdqp = NULL;
670dd3b015dSDarrick J. Wong struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL;
671c59d87c4SChristoph Hellwig
672c59d87c4SChristoph Hellwig ASSERT((mask & ATTR_SIZE) == 0);
673c59d87c4SChristoph Hellwig
674c59d87c4SChristoph Hellwig /*
675c59d87c4SChristoph Hellwig * If disk quotas is on, we make sure that the dquots do exist on disk,
676c59d87c4SChristoph Hellwig * before we start any other transactions. Trying to do this later
677c59d87c4SChristoph Hellwig * is messy. We don't care to take a readlock to look at the ids
678c59d87c4SChristoph Hellwig * in inode here, because we can't hold it across the trans_reserve.
679c59d87c4SChristoph Hellwig * If the IDs do change before we take the ilock, we're covered
680c59d87c4SChristoph Hellwig * because the i_*dquot fields will get updated anyway.
681c59d87c4SChristoph Hellwig */
682c59d87c4SChristoph Hellwig if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
683c59d87c4SChristoph Hellwig uint qflags = 0;
684c59d87c4SChristoph Hellwig
685c59d87c4SChristoph Hellwig if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
6864d7ca409SChristian Brauner uid = from_vfsuid(idmap, i_user_ns(inode),
687b27c82e1SChristian Brauner iattr->ia_vfsuid);
688c59d87c4SChristoph Hellwig qflags |= XFS_QMOPT_UQUOTA;
689c59d87c4SChristoph Hellwig } else {
6907aab1b28SDwight Engen uid = inode->i_uid;
691c59d87c4SChristoph Hellwig }
692c59d87c4SChristoph Hellwig if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
6934d7ca409SChristian Brauner gid = from_vfsgid(idmap, i_user_ns(inode),
694b27c82e1SChristian Brauner iattr->ia_vfsgid);
695c59d87c4SChristoph Hellwig qflags |= XFS_QMOPT_GQUOTA;
696c59d87c4SChristoph Hellwig } else {
6977aab1b28SDwight Engen gid = inode->i_gid;
698c59d87c4SChristoph Hellwig }
699c59d87c4SChristoph Hellwig
700c59d87c4SChristoph Hellwig /*
701c59d87c4SChristoph Hellwig * We take a reference when we initialize udqp and gdqp,
702c59d87c4SChristoph Hellwig * so it is important that we never blindly double trip on
703c59d87c4SChristoph Hellwig * the same variable. See xfs_create() for an example.
704c59d87c4SChristoph Hellwig */
705c59d87c4SChristoph Hellwig ASSERT(udqp == NULL);
706c59d87c4SChristoph Hellwig ASSERT(gdqp == NULL);
707ceaf603cSChristoph Hellwig error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
70892f8ff73SChandra Seetharaman qflags, &udqp, &gdqp, NULL);
709c59d87c4SChristoph Hellwig if (error)
710c59d87c4SChristoph Hellwig return error;
711c59d87c4SChristoph Hellwig }
712c59d87c4SChristoph Hellwig
7137317a03dSDarrick J. Wong error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
714eba0549bSDarrick J. Wong has_capability_noaudit(current, CAP_FOWNER), &tp);
715c59d87c4SChristoph Hellwig if (error)
716253f4911SChristoph Hellwig goto out_dqrele;
717c59d87c4SChristoph Hellwig
718c59d87c4SChristoph Hellwig /*
719dd3b015dSDarrick J. Wong * Register quota modifications in the transaction. Must be the owner
720dd3b015dSDarrick J. Wong * or privileged. These IDs could have changed since we last looked at
721dd3b015dSDarrick J. Wong * them. But, we're assured that if the ownership did change while we
722dd3b015dSDarrick J. Wong * didn't have the inode locked, inode's dquot(s) would have changed
723dd3b015dSDarrick J. Wong * also.
724c59d87c4SChristoph Hellwig */
72535faf310SChristian Brauner if (XFS_IS_UQUOTA_ON(mp) &&
7260dbe12f2SChristian Brauner i_uid_needs_update(idmap, iattr, inode)) {
727c59d87c4SChristoph Hellwig ASSERT(udqp);
728dd3b015dSDarrick J. Wong old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp);
729c59d87c4SChristoph Hellwig }
73035faf310SChristian Brauner if (XFS_IS_GQUOTA_ON(mp) &&
7310dbe12f2SChristian Brauner i_gid_needs_update(idmap, iattr, inode)) {
732dd3b015dSDarrick J. Wong ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp));
733c59d87c4SChristoph Hellwig ASSERT(gdqp);
734dd3b015dSDarrick J. Wong old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp);
735c59d87c4SChristoph Hellwig }
736c59d87c4SChristoph Hellwig
737c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr);
738c59d87c4SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
739c59d87c4SChristoph Hellwig
740ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_ig_attrchg);
741c59d87c4SChristoph Hellwig
7420560f31aSDave Chinner if (xfs_has_wsync(mp))
743c59d87c4SChristoph Hellwig xfs_trans_set_sync(tp);
74470393313SChristoph Hellwig error = xfs_trans_commit(tp);
745c59d87c4SChristoph Hellwig
746c59d87c4SChristoph Hellwig /*
747c59d87c4SChristoph Hellwig * Release any dquot(s) the inode had kept before chown.
748c59d87c4SChristoph Hellwig */
749dd3b015dSDarrick J. Wong xfs_qm_dqrele(old_udqp);
750dd3b015dSDarrick J. Wong xfs_qm_dqrele(old_gdqp);
751c59d87c4SChristoph Hellwig xfs_qm_dqrele(udqp);
752c59d87c4SChristoph Hellwig xfs_qm_dqrele(gdqp);
753c59d87c4SChristoph Hellwig
754c59d87c4SChristoph Hellwig if (error)
755b474c7aeSEric Sandeen return error;
756c59d87c4SChristoph Hellwig
757c59d87c4SChristoph Hellwig /*
758c59d87c4SChristoph Hellwig * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
759c59d87c4SChristoph Hellwig * update. We could avoid this with linked transactions
760c59d87c4SChristoph Hellwig * and passing down the transaction pointer all the way
761c59d87c4SChristoph Hellwig * to attr_set. No previous user of the generic
762c59d87c4SChristoph Hellwig * Posix ACL code seems to care about this issue either.
763c59d87c4SChristoph Hellwig */
7645d24ec4cSChristoph Hellwig if (mask & ATTR_MODE) {
76513e83a49SChristian Brauner error = posix_acl_chmod(idmap, dentry, inode->i_mode);
766c59d87c4SChristoph Hellwig if (error)
767b474c7aeSEric Sandeen return error;
768c59d87c4SChristoph Hellwig }
769c59d87c4SChristoph Hellwig
770c59d87c4SChristoph Hellwig return 0;
771c59d87c4SChristoph Hellwig
772253f4911SChristoph Hellwig out_dqrele:
773c59d87c4SChristoph Hellwig xfs_qm_dqrele(udqp);
774c59d87c4SChristoph Hellwig xfs_qm_dqrele(gdqp);
775c59d87c4SChristoph Hellwig return error;
776c59d87c4SChristoph Hellwig }
777c59d87c4SChristoph Hellwig
778c59d87c4SChristoph Hellwig /*
779c59d87c4SChristoph Hellwig * Truncate file. Must have write permission and not be a directory.
78069bca807SJan Kara *
78169bca807SJan Kara * Caution: The caller of this function is responsible for calling
78231051c85SJan Kara * setattr_prepare() or otherwise verifying the change is fine.
783c59d87c4SChristoph Hellwig */
7847bf7a193SDarrick J. Wong STATIC int
xfs_setattr_size(struct mnt_idmap * idmap,struct dentry * dentry,struct xfs_inode * ip,struct iattr * iattr)785c59d87c4SChristoph Hellwig xfs_setattr_size(
786c1632a0fSChristian Brauner struct mnt_idmap *idmap,
787138060baSChristian Brauner struct dentry *dentry,
788c59d87c4SChristoph Hellwig struct xfs_inode *ip,
78976ca4c23SChristoph Hellwig struct iattr *iattr)
790c59d87c4SChristoph Hellwig {
791c59d87c4SChristoph Hellwig struct xfs_mount *mp = ip->i_mount;
792c59d87c4SChristoph Hellwig struct inode *inode = VFS_I(ip);
793673e8e59SChristoph Hellwig xfs_off_t oldsize, newsize;
794c59d87c4SChristoph Hellwig struct xfs_trans *tp;
795c59d87c4SChristoph Hellwig int error;
796f38996f5SChristoph Hellwig uint lock_flags = 0;
7975885ebdaSDave Chinner bool did_zeroing = false;
798c59d87c4SChristoph Hellwig
79976ca4c23SChristoph Hellwig ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
800e8e9ad42SDave Chinner ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
801c19b3b05SDave Chinner ASSERT(S_ISREG(inode->i_mode));
802fe60a8a0SChristoph Hellwig ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
80388a9e03bSYumei Huang ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
804c59d87c4SChristoph Hellwig
805ce7ae151SChristoph Hellwig oldsize = inode->i_size;
806673e8e59SChristoph Hellwig newsize = iattr->ia_size;
807673e8e59SChristoph Hellwig
808c59d87c4SChristoph Hellwig /*
809c59d87c4SChristoph Hellwig * Short circuit the truncate case for zero length files.
810c59d87c4SChristoph Hellwig */
811daf83964SChristoph Hellwig if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
812fe60a8a0SChristoph Hellwig if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
81376ca4c23SChristoph Hellwig return 0;
814c59d87c4SChristoph Hellwig
815c59d87c4SChristoph Hellwig /*
816c59d87c4SChristoph Hellwig * Use the regular setattr path to update the timestamps.
817c59d87c4SChristoph Hellwig */
818c59d87c4SChristoph Hellwig iattr->ia_valid &= ~ATTR_SIZE;
819c1632a0fSChristian Brauner return xfs_setattr_nonsize(idmap, dentry, ip, iattr);
820c59d87c4SChristoph Hellwig }
821c59d87c4SChristoph Hellwig
822c59d87c4SChristoph Hellwig /*
823c59d87c4SChristoph Hellwig * Make sure that the dquots are attached to the inode.
824c59d87c4SChristoph Hellwig */
825c14cfccaSDarrick J. Wong error = xfs_qm_dqattach(ip);
826c59d87c4SChristoph Hellwig if (error)
82776ca4c23SChristoph Hellwig return error;
828c59d87c4SChristoph Hellwig
829c59d87c4SChristoph Hellwig /*
830f0c6bcbaSChristoph Hellwig * Wait for all direct I/O to complete.
831f0c6bcbaSChristoph Hellwig */
832f0c6bcbaSChristoph Hellwig inode_dio_wait(inode);
833f0c6bcbaSChristoph Hellwig
834f0c6bcbaSChristoph Hellwig /*
8355885ebdaSDave Chinner * File data changes must be complete before we start the transaction to
8365885ebdaSDave Chinner * modify the inode. This needs to be done before joining the inode to
8375885ebdaSDave Chinner * the transaction because the inode cannot be unlocked once it is a
8385885ebdaSDave Chinner * part of the transaction.
8395885ebdaSDave Chinner *
840f0c6bcbaSChristoph Hellwig * Start with zeroing any data beyond EOF that we may expose on file
841f0c6bcbaSChristoph Hellwig * extension, or zeroing out the rest of the block on a downward
842f0c6bcbaSChristoph Hellwig * truncate.
843c59d87c4SChristoph Hellwig */
844673e8e59SChristoph Hellwig if (newsize > oldsize) {
845f5c54717SChristoph Hellwig trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
846f1ba5fafSShiyang Ruan error = xfs_zero_range(ip, oldsize, newsize - oldsize,
847f1ba5fafSShiyang Ruan &did_zeroing);
848f0c6bcbaSChristoph Hellwig } else {
849869ae85dSBrian Foster /*
850869ae85dSBrian Foster * iomap won't detect a dirty page over an unwritten block (or a
851869ae85dSBrian Foster * cow block over a hole) and subsequently skips zeroing the
852869ae85dSBrian Foster * newly post-EOF portion of the page. Flush the new EOF to
853869ae85dSBrian Foster * convert the block before the pagecache truncate.
854869ae85dSBrian Foster */
855869ae85dSBrian Foster error = filemap_write_and_wait_range(inode->i_mapping, newsize,
856869ae85dSBrian Foster newsize);
857869ae85dSBrian Foster if (error)
858869ae85dSBrian Foster return error;
859f1ba5fafSShiyang Ruan error = xfs_truncate_page(ip, newsize, &did_zeroing);
860f0c6bcbaSChristoph Hellwig }
861f0c6bcbaSChristoph Hellwig
862c59d87c4SChristoph Hellwig if (error)
86376ca4c23SChristoph Hellwig return error;
864c59d87c4SChristoph Hellwig
865c59d87c4SChristoph Hellwig /*
8660f9160b4SDave Chinner * We've already locked out new page faults, so now we can safely remove
8670f9160b4SDave Chinner * pages from the page cache knowing they won't get refaulted until we
8680f9160b4SDave Chinner * drop the XFS_MMAP_EXCL lock after the extent manipulations are
8690f9160b4SDave Chinner * complete. The truncate_setsize() call also cleans partial EOF page
8700f9160b4SDave Chinner * PTEs on extending truncates and hence ensures sub-page block size
8710f9160b4SDave Chinner * filesystems are correctly handled, too.
87249abc3a8SDave Chinner *
8730f9160b4SDave Chinner * We have to do all the page cache truncate work outside the
8740f9160b4SDave Chinner * transaction context as the "lock" order is page lock->log space
8750f9160b4SDave Chinner * reservation as defined by extent allocation in the writeback path.
876253f4911SChristoph Hellwig * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
8770f9160b4SDave Chinner * having already truncated the in-memory version of the file (i.e. made
8780f9160b4SDave Chinner * user visible changes). There's not much we can do about this, except
8790f9160b4SDave Chinner * to hope that the caller sees ENOMEM and retries the truncate
8800f9160b4SDave Chinner * operation.
881350976aeSEryu Guan *
882350976aeSEryu Guan * And we update in-core i_size and truncate page cache beyond newsize
88313d2c10bSChristoph Hellwig * before writeback the [i_disk_size, newsize] range, so we're
88413d2c10bSChristoph Hellwig * guaranteed not to write stale data past the new EOF on truncate down.
88549abc3a8SDave Chinner */
88649abc3a8SDave Chinner truncate_setsize(inode, newsize);
887c59d87c4SChristoph Hellwig
888350976aeSEryu Guan /*
889350976aeSEryu Guan * We are going to log the inode size change in this transaction so
890350976aeSEryu Guan * any previous writes that are beyond the on disk EOF and the new
891350976aeSEryu Guan * EOF that have not been written out need to be written here. If we
892350976aeSEryu Guan * do not write the data out, we expose ourselves to the null files
893350976aeSEryu Guan * problem. Note that this includes any block zeroing we did above;
894350976aeSEryu Guan * otherwise those blocks may not be zeroed after a crash.
895350976aeSEryu Guan */
896350976aeSEryu Guan if (did_zeroing ||
89713d2c10bSChristoph Hellwig (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
898350976aeSEryu Guan error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
89913d2c10bSChristoph Hellwig ip->i_disk_size, newsize - 1);
900350976aeSEryu Guan if (error)
901350976aeSEryu Guan return error;
902350976aeSEryu Guan }
903350976aeSEryu Guan
904253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
905c59d87c4SChristoph Hellwig if (error)
906253f4911SChristoph Hellwig return error;
907c59d87c4SChristoph Hellwig
908c59d87c4SChristoph Hellwig lock_flags |= XFS_ILOCK_EXCL;
909c59d87c4SChristoph Hellwig xfs_ilock(ip, XFS_ILOCK_EXCL);
910ddc3415aSChristoph Hellwig xfs_trans_ijoin(tp, ip, 0);
911c59d87c4SChristoph Hellwig
912c59d87c4SChristoph Hellwig /*
913c59d87c4SChristoph Hellwig * Only change the c/mtime if we are changing the size or we are
914c59d87c4SChristoph Hellwig * explicitly asked to change it. This handles the semantic difference
915c59d87c4SChristoph Hellwig * between truncate() and ftruncate() as implemented in the VFS.
916c59d87c4SChristoph Hellwig *
917c59d87c4SChristoph Hellwig * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
918c59d87c4SChristoph Hellwig * special case where we need to update the times despite not having
919c59d87c4SChristoph Hellwig * these flags set. For all other operations the VFS set these flags
920c59d87c4SChristoph Hellwig * explicitly if it wants a timestamp update.
921c59d87c4SChristoph Hellwig */
922fe60a8a0SChristoph Hellwig if (newsize != oldsize &&
923fe60a8a0SChristoph Hellwig !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
924c59d87c4SChristoph Hellwig iattr->ia_ctime = iattr->ia_mtime =
925f798accdSChristian Brauner current_time(inode);
926fe60a8a0SChristoph Hellwig iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
927c59d87c4SChristoph Hellwig }
928c59d87c4SChristoph Hellwig
929673e8e59SChristoph Hellwig /*
930673e8e59SChristoph Hellwig * The first thing we do is set the size to new_size permanently on
931673e8e59SChristoph Hellwig * disk. This way we don't have to worry about anyone ever being able
932673e8e59SChristoph Hellwig * to look at the data being freed even in the face of a crash.
933673e8e59SChristoph Hellwig * What we're getting around here is the case where we free a block, it
934673e8e59SChristoph Hellwig * is allocated to another file, it is written to, and then we crash.
935673e8e59SChristoph Hellwig * If the new data gets written to the file but the log buffers
936673e8e59SChristoph Hellwig * containing the free and reallocation don't, then we'd end up with
937673e8e59SChristoph Hellwig * garbage in the blocks being freed. As long as we make the new size
938673e8e59SChristoph Hellwig * permanent before actually freeing any blocks it doesn't matter if
939673e8e59SChristoph Hellwig * they get written to.
940673e8e59SChristoph Hellwig */
94113d2c10bSChristoph Hellwig ip->i_disk_size = newsize;
942673e8e59SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
943673e8e59SChristoph Hellwig
944673e8e59SChristoph Hellwig if (newsize <= oldsize) {
945673e8e59SChristoph Hellwig error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
946c59d87c4SChristoph Hellwig if (error)
9474906e215SChristoph Hellwig goto out_trans_cancel;
948c59d87c4SChristoph Hellwig
949c59d87c4SChristoph Hellwig /*
950c59d87c4SChristoph Hellwig * Truncated "down", so we're removing references to old data
951c59d87c4SChristoph Hellwig * here - if we delay flushing for a long time, we expose
952c59d87c4SChristoph Hellwig * ourselves unduly to the notorious NULL files problem. So,
953c59d87c4SChristoph Hellwig * we mark this inode and flush it when the file is closed,
954c59d87c4SChristoph Hellwig * and do not wait the usual (long) time for writeout.
955c59d87c4SChristoph Hellwig */
956c59d87c4SChristoph Hellwig xfs_iflags_set(ip, XFS_ITRUNCATED);
95727b52867SBrian Foster
95827b52867SBrian Foster /* A truncate down always removes post-EOF blocks. */
95927b52867SBrian Foster xfs_inode_clear_eofblocks_tag(ip);
960c59d87c4SChristoph Hellwig }
961c59d87c4SChristoph Hellwig
962e014f37dSDarrick J. Wong ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
963c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr);
964c59d87c4SChristoph Hellwig xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
965c59d87c4SChristoph Hellwig
966ff6d6af2SBill O'Donnell XFS_STATS_INC(mp, xs_ig_attrchg);
967c59d87c4SChristoph Hellwig
9680560f31aSDave Chinner if (xfs_has_wsync(mp))
969c59d87c4SChristoph Hellwig xfs_trans_set_sync(tp);
970c59d87c4SChristoph Hellwig
97170393313SChristoph Hellwig error = xfs_trans_commit(tp);
972c59d87c4SChristoph Hellwig out_unlock:
973c59d87c4SChristoph Hellwig if (lock_flags)
974c59d87c4SChristoph Hellwig xfs_iunlock(ip, lock_flags);
975c59d87c4SChristoph Hellwig return error;
976c59d87c4SChristoph Hellwig
977c59d87c4SChristoph Hellwig out_trans_cancel:
9784906e215SChristoph Hellwig xfs_trans_cancel(tp);
979c59d87c4SChristoph Hellwig goto out_unlock;
980c59d87c4SChristoph Hellwig }
981c59d87c4SChristoph Hellwig
98269bca807SJan Kara int
xfs_vn_setattr_size(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)98369bca807SJan Kara xfs_vn_setattr_size(
984c1632a0fSChristian Brauner struct mnt_idmap *idmap,
985c59d87c4SChristoph Hellwig struct dentry *dentry,
986c59d87c4SChristoph Hellwig struct iattr *iattr)
987c59d87c4SChristoph Hellwig {
9882b0143b5SDavid Howells struct xfs_inode *ip = XFS_I(d_inode(dentry));
98976ca4c23SChristoph Hellwig int error;
99076ca4c23SChristoph Hellwig
99169bca807SJan Kara trace_xfs_setattr(ip);
99269bca807SJan Kara
993c1632a0fSChristian Brauner error = xfs_vn_change_ok(idmap, dentry, iattr);
99469bca807SJan Kara if (error)
99569bca807SJan Kara return error;
996c1632a0fSChristian Brauner return xfs_setattr_size(idmap, dentry, ip, iattr);
99769bca807SJan Kara }
99869bca807SJan Kara
99969bca807SJan Kara STATIC int
xfs_vn_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)100069bca807SJan Kara xfs_vn_setattr(
1001c1632a0fSChristian Brauner struct mnt_idmap *idmap,
100269bca807SJan Kara struct dentry *dentry,
100369bca807SJan Kara struct iattr *iattr)
100469bca807SJan Kara {
100526f88363SChristoph Hellwig struct inode *inode = d_inode(dentry);
100626f88363SChristoph Hellwig struct xfs_inode *ip = XFS_I(inode);
100769bca807SJan Kara int error;
100869bca807SJan Kara
100976ca4c23SChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) {
1010c63a8eaeSDan Williams uint iolock;
1011e8e9ad42SDave Chinner
101265523218SChristoph Hellwig xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
1013c63a8eaeSDan Williams iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
1014c63a8eaeSDan Williams
101569eb5fa1SDan Williams error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
1016c63a8eaeSDan Williams if (error) {
1017c63a8eaeSDan Williams xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1018c63a8eaeSDan Williams return error;
1019c63a8eaeSDan Williams }
1020c63a8eaeSDan Williams
1021c1632a0fSChristian Brauner error = xfs_vn_setattr_size(idmap, dentry, iattr);
102265523218SChristoph Hellwig xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
102376ca4c23SChristoph Hellwig } else {
102426f88363SChristoph Hellwig trace_xfs_setattr(ip);
102526f88363SChristoph Hellwig
1026c1632a0fSChristian Brauner error = xfs_vn_change_ok(idmap, dentry, iattr);
102726f88363SChristoph Hellwig if (!error)
1028c1632a0fSChristian Brauner error = xfs_setattr_nonsize(idmap, dentry, ip, iattr);
102976ca4c23SChristoph Hellwig }
103076ca4c23SChristoph Hellwig
10312451337dSDave Chinner return error;
1032c59d87c4SChristoph Hellwig }
1033c59d87c4SChristoph Hellwig
103469ff2826SChristoph Hellwig STATIC int
xfs_vn_update_time(struct inode * inode,int flags)103569ff2826SChristoph Hellwig xfs_vn_update_time(
103669ff2826SChristoph Hellwig struct inode *inode,
103769ff2826SChristoph Hellwig int flags)
103869ff2826SChristoph Hellwig {
103969ff2826SChristoph Hellwig struct xfs_inode *ip = XFS_I(inode);
104069ff2826SChristoph Hellwig struct xfs_mount *mp = ip->i_mount;
1041c3b1b131SChristoph Hellwig int log_flags = XFS_ILOG_TIMESTAMP;
104269ff2826SChristoph Hellwig struct xfs_trans *tp;
104369ff2826SChristoph Hellwig int error;
104451b0f3ebSJeff Layton struct timespec64 now;
104569ff2826SChristoph Hellwig
104669ff2826SChristoph Hellwig trace_xfs_update_time(ip);
104769ff2826SChristoph Hellwig
1048c3b1b131SChristoph Hellwig if (inode->i_sb->s_flags & SB_LAZYTIME) {
1049c3b1b131SChristoph Hellwig if (!((flags & S_VERSION) &&
1050541d4c79SJeff Layton inode_maybe_inc_iversion(inode, false))) {
1051541d4c79SJeff Layton generic_update_time(inode, flags);
1052541d4c79SJeff Layton return 0;
1053541d4c79SJeff Layton }
1054c3b1b131SChristoph Hellwig
1055c3b1b131SChristoph Hellwig /* Capture the iversion update that just occurred */
1056c3b1b131SChristoph Hellwig log_flags |= XFS_ILOG_CORE;
1057c3b1b131SChristoph Hellwig }
1058c3b1b131SChristoph Hellwig
1059253f4911SChristoph Hellwig error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1060253f4911SChristoph Hellwig if (error)
10612451337dSDave Chinner return error;
106269ff2826SChristoph Hellwig
106369ff2826SChristoph Hellwig xfs_ilock(ip, XFS_ILOCK_EXCL);
106451b0f3ebSJeff Layton if (flags & (S_CTIME|S_MTIME))
106551b0f3ebSJeff Layton now = inode_set_ctime_current(inode);
106651b0f3ebSJeff Layton else
106751b0f3ebSJeff Layton now = current_time(inode);
106851b0f3ebSJeff Layton
10693987848cSDave Chinner if (flags & S_MTIME)
107051b0f3ebSJeff Layton inode->i_mtime = now;
10713987848cSDave Chinner if (flags & S_ATIME)
107251b0f3ebSJeff Layton inode->i_atime = now;
10733987848cSDave Chinner
107469ff2826SChristoph Hellwig xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1075c3b1b131SChristoph Hellwig xfs_trans_log_inode(tp, ip, log_flags);
107670393313SChristoph Hellwig return xfs_trans_commit(tp);
107769ff2826SChristoph Hellwig }
107869ff2826SChristoph Hellwig
1079c59d87c4SChristoph Hellwig STATIC int
xfs_vn_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 length)1080c59d87c4SChristoph Hellwig xfs_vn_fiemap(
1081c59d87c4SChristoph Hellwig struct inode *inode,
1082c59d87c4SChristoph Hellwig struct fiemap_extent_info *fieinfo,
1083c59d87c4SChristoph Hellwig u64 start,
1084c59d87c4SChristoph Hellwig u64 length)
1085c59d87c4SChristoph Hellwig {
1086c59d87c4SChristoph Hellwig int error;
1087c59d87c4SChristoph Hellwig
1088d2bb140eSChristoph Hellwig xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
10891d4795e7SChristoph Hellwig if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
10901d4795e7SChristoph Hellwig fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
10911d4795e7SChristoph Hellwig error = iomap_fiemap(inode, fieinfo, start, length,
10921d4795e7SChristoph Hellwig &xfs_xattr_iomap_ops);
10931d4795e7SChristoph Hellwig } else {
10941d4795e7SChristoph Hellwig error = iomap_fiemap(inode, fieinfo, start, length,
1095690c2a38SChristoph Hellwig &xfs_read_iomap_ops);
10961d4795e7SChristoph Hellwig }
1097d2bb140eSChristoph Hellwig xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
1098d2bb140eSChristoph Hellwig
1099c59d87c4SChristoph Hellwig return error;
1100c59d87c4SChristoph Hellwig }
1101c59d87c4SChristoph Hellwig
110299b6436bSZhi Yong Wu STATIC int
xfs_vn_tmpfile(struct mnt_idmap * idmap,struct inode * dir,struct file * file,umode_t mode)110399b6436bSZhi Yong Wu xfs_vn_tmpfile(
1104011e2b71SChristian Brauner struct mnt_idmap *idmap,
110599b6436bSZhi Yong Wu struct inode *dir,
1106863f144fSMiklos Szeredi struct file *file,
110799b6436bSZhi Yong Wu umode_t mode)
110899b6436bSZhi Yong Wu {
1109f2d40141SChristian Brauner int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file);
1110863f144fSMiklos Szeredi
1111863f144fSMiklos Szeredi return finish_open_simple(file, err);
111299b6436bSZhi Yong Wu }
111399b6436bSZhi Yong Wu
1114c59d87c4SChristoph Hellwig static const struct inode_operations xfs_inode_operations = {
1115cac2f8b8SChristian Brauner .get_inode_acl = xfs_get_acl,
11162401dc29SChristoph Hellwig .set_acl = xfs_set_acl,
1117c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr,
1118c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr,
1119c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr,
1120c59d87c4SChristoph Hellwig .fiemap = xfs_vn_fiemap,
112169ff2826SChristoph Hellwig .update_time = xfs_vn_update_time,
11229fefd5dbSMiklos Szeredi .fileattr_get = xfs_fileattr_get,
11239fefd5dbSMiklos Szeredi .fileattr_set = xfs_fileattr_set,
1124c59d87c4SChristoph Hellwig };
1125c59d87c4SChristoph Hellwig
1126c59d87c4SChristoph Hellwig static const struct inode_operations xfs_dir_inode_operations = {
1127c59d87c4SChristoph Hellwig .create = xfs_vn_create,
1128c59d87c4SChristoph Hellwig .lookup = xfs_vn_lookup,
1129c59d87c4SChristoph Hellwig .link = xfs_vn_link,
1130c59d87c4SChristoph Hellwig .unlink = xfs_vn_unlink,
1131c59d87c4SChristoph Hellwig .symlink = xfs_vn_symlink,
1132c59d87c4SChristoph Hellwig .mkdir = xfs_vn_mkdir,
1133c59d87c4SChristoph Hellwig /*
1134c59d87c4SChristoph Hellwig * Yes, XFS uses the same method for rmdir and unlink.
1135c59d87c4SChristoph Hellwig *
1136c59d87c4SChristoph Hellwig * There are some subtile differences deeper in the code,
1137c59d87c4SChristoph Hellwig * but we use S_ISDIR to check for those.
1138c59d87c4SChristoph Hellwig */
1139c59d87c4SChristoph Hellwig .rmdir = xfs_vn_unlink,
1140c59d87c4SChristoph Hellwig .mknod = xfs_vn_mknod,
11412773bf00SMiklos Szeredi .rename = xfs_vn_rename,
1142cac2f8b8SChristian Brauner .get_inode_acl = xfs_get_acl,
11432401dc29SChristoph Hellwig .set_acl = xfs_set_acl,
1144c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr,
1145c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr,
1146c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr,
114769ff2826SChristoph Hellwig .update_time = xfs_vn_update_time,
114899b6436bSZhi Yong Wu .tmpfile = xfs_vn_tmpfile,
11499fefd5dbSMiklos Szeredi .fileattr_get = xfs_fileattr_get,
11509fefd5dbSMiklos Szeredi .fileattr_set = xfs_fileattr_set,
1151c59d87c4SChristoph Hellwig };
1152c59d87c4SChristoph Hellwig
1153c59d87c4SChristoph Hellwig static const struct inode_operations xfs_dir_ci_inode_operations = {
1154c59d87c4SChristoph Hellwig .create = xfs_vn_create,
1155c59d87c4SChristoph Hellwig .lookup = xfs_vn_ci_lookup,
1156c59d87c4SChristoph Hellwig .link = xfs_vn_link,
1157c59d87c4SChristoph Hellwig .unlink = xfs_vn_unlink,
1158c59d87c4SChristoph Hellwig .symlink = xfs_vn_symlink,
1159c59d87c4SChristoph Hellwig .mkdir = xfs_vn_mkdir,
1160c59d87c4SChristoph Hellwig /*
1161c59d87c4SChristoph Hellwig * Yes, XFS uses the same method for rmdir and unlink.
1162c59d87c4SChristoph Hellwig *
1163c59d87c4SChristoph Hellwig * There are some subtile differences deeper in the code,
1164c59d87c4SChristoph Hellwig * but we use S_ISDIR to check for those.
1165c59d87c4SChristoph Hellwig */
1166c59d87c4SChristoph Hellwig .rmdir = xfs_vn_unlink,
1167c59d87c4SChristoph Hellwig .mknod = xfs_vn_mknod,
11682773bf00SMiklos Szeredi .rename = xfs_vn_rename,
1169cac2f8b8SChristian Brauner .get_inode_acl = xfs_get_acl,
11702401dc29SChristoph Hellwig .set_acl = xfs_set_acl,
1171c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr,
1172c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr,
1173c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr,
117469ff2826SChristoph Hellwig .update_time = xfs_vn_update_time,
117599b6436bSZhi Yong Wu .tmpfile = xfs_vn_tmpfile,
11769fefd5dbSMiklos Szeredi .fileattr_get = xfs_fileattr_get,
11779fefd5dbSMiklos Szeredi .fileattr_set = xfs_fileattr_set,
1178c59d87c4SChristoph Hellwig };
1179c59d87c4SChristoph Hellwig
1180c59d87c4SChristoph Hellwig static const struct inode_operations xfs_symlink_inode_operations = {
11816b255391SAl Viro .get_link = xfs_vn_get_link,
1182c59d87c4SChristoph Hellwig .getattr = xfs_vn_getattr,
1183c59d87c4SChristoph Hellwig .setattr = xfs_vn_setattr,
1184c59d87c4SChristoph Hellwig .listxattr = xfs_vn_listxattr,
118569ff2826SChristoph Hellwig .update_time = xfs_vn_update_time,
1186c59d87c4SChristoph Hellwig };
1187c59d87c4SChristoph Hellwig
1188ba23cba9SDarrick J. Wong /* Figure out if this file actually supports DAX. */
1189ba23cba9SDarrick J. Wong static bool
xfs_inode_supports_dax(struct xfs_inode * ip)1190ba23cba9SDarrick J. Wong xfs_inode_supports_dax(
1191ba23cba9SDarrick J. Wong struct xfs_inode *ip)
1192ba23cba9SDarrick J. Wong {
1193ba23cba9SDarrick J. Wong struct xfs_mount *mp = ip->i_mount;
1194ba23cba9SDarrick J. Wong
119532dbc565SIra Weiny /* Only supported on regular files. */
119632dbc565SIra Weiny if (!S_ISREG(VFS_I(ip)->i_mode))
1197ba23cba9SDarrick J. Wong return false;
1198ba23cba9SDarrick J. Wong
1199ba23cba9SDarrick J. Wong /* Block size must match page size */
1200ba23cba9SDarrick J. Wong if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1201ba23cba9SDarrick J. Wong return false;
1202ba23cba9SDarrick J. Wong
1203ba23cba9SDarrick J. Wong /* Device has to support DAX too. */
120430fa529eSChristoph Hellwig return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
1205ba23cba9SDarrick J. Wong }
1206ba23cba9SDarrick J. Wong
120732dbc565SIra Weiny static bool
xfs_inode_should_enable_dax(struct xfs_inode * ip)120832dbc565SIra Weiny xfs_inode_should_enable_dax(
120932dbc565SIra Weiny struct xfs_inode *ip)
121032dbc565SIra Weiny {
121132dbc565SIra Weiny if (!IS_ENABLED(CONFIG_FS_DAX))
121232dbc565SIra Weiny return false;
12130560f31aSDave Chinner if (xfs_has_dax_never(ip->i_mount))
121432dbc565SIra Weiny return false;
121532dbc565SIra Weiny if (!xfs_inode_supports_dax(ip))
121632dbc565SIra Weiny return false;
12170560f31aSDave Chinner if (xfs_has_dax_always(ip->i_mount))
121832dbc565SIra Weiny return true;
12193e09ab8fSChristoph Hellwig if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
122032dbc565SIra Weiny return true;
122132dbc565SIra Weiny return false;
122232dbc565SIra Weiny }
122332dbc565SIra Weiny
1224840d493dSIra Weiny void
xfs_diflags_to_iflags(struct xfs_inode * ip,bool init)1225c59d87c4SChristoph Hellwig xfs_diflags_to_iflags(
1226840d493dSIra Weiny struct xfs_inode *ip,
1227840d493dSIra Weiny bool init)
1228c59d87c4SChristoph Hellwig {
1229840d493dSIra Weiny struct inode *inode = VFS_I(ip);
1230840d493dSIra Weiny unsigned int xflags = xfs_ip2xflags(ip);
1231840d493dSIra Weiny unsigned int flags = 0;
1232cbe4dab1SDave Chinner
1233840d493dSIra Weiny ASSERT(!(IS_DAX(inode) && init));
1234cbe4dab1SDave Chinner
1235840d493dSIra Weiny if (xflags & FS_XFLAG_IMMUTABLE)
1236840d493dSIra Weiny flags |= S_IMMUTABLE;
1237840d493dSIra Weiny if (xflags & FS_XFLAG_APPEND)
1238840d493dSIra Weiny flags |= S_APPEND;
1239840d493dSIra Weiny if (xflags & FS_XFLAG_SYNC)
1240840d493dSIra Weiny flags |= S_SYNC;
1241840d493dSIra Weiny if (xflags & FS_XFLAG_NOATIME)
1242840d493dSIra Weiny flags |= S_NOATIME;
1243840d493dSIra Weiny if (init && xfs_inode_should_enable_dax(ip))
1244840d493dSIra Weiny flags |= S_DAX;
1245840d493dSIra Weiny
1246840d493dSIra Weiny /*
1247840d493dSIra Weiny * S_DAX can only be set during inode initialization and is never set by
1248840d493dSIra Weiny * the VFS, so we cannot mask off S_DAX in i_flags.
1249840d493dSIra Weiny */
1250840d493dSIra Weiny inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
1251840d493dSIra Weiny inode->i_flags |= flags;
1252c59d87c4SChristoph Hellwig }
1253c59d87c4SChristoph Hellwig
1254c59d87c4SChristoph Hellwig /*
12552b3d1d41SChristoph Hellwig * Initialize the Linux inode.
1256c59d87c4SChristoph Hellwig *
125758c90473SDave Chinner * When reading existing inodes from disk this is called directly from xfs_iget,
1258132c460eSYang Xu * when creating a new inode it is called from xfs_init_new_inode after setting
1259132c460eSYang Xu * up the inode. These callers have different criteria for clearing XFS_INEW, so
1260132c460eSYang Xu * leave it up to the caller to deal with unlocking the inode appropriately.
1261c59d87c4SChristoph Hellwig */
1262c59d87c4SChristoph Hellwig void
xfs_setup_inode(struct xfs_inode * ip)1263c59d87c4SChristoph Hellwig xfs_setup_inode(
1264c59d87c4SChristoph Hellwig struct xfs_inode *ip)
1265c59d87c4SChristoph Hellwig {
1266c59d87c4SChristoph Hellwig struct inode *inode = &ip->i_vnode;
1267ad22c7a0SDave Chinner gfp_t gfp_mask;
1268c59d87c4SChristoph Hellwig
1269c59d87c4SChristoph Hellwig inode->i_ino = ip->i_ino;
1270f38a032bSDave Chinner inode->i_state |= I_NEW;
1271c59d87c4SChristoph Hellwig
1272c59d87c4SChristoph Hellwig inode_sb_list_add(inode);
1273c59d87c4SChristoph Hellwig /* make the inode look hashed for the writeback code */
12745bef9151SAl Viro inode_fake_hash(inode);
1275c59d87c4SChristoph Hellwig
127613d2c10bSChristoph Hellwig i_size_write(inode, ip->i_disk_size);
1277840d493dSIra Weiny xfs_diflags_to_iflags(ip, true);
1278c59d87c4SChristoph Hellwig
12792b3d1d41SChristoph Hellwig if (S_ISDIR(inode->i_mode)) {
1280ef215e39SDave Chinner /*
1281ef215e39SDave Chinner * We set the i_rwsem class here to avoid potential races with
1282ef215e39SDave Chinner * lockdep_annotate_inode_mutex_key() reinitialising the lock
1283ef215e39SDave Chinner * after a filehandle lookup has already found the inode in
1284ef215e39SDave Chinner * cache before it has been unlocked via unlock_new_inode().
1285ef215e39SDave Chinner */
1286ef215e39SDave Chinner lockdep_set_class(&inode->i_rwsem,
1287ef215e39SDave Chinner &inode->i_sb->s_type->i_mutex_dir_key);
12882b3d1d41SChristoph Hellwig lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
12892b3d1d41SChristoph Hellwig } else {
129093a8614eSDave Chinner lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
1291c59d87c4SChristoph Hellwig }
1292c59d87c4SChristoph Hellwig
1293c59d87c4SChristoph Hellwig /*
1294ad22c7a0SDave Chinner * Ensure all page cache allocations are done from GFP_NOFS context to
1295ad22c7a0SDave Chinner * prevent direct reclaim recursion back into the filesystem and blowing
1296ad22c7a0SDave Chinner * stacks or deadlocking.
1297ad22c7a0SDave Chinner */
1298ad22c7a0SDave Chinner gfp_mask = mapping_gfp_mask(inode->i_mapping);
1299ad22c7a0SDave Chinner mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1300ad22c7a0SDave Chinner
1301ad22c7a0SDave Chinner /*
1302*05955a70SChristoph Hellwig * For real-time inodes update the stable write flags to that of the RT
1303*05955a70SChristoph Hellwig * device instead of the data device.
1304*05955a70SChristoph Hellwig */
1305*05955a70SChristoph Hellwig if (S_ISREG(inode->i_mode) && XFS_IS_REALTIME_INODE(ip))
1306*05955a70SChristoph Hellwig xfs_update_stable_writes(ip);
1307*05955a70SChristoph Hellwig
1308*05955a70SChristoph Hellwig /*
1309c59d87c4SChristoph Hellwig * If there is no attribute fork no ACL can exist on this inode,
1310c59d87c4SChristoph Hellwig * and it can't have any file capabilities attached to it either.
1311c59d87c4SChristoph Hellwig */
1312932b42c6SDarrick J. Wong if (!xfs_inode_has_attr_fork(ip)) {
1313c59d87c4SChristoph Hellwig inode_has_no_xattr(inode);
1314c59d87c4SChristoph Hellwig cache_no_acl(inode);
1315c59d87c4SChristoph Hellwig }
1316c59d87c4SChristoph Hellwig }
13172b3d1d41SChristoph Hellwig
13182b3d1d41SChristoph Hellwig void
xfs_setup_iops(struct xfs_inode * ip)13192b3d1d41SChristoph Hellwig xfs_setup_iops(
13202b3d1d41SChristoph Hellwig struct xfs_inode *ip)
13212b3d1d41SChristoph Hellwig {
13222b3d1d41SChristoph Hellwig struct inode *inode = &ip->i_vnode;
13232b3d1d41SChristoph Hellwig
13242b3d1d41SChristoph Hellwig switch (inode->i_mode & S_IFMT) {
13252b3d1d41SChristoph Hellwig case S_IFREG:
13262b3d1d41SChristoph Hellwig inode->i_op = &xfs_inode_operations;
13272b3d1d41SChristoph Hellwig inode->i_fop = &xfs_file_operations;
13286e2608dfSDan Williams if (IS_DAX(inode))
13296e2608dfSDan Williams inode->i_mapping->a_ops = &xfs_dax_aops;
13306e2608dfSDan Williams else
13312b3d1d41SChristoph Hellwig inode->i_mapping->a_ops = &xfs_address_space_operations;
13322b3d1d41SChristoph Hellwig break;
13332b3d1d41SChristoph Hellwig case S_IFDIR:
133438c26bfdSDave Chinner if (xfs_has_asciici(XFS_M(inode->i_sb)))
13352b3d1d41SChristoph Hellwig inode->i_op = &xfs_dir_ci_inode_operations;
13362b3d1d41SChristoph Hellwig else
13372b3d1d41SChristoph Hellwig inode->i_op = &xfs_dir_inode_operations;
13382b3d1d41SChristoph Hellwig inode->i_fop = &xfs_dir_file_operations;
13392b3d1d41SChristoph Hellwig break;
13402b3d1d41SChristoph Hellwig case S_IFLNK:
13412b3d1d41SChristoph Hellwig inode->i_op = &xfs_symlink_inode_operations;
13422b3d1d41SChristoph Hellwig break;
13432b3d1d41SChristoph Hellwig default:
13442b3d1d41SChristoph Hellwig inode->i_op = &xfs_inode_operations;
13452b3d1d41SChristoph Hellwig init_special_inode(inode, inode->i_mode, inode->i_rdev);
13462b3d1d41SChristoph Hellwig break;
13472b3d1d41SChristoph Hellwig }
13482b3d1d41SChristoph Hellwig }
1349