xfs_iops.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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_acl.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_quota.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_rtalloc.h"
  35. #include "xfs_error.h"
  36. #include "xfs_itable.h"
  37. #include "xfs_rw.h"
  38. #include "xfs_attr.h"
  39. #include "xfs_buf_item.h"
  40. #include "xfs_utils.h"
  41. #include "xfs_vnodeops.h"
  42. #include "xfs_trace.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. * Bring the timestamps in the XFS inode uptodate.
  52. *
  53. * Used before writing the inode to disk.
  54. */
  55. void
  56. xfs_synchronize_times(
  57. xfs_inode_t *ip)
  58. {
  59. struct inode *inode = VFS_I(ip);
  60. ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
  61. ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
  62. ip->i_d.di_ctime.t_sec = (__int32_t)inode->i_ctime.tv_sec;
  63. ip->i_d.di_ctime.t_nsec = (__int32_t)inode->i_ctime.tv_nsec;
  64. ip->i_d.di_mtime.t_sec = (__int32_t)inode->i_mtime.tv_sec;
  65. ip->i_d.di_mtime.t_nsec = (__int32_t)inode->i_mtime.tv_nsec;
  66. }
  67. /*
  68. * If the linux inode is valid, mark it dirty.
  69. * Used when commiting a dirty inode into a transaction so that
  70. * the inode will get written back by the linux code
  71. */
  72. void
  73. xfs_mark_inode_dirty_sync(
  74. xfs_inode_t *ip)
  75. {
  76. struct inode *inode = VFS_I(ip);
  77. if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
  78. mark_inode_dirty_sync(inode);
  79. }
  80. void
  81. xfs_mark_inode_dirty(
  82. xfs_inode_t *ip)
  83. {
  84. struct inode *inode = VFS_I(ip);
  85. if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
  86. mark_inode_dirty(inode);
  87. }
  88. /*
  89. * Hook in SELinux. This is not quite correct yet, what we really need
  90. * here (as we do for default ACLs) is a mechanism by which creation of
  91. * these attrs can be journalled at inode creation time (along with the
  92. * inode, of course, such that log replay can't cause these to be lost).
  93. */
  94. STATIC int
  95. xfs_init_security(
  96. struct inode *inode,
  97. struct inode *dir)
  98. {
  99. struct xfs_inode *ip = XFS_I(inode);
  100. size_t length;
  101. void *value;
  102. unsigned char *name;
  103. int error;
  104. error = security_inode_init_security(inode, dir, (char **)&name,
  105. &value, &length);
  106. if (error) {
  107. if (error == -EOPNOTSUPP)
  108. return 0;
  109. return -error;
  110. }
  111. error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
  112. kfree(name);
  113. kfree(value);
  114. return error;
  115. }
  116. static void
  117. xfs_dentry_to_name(
  118. struct xfs_name *namep,
  119. struct dentry *dentry)
  120. {
  121. namep->name = dentry->d_name.name;
  122. namep->len = dentry->d_name.len;
  123. }
  124. STATIC void
  125. xfs_cleanup_inode(
  126. struct inode *dir,
  127. struct inode *inode,
  128. struct dentry *dentry)
  129. {
  130. struct xfs_name teardown;
  131. /* Oh, the horror.
  132. * If we can't add the ACL or we fail in
  133. * xfs_init_security we must back out.
  134. * ENOSPC can hit here, among other things.
  135. */
  136. xfs_dentry_to_name(&teardown, dentry);
  137. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  138. iput(inode);
  139. }
  140. STATIC int
  141. xfs_vn_mknod(
  142. struct inode *dir,
  143. struct dentry *dentry,
  144. int mode,
  145. dev_t rdev)
  146. {
  147. struct inode *inode;
  148. struct xfs_inode *ip = NULL;
  149. struct posix_acl *default_acl = NULL;
  150. struct xfs_name name;
  151. int error;
  152. /*
  153. * Irix uses Missed'em'V split, but doesn't want to see
  154. * the upper 5 bits of (14bit) major.
  155. */
  156. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  157. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  158. return -EINVAL;
  159. rdev = sysv_encode_dev(rdev);
  160. } else {
  161. rdev = 0;
  162. }
  163. if (IS_POSIXACL(dir)) {
  164. default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
  165. if (IS_ERR(default_acl))
  166. return -PTR_ERR(default_acl);
  167. if (!default_acl)
  168. mode &= ~current_umask();
  169. }
  170. xfs_dentry_to_name(&name, dentry);
  171. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  172. if (unlikely(error))
  173. goto out_free_acl;
  174. inode = VFS_I(ip);
  175. error = xfs_init_security(inode, dir);
  176. if (unlikely(error))
  177. goto out_cleanup_inode;
  178. if (default_acl) {
  179. error = -xfs_inherit_acl(inode, default_acl);
  180. if (unlikely(error))
  181. goto out_cleanup_inode;
  182. posix_acl_release(default_acl);
  183. }
  184. d_instantiate(dentry, inode);
  185. return -error;
  186. out_cleanup_inode:
  187. xfs_cleanup_inode(dir, inode, dentry);
  188. out_free_acl:
  189. posix_acl_release(default_acl);
  190. return -error;
  191. }
  192. STATIC int
  193. xfs_vn_create(
  194. struct inode *dir,
  195. struct dentry *dentry,
  196. int mode,
  197. struct nameidata *nd)
  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. int 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. struct nameidata *nd)
  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);
  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. struct nameidata *nd)
  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);
  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);
  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);
  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. mode_t mode;
  313. mode = S_IFLNK |
  314. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  315. xfs_dentry_to_name(&name, dentry);
  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);
  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. out:
  328. return -error;
  329. }
  330. STATIC int
  331. xfs_vn_rename(
  332. struct inode *odir,
  333. struct dentry *odentry,
  334. struct inode *ndir,
  335. struct dentry *ndentry)
  336. {
  337. struct inode *new_inode = ndentry->d_inode;
  338. struct xfs_name oname;
  339. struct xfs_name nname;
  340. xfs_dentry_to_name(&oname, odentry);
  341. xfs_dentry_to_name(&nname, ndentry);
  342. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  343. XFS_I(ndir), &nname, new_inode ?
  344. XFS_I(new_inode) : NULL);
  345. }
  346. /*
  347. * careful here - this function can get called recursively, so
  348. * we need to be very careful about how much stack we use.
  349. * uio is kmalloced for this reason...
  350. */
  351. STATIC void *
  352. xfs_vn_follow_link(
  353. struct dentry *dentry,
  354. struct nameidata *nd)
  355. {
  356. char *link;
  357. int error = -ENOMEM;
  358. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  359. if (!link)
  360. goto out_err;
  361. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  362. if (unlikely(error))
  363. goto out_kfree;
  364. nd_set_link(nd, link);
  365. return NULL;
  366. out_kfree:
  367. kfree(link);
  368. out_err:
  369. nd_set_link(nd, ERR_PTR(error));
  370. return NULL;
  371. }
  372. STATIC void
  373. xfs_vn_put_link(
  374. struct dentry *dentry,
  375. struct nameidata *nd,
  376. void *p)
  377. {
  378. char *s = nd_get_link(nd);
  379. if (!IS_ERR(s))
  380. kfree(s);
  381. }
  382. STATIC int
  383. xfs_vn_getattr(
  384. struct vfsmount *mnt,
  385. struct dentry *dentry,
  386. struct kstat *stat)
  387. {
  388. struct inode *inode = dentry->d_inode;
  389. struct xfs_inode *ip = XFS_I(inode);
  390. struct xfs_mount *mp = ip->i_mount;
  391. trace_xfs_getattr(ip);
  392. if (XFS_FORCED_SHUTDOWN(mp))
  393. return XFS_ERROR(EIO);
  394. stat->size = XFS_ISIZE(ip);
  395. stat->dev = inode->i_sb->s_dev;
  396. stat->mode = ip->i_d.di_mode;
  397. stat->nlink = ip->i_d.di_nlink;
  398. stat->uid = ip->i_d.di_uid;
  399. stat->gid = ip->i_d.di_gid;
  400. stat->ino = ip->i_ino;
  401. stat->atime = inode->i_atime;
  402. stat->mtime = inode->i_mtime;
  403. stat->ctime = inode->i_ctime;
  404. stat->blocks =
  405. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  406. switch (inode->i_mode & S_IFMT) {
  407. case S_IFBLK:
  408. case S_IFCHR:
  409. stat->blksize = BLKDEV_IOSIZE;
  410. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  411. sysv_minor(ip->i_df.if_u2.if_rdev));
  412. break;
  413. default:
  414. if (XFS_IS_REALTIME_INODE(ip)) {
  415. /*
  416. * If the file blocks are being allocated from a
  417. * realtime volume, then return the inode's realtime
  418. * extent size or the realtime volume's extent size.
  419. */
  420. stat->blksize =
  421. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  422. } else
  423. stat->blksize = xfs_preferred_iosize(mp);
  424. stat->rdev = 0;
  425. break;
  426. }
  427. return 0;
  428. }
  429. STATIC int
  430. xfs_vn_setattr(
  431. struct dentry *dentry,
  432. struct iattr *iattr)
  433. {
  434. return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
  435. }
  436. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  437. /*
  438. * Call fiemap helper to fill in user data.
  439. * Returns positive errors to xfs_getbmap.
  440. */
  441. STATIC int
  442. xfs_fiemap_format(
  443. void **arg,
  444. struct getbmapx *bmv,
  445. int *full)
  446. {
  447. int error;
  448. struct fiemap_extent_info *fieinfo = *arg;
  449. u32 fiemap_flags = 0;
  450. u64 logical, physical, length;
  451. /* Do nothing for a hole */
  452. if (bmv->bmv_block == -1LL)
  453. return 0;
  454. logical = BBTOB(bmv->bmv_offset);
  455. physical = BBTOB(bmv->bmv_block);
  456. length = BBTOB(bmv->bmv_length);
  457. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  458. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  459. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  460. fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
  461. physical = 0; /* no block yet */
  462. }
  463. if (bmv->bmv_oflags & BMV_OF_LAST)
  464. fiemap_flags |= FIEMAP_EXTENT_LAST;
  465. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  466. length, fiemap_flags);
  467. if (error > 0) {
  468. error = 0;
  469. *full = 1; /* user array now full */
  470. }
  471. return -error;
  472. }
  473. STATIC int
  474. xfs_vn_fiemap(
  475. struct inode *inode,
  476. struct fiemap_extent_info *fieinfo,
  477. u64 start,
  478. u64 length)
  479. {
  480. xfs_inode_t *ip = XFS_I(inode);
  481. struct getbmapx bm;
  482. int error;
  483. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  484. if (error)
  485. return error;
  486. /* Set up bmap header for xfs internal routine */
  487. bm.bmv_offset = BTOBB(start);
  488. /* Special case for whole file */
  489. if (length == FIEMAP_MAX_OFFSET)
  490. bm.bmv_length = -1LL;
  491. else
  492. bm.bmv_length = BTOBB(length);
  493. /* We add one because in getbmap world count includes the header */
  494. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  495. fieinfo->fi_extents_max + 1;
  496. bm.bmv_count = min_t(__s32, bm.bmv_count,
  497. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  498. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  499. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  500. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  501. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  502. bm.bmv_iflags |= BMV_IF_DELALLOC;
  503. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  504. if (error)
  505. return -error;
  506. return 0;
  507. }
  508. static const struct inode_operations xfs_inode_operations = {
  509. .check_acl = xfs_check_acl,
  510. .getattr = xfs_vn_getattr,
  511. .setattr = xfs_vn_setattr,
  512. .setxattr = generic_setxattr,
  513. .getxattr = generic_getxattr,
  514. .removexattr = generic_removexattr,
  515. .listxattr = xfs_vn_listxattr,
  516. .fiemap = xfs_vn_fiemap,
  517. };
  518. static const struct inode_operations xfs_dir_inode_operations = {
  519. .create = xfs_vn_create,
  520. .lookup = xfs_vn_lookup,
  521. .link = xfs_vn_link,
  522. .unlink = xfs_vn_unlink,
  523. .symlink = xfs_vn_symlink,
  524. .mkdir = xfs_vn_mkdir,
  525. /*
  526. * Yes, XFS uses the same method for rmdir and unlink.
  527. *
  528. * There are some subtile differences deeper in the code,
  529. * but we use S_ISDIR to check for those.
  530. */
  531. .rmdir = xfs_vn_unlink,
  532. .mknod = xfs_vn_mknod,
  533. .rename = xfs_vn_rename,
  534. .check_acl = xfs_check_acl,
  535. .getattr = xfs_vn_getattr,
  536. .setattr = xfs_vn_setattr,
  537. .setxattr = generic_setxattr,
  538. .getxattr = generic_getxattr,
  539. .removexattr = generic_removexattr,
  540. .listxattr = xfs_vn_listxattr,
  541. };
  542. static const struct inode_operations xfs_dir_ci_inode_operations = {
  543. .create = xfs_vn_create,
  544. .lookup = xfs_vn_ci_lookup,
  545. .link = xfs_vn_link,
  546. .unlink = xfs_vn_unlink,
  547. .symlink = xfs_vn_symlink,
  548. .mkdir = xfs_vn_mkdir,
  549. /*
  550. * Yes, XFS uses the same method for rmdir and unlink.
  551. *
  552. * There are some subtile differences deeper in the code,
  553. * but we use S_ISDIR to check for those.
  554. */
  555. .rmdir = xfs_vn_unlink,
  556. .mknod = xfs_vn_mknod,
  557. .rename = xfs_vn_rename,
  558. .check_acl = xfs_check_acl,
  559. .getattr = xfs_vn_getattr,
  560. .setattr = xfs_vn_setattr,
  561. .setxattr = generic_setxattr,
  562. .getxattr = generic_getxattr,
  563. .removexattr = generic_removexattr,
  564. .listxattr = xfs_vn_listxattr,
  565. };
  566. static const struct inode_operations xfs_symlink_inode_operations = {
  567. .readlink = generic_readlink,
  568. .follow_link = xfs_vn_follow_link,
  569. .put_link = xfs_vn_put_link,
  570. .check_acl = xfs_check_acl,
  571. .getattr = xfs_vn_getattr,
  572. .setattr = xfs_vn_setattr,
  573. .setxattr = generic_setxattr,
  574. .getxattr = generic_getxattr,
  575. .removexattr = generic_removexattr,
  576. .listxattr = xfs_vn_listxattr,
  577. };
  578. STATIC void
  579. xfs_diflags_to_iflags(
  580. struct inode *inode,
  581. struct xfs_inode *ip)
  582. {
  583. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  584. inode->i_flags |= S_IMMUTABLE;
  585. else
  586. inode->i_flags &= ~S_IMMUTABLE;
  587. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  588. inode->i_flags |= S_APPEND;
  589. else
  590. inode->i_flags &= ~S_APPEND;
  591. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  592. inode->i_flags |= S_SYNC;
  593. else
  594. inode->i_flags &= ~S_SYNC;
  595. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  596. inode->i_flags |= S_NOATIME;
  597. else
  598. inode->i_flags &= ~S_NOATIME;
  599. }
  600. /*
  601. * Initialize the Linux inode, set up the operation vectors and
  602. * unlock the inode.
  603. *
  604. * When reading existing inodes from disk this is called directly
  605. * from xfs_iget, when creating a new inode it is called from
  606. * xfs_ialloc after setting up the inode.
  607. *
  608. * We are always called with an uninitialised linux inode here.
  609. * We need to initialise the necessary fields and take a reference
  610. * on it.
  611. */
  612. void
  613. xfs_setup_inode(
  614. struct xfs_inode *ip)
  615. {
  616. struct inode *inode = &ip->i_vnode;
  617. inode->i_ino = ip->i_ino;
  618. inode->i_state = I_NEW;
  619. inode_sb_list_add(inode);
  620. /* make the inode look hashed for the writeback code */
  621. hlist_add_fake(&inode->i_hash);
  622. inode->i_mode = ip->i_d.di_mode;
  623. inode->i_nlink = ip->i_d.di_nlink;
  624. inode->i_uid = ip->i_d.di_uid;
  625. inode->i_gid = ip->i_d.di_gid;
  626. switch (inode->i_mode & S_IFMT) {
  627. case S_IFBLK:
  628. case S_IFCHR:
  629. inode->i_rdev =
  630. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  631. sysv_minor(ip->i_df.if_u2.if_rdev));
  632. break;
  633. default:
  634. inode->i_rdev = 0;
  635. break;
  636. }
  637. inode->i_generation = ip->i_d.di_gen;
  638. i_size_write(inode, ip->i_d.di_size);
  639. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  640. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  641. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  642. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  643. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  644. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  645. xfs_diflags_to_iflags(inode, ip);
  646. switch (inode->i_mode & S_IFMT) {
  647. case S_IFREG:
  648. inode->i_op = &xfs_inode_operations;
  649. inode->i_fop = &xfs_file_operations;
  650. inode->i_mapping->a_ops = &xfs_address_space_operations;
  651. break;
  652. case S_IFDIR:
  653. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  654. inode->i_op = &xfs_dir_ci_inode_operations;
  655. else
  656. inode->i_op = &xfs_dir_inode_operations;
  657. inode->i_fop = &xfs_dir_file_operations;
  658. break;
  659. case S_IFLNK:
  660. inode->i_op = &xfs_symlink_inode_operations;
  661. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  662. inode->i_mapping->a_ops = &xfs_address_space_operations;
  663. break;
  664. default:
  665. inode->i_op = &xfs_inode_operations;
  666. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  667. break;
  668. }
  669. xfs_iflags_clear(ip, XFS_INEW);
  670. barrier();
  671. unlock_new_inode(inode);
  672. }