xfs_rtalloc.c 36 KB

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