1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_shared.h"
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
24#include "xfs_mount.h"
25#include "xfs_da_format.h"
26#include "xfs_inode.h"
27#include "xfs_bmap.h"
28#include "xfs_bmap_util.h"
29#include "xfs_acl.h"
30#include "xfs_quota.h"
31#include "xfs_error.h"
32#include "xfs_attr.h"
33#include "xfs_trans.h"
34#include "xfs_trace.h"
35#include "xfs_icache.h"
36#include "xfs_symlink.h"
37#include "xfs_da_btree.h"
38#include "xfs_dir2.h"
39#include "xfs_pnfs.h"
40#include "xfs_iomap.h"
41#include "xfs_trans_space.h"
42
43#include <linux/capability.h>
44#include <linux/xattr.h>
45#include <linux/namei.h>
46#include <linux/posix_acl.h>
47#include <linux/security.h>
48#include <linux/iomap.h>
49#include <linux/slab.h>
50
51
52
53
54
55
56
57
58
59
60static struct lock_class_key xfs_nondir_ilock_class;
61static struct lock_class_key xfs_dir_ilock_class;
62
63static int
64xfs_initxattrs(
65 struct inode *inode,
66 const struct xattr *xattr_array,
67 void *fs_info)
68{
69 const struct xattr *xattr;
70 struct xfs_inode *ip = XFS_I(inode);
71 int error = 0;
72
73 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
74 error = xfs_attr_set(ip, xattr->name, xattr->value,
75 xattr->value_len, ATTR_SECURE);
76 if (error < 0)
77 break;
78 }
79 return error;
80}
81
82
83
84
85
86
87
88
89STATIC int
90xfs_init_security(
91 struct inode *inode,
92 struct inode *dir,
93 const struct qstr *qstr)
94{
95 return security_inode_init_security(inode, dir, qstr,
96 &xfs_initxattrs, NULL);
97}
98
99static void
100xfs_dentry_to_name(
101 struct xfs_name *namep,
102 struct dentry *dentry)
103{
104 namep->name = dentry->d_name.name;
105 namep->len = dentry->d_name.len;
106 namep->type = XFS_DIR3_FT_UNKNOWN;
107}
108
109static int
110xfs_dentry_mode_to_name(
111 struct xfs_name *namep,
112 struct dentry *dentry,
113 int mode)
114{
115 namep->name = dentry->d_name.name;
116 namep->len = dentry->d_name.len;
117 namep->type = xfs_mode_to_ftype(mode);
118
119 if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
120 return -EFSCORRUPTED;
121
122 return 0;
123}
124
125STATIC void
126xfs_cleanup_inode(
127 struct inode *dir,
128 struct inode *inode,
129 struct dentry *dentry)
130{
131 struct xfs_name teardown;
132
133
134
135
136
137
138 xfs_dentry_to_name(&teardown, dentry);
139
140 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
141}
142
143STATIC int
144xfs_generic_create(
145 struct inode *dir,
146 struct dentry *dentry,
147 umode_t mode,
148 dev_t rdev,
149 bool tmpfile)
150{
151 struct inode *inode;
152 struct xfs_inode *ip = NULL;
153 struct posix_acl *default_acl = NULL;
154 struct xfs_name name;
155 int error;
156
157
158
159
160
161 if (S_ISCHR(mode) || S_ISBLK(mode)) {
162 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
163 return -EINVAL;
164 } else {
165 rdev = 0;
166 }
167
168 if (IS_POSIXACL(dir)) {
169 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
170 if (IS_ERR(default_acl))
171 return PTR_ERR(default_acl);
172
173 if (!default_acl)
174 mode &= ~current_umask();
175 }
176
177
178 error = xfs_dentry_mode_to_name(&name, dentry, mode);
179 if (unlikely(error))
180 goto out_free_acl;
181
182 if (!tmpfile) {
183 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
184 } else {
185 error = xfs_create_tmpfile(XFS_I(dir), dentry, mode, &ip);
186 }
187 if (unlikely(error))
188 goto out_free_acl;
189
190 inode = VFS_I(ip);
191
192 error = xfs_init_security(inode, dir, &dentry->d_name);
193 if (unlikely(error))
194 goto out_cleanup_inode;
195
196 if (default_acl) {
197 error = xfs_inherit_acl(inode, default_acl);
198 default_acl = NULL;
199 if (unlikely(error))
200 goto out_cleanup_inode;
201 }
202
203 xfs_setup_iops(ip);
204
205 if (tmpfile) {
206
207
208
209
210
211
212
213
214 set_nlink(inode, 1);
215 d_tmpfile(dentry, inode);
216 } else
217 d_instantiate(dentry, inode);
218 xfs_finish_inode_setup(ip);
219 return error;
220
221 out_cleanup_inode:
222 xfs_finish_inode_setup(ip);
223 if (!tmpfile)
224 xfs_cleanup_inode(dir, inode, dentry);
225 iput(inode);
226 out_free_acl:
227 posix_acl_release(default_acl);
228 return error;
229}
230
231STATIC int
232xfs_vn_mknod(
233 struct inode *dir,
234 struct dentry *dentry,
235 umode_t mode,
236 dev_t rdev)
237{
238 return xfs_generic_create(dir, dentry, mode, rdev, false);
239}
240
241STATIC int
242xfs_vn_create(
243 struct inode *dir,
244 struct dentry *dentry,
245 umode_t mode,
246 bool flags)
247{
248 return xfs_vn_mknod(dir, dentry, mode, 0);
249}
250
251STATIC int
252xfs_vn_mkdir(
253 struct inode *dir,
254 struct dentry *dentry,
255 umode_t mode)
256{
257 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
258}
259
260STATIC struct dentry *
261xfs_vn_lookup(
262 struct inode *dir,
263 struct dentry *dentry,
264 unsigned int flags)
265{
266 struct xfs_inode *cip;
267 struct xfs_name name;
268 int error;
269
270 if (dentry->d_name.len >= MAXNAMELEN)
271 return ERR_PTR(-ENAMETOOLONG);
272
273 xfs_dentry_to_name(&name, dentry);
274 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
275 if (unlikely(error)) {
276 if (unlikely(error != -ENOENT))
277 return ERR_PTR(error);
278 d_add(dentry, NULL);
279 return NULL;
280 }
281
282 return d_splice_alias(VFS_I(cip), dentry);
283}
284
285STATIC struct dentry *
286xfs_vn_ci_lookup(
287 struct inode *dir,
288 struct dentry *dentry,
289 unsigned int flags)
290{
291 struct xfs_inode *ip;
292 struct xfs_name xname;
293 struct xfs_name ci_name;
294 struct qstr dname;
295 int error;
296
297 if (dentry->d_name.len >= MAXNAMELEN)
298 return ERR_PTR(-ENAMETOOLONG);
299
300 xfs_dentry_to_name(&xname, dentry);
301 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
302 if (unlikely(error)) {
303 if (unlikely(error != -ENOENT))
304 return ERR_PTR(error);
305
306
307
308
309
310 return NULL;
311 }
312
313
314 if (!ci_name.name)
315 return d_splice_alias(VFS_I(ip), dentry);
316
317
318 dname.name = ci_name.name;
319 dname.len = ci_name.len;
320 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
321 kmem_free(ci_name.name);
322 return dentry;
323}
324
325STATIC int
326xfs_vn_link(
327 struct dentry *old_dentry,
328 struct inode *dir,
329 struct dentry *dentry)
330{
331 struct inode *inode = old_dentry->d_inode;
332 struct xfs_name name;
333 int error;
334
335 error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
336 if (unlikely(error))
337 return error;
338
339 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
340 if (unlikely(error))
341 return error;
342
343 ihold(inode);
344 d_instantiate(dentry, inode);
345 return 0;
346}
347
348STATIC int
349xfs_vn_unlink(
350 struct inode *dir,
351 struct dentry *dentry)
352{
353 struct xfs_name name;
354 int error;
355
356 xfs_dentry_to_name(&name, dentry);
357
358 error = xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
359 if (error)
360 return error;
361
362
363
364
365
366
367 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
368 d_invalidate(dentry);
369 return 0;
370}
371
372STATIC int
373xfs_vn_symlink(
374 struct inode *dir,
375 struct dentry *dentry,
376 const char *symname)
377{
378 struct inode *inode;
379 struct xfs_inode *cip = NULL;
380 struct xfs_name name;
381 int error;
382 umode_t mode;
383
384 mode = S_IFLNK |
385 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
386 error = xfs_dentry_mode_to_name(&name, dentry, mode);
387 if (unlikely(error))
388 goto out;
389
390 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
391 if (unlikely(error))
392 goto out;
393
394 inode = VFS_I(cip);
395
396 error = xfs_init_security(inode, dir, &dentry->d_name);
397 if (unlikely(error))
398 goto out_cleanup_inode;
399
400 xfs_setup_iops(cip);
401
402 d_instantiate(dentry, inode);
403 xfs_finish_inode_setup(cip);
404 return 0;
405
406 out_cleanup_inode:
407 xfs_finish_inode_setup(cip);
408 xfs_cleanup_inode(dir, inode, dentry);
409 iput(inode);
410 out:
411 return error;
412}
413
414STATIC int
415xfs_vn_rename(
416 struct inode *odir,
417 struct dentry *odentry,
418 struct inode *ndir,
419 struct dentry *ndentry,
420 unsigned int flags)
421{
422 struct inode *new_inode = ndentry->d_inode;
423 int omode = 0;
424 int error;
425 struct xfs_name oname;
426 struct xfs_name nname;
427
428 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
429 return -EINVAL;
430
431
432 if (flags & RENAME_EXCHANGE)
433 omode = ndentry->d_inode->i_mode;
434
435 error = xfs_dentry_mode_to_name(&oname, odentry, omode);
436 if (omode && unlikely(error))
437 return error;
438
439 error = xfs_dentry_mode_to_name(&nname, ndentry,
440 d_inode(odentry)->i_mode);
441 if (unlikely(error))
442 return error;
443
444 return xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
445 XFS_I(ndir), &nname,
446 new_inode ? XFS_I(new_inode) : NULL, flags);
447}
448
449STATIC int
450xfs_vn_rename_old(
451 struct inode *odir,
452 struct dentry *odentry,
453 struct inode *ndir,
454 struct dentry *ndentry)
455{
456 return xfs_vn_rename(odir, odentry, ndir, ndentry, 0);
457}
458
459
460
461
462
463
464
465STATIC void *
466xfs_vn_follow_link(
467 struct dentry *dentry,
468 struct nameidata *nd)
469{
470 char *link;
471 int error = -ENOMEM;
472
473 link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
474 if (!link)
475 goto out_err;
476
477 error = xfs_readlink(XFS_I(dentry->d_inode), link);
478 if (unlikely(error))
479 goto out_kfree;
480
481 nd_set_link(nd, link);
482 return NULL;
483
484 out_kfree:
485 kfree(link);
486 out_err:
487 nd_set_link(nd, ERR_PTR(error));
488 return NULL;
489}
490
491STATIC void
492xfs_vn_put_link(
493 struct dentry *dentry,
494 struct nameidata *nd,
495 void *p)
496{
497 char *s = nd_get_link(nd);
498
499 if (!IS_ERR(s))
500 kfree(s);
501}
502
503STATIC int
504xfs_vn_getattr(
505 struct vfsmount *mnt,
506 struct dentry *dentry,
507 struct kstat *stat)
508{
509 struct inode *inode = dentry->d_inode;
510 struct xfs_inode *ip = XFS_I(inode);
511 struct xfs_mount *mp = ip->i_mount;
512
513 trace_xfs_getattr(ip);
514
515 if (XFS_FORCED_SHUTDOWN(mp))
516 return -EIO;
517
518 stat->size = XFS_ISIZE(ip);
519 stat->dev = inode->i_sb->s_dev;
520 stat->mode = inode->i_mode;
521 stat->nlink = inode->i_nlink;
522 stat->uid = inode->i_uid;
523 stat->gid = inode->i_gid;
524 stat->ino = ip->i_ino;
525 stat->atime = inode->i_atime;
526 stat->mtime = inode->i_mtime;
527 stat->ctime = inode->i_ctime;
528 stat->blocks =
529 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
530
531
532 switch (inode->i_mode & S_IFMT) {
533 case S_IFBLK:
534 case S_IFCHR:
535 stat->blksize = BLKDEV_IOSIZE;
536 stat->rdev = inode->i_rdev;
537 break;
538 default:
539 if (XFS_IS_REALTIME_INODE(ip)) {
540
541
542
543
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;
551 }
552
553 return 0;
554}
555
556static void
557xfs_setattr_mode(
558 struct xfs_inode *ip,
559 struct iattr *iattr)
560{
561 struct inode *inode = VFS_I(ip);
562 umode_t mode = iattr->ia_mode;
563
564 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
565
566 inode->i_mode &= S_IFMT;
567 inode->i_mode |= mode & ~S_IFMT;
568}
569
570void
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
579 if (iattr->ia_valid & ATTR_ATIME)
580 inode->i_atime = iattr->ia_atime;
581 if (iattr->ia_valid & ATTR_CTIME)
582 inode->i_ctime = iattr->ia_ctime;
583 if (iattr->ia_valid & ATTR_MTIME)
584 inode->i_mtime = iattr->ia_mtime;
585}
586
587static int
588xfs_vn_change_ok(
589 struct dentry *dentry,
590 struct iattr *iattr)
591{
592 struct inode *inode = d_inode(dentry);
593 struct xfs_inode *ip = XFS_I(inode);
594 struct xfs_mount *mp = ip->i_mount;
595
596 if (mp->m_flags & XFS_MOUNT_RDONLY)
597 return -EROFS;
598
599 if (XFS_FORCED_SHUTDOWN(mp))
600 return -EIO;
601
602 return inode_change_ok(inode, iattr);
603}
604
605
606
607
608
609
610
611int
612xfs_setattr_nonsize(
613 struct xfs_inode *ip,
614 struct iattr *iattr,
615 int flags)
616{
617 xfs_mount_t *mp = ip->i_mount;
618 struct inode *inode = VFS_I(ip);
619 int mask = iattr->ia_valid;
620 xfs_trans_t *tp;
621 int error;
622 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
623 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
624 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
625 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
626
627 ASSERT((mask & ATTR_SIZE) == 0);
628
629
630
631
632
633
634
635
636
637 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
638 uint qflags = 0;
639
640 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
641 uid = iattr->ia_uid;
642 qflags |= XFS_QMOPT_UQUOTA;
643 } else {
644 uid = inode->i_uid;
645 }
646 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
647 gid = iattr->ia_gid;
648 qflags |= XFS_QMOPT_GQUOTA;
649 } else {
650 gid = inode->i_gid;
651 }
652
653
654
655
656
657
658 ASSERT(udqp == NULL);
659 ASSERT(gdqp == NULL);
660 error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
661 xfs_kgid_to_gid(gid),
662 xfs_get_projid(ip),
663 qflags, &udqp, &gdqp, NULL);
664 if (error)
665 return error;
666 }
667
668 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
669 if (error)
670 goto out_dqrele;
671
672 xfs_ilock(ip, XFS_ILOCK_EXCL);
673 xfs_trans_ijoin(tp, ip, 0);
674
675
676
677
678 if (mask & (ATTR_UID|ATTR_GID)) {
679
680
681
682
683
684
685 iuid = inode->i_uid;
686 igid = inode->i_gid;
687 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
688 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
689
690
691
692
693
694 if (XFS_IS_QUOTA_RUNNING(mp) &&
695 ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
696 (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
697 ASSERT(tp);
698 error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
699 NULL, capable(CAP_FOWNER) ?
700 XFS_QMOPT_FORCE_RES : 0);
701 if (error)
702 goto out_cancel;
703 }
704 }
705
706
707
708
709 if (mask & (ATTR_UID|ATTR_GID)) {
710
711
712
713
714
715
716 if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
717 !capable(CAP_FSETID))
718 inode->i_mode &= ~(S_ISUID|S_ISGID);
719
720
721
722
723
724 if (!uid_eq(iuid, uid)) {
725 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
726 ASSERT(mask & ATTR_UID);
727 ASSERT(udqp);
728 olddquot1 = xfs_qm_vop_chown(tp, ip,
729 &ip->i_udquot, udqp);
730 }
731 ip->i_d.di_uid = xfs_kuid_to_uid(uid);
732 inode->i_uid = uid;
733 }
734 if (!gid_eq(igid, gid)) {
735 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
736 ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
737 !XFS_IS_PQUOTA_ON(mp));
738 ASSERT(mask & ATTR_GID);
739 ASSERT(gdqp);
740 olddquot2 = xfs_qm_vop_chown(tp, ip,
741 &ip->i_gdquot, gdqp);
742 }
743 ip->i_d.di_gid = xfs_kgid_to_gid(gid);
744 inode->i_gid = gid;
745 }
746 }
747
748 if (mask & ATTR_MODE)
749 xfs_setattr_mode(ip, iattr);
750 if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
751 xfs_setattr_time(ip, iattr);
752
753 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
754
755 XFS_STATS_INC(mp, xs_ig_attrchg);
756
757 if (mp->m_flags & XFS_MOUNT_WSYNC)
758 xfs_trans_set_sync(tp);
759 error = xfs_trans_commit(tp);
760
761 xfs_iunlock(ip, XFS_ILOCK_EXCL);
762
763
764
765
766 xfs_qm_dqrele(olddquot1);
767 xfs_qm_dqrele(olddquot2);
768 xfs_qm_dqrele(udqp);
769 xfs_qm_dqrele(gdqp);
770
771 if (error)
772 return error;
773
774
775
776
777
778
779
780
781 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
782 error = xfs_acl_chmod(inode);
783 if (error)
784 return error;
785 }
786
787 return 0;
788
789out_cancel:
790 xfs_trans_cancel(tp);
791 xfs_iunlock(ip, XFS_ILOCK_EXCL);
792out_dqrele:
793 xfs_qm_dqrele(udqp);
794 xfs_qm_dqrele(gdqp);
795 return error;
796}
797
798int
799xfs_vn_setattr_nonsize(
800 struct dentry *dentry,
801 struct iattr *iattr)
802{
803 struct xfs_inode *ip = XFS_I(d_inode(dentry));
804 int error;
805
806 trace_xfs_setattr(ip);
807
808 error = xfs_vn_change_ok(dentry, iattr);
809 if (error)
810 return error;
811 return xfs_setattr_nonsize(ip, iattr, 0);
812}
813
814
815
816
817
818
819
820STATIC int
821xfs_setattr_size(
822 struct xfs_inode *ip,
823 struct iattr *iattr)
824{
825 struct xfs_mount *mp = ip->i_mount;
826 struct inode *inode = VFS_I(ip);
827 xfs_off_t oldsize, newsize;
828 struct xfs_trans *tp;
829 int error;
830 uint lock_flags = 0;
831 bool did_zeroing = false;
832
833 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
834 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
835 ASSERT(S_ISREG(inode->i_mode));
836 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
837 ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
838
839 oldsize = inode->i_size;
840 newsize = iattr->ia_size;
841
842
843
844
845 if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
846 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
847 return 0;
848
849
850
851
852 iattr->ia_valid &= ~ATTR_SIZE;
853 return xfs_setattr_nonsize(ip, iattr, 0);
854 }
855
856
857
858
859 error = xfs_qm_dqattach(ip, 0);
860 if (error)
861 return error;
862
863
864
865
866 inode_dio_wait(inode);
867
868
869
870
871
872
873
874
875
876
877
878 if (newsize > oldsize) {
879 error = xfs_zero_eof(ip, newsize, oldsize, &did_zeroing);
880 } else {
881 error = iomap_truncate_page(inode, newsize, &did_zeroing,
882 &xfs_iomap_ops);
883 }
884
885 if (error)
886 return error;
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909 truncate_setsize(inode, newsize);
910
911
912
913
914
915
916
917
918
919 if (did_zeroing ||
920 (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
921 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
922 ip->i_d.di_size, newsize - 1);
923 if (error)
924 return error;
925 }
926
927 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
928 if (error)
929 return error;
930
931 lock_flags |= XFS_ILOCK_EXCL;
932 xfs_ilock(ip, XFS_ILOCK_EXCL);
933 xfs_trans_ijoin(tp, ip, 0);
934
935
936
937
938
939
940
941
942
943
944
945 if (newsize != oldsize &&
946 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
947 iattr->ia_ctime = iattr->ia_mtime =
948 current_fs_time(inode->i_sb);
949 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
950 }
951
952
953
954
955
956
957
958
959
960
961
962
963
964 ip->i_d.di_size = newsize;
965 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
966
967 if (newsize <= oldsize) {
968 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
969 if (error)
970 goto out_trans_cancel;
971
972
973
974
975
976
977
978
979 xfs_iflags_set(ip, XFS_ITRUNCATED);
980
981
982 xfs_inode_clear_eofblocks_tag(ip);
983 }
984
985 if (iattr->ia_valid & ATTR_MODE)
986 xfs_setattr_mode(ip, iattr);
987 if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
988 xfs_setattr_time(ip, iattr);
989
990 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
991
992 XFS_STATS_INC(mp, xs_ig_attrchg);
993
994 if (mp->m_flags & XFS_MOUNT_WSYNC)
995 xfs_trans_set_sync(tp);
996
997 error = xfs_trans_commit(tp);
998out_unlock:
999 if (lock_flags)
1000 xfs_iunlock(ip, lock_flags);
1001 return error;
1002
1003out_trans_cancel:
1004 xfs_trans_cancel(tp);
1005 goto out_unlock;
1006}
1007
1008int
1009xfs_vn_setattr_size(
1010 struct dentry *dentry,
1011 struct iattr *iattr)
1012{
1013 struct xfs_inode *ip = XFS_I(d_inode(dentry));
1014 int error;
1015
1016 trace_xfs_setattr(ip);
1017
1018 error = xfs_vn_change_ok(dentry, iattr);
1019 if (error)
1020 return error;
1021 return xfs_setattr_size(ip, iattr);
1022}
1023
1024STATIC int
1025xfs_vn_setattr(
1026 struct dentry *dentry,
1027 struct iattr *iattr)
1028{
1029 int error;
1030
1031 if (iattr->ia_valid & ATTR_SIZE) {
1032 struct inode *inode = d_inode(dentry);
1033 struct xfs_inode *ip = XFS_I(inode);
1034 uint iolock;
1035
1036 iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
1037
1038 xfs_ilock(ip, iolock);
1039 error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP, true);
1040 if (!error)
1041 error = xfs_vn_setattr_size(dentry, iattr);
1042 xfs_iunlock(ip, iolock);
1043 } else {
1044 error = xfs_vn_setattr_nonsize(dentry, iattr);
1045 }
1046
1047 return error;
1048}
1049
1050STATIC int
1051xfs_vn_update_time(
1052 struct inode *inode,
1053 struct timespec *now,
1054 int flags)
1055{
1056 struct xfs_inode *ip = XFS_I(inode);
1057 struct xfs_mount *mp = ip->i_mount;
1058 struct xfs_trans *tp;
1059 int error;
1060
1061 trace_xfs_update_time(ip);
1062
1063 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
1064 if (error)
1065 return error;
1066
1067 xfs_ilock(ip, XFS_ILOCK_EXCL);
1068 if (flags & S_CTIME)
1069 inode->i_ctime = *now;
1070 if (flags & S_MTIME)
1071 inode->i_mtime = *now;
1072 if (flags & S_ATIME)
1073 inode->i_atime = *now;
1074
1075 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1076 xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
1077 return xfs_trans_commit(tp);
1078}
1079
1080STATIC int
1081xfs_vn_fiemap(
1082 struct inode *inode,
1083 struct fiemap_extent_info *fieinfo,
1084 u64 start,
1085 u64 length)
1086{
1087 int error;
1088
1089 xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
1090 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1091 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
1092 error = iomap_fiemap(inode, fieinfo, start, length,
1093 &xfs_xattr_iomap_ops);
1094 } else {
1095 error = iomap_fiemap(inode, fieinfo, start, length,
1096 &xfs_iomap_ops);
1097 }
1098 xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
1099
1100 return error;
1101}
1102
1103STATIC int
1104xfs_vn_tmpfile(
1105 struct inode *dir,
1106 struct dentry *dentry,
1107 umode_t mode)
1108{
1109 return xfs_generic_create(dir, dentry, mode, 0, true);
1110}
1111
1112static const struct inode_operations xfs_inode_operations = {
1113 .get_acl = xfs_get_acl,
1114 .getattr = xfs_vn_getattr,
1115 .setattr = xfs_vn_setattr,
1116 .setxattr = generic_setxattr,
1117 .getxattr = generic_getxattr,
1118 .removexattr = generic_removexattr,
1119 .listxattr = xfs_vn_listxattr,
1120 .fiemap = xfs_vn_fiemap,
1121 .update_time = xfs_vn_update_time,
1122};
1123
1124static const struct inode_operations_wrapper xfs_dir_inode_operations = {
1125 .ops = {
1126 .create = xfs_vn_create,
1127 .lookup = xfs_vn_lookup,
1128 .link = xfs_vn_link,
1129 .unlink = xfs_vn_unlink,
1130 .symlink = xfs_vn_symlink,
1131 .mkdir = xfs_vn_mkdir,
1132
1133
1134
1135
1136
1137
1138 .rmdir = xfs_vn_unlink,
1139 .mknod = xfs_vn_mknod,
1140 .rename = xfs_vn_rename_old,
1141 .get_acl = xfs_get_acl,
1142 .getattr = xfs_vn_getattr,
1143 .setattr = xfs_vn_setattr,
1144 .setxattr = generic_setxattr,
1145 .getxattr = generic_getxattr,
1146 .removexattr = generic_removexattr,
1147 .listxattr = xfs_vn_listxattr,
1148 .update_time = xfs_vn_update_time,
1149 },
1150 .rename2 = xfs_vn_rename,
1151 .tmpfile = xfs_vn_tmpfile,
1152};
1153
1154static const struct inode_operations_wrapper xfs_dir_ci_inode_operations = {
1155 .ops = {
1156 .create = xfs_vn_create,
1157 .lookup = xfs_vn_ci_lookup,
1158 .link = xfs_vn_link,
1159 .unlink = xfs_vn_unlink,
1160 .symlink = xfs_vn_symlink,
1161 .mkdir = xfs_vn_mkdir,
1162
1163
1164
1165
1166
1167
1168 .rmdir = xfs_vn_unlink,
1169 .mknod = xfs_vn_mknod,
1170 .rename = xfs_vn_rename_old,
1171 .get_acl = xfs_get_acl,
1172 .getattr = xfs_vn_getattr,
1173 .setattr = xfs_vn_setattr,
1174 .setxattr = generic_setxattr,
1175 .getxattr = generic_getxattr,
1176 .removexattr = generic_removexattr,
1177 .listxattr = xfs_vn_listxattr,
1178 .update_time = xfs_vn_update_time,
1179 },
1180 .rename2 = xfs_vn_rename,
1181 .tmpfile = xfs_vn_tmpfile,
1182};
1183
1184static const struct inode_operations xfs_symlink_inode_operations = {
1185 .readlink = generic_readlink,
1186 .follow_link = xfs_vn_follow_link,
1187 .put_link = xfs_vn_put_link,
1188 .get_acl = xfs_get_acl,
1189 .getattr = xfs_vn_getattr,
1190 .setattr = xfs_vn_setattr,
1191 .setxattr = generic_setxattr,
1192 .getxattr = generic_getxattr,
1193 .removexattr = generic_removexattr,
1194 .listxattr = xfs_vn_listxattr,
1195 .update_time = xfs_vn_update_time,
1196};
1197
1198
1199static bool
1200xfs_inode_supports_dax(
1201 struct xfs_inode *ip)
1202{
1203 struct xfs_mount *mp = ip->i_mount;
1204
1205
1206 if (!S_ISREG(VFS_I(ip)->i_mode))
1207 return false;
1208
1209
1210 if (!(mp->m_flags & XFS_MOUNT_DAX))
1211 return false;
1212
1213
1214 if (mp->m_sb.sb_blocksize != PAGE_SIZE)
1215 return false;
1216
1217
1218 return xfs_find_daxdev_for_inode(VFS_I(ip)) != NULL;
1219}
1220
1221STATIC void
1222xfs_diflags_to_iflags(
1223 struct inode *inode,
1224 struct xfs_inode *ip)
1225{
1226 uint16_t flags = ip->i_d.di_flags;
1227
1228 inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC |
1229 S_NOATIME | S_DAX);
1230
1231 if (flags & XFS_DIFLAG_IMMUTABLE)
1232 inode->i_flags |= S_IMMUTABLE;
1233 if (flags & XFS_DIFLAG_APPEND)
1234 inode->i_flags |= S_APPEND;
1235 if (flags & XFS_DIFLAG_SYNC)
1236 inode->i_flags |= S_SYNC;
1237 if (flags & XFS_DIFLAG_NOATIME)
1238 inode->i_flags |= S_NOATIME;
1239 if (xfs_inode_supports_dax(ip))
1240 inode->i_flags |= S_DAX;
1241}
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251void
1252xfs_setup_inode(
1253 struct xfs_inode *ip)
1254{
1255 struct inode *inode = &ip->i_vnode;
1256 gfp_t gfp_mask;
1257
1258 inode->i_ino = ip->i_ino;
1259 inode->i_state = I_NEW;
1260
1261 inode_sb_list_add(inode);
1262
1263 hlist_add_fake(&inode->i_hash);
1264
1265 inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
1266 inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
1267
1268 i_size_write(inode, ip->i_d.di_size);
1269 xfs_diflags_to_iflags(inode, ip);
1270
1271 if (S_ISDIR(inode->i_mode)) {
1272 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
1273 ip->d_ops = ip->i_mount->m_dir_inode_ops;
1274 } else {
1275 ip->d_ops = ip->i_mount->m_nondir_inode_ops;
1276 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
1277 }
1278
1279
1280
1281
1282
1283
1284 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1285 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1286
1287
1288
1289
1290
1291 if (!XFS_IFORK_Q(ip)) {
1292 inode_has_no_xattr(inode);
1293 cache_no_acl(inode);
1294 }
1295}
1296
1297void
1298xfs_setup_iops(
1299 struct xfs_inode *ip)
1300{
1301 struct inode *inode = &ip->i_vnode;
1302
1303 switch (inode->i_mode & S_IFMT) {
1304 case S_IFREG:
1305 inode->i_op = &xfs_inode_operations;
1306 inode->i_fop = &xfs_file_operations.kabi_fops;
1307 if (IS_DAX(inode))
1308 inode->i_mapping->a_ops = &xfs_dax_aops;
1309 else
1310 inode->i_mapping->a_ops = &xfs_address_space_operations;
1311 break;
1312 case S_IFDIR:
1313 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb)) {
1314 inode->i_op = &xfs_dir_ci_inode_operations.ops;
1315 inode->i_flags |= S_IOPS_WRAPPER;
1316 } else {
1317 inode->i_op = &xfs_dir_inode_operations.ops;
1318 inode->i_flags |= S_IOPS_WRAPPER;
1319 }
1320 inode->i_fop = &xfs_dir_file_operations;
1321 break;
1322 case S_IFLNK:
1323 inode->i_op = &xfs_symlink_inode_operations;
1324 if (!(ip->i_df.if_flags & XFS_IFINLINE))
1325 inode->i_mapping->a_ops = &xfs_address_space_operations;
1326 break;
1327 default:
1328 inode->i_op = &xfs_inode_operations;
1329 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1330 break;
1331 }
1332}
1333