PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_bmap_util.c

https://bitbucket.org/alfredchen/linux-gc
C | 2091 lines | 1373 code | 239 blank | 479 comment | 317 complexity | 9f6d3d50ab1290e343d31f525e449863 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * Copyright (c) 2012 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_defer.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_btree.h"
  31. #include "xfs_trans.h"
  32. #include "xfs_extfree_item.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_bmap_util.h"
  36. #include "xfs_bmap_btree.h"
  37. #include "xfs_rtalloc.h"
  38. #include "xfs_error.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_trans_space.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_icache.h"
  43. #include "xfs_log.h"
  44. #include "xfs_rmap_btree.h"
  45. #include "xfs_iomap.h"
  46. #include "xfs_reflink.h"
  47. #include "xfs_refcount.h"
  48. /* Kernel only BMAP related definitions and functions */
  49. /*
  50. * Convert the given file system block to a disk block. We have to treat it
  51. * differently based on whether the file is a real time file or not, because the
  52. * bmap code does.
  53. */
  54. xfs_daddr_t
  55. xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
  56. {
  57. return (XFS_IS_REALTIME_INODE(ip) ? \
  58. (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
  59. XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
  60. }
  61. /*
  62. * Routine to zero an extent on disk allocated to the specific inode.
  63. *
  64. * The VFS functions take a linearised filesystem block offset, so we have to
  65. * convert the sparse xfs fsb to the right format first.
  66. * VFS types are real funky, too.
  67. */
  68. int
  69. xfs_zero_extent(
  70. struct xfs_inode *ip,
  71. xfs_fsblock_t start_fsb,
  72. xfs_off_t count_fsb)
  73. {
  74. struct xfs_mount *mp = ip->i_mount;
  75. xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
  76. sector_t block = XFS_BB_TO_FSBT(mp, sector);
  77. return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip)),
  78. block << (mp->m_super->s_blocksize_bits - 9),
  79. count_fsb << (mp->m_super->s_blocksize_bits - 9),
  80. GFP_NOFS, true);
  81. }
  82. int
  83. xfs_bmap_rtalloc(
  84. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  85. {
  86. int error; /* error return value */
  87. xfs_mount_t *mp; /* mount point structure */
  88. xfs_extlen_t prod = 0; /* product factor for allocators */
  89. xfs_extlen_t ralen = 0; /* realtime allocation length */
  90. xfs_extlen_t align; /* minimum allocation alignment */
  91. xfs_rtblock_t rtb;
  92. mp = ap->ip->i_mount;
  93. align = xfs_get_extsz_hint(ap->ip);
  94. prod = align / mp->m_sb.sb_rextsize;
  95. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  96. align, 1, ap->eof, 0,
  97. ap->conv, &ap->offset, &ap->length);
  98. if (error)
  99. return error;
  100. ASSERT(ap->length);
  101. ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
  102. /*
  103. * If the offset & length are not perfectly aligned
  104. * then kill prod, it will just get us in trouble.
  105. */
  106. if (do_mod(ap->offset, align) || ap->length % align)
  107. prod = 1;
  108. /*
  109. * Set ralen to be the actual requested length in rtextents.
  110. */
  111. ralen = ap->length / mp->m_sb.sb_rextsize;
  112. /*
  113. * If the old value was close enough to MAXEXTLEN that
  114. * we rounded up to it, cut it back so it's valid again.
  115. * Note that if it's a really large request (bigger than
  116. * MAXEXTLEN), we don't hear about that number, and can't
  117. * adjust the starting point to match it.
  118. */
  119. if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
  120. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  121. /*
  122. * Lock out modifications to both the RT bitmap and summary inodes
  123. */
  124. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
  125. xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  126. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
  127. xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  128. /*
  129. * If it's an allocation to an empty file at offset 0,
  130. * pick an extent that will space things out in the rt area.
  131. */
  132. if (ap->eof && ap->offset == 0) {
  133. xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
  134. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  135. if (error)
  136. return error;
  137. ap->blkno = rtx * mp->m_sb.sb_rextsize;
  138. } else {
  139. ap->blkno = 0;
  140. }
  141. xfs_bmap_adjacent(ap);
  142. /*
  143. * Realtime allocation, done through xfs_rtallocate_extent.
  144. */
  145. do_div(ap->blkno, mp->m_sb.sb_rextsize);
  146. rtb = ap->blkno;
  147. ap->length = ralen;
  148. error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
  149. &ralen, ap->wasdel, prod, &rtb);
  150. if (error)
  151. return error;
  152. ap->blkno = rtb;
  153. if (ap->blkno != NULLFSBLOCK) {
  154. ap->blkno *= mp->m_sb.sb_rextsize;
  155. ralen *= mp->m_sb.sb_rextsize;
  156. ap->length = ralen;
  157. ap->ip->i_d.di_nblocks += ralen;
  158. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  159. if (ap->wasdel)
  160. ap->ip->i_delayed_blks -= ralen;
  161. /*
  162. * Adjust the disk quota also. This was reserved
  163. * earlier.
  164. */
  165. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  166. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  167. XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
  168. /* Zero the extent if we were asked to do so */
  169. if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) {
  170. error = xfs_zero_extent(ap->ip, ap->blkno, ap->length);
  171. if (error)
  172. return error;
  173. }
  174. } else {
  175. ap->length = 0;
  176. }
  177. return 0;
  178. }
  179. /*
  180. * Check if the endoff is outside the last extent. If so the caller will grow
  181. * the allocation to a stripe unit boundary. All offsets are considered outside
  182. * the end of file for an empty fork, so 1 is returned in *eof in that case.
  183. */
  184. int
  185. xfs_bmap_eof(
  186. struct xfs_inode *ip,
  187. xfs_fileoff_t endoff,
  188. int whichfork,
  189. int *eof)
  190. {
  191. struct xfs_bmbt_irec rec;
  192. int error;
  193. error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
  194. if (error || *eof)
  195. return error;
  196. *eof = endoff >= rec.br_startoff + rec.br_blockcount;
  197. return 0;
  198. }
  199. /*
  200. * Extent tree block counting routines.
  201. */
  202. /*
  203. * Count leaf blocks given a range of extent records.
  204. */
  205. STATIC void
  206. xfs_bmap_count_leaves(
  207. xfs_ifork_t *ifp,
  208. xfs_extnum_t idx,
  209. int numrecs,
  210. int *count)
  211. {
  212. int b;
  213. for (b = 0; b < numrecs; b++) {
  214. xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
  215. *count += xfs_bmbt_get_blockcount(frp);
  216. }
  217. }
  218. /*
  219. * Count leaf blocks given a range of extent records originally
  220. * in btree format.
  221. */
  222. STATIC void
  223. xfs_bmap_disk_count_leaves(
  224. struct xfs_mount *mp,
  225. struct xfs_btree_block *block,
  226. int numrecs,
  227. int *count)
  228. {
  229. int b;
  230. xfs_bmbt_rec_t *frp;
  231. for (b = 1; b <= numrecs; b++) {
  232. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  233. *count += xfs_bmbt_disk_get_blockcount(frp);
  234. }
  235. }
  236. /*
  237. * Recursively walks each level of a btree
  238. * to count total fsblocks in use.
  239. */
  240. STATIC int /* error */
  241. xfs_bmap_count_tree(
  242. xfs_mount_t *mp, /* file system mount point */
  243. xfs_trans_t *tp, /* transaction pointer */
  244. xfs_ifork_t *ifp, /* inode fork pointer */
  245. xfs_fsblock_t blockno, /* file system block number */
  246. int levelin, /* level in btree */
  247. int *count) /* Count of blocks */
  248. {
  249. int error;
  250. xfs_buf_t *bp, *nbp;
  251. int level = levelin;
  252. __be64 *pp;
  253. xfs_fsblock_t bno = blockno;
  254. xfs_fsblock_t nextbno;
  255. struct xfs_btree_block *block, *nextblock;
  256. int numrecs;
  257. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
  258. &xfs_bmbt_buf_ops);
  259. if (error)
  260. return error;
  261. *count += 1;
  262. block = XFS_BUF_TO_BLOCK(bp);
  263. if (--level) {
  264. /* Not at node above leaves, count this level of nodes */
  265. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  266. while (nextbno != NULLFSBLOCK) {
  267. error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
  268. XFS_BMAP_BTREE_REF,
  269. &xfs_bmbt_buf_ops);
  270. if (error)
  271. return error;
  272. *count += 1;
  273. nextblock = XFS_BUF_TO_BLOCK(nbp);
  274. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  275. xfs_trans_brelse(tp, nbp);
  276. }
  277. /* Dive to the next level */
  278. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  279. bno = be64_to_cpu(*pp);
  280. if (unlikely((error =
  281. xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
  282. xfs_trans_brelse(tp, bp);
  283. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  284. XFS_ERRLEVEL_LOW, mp);
  285. return -EFSCORRUPTED;
  286. }
  287. xfs_trans_brelse(tp, bp);
  288. } else {
  289. /* count all level 1 nodes and their leaves */
  290. for (;;) {
  291. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  292. numrecs = be16_to_cpu(block->bb_numrecs);
  293. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  294. xfs_trans_brelse(tp, bp);
  295. if (nextbno == NULLFSBLOCK)
  296. break;
  297. bno = nextbno;
  298. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  299. XFS_BMAP_BTREE_REF,
  300. &xfs_bmbt_buf_ops);
  301. if (error)
  302. return error;
  303. *count += 1;
  304. block = XFS_BUF_TO_BLOCK(bp);
  305. }
  306. }
  307. return 0;
  308. }
  309. /*
  310. * Count fsblocks of the given fork.
  311. */
  312. static int /* error */
  313. xfs_bmap_count_blocks(
  314. xfs_trans_t *tp, /* transaction pointer */
  315. xfs_inode_t *ip, /* incore inode */
  316. int whichfork, /* data or attr fork */
  317. int *count) /* out: count of blocks */
  318. {
  319. struct xfs_btree_block *block; /* current btree block */
  320. xfs_fsblock_t bno; /* block # of "block" */
  321. xfs_ifork_t *ifp; /* fork structure */
  322. int level; /* btree level, for checking */
  323. xfs_mount_t *mp; /* file system mount structure */
  324. __be64 *pp; /* pointer to block address */
  325. bno = NULLFSBLOCK;
  326. mp = ip->i_mount;
  327. ifp = XFS_IFORK_PTR(ip, whichfork);
  328. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  329. xfs_bmap_count_leaves(ifp, 0, xfs_iext_count(ifp), count);
  330. return 0;
  331. }
  332. /*
  333. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  334. */
  335. block = ifp->if_broot;
  336. level = be16_to_cpu(block->bb_level);
  337. ASSERT(level > 0);
  338. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  339. bno = be64_to_cpu(*pp);
  340. ASSERT(bno != NULLFSBLOCK);
  341. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  342. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  343. if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
  344. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  345. mp);
  346. return -EFSCORRUPTED;
  347. }
  348. return 0;
  349. }
  350. /*
  351. * returns 1 for success, 0 if we failed to map the extent.
  352. */
  353. STATIC int
  354. xfs_getbmapx_fix_eof_hole(
  355. xfs_inode_t *ip, /* xfs incore inode pointer */
  356. int whichfork,
  357. struct getbmapx *out, /* output structure */
  358. int prealloced, /* this is a file with
  359. * preallocated data space */
  360. __int64_t end, /* last block requested */
  361. xfs_fsblock_t startblock,
  362. bool moretocome)
  363. {
  364. __int64_t fixlen;
  365. xfs_mount_t *mp; /* file system mount point */
  366. xfs_ifork_t *ifp; /* inode fork pointer */
  367. xfs_extnum_t lastx; /* last extent pointer */
  368. xfs_fileoff_t fileblock;
  369. if (startblock == HOLESTARTBLOCK) {
  370. mp = ip->i_mount;
  371. out->bmv_block = -1;
  372. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
  373. fixlen -= out->bmv_offset;
  374. if (prealloced && out->bmv_offset + out->bmv_length == end) {
  375. /* Came to hole at EOF. Trim it. */
  376. if (fixlen <= 0)
  377. return 0;
  378. out->bmv_length = fixlen;
  379. }
  380. } else {
  381. if (startblock == DELAYSTARTBLOCK)
  382. out->bmv_block = -2;
  383. else
  384. out->bmv_block = xfs_fsb_to_db(ip, startblock);
  385. fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
  386. ifp = XFS_IFORK_PTR(ip, whichfork);
  387. if (!moretocome &&
  388. xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
  389. (lastx == xfs_iext_count(ifp) - 1))
  390. out->bmv_oflags |= BMV_OF_LAST;
  391. }
  392. return 1;
  393. }
  394. /* Adjust the reported bmap around shared/unshared extent transitions. */
  395. STATIC int
  396. xfs_getbmap_adjust_shared(
  397. struct xfs_inode *ip,
  398. int whichfork,
  399. struct xfs_bmbt_irec *map,
  400. struct getbmapx *out,
  401. struct xfs_bmbt_irec *next_map)
  402. {
  403. struct xfs_mount *mp = ip->i_mount;
  404. xfs_agnumber_t agno;
  405. xfs_agblock_t agbno;
  406. xfs_agblock_t ebno;
  407. xfs_extlen_t elen;
  408. xfs_extlen_t nlen;
  409. int error;
  410. next_map->br_startblock = NULLFSBLOCK;
  411. next_map->br_startoff = NULLFILEOFF;
  412. next_map->br_blockcount = 0;
  413. /* Only written data blocks can be shared. */
  414. if (!xfs_is_reflink_inode(ip) || whichfork != XFS_DATA_FORK ||
  415. map->br_startblock == DELAYSTARTBLOCK ||
  416. map->br_startblock == HOLESTARTBLOCK ||
  417. ISUNWRITTEN(map))
  418. return 0;
  419. agno = XFS_FSB_TO_AGNO(mp, map->br_startblock);
  420. agbno = XFS_FSB_TO_AGBNO(mp, map->br_startblock);
  421. error = xfs_reflink_find_shared(mp, agno, agbno, map->br_blockcount,
  422. &ebno, &elen, true);
  423. if (error)
  424. return error;
  425. if (ebno == NULLAGBLOCK) {
  426. /* No shared blocks at all. */
  427. return 0;
  428. } else if (agbno == ebno) {
  429. /*
  430. * Shared extent at (agbno, elen). Shrink the reported
  431. * extent length and prepare to move the start of map[i]
  432. * to agbno+elen, with the aim of (re)formatting the new
  433. * map[i] the next time through the inner loop.
  434. */
  435. out->bmv_length = XFS_FSB_TO_BB(mp, elen);
  436. out->bmv_oflags |= BMV_OF_SHARED;
  437. if (elen != map->br_blockcount) {
  438. *next_map = *map;
  439. next_map->br_startblock += elen;
  440. next_map->br_startoff += elen;
  441. next_map->br_blockcount -= elen;
  442. }
  443. map->br_blockcount -= elen;
  444. } else {
  445. /*
  446. * There's an unshared extent (agbno, ebno - agbno)
  447. * followed by shared extent at (ebno, elen). Shrink
  448. * the reported extent length to cover only the unshared
  449. * extent and prepare to move up the start of map[i] to
  450. * ebno, with the aim of (re)formatting the new map[i]
  451. * the next time through the inner loop.
  452. */
  453. *next_map = *map;
  454. nlen = ebno - agbno;
  455. out->bmv_length = XFS_FSB_TO_BB(mp, nlen);
  456. next_map->br_startblock += nlen;
  457. next_map->br_startoff += nlen;
  458. next_map->br_blockcount -= nlen;
  459. map->br_blockcount -= nlen;
  460. }
  461. return 0;
  462. }
  463. /*
  464. * Get inode's extents as described in bmv, and format for output.
  465. * Calls formatter to fill the user's buffer until all extents
  466. * are mapped, until the passed-in bmv->bmv_count slots have
  467. * been filled, or until the formatter short-circuits the loop,
  468. * if it is tracking filled-in extents on its own.
  469. */
  470. int /* error code */
  471. xfs_getbmap(
  472. xfs_inode_t *ip,
  473. struct getbmapx *bmv, /* user bmap structure */
  474. xfs_bmap_format_t formatter, /* format to user */
  475. void *arg) /* formatter arg */
  476. {
  477. __int64_t bmvend; /* last block requested */
  478. int error = 0; /* return value */
  479. __int64_t fixlen; /* length for -1 case */
  480. int i; /* extent number */
  481. int lock; /* lock state */
  482. xfs_bmbt_irec_t *map; /* buffer for user's data */
  483. xfs_mount_t *mp; /* file system mount point */
  484. int nex; /* # of user extents can do */
  485. int subnex; /* # of bmapi's can do */
  486. int nmap; /* number of map entries */
  487. struct getbmapx *out; /* output structure */
  488. int whichfork; /* data or attr fork */
  489. int prealloced; /* this is a file with
  490. * preallocated data space */
  491. int iflags; /* interface flags */
  492. int bmapi_flags; /* flags for xfs_bmapi */
  493. int cur_ext = 0;
  494. struct xfs_bmbt_irec inject_map;
  495. mp = ip->i_mount;
  496. iflags = bmv->bmv_iflags;
  497. #ifndef DEBUG
  498. /* Only allow CoW fork queries if we're debugging. */
  499. if (iflags & BMV_IF_COWFORK)
  500. return -EINVAL;
  501. #endif
  502. if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
  503. return -EINVAL;
  504. if (iflags & BMV_IF_ATTRFORK)
  505. whichfork = XFS_ATTR_FORK;
  506. else if (iflags & BMV_IF_COWFORK)
  507. whichfork = XFS_COW_FORK;
  508. else
  509. whichfork = XFS_DATA_FORK;
  510. switch (whichfork) {
  511. case XFS_ATTR_FORK:
  512. if (XFS_IFORK_Q(ip)) {
  513. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  514. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  515. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  516. return -EINVAL;
  517. } else if (unlikely(
  518. ip->i_d.di_aformat != 0 &&
  519. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  520. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  521. ip->i_mount);
  522. return -EFSCORRUPTED;
  523. }
  524. prealloced = 0;
  525. fixlen = 1LL << 32;
  526. break;
  527. case XFS_COW_FORK:
  528. if (ip->i_cformat != XFS_DINODE_FMT_EXTENTS)
  529. return -EINVAL;
  530. if (xfs_get_cowextsz_hint(ip)) {
  531. prealloced = 1;
  532. fixlen = mp->m_super->s_maxbytes;
  533. } else {
  534. prealloced = 0;
  535. fixlen = XFS_ISIZE(ip);
  536. }
  537. break;
  538. default:
  539. if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  540. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  541. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  542. return -EINVAL;
  543. if (xfs_get_extsz_hint(ip) ||
  544. ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
  545. prealloced = 1;
  546. fixlen = mp->m_super->s_maxbytes;
  547. } else {
  548. prealloced = 0;
  549. fixlen = XFS_ISIZE(ip);
  550. }
  551. break;
  552. }
  553. if (bmv->bmv_length == -1) {
  554. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  555. bmv->bmv_length =
  556. max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
  557. } else if (bmv->bmv_length == 0) {
  558. bmv->bmv_entries = 0;
  559. return 0;
  560. } else if (bmv->bmv_length < 0) {
  561. return -EINVAL;
  562. }
  563. nex = bmv->bmv_count - 1;
  564. if (nex <= 0)
  565. return -EINVAL;
  566. bmvend = bmv->bmv_offset + bmv->bmv_length;
  567. if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
  568. return -ENOMEM;
  569. out = kmem_zalloc_large(bmv->bmv_count * sizeof(struct getbmapx), 0);
  570. if (!out)
  571. return -ENOMEM;
  572. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  573. switch (whichfork) {
  574. case XFS_DATA_FORK:
  575. if (!(iflags & BMV_IF_DELALLOC) &&
  576. (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
  577. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  578. if (error)
  579. goto out_unlock_iolock;
  580. /*
  581. * Even after flushing the inode, there can still be
  582. * delalloc blocks on the inode beyond EOF due to
  583. * speculative preallocation. These are not removed
  584. * until the release function is called or the inode
  585. * is inactivated. Hence we cannot assert here that
  586. * ip->i_delayed_blks == 0.
  587. */
  588. }
  589. lock = xfs_ilock_data_map_shared(ip);
  590. break;
  591. case XFS_COW_FORK:
  592. lock = XFS_ILOCK_SHARED;
  593. xfs_ilock(ip, lock);
  594. break;
  595. case XFS_ATTR_FORK:
  596. lock = xfs_ilock_attr_map_shared(ip);
  597. break;
  598. }
  599. /*
  600. * Don't let nex be bigger than the number of extents
  601. * we can have assuming alternating holes and real extents.
  602. */
  603. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  604. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  605. bmapi_flags = xfs_bmapi_aflag(whichfork);
  606. if (!(iflags & BMV_IF_PREALLOC))
  607. bmapi_flags |= XFS_BMAPI_IGSTATE;
  608. /*
  609. * Allocate enough space to handle "subnex" maps at a time.
  610. */
  611. error = -ENOMEM;
  612. subnex = 16;
  613. map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
  614. if (!map)
  615. goto out_unlock_ilock;
  616. bmv->bmv_entries = 0;
  617. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
  618. (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
  619. error = 0;
  620. goto out_free_map;
  621. }
  622. do {
  623. nmap = (nex> subnex) ? subnex : nex;
  624. error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  625. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  626. map, &nmap, bmapi_flags);
  627. if (error)
  628. goto out_free_map;
  629. ASSERT(nmap <= subnex);
  630. for (i = 0; i < nmap && bmv->bmv_length &&
  631. cur_ext < bmv->bmv_count - 1; i++) {
  632. out[cur_ext].bmv_oflags = 0;
  633. if (map[i].br_state == XFS_EXT_UNWRITTEN)
  634. out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
  635. else if (map[i].br_startblock == DELAYSTARTBLOCK)
  636. out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
  637. out[cur_ext].bmv_offset =
  638. XFS_FSB_TO_BB(mp, map[i].br_startoff);
  639. out[cur_ext].bmv_length =
  640. XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  641. out[cur_ext].bmv_unused1 = 0;
  642. out[cur_ext].bmv_unused2 = 0;
  643. /*
  644. * delayed allocation extents that start beyond EOF can
  645. * occur due to speculative EOF allocation when the
  646. * delalloc extent is larger than the largest freespace
  647. * extent at conversion time. These extents cannot be
  648. * converted by data writeback, so can exist here even
  649. * if we are not supposed to be finding delalloc
  650. * extents.
  651. */
  652. if (map[i].br_startblock == DELAYSTARTBLOCK &&
  653. map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
  654. ASSERT((iflags & BMV_IF_DELALLOC) != 0);
  655. if (map[i].br_startblock == HOLESTARTBLOCK &&
  656. whichfork == XFS_ATTR_FORK) {
  657. /* came to the end of attribute fork */
  658. out[cur_ext].bmv_oflags |= BMV_OF_LAST;
  659. goto out_free_map;
  660. }
  661. /* Is this a shared block? */
  662. error = xfs_getbmap_adjust_shared(ip, whichfork,
  663. &map[i], &out[cur_ext], &inject_map);
  664. if (error)
  665. goto out_free_map;
  666. if (!xfs_getbmapx_fix_eof_hole(ip, whichfork,
  667. &out[cur_ext], prealloced, bmvend,
  668. map[i].br_startblock,
  669. inject_map.br_startblock != NULLFSBLOCK))
  670. goto out_free_map;
  671. bmv->bmv_offset =
  672. out[cur_ext].bmv_offset +
  673. out[cur_ext].bmv_length;
  674. bmv->bmv_length =
  675. max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
  676. /*
  677. * In case we don't want to return the hole,
  678. * don't increase cur_ext so that we can reuse
  679. * it in the next loop.
  680. */
  681. if ((iflags & BMV_IF_NO_HOLES) &&
  682. map[i].br_startblock == HOLESTARTBLOCK) {
  683. memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
  684. continue;
  685. }
  686. /*
  687. * In order to report shared extents accurately,
  688. * we report each distinct shared/unshared part
  689. * of a single bmbt record using multiple bmap
  690. * extents. To make that happen, we iterate the
  691. * same map array item multiple times, each
  692. * time trimming out the subextent that we just
  693. * reported.
  694. *
  695. * Because of this, we must check the out array
  696. * index (cur_ext) directly against bmv_count-1
  697. * to avoid overflows.
  698. */
  699. if (inject_map.br_startblock != NULLFSBLOCK) {
  700. map[i] = inject_map;
  701. i--;
  702. }
  703. bmv->bmv_entries++;
  704. cur_ext++;
  705. }
  706. } while (nmap && bmv->bmv_length && cur_ext < bmv->bmv_count - 1);
  707. out_free_map:
  708. kmem_free(map);
  709. out_unlock_ilock:
  710. xfs_iunlock(ip, lock);
  711. out_unlock_iolock:
  712. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  713. for (i = 0; i < cur_ext; i++) {
  714. /* format results & advance arg */
  715. error = formatter(&arg, &out[i]);
  716. if (error)
  717. break;
  718. }
  719. kmem_free(out);
  720. return error;
  721. }
  722. /*
  723. * dead simple method of punching delalyed allocation blocks from a range in
  724. * the inode. Walks a block at a time so will be slow, but is only executed in
  725. * rare error cases so the overhead is not critical. This will always punch out
  726. * both the start and end blocks, even if the ranges only partially overlap
  727. * them, so it is up to the caller to ensure that partial blocks are not
  728. * passed in.
  729. */
  730. int
  731. xfs_bmap_punch_delalloc_range(
  732. struct xfs_inode *ip,
  733. xfs_fileoff_t start_fsb,
  734. xfs_fileoff_t length)
  735. {
  736. xfs_fileoff_t remaining = length;
  737. int error = 0;
  738. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  739. do {
  740. int done;
  741. xfs_bmbt_irec_t imap;
  742. int nimaps = 1;
  743. xfs_fsblock_t firstblock;
  744. struct xfs_defer_ops dfops;
  745. /*
  746. * Map the range first and check that it is a delalloc extent
  747. * before trying to unmap the range. Otherwise we will be
  748. * trying to remove a real extent (which requires a
  749. * transaction) or a hole, which is probably a bad idea...
  750. */
  751. error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
  752. XFS_BMAPI_ENTIRE);
  753. if (error) {
  754. /* something screwed, just bail */
  755. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  756. xfs_alert(ip->i_mount,
  757. "Failed delalloc mapping lookup ino %lld fsb %lld.",
  758. ip->i_ino, start_fsb);
  759. }
  760. break;
  761. }
  762. if (!nimaps) {
  763. /* nothing there */
  764. goto next_block;
  765. }
  766. if (imap.br_startblock != DELAYSTARTBLOCK) {
  767. /* been converted, ignore */
  768. goto next_block;
  769. }
  770. WARN_ON(imap.br_blockcount == 0);
  771. /*
  772. * Note: while we initialise the firstblock/dfops pair, they
  773. * should never be used because blocks should never be
  774. * allocated or freed for a delalloc extent and hence we need
  775. * don't cancel or finish them after the xfs_bunmapi() call.
  776. */
  777. xfs_defer_init(&dfops, &firstblock);
  778. error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
  779. &dfops, &done);
  780. if (error)
  781. break;
  782. ASSERT(!xfs_defer_has_unfinished_work(&dfops));
  783. next_block:
  784. start_fsb++;
  785. remaining--;
  786. } while(remaining > 0);
  787. return error;
  788. }
  789. /*
  790. * Test whether it is appropriate to check an inode for and free post EOF
  791. * blocks. The 'force' parameter determines whether we should also consider
  792. * regular files that are marked preallocated or append-only.
  793. */
  794. bool
  795. xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
  796. {
  797. /* prealloc/delalloc exists only on regular files */
  798. if (!S_ISREG(VFS_I(ip)->i_mode))
  799. return false;
  800. /*
  801. * Zero sized files with no cached pages and delalloc blocks will not
  802. * have speculative prealloc/delalloc blocks to remove.
  803. */
  804. if (VFS_I(ip)->i_size == 0 &&
  805. VFS_I(ip)->i_mapping->nrpages == 0 &&
  806. ip->i_delayed_blks == 0)
  807. return false;
  808. /* If we haven't read in the extent list, then don't do it now. */
  809. if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
  810. return false;
  811. /*
  812. * Do not free real preallocated or append-only files unless the file
  813. * has delalloc blocks and we are forced to remove them.
  814. */
  815. if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
  816. if (!force || ip->i_delayed_blks == 0)
  817. return false;
  818. return true;
  819. }
  820. /*
  821. * This is called by xfs_inactive to free any blocks beyond eof
  822. * when the link count isn't zero and by xfs_dm_punch_hole() when
  823. * punching a hole to EOF.
  824. */
  825. int
  826. xfs_free_eofblocks(
  827. struct xfs_inode *ip)
  828. {
  829. struct xfs_trans *tp;
  830. int error;
  831. xfs_fileoff_t end_fsb;
  832. xfs_fileoff_t last_fsb;
  833. xfs_filblks_t map_len;
  834. int nimaps;
  835. struct xfs_bmbt_irec imap;
  836. struct xfs_mount *mp = ip->i_mount;
  837. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  838. /*
  839. * Figure out if there are any blocks beyond the end
  840. * of the file. If not, then there is nothing to do.
  841. */
  842. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  843. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  844. if (last_fsb <= end_fsb)
  845. return 0;
  846. map_len = last_fsb - end_fsb;
  847. nimaps = 1;
  848. xfs_ilock(ip, XFS_ILOCK_SHARED);
  849. error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
  850. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  851. /*
  852. * If there are blocks after the end of file, truncate the file to its
  853. * current size to free them up.
  854. */
  855. if (!error && (nimaps != 0) &&
  856. (imap.br_startblock != HOLESTARTBLOCK ||
  857. ip->i_delayed_blks)) {
  858. /*
  859. * Attach the dquots to the inode up front.
  860. */
  861. error = xfs_qm_dqattach(ip, 0);
  862. if (error)
  863. return error;
  864. /* wait on dio to ensure i_size has settled */
  865. inode_dio_wait(VFS_I(ip));
  866. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
  867. &tp);
  868. if (error) {
  869. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  870. return error;
  871. }
  872. xfs_ilock(ip, XFS_ILOCK_EXCL);
  873. xfs_trans_ijoin(tp, ip, 0);
  874. /*
  875. * Do not update the on-disk file size. If we update the
  876. * on-disk file size and then the system crashes before the
  877. * contents of the file are flushed to disk then the files
  878. * may be full of holes (ie NULL files bug).
  879. */
  880. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
  881. XFS_ISIZE(ip));
  882. if (error) {
  883. /*
  884. * If we get an error at this point we simply don't
  885. * bother truncating the file.
  886. */
  887. xfs_trans_cancel(tp);
  888. } else {
  889. error = xfs_trans_commit(tp);
  890. if (!error)
  891. xfs_inode_clear_eofblocks_tag(ip);
  892. }
  893. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  894. }
  895. return error;
  896. }
  897. int
  898. xfs_alloc_file_space(
  899. struct xfs_inode *ip,
  900. xfs_off_t offset,
  901. xfs_off_t len,
  902. int alloc_type)
  903. {
  904. xfs_mount_t *mp = ip->i_mount;
  905. xfs_off_t count;
  906. xfs_filblks_t allocated_fsb;
  907. xfs_filblks_t allocatesize_fsb;
  908. xfs_extlen_t extsz, temp;
  909. xfs_fileoff_t startoffset_fsb;
  910. xfs_fsblock_t firstfsb;
  911. int nimaps;
  912. int quota_flag;
  913. int rt;
  914. xfs_trans_t *tp;
  915. xfs_bmbt_irec_t imaps[1], *imapp;
  916. struct xfs_defer_ops dfops;
  917. uint qblocks, resblks, resrtextents;
  918. int error;
  919. trace_xfs_alloc_file_space(ip);
  920. if (XFS_FORCED_SHUTDOWN(mp))
  921. return -EIO;
  922. error = xfs_qm_dqattach(ip, 0);
  923. if (error)
  924. return error;
  925. if (len <= 0)
  926. return -EINVAL;
  927. rt = XFS_IS_REALTIME_INODE(ip);
  928. extsz = xfs_get_extsz_hint(ip);
  929. count = len;
  930. imapp = &imaps[0];
  931. nimaps = 1;
  932. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  933. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  934. /*
  935. * Allocate file space until done or until there is an error
  936. */
  937. while (allocatesize_fsb && !error) {
  938. xfs_fileoff_t s, e;
  939. /*
  940. * Determine space reservations for data/realtime.
  941. */
  942. if (unlikely(extsz)) {
  943. s = startoffset_fsb;
  944. do_div(s, extsz);
  945. s *= extsz;
  946. e = startoffset_fsb + allocatesize_fsb;
  947. if ((temp = do_mod(startoffset_fsb, extsz)))
  948. e += temp;
  949. if ((temp = do_mod(e, extsz)))
  950. e += extsz - temp;
  951. } else {
  952. s = 0;
  953. e = allocatesize_fsb;
  954. }
  955. /*
  956. * The transaction reservation is limited to a 32-bit block
  957. * count, hence we need to limit the number of blocks we are
  958. * trying to reserve to avoid an overflow. We can't allocate
  959. * more than @nimaps extents, and an extent is limited on disk
  960. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  961. */
  962. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  963. if (unlikely(rt)) {
  964. resrtextents = qblocks = resblks;
  965. resrtextents /= mp->m_sb.sb_rextsize;
  966. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  967. quota_flag = XFS_QMOPT_RES_RTBLKS;
  968. } else {
  969. resrtextents = 0;
  970. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  971. quota_flag = XFS_QMOPT_RES_REGBLKS;
  972. }
  973. /*
  974. * Allocate and setup the transaction.
  975. */
  976. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
  977. resrtextents, 0, &tp);
  978. /*
  979. * Check for running out of space
  980. */
  981. if (error) {
  982. /*
  983. * Free the transaction structure.
  984. */
  985. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  986. break;
  987. }
  988. xfs_ilock(ip, XFS_ILOCK_EXCL);
  989. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  990. 0, quota_flag);
  991. if (error)
  992. goto error1;
  993. xfs_trans_ijoin(tp, ip, 0);
  994. xfs_defer_init(&dfops, &firstfsb);
  995. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  996. allocatesize_fsb, alloc_type, &firstfsb,
  997. resblks, imapp, &nimaps, &dfops);
  998. if (error)
  999. goto error0;
  1000. /*
  1001. * Complete the transaction
  1002. */
  1003. error = xfs_defer_finish(&tp, &dfops, NULL);
  1004. if (error)
  1005. goto error0;
  1006. error = xfs_trans_commit(tp);
  1007. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1008. if (error)
  1009. break;
  1010. allocated_fsb = imapp->br_blockcount;
  1011. if (nimaps == 0) {
  1012. error = -ENOSPC;
  1013. break;
  1014. }
  1015. startoffset_fsb += allocated_fsb;
  1016. allocatesize_fsb -= allocated_fsb;
  1017. }
  1018. return error;
  1019. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  1020. xfs_defer_cancel(&dfops);
  1021. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  1022. error1: /* Just cancel transaction */
  1023. xfs_trans_cancel(tp);
  1024. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1025. return error;
  1026. }
  1027. static int
  1028. xfs_unmap_extent(
  1029. struct xfs_inode *ip,
  1030. xfs_fileoff_t startoffset_fsb,
  1031. xfs_filblks_t len_fsb,
  1032. int *done)
  1033. {
  1034. struct xfs_mount *mp = ip->i_mount;
  1035. struct xfs_trans *tp;
  1036. struct xfs_defer_ops dfops;
  1037. xfs_fsblock_t firstfsb;
  1038. uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1039. int error;
  1040. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
  1041. if (error) {
  1042. ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1043. return error;
  1044. }
  1045. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1046. error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
  1047. ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
  1048. if (error)
  1049. goto out_trans_cancel;
  1050. xfs_trans_ijoin(tp, ip, 0);
  1051. xfs_defer_init(&dfops, &firstfsb);
  1052. error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, &firstfsb,
  1053. &dfops, done);
  1054. if (error)
  1055. goto out_bmap_cancel;
  1056. error = xfs_defer_finish(&tp, &dfops, ip);
  1057. if (error)
  1058. goto out_bmap_cancel;
  1059. error = xfs_trans_commit(tp);
  1060. out_unlock:
  1061. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1062. return error;
  1063. out_bmap_cancel:
  1064. xfs_defer_cancel(&dfops);
  1065. out_trans_cancel:
  1066. xfs_trans_cancel(tp);
  1067. goto out_unlock;
  1068. }
  1069. static int
  1070. xfs_adjust_extent_unmap_boundaries(
  1071. struct xfs_inode *ip,
  1072. xfs_fileoff_t *startoffset_fsb,
  1073. xfs_fileoff_t *endoffset_fsb)
  1074. {
  1075. struct xfs_mount *mp = ip->i_mount;
  1076. struct xfs_bmbt_irec imap;
  1077. int nimap, error;
  1078. xfs_extlen_t mod = 0;
  1079. nimap = 1;
  1080. error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0);
  1081. if (error)
  1082. return error;
  1083. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1084. xfs_daddr_t block;
  1085. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1086. block = imap.br_startblock;
  1087. mod = do_div(block, mp->m_sb.sb_rextsize);
  1088. if (mod)
  1089. *startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  1090. }
  1091. nimap = 1;
  1092. error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0);
  1093. if (error)
  1094. return error;
  1095. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1096. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1097. mod++;
  1098. if (mod && mod != mp->m_sb.sb_rextsize)
  1099. *endoffset_fsb -= mod;
  1100. }
  1101. return 0;
  1102. }
  1103. static int
  1104. xfs_flush_unmap_range(
  1105. struct xfs_inode *ip,
  1106. xfs_off_t offset,
  1107. xfs_off_t len)
  1108. {
  1109. struct xfs_mount *mp = ip->i_mount;
  1110. struct inode *inode = VFS_I(ip);
  1111. xfs_off_t rounding, start, end;
  1112. int error;
  1113. /* wait for the completion of any pending DIOs */
  1114. inode_dio_wait(inode);
  1115. rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
  1116. start = round_down(offset, rounding);
  1117. end = round_up(offset + len, rounding) - 1;
  1118. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1119. if (error)
  1120. return error;
  1121. truncate_pagecache_range(inode, start, end);
  1122. return 0;
  1123. }
  1124. int
  1125. xfs_free_file_space(
  1126. struct xfs_inode *ip,
  1127. xfs_off_t offset,
  1128. xfs_off_t len)
  1129. {
  1130. struct xfs_mount *mp = ip->i_mount;
  1131. xfs_fileoff_t startoffset_fsb;
  1132. xfs_fileoff_t endoffset_fsb;
  1133. int done = 0, error;
  1134. trace_xfs_free_file_space(ip);
  1135. error = xfs_qm_dqattach(ip, 0);
  1136. if (error)
  1137. return error;
  1138. if (len <= 0) /* if nothing being freed */
  1139. return 0;
  1140. error = xfs_flush_unmap_range(ip, offset, len);
  1141. if (error)
  1142. return error;
  1143. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1144. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1145. /*
  1146. * Need to zero the stuff we're not freeing, on disk. If it's a RT file
  1147. * and we can't use unwritten extents then we actually need to ensure
  1148. * to zero the whole extent, otherwise we just need to take of block
  1149. * boundaries, and xfs_bunmapi will handle the rest.
  1150. */
  1151. if (XFS_IS_REALTIME_INODE(ip) &&
  1152. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1153. error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb,
  1154. &endoffset_fsb);
  1155. if (error)
  1156. return error;
  1157. }
  1158. if (endoffset_fsb > startoffset_fsb) {
  1159. while (!done) {
  1160. error = xfs_unmap_extent(ip, startoffset_fsb,
  1161. endoffset_fsb - startoffset_fsb, &done);
  1162. if (error)
  1163. return error;
  1164. }
  1165. }
  1166. /*
  1167. * Now that we've unmap all full blocks we'll have to zero out any
  1168. * partial block at the beginning and/or end. xfs_zero_range is
  1169. * smart enough to skip any holes, including those we just created,
  1170. * but we must take care not to zero beyond EOF and enlarge i_size.
  1171. */
  1172. if (offset >= XFS_ISIZE(ip))
  1173. return 0;
  1174. if (offset + len > XFS_ISIZE(ip))
  1175. len = XFS_ISIZE(ip) - offset;
  1176. return xfs_zero_range(ip, offset, len, NULL);
  1177. }
  1178. /*
  1179. * Preallocate and zero a range of a file. This mechanism has the allocation
  1180. * semantics of fallocate and in addition converts data in the range to zeroes.
  1181. */
  1182. int
  1183. xfs_zero_file_space(
  1184. struct xfs_inode *ip,
  1185. xfs_off_t offset,
  1186. xfs_off_t len)
  1187. {
  1188. struct xfs_mount *mp = ip->i_mount;
  1189. uint blksize;
  1190. int error;
  1191. trace_xfs_zero_file_space(ip);
  1192. blksize = 1 << mp->m_sb.sb_blocklog;
  1193. /*
  1194. * Punch a hole and prealloc the range. We use hole punch rather than
  1195. * unwritten extent conversion for two reasons:
  1196. *
  1197. * 1.) Hole punch handles partial block zeroing for us.
  1198. *
  1199. * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
  1200. * by virtue of the hole punch.
  1201. */
  1202. error = xfs_free_file_space(ip, offset, len);
  1203. if (error)
  1204. goto out;
  1205. error = xfs_alloc_file_space(ip, round_down(offset, blksize),
  1206. round_up(offset + len, blksize) -
  1207. round_down(offset, blksize),
  1208. XFS_BMAPI_PREALLOC);
  1209. out:
  1210. return error;
  1211. }
  1212. /*
  1213. * @next_fsb will keep track of the extent currently undergoing shift.
  1214. * @stop_fsb will keep track of the extent at which we have to stop.
  1215. * If we are shifting left, we will start with block (offset + len) and
  1216. * shift each extent till last extent.
  1217. * If we are shifting right, we will start with last extent inside file space
  1218. * and continue until we reach the block corresponding to offset.
  1219. */
  1220. static int
  1221. xfs_shift_file_space(
  1222. struct xfs_inode *ip,
  1223. xfs_off_t offset,
  1224. xfs_off_t len,
  1225. enum shift_direction direction)
  1226. {
  1227. int done = 0;
  1228. struct xfs_mount *mp = ip->i_mount;
  1229. struct xfs_trans *tp;
  1230. int error;
  1231. struct xfs_defer_ops dfops;
  1232. xfs_fsblock_t first_block;
  1233. xfs_fileoff_t stop_fsb;
  1234. xfs_fileoff_t next_fsb;
  1235. xfs_fileoff_t shift_fsb;
  1236. uint resblks;
  1237. ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
  1238. if (direction == SHIFT_LEFT) {
  1239. /*
  1240. * Reserve blocks to cover potential extent merges after left
  1241. * shift operations.
  1242. */
  1243. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1244. next_fsb = XFS_B_TO_FSB(mp, offset + len);
  1245. stop_fsb = XFS_B_TO_FSB(mp, VFS_I(ip)->i_size);
  1246. } else {
  1247. /*
  1248. * If right shift, delegate the work of initialization of
  1249. * next_fsb to xfs_bmap_shift_extent as it has ilock held.
  1250. */
  1251. resblks = 0;
  1252. next_fsb = NULLFSBLOCK;
  1253. stop_fsb = XFS_B_TO_FSB(mp, offset);
  1254. }
  1255. shift_fsb = XFS_B_TO_FSB(mp, len);
  1256. /*
  1257. * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
  1258. * into the accessible region of the file.
  1259. */
  1260. if (xfs_can_free_eofblocks(ip, true)) {
  1261. error = xfs_free_eofblocks(ip);
  1262. if (error)
  1263. return error;
  1264. }
  1265. /*
  1266. * Writeback and invalidate cache for the remainder of the file as we're
  1267. * about to shift down every extent from offset to EOF.
  1268. */
  1269. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  1270. offset, -1);
  1271. if (error)
  1272. return error;
  1273. error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
  1274. offset >> PAGE_SHIFT, -1);
  1275. if (error)
  1276. return error;
  1277. /*
  1278. * The extent shiting code works on extent granularity. So, if
  1279. * stop_fsb is not the starting block of extent, we need to split
  1280. * the extent at stop_fsb.
  1281. */
  1282. if (direction == SHIFT_RIGHT) {
  1283. error = xfs_bmap_split_extent(ip, stop_fsb);
  1284. if (error)
  1285. return error;
  1286. }
  1287. while (!error && !done) {
  1288. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0,
  1289. &tp);
  1290. if (error)
  1291. break;
  1292. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1293. error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
  1294. ip->i_gdquot, ip->i_pdquot, resblks, 0,
  1295. XFS_QMOPT_RES_REGBLKS);
  1296. if (error)
  1297. goto out_trans_cancel;
  1298. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1299. xfs_defer_init(&dfops, &first_block);
  1300. /*
  1301. * We are using the write transaction in which max 2 bmbt
  1302. * updates are allowed
  1303. */
  1304. error = xfs_bmap_shift_extents(tp, ip, &next_fsb, shift_fsb,
  1305. &done, stop_fsb, &first_block, &dfops,
  1306. direction, XFS_BMAP_MAX_SHIFT_EXTENTS);
  1307. if (error)
  1308. goto out_bmap_cancel;
  1309. error = xfs_defer_finish(&tp, &dfops, NULL);
  1310. if (error)
  1311. goto out_bmap_cancel;
  1312. error = xfs_trans_commit(tp);
  1313. }
  1314. return error;
  1315. out_bmap_cancel:
  1316. xfs_defer_cancel(&dfops);
  1317. out_trans_cancel:
  1318. xfs_trans_cancel(tp);
  1319. return error;
  1320. }
  1321. /*
  1322. * xfs_collapse_file_space()
  1323. * This routine frees disk space and shift extent for the given file.
  1324. * The first thing we do is to free data blocks in the specified range
  1325. * by calling xfs_free_file_space(). It would also sync dirty data
  1326. * and invalidate page cache over the region on which collapse range
  1327. * is working. And Shift extent records to the left to cover a hole.
  1328. * RETURNS:
  1329. * 0 on success
  1330. * errno on error
  1331. *
  1332. */
  1333. int
  1334. xfs_collapse_file_space(
  1335. struct xfs_inode *ip,
  1336. xfs_off_t offset,
  1337. xfs_off_t len)
  1338. {
  1339. int error;
  1340. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1341. trace_xfs_collapse_file_space(ip);
  1342. error = xfs_free_file_space(ip, offset, len);
  1343. if (error)
  1344. return error;
  1345. return xfs_shift_file_space(ip, offset, len, SHIFT_LEFT);
  1346. }
  1347. /*
  1348. * xfs_insert_file_space()
  1349. * This routine create hole space by shifting extents for the given file.
  1350. * The first thing we do is to sync dirty data and invalidate page cache
  1351. * over the region on which insert range is working. And split an extent
  1352. * to two extents at given offset by calling xfs_bmap_split_extent.
  1353. * And shift all extent records which are laying between [offset,
  1354. * last allocated extent] to the right to reserve hole range.
  1355. * RETURNS:
  1356. * 0 on success
  1357. * errno on error
  1358. */
  1359. int
  1360. xfs_insert_file_space(
  1361. struct xfs_inode *ip,
  1362. loff_t offset,
  1363. loff_t len)
  1364. {
  1365. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1366. trace_xfs_insert_file_space(ip);
  1367. return xfs_shift_file_space(ip, offset, len, SHIFT_RIGHT);
  1368. }
  1369. /*
  1370. * We need to check that the format of the data fork in the temporary inode is
  1371. * valid for the target inode before doing the swap. This is not a problem with
  1372. * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
  1373. * data fork depending on the space the attribute fork is taking so we can get
  1374. * invalid formats on the target inode.
  1375. *
  1376. * E.g. target has space for 7 extents in extent format, temp inode only has
  1377. * space for 6. If we defragment down to 7 extents, then the tmp format is a
  1378. * btree, but when swapped it needs to be in extent format. Hence we can't just
  1379. * blindly swap data forks on attr2 filesystems.
  1380. *
  1381. * Note that we check the swap in both directions so that we don't end up with
  1382. * a corrupt temporary inode, either.
  1383. *
  1384. * Note that fixing the way xfs_fsr sets up the attribute fork in the source
  1385. * inode will prevent this situation from occurring, so all we do here is
  1386. * reject and log the attempt. basically we are putting the responsibility on
  1387. * userspace to get this right.
  1388. */
  1389. static int
  1390. xfs_swap_extents_check_format(
  1391. struct xfs_inode *ip, /* target inode */
  1392. struct xfs_inode *tip) /* tmp inode */
  1393. {
  1394. /* Should never get a local format */
  1395. if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
  1396. tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  1397. return -EINVAL;
  1398. /*
  1399. * if the target inode has less extents that then temporary inode then
  1400. * why did userspace call us?
  1401. */
  1402. if (ip->i_d.di_nextents < tip->i_d.di_nextents)
  1403. return -EINVAL;
  1404. /*
  1405. * If we have to use the (expensive) rmap swap method, we can
  1406. * handle any number of extents and any format.
  1407. */
  1408. if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb))
  1409. return 0;
  1410. /*
  1411. * if the target inode is in extent form and the temp inode is in btree
  1412. * form then we will end up with the target inode in the wrong format
  1413. * as we already know there are less extents in the temp inode.
  1414. */
  1415. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1416. tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  1417. return -EINVAL;
  1418. /* Check temp in extent form to max in target */
  1419. if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1420. XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
  1421. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1422. return -EINVAL;
  1423. /* Check target in extent form to max in temp */
  1424. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1425. XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
  1426. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1427. return -EINVAL;
  1428. /*
  1429. * If we are in a btree format, check that the temp root block will fit
  1430. * in the target and that it has enough extents to be in btree format
  1431. * in the target.
  1432. *
  1433. * Note that we have to be careful to allow btree->extent conversions
  1434. * (a common defrag case) which will occur when the temp inode is in
  1435. * extent format...
  1436. */
  1437. if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1438. if (XFS_IFORK_BOFF(ip) &&
  1439. XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
  1440. return -EINVAL;
  1441. if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
  1442. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  1443. return -EINVAL;
  1444. }
  1445. /* Reciprocal target->temp btree format checks */
  1446. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1447. if (XFS_IFORK_BOFF(tip) &&
  1448. XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
  1449. return -EINVAL;
  1450. if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
  1451. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  1452. return -EINVAL;
  1453. }
  1454. return 0;
  1455. }
  1456. static int
  1457. xfs_swap_extent_flush(
  1458. struct xfs_inode *ip)
  1459. {
  1460. int error;
  1461. error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
  1462. if (error)
  1463. return error;
  1464. truncate_pagecache_range(VFS_I(ip), 0, -1);
  1465. /* Verify O_DIRECT for ftmp */
  1466. if (VFS_I(ip)->i_mapping->nrpages)
  1467. return -EINVAL;
  1468. return 0;
  1469. }
  1470. /*
  1471. * Move extents from one file to another, when rmap is enabled.
  1472. */
  1473. STATIC int
  1474. xfs_swap_extent_rmap(
  1475. struct xfs_trans **tpp,
  1476. struct xfs_inode *ip,
  1477. struct xfs_inode *tip)
  1478. {
  1479. struct xfs_bmbt_irec irec;
  1480. struct xfs_bmbt_irec uirec;
  1481. struct xfs_bmbt_irec tirec;
  1482. xfs_fileoff_t offset_fsb;
  1483. xfs_fileoff_t end_fsb;
  1484. xfs_filblks_t count_fsb;
  1485. xfs_fsblock_t firstfsb;
  1486. struct xfs_defer_ops dfops;
  1487. int error;
  1488. xfs_filblks_t ilen;
  1489. xfs_filblks_t rlen;
  1490. int nimaps;
  1491. __uint64_t tip_flags2;
  1492. /*
  1493. * If the source file has shared blocks, we must flag the donor
  1494. * file as having shared blocks so that we get the shared-block
  1495. * rmap functions when we go to fix up the rmaps. The flags
  1496. * will be switch for reals later.
  1497. */
  1498. tip_flags2 = tip->i_d.di_flags2;
  1499. if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)
  1500. tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
  1501. offset_fsb = 0;
  1502. end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
  1503. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  1504. while (count_fsb) {
  1505. /* Read extent from the donor file */
  1506. nimaps = 1;
  1507. error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
  1508. &nimaps, 0);
  1509. if (error)
  1510. goto out;
  1511. ASSERT(nimaps == 1);
  1512. ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
  1513. trace_xfs_swap_extent_rmap_remap(tip, &tirec);
  1514. ilen = tirec.br_blockcount;
  1515. /* Unmap the old blocks in the source file. */
  1516. while (tirec.br_blockcount) {
  1517. xfs_defer_init(&dfops, &firstfsb);
  1518. trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
  1519. /* Read extent from the source file */
  1520. nimaps = 1;
  1521. error = xfs_bmapi_read(ip, tirec.br_startoff,
  1522. tirec.br_blockcount, &irec,
  1523. &nimaps, 0);
  1524. if (error)
  1525. goto out_defer;
  1526. ASSERT(nimaps == 1);
  1527. ASSERT(tirec.br_startoff == irec.br_startoff);
  1528. trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
  1529. /* Trim the extent. */
  1530. uirec = tirec;
  1531. uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
  1532. tirec.br_blockcount,
  1533. irec.br_blockcount);
  1534. trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
  1535. /* Remove the mapping from the donor file. */
  1536. error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
  1537. tip, &uirec);
  1538. if (error)
  1539. goto out_defer;
  1540. /* Remove the mapping from the source file. */
  1541. error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
  1542. ip, &irec);
  1543. if (error)
  1544. goto out_defer;
  1545. /* Map the donor file's blocks into the source file. */
  1546. error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
  1547. ip, &uirec);
  1548. if (error)
  1549. goto out_defer;
  1550. /* Map the source file's blocks into the donor file. */
  1551. error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
  1552. tip, &irec);
  1553. if (error)
  1554. goto out_defer;
  1555. error = xfs_defer_finish(tpp, &dfops, ip);
  1556. if (error)
  1557. goto out_defer;
  1558. tirec.br_startoff += rlen;
  1559. if (tirec.br_startblock != HOLESTARTBLOCK &&
  1560. tirec.br_startblock != DELAYSTARTBLOCK)
  1561. tirec.br_startblock += rlen;
  1562. tirec.br_blockcount -= rlen;
  1563. }
  1564. /* Roll on... */
  1565. count_fsb -= ilen;
  1566. offset_fsb += ilen;
  1567. }
  1568. tip->i_d.di_flags2 = tip_flags2;
  1569. return 0;
  1570. out_defer:
  1571. xfs_defer_cancel(&dfops);
  1572. out:
  1573. trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
  1574. tip->i_d.di_flags2 = tip_flags2;
  1575. return error;
  1576. }
  1577. /* Swap the extents of two files by swapping data forks. */
  1578. STATIC int
  1579. xfs_swap_extent_forks(
  1580. struct xfs_trans *tp,
  1581. struct xfs_inode *ip,
  1582. struct xfs_inode *tip,
  1583. int *src_log_flags,
  1584. int *target_log_flags)
  1585. {
  1586. struct xfs_ifork tempifp, *ifp, *tifp;
  1587. int aforkblks = 0;
  1588. int taforkblks = 0;
  1589. xfs_extnum_t nextents;
  1590. __uint64_t tmp;
  1591. int error;
  1592. /*
  1593. * Count the number of extended attribute blocks
  1594. */
  1595. if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
  1596. (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1597. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK,
  1598. &aforkblks);
  1599. if (error)
  1600. return error;
  1601. }
  1602. if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
  1603. (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  1604. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
  1605. &taforkblks);
  1606. if (error)
  1607. return error;
  1608. }
  1609. /*
  1610. * Before we've swapped the forks, lets set the owners of the forks
  1611. * appropriately. We have to do this as we are demand paging the btree
  1612. * buffers, and so the validation done on read will expect the owner
  1613. * field to be correctly set. Once we change the owners, we can swap the
  1614. * inode forks.
  1615. */
  1616. if (ip->i_d.di_version == 3 &&
  1617. ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1618. (*target_log_flags) |= XFS_ILOG_DOWNER;
  1619. error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK,
  1620. tip->i_ino, NULL);
  1621. if (error)
  1622. return error;
  1623. }
  1624. if (tip->i_d.di_version == 3 &&
  1625. tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  1626. (*src_log_flags) |= XFS_ILOG_DOWNER;
  1627. error = xfs_bmbt_change_owner(tp, tip, XFS_DATA_FORK,
  1628. ip->i_ino, NULL);
  1629. if (error)
  1630. return error;
  1631. }
  1632. /*
  1633. * Swap the data forks of the inodes
  1634. */
  1635. ifp = &ip->i_df;
  1636. tifp = &tip->i_df;
  1637. tempifp = *ifp; /* struct copy */
  1638. *ifp = *tifp; /* struct copy */
  1639. *tifp = tempifp; /* struct copy */
  1640. /*
  1641. * Fix the on-disk inode values
  1642. */
  1643. tmp = (__uint64_t)ip->i_d.di_nblocks;
  1644. ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
  1645. tip->i_d.di_nblocks = tmp + taforkblks

Large files files are truncated, but you can click here to view the full file