xfs_iops.c 20 KB

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