xfs_alloc.c 72 KB

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