xfs_rtalloc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_bmap.h"
  30. #include "xfs_bmap_util.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trans.h"
  35. #include "xfs_trans_space.h"
  36. #include "xfs_trace.h"
  37. #include "xfs_buf.h"
  38. #include "xfs_icache.h"
  39. #include "xfs_dinode.h"
  40. #include "xfs_rtalloc.h"
  41. /*
  42. * Read and return the summary information for a given extent size,
  43. * bitmap block combination.
  44. * Keeps track of a current summary block, so we don't keep reading
  45. * it from the buffer cache.
  46. */
  47. STATIC int /* error */
  48. xfs_rtget_summary(
  49. xfs_mount_t *mp, /* file system mount structure */
  50. xfs_trans_t *tp, /* transaction pointer */
  51. int log, /* log2 of extent size */
  52. xfs_rtblock_t bbno, /* bitmap block number */
  53. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  54. xfs_fsblock_t *rsb, /* in/out: summary block number */
  55. xfs_suminfo_t *sum) /* out: summary info for this block */
  56. {
  57. xfs_buf_t *bp; /* buffer for summary block */
  58. int error; /* error value */
  59. xfs_fsblock_t sb; /* summary fsblock */
  60. int so; /* index into the summary file */
  61. xfs_suminfo_t *sp; /* pointer to returned data */
  62. /*
  63. * Compute entry number in the summary file.
  64. */
  65. so = XFS_SUMOFFS(mp, log, bbno);
  66. /*
  67. * Compute the block number in the summary file.
  68. */
  69. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  70. /*
  71. * If we have an old buffer, and the block number matches, use that.
  72. */
  73. if (rbpp && *rbpp && *rsb == sb)
  74. bp = *rbpp;
  75. /*
  76. * Otherwise we have to get the buffer.
  77. */
  78. else {
  79. /*
  80. * If there was an old one, get rid of it first.
  81. */
  82. if (rbpp && *rbpp)
  83. xfs_trans_brelse(tp, *rbpp);
  84. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  85. if (error) {
  86. return error;
  87. }
  88. /*
  89. * Remember this buffer and block for the next call.
  90. */
  91. if (rbpp) {
  92. *rbpp = bp;
  93. *rsb = sb;
  94. }
  95. }
  96. /*
  97. * Point to the summary information & copy it out.
  98. */
  99. sp = XFS_SUMPTR(mp, bp, so);
  100. *sum = *sp;
  101. /*
  102. * Drop the buffer if we're not asked to remember it.
  103. */
  104. if (!rbpp)
  105. xfs_trans_brelse(tp, bp);
  106. return 0;
  107. }
  108. /*
  109. * Return whether there are any free extents in the size range given
  110. * by low and high, for the bitmap block bbno.
  111. */
  112. STATIC int /* error */
  113. xfs_rtany_summary(
  114. xfs_mount_t *mp, /* file system mount structure */
  115. xfs_trans_t *tp, /* transaction pointer */
  116. int low, /* low log2 extent size */
  117. int high, /* high log2 extent size */
  118. xfs_rtblock_t bbno, /* bitmap block number */
  119. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  120. xfs_fsblock_t *rsb, /* in/out: summary block number */
  121. int *stat) /* out: any good extents here? */
  122. {
  123. int error; /* error value */
  124. int log; /* loop counter, log2 of ext. size */
  125. xfs_suminfo_t sum; /* summary data */
  126. /*
  127. * Loop over logs of extent sizes. Order is irrelevant.
  128. */
  129. for (log = low; log <= high; log++) {
  130. /*
  131. * Get one summary datum.
  132. */
  133. error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
  134. if (error) {
  135. return error;
  136. }
  137. /*
  138. * If there are any, return success.
  139. */
  140. if (sum) {
  141. *stat = 1;
  142. return 0;
  143. }
  144. }
  145. /*
  146. * Found nothing, return failure.
  147. */
  148. *stat = 0;
  149. return 0;
  150. }
  151. /*
  152. * Copy and transform the summary file, given the old and new
  153. * parameters in the mount structures.
  154. */
  155. STATIC int /* error */
  156. xfs_rtcopy_summary(
  157. xfs_mount_t *omp, /* old file system mount point */
  158. xfs_mount_t *nmp, /* new file system mount point */
  159. xfs_trans_t *tp) /* transaction pointer */
  160. {
  161. xfs_rtblock_t bbno; /* bitmap block number */
  162. xfs_buf_t *bp; /* summary buffer */
  163. int error; /* error return value */
  164. int log; /* summary level number (log length) */
  165. xfs_suminfo_t sum; /* summary data */
  166. xfs_fsblock_t sumbno; /* summary block number */
  167. bp = NULL;
  168. for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
  169. for (bbno = omp->m_sb.sb_rbmblocks - 1;
  170. (xfs_srtblock_t)bbno >= 0;
  171. bbno--) {
  172. error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
  173. &sumbno, &sum);
  174. if (error)
  175. return error;
  176. if (sum == 0)
  177. continue;
  178. error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
  179. &bp, &sumbno);
  180. if (error)
  181. return error;
  182. error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
  183. &bp, &sumbno);
  184. if (error)
  185. return error;
  186. ASSERT(sum > 0);
  187. }
  188. }
  189. return 0;
  190. }
  191. /*
  192. * Mark an extent specified by start and len allocated.
  193. * Updates all the summary information as well as the bitmap.
  194. */
  195. STATIC int /* error */
  196. xfs_rtallocate_range(
  197. xfs_mount_t *mp, /* file system mount point */
  198. xfs_trans_t *tp, /* transaction pointer */
  199. xfs_rtblock_t start, /* start block to allocate */
  200. xfs_extlen_t len, /* length to allocate */
  201. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  202. xfs_fsblock_t *rsb) /* in/out: summary block number */
  203. {
  204. xfs_rtblock_t end; /* end of the allocated extent */
  205. int error; /* error value */
  206. xfs_rtblock_t postblock = 0; /* first block allocated > end */
  207. xfs_rtblock_t preblock = 0; /* first block allocated < start */
  208. end = start + len - 1;
  209. /*
  210. * Assume we're allocating out of the middle of a free extent.
  211. * We need to find the beginning and end of the extent so we can
  212. * properly update the summary.
  213. */
  214. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  215. if (error) {
  216. return error;
  217. }
  218. /*
  219. * Find the next allocated block (end of free extent).
  220. */
  221. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  222. &postblock);
  223. if (error) {
  224. return error;
  225. }
  226. /*
  227. * Decrement the summary information corresponding to the entire
  228. * (old) free extent.
  229. */
  230. error = xfs_rtmodify_summary(mp, tp,
  231. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  232. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  233. if (error) {
  234. return error;
  235. }
  236. /*
  237. * If there are blocks not being allocated at the front of the
  238. * old extent, add summary data for them to be free.
  239. */
  240. if (preblock < start) {
  241. error = xfs_rtmodify_summary(mp, tp,
  242. XFS_RTBLOCKLOG(start - preblock),
  243. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  244. if (error) {
  245. return error;
  246. }
  247. }
  248. /*
  249. * If there are blocks not being allocated at the end of the
  250. * old extent, add summary data for them to be free.
  251. */
  252. if (postblock > end) {
  253. error = xfs_rtmodify_summary(mp, tp,
  254. XFS_RTBLOCKLOG(postblock - end),
  255. XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
  256. if (error) {
  257. return error;
  258. }
  259. }
  260. /*
  261. * Modify the bitmap to mark this extent allocated.
  262. */
  263. error = xfs_rtmodify_range(mp, tp, start, len, 0);
  264. return error;
  265. }
  266. /*
  267. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  268. * bitmap block bbno. If we don't get maxlen then use prod to trim
  269. * the length, if given. Returns error; returns starting block in *rtblock.
  270. * The lengths are all in rtextents.
  271. */
  272. STATIC int /* error */
  273. xfs_rtallocate_extent_block(
  274. xfs_mount_t *mp, /* file system mount point */
  275. xfs_trans_t *tp, /* transaction pointer */
  276. xfs_rtblock_t bbno, /* bitmap block number */
  277. xfs_extlen_t minlen, /* minimum length to allocate */
  278. xfs_extlen_t maxlen, /* maximum length to allocate */
  279. xfs_extlen_t *len, /* out: actual length allocated */
  280. xfs_rtblock_t *nextp, /* out: next block to try */
  281. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  282. xfs_fsblock_t *rsb, /* in/out: summary block number */
  283. xfs_extlen_t prod, /* extent product factor */
  284. xfs_rtblock_t *rtblock) /* out: start block allocated */
  285. {
  286. xfs_rtblock_t besti; /* best rtblock found so far */
  287. xfs_rtblock_t bestlen; /* best length found so far */
  288. xfs_rtblock_t end; /* last rtblock in chunk */
  289. int error; /* error value */
  290. xfs_rtblock_t i; /* current rtblock trying */
  291. xfs_rtblock_t next; /* next rtblock to try */
  292. int stat; /* status from internal calls */
  293. /*
  294. * Loop over all the extents starting in this bitmap block,
  295. * looking for one that's long enough.
  296. */
  297. for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
  298. end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
  299. i <= end;
  300. i++) {
  301. /*
  302. * See if there's a free extent of maxlen starting at i.
  303. * If it's not so then next will contain the first non-free.
  304. */
  305. error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
  306. if (error) {
  307. return error;
  308. }
  309. if (stat) {
  310. /*
  311. * i for maxlen is all free, allocate and return that.
  312. */
  313. error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
  314. rsb);
  315. if (error) {
  316. return error;
  317. }
  318. *len = maxlen;
  319. *rtblock = i;
  320. return 0;
  321. }
  322. /*
  323. * In the case where we have a variable-sized allocation
  324. * request, figure out how big this free piece is,
  325. * and if it's big enough for the minimum, and the best
  326. * so far, remember it.
  327. */
  328. if (minlen < maxlen) {
  329. xfs_rtblock_t thislen; /* this extent size */
  330. thislen = next - i;
  331. if (thislen >= minlen && thislen > bestlen) {
  332. besti = i;
  333. bestlen = thislen;
  334. }
  335. }
  336. /*
  337. * If not done yet, find the start of the next free space.
  338. */
  339. if (next < end) {
  340. error = xfs_rtfind_forw(mp, tp, next, end, &i);
  341. if (error) {
  342. return error;
  343. }
  344. } else
  345. break;
  346. }
  347. /*
  348. * Searched the whole thing & didn't find a maxlen free extent.
  349. */
  350. if (minlen < maxlen && besti != -1) {
  351. xfs_extlen_t p; /* amount to trim length by */
  352. /*
  353. * If size should be a multiple of prod, make that so.
  354. */
  355. if (prod > 1 && (p = do_mod(bestlen, prod)))
  356. bestlen -= p;
  357. /*
  358. * Allocate besti for bestlen & return that.
  359. */
  360. error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
  361. if (error) {
  362. return error;
  363. }
  364. *len = bestlen;
  365. *rtblock = besti;
  366. return 0;
  367. }
  368. /*
  369. * Allocation failed. Set *nextp to the next block to try.
  370. */
  371. *nextp = next;
  372. *rtblock = NULLRTBLOCK;
  373. return 0;
  374. }
  375. /*
  376. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  377. * bno. If we don't get maxlen then use prod to trim the length, if given.
  378. * Returns error; returns starting block in *rtblock.
  379. * The lengths are all in rtextents.
  380. */
  381. STATIC int /* error */
  382. xfs_rtallocate_extent_exact(
  383. xfs_mount_t *mp, /* file system mount point */
  384. xfs_trans_t *tp, /* transaction pointer */
  385. xfs_rtblock_t bno, /* starting block number to allocate */
  386. xfs_extlen_t minlen, /* minimum length to allocate */
  387. xfs_extlen_t maxlen, /* maximum length to allocate */
  388. xfs_extlen_t *len, /* out: actual length allocated */
  389. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  390. xfs_fsblock_t *rsb, /* in/out: summary block number */
  391. xfs_extlen_t prod, /* extent product factor */
  392. xfs_rtblock_t *rtblock) /* out: start block allocated */
  393. {
  394. int error; /* error value */
  395. xfs_extlen_t i; /* extent length trimmed due to prod */
  396. int isfree; /* extent is free */
  397. xfs_rtblock_t next; /* next block to try (dummy) */
  398. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  399. /*
  400. * Check if the range in question (for maxlen) is free.
  401. */
  402. error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
  403. if (error) {
  404. return error;
  405. }
  406. if (isfree) {
  407. /*
  408. * If it is, allocate it and return success.
  409. */
  410. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  411. if (error) {
  412. return error;
  413. }
  414. *len = maxlen;
  415. *rtblock = bno;
  416. return 0;
  417. }
  418. /*
  419. * If not, allocate what there is, if it's at least minlen.
  420. */
  421. maxlen = next - bno;
  422. if (maxlen < minlen) {
  423. /*
  424. * Failed, return failure status.
  425. */
  426. *rtblock = NULLRTBLOCK;
  427. return 0;
  428. }
  429. /*
  430. * Trim off tail of extent, if prod is specified.
  431. */
  432. if (prod > 1 && (i = maxlen % prod)) {
  433. maxlen -= i;
  434. if (maxlen < minlen) {
  435. /*
  436. * Now we can't do it, return failure status.
  437. */
  438. *rtblock = NULLRTBLOCK;
  439. return 0;
  440. }
  441. }
  442. /*
  443. * Allocate what we can and return it.
  444. */
  445. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  446. if (error) {
  447. return error;
  448. }
  449. *len = maxlen;
  450. *rtblock = bno;
  451. return 0;
  452. }
  453. /*
  454. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  455. * to bno as possible. If we don't get maxlen then use prod to trim
  456. * the length, if given. The lengths are all in rtextents.
  457. */
  458. STATIC int /* error */
  459. xfs_rtallocate_extent_near(
  460. xfs_mount_t *mp, /* file system mount point */
  461. xfs_trans_t *tp, /* transaction pointer */
  462. xfs_rtblock_t bno, /* starting block number to allocate */
  463. xfs_extlen_t minlen, /* minimum length to allocate */
  464. xfs_extlen_t maxlen, /* maximum length to allocate */
  465. xfs_extlen_t *len, /* out: actual length allocated */
  466. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  467. xfs_fsblock_t *rsb, /* in/out: summary block number */
  468. xfs_extlen_t prod, /* extent product factor */
  469. xfs_rtblock_t *rtblock) /* out: start block allocated */
  470. {
  471. int any; /* any useful extents from summary */
  472. xfs_rtblock_t bbno; /* bitmap block number */
  473. int error; /* error value */
  474. int i; /* bitmap block offset (loop control) */
  475. int j; /* secondary loop control */
  476. int log2len; /* log2 of minlen */
  477. xfs_rtblock_t n; /* next block to try */
  478. xfs_rtblock_t r; /* result block */
  479. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  480. /*
  481. * If the block number given is off the end, silently set it to
  482. * the last block.
  483. */
  484. if (bno >= mp->m_sb.sb_rextents)
  485. bno = mp->m_sb.sb_rextents - 1;
  486. /*
  487. * Try the exact allocation first.
  488. */
  489. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
  490. rbpp, rsb, prod, &r);
  491. if (error) {
  492. return error;
  493. }
  494. /*
  495. * If the exact allocation worked, return that.
  496. */
  497. if (r != NULLRTBLOCK) {
  498. *rtblock = r;
  499. return 0;
  500. }
  501. bbno = XFS_BITTOBLOCK(mp, bno);
  502. i = 0;
  503. ASSERT(minlen != 0);
  504. log2len = xfs_highbit32(minlen);
  505. /*
  506. * Loop over all bitmap blocks (bbno + i is current block).
  507. */
  508. for (;;) {
  509. /*
  510. * Get summary information of extents of all useful levels
  511. * starting in this bitmap block.
  512. */
  513. error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
  514. bbno + i, rbpp, rsb, &any);
  515. if (error) {
  516. return error;
  517. }
  518. /*
  519. * If there are any useful extents starting here, try
  520. * allocating one.
  521. */
  522. if (any) {
  523. /*
  524. * On the positive side of the starting location.
  525. */
  526. if (i >= 0) {
  527. /*
  528. * Try to allocate an extent starting in
  529. * this block.
  530. */
  531. error = xfs_rtallocate_extent_block(mp, tp,
  532. bbno + i, minlen, maxlen, len, &n, rbpp,
  533. rsb, prod, &r);
  534. if (error) {
  535. return error;
  536. }
  537. /*
  538. * If it worked, return it.
  539. */
  540. if (r != NULLRTBLOCK) {
  541. *rtblock = r;
  542. return 0;
  543. }
  544. }
  545. /*
  546. * On the negative side of the starting location.
  547. */
  548. else { /* i < 0 */
  549. /*
  550. * Loop backwards through the bitmap blocks from
  551. * the starting point-1 up to where we are now.
  552. * There should be an extent which ends in this
  553. * bitmap block and is long enough.
  554. */
  555. for (j = -1; j > i; j--) {
  556. /*
  557. * Grab the summary information for
  558. * this bitmap block.
  559. */
  560. error = xfs_rtany_summary(mp, tp,
  561. log2len, mp->m_rsumlevels - 1,
  562. bbno + j, rbpp, rsb, &any);
  563. if (error) {
  564. return error;
  565. }
  566. /*
  567. * If there's no extent given in the
  568. * summary that means the extent we
  569. * found must carry over from an
  570. * earlier block. If there is an
  571. * extent given, we've already tried
  572. * that allocation, don't do it again.
  573. */
  574. if (any)
  575. continue;
  576. error = xfs_rtallocate_extent_block(mp,
  577. tp, bbno + j, minlen, maxlen,
  578. len, &n, rbpp, rsb, prod, &r);
  579. if (error) {
  580. return error;
  581. }
  582. /*
  583. * If it works, return the extent.
  584. */
  585. if (r != NULLRTBLOCK) {
  586. *rtblock = r;
  587. return 0;
  588. }
  589. }
  590. /*
  591. * There weren't intervening bitmap blocks
  592. * with a long enough extent, or the
  593. * allocation didn't work for some reason
  594. * (i.e. it's a little * too short).
  595. * Try to allocate from the summary block
  596. * that we found.
  597. */
  598. error = xfs_rtallocate_extent_block(mp, tp,
  599. bbno + i, minlen, maxlen, len, &n, rbpp,
  600. rsb, prod, &r);
  601. if (error) {
  602. return error;
  603. }
  604. /*
  605. * If it works, return the extent.
  606. */
  607. if (r != NULLRTBLOCK) {
  608. *rtblock = r;
  609. return 0;
  610. }
  611. }
  612. }
  613. /*
  614. * Loop control. If we were on the positive side, and there's
  615. * still more blocks on the negative side, go there.
  616. */
  617. if (i > 0 && (int)bbno - i >= 0)
  618. i = -i;
  619. /*
  620. * If positive, and no more negative, but there are more
  621. * positive, go there.
  622. */
  623. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  624. i++;
  625. /*
  626. * If negative or 0 (just started), and there are positive
  627. * blocks to go, go there. The 0 case moves to block 1.
  628. */
  629. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  630. i = 1 - i;
  631. /*
  632. * If negative or 0 and there are more negative blocks,
  633. * go there.
  634. */
  635. else if (i <= 0 && (int)bbno + i > 0)
  636. i--;
  637. /*
  638. * Must be done. Return failure.
  639. */
  640. else
  641. break;
  642. }
  643. *rtblock = NULLRTBLOCK;
  644. return 0;
  645. }
  646. /*
  647. * Allocate an extent of length minlen<=len<=maxlen, with no position
  648. * specified. If we don't get maxlen then use prod to trim
  649. * the length, if given. The lengths are all in rtextents.
  650. */
  651. STATIC int /* error */
  652. xfs_rtallocate_extent_size(
  653. xfs_mount_t *mp, /* file system mount point */
  654. xfs_trans_t *tp, /* transaction pointer */
  655. xfs_extlen_t minlen, /* minimum length to allocate */
  656. xfs_extlen_t maxlen, /* maximum length to allocate */
  657. xfs_extlen_t *len, /* out: actual length allocated */
  658. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  659. xfs_fsblock_t *rsb, /* in/out: summary block number */
  660. xfs_extlen_t prod, /* extent product factor */
  661. xfs_rtblock_t *rtblock) /* out: start block allocated */
  662. {
  663. int error; /* error value */
  664. int i; /* bitmap block number */
  665. int l; /* level number (loop control) */
  666. xfs_rtblock_t n; /* next block to be tried */
  667. xfs_rtblock_t r; /* result block number */
  668. xfs_suminfo_t sum; /* summary information for extents */
  669. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  670. ASSERT(maxlen != 0);
  671. /*
  672. * Loop over all the levels starting with maxlen.
  673. * At each level, look at all the bitmap blocks, to see if there
  674. * are extents starting there that are long enough (>= maxlen).
  675. * Note, only on the initial level can the allocation fail if
  676. * the summary says there's an extent.
  677. */
  678. for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
  679. /*
  680. * Loop over all the bitmap blocks.
  681. */
  682. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  683. /*
  684. * Get the summary for this level/block.
  685. */
  686. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  687. &sum);
  688. if (error) {
  689. return error;
  690. }
  691. /*
  692. * Nothing there, on to the next block.
  693. */
  694. if (!sum)
  695. continue;
  696. /*
  697. * Try allocating the extent.
  698. */
  699. error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
  700. maxlen, len, &n, rbpp, rsb, prod, &r);
  701. if (error) {
  702. return error;
  703. }
  704. /*
  705. * If it worked, return that.
  706. */
  707. if (r != NULLRTBLOCK) {
  708. *rtblock = r;
  709. return 0;
  710. }
  711. /*
  712. * If the "next block to try" returned from the
  713. * allocator is beyond the next bitmap block,
  714. * skip to that bitmap block.
  715. */
  716. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  717. i = XFS_BITTOBLOCK(mp, n) - 1;
  718. }
  719. }
  720. /*
  721. * Didn't find any maxlen blocks. Try smaller ones, unless
  722. * we're asking for a fixed size extent.
  723. */
  724. if (minlen > --maxlen) {
  725. *rtblock = NULLRTBLOCK;
  726. return 0;
  727. }
  728. ASSERT(minlen != 0);
  729. ASSERT(maxlen != 0);
  730. /*
  731. * Loop over sizes, from maxlen down to minlen.
  732. * This time, when we do the allocations, allow smaller ones
  733. * to succeed.
  734. */
  735. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  736. /*
  737. * Loop over all the bitmap blocks, try an allocation
  738. * starting in that block.
  739. */
  740. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  741. /*
  742. * Get the summary information for this level/block.
  743. */
  744. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  745. &sum);
  746. if (error) {
  747. return error;
  748. }
  749. /*
  750. * If nothing there, go on to next.
  751. */
  752. if (!sum)
  753. continue;
  754. /*
  755. * Try the allocation. Make sure the specified
  756. * minlen/maxlen are in the possible range for
  757. * this summary level.
  758. */
  759. error = xfs_rtallocate_extent_block(mp, tp, i,
  760. XFS_RTMAX(minlen, 1 << l),
  761. XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
  762. len, &n, rbpp, rsb, prod, &r);
  763. if (error) {
  764. return error;
  765. }
  766. /*
  767. * If it worked, return that extent.
  768. */
  769. if (r != NULLRTBLOCK) {
  770. *rtblock = r;
  771. return 0;
  772. }
  773. /*
  774. * If the "next block to try" returned from the
  775. * allocator is beyond the next bitmap block,
  776. * skip to that bitmap block.
  777. */
  778. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  779. i = XFS_BITTOBLOCK(mp, n) - 1;
  780. }
  781. }
  782. /*
  783. * Got nothing, return failure.
  784. */
  785. *rtblock = NULLRTBLOCK;
  786. return 0;
  787. }
  788. /*
  789. * Allocate space to the bitmap or summary file, and zero it, for growfs.
  790. */
  791. STATIC int /* error */
  792. xfs_growfs_rt_alloc(
  793. xfs_mount_t *mp, /* file system mount point */
  794. xfs_extlen_t oblocks, /* old count of blocks */
  795. xfs_extlen_t nblocks, /* new count of blocks */
  796. xfs_inode_t *ip) /* inode (bitmap/summary) */
  797. {
  798. xfs_fileoff_t bno; /* block number in file */
  799. xfs_buf_t *bp; /* temporary buffer for zeroing */
  800. int committed; /* transaction committed flag */
  801. xfs_daddr_t d; /* disk block address */
  802. int error; /* error return value */
  803. xfs_fsblock_t firstblock; /* first block allocated in xaction */
  804. xfs_bmap_free_t flist; /* list of freed blocks */
  805. xfs_fsblock_t fsbno; /* filesystem block for bno */
  806. xfs_bmbt_irec_t map; /* block map output */
  807. int nmap; /* number of block maps */
  808. int resblks; /* space reservation */
  809. /*
  810. * Allocate space to the file, as necessary.
  811. */
  812. while (oblocks < nblocks) {
  813. int cancelflags = 0;
  814. xfs_trans_t *tp;
  815. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
  816. resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
  817. /*
  818. * Reserve space & log for one extent added to the file.
  819. */
  820. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growdata,
  821. resblks, 0);
  822. if (error)
  823. goto error_cancel;
  824. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  825. /*
  826. * Lock the inode.
  827. */
  828. xfs_ilock(ip, XFS_ILOCK_EXCL);
  829. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  830. xfs_bmap_init(&flist, &firstblock);
  831. /*
  832. * Allocate blocks to the bitmap file.
  833. */
  834. nmap = 1;
  835. cancelflags |= XFS_TRANS_ABORT;
  836. error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
  837. XFS_BMAPI_METADATA, &firstblock,
  838. resblks, &map, &nmap, &flist);
  839. if (!error && nmap < 1)
  840. error = XFS_ERROR(ENOSPC);
  841. if (error)
  842. goto error_cancel;
  843. /*
  844. * Free any blocks freed up in the transaction, then commit.
  845. */
  846. error = xfs_bmap_finish(&tp, &flist, &committed);
  847. if (error)
  848. goto error_cancel;
  849. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  850. if (error)
  851. goto error;
  852. /*
  853. * Now we need to clear the allocated blocks.
  854. * Do this one block per transaction, to keep it simple.
  855. */
  856. cancelflags = 0;
  857. for (bno = map.br_startoff, fsbno = map.br_startblock;
  858. bno < map.br_startoff + map.br_blockcount;
  859. bno++, fsbno++) {
  860. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
  861. /*
  862. * Reserve log for one block zeroing.
  863. */
  864. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
  865. 0, 0);
  866. if (error)
  867. goto error_cancel;
  868. /*
  869. * Lock the bitmap inode.
  870. */
  871. xfs_ilock(ip, XFS_ILOCK_EXCL);
  872. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  873. /*
  874. * Get a buffer for the block.
  875. */
  876. d = XFS_FSB_TO_DADDR(mp, fsbno);
  877. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  878. mp->m_bsize, 0);
  879. if (bp == NULL) {
  880. error = XFS_ERROR(EIO);
  881. error_cancel:
  882. xfs_trans_cancel(tp, cancelflags);
  883. goto error;
  884. }
  885. memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
  886. xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
  887. /*
  888. * Commit the transaction.
  889. */
  890. error = xfs_trans_commit(tp, 0);
  891. if (error)
  892. goto error;
  893. }
  894. /*
  895. * Go on to the next extent, if any.
  896. */
  897. oblocks = map.br_startoff + map.br_blockcount;
  898. }
  899. return 0;
  900. error:
  901. return error;
  902. }
  903. /*
  904. * Visible (exported) functions.
  905. */
  906. /*
  907. * Grow the realtime area of the filesystem.
  908. */
  909. int
  910. xfs_growfs_rt(
  911. xfs_mount_t *mp, /* mount point for filesystem */
  912. xfs_growfs_rt_t *in) /* growfs rt input struct */
  913. {
  914. xfs_rtblock_t bmbno; /* bitmap block number */
  915. xfs_buf_t *bp; /* temporary buffer */
  916. int error; /* error return value */
  917. xfs_mount_t *nmp; /* new (fake) mount structure */
  918. xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
  919. xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
  920. xfs_drtbno_t nrextents; /* new number of realtime extents */
  921. uint8_t nrextslog; /* new log2 of sb_rextents */
  922. xfs_extlen_t nrsumblocks; /* new number of summary blocks */
  923. uint nrsumlevels; /* new rt summary levels */
  924. uint nrsumsize; /* new size of rt summary, bytes */
  925. xfs_sb_t *nsbp; /* new superblock */
  926. xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
  927. xfs_extlen_t rsumblocks; /* current number of rt summary blks */
  928. xfs_sb_t *sbp; /* old superblock */
  929. xfs_fsblock_t sumbno; /* summary block number */
  930. sbp = &mp->m_sb;
  931. /*
  932. * Initial error checking.
  933. */
  934. if (!capable(CAP_SYS_ADMIN))
  935. return XFS_ERROR(EPERM);
  936. if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
  937. (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
  938. (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
  939. return XFS_ERROR(EINVAL);
  940. if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
  941. return error;
  942. /*
  943. * Read in the last block of the device, make sure it exists.
  944. */
  945. bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
  946. XFS_FSB_TO_BB(mp, nrblocks - 1),
  947. XFS_FSB_TO_BB(mp, 1), 0, NULL);
  948. if (!bp)
  949. return EIO;
  950. if (bp->b_error) {
  951. error = bp->b_error;
  952. xfs_buf_relse(bp);
  953. return error;
  954. }
  955. xfs_buf_relse(bp);
  956. /*
  957. * Calculate new parameters. These are the final values to be reached.
  958. */
  959. nrextents = nrblocks;
  960. do_div(nrextents, in->extsize);
  961. nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
  962. nrextslog = xfs_highbit32(nrextents);
  963. nrsumlevels = nrextslog + 1;
  964. nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
  965. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  966. nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  967. /*
  968. * New summary size can't be more than half the size of
  969. * the log. This prevents us from getting a log overflow,
  970. * since we'll log basically the whole summary file at once.
  971. */
  972. if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
  973. return XFS_ERROR(EINVAL);
  974. /*
  975. * Get the old block counts for bitmap and summary inodes.
  976. * These can't change since other growfs callers are locked out.
  977. */
  978. rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
  979. rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
  980. /*
  981. * Allocate space to the bitmap and summary files, as necessary.
  982. */
  983. error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
  984. if (error)
  985. return error;
  986. error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
  987. if (error)
  988. return error;
  989. /*
  990. * Allocate a new (fake) mount/sb.
  991. */
  992. nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
  993. /*
  994. * Loop over the bitmap blocks.
  995. * We will do everything one bitmap block at a time.
  996. * Skip the current block if it is exactly full.
  997. * This also deals with the case where there were no rtextents before.
  998. */
  999. for (bmbno = sbp->sb_rbmblocks -
  1000. ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
  1001. bmbno < nrbmblocks;
  1002. bmbno++) {
  1003. xfs_trans_t *tp;
  1004. int cancelflags = 0;
  1005. *nmp = *mp;
  1006. nsbp = &nmp->m_sb;
  1007. /*
  1008. * Calculate new sb and mount fields for this round.
  1009. */
  1010. nsbp->sb_rextsize = in->extsize;
  1011. nsbp->sb_rbmblocks = bmbno + 1;
  1012. nsbp->sb_rblocks =
  1013. XFS_RTMIN(nrblocks,
  1014. nsbp->sb_rbmblocks * NBBY *
  1015. nsbp->sb_blocksize * nsbp->sb_rextsize);
  1016. nsbp->sb_rextents = nsbp->sb_rblocks;
  1017. do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
  1018. ASSERT(nsbp->sb_rextents != 0);
  1019. nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
  1020. nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
  1021. nrsumsize =
  1022. (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
  1023. nsbp->sb_rbmblocks;
  1024. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  1025. nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  1026. /*
  1027. * Start a transaction, get the log reservation.
  1028. */
  1029. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
  1030. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtfree,
  1031. 0, 0);
  1032. if (error)
  1033. goto error_cancel;
  1034. /*
  1035. * Lock out other callers by grabbing the bitmap inode lock.
  1036. */
  1037. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  1038. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  1039. /*
  1040. * Update the bitmap inode's size.
  1041. */
  1042. mp->m_rbmip->i_d.di_size =
  1043. nsbp->sb_rbmblocks * nsbp->sb_blocksize;
  1044. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1045. cancelflags |= XFS_TRANS_ABORT;
  1046. /*
  1047. * Get the summary inode into the transaction.
  1048. */
  1049. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
  1050. xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  1051. /*
  1052. * Update the summary inode's size.
  1053. */
  1054. mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
  1055. xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
  1056. /*
  1057. * Copy summary data from old to new sizes.
  1058. * Do this when the real size (not block-aligned) changes.
  1059. */
  1060. if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
  1061. mp->m_rsumlevels != nmp->m_rsumlevels) {
  1062. error = xfs_rtcopy_summary(mp, nmp, tp);
  1063. if (error)
  1064. goto error_cancel;
  1065. }
  1066. /*
  1067. * Update superblock fields.
  1068. */
  1069. if (nsbp->sb_rextsize != sbp->sb_rextsize)
  1070. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
  1071. nsbp->sb_rextsize - sbp->sb_rextsize);
  1072. if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
  1073. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
  1074. nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
  1075. if (nsbp->sb_rblocks != sbp->sb_rblocks)
  1076. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
  1077. nsbp->sb_rblocks - sbp->sb_rblocks);
  1078. if (nsbp->sb_rextents != sbp->sb_rextents)
  1079. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
  1080. nsbp->sb_rextents - sbp->sb_rextents);
  1081. if (nsbp->sb_rextslog != sbp->sb_rextslog)
  1082. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
  1083. nsbp->sb_rextslog - sbp->sb_rextslog);
  1084. /*
  1085. * Free new extent.
  1086. */
  1087. bp = NULL;
  1088. error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
  1089. nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
  1090. if (error) {
  1091. error_cancel:
  1092. xfs_trans_cancel(tp, cancelflags);
  1093. break;
  1094. }
  1095. /*
  1096. * Mark more blocks free in the superblock.
  1097. */
  1098. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
  1099. nsbp->sb_rextents - sbp->sb_rextents);
  1100. /*
  1101. * Update mp values into the real mp structure.
  1102. */
  1103. mp->m_rsumlevels = nrsumlevels;
  1104. mp->m_rsumsize = nrsumsize;
  1105. error = xfs_trans_commit(tp, 0);
  1106. if (error)
  1107. break;
  1108. }
  1109. /*
  1110. * Free the fake mp structure.
  1111. */
  1112. kmem_free(nmp);
  1113. return error;
  1114. }
  1115. /*
  1116. * Allocate an extent in the realtime subvolume, with the usual allocation
  1117. * parameters. The length units are all in realtime extents, as is the
  1118. * result block number.
  1119. */
  1120. int /* error */
  1121. xfs_rtallocate_extent(
  1122. xfs_trans_t *tp, /* transaction pointer */
  1123. xfs_rtblock_t bno, /* starting block number to allocate */
  1124. xfs_extlen_t minlen, /* minimum length to allocate */
  1125. xfs_extlen_t maxlen, /* maximum length to allocate */
  1126. xfs_extlen_t *len, /* out: actual length allocated */
  1127. xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
  1128. int wasdel, /* was a delayed allocation extent */
  1129. xfs_extlen_t prod, /* extent product factor */
  1130. xfs_rtblock_t *rtblock) /* out: start block allocated */
  1131. {
  1132. xfs_mount_t *mp = tp->t_mountp;
  1133. int error; /* error value */
  1134. xfs_rtblock_t r; /* result allocated block */
  1135. xfs_fsblock_t sb; /* summary file block number */
  1136. xfs_buf_t *sumbp; /* summary file block buffer */
  1137. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  1138. ASSERT(minlen > 0 && minlen <= maxlen);
  1139. /*
  1140. * If prod is set then figure out what to do to minlen and maxlen.
  1141. */
  1142. if (prod > 1) {
  1143. xfs_extlen_t i;
  1144. if ((i = maxlen % prod))
  1145. maxlen -= i;
  1146. if ((i = minlen % prod))
  1147. minlen += prod - i;
  1148. if (maxlen < minlen) {
  1149. *rtblock = NULLRTBLOCK;
  1150. return 0;
  1151. }
  1152. }
  1153. sumbp = NULL;
  1154. /*
  1155. * Allocate by size, or near another block, or exactly at some block.
  1156. */
  1157. switch (type) {
  1158. case XFS_ALLOCTYPE_ANY_AG:
  1159. error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
  1160. &sumbp, &sb, prod, &r);
  1161. break;
  1162. case XFS_ALLOCTYPE_NEAR_BNO:
  1163. error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
  1164. len, &sumbp, &sb, prod, &r);
  1165. break;
  1166. case XFS_ALLOCTYPE_THIS_BNO:
  1167. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
  1168. len, &sumbp, &sb, prod, &r);
  1169. break;
  1170. default:
  1171. error = EIO;
  1172. ASSERT(0);
  1173. }
  1174. if (error)
  1175. return error;
  1176. /*
  1177. * If it worked, update the superblock.
  1178. */
  1179. if (r != NULLRTBLOCK) {
  1180. long slen = (long)*len;
  1181. ASSERT(*len >= minlen && *len <= maxlen);
  1182. if (wasdel)
  1183. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
  1184. else
  1185. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
  1186. }
  1187. *rtblock = r;
  1188. return 0;
  1189. }
  1190. /*
  1191. * Initialize realtime fields in the mount structure.
  1192. */
  1193. int /* error */
  1194. xfs_rtmount_init(
  1195. xfs_mount_t *mp) /* file system mount structure */
  1196. {
  1197. xfs_buf_t *bp; /* buffer for last block of subvolume */
  1198. xfs_daddr_t d; /* address of last block of subvolume */
  1199. xfs_sb_t *sbp; /* filesystem superblock copy in mount */
  1200. sbp = &mp->m_sb;
  1201. if (sbp->sb_rblocks == 0)
  1202. return 0;
  1203. if (mp->m_rtdev_targp == NULL) {
  1204. xfs_warn(mp,
  1205. "Filesystem has a realtime volume, use rtdev=device option");
  1206. return XFS_ERROR(ENODEV);
  1207. }
  1208. mp->m_rsumlevels = sbp->sb_rextslog + 1;
  1209. mp->m_rsumsize =
  1210. (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
  1211. sbp->sb_rbmblocks;
  1212. mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
  1213. mp->m_rbmip = mp->m_rsumip = NULL;
  1214. /*
  1215. * Check that the realtime section is an ok size.
  1216. */
  1217. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  1218. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
  1219. xfs_warn(mp, "realtime mount -- %llu != %llu",
  1220. (unsigned long long) XFS_BB_TO_FSB(mp, d),
  1221. (unsigned long long) mp->m_sb.sb_rblocks);
  1222. return XFS_ERROR(EFBIG);
  1223. }
  1224. bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
  1225. d - XFS_FSB_TO_BB(mp, 1),
  1226. XFS_FSB_TO_BB(mp, 1), 0, NULL);
  1227. if (!bp || bp->b_error) {
  1228. xfs_warn(mp, "realtime device size check failed");
  1229. if (bp)
  1230. xfs_buf_relse(bp);
  1231. return EIO;
  1232. }
  1233. xfs_buf_relse(bp);
  1234. return 0;
  1235. }
  1236. /*
  1237. * Get the bitmap and summary inodes into the mount structure
  1238. * at mount time.
  1239. */
  1240. int /* error */
  1241. xfs_rtmount_inodes(
  1242. xfs_mount_t *mp) /* file system mount structure */
  1243. {
  1244. int error; /* error return value */
  1245. xfs_sb_t *sbp;
  1246. sbp = &mp->m_sb;
  1247. if (sbp->sb_rbmino == NULLFSINO)
  1248. return 0;
  1249. error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
  1250. if (error)
  1251. return error;
  1252. ASSERT(mp->m_rbmip != NULL);
  1253. ASSERT(sbp->sb_rsumino != NULLFSINO);
  1254. error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
  1255. if (error) {
  1256. IRELE(mp->m_rbmip);
  1257. return error;
  1258. }
  1259. ASSERT(mp->m_rsumip != NULL);
  1260. return 0;
  1261. }
  1262. void
  1263. xfs_rtunmount_inodes(
  1264. struct xfs_mount *mp)
  1265. {
  1266. if (mp->m_rbmip)
  1267. IRELE(mp->m_rbmip);
  1268. if (mp->m_rsumip)
  1269. IRELE(mp->m_rsumip);
  1270. }
  1271. /*
  1272. * Pick an extent for allocation at the start of a new realtime file.
  1273. * Use the sequence number stored in the atime field of the bitmap inode.
  1274. * Translate this to a fraction of the rtextents, and return the product
  1275. * of rtextents and the fraction.
  1276. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  1277. */
  1278. int /* error */
  1279. xfs_rtpick_extent(
  1280. xfs_mount_t *mp, /* file system mount point */
  1281. xfs_trans_t *tp, /* transaction pointer */
  1282. xfs_extlen_t len, /* allocation length (rtextents) */
  1283. xfs_rtblock_t *pick) /* result rt extent */
  1284. {
  1285. xfs_rtblock_t b; /* result block */
  1286. int log2; /* log of sequence number */
  1287. __uint64_t resid; /* residual after log removed */
  1288. __uint64_t seq; /* sequence number of file creation */
  1289. __uint64_t *seqp; /* pointer to seqno in inode */
  1290. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  1291. seqp = (__uint64_t *)&mp->m_rbmip->i_d.di_atime;
  1292. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
  1293. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  1294. *seqp = 0;
  1295. }
  1296. seq = *seqp;
  1297. if ((log2 = xfs_highbit64(seq)) == -1)
  1298. b = 0;
  1299. else {
  1300. resid = seq - (1ULL << log2);
  1301. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  1302. (log2 + 1);
  1303. if (b >= mp->m_sb.sb_rextents)
  1304. b = do_mod(b, mp->m_sb.sb_rextents);
  1305. if (b + len > mp->m_sb.sb_rextents)
  1306. b = mp->m_sb.sb_rextents - len;
  1307. }
  1308. *seqp = seq + 1;
  1309. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1310. *pick = b;
  1311. return 0;
  1312. }