xfs_rtalloc.c 61 KB

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