Merge tag 'csky-for-linus-4.21' of git://github.com/c-sky/csky-linux
[linux-2.6-block.git] / fs / xfs / xfs_iops.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4
LT
6#include "xfs.h"
7#include "xfs_fs.h"
70a9883c 8#include "xfs_shared.h"
239880ef
DC
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
1da177e4 12#include "xfs_mount.h"
57062787 13#include "xfs_da_format.h"
1da177e4
LT
14#include "xfs_inode.h"
15#include "xfs_bmap.h"
68988114 16#include "xfs_bmap_util.h"
239880ef 17#include "xfs_acl.h"
239880ef 18#include "xfs_quota.h"
1da177e4 19#include "xfs_error.h"
1da177e4 20#include "xfs_attr.h"
239880ef 21#include "xfs_trans.h"
0b1b213f 22#include "xfs_trace.h"
27b52867 23#include "xfs_icache.h"
c24b5dfa 24#include "xfs_symlink.h"
0cb97766 25#include "xfs_da_btree.h"
1b767ee3 26#include "xfs_dir2.h"
99b6436b 27#include "xfs_trans_space.h"
68a9f5e7 28#include "xfs_iomap.h"
02dff7bf 29#include "xfs_defer.h"
1da177e4 30
16f7e0fe 31#include <linux/capability.h>
1da177e4 32#include <linux/xattr.h>
ef14f0c1 33#include <linux/posix_acl.h>
446ada4a 34#include <linux/security.h>
d2bb140e 35#include <linux/iomap.h>
5a0e3ad6 36#include <linux/slab.h>
c3b1b131 37#include <linux/iversion.h>
1da177e4 38
93a8614e
DC
39/*
40 * Directories have different lock order w.r.t. mmap_sem compared to regular
41 * files. This is due to readdir potentially triggering page faults on a user
42 * buffer inside filldir(), and this happens with the ilock on the directory
43 * held. For regular files, the lock order is the other way around - the
44 * mmap_sem is taken during the page fault, and then we lock the ilock to do
45 * block mapping. Hence we need a different class for the directory ilock so
46 * that lockdep can tell them apart.
47 */
48static struct lock_class_key xfs_nondir_ilock_class;
49static struct lock_class_key xfs_dir_ilock_class;
50
8d2a5e6e
DC
51static int
52xfs_initxattrs(
53 struct inode *inode,
54 const struct xattr *xattr_array,
55 void *fs_info)
9d8f13ba 56{
8d2a5e6e
DC
57 const struct xattr *xattr;
58 struct xfs_inode *ip = XFS_I(inode);
59 int error = 0;
9d8f13ba
MZ
60
61 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
2451337d 62 error = xfs_attr_set(ip, xattr->name, xattr->value,
a5a14de2 63 xattr->value_len, ATTR_SECURE);
9d8f13ba
MZ
64 if (error < 0)
65 break;
66 }
67 return error;
68}
69
446ada4a
NS
70/*
71 * Hook in SELinux. This is not quite correct yet, what we really need
72 * here (as we do for default ACLs) is a mechanism by which creation of
73 * these attrs can be journalled at inode creation time (along with the
74 * inode, of course, such that log replay can't cause these to be lost).
75 */
9d8f13ba 76
446ada4a 77STATIC int
416c6d5b 78xfs_init_security(
af048193 79 struct inode *inode,
2a7dba39
EP
80 struct inode *dir,
81 const struct qstr *qstr)
446ada4a 82{
2451337d 83 return security_inode_init_security(inode, dir, qstr,
a5a14de2 84 &xfs_initxattrs, NULL);
446ada4a
NS
85}
86
556b8b16
BN
87static void
88xfs_dentry_to_name(
fab8eef8
AG
89 struct xfs_name *namep,
90 struct dentry *dentry)
91{
92 namep->name = dentry->d_name.name;
93 namep->len = dentry->d_name.len;
94 namep->type = XFS_DIR3_FT_UNKNOWN;
95}
96
97static int
98xfs_dentry_mode_to_name(
556b8b16 99 struct xfs_name *namep,
0cb97766
DC
100 struct dentry *dentry,
101 int mode)
556b8b16
BN
102{
103 namep->name = dentry->d_name.name;
104 namep->len = dentry->d_name.len;
1fc4d33f 105 namep->type = xfs_mode_to_ftype(mode);
fab8eef8
AG
106
107 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
108 return -EFSCORRUPTED;
109
110 return 0;
556b8b16
BN
111}
112
7989cb8e 113STATIC void
416c6d5b 114xfs_cleanup_inode(
739bfb2a 115 struct inode *dir,
af048193 116 struct inode *inode,
8f112e3b 117 struct dentry *dentry)
3a69c7dc 118{
556b8b16 119 struct xfs_name teardown;
3a69c7dc
YL
120
121 /* Oh, the horror.
220b5284 122 * If we can't add the ACL or we fail in
416c6d5b 123 * xfs_init_security we must back out.
3a69c7dc
YL
124 * ENOSPC can hit here, among other things.
125 */
fab8eef8 126 xfs_dentry_to_name(&teardown, dentry);
3a69c7dc 127
8f112e3b 128 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
3a69c7dc
YL
129}
130
1da177e4 131STATIC int
d540e43b 132xfs_generic_create(
1da177e4
LT
133 struct inode *dir,
134 struct dentry *dentry,
1a67aafb 135 umode_t mode,
d540e43b
BF
136 dev_t rdev,
137 bool tmpfile) /* unnamed file */
1da177e4 138{
db0bb7ba 139 struct inode *inode;
979ebab1 140 struct xfs_inode *ip = NULL;
2401dc29 141 struct posix_acl *default_acl, *acl;
556b8b16 142 struct xfs_name name;
1da177e4
LT
143 int error;
144
145 /*
146 * Irix uses Missed'em'V split, but doesn't want to see
147 * the upper 5 bits of (14bit) major.
148 */
517b5e8c
CH
149 if (S_ISCHR(mode) || S_ISBLK(mode)) {
150 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
151 return -EINVAL;
517b5e8c
CH
152 } else {
153 rdev = 0;
154 }
1da177e4 155
2401dc29
CH
156 error = posix_acl_create(dir, &mode, &default_acl, &acl);
157 if (error)
158 return error;
1da177e4 159
fab8eef8
AG
160 /* Verify mode is valid also for tmpfile case */
161 error = xfs_dentry_mode_to_name(&name, dentry, mode);
162 if (unlikely(error))
163 goto out_free_acl;
164
d540e43b 165 if (!tmpfile) {
d540e43b
BF
166 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
167 } else {
a1f69417 168 error = xfs_create_tmpfile(XFS_I(dir), mode, &ip);
d540e43b 169 }
db0bb7ba
CH
170 if (unlikely(error))
171 goto out_free_acl;
446ada4a 172
01651646 173 inode = VFS_I(ip);
979ebab1 174
2a7dba39 175 error = xfs_init_security(inode, dir, &dentry->d_name);
db0bb7ba
CH
176 if (unlikely(error))
177 goto out_cleanup_inode;
178
2401dc29 179#ifdef CONFIG_XFS_POSIX_ACL
db0bb7ba 180 if (default_acl) {
8ba35875 181 error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
2401dc29 182 if (error)
db0bb7ba 183 goto out_cleanup_inode;
1da177e4 184 }
2401dc29 185 if (acl) {
8ba35875 186 error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
2401dc29
CH
187 if (error)
188 goto out_cleanup_inode;
189 }
190#endif
1da177e4 191
2b3d1d41
CH
192 xfs_setup_iops(ip);
193
d540e43b
BF
194 if (tmpfile)
195 d_tmpfile(dentry, inode);
196 else
197 d_instantiate(dentry, inode);
198
58c90473
DC
199 xfs_finish_inode_setup(ip);
200
2401dc29
CH
201 out_free_acl:
202 if (default_acl)
203 posix_acl_release(default_acl);
204 if (acl)
205 posix_acl_release(acl);
2451337d 206 return error;
db0bb7ba
CH
207
208 out_cleanup_inode:
58c90473 209 xfs_finish_inode_setup(ip);
d540e43b
BF
210 if (!tmpfile)
211 xfs_cleanup_inode(dir, inode, dentry);
44a8736b 212 xfs_irele(ip);
2401dc29 213 goto out_free_acl;
1da177e4
LT
214}
215
d540e43b
BF
216STATIC int
217xfs_vn_mknod(
218 struct inode *dir,
219 struct dentry *dentry,
220 umode_t mode,
221 dev_t rdev)
222{
223 return xfs_generic_create(dir, dentry, mode, rdev, false);
224}
225
1da177e4 226STATIC int
416c6d5b 227xfs_vn_create(
1da177e4
LT
228 struct inode *dir,
229 struct dentry *dentry,
4acdaf27 230 umode_t mode,
ebfc3b49 231 bool flags)
1da177e4 232{
416c6d5b 233 return xfs_vn_mknod(dir, dentry, mode, 0);
1da177e4
LT
234}
235
236STATIC int
416c6d5b 237xfs_vn_mkdir(
1da177e4
LT
238 struct inode *dir,
239 struct dentry *dentry,
18bb1db3 240 umode_t mode)
1da177e4 241{
416c6d5b 242 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
1da177e4
LT
243}
244
245STATIC struct dentry *
416c6d5b 246xfs_vn_lookup(
1da177e4
LT
247 struct inode *dir,
248 struct dentry *dentry,
00cd8dd3 249 unsigned int flags)
1da177e4 250{
b113a6d3 251 struct inode *inode;
ef1f5e7a 252 struct xfs_inode *cip;
556b8b16 253 struct xfs_name name;
1da177e4
LT
254 int error;
255
256 if (dentry->d_name.len >= MAXNAMELEN)
257 return ERR_PTR(-ENAMETOOLONG);
258
fab8eef8 259 xfs_dentry_to_name(&name, dentry);
384f3ced 260 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
b113a6d3
AV
261 if (likely(!error))
262 inode = VFS_I(cip);
263 else if (likely(error == -ENOENT))
264 inode = NULL;
265 else
266 inode = ERR_PTR(error);
267 return d_splice_alias(inode, dentry);
1da177e4
LT
268}
269
384f3ced
BN
270STATIC struct dentry *
271xfs_vn_ci_lookup(
272 struct inode *dir,
273 struct dentry *dentry,
00cd8dd3 274 unsigned int flags)
384f3ced
BN
275{
276 struct xfs_inode *ip;
277 struct xfs_name xname;
278 struct xfs_name ci_name;
279 struct qstr dname;
280 int error;
281
282 if (dentry->d_name.len >= MAXNAMELEN)
283 return ERR_PTR(-ENAMETOOLONG);
284
fab8eef8 285 xfs_dentry_to_name(&xname, dentry);
384f3ced
BN
286 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
287 if (unlikely(error)) {
2451337d
DC
288 if (unlikely(error != -ENOENT))
289 return ERR_PTR(error);
866d5dc9
BN
290 /*
291 * call d_add(dentry, NULL) here when d_drop_negative_children
292 * is called in xfs_vn_mknod (ie. allow negative dentries
293 * with CI filesystems).
294 */
384f3ced
BN
295 return NULL;
296 }
297
298 /* if exact match, just splice and exit */
299 if (!ci_name.name)
01651646 300 return d_splice_alias(VFS_I(ip), dentry);
384f3ced
BN
301
302 /* else case-insensitive match... */
303 dname.name = ci_name.name;
304 dname.len = ci_name.len;
e45b590b 305 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
384f3ced
BN
306 kmem_free(ci_name.name);
307 return dentry;
308}
309
1da177e4 310STATIC int
416c6d5b 311xfs_vn_link(
1da177e4
LT
312 struct dentry *old_dentry,
313 struct inode *dir,
314 struct dentry *dentry)
315{
2b0143b5 316 struct inode *inode = d_inode(old_dentry);
556b8b16 317 struct xfs_name name;
1da177e4
LT
318 int error;
319
fab8eef8
AG
320 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
321 if (unlikely(error))
322 return error;
1da177e4 323
556b8b16 324 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
d9424b3c 325 if (unlikely(error))
2451337d 326 return error;
a3da7896 327
7de9c6ee 328 ihold(inode);
a3da7896
CH
329 d_instantiate(dentry, inode);
330 return 0;
1da177e4
LT
331}
332
333STATIC int
416c6d5b 334xfs_vn_unlink(
1da177e4
LT
335 struct inode *dir,
336 struct dentry *dentry)
337{
556b8b16 338 struct xfs_name name;
1da177e4
LT
339 int error;
340
fab8eef8 341 xfs_dentry_to_name(&name, dentry);
1da177e4 342
2b0143b5 343 error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
e5700704
CH
344 if (error)
345 return error;
346
347 /*
348 * With unlink, the VFS makes the dentry "negative": no inode,
349 * but still hashed. This is incompatible with case-insensitive
350 * mode, so invalidate (unhash) the dentry in CI-mode.
351 */
352 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
353 d_invalidate(dentry);
354 return 0;
1da177e4
LT
355}
356
357STATIC int
416c6d5b 358xfs_vn_symlink(
1da177e4
LT
359 struct inode *dir,
360 struct dentry *dentry,
361 const char *symname)
362{
3937be5b
CH
363 struct inode *inode;
364 struct xfs_inode *cip = NULL;
556b8b16 365 struct xfs_name name;
1da177e4 366 int error;
576b1d67 367 umode_t mode;
1da177e4 368
3e5daf05 369 mode = S_IFLNK |
ce3b0f8d 370 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
fab8eef8
AG
371 error = xfs_dentry_mode_to_name(&name, dentry, mode);
372 if (unlikely(error))
373 goto out;
1da177e4 374
6c77b0ea 375 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
3937be5b
CH
376 if (unlikely(error))
377 goto out;
378
01651646 379 inode = VFS_I(cip);
3937be5b 380
2a7dba39 381 error = xfs_init_security(inode, dir, &dentry->d_name);
3937be5b
CH
382 if (unlikely(error))
383 goto out_cleanup_inode;
384
2b3d1d41
CH
385 xfs_setup_iops(cip);
386
3937be5b 387 d_instantiate(dentry, inode);
58c90473 388 xfs_finish_inode_setup(cip);
3937be5b
CH
389 return 0;
390
391 out_cleanup_inode:
58c90473 392 xfs_finish_inode_setup(cip);
8f112e3b 393 xfs_cleanup_inode(dir, inode, dentry);
44a8736b 394 xfs_irele(cip);
3937be5b 395 out:
2451337d 396 return error;
1da177e4
LT
397}
398
1da177e4 399STATIC int
416c6d5b 400xfs_vn_rename(
1da177e4
LT
401 struct inode *odir,
402 struct dentry *odentry,
403 struct inode *ndir,
dbe1b5ca
CM
404 struct dentry *ndentry,
405 unsigned int flags)
1da177e4 406{
2b0143b5 407 struct inode *new_inode = d_inode(ndentry);
d31a1825 408 int omode = 0;
fab8eef8 409 int error;
556b8b16
BN
410 struct xfs_name oname;
411 struct xfs_name nname;
1da177e4 412
7dcf5c3e 413 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
dbe1b5ca
CM
414 return -EINVAL;
415
d31a1825
CM
416 /* if we are exchanging files, we need to set i_mode of both files */
417 if (flags & RENAME_EXCHANGE)
2b0143b5 418 omode = d_inode(ndentry)->i_mode;
d31a1825 419
fab8eef8
AG
420 error = xfs_dentry_mode_to_name(&oname, odentry, omode);
421 if (omode && unlikely(error))
422 return error;
423
424 error = xfs_dentry_mode_to_name(&nname, ndentry,
425 d_inode(odentry)->i_mode);
426 if (unlikely(error))
427 return error;
556b8b16 428
2b0143b5 429 return xfs_rename(XFS_I(odir), &oname, XFS_I(d_inode(odentry)),
dbe1b5ca 430 XFS_I(ndir), &nname,
d31a1825 431 new_inode ? XFS_I(new_inode) : NULL, flags);
1da177e4
LT
432}
433
434/*
435 * careful here - this function can get called recursively, so
436 * we need to be very careful about how much stack we use.
437 * uio is kmalloced for this reason...
438 */
680baacb 439STATIC const char *
6b255391 440xfs_vn_get_link(
1da177e4 441 struct dentry *dentry,
6b255391 442 struct inode *inode,
fceef393 443 struct delayed_call *done)
1da177e4 444{
1da177e4 445 char *link;
804c83c3 446 int error = -ENOMEM;
1da177e4 447
6b255391
AV
448 if (!dentry)
449 return ERR_PTR(-ECHILD);
450
6eb0b8df 451 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
804c83c3
CH
452 if (!link)
453 goto out_err;
1da177e4 454
2b0143b5 455 error = xfs_readlink(XFS_I(d_inode(dentry)), link);
804c83c3
CH
456 if (unlikely(error))
457 goto out_kfree;
1da177e4 458
fceef393
AV
459 set_delayed_call(done, kfree_link, link);
460 return link;
804c83c3
CH
461
462 out_kfree:
463 kfree(link);
464 out_err:
680baacb 465 return ERR_PTR(error);
1da177e4
LT
466}
467
30ee052e
CH
468STATIC const char *
469xfs_vn_get_link_inline(
470 struct dentry *dentry,
471 struct inode *inode,
472 struct delayed_call *done)
473{
ae294787
DW
474 char *link;
475
30ee052e 476 ASSERT(XFS_I(inode)->i_df.if_flags & XFS_IFINLINE);
ae294787
DW
477
478 /*
479 * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED if
480 * if_data is junk.
481 */
482 link = XFS_I(inode)->i_df.if_u1.if_data;
483 if (!link)
484 return ERR_PTR(-EFSCORRUPTED);
485 return link;
30ee052e
CH
486}
487
1da177e4 488STATIC int
416c6d5b 489xfs_vn_getattr(
a528d35e
DH
490 const struct path *path,
491 struct kstat *stat,
492 u32 request_mask,
493 unsigned int query_flags)
1da177e4 494{
a528d35e 495 struct inode *inode = d_inode(path->dentry);
c43f4087
CH
496 struct xfs_inode *ip = XFS_I(inode);
497 struct xfs_mount *mp = ip->i_mount;
498
cca28fb8 499 trace_xfs_getattr(ip);
c43f4087
CH
500
501 if (XFS_FORCED_SHUTDOWN(mp))
b474c7ae 502 return -EIO;
c43f4087
CH
503
504 stat->size = XFS_ISIZE(ip);
505 stat->dev = inode->i_sb->s_dev;
c19b3b05 506 stat->mode = inode->i_mode;
54d7b5c1 507 stat->nlink = inode->i_nlink;
7aab1b28
DE
508 stat->uid = inode->i_uid;
509 stat->gid = inode->i_gid;
c43f4087 510 stat->ino = ip->i_ino;
c43f4087 511 stat->atime = inode->i_atime;
f9581b14
CH
512 stat->mtime = inode->i_mtime;
513 stat->ctime = inode->i_ctime;
c43f4087
CH
514 stat->blocks =
515 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
516
5f955f26
DW
517 if (ip->i_d.di_version == 3) {
518 if (request_mask & STATX_BTIME) {
519 stat->result_mask |= STATX_BTIME;
520 stat->btime.tv_sec = ip->i_d.di_crtime.t_sec;
521 stat->btime.tv_nsec = ip->i_d.di_crtime.t_nsec;
522 }
523 }
524
525 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
526 stat->attributes |= STATX_ATTR_IMMUTABLE;
527 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
528 stat->attributes |= STATX_ATTR_APPEND;
529 if (ip->i_d.di_flags & XFS_DIFLAG_NODUMP)
530 stat->attributes |= STATX_ATTR_NODUMP;
c43f4087
CH
531
532 switch (inode->i_mode & S_IFMT) {
533 case S_IFBLK:
534 case S_IFCHR:
535 stat->blksize = BLKDEV_IOSIZE;
66f36464 536 stat->rdev = inode->i_rdev;
c43f4087
CH
537 break;
538 default:
71ddabb9 539 if (XFS_IS_REALTIME_INODE(ip)) {
c43f4087
CH
540 /*
541 * If the file blocks are being allocated from a
542 * realtime volume, then return the inode's realtime
543 * extent size or the realtime volume's extent size.
544 */
545 stat->blksize =
546 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
547 } else
548 stat->blksize = xfs_preferred_iosize(mp);
549 stat->rdev = 0;
550 break;
69e23b9a 551 }
c43f4087
CH
552
553 return 0;
1da177e4
LT
554}
555
56c19e89
DC
556static void
557xfs_setattr_mode(
56c19e89
DC
558 struct xfs_inode *ip,
559 struct iattr *iattr)
560{
0c3d88df
CH
561 struct inode *inode = VFS_I(ip);
562 umode_t mode = iattr->ia_mode;
56c19e89 563
56c19e89
DC
564 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
565
56c19e89
DC
566 inode->i_mode &= S_IFMT;
567 inode->i_mode |= mode & ~S_IFMT;
568}
569
52785112 570void
c91c46c1
CH
571xfs_setattr_time(
572 struct xfs_inode *ip,
573 struct iattr *iattr)
574{
575 struct inode *inode = VFS_I(ip);
576
577 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
578
3987848c 579 if (iattr->ia_valid & ATTR_ATIME)
c91c46c1 580 inode->i_atime = iattr->ia_atime;
3987848c 581 if (iattr->ia_valid & ATTR_CTIME)
c91c46c1 582 inode->i_ctime = iattr->ia_ctime;
3987848c 583 if (iattr->ia_valid & ATTR_MTIME)
c91c46c1 584 inode->i_mtime = iattr->ia_mtime;
c91c46c1
CH
585}
586
69bca807
JK
587static int
588xfs_vn_change_ok(
589 struct dentry *dentry,
590 struct iattr *iattr)
591{
31051c85 592 struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
69bca807
JK
593
594 if (mp->m_flags & XFS_MOUNT_RDONLY)
595 return -EROFS;
596
597 if (XFS_FORCED_SHUTDOWN(mp))
598 return -EIO;
599
31051c85 600 return setattr_prepare(dentry, iattr);
69bca807
JK
601}
602
603/*
604 * Set non-size attributes of an inode.
605 *
606 * Caution: The caller of this function is responsible for calling
31051c85 607 * setattr_prepare() or otherwise verifying the change is fine.
69bca807 608 */
c4ed4243
CH
609int
610xfs_setattr_nonsize(
611 struct xfs_inode *ip,
612 struct iattr *iattr,
613 int flags)
614{
615 xfs_mount_t *mp = ip->i_mount;
616 struct inode *inode = VFS_I(ip);
617 int mask = iattr->ia_valid;
618 xfs_trans_t *tp;
619 int error;
7aab1b28
DE
620 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
621 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
c4ed4243
CH
622 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
623 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
624
c4ed4243
CH
625 ASSERT((mask & ATTR_SIZE) == 0);
626
627 /*
628 * If disk quotas is on, we make sure that the dquots do exist on disk,
629 * before we start any other transactions. Trying to do this later
630 * is messy. We don't care to take a readlock to look at the ids
631 * in inode here, because we can't hold it across the trans_reserve.
632 * If the IDs do change before we take the ilock, we're covered
633 * because the i_*dquot fields will get updated anyway.
634 */
635 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
636 uint qflags = 0;
637
638 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
639 uid = iattr->ia_uid;
640 qflags |= XFS_QMOPT_UQUOTA;
641 } else {
7aab1b28 642 uid = inode->i_uid;
c4ed4243
CH
643 }
644 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
645 gid = iattr->ia_gid;
646 qflags |= XFS_QMOPT_GQUOTA;
647 } else {
7aab1b28 648 gid = inode->i_gid;
c4ed4243
CH
649 }
650
651 /*
652 * We take a reference when we initialize udqp and gdqp,
653 * so it is important that we never blindly double trip on
654 * the same variable. See xfs_create() for an example.
655 */
656 ASSERT(udqp == NULL);
657 ASSERT(gdqp == NULL);
7aab1b28
DE
658 error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
659 xfs_kgid_to_gid(gid),
660 xfs_get_projid(ip),
661 qflags, &udqp, &gdqp, NULL);
c4ed4243
CH
662 if (error)
663 return error;
664 }
665
253f4911 666 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
c4ed4243 667 if (error)
253f4911 668 goto out_dqrele;
c4ed4243
CH
669
670 xfs_ilock(ip, XFS_ILOCK_EXCL);
253f4911 671 xfs_trans_ijoin(tp, ip, 0);
c4ed4243
CH
672
673 /*
674 * Change file ownership. Must be the owner or privileged.
675 */
676 if (mask & (ATTR_UID|ATTR_GID)) {
677 /*
678 * These IDs could have changed since we last looked at them.
679 * But, we're assured that if the ownership did change
680 * while we didn't have the inode locked, inode's dquot(s)
681 * would have changed also.
682 */
7aab1b28
DE
683 iuid = inode->i_uid;
684 igid = inode->i_gid;
c4ed4243
CH
685 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
686 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
687
688 /*
689 * Do a quota reservation only if uid/gid is actually
690 * going to change.
691 */
692 if (XFS_IS_QUOTA_RUNNING(mp) &&
7aab1b28
DE
693 ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
694 (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
c4ed4243
CH
695 ASSERT(tp);
696 error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
92f8ff73 697 NULL, capable(CAP_FOWNER) ?
c4ed4243
CH
698 XFS_QMOPT_FORCE_RES : 0);
699 if (error) /* out of quota */
253f4911 700 goto out_cancel;
c4ed4243
CH
701 }
702 }
703
c4ed4243
CH
704 /*
705 * Change file ownership. Must be the owner or privileged.
706 */
707 if (mask & (ATTR_UID|ATTR_GID)) {
708 /*
709 * CAP_FSETID overrides the following restrictions:
710 *
711 * The set-user-ID and set-group-ID bits of a file will be
712 * cleared upon successful return from chown()
713 */
c19b3b05 714 if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
c4ed4243 715 !capable(CAP_FSETID))
c19b3b05 716 inode->i_mode &= ~(S_ISUID|S_ISGID);
c4ed4243
CH
717
718 /*
719 * Change the ownerships and register quota modifications
720 * in the transaction.
721 */
7aab1b28 722 if (!uid_eq(iuid, uid)) {
c4ed4243
CH
723 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
724 ASSERT(mask & ATTR_UID);
725 ASSERT(udqp);
726 olddquot1 = xfs_qm_vop_chown(tp, ip,
727 &ip->i_udquot, udqp);
728 }
7aab1b28 729 ip->i_d.di_uid = xfs_kuid_to_uid(uid);
c4ed4243
CH
730 inode->i_uid = uid;
731 }
7aab1b28 732 if (!gid_eq(igid, gid)) {
c4ed4243 733 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
5a01dd54
JL
734 ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
735 !XFS_IS_PQUOTA_ON(mp));
c4ed4243
CH
736 ASSERT(mask & ATTR_GID);
737 ASSERT(gdqp);
738 olddquot2 = xfs_qm_vop_chown(tp, ip,
739 &ip->i_gdquot, gdqp);
740 }
7aab1b28 741 ip->i_d.di_gid = xfs_kgid_to_gid(gid);
c4ed4243
CH
742 inode->i_gid = gid;
743 }
744 }
745
56c19e89 746 if (mask & ATTR_MODE)
0c3d88df 747 xfs_setattr_mode(ip, iattr);
c91c46c1
CH
748 if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
749 xfs_setattr_time(ip, iattr);
c4ed4243
CH
750
751 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
752
ff6d6af2 753 XFS_STATS_INC(mp, xs_ig_attrchg);
c4ed4243
CH
754
755 if (mp->m_flags & XFS_MOUNT_WSYNC)
756 xfs_trans_set_sync(tp);
70393313 757 error = xfs_trans_commit(tp);
c4ed4243
CH
758
759 xfs_iunlock(ip, XFS_ILOCK_EXCL);
760
761 /*
762 * Release any dquot(s) the inode had kept before chown.
763 */
764 xfs_qm_dqrele(olddquot1);
765 xfs_qm_dqrele(olddquot2);
766 xfs_qm_dqrele(udqp);
767 xfs_qm_dqrele(gdqp);
768
769 if (error)
b474c7ae 770 return error;
c4ed4243
CH
771
772 /*
773 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
774 * update. We could avoid this with linked transactions
775 * and passing down the transaction pointer all the way
776 * to attr_set. No previous user of the generic
777 * Posix ACL code seems to care about this issue either.
778 */
779 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
2451337d 780 error = posix_acl_chmod(inode, inode->i_mode);
c4ed4243 781 if (error)
b474c7ae 782 return error;
c4ed4243
CH
783 }
784
785 return 0;
786
253f4911 787out_cancel:
4906e215 788 xfs_trans_cancel(tp);
253f4911 789out_dqrele:
c4ed4243
CH
790 xfs_qm_dqrele(udqp);
791 xfs_qm_dqrele(gdqp);
792 return error;
793}
794
69bca807
JK
795int
796xfs_vn_setattr_nonsize(
797 struct dentry *dentry,
798 struct iattr *iattr)
799{
800 struct xfs_inode *ip = XFS_I(d_inode(dentry));
801 int error;
802
803 trace_xfs_setattr(ip);
804
805 error = xfs_vn_change_ok(dentry, iattr);
806 if (error)
807 return error;
808 return xfs_setattr_nonsize(ip, iattr, 0);
809}
810
c4ed4243
CH
811/*
812 * Truncate file. Must have write permission and not be a directory.
69bca807
JK
813 *
814 * Caution: The caller of this function is responsible for calling
31051c85 815 * setattr_prepare() or otherwise verifying the change is fine.
c4ed4243 816 */
7bf7a193 817STATIC int
c4ed4243
CH
818xfs_setattr_size(
819 struct xfs_inode *ip,
76ca4c23 820 struct iattr *iattr)
c4ed4243
CH
821{
822 struct xfs_mount *mp = ip->i_mount;
823 struct inode *inode = VFS_I(ip);
673e8e59 824 xfs_off_t oldsize, newsize;
c4ed4243
CH
825 struct xfs_trans *tp;
826 int error;
f38996f5 827 uint lock_flags = 0;
5885ebda 828 bool did_zeroing = false;
c4ed4243 829
76ca4c23 830 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
e8e9ad42 831 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
c19b3b05 832 ASSERT(S_ISREG(inode->i_mode));
fe60a8a0
CH
833 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
834 ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
c4ed4243 835
ce7ae151 836 oldsize = inode->i_size;
673e8e59
CH
837 newsize = iattr->ia_size;
838
c4ed4243
CH
839 /*
840 * Short circuit the truncate case for zero length files.
841 */
673e8e59 842 if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
fe60a8a0 843 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
76ca4c23 844 return 0;
681b1200
CH
845
846 /*
847 * Use the regular setattr path to update the timestamps.
848 */
681b1200
CH
849 iattr->ia_valid &= ~ATTR_SIZE;
850 return xfs_setattr_nonsize(ip, iattr, 0);
c4ed4243
CH
851 }
852
853 /*
854 * Make sure that the dquots are attached to the inode.
855 */
c14cfcca 856 error = xfs_qm_dqattach(ip);
c4ed4243 857 if (error)
76ca4c23 858 return error;
c4ed4243 859
f0c6bcba
CH
860 /*
861 * Wait for all direct I/O to complete.
862 */
863 inode_dio_wait(inode);
864
c4ed4243 865 /*
5885ebda
DC
866 * File data changes must be complete before we start the transaction to
867 * modify the inode. This needs to be done before joining the inode to
868 * the transaction because the inode cannot be unlocked once it is a
869 * part of the transaction.
870 *
f0c6bcba
CH
871 * Start with zeroing any data beyond EOF that we may expose on file
872 * extension, or zeroing out the rest of the block on a downward
873 * truncate.
c4ed4243 874 */
673e8e59 875 if (newsize > oldsize) {
f5c54717
CH
876 trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
877 error = iomap_zero_range(inode, oldsize, newsize - oldsize,
878 &did_zeroing, &xfs_iomap_ops);
f0c6bcba 879 } else {
459f0fbc
CH
880 error = iomap_truncate_page(inode, newsize, &did_zeroing,
881 &xfs_iomap_ops);
c4ed4243 882 }
c4ed4243 883
f0c6bcba
CH
884 if (error)
885 return error;
886
49abc3a8 887 /*
0f9160b4
DC
888 * We've already locked out new page faults, so now we can safely remove
889 * pages from the page cache knowing they won't get refaulted until we
890 * drop the XFS_MMAP_EXCL lock after the extent manipulations are
891 * complete. The truncate_setsize() call also cleans partial EOF page
892 * PTEs on extending truncates and hence ensures sub-page block size
893 * filesystems are correctly handled, too.
49abc3a8 894 *
0f9160b4
DC
895 * We have to do all the page cache truncate work outside the
896 * transaction context as the "lock" order is page lock->log space
897 * reservation as defined by extent allocation in the writeback path.
253f4911 898 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
0f9160b4
DC
899 * having already truncated the in-memory version of the file (i.e. made
900 * user visible changes). There's not much we can do about this, except
901 * to hope that the caller sees ENOMEM and retries the truncate
902 * operation.
350976ae
EG
903 *
904 * And we update in-core i_size and truncate page cache beyond newsize
905 * before writeback the [di_size, newsize] range, so we're guaranteed
906 * not to write stale data past the new EOF on truncate down.
49abc3a8 907 */
49abc3a8 908 truncate_setsize(inode, newsize);
c4ed4243 909
350976ae
EG
910 /*
911 * We are going to log the inode size change in this transaction so
912 * any previous writes that are beyond the on disk EOF and the new
913 * EOF that have not been written out need to be written here. If we
914 * do not write the data out, we expose ourselves to the null files
915 * problem. Note that this includes any block zeroing we did above;
916 * otherwise those blocks may not be zeroed after a crash.
917 */
918 if (did_zeroing ||
919 (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
920 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
921 ip->i_d.di_size, newsize - 1);
922 if (error)
923 return error;
924 }
925
253f4911 926 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
c4ed4243 927 if (error)
253f4911 928 return error;
c4ed4243 929
c4ed4243 930 lock_flags |= XFS_ILOCK_EXCL;
c4ed4243 931 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 932 xfs_trans_ijoin(tp, ip, 0);
c4ed4243
CH
933
934 /*
935 * Only change the c/mtime if we are changing the size or we are
936 * explicitly asked to change it. This handles the semantic difference
937 * between truncate() and ftruncate() as implemented in the VFS.
938 *
939 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
940 * special case where we need to update the times despite not having
941 * these flags set. For all other operations the VFS set these flags
942 * explicitly if it wants a timestamp update.
943 */
fe60a8a0
CH
944 if (newsize != oldsize &&
945 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
c4ed4243 946 iattr->ia_ctime = iattr->ia_mtime =
c2050a45 947 current_time(inode);
fe60a8a0 948 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
c4ed4243
CH
949 }
950
673e8e59
CH
951 /*
952 * The first thing we do is set the size to new_size permanently on
953 * disk. This way we don't have to worry about anyone ever being able
954 * to look at the data being freed even in the face of a crash.
955 * What we're getting around here is the case where we free a block, it
956 * is allocated to another file, it is written to, and then we crash.
957 * If the new data gets written to the file but the log buffers
958 * containing the free and reallocation don't, then we'd end up with
959 * garbage in the blocks being freed. As long as we make the new size
960 * permanent before actually freeing any blocks it doesn't matter if
961 * they get written to.
962 */
963 ip->i_d.di_size = newsize;
673e8e59
CH
964 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
965
966 if (newsize <= oldsize) {
967 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
c4ed4243 968 if (error)
4906e215 969 goto out_trans_cancel;
c4ed4243
CH
970
971 /*
972 * Truncated "down", so we're removing references to old data
973 * here - if we delay flushing for a long time, we expose
974 * ourselves unduly to the notorious NULL files problem. So,
975 * we mark this inode and flush it when the file is closed,
976 * and do not wait the usual (long) time for writeout.
977 */
978 xfs_iflags_set(ip, XFS_ITRUNCATED);
27b52867
BF
979
980 /* A truncate down always removes post-EOF blocks. */
981 xfs_inode_clear_eofblocks_tag(ip);
c4ed4243
CH
982 }
983
fe60a8a0 984 if (iattr->ia_valid & ATTR_MODE)
0c3d88df 985 xfs_setattr_mode(ip, iattr);
fe60a8a0 986 if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
c91c46c1 987 xfs_setattr_time(ip, iattr);
c4ed4243
CH
988
989 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
990
ff6d6af2 991 XFS_STATS_INC(mp, xs_ig_attrchg);
c4ed4243
CH
992
993 if (mp->m_flags & XFS_MOUNT_WSYNC)
994 xfs_trans_set_sync(tp);
995
70393313 996 error = xfs_trans_commit(tp);
c4ed4243
CH
997out_unlock:
998 if (lock_flags)
999 xfs_iunlock(ip, lock_flags);
1000 return error;
1001
c4ed4243 1002out_trans_cancel:
4906e215 1003 xfs_trans_cancel(tp);
c4ed4243
CH
1004 goto out_unlock;
1005}
1006
69bca807
JK
1007int
1008xfs_vn_setattr_size(
1009 struct dentry *dentry,
1010 struct iattr *iattr)
1011{
1012 struct xfs_inode *ip = XFS_I(d_inode(dentry));
1013 int error;
1014
1015 trace_xfs_setattr(ip);
1016
1017 error = xfs_vn_change_ok(dentry, iattr);
1018 if (error)
1019 return error;
1020 return xfs_setattr_size(ip, iattr);
1021}
1022
1da177e4 1023STATIC int
416c6d5b 1024xfs_vn_setattr(
76ca4c23
CH
1025 struct dentry *dentry,
1026 struct iattr *iattr)
1da177e4 1027{
76ca4c23
CH
1028 int error;
1029
1030 if (iattr->ia_valid & ATTR_SIZE) {
69eb5fa1
DW
1031 struct inode *inode = d_inode(dentry);
1032 struct xfs_inode *ip = XFS_I(inode);
c63a8eae 1033 uint iolock;
781355c6 1034
c63a8eae
DW
1035 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
1036 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
781355c6 1037
69eb5fa1 1038 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
c63a8eae
DW
1039 if (error) {
1040 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
65523218 1041 return error;
c63a8eae 1042 }
e8e9ad42 1043
0c187dc5 1044 error = xfs_vn_setattr_size(dentry, iattr);
65523218 1045 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
76ca4c23 1046 } else {
69bca807 1047 error = xfs_vn_setattr_nonsize(dentry, iattr);
76ca4c23
CH
1048 }
1049
2451337d 1050 return error;
1da177e4
LT
1051}
1052
69ff2826
CH
1053STATIC int
1054xfs_vn_update_time(
1055 struct inode *inode,
95582b00 1056 struct timespec64 *now,
69ff2826
CH
1057 int flags)
1058{
1059 struct xfs_inode *ip = XFS_I(inode);
1060 struct xfs_mount *mp = ip->i_mount;
c3b1b131 1061 int log_flags = XFS_ILOG_TIMESTAMP;
69ff2826
CH
1062 struct xfs_trans *tp;
1063 int error;
1064
1065 trace_xfs_update_time(ip);
1066
c3b1b131
CH
1067 if (inode->i_sb->s_flags & SB_LAZYTIME) {
1068 if (!((flags & S_VERSION) &&
1069 inode_maybe_inc_iversion(inode, false)))
1070 return generic_update_time(inode, now, flags);
1071
1072 /* Capture the iversion update that just occurred */
1073 log_flags |= XFS_ILOG_CORE;
1074 }
1075
253f4911
CH
1076 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1077 if (error)
2451337d 1078 return error;
69ff2826
CH
1079
1080 xfs_ilock(ip, XFS_ILOCK_EXCL);
3987848c 1081 if (flags & S_CTIME)
69ff2826 1082 inode->i_ctime = *now;
3987848c 1083 if (flags & S_MTIME)
69ff2826 1084 inode->i_mtime = *now;
3987848c 1085 if (flags & S_ATIME)
69ff2826 1086 inode->i_atime = *now;
3987848c 1087
69ff2826 1088 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
c3b1b131 1089 xfs_trans_log_inode(tp, ip, log_flags);
70393313 1090 return xfs_trans_commit(tp);
69ff2826
CH
1091}
1092
f35642e2
ES
1093STATIC int
1094xfs_vn_fiemap(
1095 struct inode *inode,
1096 struct fiemap_extent_info *fieinfo,
1097 u64 start,
1098 u64 length)
1099{
f35642e2
ES
1100 int error;
1101
d2bb140e 1102 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
1d4795e7
CH
1103 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1104 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
1105 error = iomap_fiemap(inode, fieinfo, start, length,
1106 &xfs_xattr_iomap_ops);
1107 } else {
1108 error = iomap_fiemap(inode, fieinfo, start, length,
1109 &xfs_iomap_ops);
1110 }
d2bb140e 1111 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
f35642e2 1112
d2bb140e 1113 return error;
f35642e2
ES
1114}
1115
99b6436b
ZYW
1116STATIC int
1117xfs_vn_tmpfile(
1118 struct inode *dir,
1119 struct dentry *dentry,
1120 umode_t mode)
1121{
d540e43b 1122 return xfs_generic_create(dir, dentry, mode, 0, true);
99b6436b
ZYW
1123}
1124
41be8bed 1125static const struct inode_operations xfs_inode_operations = {
4e34e719 1126 .get_acl = xfs_get_acl,
2401dc29 1127 .set_acl = xfs_set_acl,
416c6d5b
NS
1128 .getattr = xfs_vn_getattr,
1129 .setattr = xfs_vn_setattr,
416c6d5b 1130 .listxattr = xfs_vn_listxattr,
f35642e2 1131 .fiemap = xfs_vn_fiemap,
69ff2826 1132 .update_time = xfs_vn_update_time,
1da177e4
LT
1133};
1134
41be8bed 1135static const struct inode_operations xfs_dir_inode_operations = {
416c6d5b
NS
1136 .create = xfs_vn_create,
1137 .lookup = xfs_vn_lookup,
1138 .link = xfs_vn_link,
1139 .unlink = xfs_vn_unlink,
1140 .symlink = xfs_vn_symlink,
1141 .mkdir = xfs_vn_mkdir,
8f112e3b
CH
1142 /*
1143 * Yes, XFS uses the same method for rmdir and unlink.
1144 *
1145 * There are some subtile differences deeper in the code,
1146 * but we use S_ISDIR to check for those.
1147 */
1148 .rmdir = xfs_vn_unlink,
416c6d5b 1149 .mknod = xfs_vn_mknod,
2773bf00 1150 .rename = xfs_vn_rename,
4e34e719 1151 .get_acl = xfs_get_acl,
2401dc29 1152 .set_acl = xfs_set_acl,
416c6d5b
NS
1153 .getattr = xfs_vn_getattr,
1154 .setattr = xfs_vn_setattr,
416c6d5b 1155 .listxattr = xfs_vn_listxattr,
69ff2826 1156 .update_time = xfs_vn_update_time,
99b6436b 1157 .tmpfile = xfs_vn_tmpfile,
1da177e4
LT
1158};
1159
41be8bed 1160static const struct inode_operations xfs_dir_ci_inode_operations = {
384f3ced
BN
1161 .create = xfs_vn_create,
1162 .lookup = xfs_vn_ci_lookup,
1163 .link = xfs_vn_link,
1164 .unlink = xfs_vn_unlink,
1165 .symlink = xfs_vn_symlink,
1166 .mkdir = xfs_vn_mkdir,
8f112e3b
CH
1167 /*
1168 * Yes, XFS uses the same method for rmdir and unlink.
1169 *
1170 * There are some subtile differences deeper in the code,
1171 * but we use S_ISDIR to check for those.
1172 */
1173 .rmdir = xfs_vn_unlink,
384f3ced 1174 .mknod = xfs_vn_mknod,
2773bf00 1175 .rename = xfs_vn_rename,
4e34e719 1176 .get_acl = xfs_get_acl,
2401dc29 1177 .set_acl = xfs_set_acl,
384f3ced
BN
1178 .getattr = xfs_vn_getattr,
1179 .setattr = xfs_vn_setattr,
384f3ced 1180 .listxattr = xfs_vn_listxattr,
69ff2826 1181 .update_time = xfs_vn_update_time,
99b6436b 1182 .tmpfile = xfs_vn_tmpfile,
384f3ced
BN
1183};
1184
41be8bed 1185static const struct inode_operations xfs_symlink_inode_operations = {
6b255391 1186 .get_link = xfs_vn_get_link,
416c6d5b
NS
1187 .getattr = xfs_vn_getattr,
1188 .setattr = xfs_vn_setattr,
416c6d5b 1189 .listxattr = xfs_vn_listxattr,
69ff2826 1190 .update_time = xfs_vn_update_time,
1da177e4 1191};
41be8bed 1192
30ee052e 1193static const struct inode_operations xfs_inline_symlink_inode_operations = {
30ee052e
CH
1194 .get_link = xfs_vn_get_link_inline,
1195 .getattr = xfs_vn_getattr,
1196 .setattr = xfs_vn_setattr,
30ee052e
CH
1197 .listxattr = xfs_vn_listxattr,
1198 .update_time = xfs_vn_update_time,
1199};
1200
ba23cba9
DW
1201/* Figure out if this file actually supports DAX. */
1202static bool
1203xfs_inode_supports_dax(
1204 struct xfs_inode *ip)
1205{
1206 struct xfs_mount *mp = ip->i_mount;
1207
1208 /* Only supported on non-reflinked files. */
1209 if (!S_ISREG(VFS_I(ip)->i_mode) || xfs_is_reflink_inode(ip))
1210 return false;
1211
1212 /* DAX mount option or DAX iflag must be set. */
1213 if (!(mp->m_flags & XFS_MOUNT_DAX) &&
1214 !(ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
1215 return false;
1216
1217 /* Block size must match page size */
1218 if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1219 return false;
1220
1221 /* Device has to support DAX too. */
1222 return xfs_find_daxdev_for_inode(VFS_I(ip)) != NULL;
1223}
1224
41be8bed
CH
1225STATIC void
1226xfs_diflags_to_iflags(
1227 struct inode *inode,
1228 struct xfs_inode *ip)
1229{
cbe4dab1
DC
1230 uint16_t flags = ip->i_d.di_flags;
1231
1232 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC |
1233 S_NOATIME | S_DAX);
1234
1235 if (flags & XFS_DIFLAG_IMMUTABLE)
41be8bed 1236 inode->i_flags |= S_IMMUTABLE;
cbe4dab1 1237 if (flags & XFS_DIFLAG_APPEND)
41be8bed 1238 inode->i_flags |= S_APPEND;
cbe4dab1 1239 if (flags & XFS_DIFLAG_SYNC)
41be8bed 1240 inode->i_flags |= S_SYNC;
cbe4dab1 1241 if (flags & XFS_DIFLAG_NOATIME)
41be8bed 1242 inode->i_flags |= S_NOATIME;
ba23cba9 1243 if (xfs_inode_supports_dax(ip))
cbe4dab1 1244 inode->i_flags |= S_DAX;
41be8bed
CH
1245}
1246
1247/*
2b3d1d41 1248 * Initialize the Linux inode.
bf904248 1249 *
58c90473
DC
1250 * When reading existing inodes from disk this is called directly from xfs_iget,
1251 * when creating a new inode it is called from xfs_ialloc after setting up the
1252 * inode. These callers have different criteria for clearing XFS_INEW, so leave
1253 * it up to the caller to deal with unlocking the inode appropriately.
41be8bed
CH
1254 */
1255void
1256xfs_setup_inode(
1257 struct xfs_inode *ip)
1258{
bf904248 1259 struct inode *inode = &ip->i_vnode;
ad22c7a0 1260 gfp_t gfp_mask;
bf904248
DC
1261
1262 inode->i_ino = ip->i_ino;
eaff8079 1263 inode->i_state = I_NEW;
646ec461
CH
1264
1265 inode_sb_list_add(inode);
c6f6cd06 1266 /* make the inode look hashed for the writeback code */
5bef9151 1267 inode_fake_hash(inode);
41be8bed 1268
7aab1b28
DE
1269 inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
1270 inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
41be8bed 1271
41be8bed 1272 i_size_write(inode, ip->i_d.di_size);
41be8bed 1273 xfs_diflags_to_iflags(inode, ip);
41be8bed 1274
2b3d1d41 1275 if (S_ISDIR(inode->i_mode)) {
ef215e39
DC
1276 /*
1277 * We set the i_rwsem class here to avoid potential races with
1278 * lockdep_annotate_inode_mutex_key() reinitialising the lock
1279 * after a filehandle lookup has already found the inode in
1280 * cache before it has been unlocked via unlock_new_inode().
1281 */
1282 lockdep_set_class(&inode->i_rwsem,
1283 &inode->i_sb->s_type->i_mutex_dir_key);
93a8614e 1284 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
32c5483a 1285 ip->d_ops = ip->i_mount->m_dir_inode_ops;
2b3d1d41
CH
1286 } else {
1287 ip->d_ops = ip->i_mount->m_nondir_inode_ops;
1288 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
41be8bed
CH
1289 }
1290
ad22c7a0
DC
1291 /*
1292 * Ensure all page cache allocations are done from GFP_NOFS context to
1293 * prevent direct reclaim recursion back into the filesystem and blowing
1294 * stacks or deadlocking.
1295 */
1296 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1297 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1298
510792ee
CH
1299 /*
1300 * If there is no attribute fork no ACL can exist on this inode,
1301 * and it can't have any file capabilities attached to it either.
1302 */
1303 if (!XFS_IFORK_Q(ip)) {
1304 inode_has_no_xattr(inode);
6311b108 1305 cache_no_acl(inode);
510792ee 1306 }
41be8bed 1307}
2b3d1d41
CH
1308
1309void
1310xfs_setup_iops(
1311 struct xfs_inode *ip)
1312{
1313 struct inode *inode = &ip->i_vnode;
1314
41be8bed
CH
1315 switch (inode->i_mode & S_IFMT) {
1316 case S_IFREG:
1317 inode->i_op = &xfs_inode_operations;
1318 inode->i_fop = &xfs_file_operations;
6e2608df
DW
1319 if (IS_DAX(inode))
1320 inode->i_mapping->a_ops = &xfs_dax_aops;
1321 else
1322 inode->i_mapping->a_ops = &xfs_address_space_operations;
41be8bed
CH
1323 break;
1324 case S_IFDIR:
1325 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1326 inode->i_op = &xfs_dir_ci_inode_operations;
1327 else
1328 inode->i_op = &xfs_dir_inode_operations;
1329 inode->i_fop = &xfs_dir_file_operations;
1330 break;
1331 case S_IFLNK:
30ee052e
CH
1332 if (ip->i_df.if_flags & XFS_IFINLINE)
1333 inode->i_op = &xfs_inline_symlink_inode_operations;
1334 else
1335 inode->i_op = &xfs_symlink_inode_operations;
41be8bed
CH
1336 break;
1337 default:
1338 inode->i_op = &xfs_inode_operations;
1339 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1340 break;
1341 }
41be8bed 1342}