xfs_rmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2014 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.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_btree.h"
  31. #include "xfs_trans.h"
  32. #include "xfs_alloc.h"
  33. #include "xfs_rmap.h"
  34. #include "xfs_rmap_btree.h"
  35. #include "xfs_trans_space.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_error.h"
  38. #include "xfs_extent_busy.h"
  39. /*
  40. * Lookup the first record less than or equal to [bno, len, owner, offset]
  41. * in the btree given by cur.
  42. */
  43. int
  44. xfs_rmap_lookup_le(
  45. struct xfs_btree_cur *cur,
  46. xfs_agblock_t bno,
  47. xfs_extlen_t len,
  48. uint64_t owner,
  49. uint64_t offset,
  50. unsigned int flags,
  51. int *stat)
  52. {
  53. cur->bc_rec.r.rm_startblock = bno;
  54. cur->bc_rec.r.rm_blockcount = len;
  55. cur->bc_rec.r.rm_owner = owner;
  56. cur->bc_rec.r.rm_offset = offset;
  57. cur->bc_rec.r.rm_flags = flags;
  58. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  59. }
  60. /*
  61. * Lookup the record exactly matching [bno, len, owner, offset]
  62. * in the btree given by cur.
  63. */
  64. int
  65. xfs_rmap_lookup_eq(
  66. struct xfs_btree_cur *cur,
  67. xfs_agblock_t bno,
  68. xfs_extlen_t len,
  69. uint64_t owner,
  70. uint64_t offset,
  71. unsigned int flags,
  72. int *stat)
  73. {
  74. cur->bc_rec.r.rm_startblock = bno;
  75. cur->bc_rec.r.rm_blockcount = len;
  76. cur->bc_rec.r.rm_owner = owner;
  77. cur->bc_rec.r.rm_offset = offset;
  78. cur->bc_rec.r.rm_flags = flags;
  79. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  80. }
  81. /*
  82. * Update the record referred to by cur to the value given
  83. * by [bno, len, owner, offset].
  84. * This either works (return 0) or gets an EFSCORRUPTED error.
  85. */
  86. STATIC int
  87. xfs_rmap_update(
  88. struct xfs_btree_cur *cur,
  89. struct xfs_rmap_irec *irec)
  90. {
  91. union xfs_btree_rec rec;
  92. rec.rmap.rm_startblock = cpu_to_be32(irec->rm_startblock);
  93. rec.rmap.rm_blockcount = cpu_to_be32(irec->rm_blockcount);
  94. rec.rmap.rm_owner = cpu_to_be64(irec->rm_owner);
  95. rec.rmap.rm_offset = cpu_to_be64(
  96. xfs_rmap_irec_offset_pack(irec));
  97. return xfs_btree_update(cur, &rec);
  98. }
  99. static int
  100. xfs_rmap_btrec_to_irec(
  101. union xfs_btree_rec *rec,
  102. struct xfs_rmap_irec *irec)
  103. {
  104. irec->rm_flags = 0;
  105. irec->rm_startblock = be32_to_cpu(rec->rmap.rm_startblock);
  106. irec->rm_blockcount = be32_to_cpu(rec->rmap.rm_blockcount);
  107. irec->rm_owner = be64_to_cpu(rec->rmap.rm_owner);
  108. return xfs_rmap_irec_offset_unpack(be64_to_cpu(rec->rmap.rm_offset),
  109. irec);
  110. }
  111. /*
  112. * Get the data from the pointed-to record.
  113. */
  114. int
  115. xfs_rmap_get_rec(
  116. struct xfs_btree_cur *cur,
  117. struct xfs_rmap_irec *irec,
  118. int *stat)
  119. {
  120. union xfs_btree_rec *rec;
  121. int error;
  122. error = xfs_btree_get_rec(cur, &rec, stat);
  123. if (error || !*stat)
  124. return error;
  125. return xfs_rmap_btrec_to_irec(rec, irec);
  126. }
  127. int
  128. xfs_rmap_free(
  129. struct xfs_trans *tp,
  130. struct xfs_buf *agbp,
  131. xfs_agnumber_t agno,
  132. xfs_agblock_t bno,
  133. xfs_extlen_t len,
  134. struct xfs_owner_info *oinfo)
  135. {
  136. struct xfs_mount *mp = tp->t_mountp;
  137. int error = 0;
  138. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  139. return 0;
  140. trace_xfs_rmap_unmap(mp, agno, bno, len, false, oinfo);
  141. if (1)
  142. goto out_error;
  143. trace_xfs_rmap_unmap_done(mp, agno, bno, len, false, oinfo);
  144. return 0;
  145. out_error:
  146. trace_xfs_rmap_unmap_error(mp, agno, error, _RET_IP_);
  147. return error;
  148. }
  149. /*
  150. * A mergeable rmap must have the same owner and the same values for
  151. * the unwritten, attr_fork, and bmbt flags. The startblock and
  152. * offset are checked separately.
  153. */
  154. static bool
  155. xfs_rmap_is_mergeable(
  156. struct xfs_rmap_irec *irec,
  157. uint64_t owner,
  158. unsigned int flags)
  159. {
  160. if (irec->rm_owner == XFS_RMAP_OWN_NULL)
  161. return false;
  162. if (irec->rm_owner != owner)
  163. return false;
  164. if ((flags & XFS_RMAP_UNWRITTEN) ^
  165. (irec->rm_flags & XFS_RMAP_UNWRITTEN))
  166. return false;
  167. if ((flags & XFS_RMAP_ATTR_FORK) ^
  168. (irec->rm_flags & XFS_RMAP_ATTR_FORK))
  169. return false;
  170. if ((flags & XFS_RMAP_BMBT_BLOCK) ^
  171. (irec->rm_flags & XFS_RMAP_BMBT_BLOCK))
  172. return false;
  173. return true;
  174. }
  175. /*
  176. * When we allocate a new block, the first thing we do is add a reference to
  177. * the extent in the rmap btree. This takes the form of a [agbno, length,
  178. * owner, offset] record. Flags are encoded in the high bits of the offset
  179. * field.
  180. */
  181. STATIC int
  182. xfs_rmap_map(
  183. struct xfs_btree_cur *cur,
  184. xfs_agblock_t bno,
  185. xfs_extlen_t len,
  186. bool unwritten,
  187. struct xfs_owner_info *oinfo)
  188. {
  189. struct xfs_mount *mp = cur->bc_mp;
  190. struct xfs_rmap_irec ltrec;
  191. struct xfs_rmap_irec gtrec;
  192. int have_gt;
  193. int have_lt;
  194. int error = 0;
  195. int i;
  196. uint64_t owner;
  197. uint64_t offset;
  198. unsigned int flags = 0;
  199. bool ignore_off;
  200. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  201. ASSERT(owner != 0);
  202. ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
  203. (flags & XFS_RMAP_BMBT_BLOCK);
  204. if (unwritten)
  205. flags |= XFS_RMAP_UNWRITTEN;
  206. trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len,
  207. unwritten, oinfo);
  208. /*
  209. * For the initial lookup, look for an exact match or the left-adjacent
  210. * record for our insertion point. This will also give us the record for
  211. * start block contiguity tests.
  212. */
  213. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
  214. &have_lt);
  215. if (error)
  216. goto out_error;
  217. XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error);
  218. error = xfs_rmap_get_rec(cur, &ltrec, &have_lt);
  219. if (error)
  220. goto out_error;
  221. XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error);
  222. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  223. cur->bc_private.a.agno, ltrec.rm_startblock,
  224. ltrec.rm_blockcount, ltrec.rm_owner,
  225. ltrec.rm_offset, ltrec.rm_flags);
  226. if (!xfs_rmap_is_mergeable(&ltrec, owner, flags))
  227. have_lt = 0;
  228. XFS_WANT_CORRUPTED_GOTO(mp,
  229. have_lt == 0 ||
  230. ltrec.rm_startblock + ltrec.rm_blockcount <= bno, out_error);
  231. /*
  232. * Increment the cursor to see if we have a right-adjacent record to our
  233. * insertion point. This will give us the record for end block
  234. * contiguity tests.
  235. */
  236. error = xfs_btree_increment(cur, 0, &have_gt);
  237. if (error)
  238. goto out_error;
  239. if (have_gt) {
  240. error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
  241. if (error)
  242. goto out_error;
  243. XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error);
  244. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= gtrec.rm_startblock,
  245. out_error);
  246. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  247. cur->bc_private.a.agno, gtrec.rm_startblock,
  248. gtrec.rm_blockcount, gtrec.rm_owner,
  249. gtrec.rm_offset, gtrec.rm_flags);
  250. if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
  251. have_gt = 0;
  252. }
  253. /*
  254. * Note: cursor currently points one record to the right of ltrec, even
  255. * if there is no record in the tree to the right.
  256. */
  257. if (have_lt &&
  258. ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
  259. (ignore_off || ltrec.rm_offset + ltrec.rm_blockcount == offset)) {
  260. /*
  261. * left edge contiguous, merge into left record.
  262. *
  263. * ltbno ltlen
  264. * orig: |ooooooooo|
  265. * adding: |aaaaaaaaa|
  266. * result: |rrrrrrrrrrrrrrrrrrr|
  267. * bno len
  268. */
  269. ltrec.rm_blockcount += len;
  270. if (have_gt &&
  271. bno + len == gtrec.rm_startblock &&
  272. (ignore_off || offset + len == gtrec.rm_offset) &&
  273. (unsigned long)ltrec.rm_blockcount + len +
  274. gtrec.rm_blockcount <= XFS_RMAP_LEN_MAX) {
  275. /*
  276. * right edge also contiguous, delete right record
  277. * and merge into left record.
  278. *
  279. * ltbno ltlen gtbno gtlen
  280. * orig: |ooooooooo| |ooooooooo|
  281. * adding: |aaaaaaaaa|
  282. * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
  283. */
  284. ltrec.rm_blockcount += gtrec.rm_blockcount;
  285. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  286. gtrec.rm_startblock,
  287. gtrec.rm_blockcount,
  288. gtrec.rm_owner,
  289. gtrec.rm_offset,
  290. gtrec.rm_flags);
  291. error = xfs_btree_delete(cur, &i);
  292. if (error)
  293. goto out_error;
  294. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  295. }
  296. /* point the cursor back to the left record and update */
  297. error = xfs_btree_decrement(cur, 0, &have_gt);
  298. if (error)
  299. goto out_error;
  300. error = xfs_rmap_update(cur, &ltrec);
  301. if (error)
  302. goto out_error;
  303. } else if (have_gt &&
  304. bno + len == gtrec.rm_startblock &&
  305. (ignore_off || offset + len == gtrec.rm_offset)) {
  306. /*
  307. * right edge contiguous, merge into right record.
  308. *
  309. * gtbno gtlen
  310. * Orig: |ooooooooo|
  311. * adding: |aaaaaaaaa|
  312. * Result: |rrrrrrrrrrrrrrrrrrr|
  313. * bno len
  314. */
  315. gtrec.rm_startblock = bno;
  316. gtrec.rm_blockcount += len;
  317. if (!ignore_off)
  318. gtrec.rm_offset = offset;
  319. error = xfs_rmap_update(cur, &gtrec);
  320. if (error)
  321. goto out_error;
  322. } else {
  323. /*
  324. * no contiguous edge with identical owner, insert
  325. * new record at current cursor position.
  326. */
  327. cur->bc_rec.r.rm_startblock = bno;
  328. cur->bc_rec.r.rm_blockcount = len;
  329. cur->bc_rec.r.rm_owner = owner;
  330. cur->bc_rec.r.rm_offset = offset;
  331. cur->bc_rec.r.rm_flags = flags;
  332. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len,
  333. owner, offset, flags);
  334. error = xfs_btree_insert(cur, &i);
  335. if (error)
  336. goto out_error;
  337. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  338. }
  339. trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len,
  340. unwritten, oinfo);
  341. out_error:
  342. if (error)
  343. trace_xfs_rmap_map_error(mp, cur->bc_private.a.agno,
  344. error, _RET_IP_);
  345. return error;
  346. }
  347. /*
  348. * Add a reference to an extent in the rmap btree.
  349. */
  350. int
  351. xfs_rmap_alloc(
  352. struct xfs_trans *tp,
  353. struct xfs_buf *agbp,
  354. xfs_agnumber_t agno,
  355. xfs_agblock_t bno,
  356. xfs_extlen_t len,
  357. struct xfs_owner_info *oinfo)
  358. {
  359. struct xfs_mount *mp = tp->t_mountp;
  360. struct xfs_btree_cur *cur;
  361. int error;
  362. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  363. return 0;
  364. cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  365. error = xfs_rmap_map(cur, bno, len, false, oinfo);
  366. if (error)
  367. goto out_error;
  368. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  369. return 0;
  370. out_error:
  371. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  372. return error;
  373. }
  374. struct xfs_rmap_query_range_info {
  375. xfs_rmap_query_range_fn fn;
  376. void *priv;
  377. };
  378. /* Format btree record and pass to our callback. */
  379. STATIC int
  380. xfs_rmap_query_range_helper(
  381. struct xfs_btree_cur *cur,
  382. union xfs_btree_rec *rec,
  383. void *priv)
  384. {
  385. struct xfs_rmap_query_range_info *query = priv;
  386. struct xfs_rmap_irec irec;
  387. int error;
  388. error = xfs_rmap_btrec_to_irec(rec, &irec);
  389. if (error)
  390. return error;
  391. return query->fn(cur, &irec, query->priv);
  392. }
  393. /* Find all rmaps between two keys. */
  394. int
  395. xfs_rmap_query_range(
  396. struct xfs_btree_cur *cur,
  397. struct xfs_rmap_irec *low_rec,
  398. struct xfs_rmap_irec *high_rec,
  399. xfs_rmap_query_range_fn fn,
  400. void *priv)
  401. {
  402. union xfs_btree_irec low_brec;
  403. union xfs_btree_irec high_brec;
  404. struct xfs_rmap_query_range_info query;
  405. low_brec.r = *low_rec;
  406. high_brec.r = *high_rec;
  407. query.priv = priv;
  408. query.fn = fn;
  409. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  410. xfs_rmap_query_range_helper, &query);
  411. }