xfs_alloc.c 70 KB

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