xfs_iops.c 21 KB

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