xfs_iops.c 32 KB

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