xfs_rmap.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. #include "xfs_bmap.h"
  40. #include "xfs_inode.h"
  41. /*
  42. * Lookup the first record less than or equal to [bno, len, owner, offset]
  43. * in the btree given by cur.
  44. */
  45. int
  46. xfs_rmap_lookup_le(
  47. struct xfs_btree_cur *cur,
  48. xfs_agblock_t bno,
  49. xfs_extlen_t len,
  50. uint64_t owner,
  51. uint64_t offset,
  52. unsigned int flags,
  53. int *stat)
  54. {
  55. cur->bc_rec.r.rm_startblock = bno;
  56. cur->bc_rec.r.rm_blockcount = len;
  57. cur->bc_rec.r.rm_owner = owner;
  58. cur->bc_rec.r.rm_offset = offset;
  59. cur->bc_rec.r.rm_flags = flags;
  60. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  61. }
  62. /*
  63. * Lookup the record exactly matching [bno, len, owner, offset]
  64. * in the btree given by cur.
  65. */
  66. int
  67. xfs_rmap_lookup_eq(
  68. struct xfs_btree_cur *cur,
  69. xfs_agblock_t bno,
  70. xfs_extlen_t len,
  71. uint64_t owner,
  72. uint64_t offset,
  73. unsigned int flags,
  74. int *stat)
  75. {
  76. cur->bc_rec.r.rm_startblock = bno;
  77. cur->bc_rec.r.rm_blockcount = len;
  78. cur->bc_rec.r.rm_owner = owner;
  79. cur->bc_rec.r.rm_offset = offset;
  80. cur->bc_rec.r.rm_flags = flags;
  81. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  82. }
  83. /*
  84. * Update the record referred to by cur to the value given
  85. * by [bno, len, owner, offset].
  86. * This either works (return 0) or gets an EFSCORRUPTED error.
  87. */
  88. STATIC int
  89. xfs_rmap_update(
  90. struct xfs_btree_cur *cur,
  91. struct xfs_rmap_irec *irec)
  92. {
  93. union xfs_btree_rec rec;
  94. int error;
  95. trace_xfs_rmap_update(cur->bc_mp, cur->bc_private.a.agno,
  96. irec->rm_startblock, irec->rm_blockcount,
  97. irec->rm_owner, irec->rm_offset, irec->rm_flags);
  98. rec.rmap.rm_startblock = cpu_to_be32(irec->rm_startblock);
  99. rec.rmap.rm_blockcount = cpu_to_be32(irec->rm_blockcount);
  100. rec.rmap.rm_owner = cpu_to_be64(irec->rm_owner);
  101. rec.rmap.rm_offset = cpu_to_be64(
  102. xfs_rmap_irec_offset_pack(irec));
  103. error = xfs_btree_update(cur, &rec);
  104. if (error)
  105. trace_xfs_rmap_update_error(cur->bc_mp,
  106. cur->bc_private.a.agno, error, _RET_IP_);
  107. return error;
  108. }
  109. int
  110. xfs_rmap_insert(
  111. struct xfs_btree_cur *rcur,
  112. xfs_agblock_t agbno,
  113. xfs_extlen_t len,
  114. uint64_t owner,
  115. uint64_t offset,
  116. unsigned int flags)
  117. {
  118. int i;
  119. int error;
  120. trace_xfs_rmap_insert(rcur->bc_mp, rcur->bc_private.a.agno, agbno,
  121. len, owner, offset, flags);
  122. error = xfs_rmap_lookup_eq(rcur, agbno, len, owner, offset, flags, &i);
  123. if (error)
  124. goto done;
  125. XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 0, done);
  126. rcur->bc_rec.r.rm_startblock = agbno;
  127. rcur->bc_rec.r.rm_blockcount = len;
  128. rcur->bc_rec.r.rm_owner = owner;
  129. rcur->bc_rec.r.rm_offset = offset;
  130. rcur->bc_rec.r.rm_flags = flags;
  131. error = xfs_btree_insert(rcur, &i);
  132. if (error)
  133. goto done;
  134. XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 1, done);
  135. done:
  136. if (error)
  137. trace_xfs_rmap_insert_error(rcur->bc_mp,
  138. rcur->bc_private.a.agno, error, _RET_IP_);
  139. return error;
  140. }
  141. static int
  142. xfs_rmap_btrec_to_irec(
  143. union xfs_btree_rec *rec,
  144. struct xfs_rmap_irec *irec)
  145. {
  146. irec->rm_flags = 0;
  147. irec->rm_startblock = be32_to_cpu(rec->rmap.rm_startblock);
  148. irec->rm_blockcount = be32_to_cpu(rec->rmap.rm_blockcount);
  149. irec->rm_owner = be64_to_cpu(rec->rmap.rm_owner);
  150. return xfs_rmap_irec_offset_unpack(be64_to_cpu(rec->rmap.rm_offset),
  151. irec);
  152. }
  153. /*
  154. * Get the data from the pointed-to record.
  155. */
  156. int
  157. xfs_rmap_get_rec(
  158. struct xfs_btree_cur *cur,
  159. struct xfs_rmap_irec *irec,
  160. int *stat)
  161. {
  162. union xfs_btree_rec *rec;
  163. int error;
  164. error = xfs_btree_get_rec(cur, &rec, stat);
  165. if (error || !*stat)
  166. return error;
  167. return xfs_rmap_btrec_to_irec(rec, irec);
  168. }
  169. /*
  170. * Find the extent in the rmap btree and remove it.
  171. *
  172. * The record we find should always be an exact match for the extent that we're
  173. * looking for, since we insert them into the btree without modification.
  174. *
  175. * Special Case #1: when growing the filesystem, we "free" an extent when
  176. * growing the last AG. This extent is new space and so it is not tracked as
  177. * used space in the btree. The growfs code will pass in an owner of
  178. * XFS_RMAP_OWN_NULL to indicate that it expected that there is no owner of this
  179. * extent. We verify that - the extent lookup result in a record that does not
  180. * overlap.
  181. *
  182. * Special Case #2: EFIs do not record the owner of the extent, so when
  183. * recovering EFIs from the log we pass in XFS_RMAP_OWN_UNKNOWN to tell the rmap
  184. * btree to ignore the owner (i.e. wildcard match) so we don't trigger
  185. * corruption checks during log recovery.
  186. */
  187. STATIC int
  188. xfs_rmap_unmap(
  189. struct xfs_btree_cur *cur,
  190. xfs_agblock_t bno,
  191. xfs_extlen_t len,
  192. bool unwritten,
  193. struct xfs_owner_info *oinfo)
  194. {
  195. struct xfs_mount *mp = cur->bc_mp;
  196. struct xfs_rmap_irec ltrec;
  197. uint64_t ltoff;
  198. int error = 0;
  199. int i;
  200. uint64_t owner;
  201. uint64_t offset;
  202. unsigned int flags;
  203. bool ignore_off;
  204. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  205. ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
  206. (flags & XFS_RMAP_BMBT_BLOCK);
  207. if (unwritten)
  208. flags |= XFS_RMAP_UNWRITTEN;
  209. trace_xfs_rmap_unmap(mp, cur->bc_private.a.agno, bno, len,
  210. unwritten, oinfo);
  211. /*
  212. * We should always have a left record because there's a static record
  213. * for the AG headers at rm_startblock == 0 created by mkfs/growfs that
  214. * will not ever be removed from the tree.
  215. */
  216. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags, &i);
  217. if (error)
  218. goto out_error;
  219. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  220. error = xfs_rmap_get_rec(cur, &ltrec, &i);
  221. if (error)
  222. goto out_error;
  223. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  224. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  225. cur->bc_private.a.agno, ltrec.rm_startblock,
  226. ltrec.rm_blockcount, ltrec.rm_owner,
  227. ltrec.rm_offset, ltrec.rm_flags);
  228. ltoff = ltrec.rm_offset;
  229. /*
  230. * For growfs, the incoming extent must be beyond the left record we
  231. * just found as it is new space and won't be used by anyone. This is
  232. * just a corruption check as we don't actually do anything with this
  233. * extent. Note that we need to use >= instead of > because it might
  234. * be the case that the "left" extent goes all the way to EOFS.
  235. */
  236. if (owner == XFS_RMAP_OWN_NULL) {
  237. XFS_WANT_CORRUPTED_GOTO(mp, bno >= ltrec.rm_startblock +
  238. ltrec.rm_blockcount, out_error);
  239. goto out_done;
  240. }
  241. /* Make sure the unwritten flag matches. */
  242. XFS_WANT_CORRUPTED_GOTO(mp, (flags & XFS_RMAP_UNWRITTEN) ==
  243. (ltrec.rm_flags & XFS_RMAP_UNWRITTEN), out_error);
  244. /* Make sure the extent we found covers the entire freeing range. */
  245. XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_startblock <= bno &&
  246. ltrec.rm_startblock + ltrec.rm_blockcount >=
  247. bno + len, out_error);
  248. /* Make sure the owner matches what we expect to find in the tree. */
  249. XFS_WANT_CORRUPTED_GOTO(mp, owner == ltrec.rm_owner ||
  250. XFS_RMAP_NON_INODE_OWNER(owner), out_error);
  251. /* Check the offset, if necessary. */
  252. if (!XFS_RMAP_NON_INODE_OWNER(owner)) {
  253. if (flags & XFS_RMAP_BMBT_BLOCK) {
  254. XFS_WANT_CORRUPTED_GOTO(mp,
  255. ltrec.rm_flags & XFS_RMAP_BMBT_BLOCK,
  256. out_error);
  257. } else {
  258. XFS_WANT_CORRUPTED_GOTO(mp,
  259. ltrec.rm_offset <= offset, out_error);
  260. XFS_WANT_CORRUPTED_GOTO(mp,
  261. ltoff + ltrec.rm_blockcount >= offset + len,
  262. out_error);
  263. }
  264. }
  265. if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) {
  266. /* exact match, simply remove the record from rmap tree */
  267. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  268. ltrec.rm_startblock, ltrec.rm_blockcount,
  269. ltrec.rm_owner, ltrec.rm_offset,
  270. ltrec.rm_flags);
  271. error = xfs_btree_delete(cur, &i);
  272. if (error)
  273. goto out_error;
  274. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  275. } else if (ltrec.rm_startblock == bno) {
  276. /*
  277. * overlap left hand side of extent: move the start, trim the
  278. * length and update the current record.
  279. *
  280. * ltbno ltlen
  281. * Orig: |oooooooooooooooooooo|
  282. * Freeing: |fffffffff|
  283. * Result: |rrrrrrrrrr|
  284. * bno len
  285. */
  286. ltrec.rm_startblock += len;
  287. ltrec.rm_blockcount -= len;
  288. if (!ignore_off)
  289. ltrec.rm_offset += len;
  290. error = xfs_rmap_update(cur, &ltrec);
  291. if (error)
  292. goto out_error;
  293. } else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) {
  294. /*
  295. * overlap right hand side of extent: trim the length and update
  296. * the current record.
  297. *
  298. * ltbno ltlen
  299. * Orig: |oooooooooooooooooooo|
  300. * Freeing: |fffffffff|
  301. * Result: |rrrrrrrrrr|
  302. * bno len
  303. */
  304. ltrec.rm_blockcount -= len;
  305. error = xfs_rmap_update(cur, &ltrec);
  306. if (error)
  307. goto out_error;
  308. } else {
  309. /*
  310. * overlap middle of extent: trim the length of the existing
  311. * record to the length of the new left-extent size, increment
  312. * the insertion position so we can insert a new record
  313. * containing the remaining right-extent space.
  314. *
  315. * ltbno ltlen
  316. * Orig: |oooooooooooooooooooo|
  317. * Freeing: |fffffffff|
  318. * Result: |rrrrr| |rrrr|
  319. * bno len
  320. */
  321. xfs_extlen_t orig_len = ltrec.rm_blockcount;
  322. ltrec.rm_blockcount = bno - ltrec.rm_startblock;
  323. error = xfs_rmap_update(cur, &ltrec);
  324. if (error)
  325. goto out_error;
  326. error = xfs_btree_increment(cur, 0, &i);
  327. if (error)
  328. goto out_error;
  329. cur->bc_rec.r.rm_startblock = bno + len;
  330. cur->bc_rec.r.rm_blockcount = orig_len - len -
  331. ltrec.rm_blockcount;
  332. cur->bc_rec.r.rm_owner = ltrec.rm_owner;
  333. if (ignore_off)
  334. cur->bc_rec.r.rm_offset = 0;
  335. else
  336. cur->bc_rec.r.rm_offset = offset + len;
  337. cur->bc_rec.r.rm_flags = flags;
  338. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno,
  339. cur->bc_rec.r.rm_startblock,
  340. cur->bc_rec.r.rm_blockcount,
  341. cur->bc_rec.r.rm_owner,
  342. cur->bc_rec.r.rm_offset,
  343. cur->bc_rec.r.rm_flags);
  344. error = xfs_btree_insert(cur, &i);
  345. if (error)
  346. goto out_error;
  347. }
  348. out_done:
  349. trace_xfs_rmap_unmap_done(mp, cur->bc_private.a.agno, bno, len,
  350. unwritten, oinfo);
  351. out_error:
  352. if (error)
  353. trace_xfs_rmap_unmap_error(mp, cur->bc_private.a.agno,
  354. error, _RET_IP_);
  355. return error;
  356. }
  357. /*
  358. * Remove a reference to an extent in the rmap btree.
  359. */
  360. int
  361. xfs_rmap_free(
  362. struct xfs_trans *tp,
  363. struct xfs_buf *agbp,
  364. xfs_agnumber_t agno,
  365. xfs_agblock_t bno,
  366. xfs_extlen_t len,
  367. struct xfs_owner_info *oinfo)
  368. {
  369. struct xfs_mount *mp = tp->t_mountp;
  370. struct xfs_btree_cur *cur;
  371. int error;
  372. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  373. return 0;
  374. cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  375. error = xfs_rmap_unmap(cur, bno, len, false, oinfo);
  376. if (error)
  377. goto out_error;
  378. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  379. return 0;
  380. out_error:
  381. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  382. return error;
  383. }
  384. /*
  385. * A mergeable rmap must have the same owner and the same values for
  386. * the unwritten, attr_fork, and bmbt flags. The startblock and
  387. * offset are checked separately.
  388. */
  389. static bool
  390. xfs_rmap_is_mergeable(
  391. struct xfs_rmap_irec *irec,
  392. uint64_t owner,
  393. unsigned int flags)
  394. {
  395. if (irec->rm_owner == XFS_RMAP_OWN_NULL)
  396. return false;
  397. if (irec->rm_owner != owner)
  398. return false;
  399. if ((flags & XFS_RMAP_UNWRITTEN) ^
  400. (irec->rm_flags & XFS_RMAP_UNWRITTEN))
  401. return false;
  402. if ((flags & XFS_RMAP_ATTR_FORK) ^
  403. (irec->rm_flags & XFS_RMAP_ATTR_FORK))
  404. return false;
  405. if ((flags & XFS_RMAP_BMBT_BLOCK) ^
  406. (irec->rm_flags & XFS_RMAP_BMBT_BLOCK))
  407. return false;
  408. return true;
  409. }
  410. /*
  411. * When we allocate a new block, the first thing we do is add a reference to
  412. * the extent in the rmap btree. This takes the form of a [agbno, length,
  413. * owner, offset] record. Flags are encoded in the high bits of the offset
  414. * field.
  415. */
  416. STATIC int
  417. xfs_rmap_map(
  418. struct xfs_btree_cur *cur,
  419. xfs_agblock_t bno,
  420. xfs_extlen_t len,
  421. bool unwritten,
  422. struct xfs_owner_info *oinfo)
  423. {
  424. struct xfs_mount *mp = cur->bc_mp;
  425. struct xfs_rmap_irec ltrec;
  426. struct xfs_rmap_irec gtrec;
  427. int have_gt;
  428. int have_lt;
  429. int error = 0;
  430. int i;
  431. uint64_t owner;
  432. uint64_t offset;
  433. unsigned int flags = 0;
  434. bool ignore_off;
  435. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  436. ASSERT(owner != 0);
  437. ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
  438. (flags & XFS_RMAP_BMBT_BLOCK);
  439. if (unwritten)
  440. flags |= XFS_RMAP_UNWRITTEN;
  441. trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len,
  442. unwritten, oinfo);
  443. /*
  444. * For the initial lookup, look for an exact match or the left-adjacent
  445. * record for our insertion point. This will also give us the record for
  446. * start block contiguity tests.
  447. */
  448. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
  449. &have_lt);
  450. if (error)
  451. goto out_error;
  452. XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error);
  453. error = xfs_rmap_get_rec(cur, &ltrec, &have_lt);
  454. if (error)
  455. goto out_error;
  456. XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error);
  457. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  458. cur->bc_private.a.agno, ltrec.rm_startblock,
  459. ltrec.rm_blockcount, ltrec.rm_owner,
  460. ltrec.rm_offset, ltrec.rm_flags);
  461. if (!xfs_rmap_is_mergeable(&ltrec, owner, flags))
  462. have_lt = 0;
  463. XFS_WANT_CORRUPTED_GOTO(mp,
  464. have_lt == 0 ||
  465. ltrec.rm_startblock + ltrec.rm_blockcount <= bno, out_error);
  466. /*
  467. * Increment the cursor to see if we have a right-adjacent record to our
  468. * insertion point. This will give us the record for end block
  469. * contiguity tests.
  470. */
  471. error = xfs_btree_increment(cur, 0, &have_gt);
  472. if (error)
  473. goto out_error;
  474. if (have_gt) {
  475. error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
  476. if (error)
  477. goto out_error;
  478. XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error);
  479. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= gtrec.rm_startblock,
  480. out_error);
  481. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  482. cur->bc_private.a.agno, gtrec.rm_startblock,
  483. gtrec.rm_blockcount, gtrec.rm_owner,
  484. gtrec.rm_offset, gtrec.rm_flags);
  485. if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
  486. have_gt = 0;
  487. }
  488. /*
  489. * Note: cursor currently points one record to the right of ltrec, even
  490. * if there is no record in the tree to the right.
  491. */
  492. if (have_lt &&
  493. ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
  494. (ignore_off || ltrec.rm_offset + ltrec.rm_blockcount == offset)) {
  495. /*
  496. * left edge contiguous, merge into left record.
  497. *
  498. * ltbno ltlen
  499. * orig: |ooooooooo|
  500. * adding: |aaaaaaaaa|
  501. * result: |rrrrrrrrrrrrrrrrrrr|
  502. * bno len
  503. */
  504. ltrec.rm_blockcount += len;
  505. if (have_gt &&
  506. bno + len == gtrec.rm_startblock &&
  507. (ignore_off || offset + len == gtrec.rm_offset) &&
  508. (unsigned long)ltrec.rm_blockcount + len +
  509. gtrec.rm_blockcount <= XFS_RMAP_LEN_MAX) {
  510. /*
  511. * right edge also contiguous, delete right record
  512. * and merge into left record.
  513. *
  514. * ltbno ltlen gtbno gtlen
  515. * orig: |ooooooooo| |ooooooooo|
  516. * adding: |aaaaaaaaa|
  517. * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
  518. */
  519. ltrec.rm_blockcount += gtrec.rm_blockcount;
  520. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  521. gtrec.rm_startblock,
  522. gtrec.rm_blockcount,
  523. gtrec.rm_owner,
  524. gtrec.rm_offset,
  525. gtrec.rm_flags);
  526. error = xfs_btree_delete(cur, &i);
  527. if (error)
  528. goto out_error;
  529. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  530. }
  531. /* point the cursor back to the left record and update */
  532. error = xfs_btree_decrement(cur, 0, &have_gt);
  533. if (error)
  534. goto out_error;
  535. error = xfs_rmap_update(cur, &ltrec);
  536. if (error)
  537. goto out_error;
  538. } else if (have_gt &&
  539. bno + len == gtrec.rm_startblock &&
  540. (ignore_off || offset + len == gtrec.rm_offset)) {
  541. /*
  542. * right edge contiguous, merge into right record.
  543. *
  544. * gtbno gtlen
  545. * Orig: |ooooooooo|
  546. * adding: |aaaaaaaaa|
  547. * Result: |rrrrrrrrrrrrrrrrrrr|
  548. * bno len
  549. */
  550. gtrec.rm_startblock = bno;
  551. gtrec.rm_blockcount += len;
  552. if (!ignore_off)
  553. gtrec.rm_offset = offset;
  554. error = xfs_rmap_update(cur, &gtrec);
  555. if (error)
  556. goto out_error;
  557. } else {
  558. /*
  559. * no contiguous edge with identical owner, insert
  560. * new record at current cursor position.
  561. */
  562. cur->bc_rec.r.rm_startblock = bno;
  563. cur->bc_rec.r.rm_blockcount = len;
  564. cur->bc_rec.r.rm_owner = owner;
  565. cur->bc_rec.r.rm_offset = offset;
  566. cur->bc_rec.r.rm_flags = flags;
  567. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len,
  568. owner, offset, flags);
  569. error = xfs_btree_insert(cur, &i);
  570. if (error)
  571. goto out_error;
  572. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  573. }
  574. trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len,
  575. unwritten, oinfo);
  576. out_error:
  577. if (error)
  578. trace_xfs_rmap_map_error(mp, cur->bc_private.a.agno,
  579. error, _RET_IP_);
  580. return error;
  581. }
  582. /*
  583. * Add a reference to an extent in the rmap btree.
  584. */
  585. int
  586. xfs_rmap_alloc(
  587. struct xfs_trans *tp,
  588. struct xfs_buf *agbp,
  589. xfs_agnumber_t agno,
  590. xfs_agblock_t bno,
  591. xfs_extlen_t len,
  592. struct xfs_owner_info *oinfo)
  593. {
  594. struct xfs_mount *mp = tp->t_mountp;
  595. struct xfs_btree_cur *cur;
  596. int error;
  597. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  598. return 0;
  599. cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  600. error = xfs_rmap_map(cur, bno, len, false, oinfo);
  601. if (error)
  602. goto out_error;
  603. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  604. return 0;
  605. out_error:
  606. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  607. return error;
  608. }
  609. #define RMAP_LEFT_CONTIG (1 << 0)
  610. #define RMAP_RIGHT_CONTIG (1 << 1)
  611. #define RMAP_LEFT_FILLING (1 << 2)
  612. #define RMAP_RIGHT_FILLING (1 << 3)
  613. #define RMAP_LEFT_VALID (1 << 6)
  614. #define RMAP_RIGHT_VALID (1 << 7)
  615. #define LEFT r[0]
  616. #define RIGHT r[1]
  617. #define PREV r[2]
  618. #define NEW r[3]
  619. /*
  620. * Convert an unwritten extent to a real extent or vice versa.
  621. * Does not handle overlapping extents.
  622. */
  623. STATIC int
  624. xfs_rmap_convert(
  625. struct xfs_btree_cur *cur,
  626. xfs_agblock_t bno,
  627. xfs_extlen_t len,
  628. bool unwritten,
  629. struct xfs_owner_info *oinfo)
  630. {
  631. struct xfs_mount *mp = cur->bc_mp;
  632. struct xfs_rmap_irec r[4]; /* neighbor extent entries */
  633. /* left is 0, right is 1, prev is 2 */
  634. /* new is 3 */
  635. uint64_t owner;
  636. uint64_t offset;
  637. uint64_t new_endoff;
  638. unsigned int oldext;
  639. unsigned int newext;
  640. unsigned int flags = 0;
  641. int i;
  642. int state = 0;
  643. int error;
  644. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  645. ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) ||
  646. (flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))));
  647. oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0;
  648. new_endoff = offset + len;
  649. trace_xfs_rmap_convert(mp, cur->bc_private.a.agno, bno, len,
  650. unwritten, oinfo);
  651. /*
  652. * For the initial lookup, look for an exact match or the left-adjacent
  653. * record for our insertion point. This will also give us the record for
  654. * start block contiguity tests.
  655. */
  656. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
  657. if (error)
  658. goto done;
  659. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  660. error = xfs_rmap_get_rec(cur, &PREV, &i);
  661. if (error)
  662. goto done;
  663. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  664. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  665. cur->bc_private.a.agno, PREV.rm_startblock,
  666. PREV.rm_blockcount, PREV.rm_owner,
  667. PREV.rm_offset, PREV.rm_flags);
  668. ASSERT(PREV.rm_offset <= offset);
  669. ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff);
  670. ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext);
  671. newext = ~oldext & XFS_RMAP_UNWRITTEN;
  672. /*
  673. * Set flags determining what part of the previous oldext allocation
  674. * extent is being replaced by a newext allocation.
  675. */
  676. if (PREV.rm_offset == offset)
  677. state |= RMAP_LEFT_FILLING;
  678. if (PREV.rm_offset + PREV.rm_blockcount == new_endoff)
  679. state |= RMAP_RIGHT_FILLING;
  680. /*
  681. * Decrement the cursor to see if we have a left-adjacent record to our
  682. * insertion point. This will give us the record for end block
  683. * contiguity tests.
  684. */
  685. error = xfs_btree_decrement(cur, 0, &i);
  686. if (error)
  687. goto done;
  688. if (i) {
  689. state |= RMAP_LEFT_VALID;
  690. error = xfs_rmap_get_rec(cur, &LEFT, &i);
  691. if (error)
  692. goto done;
  693. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  694. XFS_WANT_CORRUPTED_GOTO(mp,
  695. LEFT.rm_startblock + LEFT.rm_blockcount <= bno,
  696. done);
  697. trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp,
  698. cur->bc_private.a.agno, LEFT.rm_startblock,
  699. LEFT.rm_blockcount, LEFT.rm_owner,
  700. LEFT.rm_offset, LEFT.rm_flags);
  701. if (LEFT.rm_startblock + LEFT.rm_blockcount == bno &&
  702. LEFT.rm_offset + LEFT.rm_blockcount == offset &&
  703. xfs_rmap_is_mergeable(&LEFT, owner, newext))
  704. state |= RMAP_LEFT_CONTIG;
  705. }
  706. /*
  707. * Increment the cursor to see if we have a right-adjacent record to our
  708. * insertion point. This will give us the record for end block
  709. * contiguity tests.
  710. */
  711. error = xfs_btree_increment(cur, 0, &i);
  712. if (error)
  713. goto done;
  714. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  715. error = xfs_btree_increment(cur, 0, &i);
  716. if (error)
  717. goto done;
  718. if (i) {
  719. state |= RMAP_RIGHT_VALID;
  720. error = xfs_rmap_get_rec(cur, &RIGHT, &i);
  721. if (error)
  722. goto done;
  723. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  724. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= RIGHT.rm_startblock,
  725. done);
  726. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  727. cur->bc_private.a.agno, RIGHT.rm_startblock,
  728. RIGHT.rm_blockcount, RIGHT.rm_owner,
  729. RIGHT.rm_offset, RIGHT.rm_flags);
  730. if (bno + len == RIGHT.rm_startblock &&
  731. offset + len == RIGHT.rm_offset &&
  732. xfs_rmap_is_mergeable(&RIGHT, owner, newext))
  733. state |= RMAP_RIGHT_CONTIG;
  734. }
  735. /* check that left + prev + right is not too long */
  736. if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  737. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) ==
  738. (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  739. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) &&
  740. (unsigned long)LEFT.rm_blockcount + len +
  741. RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX)
  742. state &= ~RMAP_RIGHT_CONTIG;
  743. trace_xfs_rmap_convert_state(mp, cur->bc_private.a.agno, state,
  744. _RET_IP_);
  745. /* reset the cursor back to PREV */
  746. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
  747. if (error)
  748. goto done;
  749. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  750. /*
  751. * Switch out based on the FILLING and CONTIG state bits.
  752. */
  753. switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  754. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) {
  755. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  756. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  757. /*
  758. * Setting all of a previous oldext extent to newext.
  759. * The left and right neighbors are both contiguous with new.
  760. */
  761. error = xfs_btree_increment(cur, 0, &i);
  762. if (error)
  763. goto done;
  764. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  765. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  766. RIGHT.rm_startblock, RIGHT.rm_blockcount,
  767. RIGHT.rm_owner, RIGHT.rm_offset,
  768. RIGHT.rm_flags);
  769. error = xfs_btree_delete(cur, &i);
  770. if (error)
  771. goto done;
  772. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  773. error = xfs_btree_decrement(cur, 0, &i);
  774. if (error)
  775. goto done;
  776. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  777. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  778. PREV.rm_startblock, PREV.rm_blockcount,
  779. PREV.rm_owner, PREV.rm_offset,
  780. PREV.rm_flags);
  781. error = xfs_btree_delete(cur, &i);
  782. if (error)
  783. goto done;
  784. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  785. error = xfs_btree_decrement(cur, 0, &i);
  786. if (error)
  787. goto done;
  788. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  789. NEW = LEFT;
  790. NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount;
  791. error = xfs_rmap_update(cur, &NEW);
  792. if (error)
  793. goto done;
  794. break;
  795. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  796. /*
  797. * Setting all of a previous oldext extent to newext.
  798. * The left neighbor is contiguous, the right is not.
  799. */
  800. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  801. PREV.rm_startblock, PREV.rm_blockcount,
  802. PREV.rm_owner, PREV.rm_offset,
  803. PREV.rm_flags);
  804. error = xfs_btree_delete(cur, &i);
  805. if (error)
  806. goto done;
  807. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  808. error = xfs_btree_decrement(cur, 0, &i);
  809. if (error)
  810. goto done;
  811. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  812. NEW = LEFT;
  813. NEW.rm_blockcount += PREV.rm_blockcount;
  814. error = xfs_rmap_update(cur, &NEW);
  815. if (error)
  816. goto done;
  817. break;
  818. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  819. /*
  820. * Setting all of a previous oldext extent to newext.
  821. * The right neighbor is contiguous, the left is not.
  822. */
  823. error = xfs_btree_increment(cur, 0, &i);
  824. if (error)
  825. goto done;
  826. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  827. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  828. RIGHT.rm_startblock, RIGHT.rm_blockcount,
  829. RIGHT.rm_owner, RIGHT.rm_offset,
  830. RIGHT.rm_flags);
  831. error = xfs_btree_delete(cur, &i);
  832. if (error)
  833. goto done;
  834. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  835. error = xfs_btree_decrement(cur, 0, &i);
  836. if (error)
  837. goto done;
  838. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  839. NEW = PREV;
  840. NEW.rm_blockcount = len + RIGHT.rm_blockcount;
  841. NEW.rm_flags = newext;
  842. error = xfs_rmap_update(cur, &NEW);
  843. if (error)
  844. goto done;
  845. break;
  846. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING:
  847. /*
  848. * Setting all of a previous oldext extent to newext.
  849. * Neither the left nor right neighbors are contiguous with
  850. * the new one.
  851. */
  852. NEW = PREV;
  853. NEW.rm_flags = newext;
  854. error = xfs_rmap_update(cur, &NEW);
  855. if (error)
  856. goto done;
  857. break;
  858. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG:
  859. /*
  860. * Setting the first part of a previous oldext extent to newext.
  861. * The left neighbor is contiguous.
  862. */
  863. NEW = PREV;
  864. NEW.rm_offset += len;
  865. NEW.rm_startblock += len;
  866. NEW.rm_blockcount -= len;
  867. error = xfs_rmap_update(cur, &NEW);
  868. if (error)
  869. goto done;
  870. error = xfs_btree_decrement(cur, 0, &i);
  871. if (error)
  872. goto done;
  873. NEW = LEFT;
  874. NEW.rm_blockcount += len;
  875. error = xfs_rmap_update(cur, &NEW);
  876. if (error)
  877. goto done;
  878. break;
  879. case RMAP_LEFT_FILLING:
  880. /*
  881. * Setting the first part of a previous oldext extent to newext.
  882. * The left neighbor is not contiguous.
  883. */
  884. NEW = PREV;
  885. NEW.rm_startblock += len;
  886. NEW.rm_offset += len;
  887. NEW.rm_blockcount -= len;
  888. error = xfs_rmap_update(cur, &NEW);
  889. if (error)
  890. goto done;
  891. NEW.rm_startblock = bno;
  892. NEW.rm_owner = owner;
  893. NEW.rm_offset = offset;
  894. NEW.rm_blockcount = len;
  895. NEW.rm_flags = newext;
  896. cur->bc_rec.r = NEW;
  897. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno,
  898. len, owner, offset, newext);
  899. error = xfs_btree_insert(cur, &i);
  900. if (error)
  901. goto done;
  902. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  903. break;
  904. case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  905. /*
  906. * Setting the last part of a previous oldext extent to newext.
  907. * The right neighbor is contiguous with the new allocation.
  908. */
  909. NEW = PREV;
  910. NEW.rm_blockcount -= len;
  911. error = xfs_rmap_update(cur, &NEW);
  912. if (error)
  913. goto done;
  914. error = xfs_btree_increment(cur, 0, &i);
  915. if (error)
  916. goto done;
  917. NEW = RIGHT;
  918. NEW.rm_offset = offset;
  919. NEW.rm_startblock = bno;
  920. NEW.rm_blockcount += len;
  921. error = xfs_rmap_update(cur, &NEW);
  922. if (error)
  923. goto done;
  924. break;
  925. case RMAP_RIGHT_FILLING:
  926. /*
  927. * Setting the last part of a previous oldext extent to newext.
  928. * The right neighbor is not contiguous.
  929. */
  930. NEW = PREV;
  931. NEW.rm_blockcount -= len;
  932. error = xfs_rmap_update(cur, &NEW);
  933. if (error)
  934. goto done;
  935. error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
  936. oldext, &i);
  937. if (error)
  938. goto done;
  939. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  940. NEW.rm_startblock = bno;
  941. NEW.rm_owner = owner;
  942. NEW.rm_offset = offset;
  943. NEW.rm_blockcount = len;
  944. NEW.rm_flags = newext;
  945. cur->bc_rec.r = NEW;
  946. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno,
  947. len, owner, offset, newext);
  948. error = xfs_btree_insert(cur, &i);
  949. if (error)
  950. goto done;
  951. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  952. break;
  953. case 0:
  954. /*
  955. * Setting the middle part of a previous oldext extent to
  956. * newext. Contiguity is impossible here.
  957. * One extent becomes three extents.
  958. */
  959. /* new right extent - oldext */
  960. NEW.rm_startblock = bno + len;
  961. NEW.rm_owner = owner;
  962. NEW.rm_offset = new_endoff;
  963. NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount -
  964. new_endoff;
  965. NEW.rm_flags = PREV.rm_flags;
  966. error = xfs_rmap_update(cur, &NEW);
  967. if (error)
  968. goto done;
  969. /* new left extent - oldext */
  970. NEW = PREV;
  971. NEW.rm_blockcount = offset - PREV.rm_offset;
  972. cur->bc_rec.r = NEW;
  973. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno,
  974. NEW.rm_startblock, NEW.rm_blockcount,
  975. NEW.rm_owner, NEW.rm_offset,
  976. NEW.rm_flags);
  977. error = xfs_btree_insert(cur, &i);
  978. if (error)
  979. goto done;
  980. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  981. /*
  982. * Reset the cursor to the position of the new extent
  983. * we are about to insert as we can't trust it after
  984. * the previous insert.
  985. */
  986. error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
  987. oldext, &i);
  988. if (error)
  989. goto done;
  990. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  991. /* new middle extent - newext */
  992. cur->bc_rec.r.rm_flags &= ~XFS_RMAP_UNWRITTEN;
  993. cur->bc_rec.r.rm_flags |= newext;
  994. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len,
  995. owner, offset, newext);
  996. error = xfs_btree_insert(cur, &i);
  997. if (error)
  998. goto done;
  999. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1000. break;
  1001. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1002. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1003. case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG:
  1004. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1005. case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1006. case RMAP_LEFT_CONTIG:
  1007. case RMAP_RIGHT_CONTIG:
  1008. /*
  1009. * These cases are all impossible.
  1010. */
  1011. ASSERT(0);
  1012. }
  1013. trace_xfs_rmap_convert_done(mp, cur->bc_private.a.agno, bno, len,
  1014. unwritten, oinfo);
  1015. done:
  1016. if (error)
  1017. trace_xfs_rmap_convert_error(cur->bc_mp,
  1018. cur->bc_private.a.agno, error, _RET_IP_);
  1019. return error;
  1020. }
  1021. #undef NEW
  1022. #undef LEFT
  1023. #undef RIGHT
  1024. #undef PREV
  1025. struct xfs_rmap_query_range_info {
  1026. xfs_rmap_query_range_fn fn;
  1027. void *priv;
  1028. };
  1029. /* Format btree record and pass to our callback. */
  1030. STATIC int
  1031. xfs_rmap_query_range_helper(
  1032. struct xfs_btree_cur *cur,
  1033. union xfs_btree_rec *rec,
  1034. void *priv)
  1035. {
  1036. struct xfs_rmap_query_range_info *query = priv;
  1037. struct xfs_rmap_irec irec;
  1038. int error;
  1039. error = xfs_rmap_btrec_to_irec(rec, &irec);
  1040. if (error)
  1041. return error;
  1042. return query->fn(cur, &irec, query->priv);
  1043. }
  1044. /* Find all rmaps between two keys. */
  1045. int
  1046. xfs_rmap_query_range(
  1047. struct xfs_btree_cur *cur,
  1048. struct xfs_rmap_irec *low_rec,
  1049. struct xfs_rmap_irec *high_rec,
  1050. xfs_rmap_query_range_fn fn,
  1051. void *priv)
  1052. {
  1053. union xfs_btree_irec low_brec;
  1054. union xfs_btree_irec high_brec;
  1055. struct xfs_rmap_query_range_info query;
  1056. low_brec.r = *low_rec;
  1057. high_brec.r = *high_rec;
  1058. query.priv = priv;
  1059. query.fn = fn;
  1060. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  1061. xfs_rmap_query_range_helper, &query);
  1062. }
  1063. /* Clean up after calling xfs_rmap_finish_one. */
  1064. void
  1065. xfs_rmap_finish_one_cleanup(
  1066. struct xfs_trans *tp,
  1067. struct xfs_btree_cur *rcur,
  1068. int error)
  1069. {
  1070. struct xfs_buf *agbp;
  1071. if (rcur == NULL)
  1072. return;
  1073. agbp = rcur->bc_private.a.agbp;
  1074. xfs_btree_del_cursor(rcur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  1075. if (error)
  1076. xfs_trans_brelse(tp, agbp);
  1077. }
  1078. /*
  1079. * Process one of the deferred rmap operations. We pass back the
  1080. * btree cursor to maintain our lock on the rmapbt between calls.
  1081. * This saves time and eliminates a buffer deadlock between the
  1082. * superblock and the AGF because we'll always grab them in the same
  1083. * order.
  1084. */
  1085. int
  1086. xfs_rmap_finish_one(
  1087. struct xfs_trans *tp,
  1088. enum xfs_rmap_intent_type type,
  1089. __uint64_t owner,
  1090. int whichfork,
  1091. xfs_fileoff_t startoff,
  1092. xfs_fsblock_t startblock,
  1093. xfs_filblks_t blockcount,
  1094. xfs_exntst_t state,
  1095. struct xfs_btree_cur **pcur)
  1096. {
  1097. struct xfs_mount *mp = tp->t_mountp;
  1098. struct xfs_btree_cur *rcur;
  1099. struct xfs_buf *agbp = NULL;
  1100. int error = 0;
  1101. xfs_agnumber_t agno;
  1102. struct xfs_owner_info oinfo;
  1103. xfs_agblock_t bno;
  1104. bool unwritten;
  1105. agno = XFS_FSB_TO_AGNO(mp, startblock);
  1106. ASSERT(agno != NULLAGNUMBER);
  1107. bno = XFS_FSB_TO_AGBNO(mp, startblock);
  1108. trace_xfs_rmap_deferred(mp, agno, type, bno, owner, whichfork,
  1109. startoff, blockcount, state);
  1110. if (XFS_TEST_ERROR(false, mp,
  1111. XFS_ERRTAG_RMAP_FINISH_ONE,
  1112. XFS_RANDOM_RMAP_FINISH_ONE))
  1113. return -EIO;
  1114. /*
  1115. * If we haven't gotten a cursor or the cursor AG doesn't match
  1116. * the startblock, get one now.
  1117. */
  1118. rcur = *pcur;
  1119. if (rcur != NULL && rcur->bc_private.a.agno != agno) {
  1120. xfs_rmap_finish_one_cleanup(tp, rcur, 0);
  1121. rcur = NULL;
  1122. *pcur = NULL;
  1123. }
  1124. if (rcur == NULL) {
  1125. /*
  1126. * Refresh the freelist before we start changing the
  1127. * rmapbt, because a shape change could cause us to
  1128. * allocate blocks.
  1129. */
  1130. error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
  1131. if (error)
  1132. return error;
  1133. if (!agbp)
  1134. return -EFSCORRUPTED;
  1135. rcur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  1136. if (!rcur) {
  1137. error = -ENOMEM;
  1138. goto out_cur;
  1139. }
  1140. }
  1141. *pcur = rcur;
  1142. xfs_rmap_ino_owner(&oinfo, owner, whichfork, startoff);
  1143. unwritten = state == XFS_EXT_UNWRITTEN;
  1144. bno = XFS_FSB_TO_AGBNO(rcur->bc_mp, startblock);
  1145. switch (type) {
  1146. case XFS_RMAP_ALLOC:
  1147. case XFS_RMAP_MAP:
  1148. error = xfs_rmap_map(rcur, bno, blockcount, unwritten, &oinfo);
  1149. break;
  1150. case XFS_RMAP_FREE:
  1151. case XFS_RMAP_UNMAP:
  1152. error = xfs_rmap_unmap(rcur, bno, blockcount, unwritten,
  1153. &oinfo);
  1154. break;
  1155. case XFS_RMAP_CONVERT:
  1156. error = xfs_rmap_convert(rcur, bno, blockcount, !unwritten,
  1157. &oinfo);
  1158. break;
  1159. default:
  1160. ASSERT(0);
  1161. error = -EFSCORRUPTED;
  1162. }
  1163. return error;
  1164. out_cur:
  1165. xfs_trans_brelse(tp, agbp);
  1166. return error;
  1167. }
  1168. /*
  1169. * Don't defer an rmap if we aren't an rmap filesystem.
  1170. */
  1171. static bool
  1172. xfs_rmap_update_is_needed(
  1173. struct xfs_mount *mp)
  1174. {
  1175. return xfs_sb_version_hasrmapbt(&mp->m_sb);
  1176. }
  1177. /*
  1178. * Record a rmap intent; the list is kept sorted first by AG and then by
  1179. * increasing age.
  1180. */
  1181. static int
  1182. __xfs_rmap_add(
  1183. struct xfs_mount *mp,
  1184. struct xfs_defer_ops *dfops,
  1185. enum xfs_rmap_intent_type type,
  1186. __uint64_t owner,
  1187. int whichfork,
  1188. struct xfs_bmbt_irec *bmap)
  1189. {
  1190. struct xfs_rmap_intent *ri;
  1191. trace_xfs_rmap_defer(mp, XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
  1192. type,
  1193. XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
  1194. owner, whichfork,
  1195. bmap->br_startoff,
  1196. bmap->br_blockcount,
  1197. bmap->br_state);
  1198. ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_SLEEP | KM_NOFS);
  1199. INIT_LIST_HEAD(&ri->ri_list);
  1200. ri->ri_type = type;
  1201. ri->ri_owner = owner;
  1202. ri->ri_whichfork = whichfork;
  1203. ri->ri_bmap = *bmap;
  1204. xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_RMAP, &ri->ri_list);
  1205. return 0;
  1206. }
  1207. /* Map an extent into a file. */
  1208. int
  1209. xfs_rmap_map_extent(
  1210. struct xfs_mount *mp,
  1211. struct xfs_defer_ops *dfops,
  1212. struct xfs_inode *ip,
  1213. int whichfork,
  1214. struct xfs_bmbt_irec *PREV)
  1215. {
  1216. if (!xfs_rmap_update_is_needed(mp))
  1217. return 0;
  1218. return __xfs_rmap_add(mp, dfops, XFS_RMAP_MAP, ip->i_ino,
  1219. whichfork, PREV);
  1220. }
  1221. /* Unmap an extent out of a file. */
  1222. int
  1223. xfs_rmap_unmap_extent(
  1224. struct xfs_mount *mp,
  1225. struct xfs_defer_ops *dfops,
  1226. struct xfs_inode *ip,
  1227. int whichfork,
  1228. struct xfs_bmbt_irec *PREV)
  1229. {
  1230. if (!xfs_rmap_update_is_needed(mp))
  1231. return 0;
  1232. return __xfs_rmap_add(mp, dfops, XFS_RMAP_UNMAP, ip->i_ino,
  1233. whichfork, PREV);
  1234. }
  1235. /* Convert a data fork extent from unwritten to real or vice versa. */
  1236. int
  1237. xfs_rmap_convert_extent(
  1238. struct xfs_mount *mp,
  1239. struct xfs_defer_ops *dfops,
  1240. struct xfs_inode *ip,
  1241. int whichfork,
  1242. struct xfs_bmbt_irec *PREV)
  1243. {
  1244. if (!xfs_rmap_update_is_needed(mp))
  1245. return 0;
  1246. return __xfs_rmap_add(mp, dfops, XFS_RMAP_CONVERT, ip->i_ino,
  1247. whichfork, PREV);
  1248. }
  1249. /* Schedule the creation of an rmap for non-file data. */
  1250. int
  1251. xfs_rmap_alloc_extent(
  1252. struct xfs_mount *mp,
  1253. struct xfs_defer_ops *dfops,
  1254. xfs_agnumber_t agno,
  1255. xfs_agblock_t bno,
  1256. xfs_extlen_t len,
  1257. __uint64_t owner)
  1258. {
  1259. struct xfs_bmbt_irec bmap;
  1260. if (!xfs_rmap_update_is_needed(mp))
  1261. return 0;
  1262. bmap.br_startblock = XFS_AGB_TO_FSB(mp, agno, bno);
  1263. bmap.br_blockcount = len;
  1264. bmap.br_startoff = 0;
  1265. bmap.br_state = XFS_EXT_NORM;
  1266. return __xfs_rmap_add(mp, dfops, XFS_RMAP_ALLOC, owner,
  1267. XFS_DATA_FORK, &bmap);
  1268. }
  1269. /* Schedule the deletion of an rmap for non-file data. */
  1270. int
  1271. xfs_rmap_free_extent(
  1272. struct xfs_mount *mp,
  1273. struct xfs_defer_ops *dfops,
  1274. xfs_agnumber_t agno,
  1275. xfs_agblock_t bno,
  1276. xfs_extlen_t len,
  1277. __uint64_t owner)
  1278. {
  1279. struct xfs_bmbt_irec bmap;
  1280. if (!xfs_rmap_update_is_needed(mp))
  1281. return 0;
  1282. bmap.br_startblock = XFS_AGB_TO_FSB(mp, agno, bno);
  1283. bmap.br_blockcount = len;
  1284. bmap.br_startoff = 0;
  1285. bmap.br_state = XFS_EXT_NORM;
  1286. return __xfs_rmap_add(mp, dfops, XFS_RMAP_FREE, owner,
  1287. XFS_DATA_FORK, &bmap);
  1288. }