xfs_rmap.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014 Red Hat, Inc.
  4. * All Rights Reserved.
  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_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_da_format.h"
  17. #include "xfs_da_btree.h"
  18. #include "xfs_btree.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_alloc.h"
  21. #include "xfs_rmap.h"
  22. #include "xfs_rmap_btree.h"
  23. #include "xfs_trans_space.h"
  24. #include "xfs_trace.h"
  25. #include "xfs_errortag.h"
  26. #include "xfs_error.h"
  27. #include "xfs_extent_busy.h"
  28. #include "xfs_bmap.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_ialloc.h"
  31. /*
  32. * Lookup the first record less than or equal to [bno, len, owner, offset]
  33. * in the btree given by cur.
  34. */
  35. int
  36. xfs_rmap_lookup_le(
  37. struct xfs_btree_cur *cur,
  38. xfs_agblock_t bno,
  39. xfs_extlen_t len,
  40. uint64_t owner,
  41. uint64_t offset,
  42. unsigned int flags,
  43. int *stat)
  44. {
  45. cur->bc_rec.r.rm_startblock = bno;
  46. cur->bc_rec.r.rm_blockcount = len;
  47. cur->bc_rec.r.rm_owner = owner;
  48. cur->bc_rec.r.rm_offset = offset;
  49. cur->bc_rec.r.rm_flags = flags;
  50. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  51. }
  52. /*
  53. * Lookup the record exactly matching [bno, len, owner, offset]
  54. * in the btree given by cur.
  55. */
  56. int
  57. xfs_rmap_lookup_eq(
  58. struct xfs_btree_cur *cur,
  59. xfs_agblock_t bno,
  60. xfs_extlen_t len,
  61. uint64_t owner,
  62. uint64_t offset,
  63. unsigned int flags,
  64. int *stat)
  65. {
  66. cur->bc_rec.r.rm_startblock = bno;
  67. cur->bc_rec.r.rm_blockcount = len;
  68. cur->bc_rec.r.rm_owner = owner;
  69. cur->bc_rec.r.rm_offset = offset;
  70. cur->bc_rec.r.rm_flags = flags;
  71. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  72. }
  73. /*
  74. * Update the record referred to by cur to the value given
  75. * by [bno, len, owner, offset].
  76. * This either works (return 0) or gets an EFSCORRUPTED error.
  77. */
  78. STATIC int
  79. xfs_rmap_update(
  80. struct xfs_btree_cur *cur,
  81. struct xfs_rmap_irec *irec)
  82. {
  83. union xfs_btree_rec rec;
  84. int error;
  85. trace_xfs_rmap_update(cur->bc_mp, cur->bc_private.a.agno,
  86. irec->rm_startblock, irec->rm_blockcount,
  87. irec->rm_owner, irec->rm_offset, irec->rm_flags);
  88. rec.rmap.rm_startblock = cpu_to_be32(irec->rm_startblock);
  89. rec.rmap.rm_blockcount = cpu_to_be32(irec->rm_blockcount);
  90. rec.rmap.rm_owner = cpu_to_be64(irec->rm_owner);
  91. rec.rmap.rm_offset = cpu_to_be64(
  92. xfs_rmap_irec_offset_pack(irec));
  93. error = xfs_btree_update(cur, &rec);
  94. if (error)
  95. trace_xfs_rmap_update_error(cur->bc_mp,
  96. cur->bc_private.a.agno, error, _RET_IP_);
  97. return error;
  98. }
  99. int
  100. xfs_rmap_insert(
  101. struct xfs_btree_cur *rcur,
  102. xfs_agblock_t agbno,
  103. xfs_extlen_t len,
  104. uint64_t owner,
  105. uint64_t offset,
  106. unsigned int flags)
  107. {
  108. int i;
  109. int error;
  110. trace_xfs_rmap_insert(rcur->bc_mp, rcur->bc_private.a.agno, agbno,
  111. len, owner, offset, flags);
  112. error = xfs_rmap_lookup_eq(rcur, agbno, len, owner, offset, flags, &i);
  113. if (error)
  114. goto done;
  115. XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 0, done);
  116. rcur->bc_rec.r.rm_startblock = agbno;
  117. rcur->bc_rec.r.rm_blockcount = len;
  118. rcur->bc_rec.r.rm_owner = owner;
  119. rcur->bc_rec.r.rm_offset = offset;
  120. rcur->bc_rec.r.rm_flags = flags;
  121. error = xfs_btree_insert(rcur, &i);
  122. if (error)
  123. goto done;
  124. XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 1, done);
  125. done:
  126. if (error)
  127. trace_xfs_rmap_insert_error(rcur->bc_mp,
  128. rcur->bc_private.a.agno, error, _RET_IP_);
  129. return error;
  130. }
  131. STATIC int
  132. xfs_rmap_delete(
  133. struct xfs_btree_cur *rcur,
  134. xfs_agblock_t agbno,
  135. xfs_extlen_t len,
  136. uint64_t owner,
  137. uint64_t offset,
  138. unsigned int flags)
  139. {
  140. int i;
  141. int error;
  142. trace_xfs_rmap_delete(rcur->bc_mp, rcur->bc_private.a.agno, agbno,
  143. len, owner, offset, flags);
  144. error = xfs_rmap_lookup_eq(rcur, agbno, len, owner, offset, flags, &i);
  145. if (error)
  146. goto done;
  147. XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 1, done);
  148. error = xfs_btree_delete(rcur, &i);
  149. if (error)
  150. goto done;
  151. XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 1, done);
  152. done:
  153. if (error)
  154. trace_xfs_rmap_delete_error(rcur->bc_mp,
  155. rcur->bc_private.a.agno, error, _RET_IP_);
  156. return error;
  157. }
  158. /* Convert an internal btree record to an rmap record. */
  159. int
  160. xfs_rmap_btrec_to_irec(
  161. union xfs_btree_rec *rec,
  162. struct xfs_rmap_irec *irec)
  163. {
  164. irec->rm_flags = 0;
  165. irec->rm_startblock = be32_to_cpu(rec->rmap.rm_startblock);
  166. irec->rm_blockcount = be32_to_cpu(rec->rmap.rm_blockcount);
  167. irec->rm_owner = be64_to_cpu(rec->rmap.rm_owner);
  168. return xfs_rmap_irec_offset_unpack(be64_to_cpu(rec->rmap.rm_offset),
  169. irec);
  170. }
  171. /*
  172. * Get the data from the pointed-to record.
  173. */
  174. int
  175. xfs_rmap_get_rec(
  176. struct xfs_btree_cur *cur,
  177. struct xfs_rmap_irec *irec,
  178. int *stat)
  179. {
  180. struct xfs_mount *mp = cur->bc_mp;
  181. xfs_agnumber_t agno = cur->bc_private.a.agno;
  182. union xfs_btree_rec *rec;
  183. int error;
  184. error = xfs_btree_get_rec(cur, &rec, stat);
  185. if (error || !*stat)
  186. return error;
  187. if (xfs_rmap_btrec_to_irec(rec, irec))
  188. goto out_bad_rec;
  189. if (irec->rm_blockcount == 0)
  190. goto out_bad_rec;
  191. if (irec->rm_startblock <= XFS_AGFL_BLOCK(mp)) {
  192. if (irec->rm_owner != XFS_RMAP_OWN_FS)
  193. goto out_bad_rec;
  194. if (irec->rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
  195. goto out_bad_rec;
  196. } else {
  197. /* check for valid extent range, including overflow */
  198. if (!xfs_verify_agbno(mp, agno, irec->rm_startblock))
  199. goto out_bad_rec;
  200. if (irec->rm_startblock >
  201. irec->rm_startblock + irec->rm_blockcount)
  202. goto out_bad_rec;
  203. if (!xfs_verify_agbno(mp, agno,
  204. irec->rm_startblock + irec->rm_blockcount - 1))
  205. goto out_bad_rec;
  206. }
  207. if (!(xfs_verify_ino(mp, irec->rm_owner) ||
  208. (irec->rm_owner <= XFS_RMAP_OWN_FS &&
  209. irec->rm_owner >= XFS_RMAP_OWN_MIN)))
  210. goto out_bad_rec;
  211. return 0;
  212. out_bad_rec:
  213. xfs_warn(mp,
  214. "Reverse Mapping BTree record corruption in AG %d detected!",
  215. agno);
  216. xfs_warn(mp,
  217. "Owner 0x%llx, flags 0x%x, start block 0x%x block count 0x%x",
  218. irec->rm_owner, irec->rm_flags, irec->rm_startblock,
  219. irec->rm_blockcount);
  220. return -EFSCORRUPTED;
  221. }
  222. struct xfs_find_left_neighbor_info {
  223. struct xfs_rmap_irec high;
  224. struct xfs_rmap_irec *irec;
  225. int *stat;
  226. };
  227. /* For each rmap given, figure out if it matches the key we want. */
  228. STATIC int
  229. xfs_rmap_find_left_neighbor_helper(
  230. struct xfs_btree_cur *cur,
  231. struct xfs_rmap_irec *rec,
  232. void *priv)
  233. {
  234. struct xfs_find_left_neighbor_info *info = priv;
  235. trace_xfs_rmap_find_left_neighbor_candidate(cur->bc_mp,
  236. cur->bc_private.a.agno, rec->rm_startblock,
  237. rec->rm_blockcount, rec->rm_owner, rec->rm_offset,
  238. rec->rm_flags);
  239. if (rec->rm_owner != info->high.rm_owner)
  240. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  241. if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) &&
  242. !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK) &&
  243. rec->rm_offset + rec->rm_blockcount - 1 != info->high.rm_offset)
  244. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  245. *info->irec = *rec;
  246. *info->stat = 1;
  247. return XFS_BTREE_QUERY_RANGE_ABORT;
  248. }
  249. /*
  250. * Find the record to the left of the given extent, being careful only to
  251. * return a match with the same owner and adjacent physical and logical
  252. * block ranges.
  253. */
  254. int
  255. xfs_rmap_find_left_neighbor(
  256. struct xfs_btree_cur *cur,
  257. xfs_agblock_t bno,
  258. uint64_t owner,
  259. uint64_t offset,
  260. unsigned int flags,
  261. struct xfs_rmap_irec *irec,
  262. int *stat)
  263. {
  264. struct xfs_find_left_neighbor_info info;
  265. int error;
  266. *stat = 0;
  267. if (bno == 0)
  268. return 0;
  269. info.high.rm_startblock = bno - 1;
  270. info.high.rm_owner = owner;
  271. if (!XFS_RMAP_NON_INODE_OWNER(owner) &&
  272. !(flags & XFS_RMAP_BMBT_BLOCK)) {
  273. if (offset == 0)
  274. return 0;
  275. info.high.rm_offset = offset - 1;
  276. } else
  277. info.high.rm_offset = 0;
  278. info.high.rm_flags = flags;
  279. info.high.rm_blockcount = 0;
  280. info.irec = irec;
  281. info.stat = stat;
  282. trace_xfs_rmap_find_left_neighbor_query(cur->bc_mp,
  283. cur->bc_private.a.agno, bno, 0, owner, offset, flags);
  284. error = xfs_rmap_query_range(cur, &info.high, &info.high,
  285. xfs_rmap_find_left_neighbor_helper, &info);
  286. if (error == XFS_BTREE_QUERY_RANGE_ABORT)
  287. error = 0;
  288. if (*stat)
  289. trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp,
  290. cur->bc_private.a.agno, irec->rm_startblock,
  291. irec->rm_blockcount, irec->rm_owner,
  292. irec->rm_offset, irec->rm_flags);
  293. return error;
  294. }
  295. /* For each rmap given, figure out if it matches the key we want. */
  296. STATIC int
  297. xfs_rmap_lookup_le_range_helper(
  298. struct xfs_btree_cur *cur,
  299. struct xfs_rmap_irec *rec,
  300. void *priv)
  301. {
  302. struct xfs_find_left_neighbor_info *info = priv;
  303. trace_xfs_rmap_lookup_le_range_candidate(cur->bc_mp,
  304. cur->bc_private.a.agno, rec->rm_startblock,
  305. rec->rm_blockcount, rec->rm_owner, rec->rm_offset,
  306. rec->rm_flags);
  307. if (rec->rm_owner != info->high.rm_owner)
  308. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  309. if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) &&
  310. !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK) &&
  311. (rec->rm_offset > info->high.rm_offset ||
  312. rec->rm_offset + rec->rm_blockcount <= info->high.rm_offset))
  313. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  314. *info->irec = *rec;
  315. *info->stat = 1;
  316. return XFS_BTREE_QUERY_RANGE_ABORT;
  317. }
  318. /*
  319. * Find the record to the left of the given extent, being careful only to
  320. * return a match with the same owner and overlapping physical and logical
  321. * block ranges. This is the overlapping-interval version of
  322. * xfs_rmap_lookup_le.
  323. */
  324. int
  325. xfs_rmap_lookup_le_range(
  326. struct xfs_btree_cur *cur,
  327. xfs_agblock_t bno,
  328. uint64_t owner,
  329. uint64_t offset,
  330. unsigned int flags,
  331. struct xfs_rmap_irec *irec,
  332. int *stat)
  333. {
  334. struct xfs_find_left_neighbor_info info;
  335. int error;
  336. info.high.rm_startblock = bno;
  337. info.high.rm_owner = owner;
  338. if (!XFS_RMAP_NON_INODE_OWNER(owner) && !(flags & XFS_RMAP_BMBT_BLOCK))
  339. info.high.rm_offset = offset;
  340. else
  341. info.high.rm_offset = 0;
  342. info.high.rm_flags = flags;
  343. info.high.rm_blockcount = 0;
  344. *stat = 0;
  345. info.irec = irec;
  346. info.stat = stat;
  347. trace_xfs_rmap_lookup_le_range(cur->bc_mp,
  348. cur->bc_private.a.agno, bno, 0, owner, offset, flags);
  349. error = xfs_rmap_query_range(cur, &info.high, &info.high,
  350. xfs_rmap_lookup_le_range_helper, &info);
  351. if (error == XFS_BTREE_QUERY_RANGE_ABORT)
  352. error = 0;
  353. if (*stat)
  354. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  355. cur->bc_private.a.agno, irec->rm_startblock,
  356. irec->rm_blockcount, irec->rm_owner,
  357. irec->rm_offset, irec->rm_flags);
  358. return error;
  359. }
  360. /*
  361. * Perform all the relevant owner checks for a removal op. If we're doing an
  362. * unknown-owner removal then we have no owner information to check.
  363. */
  364. static int
  365. xfs_rmap_free_check_owner(
  366. struct xfs_mount *mp,
  367. uint64_t ltoff,
  368. struct xfs_rmap_irec *rec,
  369. xfs_filblks_t len,
  370. uint64_t owner,
  371. uint64_t offset,
  372. unsigned int flags)
  373. {
  374. int error = 0;
  375. if (owner == XFS_RMAP_OWN_UNKNOWN)
  376. return 0;
  377. /* Make sure the unwritten flag matches. */
  378. XFS_WANT_CORRUPTED_GOTO(mp, (flags & XFS_RMAP_UNWRITTEN) ==
  379. (rec->rm_flags & XFS_RMAP_UNWRITTEN), out);
  380. /* Make sure the owner matches what we expect to find in the tree. */
  381. XFS_WANT_CORRUPTED_GOTO(mp, owner == rec->rm_owner, out);
  382. /* Check the offset, if necessary. */
  383. if (XFS_RMAP_NON_INODE_OWNER(owner))
  384. goto out;
  385. if (flags & XFS_RMAP_BMBT_BLOCK) {
  386. XFS_WANT_CORRUPTED_GOTO(mp, rec->rm_flags & XFS_RMAP_BMBT_BLOCK,
  387. out);
  388. } else {
  389. XFS_WANT_CORRUPTED_GOTO(mp, rec->rm_offset <= offset, out);
  390. XFS_WANT_CORRUPTED_GOTO(mp,
  391. ltoff + rec->rm_blockcount >= offset + len,
  392. out);
  393. }
  394. out:
  395. return error;
  396. }
  397. /*
  398. * Find the extent in the rmap btree and remove it.
  399. *
  400. * The record we find should always be an exact match for the extent that we're
  401. * looking for, since we insert them into the btree without modification.
  402. *
  403. * Special Case #1: when growing the filesystem, we "free" an extent when
  404. * growing the last AG. This extent is new space and so it is not tracked as
  405. * used space in the btree. The growfs code will pass in an owner of
  406. * XFS_RMAP_OWN_NULL to indicate that it expected that there is no owner of this
  407. * extent. We verify that - the extent lookup result in a record that does not
  408. * overlap.
  409. *
  410. * Special Case #2: EFIs do not record the owner of the extent, so when
  411. * recovering EFIs from the log we pass in XFS_RMAP_OWN_UNKNOWN to tell the rmap
  412. * btree to ignore the owner (i.e. wildcard match) so we don't trigger
  413. * corruption checks during log recovery.
  414. */
  415. STATIC int
  416. xfs_rmap_unmap(
  417. struct xfs_btree_cur *cur,
  418. xfs_agblock_t bno,
  419. xfs_extlen_t len,
  420. bool unwritten,
  421. struct xfs_owner_info *oinfo)
  422. {
  423. struct xfs_mount *mp = cur->bc_mp;
  424. struct xfs_rmap_irec ltrec;
  425. uint64_t ltoff;
  426. int error = 0;
  427. int i;
  428. uint64_t owner;
  429. uint64_t offset;
  430. unsigned int flags;
  431. bool ignore_off;
  432. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  433. ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
  434. (flags & XFS_RMAP_BMBT_BLOCK);
  435. if (unwritten)
  436. flags |= XFS_RMAP_UNWRITTEN;
  437. trace_xfs_rmap_unmap(mp, cur->bc_private.a.agno, bno, len,
  438. unwritten, oinfo);
  439. /*
  440. * We should always have a left record because there's a static record
  441. * for the AG headers at rm_startblock == 0 created by mkfs/growfs that
  442. * will not ever be removed from the tree.
  443. */
  444. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags, &i);
  445. if (error)
  446. goto out_error;
  447. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  448. error = xfs_rmap_get_rec(cur, &ltrec, &i);
  449. if (error)
  450. goto out_error;
  451. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  452. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  453. cur->bc_private.a.agno, ltrec.rm_startblock,
  454. ltrec.rm_blockcount, ltrec.rm_owner,
  455. ltrec.rm_offset, ltrec.rm_flags);
  456. ltoff = ltrec.rm_offset;
  457. /*
  458. * For growfs, the incoming extent must be beyond the left record we
  459. * just found as it is new space and won't be used by anyone. This is
  460. * just a corruption check as we don't actually do anything with this
  461. * extent. Note that we need to use >= instead of > because it might
  462. * be the case that the "left" extent goes all the way to EOFS.
  463. */
  464. if (owner == XFS_RMAP_OWN_NULL) {
  465. XFS_WANT_CORRUPTED_GOTO(mp, bno >= ltrec.rm_startblock +
  466. ltrec.rm_blockcount, out_error);
  467. goto out_done;
  468. }
  469. /*
  470. * If we're doing an unknown-owner removal for EFI recovery, we expect
  471. * to find the full range in the rmapbt or nothing at all. If we
  472. * don't find any rmaps overlapping either end of the range, we're
  473. * done. Hopefully this means that the EFI creator already queued
  474. * (and finished) a RUI to remove the rmap.
  475. */
  476. if (owner == XFS_RMAP_OWN_UNKNOWN &&
  477. ltrec.rm_startblock + ltrec.rm_blockcount <= bno) {
  478. struct xfs_rmap_irec rtrec;
  479. error = xfs_btree_increment(cur, 0, &i);
  480. if (error)
  481. goto out_error;
  482. if (i == 0)
  483. goto out_done;
  484. error = xfs_rmap_get_rec(cur, &rtrec, &i);
  485. if (error)
  486. goto out_error;
  487. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  488. if (rtrec.rm_startblock >= bno + len)
  489. goto out_done;
  490. }
  491. /* Make sure the extent we found covers the entire freeing range. */
  492. XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_startblock <= bno &&
  493. ltrec.rm_startblock + ltrec.rm_blockcount >=
  494. bno + len, out_error);
  495. /* Check owner information. */
  496. error = xfs_rmap_free_check_owner(mp, ltoff, &ltrec, len, owner,
  497. offset, flags);
  498. if (error)
  499. goto out_error;
  500. if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) {
  501. /* exact match, simply remove the record from rmap tree */
  502. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  503. ltrec.rm_startblock, ltrec.rm_blockcount,
  504. ltrec.rm_owner, ltrec.rm_offset,
  505. ltrec.rm_flags);
  506. error = xfs_btree_delete(cur, &i);
  507. if (error)
  508. goto out_error;
  509. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  510. } else if (ltrec.rm_startblock == bno) {
  511. /*
  512. * overlap left hand side of extent: move the start, trim the
  513. * length and update the current record.
  514. *
  515. * ltbno ltlen
  516. * Orig: |oooooooooooooooooooo|
  517. * Freeing: |fffffffff|
  518. * Result: |rrrrrrrrrr|
  519. * bno len
  520. */
  521. ltrec.rm_startblock += len;
  522. ltrec.rm_blockcount -= len;
  523. if (!ignore_off)
  524. ltrec.rm_offset += len;
  525. error = xfs_rmap_update(cur, &ltrec);
  526. if (error)
  527. goto out_error;
  528. } else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) {
  529. /*
  530. * overlap right hand side of extent: trim the length and update
  531. * the current record.
  532. *
  533. * ltbno ltlen
  534. * Orig: |oooooooooooooooooooo|
  535. * Freeing: |fffffffff|
  536. * Result: |rrrrrrrrrr|
  537. * bno len
  538. */
  539. ltrec.rm_blockcount -= len;
  540. error = xfs_rmap_update(cur, &ltrec);
  541. if (error)
  542. goto out_error;
  543. } else {
  544. /*
  545. * overlap middle of extent: trim the length of the existing
  546. * record to the length of the new left-extent size, increment
  547. * the insertion position so we can insert a new record
  548. * containing the remaining right-extent space.
  549. *
  550. * ltbno ltlen
  551. * Orig: |oooooooooooooooooooo|
  552. * Freeing: |fffffffff|
  553. * Result: |rrrrr| |rrrr|
  554. * bno len
  555. */
  556. xfs_extlen_t orig_len = ltrec.rm_blockcount;
  557. ltrec.rm_blockcount = bno - ltrec.rm_startblock;
  558. error = xfs_rmap_update(cur, &ltrec);
  559. if (error)
  560. goto out_error;
  561. error = xfs_btree_increment(cur, 0, &i);
  562. if (error)
  563. goto out_error;
  564. cur->bc_rec.r.rm_startblock = bno + len;
  565. cur->bc_rec.r.rm_blockcount = orig_len - len -
  566. ltrec.rm_blockcount;
  567. cur->bc_rec.r.rm_owner = ltrec.rm_owner;
  568. if (ignore_off)
  569. cur->bc_rec.r.rm_offset = 0;
  570. else
  571. cur->bc_rec.r.rm_offset = offset + len;
  572. cur->bc_rec.r.rm_flags = flags;
  573. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno,
  574. cur->bc_rec.r.rm_startblock,
  575. cur->bc_rec.r.rm_blockcount,
  576. cur->bc_rec.r.rm_owner,
  577. cur->bc_rec.r.rm_offset,
  578. cur->bc_rec.r.rm_flags);
  579. error = xfs_btree_insert(cur, &i);
  580. if (error)
  581. goto out_error;
  582. }
  583. out_done:
  584. trace_xfs_rmap_unmap_done(mp, cur->bc_private.a.agno, bno, len,
  585. unwritten, oinfo);
  586. out_error:
  587. if (error)
  588. trace_xfs_rmap_unmap_error(mp, cur->bc_private.a.agno,
  589. error, _RET_IP_);
  590. return error;
  591. }
  592. /*
  593. * Remove a reference to an extent in the rmap btree.
  594. */
  595. int
  596. xfs_rmap_free(
  597. struct xfs_trans *tp,
  598. struct xfs_buf *agbp,
  599. xfs_agnumber_t agno,
  600. xfs_agblock_t bno,
  601. xfs_extlen_t len,
  602. struct xfs_owner_info *oinfo)
  603. {
  604. struct xfs_mount *mp = tp->t_mountp;
  605. struct xfs_btree_cur *cur;
  606. int error;
  607. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  608. return 0;
  609. cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  610. error = xfs_rmap_unmap(cur, bno, len, false, oinfo);
  611. xfs_btree_del_cursor(cur, error);
  612. return error;
  613. }
  614. /*
  615. * A mergeable rmap must have the same owner and the same values for
  616. * the unwritten, attr_fork, and bmbt flags. The startblock and
  617. * offset are checked separately.
  618. */
  619. static bool
  620. xfs_rmap_is_mergeable(
  621. struct xfs_rmap_irec *irec,
  622. uint64_t owner,
  623. unsigned int flags)
  624. {
  625. if (irec->rm_owner == XFS_RMAP_OWN_NULL)
  626. return false;
  627. if (irec->rm_owner != owner)
  628. return false;
  629. if ((flags & XFS_RMAP_UNWRITTEN) ^
  630. (irec->rm_flags & XFS_RMAP_UNWRITTEN))
  631. return false;
  632. if ((flags & XFS_RMAP_ATTR_FORK) ^
  633. (irec->rm_flags & XFS_RMAP_ATTR_FORK))
  634. return false;
  635. if ((flags & XFS_RMAP_BMBT_BLOCK) ^
  636. (irec->rm_flags & XFS_RMAP_BMBT_BLOCK))
  637. return false;
  638. return true;
  639. }
  640. /*
  641. * When we allocate a new block, the first thing we do is add a reference to
  642. * the extent in the rmap btree. This takes the form of a [agbno, length,
  643. * owner, offset] record. Flags are encoded in the high bits of the offset
  644. * field.
  645. */
  646. STATIC int
  647. xfs_rmap_map(
  648. struct xfs_btree_cur *cur,
  649. xfs_agblock_t bno,
  650. xfs_extlen_t len,
  651. bool unwritten,
  652. struct xfs_owner_info *oinfo)
  653. {
  654. struct xfs_mount *mp = cur->bc_mp;
  655. struct xfs_rmap_irec ltrec;
  656. struct xfs_rmap_irec gtrec;
  657. int have_gt;
  658. int have_lt;
  659. int error = 0;
  660. int i;
  661. uint64_t owner;
  662. uint64_t offset;
  663. unsigned int flags = 0;
  664. bool ignore_off;
  665. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  666. ASSERT(owner != 0);
  667. ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
  668. (flags & XFS_RMAP_BMBT_BLOCK);
  669. if (unwritten)
  670. flags |= XFS_RMAP_UNWRITTEN;
  671. trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len,
  672. unwritten, oinfo);
  673. ASSERT(!xfs_rmap_should_skip_owner_update(oinfo));
  674. /*
  675. * For the initial lookup, look for an exact match or the left-adjacent
  676. * record for our insertion point. This will also give us the record for
  677. * start block contiguity tests.
  678. */
  679. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
  680. &have_lt);
  681. if (error)
  682. goto out_error;
  683. if (have_lt) {
  684. error = xfs_rmap_get_rec(cur, &ltrec, &have_lt);
  685. if (error)
  686. goto out_error;
  687. XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error);
  688. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  689. cur->bc_private.a.agno, ltrec.rm_startblock,
  690. ltrec.rm_blockcount, ltrec.rm_owner,
  691. ltrec.rm_offset, ltrec.rm_flags);
  692. if (!xfs_rmap_is_mergeable(&ltrec, owner, flags))
  693. have_lt = 0;
  694. }
  695. XFS_WANT_CORRUPTED_GOTO(mp,
  696. have_lt == 0 ||
  697. ltrec.rm_startblock + ltrec.rm_blockcount <= bno, out_error);
  698. /*
  699. * Increment the cursor to see if we have a right-adjacent record to our
  700. * insertion point. This will give us the record for end block
  701. * contiguity tests.
  702. */
  703. error = xfs_btree_increment(cur, 0, &have_gt);
  704. if (error)
  705. goto out_error;
  706. if (have_gt) {
  707. error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
  708. if (error)
  709. goto out_error;
  710. XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error);
  711. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= gtrec.rm_startblock,
  712. out_error);
  713. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  714. cur->bc_private.a.agno, gtrec.rm_startblock,
  715. gtrec.rm_blockcount, gtrec.rm_owner,
  716. gtrec.rm_offset, gtrec.rm_flags);
  717. if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
  718. have_gt = 0;
  719. }
  720. /*
  721. * Note: cursor currently points one record to the right of ltrec, even
  722. * if there is no record in the tree to the right.
  723. */
  724. if (have_lt &&
  725. ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
  726. (ignore_off || ltrec.rm_offset + ltrec.rm_blockcount == offset)) {
  727. /*
  728. * left edge contiguous, merge into left record.
  729. *
  730. * ltbno ltlen
  731. * orig: |ooooooooo|
  732. * adding: |aaaaaaaaa|
  733. * result: |rrrrrrrrrrrrrrrrrrr|
  734. * bno len
  735. */
  736. ltrec.rm_blockcount += len;
  737. if (have_gt &&
  738. bno + len == gtrec.rm_startblock &&
  739. (ignore_off || offset + len == gtrec.rm_offset) &&
  740. (unsigned long)ltrec.rm_blockcount + len +
  741. gtrec.rm_blockcount <= XFS_RMAP_LEN_MAX) {
  742. /*
  743. * right edge also contiguous, delete right record
  744. * and merge into left record.
  745. *
  746. * ltbno ltlen gtbno gtlen
  747. * orig: |ooooooooo| |ooooooooo|
  748. * adding: |aaaaaaaaa|
  749. * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
  750. */
  751. ltrec.rm_blockcount += gtrec.rm_blockcount;
  752. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  753. gtrec.rm_startblock,
  754. gtrec.rm_blockcount,
  755. gtrec.rm_owner,
  756. gtrec.rm_offset,
  757. gtrec.rm_flags);
  758. error = xfs_btree_delete(cur, &i);
  759. if (error)
  760. goto out_error;
  761. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  762. }
  763. /* point the cursor back to the left record and update */
  764. error = xfs_btree_decrement(cur, 0, &have_gt);
  765. if (error)
  766. goto out_error;
  767. error = xfs_rmap_update(cur, &ltrec);
  768. if (error)
  769. goto out_error;
  770. } else if (have_gt &&
  771. bno + len == gtrec.rm_startblock &&
  772. (ignore_off || offset + len == gtrec.rm_offset)) {
  773. /*
  774. * right edge contiguous, merge into right record.
  775. *
  776. * gtbno gtlen
  777. * Orig: |ooooooooo|
  778. * adding: |aaaaaaaaa|
  779. * Result: |rrrrrrrrrrrrrrrrrrr|
  780. * bno len
  781. */
  782. gtrec.rm_startblock = bno;
  783. gtrec.rm_blockcount += len;
  784. if (!ignore_off)
  785. gtrec.rm_offset = offset;
  786. error = xfs_rmap_update(cur, &gtrec);
  787. if (error)
  788. goto out_error;
  789. } else {
  790. /*
  791. * no contiguous edge with identical owner, insert
  792. * new record at current cursor position.
  793. */
  794. cur->bc_rec.r.rm_startblock = bno;
  795. cur->bc_rec.r.rm_blockcount = len;
  796. cur->bc_rec.r.rm_owner = owner;
  797. cur->bc_rec.r.rm_offset = offset;
  798. cur->bc_rec.r.rm_flags = flags;
  799. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len,
  800. owner, offset, flags);
  801. error = xfs_btree_insert(cur, &i);
  802. if (error)
  803. goto out_error;
  804. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  805. }
  806. trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len,
  807. unwritten, oinfo);
  808. out_error:
  809. if (error)
  810. trace_xfs_rmap_map_error(mp, cur->bc_private.a.agno,
  811. error, _RET_IP_);
  812. return error;
  813. }
  814. /*
  815. * Add a reference to an extent in the rmap btree.
  816. */
  817. int
  818. xfs_rmap_alloc(
  819. struct xfs_trans *tp,
  820. struct xfs_buf *agbp,
  821. xfs_agnumber_t agno,
  822. xfs_agblock_t bno,
  823. xfs_extlen_t len,
  824. struct xfs_owner_info *oinfo)
  825. {
  826. struct xfs_mount *mp = tp->t_mountp;
  827. struct xfs_btree_cur *cur;
  828. int error;
  829. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  830. return 0;
  831. cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  832. error = xfs_rmap_map(cur, bno, len, false, oinfo);
  833. xfs_btree_del_cursor(cur, error);
  834. return error;
  835. }
  836. #define RMAP_LEFT_CONTIG (1 << 0)
  837. #define RMAP_RIGHT_CONTIG (1 << 1)
  838. #define RMAP_LEFT_FILLING (1 << 2)
  839. #define RMAP_RIGHT_FILLING (1 << 3)
  840. #define RMAP_LEFT_VALID (1 << 6)
  841. #define RMAP_RIGHT_VALID (1 << 7)
  842. #define LEFT r[0]
  843. #define RIGHT r[1]
  844. #define PREV r[2]
  845. #define NEW r[3]
  846. /*
  847. * Convert an unwritten extent to a real extent or vice versa.
  848. * Does not handle overlapping extents.
  849. */
  850. STATIC int
  851. xfs_rmap_convert(
  852. struct xfs_btree_cur *cur,
  853. xfs_agblock_t bno,
  854. xfs_extlen_t len,
  855. bool unwritten,
  856. struct xfs_owner_info *oinfo)
  857. {
  858. struct xfs_mount *mp = cur->bc_mp;
  859. struct xfs_rmap_irec r[4]; /* neighbor extent entries */
  860. /* left is 0, right is 1, prev is 2 */
  861. /* new is 3 */
  862. uint64_t owner;
  863. uint64_t offset;
  864. uint64_t new_endoff;
  865. unsigned int oldext;
  866. unsigned int newext;
  867. unsigned int flags = 0;
  868. int i;
  869. int state = 0;
  870. int error;
  871. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  872. ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) ||
  873. (flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))));
  874. oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0;
  875. new_endoff = offset + len;
  876. trace_xfs_rmap_convert(mp, cur->bc_private.a.agno, bno, len,
  877. unwritten, oinfo);
  878. /*
  879. * For the initial lookup, look for an exact match or the left-adjacent
  880. * record for our insertion point. This will also give us the record for
  881. * start block contiguity tests.
  882. */
  883. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
  884. if (error)
  885. goto done;
  886. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  887. error = xfs_rmap_get_rec(cur, &PREV, &i);
  888. if (error)
  889. goto done;
  890. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  891. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  892. cur->bc_private.a.agno, PREV.rm_startblock,
  893. PREV.rm_blockcount, PREV.rm_owner,
  894. PREV.rm_offset, PREV.rm_flags);
  895. ASSERT(PREV.rm_offset <= offset);
  896. ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff);
  897. ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext);
  898. newext = ~oldext & XFS_RMAP_UNWRITTEN;
  899. /*
  900. * Set flags determining what part of the previous oldext allocation
  901. * extent is being replaced by a newext allocation.
  902. */
  903. if (PREV.rm_offset == offset)
  904. state |= RMAP_LEFT_FILLING;
  905. if (PREV.rm_offset + PREV.rm_blockcount == new_endoff)
  906. state |= RMAP_RIGHT_FILLING;
  907. /*
  908. * Decrement the cursor to see if we have a left-adjacent record to our
  909. * insertion point. This will give us the record for end block
  910. * contiguity tests.
  911. */
  912. error = xfs_btree_decrement(cur, 0, &i);
  913. if (error)
  914. goto done;
  915. if (i) {
  916. state |= RMAP_LEFT_VALID;
  917. error = xfs_rmap_get_rec(cur, &LEFT, &i);
  918. if (error)
  919. goto done;
  920. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  921. XFS_WANT_CORRUPTED_GOTO(mp,
  922. LEFT.rm_startblock + LEFT.rm_blockcount <= bno,
  923. done);
  924. trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp,
  925. cur->bc_private.a.agno, LEFT.rm_startblock,
  926. LEFT.rm_blockcount, LEFT.rm_owner,
  927. LEFT.rm_offset, LEFT.rm_flags);
  928. if (LEFT.rm_startblock + LEFT.rm_blockcount == bno &&
  929. LEFT.rm_offset + LEFT.rm_blockcount == offset &&
  930. xfs_rmap_is_mergeable(&LEFT, owner, newext))
  931. state |= RMAP_LEFT_CONTIG;
  932. }
  933. /*
  934. * Increment the cursor to see if we have a right-adjacent record to our
  935. * insertion point. This will give us the record for end block
  936. * contiguity tests.
  937. */
  938. error = xfs_btree_increment(cur, 0, &i);
  939. if (error)
  940. goto done;
  941. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  942. error = xfs_btree_increment(cur, 0, &i);
  943. if (error)
  944. goto done;
  945. if (i) {
  946. state |= RMAP_RIGHT_VALID;
  947. error = xfs_rmap_get_rec(cur, &RIGHT, &i);
  948. if (error)
  949. goto done;
  950. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  951. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= RIGHT.rm_startblock,
  952. done);
  953. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  954. cur->bc_private.a.agno, RIGHT.rm_startblock,
  955. RIGHT.rm_blockcount, RIGHT.rm_owner,
  956. RIGHT.rm_offset, RIGHT.rm_flags);
  957. if (bno + len == RIGHT.rm_startblock &&
  958. offset + len == RIGHT.rm_offset &&
  959. xfs_rmap_is_mergeable(&RIGHT, owner, newext))
  960. state |= RMAP_RIGHT_CONTIG;
  961. }
  962. /* check that left + prev + right is not too long */
  963. if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  964. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) ==
  965. (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  966. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) &&
  967. (unsigned long)LEFT.rm_blockcount + len +
  968. RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX)
  969. state &= ~RMAP_RIGHT_CONTIG;
  970. trace_xfs_rmap_convert_state(mp, cur->bc_private.a.agno, state,
  971. _RET_IP_);
  972. /* reset the cursor back to PREV */
  973. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
  974. if (error)
  975. goto done;
  976. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  977. /*
  978. * Switch out based on the FILLING and CONTIG state bits.
  979. */
  980. switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  981. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) {
  982. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  983. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  984. /*
  985. * Setting all of a previous oldext extent to newext.
  986. * The left and right neighbors are both contiguous with new.
  987. */
  988. error = xfs_btree_increment(cur, 0, &i);
  989. if (error)
  990. goto done;
  991. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  992. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  993. RIGHT.rm_startblock, RIGHT.rm_blockcount,
  994. RIGHT.rm_owner, RIGHT.rm_offset,
  995. RIGHT.rm_flags);
  996. error = xfs_btree_delete(cur, &i);
  997. if (error)
  998. goto done;
  999. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1000. error = xfs_btree_decrement(cur, 0, &i);
  1001. if (error)
  1002. goto done;
  1003. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1004. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  1005. PREV.rm_startblock, PREV.rm_blockcount,
  1006. PREV.rm_owner, PREV.rm_offset,
  1007. PREV.rm_flags);
  1008. error = xfs_btree_delete(cur, &i);
  1009. if (error)
  1010. goto done;
  1011. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1012. error = xfs_btree_decrement(cur, 0, &i);
  1013. if (error)
  1014. goto done;
  1015. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1016. NEW = LEFT;
  1017. NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount;
  1018. error = xfs_rmap_update(cur, &NEW);
  1019. if (error)
  1020. goto done;
  1021. break;
  1022. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1023. /*
  1024. * Setting all of a previous oldext extent to newext.
  1025. * The left neighbor is contiguous, the right is not.
  1026. */
  1027. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  1028. PREV.rm_startblock, PREV.rm_blockcount,
  1029. PREV.rm_owner, PREV.rm_offset,
  1030. PREV.rm_flags);
  1031. error = xfs_btree_delete(cur, &i);
  1032. if (error)
  1033. goto done;
  1034. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1035. error = xfs_btree_decrement(cur, 0, &i);
  1036. if (error)
  1037. goto done;
  1038. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1039. NEW = LEFT;
  1040. NEW.rm_blockcount += PREV.rm_blockcount;
  1041. error = xfs_rmap_update(cur, &NEW);
  1042. if (error)
  1043. goto done;
  1044. break;
  1045. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1046. /*
  1047. * Setting all of a previous oldext extent to newext.
  1048. * The right neighbor is contiguous, the left is not.
  1049. */
  1050. error = xfs_btree_increment(cur, 0, &i);
  1051. if (error)
  1052. goto done;
  1053. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1054. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  1055. RIGHT.rm_startblock, RIGHT.rm_blockcount,
  1056. RIGHT.rm_owner, RIGHT.rm_offset,
  1057. RIGHT.rm_flags);
  1058. error = xfs_btree_delete(cur, &i);
  1059. if (error)
  1060. goto done;
  1061. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1062. error = xfs_btree_decrement(cur, 0, &i);
  1063. if (error)
  1064. goto done;
  1065. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1066. NEW = PREV;
  1067. NEW.rm_blockcount = len + RIGHT.rm_blockcount;
  1068. NEW.rm_flags = newext;
  1069. error = xfs_rmap_update(cur, &NEW);
  1070. if (error)
  1071. goto done;
  1072. break;
  1073. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING:
  1074. /*
  1075. * Setting all of a previous oldext extent to newext.
  1076. * Neither the left nor right neighbors are contiguous with
  1077. * the new one.
  1078. */
  1079. NEW = PREV;
  1080. NEW.rm_flags = newext;
  1081. error = xfs_rmap_update(cur, &NEW);
  1082. if (error)
  1083. goto done;
  1084. break;
  1085. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG:
  1086. /*
  1087. * Setting the first part of a previous oldext extent to newext.
  1088. * The left neighbor is contiguous.
  1089. */
  1090. NEW = PREV;
  1091. NEW.rm_offset += len;
  1092. NEW.rm_startblock += len;
  1093. NEW.rm_blockcount -= len;
  1094. error = xfs_rmap_update(cur, &NEW);
  1095. if (error)
  1096. goto done;
  1097. error = xfs_btree_decrement(cur, 0, &i);
  1098. if (error)
  1099. goto done;
  1100. NEW = LEFT;
  1101. NEW.rm_blockcount += len;
  1102. error = xfs_rmap_update(cur, &NEW);
  1103. if (error)
  1104. goto done;
  1105. break;
  1106. case RMAP_LEFT_FILLING:
  1107. /*
  1108. * Setting the first part of a previous oldext extent to newext.
  1109. * The left neighbor is not contiguous.
  1110. */
  1111. NEW = PREV;
  1112. NEW.rm_startblock += len;
  1113. NEW.rm_offset += len;
  1114. NEW.rm_blockcount -= len;
  1115. error = xfs_rmap_update(cur, &NEW);
  1116. if (error)
  1117. goto done;
  1118. NEW.rm_startblock = bno;
  1119. NEW.rm_owner = owner;
  1120. NEW.rm_offset = offset;
  1121. NEW.rm_blockcount = len;
  1122. NEW.rm_flags = newext;
  1123. cur->bc_rec.r = NEW;
  1124. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno,
  1125. len, owner, offset, newext);
  1126. error = xfs_btree_insert(cur, &i);
  1127. if (error)
  1128. goto done;
  1129. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1130. break;
  1131. case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1132. /*
  1133. * Setting the last part of a previous oldext extent to newext.
  1134. * The right neighbor is contiguous with the new allocation.
  1135. */
  1136. NEW = PREV;
  1137. NEW.rm_blockcount -= len;
  1138. error = xfs_rmap_update(cur, &NEW);
  1139. if (error)
  1140. goto done;
  1141. error = xfs_btree_increment(cur, 0, &i);
  1142. if (error)
  1143. goto done;
  1144. NEW = RIGHT;
  1145. NEW.rm_offset = offset;
  1146. NEW.rm_startblock = bno;
  1147. NEW.rm_blockcount += len;
  1148. error = xfs_rmap_update(cur, &NEW);
  1149. if (error)
  1150. goto done;
  1151. break;
  1152. case RMAP_RIGHT_FILLING:
  1153. /*
  1154. * Setting the last part of a previous oldext extent to newext.
  1155. * The right neighbor is not contiguous.
  1156. */
  1157. NEW = PREV;
  1158. NEW.rm_blockcount -= len;
  1159. error = xfs_rmap_update(cur, &NEW);
  1160. if (error)
  1161. goto done;
  1162. error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
  1163. oldext, &i);
  1164. if (error)
  1165. goto done;
  1166. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1167. NEW.rm_startblock = bno;
  1168. NEW.rm_owner = owner;
  1169. NEW.rm_offset = offset;
  1170. NEW.rm_blockcount = len;
  1171. NEW.rm_flags = newext;
  1172. cur->bc_rec.r = NEW;
  1173. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno,
  1174. len, owner, offset, newext);
  1175. error = xfs_btree_insert(cur, &i);
  1176. if (error)
  1177. goto done;
  1178. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1179. break;
  1180. case 0:
  1181. /*
  1182. * Setting the middle part of a previous oldext extent to
  1183. * newext. Contiguity is impossible here.
  1184. * One extent becomes three extents.
  1185. */
  1186. /* new right extent - oldext */
  1187. NEW.rm_startblock = bno + len;
  1188. NEW.rm_owner = owner;
  1189. NEW.rm_offset = new_endoff;
  1190. NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount -
  1191. new_endoff;
  1192. NEW.rm_flags = PREV.rm_flags;
  1193. error = xfs_rmap_update(cur, &NEW);
  1194. if (error)
  1195. goto done;
  1196. /* new left extent - oldext */
  1197. NEW = PREV;
  1198. NEW.rm_blockcount = offset - PREV.rm_offset;
  1199. cur->bc_rec.r = NEW;
  1200. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno,
  1201. NEW.rm_startblock, NEW.rm_blockcount,
  1202. NEW.rm_owner, NEW.rm_offset,
  1203. NEW.rm_flags);
  1204. error = xfs_btree_insert(cur, &i);
  1205. if (error)
  1206. goto done;
  1207. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1208. /*
  1209. * Reset the cursor to the position of the new extent
  1210. * we are about to insert as we can't trust it after
  1211. * the previous insert.
  1212. */
  1213. error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
  1214. oldext, &i);
  1215. if (error)
  1216. goto done;
  1217. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1218. /* new middle extent - newext */
  1219. cur->bc_rec.r.rm_flags &= ~XFS_RMAP_UNWRITTEN;
  1220. cur->bc_rec.r.rm_flags |= newext;
  1221. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len,
  1222. owner, offset, newext);
  1223. error = xfs_btree_insert(cur, &i);
  1224. if (error)
  1225. goto done;
  1226. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1227. break;
  1228. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1229. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1230. case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG:
  1231. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1232. case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1233. case RMAP_LEFT_CONTIG:
  1234. case RMAP_RIGHT_CONTIG:
  1235. /*
  1236. * These cases are all impossible.
  1237. */
  1238. ASSERT(0);
  1239. }
  1240. trace_xfs_rmap_convert_done(mp, cur->bc_private.a.agno, bno, len,
  1241. unwritten, oinfo);
  1242. done:
  1243. if (error)
  1244. trace_xfs_rmap_convert_error(cur->bc_mp,
  1245. cur->bc_private.a.agno, error, _RET_IP_);
  1246. return error;
  1247. }
  1248. /*
  1249. * Convert an unwritten extent to a real extent or vice versa. If there is no
  1250. * possibility of overlapping extents, delegate to the simpler convert
  1251. * function.
  1252. */
  1253. STATIC int
  1254. xfs_rmap_convert_shared(
  1255. struct xfs_btree_cur *cur,
  1256. xfs_agblock_t bno,
  1257. xfs_extlen_t len,
  1258. bool unwritten,
  1259. struct xfs_owner_info *oinfo)
  1260. {
  1261. struct xfs_mount *mp = cur->bc_mp;
  1262. struct xfs_rmap_irec r[4]; /* neighbor extent entries */
  1263. /* left is 0, right is 1, prev is 2 */
  1264. /* new is 3 */
  1265. uint64_t owner;
  1266. uint64_t offset;
  1267. uint64_t new_endoff;
  1268. unsigned int oldext;
  1269. unsigned int newext;
  1270. unsigned int flags = 0;
  1271. int i;
  1272. int state = 0;
  1273. int error;
  1274. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  1275. ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) ||
  1276. (flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))));
  1277. oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0;
  1278. new_endoff = offset + len;
  1279. trace_xfs_rmap_convert(mp, cur->bc_private.a.agno, bno, len,
  1280. unwritten, oinfo);
  1281. /*
  1282. * For the initial lookup, look for and exact match or the left-adjacent
  1283. * record for our insertion point. This will also give us the record for
  1284. * start block contiguity tests.
  1285. */
  1286. error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, oldext,
  1287. &PREV, &i);
  1288. if (error)
  1289. goto done;
  1290. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1291. ASSERT(PREV.rm_offset <= offset);
  1292. ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff);
  1293. ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext);
  1294. newext = ~oldext & XFS_RMAP_UNWRITTEN;
  1295. /*
  1296. * Set flags determining what part of the previous oldext allocation
  1297. * extent is being replaced by a newext allocation.
  1298. */
  1299. if (PREV.rm_offset == offset)
  1300. state |= RMAP_LEFT_FILLING;
  1301. if (PREV.rm_offset + PREV.rm_blockcount == new_endoff)
  1302. state |= RMAP_RIGHT_FILLING;
  1303. /* Is there a left record that abuts our range? */
  1304. error = xfs_rmap_find_left_neighbor(cur, bno, owner, offset, newext,
  1305. &LEFT, &i);
  1306. if (error)
  1307. goto done;
  1308. if (i) {
  1309. state |= RMAP_LEFT_VALID;
  1310. XFS_WANT_CORRUPTED_GOTO(mp,
  1311. LEFT.rm_startblock + LEFT.rm_blockcount <= bno,
  1312. done);
  1313. if (xfs_rmap_is_mergeable(&LEFT, owner, newext))
  1314. state |= RMAP_LEFT_CONTIG;
  1315. }
  1316. /* Is there a right record that abuts our range? */
  1317. error = xfs_rmap_lookup_eq(cur, bno + len, len, owner, offset + len,
  1318. newext, &i);
  1319. if (error)
  1320. goto done;
  1321. if (i) {
  1322. state |= RMAP_RIGHT_VALID;
  1323. error = xfs_rmap_get_rec(cur, &RIGHT, &i);
  1324. if (error)
  1325. goto done;
  1326. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1327. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= RIGHT.rm_startblock,
  1328. done);
  1329. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  1330. cur->bc_private.a.agno, RIGHT.rm_startblock,
  1331. RIGHT.rm_blockcount, RIGHT.rm_owner,
  1332. RIGHT.rm_offset, RIGHT.rm_flags);
  1333. if (xfs_rmap_is_mergeable(&RIGHT, owner, newext))
  1334. state |= RMAP_RIGHT_CONTIG;
  1335. }
  1336. /* check that left + prev + right is not too long */
  1337. if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1338. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) ==
  1339. (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1340. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) &&
  1341. (unsigned long)LEFT.rm_blockcount + len +
  1342. RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX)
  1343. state &= ~RMAP_RIGHT_CONTIG;
  1344. trace_xfs_rmap_convert_state(mp, cur->bc_private.a.agno, state,
  1345. _RET_IP_);
  1346. /*
  1347. * Switch out based on the FILLING and CONTIG state bits.
  1348. */
  1349. switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1350. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) {
  1351. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1352. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1353. /*
  1354. * Setting all of a previous oldext extent to newext.
  1355. * The left and right neighbors are both contiguous with new.
  1356. */
  1357. error = xfs_rmap_delete(cur, RIGHT.rm_startblock,
  1358. RIGHT.rm_blockcount, RIGHT.rm_owner,
  1359. RIGHT.rm_offset, RIGHT.rm_flags);
  1360. if (error)
  1361. goto done;
  1362. error = xfs_rmap_delete(cur, PREV.rm_startblock,
  1363. PREV.rm_blockcount, PREV.rm_owner,
  1364. PREV.rm_offset, PREV.rm_flags);
  1365. if (error)
  1366. goto done;
  1367. NEW = LEFT;
  1368. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1369. NEW.rm_blockcount, NEW.rm_owner,
  1370. NEW.rm_offset, NEW.rm_flags, &i);
  1371. if (error)
  1372. goto done;
  1373. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1374. NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount;
  1375. error = xfs_rmap_update(cur, &NEW);
  1376. if (error)
  1377. goto done;
  1378. break;
  1379. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1380. /*
  1381. * Setting all of a previous oldext extent to newext.
  1382. * The left neighbor is contiguous, the right is not.
  1383. */
  1384. error = xfs_rmap_delete(cur, PREV.rm_startblock,
  1385. PREV.rm_blockcount, PREV.rm_owner,
  1386. PREV.rm_offset, PREV.rm_flags);
  1387. if (error)
  1388. goto done;
  1389. NEW = LEFT;
  1390. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1391. NEW.rm_blockcount, NEW.rm_owner,
  1392. NEW.rm_offset, NEW.rm_flags, &i);
  1393. if (error)
  1394. goto done;
  1395. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1396. NEW.rm_blockcount += PREV.rm_blockcount;
  1397. error = xfs_rmap_update(cur, &NEW);
  1398. if (error)
  1399. goto done;
  1400. break;
  1401. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1402. /*
  1403. * Setting all of a previous oldext extent to newext.
  1404. * The right neighbor is contiguous, the left is not.
  1405. */
  1406. error = xfs_rmap_delete(cur, RIGHT.rm_startblock,
  1407. RIGHT.rm_blockcount, RIGHT.rm_owner,
  1408. RIGHT.rm_offset, RIGHT.rm_flags);
  1409. if (error)
  1410. goto done;
  1411. NEW = PREV;
  1412. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1413. NEW.rm_blockcount, NEW.rm_owner,
  1414. NEW.rm_offset, NEW.rm_flags, &i);
  1415. if (error)
  1416. goto done;
  1417. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1418. NEW.rm_blockcount += RIGHT.rm_blockcount;
  1419. NEW.rm_flags = RIGHT.rm_flags;
  1420. error = xfs_rmap_update(cur, &NEW);
  1421. if (error)
  1422. goto done;
  1423. break;
  1424. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING:
  1425. /*
  1426. * Setting all of a previous oldext extent to newext.
  1427. * Neither the left nor right neighbors are contiguous with
  1428. * the new one.
  1429. */
  1430. NEW = PREV;
  1431. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1432. NEW.rm_blockcount, NEW.rm_owner,
  1433. NEW.rm_offset, NEW.rm_flags, &i);
  1434. if (error)
  1435. goto done;
  1436. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1437. NEW.rm_flags = newext;
  1438. error = xfs_rmap_update(cur, &NEW);
  1439. if (error)
  1440. goto done;
  1441. break;
  1442. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG:
  1443. /*
  1444. * Setting the first part of a previous oldext extent to newext.
  1445. * The left neighbor is contiguous.
  1446. */
  1447. NEW = PREV;
  1448. error = xfs_rmap_delete(cur, NEW.rm_startblock,
  1449. NEW.rm_blockcount, NEW.rm_owner,
  1450. NEW.rm_offset, NEW.rm_flags);
  1451. if (error)
  1452. goto done;
  1453. NEW.rm_offset += len;
  1454. NEW.rm_startblock += len;
  1455. NEW.rm_blockcount -= len;
  1456. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1457. NEW.rm_blockcount, NEW.rm_owner,
  1458. NEW.rm_offset, NEW.rm_flags);
  1459. if (error)
  1460. goto done;
  1461. NEW = LEFT;
  1462. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1463. NEW.rm_blockcount, NEW.rm_owner,
  1464. NEW.rm_offset, NEW.rm_flags, &i);
  1465. if (error)
  1466. goto done;
  1467. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1468. NEW.rm_blockcount += len;
  1469. error = xfs_rmap_update(cur, &NEW);
  1470. if (error)
  1471. goto done;
  1472. break;
  1473. case RMAP_LEFT_FILLING:
  1474. /*
  1475. * Setting the first part of a previous oldext extent to newext.
  1476. * The left neighbor is not contiguous.
  1477. */
  1478. NEW = PREV;
  1479. error = xfs_rmap_delete(cur, NEW.rm_startblock,
  1480. NEW.rm_blockcount, NEW.rm_owner,
  1481. NEW.rm_offset, NEW.rm_flags);
  1482. if (error)
  1483. goto done;
  1484. NEW.rm_offset += len;
  1485. NEW.rm_startblock += len;
  1486. NEW.rm_blockcount -= len;
  1487. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1488. NEW.rm_blockcount, NEW.rm_owner,
  1489. NEW.rm_offset, NEW.rm_flags);
  1490. if (error)
  1491. goto done;
  1492. error = xfs_rmap_insert(cur, bno, len, owner, offset, newext);
  1493. if (error)
  1494. goto done;
  1495. break;
  1496. case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1497. /*
  1498. * Setting the last part of a previous oldext extent to newext.
  1499. * The right neighbor is contiguous with the new allocation.
  1500. */
  1501. NEW = PREV;
  1502. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1503. NEW.rm_blockcount, NEW.rm_owner,
  1504. NEW.rm_offset, NEW.rm_flags, &i);
  1505. if (error)
  1506. goto done;
  1507. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1508. NEW.rm_blockcount = offset - NEW.rm_offset;
  1509. error = xfs_rmap_update(cur, &NEW);
  1510. if (error)
  1511. goto done;
  1512. NEW = RIGHT;
  1513. error = xfs_rmap_delete(cur, NEW.rm_startblock,
  1514. NEW.rm_blockcount, NEW.rm_owner,
  1515. NEW.rm_offset, NEW.rm_flags);
  1516. if (error)
  1517. goto done;
  1518. NEW.rm_offset = offset;
  1519. NEW.rm_startblock = bno;
  1520. NEW.rm_blockcount += len;
  1521. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1522. NEW.rm_blockcount, NEW.rm_owner,
  1523. NEW.rm_offset, NEW.rm_flags);
  1524. if (error)
  1525. goto done;
  1526. break;
  1527. case RMAP_RIGHT_FILLING:
  1528. /*
  1529. * Setting the last part of a previous oldext extent to newext.
  1530. * The right neighbor is not contiguous.
  1531. */
  1532. NEW = PREV;
  1533. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1534. NEW.rm_blockcount, NEW.rm_owner,
  1535. NEW.rm_offset, NEW.rm_flags, &i);
  1536. if (error)
  1537. goto done;
  1538. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1539. NEW.rm_blockcount -= len;
  1540. error = xfs_rmap_update(cur, &NEW);
  1541. if (error)
  1542. goto done;
  1543. error = xfs_rmap_insert(cur, bno, len, owner, offset, newext);
  1544. if (error)
  1545. goto done;
  1546. break;
  1547. case 0:
  1548. /*
  1549. * Setting the middle part of a previous oldext extent to
  1550. * newext. Contiguity is impossible here.
  1551. * One extent becomes three extents.
  1552. */
  1553. /* new right extent - oldext */
  1554. NEW.rm_startblock = bno + len;
  1555. NEW.rm_owner = owner;
  1556. NEW.rm_offset = new_endoff;
  1557. NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount -
  1558. new_endoff;
  1559. NEW.rm_flags = PREV.rm_flags;
  1560. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1561. NEW.rm_blockcount, NEW.rm_owner, NEW.rm_offset,
  1562. NEW.rm_flags);
  1563. if (error)
  1564. goto done;
  1565. /* new left extent - oldext */
  1566. NEW = PREV;
  1567. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1568. NEW.rm_blockcount, NEW.rm_owner,
  1569. NEW.rm_offset, NEW.rm_flags, &i);
  1570. if (error)
  1571. goto done;
  1572. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1573. NEW.rm_blockcount = offset - NEW.rm_offset;
  1574. error = xfs_rmap_update(cur, &NEW);
  1575. if (error)
  1576. goto done;
  1577. /* new middle extent - newext */
  1578. NEW.rm_startblock = bno;
  1579. NEW.rm_blockcount = len;
  1580. NEW.rm_owner = owner;
  1581. NEW.rm_offset = offset;
  1582. NEW.rm_flags = newext;
  1583. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1584. NEW.rm_blockcount, NEW.rm_owner, NEW.rm_offset,
  1585. NEW.rm_flags);
  1586. if (error)
  1587. goto done;
  1588. break;
  1589. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1590. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1591. case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG:
  1592. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1593. case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1594. case RMAP_LEFT_CONTIG:
  1595. case RMAP_RIGHT_CONTIG:
  1596. /*
  1597. * These cases are all impossible.
  1598. */
  1599. ASSERT(0);
  1600. }
  1601. trace_xfs_rmap_convert_done(mp, cur->bc_private.a.agno, bno, len,
  1602. unwritten, oinfo);
  1603. done:
  1604. if (error)
  1605. trace_xfs_rmap_convert_error(cur->bc_mp,
  1606. cur->bc_private.a.agno, error, _RET_IP_);
  1607. return error;
  1608. }
  1609. #undef NEW
  1610. #undef LEFT
  1611. #undef RIGHT
  1612. #undef PREV
  1613. /*
  1614. * Find an extent in the rmap btree and unmap it. For rmap extent types that
  1615. * can overlap (data fork rmaps on reflink filesystems) we must be careful
  1616. * that the prev/next records in the btree might belong to another owner.
  1617. * Therefore we must use delete+insert to alter any of the key fields.
  1618. *
  1619. * For every other situation there can only be one owner for a given extent,
  1620. * so we can call the regular _free function.
  1621. */
  1622. STATIC int
  1623. xfs_rmap_unmap_shared(
  1624. struct xfs_btree_cur *cur,
  1625. xfs_agblock_t bno,
  1626. xfs_extlen_t len,
  1627. bool unwritten,
  1628. struct xfs_owner_info *oinfo)
  1629. {
  1630. struct xfs_mount *mp = cur->bc_mp;
  1631. struct xfs_rmap_irec ltrec;
  1632. uint64_t ltoff;
  1633. int error = 0;
  1634. int i;
  1635. uint64_t owner;
  1636. uint64_t offset;
  1637. unsigned int flags;
  1638. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  1639. if (unwritten)
  1640. flags |= XFS_RMAP_UNWRITTEN;
  1641. trace_xfs_rmap_unmap(mp, cur->bc_private.a.agno, bno, len,
  1642. unwritten, oinfo);
  1643. /*
  1644. * We should always have a left record because there's a static record
  1645. * for the AG headers at rm_startblock == 0 created by mkfs/growfs that
  1646. * will not ever be removed from the tree.
  1647. */
  1648. error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, flags,
  1649. &ltrec, &i);
  1650. if (error)
  1651. goto out_error;
  1652. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1653. ltoff = ltrec.rm_offset;
  1654. /* Make sure the extent we found covers the entire freeing range. */
  1655. XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_startblock <= bno &&
  1656. ltrec.rm_startblock + ltrec.rm_blockcount >=
  1657. bno + len, out_error);
  1658. /* Make sure the owner matches what we expect to find in the tree. */
  1659. XFS_WANT_CORRUPTED_GOTO(mp, owner == ltrec.rm_owner, out_error);
  1660. /* Make sure the unwritten flag matches. */
  1661. XFS_WANT_CORRUPTED_GOTO(mp, (flags & XFS_RMAP_UNWRITTEN) ==
  1662. (ltrec.rm_flags & XFS_RMAP_UNWRITTEN), out_error);
  1663. /* Check the offset. */
  1664. XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_offset <= offset, out_error);
  1665. XFS_WANT_CORRUPTED_GOTO(mp, offset <= ltoff + ltrec.rm_blockcount,
  1666. out_error);
  1667. if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) {
  1668. /* Exact match, simply remove the record from rmap tree. */
  1669. error = xfs_rmap_delete(cur, ltrec.rm_startblock,
  1670. ltrec.rm_blockcount, ltrec.rm_owner,
  1671. ltrec.rm_offset, ltrec.rm_flags);
  1672. if (error)
  1673. goto out_error;
  1674. } else if (ltrec.rm_startblock == bno) {
  1675. /*
  1676. * Overlap left hand side of extent: move the start, trim the
  1677. * length and update the current record.
  1678. *
  1679. * ltbno ltlen
  1680. * Orig: |oooooooooooooooooooo|
  1681. * Freeing: |fffffffff|
  1682. * Result: |rrrrrrrrrr|
  1683. * bno len
  1684. */
  1685. /* Delete prev rmap. */
  1686. error = xfs_rmap_delete(cur, ltrec.rm_startblock,
  1687. ltrec.rm_blockcount, ltrec.rm_owner,
  1688. ltrec.rm_offset, ltrec.rm_flags);
  1689. if (error)
  1690. goto out_error;
  1691. /* Add an rmap at the new offset. */
  1692. ltrec.rm_startblock += len;
  1693. ltrec.rm_blockcount -= len;
  1694. ltrec.rm_offset += len;
  1695. error = xfs_rmap_insert(cur, ltrec.rm_startblock,
  1696. ltrec.rm_blockcount, ltrec.rm_owner,
  1697. ltrec.rm_offset, ltrec.rm_flags);
  1698. if (error)
  1699. goto out_error;
  1700. } else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) {
  1701. /*
  1702. * Overlap right hand side of extent: trim the length and
  1703. * update the current record.
  1704. *
  1705. * ltbno ltlen
  1706. * Orig: |oooooooooooooooooooo|
  1707. * Freeing: |fffffffff|
  1708. * Result: |rrrrrrrrrr|
  1709. * bno len
  1710. */
  1711. error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
  1712. ltrec.rm_blockcount, ltrec.rm_owner,
  1713. ltrec.rm_offset, ltrec.rm_flags, &i);
  1714. if (error)
  1715. goto out_error;
  1716. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1717. ltrec.rm_blockcount -= len;
  1718. error = xfs_rmap_update(cur, &ltrec);
  1719. if (error)
  1720. goto out_error;
  1721. } else {
  1722. /*
  1723. * Overlap middle of extent: trim the length of the existing
  1724. * record to the length of the new left-extent size, increment
  1725. * the insertion position so we can insert a new record
  1726. * containing the remaining right-extent space.
  1727. *
  1728. * ltbno ltlen
  1729. * Orig: |oooooooooooooooooooo|
  1730. * Freeing: |fffffffff|
  1731. * Result: |rrrrr| |rrrr|
  1732. * bno len
  1733. */
  1734. xfs_extlen_t orig_len = ltrec.rm_blockcount;
  1735. /* Shrink the left side of the rmap */
  1736. error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
  1737. ltrec.rm_blockcount, ltrec.rm_owner,
  1738. ltrec.rm_offset, ltrec.rm_flags, &i);
  1739. if (error)
  1740. goto out_error;
  1741. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1742. ltrec.rm_blockcount = bno - ltrec.rm_startblock;
  1743. error = xfs_rmap_update(cur, &ltrec);
  1744. if (error)
  1745. goto out_error;
  1746. /* Add an rmap at the new offset */
  1747. error = xfs_rmap_insert(cur, bno + len,
  1748. orig_len - len - ltrec.rm_blockcount,
  1749. ltrec.rm_owner, offset + len,
  1750. ltrec.rm_flags);
  1751. if (error)
  1752. goto out_error;
  1753. }
  1754. trace_xfs_rmap_unmap_done(mp, cur->bc_private.a.agno, bno, len,
  1755. unwritten, oinfo);
  1756. out_error:
  1757. if (error)
  1758. trace_xfs_rmap_unmap_error(cur->bc_mp,
  1759. cur->bc_private.a.agno, error, _RET_IP_);
  1760. return error;
  1761. }
  1762. /*
  1763. * Find an extent in the rmap btree and map it. For rmap extent types that
  1764. * can overlap (data fork rmaps on reflink filesystems) we must be careful
  1765. * that the prev/next records in the btree might belong to another owner.
  1766. * Therefore we must use delete+insert to alter any of the key fields.
  1767. *
  1768. * For every other situation there can only be one owner for a given extent,
  1769. * so we can call the regular _alloc function.
  1770. */
  1771. STATIC int
  1772. xfs_rmap_map_shared(
  1773. struct xfs_btree_cur *cur,
  1774. xfs_agblock_t bno,
  1775. xfs_extlen_t len,
  1776. bool unwritten,
  1777. struct xfs_owner_info *oinfo)
  1778. {
  1779. struct xfs_mount *mp = cur->bc_mp;
  1780. struct xfs_rmap_irec ltrec;
  1781. struct xfs_rmap_irec gtrec;
  1782. int have_gt;
  1783. int have_lt;
  1784. int error = 0;
  1785. int i;
  1786. uint64_t owner;
  1787. uint64_t offset;
  1788. unsigned int flags = 0;
  1789. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  1790. if (unwritten)
  1791. flags |= XFS_RMAP_UNWRITTEN;
  1792. trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len,
  1793. unwritten, oinfo);
  1794. /* Is there a left record that abuts our range? */
  1795. error = xfs_rmap_find_left_neighbor(cur, bno, owner, offset, flags,
  1796. &ltrec, &have_lt);
  1797. if (error)
  1798. goto out_error;
  1799. if (have_lt &&
  1800. !xfs_rmap_is_mergeable(&ltrec, owner, flags))
  1801. have_lt = 0;
  1802. /* Is there a right record that abuts our range? */
  1803. error = xfs_rmap_lookup_eq(cur, bno + len, len, owner, offset + len,
  1804. flags, &have_gt);
  1805. if (error)
  1806. goto out_error;
  1807. if (have_gt) {
  1808. error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
  1809. if (error)
  1810. goto out_error;
  1811. XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error);
  1812. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  1813. cur->bc_private.a.agno, gtrec.rm_startblock,
  1814. gtrec.rm_blockcount, gtrec.rm_owner,
  1815. gtrec.rm_offset, gtrec.rm_flags);
  1816. if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
  1817. have_gt = 0;
  1818. }
  1819. if (have_lt &&
  1820. ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
  1821. ltrec.rm_offset + ltrec.rm_blockcount == offset) {
  1822. /*
  1823. * Left edge contiguous, merge into left record.
  1824. *
  1825. * ltbno ltlen
  1826. * orig: |ooooooooo|
  1827. * adding: |aaaaaaaaa|
  1828. * result: |rrrrrrrrrrrrrrrrrrr|
  1829. * bno len
  1830. */
  1831. ltrec.rm_blockcount += len;
  1832. if (have_gt &&
  1833. bno + len == gtrec.rm_startblock &&
  1834. offset + len == gtrec.rm_offset) {
  1835. /*
  1836. * Right edge also contiguous, delete right record
  1837. * and merge into left record.
  1838. *
  1839. * ltbno ltlen gtbno gtlen
  1840. * orig: |ooooooooo| |ooooooooo|
  1841. * adding: |aaaaaaaaa|
  1842. * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
  1843. */
  1844. ltrec.rm_blockcount += gtrec.rm_blockcount;
  1845. error = xfs_rmap_delete(cur, gtrec.rm_startblock,
  1846. gtrec.rm_blockcount, gtrec.rm_owner,
  1847. gtrec.rm_offset, gtrec.rm_flags);
  1848. if (error)
  1849. goto out_error;
  1850. }
  1851. /* Point the cursor back to the left record and update. */
  1852. error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
  1853. ltrec.rm_blockcount, ltrec.rm_owner,
  1854. ltrec.rm_offset, ltrec.rm_flags, &i);
  1855. if (error)
  1856. goto out_error;
  1857. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1858. error = xfs_rmap_update(cur, &ltrec);
  1859. if (error)
  1860. goto out_error;
  1861. } else if (have_gt &&
  1862. bno + len == gtrec.rm_startblock &&
  1863. offset + len == gtrec.rm_offset) {
  1864. /*
  1865. * Right edge contiguous, merge into right record.
  1866. *
  1867. * gtbno gtlen
  1868. * Orig: |ooooooooo|
  1869. * adding: |aaaaaaaaa|
  1870. * Result: |rrrrrrrrrrrrrrrrrrr|
  1871. * bno len
  1872. */
  1873. /* Delete the old record. */
  1874. error = xfs_rmap_delete(cur, gtrec.rm_startblock,
  1875. gtrec.rm_blockcount, gtrec.rm_owner,
  1876. gtrec.rm_offset, gtrec.rm_flags);
  1877. if (error)
  1878. goto out_error;
  1879. /* Move the start and re-add it. */
  1880. gtrec.rm_startblock = bno;
  1881. gtrec.rm_blockcount += len;
  1882. gtrec.rm_offset = offset;
  1883. error = xfs_rmap_insert(cur, gtrec.rm_startblock,
  1884. gtrec.rm_blockcount, gtrec.rm_owner,
  1885. gtrec.rm_offset, gtrec.rm_flags);
  1886. if (error)
  1887. goto out_error;
  1888. } else {
  1889. /*
  1890. * No contiguous edge with identical owner, insert
  1891. * new record at current cursor position.
  1892. */
  1893. error = xfs_rmap_insert(cur, bno, len, owner, offset, flags);
  1894. if (error)
  1895. goto out_error;
  1896. }
  1897. trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len,
  1898. unwritten, oinfo);
  1899. out_error:
  1900. if (error)
  1901. trace_xfs_rmap_map_error(cur->bc_mp,
  1902. cur->bc_private.a.agno, error, _RET_IP_);
  1903. return error;
  1904. }
  1905. /* Insert a raw rmap into the rmapbt. */
  1906. int
  1907. xfs_rmap_map_raw(
  1908. struct xfs_btree_cur *cur,
  1909. struct xfs_rmap_irec *rmap)
  1910. {
  1911. struct xfs_owner_info oinfo;
  1912. oinfo.oi_owner = rmap->rm_owner;
  1913. oinfo.oi_offset = rmap->rm_offset;
  1914. oinfo.oi_flags = 0;
  1915. if (rmap->rm_flags & XFS_RMAP_ATTR_FORK)
  1916. oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
  1917. if (rmap->rm_flags & XFS_RMAP_BMBT_BLOCK)
  1918. oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
  1919. if (rmap->rm_flags || XFS_RMAP_NON_INODE_OWNER(rmap->rm_owner))
  1920. return xfs_rmap_map(cur, rmap->rm_startblock,
  1921. rmap->rm_blockcount,
  1922. rmap->rm_flags & XFS_RMAP_UNWRITTEN,
  1923. &oinfo);
  1924. return xfs_rmap_map_shared(cur, rmap->rm_startblock,
  1925. rmap->rm_blockcount,
  1926. rmap->rm_flags & XFS_RMAP_UNWRITTEN,
  1927. &oinfo);
  1928. }
  1929. struct xfs_rmap_query_range_info {
  1930. xfs_rmap_query_range_fn fn;
  1931. void *priv;
  1932. };
  1933. /* Format btree record and pass to our callback. */
  1934. STATIC int
  1935. xfs_rmap_query_range_helper(
  1936. struct xfs_btree_cur *cur,
  1937. union xfs_btree_rec *rec,
  1938. void *priv)
  1939. {
  1940. struct xfs_rmap_query_range_info *query = priv;
  1941. struct xfs_rmap_irec irec;
  1942. int error;
  1943. error = xfs_rmap_btrec_to_irec(rec, &irec);
  1944. if (error)
  1945. return error;
  1946. return query->fn(cur, &irec, query->priv);
  1947. }
  1948. /* Find all rmaps between two keys. */
  1949. int
  1950. xfs_rmap_query_range(
  1951. struct xfs_btree_cur *cur,
  1952. struct xfs_rmap_irec *low_rec,
  1953. struct xfs_rmap_irec *high_rec,
  1954. xfs_rmap_query_range_fn fn,
  1955. void *priv)
  1956. {
  1957. union xfs_btree_irec low_brec;
  1958. union xfs_btree_irec high_brec;
  1959. struct xfs_rmap_query_range_info query;
  1960. low_brec.r = *low_rec;
  1961. high_brec.r = *high_rec;
  1962. query.priv = priv;
  1963. query.fn = fn;
  1964. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  1965. xfs_rmap_query_range_helper, &query);
  1966. }
  1967. /* Find all rmaps. */
  1968. int
  1969. xfs_rmap_query_all(
  1970. struct xfs_btree_cur *cur,
  1971. xfs_rmap_query_range_fn fn,
  1972. void *priv)
  1973. {
  1974. struct xfs_rmap_query_range_info query;
  1975. query.priv = priv;
  1976. query.fn = fn;
  1977. return xfs_btree_query_all(cur, xfs_rmap_query_range_helper, &query);
  1978. }
  1979. /* Clean up after calling xfs_rmap_finish_one. */
  1980. void
  1981. xfs_rmap_finish_one_cleanup(
  1982. struct xfs_trans *tp,
  1983. struct xfs_btree_cur *rcur,
  1984. int error)
  1985. {
  1986. struct xfs_buf *agbp;
  1987. if (rcur == NULL)
  1988. return;
  1989. agbp = rcur->bc_private.a.agbp;
  1990. xfs_btree_del_cursor(rcur, error);
  1991. if (error)
  1992. xfs_trans_brelse(tp, agbp);
  1993. }
  1994. /*
  1995. * Process one of the deferred rmap operations. We pass back the
  1996. * btree cursor to maintain our lock on the rmapbt between calls.
  1997. * This saves time and eliminates a buffer deadlock between the
  1998. * superblock and the AGF because we'll always grab them in the same
  1999. * order.
  2000. */
  2001. int
  2002. xfs_rmap_finish_one(
  2003. struct xfs_trans *tp,
  2004. enum xfs_rmap_intent_type type,
  2005. uint64_t owner,
  2006. int whichfork,
  2007. xfs_fileoff_t startoff,
  2008. xfs_fsblock_t startblock,
  2009. xfs_filblks_t blockcount,
  2010. xfs_exntst_t state,
  2011. struct xfs_btree_cur **pcur)
  2012. {
  2013. struct xfs_mount *mp = tp->t_mountp;
  2014. struct xfs_btree_cur *rcur;
  2015. struct xfs_buf *agbp = NULL;
  2016. int error = 0;
  2017. xfs_agnumber_t agno;
  2018. struct xfs_owner_info oinfo;
  2019. xfs_agblock_t bno;
  2020. bool unwritten;
  2021. agno = XFS_FSB_TO_AGNO(mp, startblock);
  2022. ASSERT(agno != NULLAGNUMBER);
  2023. bno = XFS_FSB_TO_AGBNO(mp, startblock);
  2024. trace_xfs_rmap_deferred(mp, agno, type, bno, owner, whichfork,
  2025. startoff, blockcount, state);
  2026. if (XFS_TEST_ERROR(false, mp,
  2027. XFS_ERRTAG_RMAP_FINISH_ONE))
  2028. return -EIO;
  2029. /*
  2030. * If we haven't gotten a cursor or the cursor AG doesn't match
  2031. * the startblock, get one now.
  2032. */
  2033. rcur = *pcur;
  2034. if (rcur != NULL && rcur->bc_private.a.agno != agno) {
  2035. xfs_rmap_finish_one_cleanup(tp, rcur, 0);
  2036. rcur = NULL;
  2037. *pcur = NULL;
  2038. }
  2039. if (rcur == NULL) {
  2040. /*
  2041. * Refresh the freelist before we start changing the
  2042. * rmapbt, because a shape change could cause us to
  2043. * allocate blocks.
  2044. */
  2045. error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
  2046. if (error)
  2047. return error;
  2048. if (!agbp)
  2049. return -EFSCORRUPTED;
  2050. rcur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  2051. if (!rcur) {
  2052. error = -ENOMEM;
  2053. goto out_cur;
  2054. }
  2055. }
  2056. *pcur = rcur;
  2057. xfs_rmap_ino_owner(&oinfo, owner, whichfork, startoff);
  2058. unwritten = state == XFS_EXT_UNWRITTEN;
  2059. bno = XFS_FSB_TO_AGBNO(rcur->bc_mp, startblock);
  2060. switch (type) {
  2061. case XFS_RMAP_ALLOC:
  2062. case XFS_RMAP_MAP:
  2063. error = xfs_rmap_map(rcur, bno, blockcount, unwritten, &oinfo);
  2064. break;
  2065. case XFS_RMAP_MAP_SHARED:
  2066. error = xfs_rmap_map_shared(rcur, bno, blockcount, unwritten,
  2067. &oinfo);
  2068. break;
  2069. case XFS_RMAP_FREE:
  2070. case XFS_RMAP_UNMAP:
  2071. error = xfs_rmap_unmap(rcur, bno, blockcount, unwritten,
  2072. &oinfo);
  2073. break;
  2074. case XFS_RMAP_UNMAP_SHARED:
  2075. error = xfs_rmap_unmap_shared(rcur, bno, blockcount, unwritten,
  2076. &oinfo);
  2077. break;
  2078. case XFS_RMAP_CONVERT:
  2079. error = xfs_rmap_convert(rcur, bno, blockcount, !unwritten,
  2080. &oinfo);
  2081. break;
  2082. case XFS_RMAP_CONVERT_SHARED:
  2083. error = xfs_rmap_convert_shared(rcur, bno, blockcount,
  2084. !unwritten, &oinfo);
  2085. break;
  2086. default:
  2087. ASSERT(0);
  2088. error = -EFSCORRUPTED;
  2089. }
  2090. return error;
  2091. out_cur:
  2092. xfs_trans_brelse(tp, agbp);
  2093. return error;
  2094. }
  2095. /*
  2096. * Don't defer an rmap if we aren't an rmap filesystem.
  2097. */
  2098. static bool
  2099. xfs_rmap_update_is_needed(
  2100. struct xfs_mount *mp,
  2101. int whichfork)
  2102. {
  2103. return xfs_sb_version_hasrmapbt(&mp->m_sb) && whichfork != XFS_COW_FORK;
  2104. }
  2105. /*
  2106. * Record a rmap intent; the list is kept sorted first by AG and then by
  2107. * increasing age.
  2108. */
  2109. static int
  2110. __xfs_rmap_add(
  2111. struct xfs_trans *tp,
  2112. enum xfs_rmap_intent_type type,
  2113. uint64_t owner,
  2114. int whichfork,
  2115. struct xfs_bmbt_irec *bmap)
  2116. {
  2117. struct xfs_rmap_intent *ri;
  2118. trace_xfs_rmap_defer(tp->t_mountp,
  2119. XFS_FSB_TO_AGNO(tp->t_mountp, bmap->br_startblock),
  2120. type,
  2121. XFS_FSB_TO_AGBNO(tp->t_mountp, bmap->br_startblock),
  2122. owner, whichfork,
  2123. bmap->br_startoff,
  2124. bmap->br_blockcount,
  2125. bmap->br_state);
  2126. ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_SLEEP | KM_NOFS);
  2127. INIT_LIST_HEAD(&ri->ri_list);
  2128. ri->ri_type = type;
  2129. ri->ri_owner = owner;
  2130. ri->ri_whichfork = whichfork;
  2131. ri->ri_bmap = *bmap;
  2132. xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_RMAP, &ri->ri_list);
  2133. return 0;
  2134. }
  2135. /* Map an extent into a file. */
  2136. int
  2137. xfs_rmap_map_extent(
  2138. struct xfs_trans *tp,
  2139. struct xfs_inode *ip,
  2140. int whichfork,
  2141. struct xfs_bmbt_irec *PREV)
  2142. {
  2143. if (!xfs_rmap_update_is_needed(tp->t_mountp, whichfork))
  2144. return 0;
  2145. return __xfs_rmap_add(tp, xfs_is_reflink_inode(ip) ?
  2146. XFS_RMAP_MAP_SHARED : XFS_RMAP_MAP, ip->i_ino,
  2147. whichfork, PREV);
  2148. }
  2149. /* Unmap an extent out of a file. */
  2150. int
  2151. xfs_rmap_unmap_extent(
  2152. struct xfs_trans *tp,
  2153. struct xfs_inode *ip,
  2154. int whichfork,
  2155. struct xfs_bmbt_irec *PREV)
  2156. {
  2157. if (!xfs_rmap_update_is_needed(tp->t_mountp, whichfork))
  2158. return 0;
  2159. return __xfs_rmap_add(tp, xfs_is_reflink_inode(ip) ?
  2160. XFS_RMAP_UNMAP_SHARED : XFS_RMAP_UNMAP, ip->i_ino,
  2161. whichfork, PREV);
  2162. }
  2163. /*
  2164. * Convert a data fork extent from unwritten to real or vice versa.
  2165. *
  2166. * Note that tp can be NULL here as no transaction is used for COW fork
  2167. * unwritten conversion.
  2168. */
  2169. int
  2170. xfs_rmap_convert_extent(
  2171. struct xfs_mount *mp,
  2172. struct xfs_trans *tp,
  2173. struct xfs_inode *ip,
  2174. int whichfork,
  2175. struct xfs_bmbt_irec *PREV)
  2176. {
  2177. if (!xfs_rmap_update_is_needed(mp, whichfork))
  2178. return 0;
  2179. return __xfs_rmap_add(tp, xfs_is_reflink_inode(ip) ?
  2180. XFS_RMAP_CONVERT_SHARED : XFS_RMAP_CONVERT, ip->i_ino,
  2181. whichfork, PREV);
  2182. }
  2183. /* Schedule the creation of an rmap for non-file data. */
  2184. int
  2185. xfs_rmap_alloc_extent(
  2186. struct xfs_trans *tp,
  2187. xfs_agnumber_t agno,
  2188. xfs_agblock_t bno,
  2189. xfs_extlen_t len,
  2190. uint64_t owner)
  2191. {
  2192. struct xfs_bmbt_irec bmap;
  2193. if (!xfs_rmap_update_is_needed(tp->t_mountp, XFS_DATA_FORK))
  2194. return 0;
  2195. bmap.br_startblock = XFS_AGB_TO_FSB(tp->t_mountp, agno, bno);
  2196. bmap.br_blockcount = len;
  2197. bmap.br_startoff = 0;
  2198. bmap.br_state = XFS_EXT_NORM;
  2199. return __xfs_rmap_add(tp, XFS_RMAP_ALLOC, owner, XFS_DATA_FORK, &bmap);
  2200. }
  2201. /* Schedule the deletion of an rmap for non-file data. */
  2202. int
  2203. xfs_rmap_free_extent(
  2204. struct xfs_trans *tp,
  2205. xfs_agnumber_t agno,
  2206. xfs_agblock_t bno,
  2207. xfs_extlen_t len,
  2208. uint64_t owner)
  2209. {
  2210. struct xfs_bmbt_irec bmap;
  2211. if (!xfs_rmap_update_is_needed(tp->t_mountp, XFS_DATA_FORK))
  2212. return 0;
  2213. bmap.br_startblock = XFS_AGB_TO_FSB(tp->t_mountp, agno, bno);
  2214. bmap.br_blockcount = len;
  2215. bmap.br_startoff = 0;
  2216. bmap.br_state = XFS_EXT_NORM;
  2217. return __xfs_rmap_add(tp, XFS_RMAP_FREE, owner, XFS_DATA_FORK, &bmap);
  2218. }
  2219. /* Compare rmap records. Returns -1 if a < b, 1 if a > b, and 0 if equal. */
  2220. int
  2221. xfs_rmap_compare(
  2222. const struct xfs_rmap_irec *a,
  2223. const struct xfs_rmap_irec *b)
  2224. {
  2225. __u64 oa;
  2226. __u64 ob;
  2227. oa = xfs_rmap_irec_offset_pack(a);
  2228. ob = xfs_rmap_irec_offset_pack(b);
  2229. if (a->rm_startblock < b->rm_startblock)
  2230. return -1;
  2231. else if (a->rm_startblock > b->rm_startblock)
  2232. return 1;
  2233. else if (a->rm_owner < b->rm_owner)
  2234. return -1;
  2235. else if (a->rm_owner > b->rm_owner)
  2236. return 1;
  2237. else if (oa < ob)
  2238. return -1;
  2239. else if (oa > ob)
  2240. return 1;
  2241. else
  2242. return 0;
  2243. }
  2244. /* Is there a record covering a given extent? */
  2245. int
  2246. xfs_rmap_has_record(
  2247. struct xfs_btree_cur *cur,
  2248. xfs_agblock_t bno,
  2249. xfs_extlen_t len,
  2250. bool *exists)
  2251. {
  2252. union xfs_btree_irec low;
  2253. union xfs_btree_irec high;
  2254. memset(&low, 0, sizeof(low));
  2255. low.r.rm_startblock = bno;
  2256. memset(&high, 0xFF, sizeof(high));
  2257. high.r.rm_startblock = bno + len - 1;
  2258. return xfs_btree_has_record(cur, &low, &high, exists);
  2259. }
  2260. /*
  2261. * Is there a record for this owner completely covering a given physical
  2262. * extent? If so, *has_rmap will be set to true. If there is no record
  2263. * or the record only covers part of the range, we set *has_rmap to false.
  2264. * This function doesn't perform range lookups or offset checks, so it is
  2265. * not suitable for checking data fork blocks.
  2266. */
  2267. int
  2268. xfs_rmap_record_exists(
  2269. struct xfs_btree_cur *cur,
  2270. xfs_agblock_t bno,
  2271. xfs_extlen_t len,
  2272. struct xfs_owner_info *oinfo,
  2273. bool *has_rmap)
  2274. {
  2275. uint64_t owner;
  2276. uint64_t offset;
  2277. unsigned int flags;
  2278. int has_record;
  2279. struct xfs_rmap_irec irec;
  2280. int error;
  2281. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  2282. ASSERT(XFS_RMAP_NON_INODE_OWNER(owner) ||
  2283. (flags & XFS_RMAP_BMBT_BLOCK));
  2284. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
  2285. &has_record);
  2286. if (error)
  2287. return error;
  2288. if (!has_record) {
  2289. *has_rmap = false;
  2290. return 0;
  2291. }
  2292. error = xfs_rmap_get_rec(cur, &irec, &has_record);
  2293. if (error)
  2294. return error;
  2295. if (!has_record) {
  2296. *has_rmap = false;
  2297. return 0;
  2298. }
  2299. *has_rmap = (irec.rm_owner == owner && irec.rm_startblock <= bno &&
  2300. irec.rm_startblock + irec.rm_blockcount >= bno + len);
  2301. return 0;
  2302. }
  2303. struct xfs_rmap_key_state {
  2304. uint64_t owner;
  2305. uint64_t offset;
  2306. unsigned int flags;
  2307. bool has_rmap;
  2308. };
  2309. /* For each rmap given, figure out if it doesn't match the key we want. */
  2310. STATIC int
  2311. xfs_rmap_has_other_keys_helper(
  2312. struct xfs_btree_cur *cur,
  2313. struct xfs_rmap_irec *rec,
  2314. void *priv)
  2315. {
  2316. struct xfs_rmap_key_state *rks = priv;
  2317. if (rks->owner == rec->rm_owner && rks->offset == rec->rm_offset &&
  2318. ((rks->flags & rec->rm_flags) & XFS_RMAP_KEY_FLAGS) == rks->flags)
  2319. return 0;
  2320. rks->has_rmap = true;
  2321. return XFS_BTREE_QUERY_RANGE_ABORT;
  2322. }
  2323. /*
  2324. * Given an extent and some owner info, can we find records overlapping
  2325. * the extent whose owner info does not match the given owner?
  2326. */
  2327. int
  2328. xfs_rmap_has_other_keys(
  2329. struct xfs_btree_cur *cur,
  2330. xfs_agblock_t bno,
  2331. xfs_extlen_t len,
  2332. struct xfs_owner_info *oinfo,
  2333. bool *has_rmap)
  2334. {
  2335. struct xfs_rmap_irec low = {0};
  2336. struct xfs_rmap_irec high;
  2337. struct xfs_rmap_key_state rks;
  2338. int error;
  2339. xfs_owner_info_unpack(oinfo, &rks.owner, &rks.offset, &rks.flags);
  2340. rks.has_rmap = false;
  2341. low.rm_startblock = bno;
  2342. memset(&high, 0xFF, sizeof(high));
  2343. high.rm_startblock = bno + len - 1;
  2344. error = xfs_rmap_query_range(cur, &low, &high,
  2345. xfs_rmap_has_other_keys_helper, &rks);
  2346. *has_rmap = rks.has_rmap;
  2347. return error;
  2348. }