xfs_iops.c 33 KB

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