xfs_alloc.c 77 KB

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