blob: 80a13c8561d85a4904f2477d2bc7d2026b31ff36 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
7#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110014#include "xfs_acl.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_attr.h"
Dave Chinner239880e2013-10-23 10:50:10 +110017#include "xfs_trans.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000018#include "xfs_trace.h"
Brian Foster27b52862012-11-06 09:50:38 -050019#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100020#include "xfs_symlink.h"
Dave Chinner1b767ee2014-12-04 09:43:17 +110021#include "xfs_dir2.h"
Christoph Hellwig68a9f5e2016-06-21 09:53:44 +100022#include "xfs_iomap.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070023#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020025#include <linux/posix_acl.h>
Nathan Scott446ada42006-01-11 15:35:44 +110026#include <linux/security.h>
Christoph Hellwigc3b1b132018-03-06 17:04:00 -080027#include <linux/iversion.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020028#include <linux/fiemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Dave Chinner93a86142014-02-27 16:51:39 +110030/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070031 * Directories have different lock order w.r.t. mmap_lock compared to regular
Dave Chinner93a86142014-02-27 16:51:39 +110032 * files. This is due to readdir potentially triggering page faults on a user
33 * buffer inside filldir(), and this happens with the ilock on the directory
34 * held. For regular files, the lock order is the other way around - the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070035 * mmap_lock is taken during the page fault, and then we lock the ilock to do
Dave Chinner93a86142014-02-27 16:51:39 +110036 * block mapping. Hence we need a different class for the directory ilock so
37 * that lockdep can tell them apart.
38 */
39static struct lock_class_key xfs_nondir_ilock_class;
40static struct lock_class_key xfs_dir_ilock_class;
41
Dave Chinner8d2a5e62012-03-07 04:50:19 +000042static int
43xfs_initxattrs(
44 struct inode *inode,
45 const struct xattr *xattr_array,
46 void *fs_info)
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040047{
Dave Chinner8d2a5e62012-03-07 04:50:19 +000048 const struct xattr *xattr;
49 struct xfs_inode *ip = XFS_I(inode);
50 int error = 0;
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040051
52 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Christoph Hellwiga2544622020-02-26 17:30:33 -080053 struct xfs_da_args args = {
54 .dp = ip,
Christoph Hellwigd5f0f492020-02-26 17:30:42 -080055 .attr_filter = XFS_ATTR_SECURE,
Christoph Hellwiga2544622020-02-26 17:30:33 -080056 .name = xattr->name,
57 .namelen = strlen(xattr->name),
58 .value = xattr->value,
59 .valuelen = xattr->value_len,
60 };
61 error = xfs_attr_set(&args);
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040062 if (error < 0)
63 break;
64 }
65 return error;
66}
67
David Chinner5d51eff2007-11-23 16:29:18 +110068/*
Nathan Scott446ada42006-01-11 15:35:44 +110069 * Hook in SELinux. This is not quite correct yet, what we really need
70 * here (as we do for default ACLs) is a mechanism by which creation of
71 * these attrs can be journalled at inode creation time (along with the
72 * inode, of course, such that log replay can't cause these to be lost).
73 */
Mimi Zohar9d8f13b2011-06-06 15:29:25 -040074
Nathan Scott446ada42006-01-11 15:35:44 +110075STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +110076xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +110077 struct inode *inode,
Eric Paris2a7dba32011-02-01 11:05:39 -050078 struct inode *dir,
79 const struct qstr *qstr)
Nathan Scott446ada42006-01-11 15:35:44 +110080{
Dave Chinner24513372014-06-25 14:58:08 +100081 return security_inode_init_security(inode, dir, qstr,
Dave Chinnera5a14de2014-05-15 09:22:21 +100082 &xfs_initxattrs, NULL);
Nathan Scott446ada42006-01-11 15:35:44 +110083}
84
Barry Naujok556b8b12008-04-10 12:22:07 +100085static void
86xfs_dentry_to_name(
87 struct xfs_name *namep,
Amir Goldsteinfab8eef2017-01-17 11:41:44 -080088 struct dentry *dentry)
89{
90 namep->name = dentry->d_name.name;
91 namep->len = dentry->d_name.len;
92 namep->type = XFS_DIR3_FT_UNKNOWN;
93}
94
95static int
96xfs_dentry_mode_to_name(
97 struct xfs_name *namep,
Dave Chinner0cb97762013-08-12 20:50:09 +100098 struct dentry *dentry,
99 int mode)
Barry Naujok556b8b12008-04-10 12:22:07 +1000100{
101 namep->name = dentry->d_name.name;
102 namep->len = dentry->d_name.len;
Amir Goldstein1fc4d332017-01-17 11:41:43 -0800103 namep->type = xfs_mode_to_ftype(mode);
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800104
105 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
106 return -EFSCORRUPTED;
107
108 return 0;
Barry Naujok556b8b12008-04-10 12:22:07 +1000109}
110
David Chinner7989cb82007-02-10 18:34:56 +1100111STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100112xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000113 struct inode *dir,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100114 struct inode *inode,
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000115 struct dentry *dentry)
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100116{
Barry Naujok556b8b12008-04-10 12:22:07 +1000117 struct xfs_name teardown;
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100118
119 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100120 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100121 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100122 * ENOSPC can hit here, among other things.
123 */
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800124 xfs_dentry_to_name(&teardown, dentry);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100125
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000126 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129STATIC int
Brian Fosterd540e432014-05-06 07:34:28 +1000130xfs_generic_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct inode *dir,
132 struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400133 umode_t mode,
Brian Fosterd540e432014-05-06 07:34:28 +1000134 dev_t rdev,
135 bool tmpfile) /* unnamed file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100137 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100138 struct xfs_inode *ip = NULL;
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800139 struct posix_acl *default_acl, *acl;
Barry Naujok556b8b12008-04-10 12:22:07 +1000140 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int error;
142
143 /*
144 * Irix uses Missed'em'V split, but doesn't want to see
145 * the upper 5 bits of (14bit) major.
146 */
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100147 if (S_ISCHR(mode) || S_ISBLK(mode)) {
148 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
149 return -EINVAL;
Christoph Hellwig517b5e82009-02-09 08:38:02 +0100150 } else {
151 rdev = 0;
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800154 error = posix_acl_create(dir, &mode, &default_acl, &acl);
155 if (error)
156 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800158 /* Verify mode is valid also for tmpfile case */
159 error = xfs_dentry_mode_to_name(&name, dentry, mode);
160 if (unlikely(error))
161 goto out_free_acl;
162
Brian Fosterd540e432014-05-06 07:34:28 +1000163 if (!tmpfile) {
Brian Fosterd540e432014-05-06 07:34:28 +1000164 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
165 } else {
Eric Sandeena1f69412018-04-06 10:09:42 -0700166 error = xfs_create_tmpfile(XFS_I(dir), mode, &ip);
Brian Fosterd540e432014-05-06 07:34:28 +1000167 }
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100168 if (unlikely(error))
169 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100170
David Chinner01651642008-08-13 15:45:15 +1000171 inode = VFS_I(ip);
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100172
Eric Paris2a7dba32011-02-01 11:05:39 -0500173 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100174 if (unlikely(error))
175 goto out_cleanup_inode;
176
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800177#ifdef CONFIG_XFS_POSIX_ACL
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100178 if (default_acl) {
Jan Kara8ba35872017-06-26 08:48:18 -0700179 error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800180 if (error)
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100181 goto out_cleanup_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800183 if (acl) {
Jan Kara8ba35872017-06-26 08:48:18 -0700184 error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800185 if (error)
186 goto out_cleanup_inode;
187 }
188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +1000190 xfs_setup_iops(ip);
191
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -0800192 if (tmpfile) {
193 /*
194 * The VFS requires that any inode fed to d_tmpfile must have
195 * nlink == 1 so that it can decrement the nlink in d_tmpfile.
196 * However, we created the temp file with nlink == 0 because
197 * we're not allowed to put an inode with nlink > 0 on the
198 * unlinked list. Therefore we have to set nlink to 1 so that
199 * d_tmpfile can immediately set it back to zero.
200 */
201 set_nlink(inode, 1);
Brian Fosterd540e432014-05-06 07:34:28 +1000202 d_tmpfile(dentry, inode);
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -0800203 } else
Brian Fosterd540e432014-05-06 07:34:28 +1000204 d_instantiate(dentry, inode);
205
Dave Chinner58c90472015-02-23 22:38:08 +1100206 xfs_finish_inode_setup(ip);
207
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800208 out_free_acl:
209 if (default_acl)
210 posix_acl_release(default_acl);
211 if (acl)
212 posix_acl_release(acl);
Dave Chinner24513372014-06-25 14:58:08 +1000213 return error;
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100214
215 out_cleanup_inode:
Dave Chinner58c90472015-02-23 22:38:08 +1100216 xfs_finish_inode_setup(ip);
Brian Fosterd540e432014-05-06 07:34:28 +1000217 if (!tmpfile)
218 xfs_cleanup_inode(dir, inode, dentry);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700219 xfs_irele(ip);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800220 goto out_free_acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223STATIC int
Brian Fosterd540e432014-05-06 07:34:28 +1000224xfs_vn_mknod(
225 struct inode *dir,
226 struct dentry *dentry,
227 umode_t mode,
228 dev_t rdev)
229{
230 return xfs_generic_create(dir, dentry, mode, rdev, false);
231}
232
233STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100234xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct inode *dir,
236 struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -0400237 umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400238 bool flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Nathan Scott416c6d52006-03-14 14:00:51 +1100240 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100244xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct inode *dir,
246 struct dentry *dentry,
Al Viro18bb1db2011-07-26 01:41:39 -0400247 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Nathan Scott416c6d52006-03-14 14:00:51 +1100249 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100253xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct inode *dir,
255 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400256 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Al Virob113a6d2018-04-30 23:04:19 -0400258 struct inode *inode;
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100259 struct xfs_inode *cip;
Barry Naujok556b8b12008-04-10 12:22:07 +1000260 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int error;
262
263 if (dentry->d_name.len >= MAXNAMELEN)
264 return ERR_PTR(-ENAMETOOLONG);
265
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800266 xfs_dentry_to_name(&name, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000267 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
Al Virob113a6d2018-04-30 23:04:19 -0400268 if (likely(!error))
269 inode = VFS_I(cip);
270 else if (likely(error == -ENOENT))
271 inode = NULL;
272 else
273 inode = ERR_PTR(error);
274 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Barry Naujok384f3ce2008-05-21 16:58:22 +1000277STATIC struct dentry *
278xfs_vn_ci_lookup(
279 struct inode *dir,
280 struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400281 unsigned int flags)
Barry Naujok384f3ce2008-05-21 16:58:22 +1000282{
283 struct xfs_inode *ip;
284 struct xfs_name xname;
285 struct xfs_name ci_name;
286 struct qstr dname;
287 int error;
288
289 if (dentry->d_name.len >= MAXNAMELEN)
290 return ERR_PTR(-ENAMETOOLONG);
291
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800292 xfs_dentry_to_name(&xname, dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000293 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
294 if (unlikely(error)) {
Dave Chinner24513372014-06-25 14:58:08 +1000295 if (unlikely(error != -ENOENT))
296 return ERR_PTR(error);
Barry Naujok866d5dc2008-05-22 17:21:40 +1000297 /*
298 * call d_add(dentry, NULL) here when d_drop_negative_children
299 * is called in xfs_vn_mknod (ie. allow negative dentries
300 * with CI filesystems).
301 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000302 return NULL;
303 }
304
305 /* if exact match, just splice and exit */
306 if (!ci_name.name)
David Chinner01651642008-08-13 15:45:15 +1000307 return d_splice_alias(VFS_I(ip), dentry);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000308
309 /* else case-insensitive match... */
310 dname.name = ci_name.name;
311 dname.len = ci_name.len;
Christoph Hellwige45b5902008-08-07 23:49:07 +0200312 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000313 kmem_free(ci_name.name);
314 return dentry;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100318xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct dentry *old_dentry,
320 struct inode *dir,
321 struct dentry *dentry)
322{
David Howells2b0143b2015-03-17 22:25:59 +0000323 struct inode *inode = d_inode(old_dentry);
Barry Naujok556b8b12008-04-10 12:22:07 +1000324 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int error;
326
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800327 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
328 if (unlikely(error))
329 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Barry Naujok556b8b12008-04-10 12:22:07 +1000331 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
Christoph Hellwigd9424b32008-12-03 12:20:27 +0100332 if (unlikely(error))
Dave Chinner24513372014-06-25 14:58:08 +1000333 return error;
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100334
Al Viro7de9c6ee2010-10-23 11:11:40 -0400335 ihold(inode);
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100336 d_instantiate(dentry, inode);
337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100341xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct inode *dir,
343 struct dentry *dentry)
344{
Barry Naujok556b8b12008-04-10 12:22:07 +1000345 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int error;
347
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800348 xfs_dentry_to_name(&name, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
David Howells2b0143b2015-03-17 22:25:59 +0000350 error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
Christoph Hellwige5700702008-06-23 13:25:25 +1000351 if (error)
352 return error;
353
354 /*
355 * With unlink, the VFS makes the dentry "negative": no inode,
356 * but still hashed. This is incompatible with case-insensitive
357 * mode, so invalidate (unhash) the dentry in CI-mode.
358 */
359 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
360 d_invalidate(dentry);
361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100365xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct inode *dir,
367 struct dentry *dentry,
368 const char *symname)
369{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100370 struct inode *inode;
371 struct xfs_inode *cip = NULL;
Barry Naujok556b8b12008-04-10 12:22:07 +1000372 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int error;
Al Viro576b1d62011-07-26 02:50:15 -0400374 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000376 mode = S_IFLNK |
Al Viroce3b0f82009-03-29 19:08:22 -0400377 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800378 error = xfs_dentry_mode_to_name(&name, dentry, mode);
379 if (unlikely(error))
380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Christoph Hellwig6c77b0e2010-10-06 18:41:17 +0000382 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100383 if (unlikely(error))
384 goto out;
385
David Chinner01651642008-08-13 15:45:15 +1000386 inode = VFS_I(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100387
Eric Paris2a7dba32011-02-01 11:05:39 -0500388 error = xfs_init_security(inode, dir, &dentry->d_name);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100389 if (unlikely(error))
390 goto out_cleanup_inode;
391
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +1000392 xfs_setup_iops(cip);
393
Christoph Hellwig3937be52008-03-06 13:46:19 +1100394 d_instantiate(dentry, inode);
Dave Chinner58c90472015-02-23 22:38:08 +1100395 xfs_finish_inode_setup(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100396 return 0;
397
398 out_cleanup_inode:
Dave Chinner58c90472015-02-23 22:38:08 +1100399 xfs_finish_inode_setup(cip);
Christoph Hellwig8f112e32008-06-23 13:25:17 +1000400 xfs_cleanup_inode(dir, inode, dentry);
Darrick J. Wong44a87362018-07-25 12:52:32 -0700401 xfs_irele(cip);
Christoph Hellwig3937be52008-03-06 13:46:19 +1100402 out:
Dave Chinner24513372014-06-25 14:58:08 +1000403 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100407xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct inode *odir,
409 struct dentry *odentry,
410 struct inode *ndir,
Carlos Maiolinodbe1b5c2014-12-24 08:51:38 +1100411 struct dentry *ndentry,
412 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
David Howells2b0143b2015-03-17 22:25:59 +0000414 struct inode *new_inode = d_inode(ndentry);
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100415 int omode = 0;
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800416 int error;
Barry Naujok556b8b12008-04-10 12:22:07 +1000417 struct xfs_name oname;
418 struct xfs_name nname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Dave Chinner7dcf5c32015-03-25 14:08:08 +1100420 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Carlos Maiolinodbe1b5c2014-12-24 08:51:38 +1100421 return -EINVAL;
422
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100423 /* if we are exchanging files, we need to set i_mode of both files */
424 if (flags & RENAME_EXCHANGE)
David Howells2b0143b2015-03-17 22:25:59 +0000425 omode = d_inode(ndentry)->i_mode;
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100426
Amir Goldsteinfab8eef2017-01-17 11:41:44 -0800427 error = xfs_dentry_mode_to_name(&oname, odentry, omode);
428 if (omode && unlikely(error))
429 return error;
430
431 error = xfs_dentry_mode_to_name(&nname, ndentry,
432 d_inode(odentry)->i_mode);
433 if (unlikely(error))
434 return error;
Barry Naujok556b8b12008-04-10 12:22:07 +1000435
David Howells2b0143b2015-03-17 22:25:59 +0000436 return xfs_rename(XFS_I(odir), &oname, XFS_I(d_inode(odentry)),
Carlos Maiolinodbe1b5c2014-12-24 08:51:38 +1100437 XFS_I(ndir), &nname,
Carlos Maiolinod31a1822014-12-24 08:51:42 +1100438 new_inode ? XFS_I(new_inode) : NULL, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/*
442 * careful here - this function can get called recursively, so
443 * we need to be very careful about how much stack we use.
444 * uio is kmalloced for this reason...
445 */
Al Viro680baac2015-05-02 13:32:22 -0400446STATIC const char *
Al Viro6b255392015-11-17 10:20:54 -0500447xfs_vn_get_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct dentry *dentry,
Al Viro6b255392015-11-17 10:20:54 -0500449 struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -0500450 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000453 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Al Viro6b255392015-11-17 10:20:54 -0500455 if (!dentry)
456 return ERR_PTR(-ECHILD);
457
Darrick J. Wong6eb0b8d2017-07-07 08:37:26 -0700458 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000459 if (!link)
460 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
David Howells2b0143b2015-03-17 22:25:59 +0000462 error = xfs_readlink(XFS_I(d_inode(dentry)), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000463 if (unlikely(error))
464 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Al Virofceef392015-12-29 15:58:39 -0500466 set_delayed_call(done, kfree_link, link);
467 return link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000468
469 out_kfree:
470 kfree(link);
471 out_err:
Al Viro680baac2015-05-02 13:32:22 -0400472 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000475STATIC const char *
476xfs_vn_get_link_inline(
477 struct dentry *dentry,
478 struct inode *inode,
479 struct delayed_call *done)
480{
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700481 struct xfs_inode *ip = XFS_I(inode);
Darrick J. Wongae294782018-09-29 13:40:40 +1000482 char *link;
483
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700484 ASSERT(ip->i_df.if_flags & XFS_IFINLINE);
Darrick J. Wongae294782018-09-29 13:40:40 +1000485
486 /*
487 * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED if
488 * if_data is junk.
489 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700490 link = ip->i_df.if_u1.if_data;
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800491 if (XFS_IS_CORRUPT(ip->i_mount, !link))
Darrick J. Wongae294782018-09-29 13:40:40 +1000492 return ERR_PTR(-EFSCORRUPTED);
493 return link;
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000494}
495
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700496static uint32_t
497xfs_stat_blksize(
498 struct xfs_inode *ip)
499{
500 struct xfs_mount *mp = ip->i_mount;
501
502 /*
503 * If the file blocks are being allocated from a realtime volume, then
504 * always return the realtime extent size.
505 */
506 if (XFS_IS_REALTIME_INODE(ip))
507 return xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
508
509 /*
510 * Allow large block sizes to be reported to userspace programs if the
511 * "largeio" mount option is used.
512 *
513 * If compatibility mode is specified, simply return the basic unit of
514 * caching so that we don't get inefficient read/modify/write I/O from
515 * user apps. Otherwise....
516 *
517 * If the underlying volume is a stripe, then return the stripe width in
518 * bytes as the recommended I/O size. It is not a stripe and we've set a
519 * default buffered I/O size, return that, otherwise return the compat
520 * default.
521 */
Christoph Hellwig7c6b94b2019-10-28 08:41:46 -0700522 if (mp->m_flags & XFS_MOUNT_LARGEIO) {
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700523 if (mp->m_swidth)
524 return mp->m_swidth << mp->m_sb.sb_blocklog;
Christoph Hellwig3274d002019-10-28 08:41:45 -0700525 if (mp->m_flags & XFS_MOUNT_ALLOCSIZE)
Christoph Hellwig5da8a072019-10-28 08:41:44 -0700526 return 1U << mp->m_allocsize_log;
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700527 }
528
529 return PAGE_SIZE;
530}
531
Christoph Hellwig45767582008-02-05 12:13:24 +1100532STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100533xfs_vn_getattr(
David Howellsa528d352017-01-31 16:46:22 +0000534 const struct path *path,
535 struct kstat *stat,
536 u32 request_mask,
537 unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
David Howellsa528d352017-01-31 16:46:22 +0000539 struct inode *inode = d_inode(path->dentry);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000540 struct xfs_inode *ip = XFS_I(inode);
541 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000543 trace_xfs_getattr(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000544
545 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000546 return -EIO;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000547
548 stat->size = XFS_ISIZE(ip);
549 stat->dev = inode->i_sb->s_dev;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100550 stat->mode = inode->i_mode;
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100551 stat->nlink = inode->i_nlink;
Dwight Engen7aab1b22013-08-15 14:08:01 -0400552 stat->uid = inode->i_uid;
553 stat->gid = inode->i_gid;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000554 stat->ino = ip->i_ino;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000555 stat->atime = inode->i_atime;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000556 stat->mtime = inode->i_mtime;
557 stat->ctime = inode->i_ctime;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000558 stat->blocks =
559 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
560
Christoph Hellwig6471e9c2020-03-18 08:15:11 -0700561 if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100562 if (request_mask & STATX_BTIME) {
563 stat->result_mask |= STATX_BTIME;
Christoph Hellwig8d2d8782019-11-12 08:20:42 -0800564 stat->btime = ip->i_d.di_crtime;
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100565 }
566 }
567
Luis R. Rodriguez1b9598c2019-03-01 08:14:57 -0800568 /*
569 * Note: If you add another clause to set an attribute flag, please
570 * update attributes_mask below.
571 */
Darrick J. Wong5f955f22017-03-31 18:32:03 +0100572 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
573 stat->attributes |= STATX_ATTR_IMMUTABLE;
574 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
575 stat->attributes |= STATX_ATTR_APPEND;
576 if (ip->i_d.di_flags & XFS_DIFLAG_NODUMP)
577 stat->attributes |= STATX_ATTR_NODUMP;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000578
Luis R. Rodriguez1b9598c2019-03-01 08:14:57 -0800579 stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
580 STATX_ATTR_APPEND |
581 STATX_ATTR_NODUMP);
582
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000583 switch (inode->i_mode & S_IFMT) {
584 case S_IFBLK:
585 case S_IFCHR:
586 stat->blksize = BLKDEV_IOSIZE;
Christoph Hellwig66f36462017-10-19 11:07:09 -0700587 stat->rdev = inode->i_rdev;
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000588 break;
589 default:
Christoph Hellwigdd2d5352019-10-28 08:41:43 -0700590 stat->blksize = xfs_stat_blksize(ip);
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000591 stat->rdev = 0;
592 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000593 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000594
595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Dave Chinner56c19e82013-05-27 16:38:25 +1000598static void
599xfs_setattr_mode(
Dave Chinner56c19e82013-05-27 16:38:25 +1000600 struct xfs_inode *ip,
601 struct iattr *iattr)
602{
Christoph Hellwig0c3d88d2013-11-18 05:10:40 -0800603 struct inode *inode = VFS_I(ip);
604 umode_t mode = iattr->ia_mode;
Dave Chinner56c19e82013-05-27 16:38:25 +1000605
Dave Chinner56c19e82013-05-27 16:38:25 +1000606 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
607
Dave Chinner56c19e82013-05-27 16:38:25 +1000608 inode->i_mode &= S_IFMT;
609 inode->i_mode |= mode & ~S_IFMT;
610}
611
Christoph Hellwig52785112015-02-16 11:49:23 +1100612void
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800613xfs_setattr_time(
614 struct xfs_inode *ip,
615 struct iattr *iattr)
616{
617 struct inode *inode = VFS_I(ip);
618
619 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
620
Dave Chinner39878482016-02-09 16:54:58 +1100621 if (iattr->ia_valid & ATTR_ATIME)
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800622 inode->i_atime = iattr->ia_atime;
Dave Chinner39878482016-02-09 16:54:58 +1100623 if (iattr->ia_valid & ATTR_CTIME)
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800624 inode->i_ctime = iattr->ia_ctime;
Dave Chinner39878482016-02-09 16:54:58 +1100625 if (iattr->ia_valid & ATTR_MTIME)
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800626 inode->i_mtime = iattr->ia_mtime;
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800627}
628
Jan Kara69bca802016-05-26 14:46:43 +0200629static int
630xfs_vn_change_ok(
631 struct dentry *dentry,
632 struct iattr *iattr)
633{
Jan Kara31051c82016-05-26 16:55:18 +0200634 struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
Jan Kara69bca802016-05-26 14:46:43 +0200635
636 if (mp->m_flags & XFS_MOUNT_RDONLY)
637 return -EROFS;
638
639 if (XFS_FORCED_SHUTDOWN(mp))
640 return -EIO;
641
Jan Kara31051c82016-05-26 16:55:18 +0200642 return setattr_prepare(dentry, iattr);
Jan Kara69bca802016-05-26 14:46:43 +0200643}
644
645/*
646 * Set non-size attributes of an inode.
647 *
648 * Caution: The caller of this function is responsible for calling
Jan Kara31051c82016-05-26 16:55:18 +0200649 * setattr_prepare() or otherwise verifying the change is fine.
Jan Kara69bca802016-05-26 14:46:43 +0200650 */
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200651int
652xfs_setattr_nonsize(
653 struct xfs_inode *ip,
654 struct iattr *iattr,
655 int flags)
656{
657 xfs_mount_t *mp = ip->i_mount;
658 struct inode *inode = VFS_I(ip);
659 int mask = iattr->ia_valid;
660 xfs_trans_t *tp;
661 int error;
Dwight Engen7aab1b22013-08-15 14:08:01 -0400662 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
663 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200664 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
665 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
666
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200667 ASSERT((mask & ATTR_SIZE) == 0);
668
669 /*
670 * If disk quotas is on, we make sure that the dquots do exist on disk,
671 * before we start any other transactions. Trying to do this later
672 * is messy. We don't care to take a readlock to look at the ids
673 * in inode here, because we can't hold it across the trans_reserve.
674 * If the IDs do change before we take the ilock, we're covered
675 * because the i_*dquot fields will get updated anyway.
676 */
677 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
678 uint qflags = 0;
679
680 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
681 uid = iattr->ia_uid;
682 qflags |= XFS_QMOPT_UQUOTA;
683 } else {
Dwight Engen7aab1b22013-08-15 14:08:01 -0400684 uid = inode->i_uid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200685 }
686 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
687 gid = iattr->ia_gid;
688 qflags |= XFS_QMOPT_GQUOTA;
689 } else {
Dwight Engen7aab1b22013-08-15 14:08:01 -0400690 gid = inode->i_gid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200691 }
692
693 /*
694 * We take a reference when we initialize udqp and gdqp,
695 * so it is important that we never blindly double trip on
696 * the same variable. See xfs_create() for an example.
697 */
698 ASSERT(udqp == NULL);
699 ASSERT(gdqp == NULL);
Christoph Hellwig54295152020-02-21 08:31:27 -0800700 error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
Dwight Engen7aab1b22013-08-15 14:08:01 -0400701 qflags, &udqp, &gdqp, NULL);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200702 if (error)
703 return error;
704 }
705
Christoph Hellwig253f4912016-04-06 09:19:55 +1000706 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200707 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000708 goto out_dqrele;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200709
710 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000711 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200712
713 /*
714 * Change file ownership. Must be the owner or privileged.
715 */
716 if (mask & (ATTR_UID|ATTR_GID)) {
717 /*
718 * These IDs could have changed since we last looked at them.
719 * But, we're assured that if the ownership did change
720 * while we didn't have the inode locked, inode's dquot(s)
721 * would have changed also.
722 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400723 iuid = inode->i_uid;
724 igid = inode->i_gid;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200725 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
726 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
727
728 /*
729 * Do a quota reservation only if uid/gid is actually
730 * going to change.
731 */
732 if (XFS_IS_QUOTA_RUNNING(mp) &&
Dwight Engen7aab1b22013-08-15 14:08:01 -0400733 ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
734 (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200735 ASSERT(tp);
736 error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500737 NULL, capable(CAP_FOWNER) ?
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200738 XFS_QMOPT_FORCE_RES : 0);
739 if (error) /* out of quota */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000740 goto out_cancel;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200741 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200742
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200743 /*
744 * CAP_FSETID overrides the following restrictions:
745 *
746 * The set-user-ID and set-group-ID bits of a file will be
747 * cleared upon successful return from chown()
748 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100749 if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200750 !capable(CAP_FSETID))
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100751 inode->i_mode &= ~(S_ISUID|S_ISGID);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200752
753 /*
754 * Change the ownerships and register quota modifications
755 * in the transaction.
756 */
Dwight Engen7aab1b22013-08-15 14:08:01 -0400757 if (!uid_eq(iuid, uid)) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200758 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
759 ASSERT(mask & ATTR_UID);
760 ASSERT(udqp);
761 olddquot1 = xfs_qm_vop_chown(tp, ip,
762 &ip->i_udquot, udqp);
763 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200764 inode->i_uid = uid;
765 }
Dwight Engen7aab1b22013-08-15 14:08:01 -0400766 if (!gid_eq(igid, gid)) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200767 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
Jie Liu5a01dd52013-11-26 21:38:34 +0800768 ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
769 !XFS_IS_PQUOTA_ON(mp));
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200770 ASSERT(mask & ATTR_GID);
771 ASSERT(gdqp);
772 olddquot2 = xfs_qm_vop_chown(tp, ip,
773 &ip->i_gdquot, gdqp);
774 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200775 inode->i_gid = gid;
776 }
777 }
778
Dave Chinner56c19e82013-05-27 16:38:25 +1000779 if (mask & ATTR_MODE)
Christoph Hellwig0c3d88d2013-11-18 05:10:40 -0800780 xfs_setattr_mode(ip, iattr);
Christoph Hellwigc91c46c2013-11-18 05:10:52 -0800781 if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
782 xfs_setattr_time(ip, iattr);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200783
784 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
785
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100786 XFS_STATS_INC(mp, xs_ig_attrchg);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200787
788 if (mp->m_flags & XFS_MOUNT_WSYNC)
789 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +1000790 error = xfs_trans_commit(tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200791
792 xfs_iunlock(ip, XFS_ILOCK_EXCL);
793
794 /*
795 * Release any dquot(s) the inode had kept before chown.
796 */
797 xfs_qm_dqrele(olddquot1);
798 xfs_qm_dqrele(olddquot2);
799 xfs_qm_dqrele(udqp);
800 xfs_qm_dqrele(gdqp);
801
802 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000803 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200804
805 /*
806 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
807 * update. We could avoid this with linked transactions
808 * and passing down the transaction pointer all the way
809 * to attr_set. No previous user of the generic
810 * Posix ACL code seems to care about this issue either.
811 */
812 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
Dave Chinner24513372014-06-25 14:58:08 +1000813 error = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200814 if (error)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000815 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200816 }
817
818 return 0;
819
Christoph Hellwig253f4912016-04-06 09:19:55 +1000820out_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000821 xfs_trans_cancel(tp);
Darrick J. Wong1fb254a2019-08-22 20:55:54 -0700822 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000823out_dqrele:
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200824 xfs_qm_dqrele(udqp);
825 xfs_qm_dqrele(gdqp);
826 return error;
827}
828
Jan Kara69bca802016-05-26 14:46:43 +0200829int
830xfs_vn_setattr_nonsize(
831 struct dentry *dentry,
832 struct iattr *iattr)
833{
834 struct xfs_inode *ip = XFS_I(d_inode(dentry));
835 int error;
836
837 trace_xfs_setattr(ip);
838
839 error = xfs_vn_change_ok(dentry, iattr);
840 if (error)
841 return error;
842 return xfs_setattr_nonsize(ip, iattr, 0);
843}
844
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200845/*
846 * Truncate file. Must have write permission and not be a directory.
Jan Kara69bca802016-05-26 14:46:43 +0200847 *
848 * Caution: The caller of this function is responsible for calling
Jan Kara31051c82016-05-26 16:55:18 +0200849 * setattr_prepare() or otherwise verifying the change is fine.
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200850 */
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700851STATIC int
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200852xfs_setattr_size(
853 struct xfs_inode *ip,
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700854 struct iattr *iattr)
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200855{
856 struct xfs_mount *mp = ip->i_mount;
857 struct inode *inode = VFS_I(ip);
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000858 xfs_off_t oldsize, newsize;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200859 struct xfs_trans *tp;
860 int error;
Christoph Hellwigf38996f2012-03-27 10:34:48 -0400861 uint lock_flags = 0;
Dave Chinner5885ebd2015-02-23 22:37:08 +1100862 bool did_zeroing = false;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200863
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700864 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Dave Chinnere8e9ad42015-02-23 21:45:32 +1100865 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100866 ASSERT(S_ISREG(inode->i_mode));
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +1100867 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
868 ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200869
Christoph Hellwigce7ae1512011-12-18 20:00:11 +0000870 oldsize = inode->i_size;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000871 newsize = iattr->ia_size;
872
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200873 /*
874 * Short circuit the truncate case for zero length files.
875 */
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700876 if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +1100877 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700878 return 0;
Christoph Hellwig681b1202011-07-08 14:34:26 +0200879
880 /*
881 * Use the regular setattr path to update the timestamps.
882 */
Christoph Hellwig681b1202011-07-08 14:34:26 +0200883 iattr->ia_valid &= ~ATTR_SIZE;
884 return xfs_setattr_nonsize(ip, iattr, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200885 }
886
887 /*
888 * Make sure that the dquots are attached to the inode.
889 */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700890 error = xfs_qm_dqattach(ip);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200891 if (error)
Christoph Hellwig76ca4c22013-10-14 07:09:35 -0700892 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200893
894 /*
Christoph Hellwigf0c6bcb2016-06-21 09:52:47 +1000895 * Wait for all direct I/O to complete.
896 */
897 inode_dio_wait(inode);
898
899 /*
Dave Chinner5885ebd2015-02-23 22:37:08 +1100900 * File data changes must be complete before we start the transaction to
901 * modify the inode. This needs to be done before joining the inode to
902 * the transaction because the inode cannot be unlocked once it is a
903 * part of the transaction.
904 *
Christoph Hellwigf0c6bcb2016-06-21 09:52:47 +1000905 * Start with zeroing any data beyond EOF that we may expose on file
906 * extension, or zeroing out the rest of the block on a downward
907 * truncate.
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200908 */
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000909 if (newsize > oldsize) {
Christoph Hellwigf5c547172018-03-13 23:15:32 -0700910 trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
911 error = iomap_zero_range(inode, oldsize, newsize - oldsize,
Christoph Hellwigf150b422019-10-19 09:09:46 -0700912 &did_zeroing, &xfs_buffered_write_iomap_ops);
Christoph Hellwigf0c6bcb2016-06-21 09:52:47 +1000913 } else {
Christoph Hellwig459f0fb2016-06-21 09:55:18 +1000914 error = iomap_truncate_page(inode, newsize, &did_zeroing,
Christoph Hellwigf150b422019-10-19 09:09:46 -0700915 &xfs_buffered_write_iomap_ops);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200916 }
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200917
Christoph Hellwigf0c6bcb2016-06-21 09:52:47 +1000918 if (error)
919 return error;
920
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200921 /*
Dave Chinner0f9160b2015-02-23 21:46:58 +1100922 * We've already locked out new page faults, so now we can safely remove
923 * pages from the page cache knowing they won't get refaulted until we
924 * drop the XFS_MMAP_EXCL lock after the extent manipulations are
925 * complete. The truncate_setsize() call also cleans partial EOF page
926 * PTEs on extending truncates and hence ensures sub-page block size
927 * filesystems are correctly handled, too.
Dave Chinner49abc3a2014-05-07 08:05:45 +1000928 *
Dave Chinner0f9160b2015-02-23 21:46:58 +1100929 * We have to do all the page cache truncate work outside the
930 * transaction context as the "lock" order is page lock->log space
931 * reservation as defined by extent allocation in the writeback path.
Christoph Hellwig253f4912016-04-06 09:19:55 +1000932 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
Dave Chinner0f9160b2015-02-23 21:46:58 +1100933 * having already truncated the in-memory version of the file (i.e. made
934 * user visible changes). There's not much we can do about this, except
935 * to hope that the caller sees ENOMEM and retries the truncate
936 * operation.
Eryu Guan350976ae2017-11-01 21:43:50 -0700937 *
938 * And we update in-core i_size and truncate page cache beyond newsize
939 * before writeback the [di_size, newsize] range, so we're guaranteed
940 * not to write stale data past the new EOF on truncate down.
Dave Chinner49abc3a2014-05-07 08:05:45 +1000941 */
Dave Chinner49abc3a2014-05-07 08:05:45 +1000942 truncate_setsize(inode, newsize);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200943
Eryu Guan350976ae2017-11-01 21:43:50 -0700944 /*
945 * We are going to log the inode size change in this transaction so
946 * any previous writes that are beyond the on disk EOF and the new
947 * EOF that have not been written out need to be written here. If we
948 * do not write the data out, we expose ourselves to the null files
949 * problem. Note that this includes any block zeroing we did above;
950 * otherwise those blocks may not be zeroed after a crash.
951 */
952 if (did_zeroing ||
953 (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
954 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
955 ip->i_d.di_size, newsize - 1);
956 if (error)
957 return error;
958 }
959
Christoph Hellwig253f4912016-04-06 09:19:55 +1000960 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200961 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000962 return error;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200963
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200964 lock_flags |= XFS_ILOCK_EXCL;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200965 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000966 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200967
968 /*
969 * Only change the c/mtime if we are changing the size or we are
970 * explicitly asked to change it. This handles the semantic difference
971 * between truncate() and ftruncate() as implemented in the VFS.
972 *
973 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
974 * special case where we need to update the times despite not having
975 * these flags set. For all other operations the VFS set these flags
976 * explicitly if it wants a timestamp update.
977 */
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +1100978 if (newsize != oldsize &&
979 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200980 iattr->ia_ctime = iattr->ia_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700981 current_time(inode);
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +1100982 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +0200983 }
984
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000985 /*
986 * The first thing we do is set the size to new_size permanently on
987 * disk. This way we don't have to worry about anyone ever being able
988 * to look at the data being freed even in the face of a crash.
989 * What we're getting around here is the case where we free a block, it
990 * is allocated to another file, it is written to, and then we crash.
991 * If the new data gets written to the file but the log buffers
992 * containing the free and reallocation don't, then we'd end up with
993 * garbage in the blocks being freed. As long as we make the new size
994 * permanent before actually freeing any blocks it doesn't matter if
995 * they get written to.
996 */
997 ip->i_d.di_size = newsize;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000998 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
999
1000 if (newsize <= oldsize) {
1001 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001002 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001003 goto out_trans_cancel;
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001004
1005 /*
1006 * Truncated "down", so we're removing references to old data
1007 * here - if we delay flushing for a long time, we expose
1008 * ourselves unduly to the notorious NULL files problem. So,
1009 * we mark this inode and flush it when the file is closed,
1010 * and do not wait the usual (long) time for writeout.
1011 */
1012 xfs_iflags_set(ip, XFS_ITRUNCATED);
Brian Foster27b52862012-11-06 09:50:38 -05001013
1014 /* A truncate down always removes post-EOF blocks. */
1015 xfs_inode_clear_eofblocks_tag(ip);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001016 }
1017
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +11001018 if (iattr->ia_valid & ATTR_MODE)
Christoph Hellwig0c3d88d2013-11-18 05:10:40 -08001019 xfs_setattr_mode(ip, iattr);
Christoph Hellwigfe60a8a2014-02-10 10:35:22 +11001020 if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
Christoph Hellwigc91c46c2013-11-18 05:10:52 -08001021 xfs_setattr_time(ip, iattr);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001022
1023 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1024
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001025 XFS_STATS_INC(mp, xs_ig_attrchg);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001026
1027 if (mp->m_flags & XFS_MOUNT_WSYNC)
1028 xfs_trans_set_sync(tp);
1029
Christoph Hellwig70393312015-06-04 13:48:08 +10001030 error = xfs_trans_commit(tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001031out_unlock:
1032 if (lock_flags)
1033 xfs_iunlock(ip, lock_flags);
1034 return error;
1035
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001036out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001037 xfs_trans_cancel(tp);
Christoph Hellwigc4ed4242011-07-08 14:34:23 +02001038 goto out_unlock;
1039}
1040
Jan Kara69bca802016-05-26 14:46:43 +02001041int
1042xfs_vn_setattr_size(
1043 struct dentry *dentry,
1044 struct iattr *iattr)
1045{
1046 struct xfs_inode *ip = XFS_I(d_inode(dentry));
1047 int error;
1048
1049 trace_xfs_setattr(ip);
1050
1051 error = xfs_vn_change_ok(dentry, iattr);
1052 if (error)
1053 return error;
1054 return xfs_setattr_size(ip, iattr);
1055}
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +11001058xfs_vn_setattr(
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001059 struct dentry *dentry,
1060 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001062 int error;
1063
1064 if (iattr->ia_valid & ATTR_SIZE) {
Dan Williams69eb5fa2018-03-20 14:42:38 -07001065 struct inode *inode = d_inode(dentry);
1066 struct xfs_inode *ip = XFS_I(inode);
Dan Williamsc63a8ea2018-03-12 14:12:29 -07001067 uint iolock;
Dave Chinnere8e9ad42015-02-23 21:45:32 +11001068
Christoph Hellwig65523212016-11-30 14:33:25 +11001069 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
Dan Williamsc63a8ea2018-03-12 14:12:29 -07001070 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
1071
Dan Williams69eb5fa2018-03-20 14:42:38 -07001072 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
Dan Williamsc63a8ea2018-03-12 14:12:29 -07001073 if (error) {
1074 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1075 return error;
1076 }
1077
Eryu Guan0c187dc2016-12-09 16:49:54 +11001078 error = xfs_vn_setattr_size(dentry, iattr);
Christoph Hellwig65523212016-11-30 14:33:25 +11001079 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001080 } else {
Jan Kara69bca802016-05-26 14:46:43 +02001081 error = xfs_vn_setattr_nonsize(dentry, iattr);
Christoph Hellwig76ca4c22013-10-14 07:09:35 -07001082 }
1083
Dave Chinner24513372014-06-25 14:58:08 +10001084 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001087STATIC int
1088xfs_vn_update_time(
1089 struct inode *inode,
Deepa Dinamani95582b02018-05-08 19:36:02 -07001090 struct timespec64 *now,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001091 int flags)
1092{
1093 struct xfs_inode *ip = XFS_I(inode);
1094 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigc3b1b132018-03-06 17:04:00 -08001095 int log_flags = XFS_ILOG_TIMESTAMP;
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001096 struct xfs_trans *tp;
1097 int error;
1098
1099 trace_xfs_update_time(ip);
1100
Christoph Hellwigc3b1b132018-03-06 17:04:00 -08001101 if (inode->i_sb->s_flags & SB_LAZYTIME) {
1102 if (!((flags & S_VERSION) &&
1103 inode_maybe_inc_iversion(inode, false)))
1104 return generic_update_time(inode, now, flags);
1105
1106 /* Capture the iversion update that just occurred */
1107 log_flags |= XFS_ILOG_CORE;
1108 }
1109
Christoph Hellwig253f4912016-04-06 09:19:55 +10001110 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1111 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10001112 return error;
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001113
1114 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner39878482016-02-09 16:54:58 +11001115 if (flags & S_CTIME)
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001116 inode->i_ctime = *now;
Dave Chinner39878482016-02-09 16:54:58 +11001117 if (flags & S_MTIME)
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001118 inode->i_mtime = *now;
Dave Chinner39878482016-02-09 16:54:58 +11001119 if (flags & S_ATIME)
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001120 inode->i_atime = *now;
Dave Chinner39878482016-02-09 16:54:58 +11001121
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001122 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Christoph Hellwigc3b1b132018-03-06 17:04:00 -08001123 xfs_trans_log_inode(tp, ip, log_flags);
Christoph Hellwig70393312015-06-04 13:48:08 +10001124 return xfs_trans_commit(tp);
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001125}
1126
Eric Sandeenf35642e2008-11-28 14:23:35 +11001127STATIC int
1128xfs_vn_fiemap(
1129 struct inode *inode,
1130 struct fiemap_extent_info *fieinfo,
1131 u64 start,
1132 u64 length)
1133{
Eric Sandeenf35642e2008-11-28 14:23:35 +11001134 int error;
1135
Christoph Hellwigd2bb1402016-06-21 09:54:53 +10001136 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001137 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1138 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
1139 error = iomap_fiemap(inode, fieinfo, start, length,
1140 &xfs_xattr_iomap_ops);
1141 } else {
1142 error = iomap_fiemap(inode, fieinfo, start, length,
Christoph Hellwig690c2a32019-10-19 09:09:45 -07001143 &xfs_read_iomap_ops);
Christoph Hellwig1d4795e2016-08-17 08:45:30 +10001144 }
Christoph Hellwigd2bb1402016-06-21 09:54:53 +10001145 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
Eric Sandeenf35642e2008-11-28 14:23:35 +11001146
Christoph Hellwigd2bb1402016-06-21 09:54:53 +10001147 return error;
Eric Sandeenf35642e2008-11-28 14:23:35 +11001148}
1149
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001150STATIC int
1151xfs_vn_tmpfile(
1152 struct inode *dir,
1153 struct dentry *dentry,
1154 umode_t mode)
1155{
Brian Fosterd540e432014-05-06 07:34:28 +10001156 return xfs_generic_create(dir, dentry, mode, 0, true);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001157}
1158
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001159static const struct inode_operations xfs_inode_operations = {
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001160 .get_acl = xfs_get_acl,
Christoph Hellwig2401dc22013-12-20 05:16:50 -08001161 .set_acl = xfs_set_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +11001162 .getattr = xfs_vn_getattr,
1163 .setattr = xfs_vn_setattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001164 .listxattr = xfs_vn_listxattr,
Eric Sandeenf35642e2008-11-28 14:23:35 +11001165 .fiemap = xfs_vn_fiemap,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001166 .update_time = xfs_vn_update_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167};
1168
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001169static const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +11001170 .create = xfs_vn_create,
1171 .lookup = xfs_vn_lookup,
1172 .link = xfs_vn_link,
1173 .unlink = xfs_vn_unlink,
1174 .symlink = xfs_vn_symlink,
1175 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001176 /*
1177 * Yes, XFS uses the same method for rmdir and unlink.
1178 *
1179 * There are some subtile differences deeper in the code,
1180 * but we use S_ISDIR to check for those.
1181 */
1182 .rmdir = xfs_vn_unlink,
Nathan Scott416c6d52006-03-14 14:00:51 +11001183 .mknod = xfs_vn_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001184 .rename = xfs_vn_rename,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001185 .get_acl = xfs_get_acl,
Christoph Hellwig2401dc22013-12-20 05:16:50 -08001186 .set_acl = xfs_set_acl,
Nathan Scott416c6d52006-03-14 14:00:51 +11001187 .getattr = xfs_vn_getattr,
1188 .setattr = xfs_vn_setattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001189 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001190 .update_time = xfs_vn_update_time,
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001191 .tmpfile = xfs_vn_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192};
1193
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001194static const struct inode_operations xfs_dir_ci_inode_operations = {
Barry Naujok384f3ce2008-05-21 16:58:22 +10001195 .create = xfs_vn_create,
1196 .lookup = xfs_vn_ci_lookup,
1197 .link = xfs_vn_link,
1198 .unlink = xfs_vn_unlink,
1199 .symlink = xfs_vn_symlink,
1200 .mkdir = xfs_vn_mkdir,
Christoph Hellwig8f112e32008-06-23 13:25:17 +10001201 /*
1202 * Yes, XFS uses the same method for rmdir and unlink.
1203 *
1204 * There are some subtile differences deeper in the code,
1205 * but we use S_ISDIR to check for those.
1206 */
1207 .rmdir = xfs_vn_unlink,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001208 .mknod = xfs_vn_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001209 .rename = xfs_vn_rename,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001210 .get_acl = xfs_get_acl,
Christoph Hellwig2401dc22013-12-20 05:16:50 -08001211 .set_acl = xfs_set_acl,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001212 .getattr = xfs_vn_getattr,
1213 .setattr = xfs_vn_setattr,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001214 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001215 .update_time = xfs_vn_update_time,
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001216 .tmpfile = xfs_vn_tmpfile,
Barry Naujok384f3ce2008-05-21 16:58:22 +10001217};
1218
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001219static const struct inode_operations xfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05001220 .get_link = xfs_vn_get_link,
Nathan Scott416c6d52006-03-14 14:00:51 +11001221 .getattr = xfs_vn_getattr,
1222 .setattr = xfs_vn_setattr,
Nathan Scott416c6d52006-03-14 14:00:51 +11001223 .listxattr = xfs_vn_listxattr,
Christoph Hellwig69ff2822012-06-06 17:01:28 -04001224 .update_time = xfs_vn_update_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225};
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001226
Christoph Hellwig30ee0522016-04-06 07:53:29 +10001227static const struct inode_operations xfs_inline_symlink_inode_operations = {
Christoph Hellwig30ee0522016-04-06 07:53:29 +10001228 .get_link = xfs_vn_get_link_inline,
1229 .getattr = xfs_vn_getattr,
1230 .setattr = xfs_vn_setattr,
Christoph Hellwig30ee0522016-04-06 07:53:29 +10001231 .listxattr = xfs_vn_listxattr,
1232 .update_time = xfs_vn_update_time,
1233};
1234
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001235/* Figure out if this file actually supports DAX. */
1236static bool
1237xfs_inode_supports_dax(
1238 struct xfs_inode *ip)
1239{
1240 struct xfs_mount *mp = ip->i_mount;
1241
Ira Weiny32dbc562020-05-04 09:02:42 -07001242 /* Only supported on regular files. */
1243 if (!S_ISREG(VFS_I(ip)->i_mode))
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001244 return false;
1245
Ira Weiny32dbc562020-05-04 09:02:42 -07001246 /* Only supported on non-reflinked files. */
1247 if (xfs_is_reflink_inode(ip))
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001248 return false;
1249
1250 /* Block size must match page size */
1251 if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1252 return false;
1253
1254 /* Device has to support DAX too. */
Christoph Hellwig30fa5292019-10-24 22:25:38 -07001255 return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
Darrick J. Wongba23cba2018-05-30 13:03:45 -07001256}
1257
Ira Weiny32dbc562020-05-04 09:02:42 -07001258static bool
1259xfs_inode_should_enable_dax(
1260 struct xfs_inode *ip)
1261{
1262 if (!IS_ENABLED(CONFIG_FS_DAX))
1263 return false;
1264 if (ip->i_mount->m_flags & XFS_MOUNT_DAX_NEVER)
1265 return false;
1266 if (!xfs_inode_supports_dax(ip))
1267 return false;
1268 if (ip->i_mount->m_flags & XFS_MOUNT_DAX_ALWAYS)
1269 return true;
1270 if (ip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
1271 return true;
1272 return false;
1273}
1274
Ira Weiny840d4932020-05-04 09:02:43 -07001275void
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001276xfs_diflags_to_iflags(
Ira Weiny840d4932020-05-04 09:02:43 -07001277 struct xfs_inode *ip,
1278 bool init)
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001279{
Ira Weiny840d4932020-05-04 09:02:43 -07001280 struct inode *inode = VFS_I(ip);
1281 unsigned int xflags = xfs_ip2xflags(ip);
1282 unsigned int flags = 0;
Dave Chinnercbe4dab2015-06-04 09:19:18 +10001283
Ira Weiny840d4932020-05-04 09:02:43 -07001284 ASSERT(!(IS_DAX(inode) && init));
Dave Chinnercbe4dab2015-06-04 09:19:18 +10001285
Ira Weiny840d4932020-05-04 09:02:43 -07001286 if (xflags & FS_XFLAG_IMMUTABLE)
1287 flags |= S_IMMUTABLE;
1288 if (xflags & FS_XFLAG_APPEND)
1289 flags |= S_APPEND;
1290 if (xflags & FS_XFLAG_SYNC)
1291 flags |= S_SYNC;
1292 if (xflags & FS_XFLAG_NOATIME)
1293 flags |= S_NOATIME;
1294 if (init && xfs_inode_should_enable_dax(ip))
1295 flags |= S_DAX;
1296
1297 /*
1298 * S_DAX can only be set during inode initialization and is never set by
1299 * the VFS, so we cannot mask off S_DAX in i_flags.
1300 */
1301 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
1302 inode->i_flags |= flags;
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001303}
1304
1305/*
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001306 * Initialize the Linux inode.
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001307 *
Dave Chinner58c90472015-02-23 22:38:08 +11001308 * When reading existing inodes from disk this is called directly from xfs_iget,
1309 * when creating a new inode it is called from xfs_ialloc after setting up the
1310 * inode. These callers have different criteria for clearing XFS_INEW, so leave
1311 * it up to the caller to deal with unlocking the inode appropriately.
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001312 */
1313void
1314xfs_setup_inode(
1315 struct xfs_inode *ip)
1316{
David Chinnerbf904242008-10-30 17:36:14 +11001317 struct inode *inode = &ip->i_vnode;
Dave Chinnerad22c7a2013-10-29 22:11:57 +11001318 gfp_t gfp_mask;
David Chinnerbf904242008-10-30 17:36:14 +11001319
1320 inode->i_ino = ip->i_ino;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001321 inode->i_state = I_NEW;
Christoph Hellwig646ec462010-10-23 07:15:32 -04001322
1323 inode_sb_list_add(inode);
Christoph Hellwigc6f6cd02010-11-06 11:43:08 +00001324 /* make the inode look hashed for the writeback code */
Al Viro5bef9152018-06-29 19:36:57 -04001325 inode_fake_hash(inode);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001326
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001327 i_size_write(inode, ip->i_d.di_size);
Ira Weiny840d4932020-05-04 09:02:43 -07001328 xfs_diflags_to_iflags(ip, true);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001329
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001330 if (S_ISDIR(inode->i_mode)) {
Dave Chinneref215e32018-06-07 07:36:08 -07001331 /*
1332 * We set the i_rwsem class here to avoid potential races with
1333 * lockdep_annotate_inode_mutex_key() reinitialising the lock
1334 * after a filehandle lookup has already found the inode in
1335 * cache before it has been unlocked via unlock_new_inode().
1336 */
1337 lockdep_set_class(&inode->i_rwsem,
1338 &inode->i_sb->s_type->i_mutex_dir_key);
Dave Chinner93a86142014-02-27 16:51:39 +11001339 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001340 } else {
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001341 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001342 }
1343
Christoph Hellwig510792e2011-07-26 15:07:29 +00001344 /*
Dave Chinnerad22c7a2013-10-29 22:11:57 +11001345 * Ensure all page cache allocations are done from GFP_NOFS context to
1346 * prevent direct reclaim recursion back into the filesystem and blowing
1347 * stacks or deadlocking.
1348 */
1349 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1350 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1351
1352 /*
Christoph Hellwig510792e2011-07-26 15:07:29 +00001353 * If there is no attribute fork no ACL can exist on this inode,
1354 * and it can't have any file capabilities attached to it either.
1355 */
1356 if (!XFS_IFORK_Q(ip)) {
1357 inode_has_no_xattr(inode);
Christoph Hellwig6311b102011-07-23 17:36:50 +02001358 cache_no_acl(inode);
Christoph Hellwig510792e2011-07-26 15:07:29 +00001359 }
Christoph Hellwig41be8be2008-08-13 16:23:13 +10001360}
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001361
1362void
1363xfs_setup_iops(
1364 struct xfs_inode *ip)
1365{
1366 struct inode *inode = &ip->i_vnode;
1367
1368 switch (inode->i_mode & S_IFMT) {
1369 case S_IFREG:
1370 inode->i_op = &xfs_inode_operations;
1371 inode->i_fop = &xfs_file_operations;
Dan Williams6e2608d2018-03-07 15:26:44 -08001372 if (IS_DAX(inode))
1373 inode->i_mapping->a_ops = &xfs_dax_aops;
1374 else
1375 inode->i_mapping->a_ops = &xfs_address_space_operations;
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001376 break;
1377 case S_IFDIR:
1378 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1379 inode->i_op = &xfs_dir_ci_inode_operations;
1380 else
1381 inode->i_op = &xfs_dir_inode_operations;
1382 inode->i_fop = &xfs_dir_file_operations;
1383 break;
1384 case S_IFLNK:
Christoph Hellwig30ee0522016-04-06 07:53:29 +10001385 if (ip->i_df.if_flags & XFS_IFINLINE)
1386 inode->i_op = &xfs_inline_symlink_inode_operations;
1387 else
1388 inode->i_op = &xfs_symlink_inode_operations;
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10001389 break;
1390 default:
1391 inode->i_op = &xfs_inode_operations;
1392 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1393 break;
1394 }
1395}