xfs_alloc.c 78 KB

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