xfs_iops.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. /*
  49. * Directories have different lock order w.r.t. mmap_sem compared to regular
  50. * files. This is due to readdir potentially triggering page faults on a user
  51. * buffer inside filldir(), and this happens with the ilock on the directory
  52. * held. For regular files, the lock order is the other way around - the
  53. * mmap_sem is taken during the page fault, and then we lock the ilock to do
  54. * block mapping. Hence we need a different class for the directory ilock so
  55. * that lockdep can tell them apart.
  56. */
  57. static struct lock_class_key xfs_nondir_ilock_class;
  58. static struct lock_class_key xfs_dir_ilock_class;
  59. static int
  60. xfs_initxattrs(
  61. struct inode *inode,
  62. const struct xattr *xattr_array,
  63. void *fs_info)
  64. {
  65. const struct xattr *xattr;
  66. struct xfs_inode *ip = XFS_I(inode);
  67. int error = 0;
  68. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  69. error = xfs_attr_set(ip, xattr->name, xattr->value,
  70. xattr->value_len, ATTR_SECURE);
  71. if (error < 0)
  72. break;
  73. }
  74. return error;
  75. }
  76. /*
  77. * Hook in SELinux. This is not quite correct yet, what we really need
  78. * here (as we do for default ACLs) is a mechanism by which creation of
  79. * these attrs can be journalled at inode creation time (along with the
  80. * inode, of course, such that log replay can't cause these to be lost).
  81. */
  82. STATIC int
  83. xfs_init_security(
  84. struct inode *inode,
  85. struct inode *dir,
  86. const struct qstr *qstr)
  87. {
  88. return security_inode_init_security(inode, dir, qstr,
  89. &xfs_initxattrs, NULL);
  90. }
  91. static void
  92. xfs_dentry_to_name(
  93. struct xfs_name *namep,
  94. struct dentry *dentry)
  95. {
  96. namep->name = dentry->d_name.name;
  97. namep->len = dentry->d_name.len;
  98. namep->type = XFS_DIR3_FT_UNKNOWN;
  99. }
  100. static int
  101. xfs_dentry_mode_to_name(
  102. struct xfs_name *namep,
  103. struct dentry *dentry,
  104. int mode)
  105. {
  106. namep->name = dentry->d_name.name;
  107. namep->len = dentry->d_name.len;
  108. namep->type = xfs_mode_to_ftype(mode);
  109. if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
  110. return -EFSCORRUPTED;
  111. return 0;
  112. }
  113. STATIC void
  114. xfs_cleanup_inode(
  115. struct inode *dir,
  116. struct inode *inode,
  117. struct dentry *dentry)
  118. {
  119. struct xfs_name teardown;
  120. /* Oh, the horror.
  121. * If we can't add the ACL or we fail in
  122. * xfs_init_security we must back out.
  123. * ENOSPC can hit here, among other things.
  124. */
  125. xfs_dentry_to_name(&teardown, dentry);
  126. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  127. }
  128. STATIC int
  129. xfs_generic_create(
  130. struct inode *dir,
  131. struct dentry *dentry,
  132. umode_t mode,
  133. dev_t rdev,
  134. bool tmpfile) /* unnamed file */
  135. {
  136. struct inode *inode;
  137. struct xfs_inode *ip = NULL;
  138. struct posix_acl *default_acl, *acl;
  139. struct xfs_name name;
  140. int error;
  141. /*
  142. * Irix uses Missed'em'V split, but doesn't want to see
  143. * the upper 5 bits of (14bit) major.
  144. */
  145. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  146. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  147. return -EINVAL;
  148. rdev = sysv_encode_dev(rdev);
  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), dentry, 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(MAXPATHLEN+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. switch (inode->i_mode & S_IFMT) {
  448. case S_IFBLK:
  449. case S_IFCHR:
  450. stat->blksize = BLKDEV_IOSIZE;
  451. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  452. sysv_minor(ip->i_df.if_u2.if_rdev));
  453. break;
  454. default:
  455. if (XFS_IS_REALTIME_INODE(ip)) {
  456. /*
  457. * If the file blocks are being allocated from a
  458. * realtime volume, then return the inode's realtime
  459. * extent size or the realtime volume's extent size.
  460. */
  461. stat->blksize =
  462. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  463. } else
  464. stat->blksize = xfs_preferred_iosize(mp);
  465. stat->rdev = 0;
  466. break;
  467. }
  468. return 0;
  469. }
  470. static void
  471. xfs_setattr_mode(
  472. struct xfs_inode *ip,
  473. struct iattr *iattr)
  474. {
  475. struct inode *inode = VFS_I(ip);
  476. umode_t mode = iattr->ia_mode;
  477. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  478. inode->i_mode &= S_IFMT;
  479. inode->i_mode |= mode & ~S_IFMT;
  480. }
  481. void
  482. xfs_setattr_time(
  483. struct xfs_inode *ip,
  484. struct iattr *iattr)
  485. {
  486. struct inode *inode = VFS_I(ip);
  487. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  488. if (iattr->ia_valid & ATTR_ATIME)
  489. inode->i_atime = iattr->ia_atime;
  490. if (iattr->ia_valid & ATTR_CTIME)
  491. inode->i_ctime = iattr->ia_ctime;
  492. if (iattr->ia_valid & ATTR_MTIME)
  493. inode->i_mtime = iattr->ia_mtime;
  494. }
  495. static int
  496. xfs_vn_change_ok(
  497. struct dentry *dentry,
  498. struct iattr *iattr)
  499. {
  500. struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
  501. if (mp->m_flags & XFS_MOUNT_RDONLY)
  502. return -EROFS;
  503. if (XFS_FORCED_SHUTDOWN(mp))
  504. return -EIO;
  505. return setattr_prepare(dentry, iattr);
  506. }
  507. /*
  508. * Set non-size attributes of an inode.
  509. *
  510. * Caution: The caller of this function is responsible for calling
  511. * setattr_prepare() or otherwise verifying the change is fine.
  512. */
  513. int
  514. xfs_setattr_nonsize(
  515. struct xfs_inode *ip,
  516. struct iattr *iattr,
  517. int flags)
  518. {
  519. xfs_mount_t *mp = ip->i_mount;
  520. struct inode *inode = VFS_I(ip);
  521. int mask = iattr->ia_valid;
  522. xfs_trans_t *tp;
  523. int error;
  524. kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
  525. kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
  526. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  527. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  528. ASSERT((mask & ATTR_SIZE) == 0);
  529. /*
  530. * If disk quotas is on, we make sure that the dquots do exist on disk,
  531. * before we start any other transactions. Trying to do this later
  532. * is messy. We don't care to take a readlock to look at the ids
  533. * in inode here, because we can't hold it across the trans_reserve.
  534. * If the IDs do change before we take the ilock, we're covered
  535. * because the i_*dquot fields will get updated anyway.
  536. */
  537. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  538. uint qflags = 0;
  539. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  540. uid = iattr->ia_uid;
  541. qflags |= XFS_QMOPT_UQUOTA;
  542. } else {
  543. uid = inode->i_uid;
  544. }
  545. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  546. gid = iattr->ia_gid;
  547. qflags |= XFS_QMOPT_GQUOTA;
  548. } else {
  549. gid = inode->i_gid;
  550. }
  551. /*
  552. * We take a reference when we initialize udqp and gdqp,
  553. * so it is important that we never blindly double trip on
  554. * the same variable. See xfs_create() for an example.
  555. */
  556. ASSERT(udqp == NULL);
  557. ASSERT(gdqp == NULL);
  558. error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
  559. xfs_kgid_to_gid(gid),
  560. xfs_get_projid(ip),
  561. qflags, &udqp, &gdqp, NULL);
  562. if (error)
  563. return error;
  564. }
  565. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  566. if (error)
  567. goto out_dqrele;
  568. xfs_ilock(ip, XFS_ILOCK_EXCL);
  569. xfs_trans_ijoin(tp, ip, 0);
  570. /*
  571. * Change file ownership. Must be the owner or privileged.
  572. */
  573. if (mask & (ATTR_UID|ATTR_GID)) {
  574. /*
  575. * These IDs could have changed since we last looked at them.
  576. * But, we're assured that if the ownership did change
  577. * while we didn't have the inode locked, inode's dquot(s)
  578. * would have changed also.
  579. */
  580. iuid = inode->i_uid;
  581. igid = inode->i_gid;
  582. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  583. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  584. /*
  585. * Do a quota reservation only if uid/gid is actually
  586. * going to change.
  587. */
  588. if (XFS_IS_QUOTA_RUNNING(mp) &&
  589. ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
  590. (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
  591. ASSERT(tp);
  592. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  593. NULL, capable(CAP_FOWNER) ?
  594. XFS_QMOPT_FORCE_RES : 0);
  595. if (error) /* out of quota */
  596. goto out_cancel;
  597. }
  598. }
  599. /*
  600. * Change file ownership. Must be the owner or privileged.
  601. */
  602. if (mask & (ATTR_UID|ATTR_GID)) {
  603. /*
  604. * CAP_FSETID overrides the following restrictions:
  605. *
  606. * The set-user-ID and set-group-ID bits of a file will be
  607. * cleared upon successful return from chown()
  608. */
  609. if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
  610. !capable(CAP_FSETID))
  611. inode->i_mode &= ~(S_ISUID|S_ISGID);
  612. /*
  613. * Change the ownerships and register quota modifications
  614. * in the transaction.
  615. */
  616. if (!uid_eq(iuid, uid)) {
  617. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  618. ASSERT(mask & ATTR_UID);
  619. ASSERT(udqp);
  620. olddquot1 = xfs_qm_vop_chown(tp, ip,
  621. &ip->i_udquot, udqp);
  622. }
  623. ip->i_d.di_uid = xfs_kuid_to_uid(uid);
  624. inode->i_uid = uid;
  625. }
  626. if (!gid_eq(igid, gid)) {
  627. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  628. ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
  629. !XFS_IS_PQUOTA_ON(mp));
  630. ASSERT(mask & ATTR_GID);
  631. ASSERT(gdqp);
  632. olddquot2 = xfs_qm_vop_chown(tp, ip,
  633. &ip->i_gdquot, gdqp);
  634. }
  635. ip->i_d.di_gid = xfs_kgid_to_gid(gid);
  636. inode->i_gid = gid;
  637. }
  638. }
  639. if (mask & ATTR_MODE)
  640. xfs_setattr_mode(ip, iattr);
  641. if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  642. xfs_setattr_time(ip, iattr);
  643. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  644. XFS_STATS_INC(mp, xs_ig_attrchg);
  645. if (mp->m_flags & XFS_MOUNT_WSYNC)
  646. xfs_trans_set_sync(tp);
  647. error = xfs_trans_commit(tp);
  648. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  649. /*
  650. * Release any dquot(s) the inode had kept before chown.
  651. */
  652. xfs_qm_dqrele(olddquot1);
  653. xfs_qm_dqrele(olddquot2);
  654. xfs_qm_dqrele(udqp);
  655. xfs_qm_dqrele(gdqp);
  656. if (error)
  657. return error;
  658. /*
  659. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  660. * update. We could avoid this with linked transactions
  661. * and passing down the transaction pointer all the way
  662. * to attr_set. No previous user of the generic
  663. * Posix ACL code seems to care about this issue either.
  664. */
  665. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  666. error = posix_acl_chmod(inode, inode->i_mode);
  667. if (error)
  668. return error;
  669. }
  670. return 0;
  671. out_cancel:
  672. xfs_trans_cancel(tp);
  673. out_dqrele:
  674. xfs_qm_dqrele(udqp);
  675. xfs_qm_dqrele(gdqp);
  676. return error;
  677. }
  678. int
  679. xfs_vn_setattr_nonsize(
  680. struct dentry *dentry,
  681. struct iattr *iattr)
  682. {
  683. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  684. int error;
  685. trace_xfs_setattr(ip);
  686. error = xfs_vn_change_ok(dentry, iattr);
  687. if (error)
  688. return error;
  689. return xfs_setattr_nonsize(ip, iattr, 0);
  690. }
  691. /*
  692. * Truncate file. Must have write permission and not be a directory.
  693. *
  694. * Caution: The caller of this function is responsible for calling
  695. * setattr_prepare() or otherwise verifying the change is fine.
  696. */
  697. int
  698. xfs_setattr_size(
  699. struct xfs_inode *ip,
  700. struct iattr *iattr)
  701. {
  702. struct xfs_mount *mp = ip->i_mount;
  703. struct inode *inode = VFS_I(ip);
  704. xfs_off_t oldsize, newsize;
  705. struct xfs_trans *tp;
  706. int error;
  707. uint lock_flags = 0;
  708. bool did_zeroing = false;
  709. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  710. ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
  711. ASSERT(S_ISREG(inode->i_mode));
  712. ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  713. ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  714. oldsize = inode->i_size;
  715. newsize = iattr->ia_size;
  716. /*
  717. * Short circuit the truncate case for zero length files.
  718. */
  719. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  720. if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
  721. return 0;
  722. /*
  723. * Use the regular setattr path to update the timestamps.
  724. */
  725. iattr->ia_valid &= ~ATTR_SIZE;
  726. return xfs_setattr_nonsize(ip, iattr, 0);
  727. }
  728. /*
  729. * Make sure that the dquots are attached to the inode.
  730. */
  731. error = xfs_qm_dqattach(ip, 0);
  732. if (error)
  733. return error;
  734. /*
  735. * Wait for all direct I/O to complete.
  736. */
  737. inode_dio_wait(inode);
  738. /*
  739. * File data changes must be complete before we start the transaction to
  740. * modify the inode. This needs to be done before joining the inode to
  741. * the transaction because the inode cannot be unlocked once it is a
  742. * part of the transaction.
  743. *
  744. * Start with zeroing any data beyond EOF that we may expose on file
  745. * extension, or zeroing out the rest of the block on a downward
  746. * truncate.
  747. */
  748. if (newsize > oldsize) {
  749. error = xfs_zero_eof(ip, newsize, oldsize, &did_zeroing);
  750. } else {
  751. error = iomap_truncate_page(inode, newsize, &did_zeroing,
  752. &xfs_iomap_ops);
  753. }
  754. if (error)
  755. return error;
  756. /*
  757. * We are going to log the inode size change in this transaction so
  758. * any previous writes that are beyond the on disk EOF and the new
  759. * EOF that have not been written out need to be written here. If we
  760. * do not write the data out, we expose ourselves to the null files
  761. * problem. Note that this includes any block zeroing we did above;
  762. * otherwise those blocks may not be zeroed after a crash.
  763. */
  764. if (did_zeroing ||
  765. (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
  766. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  767. ip->i_d.di_size, newsize);
  768. if (error)
  769. return error;
  770. }
  771. /*
  772. * We've already locked out new page faults, so now we can safely remove
  773. * pages from the page cache knowing they won't get refaulted until we
  774. * drop the XFS_MMAP_EXCL lock after the extent manipulations are
  775. * complete. The truncate_setsize() call also cleans partial EOF page
  776. * PTEs on extending truncates and hence ensures sub-page block size
  777. * filesystems are correctly handled, too.
  778. *
  779. * We have to do all the page cache truncate work outside the
  780. * transaction context as the "lock" order is page lock->log space
  781. * reservation as defined by extent allocation in the writeback path.
  782. * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
  783. * having already truncated the in-memory version of the file (i.e. made
  784. * user visible changes). There's not much we can do about this, except
  785. * to hope that the caller sees ENOMEM and retries the truncate
  786. * operation.
  787. */
  788. truncate_setsize(inode, newsize);
  789. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  790. if (error)
  791. return error;
  792. lock_flags |= XFS_ILOCK_EXCL;
  793. xfs_ilock(ip, XFS_ILOCK_EXCL);
  794. xfs_trans_ijoin(tp, ip, 0);
  795. /*
  796. * Only change the c/mtime if we are changing the size or we are
  797. * explicitly asked to change it. This handles the semantic difference
  798. * between truncate() and ftruncate() as implemented in the VFS.
  799. *
  800. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  801. * special case where we need to update the times despite not having
  802. * these flags set. For all other operations the VFS set these flags
  803. * explicitly if it wants a timestamp update.
  804. */
  805. if (newsize != oldsize &&
  806. !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
  807. iattr->ia_ctime = iattr->ia_mtime =
  808. current_time(inode);
  809. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  810. }
  811. /*
  812. * The first thing we do is set the size to new_size permanently on
  813. * disk. This way we don't have to worry about anyone ever being able
  814. * to look at the data being freed even in the face of a crash.
  815. * What we're getting around here is the case where we free a block, it
  816. * is allocated to another file, it is written to, and then we crash.
  817. * If the new data gets written to the file but the log buffers
  818. * containing the free and reallocation don't, then we'd end up with
  819. * garbage in the blocks being freed. As long as we make the new size
  820. * permanent before actually freeing any blocks it doesn't matter if
  821. * they get written to.
  822. */
  823. ip->i_d.di_size = newsize;
  824. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  825. if (newsize <= oldsize) {
  826. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  827. if (error)
  828. goto out_trans_cancel;
  829. /*
  830. * Truncated "down", so we're removing references to old data
  831. * here - if we delay flushing for a long time, we expose
  832. * ourselves unduly to the notorious NULL files problem. So,
  833. * we mark this inode and flush it when the file is closed,
  834. * and do not wait the usual (long) time for writeout.
  835. */
  836. xfs_iflags_set(ip, XFS_ITRUNCATED);
  837. /* A truncate down always removes post-EOF blocks. */
  838. xfs_inode_clear_eofblocks_tag(ip);
  839. }
  840. if (iattr->ia_valid & ATTR_MODE)
  841. xfs_setattr_mode(ip, iattr);
  842. if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  843. xfs_setattr_time(ip, iattr);
  844. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  845. XFS_STATS_INC(mp, xs_ig_attrchg);
  846. if (mp->m_flags & XFS_MOUNT_WSYNC)
  847. xfs_trans_set_sync(tp);
  848. error = xfs_trans_commit(tp);
  849. out_unlock:
  850. if (lock_flags)
  851. xfs_iunlock(ip, lock_flags);
  852. return error;
  853. out_trans_cancel:
  854. xfs_trans_cancel(tp);
  855. goto out_unlock;
  856. }
  857. int
  858. xfs_vn_setattr_size(
  859. struct dentry *dentry,
  860. struct iattr *iattr)
  861. {
  862. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  863. int error;
  864. trace_xfs_setattr(ip);
  865. error = xfs_vn_change_ok(dentry, iattr);
  866. if (error)
  867. return error;
  868. return xfs_setattr_size(ip, iattr);
  869. }
  870. STATIC int
  871. xfs_vn_setattr(
  872. struct dentry *dentry,
  873. struct iattr *iattr)
  874. {
  875. int error;
  876. if (iattr->ia_valid & ATTR_SIZE) {
  877. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  878. uint iolock = XFS_IOLOCK_EXCL;
  879. error = xfs_break_layouts(d_inode(dentry), &iolock);
  880. if (error)
  881. return error;
  882. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  883. error = xfs_vn_setattr_size(dentry, iattr);
  884. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  885. } else {
  886. error = xfs_vn_setattr_nonsize(dentry, iattr);
  887. }
  888. return error;
  889. }
  890. STATIC int
  891. xfs_vn_update_time(
  892. struct inode *inode,
  893. struct timespec *now,
  894. int flags)
  895. {
  896. struct xfs_inode *ip = XFS_I(inode);
  897. struct xfs_mount *mp = ip->i_mount;
  898. struct xfs_trans *tp;
  899. int error;
  900. trace_xfs_update_time(ip);
  901. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  902. if (error)
  903. return error;
  904. xfs_ilock(ip, XFS_ILOCK_EXCL);
  905. if (flags & S_CTIME)
  906. inode->i_ctime = *now;
  907. if (flags & S_MTIME)
  908. inode->i_mtime = *now;
  909. if (flags & S_ATIME)
  910. inode->i_atime = *now;
  911. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  912. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  913. return xfs_trans_commit(tp);
  914. }
  915. STATIC int
  916. xfs_vn_fiemap(
  917. struct inode *inode,
  918. struct fiemap_extent_info *fieinfo,
  919. u64 start,
  920. u64 length)
  921. {
  922. int error;
  923. xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
  924. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  925. fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
  926. error = iomap_fiemap(inode, fieinfo, start, length,
  927. &xfs_xattr_iomap_ops);
  928. } else {
  929. error = iomap_fiemap(inode, fieinfo, start, length,
  930. &xfs_iomap_ops);
  931. }
  932. xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
  933. return error;
  934. }
  935. STATIC int
  936. xfs_vn_tmpfile(
  937. struct inode *dir,
  938. struct dentry *dentry,
  939. umode_t mode)
  940. {
  941. return xfs_generic_create(dir, dentry, mode, 0, true);
  942. }
  943. static const struct inode_operations xfs_inode_operations = {
  944. .get_acl = xfs_get_acl,
  945. .set_acl = xfs_set_acl,
  946. .getattr = xfs_vn_getattr,
  947. .setattr = xfs_vn_setattr,
  948. .listxattr = xfs_vn_listxattr,
  949. .fiemap = xfs_vn_fiemap,
  950. .update_time = xfs_vn_update_time,
  951. };
  952. static const struct inode_operations xfs_dir_inode_operations = {
  953. .create = xfs_vn_create,
  954. .lookup = xfs_vn_lookup,
  955. .link = xfs_vn_link,
  956. .unlink = xfs_vn_unlink,
  957. .symlink = xfs_vn_symlink,
  958. .mkdir = xfs_vn_mkdir,
  959. /*
  960. * Yes, XFS uses the same method for rmdir and unlink.
  961. *
  962. * There are some subtile differences deeper in the code,
  963. * but we use S_ISDIR to check for those.
  964. */
  965. .rmdir = xfs_vn_unlink,
  966. .mknod = xfs_vn_mknod,
  967. .rename = xfs_vn_rename,
  968. .get_acl = xfs_get_acl,
  969. .set_acl = xfs_set_acl,
  970. .getattr = xfs_vn_getattr,
  971. .setattr = xfs_vn_setattr,
  972. .listxattr = xfs_vn_listxattr,
  973. .update_time = xfs_vn_update_time,
  974. .tmpfile = xfs_vn_tmpfile,
  975. };
  976. static const struct inode_operations xfs_dir_ci_inode_operations = {
  977. .create = xfs_vn_create,
  978. .lookup = xfs_vn_ci_lookup,
  979. .link = xfs_vn_link,
  980. .unlink = xfs_vn_unlink,
  981. .symlink = xfs_vn_symlink,
  982. .mkdir = xfs_vn_mkdir,
  983. /*
  984. * Yes, XFS uses the same method for rmdir and unlink.
  985. *
  986. * There are some subtile differences deeper in the code,
  987. * but we use S_ISDIR to check for those.
  988. */
  989. .rmdir = xfs_vn_unlink,
  990. .mknod = xfs_vn_mknod,
  991. .rename = xfs_vn_rename,
  992. .get_acl = xfs_get_acl,
  993. .set_acl = xfs_set_acl,
  994. .getattr = xfs_vn_getattr,
  995. .setattr = xfs_vn_setattr,
  996. .listxattr = xfs_vn_listxattr,
  997. .update_time = xfs_vn_update_time,
  998. .tmpfile = xfs_vn_tmpfile,
  999. };
  1000. static const struct inode_operations xfs_symlink_inode_operations = {
  1001. .get_link = xfs_vn_get_link,
  1002. .getattr = xfs_vn_getattr,
  1003. .setattr = xfs_vn_setattr,
  1004. .listxattr = xfs_vn_listxattr,
  1005. .update_time = xfs_vn_update_time,
  1006. };
  1007. static const struct inode_operations xfs_inline_symlink_inode_operations = {
  1008. .get_link = xfs_vn_get_link_inline,
  1009. .getattr = xfs_vn_getattr,
  1010. .setattr = xfs_vn_setattr,
  1011. .listxattr = xfs_vn_listxattr,
  1012. .update_time = xfs_vn_update_time,
  1013. };
  1014. STATIC void
  1015. xfs_diflags_to_iflags(
  1016. struct inode *inode,
  1017. struct xfs_inode *ip)
  1018. {
  1019. uint16_t flags = ip->i_d.di_flags;
  1020. inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC |
  1021. S_NOATIME | S_DAX);
  1022. if (flags & XFS_DIFLAG_IMMUTABLE)
  1023. inode->i_flags |= S_IMMUTABLE;
  1024. if (flags & XFS_DIFLAG_APPEND)
  1025. inode->i_flags |= S_APPEND;
  1026. if (flags & XFS_DIFLAG_SYNC)
  1027. inode->i_flags |= S_SYNC;
  1028. if (flags & XFS_DIFLAG_NOATIME)
  1029. inode->i_flags |= S_NOATIME;
  1030. if (S_ISREG(inode->i_mode) &&
  1031. ip->i_mount->m_sb.sb_blocksize == PAGE_SIZE &&
  1032. !xfs_is_reflink_inode(ip) &&
  1033. (ip->i_mount->m_flags & XFS_MOUNT_DAX ||
  1034. ip->i_d.di_flags2 & XFS_DIFLAG2_DAX))
  1035. inode->i_flags |= S_DAX;
  1036. }
  1037. /*
  1038. * Initialize the Linux inode.
  1039. *
  1040. * When reading existing inodes from disk this is called directly from xfs_iget,
  1041. * when creating a new inode it is called from xfs_ialloc after setting up the
  1042. * inode. These callers have different criteria for clearing XFS_INEW, so leave
  1043. * it up to the caller to deal with unlocking the inode appropriately.
  1044. */
  1045. void
  1046. xfs_setup_inode(
  1047. struct xfs_inode *ip)
  1048. {
  1049. struct inode *inode = &ip->i_vnode;
  1050. gfp_t gfp_mask;
  1051. inode->i_ino = ip->i_ino;
  1052. inode->i_state = I_NEW;
  1053. inode_sb_list_add(inode);
  1054. /* make the inode look hashed for the writeback code */
  1055. hlist_add_fake(&inode->i_hash);
  1056. inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
  1057. inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
  1058. switch (inode->i_mode & S_IFMT) {
  1059. case S_IFBLK:
  1060. case S_IFCHR:
  1061. inode->i_rdev =
  1062. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1063. sysv_minor(ip->i_df.if_u2.if_rdev));
  1064. break;
  1065. default:
  1066. inode->i_rdev = 0;
  1067. break;
  1068. }
  1069. i_size_write(inode, ip->i_d.di_size);
  1070. xfs_diflags_to_iflags(inode, ip);
  1071. if (S_ISDIR(inode->i_mode)) {
  1072. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
  1073. ip->d_ops = ip->i_mount->m_dir_inode_ops;
  1074. } else {
  1075. ip->d_ops = ip->i_mount->m_nondir_inode_ops;
  1076. lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
  1077. }
  1078. /*
  1079. * Ensure all page cache allocations are done from GFP_NOFS context to
  1080. * prevent direct reclaim recursion back into the filesystem and blowing
  1081. * stacks or deadlocking.
  1082. */
  1083. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  1084. mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
  1085. /*
  1086. * If there is no attribute fork no ACL can exist on this inode,
  1087. * and it can't have any file capabilities attached to it either.
  1088. */
  1089. if (!XFS_IFORK_Q(ip)) {
  1090. inode_has_no_xattr(inode);
  1091. cache_no_acl(inode);
  1092. }
  1093. }
  1094. void
  1095. xfs_setup_iops(
  1096. struct xfs_inode *ip)
  1097. {
  1098. struct inode *inode = &ip->i_vnode;
  1099. switch (inode->i_mode & S_IFMT) {
  1100. case S_IFREG:
  1101. inode->i_op = &xfs_inode_operations;
  1102. inode->i_fop = &xfs_file_operations;
  1103. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1104. break;
  1105. case S_IFDIR:
  1106. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1107. inode->i_op = &xfs_dir_ci_inode_operations;
  1108. else
  1109. inode->i_op = &xfs_dir_inode_operations;
  1110. inode->i_fop = &xfs_dir_file_operations;
  1111. break;
  1112. case S_IFLNK:
  1113. if (ip->i_df.if_flags & XFS_IFINLINE)
  1114. inode->i_op = &xfs_inline_symlink_inode_operations;
  1115. else
  1116. inode->i_op = &xfs_symlink_inode_operations;
  1117. break;
  1118. default:
  1119. inode->i_op = &xfs_inode_operations;
  1120. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1121. break;
  1122. }
  1123. }