xfs_iops.c 33 KB

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