xfs_iops.c 32 KB

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