xfs_alloc.c 74 KB

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