xfs_iops.c 33 KB

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