xfs_rtalloc.c 61 KB

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