xfs_rtalloc.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  1. /*
  2. * Copyright (c) 2000-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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_bmap.h"
  30. #include "xfs_bmap_util.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trans.h"
  35. #include "xfs_trans_space.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_buf.h"
  38. #include "xfs_icache.h"
  39. #include "xfs_dinode.h"
  40. /*
  41. * Prototypes for internal functions.
  42. */
  43. STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  44. xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
  45. STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
  46. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
  47. STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  48. xfs_extlen_t, int, xfs_rtblock_t *, int *);
  49. STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  50. xfs_rtblock_t, xfs_rtblock_t *);
  51. STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  52. xfs_rtblock_t, xfs_rtblock_t *);
  53. STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
  54. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
  55. STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  56. xfs_extlen_t, int);
  57. STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
  58. xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
  59. /*
  60. * Internal functions.
  61. */
  62. /*
  63. * Allocate space to the bitmap or summary file, and zero it, for growfs.
  64. */
  65. STATIC int /* error */
  66. xfs_growfs_rt_alloc(
  67. xfs_mount_t *mp, /* file system mount point */
  68. xfs_extlen_t oblocks, /* old count of blocks */
  69. xfs_extlen_t nblocks, /* new count of blocks */
  70. xfs_inode_t *ip) /* inode (bitmap/summary) */
  71. {
  72. xfs_fileoff_t bno; /* block number in file */
  73. xfs_buf_t *bp; /* temporary buffer for zeroing */
  74. int committed; /* transaction committed flag */
  75. xfs_daddr_t d; /* disk block address */
  76. int error; /* error return value */
  77. xfs_fsblock_t firstblock; /* first block allocated in xaction */
  78. xfs_bmap_free_t flist; /* list of freed blocks */
  79. xfs_fsblock_t fsbno; /* filesystem block for bno */
  80. xfs_bmbt_irec_t map; /* block map output */
  81. int nmap; /* number of block maps */
  82. int resblks; /* space reservation */
  83. /*
  84. * Allocate space to the file, as necessary.
  85. */
  86. while (oblocks < nblocks) {
  87. int cancelflags = 0;
  88. xfs_trans_t *tp;
  89. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
  90. resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
  91. /*
  92. * Reserve space & log for one extent added to the file.
  93. */
  94. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growdata,
  95. resblks, 0);
  96. if (error)
  97. goto error_cancel;
  98. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  99. /*
  100. * Lock the inode.
  101. */
  102. xfs_ilock(ip, XFS_ILOCK_EXCL);
  103. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  104. xfs_bmap_init(&flist, &firstblock);
  105. /*
  106. * Allocate blocks to the bitmap file.
  107. */
  108. nmap = 1;
  109. cancelflags |= XFS_TRANS_ABORT;
  110. error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
  111. XFS_BMAPI_METADATA, &firstblock,
  112. resblks, &map, &nmap, &flist);
  113. if (!error && nmap < 1)
  114. error = XFS_ERROR(ENOSPC);
  115. if (error)
  116. goto error_cancel;
  117. /*
  118. * Free any blocks freed up in the transaction, then commit.
  119. */
  120. error = xfs_bmap_finish(&tp, &flist, &committed);
  121. if (error)
  122. goto error_cancel;
  123. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  124. if (error)
  125. goto error;
  126. /*
  127. * Now we need to clear the allocated blocks.
  128. * Do this one block per transaction, to keep it simple.
  129. */
  130. cancelflags = 0;
  131. for (bno = map.br_startoff, fsbno = map.br_startblock;
  132. bno < map.br_startoff + map.br_blockcount;
  133. bno++, fsbno++) {
  134. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
  135. /*
  136. * Reserve log for one block zeroing.
  137. */
  138. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
  139. 0, 0);
  140. if (error)
  141. goto error_cancel;
  142. /*
  143. * Lock the bitmap inode.
  144. */
  145. xfs_ilock(ip, XFS_ILOCK_EXCL);
  146. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  147. /*
  148. * Get a buffer for the block.
  149. */
  150. d = XFS_FSB_TO_DADDR(mp, fsbno);
  151. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  152. mp->m_bsize, 0);
  153. if (bp == NULL) {
  154. error = XFS_ERROR(EIO);
  155. error_cancel:
  156. xfs_trans_cancel(tp, cancelflags);
  157. goto error;
  158. }
  159. memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
  160. xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
  161. /*
  162. * Commit the transaction.
  163. */
  164. error = xfs_trans_commit(tp, 0);
  165. if (error)
  166. goto error;
  167. }
  168. /*
  169. * Go on to the next extent, if any.
  170. */
  171. oblocks = map.br_startoff + map.br_blockcount;
  172. }
  173. return 0;
  174. error:
  175. return error;
  176. }
  177. /*
  178. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  179. * bitmap block bbno. If we don't get maxlen then use prod to trim
  180. * the length, if given. Returns error; returns starting block in *rtblock.
  181. * The lengths are all in rtextents.
  182. */
  183. STATIC int /* error */
  184. xfs_rtallocate_extent_block(
  185. xfs_mount_t *mp, /* file system mount point */
  186. xfs_trans_t *tp, /* transaction pointer */
  187. xfs_rtblock_t bbno, /* bitmap block number */
  188. xfs_extlen_t minlen, /* minimum length to allocate */
  189. xfs_extlen_t maxlen, /* maximum length to allocate */
  190. xfs_extlen_t *len, /* out: actual length allocated */
  191. xfs_rtblock_t *nextp, /* out: next block to try */
  192. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  193. xfs_fsblock_t *rsb, /* in/out: summary block number */
  194. xfs_extlen_t prod, /* extent product factor */
  195. xfs_rtblock_t *rtblock) /* out: start block allocated */
  196. {
  197. xfs_rtblock_t besti; /* best rtblock found so far */
  198. xfs_rtblock_t bestlen; /* best length found so far */
  199. xfs_rtblock_t end; /* last rtblock in chunk */
  200. int error; /* error value */
  201. xfs_rtblock_t i; /* current rtblock trying */
  202. xfs_rtblock_t next; /* next rtblock to try */
  203. int stat; /* status from internal calls */
  204. /*
  205. * Loop over all the extents starting in this bitmap block,
  206. * looking for one that's long enough.
  207. */
  208. for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
  209. end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
  210. i <= end;
  211. i++) {
  212. /*
  213. * See if there's a free extent of maxlen starting at i.
  214. * If it's not so then next will contain the first non-free.
  215. */
  216. error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
  217. if (error) {
  218. return error;
  219. }
  220. if (stat) {
  221. /*
  222. * i for maxlen is all free, allocate and return that.
  223. */
  224. error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
  225. rsb);
  226. if (error) {
  227. return error;
  228. }
  229. *len = maxlen;
  230. *rtblock = i;
  231. return 0;
  232. }
  233. /*
  234. * In the case where we have a variable-sized allocation
  235. * request, figure out how big this free piece is,
  236. * and if it's big enough for the minimum, and the best
  237. * so far, remember it.
  238. */
  239. if (minlen < maxlen) {
  240. xfs_rtblock_t thislen; /* this extent size */
  241. thislen = next - i;
  242. if (thislen >= minlen && thislen > bestlen) {
  243. besti = i;
  244. bestlen = thislen;
  245. }
  246. }
  247. /*
  248. * If not done yet, find the start of the next free space.
  249. */
  250. if (next < end) {
  251. error = xfs_rtfind_forw(mp, tp, next, end, &i);
  252. if (error) {
  253. return error;
  254. }
  255. } else
  256. break;
  257. }
  258. /*
  259. * Searched the whole thing & didn't find a maxlen free extent.
  260. */
  261. if (minlen < maxlen && besti != -1) {
  262. xfs_extlen_t p; /* amount to trim length by */
  263. /*
  264. * If size should be a multiple of prod, make that so.
  265. */
  266. if (prod > 1 && (p = do_mod(bestlen, prod)))
  267. bestlen -= p;
  268. /*
  269. * Allocate besti for bestlen & return that.
  270. */
  271. error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
  272. if (error) {
  273. return error;
  274. }
  275. *len = bestlen;
  276. *rtblock = besti;
  277. return 0;
  278. }
  279. /*
  280. * Allocation failed. Set *nextp to the next block to try.
  281. */
  282. *nextp = next;
  283. *rtblock = NULLRTBLOCK;
  284. return 0;
  285. }
  286. /*
  287. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  288. * bno. If we don't get maxlen then use prod to trim the length, if given.
  289. * Returns error; returns starting block in *rtblock.
  290. * The lengths are all in rtextents.
  291. */
  292. STATIC int /* error */
  293. xfs_rtallocate_extent_exact(
  294. xfs_mount_t *mp, /* file system mount point */
  295. xfs_trans_t *tp, /* transaction pointer */
  296. xfs_rtblock_t bno, /* starting block number to allocate */
  297. xfs_extlen_t minlen, /* minimum length to allocate */
  298. xfs_extlen_t maxlen, /* maximum length to allocate */
  299. xfs_extlen_t *len, /* out: actual length allocated */
  300. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  301. xfs_fsblock_t *rsb, /* in/out: summary block number */
  302. xfs_extlen_t prod, /* extent product factor */
  303. xfs_rtblock_t *rtblock) /* out: start block allocated */
  304. {
  305. int error; /* error value */
  306. xfs_extlen_t i; /* extent length trimmed due to prod */
  307. int isfree; /* extent is free */
  308. xfs_rtblock_t next; /* next block to try (dummy) */
  309. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  310. /*
  311. * Check if the range in question (for maxlen) is free.
  312. */
  313. error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
  314. if (error) {
  315. return error;
  316. }
  317. if (isfree) {
  318. /*
  319. * If it is, allocate it and return success.
  320. */
  321. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  322. if (error) {
  323. return error;
  324. }
  325. *len = maxlen;
  326. *rtblock = bno;
  327. return 0;
  328. }
  329. /*
  330. * If not, allocate what there is, if it's at least minlen.
  331. */
  332. maxlen = next - bno;
  333. if (maxlen < minlen) {
  334. /*
  335. * Failed, return failure status.
  336. */
  337. *rtblock = NULLRTBLOCK;
  338. return 0;
  339. }
  340. /*
  341. * Trim off tail of extent, if prod is specified.
  342. */
  343. if (prod > 1 && (i = maxlen % prod)) {
  344. maxlen -= i;
  345. if (maxlen < minlen) {
  346. /*
  347. * Now we can't do it, return failure status.
  348. */
  349. *rtblock = NULLRTBLOCK;
  350. return 0;
  351. }
  352. }
  353. /*
  354. * Allocate what we can and return it.
  355. */
  356. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  357. if (error) {
  358. return error;
  359. }
  360. *len = maxlen;
  361. *rtblock = bno;
  362. return 0;
  363. }
  364. /*
  365. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  366. * to bno as possible. If we don't get maxlen then use prod to trim
  367. * the length, if given. The lengths are all in rtextents.
  368. */
  369. STATIC int /* error */
  370. xfs_rtallocate_extent_near(
  371. xfs_mount_t *mp, /* file system mount point */
  372. xfs_trans_t *tp, /* transaction pointer */
  373. xfs_rtblock_t bno, /* starting block number to allocate */
  374. xfs_extlen_t minlen, /* minimum length to allocate */
  375. xfs_extlen_t maxlen, /* maximum length to allocate */
  376. xfs_extlen_t *len, /* out: actual length allocated */
  377. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  378. xfs_fsblock_t *rsb, /* in/out: summary block number */
  379. xfs_extlen_t prod, /* extent product factor */
  380. xfs_rtblock_t *rtblock) /* out: start block allocated */
  381. {
  382. int any; /* any useful extents from summary */
  383. xfs_rtblock_t bbno; /* bitmap block number */
  384. int error; /* error value */
  385. int i; /* bitmap block offset (loop control) */
  386. int j; /* secondary loop control */
  387. int log2len; /* log2 of minlen */
  388. xfs_rtblock_t n; /* next block to try */
  389. xfs_rtblock_t r; /* result block */
  390. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  391. /*
  392. * If the block number given is off the end, silently set it to
  393. * the last block.
  394. */
  395. if (bno >= mp->m_sb.sb_rextents)
  396. bno = mp->m_sb.sb_rextents - 1;
  397. /*
  398. * Try the exact allocation first.
  399. */
  400. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
  401. rbpp, rsb, prod, &r);
  402. if (error) {
  403. return error;
  404. }
  405. /*
  406. * If the exact allocation worked, return that.
  407. */
  408. if (r != NULLRTBLOCK) {
  409. *rtblock = r;
  410. return 0;
  411. }
  412. bbno = XFS_BITTOBLOCK(mp, bno);
  413. i = 0;
  414. ASSERT(minlen != 0);
  415. log2len = xfs_highbit32(minlen);
  416. /*
  417. * Loop over all bitmap blocks (bbno + i is current block).
  418. */
  419. for (;;) {
  420. /*
  421. * Get summary information of extents of all useful levels
  422. * starting in this bitmap block.
  423. */
  424. error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
  425. bbno + i, rbpp, rsb, &any);
  426. if (error) {
  427. return error;
  428. }
  429. /*
  430. * If there are any useful extents starting here, try
  431. * allocating one.
  432. */
  433. if (any) {
  434. /*
  435. * On the positive side of the starting location.
  436. */
  437. if (i >= 0) {
  438. /*
  439. * Try to allocate an extent starting in
  440. * this block.
  441. */
  442. error = xfs_rtallocate_extent_block(mp, tp,
  443. bbno + i, minlen, maxlen, len, &n, rbpp,
  444. rsb, prod, &r);
  445. if (error) {
  446. return error;
  447. }
  448. /*
  449. * If it worked, return it.
  450. */
  451. if (r != NULLRTBLOCK) {
  452. *rtblock = r;
  453. return 0;
  454. }
  455. }
  456. /*
  457. * On the negative side of the starting location.
  458. */
  459. else { /* i < 0 */
  460. /*
  461. * Loop backwards through the bitmap blocks from
  462. * the starting point-1 up to where we are now.
  463. * There should be an extent which ends in this
  464. * bitmap block and is long enough.
  465. */
  466. for (j = -1; j > i; j--) {
  467. /*
  468. * Grab the summary information for
  469. * this bitmap block.
  470. */
  471. error = xfs_rtany_summary(mp, tp,
  472. log2len, mp->m_rsumlevels - 1,
  473. bbno + j, rbpp, rsb, &any);
  474. if (error) {
  475. return error;
  476. }
  477. /*
  478. * If there's no extent given in the
  479. * summary that means the extent we
  480. * found must carry over from an
  481. * earlier block. If there is an
  482. * extent given, we've already tried
  483. * that allocation, don't do it again.
  484. */
  485. if (any)
  486. continue;
  487. error = xfs_rtallocate_extent_block(mp,
  488. tp, bbno + j, minlen, maxlen,
  489. len, &n, rbpp, rsb, prod, &r);
  490. if (error) {
  491. return error;
  492. }
  493. /*
  494. * If it works, return the extent.
  495. */
  496. if (r != NULLRTBLOCK) {
  497. *rtblock = r;
  498. return 0;
  499. }
  500. }
  501. /*
  502. * There weren't intervening bitmap blocks
  503. * with a long enough extent, or the
  504. * allocation didn't work for some reason
  505. * (i.e. it's a little * too short).
  506. * Try to allocate from the summary block
  507. * that we found.
  508. */
  509. error = xfs_rtallocate_extent_block(mp, tp,
  510. bbno + i, minlen, maxlen, len, &n, rbpp,
  511. rsb, prod, &r);
  512. if (error) {
  513. return error;
  514. }
  515. /*
  516. * If it works, return the extent.
  517. */
  518. if (r != NULLRTBLOCK) {
  519. *rtblock = r;
  520. return 0;
  521. }
  522. }
  523. }
  524. /*
  525. * Loop control. If we were on the positive side, and there's
  526. * still more blocks on the negative side, go there.
  527. */
  528. if (i > 0 && (int)bbno - i >= 0)
  529. i = -i;
  530. /*
  531. * If positive, and no more negative, but there are more
  532. * positive, go there.
  533. */
  534. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  535. i++;
  536. /*
  537. * If negative or 0 (just started), and there are positive
  538. * blocks to go, go there. The 0 case moves to block 1.
  539. */
  540. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  541. i = 1 - i;
  542. /*
  543. * If negative or 0 and there are more negative blocks,
  544. * go there.
  545. */
  546. else if (i <= 0 && (int)bbno + i > 0)
  547. i--;
  548. /*
  549. * Must be done. Return failure.
  550. */
  551. else
  552. break;
  553. }
  554. *rtblock = NULLRTBLOCK;
  555. return 0;
  556. }
  557. /*
  558. * Allocate an extent of length minlen<=len<=maxlen, with no position
  559. * specified. If we don't get maxlen then use prod to trim
  560. * the length, if given. The lengths are all in rtextents.
  561. */
  562. STATIC int /* error */
  563. xfs_rtallocate_extent_size(
  564. xfs_mount_t *mp, /* file system mount point */
  565. xfs_trans_t *tp, /* transaction pointer */
  566. xfs_extlen_t minlen, /* minimum length to allocate */
  567. xfs_extlen_t maxlen, /* maximum length to allocate */
  568. xfs_extlen_t *len, /* out: actual length allocated */
  569. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  570. xfs_fsblock_t *rsb, /* in/out: summary block number */
  571. xfs_extlen_t prod, /* extent product factor */
  572. xfs_rtblock_t *rtblock) /* out: start block allocated */
  573. {
  574. int error; /* error value */
  575. int i; /* bitmap block number */
  576. int l; /* level number (loop control) */
  577. xfs_rtblock_t n; /* next block to be tried */
  578. xfs_rtblock_t r; /* result block number */
  579. xfs_suminfo_t sum; /* summary information for extents */
  580. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  581. ASSERT(maxlen != 0);
  582. /*
  583. * Loop over all the levels starting with maxlen.
  584. * At each level, look at all the bitmap blocks, to see if there
  585. * are extents starting there that are long enough (>= maxlen).
  586. * Note, only on the initial level can the allocation fail if
  587. * the summary says there's an extent.
  588. */
  589. for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
  590. /*
  591. * Loop over all the bitmap blocks.
  592. */
  593. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  594. /*
  595. * Get the summary for this level/block.
  596. */
  597. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  598. &sum);
  599. if (error) {
  600. return error;
  601. }
  602. /*
  603. * Nothing there, on to the next block.
  604. */
  605. if (!sum)
  606. continue;
  607. /*
  608. * Try allocating the extent.
  609. */
  610. error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
  611. maxlen, len, &n, rbpp, rsb, prod, &r);
  612. if (error) {
  613. return error;
  614. }
  615. /*
  616. * If it worked, return that.
  617. */
  618. if (r != NULLRTBLOCK) {
  619. *rtblock = r;
  620. return 0;
  621. }
  622. /*
  623. * If the "next block to try" returned from the
  624. * allocator is beyond the next bitmap block,
  625. * skip to that bitmap block.
  626. */
  627. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  628. i = XFS_BITTOBLOCK(mp, n) - 1;
  629. }
  630. }
  631. /*
  632. * Didn't find any maxlen blocks. Try smaller ones, unless
  633. * we're asking for a fixed size extent.
  634. */
  635. if (minlen > --maxlen) {
  636. *rtblock = NULLRTBLOCK;
  637. return 0;
  638. }
  639. ASSERT(minlen != 0);
  640. ASSERT(maxlen != 0);
  641. /*
  642. * Loop over sizes, from maxlen down to minlen.
  643. * This time, when we do the allocations, allow smaller ones
  644. * to succeed.
  645. */
  646. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  647. /*
  648. * Loop over all the bitmap blocks, try an allocation
  649. * starting in that block.
  650. */
  651. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  652. /*
  653. * Get the summary information for this level/block.
  654. */
  655. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  656. &sum);
  657. if (error) {
  658. return error;
  659. }
  660. /*
  661. * If nothing there, go on to next.
  662. */
  663. if (!sum)
  664. continue;
  665. /*
  666. * Try the allocation. Make sure the specified
  667. * minlen/maxlen are in the possible range for
  668. * this summary level.
  669. */
  670. error = xfs_rtallocate_extent_block(mp, tp, i,
  671. XFS_RTMAX(minlen, 1 << l),
  672. XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
  673. len, &n, rbpp, rsb, prod, &r);
  674. if (error) {
  675. return error;
  676. }
  677. /*
  678. * If it worked, return that extent.
  679. */
  680. if (r != NULLRTBLOCK) {
  681. *rtblock = r;
  682. return 0;
  683. }
  684. /*
  685. * If the "next block to try" returned from the
  686. * allocator is beyond the next bitmap block,
  687. * skip to that bitmap block.
  688. */
  689. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  690. i = XFS_BITTOBLOCK(mp, n) - 1;
  691. }
  692. }
  693. /*
  694. * Got nothing, return failure.
  695. */
  696. *rtblock = NULLRTBLOCK;
  697. return 0;
  698. }
  699. /*
  700. * Mark an extent specified by start and len allocated.
  701. * Updates all the summary information as well as the bitmap.
  702. */
  703. STATIC int /* error */
  704. xfs_rtallocate_range(
  705. xfs_mount_t *mp, /* file system mount point */
  706. xfs_trans_t *tp, /* transaction pointer */
  707. xfs_rtblock_t start, /* start block to allocate */
  708. xfs_extlen_t len, /* length to allocate */
  709. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  710. xfs_fsblock_t *rsb) /* in/out: summary block number */
  711. {
  712. xfs_rtblock_t end; /* end of the allocated extent */
  713. int error; /* error value */
  714. xfs_rtblock_t postblock = 0; /* first block allocated > end */
  715. xfs_rtblock_t preblock = 0; /* first block allocated < start */
  716. end = start + len - 1;
  717. /*
  718. * Assume we're allocating out of the middle of a free extent.
  719. * We need to find the beginning and end of the extent so we can
  720. * properly update the summary.
  721. */
  722. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  723. if (error) {
  724. return error;
  725. }
  726. /*
  727. * Find the next allocated block (end of free extent).
  728. */
  729. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  730. &postblock);
  731. if (error) {
  732. return error;
  733. }
  734. /*
  735. * Decrement the summary information corresponding to the entire
  736. * (old) free extent.
  737. */
  738. error = xfs_rtmodify_summary(mp, tp,
  739. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  740. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  741. if (error) {
  742. return error;
  743. }
  744. /*
  745. * If there are blocks not being allocated at the front of the
  746. * old extent, add summary data for them to be free.
  747. */
  748. if (preblock < start) {
  749. error = xfs_rtmodify_summary(mp, tp,
  750. XFS_RTBLOCKLOG(start - preblock),
  751. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  752. if (error) {
  753. return error;
  754. }
  755. }
  756. /*
  757. * If there are blocks not being allocated at the end of the
  758. * old extent, add summary data for them to be free.
  759. */
  760. if (postblock > end) {
  761. error = xfs_rtmodify_summary(mp, tp,
  762. XFS_RTBLOCKLOG(postblock - end),
  763. XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
  764. if (error) {
  765. return error;
  766. }
  767. }
  768. /*
  769. * Modify the bitmap to mark this extent allocated.
  770. */
  771. error = xfs_rtmodify_range(mp, tp, start, len, 0);
  772. return error;
  773. }
  774. /*
  775. * Return whether there are any free extents in the size range given
  776. * by low and high, for the bitmap block bbno.
  777. */
  778. STATIC int /* error */
  779. xfs_rtany_summary(
  780. xfs_mount_t *mp, /* file system mount structure */
  781. xfs_trans_t *tp, /* transaction pointer */
  782. int low, /* low log2 extent size */
  783. int high, /* high log2 extent size */
  784. xfs_rtblock_t bbno, /* bitmap block number */
  785. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  786. xfs_fsblock_t *rsb, /* in/out: summary block number */
  787. int *stat) /* out: any good extents here? */
  788. {
  789. int error; /* error value */
  790. int log; /* loop counter, log2 of ext. size */
  791. xfs_suminfo_t sum; /* summary data */
  792. /*
  793. * Loop over logs of extent sizes. Order is irrelevant.
  794. */
  795. for (log = low; log <= high; log++) {
  796. /*
  797. * Get one summary datum.
  798. */
  799. error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
  800. if (error) {
  801. return error;
  802. }
  803. /*
  804. * If there are any, return success.
  805. */
  806. if (sum) {
  807. *stat = 1;
  808. return 0;
  809. }
  810. }
  811. /*
  812. * Found nothing, return failure.
  813. */
  814. *stat = 0;
  815. return 0;
  816. }
  817. /*
  818. * Get a buffer for the bitmap or summary file block specified.
  819. * The buffer is returned read and locked.
  820. */
  821. STATIC int /* error */
  822. xfs_rtbuf_get(
  823. xfs_mount_t *mp, /* file system mount structure */
  824. xfs_trans_t *tp, /* transaction pointer */
  825. xfs_rtblock_t block, /* block number in bitmap or summary */
  826. int issum, /* is summary not bitmap */
  827. xfs_buf_t **bpp) /* output: buffer for the block */
  828. {
  829. xfs_buf_t *bp; /* block buffer, result */
  830. xfs_inode_t *ip; /* bitmap or summary inode */
  831. xfs_bmbt_irec_t map;
  832. int nmap = 1;
  833. int error; /* error value */
  834. ip = issum ? mp->m_rsumip : mp->m_rbmip;
  835. error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
  836. if (error)
  837. return error;
  838. ASSERT(map.br_startblock != NULLFSBLOCK);
  839. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  840. XFS_FSB_TO_DADDR(mp, map.br_startblock),
  841. mp->m_bsize, 0, &bp, NULL);
  842. if (error)
  843. return error;
  844. ASSERT(!xfs_buf_geterror(bp));
  845. *bpp = bp;
  846. return 0;
  847. }
  848. #ifdef DEBUG
  849. /*
  850. * Check that the given extent (block range) is allocated already.
  851. */
  852. STATIC int /* error */
  853. xfs_rtcheck_alloc_range(
  854. xfs_mount_t *mp, /* file system mount point */
  855. xfs_trans_t *tp, /* transaction pointer */
  856. xfs_rtblock_t bno, /* starting block number of extent */
  857. xfs_extlen_t len, /* length of extent */
  858. int *stat) /* out: 1 for allocated, 0 for not */
  859. {
  860. xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
  861. return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
  862. }
  863. #endif
  864. /*
  865. * Check that the given range is either all allocated (val = 0) or
  866. * all free (val = 1).
  867. */
  868. STATIC int /* error */
  869. xfs_rtcheck_range(
  870. xfs_mount_t *mp, /* file system mount point */
  871. xfs_trans_t *tp, /* transaction pointer */
  872. xfs_rtblock_t start, /* starting block number of extent */
  873. xfs_extlen_t len, /* length of extent */
  874. int val, /* 1 for free, 0 for allocated */
  875. xfs_rtblock_t *new, /* out: first block not matching */
  876. int *stat) /* out: 1 for matches, 0 for not */
  877. {
  878. xfs_rtword_t *b; /* current word in buffer */
  879. int bit; /* bit number in the word */
  880. xfs_rtblock_t block; /* bitmap block number */
  881. xfs_buf_t *bp; /* buf for the block */
  882. xfs_rtword_t *bufp; /* starting word in buffer */
  883. int error; /* error value */
  884. xfs_rtblock_t i; /* current bit number rel. to start */
  885. xfs_rtblock_t lastbit; /* last useful bit in word */
  886. xfs_rtword_t mask; /* mask of relevant bits for value */
  887. xfs_rtword_t wdiff; /* difference from wanted value */
  888. int word; /* word number in the buffer */
  889. /*
  890. * Compute starting bitmap block number
  891. */
  892. block = XFS_BITTOBLOCK(mp, start);
  893. /*
  894. * Read the bitmap block.
  895. */
  896. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  897. if (error) {
  898. return error;
  899. }
  900. bufp = bp->b_addr;
  901. /*
  902. * Compute the starting word's address, and starting bit.
  903. */
  904. word = XFS_BITTOWORD(mp, start);
  905. b = &bufp[word];
  906. bit = (int)(start & (XFS_NBWORD - 1));
  907. /*
  908. * 0 (allocated) => all zero's; 1 (free) => all one's.
  909. */
  910. val = -val;
  911. /*
  912. * If not starting on a word boundary, deal with the first
  913. * (partial) word.
  914. */
  915. if (bit) {
  916. /*
  917. * Compute first bit not examined.
  918. */
  919. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  920. /*
  921. * Mask of relevant bits.
  922. */
  923. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  924. /*
  925. * Compute difference between actual and desired value.
  926. */
  927. if ((wdiff = (*b ^ val) & mask)) {
  928. /*
  929. * Different, compute first wrong bit and return.
  930. */
  931. xfs_trans_brelse(tp, bp);
  932. i = XFS_RTLOBIT(wdiff) - bit;
  933. *new = start + i;
  934. *stat = 0;
  935. return 0;
  936. }
  937. i = lastbit - bit;
  938. /*
  939. * Go on to next block if that's where the next word is
  940. * and we need the next word.
  941. */
  942. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  943. /*
  944. * If done with this block, get the next one.
  945. */
  946. xfs_trans_brelse(tp, bp);
  947. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  948. if (error) {
  949. return error;
  950. }
  951. b = bufp = bp->b_addr;
  952. word = 0;
  953. } else {
  954. /*
  955. * Go on to the next word in the buffer.
  956. */
  957. b++;
  958. }
  959. } else {
  960. /*
  961. * Starting on a word boundary, no partial word.
  962. */
  963. i = 0;
  964. }
  965. /*
  966. * Loop over whole words in buffers. When we use up one buffer
  967. * we move on to the next one.
  968. */
  969. while (len - i >= XFS_NBWORD) {
  970. /*
  971. * Compute difference between actual and desired value.
  972. */
  973. if ((wdiff = *b ^ val)) {
  974. /*
  975. * Different, compute first wrong bit and return.
  976. */
  977. xfs_trans_brelse(tp, bp);
  978. i += XFS_RTLOBIT(wdiff);
  979. *new = start + i;
  980. *stat = 0;
  981. return 0;
  982. }
  983. i += XFS_NBWORD;
  984. /*
  985. * Go on to next block if that's where the next word is
  986. * and we need the next word.
  987. */
  988. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  989. /*
  990. * If done with this block, get the next one.
  991. */
  992. xfs_trans_brelse(tp, bp);
  993. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  994. if (error) {
  995. return error;
  996. }
  997. b = bufp = bp->b_addr;
  998. word = 0;
  999. } else {
  1000. /*
  1001. * Go on to the next word in the buffer.
  1002. */
  1003. b++;
  1004. }
  1005. }
  1006. /*
  1007. * If not ending on a word boundary, deal with the last
  1008. * (partial) word.
  1009. */
  1010. if ((lastbit = len - i)) {
  1011. /*
  1012. * Mask of relevant bits.
  1013. */
  1014. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1015. /*
  1016. * Compute difference between actual and desired value.
  1017. */
  1018. if ((wdiff = (*b ^ val) & mask)) {
  1019. /*
  1020. * Different, compute first wrong bit and return.
  1021. */
  1022. xfs_trans_brelse(tp, bp);
  1023. i += XFS_RTLOBIT(wdiff);
  1024. *new = start + i;
  1025. *stat = 0;
  1026. return 0;
  1027. } else
  1028. i = len;
  1029. }
  1030. /*
  1031. * Successful, return.
  1032. */
  1033. xfs_trans_brelse(tp, bp);
  1034. *new = start + i;
  1035. *stat = 1;
  1036. return 0;
  1037. }
  1038. /*
  1039. * Copy and transform the summary file, given the old and new
  1040. * parameters in the mount structures.
  1041. */
  1042. STATIC int /* error */
  1043. xfs_rtcopy_summary(
  1044. xfs_mount_t *omp, /* old file system mount point */
  1045. xfs_mount_t *nmp, /* new file system mount point */
  1046. xfs_trans_t *tp) /* transaction pointer */
  1047. {
  1048. xfs_rtblock_t bbno; /* bitmap block number */
  1049. xfs_buf_t *bp; /* summary buffer */
  1050. int error; /* error return value */
  1051. int log; /* summary level number (log length) */
  1052. xfs_suminfo_t sum; /* summary data */
  1053. xfs_fsblock_t sumbno; /* summary block number */
  1054. bp = NULL;
  1055. for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
  1056. for (bbno = omp->m_sb.sb_rbmblocks - 1;
  1057. (xfs_srtblock_t)bbno >= 0;
  1058. bbno--) {
  1059. error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
  1060. &sumbno, &sum);
  1061. if (error)
  1062. return error;
  1063. if (sum == 0)
  1064. continue;
  1065. error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
  1066. &bp, &sumbno);
  1067. if (error)
  1068. return error;
  1069. error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
  1070. &bp, &sumbno);
  1071. if (error)
  1072. return error;
  1073. ASSERT(sum > 0);
  1074. }
  1075. }
  1076. return 0;
  1077. }
  1078. /*
  1079. * Searching backward from start to limit, find the first block whose
  1080. * allocated/free state is different from start's.
  1081. */
  1082. STATIC int /* error */
  1083. xfs_rtfind_back(
  1084. xfs_mount_t *mp, /* file system mount point */
  1085. xfs_trans_t *tp, /* transaction pointer */
  1086. xfs_rtblock_t start, /* starting block to look at */
  1087. xfs_rtblock_t limit, /* last block to look at */
  1088. xfs_rtblock_t *rtblock) /* out: start block found */
  1089. {
  1090. xfs_rtword_t *b; /* current word in buffer */
  1091. int bit; /* bit number in the word */
  1092. xfs_rtblock_t block; /* bitmap block number */
  1093. xfs_buf_t *bp; /* buf for the block */
  1094. xfs_rtword_t *bufp; /* starting word in buffer */
  1095. int error; /* error value */
  1096. xfs_rtblock_t firstbit; /* first useful bit in the word */
  1097. xfs_rtblock_t i; /* current bit number rel. to start */
  1098. xfs_rtblock_t len; /* length of inspected area */
  1099. xfs_rtword_t mask; /* mask of relevant bits for value */
  1100. xfs_rtword_t want; /* mask for "good" values */
  1101. xfs_rtword_t wdiff; /* difference from wanted value */
  1102. int word; /* word number in the buffer */
  1103. /*
  1104. * Compute and read in starting bitmap block for starting block.
  1105. */
  1106. block = XFS_BITTOBLOCK(mp, start);
  1107. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1108. if (error) {
  1109. return error;
  1110. }
  1111. bufp = bp->b_addr;
  1112. /*
  1113. * Get the first word's index & point to it.
  1114. */
  1115. word = XFS_BITTOWORD(mp, start);
  1116. b = &bufp[word];
  1117. bit = (int)(start & (XFS_NBWORD - 1));
  1118. len = start - limit + 1;
  1119. /*
  1120. * Compute match value, based on the bit at start: if 1 (free)
  1121. * then all-ones, else all-zeroes.
  1122. */
  1123. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1124. /*
  1125. * If the starting position is not word-aligned, deal with the
  1126. * partial word.
  1127. */
  1128. if (bit < XFS_NBWORD - 1) {
  1129. /*
  1130. * Calculate first (leftmost) bit number to look at,
  1131. * and mask for all the relevant bits in this word.
  1132. */
  1133. firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
  1134. mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
  1135. firstbit;
  1136. /*
  1137. * Calculate the difference between the value there
  1138. * and what we're looking for.
  1139. */
  1140. if ((wdiff = (*b ^ want) & mask)) {
  1141. /*
  1142. * Different. Mark where we are and return.
  1143. */
  1144. xfs_trans_brelse(tp, bp);
  1145. i = bit - XFS_RTHIBIT(wdiff);
  1146. *rtblock = start - i + 1;
  1147. return 0;
  1148. }
  1149. i = bit - firstbit + 1;
  1150. /*
  1151. * Go on to previous block if that's where the previous word is
  1152. * and we need the previous word.
  1153. */
  1154. if (--word == -1 && i < len) {
  1155. /*
  1156. * If done with this block, get the previous one.
  1157. */
  1158. xfs_trans_brelse(tp, bp);
  1159. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1160. if (error) {
  1161. return error;
  1162. }
  1163. bufp = bp->b_addr;
  1164. word = XFS_BLOCKWMASK(mp);
  1165. b = &bufp[word];
  1166. } else {
  1167. /*
  1168. * Go on to the previous word in the buffer.
  1169. */
  1170. b--;
  1171. }
  1172. } else {
  1173. /*
  1174. * Starting on a word boundary, no partial word.
  1175. */
  1176. i = 0;
  1177. }
  1178. /*
  1179. * Loop over whole words in buffers. When we use up one buffer
  1180. * we move on to the previous one.
  1181. */
  1182. while (len - i >= XFS_NBWORD) {
  1183. /*
  1184. * Compute difference between actual and desired value.
  1185. */
  1186. if ((wdiff = *b ^ want)) {
  1187. /*
  1188. * Different, mark where we are and return.
  1189. */
  1190. xfs_trans_brelse(tp, bp);
  1191. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1192. *rtblock = start - i + 1;
  1193. return 0;
  1194. }
  1195. i += XFS_NBWORD;
  1196. /*
  1197. * Go on to previous block if that's where the previous word is
  1198. * and we need the previous word.
  1199. */
  1200. if (--word == -1 && i < len) {
  1201. /*
  1202. * If done with this block, get the previous one.
  1203. */
  1204. xfs_trans_brelse(tp, bp);
  1205. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1206. if (error) {
  1207. return error;
  1208. }
  1209. bufp = bp->b_addr;
  1210. word = XFS_BLOCKWMASK(mp);
  1211. b = &bufp[word];
  1212. } else {
  1213. /*
  1214. * Go on to the previous word in the buffer.
  1215. */
  1216. b--;
  1217. }
  1218. }
  1219. /*
  1220. * If not ending on a word boundary, deal with the last
  1221. * (partial) word.
  1222. */
  1223. if (len - i) {
  1224. /*
  1225. * Calculate first (leftmost) bit number to look at,
  1226. * and mask for all the relevant bits in this word.
  1227. */
  1228. firstbit = XFS_NBWORD - (len - i);
  1229. mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
  1230. /*
  1231. * Compute difference between actual and desired value.
  1232. */
  1233. if ((wdiff = (*b ^ want) & mask)) {
  1234. /*
  1235. * Different, mark where we are and return.
  1236. */
  1237. xfs_trans_brelse(tp, bp);
  1238. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1239. *rtblock = start - i + 1;
  1240. return 0;
  1241. } else
  1242. i = len;
  1243. }
  1244. /*
  1245. * No match, return that we scanned the whole area.
  1246. */
  1247. xfs_trans_brelse(tp, bp);
  1248. *rtblock = start - i + 1;
  1249. return 0;
  1250. }
  1251. /*
  1252. * Searching forward from start to limit, find the first block whose
  1253. * allocated/free state is different from start's.
  1254. */
  1255. STATIC int /* error */
  1256. xfs_rtfind_forw(
  1257. xfs_mount_t *mp, /* file system mount point */
  1258. xfs_trans_t *tp, /* transaction pointer */
  1259. xfs_rtblock_t start, /* starting block to look at */
  1260. xfs_rtblock_t limit, /* last block to look at */
  1261. xfs_rtblock_t *rtblock) /* out: start block found */
  1262. {
  1263. xfs_rtword_t *b; /* current word in buffer */
  1264. int bit; /* bit number in the word */
  1265. xfs_rtblock_t block; /* bitmap block number */
  1266. xfs_buf_t *bp; /* buf for the block */
  1267. xfs_rtword_t *bufp; /* starting word in buffer */
  1268. int error; /* error value */
  1269. xfs_rtblock_t i; /* current bit number rel. to start */
  1270. xfs_rtblock_t lastbit; /* last useful bit in the word */
  1271. xfs_rtblock_t len; /* length of inspected area */
  1272. xfs_rtword_t mask; /* mask of relevant bits for value */
  1273. xfs_rtword_t want; /* mask for "good" values */
  1274. xfs_rtword_t wdiff; /* difference from wanted value */
  1275. int word; /* word number in the buffer */
  1276. /*
  1277. * Compute and read in starting bitmap block for starting block.
  1278. */
  1279. block = XFS_BITTOBLOCK(mp, start);
  1280. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1281. if (error) {
  1282. return error;
  1283. }
  1284. bufp = bp->b_addr;
  1285. /*
  1286. * Get the first word's index & point to it.
  1287. */
  1288. word = XFS_BITTOWORD(mp, start);
  1289. b = &bufp[word];
  1290. bit = (int)(start & (XFS_NBWORD - 1));
  1291. len = limit - start + 1;
  1292. /*
  1293. * Compute match value, based on the bit at start: if 1 (free)
  1294. * then all-ones, else all-zeroes.
  1295. */
  1296. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1297. /*
  1298. * If the starting position is not word-aligned, deal with the
  1299. * partial word.
  1300. */
  1301. if (bit) {
  1302. /*
  1303. * Calculate last (rightmost) bit number to look at,
  1304. * and mask for all the relevant bits in this word.
  1305. */
  1306. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1307. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1308. /*
  1309. * Calculate the difference between the value there
  1310. * and what we're looking for.
  1311. */
  1312. if ((wdiff = (*b ^ want) & mask)) {
  1313. /*
  1314. * Different. Mark where we are and return.
  1315. */
  1316. xfs_trans_brelse(tp, bp);
  1317. i = XFS_RTLOBIT(wdiff) - bit;
  1318. *rtblock = start + i - 1;
  1319. return 0;
  1320. }
  1321. i = lastbit - bit;
  1322. /*
  1323. * Go on to next block if that's where the next word is
  1324. * and we need the next word.
  1325. */
  1326. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1327. /*
  1328. * If done with this block, get the previous one.
  1329. */
  1330. xfs_trans_brelse(tp, bp);
  1331. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1332. if (error) {
  1333. return error;
  1334. }
  1335. b = bufp = bp->b_addr;
  1336. word = 0;
  1337. } else {
  1338. /*
  1339. * Go on to the previous word in the buffer.
  1340. */
  1341. b++;
  1342. }
  1343. } else {
  1344. /*
  1345. * Starting on a word boundary, no partial word.
  1346. */
  1347. i = 0;
  1348. }
  1349. /*
  1350. * Loop over whole words in buffers. When we use up one buffer
  1351. * we move on to the next one.
  1352. */
  1353. while (len - i >= XFS_NBWORD) {
  1354. /*
  1355. * Compute difference between actual and desired value.
  1356. */
  1357. if ((wdiff = *b ^ want)) {
  1358. /*
  1359. * Different, mark where we are and return.
  1360. */
  1361. xfs_trans_brelse(tp, bp);
  1362. i += XFS_RTLOBIT(wdiff);
  1363. *rtblock = start + i - 1;
  1364. return 0;
  1365. }
  1366. i += XFS_NBWORD;
  1367. /*
  1368. * Go on to next block if that's where the next word is
  1369. * and we need the next word.
  1370. */
  1371. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1372. /*
  1373. * If done with this block, get the next one.
  1374. */
  1375. xfs_trans_brelse(tp, bp);
  1376. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1377. if (error) {
  1378. return error;
  1379. }
  1380. b = bufp = bp->b_addr;
  1381. word = 0;
  1382. } else {
  1383. /*
  1384. * Go on to the next word in the buffer.
  1385. */
  1386. b++;
  1387. }
  1388. }
  1389. /*
  1390. * If not ending on a word boundary, deal with the last
  1391. * (partial) word.
  1392. */
  1393. if ((lastbit = len - i)) {
  1394. /*
  1395. * Calculate mask for all the relevant bits in this word.
  1396. */
  1397. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1398. /*
  1399. * Compute difference between actual and desired value.
  1400. */
  1401. if ((wdiff = (*b ^ want) & mask)) {
  1402. /*
  1403. * Different, mark where we are and return.
  1404. */
  1405. xfs_trans_brelse(tp, bp);
  1406. i += XFS_RTLOBIT(wdiff);
  1407. *rtblock = start + i - 1;
  1408. return 0;
  1409. } else
  1410. i = len;
  1411. }
  1412. /*
  1413. * No match, return that we scanned the whole area.
  1414. */
  1415. xfs_trans_brelse(tp, bp);
  1416. *rtblock = start + i - 1;
  1417. return 0;
  1418. }
  1419. /*
  1420. * Mark an extent specified by start and len freed.
  1421. * Updates all the summary information as well as the bitmap.
  1422. */
  1423. STATIC int /* error */
  1424. xfs_rtfree_range(
  1425. xfs_mount_t *mp, /* file system mount point */
  1426. xfs_trans_t *tp, /* transaction pointer */
  1427. xfs_rtblock_t start, /* starting block to free */
  1428. xfs_extlen_t len, /* length to free */
  1429. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1430. xfs_fsblock_t *rsb) /* in/out: summary block number */
  1431. {
  1432. xfs_rtblock_t end; /* end of the freed extent */
  1433. int error; /* error value */
  1434. xfs_rtblock_t postblock; /* first block freed > end */
  1435. xfs_rtblock_t preblock; /* first block freed < start */
  1436. end = start + len - 1;
  1437. /*
  1438. * Modify the bitmap to mark this extent freed.
  1439. */
  1440. error = xfs_rtmodify_range(mp, tp, start, len, 1);
  1441. if (error) {
  1442. return error;
  1443. }
  1444. /*
  1445. * Assume we're freeing out of the middle of an allocated extent.
  1446. * We need to find the beginning and end of the extent so we can
  1447. * properly update the summary.
  1448. */
  1449. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  1450. if (error) {
  1451. return error;
  1452. }
  1453. /*
  1454. * Find the next allocated block (end of allocated extent).
  1455. */
  1456. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  1457. &postblock);
  1458. if (error)
  1459. return error;
  1460. /*
  1461. * If there are blocks not being freed at the front of the
  1462. * old extent, add summary data for them to be allocated.
  1463. */
  1464. if (preblock < start) {
  1465. error = xfs_rtmodify_summary(mp, tp,
  1466. XFS_RTBLOCKLOG(start - preblock),
  1467. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  1468. if (error) {
  1469. return error;
  1470. }
  1471. }
  1472. /*
  1473. * If there are blocks not being freed at the end of the
  1474. * old extent, add summary data for them to be allocated.
  1475. */
  1476. if (postblock > end) {
  1477. error = xfs_rtmodify_summary(mp, tp,
  1478. XFS_RTBLOCKLOG(postblock - end),
  1479. XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
  1480. if (error) {
  1481. return error;
  1482. }
  1483. }
  1484. /*
  1485. * Increment the summary information corresponding to the entire
  1486. * (new) free extent.
  1487. */
  1488. error = xfs_rtmodify_summary(mp, tp,
  1489. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  1490. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  1491. return error;
  1492. }
  1493. /*
  1494. * Read and return the summary information for a given extent size,
  1495. * bitmap block combination.
  1496. * Keeps track of a current summary block, so we don't keep reading
  1497. * it from the buffer cache.
  1498. */
  1499. STATIC int /* error */
  1500. xfs_rtget_summary(
  1501. xfs_mount_t *mp, /* file system mount structure */
  1502. xfs_trans_t *tp, /* transaction pointer */
  1503. int log, /* log2 of extent size */
  1504. xfs_rtblock_t bbno, /* bitmap block number */
  1505. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1506. xfs_fsblock_t *rsb, /* in/out: summary block number */
  1507. xfs_suminfo_t *sum) /* out: summary info for this block */
  1508. {
  1509. xfs_buf_t *bp; /* buffer for summary block */
  1510. int error; /* error value */
  1511. xfs_fsblock_t sb; /* summary fsblock */
  1512. int so; /* index into the summary file */
  1513. xfs_suminfo_t *sp; /* pointer to returned data */
  1514. /*
  1515. * Compute entry number in the summary file.
  1516. */
  1517. so = XFS_SUMOFFS(mp, log, bbno);
  1518. /*
  1519. * Compute the block number in the summary file.
  1520. */
  1521. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  1522. /*
  1523. * If we have an old buffer, and the block number matches, use that.
  1524. */
  1525. if (rbpp && *rbpp && *rsb == sb)
  1526. bp = *rbpp;
  1527. /*
  1528. * Otherwise we have to get the buffer.
  1529. */
  1530. else {
  1531. /*
  1532. * If there was an old one, get rid of it first.
  1533. */
  1534. if (rbpp && *rbpp)
  1535. xfs_trans_brelse(tp, *rbpp);
  1536. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  1537. if (error) {
  1538. return error;
  1539. }
  1540. /*
  1541. * Remember this buffer and block for the next call.
  1542. */
  1543. if (rbpp) {
  1544. *rbpp = bp;
  1545. *rsb = sb;
  1546. }
  1547. }
  1548. /*
  1549. * Point to the summary information & copy it out.
  1550. */
  1551. sp = XFS_SUMPTR(mp, bp, so);
  1552. *sum = *sp;
  1553. /*
  1554. * Drop the buffer if we're not asked to remember it.
  1555. */
  1556. if (!rbpp)
  1557. xfs_trans_brelse(tp, bp);
  1558. return 0;
  1559. }
  1560. /*
  1561. * Set the given range of bitmap bits to the given value.
  1562. * Do whatever I/O and logging is required.
  1563. */
  1564. STATIC int /* error */
  1565. xfs_rtmodify_range(
  1566. xfs_mount_t *mp, /* file system mount point */
  1567. xfs_trans_t *tp, /* transaction pointer */
  1568. xfs_rtblock_t start, /* starting block to modify */
  1569. xfs_extlen_t len, /* length of extent to modify */
  1570. int val) /* 1 for free, 0 for allocated */
  1571. {
  1572. xfs_rtword_t *b; /* current word in buffer */
  1573. int bit; /* bit number in the word */
  1574. xfs_rtblock_t block; /* bitmap block number */
  1575. xfs_buf_t *bp; /* buf for the block */
  1576. xfs_rtword_t *bufp; /* starting word in buffer */
  1577. int error; /* error value */
  1578. xfs_rtword_t *first; /* first used word in the buffer */
  1579. int i; /* current bit number rel. to start */
  1580. int lastbit; /* last useful bit in word */
  1581. xfs_rtword_t mask; /* mask o frelevant bits for value */
  1582. int word; /* word number in the buffer */
  1583. /*
  1584. * Compute starting bitmap block number.
  1585. */
  1586. block = XFS_BITTOBLOCK(mp, start);
  1587. /*
  1588. * Read the bitmap block, and point to its data.
  1589. */
  1590. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1591. if (error) {
  1592. return error;
  1593. }
  1594. bufp = bp->b_addr;
  1595. /*
  1596. * Compute the starting word's address, and starting bit.
  1597. */
  1598. word = XFS_BITTOWORD(mp, start);
  1599. first = b = &bufp[word];
  1600. bit = (int)(start & (XFS_NBWORD - 1));
  1601. /*
  1602. * 0 (allocated) => all zeroes; 1 (free) => all ones.
  1603. */
  1604. val = -val;
  1605. /*
  1606. * If not starting on a word boundary, deal with the first
  1607. * (partial) word.
  1608. */
  1609. if (bit) {
  1610. /*
  1611. * Compute first bit not changed and mask of relevant bits.
  1612. */
  1613. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1614. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1615. /*
  1616. * Set/clear the active bits.
  1617. */
  1618. if (val)
  1619. *b |= mask;
  1620. else
  1621. *b &= ~mask;
  1622. i = lastbit - bit;
  1623. /*
  1624. * Go on to the next block if that's where the next word is
  1625. * and we need the next word.
  1626. */
  1627. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1628. /*
  1629. * Log the changed part of this block.
  1630. * Get the next one.
  1631. */
  1632. xfs_trans_log_buf(tp, bp,
  1633. (uint)((char *)first - (char *)bufp),
  1634. (uint)((char *)b - (char *)bufp));
  1635. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1636. if (error) {
  1637. return error;
  1638. }
  1639. first = b = bufp = bp->b_addr;
  1640. word = 0;
  1641. } else {
  1642. /*
  1643. * Go on to the next word in the buffer
  1644. */
  1645. b++;
  1646. }
  1647. } else {
  1648. /*
  1649. * Starting on a word boundary, no partial word.
  1650. */
  1651. i = 0;
  1652. }
  1653. /*
  1654. * Loop over whole words in buffers. When we use up one buffer
  1655. * we move on to the next one.
  1656. */
  1657. while (len - i >= XFS_NBWORD) {
  1658. /*
  1659. * Set the word value correctly.
  1660. */
  1661. *b = val;
  1662. i += XFS_NBWORD;
  1663. /*
  1664. * Go on to the next block if that's where the next word is
  1665. * and we need the next word.
  1666. */
  1667. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1668. /*
  1669. * Log the changed part of this block.
  1670. * Get the next one.
  1671. */
  1672. xfs_trans_log_buf(tp, bp,
  1673. (uint)((char *)first - (char *)bufp),
  1674. (uint)((char *)b - (char *)bufp));
  1675. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1676. if (error) {
  1677. return error;
  1678. }
  1679. first = b = bufp = bp->b_addr;
  1680. word = 0;
  1681. } else {
  1682. /*
  1683. * Go on to the next word in the buffer
  1684. */
  1685. b++;
  1686. }
  1687. }
  1688. /*
  1689. * If not ending on a word boundary, deal with the last
  1690. * (partial) word.
  1691. */
  1692. if ((lastbit = len - i)) {
  1693. /*
  1694. * Compute a mask of relevant bits.
  1695. */
  1696. bit = 0;
  1697. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1698. /*
  1699. * Set/clear the active bits.
  1700. */
  1701. if (val)
  1702. *b |= mask;
  1703. else
  1704. *b &= ~mask;
  1705. b++;
  1706. }
  1707. /*
  1708. * Log any remaining changed bytes.
  1709. */
  1710. if (b > first)
  1711. xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
  1712. (uint)((char *)b - (char *)bufp - 1));
  1713. return 0;
  1714. }
  1715. /*
  1716. * Read and modify the summary information for a given extent size,
  1717. * bitmap block combination.
  1718. * Keeps track of a current summary block, so we don't keep reading
  1719. * it from the buffer cache.
  1720. */
  1721. STATIC int /* error */
  1722. xfs_rtmodify_summary(
  1723. xfs_mount_t *mp, /* file system mount point */
  1724. xfs_trans_t *tp, /* transaction pointer */
  1725. int log, /* log2 of extent size */
  1726. xfs_rtblock_t bbno, /* bitmap block number */
  1727. int delta, /* change to make to summary info */
  1728. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1729. xfs_fsblock_t *rsb) /* in/out: summary block number */
  1730. {
  1731. xfs_buf_t *bp; /* buffer for the summary block */
  1732. int error; /* error value */
  1733. xfs_fsblock_t sb; /* summary fsblock */
  1734. int so; /* index into the summary file */
  1735. xfs_suminfo_t *sp; /* pointer to returned data */
  1736. /*
  1737. * Compute entry number in the summary file.
  1738. */
  1739. so = XFS_SUMOFFS(mp, log, bbno);
  1740. /*
  1741. * Compute the block number in the summary file.
  1742. */
  1743. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  1744. /*
  1745. * If we have an old buffer, and the block number matches, use that.
  1746. */
  1747. if (rbpp && *rbpp && *rsb == sb)
  1748. bp = *rbpp;
  1749. /*
  1750. * Otherwise we have to get the buffer.
  1751. */
  1752. else {
  1753. /*
  1754. * If there was an old one, get rid of it first.
  1755. */
  1756. if (rbpp && *rbpp)
  1757. xfs_trans_brelse(tp, *rbpp);
  1758. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  1759. if (error) {
  1760. return error;
  1761. }
  1762. /*
  1763. * Remember this buffer and block for the next call.
  1764. */
  1765. if (rbpp) {
  1766. *rbpp = bp;
  1767. *rsb = sb;
  1768. }
  1769. }
  1770. /*
  1771. * Point to the summary information, modify and log it.
  1772. */
  1773. sp = XFS_SUMPTR(mp, bp, so);
  1774. *sp += delta;
  1775. xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)bp->b_addr),
  1776. (uint)((char *)sp - (char *)bp->b_addr + sizeof(*sp) - 1));
  1777. return 0;
  1778. }
  1779. /*
  1780. * Visible (exported) functions.
  1781. */
  1782. /*
  1783. * Grow the realtime area of the filesystem.
  1784. */
  1785. int
  1786. xfs_growfs_rt(
  1787. xfs_mount_t *mp, /* mount point for filesystem */
  1788. xfs_growfs_rt_t *in) /* growfs rt input struct */
  1789. {
  1790. xfs_rtblock_t bmbno; /* bitmap block number */
  1791. xfs_buf_t *bp; /* temporary buffer */
  1792. int error; /* error return value */
  1793. xfs_mount_t *nmp; /* new (fake) mount structure */
  1794. xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
  1795. xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
  1796. xfs_drtbno_t nrextents; /* new number of realtime extents */
  1797. uint8_t nrextslog; /* new log2 of sb_rextents */
  1798. xfs_extlen_t nrsumblocks; /* new number of summary blocks */
  1799. uint nrsumlevels; /* new rt summary levels */
  1800. uint nrsumsize; /* new size of rt summary, bytes */
  1801. xfs_sb_t *nsbp; /* new superblock */
  1802. xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
  1803. xfs_extlen_t rsumblocks; /* current number of rt summary blks */
  1804. xfs_sb_t *sbp; /* old superblock */
  1805. xfs_fsblock_t sumbno; /* summary block number */
  1806. sbp = &mp->m_sb;
  1807. /*
  1808. * Initial error checking.
  1809. */
  1810. if (!capable(CAP_SYS_ADMIN))
  1811. return XFS_ERROR(EPERM);
  1812. if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
  1813. (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
  1814. (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
  1815. return XFS_ERROR(EINVAL);
  1816. if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
  1817. return error;
  1818. /*
  1819. * Read in the last block of the device, make sure it exists.
  1820. */
  1821. bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
  1822. XFS_FSB_TO_BB(mp, nrblocks - 1),
  1823. XFS_FSB_TO_BB(mp, 1), 0, NULL);
  1824. if (!bp)
  1825. return EIO;
  1826. if (bp->b_error) {
  1827. error = bp->b_error;
  1828. xfs_buf_relse(bp);
  1829. return error;
  1830. }
  1831. xfs_buf_relse(bp);
  1832. /*
  1833. * Calculate new parameters. These are the final values to be reached.
  1834. */
  1835. nrextents = nrblocks;
  1836. do_div(nrextents, in->extsize);
  1837. nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
  1838. nrextslog = xfs_highbit32(nrextents);
  1839. nrsumlevels = nrextslog + 1;
  1840. nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
  1841. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  1842. nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  1843. /*
  1844. * New summary size can't be more than half the size of
  1845. * the log. This prevents us from getting a log overflow,
  1846. * since we'll log basically the whole summary file at once.
  1847. */
  1848. if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
  1849. return XFS_ERROR(EINVAL);
  1850. /*
  1851. * Get the old block counts for bitmap and summary inodes.
  1852. * These can't change since other growfs callers are locked out.
  1853. */
  1854. rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
  1855. rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
  1856. /*
  1857. * Allocate space to the bitmap and summary files, as necessary.
  1858. */
  1859. error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
  1860. if (error)
  1861. return error;
  1862. error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
  1863. if (error)
  1864. return error;
  1865. /*
  1866. * Allocate a new (fake) mount/sb.
  1867. */
  1868. nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
  1869. /*
  1870. * Loop over the bitmap blocks.
  1871. * We will do everything one bitmap block at a time.
  1872. * Skip the current block if it is exactly full.
  1873. * This also deals with the case where there were no rtextents before.
  1874. */
  1875. for (bmbno = sbp->sb_rbmblocks -
  1876. ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
  1877. bmbno < nrbmblocks;
  1878. bmbno++) {
  1879. xfs_trans_t *tp;
  1880. int cancelflags = 0;
  1881. *nmp = *mp;
  1882. nsbp = &nmp->m_sb;
  1883. /*
  1884. * Calculate new sb and mount fields for this round.
  1885. */
  1886. nsbp->sb_rextsize = in->extsize;
  1887. nsbp->sb_rbmblocks = bmbno + 1;
  1888. nsbp->sb_rblocks =
  1889. XFS_RTMIN(nrblocks,
  1890. nsbp->sb_rbmblocks * NBBY *
  1891. nsbp->sb_blocksize * nsbp->sb_rextsize);
  1892. nsbp->sb_rextents = nsbp->sb_rblocks;
  1893. do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
  1894. ASSERT(nsbp->sb_rextents != 0);
  1895. nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
  1896. nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
  1897. nrsumsize =
  1898. (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
  1899. nsbp->sb_rbmblocks;
  1900. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  1901. nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  1902. /*
  1903. * Start a transaction, get the log reservation.
  1904. */
  1905. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
  1906. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtfree,
  1907. 0, 0);
  1908. if (error)
  1909. goto error_cancel;
  1910. /*
  1911. * Lock out other callers by grabbing the bitmap inode lock.
  1912. */
  1913. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  1914. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  1915. /*
  1916. * Update the bitmap inode's size.
  1917. */
  1918. mp->m_rbmip->i_d.di_size =
  1919. nsbp->sb_rbmblocks * nsbp->sb_blocksize;
  1920. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1921. cancelflags |= XFS_TRANS_ABORT;
  1922. /*
  1923. * Get the summary inode into the transaction.
  1924. */
  1925. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
  1926. xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  1927. /*
  1928. * Update the summary inode's size.
  1929. */
  1930. mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
  1931. xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
  1932. /*
  1933. * Copy summary data from old to new sizes.
  1934. * Do this when the real size (not block-aligned) changes.
  1935. */
  1936. if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
  1937. mp->m_rsumlevels != nmp->m_rsumlevels) {
  1938. error = xfs_rtcopy_summary(mp, nmp, tp);
  1939. if (error)
  1940. goto error_cancel;
  1941. }
  1942. /*
  1943. * Update superblock fields.
  1944. */
  1945. if (nsbp->sb_rextsize != sbp->sb_rextsize)
  1946. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
  1947. nsbp->sb_rextsize - sbp->sb_rextsize);
  1948. if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
  1949. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
  1950. nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
  1951. if (nsbp->sb_rblocks != sbp->sb_rblocks)
  1952. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
  1953. nsbp->sb_rblocks - sbp->sb_rblocks);
  1954. if (nsbp->sb_rextents != sbp->sb_rextents)
  1955. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
  1956. nsbp->sb_rextents - sbp->sb_rextents);
  1957. if (nsbp->sb_rextslog != sbp->sb_rextslog)
  1958. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
  1959. nsbp->sb_rextslog - sbp->sb_rextslog);
  1960. /*
  1961. * Free new extent.
  1962. */
  1963. bp = NULL;
  1964. error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
  1965. nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
  1966. if (error) {
  1967. error_cancel:
  1968. xfs_trans_cancel(tp, cancelflags);
  1969. break;
  1970. }
  1971. /*
  1972. * Mark more blocks free in the superblock.
  1973. */
  1974. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
  1975. nsbp->sb_rextents - sbp->sb_rextents);
  1976. /*
  1977. * Update mp values into the real mp structure.
  1978. */
  1979. mp->m_rsumlevels = nrsumlevels;
  1980. mp->m_rsumsize = nrsumsize;
  1981. error = xfs_trans_commit(tp, 0);
  1982. if (error)
  1983. break;
  1984. }
  1985. /*
  1986. * Free the fake mp structure.
  1987. */
  1988. kmem_free(nmp);
  1989. return error;
  1990. }
  1991. /*
  1992. * Allocate an extent in the realtime subvolume, with the usual allocation
  1993. * parameters. The length units are all in realtime extents, as is the
  1994. * result block number.
  1995. */
  1996. int /* error */
  1997. xfs_rtallocate_extent(
  1998. xfs_trans_t *tp, /* transaction pointer */
  1999. xfs_rtblock_t bno, /* starting block number to allocate */
  2000. xfs_extlen_t minlen, /* minimum length to allocate */
  2001. xfs_extlen_t maxlen, /* maximum length to allocate */
  2002. xfs_extlen_t *len, /* out: actual length allocated */
  2003. xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
  2004. int wasdel, /* was a delayed allocation extent */
  2005. xfs_extlen_t prod, /* extent product factor */
  2006. xfs_rtblock_t *rtblock) /* out: start block allocated */
  2007. {
  2008. xfs_mount_t *mp = tp->t_mountp;
  2009. int error; /* error value */
  2010. xfs_rtblock_t r; /* result allocated block */
  2011. xfs_fsblock_t sb; /* summary file block number */
  2012. xfs_buf_t *sumbp; /* summary file block buffer */
  2013. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  2014. ASSERT(minlen > 0 && minlen <= maxlen);
  2015. /*
  2016. * If prod is set then figure out what to do to minlen and maxlen.
  2017. */
  2018. if (prod > 1) {
  2019. xfs_extlen_t i;
  2020. if ((i = maxlen % prod))
  2021. maxlen -= i;
  2022. if ((i = minlen % prod))
  2023. minlen += prod - i;
  2024. if (maxlen < minlen) {
  2025. *rtblock = NULLRTBLOCK;
  2026. return 0;
  2027. }
  2028. }
  2029. sumbp = NULL;
  2030. /*
  2031. * Allocate by size, or near another block, or exactly at some block.
  2032. */
  2033. switch (type) {
  2034. case XFS_ALLOCTYPE_ANY_AG:
  2035. error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
  2036. &sumbp, &sb, prod, &r);
  2037. break;
  2038. case XFS_ALLOCTYPE_NEAR_BNO:
  2039. error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
  2040. len, &sumbp, &sb, prod, &r);
  2041. break;
  2042. case XFS_ALLOCTYPE_THIS_BNO:
  2043. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
  2044. len, &sumbp, &sb, prod, &r);
  2045. break;
  2046. default:
  2047. error = EIO;
  2048. ASSERT(0);
  2049. }
  2050. if (error)
  2051. return error;
  2052. /*
  2053. * If it worked, update the superblock.
  2054. */
  2055. if (r != NULLRTBLOCK) {
  2056. long slen = (long)*len;
  2057. ASSERT(*len >= minlen && *len <= maxlen);
  2058. if (wasdel)
  2059. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
  2060. else
  2061. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
  2062. }
  2063. *rtblock = r;
  2064. return 0;
  2065. }
  2066. /*
  2067. * Free an extent in the realtime subvolume. Length is expressed in
  2068. * realtime extents, as is the block number.
  2069. */
  2070. int /* error */
  2071. xfs_rtfree_extent(
  2072. xfs_trans_t *tp, /* transaction pointer */
  2073. xfs_rtblock_t bno, /* starting block number to free */
  2074. xfs_extlen_t len) /* length of extent freed */
  2075. {
  2076. int error; /* error value */
  2077. xfs_mount_t *mp; /* file system mount structure */
  2078. xfs_fsblock_t sb; /* summary file block number */
  2079. xfs_buf_t *sumbp; /* summary file block buffer */
  2080. mp = tp->t_mountp;
  2081. ASSERT(mp->m_rbmip->i_itemp != NULL);
  2082. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  2083. #ifdef DEBUG
  2084. /*
  2085. * Check to see that this whole range is currently allocated.
  2086. */
  2087. {
  2088. int stat; /* result from checking range */
  2089. error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
  2090. if (error) {
  2091. return error;
  2092. }
  2093. ASSERT(stat);
  2094. }
  2095. #endif
  2096. sumbp = NULL;
  2097. /*
  2098. * Free the range of realtime blocks.
  2099. */
  2100. error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
  2101. if (error) {
  2102. return error;
  2103. }
  2104. /*
  2105. * Mark more blocks free in the superblock.
  2106. */
  2107. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
  2108. /*
  2109. * If we've now freed all the blocks, reset the file sequence
  2110. * number to 0.
  2111. */
  2112. if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
  2113. mp->m_sb.sb_rextents) {
  2114. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
  2115. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  2116. *(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
  2117. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  2118. }
  2119. return 0;
  2120. }
  2121. /*
  2122. * Initialize realtime fields in the mount structure.
  2123. */
  2124. int /* error */
  2125. xfs_rtmount_init(
  2126. xfs_mount_t *mp) /* file system mount structure */
  2127. {
  2128. xfs_buf_t *bp; /* buffer for last block of subvolume */
  2129. xfs_daddr_t d; /* address of last block of subvolume */
  2130. xfs_sb_t *sbp; /* filesystem superblock copy in mount */
  2131. sbp = &mp->m_sb;
  2132. if (sbp->sb_rblocks == 0)
  2133. return 0;
  2134. if (mp->m_rtdev_targp == NULL) {
  2135. xfs_warn(mp,
  2136. "Filesystem has a realtime volume, use rtdev=device option");
  2137. return XFS_ERROR(ENODEV);
  2138. }
  2139. mp->m_rsumlevels = sbp->sb_rextslog + 1;
  2140. mp->m_rsumsize =
  2141. (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
  2142. sbp->sb_rbmblocks;
  2143. mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
  2144. mp->m_rbmip = mp->m_rsumip = NULL;
  2145. /*
  2146. * Check that the realtime section is an ok size.
  2147. */
  2148. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  2149. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
  2150. xfs_warn(mp, "realtime mount -- %llu != %llu",
  2151. (unsigned long long) XFS_BB_TO_FSB(mp, d),
  2152. (unsigned long long) mp->m_sb.sb_rblocks);
  2153. return XFS_ERROR(EFBIG);
  2154. }
  2155. bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
  2156. d - XFS_FSB_TO_BB(mp, 1),
  2157. XFS_FSB_TO_BB(mp, 1), 0, NULL);
  2158. if (!bp || bp->b_error) {
  2159. xfs_warn(mp, "realtime device size check failed");
  2160. if (bp)
  2161. xfs_buf_relse(bp);
  2162. return EIO;
  2163. }
  2164. xfs_buf_relse(bp);
  2165. return 0;
  2166. }
  2167. /*
  2168. * Get the bitmap and summary inodes into the mount structure
  2169. * at mount time.
  2170. */
  2171. int /* error */
  2172. xfs_rtmount_inodes(
  2173. xfs_mount_t *mp) /* file system mount structure */
  2174. {
  2175. int error; /* error return value */
  2176. xfs_sb_t *sbp;
  2177. sbp = &mp->m_sb;
  2178. if (sbp->sb_rbmino == NULLFSINO)
  2179. return 0;
  2180. error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
  2181. if (error)
  2182. return error;
  2183. ASSERT(mp->m_rbmip != NULL);
  2184. ASSERT(sbp->sb_rsumino != NULLFSINO);
  2185. error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
  2186. if (error) {
  2187. IRELE(mp->m_rbmip);
  2188. return error;
  2189. }
  2190. ASSERT(mp->m_rsumip != NULL);
  2191. return 0;
  2192. }
  2193. void
  2194. xfs_rtunmount_inodes(
  2195. struct xfs_mount *mp)
  2196. {
  2197. if (mp->m_rbmip)
  2198. IRELE(mp->m_rbmip);
  2199. if (mp->m_rsumip)
  2200. IRELE(mp->m_rsumip);
  2201. }
  2202. /*
  2203. * Pick an extent for allocation at the start of a new realtime file.
  2204. * Use the sequence number stored in the atime field of the bitmap inode.
  2205. * Translate this to a fraction of the rtextents, and return the product
  2206. * of rtextents and the fraction.
  2207. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  2208. */
  2209. int /* error */
  2210. xfs_rtpick_extent(
  2211. xfs_mount_t *mp, /* file system mount point */
  2212. xfs_trans_t *tp, /* transaction pointer */
  2213. xfs_extlen_t len, /* allocation length (rtextents) */
  2214. xfs_rtblock_t *pick) /* result rt extent */
  2215. {
  2216. xfs_rtblock_t b; /* result block */
  2217. int log2; /* log of sequence number */
  2218. __uint64_t resid; /* residual after log removed */
  2219. __uint64_t seq; /* sequence number of file creation */
  2220. __uint64_t *seqp; /* pointer to seqno in inode */
  2221. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  2222. seqp = (__uint64_t *)&mp->m_rbmip->i_d.di_atime;
  2223. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
  2224. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  2225. *seqp = 0;
  2226. }
  2227. seq = *seqp;
  2228. if ((log2 = xfs_highbit64(seq)) == -1)
  2229. b = 0;
  2230. else {
  2231. resid = seq - (1ULL << log2);
  2232. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  2233. (log2 + 1);
  2234. if (b >= mp->m_sb.sb_rextents)
  2235. b = do_mod(b, mp->m_sb.sb_rextents);
  2236. if (b + len > mp->m_sb.sb_rextents)
  2237. b = mp->m_sb.sb_rextents - len;
  2238. }
  2239. *seqp = seq + 1;
  2240. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  2241. *pick = b;
  2242. return 0;
  2243. }