xfs_iops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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_dir2.h"
  28. #include "xfs_alloc.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_quota.h"
  31. #include "xfs_mount.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir2_sf.h"
  36. #include "xfs_attr_sf.h"
  37. #include "xfs_dinode.h"
  38. #include "xfs_inode.h"
  39. #include "xfs_bmap.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_rtalloc.h"
  43. #include "xfs_error.h"
  44. #include "xfs_itable.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_attr.h"
  47. #include "xfs_buf_item.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_vnodeops.h"
  50. #include <linux/capability.h>
  51. #include <linux/xattr.h>
  52. #include <linux/namei.h>
  53. #include <linux/posix_acl.h>
  54. #include <linux/security.h>
  55. #include <linux/falloc.h>
  56. #include <linux/fiemap.h>
  57. /*
  58. * Bring the timestamps in the XFS inode uptodate.
  59. *
  60. * Used before writing the inode to disk.
  61. */
  62. void
  63. xfs_synchronize_times(
  64. xfs_inode_t *ip)
  65. {
  66. struct inode *inode = VFS_I(ip);
  67. ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
  68. ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
  69. ip->i_d.di_ctime.t_sec = (__int32_t)inode->i_ctime.tv_sec;
  70. ip->i_d.di_ctime.t_nsec = (__int32_t)inode->i_ctime.tv_nsec;
  71. ip->i_d.di_mtime.t_sec = (__int32_t)inode->i_mtime.tv_sec;
  72. ip->i_d.di_mtime.t_nsec = (__int32_t)inode->i_mtime.tv_nsec;
  73. }
  74. /*
  75. * If the linux inode is valid, mark it dirty.
  76. * Used when commiting a dirty inode into a transaction so that
  77. * the inode will get written back by the linux code
  78. */
  79. void
  80. xfs_mark_inode_dirty_sync(
  81. xfs_inode_t *ip)
  82. {
  83. struct inode *inode = VFS_I(ip);
  84. if (!(inode->i_state & (I_WILL_FREE|I_FREEING|I_CLEAR)))
  85. mark_inode_dirty_sync(inode);
  86. }
  87. /*
  88. * Change the requested timestamp in the given inode.
  89. * We don't lock across timestamp updates, and we don't log them but
  90. * we do record the fact that there is dirty information in core.
  91. */
  92. void
  93. xfs_ichgtime(
  94. xfs_inode_t *ip,
  95. int flags)
  96. {
  97. struct inode *inode = VFS_I(ip);
  98. timespec_t tv;
  99. int sync_it = 0;
  100. tv = current_fs_time(inode->i_sb);
  101. if ((flags & XFS_ICHGTIME_MOD) &&
  102. !timespec_equal(&inode->i_mtime, &tv)) {
  103. inode->i_mtime = tv;
  104. sync_it = 1;
  105. }
  106. if ((flags & XFS_ICHGTIME_CHG) &&
  107. !timespec_equal(&inode->i_ctime, &tv)) {
  108. inode->i_ctime = tv;
  109. sync_it = 1;
  110. }
  111. /*
  112. * Update complete - now make sure everyone knows that the inode
  113. * is dirty.
  114. */
  115. if (sync_it)
  116. xfs_mark_inode_dirty_sync(ip);
  117. }
  118. /*
  119. * Hook in SELinux. This is not quite correct yet, what we really need
  120. * here (as we do for default ACLs) is a mechanism by which creation of
  121. * these attrs can be journalled at inode creation time (along with the
  122. * inode, of course, such that log replay can't cause these to be lost).
  123. */
  124. STATIC int
  125. xfs_init_security(
  126. struct inode *inode,
  127. struct inode *dir)
  128. {
  129. struct xfs_inode *ip = XFS_I(inode);
  130. size_t length;
  131. void *value;
  132. char *name;
  133. int error;
  134. error = security_inode_init_security(inode, dir, &name,
  135. &value, &length);
  136. if (error) {
  137. if (error == -EOPNOTSUPP)
  138. return 0;
  139. return -error;
  140. }
  141. error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
  142. kfree(name);
  143. kfree(value);
  144. return error;
  145. }
  146. static void
  147. xfs_dentry_to_name(
  148. struct xfs_name *namep,
  149. struct dentry *dentry)
  150. {
  151. namep->name = dentry->d_name.name;
  152. namep->len = dentry->d_name.len;
  153. }
  154. STATIC void
  155. xfs_cleanup_inode(
  156. struct inode *dir,
  157. struct inode *inode,
  158. struct dentry *dentry)
  159. {
  160. struct xfs_name teardown;
  161. /* Oh, the horror.
  162. * If we can't add the ACL or we fail in
  163. * xfs_init_security we must back out.
  164. * ENOSPC can hit here, among other things.
  165. */
  166. xfs_dentry_to_name(&teardown, dentry);
  167. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  168. iput(inode);
  169. }
  170. STATIC int
  171. xfs_vn_mknod(
  172. struct inode *dir,
  173. struct dentry *dentry,
  174. int mode,
  175. dev_t rdev)
  176. {
  177. struct inode *inode;
  178. struct xfs_inode *ip = NULL;
  179. struct posix_acl *default_acl = NULL;
  180. struct xfs_name name;
  181. int error;
  182. /*
  183. * Irix uses Missed'em'V split, but doesn't want to see
  184. * the upper 5 bits of (14bit) major.
  185. */
  186. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  187. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  188. return -EINVAL;
  189. rdev = sysv_encode_dev(rdev);
  190. } else {
  191. rdev = 0;
  192. }
  193. if (IS_POSIXACL(dir)) {
  194. default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
  195. if (IS_ERR(default_acl))
  196. return -PTR_ERR(default_acl);
  197. if (!default_acl)
  198. mode &= ~current_umask();
  199. }
  200. xfs_dentry_to_name(&name, dentry);
  201. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
  202. if (unlikely(error))
  203. goto out_free_acl;
  204. inode = VFS_I(ip);
  205. error = xfs_init_security(inode, dir);
  206. if (unlikely(error))
  207. goto out_cleanup_inode;
  208. if (default_acl) {
  209. error = -xfs_inherit_acl(inode, default_acl);
  210. if (unlikely(error))
  211. goto out_cleanup_inode;
  212. posix_acl_release(default_acl);
  213. }
  214. d_instantiate(dentry, inode);
  215. return -error;
  216. out_cleanup_inode:
  217. xfs_cleanup_inode(dir, inode, dentry);
  218. out_free_acl:
  219. posix_acl_release(default_acl);
  220. return -error;
  221. }
  222. STATIC int
  223. xfs_vn_create(
  224. struct inode *dir,
  225. struct dentry *dentry,
  226. int mode,
  227. struct nameidata *nd)
  228. {
  229. return xfs_vn_mknod(dir, dentry, mode, 0);
  230. }
  231. STATIC int
  232. xfs_vn_mkdir(
  233. struct inode *dir,
  234. struct dentry *dentry,
  235. int mode)
  236. {
  237. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  238. }
  239. STATIC struct dentry *
  240. xfs_vn_lookup(
  241. struct inode *dir,
  242. struct dentry *dentry,
  243. struct nameidata *nd)
  244. {
  245. struct xfs_inode *cip;
  246. struct xfs_name name;
  247. int error;
  248. if (dentry->d_name.len >= MAXNAMELEN)
  249. return ERR_PTR(-ENAMETOOLONG);
  250. xfs_dentry_to_name(&name, dentry);
  251. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  252. if (unlikely(error)) {
  253. if (unlikely(error != ENOENT))
  254. return ERR_PTR(-error);
  255. d_add(dentry, NULL);
  256. return NULL;
  257. }
  258. return d_splice_alias(VFS_I(cip), dentry);
  259. }
  260. STATIC struct dentry *
  261. xfs_vn_ci_lookup(
  262. struct inode *dir,
  263. struct dentry *dentry,
  264. struct nameidata *nd)
  265. {
  266. struct xfs_inode *ip;
  267. struct xfs_name xname;
  268. struct xfs_name ci_name;
  269. struct qstr dname;
  270. int error;
  271. if (dentry->d_name.len >= MAXNAMELEN)
  272. return ERR_PTR(-ENAMETOOLONG);
  273. xfs_dentry_to_name(&xname, dentry);
  274. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  275. if (unlikely(error)) {
  276. if (unlikely(error != ENOENT))
  277. return ERR_PTR(-error);
  278. /*
  279. * call d_add(dentry, NULL) here when d_drop_negative_children
  280. * is called in xfs_vn_mknod (ie. allow negative dentries
  281. * with CI filesystems).
  282. */
  283. return NULL;
  284. }
  285. /* if exact match, just splice and exit */
  286. if (!ci_name.name)
  287. return d_splice_alias(VFS_I(ip), dentry);
  288. /* else case-insensitive match... */
  289. dname.name = ci_name.name;
  290. dname.len = ci_name.len;
  291. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  292. kmem_free(ci_name.name);
  293. return dentry;
  294. }
  295. STATIC int
  296. xfs_vn_link(
  297. struct dentry *old_dentry,
  298. struct inode *dir,
  299. struct dentry *dentry)
  300. {
  301. struct inode *inode = old_dentry->d_inode;
  302. struct xfs_name name;
  303. int error;
  304. xfs_dentry_to_name(&name, dentry);
  305. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  306. if (unlikely(error))
  307. return -error;
  308. atomic_inc(&inode->i_count);
  309. d_instantiate(dentry, inode);
  310. return 0;
  311. }
  312. STATIC int
  313. xfs_vn_unlink(
  314. struct inode *dir,
  315. struct dentry *dentry)
  316. {
  317. struct xfs_name name;
  318. int error;
  319. xfs_dentry_to_name(&name, dentry);
  320. error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
  321. if (error)
  322. return error;
  323. /*
  324. * With unlink, the VFS makes the dentry "negative": no inode,
  325. * but still hashed. This is incompatible with case-insensitive
  326. * mode, so invalidate (unhash) the dentry in CI-mode.
  327. */
  328. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  329. d_invalidate(dentry);
  330. return 0;
  331. }
  332. STATIC int
  333. xfs_vn_symlink(
  334. struct inode *dir,
  335. struct dentry *dentry,
  336. const char *symname)
  337. {
  338. struct inode *inode;
  339. struct xfs_inode *cip = NULL;
  340. struct xfs_name name;
  341. int error;
  342. mode_t mode;
  343. mode = S_IFLNK |
  344. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  345. xfs_dentry_to_name(&name, dentry);
  346. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
  347. if (unlikely(error))
  348. goto out;
  349. inode = VFS_I(cip);
  350. error = xfs_init_security(inode, dir);
  351. if (unlikely(error))
  352. goto out_cleanup_inode;
  353. d_instantiate(dentry, inode);
  354. return 0;
  355. out_cleanup_inode:
  356. xfs_cleanup_inode(dir, inode, dentry);
  357. out:
  358. return -error;
  359. }
  360. STATIC int
  361. xfs_vn_rename(
  362. struct inode *odir,
  363. struct dentry *odentry,
  364. struct inode *ndir,
  365. struct dentry *ndentry)
  366. {
  367. struct inode *new_inode = ndentry->d_inode;
  368. struct xfs_name oname;
  369. struct xfs_name nname;
  370. xfs_dentry_to_name(&oname, odentry);
  371. xfs_dentry_to_name(&nname, ndentry);
  372. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  373. XFS_I(ndir), &nname, new_inode ?
  374. XFS_I(new_inode) : NULL);
  375. }
  376. /*
  377. * careful here - this function can get called recursively, so
  378. * we need to be very careful about how much stack we use.
  379. * uio is kmalloced for this reason...
  380. */
  381. STATIC void *
  382. xfs_vn_follow_link(
  383. struct dentry *dentry,
  384. struct nameidata *nd)
  385. {
  386. char *link;
  387. int error = -ENOMEM;
  388. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  389. if (!link)
  390. goto out_err;
  391. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  392. if (unlikely(error))
  393. goto out_kfree;
  394. nd_set_link(nd, link);
  395. return NULL;
  396. out_kfree:
  397. kfree(link);
  398. out_err:
  399. nd_set_link(nd, ERR_PTR(error));
  400. return NULL;
  401. }
  402. STATIC void
  403. xfs_vn_put_link(
  404. struct dentry *dentry,
  405. struct nameidata *nd,
  406. void *p)
  407. {
  408. char *s = nd_get_link(nd);
  409. if (!IS_ERR(s))
  410. kfree(s);
  411. }
  412. STATIC int
  413. xfs_vn_permission(
  414. struct inode *inode,
  415. int mask)
  416. {
  417. return generic_permission(inode, mask, xfs_check_acl);
  418. }
  419. STATIC int
  420. xfs_vn_getattr(
  421. struct vfsmount *mnt,
  422. struct dentry *dentry,
  423. struct kstat *stat)
  424. {
  425. struct inode *inode = dentry->d_inode;
  426. struct xfs_inode *ip = XFS_I(inode);
  427. struct xfs_mount *mp = ip->i_mount;
  428. xfs_itrace_entry(ip);
  429. if (XFS_FORCED_SHUTDOWN(mp))
  430. return XFS_ERROR(EIO);
  431. stat->size = XFS_ISIZE(ip);
  432. stat->dev = inode->i_sb->s_dev;
  433. stat->mode = ip->i_d.di_mode;
  434. stat->nlink = ip->i_d.di_nlink;
  435. stat->uid = ip->i_d.di_uid;
  436. stat->gid = ip->i_d.di_gid;
  437. stat->ino = ip->i_ino;
  438. stat->atime = inode->i_atime;
  439. stat->mtime = inode->i_mtime;
  440. stat->ctime = inode->i_ctime;
  441. stat->blocks =
  442. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  443. switch (inode->i_mode & S_IFMT) {
  444. case S_IFBLK:
  445. case S_IFCHR:
  446. stat->blksize = BLKDEV_IOSIZE;
  447. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  448. sysv_minor(ip->i_df.if_u2.if_rdev));
  449. break;
  450. default:
  451. if (XFS_IS_REALTIME_INODE(ip)) {
  452. /*
  453. * If the file blocks are being allocated from a
  454. * realtime volume, then return the inode's realtime
  455. * extent size or the realtime volume's extent size.
  456. */
  457. stat->blksize =
  458. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  459. } else
  460. stat->blksize = xfs_preferred_iosize(mp);
  461. stat->rdev = 0;
  462. break;
  463. }
  464. return 0;
  465. }
  466. STATIC int
  467. xfs_vn_setattr(
  468. struct dentry *dentry,
  469. struct iattr *iattr)
  470. {
  471. return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
  472. }
  473. /*
  474. * block_truncate_page can return an error, but we can't propagate it
  475. * at all here. Leave a complaint + stack trace in the syslog because
  476. * this could be bad. If it is bad, we need to propagate the error further.
  477. */
  478. STATIC void
  479. xfs_vn_truncate(
  480. struct inode *inode)
  481. {
  482. int error;
  483. error = block_truncate_page(inode->i_mapping, inode->i_size,
  484. xfs_get_blocks);
  485. WARN_ON(error);
  486. }
  487. STATIC long
  488. xfs_vn_fallocate(
  489. struct inode *inode,
  490. int mode,
  491. loff_t offset,
  492. loff_t len)
  493. {
  494. long error;
  495. loff_t new_size = 0;
  496. xfs_flock64_t bf;
  497. xfs_inode_t *ip = XFS_I(inode);
  498. /* preallocation on directories not yet supported */
  499. error = -ENODEV;
  500. if (S_ISDIR(inode->i_mode))
  501. goto out_error;
  502. bf.l_whence = 0;
  503. bf.l_start = offset;
  504. bf.l_len = len;
  505. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  506. error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
  507. 0, XFS_ATTR_NOLOCK);
  508. if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
  509. offset + len > i_size_read(inode))
  510. new_size = offset + len;
  511. /* Change file size if needed */
  512. if (new_size) {
  513. struct iattr iattr;
  514. iattr.ia_valid = ATTR_SIZE;
  515. iattr.ia_size = new_size;
  516. error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
  517. }
  518. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  519. out_error:
  520. return error;
  521. }
  522. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  523. /*
  524. * Call fiemap helper to fill in user data.
  525. * Returns positive errors to xfs_getbmap.
  526. */
  527. STATIC int
  528. xfs_fiemap_format(
  529. void **arg,
  530. struct getbmapx *bmv,
  531. int *full)
  532. {
  533. int error;
  534. struct fiemap_extent_info *fieinfo = *arg;
  535. u32 fiemap_flags = 0;
  536. u64 logical, physical, length;
  537. /* Do nothing for a hole */
  538. if (bmv->bmv_block == -1LL)
  539. return 0;
  540. logical = BBTOB(bmv->bmv_offset);
  541. physical = BBTOB(bmv->bmv_block);
  542. length = BBTOB(bmv->bmv_length);
  543. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  544. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  545. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  546. fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
  547. physical = 0; /* no block yet */
  548. }
  549. if (bmv->bmv_oflags & BMV_OF_LAST)
  550. fiemap_flags |= FIEMAP_EXTENT_LAST;
  551. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  552. length, fiemap_flags);
  553. if (error > 0) {
  554. error = 0;
  555. *full = 1; /* user array now full */
  556. }
  557. return -error;
  558. }
  559. STATIC int
  560. xfs_vn_fiemap(
  561. struct inode *inode,
  562. struct fiemap_extent_info *fieinfo,
  563. u64 start,
  564. u64 length)
  565. {
  566. xfs_inode_t *ip = XFS_I(inode);
  567. struct getbmapx bm;
  568. int error;
  569. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  570. if (error)
  571. return error;
  572. /* Set up bmap header for xfs internal routine */
  573. bm.bmv_offset = BTOBB(start);
  574. /* Special case for whole file */
  575. if (length == FIEMAP_MAX_OFFSET)
  576. bm.bmv_length = -1LL;
  577. else
  578. bm.bmv_length = BTOBB(length);
  579. /* We add one because in getbmap world count includes the header */
  580. bm.bmv_count = fieinfo->fi_extents_max + 1;
  581. bm.bmv_iflags = BMV_IF_PREALLOC;
  582. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  583. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  584. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  585. bm.bmv_iflags |= BMV_IF_DELALLOC;
  586. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  587. if (error)
  588. return -error;
  589. return 0;
  590. }
  591. static const struct inode_operations xfs_inode_operations = {
  592. .permission = xfs_vn_permission,
  593. .truncate = xfs_vn_truncate,
  594. .getattr = xfs_vn_getattr,
  595. .setattr = xfs_vn_setattr,
  596. .setxattr = generic_setxattr,
  597. .getxattr = generic_getxattr,
  598. .removexattr = generic_removexattr,
  599. .listxattr = xfs_vn_listxattr,
  600. .fallocate = xfs_vn_fallocate,
  601. .fiemap = xfs_vn_fiemap,
  602. };
  603. static const struct inode_operations xfs_dir_inode_operations = {
  604. .create = xfs_vn_create,
  605. .lookup = xfs_vn_lookup,
  606. .link = xfs_vn_link,
  607. .unlink = xfs_vn_unlink,
  608. .symlink = xfs_vn_symlink,
  609. .mkdir = xfs_vn_mkdir,
  610. /*
  611. * Yes, XFS uses the same method for rmdir and unlink.
  612. *
  613. * There are some subtile differences deeper in the code,
  614. * but we use S_ISDIR to check for those.
  615. */
  616. .rmdir = xfs_vn_unlink,
  617. .mknod = xfs_vn_mknod,
  618. .rename = xfs_vn_rename,
  619. .permission = xfs_vn_permission,
  620. .getattr = xfs_vn_getattr,
  621. .setattr = xfs_vn_setattr,
  622. .setxattr = generic_setxattr,
  623. .getxattr = generic_getxattr,
  624. .removexattr = generic_removexattr,
  625. .listxattr = xfs_vn_listxattr,
  626. };
  627. static const struct inode_operations xfs_dir_ci_inode_operations = {
  628. .create = xfs_vn_create,
  629. .lookup = xfs_vn_ci_lookup,
  630. .link = xfs_vn_link,
  631. .unlink = xfs_vn_unlink,
  632. .symlink = xfs_vn_symlink,
  633. .mkdir = xfs_vn_mkdir,
  634. /*
  635. * Yes, XFS uses the same method for rmdir and unlink.
  636. *
  637. * There are some subtile differences deeper in the code,
  638. * but we use S_ISDIR to check for those.
  639. */
  640. .rmdir = xfs_vn_unlink,
  641. .mknod = xfs_vn_mknod,
  642. .rename = xfs_vn_rename,
  643. .permission = xfs_vn_permission,
  644. .getattr = xfs_vn_getattr,
  645. .setattr = xfs_vn_setattr,
  646. .setxattr = generic_setxattr,
  647. .getxattr = generic_getxattr,
  648. .removexattr = generic_removexattr,
  649. .listxattr = xfs_vn_listxattr,
  650. };
  651. static const struct inode_operations xfs_symlink_inode_operations = {
  652. .readlink = generic_readlink,
  653. .follow_link = xfs_vn_follow_link,
  654. .put_link = xfs_vn_put_link,
  655. .permission = xfs_vn_permission,
  656. .getattr = xfs_vn_getattr,
  657. .setattr = xfs_vn_setattr,
  658. .setxattr = generic_setxattr,
  659. .getxattr = generic_getxattr,
  660. .removexattr = generic_removexattr,
  661. .listxattr = xfs_vn_listxattr,
  662. };
  663. STATIC void
  664. xfs_diflags_to_iflags(
  665. struct inode *inode,
  666. struct xfs_inode *ip)
  667. {
  668. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  669. inode->i_flags |= S_IMMUTABLE;
  670. else
  671. inode->i_flags &= ~S_IMMUTABLE;
  672. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  673. inode->i_flags |= S_APPEND;
  674. else
  675. inode->i_flags &= ~S_APPEND;
  676. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  677. inode->i_flags |= S_SYNC;
  678. else
  679. inode->i_flags &= ~S_SYNC;
  680. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  681. inode->i_flags |= S_NOATIME;
  682. else
  683. inode->i_flags &= ~S_NOATIME;
  684. }
  685. /*
  686. * Initialize the Linux inode, set up the operation vectors and
  687. * unlock the inode.
  688. *
  689. * When reading existing inodes from disk this is called directly
  690. * from xfs_iget, when creating a new inode it is called from
  691. * xfs_ialloc after setting up the inode.
  692. *
  693. * We are always called with an uninitialised linux inode here.
  694. * We need to initialise the necessary fields and take a reference
  695. * on it.
  696. */
  697. void
  698. xfs_setup_inode(
  699. struct xfs_inode *ip)
  700. {
  701. struct inode *inode = &ip->i_vnode;
  702. inode->i_ino = ip->i_ino;
  703. inode->i_state = I_NEW|I_LOCK;
  704. inode_add_to_lists(ip->i_mount->m_super, inode);
  705. inode->i_mode = ip->i_d.di_mode;
  706. inode->i_nlink = ip->i_d.di_nlink;
  707. inode->i_uid = ip->i_d.di_uid;
  708. inode->i_gid = ip->i_d.di_gid;
  709. switch (inode->i_mode & S_IFMT) {
  710. case S_IFBLK:
  711. case S_IFCHR:
  712. inode->i_rdev =
  713. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  714. sysv_minor(ip->i_df.if_u2.if_rdev));
  715. break;
  716. default:
  717. inode->i_rdev = 0;
  718. break;
  719. }
  720. inode->i_generation = ip->i_d.di_gen;
  721. i_size_write(inode, ip->i_d.di_size);
  722. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  723. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  724. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  725. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  726. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  727. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  728. xfs_diflags_to_iflags(inode, ip);
  729. switch (inode->i_mode & S_IFMT) {
  730. case S_IFREG:
  731. inode->i_op = &xfs_inode_operations;
  732. inode->i_fop = &xfs_file_operations;
  733. inode->i_mapping->a_ops = &xfs_address_space_operations;
  734. break;
  735. case S_IFDIR:
  736. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  737. inode->i_op = &xfs_dir_ci_inode_operations;
  738. else
  739. inode->i_op = &xfs_dir_inode_operations;
  740. inode->i_fop = &xfs_dir_file_operations;
  741. break;
  742. case S_IFLNK:
  743. inode->i_op = &xfs_symlink_inode_operations;
  744. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  745. inode->i_mapping->a_ops = &xfs_address_space_operations;
  746. break;
  747. default:
  748. inode->i_op = &xfs_inode_operations;
  749. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  750. break;
  751. }
  752. xfs_iflags_clear(ip, XFS_INEW);
  753. barrier();
  754. unlock_new_inode(inode);
  755. }