xfs_iops.c 35 KB

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