0001
0002
0003
0004
0005
0006 #include "xfs.h"
0007 #include "xfs_fs.h"
0008 #include "xfs_shared.h"
0009 #include "xfs_format.h"
0010 #include "xfs_log_format.h"
0011 #include "xfs_trans_resv.h"
0012 #include "xfs_mount.h"
0013 #include "xfs_inode.h"
0014 #include "xfs_acl.h"
0015 #include "xfs_quota.h"
0016 #include "xfs_da_format.h"
0017 #include "xfs_da_btree.h"
0018 #include "xfs_attr.h"
0019 #include "xfs_trans.h"
0020 #include "xfs_trace.h"
0021 #include "xfs_icache.h"
0022 #include "xfs_symlink.h"
0023 #include "xfs_dir2.h"
0024 #include "xfs_iomap.h"
0025 #include "xfs_error.h"
0026 #include "xfs_ioctl.h"
0027 #include "xfs_xattr.h"
0028
0029 #include <linux/posix_acl.h>
0030 #include <linux/security.h>
0031 #include <linux/iversion.h>
0032 #include <linux/fiemap.h>
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 static struct lock_class_key xfs_nondir_ilock_class;
0044 static struct lock_class_key xfs_dir_ilock_class;
0045
0046 static int
0047 xfs_initxattrs(
0048 struct inode *inode,
0049 const struct xattr *xattr_array,
0050 void *fs_info)
0051 {
0052 const struct xattr *xattr;
0053 struct xfs_inode *ip = XFS_I(inode);
0054 int error = 0;
0055
0056 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
0057 struct xfs_da_args args = {
0058 .dp = ip,
0059 .attr_filter = XFS_ATTR_SECURE,
0060 .name = xattr->name,
0061 .namelen = strlen(xattr->name),
0062 .value = xattr->value,
0063 .valuelen = xattr->value_len,
0064 };
0065 error = xfs_attr_change(&args);
0066 if (error < 0)
0067 break;
0068 }
0069 return error;
0070 }
0071
0072
0073
0074
0075
0076
0077
0078 int
0079 xfs_inode_init_security(
0080 struct inode *inode,
0081 struct inode *dir,
0082 const struct qstr *qstr)
0083 {
0084 return security_inode_init_security(inode, dir, qstr,
0085 &xfs_initxattrs, NULL);
0086 }
0087
0088 static void
0089 xfs_dentry_to_name(
0090 struct xfs_name *namep,
0091 struct dentry *dentry)
0092 {
0093 namep->name = dentry->d_name.name;
0094 namep->len = dentry->d_name.len;
0095 namep->type = XFS_DIR3_FT_UNKNOWN;
0096 }
0097
0098 static int
0099 xfs_dentry_mode_to_name(
0100 struct xfs_name *namep,
0101 struct dentry *dentry,
0102 int mode)
0103 {
0104 namep->name = dentry->d_name.name;
0105 namep->len = dentry->d_name.len;
0106 namep->type = xfs_mode_to_ftype(mode);
0107
0108 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
0109 return -EFSCORRUPTED;
0110
0111 return 0;
0112 }
0113
0114 STATIC void
0115 xfs_cleanup_inode(
0116 struct inode *dir,
0117 struct inode *inode,
0118 struct dentry *dentry)
0119 {
0120 struct xfs_name teardown;
0121
0122
0123
0124
0125
0126
0127 xfs_dentry_to_name(&teardown, dentry);
0128
0129 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
0130 }
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 static inline bool
0146 xfs_create_need_xattr(
0147 struct inode *dir,
0148 struct posix_acl *default_acl,
0149 struct posix_acl *acl)
0150 {
0151 if (acl)
0152 return true;
0153 if (default_acl)
0154 return true;
0155 #if IS_ENABLED(CONFIG_SECURITY)
0156 if (dir->i_sb->s_security)
0157 return true;
0158 #endif
0159 return false;
0160 }
0161
0162
0163 STATIC int
0164 xfs_generic_create(
0165 struct user_namespace *mnt_userns,
0166 struct inode *dir,
0167 struct dentry *dentry,
0168 umode_t mode,
0169 dev_t rdev,
0170 bool tmpfile)
0171 {
0172 struct inode *inode;
0173 struct xfs_inode *ip = NULL;
0174 struct posix_acl *default_acl, *acl;
0175 struct xfs_name name;
0176 int error;
0177
0178
0179
0180
0181
0182 if (S_ISCHR(mode) || S_ISBLK(mode)) {
0183 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
0184 return -EINVAL;
0185 } else {
0186 rdev = 0;
0187 }
0188
0189 error = posix_acl_create(dir, &mode, &default_acl, &acl);
0190 if (error)
0191 return error;
0192
0193
0194 error = xfs_dentry_mode_to_name(&name, dentry, mode);
0195 if (unlikely(error))
0196 goto out_free_acl;
0197
0198 if (!tmpfile) {
0199 error = xfs_create(mnt_userns, XFS_I(dir), &name, mode, rdev,
0200 xfs_create_need_xattr(dir, default_acl, acl),
0201 &ip);
0202 } else {
0203 error = xfs_create_tmpfile(mnt_userns, XFS_I(dir), mode, &ip);
0204 }
0205 if (unlikely(error))
0206 goto out_free_acl;
0207
0208 inode = VFS_I(ip);
0209
0210 error = xfs_inode_init_security(inode, dir, &dentry->d_name);
0211 if (unlikely(error))
0212 goto out_cleanup_inode;
0213
0214 if (default_acl) {
0215 error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
0216 if (error)
0217 goto out_cleanup_inode;
0218 }
0219 if (acl) {
0220 error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
0221 if (error)
0222 goto out_cleanup_inode;
0223 }
0224
0225 xfs_setup_iops(ip);
0226
0227 if (tmpfile) {
0228
0229
0230
0231
0232
0233
0234
0235
0236 set_nlink(inode, 1);
0237 d_tmpfile(dentry, inode);
0238 } else
0239 d_instantiate(dentry, inode);
0240
0241 xfs_finish_inode_setup(ip);
0242
0243 out_free_acl:
0244 posix_acl_release(default_acl);
0245 posix_acl_release(acl);
0246 return error;
0247
0248 out_cleanup_inode:
0249 xfs_finish_inode_setup(ip);
0250 if (!tmpfile)
0251 xfs_cleanup_inode(dir, inode, dentry);
0252 xfs_irele(ip);
0253 goto out_free_acl;
0254 }
0255
0256 STATIC int
0257 xfs_vn_mknod(
0258 struct user_namespace *mnt_userns,
0259 struct inode *dir,
0260 struct dentry *dentry,
0261 umode_t mode,
0262 dev_t rdev)
0263 {
0264 return xfs_generic_create(mnt_userns, dir, dentry, mode, rdev, false);
0265 }
0266
0267 STATIC int
0268 xfs_vn_create(
0269 struct user_namespace *mnt_userns,
0270 struct inode *dir,
0271 struct dentry *dentry,
0272 umode_t mode,
0273 bool flags)
0274 {
0275 return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, false);
0276 }
0277
0278 STATIC int
0279 xfs_vn_mkdir(
0280 struct user_namespace *mnt_userns,
0281 struct inode *dir,
0282 struct dentry *dentry,
0283 umode_t mode)
0284 {
0285 return xfs_generic_create(mnt_userns, dir, dentry, mode | S_IFDIR, 0,
0286 false);
0287 }
0288
0289 STATIC struct dentry *
0290 xfs_vn_lookup(
0291 struct inode *dir,
0292 struct dentry *dentry,
0293 unsigned int flags)
0294 {
0295 struct inode *inode;
0296 struct xfs_inode *cip;
0297 struct xfs_name name;
0298 int error;
0299
0300 if (dentry->d_name.len >= MAXNAMELEN)
0301 return ERR_PTR(-ENAMETOOLONG);
0302
0303 xfs_dentry_to_name(&name, dentry);
0304 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
0305 if (likely(!error))
0306 inode = VFS_I(cip);
0307 else if (likely(error == -ENOENT))
0308 inode = NULL;
0309 else
0310 inode = ERR_PTR(error);
0311 return d_splice_alias(inode, dentry);
0312 }
0313
0314 STATIC struct dentry *
0315 xfs_vn_ci_lookup(
0316 struct inode *dir,
0317 struct dentry *dentry,
0318 unsigned int flags)
0319 {
0320 struct xfs_inode *ip;
0321 struct xfs_name xname;
0322 struct xfs_name ci_name;
0323 struct qstr dname;
0324 int error;
0325
0326 if (dentry->d_name.len >= MAXNAMELEN)
0327 return ERR_PTR(-ENAMETOOLONG);
0328
0329 xfs_dentry_to_name(&xname, dentry);
0330 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
0331 if (unlikely(error)) {
0332 if (unlikely(error != -ENOENT))
0333 return ERR_PTR(error);
0334
0335
0336
0337
0338
0339 return NULL;
0340 }
0341
0342
0343 if (!ci_name.name)
0344 return d_splice_alias(VFS_I(ip), dentry);
0345
0346
0347 dname.name = ci_name.name;
0348 dname.len = ci_name.len;
0349 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
0350 kmem_free(ci_name.name);
0351 return dentry;
0352 }
0353
0354 STATIC int
0355 xfs_vn_link(
0356 struct dentry *old_dentry,
0357 struct inode *dir,
0358 struct dentry *dentry)
0359 {
0360 struct inode *inode = d_inode(old_dentry);
0361 struct xfs_name name;
0362 int error;
0363
0364 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
0365 if (unlikely(error))
0366 return error;
0367
0368 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
0369 if (unlikely(error))
0370 return error;
0371
0372 ihold(inode);
0373 d_instantiate(dentry, inode);
0374 return 0;
0375 }
0376
0377 STATIC int
0378 xfs_vn_unlink(
0379 struct inode *dir,
0380 struct dentry *dentry)
0381 {
0382 struct xfs_name name;
0383 int error;
0384
0385 xfs_dentry_to_name(&name, dentry);
0386
0387 error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
0388 if (error)
0389 return error;
0390
0391
0392
0393
0394
0395
0396 if (xfs_has_asciici(XFS_M(dir->i_sb)))
0397 d_invalidate(dentry);
0398 return 0;
0399 }
0400
0401 STATIC int
0402 xfs_vn_symlink(
0403 struct user_namespace *mnt_userns,
0404 struct inode *dir,
0405 struct dentry *dentry,
0406 const char *symname)
0407 {
0408 struct inode *inode;
0409 struct xfs_inode *cip = NULL;
0410 struct xfs_name name;
0411 int error;
0412 umode_t mode;
0413
0414 mode = S_IFLNK |
0415 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
0416 error = xfs_dentry_mode_to_name(&name, dentry, mode);
0417 if (unlikely(error))
0418 goto out;
0419
0420 error = xfs_symlink(mnt_userns, XFS_I(dir), &name, symname, mode, &cip);
0421 if (unlikely(error))
0422 goto out;
0423
0424 inode = VFS_I(cip);
0425
0426 error = xfs_inode_init_security(inode, dir, &dentry->d_name);
0427 if (unlikely(error))
0428 goto out_cleanup_inode;
0429
0430 xfs_setup_iops(cip);
0431
0432 d_instantiate(dentry, inode);
0433 xfs_finish_inode_setup(cip);
0434 return 0;
0435
0436 out_cleanup_inode:
0437 xfs_finish_inode_setup(cip);
0438 xfs_cleanup_inode(dir, inode, dentry);
0439 xfs_irele(cip);
0440 out:
0441 return error;
0442 }
0443
0444 STATIC int
0445 xfs_vn_rename(
0446 struct user_namespace *mnt_userns,
0447 struct inode *odir,
0448 struct dentry *odentry,
0449 struct inode *ndir,
0450 struct dentry *ndentry,
0451 unsigned int flags)
0452 {
0453 struct inode *new_inode = d_inode(ndentry);
0454 int omode = 0;
0455 int error;
0456 struct xfs_name oname;
0457 struct xfs_name nname;
0458
0459 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
0460 return -EINVAL;
0461
0462
0463 if (flags & RENAME_EXCHANGE)
0464 omode = d_inode(ndentry)->i_mode;
0465
0466 error = xfs_dentry_mode_to_name(&oname, odentry, omode);
0467 if (omode && unlikely(error))
0468 return error;
0469
0470 error = xfs_dentry_mode_to_name(&nname, ndentry,
0471 d_inode(odentry)->i_mode);
0472 if (unlikely(error))
0473 return error;
0474
0475 return xfs_rename(mnt_userns, XFS_I(odir), &oname,
0476 XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
0477 new_inode ? XFS_I(new_inode) : NULL, flags);
0478 }
0479
0480
0481
0482
0483
0484
0485 STATIC const char *
0486 xfs_vn_get_link(
0487 struct dentry *dentry,
0488 struct inode *inode,
0489 struct delayed_call *done)
0490 {
0491 char *link;
0492 int error = -ENOMEM;
0493
0494 if (!dentry)
0495 return ERR_PTR(-ECHILD);
0496
0497 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
0498 if (!link)
0499 goto out_err;
0500
0501 error = xfs_readlink(XFS_I(d_inode(dentry)), link);
0502 if (unlikely(error))
0503 goto out_kfree;
0504
0505 set_delayed_call(done, kfree_link, link);
0506 return link;
0507
0508 out_kfree:
0509 kfree(link);
0510 out_err:
0511 return ERR_PTR(error);
0512 }
0513
0514 static uint32_t
0515 xfs_stat_blksize(
0516 struct xfs_inode *ip)
0517 {
0518 struct xfs_mount *mp = ip->i_mount;
0519
0520
0521
0522
0523
0524 if (XFS_IS_REALTIME_INODE(ip))
0525 return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip));
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540 if (xfs_has_large_iosize(mp)) {
0541 if (mp->m_swidth)
0542 return XFS_FSB_TO_B(mp, mp->m_swidth);
0543 if (xfs_has_allocsize(mp))
0544 return 1U << mp->m_allocsize_log;
0545 }
0546
0547 return PAGE_SIZE;
0548 }
0549
0550 STATIC int
0551 xfs_vn_getattr(
0552 struct user_namespace *mnt_userns,
0553 const struct path *path,
0554 struct kstat *stat,
0555 u32 request_mask,
0556 unsigned int query_flags)
0557 {
0558 struct inode *inode = d_inode(path->dentry);
0559 struct xfs_inode *ip = XFS_I(inode);
0560 struct xfs_mount *mp = ip->i_mount;
0561
0562 trace_xfs_getattr(ip);
0563
0564 if (xfs_is_shutdown(mp))
0565 return -EIO;
0566
0567 stat->size = XFS_ISIZE(ip);
0568 stat->dev = inode->i_sb->s_dev;
0569 stat->mode = inode->i_mode;
0570 stat->nlink = inode->i_nlink;
0571 stat->uid = i_uid_into_mnt(mnt_userns, inode);
0572 stat->gid = i_gid_into_mnt(mnt_userns, inode);
0573 stat->ino = ip->i_ino;
0574 stat->atime = inode->i_atime;
0575 stat->mtime = inode->i_mtime;
0576 stat->ctime = inode->i_ctime;
0577 stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
0578
0579 if (xfs_has_v3inodes(mp)) {
0580 if (request_mask & STATX_BTIME) {
0581 stat->result_mask |= STATX_BTIME;
0582 stat->btime = ip->i_crtime;
0583 }
0584 }
0585
0586
0587
0588
0589
0590 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
0591 stat->attributes |= STATX_ATTR_IMMUTABLE;
0592 if (ip->i_diflags & XFS_DIFLAG_APPEND)
0593 stat->attributes |= STATX_ATTR_APPEND;
0594 if (ip->i_diflags & XFS_DIFLAG_NODUMP)
0595 stat->attributes |= STATX_ATTR_NODUMP;
0596
0597 stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
0598 STATX_ATTR_APPEND |
0599 STATX_ATTR_NODUMP);
0600
0601 switch (inode->i_mode & S_IFMT) {
0602 case S_IFBLK:
0603 case S_IFCHR:
0604 stat->blksize = BLKDEV_IOSIZE;
0605 stat->rdev = inode->i_rdev;
0606 break;
0607 default:
0608 stat->blksize = xfs_stat_blksize(ip);
0609 stat->rdev = 0;
0610 break;
0611 }
0612
0613 return 0;
0614 }
0615
0616 static int
0617 xfs_vn_change_ok(
0618 struct user_namespace *mnt_userns,
0619 struct dentry *dentry,
0620 struct iattr *iattr)
0621 {
0622 struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
0623
0624 if (xfs_is_readonly(mp))
0625 return -EROFS;
0626
0627 if (xfs_is_shutdown(mp))
0628 return -EIO;
0629
0630 return setattr_prepare(mnt_userns, dentry, iattr);
0631 }
0632
0633
0634
0635
0636
0637
0638
0639 static int
0640 xfs_setattr_nonsize(
0641 struct user_namespace *mnt_userns,
0642 struct xfs_inode *ip,
0643 struct iattr *iattr)
0644 {
0645 xfs_mount_t *mp = ip->i_mount;
0646 struct inode *inode = VFS_I(ip);
0647 int mask = iattr->ia_valid;
0648 xfs_trans_t *tp;
0649 int error;
0650 kuid_t uid = GLOBAL_ROOT_UID;
0651 kgid_t gid = GLOBAL_ROOT_GID;
0652 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
0653 struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL;
0654
0655 ASSERT((mask & ATTR_SIZE) == 0);
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
0666 uint qflags = 0;
0667
0668 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
0669 uid = from_vfsuid(mnt_userns, i_user_ns(inode),
0670 iattr->ia_vfsuid);
0671 qflags |= XFS_QMOPT_UQUOTA;
0672 } else {
0673 uid = inode->i_uid;
0674 }
0675 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
0676 gid = from_vfsgid(mnt_userns, i_user_ns(inode),
0677 iattr->ia_vfsgid);
0678 qflags |= XFS_QMOPT_GQUOTA;
0679 } else {
0680 gid = inode->i_gid;
0681 }
0682
0683
0684
0685
0686
0687
0688 ASSERT(udqp == NULL);
0689 ASSERT(gdqp == NULL);
0690 error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
0691 qflags, &udqp, &gdqp, NULL);
0692 if (error)
0693 return error;
0694 }
0695
0696 error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
0697 has_capability_noaudit(current, CAP_FOWNER), &tp);
0698 if (error)
0699 goto out_dqrele;
0700
0701
0702
0703
0704
0705
0706
0707
0708 if (XFS_IS_UQUOTA_ON(mp) &&
0709 i_uid_needs_update(mnt_userns, iattr, inode)) {
0710 ASSERT(udqp);
0711 old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp);
0712 }
0713 if (XFS_IS_GQUOTA_ON(mp) &&
0714 i_gid_needs_update(mnt_userns, iattr, inode)) {
0715 ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp));
0716 ASSERT(gdqp);
0717 old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp);
0718 }
0719
0720 setattr_copy(mnt_userns, inode, iattr);
0721 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0722
0723 XFS_STATS_INC(mp, xs_ig_attrchg);
0724
0725 if (xfs_has_wsync(mp))
0726 xfs_trans_set_sync(tp);
0727 error = xfs_trans_commit(tp);
0728
0729
0730
0731
0732 xfs_qm_dqrele(old_udqp);
0733 xfs_qm_dqrele(old_gdqp);
0734 xfs_qm_dqrele(udqp);
0735 xfs_qm_dqrele(gdqp);
0736
0737 if (error)
0738 return error;
0739
0740
0741
0742
0743
0744
0745
0746
0747 if (mask & ATTR_MODE) {
0748 error = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
0749 if (error)
0750 return error;
0751 }
0752
0753 return 0;
0754
0755 out_dqrele:
0756 xfs_qm_dqrele(udqp);
0757 xfs_qm_dqrele(gdqp);
0758 return error;
0759 }
0760
0761
0762
0763
0764
0765
0766
0767 STATIC int
0768 xfs_setattr_size(
0769 struct user_namespace *mnt_userns,
0770 struct xfs_inode *ip,
0771 struct iattr *iattr)
0772 {
0773 struct xfs_mount *mp = ip->i_mount;
0774 struct inode *inode = VFS_I(ip);
0775 xfs_off_t oldsize, newsize;
0776 struct xfs_trans *tp;
0777 int error;
0778 uint lock_flags = 0;
0779 bool did_zeroing = false;
0780
0781 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
0782 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
0783 ASSERT(S_ISREG(inode->i_mode));
0784 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
0785 ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
0786
0787 oldsize = inode->i_size;
0788 newsize = iattr->ia_size;
0789
0790
0791
0792
0793 if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
0794 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
0795 return 0;
0796
0797
0798
0799
0800 iattr->ia_valid &= ~ATTR_SIZE;
0801 return xfs_setattr_nonsize(mnt_userns, ip, iattr);
0802 }
0803
0804
0805
0806
0807 error = xfs_qm_dqattach(ip);
0808 if (error)
0809 return error;
0810
0811
0812
0813
0814 inode_dio_wait(inode);
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826 if (newsize > oldsize) {
0827 trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
0828 error = xfs_zero_range(ip, oldsize, newsize - oldsize,
0829 &did_zeroing);
0830 } else {
0831
0832
0833
0834
0835
0836
0837 error = filemap_write_and_wait_range(inode->i_mapping, newsize,
0838 newsize);
0839 if (error)
0840 return error;
0841 error = xfs_truncate_page(ip, newsize, &did_zeroing);
0842 }
0843
0844 if (error)
0845 return error;
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868 truncate_setsize(inode, newsize);
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878 if (did_zeroing ||
0879 (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
0880 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
0881 ip->i_disk_size, newsize - 1);
0882 if (error)
0883 return error;
0884 }
0885
0886 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
0887 if (error)
0888 return error;
0889
0890 lock_flags |= XFS_ILOCK_EXCL;
0891 xfs_ilock(ip, XFS_ILOCK_EXCL);
0892 xfs_trans_ijoin(tp, ip, 0);
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904 if (newsize != oldsize &&
0905 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
0906 iattr->ia_ctime = iattr->ia_mtime =
0907 current_time(inode);
0908 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
0909 }
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923 ip->i_disk_size = newsize;
0924 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0925
0926 if (newsize <= oldsize) {
0927 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
0928 if (error)
0929 goto out_trans_cancel;
0930
0931
0932
0933
0934
0935
0936
0937
0938 xfs_iflags_set(ip, XFS_ITRUNCATED);
0939
0940
0941 xfs_inode_clear_eofblocks_tag(ip);
0942 }
0943
0944 ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
0945 setattr_copy(mnt_userns, inode, iattr);
0946 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0947
0948 XFS_STATS_INC(mp, xs_ig_attrchg);
0949
0950 if (xfs_has_wsync(mp))
0951 xfs_trans_set_sync(tp);
0952
0953 error = xfs_trans_commit(tp);
0954 out_unlock:
0955 if (lock_flags)
0956 xfs_iunlock(ip, lock_flags);
0957 return error;
0958
0959 out_trans_cancel:
0960 xfs_trans_cancel(tp);
0961 goto out_unlock;
0962 }
0963
0964 int
0965 xfs_vn_setattr_size(
0966 struct user_namespace *mnt_userns,
0967 struct dentry *dentry,
0968 struct iattr *iattr)
0969 {
0970 struct xfs_inode *ip = XFS_I(d_inode(dentry));
0971 int error;
0972
0973 trace_xfs_setattr(ip);
0974
0975 error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
0976 if (error)
0977 return error;
0978 return xfs_setattr_size(mnt_userns, ip, iattr);
0979 }
0980
0981 STATIC int
0982 xfs_vn_setattr(
0983 struct user_namespace *mnt_userns,
0984 struct dentry *dentry,
0985 struct iattr *iattr)
0986 {
0987 struct inode *inode = d_inode(dentry);
0988 struct xfs_inode *ip = XFS_I(inode);
0989 int error;
0990
0991 if (iattr->ia_valid & ATTR_SIZE) {
0992 uint iolock;
0993
0994 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
0995 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
0996
0997 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
0998 if (error) {
0999 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1000 return error;
1001 }
1002
1003 error = xfs_vn_setattr_size(mnt_userns, dentry, iattr);
1004 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1005 } else {
1006 trace_xfs_setattr(ip);
1007
1008 error = xfs_vn_change_ok(mnt_userns, dentry, iattr);
1009 if (!error)
1010 error = xfs_setattr_nonsize(mnt_userns, ip, iattr);
1011 }
1012
1013 return error;
1014 }
1015
1016 STATIC int
1017 xfs_vn_update_time(
1018 struct inode *inode,
1019 struct timespec64 *now,
1020 int flags)
1021 {
1022 struct xfs_inode *ip = XFS_I(inode);
1023 struct xfs_mount *mp = ip->i_mount;
1024 int log_flags = XFS_ILOG_TIMESTAMP;
1025 struct xfs_trans *tp;
1026 int error;
1027
1028 trace_xfs_update_time(ip);
1029
1030 if (inode->i_sb->s_flags & SB_LAZYTIME) {
1031 if (!((flags & S_VERSION) &&
1032 inode_maybe_inc_iversion(inode, false)))
1033 return generic_update_time(inode, now, flags);
1034
1035
1036 log_flags |= XFS_ILOG_CORE;
1037 }
1038
1039 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1040 if (error)
1041 return error;
1042
1043 xfs_ilock(ip, XFS_ILOCK_EXCL);
1044 if (flags & S_CTIME)
1045 inode->i_ctime = *now;
1046 if (flags & S_MTIME)
1047 inode->i_mtime = *now;
1048 if (flags & S_ATIME)
1049 inode->i_atime = *now;
1050
1051 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1052 xfs_trans_log_inode(tp, ip, log_flags);
1053 return xfs_trans_commit(tp);
1054 }
1055
1056 STATIC int
1057 xfs_vn_fiemap(
1058 struct inode *inode,
1059 struct fiemap_extent_info *fieinfo,
1060 u64 start,
1061 u64 length)
1062 {
1063 int error;
1064
1065 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
1066 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1067 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
1068 error = iomap_fiemap(inode, fieinfo, start, length,
1069 &xfs_xattr_iomap_ops);
1070 } else {
1071 error = iomap_fiemap(inode, fieinfo, start, length,
1072 &xfs_read_iomap_ops);
1073 }
1074 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
1075
1076 return error;
1077 }
1078
1079 STATIC int
1080 xfs_vn_tmpfile(
1081 struct user_namespace *mnt_userns,
1082 struct inode *dir,
1083 struct dentry *dentry,
1084 umode_t mode)
1085 {
1086 return xfs_generic_create(mnt_userns, dir, dentry, mode, 0, true);
1087 }
1088
1089 static const struct inode_operations xfs_inode_operations = {
1090 .get_acl = xfs_get_acl,
1091 .set_acl = xfs_set_acl,
1092 .getattr = xfs_vn_getattr,
1093 .setattr = xfs_vn_setattr,
1094 .listxattr = xfs_vn_listxattr,
1095 .fiemap = xfs_vn_fiemap,
1096 .update_time = xfs_vn_update_time,
1097 .fileattr_get = xfs_fileattr_get,
1098 .fileattr_set = xfs_fileattr_set,
1099 };
1100
1101 static const struct inode_operations xfs_dir_inode_operations = {
1102 .create = xfs_vn_create,
1103 .lookup = xfs_vn_lookup,
1104 .link = xfs_vn_link,
1105 .unlink = xfs_vn_unlink,
1106 .symlink = xfs_vn_symlink,
1107 .mkdir = xfs_vn_mkdir,
1108
1109
1110
1111
1112
1113
1114 .rmdir = xfs_vn_unlink,
1115 .mknod = xfs_vn_mknod,
1116 .rename = xfs_vn_rename,
1117 .get_acl = xfs_get_acl,
1118 .set_acl = xfs_set_acl,
1119 .getattr = xfs_vn_getattr,
1120 .setattr = xfs_vn_setattr,
1121 .listxattr = xfs_vn_listxattr,
1122 .update_time = xfs_vn_update_time,
1123 .tmpfile = xfs_vn_tmpfile,
1124 .fileattr_get = xfs_fileattr_get,
1125 .fileattr_set = xfs_fileattr_set,
1126 };
1127
1128 static const struct inode_operations xfs_dir_ci_inode_operations = {
1129 .create = xfs_vn_create,
1130 .lookup = xfs_vn_ci_lookup,
1131 .link = xfs_vn_link,
1132 .unlink = xfs_vn_unlink,
1133 .symlink = xfs_vn_symlink,
1134 .mkdir = xfs_vn_mkdir,
1135
1136
1137
1138
1139
1140
1141 .rmdir = xfs_vn_unlink,
1142 .mknod = xfs_vn_mknod,
1143 .rename = xfs_vn_rename,
1144 .get_acl = xfs_get_acl,
1145 .set_acl = xfs_set_acl,
1146 .getattr = xfs_vn_getattr,
1147 .setattr = xfs_vn_setattr,
1148 .listxattr = xfs_vn_listxattr,
1149 .update_time = xfs_vn_update_time,
1150 .tmpfile = xfs_vn_tmpfile,
1151 .fileattr_get = xfs_fileattr_get,
1152 .fileattr_set = xfs_fileattr_set,
1153 };
1154
1155 static const struct inode_operations xfs_symlink_inode_operations = {
1156 .get_link = xfs_vn_get_link,
1157 .getattr = xfs_vn_getattr,
1158 .setattr = xfs_vn_setattr,
1159 .listxattr = xfs_vn_listxattr,
1160 .update_time = xfs_vn_update_time,
1161 };
1162
1163
1164 static bool
1165 xfs_inode_supports_dax(
1166 struct xfs_inode *ip)
1167 {
1168 struct xfs_mount *mp = ip->i_mount;
1169
1170
1171 if (!S_ISREG(VFS_I(ip)->i_mode))
1172 return false;
1173
1174
1175 if (xfs_is_reflink_inode(ip))
1176 return false;
1177
1178
1179 if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1180 return false;
1181
1182
1183 return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
1184 }
1185
1186 static bool
1187 xfs_inode_should_enable_dax(
1188 struct xfs_inode *ip)
1189 {
1190 if (!IS_ENABLED(CONFIG_FS_DAX))
1191 return false;
1192 if (xfs_has_dax_never(ip->i_mount))
1193 return false;
1194 if (!xfs_inode_supports_dax(ip))
1195 return false;
1196 if (xfs_has_dax_always(ip->i_mount))
1197 return true;
1198 if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
1199 return true;
1200 return false;
1201 }
1202
1203 void
1204 xfs_diflags_to_iflags(
1205 struct xfs_inode *ip,
1206 bool init)
1207 {
1208 struct inode *inode = VFS_I(ip);
1209 unsigned int xflags = xfs_ip2xflags(ip);
1210 unsigned int flags = 0;
1211
1212 ASSERT(!(IS_DAX(inode) && init));
1213
1214 if (xflags & FS_XFLAG_IMMUTABLE)
1215 flags |= S_IMMUTABLE;
1216 if (xflags & FS_XFLAG_APPEND)
1217 flags |= S_APPEND;
1218 if (xflags & FS_XFLAG_SYNC)
1219 flags |= S_SYNC;
1220 if (xflags & FS_XFLAG_NOATIME)
1221 flags |= S_NOATIME;
1222 if (init && xfs_inode_should_enable_dax(ip))
1223 flags |= S_DAX;
1224
1225
1226
1227
1228
1229 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
1230 inode->i_flags |= flags;
1231 }
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241 void
1242 xfs_setup_inode(
1243 struct xfs_inode *ip)
1244 {
1245 struct inode *inode = &ip->i_vnode;
1246 gfp_t gfp_mask;
1247
1248 inode->i_ino = ip->i_ino;
1249 inode->i_state |= I_NEW;
1250
1251 inode_sb_list_add(inode);
1252
1253 inode_fake_hash(inode);
1254
1255 i_size_write(inode, ip->i_disk_size);
1256 xfs_diflags_to_iflags(ip, true);
1257
1258 if (S_ISDIR(inode->i_mode)) {
1259
1260
1261
1262
1263
1264
1265 lockdep_set_class(&inode->i_rwsem,
1266 &inode->i_sb->s_type->i_mutex_dir_key);
1267 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
1268 } else {
1269 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
1270 }
1271
1272
1273
1274
1275
1276
1277 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1278 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1279
1280
1281
1282
1283
1284 if (!xfs_inode_has_attr_fork(ip)) {
1285 inode_has_no_xattr(inode);
1286 cache_no_acl(inode);
1287 }
1288 }
1289
1290 void
1291 xfs_setup_iops(
1292 struct xfs_inode *ip)
1293 {
1294 struct inode *inode = &ip->i_vnode;
1295
1296 switch (inode->i_mode & S_IFMT) {
1297 case S_IFREG:
1298 inode->i_op = &xfs_inode_operations;
1299 inode->i_fop = &xfs_file_operations;
1300 if (IS_DAX(inode))
1301 inode->i_mapping->a_ops = &xfs_dax_aops;
1302 else
1303 inode->i_mapping->a_ops = &xfs_address_space_operations;
1304 break;
1305 case S_IFDIR:
1306 if (xfs_has_asciici(XFS_M(inode->i_sb)))
1307 inode->i_op = &xfs_dir_ci_inode_operations;
1308 else
1309 inode->i_op = &xfs_dir_inode_operations;
1310 inode->i_fop = &xfs_dir_file_operations;
1311 break;
1312 case S_IFLNK:
1313 inode->i_op = &xfs_symlink_inode_operations;
1314 break;
1315 default:
1316 inode->i_op = &xfs_inode_operations;
1317 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1318 break;
1319 }
1320 }