xfs_reflink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Copyright (C) 2016 Oracle. All Rights Reserved.
  3. *
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  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
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_shared.h"
  23. #include "xfs_format.h"
  24. #include "xfs_log_format.h"
  25. #include "xfs_trans_resv.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_da_format.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_trans.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_bmap_util.h"
  35. #include "xfs_error.h"
  36. #include "xfs_dir2.h"
  37. #include "xfs_dir2_priv.h"
  38. #include "xfs_ioctl.h"
  39. #include "xfs_trace.h"
  40. #include "xfs_log.h"
  41. #include "xfs_icache.h"
  42. #include "xfs_pnfs.h"
  43. #include "xfs_refcount_btree.h"
  44. #include "xfs_refcount.h"
  45. #include "xfs_bmap_btree.h"
  46. #include "xfs_trans_space.h"
  47. #include "xfs_bit.h"
  48. #include "xfs_alloc.h"
  49. #include "xfs_quota_defs.h"
  50. #include "xfs_quota.h"
  51. #include "xfs_btree.h"
  52. #include "xfs_bmap_btree.h"
  53. #include "xfs_reflink.h"
  54. #include "xfs_iomap.h"
  55. #include "xfs_rmap_btree.h"
  56. /*
  57. * Copy on Write of Shared Blocks
  58. *
  59. * XFS must preserve "the usual" file semantics even when two files share
  60. * the same physical blocks. This means that a write to one file must not
  61. * alter the blocks in a different file; the way that we'll do that is
  62. * through the use of a copy-on-write mechanism. At a high level, that
  63. * means that when we want to write to a shared block, we allocate a new
  64. * block, write the data to the new block, and if that succeeds we map the
  65. * new block into the file.
  66. *
  67. * XFS provides a "delayed allocation" mechanism that defers the allocation
  68. * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
  69. * possible. This reduces fragmentation by enabling the filesystem to ask
  70. * for bigger chunks less often, which is exactly what we want for CoW.
  71. *
  72. * The delalloc mechanism begins when the kernel wants to make a block
  73. * writable (write_begin or page_mkwrite). If the offset is not mapped, we
  74. * create a delalloc mapping, which is a regular in-core extent, but without
  75. * a real startblock. (For delalloc mappings, the startblock encodes both
  76. * a flag that this is a delalloc mapping, and a worst-case estimate of how
  77. * many blocks might be required to put the mapping into the BMBT.) delalloc
  78. * mappings are a reservation against the free space in the filesystem;
  79. * adjacent mappings can also be combined into fewer larger mappings.
  80. *
  81. * When dirty pages are being written out (typically in writepage), the
  82. * delalloc reservations are converted into real mappings by allocating
  83. * blocks and replacing the delalloc mapping with real ones. A delalloc
  84. * mapping can be replaced by several real ones if the free space is
  85. * fragmented.
  86. *
  87. * We want to adapt the delalloc mechanism for copy-on-write, since the
  88. * write paths are similar. The first two steps (creating the reservation
  89. * and allocating the blocks) are exactly the same as delalloc except that
  90. * the mappings must be stored in a separate CoW fork because we do not want
  91. * to disturb the mapping in the data fork until we're sure that the write
  92. * succeeded. IO completion in this case is the process of removing the old
  93. * mapping from the data fork and moving the new mapping from the CoW fork to
  94. * the data fork. This will be discussed shortly.
  95. *
  96. * For now, unaligned directio writes will be bounced back to the page cache.
  97. * Block-aligned directio writes will use the same mechanism as buffered
  98. * writes.
  99. *
  100. * CoW remapping must be done after the data block write completes,
  101. * because we don't want to destroy the old data fork map until we're sure
  102. * the new block has been written. Since the new mappings are kept in a
  103. * separate fork, we can simply iterate these mappings to find the ones
  104. * that cover the file blocks that we just CoW'd. For each extent, simply
  105. * unmap the corresponding range in the data fork, map the new range into
  106. * the data fork, and remove the extent from the CoW fork.
  107. *
  108. * Since the remapping operation can be applied to an arbitrary file
  109. * range, we record the need for the remap step as a flag in the ioend
  110. * instead of declaring a new IO type. This is required for direct io
  111. * because we only have ioend for the whole dio, and we have to be able to
  112. * remember the presence of unwritten blocks and CoW blocks with a single
  113. * ioend structure. Better yet, the more ground we can cover with one
  114. * ioend, the better.
  115. */
  116. /*
  117. * Given an AG extent, find the lowest-numbered run of shared blocks
  118. * within that range and return the range in fbno/flen. If
  119. * find_end_of_shared is true, return the longest contiguous extent of
  120. * shared blocks. If there are no shared extents, fbno and flen will
  121. * be set to NULLAGBLOCK and 0, respectively.
  122. */
  123. int
  124. xfs_reflink_find_shared(
  125. struct xfs_mount *mp,
  126. xfs_agnumber_t agno,
  127. xfs_agblock_t agbno,
  128. xfs_extlen_t aglen,
  129. xfs_agblock_t *fbno,
  130. xfs_extlen_t *flen,
  131. bool find_end_of_shared)
  132. {
  133. struct xfs_buf *agbp;
  134. struct xfs_btree_cur *cur;
  135. int error;
  136. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  137. if (error)
  138. return error;
  139. cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
  140. error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
  141. find_end_of_shared);
  142. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  143. xfs_buf_relse(agbp);
  144. return error;
  145. }
  146. /*
  147. * Trim the mapping to the next block where there's a change in the
  148. * shared/unshared status. More specifically, this means that we
  149. * find the lowest-numbered extent of shared blocks that coincides with
  150. * the given block mapping. If the shared extent overlaps the start of
  151. * the mapping, trim the mapping to the end of the shared extent. If
  152. * the shared region intersects the mapping, trim the mapping to the
  153. * start of the shared extent. If there are no shared regions that
  154. * overlap, just return the original extent.
  155. */
  156. int
  157. xfs_reflink_trim_around_shared(
  158. struct xfs_inode *ip,
  159. struct xfs_bmbt_irec *irec,
  160. bool *shared,
  161. bool *trimmed)
  162. {
  163. xfs_agnumber_t agno;
  164. xfs_agblock_t agbno;
  165. xfs_extlen_t aglen;
  166. xfs_agblock_t fbno;
  167. xfs_extlen_t flen;
  168. int error = 0;
  169. /* Holes, unwritten, and delalloc extents cannot be shared */
  170. if (!xfs_is_reflink_inode(ip) ||
  171. ISUNWRITTEN(irec) ||
  172. irec->br_startblock == HOLESTARTBLOCK ||
  173. irec->br_startblock == DELAYSTARTBLOCK) {
  174. *shared = false;
  175. return 0;
  176. }
  177. trace_xfs_reflink_trim_around_shared(ip, irec);
  178. agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
  179. agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
  180. aglen = irec->br_blockcount;
  181. error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
  182. aglen, &fbno, &flen, true);
  183. if (error)
  184. return error;
  185. *shared = *trimmed = false;
  186. if (fbno == NULLAGBLOCK) {
  187. /* No shared blocks at all. */
  188. return 0;
  189. } else if (fbno == agbno) {
  190. /*
  191. * The start of this extent is shared. Truncate the
  192. * mapping at the end of the shared region so that a
  193. * subsequent iteration starts at the start of the
  194. * unshared region.
  195. */
  196. irec->br_blockcount = flen;
  197. *shared = true;
  198. if (flen != aglen)
  199. *trimmed = true;
  200. return 0;
  201. } else {
  202. /*
  203. * There's a shared extent midway through this extent.
  204. * Truncate the mapping at the start of the shared
  205. * extent so that a subsequent iteration starts at the
  206. * start of the shared region.
  207. */
  208. irec->br_blockcount = fbno - agbno;
  209. *trimmed = true;
  210. return 0;
  211. }
  212. }
  213. /* Create a CoW reservation for a range of blocks within a file. */
  214. static int
  215. __xfs_reflink_reserve_cow(
  216. struct xfs_inode *ip,
  217. xfs_fileoff_t *offset_fsb,
  218. xfs_fileoff_t end_fsb)
  219. {
  220. struct xfs_bmbt_irec got, prev, imap;
  221. xfs_fileoff_t orig_end_fsb;
  222. int nimaps, eof = 0, error = 0;
  223. bool shared = false, trimmed = false;
  224. xfs_extnum_t idx;
  225. /* Already reserved? Skip the refcount btree access. */
  226. xfs_bmap_search_extents(ip, *offset_fsb, XFS_COW_FORK, &eof, &idx,
  227. &got, &prev);
  228. if (!eof && got.br_startoff <= *offset_fsb) {
  229. end_fsb = orig_end_fsb = got.br_startoff + got.br_blockcount;
  230. trace_xfs_reflink_cow_found(ip, &got);
  231. goto done;
  232. }
  233. /* Read extent from the source file. */
  234. nimaps = 1;
  235. error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
  236. &imap, &nimaps, 0);
  237. if (error)
  238. goto out_unlock;
  239. ASSERT(nimaps == 1);
  240. /* Trim the mapping to the nearest shared extent boundary. */
  241. error = xfs_reflink_trim_around_shared(ip, &imap, &shared, &trimmed);
  242. if (error)
  243. goto out_unlock;
  244. end_fsb = orig_end_fsb = imap.br_startoff + imap.br_blockcount;
  245. /* Not shared? Just report the (potentially capped) extent. */
  246. if (!shared)
  247. goto done;
  248. /*
  249. * Fork all the shared blocks from our write offset until the end of
  250. * the extent.
  251. */
  252. error = xfs_qm_dqattach_locked(ip, 0);
  253. if (error)
  254. goto out_unlock;
  255. retry:
  256. error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, *offset_fsb,
  257. end_fsb - *offset_fsb, &got,
  258. &prev, &idx, eof);
  259. switch (error) {
  260. case 0:
  261. break;
  262. case -ENOSPC:
  263. case -EDQUOT:
  264. /* retry without any preallocation */
  265. trace_xfs_reflink_cow_enospc(ip, &imap);
  266. if (end_fsb != orig_end_fsb) {
  267. end_fsb = orig_end_fsb;
  268. goto retry;
  269. }
  270. /*FALLTHRU*/
  271. default:
  272. goto out_unlock;
  273. }
  274. trace_xfs_reflink_cow_alloc(ip, &got);
  275. done:
  276. *offset_fsb = end_fsb;
  277. out_unlock:
  278. return error;
  279. }
  280. /* Create a CoW reservation for part of a file. */
  281. int
  282. xfs_reflink_reserve_cow_range(
  283. struct xfs_inode *ip,
  284. xfs_off_t offset,
  285. xfs_off_t count)
  286. {
  287. struct xfs_mount *mp = ip->i_mount;
  288. xfs_fileoff_t offset_fsb, end_fsb;
  289. int error;
  290. trace_xfs_reflink_reserve_cow_range(ip, offset, count);
  291. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  292. end_fsb = XFS_B_TO_FSB(mp, offset + count);
  293. xfs_ilock(ip, XFS_ILOCK_EXCL);
  294. while (offset_fsb < end_fsb) {
  295. error = __xfs_reflink_reserve_cow(ip, &offset_fsb, end_fsb);
  296. if (error) {
  297. trace_xfs_reflink_reserve_cow_range_error(ip, error,
  298. _RET_IP_);
  299. break;
  300. }
  301. }
  302. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  303. return error;
  304. }
  305. /*
  306. * Find the CoW reservation (and whether or not it needs block allocation)
  307. * for a given byte offset of a file.
  308. */
  309. bool
  310. xfs_reflink_find_cow_mapping(
  311. struct xfs_inode *ip,
  312. xfs_off_t offset,
  313. struct xfs_bmbt_irec *imap,
  314. bool *need_alloc)
  315. {
  316. struct xfs_bmbt_irec irec;
  317. struct xfs_ifork *ifp;
  318. struct xfs_bmbt_rec_host *gotp;
  319. xfs_fileoff_t bno;
  320. xfs_extnum_t idx;
  321. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
  322. ASSERT(xfs_is_reflink_inode(ip));
  323. /* Find the extent in the CoW fork. */
  324. ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  325. bno = XFS_B_TO_FSBT(ip->i_mount, offset);
  326. gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
  327. if (!gotp)
  328. return false;
  329. xfs_bmbt_get_all(gotp, &irec);
  330. if (bno >= irec.br_startoff + irec.br_blockcount ||
  331. bno < irec.br_startoff)
  332. return false;
  333. trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
  334. &irec);
  335. /* If it's still delalloc, we must allocate later. */
  336. *imap = irec;
  337. *need_alloc = !!(isnullstartblock(irec.br_startblock));
  338. return true;
  339. }
  340. /*
  341. * Trim an extent to end at the next CoW reservation past offset_fsb.
  342. */
  343. int
  344. xfs_reflink_trim_irec_to_next_cow(
  345. struct xfs_inode *ip,
  346. xfs_fileoff_t offset_fsb,
  347. struct xfs_bmbt_irec *imap)
  348. {
  349. struct xfs_bmbt_irec irec;
  350. struct xfs_ifork *ifp;
  351. struct xfs_bmbt_rec_host *gotp;
  352. xfs_extnum_t idx;
  353. if (!xfs_is_reflink_inode(ip))
  354. return 0;
  355. /* Find the extent in the CoW fork. */
  356. ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  357. gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
  358. if (!gotp)
  359. return 0;
  360. xfs_bmbt_get_all(gotp, &irec);
  361. /* This is the extent before; try sliding up one. */
  362. if (irec.br_startoff < offset_fsb) {
  363. idx++;
  364. if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
  365. return 0;
  366. gotp = xfs_iext_get_ext(ifp, idx);
  367. xfs_bmbt_get_all(gotp, &irec);
  368. }
  369. if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
  370. return 0;
  371. imap->br_blockcount = irec.br_startoff - imap->br_startoff;
  372. trace_xfs_reflink_trim_irec(ip, imap);
  373. return 0;
  374. }
  375. /*
  376. * Cancel all pending CoW reservations for some block range of an inode.
  377. */
  378. int
  379. xfs_reflink_cancel_cow_blocks(
  380. struct xfs_inode *ip,
  381. struct xfs_trans **tpp,
  382. xfs_fileoff_t offset_fsb,
  383. xfs_fileoff_t end_fsb)
  384. {
  385. struct xfs_bmbt_irec irec;
  386. xfs_filblks_t count_fsb;
  387. xfs_fsblock_t firstfsb;
  388. struct xfs_defer_ops dfops;
  389. int error = 0;
  390. int nimaps;
  391. if (!xfs_is_reflink_inode(ip))
  392. return 0;
  393. /* Go find the old extent in the CoW fork. */
  394. while (offset_fsb < end_fsb) {
  395. nimaps = 1;
  396. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  397. error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
  398. &nimaps, XFS_BMAPI_COWFORK);
  399. if (error)
  400. break;
  401. ASSERT(nimaps == 1);
  402. trace_xfs_reflink_cancel_cow(ip, &irec);
  403. if (irec.br_startblock == DELAYSTARTBLOCK) {
  404. /* Free a delayed allocation. */
  405. xfs_mod_fdblocks(ip->i_mount, irec.br_blockcount,
  406. false);
  407. ip->i_delayed_blks -= irec.br_blockcount;
  408. /* Remove the mapping from the CoW fork. */
  409. error = xfs_bunmapi_cow(ip, &irec);
  410. if (error)
  411. break;
  412. } else if (irec.br_startblock == HOLESTARTBLOCK) {
  413. /* empty */
  414. } else {
  415. xfs_trans_ijoin(*tpp, ip, 0);
  416. xfs_defer_init(&dfops, &firstfsb);
  417. xfs_bmap_add_free(ip->i_mount, &dfops,
  418. irec.br_startblock, irec.br_blockcount,
  419. NULL);
  420. /* Update quota accounting */
  421. xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
  422. -(long)irec.br_blockcount);
  423. /* Roll the transaction */
  424. error = xfs_defer_finish(tpp, &dfops, ip);
  425. if (error) {
  426. xfs_defer_cancel(&dfops);
  427. break;
  428. }
  429. /* Remove the mapping from the CoW fork. */
  430. error = xfs_bunmapi_cow(ip, &irec);
  431. if (error)
  432. break;
  433. }
  434. /* Roll on... */
  435. offset_fsb = irec.br_startoff + irec.br_blockcount;
  436. }
  437. return error;
  438. }
  439. /*
  440. * Cancel all pending CoW reservations for some byte range of an inode.
  441. */
  442. int
  443. xfs_reflink_cancel_cow_range(
  444. struct xfs_inode *ip,
  445. xfs_off_t offset,
  446. xfs_off_t count)
  447. {
  448. struct xfs_trans *tp;
  449. xfs_fileoff_t offset_fsb;
  450. xfs_fileoff_t end_fsb;
  451. int error;
  452. trace_xfs_reflink_cancel_cow_range(ip, offset, count);
  453. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  454. if (count == NULLFILEOFF)
  455. end_fsb = NULLFILEOFF;
  456. else
  457. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  458. /* Start a rolling transaction to remove the mappings */
  459. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  460. 0, 0, 0, &tp);
  461. if (error)
  462. goto out;
  463. xfs_ilock(ip, XFS_ILOCK_EXCL);
  464. xfs_trans_ijoin(tp, ip, 0);
  465. /* Scrape out the old CoW reservations */
  466. error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
  467. if (error)
  468. goto out_cancel;
  469. error = xfs_trans_commit(tp);
  470. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  471. return error;
  472. out_cancel:
  473. xfs_trans_cancel(tp);
  474. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  475. out:
  476. trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
  477. return error;
  478. }
  479. /*
  480. * Remap parts of a file's data fork after a successful CoW.
  481. */
  482. int
  483. xfs_reflink_end_cow(
  484. struct xfs_inode *ip,
  485. xfs_off_t offset,
  486. xfs_off_t count)
  487. {
  488. struct xfs_bmbt_irec irec;
  489. struct xfs_bmbt_irec uirec;
  490. struct xfs_trans *tp;
  491. xfs_fileoff_t offset_fsb;
  492. xfs_fileoff_t end_fsb;
  493. xfs_filblks_t count_fsb;
  494. xfs_fsblock_t firstfsb;
  495. struct xfs_defer_ops dfops;
  496. int error;
  497. unsigned int resblks;
  498. xfs_filblks_t ilen;
  499. xfs_filblks_t rlen;
  500. int nimaps;
  501. trace_xfs_reflink_end_cow(ip, offset, count);
  502. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  503. end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
  504. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  505. /* Start a rolling transaction to switch the mappings */
  506. resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
  507. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
  508. resblks, 0, 0, &tp);
  509. if (error)
  510. goto out;
  511. xfs_ilock(ip, XFS_ILOCK_EXCL);
  512. xfs_trans_ijoin(tp, ip, 0);
  513. /* Go find the old extent in the CoW fork. */
  514. while (offset_fsb < end_fsb) {
  515. /* Read extent from the source file */
  516. nimaps = 1;
  517. count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
  518. error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
  519. &nimaps, XFS_BMAPI_COWFORK);
  520. if (error)
  521. goto out_cancel;
  522. ASSERT(nimaps == 1);
  523. ASSERT(irec.br_startblock != DELAYSTARTBLOCK);
  524. trace_xfs_reflink_cow_remap(ip, &irec);
  525. /*
  526. * We can have a hole in the CoW fork if part of a directio
  527. * write is CoW but part of it isn't.
  528. */
  529. rlen = ilen = irec.br_blockcount;
  530. if (irec.br_startblock == HOLESTARTBLOCK)
  531. goto next_extent;
  532. /* Unmap the old blocks in the data fork. */
  533. while (rlen) {
  534. xfs_defer_init(&dfops, &firstfsb);
  535. error = __xfs_bunmapi(tp, ip, irec.br_startoff,
  536. &rlen, 0, 1, &firstfsb, &dfops);
  537. if (error)
  538. goto out_defer;
  539. /*
  540. * Trim the extent to whatever got unmapped.
  541. * Remember, bunmapi works backwards.
  542. */
  543. uirec.br_startblock = irec.br_startblock + rlen;
  544. uirec.br_startoff = irec.br_startoff + rlen;
  545. uirec.br_blockcount = irec.br_blockcount - rlen;
  546. irec.br_blockcount = rlen;
  547. trace_xfs_reflink_cow_remap_piece(ip, &uirec);
  548. /* Map the new blocks into the data fork. */
  549. error = xfs_bmap_map_extent(tp->t_mountp, &dfops,
  550. ip, &uirec);
  551. if (error)
  552. goto out_defer;
  553. /* Remove the mapping from the CoW fork. */
  554. error = xfs_bunmapi_cow(ip, &uirec);
  555. if (error)
  556. goto out_defer;
  557. error = xfs_defer_finish(&tp, &dfops, ip);
  558. if (error)
  559. goto out_defer;
  560. }
  561. next_extent:
  562. /* Roll on... */
  563. offset_fsb = irec.br_startoff + ilen;
  564. }
  565. error = xfs_trans_commit(tp);
  566. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  567. if (error)
  568. goto out;
  569. return 0;
  570. out_defer:
  571. xfs_defer_cancel(&dfops);
  572. out_cancel:
  573. xfs_trans_cancel(tp);
  574. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  575. out:
  576. trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
  577. return error;
  578. }