xfs_iops.c 33 KB

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