xfs_iops.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_acl.h"
  15. #include "xfs_quota.h"
  16. #include "xfs_da_format.h"
  17. #include "xfs_da_btree.h"
  18. #include "xfs_attr.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_trans_space.h"
  21. #include "xfs_bmap_btree.h"
  22. #include "xfs_trace.h"
  23. #include "xfs_icache.h"
  24. #include "xfs_symlink.h"
  25. #include "xfs_dir2.h"
  26. #include "xfs_iomap.h"
  27. #include "xfs_error.h"
  28. #include "xfs_ioctl.h"
  29. #include "xfs_xattr.h"
  30. #include "xfs_file.h"
  31. #include "xfs_bmap.h"
  32. #include <linux/posix_acl.h>
  33. #include <linux/security.h>
  34. #include <linux/iversion.h>
  35. #include <linux/fiemap.h>
  36. /*
  37. * Directories have different lock order w.r.t. mmap_lock compared to regular
  38. * files. This is due to readdir potentially triggering page faults on a user
  39. * buffer inside filldir(), and this happens with the ilock on the directory
  40. * held. For regular files, the lock order is the other way around - the
  41. * mmap_lock is taken during the page fault, and then we lock the ilock to do
  42. * block mapping. Hence we need a different class for the directory ilock so
  43. * that lockdep can tell them apart.
  44. */
  45. static struct lock_class_key xfs_nondir_ilock_class;
  46. static struct lock_class_key xfs_dir_ilock_class;
  47. static int
  48. xfs_initxattrs(
  49. struct inode *inode,
  50. const struct xattr *xattr_array,
  51. void *fs_info)
  52. {
  53. const struct xattr *xattr;
  54. struct xfs_inode *ip = XFS_I(inode);
  55. int error = 0;
  56. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  57. struct xfs_da_args args = {
  58. .dp = ip,
  59. .attr_filter = XFS_ATTR_SECURE,
  60. .name = xattr->name,
  61. .namelen = strlen(xattr->name),
  62. .value = xattr->value,
  63. .valuelen = xattr->value_len,
  64. };
  65. error = xfs_attr_change(&args, XFS_ATTRUPDATE_UPSERT);
  66. if (error < 0)
  67. break;
  68. }
  69. return error;
  70. }
  71. /*
  72. * Hook in SELinux. This is not quite correct yet, what we really need
  73. * here (as we do for default ACLs) is a mechanism by which creation of
  74. * these attrs can be journalled at inode creation time (along with the
  75. * inode, of course, such that log replay can't cause these to be lost).
  76. */
  77. int
  78. xfs_inode_init_security(
  79. struct inode *inode,
  80. struct inode *dir,
  81. const struct qstr *qstr)
  82. {
  83. return security_inode_init_security(inode, dir, qstr,
  84. &xfs_initxattrs, NULL);
  85. }
  86. static void
  87. xfs_dentry_to_name(
  88. struct xfs_name *namep,
  89. struct dentry *dentry)
  90. {
  91. namep->name = dentry->d_name.name;
  92. namep->len = dentry->d_name.len;
  93. namep->type = XFS_DIR3_FT_UNKNOWN;
  94. }
  95. static int
  96. xfs_dentry_mode_to_name(
  97. struct xfs_name *namep,
  98. struct dentry *dentry,
  99. int mode)
  100. {
  101. namep->name = dentry->d_name.name;
  102. namep->len = dentry->d_name.len;
  103. namep->type = xfs_mode_to_ftype(mode);
  104. if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
  105. return -EFSCORRUPTED;
  106. return 0;
  107. }
  108. STATIC void
  109. xfs_cleanup_inode(
  110. struct inode *dir,
  111. struct inode *inode,
  112. struct dentry *dentry)
  113. {
  114. struct xfs_name teardown;
  115. /* Oh, the horror.
  116. * If we can't add the ACL or we fail in
  117. * xfs_inode_init_security we must back out.
  118. * ENOSPC can hit here, among other things.
  119. */
  120. xfs_dentry_to_name(&teardown, dentry);
  121. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  122. }
  123. /*
  124. * Check to see if we are likely to need an extended attribute to be added to
  125. * the inode we are about to allocate. This allows the attribute fork to be
  126. * created during the inode allocation, reducing the number of transactions we
  127. * need to do in this fast path.
  128. *
  129. * The security checks are optimistic, but not guaranteed. The two LSMs that
  130. * require xattrs to be added here (selinux and smack) are also the only two
  131. * LSMs that add a sb->s_security structure to the superblock. Hence if security
  132. * is enabled and sb->s_security is set, we have a pretty good idea that we are
  133. * going to be asked to add a security xattr immediately after allocating the
  134. * xfs inode and instantiating the VFS inode.
  135. */
  136. static inline bool
  137. xfs_create_need_xattr(
  138. struct inode *dir,
  139. struct posix_acl *default_acl,
  140. struct posix_acl *acl)
  141. {
  142. if (acl)
  143. return true;
  144. if (default_acl)
  145. return true;
  146. #if IS_ENABLED(CONFIG_SECURITY)
  147. if (dir->i_sb->s_security)
  148. return true;
  149. #endif
  150. return false;
  151. }
  152. STATIC int
  153. xfs_generic_create(
  154. struct mnt_idmap *idmap,
  155. struct inode *dir,
  156. struct dentry *dentry,
  157. umode_t mode,
  158. dev_t rdev,
  159. struct file *tmpfile) /* unnamed file */
  160. {
  161. struct xfs_icreate_args args = {
  162. .idmap = idmap,
  163. .pip = XFS_I(dir),
  164. .rdev = rdev,
  165. .mode = mode,
  166. };
  167. struct inode *inode;
  168. struct xfs_inode *ip = NULL;
  169. struct posix_acl *default_acl, *acl;
  170. struct xfs_name name;
  171. int error;
  172. /*
  173. * Irix uses Missed'em'V split, but doesn't want to see
  174. * the upper 5 bits of (14bit) major.
  175. */
  176. if (S_ISCHR(args.mode) || S_ISBLK(args.mode)) {
  177. if (unlikely(!sysv_valid_dev(args.rdev) ||
  178. MAJOR(args.rdev) & ~0x1ff))
  179. return -EINVAL;
  180. } else {
  181. args.rdev = 0;
  182. }
  183. error = posix_acl_create(dir, &args.mode, &default_acl, &acl);
  184. if (error)
  185. return error;
  186. /* Verify mode is valid also for tmpfile case */
  187. error = xfs_dentry_mode_to_name(&name, dentry, args.mode);
  188. if (unlikely(error))
  189. goto out_free_acl;
  190. if (!tmpfile) {
  191. if (xfs_create_need_xattr(dir, default_acl, acl))
  192. args.flags |= XFS_ICREATE_INIT_XATTRS;
  193. error = xfs_create(&args, &name, &ip);
  194. } else {
  195. args.flags |= XFS_ICREATE_TMPFILE;
  196. /*
  197. * If this temporary file will not be linkable, don't bother
  198. * creating an attr fork to receive a parent pointer.
  199. */
  200. if (tmpfile->f_flags & O_EXCL)
  201. args.flags |= XFS_ICREATE_UNLINKABLE;
  202. error = xfs_create_tmpfile(&args, &ip);
  203. }
  204. if (unlikely(error))
  205. goto out_free_acl;
  206. inode = VFS_I(ip);
  207. error = xfs_inode_init_security(inode, dir, &dentry->d_name);
  208. if (unlikely(error))
  209. goto out_cleanup_inode;
  210. if (default_acl) {
  211. error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  212. if (error)
  213. goto out_cleanup_inode;
  214. }
  215. if (acl) {
  216. error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  217. if (error)
  218. goto out_cleanup_inode;
  219. }
  220. xfs_setup_iops(ip);
  221. if (tmpfile) {
  222. /*
  223. * The VFS requires that any inode fed to d_tmpfile must have
  224. * nlink == 1 so that it can decrement the nlink in d_tmpfile.
  225. * However, we created the temp file with nlink == 0 because
  226. * we're not allowed to put an inode with nlink > 0 on the
  227. * unlinked list. Therefore we have to set nlink to 1 so that
  228. * d_tmpfile can immediately set it back to zero.
  229. */
  230. set_nlink(inode, 1);
  231. d_tmpfile(tmpfile, inode);
  232. } else
  233. d_instantiate(dentry, inode);
  234. xfs_finish_inode_setup(ip);
  235. out_free_acl:
  236. posix_acl_release(default_acl);
  237. posix_acl_release(acl);
  238. return error;
  239. out_cleanup_inode:
  240. xfs_finish_inode_setup(ip);
  241. if (!tmpfile)
  242. xfs_cleanup_inode(dir, inode, dentry);
  243. xfs_irele(ip);
  244. goto out_free_acl;
  245. }
  246. STATIC int
  247. xfs_vn_mknod(
  248. struct mnt_idmap *idmap,
  249. struct inode *dir,
  250. struct dentry *dentry,
  251. umode_t mode,
  252. dev_t rdev)
  253. {
  254. return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL);
  255. }
  256. STATIC int
  257. xfs_vn_create(
  258. struct mnt_idmap *idmap,
  259. struct inode *dir,
  260. struct dentry *dentry,
  261. umode_t mode,
  262. bool flags)
  263. {
  264. return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL);
  265. }
  266. STATIC int
  267. xfs_vn_mkdir(
  268. struct mnt_idmap *idmap,
  269. struct inode *dir,
  270. struct dentry *dentry,
  271. umode_t mode)
  272. {
  273. return xfs_generic_create(idmap, dir, dentry, mode | S_IFDIR, 0, NULL);
  274. }
  275. STATIC struct dentry *
  276. xfs_vn_lookup(
  277. struct inode *dir,
  278. struct dentry *dentry,
  279. unsigned int flags)
  280. {
  281. struct inode *inode;
  282. struct xfs_inode *cip;
  283. struct xfs_name name;
  284. int error;
  285. if (dentry->d_name.len >= MAXNAMELEN)
  286. return ERR_PTR(-ENAMETOOLONG);
  287. xfs_dentry_to_name(&name, dentry);
  288. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  289. if (likely(!error))
  290. inode = VFS_I(cip);
  291. else if (likely(error == -ENOENT))
  292. inode = NULL;
  293. else
  294. inode = ERR_PTR(error);
  295. return d_splice_alias(inode, dentry);
  296. }
  297. STATIC struct dentry *
  298. xfs_vn_ci_lookup(
  299. struct inode *dir,
  300. struct dentry *dentry,
  301. unsigned int flags)
  302. {
  303. struct xfs_inode *ip;
  304. struct xfs_name xname;
  305. struct xfs_name ci_name;
  306. struct qstr dname;
  307. int error;
  308. if (dentry->d_name.len >= MAXNAMELEN)
  309. return ERR_PTR(-ENAMETOOLONG);
  310. xfs_dentry_to_name(&xname, dentry);
  311. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  312. if (unlikely(error)) {
  313. if (unlikely(error != -ENOENT))
  314. return ERR_PTR(error);
  315. /*
  316. * call d_add(dentry, NULL) here when d_drop_negative_children
  317. * is called in xfs_vn_mknod (ie. allow negative dentries
  318. * with CI filesystems).
  319. */
  320. return NULL;
  321. }
  322. /* if exact match, just splice and exit */
  323. if (!ci_name.name)
  324. return d_splice_alias(VFS_I(ip), dentry);
  325. /* else case-insensitive match... */
  326. dname.name = ci_name.name;
  327. dname.len = ci_name.len;
  328. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  329. kfree(ci_name.name);
  330. return dentry;
  331. }
  332. STATIC int
  333. xfs_vn_link(
  334. struct dentry *old_dentry,
  335. struct inode *dir,
  336. struct dentry *dentry)
  337. {
  338. struct inode *inode = d_inode(old_dentry);
  339. struct xfs_name name;
  340. int error;
  341. error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
  342. if (unlikely(error))
  343. return error;
  344. if (IS_PRIVATE(inode))
  345. return -EPERM;
  346. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  347. if (unlikely(error))
  348. return error;
  349. ihold(inode);
  350. d_instantiate(dentry, inode);
  351. return 0;
  352. }
  353. STATIC int
  354. xfs_vn_unlink(
  355. struct inode *dir,
  356. struct dentry *dentry)
  357. {
  358. struct xfs_name name;
  359. int error;
  360. xfs_dentry_to_name(&name, dentry);
  361. error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
  362. if (error)
  363. return error;
  364. /*
  365. * With unlink, the VFS makes the dentry "negative": no inode,
  366. * but still hashed. This is incompatible with case-insensitive
  367. * mode, so invalidate (unhash) the dentry in CI-mode.
  368. */
  369. if (xfs_has_asciici(XFS_M(dir->i_sb)))
  370. d_invalidate(dentry);
  371. return 0;
  372. }
  373. STATIC int
  374. xfs_vn_symlink(
  375. struct mnt_idmap *idmap,
  376. struct inode *dir,
  377. struct dentry *dentry,
  378. const char *symname)
  379. {
  380. struct inode *inode;
  381. struct xfs_inode *cip = NULL;
  382. struct xfs_name name;
  383. int error;
  384. umode_t mode;
  385. mode = S_IFLNK |
  386. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  387. error = xfs_dentry_mode_to_name(&name, dentry, mode);
  388. if (unlikely(error))
  389. goto out;
  390. error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip);
  391. if (unlikely(error))
  392. goto out;
  393. inode = VFS_I(cip);
  394. error = xfs_inode_init_security(inode, dir, &dentry->d_name);
  395. if (unlikely(error))
  396. goto out_cleanup_inode;
  397. xfs_setup_iops(cip);
  398. d_instantiate(dentry, inode);
  399. xfs_finish_inode_setup(cip);
  400. return 0;
  401. out_cleanup_inode:
  402. xfs_finish_inode_setup(cip);
  403. xfs_cleanup_inode(dir, inode, dentry);
  404. xfs_irele(cip);
  405. out:
  406. return error;
  407. }
  408. STATIC int
  409. xfs_vn_rename(
  410. struct mnt_idmap *idmap,
  411. struct inode *odir,
  412. struct dentry *odentry,
  413. struct inode *ndir,
  414. struct dentry *ndentry,
  415. unsigned int flags)
  416. {
  417. struct inode *new_inode = d_inode(ndentry);
  418. int omode = 0;
  419. int error;
  420. struct xfs_name oname;
  421. struct xfs_name nname;
  422. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  423. return -EINVAL;
  424. /* if we are exchanging files, we need to set i_mode of both files */
  425. if (flags & RENAME_EXCHANGE)
  426. omode = d_inode(ndentry)->i_mode;
  427. error = xfs_dentry_mode_to_name(&oname, odentry, omode);
  428. if (omode && unlikely(error))
  429. return error;
  430. error = xfs_dentry_mode_to_name(&nname, ndentry,
  431. d_inode(odentry)->i_mode);
  432. if (unlikely(error))
  433. return error;
  434. return xfs_rename(idmap, XFS_I(odir), &oname,
  435. XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
  436. new_inode ? XFS_I(new_inode) : NULL, flags);
  437. }
  438. /*
  439. * careful here - this function can get called recursively, so
  440. * we need to be very careful about how much stack we use.
  441. * uio is kmalloced for this reason...
  442. */
  443. STATIC const char *
  444. xfs_vn_get_link(
  445. struct dentry *dentry,
  446. struct inode *inode,
  447. struct delayed_call *done)
  448. {
  449. char *link;
  450. int error = -ENOMEM;
  451. if (!dentry)
  452. return ERR_PTR(-ECHILD);
  453. link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
  454. if (!link)
  455. goto out_err;
  456. error = xfs_readlink(XFS_I(d_inode(dentry)), link);
  457. if (unlikely(error))
  458. goto out_kfree;
  459. set_delayed_call(done, kfree_link, link);
  460. return link;
  461. out_kfree:
  462. kfree(link);
  463. out_err:
  464. return ERR_PTR(error);
  465. }
  466. static uint32_t
  467. xfs_stat_blksize(
  468. struct xfs_inode *ip)
  469. {
  470. struct xfs_mount *mp = ip->i_mount;
  471. /*
  472. * If the file blocks are being allocated from a realtime volume, then
  473. * always return the realtime extent size.
  474. */
  475. if (XFS_IS_REALTIME_INODE(ip))
  476. return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip) ? : 1);
  477. /*
  478. * Allow large block sizes to be reported to userspace programs if the
  479. * "largeio" mount option is used.
  480. *
  481. * If compatibility mode is specified, simply return the basic unit of
  482. * caching so that we don't get inefficient read/modify/write I/O from
  483. * user apps. Otherwise....
  484. *
  485. * If the underlying volume is a stripe, then return the stripe width in
  486. * bytes as the recommended I/O size. It is not a stripe and we've set a
  487. * default buffered I/O size, return that, otherwise return the compat
  488. * default.
  489. */
  490. if (xfs_has_large_iosize(mp)) {
  491. if (mp->m_swidth)
  492. return XFS_FSB_TO_B(mp, mp->m_swidth);
  493. if (xfs_has_allocsize(mp))
  494. return 1U << mp->m_allocsize_log;
  495. }
  496. return max_t(uint32_t, PAGE_SIZE, mp->m_sb.sb_blocksize);
  497. }
  498. STATIC int
  499. xfs_vn_getattr(
  500. struct mnt_idmap *idmap,
  501. const struct path *path,
  502. struct kstat *stat,
  503. u32 request_mask,
  504. unsigned int query_flags)
  505. {
  506. struct inode *inode = d_inode(path->dentry);
  507. struct xfs_inode *ip = XFS_I(inode);
  508. struct xfs_mount *mp = ip->i_mount;
  509. vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
  510. vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
  511. trace_xfs_getattr(ip);
  512. if (xfs_is_shutdown(mp))
  513. return -EIO;
  514. stat->size = XFS_ISIZE(ip);
  515. stat->dev = inode->i_sb->s_dev;
  516. stat->mode = inode->i_mode;
  517. stat->nlink = inode->i_nlink;
  518. stat->uid = vfsuid_into_kuid(vfsuid);
  519. stat->gid = vfsgid_into_kgid(vfsgid);
  520. stat->ino = ip->i_ino;
  521. stat->atime = inode_get_atime(inode);
  522. stat->mtime = inode_get_mtime(inode);
  523. stat->ctime = inode_get_ctime(inode);
  524. stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
  525. if (xfs_has_v3inodes(mp)) {
  526. if (request_mask & STATX_BTIME) {
  527. stat->result_mask |= STATX_BTIME;
  528. stat->btime = ip->i_crtime;
  529. }
  530. }
  531. if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
  532. stat->change_cookie = inode_query_iversion(inode);
  533. stat->result_mask |= STATX_CHANGE_COOKIE;
  534. }
  535. /*
  536. * Note: If you add another clause to set an attribute flag, please
  537. * update attributes_mask below.
  538. */
  539. if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
  540. stat->attributes |= STATX_ATTR_IMMUTABLE;
  541. if (ip->i_diflags & XFS_DIFLAG_APPEND)
  542. stat->attributes |= STATX_ATTR_APPEND;
  543. if (ip->i_diflags & XFS_DIFLAG_NODUMP)
  544. stat->attributes |= STATX_ATTR_NODUMP;
  545. stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
  546. STATX_ATTR_APPEND |
  547. STATX_ATTR_NODUMP);
  548. switch (inode->i_mode & S_IFMT) {
  549. case S_IFBLK:
  550. case S_IFCHR:
  551. stat->blksize = BLKDEV_IOSIZE;
  552. stat->rdev = inode->i_rdev;
  553. break;
  554. case S_IFREG:
  555. if (request_mask & STATX_DIOALIGN) {
  556. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  557. struct block_device *bdev = target->bt_bdev;
  558. stat->result_mask |= STATX_DIOALIGN;
  559. stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
  560. stat->dio_offset_align = bdev_logical_block_size(bdev);
  561. }
  562. fallthrough;
  563. default:
  564. stat->blksize = xfs_stat_blksize(ip);
  565. stat->rdev = 0;
  566. break;
  567. }
  568. return 0;
  569. }
  570. static int
  571. xfs_vn_change_ok(
  572. struct mnt_idmap *idmap,
  573. struct dentry *dentry,
  574. struct iattr *iattr)
  575. {
  576. struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
  577. if (xfs_is_readonly(mp))
  578. return -EROFS;
  579. if (xfs_is_shutdown(mp))
  580. return -EIO;
  581. return setattr_prepare(idmap, dentry, iattr);
  582. }
  583. /*
  584. * Set non-size attributes of an inode.
  585. *
  586. * Caution: The caller of this function is responsible for calling
  587. * setattr_prepare() or otherwise verifying the change is fine.
  588. */
  589. static int
  590. xfs_setattr_nonsize(
  591. struct mnt_idmap *idmap,
  592. struct dentry *dentry,
  593. struct xfs_inode *ip,
  594. struct iattr *iattr)
  595. {
  596. xfs_mount_t *mp = ip->i_mount;
  597. struct inode *inode = VFS_I(ip);
  598. int mask = iattr->ia_valid;
  599. xfs_trans_t *tp;
  600. int error;
  601. kuid_t uid = GLOBAL_ROOT_UID;
  602. kgid_t gid = GLOBAL_ROOT_GID;
  603. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  604. struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL;
  605. ASSERT((mask & ATTR_SIZE) == 0);
  606. /*
  607. * If disk quotas is on, we make sure that the dquots do exist on disk,
  608. * before we start any other transactions. Trying to do this later
  609. * is messy. We don't care to take a readlock to look at the ids
  610. * in inode here, because we can't hold it across the trans_reserve.
  611. * If the IDs do change before we take the ilock, we're covered
  612. * because the i_*dquot fields will get updated anyway.
  613. */
  614. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  615. uint qflags = 0;
  616. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  617. uid = from_vfsuid(idmap, i_user_ns(inode),
  618. iattr->ia_vfsuid);
  619. qflags |= XFS_QMOPT_UQUOTA;
  620. } else {
  621. uid = inode->i_uid;
  622. }
  623. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  624. gid = from_vfsgid(idmap, i_user_ns(inode),
  625. iattr->ia_vfsgid);
  626. qflags |= XFS_QMOPT_GQUOTA;
  627. } else {
  628. gid = inode->i_gid;
  629. }
  630. /*
  631. * We take a reference when we initialize udqp and gdqp,
  632. * so it is important that we never blindly double trip on
  633. * the same variable. See xfs_create() for an example.
  634. */
  635. ASSERT(udqp == NULL);
  636. ASSERT(gdqp == NULL);
  637. error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
  638. qflags, &udqp, &gdqp, NULL);
  639. if (error)
  640. return error;
  641. }
  642. error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
  643. has_capability_noaudit(current, CAP_FOWNER), &tp);
  644. if (error)
  645. goto out_dqrele;
  646. /*
  647. * Register quota modifications in the transaction. Must be the owner
  648. * or privileged. These IDs could have changed since we last looked at
  649. * them. But, we're assured that if the ownership did change while we
  650. * didn't have the inode locked, inode's dquot(s) would have changed
  651. * also.
  652. */
  653. if (XFS_IS_UQUOTA_ON(mp) &&
  654. i_uid_needs_update(idmap, iattr, inode)) {
  655. ASSERT(udqp);
  656. old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp);
  657. }
  658. if (XFS_IS_GQUOTA_ON(mp) &&
  659. i_gid_needs_update(idmap, iattr, inode)) {
  660. ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp));
  661. ASSERT(gdqp);
  662. old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp);
  663. }
  664. setattr_copy(idmap, inode, iattr);
  665. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  666. XFS_STATS_INC(mp, xs_ig_attrchg);
  667. if (xfs_has_wsync(mp))
  668. xfs_trans_set_sync(tp);
  669. error = xfs_trans_commit(tp);
  670. /*
  671. * Release any dquot(s) the inode had kept before chown.
  672. */
  673. xfs_qm_dqrele(old_udqp);
  674. xfs_qm_dqrele(old_gdqp);
  675. xfs_qm_dqrele(udqp);
  676. xfs_qm_dqrele(gdqp);
  677. if (error)
  678. return error;
  679. /*
  680. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  681. * update. We could avoid this with linked transactions
  682. * and passing down the transaction pointer all the way
  683. * to attr_set. No previous user of the generic
  684. * Posix ACL code seems to care about this issue either.
  685. */
  686. if (mask & ATTR_MODE) {
  687. error = posix_acl_chmod(idmap, dentry, inode->i_mode);
  688. if (error)
  689. return error;
  690. }
  691. return 0;
  692. out_dqrele:
  693. xfs_qm_dqrele(udqp);
  694. xfs_qm_dqrele(gdqp);
  695. return error;
  696. }
  697. /*
  698. * Truncate file. Must have write permission and not be a directory.
  699. *
  700. * Caution: The caller of this function is responsible for calling
  701. * setattr_prepare() or otherwise verifying the change is fine.
  702. */
  703. STATIC int
  704. xfs_setattr_size(
  705. struct mnt_idmap *idmap,
  706. struct dentry *dentry,
  707. struct xfs_inode *ip,
  708. struct iattr *iattr)
  709. {
  710. struct xfs_mount *mp = ip->i_mount;
  711. struct inode *inode = VFS_I(ip);
  712. xfs_off_t oldsize, newsize;
  713. struct xfs_trans *tp;
  714. int error;
  715. uint lock_flags = 0;
  716. uint resblks = 0;
  717. bool did_zeroing = false;
  718. xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
  719. ASSERT(S_ISREG(inode->i_mode));
  720. ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  721. ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
  722. oldsize = inode->i_size;
  723. newsize = iattr->ia_size;
  724. /*
  725. * Short circuit the truncate case for zero length files.
  726. */
  727. if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
  728. if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
  729. return 0;
  730. /*
  731. * Use the regular setattr path to update the timestamps.
  732. */
  733. iattr->ia_valid &= ~ATTR_SIZE;
  734. return xfs_setattr_nonsize(idmap, dentry, ip, iattr);
  735. }
  736. /*
  737. * Make sure that the dquots are attached to the inode.
  738. */
  739. error = xfs_qm_dqattach(ip);
  740. if (error)
  741. return error;
  742. /*
  743. * Wait for all direct I/O to complete.
  744. */
  745. inode_dio_wait(inode);
  746. /*
  747. * File data changes must be complete before we start the transaction to
  748. * modify the inode. This needs to be done before joining the inode to
  749. * the transaction because the inode cannot be unlocked once it is a
  750. * part of the transaction.
  751. *
  752. * Start with zeroing any data beyond EOF that we may expose on file
  753. * extension, or zeroing out the rest of the block on a downward
  754. * truncate.
  755. */
  756. if (newsize > oldsize) {
  757. trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
  758. error = xfs_zero_range(ip, oldsize, newsize - oldsize,
  759. &did_zeroing);
  760. } else {
  761. error = xfs_truncate_page(ip, newsize, &did_zeroing);
  762. }
  763. if (error)
  764. return error;
  765. /*
  766. * We've already locked out new page faults, so now we can safely remove
  767. * pages from the page cache knowing they won't get refaulted until we
  768. * drop the XFS_MMAP_EXCL lock after the extent manipulations are
  769. * complete. The truncate_setsize() call also cleans partial EOF page
  770. * PTEs on extending truncates and hence ensures sub-page block size
  771. * filesystems are correctly handled, too.
  772. *
  773. * We have to do all the page cache truncate work outside the
  774. * transaction context as the "lock" order is page lock->log space
  775. * reservation as defined by extent allocation in the writeback path.
  776. * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
  777. * having already truncated the in-memory version of the file (i.e. made
  778. * user visible changes). There's not much we can do about this, except
  779. * to hope that the caller sees ENOMEM and retries the truncate
  780. * operation.
  781. *
  782. * And we update in-core i_size and truncate page cache beyond newsize
  783. * before writeback the [i_disk_size, newsize] range, so we're
  784. * guaranteed not to write stale data past the new EOF on truncate down.
  785. */
  786. truncate_setsize(inode, newsize);
  787. /*
  788. * We are going to log the inode size change in this transaction so
  789. * any previous writes that are beyond the on disk EOF and the new
  790. * EOF that have not been written out need to be written here. If we
  791. * do not write the data out, we expose ourselves to the null files
  792. * problem. Note that this includes any block zeroing we did above;
  793. * otherwise those blocks may not be zeroed after a crash.
  794. */
  795. if (did_zeroing ||
  796. (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
  797. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  798. ip->i_disk_size, newsize - 1);
  799. if (error)
  800. return error;
  801. }
  802. /*
  803. * For realtime inode with more than one block rtextsize, we need the
  804. * block reservation for bmap btree block allocations/splits that can
  805. * happen since it could split the tail written extent and convert the
  806. * right beyond EOF one to unwritten.
  807. */
  808. if (xfs_inode_has_bigrtalloc(ip))
  809. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  810. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks,
  811. 0, 0, &tp);
  812. if (error)
  813. return error;
  814. lock_flags |= XFS_ILOCK_EXCL;
  815. xfs_ilock(ip, XFS_ILOCK_EXCL);
  816. xfs_trans_ijoin(tp, ip, 0);
  817. /*
  818. * Only change the c/mtime if we are changing the size or we are
  819. * explicitly asked to change it. This handles the semantic difference
  820. * between truncate() and ftruncate() as implemented in the VFS.
  821. *
  822. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  823. * special case where we need to update the times despite not having
  824. * these flags set. For all other operations the VFS set these flags
  825. * explicitly if it wants a timestamp update.
  826. */
  827. if (newsize != oldsize &&
  828. !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
  829. iattr->ia_ctime = iattr->ia_mtime =
  830. current_time(inode);
  831. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  832. }
  833. /*
  834. * The first thing we do is set the size to new_size permanently on
  835. * disk. This way we don't have to worry about anyone ever being able
  836. * to look at the data being freed even in the face of a crash.
  837. * What we're getting around here is the case where we free a block, it
  838. * is allocated to another file, it is written to, and then we crash.
  839. * If the new data gets written to the file but the log buffers
  840. * containing the free and reallocation don't, then we'd end up with
  841. * garbage in the blocks being freed. As long as we make the new size
  842. * permanent before actually freeing any blocks it doesn't matter if
  843. * they get written to.
  844. */
  845. ip->i_disk_size = newsize;
  846. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  847. if (newsize <= oldsize) {
  848. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  849. if (error)
  850. goto out_trans_cancel;
  851. /*
  852. * Truncated "down", so we're removing references to old data
  853. * here - if we delay flushing for a long time, we expose
  854. * ourselves unduly to the notorious NULL files problem. So,
  855. * we mark this inode and flush it when the file is closed,
  856. * and do not wait the usual (long) time for writeout.
  857. */
  858. xfs_iflags_set(ip, XFS_ITRUNCATED);
  859. /* A truncate down always removes post-EOF blocks. */
  860. xfs_inode_clear_eofblocks_tag(ip);
  861. }
  862. ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
  863. setattr_copy(idmap, inode, iattr);
  864. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  865. XFS_STATS_INC(mp, xs_ig_attrchg);
  866. if (xfs_has_wsync(mp))
  867. xfs_trans_set_sync(tp);
  868. error = xfs_trans_commit(tp);
  869. out_unlock:
  870. if (lock_flags)
  871. xfs_iunlock(ip, lock_flags);
  872. return error;
  873. out_trans_cancel:
  874. xfs_trans_cancel(tp);
  875. goto out_unlock;
  876. }
  877. int
  878. xfs_vn_setattr_size(
  879. struct mnt_idmap *idmap,
  880. struct dentry *dentry,
  881. struct iattr *iattr)
  882. {
  883. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  884. int error;
  885. trace_xfs_setattr(ip);
  886. error = xfs_vn_change_ok(idmap, dentry, iattr);
  887. if (error)
  888. return error;
  889. return xfs_setattr_size(idmap, dentry, ip, iattr);
  890. }
  891. STATIC int
  892. xfs_vn_setattr(
  893. struct mnt_idmap *idmap,
  894. struct dentry *dentry,
  895. struct iattr *iattr)
  896. {
  897. struct inode *inode = d_inode(dentry);
  898. struct xfs_inode *ip = XFS_I(inode);
  899. int error;
  900. if (iattr->ia_valid & ATTR_SIZE) {
  901. uint iolock;
  902. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  903. iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  904. error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
  905. if (error) {
  906. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  907. return error;
  908. }
  909. error = xfs_vn_setattr_size(idmap, dentry, iattr);
  910. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  911. } else {
  912. trace_xfs_setattr(ip);
  913. error = xfs_vn_change_ok(idmap, dentry, iattr);
  914. if (!error)
  915. error = xfs_setattr_nonsize(idmap, dentry, ip, iattr);
  916. }
  917. return error;
  918. }
  919. STATIC int
  920. xfs_vn_update_time(
  921. struct inode *inode,
  922. int flags)
  923. {
  924. struct xfs_inode *ip = XFS_I(inode);
  925. struct xfs_mount *mp = ip->i_mount;
  926. int log_flags = XFS_ILOG_TIMESTAMP;
  927. struct xfs_trans *tp;
  928. int error;
  929. struct timespec64 now;
  930. trace_xfs_update_time(ip);
  931. if (inode->i_sb->s_flags & SB_LAZYTIME) {
  932. if (!((flags & S_VERSION) &&
  933. inode_maybe_inc_iversion(inode, false))) {
  934. generic_update_time(inode, flags);
  935. return 0;
  936. }
  937. /* Capture the iversion update that just occurred */
  938. log_flags |= XFS_ILOG_CORE;
  939. }
  940. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  941. if (error)
  942. return error;
  943. xfs_ilock(ip, XFS_ILOCK_EXCL);
  944. if (flags & (S_CTIME|S_MTIME))
  945. now = inode_set_ctime_current(inode);
  946. else
  947. now = current_time(inode);
  948. if (flags & S_MTIME)
  949. inode_set_mtime_to_ts(inode, now);
  950. if (flags & S_ATIME)
  951. inode_set_atime_to_ts(inode, now);
  952. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  953. xfs_trans_log_inode(tp, ip, log_flags);
  954. return xfs_trans_commit(tp);
  955. }
  956. STATIC int
  957. xfs_vn_fiemap(
  958. struct inode *inode,
  959. struct fiemap_extent_info *fieinfo,
  960. u64 start,
  961. u64 length)
  962. {
  963. int error;
  964. xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
  965. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  966. fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
  967. error = iomap_fiemap(inode, fieinfo, start, length,
  968. &xfs_xattr_iomap_ops);
  969. } else {
  970. error = iomap_fiemap(inode, fieinfo, start, length,
  971. &xfs_read_iomap_ops);
  972. }
  973. xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
  974. return error;
  975. }
  976. STATIC int
  977. xfs_vn_tmpfile(
  978. struct mnt_idmap *idmap,
  979. struct inode *dir,
  980. struct file *file,
  981. umode_t mode)
  982. {
  983. int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file);
  984. return finish_open_simple(file, err);
  985. }
  986. static const struct inode_operations xfs_inode_operations = {
  987. .get_inode_acl = xfs_get_acl,
  988. .set_acl = xfs_set_acl,
  989. .getattr = xfs_vn_getattr,
  990. .setattr = xfs_vn_setattr,
  991. .listxattr = xfs_vn_listxattr,
  992. .fiemap = xfs_vn_fiemap,
  993. .update_time = xfs_vn_update_time,
  994. .fileattr_get = xfs_fileattr_get,
  995. .fileattr_set = xfs_fileattr_set,
  996. };
  997. static const struct inode_operations xfs_dir_inode_operations = {
  998. .create = xfs_vn_create,
  999. .lookup = xfs_vn_lookup,
  1000. .link = xfs_vn_link,
  1001. .unlink = xfs_vn_unlink,
  1002. .symlink = xfs_vn_symlink,
  1003. .mkdir = xfs_vn_mkdir,
  1004. /*
  1005. * Yes, XFS uses the same method for rmdir and unlink.
  1006. *
  1007. * There are some subtile differences deeper in the code,
  1008. * but we use S_ISDIR to check for those.
  1009. */
  1010. .rmdir = xfs_vn_unlink,
  1011. .mknod = xfs_vn_mknod,
  1012. .rename = xfs_vn_rename,
  1013. .get_inode_acl = xfs_get_acl,
  1014. .set_acl = xfs_set_acl,
  1015. .getattr = xfs_vn_getattr,
  1016. .setattr = xfs_vn_setattr,
  1017. .listxattr = xfs_vn_listxattr,
  1018. .update_time = xfs_vn_update_time,
  1019. .tmpfile = xfs_vn_tmpfile,
  1020. .fileattr_get = xfs_fileattr_get,
  1021. .fileattr_set = xfs_fileattr_set,
  1022. };
  1023. static const struct inode_operations xfs_dir_ci_inode_operations = {
  1024. .create = xfs_vn_create,
  1025. .lookup = xfs_vn_ci_lookup,
  1026. .link = xfs_vn_link,
  1027. .unlink = xfs_vn_unlink,
  1028. .symlink = xfs_vn_symlink,
  1029. .mkdir = xfs_vn_mkdir,
  1030. /*
  1031. * Yes, XFS uses the same method for rmdir and unlink.
  1032. *
  1033. * There are some subtile differences deeper in the code,
  1034. * but we use S_ISDIR to check for those.
  1035. */
  1036. .rmdir = xfs_vn_unlink,
  1037. .mknod = xfs_vn_mknod,
  1038. .rename = xfs_vn_rename,
  1039. .get_inode_acl = xfs_get_acl,
  1040. .set_acl = xfs_set_acl,
  1041. .getattr = xfs_vn_getattr,
  1042. .setattr = xfs_vn_setattr,
  1043. .listxattr = xfs_vn_listxattr,
  1044. .update_time = xfs_vn_update_time,
  1045. .tmpfile = xfs_vn_tmpfile,
  1046. .fileattr_get = xfs_fileattr_get,
  1047. .fileattr_set = xfs_fileattr_set,
  1048. };
  1049. static const struct inode_operations xfs_symlink_inode_operations = {
  1050. .get_link = xfs_vn_get_link,
  1051. .getattr = xfs_vn_getattr,
  1052. .setattr = xfs_vn_setattr,
  1053. .listxattr = xfs_vn_listxattr,
  1054. .update_time = xfs_vn_update_time,
  1055. };
  1056. /* Figure out if this file actually supports DAX. */
  1057. static bool
  1058. xfs_inode_supports_dax(
  1059. struct xfs_inode *ip)
  1060. {
  1061. struct xfs_mount *mp = ip->i_mount;
  1062. /* Only supported on regular files. */
  1063. if (!S_ISREG(VFS_I(ip)->i_mode))
  1064. return false;
  1065. /* Block size must match page size */
  1066. if (mp->m_sb.sb_blocksize != PAGE_SIZE)
  1067. return false;
  1068. /* Device has to support DAX too. */
  1069. return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
  1070. }
  1071. static bool
  1072. xfs_inode_should_enable_dax(
  1073. struct xfs_inode *ip)
  1074. {
  1075. if (!IS_ENABLED(CONFIG_FS_DAX))
  1076. return false;
  1077. if (xfs_has_dax_never(ip->i_mount))
  1078. return false;
  1079. if (!xfs_inode_supports_dax(ip))
  1080. return false;
  1081. if (xfs_has_dax_always(ip->i_mount))
  1082. return true;
  1083. if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
  1084. return true;
  1085. return false;
  1086. }
  1087. void
  1088. xfs_diflags_to_iflags(
  1089. struct xfs_inode *ip,
  1090. bool init)
  1091. {
  1092. struct inode *inode = VFS_I(ip);
  1093. unsigned int xflags = xfs_ip2xflags(ip);
  1094. unsigned int flags = 0;
  1095. ASSERT(!(IS_DAX(inode) && init));
  1096. if (xflags & FS_XFLAG_IMMUTABLE)
  1097. flags |= S_IMMUTABLE;
  1098. if (xflags & FS_XFLAG_APPEND)
  1099. flags |= S_APPEND;
  1100. if (xflags & FS_XFLAG_SYNC)
  1101. flags |= S_SYNC;
  1102. if (xflags & FS_XFLAG_NOATIME)
  1103. flags |= S_NOATIME;
  1104. if (init && xfs_inode_should_enable_dax(ip))
  1105. flags |= S_DAX;
  1106. /*
  1107. * S_DAX can only be set during inode initialization and is never set by
  1108. * the VFS, so we cannot mask off S_DAX in i_flags.
  1109. */
  1110. inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
  1111. inode->i_flags |= flags;
  1112. }
  1113. /*
  1114. * Initialize the Linux inode.
  1115. *
  1116. * When reading existing inodes from disk this is called directly from xfs_iget,
  1117. * when creating a new inode it is called from xfs_init_new_inode after setting
  1118. * up the inode. These callers have different criteria for clearing XFS_INEW, so
  1119. * leave it up to the caller to deal with unlocking the inode appropriately.
  1120. */
  1121. void
  1122. xfs_setup_inode(
  1123. struct xfs_inode *ip)
  1124. {
  1125. struct inode *inode = &ip->i_vnode;
  1126. gfp_t gfp_mask;
  1127. inode->i_ino = ip->i_ino;
  1128. inode->i_state |= I_NEW;
  1129. inode_sb_list_add(inode);
  1130. /* make the inode look hashed for the writeback code */
  1131. inode_fake_hash(inode);
  1132. i_size_write(inode, ip->i_disk_size);
  1133. xfs_diflags_to_iflags(ip, true);
  1134. if (S_ISDIR(inode->i_mode)) {
  1135. /*
  1136. * We set the i_rwsem class here to avoid potential races with
  1137. * lockdep_annotate_inode_mutex_key() reinitialising the lock
  1138. * after a filehandle lookup has already found the inode in
  1139. * cache before it has been unlocked via unlock_new_inode().
  1140. */
  1141. lockdep_set_class(&inode->i_rwsem,
  1142. &inode->i_sb->s_type->i_mutex_dir_key);
  1143. lockdep_set_class(&ip->i_lock, &xfs_dir_ilock_class);
  1144. } else {
  1145. lockdep_set_class(&ip->i_lock, &xfs_nondir_ilock_class);
  1146. }
  1147. /*
  1148. * Ensure all page cache allocations are done from GFP_NOFS context to
  1149. * prevent direct reclaim recursion back into the filesystem and blowing
  1150. * stacks or deadlocking.
  1151. */
  1152. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  1153. mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
  1154. /*
  1155. * For real-time inodes update the stable write flags to that of the RT
  1156. * device instead of the data device.
  1157. */
  1158. if (S_ISREG(inode->i_mode) && XFS_IS_REALTIME_INODE(ip))
  1159. xfs_update_stable_writes(ip);
  1160. /*
  1161. * If there is no attribute fork no ACL can exist on this inode,
  1162. * and it can't have any file capabilities attached to it either.
  1163. */
  1164. if (!xfs_inode_has_attr_fork(ip)) {
  1165. inode_has_no_xattr(inode);
  1166. cache_no_acl(inode);
  1167. }
  1168. }
  1169. void
  1170. xfs_setup_iops(
  1171. struct xfs_inode *ip)
  1172. {
  1173. struct inode *inode = &ip->i_vnode;
  1174. switch (inode->i_mode & S_IFMT) {
  1175. case S_IFREG:
  1176. inode->i_op = &xfs_inode_operations;
  1177. inode->i_fop = &xfs_file_operations;
  1178. if (IS_DAX(inode))
  1179. inode->i_mapping->a_ops = &xfs_dax_aops;
  1180. else
  1181. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1182. break;
  1183. case S_IFDIR:
  1184. if (xfs_has_asciici(XFS_M(inode->i_sb)))
  1185. inode->i_op = &xfs_dir_ci_inode_operations;
  1186. else
  1187. inode->i_op = &xfs_dir_inode_operations;
  1188. inode->i_fop = &xfs_dir_file_operations;
  1189. break;
  1190. case S_IFLNK:
  1191. inode->i_op = &xfs_symlink_inode_operations;
  1192. break;
  1193. default:
  1194. inode->i_op = &xfs_inode_operations;
  1195. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1196. break;
  1197. }
  1198. }