xfs_iops.c 33 KB

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