xfs_alloc.c 78 KB

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