xfs_iops.c 34 KB

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