xfs_rmap.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  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. if (error)
  612. goto out_error;
  613. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  614. return 0;
  615. out_error:
  616. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  617. return error;
  618. }
  619. /*
  620. * A mergeable rmap must have the same owner and the same values for
  621. * the unwritten, attr_fork, and bmbt flags. The startblock and
  622. * offset are checked separately.
  623. */
  624. static bool
  625. xfs_rmap_is_mergeable(
  626. struct xfs_rmap_irec *irec,
  627. uint64_t owner,
  628. unsigned int flags)
  629. {
  630. if (irec->rm_owner == XFS_RMAP_OWN_NULL)
  631. return false;
  632. if (irec->rm_owner != owner)
  633. return false;
  634. if ((flags & XFS_RMAP_UNWRITTEN) ^
  635. (irec->rm_flags & XFS_RMAP_UNWRITTEN))
  636. return false;
  637. if ((flags & XFS_RMAP_ATTR_FORK) ^
  638. (irec->rm_flags & XFS_RMAP_ATTR_FORK))
  639. return false;
  640. if ((flags & XFS_RMAP_BMBT_BLOCK) ^
  641. (irec->rm_flags & XFS_RMAP_BMBT_BLOCK))
  642. return false;
  643. return true;
  644. }
  645. /*
  646. * When we allocate a new block, the first thing we do is add a reference to
  647. * the extent in the rmap btree. This takes the form of a [agbno, length,
  648. * owner, offset] record. Flags are encoded in the high bits of the offset
  649. * field.
  650. */
  651. STATIC int
  652. xfs_rmap_map(
  653. struct xfs_btree_cur *cur,
  654. xfs_agblock_t bno,
  655. xfs_extlen_t len,
  656. bool unwritten,
  657. struct xfs_owner_info *oinfo)
  658. {
  659. struct xfs_mount *mp = cur->bc_mp;
  660. struct xfs_rmap_irec ltrec;
  661. struct xfs_rmap_irec gtrec;
  662. int have_gt;
  663. int have_lt;
  664. int error = 0;
  665. int i;
  666. uint64_t owner;
  667. uint64_t offset;
  668. unsigned int flags = 0;
  669. bool ignore_off;
  670. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  671. ASSERT(owner != 0);
  672. ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) ||
  673. (flags & XFS_RMAP_BMBT_BLOCK);
  674. if (unwritten)
  675. flags |= XFS_RMAP_UNWRITTEN;
  676. trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len,
  677. unwritten, oinfo);
  678. ASSERT(!xfs_rmap_should_skip_owner_update(oinfo));
  679. /*
  680. * For the initial lookup, look for an exact match or the left-adjacent
  681. * record for our insertion point. This will also give us the record for
  682. * start block contiguity tests.
  683. */
  684. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
  685. &have_lt);
  686. if (error)
  687. goto out_error;
  688. XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error);
  689. error = xfs_rmap_get_rec(cur, &ltrec, &have_lt);
  690. if (error)
  691. goto out_error;
  692. XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error);
  693. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  694. cur->bc_private.a.agno, ltrec.rm_startblock,
  695. ltrec.rm_blockcount, ltrec.rm_owner,
  696. ltrec.rm_offset, ltrec.rm_flags);
  697. if (!xfs_rmap_is_mergeable(&ltrec, owner, flags))
  698. have_lt = 0;
  699. XFS_WANT_CORRUPTED_GOTO(mp,
  700. have_lt == 0 ||
  701. ltrec.rm_startblock + ltrec.rm_blockcount <= bno, out_error);
  702. /*
  703. * Increment the cursor to see if we have a right-adjacent record to our
  704. * insertion point. This will give us the record for end block
  705. * contiguity tests.
  706. */
  707. error = xfs_btree_increment(cur, 0, &have_gt);
  708. if (error)
  709. goto out_error;
  710. if (have_gt) {
  711. error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
  712. if (error)
  713. goto out_error;
  714. XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error);
  715. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= gtrec.rm_startblock,
  716. out_error);
  717. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  718. cur->bc_private.a.agno, gtrec.rm_startblock,
  719. gtrec.rm_blockcount, gtrec.rm_owner,
  720. gtrec.rm_offset, gtrec.rm_flags);
  721. if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
  722. have_gt = 0;
  723. }
  724. /*
  725. * Note: cursor currently points one record to the right of ltrec, even
  726. * if there is no record in the tree to the right.
  727. */
  728. if (have_lt &&
  729. ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
  730. (ignore_off || ltrec.rm_offset + ltrec.rm_blockcount == offset)) {
  731. /*
  732. * left edge contiguous, merge into left record.
  733. *
  734. * ltbno ltlen
  735. * orig: |ooooooooo|
  736. * adding: |aaaaaaaaa|
  737. * result: |rrrrrrrrrrrrrrrrrrr|
  738. * bno len
  739. */
  740. ltrec.rm_blockcount += len;
  741. if (have_gt &&
  742. bno + len == gtrec.rm_startblock &&
  743. (ignore_off || offset + len == gtrec.rm_offset) &&
  744. (unsigned long)ltrec.rm_blockcount + len +
  745. gtrec.rm_blockcount <= XFS_RMAP_LEN_MAX) {
  746. /*
  747. * right edge also contiguous, delete right record
  748. * and merge into left record.
  749. *
  750. * ltbno ltlen gtbno gtlen
  751. * orig: |ooooooooo| |ooooooooo|
  752. * adding: |aaaaaaaaa|
  753. * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
  754. */
  755. ltrec.rm_blockcount += gtrec.rm_blockcount;
  756. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  757. gtrec.rm_startblock,
  758. gtrec.rm_blockcount,
  759. gtrec.rm_owner,
  760. gtrec.rm_offset,
  761. gtrec.rm_flags);
  762. error = xfs_btree_delete(cur, &i);
  763. if (error)
  764. goto out_error;
  765. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  766. }
  767. /* point the cursor back to the left record and update */
  768. error = xfs_btree_decrement(cur, 0, &have_gt);
  769. if (error)
  770. goto out_error;
  771. error = xfs_rmap_update(cur, &ltrec);
  772. if (error)
  773. goto out_error;
  774. } else if (have_gt &&
  775. bno + len == gtrec.rm_startblock &&
  776. (ignore_off || offset + len == gtrec.rm_offset)) {
  777. /*
  778. * right edge contiguous, merge into right record.
  779. *
  780. * gtbno gtlen
  781. * Orig: |ooooooooo|
  782. * adding: |aaaaaaaaa|
  783. * Result: |rrrrrrrrrrrrrrrrrrr|
  784. * bno len
  785. */
  786. gtrec.rm_startblock = bno;
  787. gtrec.rm_blockcount += len;
  788. if (!ignore_off)
  789. gtrec.rm_offset = offset;
  790. error = xfs_rmap_update(cur, &gtrec);
  791. if (error)
  792. goto out_error;
  793. } else {
  794. /*
  795. * no contiguous edge with identical owner, insert
  796. * new record at current cursor position.
  797. */
  798. cur->bc_rec.r.rm_startblock = bno;
  799. cur->bc_rec.r.rm_blockcount = len;
  800. cur->bc_rec.r.rm_owner = owner;
  801. cur->bc_rec.r.rm_offset = offset;
  802. cur->bc_rec.r.rm_flags = flags;
  803. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len,
  804. owner, offset, flags);
  805. error = xfs_btree_insert(cur, &i);
  806. if (error)
  807. goto out_error;
  808. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  809. }
  810. trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len,
  811. unwritten, oinfo);
  812. out_error:
  813. if (error)
  814. trace_xfs_rmap_map_error(mp, cur->bc_private.a.agno,
  815. error, _RET_IP_);
  816. return error;
  817. }
  818. /*
  819. * Add a reference to an extent in the rmap btree.
  820. */
  821. int
  822. xfs_rmap_alloc(
  823. struct xfs_trans *tp,
  824. struct xfs_buf *agbp,
  825. xfs_agnumber_t agno,
  826. xfs_agblock_t bno,
  827. xfs_extlen_t len,
  828. struct xfs_owner_info *oinfo)
  829. {
  830. struct xfs_mount *mp = tp->t_mountp;
  831. struct xfs_btree_cur *cur;
  832. int error;
  833. if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
  834. return 0;
  835. cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  836. error = xfs_rmap_map(cur, bno, len, false, oinfo);
  837. if (error)
  838. goto out_error;
  839. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  840. return 0;
  841. out_error:
  842. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  843. return error;
  844. }
  845. #define RMAP_LEFT_CONTIG (1 << 0)
  846. #define RMAP_RIGHT_CONTIG (1 << 1)
  847. #define RMAP_LEFT_FILLING (1 << 2)
  848. #define RMAP_RIGHT_FILLING (1 << 3)
  849. #define RMAP_LEFT_VALID (1 << 6)
  850. #define RMAP_RIGHT_VALID (1 << 7)
  851. #define LEFT r[0]
  852. #define RIGHT r[1]
  853. #define PREV r[2]
  854. #define NEW r[3]
  855. /*
  856. * Convert an unwritten extent to a real extent or vice versa.
  857. * Does not handle overlapping extents.
  858. */
  859. STATIC int
  860. xfs_rmap_convert(
  861. struct xfs_btree_cur *cur,
  862. xfs_agblock_t bno,
  863. xfs_extlen_t len,
  864. bool unwritten,
  865. struct xfs_owner_info *oinfo)
  866. {
  867. struct xfs_mount *mp = cur->bc_mp;
  868. struct xfs_rmap_irec r[4]; /* neighbor extent entries */
  869. /* left is 0, right is 1, prev is 2 */
  870. /* new is 3 */
  871. uint64_t owner;
  872. uint64_t offset;
  873. uint64_t new_endoff;
  874. unsigned int oldext;
  875. unsigned int newext;
  876. unsigned int flags = 0;
  877. int i;
  878. int state = 0;
  879. int error;
  880. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  881. ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) ||
  882. (flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))));
  883. oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0;
  884. new_endoff = offset + len;
  885. trace_xfs_rmap_convert(mp, cur->bc_private.a.agno, bno, len,
  886. unwritten, oinfo);
  887. /*
  888. * For the initial lookup, look for an exact match or the left-adjacent
  889. * record for our insertion point. This will also give us the record for
  890. * start block contiguity tests.
  891. */
  892. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
  893. if (error)
  894. goto done;
  895. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  896. error = xfs_rmap_get_rec(cur, &PREV, &i);
  897. if (error)
  898. goto done;
  899. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  900. trace_xfs_rmap_lookup_le_range_result(cur->bc_mp,
  901. cur->bc_private.a.agno, PREV.rm_startblock,
  902. PREV.rm_blockcount, PREV.rm_owner,
  903. PREV.rm_offset, PREV.rm_flags);
  904. ASSERT(PREV.rm_offset <= offset);
  905. ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff);
  906. ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext);
  907. newext = ~oldext & XFS_RMAP_UNWRITTEN;
  908. /*
  909. * Set flags determining what part of the previous oldext allocation
  910. * extent is being replaced by a newext allocation.
  911. */
  912. if (PREV.rm_offset == offset)
  913. state |= RMAP_LEFT_FILLING;
  914. if (PREV.rm_offset + PREV.rm_blockcount == new_endoff)
  915. state |= RMAP_RIGHT_FILLING;
  916. /*
  917. * Decrement the cursor to see if we have a left-adjacent record to our
  918. * insertion point. This will give us the record for end block
  919. * contiguity tests.
  920. */
  921. error = xfs_btree_decrement(cur, 0, &i);
  922. if (error)
  923. goto done;
  924. if (i) {
  925. state |= RMAP_LEFT_VALID;
  926. error = xfs_rmap_get_rec(cur, &LEFT, &i);
  927. if (error)
  928. goto done;
  929. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  930. XFS_WANT_CORRUPTED_GOTO(mp,
  931. LEFT.rm_startblock + LEFT.rm_blockcount <= bno,
  932. done);
  933. trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp,
  934. cur->bc_private.a.agno, LEFT.rm_startblock,
  935. LEFT.rm_blockcount, LEFT.rm_owner,
  936. LEFT.rm_offset, LEFT.rm_flags);
  937. if (LEFT.rm_startblock + LEFT.rm_blockcount == bno &&
  938. LEFT.rm_offset + LEFT.rm_blockcount == offset &&
  939. xfs_rmap_is_mergeable(&LEFT, owner, newext))
  940. state |= RMAP_LEFT_CONTIG;
  941. }
  942. /*
  943. * Increment the cursor to see if we have a right-adjacent record to our
  944. * insertion point. This will give us the record for end block
  945. * contiguity tests.
  946. */
  947. error = xfs_btree_increment(cur, 0, &i);
  948. if (error)
  949. goto done;
  950. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  951. error = xfs_btree_increment(cur, 0, &i);
  952. if (error)
  953. goto done;
  954. if (i) {
  955. state |= RMAP_RIGHT_VALID;
  956. error = xfs_rmap_get_rec(cur, &RIGHT, &i);
  957. if (error)
  958. goto done;
  959. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  960. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= RIGHT.rm_startblock,
  961. done);
  962. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  963. cur->bc_private.a.agno, RIGHT.rm_startblock,
  964. RIGHT.rm_blockcount, RIGHT.rm_owner,
  965. RIGHT.rm_offset, RIGHT.rm_flags);
  966. if (bno + len == RIGHT.rm_startblock &&
  967. offset + len == RIGHT.rm_offset &&
  968. xfs_rmap_is_mergeable(&RIGHT, owner, newext))
  969. state |= RMAP_RIGHT_CONTIG;
  970. }
  971. /* check that left + prev + right is not too long */
  972. if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  973. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) ==
  974. (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  975. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) &&
  976. (unsigned long)LEFT.rm_blockcount + len +
  977. RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX)
  978. state &= ~RMAP_RIGHT_CONTIG;
  979. trace_xfs_rmap_convert_state(mp, cur->bc_private.a.agno, state,
  980. _RET_IP_);
  981. /* reset the cursor back to PREV */
  982. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i);
  983. if (error)
  984. goto done;
  985. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  986. /*
  987. * Switch out based on the FILLING and CONTIG state bits.
  988. */
  989. switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  990. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) {
  991. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  992. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  993. /*
  994. * Setting all of a previous oldext extent to newext.
  995. * The left and right neighbors are both contiguous with new.
  996. */
  997. error = xfs_btree_increment(cur, 0, &i);
  998. if (error)
  999. goto done;
  1000. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1001. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  1002. RIGHT.rm_startblock, RIGHT.rm_blockcount,
  1003. RIGHT.rm_owner, RIGHT.rm_offset,
  1004. RIGHT.rm_flags);
  1005. error = xfs_btree_delete(cur, &i);
  1006. if (error)
  1007. goto done;
  1008. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1009. error = xfs_btree_decrement(cur, 0, &i);
  1010. if (error)
  1011. goto done;
  1012. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1013. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  1014. PREV.rm_startblock, PREV.rm_blockcount,
  1015. PREV.rm_owner, PREV.rm_offset,
  1016. PREV.rm_flags);
  1017. error = xfs_btree_delete(cur, &i);
  1018. if (error)
  1019. goto done;
  1020. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1021. error = xfs_btree_decrement(cur, 0, &i);
  1022. if (error)
  1023. goto done;
  1024. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1025. NEW = LEFT;
  1026. NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount;
  1027. error = xfs_rmap_update(cur, &NEW);
  1028. if (error)
  1029. goto done;
  1030. break;
  1031. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1032. /*
  1033. * Setting all of a previous oldext extent to newext.
  1034. * The left neighbor is contiguous, the right is not.
  1035. */
  1036. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  1037. PREV.rm_startblock, PREV.rm_blockcount,
  1038. PREV.rm_owner, PREV.rm_offset,
  1039. PREV.rm_flags);
  1040. error = xfs_btree_delete(cur, &i);
  1041. if (error)
  1042. goto done;
  1043. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1044. error = xfs_btree_decrement(cur, 0, &i);
  1045. if (error)
  1046. goto done;
  1047. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1048. NEW = LEFT;
  1049. NEW.rm_blockcount += PREV.rm_blockcount;
  1050. error = xfs_rmap_update(cur, &NEW);
  1051. if (error)
  1052. goto done;
  1053. break;
  1054. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1055. /*
  1056. * Setting all of a previous oldext extent to newext.
  1057. * The right neighbor is contiguous, the left is not.
  1058. */
  1059. error = xfs_btree_increment(cur, 0, &i);
  1060. if (error)
  1061. goto done;
  1062. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1063. trace_xfs_rmap_delete(mp, cur->bc_private.a.agno,
  1064. RIGHT.rm_startblock, RIGHT.rm_blockcount,
  1065. RIGHT.rm_owner, RIGHT.rm_offset,
  1066. RIGHT.rm_flags);
  1067. error = xfs_btree_delete(cur, &i);
  1068. if (error)
  1069. goto done;
  1070. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1071. error = xfs_btree_decrement(cur, 0, &i);
  1072. if (error)
  1073. goto done;
  1074. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1075. NEW = PREV;
  1076. NEW.rm_blockcount = len + RIGHT.rm_blockcount;
  1077. NEW.rm_flags = newext;
  1078. error = xfs_rmap_update(cur, &NEW);
  1079. if (error)
  1080. goto done;
  1081. break;
  1082. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING:
  1083. /*
  1084. * Setting all of a previous oldext extent to newext.
  1085. * Neither the left nor right neighbors are contiguous with
  1086. * the new one.
  1087. */
  1088. NEW = PREV;
  1089. NEW.rm_flags = newext;
  1090. error = xfs_rmap_update(cur, &NEW);
  1091. if (error)
  1092. goto done;
  1093. break;
  1094. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG:
  1095. /*
  1096. * Setting the first part of a previous oldext extent to newext.
  1097. * The left neighbor is contiguous.
  1098. */
  1099. NEW = PREV;
  1100. NEW.rm_offset += len;
  1101. NEW.rm_startblock += len;
  1102. NEW.rm_blockcount -= len;
  1103. error = xfs_rmap_update(cur, &NEW);
  1104. if (error)
  1105. goto done;
  1106. error = xfs_btree_decrement(cur, 0, &i);
  1107. if (error)
  1108. goto done;
  1109. NEW = LEFT;
  1110. NEW.rm_blockcount += len;
  1111. error = xfs_rmap_update(cur, &NEW);
  1112. if (error)
  1113. goto done;
  1114. break;
  1115. case RMAP_LEFT_FILLING:
  1116. /*
  1117. * Setting the first part of a previous oldext extent to newext.
  1118. * The left neighbor is not contiguous.
  1119. */
  1120. NEW = PREV;
  1121. NEW.rm_startblock += len;
  1122. NEW.rm_offset += len;
  1123. NEW.rm_blockcount -= len;
  1124. error = xfs_rmap_update(cur, &NEW);
  1125. if (error)
  1126. goto done;
  1127. NEW.rm_startblock = bno;
  1128. NEW.rm_owner = owner;
  1129. NEW.rm_offset = offset;
  1130. NEW.rm_blockcount = len;
  1131. NEW.rm_flags = newext;
  1132. cur->bc_rec.r = NEW;
  1133. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno,
  1134. len, owner, offset, newext);
  1135. error = xfs_btree_insert(cur, &i);
  1136. if (error)
  1137. goto done;
  1138. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1139. break;
  1140. case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1141. /*
  1142. * Setting the last part of a previous oldext extent to newext.
  1143. * The right neighbor is contiguous with the new allocation.
  1144. */
  1145. NEW = PREV;
  1146. NEW.rm_blockcount -= len;
  1147. error = xfs_rmap_update(cur, &NEW);
  1148. if (error)
  1149. goto done;
  1150. error = xfs_btree_increment(cur, 0, &i);
  1151. if (error)
  1152. goto done;
  1153. NEW = RIGHT;
  1154. NEW.rm_offset = offset;
  1155. NEW.rm_startblock = bno;
  1156. NEW.rm_blockcount += len;
  1157. error = xfs_rmap_update(cur, &NEW);
  1158. if (error)
  1159. goto done;
  1160. break;
  1161. case RMAP_RIGHT_FILLING:
  1162. /*
  1163. * Setting the last part of a previous oldext extent to newext.
  1164. * The right neighbor is not contiguous.
  1165. */
  1166. NEW = PREV;
  1167. NEW.rm_blockcount -= len;
  1168. error = xfs_rmap_update(cur, &NEW);
  1169. if (error)
  1170. goto done;
  1171. error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
  1172. oldext, &i);
  1173. if (error)
  1174. goto done;
  1175. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1176. NEW.rm_startblock = bno;
  1177. NEW.rm_owner = owner;
  1178. NEW.rm_offset = offset;
  1179. NEW.rm_blockcount = len;
  1180. NEW.rm_flags = newext;
  1181. cur->bc_rec.r = NEW;
  1182. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno,
  1183. len, owner, offset, newext);
  1184. error = xfs_btree_insert(cur, &i);
  1185. if (error)
  1186. goto done;
  1187. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1188. break;
  1189. case 0:
  1190. /*
  1191. * Setting the middle part of a previous oldext extent to
  1192. * newext. Contiguity is impossible here.
  1193. * One extent becomes three extents.
  1194. */
  1195. /* new right extent - oldext */
  1196. NEW.rm_startblock = bno + len;
  1197. NEW.rm_owner = owner;
  1198. NEW.rm_offset = new_endoff;
  1199. NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount -
  1200. new_endoff;
  1201. NEW.rm_flags = PREV.rm_flags;
  1202. error = xfs_rmap_update(cur, &NEW);
  1203. if (error)
  1204. goto done;
  1205. /* new left extent - oldext */
  1206. NEW = PREV;
  1207. NEW.rm_blockcount = offset - PREV.rm_offset;
  1208. cur->bc_rec.r = NEW;
  1209. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno,
  1210. NEW.rm_startblock, NEW.rm_blockcount,
  1211. NEW.rm_owner, NEW.rm_offset,
  1212. NEW.rm_flags);
  1213. error = xfs_btree_insert(cur, &i);
  1214. if (error)
  1215. goto done;
  1216. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1217. /*
  1218. * Reset the cursor to the position of the new extent
  1219. * we are about to insert as we can't trust it after
  1220. * the previous insert.
  1221. */
  1222. error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset,
  1223. oldext, &i);
  1224. if (error)
  1225. goto done;
  1226. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1227. /* new middle extent - newext */
  1228. cur->bc_rec.r.rm_flags &= ~XFS_RMAP_UNWRITTEN;
  1229. cur->bc_rec.r.rm_flags |= newext;
  1230. trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len,
  1231. owner, offset, newext);
  1232. error = xfs_btree_insert(cur, &i);
  1233. if (error)
  1234. goto done;
  1235. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1236. break;
  1237. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1238. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1239. case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG:
  1240. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1241. case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1242. case RMAP_LEFT_CONTIG:
  1243. case RMAP_RIGHT_CONTIG:
  1244. /*
  1245. * These cases are all impossible.
  1246. */
  1247. ASSERT(0);
  1248. }
  1249. trace_xfs_rmap_convert_done(mp, cur->bc_private.a.agno, bno, len,
  1250. unwritten, oinfo);
  1251. done:
  1252. if (error)
  1253. trace_xfs_rmap_convert_error(cur->bc_mp,
  1254. cur->bc_private.a.agno, error, _RET_IP_);
  1255. return error;
  1256. }
  1257. /*
  1258. * Convert an unwritten extent to a real extent or vice versa. If there is no
  1259. * possibility of overlapping extents, delegate to the simpler convert
  1260. * function.
  1261. */
  1262. STATIC int
  1263. xfs_rmap_convert_shared(
  1264. struct xfs_btree_cur *cur,
  1265. xfs_agblock_t bno,
  1266. xfs_extlen_t len,
  1267. bool unwritten,
  1268. struct xfs_owner_info *oinfo)
  1269. {
  1270. struct xfs_mount *mp = cur->bc_mp;
  1271. struct xfs_rmap_irec r[4]; /* neighbor extent entries */
  1272. /* left is 0, right is 1, prev is 2 */
  1273. /* new is 3 */
  1274. uint64_t owner;
  1275. uint64_t offset;
  1276. uint64_t new_endoff;
  1277. unsigned int oldext;
  1278. unsigned int newext;
  1279. unsigned int flags = 0;
  1280. int i;
  1281. int state = 0;
  1282. int error;
  1283. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  1284. ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) ||
  1285. (flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))));
  1286. oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0;
  1287. new_endoff = offset + len;
  1288. trace_xfs_rmap_convert(mp, cur->bc_private.a.agno, bno, len,
  1289. unwritten, oinfo);
  1290. /*
  1291. * For the initial lookup, look for and exact match or the left-adjacent
  1292. * record for our insertion point. This will also give us the record for
  1293. * start block contiguity tests.
  1294. */
  1295. error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, flags,
  1296. &PREV, &i);
  1297. if (error)
  1298. goto done;
  1299. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1300. ASSERT(PREV.rm_offset <= offset);
  1301. ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff);
  1302. ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext);
  1303. newext = ~oldext & XFS_RMAP_UNWRITTEN;
  1304. /*
  1305. * Set flags determining what part of the previous oldext allocation
  1306. * extent is being replaced by a newext allocation.
  1307. */
  1308. if (PREV.rm_offset == offset)
  1309. state |= RMAP_LEFT_FILLING;
  1310. if (PREV.rm_offset + PREV.rm_blockcount == new_endoff)
  1311. state |= RMAP_RIGHT_FILLING;
  1312. /* Is there a left record that abuts our range? */
  1313. error = xfs_rmap_find_left_neighbor(cur, bno, owner, offset, newext,
  1314. &LEFT, &i);
  1315. if (error)
  1316. goto done;
  1317. if (i) {
  1318. state |= RMAP_LEFT_VALID;
  1319. XFS_WANT_CORRUPTED_GOTO(mp,
  1320. LEFT.rm_startblock + LEFT.rm_blockcount <= bno,
  1321. done);
  1322. if (xfs_rmap_is_mergeable(&LEFT, owner, newext))
  1323. state |= RMAP_LEFT_CONTIG;
  1324. }
  1325. /* Is there a right record that abuts our range? */
  1326. error = xfs_rmap_lookup_eq(cur, bno + len, len, owner, offset + len,
  1327. newext, &i);
  1328. if (error)
  1329. goto done;
  1330. if (i) {
  1331. state |= RMAP_RIGHT_VALID;
  1332. error = xfs_rmap_get_rec(cur, &RIGHT, &i);
  1333. if (error)
  1334. goto done;
  1335. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1336. XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= RIGHT.rm_startblock,
  1337. done);
  1338. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  1339. cur->bc_private.a.agno, RIGHT.rm_startblock,
  1340. RIGHT.rm_blockcount, RIGHT.rm_owner,
  1341. RIGHT.rm_offset, RIGHT.rm_flags);
  1342. if (xfs_rmap_is_mergeable(&RIGHT, owner, newext))
  1343. state |= RMAP_RIGHT_CONTIG;
  1344. }
  1345. /* check that left + prev + right is not too long */
  1346. if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1347. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) ==
  1348. (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1349. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) &&
  1350. (unsigned long)LEFT.rm_blockcount + len +
  1351. RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX)
  1352. state &= ~RMAP_RIGHT_CONTIG;
  1353. trace_xfs_rmap_convert_state(mp, cur->bc_private.a.agno, state,
  1354. _RET_IP_);
  1355. /*
  1356. * Switch out based on the FILLING and CONTIG state bits.
  1357. */
  1358. switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1359. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) {
  1360. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG |
  1361. RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1362. /*
  1363. * Setting all of a previous oldext extent to newext.
  1364. * The left and right neighbors are both contiguous with new.
  1365. */
  1366. error = xfs_rmap_delete(cur, RIGHT.rm_startblock,
  1367. RIGHT.rm_blockcount, RIGHT.rm_owner,
  1368. RIGHT.rm_offset, RIGHT.rm_flags);
  1369. if (error)
  1370. goto done;
  1371. error = xfs_rmap_delete(cur, PREV.rm_startblock,
  1372. PREV.rm_blockcount, PREV.rm_owner,
  1373. PREV.rm_offset, PREV.rm_flags);
  1374. if (error)
  1375. goto done;
  1376. NEW = LEFT;
  1377. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1378. NEW.rm_blockcount, NEW.rm_owner,
  1379. NEW.rm_offset, NEW.rm_flags, &i);
  1380. if (error)
  1381. goto done;
  1382. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1383. NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount;
  1384. error = xfs_rmap_update(cur, &NEW);
  1385. if (error)
  1386. goto done;
  1387. break;
  1388. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1389. /*
  1390. * Setting all of a previous oldext extent to newext.
  1391. * The left neighbor is contiguous, the right is not.
  1392. */
  1393. error = xfs_rmap_delete(cur, PREV.rm_startblock,
  1394. PREV.rm_blockcount, PREV.rm_owner,
  1395. PREV.rm_offset, PREV.rm_flags);
  1396. if (error)
  1397. goto done;
  1398. NEW = LEFT;
  1399. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1400. NEW.rm_blockcount, NEW.rm_owner,
  1401. NEW.rm_offset, NEW.rm_flags, &i);
  1402. if (error)
  1403. goto done;
  1404. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1405. NEW.rm_blockcount += PREV.rm_blockcount;
  1406. error = xfs_rmap_update(cur, &NEW);
  1407. if (error)
  1408. goto done;
  1409. break;
  1410. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1411. /*
  1412. * Setting all of a previous oldext extent to newext.
  1413. * The right neighbor is contiguous, the left is not.
  1414. */
  1415. error = xfs_rmap_delete(cur, RIGHT.rm_startblock,
  1416. RIGHT.rm_blockcount, RIGHT.rm_owner,
  1417. RIGHT.rm_offset, RIGHT.rm_flags);
  1418. if (error)
  1419. goto done;
  1420. NEW = PREV;
  1421. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1422. NEW.rm_blockcount, NEW.rm_owner,
  1423. NEW.rm_offset, NEW.rm_flags, &i);
  1424. if (error)
  1425. goto done;
  1426. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1427. NEW.rm_blockcount += RIGHT.rm_blockcount;
  1428. NEW.rm_flags = RIGHT.rm_flags;
  1429. error = xfs_rmap_update(cur, &NEW);
  1430. if (error)
  1431. goto done;
  1432. break;
  1433. case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING:
  1434. /*
  1435. * Setting all of a previous oldext extent to newext.
  1436. * Neither the left nor right neighbors are contiguous with
  1437. * the new one.
  1438. */
  1439. NEW = PREV;
  1440. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1441. NEW.rm_blockcount, NEW.rm_owner,
  1442. NEW.rm_offset, NEW.rm_flags, &i);
  1443. if (error)
  1444. goto done;
  1445. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1446. NEW.rm_flags = newext;
  1447. error = xfs_rmap_update(cur, &NEW);
  1448. if (error)
  1449. goto done;
  1450. break;
  1451. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG:
  1452. /*
  1453. * Setting the first part of a previous oldext extent to newext.
  1454. * The left neighbor is contiguous.
  1455. */
  1456. NEW = PREV;
  1457. error = xfs_rmap_delete(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. NEW.rm_offset += len;
  1463. NEW.rm_startblock += len;
  1464. NEW.rm_blockcount -= len;
  1465. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1466. NEW.rm_blockcount, NEW.rm_owner,
  1467. NEW.rm_offset, NEW.rm_flags);
  1468. if (error)
  1469. goto done;
  1470. NEW = LEFT;
  1471. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1472. NEW.rm_blockcount, NEW.rm_owner,
  1473. NEW.rm_offset, NEW.rm_flags, &i);
  1474. if (error)
  1475. goto done;
  1476. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1477. NEW.rm_blockcount += len;
  1478. error = xfs_rmap_update(cur, &NEW);
  1479. if (error)
  1480. goto done;
  1481. break;
  1482. case RMAP_LEFT_FILLING:
  1483. /*
  1484. * Setting the first part of a previous oldext extent to newext.
  1485. * The left neighbor is not contiguous.
  1486. */
  1487. NEW = PREV;
  1488. error = xfs_rmap_delete(cur, NEW.rm_startblock,
  1489. NEW.rm_blockcount, NEW.rm_owner,
  1490. NEW.rm_offset, NEW.rm_flags);
  1491. if (error)
  1492. goto done;
  1493. NEW.rm_offset += len;
  1494. NEW.rm_startblock += len;
  1495. NEW.rm_blockcount -= len;
  1496. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1497. NEW.rm_blockcount, NEW.rm_owner,
  1498. NEW.rm_offset, NEW.rm_flags);
  1499. if (error)
  1500. goto done;
  1501. error = xfs_rmap_insert(cur, bno, len, owner, offset, newext);
  1502. if (error)
  1503. goto done;
  1504. break;
  1505. case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG:
  1506. /*
  1507. * Setting the last part of a previous oldext extent to newext.
  1508. * The right neighbor is contiguous with the new allocation.
  1509. */
  1510. NEW = PREV;
  1511. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1512. NEW.rm_blockcount, NEW.rm_owner,
  1513. NEW.rm_offset, NEW.rm_flags, &i);
  1514. if (error)
  1515. goto done;
  1516. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1517. NEW.rm_blockcount = offset - NEW.rm_offset;
  1518. error = xfs_rmap_update(cur, &NEW);
  1519. if (error)
  1520. goto done;
  1521. NEW = RIGHT;
  1522. error = xfs_rmap_delete(cur, NEW.rm_startblock,
  1523. NEW.rm_blockcount, NEW.rm_owner,
  1524. NEW.rm_offset, NEW.rm_flags);
  1525. if (error)
  1526. goto done;
  1527. NEW.rm_offset = offset;
  1528. NEW.rm_startblock = bno;
  1529. NEW.rm_blockcount += len;
  1530. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1531. NEW.rm_blockcount, NEW.rm_owner,
  1532. NEW.rm_offset, NEW.rm_flags);
  1533. if (error)
  1534. goto done;
  1535. break;
  1536. case RMAP_RIGHT_FILLING:
  1537. /*
  1538. * Setting the last part of a previous oldext extent to newext.
  1539. * The right neighbor is not contiguous.
  1540. */
  1541. NEW = PREV;
  1542. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1543. NEW.rm_blockcount, NEW.rm_owner,
  1544. NEW.rm_offset, NEW.rm_flags, &i);
  1545. if (error)
  1546. goto done;
  1547. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1548. NEW.rm_blockcount -= len;
  1549. error = xfs_rmap_update(cur, &NEW);
  1550. if (error)
  1551. goto done;
  1552. error = xfs_rmap_insert(cur, bno, len, owner, offset, newext);
  1553. if (error)
  1554. goto done;
  1555. break;
  1556. case 0:
  1557. /*
  1558. * Setting the middle part of a previous oldext extent to
  1559. * newext. Contiguity is impossible here.
  1560. * One extent becomes three extents.
  1561. */
  1562. /* new right extent - oldext */
  1563. NEW.rm_startblock = bno + len;
  1564. NEW.rm_owner = owner;
  1565. NEW.rm_offset = new_endoff;
  1566. NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount -
  1567. new_endoff;
  1568. NEW.rm_flags = PREV.rm_flags;
  1569. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1570. NEW.rm_blockcount, NEW.rm_owner, NEW.rm_offset,
  1571. NEW.rm_flags);
  1572. if (error)
  1573. goto done;
  1574. /* new left extent - oldext */
  1575. NEW = PREV;
  1576. error = xfs_rmap_lookup_eq(cur, NEW.rm_startblock,
  1577. NEW.rm_blockcount, NEW.rm_owner,
  1578. NEW.rm_offset, NEW.rm_flags, &i);
  1579. if (error)
  1580. goto done;
  1581. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1582. NEW.rm_blockcount = offset - NEW.rm_offset;
  1583. error = xfs_rmap_update(cur, &NEW);
  1584. if (error)
  1585. goto done;
  1586. /* new middle extent - newext */
  1587. NEW.rm_startblock = bno;
  1588. NEW.rm_blockcount = len;
  1589. NEW.rm_owner = owner;
  1590. NEW.rm_offset = offset;
  1591. NEW.rm_flags = newext;
  1592. error = xfs_rmap_insert(cur, NEW.rm_startblock,
  1593. NEW.rm_blockcount, NEW.rm_owner, NEW.rm_offset,
  1594. NEW.rm_flags);
  1595. if (error)
  1596. goto done;
  1597. break;
  1598. case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1599. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1600. case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG:
  1601. case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG:
  1602. case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG:
  1603. case RMAP_LEFT_CONTIG:
  1604. case RMAP_RIGHT_CONTIG:
  1605. /*
  1606. * These cases are all impossible.
  1607. */
  1608. ASSERT(0);
  1609. }
  1610. trace_xfs_rmap_convert_done(mp, cur->bc_private.a.agno, bno, len,
  1611. unwritten, oinfo);
  1612. done:
  1613. if (error)
  1614. trace_xfs_rmap_convert_error(cur->bc_mp,
  1615. cur->bc_private.a.agno, error, _RET_IP_);
  1616. return error;
  1617. }
  1618. #undef NEW
  1619. #undef LEFT
  1620. #undef RIGHT
  1621. #undef PREV
  1622. /*
  1623. * Find an extent in the rmap btree and unmap it. For rmap extent types that
  1624. * can overlap (data fork rmaps on reflink filesystems) we must be careful
  1625. * that the prev/next records in the btree might belong to another owner.
  1626. * Therefore we must use delete+insert to alter any of the key fields.
  1627. *
  1628. * For every other situation there can only be one owner for a given extent,
  1629. * so we can call the regular _free function.
  1630. */
  1631. STATIC int
  1632. xfs_rmap_unmap_shared(
  1633. struct xfs_btree_cur *cur,
  1634. xfs_agblock_t bno,
  1635. xfs_extlen_t len,
  1636. bool unwritten,
  1637. struct xfs_owner_info *oinfo)
  1638. {
  1639. struct xfs_mount *mp = cur->bc_mp;
  1640. struct xfs_rmap_irec ltrec;
  1641. uint64_t ltoff;
  1642. int error = 0;
  1643. int i;
  1644. uint64_t owner;
  1645. uint64_t offset;
  1646. unsigned int flags;
  1647. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  1648. if (unwritten)
  1649. flags |= XFS_RMAP_UNWRITTEN;
  1650. trace_xfs_rmap_unmap(mp, cur->bc_private.a.agno, bno, len,
  1651. unwritten, oinfo);
  1652. /*
  1653. * We should always have a left record because there's a static record
  1654. * for the AG headers at rm_startblock == 0 created by mkfs/growfs that
  1655. * will not ever be removed from the tree.
  1656. */
  1657. error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, flags,
  1658. &ltrec, &i);
  1659. if (error)
  1660. goto out_error;
  1661. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1662. ltoff = ltrec.rm_offset;
  1663. /* Make sure the extent we found covers the entire freeing range. */
  1664. XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_startblock <= bno &&
  1665. ltrec.rm_startblock + ltrec.rm_blockcount >=
  1666. bno + len, out_error);
  1667. /* Make sure the owner matches what we expect to find in the tree. */
  1668. XFS_WANT_CORRUPTED_GOTO(mp, owner == ltrec.rm_owner, out_error);
  1669. /* Make sure the unwritten flag matches. */
  1670. XFS_WANT_CORRUPTED_GOTO(mp, (flags & XFS_RMAP_UNWRITTEN) ==
  1671. (ltrec.rm_flags & XFS_RMAP_UNWRITTEN), out_error);
  1672. /* Check the offset. */
  1673. XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_offset <= offset, out_error);
  1674. XFS_WANT_CORRUPTED_GOTO(mp, offset <= ltoff + ltrec.rm_blockcount,
  1675. out_error);
  1676. if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) {
  1677. /* Exact match, simply remove the record from rmap tree. */
  1678. error = xfs_rmap_delete(cur, ltrec.rm_startblock,
  1679. ltrec.rm_blockcount, ltrec.rm_owner,
  1680. ltrec.rm_offset, ltrec.rm_flags);
  1681. if (error)
  1682. goto out_error;
  1683. } else if (ltrec.rm_startblock == bno) {
  1684. /*
  1685. * Overlap left hand side of extent: move the start, trim the
  1686. * length and update the current record.
  1687. *
  1688. * ltbno ltlen
  1689. * Orig: |oooooooooooooooooooo|
  1690. * Freeing: |fffffffff|
  1691. * Result: |rrrrrrrrrr|
  1692. * bno len
  1693. */
  1694. /* Delete prev rmap. */
  1695. error = xfs_rmap_delete(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. /* Add an rmap at the new offset. */
  1701. ltrec.rm_startblock += len;
  1702. ltrec.rm_blockcount -= len;
  1703. ltrec.rm_offset += len;
  1704. error = xfs_rmap_insert(cur, ltrec.rm_startblock,
  1705. ltrec.rm_blockcount, ltrec.rm_owner,
  1706. ltrec.rm_offset, ltrec.rm_flags);
  1707. if (error)
  1708. goto out_error;
  1709. } else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) {
  1710. /*
  1711. * Overlap right hand side of extent: trim the length and
  1712. * update the current record.
  1713. *
  1714. * ltbno ltlen
  1715. * Orig: |oooooooooooooooooooo|
  1716. * Freeing: |fffffffff|
  1717. * Result: |rrrrrrrrrr|
  1718. * bno len
  1719. */
  1720. error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
  1721. ltrec.rm_blockcount, ltrec.rm_owner,
  1722. ltrec.rm_offset, ltrec.rm_flags, &i);
  1723. if (error)
  1724. goto out_error;
  1725. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1726. ltrec.rm_blockcount -= len;
  1727. error = xfs_rmap_update(cur, &ltrec);
  1728. if (error)
  1729. goto out_error;
  1730. } else {
  1731. /*
  1732. * Overlap middle of extent: trim the length of the existing
  1733. * record to the length of the new left-extent size, increment
  1734. * the insertion position so we can insert a new record
  1735. * containing the remaining right-extent space.
  1736. *
  1737. * ltbno ltlen
  1738. * Orig: |oooooooooooooooooooo|
  1739. * Freeing: |fffffffff|
  1740. * Result: |rrrrr| |rrrr|
  1741. * bno len
  1742. */
  1743. xfs_extlen_t orig_len = ltrec.rm_blockcount;
  1744. /* Shrink the left side of the rmap */
  1745. error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
  1746. ltrec.rm_blockcount, ltrec.rm_owner,
  1747. ltrec.rm_offset, ltrec.rm_flags, &i);
  1748. if (error)
  1749. goto out_error;
  1750. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1751. ltrec.rm_blockcount = bno - ltrec.rm_startblock;
  1752. error = xfs_rmap_update(cur, &ltrec);
  1753. if (error)
  1754. goto out_error;
  1755. /* Add an rmap at the new offset */
  1756. error = xfs_rmap_insert(cur, bno + len,
  1757. orig_len - len - ltrec.rm_blockcount,
  1758. ltrec.rm_owner, offset + len,
  1759. ltrec.rm_flags);
  1760. if (error)
  1761. goto out_error;
  1762. }
  1763. trace_xfs_rmap_unmap_done(mp, cur->bc_private.a.agno, bno, len,
  1764. unwritten, oinfo);
  1765. out_error:
  1766. if (error)
  1767. trace_xfs_rmap_unmap_error(cur->bc_mp,
  1768. cur->bc_private.a.agno, error, _RET_IP_);
  1769. return error;
  1770. }
  1771. /*
  1772. * Find an extent in the rmap btree and map it. For rmap extent types that
  1773. * can overlap (data fork rmaps on reflink filesystems) we must be careful
  1774. * that the prev/next records in the btree might belong to another owner.
  1775. * Therefore we must use delete+insert to alter any of the key fields.
  1776. *
  1777. * For every other situation there can only be one owner for a given extent,
  1778. * so we can call the regular _alloc function.
  1779. */
  1780. STATIC int
  1781. xfs_rmap_map_shared(
  1782. struct xfs_btree_cur *cur,
  1783. xfs_agblock_t bno,
  1784. xfs_extlen_t len,
  1785. bool unwritten,
  1786. struct xfs_owner_info *oinfo)
  1787. {
  1788. struct xfs_mount *mp = cur->bc_mp;
  1789. struct xfs_rmap_irec ltrec;
  1790. struct xfs_rmap_irec gtrec;
  1791. int have_gt;
  1792. int have_lt;
  1793. int error = 0;
  1794. int i;
  1795. uint64_t owner;
  1796. uint64_t offset;
  1797. unsigned int flags = 0;
  1798. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  1799. if (unwritten)
  1800. flags |= XFS_RMAP_UNWRITTEN;
  1801. trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len,
  1802. unwritten, oinfo);
  1803. /* Is there a left record that abuts our range? */
  1804. error = xfs_rmap_find_left_neighbor(cur, bno, owner, offset, flags,
  1805. &ltrec, &have_lt);
  1806. if (error)
  1807. goto out_error;
  1808. if (have_lt &&
  1809. !xfs_rmap_is_mergeable(&ltrec, owner, flags))
  1810. have_lt = 0;
  1811. /* Is there a right record that abuts our range? */
  1812. error = xfs_rmap_lookup_eq(cur, bno + len, len, owner, offset + len,
  1813. flags, &have_gt);
  1814. if (error)
  1815. goto out_error;
  1816. if (have_gt) {
  1817. error = xfs_rmap_get_rec(cur, &gtrec, &have_gt);
  1818. if (error)
  1819. goto out_error;
  1820. XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error);
  1821. trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp,
  1822. cur->bc_private.a.agno, gtrec.rm_startblock,
  1823. gtrec.rm_blockcount, gtrec.rm_owner,
  1824. gtrec.rm_offset, gtrec.rm_flags);
  1825. if (!xfs_rmap_is_mergeable(&gtrec, owner, flags))
  1826. have_gt = 0;
  1827. }
  1828. if (have_lt &&
  1829. ltrec.rm_startblock + ltrec.rm_blockcount == bno &&
  1830. ltrec.rm_offset + ltrec.rm_blockcount == offset) {
  1831. /*
  1832. * Left edge contiguous, merge into left record.
  1833. *
  1834. * ltbno ltlen
  1835. * orig: |ooooooooo|
  1836. * adding: |aaaaaaaaa|
  1837. * result: |rrrrrrrrrrrrrrrrrrr|
  1838. * bno len
  1839. */
  1840. ltrec.rm_blockcount += len;
  1841. if (have_gt &&
  1842. bno + len == gtrec.rm_startblock &&
  1843. offset + len == gtrec.rm_offset) {
  1844. /*
  1845. * Right edge also contiguous, delete right record
  1846. * and merge into left record.
  1847. *
  1848. * ltbno ltlen gtbno gtlen
  1849. * orig: |ooooooooo| |ooooooooo|
  1850. * adding: |aaaaaaaaa|
  1851. * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
  1852. */
  1853. ltrec.rm_blockcount += gtrec.rm_blockcount;
  1854. error = xfs_rmap_delete(cur, gtrec.rm_startblock,
  1855. gtrec.rm_blockcount, gtrec.rm_owner,
  1856. gtrec.rm_offset, gtrec.rm_flags);
  1857. if (error)
  1858. goto out_error;
  1859. }
  1860. /* Point the cursor back to the left record and update. */
  1861. error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock,
  1862. ltrec.rm_blockcount, ltrec.rm_owner,
  1863. ltrec.rm_offset, ltrec.rm_flags, &i);
  1864. if (error)
  1865. goto out_error;
  1866. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error);
  1867. error = xfs_rmap_update(cur, &ltrec);
  1868. if (error)
  1869. goto out_error;
  1870. } else if (have_gt &&
  1871. bno + len == gtrec.rm_startblock &&
  1872. offset + len == gtrec.rm_offset) {
  1873. /*
  1874. * Right edge contiguous, merge into right record.
  1875. *
  1876. * gtbno gtlen
  1877. * Orig: |ooooooooo|
  1878. * adding: |aaaaaaaaa|
  1879. * Result: |rrrrrrrrrrrrrrrrrrr|
  1880. * bno len
  1881. */
  1882. /* Delete the old record. */
  1883. error = xfs_rmap_delete(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. /* Move the start and re-add it. */
  1889. gtrec.rm_startblock = bno;
  1890. gtrec.rm_blockcount += len;
  1891. gtrec.rm_offset = offset;
  1892. error = xfs_rmap_insert(cur, gtrec.rm_startblock,
  1893. gtrec.rm_blockcount, gtrec.rm_owner,
  1894. gtrec.rm_offset, gtrec.rm_flags);
  1895. if (error)
  1896. goto out_error;
  1897. } else {
  1898. /*
  1899. * No contiguous edge with identical owner, insert
  1900. * new record at current cursor position.
  1901. */
  1902. error = xfs_rmap_insert(cur, bno, len, owner, offset, flags);
  1903. if (error)
  1904. goto out_error;
  1905. }
  1906. trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len,
  1907. unwritten, oinfo);
  1908. out_error:
  1909. if (error)
  1910. trace_xfs_rmap_map_error(cur->bc_mp,
  1911. cur->bc_private.a.agno, error, _RET_IP_);
  1912. return error;
  1913. }
  1914. /* Insert a raw rmap into the rmapbt. */
  1915. int
  1916. xfs_rmap_map_raw(
  1917. struct xfs_btree_cur *cur,
  1918. struct xfs_rmap_irec *rmap)
  1919. {
  1920. struct xfs_owner_info oinfo;
  1921. oinfo.oi_owner = rmap->rm_owner;
  1922. oinfo.oi_offset = rmap->rm_offset;
  1923. oinfo.oi_flags = 0;
  1924. if (rmap->rm_flags & XFS_RMAP_ATTR_FORK)
  1925. oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
  1926. if (rmap->rm_flags & XFS_RMAP_BMBT_BLOCK)
  1927. oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
  1928. if (rmap->rm_flags || XFS_RMAP_NON_INODE_OWNER(rmap->rm_owner))
  1929. return xfs_rmap_map(cur, rmap->rm_startblock,
  1930. rmap->rm_blockcount,
  1931. rmap->rm_flags & XFS_RMAP_UNWRITTEN,
  1932. &oinfo);
  1933. return xfs_rmap_map_shared(cur, rmap->rm_startblock,
  1934. rmap->rm_blockcount,
  1935. rmap->rm_flags & XFS_RMAP_UNWRITTEN,
  1936. &oinfo);
  1937. }
  1938. struct xfs_rmap_query_range_info {
  1939. xfs_rmap_query_range_fn fn;
  1940. void *priv;
  1941. };
  1942. /* Format btree record and pass to our callback. */
  1943. STATIC int
  1944. xfs_rmap_query_range_helper(
  1945. struct xfs_btree_cur *cur,
  1946. union xfs_btree_rec *rec,
  1947. void *priv)
  1948. {
  1949. struct xfs_rmap_query_range_info *query = priv;
  1950. struct xfs_rmap_irec irec;
  1951. int error;
  1952. error = xfs_rmap_btrec_to_irec(rec, &irec);
  1953. if (error)
  1954. return error;
  1955. return query->fn(cur, &irec, query->priv);
  1956. }
  1957. /* Find all rmaps between two keys. */
  1958. int
  1959. xfs_rmap_query_range(
  1960. struct xfs_btree_cur *cur,
  1961. struct xfs_rmap_irec *low_rec,
  1962. struct xfs_rmap_irec *high_rec,
  1963. xfs_rmap_query_range_fn fn,
  1964. void *priv)
  1965. {
  1966. union xfs_btree_irec low_brec;
  1967. union xfs_btree_irec high_brec;
  1968. struct xfs_rmap_query_range_info query;
  1969. low_brec.r = *low_rec;
  1970. high_brec.r = *high_rec;
  1971. query.priv = priv;
  1972. query.fn = fn;
  1973. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  1974. xfs_rmap_query_range_helper, &query);
  1975. }
  1976. /* Find all rmaps. */
  1977. int
  1978. xfs_rmap_query_all(
  1979. struct xfs_btree_cur *cur,
  1980. xfs_rmap_query_range_fn fn,
  1981. void *priv)
  1982. {
  1983. struct xfs_rmap_query_range_info query;
  1984. query.priv = priv;
  1985. query.fn = fn;
  1986. return xfs_btree_query_all(cur, xfs_rmap_query_range_helper, &query);
  1987. }
  1988. /* Clean up after calling xfs_rmap_finish_one. */
  1989. void
  1990. xfs_rmap_finish_one_cleanup(
  1991. struct xfs_trans *tp,
  1992. struct xfs_btree_cur *rcur,
  1993. int error)
  1994. {
  1995. struct xfs_buf *agbp;
  1996. if (rcur == NULL)
  1997. return;
  1998. agbp = rcur->bc_private.a.agbp;
  1999. xfs_btree_del_cursor(rcur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  2000. if (error)
  2001. xfs_trans_brelse(tp, agbp);
  2002. }
  2003. /*
  2004. * Process one of the deferred rmap operations. We pass back the
  2005. * btree cursor to maintain our lock on the rmapbt between calls.
  2006. * This saves time and eliminates a buffer deadlock between the
  2007. * superblock and the AGF because we'll always grab them in the same
  2008. * order.
  2009. */
  2010. int
  2011. xfs_rmap_finish_one(
  2012. struct xfs_trans *tp,
  2013. enum xfs_rmap_intent_type type,
  2014. uint64_t owner,
  2015. int whichfork,
  2016. xfs_fileoff_t startoff,
  2017. xfs_fsblock_t startblock,
  2018. xfs_filblks_t blockcount,
  2019. xfs_exntst_t state,
  2020. struct xfs_btree_cur **pcur)
  2021. {
  2022. struct xfs_mount *mp = tp->t_mountp;
  2023. struct xfs_btree_cur *rcur;
  2024. struct xfs_buf *agbp = NULL;
  2025. int error = 0;
  2026. xfs_agnumber_t agno;
  2027. struct xfs_owner_info oinfo;
  2028. xfs_agblock_t bno;
  2029. bool unwritten;
  2030. agno = XFS_FSB_TO_AGNO(mp, startblock);
  2031. ASSERT(agno != NULLAGNUMBER);
  2032. bno = XFS_FSB_TO_AGBNO(mp, startblock);
  2033. trace_xfs_rmap_deferred(mp, agno, type, bno, owner, whichfork,
  2034. startoff, blockcount, state);
  2035. if (XFS_TEST_ERROR(false, mp,
  2036. XFS_ERRTAG_RMAP_FINISH_ONE))
  2037. return -EIO;
  2038. /*
  2039. * If we haven't gotten a cursor or the cursor AG doesn't match
  2040. * the startblock, get one now.
  2041. */
  2042. rcur = *pcur;
  2043. if (rcur != NULL && rcur->bc_private.a.agno != agno) {
  2044. xfs_rmap_finish_one_cleanup(tp, rcur, 0);
  2045. rcur = NULL;
  2046. *pcur = NULL;
  2047. }
  2048. if (rcur == NULL) {
  2049. /*
  2050. * Refresh the freelist before we start changing the
  2051. * rmapbt, because a shape change could cause us to
  2052. * allocate blocks.
  2053. */
  2054. error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
  2055. if (error)
  2056. return error;
  2057. if (!agbp)
  2058. return -EFSCORRUPTED;
  2059. rcur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno);
  2060. if (!rcur) {
  2061. error = -ENOMEM;
  2062. goto out_cur;
  2063. }
  2064. }
  2065. *pcur = rcur;
  2066. xfs_rmap_ino_owner(&oinfo, owner, whichfork, startoff);
  2067. unwritten = state == XFS_EXT_UNWRITTEN;
  2068. bno = XFS_FSB_TO_AGBNO(rcur->bc_mp, startblock);
  2069. switch (type) {
  2070. case XFS_RMAP_ALLOC:
  2071. case XFS_RMAP_MAP:
  2072. error = xfs_rmap_map(rcur, bno, blockcount, unwritten, &oinfo);
  2073. break;
  2074. case XFS_RMAP_MAP_SHARED:
  2075. error = xfs_rmap_map_shared(rcur, bno, blockcount, unwritten,
  2076. &oinfo);
  2077. break;
  2078. case XFS_RMAP_FREE:
  2079. case XFS_RMAP_UNMAP:
  2080. error = xfs_rmap_unmap(rcur, bno, blockcount, unwritten,
  2081. &oinfo);
  2082. break;
  2083. case XFS_RMAP_UNMAP_SHARED:
  2084. error = xfs_rmap_unmap_shared(rcur, bno, blockcount, unwritten,
  2085. &oinfo);
  2086. break;
  2087. case XFS_RMAP_CONVERT:
  2088. error = xfs_rmap_convert(rcur, bno, blockcount, !unwritten,
  2089. &oinfo);
  2090. break;
  2091. case XFS_RMAP_CONVERT_SHARED:
  2092. error = xfs_rmap_convert_shared(rcur, bno, blockcount,
  2093. !unwritten, &oinfo);
  2094. break;
  2095. default:
  2096. ASSERT(0);
  2097. error = -EFSCORRUPTED;
  2098. }
  2099. return error;
  2100. out_cur:
  2101. xfs_trans_brelse(tp, agbp);
  2102. return error;
  2103. }
  2104. /*
  2105. * Don't defer an rmap if we aren't an rmap filesystem.
  2106. */
  2107. static bool
  2108. xfs_rmap_update_is_needed(
  2109. struct xfs_mount *mp,
  2110. int whichfork)
  2111. {
  2112. return xfs_sb_version_hasrmapbt(&mp->m_sb) && whichfork != XFS_COW_FORK;
  2113. }
  2114. /*
  2115. * Record a rmap intent; the list is kept sorted first by AG and then by
  2116. * increasing age.
  2117. */
  2118. static int
  2119. __xfs_rmap_add(
  2120. struct xfs_mount *mp,
  2121. struct xfs_defer_ops *dfops,
  2122. enum xfs_rmap_intent_type type,
  2123. uint64_t owner,
  2124. int whichfork,
  2125. struct xfs_bmbt_irec *bmap)
  2126. {
  2127. struct xfs_rmap_intent *ri;
  2128. trace_xfs_rmap_defer(mp, XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
  2129. type,
  2130. XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
  2131. owner, whichfork,
  2132. bmap->br_startoff,
  2133. bmap->br_blockcount,
  2134. bmap->br_state);
  2135. ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_SLEEP | KM_NOFS);
  2136. INIT_LIST_HEAD(&ri->ri_list);
  2137. ri->ri_type = type;
  2138. ri->ri_owner = owner;
  2139. ri->ri_whichfork = whichfork;
  2140. ri->ri_bmap = *bmap;
  2141. xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_RMAP, &ri->ri_list);
  2142. return 0;
  2143. }
  2144. /* Map an extent into a file. */
  2145. int
  2146. xfs_rmap_map_extent(
  2147. struct xfs_mount *mp,
  2148. struct xfs_defer_ops *dfops,
  2149. struct xfs_inode *ip,
  2150. int whichfork,
  2151. struct xfs_bmbt_irec *PREV)
  2152. {
  2153. if (!xfs_rmap_update_is_needed(mp, whichfork))
  2154. return 0;
  2155. return __xfs_rmap_add(mp, dfops, xfs_is_reflink_inode(ip) ?
  2156. XFS_RMAP_MAP_SHARED : XFS_RMAP_MAP, ip->i_ino,
  2157. whichfork, PREV);
  2158. }
  2159. /* Unmap an extent out of a file. */
  2160. int
  2161. xfs_rmap_unmap_extent(
  2162. struct xfs_mount *mp,
  2163. struct xfs_defer_ops *dfops,
  2164. struct xfs_inode *ip,
  2165. int whichfork,
  2166. struct xfs_bmbt_irec *PREV)
  2167. {
  2168. if (!xfs_rmap_update_is_needed(mp, whichfork))
  2169. return 0;
  2170. return __xfs_rmap_add(mp, dfops, xfs_is_reflink_inode(ip) ?
  2171. XFS_RMAP_UNMAP_SHARED : XFS_RMAP_UNMAP, ip->i_ino,
  2172. whichfork, PREV);
  2173. }
  2174. /* Convert a data fork extent from unwritten to real or vice versa. */
  2175. int
  2176. xfs_rmap_convert_extent(
  2177. struct xfs_mount *mp,
  2178. struct xfs_defer_ops *dfops,
  2179. struct xfs_inode *ip,
  2180. int whichfork,
  2181. struct xfs_bmbt_irec *PREV)
  2182. {
  2183. if (!xfs_rmap_update_is_needed(mp, whichfork))
  2184. return 0;
  2185. return __xfs_rmap_add(mp, dfops, xfs_is_reflink_inode(ip) ?
  2186. XFS_RMAP_CONVERT_SHARED : XFS_RMAP_CONVERT, ip->i_ino,
  2187. whichfork, PREV);
  2188. }
  2189. /* Schedule the creation of an rmap for non-file data. */
  2190. int
  2191. xfs_rmap_alloc_extent(
  2192. struct xfs_mount *mp,
  2193. struct xfs_defer_ops *dfops,
  2194. xfs_agnumber_t agno,
  2195. xfs_agblock_t bno,
  2196. xfs_extlen_t len,
  2197. uint64_t owner)
  2198. {
  2199. struct xfs_bmbt_irec bmap;
  2200. if (!xfs_rmap_update_is_needed(mp, XFS_DATA_FORK))
  2201. return 0;
  2202. bmap.br_startblock = XFS_AGB_TO_FSB(mp, agno, bno);
  2203. bmap.br_blockcount = len;
  2204. bmap.br_startoff = 0;
  2205. bmap.br_state = XFS_EXT_NORM;
  2206. return __xfs_rmap_add(mp, dfops, XFS_RMAP_ALLOC, owner,
  2207. XFS_DATA_FORK, &bmap);
  2208. }
  2209. /* Schedule the deletion of an rmap for non-file data. */
  2210. int
  2211. xfs_rmap_free_extent(
  2212. struct xfs_mount *mp,
  2213. struct xfs_defer_ops *dfops,
  2214. xfs_agnumber_t agno,
  2215. xfs_agblock_t bno,
  2216. xfs_extlen_t len,
  2217. uint64_t owner)
  2218. {
  2219. struct xfs_bmbt_irec bmap;
  2220. if (!xfs_rmap_update_is_needed(mp, XFS_DATA_FORK))
  2221. return 0;
  2222. bmap.br_startblock = XFS_AGB_TO_FSB(mp, agno, bno);
  2223. bmap.br_blockcount = len;
  2224. bmap.br_startoff = 0;
  2225. bmap.br_state = XFS_EXT_NORM;
  2226. return __xfs_rmap_add(mp, dfops, XFS_RMAP_FREE, owner,
  2227. XFS_DATA_FORK, &bmap);
  2228. }
  2229. /* Compare rmap records. Returns -1 if a < b, 1 if a > b, and 0 if equal. */
  2230. int
  2231. xfs_rmap_compare(
  2232. const struct xfs_rmap_irec *a,
  2233. const struct xfs_rmap_irec *b)
  2234. {
  2235. __u64 oa;
  2236. __u64 ob;
  2237. oa = xfs_rmap_irec_offset_pack(a);
  2238. ob = xfs_rmap_irec_offset_pack(b);
  2239. if (a->rm_startblock < b->rm_startblock)
  2240. return -1;
  2241. else if (a->rm_startblock > b->rm_startblock)
  2242. return 1;
  2243. else if (a->rm_owner < b->rm_owner)
  2244. return -1;
  2245. else if (a->rm_owner > b->rm_owner)
  2246. return 1;
  2247. else if (oa < ob)
  2248. return -1;
  2249. else if (oa > ob)
  2250. return 1;
  2251. else
  2252. return 0;
  2253. }
  2254. /* Is there a record covering a given extent? */
  2255. int
  2256. xfs_rmap_has_record(
  2257. struct xfs_btree_cur *cur,
  2258. xfs_agblock_t bno,
  2259. xfs_extlen_t len,
  2260. bool *exists)
  2261. {
  2262. union xfs_btree_irec low;
  2263. union xfs_btree_irec high;
  2264. memset(&low, 0, sizeof(low));
  2265. low.r.rm_startblock = bno;
  2266. memset(&high, 0xFF, sizeof(high));
  2267. high.r.rm_startblock = bno + len - 1;
  2268. return xfs_btree_has_record(cur, &low, &high, exists);
  2269. }
  2270. /*
  2271. * Is there a record for this owner completely covering a given physical
  2272. * extent? If so, *has_rmap will be set to true. If there is no record
  2273. * or the record only covers part of the range, we set *has_rmap to false.
  2274. * This function doesn't perform range lookups or offset checks, so it is
  2275. * not suitable for checking data fork blocks.
  2276. */
  2277. int
  2278. xfs_rmap_record_exists(
  2279. struct xfs_btree_cur *cur,
  2280. xfs_agblock_t bno,
  2281. xfs_extlen_t len,
  2282. struct xfs_owner_info *oinfo,
  2283. bool *has_rmap)
  2284. {
  2285. uint64_t owner;
  2286. uint64_t offset;
  2287. unsigned int flags;
  2288. int has_record;
  2289. struct xfs_rmap_irec irec;
  2290. int error;
  2291. xfs_owner_info_unpack(oinfo, &owner, &offset, &flags);
  2292. ASSERT(XFS_RMAP_NON_INODE_OWNER(owner) ||
  2293. (flags & XFS_RMAP_BMBT_BLOCK));
  2294. error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags,
  2295. &has_record);
  2296. if (error)
  2297. return error;
  2298. if (!has_record) {
  2299. *has_rmap = false;
  2300. return 0;
  2301. }
  2302. error = xfs_rmap_get_rec(cur, &irec, &has_record);
  2303. if (error)
  2304. return error;
  2305. if (!has_record) {
  2306. *has_rmap = false;
  2307. return 0;
  2308. }
  2309. *has_rmap = (irec.rm_owner == owner && irec.rm_startblock <= bno &&
  2310. irec.rm_startblock + irec.rm_blockcount >= bno + len);
  2311. return 0;
  2312. }
  2313. struct xfs_rmap_key_state {
  2314. uint64_t owner;
  2315. uint64_t offset;
  2316. unsigned int flags;
  2317. bool has_rmap;
  2318. };
  2319. /* For each rmap given, figure out if it doesn't match the key we want. */
  2320. STATIC int
  2321. xfs_rmap_has_other_keys_helper(
  2322. struct xfs_btree_cur *cur,
  2323. struct xfs_rmap_irec *rec,
  2324. void *priv)
  2325. {
  2326. struct xfs_rmap_key_state *rks = priv;
  2327. if (rks->owner == rec->rm_owner && rks->offset == rec->rm_offset &&
  2328. ((rks->flags & rec->rm_flags) & XFS_RMAP_KEY_FLAGS) == rks->flags)
  2329. return 0;
  2330. rks->has_rmap = true;
  2331. return XFS_BTREE_QUERY_RANGE_ABORT;
  2332. }
  2333. /*
  2334. * Given an extent and some owner info, can we find records overlapping
  2335. * the extent whose owner info does not match the given owner?
  2336. */
  2337. int
  2338. xfs_rmap_has_other_keys(
  2339. struct xfs_btree_cur *cur,
  2340. xfs_agblock_t bno,
  2341. xfs_extlen_t len,
  2342. struct xfs_owner_info *oinfo,
  2343. bool *has_rmap)
  2344. {
  2345. struct xfs_rmap_irec low = {0};
  2346. struct xfs_rmap_irec high;
  2347. struct xfs_rmap_key_state rks;
  2348. int error;
  2349. xfs_owner_info_unpack(oinfo, &rks.owner, &rks.offset, &rks.flags);
  2350. rks.has_rmap = false;
  2351. low.rm_startblock = bno;
  2352. memset(&high, 0xFF, sizeof(high));
  2353. high.rm_startblock = bno + len - 1;
  2354. error = xfs_rmap_query_range(cur, &low, &high,
  2355. xfs_rmap_has_other_keys_helper, &rks);
  2356. *has_rmap = rks.has_rmap;
  2357. return error;
  2358. }