xfs_iops.c 34 KB

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