xfs_iops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. kfree(name);
  151. kfree(value);
  152. return error;
  153. }
  154. static void
  155. xfs_dentry_to_name(
  156. struct xfs_name *namep,
  157. struct dentry *dentry)
  158. {
  159. namep->name = dentry->d_name.name;
  160. namep->len = dentry->d_name.len;
  161. }
  162. STATIC void
  163. xfs_cleanup_inode(
  164. struct inode *dir,
  165. struct inode *inode,
  166. struct dentry *dentry)
  167. {
  168. struct xfs_name teardown;
  169. /* Oh, the horror.
  170. * If we can't add the ACL or we fail in
  171. * xfs_init_security we must back out.
  172. * ENOSPC can hit here, among other things.
  173. */
  174. xfs_dentry_to_name(&teardown, dentry);
  175. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  176. iput(inode);
  177. }
  178. STATIC int
  179. xfs_vn_mknod(
  180. struct inode *dir,
  181. struct dentry *dentry,
  182. int mode,
  183. dev_t rdev)
  184. {
  185. struct inode *inode;
  186. struct xfs_inode *ip = NULL;
  187. xfs_acl_t *default_acl = NULL;
  188. struct xfs_name name;
  189. int (*test_default_acl)(struct inode *) = _ACL_DEFAULT_EXISTS;
  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 (test_default_acl && test_default_acl(dir)) {
  203. if (!_ACL_ALLOC(default_acl)) {
  204. return -ENOMEM;
  205. }
  206. if (!_ACL_GET_DEFAULT(dir, default_acl)) {
  207. _ACL_FREE(default_acl);
  208. default_acl = NULL;
  209. }
  210. }
  211. if (IS_POSIXACL(dir) && !default_acl)
  212. mode &= ~current_umask();
  213. xfs_dentry_to_name(&name, dentry);
  214. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip, NULL);
  215. if (unlikely(error))
  216. goto out_free_acl;
  217. inode = VFS_I(ip);
  218. error = xfs_init_security(inode, dir);
  219. if (unlikely(error))
  220. goto out_cleanup_inode;
  221. if (default_acl) {
  222. error = _ACL_INHERIT(inode, mode, default_acl);
  223. if (unlikely(error))
  224. goto out_cleanup_inode;
  225. _ACL_FREE(default_acl);
  226. }
  227. d_instantiate(dentry, inode);
  228. return -error;
  229. out_cleanup_inode:
  230. xfs_cleanup_inode(dir, inode, dentry);
  231. out_free_acl:
  232. if (default_acl)
  233. _ACL_FREE(default_acl);
  234. return -error;
  235. }
  236. STATIC int
  237. xfs_vn_create(
  238. struct inode *dir,
  239. struct dentry *dentry,
  240. int mode,
  241. struct nameidata *nd)
  242. {
  243. return xfs_vn_mknod(dir, dentry, mode, 0);
  244. }
  245. STATIC int
  246. xfs_vn_mkdir(
  247. struct inode *dir,
  248. struct dentry *dentry,
  249. int mode)
  250. {
  251. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  252. }
  253. STATIC struct dentry *
  254. xfs_vn_lookup(
  255. struct inode *dir,
  256. struct dentry *dentry,
  257. struct nameidata *nd)
  258. {
  259. struct xfs_inode *cip;
  260. struct xfs_name name;
  261. int error;
  262. if (dentry->d_name.len >= MAXNAMELEN)
  263. return ERR_PTR(-ENAMETOOLONG);
  264. xfs_dentry_to_name(&name, dentry);
  265. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  266. if (unlikely(error)) {
  267. if (unlikely(error != ENOENT))
  268. return ERR_PTR(-error);
  269. d_add(dentry, NULL);
  270. return NULL;
  271. }
  272. return d_splice_alias(VFS_I(cip), dentry);
  273. }
  274. STATIC struct dentry *
  275. xfs_vn_ci_lookup(
  276. struct inode *dir,
  277. struct dentry *dentry,
  278. struct nameidata *nd)
  279. {
  280. struct xfs_inode *ip;
  281. struct xfs_name xname;
  282. struct xfs_name ci_name;
  283. struct qstr dname;
  284. int error;
  285. if (dentry->d_name.len >= MAXNAMELEN)
  286. return ERR_PTR(-ENAMETOOLONG);
  287. xfs_dentry_to_name(&xname, dentry);
  288. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  289. if (unlikely(error)) {
  290. if (unlikely(error != ENOENT))
  291. return ERR_PTR(-error);
  292. /*
  293. * call d_add(dentry, NULL) here when d_drop_negative_children
  294. * is called in xfs_vn_mknod (ie. allow negative dentries
  295. * with CI filesystems).
  296. */
  297. return NULL;
  298. }
  299. /* if exact match, just splice and exit */
  300. if (!ci_name.name)
  301. return d_splice_alias(VFS_I(ip), dentry);
  302. /* else case-insensitive match... */
  303. dname.name = ci_name.name;
  304. dname.len = ci_name.len;
  305. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  306. kmem_free(ci_name.name);
  307. return dentry;
  308. }
  309. STATIC int
  310. xfs_vn_link(
  311. struct dentry *old_dentry,
  312. struct inode *dir,
  313. struct dentry *dentry)
  314. {
  315. struct inode *inode = old_dentry->d_inode;
  316. struct xfs_name name;
  317. int error;
  318. xfs_dentry_to_name(&name, dentry);
  319. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  320. if (unlikely(error))
  321. return -error;
  322. atomic_inc(&inode->i_count);
  323. d_instantiate(dentry, inode);
  324. return 0;
  325. }
  326. STATIC int
  327. xfs_vn_unlink(
  328. struct inode *dir,
  329. struct dentry *dentry)
  330. {
  331. struct xfs_name name;
  332. int error;
  333. xfs_dentry_to_name(&name, dentry);
  334. error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
  335. if (error)
  336. return error;
  337. /*
  338. * With unlink, the VFS makes the dentry "negative": no inode,
  339. * but still hashed. This is incompatible with case-insensitive
  340. * mode, so invalidate (unhash) the dentry in CI-mode.
  341. */
  342. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  343. d_invalidate(dentry);
  344. return 0;
  345. }
  346. STATIC int
  347. xfs_vn_symlink(
  348. struct inode *dir,
  349. struct dentry *dentry,
  350. const char *symname)
  351. {
  352. struct inode *inode;
  353. struct xfs_inode *cip = NULL;
  354. struct xfs_name name;
  355. int error;
  356. mode_t mode;
  357. mode = S_IFLNK |
  358. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  359. xfs_dentry_to_name(&name, dentry);
  360. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip, NULL);
  361. if (unlikely(error))
  362. goto out;
  363. inode = VFS_I(cip);
  364. error = xfs_init_security(inode, dir);
  365. if (unlikely(error))
  366. goto out_cleanup_inode;
  367. d_instantiate(dentry, inode);
  368. return 0;
  369. out_cleanup_inode:
  370. xfs_cleanup_inode(dir, inode, dentry);
  371. out:
  372. return -error;
  373. }
  374. STATIC int
  375. xfs_vn_rename(
  376. struct inode *odir,
  377. struct dentry *odentry,
  378. struct inode *ndir,
  379. struct dentry *ndentry)
  380. {
  381. struct inode *new_inode = ndentry->d_inode;
  382. struct xfs_name oname;
  383. struct xfs_name nname;
  384. xfs_dentry_to_name(&oname, odentry);
  385. xfs_dentry_to_name(&nname, ndentry);
  386. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  387. XFS_I(ndir), &nname, new_inode ?
  388. XFS_I(new_inode) : NULL);
  389. }
  390. /*
  391. * careful here - this function can get called recursively, so
  392. * we need to be very careful about how much stack we use.
  393. * uio is kmalloced for this reason...
  394. */
  395. STATIC void *
  396. xfs_vn_follow_link(
  397. struct dentry *dentry,
  398. struct nameidata *nd)
  399. {
  400. char *link;
  401. int error = -ENOMEM;
  402. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  403. if (!link)
  404. goto out_err;
  405. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  406. if (unlikely(error))
  407. goto out_kfree;
  408. nd_set_link(nd, link);
  409. return NULL;
  410. out_kfree:
  411. kfree(link);
  412. out_err:
  413. nd_set_link(nd, ERR_PTR(error));
  414. return NULL;
  415. }
  416. STATIC void
  417. xfs_vn_put_link(
  418. struct dentry *dentry,
  419. struct nameidata *nd,
  420. void *p)
  421. {
  422. char *s = nd_get_link(nd);
  423. if (!IS_ERR(s))
  424. kfree(s);
  425. }
  426. #ifdef CONFIG_XFS_POSIX_ACL
  427. STATIC int
  428. xfs_check_acl(
  429. struct inode *inode,
  430. int mask)
  431. {
  432. struct xfs_inode *ip = XFS_I(inode);
  433. int error;
  434. xfs_itrace_entry(ip);
  435. if (XFS_IFORK_Q(ip)) {
  436. error = xfs_acl_iaccess(ip, mask, NULL);
  437. if (error != -1)
  438. return -error;
  439. }
  440. return -EAGAIN;
  441. }
  442. STATIC int
  443. xfs_vn_permission(
  444. struct inode *inode,
  445. int mask)
  446. {
  447. return generic_permission(inode, mask, xfs_check_acl);
  448. }
  449. #else
  450. #define xfs_vn_permission NULL
  451. #endif
  452. STATIC int
  453. xfs_vn_getattr(
  454. struct vfsmount *mnt,
  455. struct dentry *dentry,
  456. struct kstat *stat)
  457. {
  458. struct inode *inode = dentry->d_inode;
  459. struct xfs_inode *ip = XFS_I(inode);
  460. struct xfs_mount *mp = ip->i_mount;
  461. xfs_itrace_entry(ip);
  462. if (XFS_FORCED_SHUTDOWN(mp))
  463. return XFS_ERROR(EIO);
  464. stat->size = XFS_ISIZE(ip);
  465. stat->dev = inode->i_sb->s_dev;
  466. stat->mode = ip->i_d.di_mode;
  467. stat->nlink = ip->i_d.di_nlink;
  468. stat->uid = ip->i_d.di_uid;
  469. stat->gid = ip->i_d.di_gid;
  470. stat->ino = ip->i_ino;
  471. stat->atime = inode->i_atime;
  472. stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  473. stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  474. stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  475. stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  476. stat->blocks =
  477. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  478. switch (inode->i_mode & S_IFMT) {
  479. case S_IFBLK:
  480. case S_IFCHR:
  481. stat->blksize = BLKDEV_IOSIZE;
  482. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  483. sysv_minor(ip->i_df.if_u2.if_rdev));
  484. break;
  485. default:
  486. if (XFS_IS_REALTIME_INODE(ip)) {
  487. /*
  488. * If the file blocks are being allocated from a
  489. * realtime volume, then return the inode's realtime
  490. * extent size or the realtime volume's extent size.
  491. */
  492. stat->blksize =
  493. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  494. } else
  495. stat->blksize = xfs_preferred_iosize(mp);
  496. stat->rdev = 0;
  497. break;
  498. }
  499. return 0;
  500. }
  501. STATIC int
  502. xfs_vn_setattr(
  503. struct dentry *dentry,
  504. struct iattr *iattr)
  505. {
  506. return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
  507. }
  508. /*
  509. * block_truncate_page can return an error, but we can't propagate it
  510. * at all here. Leave a complaint + stack trace in the syslog because
  511. * this could be bad. If it is bad, we need to propagate the error further.
  512. */
  513. STATIC void
  514. xfs_vn_truncate(
  515. struct inode *inode)
  516. {
  517. int error;
  518. error = block_truncate_page(inode->i_mapping, inode->i_size,
  519. xfs_get_blocks);
  520. WARN_ON(error);
  521. }
  522. STATIC long
  523. xfs_vn_fallocate(
  524. struct inode *inode,
  525. int mode,
  526. loff_t offset,
  527. loff_t len)
  528. {
  529. long error;
  530. loff_t new_size = 0;
  531. xfs_flock64_t bf;
  532. xfs_inode_t *ip = XFS_I(inode);
  533. /* preallocation on directories not yet supported */
  534. error = -ENODEV;
  535. if (S_ISDIR(inode->i_mode))
  536. goto out_error;
  537. bf.l_whence = 0;
  538. bf.l_start = offset;
  539. bf.l_len = len;
  540. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  541. error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
  542. 0, XFS_ATTR_NOLOCK);
  543. if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
  544. offset + len > i_size_read(inode))
  545. new_size = offset + len;
  546. /* Change file size if needed */
  547. if (new_size) {
  548. struct iattr iattr;
  549. iattr.ia_valid = ATTR_SIZE;
  550. iattr.ia_size = new_size;
  551. error = xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
  552. }
  553. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  554. out_error:
  555. return error;
  556. }
  557. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  558. /*
  559. * Call fiemap helper to fill in user data.
  560. * Returns positive errors to xfs_getbmap.
  561. */
  562. STATIC int
  563. xfs_fiemap_format(
  564. void **arg,
  565. struct getbmapx *bmv,
  566. int *full)
  567. {
  568. int error;
  569. struct fiemap_extent_info *fieinfo = *arg;
  570. u32 fiemap_flags = 0;
  571. u64 logical, physical, length;
  572. /* Do nothing for a hole */
  573. if (bmv->bmv_block == -1LL)
  574. return 0;
  575. logical = BBTOB(bmv->bmv_offset);
  576. physical = BBTOB(bmv->bmv_block);
  577. length = BBTOB(bmv->bmv_length);
  578. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  579. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  580. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  581. fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
  582. physical = 0; /* no block yet */
  583. }
  584. if (bmv->bmv_oflags & BMV_OF_LAST)
  585. fiemap_flags |= FIEMAP_EXTENT_LAST;
  586. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  587. length, fiemap_flags);
  588. if (error > 0) {
  589. error = 0;
  590. *full = 1; /* user array now full */
  591. }
  592. return -error;
  593. }
  594. STATIC int
  595. xfs_vn_fiemap(
  596. struct inode *inode,
  597. struct fiemap_extent_info *fieinfo,
  598. u64 start,
  599. u64 length)
  600. {
  601. xfs_inode_t *ip = XFS_I(inode);
  602. struct getbmapx bm;
  603. int error;
  604. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  605. if (error)
  606. return error;
  607. /* Set up bmap header for xfs internal routine */
  608. bm.bmv_offset = BTOBB(start);
  609. /* Special case for whole file */
  610. if (length == FIEMAP_MAX_OFFSET)
  611. bm.bmv_length = -1LL;
  612. else
  613. bm.bmv_length = BTOBB(length);
  614. /* our formatter will tell xfs_getbmap when to stop. */
  615. bm.bmv_count = MAXEXTNUM;
  616. bm.bmv_iflags = BMV_IF_PREALLOC;
  617. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  618. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  619. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  620. bm.bmv_iflags |= BMV_IF_DELALLOC;
  621. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  622. if (error)
  623. return -error;
  624. return 0;
  625. }
  626. static const struct inode_operations xfs_inode_operations = {
  627. .permission = xfs_vn_permission,
  628. .truncate = xfs_vn_truncate,
  629. .getattr = xfs_vn_getattr,
  630. .setattr = xfs_vn_setattr,
  631. .setxattr = generic_setxattr,
  632. .getxattr = generic_getxattr,
  633. .removexattr = generic_removexattr,
  634. .listxattr = xfs_vn_listxattr,
  635. .fallocate = xfs_vn_fallocate,
  636. .fiemap = xfs_vn_fiemap,
  637. };
  638. static const struct inode_operations xfs_dir_inode_operations = {
  639. .create = xfs_vn_create,
  640. .lookup = xfs_vn_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_dir_ci_inode_operations = {
  663. .create = xfs_vn_create,
  664. .lookup = xfs_vn_ci_lookup,
  665. .link = xfs_vn_link,
  666. .unlink = xfs_vn_unlink,
  667. .symlink = xfs_vn_symlink,
  668. .mkdir = xfs_vn_mkdir,
  669. /*
  670. * Yes, XFS uses the same method for rmdir and unlink.
  671. *
  672. * There are some subtile differences deeper in the code,
  673. * but we use S_ISDIR to check for those.
  674. */
  675. .rmdir = xfs_vn_unlink,
  676. .mknod = xfs_vn_mknod,
  677. .rename = xfs_vn_rename,
  678. .permission = xfs_vn_permission,
  679. .getattr = xfs_vn_getattr,
  680. .setattr = xfs_vn_setattr,
  681. .setxattr = generic_setxattr,
  682. .getxattr = generic_getxattr,
  683. .removexattr = generic_removexattr,
  684. .listxattr = xfs_vn_listxattr,
  685. };
  686. static const struct inode_operations xfs_symlink_inode_operations = {
  687. .readlink = generic_readlink,
  688. .follow_link = xfs_vn_follow_link,
  689. .put_link = xfs_vn_put_link,
  690. .permission = xfs_vn_permission,
  691. .getattr = xfs_vn_getattr,
  692. .setattr = xfs_vn_setattr,
  693. .setxattr = generic_setxattr,
  694. .getxattr = generic_getxattr,
  695. .removexattr = generic_removexattr,
  696. .listxattr = xfs_vn_listxattr,
  697. };
  698. STATIC void
  699. xfs_diflags_to_iflags(
  700. struct inode *inode,
  701. struct xfs_inode *ip)
  702. {
  703. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  704. inode->i_flags |= S_IMMUTABLE;
  705. else
  706. inode->i_flags &= ~S_IMMUTABLE;
  707. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  708. inode->i_flags |= S_APPEND;
  709. else
  710. inode->i_flags &= ~S_APPEND;
  711. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  712. inode->i_flags |= S_SYNC;
  713. else
  714. inode->i_flags &= ~S_SYNC;
  715. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  716. inode->i_flags |= S_NOATIME;
  717. else
  718. inode->i_flags &= ~S_NOATIME;
  719. }
  720. /*
  721. * Initialize the Linux inode, set up the operation vectors and
  722. * unlock the inode.
  723. *
  724. * When reading existing inodes from disk this is called directly
  725. * from xfs_iget, when creating a new inode it is called from
  726. * xfs_ialloc after setting up the inode.
  727. *
  728. * We are always called with an uninitialised linux inode here.
  729. * We need to initialise the necessary fields and take a reference
  730. * on it.
  731. */
  732. void
  733. xfs_setup_inode(
  734. struct xfs_inode *ip)
  735. {
  736. struct inode *inode = &ip->i_vnode;
  737. inode->i_ino = ip->i_ino;
  738. inode->i_state = I_NEW|I_LOCK;
  739. inode_add_to_lists(ip->i_mount->m_super, inode);
  740. inode->i_mode = ip->i_d.di_mode;
  741. inode->i_nlink = ip->i_d.di_nlink;
  742. inode->i_uid = ip->i_d.di_uid;
  743. inode->i_gid = ip->i_d.di_gid;
  744. switch (inode->i_mode & S_IFMT) {
  745. case S_IFBLK:
  746. case S_IFCHR:
  747. inode->i_rdev =
  748. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  749. sysv_minor(ip->i_df.if_u2.if_rdev));
  750. break;
  751. default:
  752. inode->i_rdev = 0;
  753. break;
  754. }
  755. inode->i_generation = ip->i_d.di_gen;
  756. i_size_write(inode, ip->i_d.di_size);
  757. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  758. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  759. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  760. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  761. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  762. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  763. xfs_diflags_to_iflags(inode, ip);
  764. switch (inode->i_mode & S_IFMT) {
  765. case S_IFREG:
  766. inode->i_op = &xfs_inode_operations;
  767. inode->i_fop = &xfs_file_operations;
  768. inode->i_mapping->a_ops = &xfs_address_space_operations;
  769. break;
  770. case S_IFDIR:
  771. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  772. inode->i_op = &xfs_dir_ci_inode_operations;
  773. else
  774. inode->i_op = &xfs_dir_inode_operations;
  775. inode->i_fop = &xfs_dir_file_operations;
  776. break;
  777. case S_IFLNK:
  778. inode->i_op = &xfs_symlink_inode_operations;
  779. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  780. inode->i_mapping->a_ops = &xfs_address_space_operations;
  781. break;
  782. default:
  783. inode->i_op = &xfs_inode_operations;
  784. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  785. break;
  786. }
  787. xfs_iflags_clear(ip, XFS_INEW);
  788. barrier();
  789. unlock_new_inode(inode);
  790. }