xfs_iops.c 34 KB

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