xfs_alloc.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, 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_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_btree.h"
  34. #include "xfs_alloc.h"
  35. #include "xfs_error.h"
  36. #include "xfs_trace.h"
  37. #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
  38. #define XFSA_FIXUP_BNO_OK 1
  39. #define XFSA_FIXUP_CNT_OK 2
  40. static int
  41. xfs_alloc_busy_search(struct xfs_mount *mp, xfs_agnumber_t agno,
  42. xfs_agblock_t bno, xfs_extlen_t len);
  43. /*
  44. * Prototypes for per-ag allocation routines
  45. */
  46. STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
  47. STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
  48. STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
  49. STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
  50. xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
  51. /*
  52. * Internal functions.
  53. */
  54. /*
  55. * Lookup the record equal to [bno, len] in the btree given by cur.
  56. */
  57. STATIC int /* error */
  58. xfs_alloc_lookup_eq(
  59. struct xfs_btree_cur *cur, /* btree cursor */
  60. xfs_agblock_t bno, /* starting block of extent */
  61. xfs_extlen_t len, /* length of extent */
  62. int *stat) /* success/failure */
  63. {
  64. cur->bc_rec.a.ar_startblock = bno;
  65. cur->bc_rec.a.ar_blockcount = len;
  66. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  67. }
  68. /*
  69. * Lookup the first record greater than or equal to [bno, len]
  70. * in the btree given by cur.
  71. */
  72. STATIC int /* error */
  73. xfs_alloc_lookup_ge(
  74. struct xfs_btree_cur *cur, /* btree cursor */
  75. xfs_agblock_t bno, /* starting block of extent */
  76. xfs_extlen_t len, /* length of extent */
  77. int *stat) /* success/failure */
  78. {
  79. cur->bc_rec.a.ar_startblock = bno;
  80. cur->bc_rec.a.ar_blockcount = len;
  81. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  82. }
  83. /*
  84. * Lookup the first record less than or equal to [bno, len]
  85. * in the btree given by cur.
  86. */
  87. STATIC int /* error */
  88. xfs_alloc_lookup_le(
  89. struct xfs_btree_cur *cur, /* btree cursor */
  90. xfs_agblock_t bno, /* starting block of extent */
  91. xfs_extlen_t len, /* length of extent */
  92. int *stat) /* success/failure */
  93. {
  94. cur->bc_rec.a.ar_startblock = bno;
  95. cur->bc_rec.a.ar_blockcount = len;
  96. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  97. }
  98. /*
  99. * Update the record referred to by cur to the value given
  100. * by [bno, len].
  101. * This either works (return 0) or gets an EFSCORRUPTED error.
  102. */
  103. STATIC int /* error */
  104. xfs_alloc_update(
  105. struct xfs_btree_cur *cur, /* btree cursor */
  106. xfs_agblock_t bno, /* starting block of extent */
  107. xfs_extlen_t len) /* length of extent */
  108. {
  109. union xfs_btree_rec rec;
  110. rec.alloc.ar_startblock = cpu_to_be32(bno);
  111. rec.alloc.ar_blockcount = cpu_to_be32(len);
  112. return xfs_btree_update(cur, &rec);
  113. }
  114. /*
  115. * Get the data from the pointed-to record.
  116. */
  117. STATIC int /* error */
  118. xfs_alloc_get_rec(
  119. struct xfs_btree_cur *cur, /* btree cursor */
  120. xfs_agblock_t *bno, /* output: starting block of extent */
  121. xfs_extlen_t *len, /* output: length of extent */
  122. int *stat) /* output: success/failure */
  123. {
  124. union xfs_btree_rec *rec;
  125. int error;
  126. error = xfs_btree_get_rec(cur, &rec, stat);
  127. if (!error && *stat == 1) {
  128. *bno = be32_to_cpu(rec->alloc.ar_startblock);
  129. *len = be32_to_cpu(rec->alloc.ar_blockcount);
  130. }
  131. return error;
  132. }
  133. /*
  134. * Compute aligned version of the found extent.
  135. * Takes alignment and min length into account.
  136. */
  137. STATIC void
  138. xfs_alloc_compute_aligned(
  139. xfs_agblock_t foundbno, /* starting block in found extent */
  140. xfs_extlen_t foundlen, /* length in found extent */
  141. xfs_extlen_t alignment, /* alignment for allocation */
  142. xfs_extlen_t minlen, /* minimum length for allocation */
  143. xfs_agblock_t *resbno, /* result block number */
  144. xfs_extlen_t *reslen) /* result length */
  145. {
  146. xfs_agblock_t bno;
  147. xfs_extlen_t diff;
  148. xfs_extlen_t len;
  149. if (alignment > 1 && foundlen >= minlen) {
  150. bno = roundup(foundbno, alignment);
  151. diff = bno - foundbno;
  152. len = diff >= foundlen ? 0 : foundlen - diff;
  153. } else {
  154. bno = foundbno;
  155. len = foundlen;
  156. }
  157. *resbno = bno;
  158. *reslen = len;
  159. }
  160. /*
  161. * Compute best start block and diff for "near" allocations.
  162. * freelen >= wantlen already checked by caller.
  163. */
  164. STATIC xfs_extlen_t /* difference value (absolute) */
  165. xfs_alloc_compute_diff(
  166. xfs_agblock_t wantbno, /* target starting block */
  167. xfs_extlen_t wantlen, /* target length */
  168. xfs_extlen_t alignment, /* target alignment */
  169. xfs_agblock_t freebno, /* freespace's starting block */
  170. xfs_extlen_t freelen, /* freespace's length */
  171. xfs_agblock_t *newbnop) /* result: best start block from free */
  172. {
  173. xfs_agblock_t freeend; /* end of freespace extent */
  174. xfs_agblock_t newbno1; /* return block number */
  175. xfs_agblock_t newbno2; /* other new block number */
  176. xfs_extlen_t newlen1=0; /* length with newbno1 */
  177. xfs_extlen_t newlen2=0; /* length with newbno2 */
  178. xfs_agblock_t wantend; /* end of target extent */
  179. ASSERT(freelen >= wantlen);
  180. freeend = freebno + freelen;
  181. wantend = wantbno + wantlen;
  182. if (freebno >= wantbno) {
  183. if ((newbno1 = roundup(freebno, alignment)) >= freeend)
  184. newbno1 = NULLAGBLOCK;
  185. } else if (freeend >= wantend && alignment > 1) {
  186. newbno1 = roundup(wantbno, alignment);
  187. newbno2 = newbno1 - alignment;
  188. if (newbno1 >= freeend)
  189. newbno1 = NULLAGBLOCK;
  190. else
  191. newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
  192. if (newbno2 < freebno)
  193. newbno2 = NULLAGBLOCK;
  194. else
  195. newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
  196. if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
  197. if (newlen1 < newlen2 ||
  198. (newlen1 == newlen2 &&
  199. XFS_ABSDIFF(newbno1, wantbno) >
  200. XFS_ABSDIFF(newbno2, wantbno)))
  201. newbno1 = newbno2;
  202. } else if (newbno2 != NULLAGBLOCK)
  203. newbno1 = newbno2;
  204. } else if (freeend >= wantend) {
  205. newbno1 = wantbno;
  206. } else if (alignment > 1) {
  207. newbno1 = roundup(freeend - wantlen, alignment);
  208. if (newbno1 > freeend - wantlen &&
  209. newbno1 - alignment >= freebno)
  210. newbno1 -= alignment;
  211. else if (newbno1 >= freeend)
  212. newbno1 = NULLAGBLOCK;
  213. } else
  214. newbno1 = freeend - wantlen;
  215. *newbnop = newbno1;
  216. return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
  217. }
  218. /*
  219. * Fix up the length, based on mod and prod.
  220. * len should be k * prod + mod for some k.
  221. * If len is too small it is returned unchanged.
  222. * If len hits maxlen it is left alone.
  223. */
  224. STATIC void
  225. xfs_alloc_fix_len(
  226. xfs_alloc_arg_t *args) /* allocation argument structure */
  227. {
  228. xfs_extlen_t k;
  229. xfs_extlen_t rlen;
  230. ASSERT(args->mod < args->prod);
  231. rlen = args->len;
  232. ASSERT(rlen >= args->minlen);
  233. ASSERT(rlen <= args->maxlen);
  234. if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
  235. (args->mod == 0 && rlen < args->prod))
  236. return;
  237. k = rlen % args->prod;
  238. if (k == args->mod)
  239. return;
  240. if (k > args->mod) {
  241. if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
  242. return;
  243. } else {
  244. if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
  245. (int)args->minlen)
  246. return;
  247. }
  248. ASSERT(rlen >= args->minlen);
  249. ASSERT(rlen <= args->maxlen);
  250. args->len = rlen;
  251. }
  252. /*
  253. * Fix up length if there is too little space left in the a.g.
  254. * Return 1 if ok, 0 if too little, should give up.
  255. */
  256. STATIC int
  257. xfs_alloc_fix_minleft(
  258. xfs_alloc_arg_t *args) /* allocation argument structure */
  259. {
  260. xfs_agf_t *agf; /* a.g. freelist header */
  261. int diff; /* free space difference */
  262. if (args->minleft == 0)
  263. return 1;
  264. agf = XFS_BUF_TO_AGF(args->agbp);
  265. diff = be32_to_cpu(agf->agf_freeblks)
  266. + be32_to_cpu(agf->agf_flcount)
  267. - args->len - args->minleft;
  268. if (diff >= 0)
  269. return 1;
  270. args->len += diff; /* shrink the allocated space */
  271. if (args->len >= args->minlen)
  272. return 1;
  273. args->agbno = NULLAGBLOCK;
  274. return 0;
  275. }
  276. /*
  277. * Update the two btrees, logically removing from freespace the extent
  278. * starting at rbno, rlen blocks. The extent is contained within the
  279. * actual (current) free extent fbno for flen blocks.
  280. * Flags are passed in indicating whether the cursors are set to the
  281. * relevant records.
  282. */
  283. STATIC int /* error code */
  284. xfs_alloc_fixup_trees(
  285. xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
  286. xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
  287. xfs_agblock_t fbno, /* starting block of free extent */
  288. xfs_extlen_t flen, /* length of free extent */
  289. xfs_agblock_t rbno, /* starting block of returned extent */
  290. xfs_extlen_t rlen, /* length of returned extent */
  291. int flags) /* flags, XFSA_FIXUP_... */
  292. {
  293. int error; /* error code */
  294. int i; /* operation results */
  295. xfs_agblock_t nfbno1; /* first new free startblock */
  296. xfs_agblock_t nfbno2; /* second new free startblock */
  297. xfs_extlen_t nflen1=0; /* first new free length */
  298. xfs_extlen_t nflen2=0; /* second new free length */
  299. /*
  300. * Look up the record in the by-size tree if necessary.
  301. */
  302. if (flags & XFSA_FIXUP_CNT_OK) {
  303. #ifdef DEBUG
  304. if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
  305. return error;
  306. XFS_WANT_CORRUPTED_RETURN(
  307. i == 1 && nfbno1 == fbno && nflen1 == flen);
  308. #endif
  309. } else {
  310. if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
  311. return error;
  312. XFS_WANT_CORRUPTED_RETURN(i == 1);
  313. }
  314. /*
  315. * Look up the record in the by-block tree if necessary.
  316. */
  317. if (flags & XFSA_FIXUP_BNO_OK) {
  318. #ifdef DEBUG
  319. if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
  320. return error;
  321. XFS_WANT_CORRUPTED_RETURN(
  322. i == 1 && nfbno1 == fbno && nflen1 == flen);
  323. #endif
  324. } else {
  325. if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
  326. return error;
  327. XFS_WANT_CORRUPTED_RETURN(i == 1);
  328. }
  329. #ifdef DEBUG
  330. if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
  331. struct xfs_btree_block *bnoblock;
  332. struct xfs_btree_block *cntblock;
  333. bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
  334. cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
  335. XFS_WANT_CORRUPTED_RETURN(
  336. bnoblock->bb_numrecs == cntblock->bb_numrecs);
  337. }
  338. #endif
  339. /*
  340. * Deal with all four cases: the allocated record is contained
  341. * within the freespace record, so we can have new freespace
  342. * at either (or both) end, or no freespace remaining.
  343. */
  344. if (rbno == fbno && rlen == flen)
  345. nfbno1 = nfbno2 = NULLAGBLOCK;
  346. else if (rbno == fbno) {
  347. nfbno1 = rbno + rlen;
  348. nflen1 = flen - rlen;
  349. nfbno2 = NULLAGBLOCK;
  350. } else if (rbno + rlen == fbno + flen) {
  351. nfbno1 = fbno;
  352. nflen1 = flen - rlen;
  353. nfbno2 = NULLAGBLOCK;
  354. } else {
  355. nfbno1 = fbno;
  356. nflen1 = rbno - fbno;
  357. nfbno2 = rbno + rlen;
  358. nflen2 = (fbno + flen) - nfbno2;
  359. }
  360. /*
  361. * Delete the entry from the by-size btree.
  362. */
  363. if ((error = xfs_btree_delete(cnt_cur, &i)))
  364. return error;
  365. XFS_WANT_CORRUPTED_RETURN(i == 1);
  366. /*
  367. * Add new by-size btree entry(s).
  368. */
  369. if (nfbno1 != NULLAGBLOCK) {
  370. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
  371. return error;
  372. XFS_WANT_CORRUPTED_RETURN(i == 0);
  373. if ((error = xfs_btree_insert(cnt_cur, &i)))
  374. return error;
  375. XFS_WANT_CORRUPTED_RETURN(i == 1);
  376. }
  377. if (nfbno2 != NULLAGBLOCK) {
  378. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
  379. return error;
  380. XFS_WANT_CORRUPTED_RETURN(i == 0);
  381. if ((error = xfs_btree_insert(cnt_cur, &i)))
  382. return error;
  383. XFS_WANT_CORRUPTED_RETURN(i == 1);
  384. }
  385. /*
  386. * Fix up the by-block btree entry(s).
  387. */
  388. if (nfbno1 == NULLAGBLOCK) {
  389. /*
  390. * No remaining freespace, just delete the by-block tree entry.
  391. */
  392. if ((error = xfs_btree_delete(bno_cur, &i)))
  393. return error;
  394. XFS_WANT_CORRUPTED_RETURN(i == 1);
  395. } else {
  396. /*
  397. * Update the by-block entry to start later|be shorter.
  398. */
  399. if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
  400. return error;
  401. }
  402. if (nfbno2 != NULLAGBLOCK) {
  403. /*
  404. * 2 resulting free entries, need to add one.
  405. */
  406. if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
  407. return error;
  408. XFS_WANT_CORRUPTED_RETURN(i == 0);
  409. if ((error = xfs_btree_insert(bno_cur, &i)))
  410. return error;
  411. XFS_WANT_CORRUPTED_RETURN(i == 1);
  412. }
  413. return 0;
  414. }
  415. /*
  416. * Read in the allocation group free block array.
  417. */
  418. STATIC int /* error */
  419. xfs_alloc_read_agfl(
  420. xfs_mount_t *mp, /* mount point structure */
  421. xfs_trans_t *tp, /* transaction pointer */
  422. xfs_agnumber_t agno, /* allocation group number */
  423. xfs_buf_t **bpp) /* buffer for the ag free block array */
  424. {
  425. xfs_buf_t *bp; /* return value */
  426. int error;
  427. ASSERT(agno != NULLAGNUMBER);
  428. error = xfs_trans_read_buf(
  429. mp, tp, mp->m_ddev_targp,
  430. XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
  431. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  432. if (error)
  433. return error;
  434. ASSERT(bp);
  435. ASSERT(!XFS_BUF_GETERROR(bp));
  436. XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGFL, XFS_AGFL_REF);
  437. *bpp = bp;
  438. return 0;
  439. }
  440. /*
  441. * Allocation group level functions.
  442. */
  443. /*
  444. * Allocate a variable extent in the allocation group agno.
  445. * Type and bno are used to determine where in the allocation group the
  446. * extent will start.
  447. * Extent's length (returned in *len) will be between minlen and maxlen,
  448. * and of the form k * prod + mod unless there's nothing that large.
  449. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  450. */
  451. STATIC int /* error */
  452. xfs_alloc_ag_vextent(
  453. xfs_alloc_arg_t *args) /* argument structure for allocation */
  454. {
  455. int error=0;
  456. ASSERT(args->minlen > 0);
  457. ASSERT(args->maxlen > 0);
  458. ASSERT(args->minlen <= args->maxlen);
  459. ASSERT(args->mod < args->prod);
  460. ASSERT(args->alignment > 0);
  461. /*
  462. * Branch to correct routine based on the type.
  463. */
  464. args->wasfromfl = 0;
  465. switch (args->type) {
  466. case XFS_ALLOCTYPE_THIS_AG:
  467. error = xfs_alloc_ag_vextent_size(args);
  468. break;
  469. case XFS_ALLOCTYPE_NEAR_BNO:
  470. error = xfs_alloc_ag_vextent_near(args);
  471. break;
  472. case XFS_ALLOCTYPE_THIS_BNO:
  473. error = xfs_alloc_ag_vextent_exact(args);
  474. break;
  475. default:
  476. ASSERT(0);
  477. /* NOTREACHED */
  478. }
  479. if (error)
  480. return error;
  481. /*
  482. * If the allocation worked, need to change the agf structure
  483. * (and log it), and the superblock.
  484. */
  485. if (args->agbno != NULLAGBLOCK) {
  486. xfs_agf_t *agf; /* allocation group freelist header */
  487. long slen = (long)args->len;
  488. ASSERT(args->len >= args->minlen && args->len <= args->maxlen);
  489. ASSERT(!(args->wasfromfl) || !args->isfl);
  490. ASSERT(args->agbno % args->alignment == 0);
  491. if (!(args->wasfromfl)) {
  492. agf = XFS_BUF_TO_AGF(args->agbp);
  493. be32_add_cpu(&agf->agf_freeblks, -(args->len));
  494. xfs_trans_agblocks_delta(args->tp,
  495. -((long)(args->len)));
  496. args->pag->pagf_freeblks -= args->len;
  497. ASSERT(be32_to_cpu(agf->agf_freeblks) <=
  498. be32_to_cpu(agf->agf_length));
  499. xfs_alloc_log_agf(args->tp, args->agbp,
  500. XFS_AGF_FREEBLKS);
  501. /*
  502. * Search the busylist for these blocks and mark the
  503. * transaction as synchronous if blocks are found. This
  504. * avoids the need to block due to a synchronous log
  505. * force to ensure correct ordering as the synchronous
  506. * transaction will guarantee that for us.
  507. */
  508. if (xfs_alloc_busy_search(args->mp, args->agno,
  509. args->agbno, args->len))
  510. xfs_trans_set_sync(args->tp);
  511. }
  512. if (!args->isfl)
  513. xfs_trans_mod_sb(args->tp,
  514. args->wasdel ? XFS_TRANS_SB_RES_FDBLOCKS :
  515. XFS_TRANS_SB_FDBLOCKS, -slen);
  516. XFS_STATS_INC(xs_allocx);
  517. XFS_STATS_ADD(xs_allocb, args->len);
  518. }
  519. return 0;
  520. }
  521. /*
  522. * Allocate a variable extent at exactly agno/bno.
  523. * Extent's length (returned in *len) will be between minlen and maxlen,
  524. * and of the form k * prod + mod unless there's nothing that large.
  525. * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
  526. */
  527. STATIC int /* error */
  528. xfs_alloc_ag_vextent_exact(
  529. xfs_alloc_arg_t *args) /* allocation argument structure */
  530. {
  531. xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
  532. xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
  533. xfs_agblock_t end; /* end of allocated extent */
  534. int error;
  535. xfs_agblock_t fbno; /* start block of found extent */
  536. xfs_agblock_t fend; /* end block of found extent */
  537. xfs_extlen_t flen; /* length of found extent */
  538. int i; /* success/failure of operation */
  539. xfs_agblock_t maxend; /* end of maximal extent */
  540. xfs_agblock_t minend; /* end of minimal extent */
  541. xfs_extlen_t rlen; /* length of returned extent */
  542. ASSERT(args->alignment == 1);
  543. /*
  544. * Allocate/initialize a cursor for the by-number freespace btree.
  545. */
  546. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  547. args->agno, XFS_BTNUM_BNO);
  548. /*
  549. * Lookup bno and minlen in the btree (minlen is irrelevant, really).
  550. * Look for the closest free block <= bno, it must contain bno
  551. * if any free block does.
  552. */
  553. error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
  554. if (error)
  555. goto error0;
  556. if (!i)
  557. goto not_found;
  558. /*
  559. * Grab the freespace record.
  560. */
  561. error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
  562. if (error)
  563. goto error0;
  564. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  565. ASSERT(fbno <= args->agbno);
  566. minend = args->agbno + args->minlen;
  567. maxend = args->agbno + args->maxlen;
  568. fend = fbno + flen;
  569. /*
  570. * Give up if the freespace isn't long enough for the minimum request.
  571. */
  572. if (fend < minend)
  573. goto not_found;
  574. /*
  575. * End of extent will be smaller of the freespace end and the
  576. * maximal requested end.
  577. *
  578. * Fix the length according to mod and prod if given.
  579. */
  580. end = XFS_AGBLOCK_MIN(fend, maxend);
  581. args->len = end - args->agbno;
  582. xfs_alloc_fix_len(args);
  583. if (!xfs_alloc_fix_minleft(args))
  584. goto not_found;
  585. rlen = args->len;
  586. ASSERT(args->agbno + rlen <= fend);
  587. end = args->agbno + rlen;
  588. /*
  589. * We are allocating agbno for rlen [agbno .. end]
  590. * Allocate/initialize a cursor for the by-size btree.
  591. */
  592. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  593. args->agno, XFS_BTNUM_CNT);
  594. ASSERT(args->agbno + args->len <=
  595. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  596. error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
  597. args->len, XFSA_FIXUP_BNO_OK);
  598. if (error) {
  599. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  600. goto error0;
  601. }
  602. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  603. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  604. args->wasfromfl = 0;
  605. trace_xfs_alloc_exact_done(args);
  606. return 0;
  607. not_found:
  608. /* Didn't find it, return null. */
  609. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  610. args->agbno = NULLAGBLOCK;
  611. trace_xfs_alloc_exact_notfound(args);
  612. return 0;
  613. error0:
  614. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  615. trace_xfs_alloc_exact_error(args);
  616. return error;
  617. }
  618. /*
  619. * Allocate a variable extent near bno in the allocation group agno.
  620. * Extent's length (returned in len) will be between minlen and maxlen,
  621. * and of the form k * prod + mod unless there's nothing that large.
  622. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  623. */
  624. STATIC int /* error */
  625. xfs_alloc_ag_vextent_near(
  626. xfs_alloc_arg_t *args) /* allocation argument structure */
  627. {
  628. xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
  629. xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
  630. xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
  631. xfs_agblock_t gtbno; /* start bno of right side entry */
  632. xfs_agblock_t gtbnoa; /* aligned ... */
  633. xfs_extlen_t gtdiff; /* difference to right side entry */
  634. xfs_extlen_t gtlen; /* length of right side entry */
  635. xfs_extlen_t gtlena = 0; /* aligned ... */
  636. xfs_agblock_t gtnew; /* useful start bno of right side */
  637. int error; /* error code */
  638. int i; /* result code, temporary */
  639. int j; /* result code, temporary */
  640. xfs_agblock_t ltbno; /* start bno of left side entry */
  641. xfs_agblock_t ltbnoa; /* aligned ... */
  642. xfs_extlen_t ltdiff; /* difference to left side entry */
  643. xfs_extlen_t ltlen; /* length of left side entry */
  644. xfs_extlen_t ltlena = 0; /* aligned ... */
  645. xfs_agblock_t ltnew; /* useful start bno of left side */
  646. xfs_extlen_t rlen; /* length of returned extent */
  647. #if defined(DEBUG) && defined(__KERNEL__)
  648. /*
  649. * Randomly don't execute the first algorithm.
  650. */
  651. int dofirst; /* set to do first algorithm */
  652. dofirst = random32() & 1;
  653. #endif
  654. /*
  655. * Get a cursor for the by-size btree.
  656. */
  657. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  658. args->agno, XFS_BTNUM_CNT);
  659. ltlen = 0;
  660. bno_cur_lt = bno_cur_gt = NULL;
  661. /*
  662. * See if there are any free extents as big as maxlen.
  663. */
  664. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
  665. goto error0;
  666. /*
  667. * If none, then pick up the last entry in the tree unless the
  668. * tree is empty.
  669. */
  670. if (!i) {
  671. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
  672. &ltlen, &i)))
  673. goto error0;
  674. if (i == 0 || ltlen == 0) {
  675. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  676. return 0;
  677. }
  678. ASSERT(i == 1);
  679. }
  680. args->wasfromfl = 0;
  681. /*
  682. * First algorithm.
  683. * If the requested extent is large wrt the freespaces available
  684. * in this a.g., then the cursor will be pointing to a btree entry
  685. * near the right edge of the tree. If it's in the last btree leaf
  686. * block, then we just examine all the entries in that block
  687. * that are big enough, and pick the best one.
  688. * This is written as a while loop so we can break out of it,
  689. * but we never loop back to the top.
  690. */
  691. while (xfs_btree_islastblock(cnt_cur, 0)) {
  692. xfs_extlen_t bdiff;
  693. int besti=0;
  694. xfs_extlen_t blen=0;
  695. xfs_agblock_t bnew=0;
  696. #if defined(DEBUG) && defined(__KERNEL__)
  697. if (!dofirst)
  698. break;
  699. #endif
  700. /*
  701. * Start from the entry that lookup found, sequence through
  702. * all larger free blocks. If we're actually pointing at a
  703. * record smaller than maxlen, go to the start of this block,
  704. * and skip all those smaller than minlen.
  705. */
  706. if (ltlen || args->alignment > 1) {
  707. cnt_cur->bc_ptrs[0] = 1;
  708. do {
  709. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
  710. &ltlen, &i)))
  711. goto error0;
  712. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  713. if (ltlen >= args->minlen)
  714. break;
  715. if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
  716. goto error0;
  717. } while (i);
  718. ASSERT(ltlen >= args->minlen);
  719. if (!i)
  720. break;
  721. }
  722. i = cnt_cur->bc_ptrs[0];
  723. for (j = 1, blen = 0, bdiff = 0;
  724. !error && j && (blen < args->maxlen || bdiff > 0);
  725. error = xfs_btree_increment(cnt_cur, 0, &j)) {
  726. /*
  727. * For each entry, decide if it's better than
  728. * the previous best entry.
  729. */
  730. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  731. goto error0;
  732. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  733. xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
  734. args->minlen, &ltbnoa, &ltlena);
  735. if (ltlena < args->minlen)
  736. continue;
  737. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  738. xfs_alloc_fix_len(args);
  739. ASSERT(args->len >= args->minlen);
  740. if (args->len < blen)
  741. continue;
  742. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  743. args->alignment, ltbno, ltlen, &ltnew);
  744. if (ltnew != NULLAGBLOCK &&
  745. (args->len > blen || ltdiff < bdiff)) {
  746. bdiff = ltdiff;
  747. bnew = ltnew;
  748. blen = args->len;
  749. besti = cnt_cur->bc_ptrs[0];
  750. }
  751. }
  752. /*
  753. * It didn't work. We COULD be in a case where
  754. * there's a good record somewhere, so try again.
  755. */
  756. if (blen == 0)
  757. break;
  758. /*
  759. * Point at the best entry, and retrieve it again.
  760. */
  761. cnt_cur->bc_ptrs[0] = besti;
  762. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  763. goto error0;
  764. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  765. ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  766. args->len = blen;
  767. if (!xfs_alloc_fix_minleft(args)) {
  768. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  769. trace_xfs_alloc_near_nominleft(args);
  770. return 0;
  771. }
  772. blen = args->len;
  773. /*
  774. * We are allocating starting at bnew for blen blocks.
  775. */
  776. args->agbno = bnew;
  777. ASSERT(bnew >= ltbno);
  778. ASSERT(bnew + blen <= ltbno + ltlen);
  779. /*
  780. * Set up a cursor for the by-bno tree.
  781. */
  782. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
  783. args->agbp, args->agno, XFS_BTNUM_BNO);
  784. /*
  785. * Fix up the btree entries.
  786. */
  787. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
  788. ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
  789. goto error0;
  790. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  791. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  792. trace_xfs_alloc_near_first(args);
  793. return 0;
  794. }
  795. /*
  796. * Second algorithm.
  797. * Search in the by-bno tree to the left and to the right
  798. * simultaneously, until in each case we find a space big enough,
  799. * or run into the edge of the tree. When we run into the edge,
  800. * we deallocate that cursor.
  801. * If both searches succeed, we compare the two spaces and pick
  802. * the better one.
  803. * With alignment, it's possible for both to fail; the upper
  804. * level algorithm that picks allocation groups for allocations
  805. * is not supposed to do this.
  806. */
  807. /*
  808. * Allocate and initialize the cursor for the leftward search.
  809. */
  810. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  811. args->agno, XFS_BTNUM_BNO);
  812. /*
  813. * Lookup <= bno to find the leftward search's starting point.
  814. */
  815. if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
  816. goto error0;
  817. if (!i) {
  818. /*
  819. * Didn't find anything; use this cursor for the rightward
  820. * search.
  821. */
  822. bno_cur_gt = bno_cur_lt;
  823. bno_cur_lt = NULL;
  824. }
  825. /*
  826. * Found something. Duplicate the cursor for the rightward search.
  827. */
  828. else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
  829. goto error0;
  830. /*
  831. * Increment the cursor, so we will point at the entry just right
  832. * of the leftward entry if any, or to the leftmost entry.
  833. */
  834. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  835. goto error0;
  836. if (!i) {
  837. /*
  838. * It failed, there are no rightward entries.
  839. */
  840. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
  841. bno_cur_gt = NULL;
  842. }
  843. /*
  844. * Loop going left with the leftward cursor, right with the
  845. * rightward cursor, until either both directions give up or
  846. * we find an entry at least as big as minlen.
  847. */
  848. do {
  849. if (bno_cur_lt) {
  850. if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
  851. goto error0;
  852. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  853. xfs_alloc_compute_aligned(ltbno, ltlen, args->alignment,
  854. args->minlen, &ltbnoa, &ltlena);
  855. if (ltlena >= args->minlen)
  856. break;
  857. if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
  858. goto error0;
  859. if (!i) {
  860. xfs_btree_del_cursor(bno_cur_lt,
  861. XFS_BTREE_NOERROR);
  862. bno_cur_lt = NULL;
  863. }
  864. }
  865. if (bno_cur_gt) {
  866. if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
  867. goto error0;
  868. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  869. xfs_alloc_compute_aligned(gtbno, gtlen, args->alignment,
  870. args->minlen, &gtbnoa, &gtlena);
  871. if (gtlena >= args->minlen)
  872. break;
  873. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  874. goto error0;
  875. if (!i) {
  876. xfs_btree_del_cursor(bno_cur_gt,
  877. XFS_BTREE_NOERROR);
  878. bno_cur_gt = NULL;
  879. }
  880. }
  881. } while (bno_cur_lt || bno_cur_gt);
  882. /*
  883. * Got both cursors still active, need to find better entry.
  884. */
  885. if (bno_cur_lt && bno_cur_gt) {
  886. /*
  887. * Left side is long enough, look for a right side entry.
  888. */
  889. if (ltlena >= args->minlen) {
  890. /*
  891. * Fix up the length.
  892. */
  893. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  894. xfs_alloc_fix_len(args);
  895. rlen = args->len;
  896. ltdiff = xfs_alloc_compute_diff(args->agbno, rlen,
  897. args->alignment, ltbno, ltlen, &ltnew);
  898. /*
  899. * Not perfect.
  900. */
  901. if (ltdiff) {
  902. /*
  903. * Look until we find a better one, run out of
  904. * space, or run off the end.
  905. */
  906. while (bno_cur_lt && bno_cur_gt) {
  907. if ((error = xfs_alloc_get_rec(
  908. bno_cur_gt, &gtbno,
  909. &gtlen, &i)))
  910. goto error0;
  911. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  912. xfs_alloc_compute_aligned(gtbno, gtlen,
  913. args->alignment, args->minlen,
  914. &gtbnoa, &gtlena);
  915. /*
  916. * The left one is clearly better.
  917. */
  918. if (gtbnoa >= args->agbno + ltdiff) {
  919. xfs_btree_del_cursor(
  920. bno_cur_gt,
  921. XFS_BTREE_NOERROR);
  922. bno_cur_gt = NULL;
  923. break;
  924. }
  925. /*
  926. * If we reach a big enough entry,
  927. * compare the two and pick the best.
  928. */
  929. if (gtlena >= args->minlen) {
  930. args->len =
  931. XFS_EXTLEN_MIN(gtlena,
  932. args->maxlen);
  933. xfs_alloc_fix_len(args);
  934. rlen = args->len;
  935. gtdiff = xfs_alloc_compute_diff(
  936. args->agbno, rlen,
  937. args->alignment,
  938. gtbno, gtlen, &gtnew);
  939. /*
  940. * Right side is better.
  941. */
  942. if (gtdiff < ltdiff) {
  943. xfs_btree_del_cursor(
  944. bno_cur_lt,
  945. XFS_BTREE_NOERROR);
  946. bno_cur_lt = NULL;
  947. }
  948. /*
  949. * Left side is better.
  950. */
  951. else {
  952. xfs_btree_del_cursor(
  953. bno_cur_gt,
  954. XFS_BTREE_NOERROR);
  955. bno_cur_gt = NULL;
  956. }
  957. break;
  958. }
  959. /*
  960. * Fell off the right end.
  961. */
  962. if ((error = xfs_btree_increment(
  963. bno_cur_gt, 0, &i)))
  964. goto error0;
  965. if (!i) {
  966. xfs_btree_del_cursor(
  967. bno_cur_gt,
  968. XFS_BTREE_NOERROR);
  969. bno_cur_gt = NULL;
  970. break;
  971. }
  972. }
  973. }
  974. /*
  975. * The left side is perfect, trash the right side.
  976. */
  977. else {
  978. xfs_btree_del_cursor(bno_cur_gt,
  979. XFS_BTREE_NOERROR);
  980. bno_cur_gt = NULL;
  981. }
  982. }
  983. /*
  984. * It's the right side that was found first, look left.
  985. */
  986. else {
  987. /*
  988. * Fix up the length.
  989. */
  990. args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
  991. xfs_alloc_fix_len(args);
  992. rlen = args->len;
  993. gtdiff = xfs_alloc_compute_diff(args->agbno, rlen,
  994. args->alignment, gtbno, gtlen, &gtnew);
  995. /*
  996. * Right side entry isn't perfect.
  997. */
  998. if (gtdiff) {
  999. /*
  1000. * Look until we find a better one, run out of
  1001. * space, or run off the end.
  1002. */
  1003. while (bno_cur_lt && bno_cur_gt) {
  1004. if ((error = xfs_alloc_get_rec(
  1005. bno_cur_lt, &ltbno,
  1006. &ltlen, &i)))
  1007. goto error0;
  1008. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1009. xfs_alloc_compute_aligned(ltbno, ltlen,
  1010. args->alignment, args->minlen,
  1011. &ltbnoa, &ltlena);
  1012. /*
  1013. * The right one is clearly better.
  1014. */
  1015. if (ltbnoa <= args->agbno - gtdiff) {
  1016. xfs_btree_del_cursor(
  1017. bno_cur_lt,
  1018. XFS_BTREE_NOERROR);
  1019. bno_cur_lt = NULL;
  1020. break;
  1021. }
  1022. /*
  1023. * If we reach a big enough entry,
  1024. * compare the two and pick the best.
  1025. */
  1026. if (ltlena >= args->minlen) {
  1027. args->len = XFS_EXTLEN_MIN(
  1028. ltlena, args->maxlen);
  1029. xfs_alloc_fix_len(args);
  1030. rlen = args->len;
  1031. ltdiff = xfs_alloc_compute_diff(
  1032. args->agbno, rlen,
  1033. args->alignment,
  1034. ltbno, ltlen, &ltnew);
  1035. /*
  1036. * Left side is better.
  1037. */
  1038. if (ltdiff < gtdiff) {
  1039. xfs_btree_del_cursor(
  1040. bno_cur_gt,
  1041. XFS_BTREE_NOERROR);
  1042. bno_cur_gt = NULL;
  1043. }
  1044. /*
  1045. * Right side is better.
  1046. */
  1047. else {
  1048. xfs_btree_del_cursor(
  1049. bno_cur_lt,
  1050. XFS_BTREE_NOERROR);
  1051. bno_cur_lt = NULL;
  1052. }
  1053. break;
  1054. }
  1055. /*
  1056. * Fell off the left end.
  1057. */
  1058. if ((error = xfs_btree_decrement(
  1059. bno_cur_lt, 0, &i)))
  1060. goto error0;
  1061. if (!i) {
  1062. xfs_btree_del_cursor(bno_cur_lt,
  1063. XFS_BTREE_NOERROR);
  1064. bno_cur_lt = NULL;
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. /*
  1070. * The right side is perfect, trash the left side.
  1071. */
  1072. else {
  1073. xfs_btree_del_cursor(bno_cur_lt,
  1074. XFS_BTREE_NOERROR);
  1075. bno_cur_lt = NULL;
  1076. }
  1077. }
  1078. }
  1079. /*
  1080. * If we couldn't get anything, give up.
  1081. */
  1082. if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
  1083. trace_xfs_alloc_size_neither(args);
  1084. args->agbno = NULLAGBLOCK;
  1085. return 0;
  1086. }
  1087. /*
  1088. * At this point we have selected a freespace entry, either to the
  1089. * left or to the right. If it's on the right, copy all the
  1090. * useful variables to the "left" set so we only have one
  1091. * copy of this code.
  1092. */
  1093. if (bno_cur_gt) {
  1094. bno_cur_lt = bno_cur_gt;
  1095. bno_cur_gt = NULL;
  1096. ltbno = gtbno;
  1097. ltbnoa = gtbnoa;
  1098. ltlen = gtlen;
  1099. ltlena = gtlena;
  1100. j = 1;
  1101. } else
  1102. j = 0;
  1103. /*
  1104. * Fix up the length and compute the useful address.
  1105. */
  1106. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1107. xfs_alloc_fix_len(args);
  1108. if (!xfs_alloc_fix_minleft(args)) {
  1109. trace_xfs_alloc_near_nominleft(args);
  1110. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1111. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1112. return 0;
  1113. }
  1114. rlen = args->len;
  1115. (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment, ltbno,
  1116. ltlen, &ltnew);
  1117. ASSERT(ltnew >= ltbno);
  1118. ASSERT(ltnew + rlen <= ltbno + ltlen);
  1119. ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1120. args->agbno = ltnew;
  1121. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
  1122. ltnew, rlen, XFSA_FIXUP_BNO_OK)))
  1123. goto error0;
  1124. if (j)
  1125. trace_xfs_alloc_near_greater(args);
  1126. else
  1127. trace_xfs_alloc_near_lesser(args);
  1128. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1129. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1130. return 0;
  1131. error0:
  1132. trace_xfs_alloc_near_error(args);
  1133. if (cnt_cur != NULL)
  1134. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1135. if (bno_cur_lt != NULL)
  1136. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
  1137. if (bno_cur_gt != NULL)
  1138. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
  1139. return error;
  1140. }
  1141. /*
  1142. * Allocate a variable extent anywhere in the allocation group agno.
  1143. * Extent's length (returned in len) will be between minlen and maxlen,
  1144. * and of the form k * prod + mod unless there's nothing that large.
  1145. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1146. */
  1147. STATIC int /* error */
  1148. xfs_alloc_ag_vextent_size(
  1149. xfs_alloc_arg_t *args) /* allocation argument structure */
  1150. {
  1151. xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
  1152. xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
  1153. int error; /* error result */
  1154. xfs_agblock_t fbno; /* start of found freespace */
  1155. xfs_extlen_t flen; /* length of found freespace */
  1156. int i; /* temp status variable */
  1157. xfs_agblock_t rbno; /* returned block number */
  1158. xfs_extlen_t rlen; /* length of returned extent */
  1159. /*
  1160. * Allocate and initialize a cursor for the by-size btree.
  1161. */
  1162. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1163. args->agno, XFS_BTNUM_CNT);
  1164. bno_cur = NULL;
  1165. /*
  1166. * Look for an entry >= maxlen+alignment-1 blocks.
  1167. */
  1168. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
  1169. args->maxlen + args->alignment - 1, &i)))
  1170. goto error0;
  1171. /*
  1172. * If none, then pick up the last entry in the tree unless the
  1173. * tree is empty.
  1174. */
  1175. if (!i) {
  1176. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &fbno,
  1177. &flen, &i)))
  1178. goto error0;
  1179. if (i == 0 || flen == 0) {
  1180. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1181. trace_xfs_alloc_size_noentry(args);
  1182. return 0;
  1183. }
  1184. ASSERT(i == 1);
  1185. }
  1186. /*
  1187. * There's a freespace as big as maxlen+alignment-1, get it.
  1188. */
  1189. else {
  1190. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i)))
  1191. goto error0;
  1192. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1193. }
  1194. /*
  1195. * In the first case above, we got the last entry in the
  1196. * by-size btree. Now we check to see if the space hits maxlen
  1197. * once aligned; if not, we search left for something better.
  1198. * This can't happen in the second case above.
  1199. */
  1200. xfs_alloc_compute_aligned(fbno, flen, args->alignment, args->minlen,
  1201. &rbno, &rlen);
  1202. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1203. XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
  1204. (rlen <= flen && rbno + rlen <= fbno + flen), error0);
  1205. if (rlen < args->maxlen) {
  1206. xfs_agblock_t bestfbno;
  1207. xfs_extlen_t bestflen;
  1208. xfs_agblock_t bestrbno;
  1209. xfs_extlen_t bestrlen;
  1210. bestrlen = rlen;
  1211. bestrbno = rbno;
  1212. bestflen = flen;
  1213. bestfbno = fbno;
  1214. for (;;) {
  1215. if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
  1216. goto error0;
  1217. if (i == 0)
  1218. break;
  1219. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
  1220. &i)))
  1221. goto error0;
  1222. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1223. if (flen < bestrlen)
  1224. break;
  1225. xfs_alloc_compute_aligned(fbno, flen, args->alignment,
  1226. args->minlen, &rbno, &rlen);
  1227. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1228. XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
  1229. (rlen <= flen && rbno + rlen <= fbno + flen),
  1230. error0);
  1231. if (rlen > bestrlen) {
  1232. bestrlen = rlen;
  1233. bestrbno = rbno;
  1234. bestflen = flen;
  1235. bestfbno = fbno;
  1236. if (rlen == args->maxlen)
  1237. break;
  1238. }
  1239. }
  1240. if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
  1241. &i)))
  1242. goto error0;
  1243. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1244. rlen = bestrlen;
  1245. rbno = bestrbno;
  1246. flen = bestflen;
  1247. fbno = bestfbno;
  1248. }
  1249. args->wasfromfl = 0;
  1250. /*
  1251. * Fix up the length.
  1252. */
  1253. args->len = rlen;
  1254. xfs_alloc_fix_len(args);
  1255. if (rlen < args->minlen || !xfs_alloc_fix_minleft(args)) {
  1256. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1257. trace_xfs_alloc_size_nominleft(args);
  1258. args->agbno = NULLAGBLOCK;
  1259. return 0;
  1260. }
  1261. rlen = args->len;
  1262. XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
  1263. /*
  1264. * Allocate and initialize a cursor for the by-block tree.
  1265. */
  1266. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1267. args->agno, XFS_BTNUM_BNO);
  1268. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  1269. rbno, rlen, XFSA_FIXUP_CNT_OK)))
  1270. goto error0;
  1271. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1272. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1273. cnt_cur = bno_cur = NULL;
  1274. args->len = rlen;
  1275. args->agbno = rbno;
  1276. XFS_WANT_CORRUPTED_GOTO(
  1277. args->agbno + args->len <=
  1278. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1279. error0);
  1280. trace_xfs_alloc_size_done(args);
  1281. return 0;
  1282. error0:
  1283. trace_xfs_alloc_size_error(args);
  1284. if (cnt_cur)
  1285. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1286. if (bno_cur)
  1287. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1288. return error;
  1289. }
  1290. /*
  1291. * Deal with the case where only small freespaces remain.
  1292. * Either return the contents of the last freespace record,
  1293. * or allocate space from the freelist if there is nothing in the tree.
  1294. */
  1295. STATIC int /* error */
  1296. xfs_alloc_ag_vextent_small(
  1297. xfs_alloc_arg_t *args, /* allocation argument structure */
  1298. xfs_btree_cur_t *ccur, /* by-size cursor */
  1299. xfs_agblock_t *fbnop, /* result block number */
  1300. xfs_extlen_t *flenp, /* result length */
  1301. int *stat) /* status: 0-freelist, 1-normal/none */
  1302. {
  1303. int error;
  1304. xfs_agblock_t fbno;
  1305. xfs_extlen_t flen;
  1306. int i;
  1307. if ((error = xfs_btree_decrement(ccur, 0, &i)))
  1308. goto error0;
  1309. if (i) {
  1310. if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
  1311. goto error0;
  1312. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1313. }
  1314. /*
  1315. * Nothing in the btree, try the freelist. Make sure
  1316. * to respect minleft even when pulling from the
  1317. * freelist.
  1318. */
  1319. else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
  1320. (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
  1321. > args->minleft)) {
  1322. error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
  1323. if (error)
  1324. goto error0;
  1325. if (fbno != NULLAGBLOCK) {
  1326. if (args->userdata) {
  1327. xfs_buf_t *bp;
  1328. bp = xfs_btree_get_bufs(args->mp, args->tp,
  1329. args->agno, fbno, 0);
  1330. xfs_trans_binval(args->tp, bp);
  1331. }
  1332. args->len = 1;
  1333. args->agbno = fbno;
  1334. XFS_WANT_CORRUPTED_GOTO(
  1335. args->agbno + args->len <=
  1336. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1337. error0);
  1338. args->wasfromfl = 1;
  1339. trace_xfs_alloc_small_freelist(args);
  1340. *stat = 0;
  1341. return 0;
  1342. }
  1343. /*
  1344. * Nothing in the freelist.
  1345. */
  1346. else
  1347. flen = 0;
  1348. }
  1349. /*
  1350. * Can't allocate from the freelist for some reason.
  1351. */
  1352. else {
  1353. fbno = NULLAGBLOCK;
  1354. flen = 0;
  1355. }
  1356. /*
  1357. * Can't do the allocation, give up.
  1358. */
  1359. if (flen < args->minlen) {
  1360. args->agbno = NULLAGBLOCK;
  1361. trace_xfs_alloc_small_notenough(args);
  1362. flen = 0;
  1363. }
  1364. *fbnop = fbno;
  1365. *flenp = flen;
  1366. *stat = 1;
  1367. trace_xfs_alloc_small_done(args);
  1368. return 0;
  1369. error0:
  1370. trace_xfs_alloc_small_error(args);
  1371. return error;
  1372. }
  1373. /*
  1374. * Free the extent starting at agno/bno for length.
  1375. */
  1376. STATIC int /* error */
  1377. xfs_free_ag_extent(
  1378. xfs_trans_t *tp, /* transaction pointer */
  1379. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  1380. xfs_agnumber_t agno, /* allocation group number */
  1381. xfs_agblock_t bno, /* starting block number */
  1382. xfs_extlen_t len, /* length of extent */
  1383. int isfl) /* set if is freelist blocks - no sb acctg */
  1384. {
  1385. xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
  1386. xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
  1387. int error; /* error return value */
  1388. xfs_agblock_t gtbno; /* start of right neighbor block */
  1389. xfs_extlen_t gtlen; /* length of right neighbor block */
  1390. int haveleft; /* have a left neighbor block */
  1391. int haveright; /* have a right neighbor block */
  1392. int i; /* temp, result code */
  1393. xfs_agblock_t ltbno; /* start of left neighbor block */
  1394. xfs_extlen_t ltlen; /* length of left neighbor block */
  1395. xfs_mount_t *mp; /* mount point struct for filesystem */
  1396. xfs_agblock_t nbno; /* new starting block of freespace */
  1397. xfs_extlen_t nlen; /* new length of freespace */
  1398. mp = tp->t_mountp;
  1399. /*
  1400. * Allocate and initialize a cursor for the by-block btree.
  1401. */
  1402. bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
  1403. cnt_cur = NULL;
  1404. /*
  1405. * Look for a neighboring block on the left (lower block numbers)
  1406. * that is contiguous with this space.
  1407. */
  1408. if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
  1409. goto error0;
  1410. if (haveleft) {
  1411. /*
  1412. * There is a block to our left.
  1413. */
  1414. if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
  1415. goto error0;
  1416. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1417. /*
  1418. * It's not contiguous, though.
  1419. */
  1420. if (ltbno + ltlen < bno)
  1421. haveleft = 0;
  1422. else {
  1423. /*
  1424. * If this failure happens the request to free this
  1425. * space was invalid, it's (partly) already free.
  1426. * Very bad.
  1427. */
  1428. XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
  1429. }
  1430. }
  1431. /*
  1432. * Look for a neighboring block on the right (higher block numbers)
  1433. * that is contiguous with this space.
  1434. */
  1435. if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
  1436. goto error0;
  1437. if (haveright) {
  1438. /*
  1439. * There is a block to our right.
  1440. */
  1441. if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
  1442. goto error0;
  1443. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1444. /*
  1445. * It's not contiguous, though.
  1446. */
  1447. if (bno + len < gtbno)
  1448. haveright = 0;
  1449. else {
  1450. /*
  1451. * If this failure happens the request to free this
  1452. * space was invalid, it's (partly) already free.
  1453. * Very bad.
  1454. */
  1455. XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
  1456. }
  1457. }
  1458. /*
  1459. * Now allocate and initialize a cursor for the by-size tree.
  1460. */
  1461. cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
  1462. /*
  1463. * Have both left and right contiguous neighbors.
  1464. * Merge all three into a single free block.
  1465. */
  1466. if (haveleft && haveright) {
  1467. /*
  1468. * Delete the old by-size entry on the left.
  1469. */
  1470. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1471. goto error0;
  1472. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1473. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1474. goto error0;
  1475. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1476. /*
  1477. * Delete the old by-size entry on the right.
  1478. */
  1479. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1480. goto error0;
  1481. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1482. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1483. goto error0;
  1484. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1485. /*
  1486. * Delete the old by-block entry for the right block.
  1487. */
  1488. if ((error = xfs_btree_delete(bno_cur, &i)))
  1489. goto error0;
  1490. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1491. /*
  1492. * Move the by-block cursor back to the left neighbor.
  1493. */
  1494. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1495. goto error0;
  1496. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1497. #ifdef DEBUG
  1498. /*
  1499. * Check that this is the right record: delete didn't
  1500. * mangle the cursor.
  1501. */
  1502. {
  1503. xfs_agblock_t xxbno;
  1504. xfs_extlen_t xxlen;
  1505. if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
  1506. &i)))
  1507. goto error0;
  1508. XFS_WANT_CORRUPTED_GOTO(
  1509. i == 1 && xxbno == ltbno && xxlen == ltlen,
  1510. error0);
  1511. }
  1512. #endif
  1513. /*
  1514. * Update remaining by-block entry to the new, joined block.
  1515. */
  1516. nbno = ltbno;
  1517. nlen = len + ltlen + gtlen;
  1518. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1519. goto error0;
  1520. }
  1521. /*
  1522. * Have only a left contiguous neighbor.
  1523. * Merge it together with the new freespace.
  1524. */
  1525. else if (haveleft) {
  1526. /*
  1527. * Delete the old by-size entry on the left.
  1528. */
  1529. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1530. goto error0;
  1531. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1532. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1533. goto error0;
  1534. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1535. /*
  1536. * Back up the by-block cursor to the left neighbor, and
  1537. * update its length.
  1538. */
  1539. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1540. goto error0;
  1541. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1542. nbno = ltbno;
  1543. nlen = len + ltlen;
  1544. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1545. goto error0;
  1546. }
  1547. /*
  1548. * Have only a right contiguous neighbor.
  1549. * Merge it together with the new freespace.
  1550. */
  1551. else if (haveright) {
  1552. /*
  1553. * Delete the old by-size entry on the right.
  1554. */
  1555. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1556. goto error0;
  1557. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1558. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1559. goto error0;
  1560. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1561. /*
  1562. * Update the starting block and length of the right
  1563. * neighbor in the by-block tree.
  1564. */
  1565. nbno = bno;
  1566. nlen = len + gtlen;
  1567. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1568. goto error0;
  1569. }
  1570. /*
  1571. * No contiguous neighbors.
  1572. * Insert the new freespace into the by-block tree.
  1573. */
  1574. else {
  1575. nbno = bno;
  1576. nlen = len;
  1577. if ((error = xfs_btree_insert(bno_cur, &i)))
  1578. goto error0;
  1579. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1580. }
  1581. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1582. bno_cur = NULL;
  1583. /*
  1584. * In all cases we need to insert the new freespace in the by-size tree.
  1585. */
  1586. if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
  1587. goto error0;
  1588. XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
  1589. if ((error = xfs_btree_insert(cnt_cur, &i)))
  1590. goto error0;
  1591. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1592. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1593. cnt_cur = NULL;
  1594. /*
  1595. * Update the freespace totals in the ag and superblock.
  1596. */
  1597. {
  1598. xfs_agf_t *agf;
  1599. xfs_perag_t *pag; /* per allocation group data */
  1600. pag = xfs_perag_get(mp, agno);
  1601. pag->pagf_freeblks += len;
  1602. xfs_perag_put(pag);
  1603. agf = XFS_BUF_TO_AGF(agbp);
  1604. be32_add_cpu(&agf->agf_freeblks, len);
  1605. xfs_trans_agblocks_delta(tp, len);
  1606. XFS_WANT_CORRUPTED_GOTO(
  1607. be32_to_cpu(agf->agf_freeblks) <=
  1608. be32_to_cpu(agf->agf_length),
  1609. error0);
  1610. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
  1611. if (!isfl)
  1612. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
  1613. XFS_STATS_INC(xs_freex);
  1614. XFS_STATS_ADD(xs_freeb, len);
  1615. }
  1616. trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
  1617. /*
  1618. * Since blocks move to the free list without the coordination
  1619. * used in xfs_bmap_finish, we can't allow block to be available
  1620. * for reallocation and non-transaction writing (user data)
  1621. * until we know that the transaction that moved it to the free
  1622. * list is permanently on disk. We track the blocks by declaring
  1623. * these blocks as "busy"; the busy list is maintained on a per-ag
  1624. * basis and each transaction records which entries should be removed
  1625. * when the iclog commits to disk. If a busy block is allocated,
  1626. * the iclog is pushed up to the LSN that freed the block.
  1627. */
  1628. xfs_alloc_busy_insert(tp, agno, bno, len);
  1629. return 0;
  1630. error0:
  1631. trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
  1632. if (bno_cur)
  1633. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1634. if (cnt_cur)
  1635. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1636. return error;
  1637. }
  1638. /*
  1639. * Visible (exported) allocation/free functions.
  1640. * Some of these are used just by xfs_alloc_btree.c and this file.
  1641. */
  1642. /*
  1643. * Compute and fill in value of m_ag_maxlevels.
  1644. */
  1645. void
  1646. xfs_alloc_compute_maxlevels(
  1647. xfs_mount_t *mp) /* file system mount structure */
  1648. {
  1649. int level;
  1650. uint maxblocks;
  1651. uint maxleafents;
  1652. int minleafrecs;
  1653. int minnoderecs;
  1654. maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
  1655. minleafrecs = mp->m_alloc_mnr[0];
  1656. minnoderecs = mp->m_alloc_mnr[1];
  1657. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  1658. for (level = 1; maxblocks > 1; level++)
  1659. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  1660. mp->m_ag_maxlevels = level;
  1661. }
  1662. /*
  1663. * Find the length of the longest extent in an AG.
  1664. */
  1665. xfs_extlen_t
  1666. xfs_alloc_longest_free_extent(
  1667. struct xfs_mount *mp,
  1668. struct xfs_perag *pag)
  1669. {
  1670. xfs_extlen_t need, delta = 0;
  1671. need = XFS_MIN_FREELIST_PAG(pag, mp);
  1672. if (need > pag->pagf_flcount)
  1673. delta = need - pag->pagf_flcount;
  1674. if (pag->pagf_longest > delta)
  1675. return pag->pagf_longest - delta;
  1676. return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
  1677. }
  1678. /*
  1679. * Decide whether to use this allocation group for this allocation.
  1680. * If so, fix up the btree freelist's size.
  1681. */
  1682. STATIC int /* error */
  1683. xfs_alloc_fix_freelist(
  1684. xfs_alloc_arg_t *args, /* allocation argument structure */
  1685. int flags) /* XFS_ALLOC_FLAG_... */
  1686. {
  1687. xfs_buf_t *agbp; /* agf buffer pointer */
  1688. xfs_agf_t *agf; /* a.g. freespace structure pointer */
  1689. xfs_buf_t *agflbp;/* agfl buffer pointer */
  1690. xfs_agblock_t bno; /* freelist block */
  1691. xfs_extlen_t delta; /* new blocks needed in freelist */
  1692. int error; /* error result code */
  1693. xfs_extlen_t longest;/* longest extent in allocation group */
  1694. xfs_mount_t *mp; /* file system mount point structure */
  1695. xfs_extlen_t need; /* total blocks needed in freelist */
  1696. xfs_perag_t *pag; /* per-ag information structure */
  1697. xfs_alloc_arg_t targs; /* local allocation arguments */
  1698. xfs_trans_t *tp; /* transaction pointer */
  1699. mp = args->mp;
  1700. pag = args->pag;
  1701. tp = args->tp;
  1702. if (!pag->pagf_init) {
  1703. if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
  1704. &agbp)))
  1705. return error;
  1706. if (!pag->pagf_init) {
  1707. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  1708. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  1709. args->agbp = NULL;
  1710. return 0;
  1711. }
  1712. } else
  1713. agbp = NULL;
  1714. /*
  1715. * If this is a metadata preferred pag and we are user data
  1716. * then try somewhere else if we are not being asked to
  1717. * try harder at this point
  1718. */
  1719. if (pag->pagf_metadata && args->userdata &&
  1720. (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
  1721. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  1722. args->agbp = NULL;
  1723. return 0;
  1724. }
  1725. if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
  1726. /*
  1727. * If it looks like there isn't a long enough extent, or enough
  1728. * total blocks, reject it.
  1729. */
  1730. need = XFS_MIN_FREELIST_PAG(pag, mp);
  1731. longest = xfs_alloc_longest_free_extent(mp, pag);
  1732. if ((args->minlen + args->alignment + args->minalignslop - 1) >
  1733. longest ||
  1734. ((int)(pag->pagf_freeblks + pag->pagf_flcount -
  1735. need - args->total) < (int)args->minleft)) {
  1736. if (agbp)
  1737. xfs_trans_brelse(tp, agbp);
  1738. args->agbp = NULL;
  1739. return 0;
  1740. }
  1741. }
  1742. /*
  1743. * Get the a.g. freespace buffer.
  1744. * Can fail if we're not blocking on locks, and it's held.
  1745. */
  1746. if (agbp == NULL) {
  1747. if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
  1748. &agbp)))
  1749. return error;
  1750. if (agbp == NULL) {
  1751. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  1752. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  1753. args->agbp = NULL;
  1754. return 0;
  1755. }
  1756. }
  1757. /*
  1758. * Figure out how many blocks we should have in the freelist.
  1759. */
  1760. agf = XFS_BUF_TO_AGF(agbp);
  1761. need = XFS_MIN_FREELIST(agf, mp);
  1762. /*
  1763. * If there isn't enough total or single-extent, reject it.
  1764. */
  1765. if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
  1766. delta = need > be32_to_cpu(agf->agf_flcount) ?
  1767. (need - be32_to_cpu(agf->agf_flcount)) : 0;
  1768. longest = be32_to_cpu(agf->agf_longest);
  1769. longest = (longest > delta) ? (longest - delta) :
  1770. (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
  1771. if ((args->minlen + args->alignment + args->minalignslop - 1) >
  1772. longest ||
  1773. ((int)(be32_to_cpu(agf->agf_freeblks) +
  1774. be32_to_cpu(agf->agf_flcount) - need - args->total) <
  1775. (int)args->minleft)) {
  1776. xfs_trans_brelse(tp, agbp);
  1777. args->agbp = NULL;
  1778. return 0;
  1779. }
  1780. }
  1781. /*
  1782. * Make the freelist shorter if it's too long.
  1783. */
  1784. while (be32_to_cpu(agf->agf_flcount) > need) {
  1785. xfs_buf_t *bp;
  1786. error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
  1787. if (error)
  1788. return error;
  1789. if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
  1790. return error;
  1791. bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
  1792. xfs_trans_binval(tp, bp);
  1793. }
  1794. /*
  1795. * Initialize the args structure.
  1796. */
  1797. targs.tp = tp;
  1798. targs.mp = mp;
  1799. targs.agbp = agbp;
  1800. targs.agno = args->agno;
  1801. targs.mod = targs.minleft = targs.wasdel = targs.userdata =
  1802. targs.minalignslop = 0;
  1803. targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
  1804. targs.type = XFS_ALLOCTYPE_THIS_AG;
  1805. targs.pag = pag;
  1806. if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
  1807. return error;
  1808. /*
  1809. * Make the freelist longer if it's too short.
  1810. */
  1811. while (be32_to_cpu(agf->agf_flcount) < need) {
  1812. targs.agbno = 0;
  1813. targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
  1814. /*
  1815. * Allocate as many blocks as possible at once.
  1816. */
  1817. if ((error = xfs_alloc_ag_vextent(&targs))) {
  1818. xfs_trans_brelse(tp, agflbp);
  1819. return error;
  1820. }
  1821. /*
  1822. * Stop if we run out. Won't happen if callers are obeying
  1823. * the restrictions correctly. Can happen for free calls
  1824. * on a completely full ag.
  1825. */
  1826. if (targs.agbno == NULLAGBLOCK) {
  1827. if (flags & XFS_ALLOC_FLAG_FREEING)
  1828. break;
  1829. xfs_trans_brelse(tp, agflbp);
  1830. args->agbp = NULL;
  1831. return 0;
  1832. }
  1833. /*
  1834. * Put each allocated block on the list.
  1835. */
  1836. for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
  1837. error = xfs_alloc_put_freelist(tp, agbp,
  1838. agflbp, bno, 0);
  1839. if (error)
  1840. return error;
  1841. }
  1842. }
  1843. xfs_trans_brelse(tp, agflbp);
  1844. args->agbp = agbp;
  1845. return 0;
  1846. }
  1847. /*
  1848. * Get a block from the freelist.
  1849. * Returns with the buffer for the block gotten.
  1850. */
  1851. int /* error */
  1852. xfs_alloc_get_freelist(
  1853. xfs_trans_t *tp, /* transaction pointer */
  1854. xfs_buf_t *agbp, /* buffer containing the agf structure */
  1855. xfs_agblock_t *bnop, /* block address retrieved from freelist */
  1856. int btreeblk) /* destination is a AGF btree */
  1857. {
  1858. xfs_agf_t *agf; /* a.g. freespace structure */
  1859. xfs_agfl_t *agfl; /* a.g. freelist structure */
  1860. xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
  1861. xfs_agblock_t bno; /* block number returned */
  1862. int error;
  1863. int logflags;
  1864. xfs_mount_t *mp; /* mount structure */
  1865. xfs_perag_t *pag; /* per allocation group data */
  1866. agf = XFS_BUF_TO_AGF(agbp);
  1867. /*
  1868. * Freelist is empty, give up.
  1869. */
  1870. if (!agf->agf_flcount) {
  1871. *bnop = NULLAGBLOCK;
  1872. return 0;
  1873. }
  1874. /*
  1875. * Read the array of free blocks.
  1876. */
  1877. mp = tp->t_mountp;
  1878. if ((error = xfs_alloc_read_agfl(mp, tp,
  1879. be32_to_cpu(agf->agf_seqno), &agflbp)))
  1880. return error;
  1881. agfl = XFS_BUF_TO_AGFL(agflbp);
  1882. /*
  1883. * Get the block number and update the data structures.
  1884. */
  1885. bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
  1886. be32_add_cpu(&agf->agf_flfirst, 1);
  1887. xfs_trans_brelse(tp, agflbp);
  1888. if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
  1889. agf->agf_flfirst = 0;
  1890. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  1891. be32_add_cpu(&agf->agf_flcount, -1);
  1892. xfs_trans_agflist_delta(tp, -1);
  1893. pag->pagf_flcount--;
  1894. xfs_perag_put(pag);
  1895. logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
  1896. if (btreeblk) {
  1897. be32_add_cpu(&agf->agf_btreeblks, 1);
  1898. pag->pagf_btreeblks++;
  1899. logflags |= XFS_AGF_BTREEBLKS;
  1900. }
  1901. xfs_alloc_log_agf(tp, agbp, logflags);
  1902. *bnop = bno;
  1903. /*
  1904. * As blocks are freed, they are added to the per-ag busy list and
  1905. * remain there until the freeing transaction is committed to disk.
  1906. * Now that we have allocated blocks, this list must be searched to see
  1907. * if a block is being reused. If one is, then the freeing transaction
  1908. * must be pushed to disk before this transaction.
  1909. *
  1910. * We do this by setting the current transaction to a sync transaction
  1911. * which guarantees that the freeing transaction is on disk before this
  1912. * transaction. This is done instead of a synchronous log force here so
  1913. * that we don't sit and wait with the AGF locked in the transaction
  1914. * during the log force.
  1915. */
  1916. if (xfs_alloc_busy_search(mp, be32_to_cpu(agf->agf_seqno), bno, 1))
  1917. xfs_trans_set_sync(tp);
  1918. return 0;
  1919. }
  1920. /*
  1921. * Log the given fields from the agf structure.
  1922. */
  1923. void
  1924. xfs_alloc_log_agf(
  1925. xfs_trans_t *tp, /* transaction pointer */
  1926. xfs_buf_t *bp, /* buffer for a.g. freelist header */
  1927. int fields) /* mask of fields to be logged (XFS_AGF_...) */
  1928. {
  1929. int first; /* first byte offset */
  1930. int last; /* last byte offset */
  1931. static const short offsets[] = {
  1932. offsetof(xfs_agf_t, agf_magicnum),
  1933. offsetof(xfs_agf_t, agf_versionnum),
  1934. offsetof(xfs_agf_t, agf_seqno),
  1935. offsetof(xfs_agf_t, agf_length),
  1936. offsetof(xfs_agf_t, agf_roots[0]),
  1937. offsetof(xfs_agf_t, agf_levels[0]),
  1938. offsetof(xfs_agf_t, agf_flfirst),
  1939. offsetof(xfs_agf_t, agf_fllast),
  1940. offsetof(xfs_agf_t, agf_flcount),
  1941. offsetof(xfs_agf_t, agf_freeblks),
  1942. offsetof(xfs_agf_t, agf_longest),
  1943. offsetof(xfs_agf_t, agf_btreeblks),
  1944. sizeof(xfs_agf_t)
  1945. };
  1946. trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
  1947. xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
  1948. xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
  1949. }
  1950. /*
  1951. * Interface for inode allocation to force the pag data to be initialized.
  1952. */
  1953. int /* error */
  1954. xfs_alloc_pagf_init(
  1955. xfs_mount_t *mp, /* file system mount structure */
  1956. xfs_trans_t *tp, /* transaction pointer */
  1957. xfs_agnumber_t agno, /* allocation group number */
  1958. int flags) /* XFS_ALLOC_FLAGS_... */
  1959. {
  1960. xfs_buf_t *bp;
  1961. int error;
  1962. if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
  1963. return error;
  1964. if (bp)
  1965. xfs_trans_brelse(tp, bp);
  1966. return 0;
  1967. }
  1968. /*
  1969. * Put the block on the freelist for the allocation group.
  1970. */
  1971. int /* error */
  1972. xfs_alloc_put_freelist(
  1973. xfs_trans_t *tp, /* transaction pointer */
  1974. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  1975. xfs_buf_t *agflbp,/* buffer for a.g. free block array */
  1976. xfs_agblock_t bno, /* block being freed */
  1977. int btreeblk) /* block came from a AGF btree */
  1978. {
  1979. xfs_agf_t *agf; /* a.g. freespace structure */
  1980. xfs_agfl_t *agfl; /* a.g. free block array */
  1981. __be32 *blockp;/* pointer to array entry */
  1982. int error;
  1983. int logflags;
  1984. xfs_mount_t *mp; /* mount structure */
  1985. xfs_perag_t *pag; /* per allocation group data */
  1986. agf = XFS_BUF_TO_AGF(agbp);
  1987. mp = tp->t_mountp;
  1988. if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
  1989. be32_to_cpu(agf->agf_seqno), &agflbp)))
  1990. return error;
  1991. agfl = XFS_BUF_TO_AGFL(agflbp);
  1992. be32_add_cpu(&agf->agf_fllast, 1);
  1993. if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
  1994. agf->agf_fllast = 0;
  1995. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  1996. be32_add_cpu(&agf->agf_flcount, 1);
  1997. xfs_trans_agflist_delta(tp, 1);
  1998. pag->pagf_flcount++;
  1999. logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
  2000. if (btreeblk) {
  2001. be32_add_cpu(&agf->agf_btreeblks, -1);
  2002. pag->pagf_btreeblks--;
  2003. logflags |= XFS_AGF_BTREEBLKS;
  2004. }
  2005. xfs_perag_put(pag);
  2006. xfs_alloc_log_agf(tp, agbp, logflags);
  2007. ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
  2008. blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
  2009. *blockp = cpu_to_be32(bno);
  2010. xfs_alloc_log_agf(tp, agbp, logflags);
  2011. xfs_trans_log_buf(tp, agflbp,
  2012. (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
  2013. (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
  2014. sizeof(xfs_agblock_t) - 1));
  2015. return 0;
  2016. }
  2017. /*
  2018. * Read in the allocation group header (free/alloc section).
  2019. */
  2020. int /* error */
  2021. xfs_read_agf(
  2022. struct xfs_mount *mp, /* mount point structure */
  2023. struct xfs_trans *tp, /* transaction pointer */
  2024. xfs_agnumber_t agno, /* allocation group number */
  2025. int flags, /* XFS_BUF_ */
  2026. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2027. {
  2028. struct xfs_agf *agf; /* ag freelist header */
  2029. int agf_ok; /* set if agf is consistent */
  2030. int error;
  2031. ASSERT(agno != NULLAGNUMBER);
  2032. error = xfs_trans_read_buf(
  2033. mp, tp, mp->m_ddev_targp,
  2034. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  2035. XFS_FSS_TO_BB(mp, 1), flags, bpp);
  2036. if (error)
  2037. return error;
  2038. if (!*bpp)
  2039. return 0;
  2040. ASSERT(!XFS_BUF_GETERROR(*bpp));
  2041. agf = XFS_BUF_TO_AGF(*bpp);
  2042. /*
  2043. * Validate the magic number of the agf block.
  2044. */
  2045. agf_ok =
  2046. be32_to_cpu(agf->agf_magicnum) == XFS_AGF_MAGIC &&
  2047. XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
  2048. be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
  2049. be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
  2050. be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
  2051. be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp) &&
  2052. be32_to_cpu(agf->agf_seqno) == agno;
  2053. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  2054. agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
  2055. be32_to_cpu(agf->agf_length);
  2056. if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
  2057. XFS_RANDOM_ALLOC_READ_AGF))) {
  2058. XFS_CORRUPTION_ERROR("xfs_alloc_read_agf",
  2059. XFS_ERRLEVEL_LOW, mp, agf);
  2060. xfs_trans_brelse(tp, *bpp);
  2061. return XFS_ERROR(EFSCORRUPTED);
  2062. }
  2063. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGF, XFS_AGF_REF);
  2064. return 0;
  2065. }
  2066. /*
  2067. * Read in the allocation group header (free/alloc section).
  2068. */
  2069. int /* error */
  2070. xfs_alloc_read_agf(
  2071. struct xfs_mount *mp, /* mount point structure */
  2072. struct xfs_trans *tp, /* transaction pointer */
  2073. xfs_agnumber_t agno, /* allocation group number */
  2074. int flags, /* XFS_ALLOC_FLAG_... */
  2075. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2076. {
  2077. struct xfs_agf *agf; /* ag freelist header */
  2078. struct xfs_perag *pag; /* per allocation group data */
  2079. int error;
  2080. ASSERT(agno != NULLAGNUMBER);
  2081. error = xfs_read_agf(mp, tp, agno,
  2082. (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
  2083. bpp);
  2084. if (error)
  2085. return error;
  2086. if (!*bpp)
  2087. return 0;
  2088. ASSERT(!XFS_BUF_GETERROR(*bpp));
  2089. agf = XFS_BUF_TO_AGF(*bpp);
  2090. pag = xfs_perag_get(mp, agno);
  2091. if (!pag->pagf_init) {
  2092. pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
  2093. pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
  2094. pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
  2095. pag->pagf_longest = be32_to_cpu(agf->agf_longest);
  2096. pag->pagf_levels[XFS_BTNUM_BNOi] =
  2097. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
  2098. pag->pagf_levels[XFS_BTNUM_CNTi] =
  2099. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
  2100. spin_lock_init(&pag->pagb_lock);
  2101. pag->pagb_count = 0;
  2102. pag->pagb_tree = RB_ROOT;
  2103. pag->pagf_init = 1;
  2104. }
  2105. #ifdef DEBUG
  2106. else if (!XFS_FORCED_SHUTDOWN(mp)) {
  2107. ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
  2108. ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
  2109. ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
  2110. ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
  2111. ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
  2112. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
  2113. ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
  2114. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
  2115. }
  2116. #endif
  2117. xfs_perag_put(pag);
  2118. return 0;
  2119. }
  2120. /*
  2121. * Allocate an extent (variable-size).
  2122. * Depending on the allocation type, we either look in a single allocation
  2123. * group or loop over the allocation groups to find the result.
  2124. */
  2125. int /* error */
  2126. xfs_alloc_vextent(
  2127. xfs_alloc_arg_t *args) /* allocation argument structure */
  2128. {
  2129. xfs_agblock_t agsize; /* allocation group size */
  2130. int error;
  2131. int flags; /* XFS_ALLOC_FLAG_... locking flags */
  2132. xfs_extlen_t minleft;/* minimum left value, temp copy */
  2133. xfs_mount_t *mp; /* mount structure pointer */
  2134. xfs_agnumber_t sagno; /* starting allocation group number */
  2135. xfs_alloctype_t type; /* input allocation type */
  2136. int bump_rotor = 0;
  2137. int no_min = 0;
  2138. xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
  2139. mp = args->mp;
  2140. type = args->otype = args->type;
  2141. args->agbno = NULLAGBLOCK;
  2142. /*
  2143. * Just fix this up, for the case where the last a.g. is shorter
  2144. * (or there's only one a.g.) and the caller couldn't easily figure
  2145. * that out (xfs_bmap_alloc).
  2146. */
  2147. agsize = mp->m_sb.sb_agblocks;
  2148. if (args->maxlen > agsize)
  2149. args->maxlen = agsize;
  2150. if (args->alignment == 0)
  2151. args->alignment = 1;
  2152. ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
  2153. ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
  2154. ASSERT(args->minlen <= args->maxlen);
  2155. ASSERT(args->minlen <= agsize);
  2156. ASSERT(args->mod < args->prod);
  2157. if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
  2158. XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
  2159. args->minlen > args->maxlen || args->minlen > agsize ||
  2160. args->mod >= args->prod) {
  2161. args->fsbno = NULLFSBLOCK;
  2162. trace_xfs_alloc_vextent_badargs(args);
  2163. return 0;
  2164. }
  2165. minleft = args->minleft;
  2166. switch (type) {
  2167. case XFS_ALLOCTYPE_THIS_AG:
  2168. case XFS_ALLOCTYPE_NEAR_BNO:
  2169. case XFS_ALLOCTYPE_THIS_BNO:
  2170. /*
  2171. * These three force us into a single a.g.
  2172. */
  2173. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2174. args->pag = xfs_perag_get(mp, args->agno);
  2175. args->minleft = 0;
  2176. error = xfs_alloc_fix_freelist(args, 0);
  2177. args->minleft = minleft;
  2178. if (error) {
  2179. trace_xfs_alloc_vextent_nofix(args);
  2180. goto error0;
  2181. }
  2182. if (!args->agbp) {
  2183. trace_xfs_alloc_vextent_noagbp(args);
  2184. break;
  2185. }
  2186. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2187. if ((error = xfs_alloc_ag_vextent(args)))
  2188. goto error0;
  2189. break;
  2190. case XFS_ALLOCTYPE_START_BNO:
  2191. /*
  2192. * Try near allocation first, then anywhere-in-ag after
  2193. * the first a.g. fails.
  2194. */
  2195. if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
  2196. (mp->m_flags & XFS_MOUNT_32BITINODES)) {
  2197. args->fsbno = XFS_AGB_TO_FSB(mp,
  2198. ((mp->m_agfrotor / rotorstep) %
  2199. mp->m_sb.sb_agcount), 0);
  2200. bump_rotor = 1;
  2201. }
  2202. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2203. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2204. /* FALLTHROUGH */
  2205. case XFS_ALLOCTYPE_ANY_AG:
  2206. case XFS_ALLOCTYPE_START_AG:
  2207. case XFS_ALLOCTYPE_FIRST_AG:
  2208. /*
  2209. * Rotate through the allocation groups looking for a winner.
  2210. */
  2211. if (type == XFS_ALLOCTYPE_ANY_AG) {
  2212. /*
  2213. * Start with the last place we left off.
  2214. */
  2215. args->agno = sagno = (mp->m_agfrotor / rotorstep) %
  2216. mp->m_sb.sb_agcount;
  2217. args->type = XFS_ALLOCTYPE_THIS_AG;
  2218. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2219. } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
  2220. /*
  2221. * Start with allocation group given by bno.
  2222. */
  2223. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2224. args->type = XFS_ALLOCTYPE_THIS_AG;
  2225. sagno = 0;
  2226. flags = 0;
  2227. } else {
  2228. if (type == XFS_ALLOCTYPE_START_AG)
  2229. args->type = XFS_ALLOCTYPE_THIS_AG;
  2230. /*
  2231. * Start with the given allocation group.
  2232. */
  2233. args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2234. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2235. }
  2236. /*
  2237. * Loop over allocation groups twice; first time with
  2238. * trylock set, second time without.
  2239. */
  2240. for (;;) {
  2241. args->pag = xfs_perag_get(mp, args->agno);
  2242. if (no_min) args->minleft = 0;
  2243. error = xfs_alloc_fix_freelist(args, flags);
  2244. args->minleft = minleft;
  2245. if (error) {
  2246. trace_xfs_alloc_vextent_nofix(args);
  2247. goto error0;
  2248. }
  2249. /*
  2250. * If we get a buffer back then the allocation will fly.
  2251. */
  2252. if (args->agbp) {
  2253. if ((error = xfs_alloc_ag_vextent(args)))
  2254. goto error0;
  2255. break;
  2256. }
  2257. trace_xfs_alloc_vextent_loopfailed(args);
  2258. /*
  2259. * Didn't work, figure out the next iteration.
  2260. */
  2261. if (args->agno == sagno &&
  2262. type == XFS_ALLOCTYPE_START_BNO)
  2263. args->type = XFS_ALLOCTYPE_THIS_AG;
  2264. /*
  2265. * For the first allocation, we can try any AG to get
  2266. * space. However, if we already have allocated a
  2267. * block, we don't want to try AGs whose number is below
  2268. * sagno. Otherwise, we may end up with out-of-order
  2269. * locking of AGF, which might cause deadlock.
  2270. */
  2271. if (++(args->agno) == mp->m_sb.sb_agcount) {
  2272. if (args->firstblock != NULLFSBLOCK)
  2273. args->agno = sagno;
  2274. else
  2275. args->agno = 0;
  2276. }
  2277. /*
  2278. * Reached the starting a.g., must either be done
  2279. * or switch to non-trylock mode.
  2280. */
  2281. if (args->agno == sagno) {
  2282. if (no_min == 1) {
  2283. args->agbno = NULLAGBLOCK;
  2284. trace_xfs_alloc_vextent_allfailed(args);
  2285. break;
  2286. }
  2287. if (flags == 0) {
  2288. no_min = 1;
  2289. } else {
  2290. flags = 0;
  2291. if (type == XFS_ALLOCTYPE_START_BNO) {
  2292. args->agbno = XFS_FSB_TO_AGBNO(mp,
  2293. args->fsbno);
  2294. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2295. }
  2296. }
  2297. }
  2298. xfs_perag_put(args->pag);
  2299. }
  2300. if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
  2301. if (args->agno == sagno)
  2302. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  2303. (mp->m_sb.sb_agcount * rotorstep);
  2304. else
  2305. mp->m_agfrotor = (args->agno * rotorstep + 1) %
  2306. (mp->m_sb.sb_agcount * rotorstep);
  2307. }
  2308. break;
  2309. default:
  2310. ASSERT(0);
  2311. /* NOTREACHED */
  2312. }
  2313. if (args->agbno == NULLAGBLOCK)
  2314. args->fsbno = NULLFSBLOCK;
  2315. else {
  2316. args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
  2317. #ifdef DEBUG
  2318. ASSERT(args->len >= args->minlen);
  2319. ASSERT(args->len <= args->maxlen);
  2320. ASSERT(args->agbno % args->alignment == 0);
  2321. XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
  2322. args->len);
  2323. #endif
  2324. }
  2325. xfs_perag_put(args->pag);
  2326. return 0;
  2327. error0:
  2328. xfs_perag_put(args->pag);
  2329. return error;
  2330. }
  2331. /*
  2332. * Free an extent.
  2333. * Just break up the extent address and hand off to xfs_free_ag_extent
  2334. * after fixing up the freelist.
  2335. */
  2336. int /* error */
  2337. xfs_free_extent(
  2338. xfs_trans_t *tp, /* transaction pointer */
  2339. xfs_fsblock_t bno, /* starting block number of extent */
  2340. xfs_extlen_t len) /* length of extent */
  2341. {
  2342. xfs_alloc_arg_t args;
  2343. int error;
  2344. ASSERT(len != 0);
  2345. memset(&args, 0, sizeof(xfs_alloc_arg_t));
  2346. args.tp = tp;
  2347. args.mp = tp->t_mountp;
  2348. args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
  2349. ASSERT(args.agno < args.mp->m_sb.sb_agcount);
  2350. args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
  2351. args.pag = xfs_perag_get(args.mp, args.agno);
  2352. if ((error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING)))
  2353. goto error0;
  2354. #ifdef DEBUG
  2355. ASSERT(args.agbp != NULL);
  2356. ASSERT((args.agbno + len) <=
  2357. be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length));
  2358. #endif
  2359. error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
  2360. error0:
  2361. xfs_perag_put(args.pag);
  2362. return error;
  2363. }
  2364. /*
  2365. * AG Busy list management
  2366. * The busy list contains block ranges that have been freed but whose
  2367. * transactions have not yet hit disk. If any block listed in a busy
  2368. * list is reused, the transaction that freed it must be forced to disk
  2369. * before continuing to use the block.
  2370. *
  2371. * xfs_alloc_busy_insert - add to the per-ag busy list
  2372. * xfs_alloc_busy_clear - remove an item from the per-ag busy list
  2373. * xfs_alloc_busy_search - search for a busy extent
  2374. */
  2375. /*
  2376. * Insert a new extent into the busy tree.
  2377. *
  2378. * The busy extent tree is indexed by the start block of the busy extent.
  2379. * there can be multiple overlapping ranges in the busy extent tree but only
  2380. * ever one entry at a given start block. The reason for this is that
  2381. * multi-block extents can be freed, then smaller chunks of that extent
  2382. * allocated and freed again before the first transaction commit is on disk.
  2383. * If the exact same start block is freed a second time, we have to wait for
  2384. * that busy extent to pass out of the tree before the new extent is inserted.
  2385. * There are two main cases we have to handle here.
  2386. *
  2387. * The first case is a transaction that triggers a "free - allocate - free"
  2388. * cycle. This can occur during btree manipulations as a btree block is freed
  2389. * to the freelist, then allocated from the free list, then freed again. In
  2390. * this case, the second extxpnet free is what triggers the duplicate and as
  2391. * such the transaction IDs should match. Because the extent was allocated in
  2392. * this transaction, the transaction must be marked as synchronous. This is
  2393. * true for all cases where the free/alloc/free occurs in the one transaction,
  2394. * hence the addition of the ASSERT(tp->t_flags & XFS_TRANS_SYNC) to this case.
  2395. * This serves to catch violations of the second case quite effectively.
  2396. *
  2397. * The second case is where the free/alloc/free occur in different
  2398. * transactions. In this case, the thread freeing the extent the second time
  2399. * can't mark the extent busy immediately because it is already tracked in a
  2400. * transaction that may be committing. When the log commit for the existing
  2401. * busy extent completes, the busy extent will be removed from the tree. If we
  2402. * allow the second busy insert to continue using that busy extent structure,
  2403. * it can be freed before this transaction is safely in the log. Hence our
  2404. * only option in this case is to force the log to remove the existing busy
  2405. * extent from the list before we insert the new one with the current
  2406. * transaction ID.
  2407. *
  2408. * The problem we are trying to avoid in the free-alloc-free in separate
  2409. * transactions is most easily described with a timeline:
  2410. *
  2411. * Thread 1 Thread 2 Thread 3 xfslogd
  2412. * xact alloc
  2413. * free X
  2414. * mark busy
  2415. * commit xact
  2416. * free xact
  2417. * xact alloc
  2418. * alloc X
  2419. * busy search
  2420. * mark xact sync
  2421. * commit xact
  2422. * free xact
  2423. * force log
  2424. * checkpoint starts
  2425. * ....
  2426. * xact alloc
  2427. * free X
  2428. * mark busy
  2429. * finds match
  2430. * *** KABOOM! ***
  2431. * ....
  2432. * log IO completes
  2433. * unbusy X
  2434. * checkpoint completes
  2435. *
  2436. * By issuing a log force in thread 3 @ "KABOOM", the thread will block until
  2437. * the checkpoint completes, and the busy extent it matched will have been
  2438. * removed from the tree when it is woken. Hence it can then continue safely.
  2439. *
  2440. * However, to ensure this matching process is robust, we need to use the
  2441. * transaction ID for identifying transaction, as delayed logging results in
  2442. * the busy extent and transaction lifecycles being different. i.e. the busy
  2443. * extent is active for a lot longer than the transaction. Hence the
  2444. * transaction structure can be freed and reallocated, then mark the same
  2445. * extent busy again in the new transaction. In this case the new transaction
  2446. * will have a different tid but can have the same address, and hence we need
  2447. * to check against the tid.
  2448. *
  2449. * Future: for delayed logging, we could avoid the log force if the extent was
  2450. * first freed in the current checkpoint sequence. This, however, requires the
  2451. * ability to pin the current checkpoint in memory until this transaction
  2452. * commits to ensure that both the original free and the current one combine
  2453. * logically into the one checkpoint. If the checkpoint sequences are
  2454. * different, however, we still need to wait on a log force.
  2455. */
  2456. void
  2457. xfs_alloc_busy_insert(
  2458. struct xfs_trans *tp,
  2459. xfs_agnumber_t agno,
  2460. xfs_agblock_t bno,
  2461. xfs_extlen_t len)
  2462. {
  2463. struct xfs_busy_extent *new;
  2464. struct xfs_busy_extent *busyp;
  2465. struct xfs_perag *pag;
  2466. struct rb_node **rbp;
  2467. struct rb_node *parent;
  2468. int match;
  2469. new = kmem_zalloc(sizeof(struct xfs_busy_extent), KM_MAYFAIL);
  2470. if (!new) {
  2471. /*
  2472. * No Memory! Since it is now not possible to track the free
  2473. * block, make this a synchronous transaction to insure that
  2474. * the block is not reused before this transaction commits.
  2475. */
  2476. trace_xfs_alloc_busy(tp, agno, bno, len, 1);
  2477. xfs_trans_set_sync(tp);
  2478. return;
  2479. }
  2480. new->agno = agno;
  2481. new->bno = bno;
  2482. new->length = len;
  2483. new->tid = xfs_log_get_trans_ident(tp);
  2484. INIT_LIST_HEAD(&new->list);
  2485. /* trace before insert to be able to see failed inserts */
  2486. trace_xfs_alloc_busy(tp, agno, bno, len, 0);
  2487. pag = xfs_perag_get(tp->t_mountp, new->agno);
  2488. restart:
  2489. spin_lock(&pag->pagb_lock);
  2490. rbp = &pag->pagb_tree.rb_node;
  2491. parent = NULL;
  2492. busyp = NULL;
  2493. match = 0;
  2494. while (*rbp && match >= 0) {
  2495. parent = *rbp;
  2496. busyp = rb_entry(parent, struct xfs_busy_extent, rb_node);
  2497. if (new->bno < busyp->bno) {
  2498. /* may overlap, but exact start block is lower */
  2499. rbp = &(*rbp)->rb_left;
  2500. if (new->bno + new->length > busyp->bno)
  2501. match = busyp->tid == new->tid ? 1 : -1;
  2502. } else if (new->bno > busyp->bno) {
  2503. /* may overlap, but exact start block is higher */
  2504. rbp = &(*rbp)->rb_right;
  2505. if (bno < busyp->bno + busyp->length)
  2506. match = busyp->tid == new->tid ? 1 : -1;
  2507. } else {
  2508. match = busyp->tid == new->tid ? 1 : -1;
  2509. break;
  2510. }
  2511. }
  2512. if (match < 0) {
  2513. /* overlap marked busy in different transaction */
  2514. spin_unlock(&pag->pagb_lock);
  2515. xfs_log_force(tp->t_mountp, XFS_LOG_SYNC);
  2516. goto restart;
  2517. }
  2518. if (match > 0) {
  2519. /*
  2520. * overlap marked busy in same transaction. Update if exact
  2521. * start block match, otherwise combine the busy extents into
  2522. * a single range.
  2523. */
  2524. if (busyp->bno == new->bno) {
  2525. busyp->length = max(busyp->length, new->length);
  2526. spin_unlock(&pag->pagb_lock);
  2527. ASSERT(tp->t_flags & XFS_TRANS_SYNC);
  2528. xfs_perag_put(pag);
  2529. kmem_free(new);
  2530. return;
  2531. }
  2532. rb_erase(&busyp->rb_node, &pag->pagb_tree);
  2533. new->length = max(busyp->bno + busyp->length,
  2534. new->bno + new->length) -
  2535. min(busyp->bno, new->bno);
  2536. new->bno = min(busyp->bno, new->bno);
  2537. } else
  2538. busyp = NULL;
  2539. rb_link_node(&new->rb_node, parent, rbp);
  2540. rb_insert_color(&new->rb_node, &pag->pagb_tree);
  2541. list_add(&new->list, &tp->t_busy);
  2542. spin_unlock(&pag->pagb_lock);
  2543. xfs_perag_put(pag);
  2544. kmem_free(busyp);
  2545. }
  2546. /*
  2547. * Search for a busy extent within the range of the extent we are about to
  2548. * allocate. You need to be holding the busy extent tree lock when calling
  2549. * xfs_alloc_busy_search(). This function returns 0 for no overlapping busy
  2550. * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
  2551. * match. This is done so that a non-zero return indicates an overlap that
  2552. * will require a synchronous transaction, but it can still be
  2553. * used to distinguish between a partial or exact match.
  2554. */
  2555. static int
  2556. xfs_alloc_busy_search(
  2557. struct xfs_mount *mp,
  2558. xfs_agnumber_t agno,
  2559. xfs_agblock_t bno,
  2560. xfs_extlen_t len)
  2561. {
  2562. struct xfs_perag *pag;
  2563. struct rb_node *rbp;
  2564. struct xfs_busy_extent *busyp;
  2565. int match = 0;
  2566. pag = xfs_perag_get(mp, agno);
  2567. spin_lock(&pag->pagb_lock);
  2568. rbp = pag->pagb_tree.rb_node;
  2569. /* find closest start bno overlap */
  2570. while (rbp) {
  2571. busyp = rb_entry(rbp, struct xfs_busy_extent, rb_node);
  2572. if (bno < busyp->bno) {
  2573. /* may overlap, but exact start block is lower */
  2574. if (bno + len > busyp->bno)
  2575. match = -1;
  2576. rbp = rbp->rb_left;
  2577. } else if (bno > busyp->bno) {
  2578. /* may overlap, but exact start block is higher */
  2579. if (bno < busyp->bno + busyp->length)
  2580. match = -1;
  2581. rbp = rbp->rb_right;
  2582. } else {
  2583. /* bno matches busyp, length determines exact match */
  2584. match = (busyp->length == len) ? 1 : -1;
  2585. break;
  2586. }
  2587. }
  2588. spin_unlock(&pag->pagb_lock);
  2589. trace_xfs_alloc_busysearch(mp, agno, bno, len, !!match);
  2590. xfs_perag_put(pag);
  2591. return match;
  2592. }
  2593. void
  2594. xfs_alloc_busy_clear(
  2595. struct xfs_mount *mp,
  2596. struct xfs_busy_extent *busyp)
  2597. {
  2598. struct xfs_perag *pag;
  2599. trace_xfs_alloc_unbusy(mp, busyp->agno, busyp->bno,
  2600. busyp->length);
  2601. ASSERT(xfs_alloc_busy_search(mp, busyp->agno, busyp->bno,
  2602. busyp->length) == 1);
  2603. list_del_init(&busyp->list);
  2604. pag = xfs_perag_get(mp, busyp->agno);
  2605. spin_lock(&pag->pagb_lock);
  2606. rb_erase(&busyp->rb_node, &pag->pagb_tree);
  2607. spin_unlock(&pag->pagb_lock);
  2608. xfs_perag_put(pag);
  2609. kmem_free(busyp);
  2610. }