xfs_exchmaps.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2020-2024 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_defer.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_bmap.h"
  17. #include "xfs_icache.h"
  18. #include "xfs_quota.h"
  19. #include "xfs_exchmaps.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_bmap_btree.h"
  22. #include "xfs_trans_space.h"
  23. #include "xfs_error.h"
  24. #include "xfs_errortag.h"
  25. #include "xfs_health.h"
  26. #include "xfs_exchmaps_item.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_attr_leaf.h"
  30. #include "xfs_attr.h"
  31. #include "xfs_dir2_priv.h"
  32. #include "xfs_dir2.h"
  33. #include "xfs_symlink_remote.h"
  34. struct kmem_cache *xfs_exchmaps_intent_cache;
  35. /* bmbt mappings adjacent to a pair of records. */
  36. struct xfs_exchmaps_adjacent {
  37. struct xfs_bmbt_irec left1;
  38. struct xfs_bmbt_irec right1;
  39. struct xfs_bmbt_irec left2;
  40. struct xfs_bmbt_irec right2;
  41. };
  42. #define ADJACENT_INIT { \
  43. .left1 = { .br_startblock = HOLESTARTBLOCK }, \
  44. .right1 = { .br_startblock = HOLESTARTBLOCK }, \
  45. .left2 = { .br_startblock = HOLESTARTBLOCK }, \
  46. .right2 = { .br_startblock = HOLESTARTBLOCK }, \
  47. }
  48. /* Information to reset reflink flag / CoW fork state after an exchange. */
  49. /*
  50. * If the reflink flag is set on either inode, make sure it has an incore CoW
  51. * fork, since all reflink inodes must have them. If there's a CoW fork and it
  52. * has mappings in it, make sure the inodes are tagged appropriately so that
  53. * speculative preallocations can be GC'd if we run low of space.
  54. */
  55. static inline void
  56. xfs_exchmaps_ensure_cowfork(
  57. struct xfs_inode *ip)
  58. {
  59. struct xfs_ifork *cfork;
  60. if (xfs_is_reflink_inode(ip))
  61. xfs_ifork_init_cow(ip);
  62. cfork = xfs_ifork_ptr(ip, XFS_COW_FORK);
  63. if (!cfork)
  64. return;
  65. if (cfork->if_bytes > 0)
  66. xfs_inode_set_cowblocks_tag(ip);
  67. else
  68. xfs_inode_clear_cowblocks_tag(ip);
  69. }
  70. /*
  71. * Adjust the on-disk inode size upwards if needed so that we never add
  72. * mappings into the file past EOF. This is crucial so that log recovery won't
  73. * get confused by the sudden appearance of post-eof mappings.
  74. */
  75. STATIC void
  76. xfs_exchmaps_update_size(
  77. struct xfs_trans *tp,
  78. struct xfs_inode *ip,
  79. struct xfs_bmbt_irec *imap,
  80. xfs_fsize_t new_isize)
  81. {
  82. struct xfs_mount *mp = tp->t_mountp;
  83. xfs_fsize_t len;
  84. if (new_isize < 0)
  85. return;
  86. len = min(XFS_FSB_TO_B(mp, imap->br_startoff + imap->br_blockcount),
  87. new_isize);
  88. if (len <= ip->i_disk_size)
  89. return;
  90. trace_xfs_exchmaps_update_inode_size(ip, len);
  91. ip->i_disk_size = len;
  92. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  93. }
  94. /* Advance the incore state tracking after exchanging a mapping. */
  95. static inline void
  96. xmi_advance(
  97. struct xfs_exchmaps_intent *xmi,
  98. const struct xfs_bmbt_irec *irec)
  99. {
  100. xmi->xmi_startoff1 += irec->br_blockcount;
  101. xmi->xmi_startoff2 += irec->br_blockcount;
  102. xmi->xmi_blockcount -= irec->br_blockcount;
  103. }
  104. /* Do we still have more mappings to exchange? */
  105. static inline bool
  106. xmi_has_more_exchange_work(const struct xfs_exchmaps_intent *xmi)
  107. {
  108. return xmi->xmi_blockcount > 0;
  109. }
  110. /* Do we have post-operation cleanups to perform? */
  111. static inline bool
  112. xmi_has_postop_work(const struct xfs_exchmaps_intent *xmi)
  113. {
  114. return xmi->xmi_flags & (XFS_EXCHMAPS_CLEAR_INO1_REFLINK |
  115. XFS_EXCHMAPS_CLEAR_INO2_REFLINK |
  116. __XFS_EXCHMAPS_INO2_SHORTFORM);
  117. }
  118. /* Check all mappings to make sure we can actually exchange them. */
  119. int
  120. xfs_exchmaps_check_forks(
  121. struct xfs_mount *mp,
  122. const struct xfs_exchmaps_req *req)
  123. {
  124. struct xfs_ifork *ifp1, *ifp2;
  125. int whichfork = xfs_exchmaps_reqfork(req);
  126. /* No fork? */
  127. ifp1 = xfs_ifork_ptr(req->ip1, whichfork);
  128. ifp2 = xfs_ifork_ptr(req->ip2, whichfork);
  129. if (!ifp1 || !ifp2)
  130. return -EINVAL;
  131. /* We don't know how to exchange local format forks. */
  132. if (ifp1->if_format == XFS_DINODE_FMT_LOCAL ||
  133. ifp2->if_format == XFS_DINODE_FMT_LOCAL)
  134. return -EINVAL;
  135. return 0;
  136. }
  137. #ifdef CONFIG_XFS_QUOTA
  138. /* Log the actual updates to the quota accounting. */
  139. static inline void
  140. xfs_exchmaps_update_quota(
  141. struct xfs_trans *tp,
  142. struct xfs_exchmaps_intent *xmi,
  143. struct xfs_bmbt_irec *irec1,
  144. struct xfs_bmbt_irec *irec2)
  145. {
  146. int64_t ip1_delta = 0, ip2_delta = 0;
  147. unsigned int qflag;
  148. qflag = XFS_IS_REALTIME_INODE(xmi->xmi_ip1) ? XFS_TRANS_DQ_RTBCOUNT :
  149. XFS_TRANS_DQ_BCOUNT;
  150. if (xfs_bmap_is_real_extent(irec1)) {
  151. ip1_delta -= irec1->br_blockcount;
  152. ip2_delta += irec1->br_blockcount;
  153. }
  154. if (xfs_bmap_is_real_extent(irec2)) {
  155. ip1_delta += irec2->br_blockcount;
  156. ip2_delta -= irec2->br_blockcount;
  157. }
  158. xfs_trans_mod_dquot_byino(tp, xmi->xmi_ip1, qflag, ip1_delta);
  159. xfs_trans_mod_dquot_byino(tp, xmi->xmi_ip2, qflag, ip2_delta);
  160. }
  161. #else
  162. # define xfs_exchmaps_update_quota(tp, xmi, irec1, irec2) ((void)0)
  163. #endif
  164. /* Decide if we want to skip this mapping from file1. */
  165. static inline bool
  166. xfs_exchmaps_can_skip_mapping(
  167. struct xfs_exchmaps_intent *xmi,
  168. struct xfs_bmbt_irec *irec)
  169. {
  170. struct xfs_mount *mp = xmi->xmi_ip1->i_mount;
  171. /* Do not skip this mapping if the caller did not tell us to. */
  172. if (!(xmi->xmi_flags & XFS_EXCHMAPS_INO1_WRITTEN))
  173. return false;
  174. /* Do not skip mapped, written mappings. */
  175. if (xfs_bmap_is_written_extent(irec))
  176. return false;
  177. /*
  178. * The mapping is unwritten or a hole. It cannot be a delalloc
  179. * reservation because we already excluded those. It cannot be an
  180. * unwritten extent with dirty page cache because we flushed the page
  181. * cache. For files where the allocation unit is 1FSB (files on the
  182. * data dev, rt files if the extent size is 1FSB), we can safely
  183. * skip this mapping.
  184. */
  185. if (!xfs_inode_has_bigrtalloc(xmi->xmi_ip1))
  186. return true;
  187. /*
  188. * For a realtime file with a multi-fsb allocation unit, the decision
  189. * is trickier because we can only swap full allocation units.
  190. * Unwritten mappings can appear in the middle of an rtx if the rtx is
  191. * partially written, but they can also appear for preallocations.
  192. *
  193. * If the mapping is a hole, skip it entirely. Holes should align with
  194. * rtx boundaries.
  195. */
  196. if (!xfs_bmap_is_real_extent(irec))
  197. return true;
  198. /*
  199. * All mappings below this point are unwritten.
  200. *
  201. * - If the beginning is not aligned to an rtx, trim the end of the
  202. * mapping so that it does not cross an rtx boundary, and swap it.
  203. *
  204. * - If both ends are aligned to an rtx, skip the entire mapping.
  205. */
  206. if (!isaligned_64(irec->br_startoff, mp->m_sb.sb_rextsize)) {
  207. xfs_fileoff_t new_end;
  208. new_end = roundup_64(irec->br_startoff, mp->m_sb.sb_rextsize);
  209. irec->br_blockcount = min(irec->br_blockcount,
  210. new_end - irec->br_startoff);
  211. return false;
  212. }
  213. if (isaligned_64(irec->br_blockcount, mp->m_sb.sb_rextsize))
  214. return true;
  215. /*
  216. * All mappings below this point are unwritten, start on an rtx
  217. * boundary, and do not end on an rtx boundary.
  218. *
  219. * - If the mapping is longer than one rtx, trim the end of the mapping
  220. * down to an rtx boundary and skip it.
  221. *
  222. * - The mapping is shorter than one rtx. Swap it.
  223. */
  224. if (irec->br_blockcount > mp->m_sb.sb_rextsize) {
  225. xfs_fileoff_t new_end;
  226. new_end = rounddown_64(irec->br_startoff + irec->br_blockcount,
  227. mp->m_sb.sb_rextsize);
  228. irec->br_blockcount = new_end - irec->br_startoff;
  229. return true;
  230. }
  231. return false;
  232. }
  233. /*
  234. * Walk forward through the file ranges in @xmi until we find two different
  235. * mappings to exchange. If there is work to do, return the mappings;
  236. * otherwise we've reached the end of the range and xmi_blockcount will be
  237. * zero.
  238. *
  239. * If the walk skips over a pair of mappings to the same storage, save them as
  240. * the left records in @adj (if provided) so that the simulation phase can
  241. * avoid an extra lookup.
  242. */
  243. static int
  244. xfs_exchmaps_find_mappings(
  245. struct xfs_exchmaps_intent *xmi,
  246. struct xfs_bmbt_irec *irec1,
  247. struct xfs_bmbt_irec *irec2,
  248. struct xfs_exchmaps_adjacent *adj)
  249. {
  250. int nimaps;
  251. int bmap_flags;
  252. int error;
  253. bmap_flags = xfs_bmapi_aflag(xfs_exchmaps_whichfork(xmi));
  254. for (; xmi_has_more_exchange_work(xmi); xmi_advance(xmi, irec1)) {
  255. /* Read mapping from the first file */
  256. nimaps = 1;
  257. error = xfs_bmapi_read(xmi->xmi_ip1, xmi->xmi_startoff1,
  258. xmi->xmi_blockcount, irec1, &nimaps,
  259. bmap_flags);
  260. if (error)
  261. return error;
  262. if (nimaps != 1 ||
  263. irec1->br_startblock == DELAYSTARTBLOCK ||
  264. irec1->br_startoff != xmi->xmi_startoff1) {
  265. /*
  266. * We should never get no mapping or a delalloc mapping
  267. * or something that doesn't match what we asked for,
  268. * since the caller flushed both inodes and we hold the
  269. * ILOCKs for both inodes.
  270. */
  271. ASSERT(0);
  272. return -EINVAL;
  273. }
  274. if (xfs_exchmaps_can_skip_mapping(xmi, irec1)) {
  275. trace_xfs_exchmaps_mapping1_skip(xmi->xmi_ip1, irec1);
  276. continue;
  277. }
  278. /* Read mapping from the second file */
  279. nimaps = 1;
  280. error = xfs_bmapi_read(xmi->xmi_ip2, xmi->xmi_startoff2,
  281. irec1->br_blockcount, irec2, &nimaps,
  282. bmap_flags);
  283. if (error)
  284. return error;
  285. if (nimaps != 1 ||
  286. irec2->br_startblock == DELAYSTARTBLOCK ||
  287. irec2->br_startoff != xmi->xmi_startoff2) {
  288. /*
  289. * We should never get no mapping or a delalloc mapping
  290. * or something that doesn't match what we asked for,
  291. * since the caller flushed both inodes and we hold the
  292. * ILOCKs for both inodes.
  293. */
  294. ASSERT(0);
  295. return -EINVAL;
  296. }
  297. /*
  298. * We can only exchange as many blocks as the smaller of the
  299. * two mapping maps.
  300. */
  301. irec1->br_blockcount = min(irec1->br_blockcount,
  302. irec2->br_blockcount);
  303. trace_xfs_exchmaps_mapping1(xmi->xmi_ip1, irec1);
  304. trace_xfs_exchmaps_mapping2(xmi->xmi_ip2, irec2);
  305. /* We found something to exchange, so return it. */
  306. if (irec1->br_startblock != irec2->br_startblock)
  307. return 0;
  308. /*
  309. * Two mappings pointing to the same physical block must not
  310. * have different states; that's filesystem corruption. Move
  311. * on to the next mapping if they're both holes or both point
  312. * to the same physical space extent.
  313. */
  314. if (irec1->br_state != irec2->br_state) {
  315. xfs_bmap_mark_sick(xmi->xmi_ip1,
  316. xfs_exchmaps_whichfork(xmi));
  317. xfs_bmap_mark_sick(xmi->xmi_ip2,
  318. xfs_exchmaps_whichfork(xmi));
  319. return -EFSCORRUPTED;
  320. }
  321. /*
  322. * Save the mappings if we're estimating work and skipping
  323. * these identical mappings.
  324. */
  325. if (adj) {
  326. memcpy(&adj->left1, irec1, sizeof(*irec1));
  327. memcpy(&adj->left2, irec2, sizeof(*irec2));
  328. }
  329. }
  330. return 0;
  331. }
  332. /* Exchange these two mappings. */
  333. static void
  334. xfs_exchmaps_one_step(
  335. struct xfs_trans *tp,
  336. struct xfs_exchmaps_intent *xmi,
  337. struct xfs_bmbt_irec *irec1,
  338. struct xfs_bmbt_irec *irec2)
  339. {
  340. int whichfork = xfs_exchmaps_whichfork(xmi);
  341. xfs_exchmaps_update_quota(tp, xmi, irec1, irec2);
  342. /* Remove both mappings. */
  343. xfs_bmap_unmap_extent(tp, xmi->xmi_ip1, whichfork, irec1);
  344. xfs_bmap_unmap_extent(tp, xmi->xmi_ip2, whichfork, irec2);
  345. /*
  346. * Re-add both mappings. We exchange the file offsets between the two
  347. * maps and add the opposite map, which has the effect of filling the
  348. * logical offsets we just unmapped, but with with the physical mapping
  349. * information exchanged.
  350. */
  351. swap(irec1->br_startoff, irec2->br_startoff);
  352. xfs_bmap_map_extent(tp, xmi->xmi_ip1, whichfork, irec2);
  353. xfs_bmap_map_extent(tp, xmi->xmi_ip2, whichfork, irec1);
  354. /* Make sure we're not adding mappings past EOF. */
  355. if (whichfork == XFS_DATA_FORK) {
  356. xfs_exchmaps_update_size(tp, xmi->xmi_ip1, irec2,
  357. xmi->xmi_isize1);
  358. xfs_exchmaps_update_size(tp, xmi->xmi_ip2, irec1,
  359. xmi->xmi_isize2);
  360. }
  361. /*
  362. * Advance our cursor and exit. The caller (either defer ops or log
  363. * recovery) will log the XMD item, and if *blockcount is nonzero, it
  364. * will log a new XMI item for the remainder and call us back.
  365. */
  366. xmi_advance(xmi, irec1);
  367. }
  368. /* Convert inode2's leaf attr fork back to shortform, if possible.. */
  369. STATIC int
  370. xfs_exchmaps_attr_to_sf(
  371. struct xfs_trans *tp,
  372. struct xfs_exchmaps_intent *xmi)
  373. {
  374. struct xfs_da_args args = {
  375. .dp = xmi->xmi_ip2,
  376. .geo = tp->t_mountp->m_attr_geo,
  377. .whichfork = XFS_ATTR_FORK,
  378. .trans = tp,
  379. .owner = xmi->xmi_ip2->i_ino,
  380. };
  381. struct xfs_buf *bp;
  382. int forkoff;
  383. int error;
  384. if (!xfs_attr_is_leaf(xmi->xmi_ip2))
  385. return 0;
  386. error = xfs_attr3_leaf_read(tp, xmi->xmi_ip2, xmi->xmi_ip2->i_ino, 0,
  387. &bp);
  388. if (error)
  389. return error;
  390. forkoff = xfs_attr_shortform_allfit(bp, xmi->xmi_ip2);
  391. if (forkoff == 0)
  392. return 0;
  393. return xfs_attr3_leaf_to_shortform(bp, &args, forkoff);
  394. }
  395. /* Convert inode2's block dir fork back to shortform, if possible.. */
  396. STATIC int
  397. xfs_exchmaps_dir_to_sf(
  398. struct xfs_trans *tp,
  399. struct xfs_exchmaps_intent *xmi)
  400. {
  401. struct xfs_da_args args = {
  402. .dp = xmi->xmi_ip2,
  403. .geo = tp->t_mountp->m_dir_geo,
  404. .whichfork = XFS_DATA_FORK,
  405. .trans = tp,
  406. .owner = xmi->xmi_ip2->i_ino,
  407. };
  408. struct xfs_dir2_sf_hdr sfh;
  409. struct xfs_buf *bp;
  410. int size;
  411. int error = 0;
  412. if (xfs_dir2_format(&args, &error) != XFS_DIR2_FMT_BLOCK)
  413. return error;
  414. error = xfs_dir3_block_read(tp, xmi->xmi_ip2, xmi->xmi_ip2->i_ino, &bp);
  415. if (error)
  416. return error;
  417. size = xfs_dir2_block_sfsize(xmi->xmi_ip2, bp->b_addr, &sfh);
  418. if (size > xfs_inode_data_fork_size(xmi->xmi_ip2))
  419. return 0;
  420. return xfs_dir2_block_to_sf(&args, bp, size, &sfh);
  421. }
  422. /* Convert inode2's remote symlink target back to shortform, if possible. */
  423. STATIC int
  424. xfs_exchmaps_link_to_sf(
  425. struct xfs_trans *tp,
  426. struct xfs_exchmaps_intent *xmi)
  427. {
  428. struct xfs_inode *ip = xmi->xmi_ip2;
  429. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  430. char *buf;
  431. int error;
  432. if (ifp->if_format == XFS_DINODE_FMT_LOCAL ||
  433. ip->i_disk_size > xfs_inode_data_fork_size(ip))
  434. return 0;
  435. /* Read the current symlink target into a buffer. */
  436. buf = kmalloc(ip->i_disk_size + 1,
  437. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  438. if (!buf) {
  439. ASSERT(0);
  440. return -ENOMEM;
  441. }
  442. error = xfs_symlink_remote_read(ip, buf);
  443. if (error)
  444. goto free;
  445. /* Remove the blocks. */
  446. error = xfs_symlink_remote_truncate(tp, ip);
  447. if (error)
  448. goto free;
  449. /* Convert fork to local format and log our changes. */
  450. xfs_idestroy_fork(ifp);
  451. ifp->if_bytes = 0;
  452. ifp->if_format = XFS_DINODE_FMT_LOCAL;
  453. xfs_init_local_fork(ip, XFS_DATA_FORK, buf, ip->i_disk_size);
  454. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  455. free:
  456. kfree(buf);
  457. return error;
  458. }
  459. /* Clear the reflink flag after an exchange. */
  460. static inline void
  461. xfs_exchmaps_clear_reflink(
  462. struct xfs_trans *tp,
  463. struct xfs_inode *ip)
  464. {
  465. trace_xfs_reflink_unset_inode_flag(ip);
  466. ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  467. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  468. }
  469. /* Finish whatever work might come after an exchange operation. */
  470. static int
  471. xfs_exchmaps_do_postop_work(
  472. struct xfs_trans *tp,
  473. struct xfs_exchmaps_intent *xmi)
  474. {
  475. if (xmi->xmi_flags & __XFS_EXCHMAPS_INO2_SHORTFORM) {
  476. int error = 0;
  477. if (xmi->xmi_flags & XFS_EXCHMAPS_ATTR_FORK)
  478. error = xfs_exchmaps_attr_to_sf(tp, xmi);
  479. else if (S_ISDIR(VFS_I(xmi->xmi_ip2)->i_mode))
  480. error = xfs_exchmaps_dir_to_sf(tp, xmi);
  481. else if (S_ISLNK(VFS_I(xmi->xmi_ip2)->i_mode))
  482. error = xfs_exchmaps_link_to_sf(tp, xmi);
  483. xmi->xmi_flags &= ~__XFS_EXCHMAPS_INO2_SHORTFORM;
  484. if (error)
  485. return error;
  486. }
  487. if (xmi->xmi_flags & XFS_EXCHMAPS_CLEAR_INO1_REFLINK) {
  488. xfs_exchmaps_clear_reflink(tp, xmi->xmi_ip1);
  489. xmi->xmi_flags &= ~XFS_EXCHMAPS_CLEAR_INO1_REFLINK;
  490. }
  491. if (xmi->xmi_flags & XFS_EXCHMAPS_CLEAR_INO2_REFLINK) {
  492. xfs_exchmaps_clear_reflink(tp, xmi->xmi_ip2);
  493. xmi->xmi_flags &= ~XFS_EXCHMAPS_CLEAR_INO2_REFLINK;
  494. }
  495. return 0;
  496. }
  497. /* Finish one step in a mapping exchange operation, possibly relogging. */
  498. int
  499. xfs_exchmaps_finish_one(
  500. struct xfs_trans *tp,
  501. struct xfs_exchmaps_intent *xmi)
  502. {
  503. struct xfs_bmbt_irec irec1, irec2;
  504. int error;
  505. if (xmi_has_more_exchange_work(xmi)) {
  506. /*
  507. * If the operation state says that some range of the files
  508. * have not yet been exchanged, look for mappings in that range
  509. * to exchange. If we find some mappings, exchange them.
  510. */
  511. error = xfs_exchmaps_find_mappings(xmi, &irec1, &irec2, NULL);
  512. if (error)
  513. return error;
  514. if (xmi_has_more_exchange_work(xmi))
  515. xfs_exchmaps_one_step(tp, xmi, &irec1, &irec2);
  516. /*
  517. * If the caller asked us to exchange the file sizes after the
  518. * exchange and either we just exchanged the last mappings in
  519. * the range or we didn't find anything to exchange, update the
  520. * ondisk file sizes.
  521. */
  522. if ((xmi->xmi_flags & XFS_EXCHMAPS_SET_SIZES) &&
  523. !xmi_has_more_exchange_work(xmi)) {
  524. xmi->xmi_ip1->i_disk_size = xmi->xmi_isize1;
  525. xmi->xmi_ip2->i_disk_size = xmi->xmi_isize2;
  526. xfs_trans_log_inode(tp, xmi->xmi_ip1, XFS_ILOG_CORE);
  527. xfs_trans_log_inode(tp, xmi->xmi_ip2, XFS_ILOG_CORE);
  528. }
  529. } else if (xmi_has_postop_work(xmi)) {
  530. /*
  531. * Now that we're finished with the exchange operation,
  532. * complete the post-op cleanup work.
  533. */
  534. error = xfs_exchmaps_do_postop_work(tp, xmi);
  535. if (error)
  536. return error;
  537. }
  538. if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_EXCHMAPS_FINISH_ONE))
  539. return -EIO;
  540. /* If we still have work to do, ask for a new transaction. */
  541. if (xmi_has_more_exchange_work(xmi) || xmi_has_postop_work(xmi)) {
  542. trace_xfs_exchmaps_defer(tp->t_mountp, xmi);
  543. return -EAGAIN;
  544. }
  545. /*
  546. * If we reach here, we've finished all the exchange work and the post
  547. * operation work. The last thing we need to do before returning to
  548. * the caller is to make sure that COW forks are set up correctly.
  549. */
  550. if (!(xmi->xmi_flags & XFS_EXCHMAPS_ATTR_FORK)) {
  551. xfs_exchmaps_ensure_cowfork(xmi->xmi_ip1);
  552. xfs_exchmaps_ensure_cowfork(xmi->xmi_ip2);
  553. }
  554. return 0;
  555. }
  556. /*
  557. * Compute the amount of bmbt blocks we should reserve for each file. In the
  558. * worst case, each exchange will fill a hole with a new mapping, which could
  559. * result in a btree split every time we add a new leaf block.
  560. */
  561. static inline uint64_t
  562. xfs_exchmaps_bmbt_blocks(
  563. struct xfs_mount *mp,
  564. const struct xfs_exchmaps_req *req)
  565. {
  566. return howmany_64(req->nr_exchanges,
  567. XFS_MAX_CONTIG_BMAPS_PER_BLOCK(mp)) *
  568. XFS_EXTENTADD_SPACE_RES(mp, xfs_exchmaps_reqfork(req));
  569. }
  570. /* Compute the space we should reserve for the rmap btree expansions. */
  571. static inline uint64_t
  572. xfs_exchmaps_rmapbt_blocks(
  573. struct xfs_mount *mp,
  574. const struct xfs_exchmaps_req *req)
  575. {
  576. if (!xfs_has_rmapbt(mp))
  577. return 0;
  578. if (XFS_IS_REALTIME_INODE(req->ip1))
  579. return 0;
  580. return howmany_64(req->nr_exchanges,
  581. XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)) *
  582. XFS_RMAPADD_SPACE_RES(mp);
  583. }
  584. /* Estimate the bmbt and rmapbt overhead required to exchange mappings. */
  585. int
  586. xfs_exchmaps_estimate_overhead(
  587. struct xfs_exchmaps_req *req)
  588. {
  589. struct xfs_mount *mp = req->ip1->i_mount;
  590. xfs_filblks_t bmbt_blocks;
  591. xfs_filblks_t rmapbt_blocks;
  592. xfs_filblks_t resblks = req->resblks;
  593. /*
  594. * Compute the number of bmbt and rmapbt blocks we might need to handle
  595. * the estimated number of exchanges.
  596. */
  597. bmbt_blocks = xfs_exchmaps_bmbt_blocks(mp, req);
  598. rmapbt_blocks = xfs_exchmaps_rmapbt_blocks(mp, req);
  599. trace_xfs_exchmaps_overhead(mp, bmbt_blocks, rmapbt_blocks);
  600. /* Make sure the change in file block count doesn't overflow. */
  601. if (check_add_overflow(req->ip1_bcount, bmbt_blocks, &req->ip1_bcount))
  602. return -EFBIG;
  603. if (check_add_overflow(req->ip2_bcount, bmbt_blocks, &req->ip2_bcount))
  604. return -EFBIG;
  605. /*
  606. * Add together the number of blocks we need to handle btree growth,
  607. * then add it to the number of blocks we need to reserve to this
  608. * transaction.
  609. */
  610. if (check_add_overflow(resblks, bmbt_blocks, &resblks))
  611. return -ENOSPC;
  612. if (check_add_overflow(resblks, bmbt_blocks, &resblks))
  613. return -ENOSPC;
  614. if (check_add_overflow(resblks, rmapbt_blocks, &resblks))
  615. return -ENOSPC;
  616. if (check_add_overflow(resblks, rmapbt_blocks, &resblks))
  617. return -ENOSPC;
  618. /* Can't actually reserve more than UINT_MAX blocks. */
  619. if (req->resblks > UINT_MAX)
  620. return -ENOSPC;
  621. req->resblks = resblks;
  622. trace_xfs_exchmaps_final_estimate(req);
  623. return 0;
  624. }
  625. /* Decide if we can merge two real mappings. */
  626. static inline bool
  627. xmi_can_merge(
  628. const struct xfs_bmbt_irec *b1,
  629. const struct xfs_bmbt_irec *b2)
  630. {
  631. /* Don't merge holes. */
  632. if (b1->br_startblock == HOLESTARTBLOCK ||
  633. b2->br_startblock == HOLESTARTBLOCK)
  634. return false;
  635. /* We don't merge holes. */
  636. if (!xfs_bmap_is_real_extent(b1) || !xfs_bmap_is_real_extent(b2))
  637. return false;
  638. if (b1->br_startoff + b1->br_blockcount == b2->br_startoff &&
  639. b1->br_startblock + b1->br_blockcount == b2->br_startblock &&
  640. b1->br_state == b2->br_state &&
  641. b1->br_blockcount + b2->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
  642. return true;
  643. return false;
  644. }
  645. /*
  646. * Decide if we can merge three mappings. Caller must ensure all three
  647. * mappings must not be holes or delalloc reservations.
  648. */
  649. static inline bool
  650. xmi_can_merge_all(
  651. const struct xfs_bmbt_irec *l,
  652. const struct xfs_bmbt_irec *m,
  653. const struct xfs_bmbt_irec *r)
  654. {
  655. xfs_filblks_t new_len;
  656. new_len = l->br_blockcount + m->br_blockcount + r->br_blockcount;
  657. return new_len <= XFS_MAX_BMBT_EXTLEN;
  658. }
  659. #define CLEFT_CONTIG 0x01
  660. #define CRIGHT_CONTIG 0x02
  661. #define CHOLE 0x04
  662. #define CBOTH_CONTIG (CLEFT_CONTIG | CRIGHT_CONTIG)
  663. #define NLEFT_CONTIG 0x10
  664. #define NRIGHT_CONTIG 0x20
  665. #define NHOLE 0x40
  666. #define NBOTH_CONTIG (NLEFT_CONTIG | NRIGHT_CONTIG)
  667. /* Estimate the effect of a single exchange on mapping count. */
  668. static inline int
  669. xmi_delta_nextents_step(
  670. struct xfs_mount *mp,
  671. const struct xfs_bmbt_irec *left,
  672. const struct xfs_bmbt_irec *curr,
  673. const struct xfs_bmbt_irec *new,
  674. const struct xfs_bmbt_irec *right)
  675. {
  676. bool lhole, rhole, chole, nhole;
  677. unsigned int state = 0;
  678. int ret = 0;
  679. lhole = left->br_startblock == HOLESTARTBLOCK;
  680. rhole = right->br_startblock == HOLESTARTBLOCK;
  681. chole = curr->br_startblock == HOLESTARTBLOCK;
  682. nhole = new->br_startblock == HOLESTARTBLOCK;
  683. if (chole)
  684. state |= CHOLE;
  685. if (!lhole && !chole && xmi_can_merge(left, curr))
  686. state |= CLEFT_CONTIG;
  687. if (!rhole && !chole && xmi_can_merge(curr, right))
  688. state |= CRIGHT_CONTIG;
  689. if ((state & CBOTH_CONTIG) == CBOTH_CONTIG &&
  690. !xmi_can_merge_all(left, curr, right))
  691. state &= ~CRIGHT_CONTIG;
  692. if (nhole)
  693. state |= NHOLE;
  694. if (!lhole && !nhole && xmi_can_merge(left, new))
  695. state |= NLEFT_CONTIG;
  696. if (!rhole && !nhole && xmi_can_merge(new, right))
  697. state |= NRIGHT_CONTIG;
  698. if ((state & NBOTH_CONTIG) == NBOTH_CONTIG &&
  699. !xmi_can_merge_all(left, new, right))
  700. state &= ~NRIGHT_CONTIG;
  701. switch (state & (CLEFT_CONTIG | CRIGHT_CONTIG | CHOLE)) {
  702. case CLEFT_CONTIG | CRIGHT_CONTIG:
  703. /*
  704. * left/curr/right are the same mapping, so deleting curr
  705. * causes 2 new mappings to be created.
  706. */
  707. ret += 2;
  708. break;
  709. case 0:
  710. /*
  711. * curr is not contiguous with any mapping, so we remove curr
  712. * completely
  713. */
  714. ret--;
  715. break;
  716. case CHOLE:
  717. /* hole, do nothing */
  718. break;
  719. case CLEFT_CONTIG:
  720. case CRIGHT_CONTIG:
  721. /* trim either left or right, no change */
  722. break;
  723. }
  724. switch (state & (NLEFT_CONTIG | NRIGHT_CONTIG | NHOLE)) {
  725. case NLEFT_CONTIG | NRIGHT_CONTIG:
  726. /*
  727. * left/curr/right will become the same mapping, so adding
  728. * curr causes the deletion of right.
  729. */
  730. ret--;
  731. break;
  732. case 0:
  733. /* new is not contiguous with any mapping */
  734. ret++;
  735. break;
  736. case NHOLE:
  737. /* hole, do nothing. */
  738. break;
  739. case NLEFT_CONTIG:
  740. case NRIGHT_CONTIG:
  741. /* new is absorbed into left or right, no change */
  742. break;
  743. }
  744. trace_xfs_exchmaps_delta_nextents_step(mp, left, curr, new, right, ret,
  745. state);
  746. return ret;
  747. }
  748. /* Make sure we don't overflow the extent (mapping) counters. */
  749. static inline int
  750. xmi_ensure_delta_nextents(
  751. struct xfs_exchmaps_req *req,
  752. struct xfs_inode *ip,
  753. int64_t delta)
  754. {
  755. struct xfs_mount *mp = ip->i_mount;
  756. int whichfork = xfs_exchmaps_reqfork(req);
  757. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  758. uint64_t new_nextents;
  759. xfs_extnum_t max_nextents;
  760. if (delta < 0)
  761. return 0;
  762. /*
  763. * It's always an error if the delta causes integer overflow. delta
  764. * needs an explicit cast here to avoid warnings about implicit casts
  765. * coded into the overflow check.
  766. */
  767. if (check_add_overflow(ifp->if_nextents, (uint64_t)delta,
  768. &new_nextents))
  769. return -EFBIG;
  770. if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REDUCE_MAX_IEXTENTS) &&
  771. new_nextents > 10)
  772. return -EFBIG;
  773. /*
  774. * We always promote both inodes to have large extent counts if the
  775. * superblock feature is enabled, so we only need to check against the
  776. * theoretical maximum.
  777. */
  778. max_nextents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
  779. whichfork);
  780. if (new_nextents > max_nextents)
  781. return -EFBIG;
  782. return 0;
  783. }
  784. /* Find the next mapping after irec. */
  785. static inline int
  786. xmi_next(
  787. struct xfs_inode *ip,
  788. int bmap_flags,
  789. const struct xfs_bmbt_irec *irec,
  790. struct xfs_bmbt_irec *nrec)
  791. {
  792. xfs_fileoff_t off;
  793. xfs_filblks_t blockcount;
  794. int nimaps = 1;
  795. int error;
  796. off = irec->br_startoff + irec->br_blockcount;
  797. blockcount = XFS_MAX_FILEOFF - off;
  798. error = xfs_bmapi_read(ip, off, blockcount, nrec, &nimaps, bmap_flags);
  799. if (error)
  800. return error;
  801. if (nrec->br_startblock == DELAYSTARTBLOCK ||
  802. nrec->br_startoff != off) {
  803. /*
  804. * If we don't get the mapping we want, return a zero-length
  805. * mapping, which our estimator function will pretend is a hole.
  806. * We shouldn't get delalloc reservations.
  807. */
  808. nrec->br_startblock = HOLESTARTBLOCK;
  809. }
  810. return 0;
  811. }
  812. int __init
  813. xfs_exchmaps_intent_init_cache(void)
  814. {
  815. xfs_exchmaps_intent_cache = kmem_cache_create("xfs_exchmaps_intent",
  816. sizeof(struct xfs_exchmaps_intent),
  817. 0, 0, NULL);
  818. return xfs_exchmaps_intent_cache != NULL ? 0 : -ENOMEM;
  819. }
  820. void
  821. xfs_exchmaps_intent_destroy_cache(void)
  822. {
  823. kmem_cache_destroy(xfs_exchmaps_intent_cache);
  824. xfs_exchmaps_intent_cache = NULL;
  825. }
  826. /*
  827. * Decide if we will exchange the reflink flags between the two files after the
  828. * exchange. The only time we want to do this is if we're exchanging all
  829. * mappings under EOF and the inode reflink flags have different states.
  830. */
  831. static inline bool
  832. xmi_can_exchange_reflink_flags(
  833. const struct xfs_exchmaps_req *req,
  834. unsigned int reflink_state)
  835. {
  836. struct xfs_mount *mp = req->ip1->i_mount;
  837. if (hweight32(reflink_state) != 1)
  838. return false;
  839. if (req->startoff1 != 0 || req->startoff2 != 0)
  840. return false;
  841. if (req->blockcount != XFS_B_TO_FSB(mp, req->ip1->i_disk_size))
  842. return false;
  843. if (req->blockcount != XFS_B_TO_FSB(mp, req->ip2->i_disk_size))
  844. return false;
  845. return true;
  846. }
  847. /* Allocate and initialize a new incore intent item from a request. */
  848. struct xfs_exchmaps_intent *
  849. xfs_exchmaps_init_intent(
  850. const struct xfs_exchmaps_req *req)
  851. {
  852. struct xfs_exchmaps_intent *xmi;
  853. unsigned int rs = 0;
  854. xmi = kmem_cache_zalloc(xfs_exchmaps_intent_cache,
  855. GFP_NOFS | __GFP_NOFAIL);
  856. INIT_LIST_HEAD(&xmi->xmi_list);
  857. xmi->xmi_ip1 = req->ip1;
  858. xmi->xmi_ip2 = req->ip2;
  859. xmi->xmi_startoff1 = req->startoff1;
  860. xmi->xmi_startoff2 = req->startoff2;
  861. xmi->xmi_blockcount = req->blockcount;
  862. xmi->xmi_isize1 = xmi->xmi_isize2 = -1;
  863. xmi->xmi_flags = req->flags & XFS_EXCHMAPS_PARAMS;
  864. if (xfs_exchmaps_whichfork(xmi) == XFS_ATTR_FORK) {
  865. xmi->xmi_flags |= __XFS_EXCHMAPS_INO2_SHORTFORM;
  866. return xmi;
  867. }
  868. if (req->flags & XFS_EXCHMAPS_SET_SIZES) {
  869. xmi->xmi_flags |= XFS_EXCHMAPS_SET_SIZES;
  870. xmi->xmi_isize1 = req->ip2->i_disk_size;
  871. xmi->xmi_isize2 = req->ip1->i_disk_size;
  872. }
  873. /* Record the state of each inode's reflink flag before the op. */
  874. if (xfs_is_reflink_inode(req->ip1))
  875. rs |= 1;
  876. if (xfs_is_reflink_inode(req->ip2))
  877. rs |= 2;
  878. /*
  879. * Figure out if we're clearing the reflink flags (which effectively
  880. * exchanges them) after the operation.
  881. */
  882. if (xmi_can_exchange_reflink_flags(req, rs)) {
  883. if (rs & 1)
  884. xmi->xmi_flags |= XFS_EXCHMAPS_CLEAR_INO1_REFLINK;
  885. if (rs & 2)
  886. xmi->xmi_flags |= XFS_EXCHMAPS_CLEAR_INO2_REFLINK;
  887. }
  888. if (S_ISDIR(VFS_I(xmi->xmi_ip2)->i_mode) ||
  889. S_ISLNK(VFS_I(xmi->xmi_ip2)->i_mode))
  890. xmi->xmi_flags |= __XFS_EXCHMAPS_INO2_SHORTFORM;
  891. return xmi;
  892. }
  893. /*
  894. * Estimate the number of exchange operations and the number of file blocks
  895. * in each file that will be affected by the exchange operation.
  896. */
  897. int
  898. xfs_exchmaps_estimate(
  899. struct xfs_exchmaps_req *req)
  900. {
  901. struct xfs_exchmaps_intent *xmi;
  902. struct xfs_bmbt_irec irec1, irec2;
  903. struct xfs_exchmaps_adjacent adj = ADJACENT_INIT;
  904. xfs_filblks_t ip1_blocks = 0, ip2_blocks = 0;
  905. int64_t d_nexts1, d_nexts2;
  906. int bmap_flags;
  907. int error;
  908. ASSERT(!(req->flags & ~XFS_EXCHMAPS_PARAMS));
  909. bmap_flags = xfs_bmapi_aflag(xfs_exchmaps_reqfork(req));
  910. xmi = xfs_exchmaps_init_intent(req);
  911. /*
  912. * To guard against the possibility of overflowing the extent counters,
  913. * we have to estimate an upper bound on the potential increase in that
  914. * counter. We can split the mapping at each end of the range, and for
  915. * each step of the exchange we can split the mapping that we're
  916. * working on if the mappings do not align.
  917. */
  918. d_nexts1 = d_nexts2 = 3;
  919. while (xmi_has_more_exchange_work(xmi)) {
  920. /*
  921. * Walk through the file ranges until we find something to
  922. * exchange. Because we're simulating the exchange, pass in
  923. * adj to capture skipped mappings for correct estimation of
  924. * bmbt record merges.
  925. */
  926. error = xfs_exchmaps_find_mappings(xmi, &irec1, &irec2, &adj);
  927. if (error)
  928. goto out_free;
  929. if (!xmi_has_more_exchange_work(xmi))
  930. break;
  931. /* Update accounting. */
  932. if (xfs_bmap_is_real_extent(&irec1))
  933. ip1_blocks += irec1.br_blockcount;
  934. if (xfs_bmap_is_real_extent(&irec2))
  935. ip2_blocks += irec2.br_blockcount;
  936. req->nr_exchanges++;
  937. /* Read the next mappings from both files. */
  938. error = xmi_next(req->ip1, bmap_flags, &irec1, &adj.right1);
  939. if (error)
  940. goto out_free;
  941. error = xmi_next(req->ip2, bmap_flags, &irec2, &adj.right2);
  942. if (error)
  943. goto out_free;
  944. /* Update extent count deltas. */
  945. d_nexts1 += xmi_delta_nextents_step(req->ip1->i_mount,
  946. &adj.left1, &irec1, &irec2, &adj.right1);
  947. d_nexts2 += xmi_delta_nextents_step(req->ip1->i_mount,
  948. &adj.left2, &irec2, &irec1, &adj.right2);
  949. /* Now pretend we exchanged the mappings. */
  950. if (xmi_can_merge(&adj.left2, &irec1))
  951. adj.left2.br_blockcount += irec1.br_blockcount;
  952. else
  953. memcpy(&adj.left2, &irec1, sizeof(irec1));
  954. if (xmi_can_merge(&adj.left1, &irec2))
  955. adj.left1.br_blockcount += irec2.br_blockcount;
  956. else
  957. memcpy(&adj.left1, &irec2, sizeof(irec2));
  958. xmi_advance(xmi, &irec1);
  959. }
  960. /* Account for the blocks that are being exchanged. */
  961. if (XFS_IS_REALTIME_INODE(req->ip1) &&
  962. xfs_exchmaps_reqfork(req) == XFS_DATA_FORK) {
  963. req->ip1_rtbcount = ip1_blocks;
  964. req->ip2_rtbcount = ip2_blocks;
  965. } else {
  966. req->ip1_bcount = ip1_blocks;
  967. req->ip2_bcount = ip2_blocks;
  968. }
  969. /*
  970. * Make sure that both forks have enough slack left in their extent
  971. * counters that the exchange operation will not overflow.
  972. */
  973. trace_xfs_exchmaps_delta_nextents(req, d_nexts1, d_nexts2);
  974. if (req->ip1 == req->ip2) {
  975. error = xmi_ensure_delta_nextents(req, req->ip1,
  976. d_nexts1 + d_nexts2);
  977. } else {
  978. error = xmi_ensure_delta_nextents(req, req->ip1, d_nexts1);
  979. if (error)
  980. goto out_free;
  981. error = xmi_ensure_delta_nextents(req, req->ip2, d_nexts2);
  982. }
  983. if (error)
  984. goto out_free;
  985. trace_xfs_exchmaps_initial_estimate(req);
  986. error = xfs_exchmaps_estimate_overhead(req);
  987. out_free:
  988. kmem_cache_free(xfs_exchmaps_intent_cache, xmi);
  989. return error;
  990. }
  991. /* Set the reflink flag before an operation. */
  992. static inline void
  993. xfs_exchmaps_set_reflink(
  994. struct xfs_trans *tp,
  995. struct xfs_inode *ip)
  996. {
  997. trace_xfs_reflink_set_inode_flag(ip);
  998. ip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
  999. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1000. }
  1001. /*
  1002. * If either file has shared blocks and we're exchanging data forks, we must
  1003. * flag the other file as having shared blocks so that we get the shared-block
  1004. * rmap functions if we need to fix up the rmaps.
  1005. */
  1006. void
  1007. xfs_exchmaps_ensure_reflink(
  1008. struct xfs_trans *tp,
  1009. const struct xfs_exchmaps_intent *xmi)
  1010. {
  1011. unsigned int rs = 0;
  1012. if (xfs_is_reflink_inode(xmi->xmi_ip1))
  1013. rs |= 1;
  1014. if (xfs_is_reflink_inode(xmi->xmi_ip2))
  1015. rs |= 2;
  1016. if ((rs & 1) && !xfs_is_reflink_inode(xmi->xmi_ip2))
  1017. xfs_exchmaps_set_reflink(tp, xmi->xmi_ip2);
  1018. if ((rs & 2) && !xfs_is_reflink_inode(xmi->xmi_ip1))
  1019. xfs_exchmaps_set_reflink(tp, xmi->xmi_ip1);
  1020. }
  1021. /* Set the large extent count flag before an operation if needed. */
  1022. static inline void
  1023. xfs_exchmaps_ensure_large_extent_counts(
  1024. struct xfs_trans *tp,
  1025. struct xfs_inode *ip)
  1026. {
  1027. if (xfs_inode_has_large_extent_counts(ip))
  1028. return;
  1029. ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
  1030. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1031. }
  1032. /* Widen the extent counter fields of both inodes if necessary. */
  1033. void
  1034. xfs_exchmaps_upgrade_extent_counts(
  1035. struct xfs_trans *tp,
  1036. const struct xfs_exchmaps_intent *xmi)
  1037. {
  1038. if (!xfs_has_large_extent_counts(tp->t_mountp))
  1039. return;
  1040. xfs_exchmaps_ensure_large_extent_counts(tp, xmi->xmi_ip1);
  1041. xfs_exchmaps_ensure_large_extent_counts(tp, xmi->xmi_ip2);
  1042. }
  1043. /*
  1044. * Schedule an exchange a range of mappings from one inode to another.
  1045. *
  1046. * The use of file mapping exchange log intent items ensures the operation can
  1047. * be resumed even if the system goes down. The caller must commit the
  1048. * transaction to start the work.
  1049. *
  1050. * The caller must ensure the inodes must be joined to the transaction and
  1051. * ILOCKd; they will still be joined to the transaction at exit.
  1052. */
  1053. void
  1054. xfs_exchange_mappings(
  1055. struct xfs_trans *tp,
  1056. const struct xfs_exchmaps_req *req)
  1057. {
  1058. struct xfs_exchmaps_intent *xmi;
  1059. BUILD_BUG_ON(XFS_EXCHMAPS_INTERNAL_FLAGS & XFS_EXCHMAPS_LOGGED_FLAGS);
  1060. xfs_assert_ilocked(req->ip1, XFS_ILOCK_EXCL);
  1061. xfs_assert_ilocked(req->ip2, XFS_ILOCK_EXCL);
  1062. ASSERT(!(req->flags & ~XFS_EXCHMAPS_LOGGED_FLAGS));
  1063. if (req->flags & XFS_EXCHMAPS_SET_SIZES)
  1064. ASSERT(!(req->flags & XFS_EXCHMAPS_ATTR_FORK));
  1065. ASSERT(xfs_has_exchange_range(tp->t_mountp));
  1066. if (req->blockcount == 0)
  1067. return;
  1068. xmi = xfs_exchmaps_init_intent(req);
  1069. xfs_exchmaps_defer_add(tp, xmi);
  1070. xfs_exchmaps_ensure_reflink(tp, xmi);
  1071. xfs_exchmaps_upgrade_extent_counts(tp, xmi);
  1072. }