xfs_rmap.c 62 KB

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