xfs_iops.c 20 KB

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