xfs_iops.c 34 KB

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