xfs_alloc.c 76 KB

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