xfs_rtbitmap.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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_mount.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_bmap.h"
  28. #include "xfs_bmap_util.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trans.h"
  33. #include "xfs_trans_space.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_buf.h"
  36. #include "xfs_icache.h"
  37. #include "xfs_rtalloc.h"
  38. /*
  39. * Realtime allocator bitmap functions shared with userspace.
  40. */
  41. /*
  42. * Real time buffers need verifiers to avoid runtime warnings during IO.
  43. * We don't have anything to verify, however, so these are just dummy
  44. * operations.
  45. */
  46. static void
  47. xfs_rtbuf_verify_read(
  48. struct xfs_buf *bp)
  49. {
  50. return;
  51. }
  52. static void
  53. xfs_rtbuf_verify_write(
  54. struct xfs_buf *bp)
  55. {
  56. return;
  57. }
  58. const struct xfs_buf_ops xfs_rtbuf_ops = {
  59. .name = "rtbuf",
  60. .verify_read = xfs_rtbuf_verify_read,
  61. .verify_write = xfs_rtbuf_verify_write,
  62. };
  63. /*
  64. * Get a buffer for the bitmap or summary file block specified.
  65. * The buffer is returned read and locked.
  66. */
  67. int
  68. xfs_rtbuf_get(
  69. xfs_mount_t *mp, /* file system mount structure */
  70. xfs_trans_t *tp, /* transaction pointer */
  71. xfs_rtblock_t block, /* block number in bitmap or summary */
  72. int issum, /* is summary not bitmap */
  73. xfs_buf_t **bpp) /* output: buffer for the block */
  74. {
  75. xfs_buf_t *bp; /* block buffer, result */
  76. xfs_inode_t *ip; /* bitmap or summary inode */
  77. xfs_bmbt_irec_t map;
  78. int nmap = 1;
  79. int error; /* error value */
  80. ip = issum ? mp->m_rsumip : mp->m_rbmip;
  81. error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
  82. if (error)
  83. return error;
  84. if (nmap == 0 || !xfs_bmap_is_real_extent(&map))
  85. return -EFSCORRUPTED;
  86. ASSERT(map.br_startblock != NULLFSBLOCK);
  87. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  88. XFS_FSB_TO_DADDR(mp, map.br_startblock),
  89. mp->m_bsize, 0, &bp, &xfs_rtbuf_ops);
  90. if (error)
  91. return error;
  92. xfs_trans_buf_set_type(tp, bp, issum ? XFS_BLFT_RTSUMMARY_BUF
  93. : XFS_BLFT_RTBITMAP_BUF);
  94. *bpp = bp;
  95. return 0;
  96. }
  97. /*
  98. * Searching backward from start to limit, find the first block whose
  99. * allocated/free state is different from start's.
  100. */
  101. int
  102. xfs_rtfind_back(
  103. xfs_mount_t *mp, /* file system mount point */
  104. xfs_trans_t *tp, /* transaction pointer */
  105. xfs_rtblock_t start, /* starting block to look at */
  106. xfs_rtblock_t limit, /* last block to look at */
  107. xfs_rtblock_t *rtblock) /* out: start block found */
  108. {
  109. xfs_rtword_t *b; /* current word in buffer */
  110. int bit; /* bit number in the word */
  111. xfs_rtblock_t block; /* bitmap block number */
  112. xfs_buf_t *bp; /* buf for the block */
  113. xfs_rtword_t *bufp; /* starting word in buffer */
  114. int error; /* error value */
  115. xfs_rtblock_t firstbit; /* first useful bit in the word */
  116. xfs_rtblock_t i; /* current bit number rel. to start */
  117. xfs_rtblock_t len; /* length of inspected area */
  118. xfs_rtword_t mask; /* mask of relevant bits for value */
  119. xfs_rtword_t want; /* mask for "good" values */
  120. xfs_rtword_t wdiff; /* difference from wanted value */
  121. int word; /* word number in the buffer */
  122. /*
  123. * Compute and read in starting bitmap block for starting block.
  124. */
  125. block = XFS_BITTOBLOCK(mp, start);
  126. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  127. if (error) {
  128. return error;
  129. }
  130. bufp = bp->b_addr;
  131. /*
  132. * Get the first word's index & point to it.
  133. */
  134. word = XFS_BITTOWORD(mp, start);
  135. b = &bufp[word];
  136. bit = (int)(start & (XFS_NBWORD - 1));
  137. len = start - limit + 1;
  138. /*
  139. * Compute match value, based on the bit at start: if 1 (free)
  140. * then all-ones, else all-zeroes.
  141. */
  142. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  143. /*
  144. * If the starting position is not word-aligned, deal with the
  145. * partial word.
  146. */
  147. if (bit < XFS_NBWORD - 1) {
  148. /*
  149. * Calculate first (leftmost) bit number to look at,
  150. * and mask for all the relevant bits in this word.
  151. */
  152. firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
  153. mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
  154. firstbit;
  155. /*
  156. * Calculate the difference between the value there
  157. * and what we're looking for.
  158. */
  159. if ((wdiff = (*b ^ want) & mask)) {
  160. /*
  161. * Different. Mark where we are and return.
  162. */
  163. xfs_trans_brelse(tp, bp);
  164. i = bit - XFS_RTHIBIT(wdiff);
  165. *rtblock = start - i + 1;
  166. return 0;
  167. }
  168. i = bit - firstbit + 1;
  169. /*
  170. * Go on to previous block if that's where the previous word is
  171. * and we need the previous word.
  172. */
  173. if (--word == -1 && i < len) {
  174. /*
  175. * If done with this block, get the previous one.
  176. */
  177. xfs_trans_brelse(tp, bp);
  178. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  179. if (error) {
  180. return error;
  181. }
  182. bufp = bp->b_addr;
  183. word = XFS_BLOCKWMASK(mp);
  184. b = &bufp[word];
  185. } else {
  186. /*
  187. * Go on to the previous word in the buffer.
  188. */
  189. b--;
  190. }
  191. } else {
  192. /*
  193. * Starting on a word boundary, no partial word.
  194. */
  195. i = 0;
  196. }
  197. /*
  198. * Loop over whole words in buffers. When we use up one buffer
  199. * we move on to the previous one.
  200. */
  201. while (len - i >= XFS_NBWORD) {
  202. /*
  203. * Compute difference between actual and desired value.
  204. */
  205. if ((wdiff = *b ^ want)) {
  206. /*
  207. * Different, mark where we are and return.
  208. */
  209. xfs_trans_brelse(tp, bp);
  210. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  211. *rtblock = start - i + 1;
  212. return 0;
  213. }
  214. i += XFS_NBWORD;
  215. /*
  216. * Go on to previous block if that's where the previous word is
  217. * and we need the previous word.
  218. */
  219. if (--word == -1 && i < len) {
  220. /*
  221. * If done with this block, get the previous one.
  222. */
  223. xfs_trans_brelse(tp, bp);
  224. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  225. if (error) {
  226. return error;
  227. }
  228. bufp = bp->b_addr;
  229. word = XFS_BLOCKWMASK(mp);
  230. b = &bufp[word];
  231. } else {
  232. /*
  233. * Go on to the previous word in the buffer.
  234. */
  235. b--;
  236. }
  237. }
  238. /*
  239. * If not ending on a word boundary, deal with the last
  240. * (partial) word.
  241. */
  242. if (len - i) {
  243. /*
  244. * Calculate first (leftmost) bit number to look at,
  245. * and mask for all the relevant bits in this word.
  246. */
  247. firstbit = XFS_NBWORD - (len - i);
  248. mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
  249. /*
  250. * Compute difference between actual and desired value.
  251. */
  252. if ((wdiff = (*b ^ want) & mask)) {
  253. /*
  254. * Different, mark where we are and return.
  255. */
  256. xfs_trans_brelse(tp, bp);
  257. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  258. *rtblock = start - i + 1;
  259. return 0;
  260. } else
  261. i = len;
  262. }
  263. /*
  264. * No match, return that we scanned the whole area.
  265. */
  266. xfs_trans_brelse(tp, bp);
  267. *rtblock = start - i + 1;
  268. return 0;
  269. }
  270. /*
  271. * Searching forward from start to limit, find the first block whose
  272. * allocated/free state is different from start's.
  273. */
  274. int
  275. xfs_rtfind_forw(
  276. xfs_mount_t *mp, /* file system mount point */
  277. xfs_trans_t *tp, /* transaction pointer */
  278. xfs_rtblock_t start, /* starting block to look at */
  279. xfs_rtblock_t limit, /* last block to look at */
  280. xfs_rtblock_t *rtblock) /* out: start block found */
  281. {
  282. xfs_rtword_t *b; /* current word in buffer */
  283. int bit; /* bit number in the word */
  284. xfs_rtblock_t block; /* bitmap block number */
  285. xfs_buf_t *bp; /* buf for the block */
  286. xfs_rtword_t *bufp; /* starting word in buffer */
  287. int error; /* error value */
  288. xfs_rtblock_t i; /* current bit number rel. to start */
  289. xfs_rtblock_t lastbit; /* last useful bit in the word */
  290. xfs_rtblock_t len; /* length of inspected area */
  291. xfs_rtword_t mask; /* mask of relevant bits for value */
  292. xfs_rtword_t want; /* mask for "good" values */
  293. xfs_rtword_t wdiff; /* difference from wanted value */
  294. int word; /* word number in the buffer */
  295. /*
  296. * Compute and read in starting bitmap block for starting block.
  297. */
  298. block = XFS_BITTOBLOCK(mp, start);
  299. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  300. if (error) {
  301. return error;
  302. }
  303. bufp = bp->b_addr;
  304. /*
  305. * Get the first word's index & point to it.
  306. */
  307. word = XFS_BITTOWORD(mp, start);
  308. b = &bufp[word];
  309. bit = (int)(start & (XFS_NBWORD - 1));
  310. len = limit - start + 1;
  311. /*
  312. * Compute match value, based on the bit at start: if 1 (free)
  313. * then all-ones, else all-zeroes.
  314. */
  315. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  316. /*
  317. * If the starting position is not word-aligned, deal with the
  318. * partial word.
  319. */
  320. if (bit) {
  321. /*
  322. * Calculate last (rightmost) bit number to look at,
  323. * and mask for all the relevant bits in this word.
  324. */
  325. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  326. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  327. /*
  328. * Calculate the difference between the value there
  329. * and what we're looking for.
  330. */
  331. if ((wdiff = (*b ^ want) & mask)) {
  332. /*
  333. * Different. Mark where we are and return.
  334. */
  335. xfs_trans_brelse(tp, bp);
  336. i = XFS_RTLOBIT(wdiff) - bit;
  337. *rtblock = start + i - 1;
  338. return 0;
  339. }
  340. i = lastbit - bit;
  341. /*
  342. * Go on to next block if that's where the next word is
  343. * and we need the next word.
  344. */
  345. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  346. /*
  347. * If done with this block, get the previous one.
  348. */
  349. xfs_trans_brelse(tp, bp);
  350. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  351. if (error) {
  352. return error;
  353. }
  354. b = bufp = bp->b_addr;
  355. word = 0;
  356. } else {
  357. /*
  358. * Go on to the previous word in the buffer.
  359. */
  360. b++;
  361. }
  362. } else {
  363. /*
  364. * Starting on a word boundary, no partial word.
  365. */
  366. i = 0;
  367. }
  368. /*
  369. * Loop over whole words in buffers. When we use up one buffer
  370. * we move on to the next one.
  371. */
  372. while (len - i >= XFS_NBWORD) {
  373. /*
  374. * Compute difference between actual and desired value.
  375. */
  376. if ((wdiff = *b ^ want)) {
  377. /*
  378. * Different, mark where we are and return.
  379. */
  380. xfs_trans_brelse(tp, bp);
  381. i += XFS_RTLOBIT(wdiff);
  382. *rtblock = start + i - 1;
  383. return 0;
  384. }
  385. i += XFS_NBWORD;
  386. /*
  387. * Go on to next block if that's where the next word is
  388. * and we need the next word.
  389. */
  390. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  391. /*
  392. * If done with this block, get the next one.
  393. */
  394. xfs_trans_brelse(tp, bp);
  395. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  396. if (error) {
  397. return error;
  398. }
  399. b = bufp = bp->b_addr;
  400. word = 0;
  401. } else {
  402. /*
  403. * Go on to the next word in the buffer.
  404. */
  405. b++;
  406. }
  407. }
  408. /*
  409. * If not ending on a word boundary, deal with the last
  410. * (partial) word.
  411. */
  412. if ((lastbit = len - i)) {
  413. /*
  414. * Calculate mask for all the relevant bits in this word.
  415. */
  416. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  417. /*
  418. * Compute difference between actual and desired value.
  419. */
  420. if ((wdiff = (*b ^ want) & mask)) {
  421. /*
  422. * Different, mark where we are and return.
  423. */
  424. xfs_trans_brelse(tp, bp);
  425. i += XFS_RTLOBIT(wdiff);
  426. *rtblock = start + i - 1;
  427. return 0;
  428. } else
  429. i = len;
  430. }
  431. /*
  432. * No match, return that we scanned the whole area.
  433. */
  434. xfs_trans_brelse(tp, bp);
  435. *rtblock = start + i - 1;
  436. return 0;
  437. }
  438. /*
  439. * Read and/or modify the summary information for a given extent size,
  440. * bitmap block combination.
  441. * Keeps track of a current summary block, so we don't keep reading
  442. * it from the buffer cache.
  443. *
  444. * Summary information is returned in *sum if specified.
  445. * If no delta is specified, returns summary only.
  446. */
  447. int
  448. xfs_rtmodify_summary_int(
  449. xfs_mount_t *mp, /* file system mount structure */
  450. xfs_trans_t *tp, /* transaction pointer */
  451. int log, /* log2 of extent size */
  452. xfs_rtblock_t bbno, /* bitmap block number */
  453. int delta, /* change to make to summary info */
  454. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  455. xfs_fsblock_t *rsb, /* in/out: summary block number */
  456. xfs_suminfo_t *sum) /* out: summary info for this block */
  457. {
  458. xfs_buf_t *bp; /* buffer for the summary block */
  459. int error; /* error value */
  460. xfs_fsblock_t sb; /* summary fsblock */
  461. int so; /* index into the summary file */
  462. xfs_suminfo_t *sp; /* pointer to returned data */
  463. /*
  464. * Compute entry number in the summary file.
  465. */
  466. so = XFS_SUMOFFS(mp, log, bbno);
  467. /*
  468. * Compute the block number in the summary file.
  469. */
  470. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  471. /*
  472. * If we have an old buffer, and the block number matches, use that.
  473. */
  474. if (*rbpp && *rsb == sb)
  475. bp = *rbpp;
  476. /*
  477. * Otherwise we have to get the buffer.
  478. */
  479. else {
  480. /*
  481. * If there was an old one, get rid of it first.
  482. */
  483. if (*rbpp)
  484. xfs_trans_brelse(tp, *rbpp);
  485. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  486. if (error) {
  487. return error;
  488. }
  489. /*
  490. * Remember this buffer and block for the next call.
  491. */
  492. *rbpp = bp;
  493. *rsb = sb;
  494. }
  495. /*
  496. * Point to the summary information, modify/log it, and/or copy it out.
  497. */
  498. sp = XFS_SUMPTR(mp, bp, so);
  499. if (delta) {
  500. uint first = (uint)((char *)sp - (char *)bp->b_addr);
  501. *sp += delta;
  502. xfs_trans_log_buf(tp, bp, first, first + sizeof(*sp) - 1);
  503. }
  504. if (sum)
  505. *sum = *sp;
  506. return 0;
  507. }
  508. int
  509. xfs_rtmodify_summary(
  510. xfs_mount_t *mp, /* file system mount structure */
  511. xfs_trans_t *tp, /* transaction pointer */
  512. int log, /* log2 of extent size */
  513. xfs_rtblock_t bbno, /* bitmap block number */
  514. int delta, /* change to make to summary info */
  515. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  516. xfs_fsblock_t *rsb) /* in/out: summary block number */
  517. {
  518. return xfs_rtmodify_summary_int(mp, tp, log, bbno,
  519. delta, rbpp, rsb, NULL);
  520. }
  521. /*
  522. * Set the given range of bitmap bits to the given value.
  523. * Do whatever I/O and logging is required.
  524. */
  525. int
  526. xfs_rtmodify_range(
  527. xfs_mount_t *mp, /* file system mount point */
  528. xfs_trans_t *tp, /* transaction pointer */
  529. xfs_rtblock_t start, /* starting block to modify */
  530. xfs_extlen_t len, /* length of extent to modify */
  531. int val) /* 1 for free, 0 for allocated */
  532. {
  533. xfs_rtword_t *b; /* current word in buffer */
  534. int bit; /* bit number in the word */
  535. xfs_rtblock_t block; /* bitmap block number */
  536. xfs_buf_t *bp; /* buf for the block */
  537. xfs_rtword_t *bufp; /* starting word in buffer */
  538. int error; /* error value */
  539. xfs_rtword_t *first; /* first used word in the buffer */
  540. int i; /* current bit number rel. to start */
  541. int lastbit; /* last useful bit in word */
  542. xfs_rtword_t mask; /* mask o frelevant bits for value */
  543. int word; /* word number in the buffer */
  544. /*
  545. * Compute starting bitmap block number.
  546. */
  547. block = XFS_BITTOBLOCK(mp, start);
  548. /*
  549. * Read the bitmap block, and point to its data.
  550. */
  551. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  552. if (error) {
  553. return error;
  554. }
  555. bufp = bp->b_addr;
  556. /*
  557. * Compute the starting word's address, and starting bit.
  558. */
  559. word = XFS_BITTOWORD(mp, start);
  560. first = b = &bufp[word];
  561. bit = (int)(start & (XFS_NBWORD - 1));
  562. /*
  563. * 0 (allocated) => all zeroes; 1 (free) => all ones.
  564. */
  565. val = -val;
  566. /*
  567. * If not starting on a word boundary, deal with the first
  568. * (partial) word.
  569. */
  570. if (bit) {
  571. /*
  572. * Compute first bit not changed and mask of relevant bits.
  573. */
  574. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  575. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  576. /*
  577. * Set/clear the active bits.
  578. */
  579. if (val)
  580. *b |= mask;
  581. else
  582. *b &= ~mask;
  583. i = lastbit - bit;
  584. /*
  585. * Go on to the next block if that's where the next word is
  586. * and we need the next word.
  587. */
  588. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  589. /*
  590. * Log the changed part of this block.
  591. * Get the next one.
  592. */
  593. xfs_trans_log_buf(tp, bp,
  594. (uint)((char *)first - (char *)bufp),
  595. (uint)((char *)b - (char *)bufp));
  596. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  597. if (error) {
  598. return error;
  599. }
  600. first = b = bufp = bp->b_addr;
  601. word = 0;
  602. } else {
  603. /*
  604. * Go on to the next word in the buffer
  605. */
  606. b++;
  607. }
  608. } else {
  609. /*
  610. * Starting on a word boundary, no partial word.
  611. */
  612. i = 0;
  613. }
  614. /*
  615. * Loop over whole words in buffers. When we use up one buffer
  616. * we move on to the next one.
  617. */
  618. while (len - i >= XFS_NBWORD) {
  619. /*
  620. * Set the word value correctly.
  621. */
  622. *b = val;
  623. i += XFS_NBWORD;
  624. /*
  625. * Go on to the next block if that's where the next word is
  626. * and we need the next word.
  627. */
  628. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  629. /*
  630. * Log the changed part of this block.
  631. * Get the next one.
  632. */
  633. xfs_trans_log_buf(tp, bp,
  634. (uint)((char *)first - (char *)bufp),
  635. (uint)((char *)b - (char *)bufp));
  636. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  637. if (error) {
  638. return error;
  639. }
  640. first = b = bufp = bp->b_addr;
  641. word = 0;
  642. } else {
  643. /*
  644. * Go on to the next word in the buffer
  645. */
  646. b++;
  647. }
  648. }
  649. /*
  650. * If not ending on a word boundary, deal with the last
  651. * (partial) word.
  652. */
  653. if ((lastbit = len - i)) {
  654. /*
  655. * Compute a mask of relevant bits.
  656. */
  657. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  658. /*
  659. * Set/clear the active bits.
  660. */
  661. if (val)
  662. *b |= mask;
  663. else
  664. *b &= ~mask;
  665. b++;
  666. }
  667. /*
  668. * Log any remaining changed bytes.
  669. */
  670. if (b > first)
  671. xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
  672. (uint)((char *)b - (char *)bufp - 1));
  673. return 0;
  674. }
  675. /*
  676. * Mark an extent specified by start and len freed.
  677. * Updates all the summary information as well as the bitmap.
  678. */
  679. int
  680. xfs_rtfree_range(
  681. xfs_mount_t *mp, /* file system mount point */
  682. xfs_trans_t *tp, /* transaction pointer */
  683. xfs_rtblock_t start, /* starting block to free */
  684. xfs_extlen_t len, /* length to free */
  685. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  686. xfs_fsblock_t *rsb) /* in/out: summary block number */
  687. {
  688. xfs_rtblock_t end; /* end of the freed extent */
  689. int error; /* error value */
  690. xfs_rtblock_t postblock; /* first block freed > end */
  691. xfs_rtblock_t preblock; /* first block freed < start */
  692. end = start + len - 1;
  693. /*
  694. * Modify the bitmap to mark this extent freed.
  695. */
  696. error = xfs_rtmodify_range(mp, tp, start, len, 1);
  697. if (error) {
  698. return error;
  699. }
  700. /*
  701. * Assume we're freeing out of the middle of an allocated extent.
  702. * We need to find the beginning and end of the extent so we can
  703. * properly update the summary.
  704. */
  705. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  706. if (error) {
  707. return error;
  708. }
  709. /*
  710. * Find the next allocated block (end of allocated extent).
  711. */
  712. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  713. &postblock);
  714. if (error)
  715. return error;
  716. /*
  717. * If there are blocks not being freed at the front of the
  718. * old extent, add summary data for them to be allocated.
  719. */
  720. if (preblock < start) {
  721. error = xfs_rtmodify_summary(mp, tp,
  722. XFS_RTBLOCKLOG(start - preblock),
  723. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  724. if (error) {
  725. return error;
  726. }
  727. }
  728. /*
  729. * If there are blocks not being freed at the end of the
  730. * old extent, add summary data for them to be allocated.
  731. */
  732. if (postblock > end) {
  733. error = xfs_rtmodify_summary(mp, tp,
  734. XFS_RTBLOCKLOG(postblock - end),
  735. XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
  736. if (error) {
  737. return error;
  738. }
  739. }
  740. /*
  741. * Increment the summary information corresponding to the entire
  742. * (new) free extent.
  743. */
  744. error = xfs_rtmodify_summary(mp, tp,
  745. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  746. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  747. return error;
  748. }
  749. /*
  750. * Check that the given range is either all allocated (val = 0) or
  751. * all free (val = 1).
  752. */
  753. int
  754. xfs_rtcheck_range(
  755. xfs_mount_t *mp, /* file system mount point */
  756. xfs_trans_t *tp, /* transaction pointer */
  757. xfs_rtblock_t start, /* starting block number of extent */
  758. xfs_extlen_t len, /* length of extent */
  759. int val, /* 1 for free, 0 for allocated */
  760. xfs_rtblock_t *new, /* out: first block not matching */
  761. int *stat) /* out: 1 for matches, 0 for not */
  762. {
  763. xfs_rtword_t *b; /* current word in buffer */
  764. int bit; /* bit number in the word */
  765. xfs_rtblock_t block; /* bitmap block number */
  766. xfs_buf_t *bp; /* buf for the block */
  767. xfs_rtword_t *bufp; /* starting word in buffer */
  768. int error; /* error value */
  769. xfs_rtblock_t i; /* current bit number rel. to start */
  770. xfs_rtblock_t lastbit; /* last useful bit in word */
  771. xfs_rtword_t mask; /* mask of relevant bits for value */
  772. xfs_rtword_t wdiff; /* difference from wanted value */
  773. int word; /* word number in the buffer */
  774. /*
  775. * Compute starting bitmap block number
  776. */
  777. block = XFS_BITTOBLOCK(mp, start);
  778. /*
  779. * Read the bitmap block.
  780. */
  781. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  782. if (error) {
  783. return error;
  784. }
  785. bufp = bp->b_addr;
  786. /*
  787. * Compute the starting word's address, and starting bit.
  788. */
  789. word = XFS_BITTOWORD(mp, start);
  790. b = &bufp[word];
  791. bit = (int)(start & (XFS_NBWORD - 1));
  792. /*
  793. * 0 (allocated) => all zero's; 1 (free) => all one's.
  794. */
  795. val = -val;
  796. /*
  797. * If not starting on a word boundary, deal with the first
  798. * (partial) word.
  799. */
  800. if (bit) {
  801. /*
  802. * Compute first bit not examined.
  803. */
  804. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  805. /*
  806. * Mask of relevant bits.
  807. */
  808. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  809. /*
  810. * Compute difference between actual and desired value.
  811. */
  812. if ((wdiff = (*b ^ val) & mask)) {
  813. /*
  814. * Different, compute first wrong bit and return.
  815. */
  816. xfs_trans_brelse(tp, bp);
  817. i = XFS_RTLOBIT(wdiff) - bit;
  818. *new = start + i;
  819. *stat = 0;
  820. return 0;
  821. }
  822. i = lastbit - bit;
  823. /*
  824. * Go on to next block if that's where the next word is
  825. * and we need the next word.
  826. */
  827. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  828. /*
  829. * If done with this block, get the next one.
  830. */
  831. xfs_trans_brelse(tp, bp);
  832. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  833. if (error) {
  834. return error;
  835. }
  836. b = bufp = bp->b_addr;
  837. word = 0;
  838. } else {
  839. /*
  840. * Go on to the next word in the buffer.
  841. */
  842. b++;
  843. }
  844. } else {
  845. /*
  846. * Starting on a word boundary, no partial word.
  847. */
  848. i = 0;
  849. }
  850. /*
  851. * Loop over whole words in buffers. When we use up one buffer
  852. * we move on to the next one.
  853. */
  854. while (len - i >= XFS_NBWORD) {
  855. /*
  856. * Compute difference between actual and desired value.
  857. */
  858. if ((wdiff = *b ^ val)) {
  859. /*
  860. * Different, compute first wrong bit and return.
  861. */
  862. xfs_trans_brelse(tp, bp);
  863. i += XFS_RTLOBIT(wdiff);
  864. *new = start + i;
  865. *stat = 0;
  866. return 0;
  867. }
  868. i += XFS_NBWORD;
  869. /*
  870. * Go on to next block if that's where the next word is
  871. * and we need the next word.
  872. */
  873. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  874. /*
  875. * If done with this block, get the next one.
  876. */
  877. xfs_trans_brelse(tp, bp);
  878. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  879. if (error) {
  880. return error;
  881. }
  882. b = bufp = bp->b_addr;
  883. word = 0;
  884. } else {
  885. /*
  886. * Go on to the next word in the buffer.
  887. */
  888. b++;
  889. }
  890. }
  891. /*
  892. * If not ending on a word boundary, deal with the last
  893. * (partial) word.
  894. */
  895. if ((lastbit = len - i)) {
  896. /*
  897. * Mask of relevant bits.
  898. */
  899. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  900. /*
  901. * Compute difference between actual and desired value.
  902. */
  903. if ((wdiff = (*b ^ val) & mask)) {
  904. /*
  905. * Different, compute first wrong bit and return.
  906. */
  907. xfs_trans_brelse(tp, bp);
  908. i += XFS_RTLOBIT(wdiff);
  909. *new = start + i;
  910. *stat = 0;
  911. return 0;
  912. } else
  913. i = len;
  914. }
  915. /*
  916. * Successful, return.
  917. */
  918. xfs_trans_brelse(tp, bp);
  919. *new = start + i;
  920. *stat = 1;
  921. return 0;
  922. }
  923. #ifdef DEBUG
  924. /*
  925. * Check that the given extent (block range) is allocated already.
  926. */
  927. STATIC int /* error */
  928. xfs_rtcheck_alloc_range(
  929. xfs_mount_t *mp, /* file system mount point */
  930. xfs_trans_t *tp, /* transaction pointer */
  931. xfs_rtblock_t bno, /* starting block number of extent */
  932. xfs_extlen_t len) /* length of extent */
  933. {
  934. xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
  935. int stat;
  936. int error;
  937. error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
  938. if (error)
  939. return error;
  940. ASSERT(stat);
  941. return 0;
  942. }
  943. #else
  944. #define xfs_rtcheck_alloc_range(m,t,b,l) (0)
  945. #endif
  946. /*
  947. * Free an extent in the realtime subvolume. Length is expressed in
  948. * realtime extents, as is the block number.
  949. */
  950. int /* error */
  951. xfs_rtfree_extent(
  952. xfs_trans_t *tp, /* transaction pointer */
  953. xfs_rtblock_t bno, /* starting block number to free */
  954. xfs_extlen_t len) /* length of extent freed */
  955. {
  956. int error; /* error value */
  957. xfs_mount_t *mp; /* file system mount structure */
  958. xfs_fsblock_t sb; /* summary file block number */
  959. xfs_buf_t *sumbp = NULL; /* summary file block buffer */
  960. mp = tp->t_mountp;
  961. ASSERT(mp->m_rbmip->i_itemp != NULL);
  962. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  963. error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
  964. if (error)
  965. return error;
  966. /*
  967. * Free the range of realtime blocks.
  968. */
  969. error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
  970. if (error) {
  971. return error;
  972. }
  973. /*
  974. * Mark more blocks free in the superblock.
  975. */
  976. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
  977. /*
  978. * If we've now freed all the blocks, reset the file sequence
  979. * number to 0.
  980. */
  981. if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
  982. mp->m_sb.sb_rextents) {
  983. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
  984. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  985. *(uint64_t *)&VFS_I(mp->m_rbmip)->i_atime = 0;
  986. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  987. }
  988. return 0;
  989. }
  990. /* Find all the free records within a given range. */
  991. int
  992. xfs_rtalloc_query_range(
  993. struct xfs_trans *tp,
  994. struct xfs_rtalloc_rec *low_rec,
  995. struct xfs_rtalloc_rec *high_rec,
  996. xfs_rtalloc_query_range_fn fn,
  997. void *priv)
  998. {
  999. struct xfs_rtalloc_rec rec;
  1000. struct xfs_mount *mp = tp->t_mountp;
  1001. xfs_rtblock_t rtstart;
  1002. xfs_rtblock_t rtend;
  1003. xfs_rtblock_t rem;
  1004. int is_free;
  1005. int error = 0;
  1006. if (low_rec->ar_startext > high_rec->ar_startext)
  1007. return -EINVAL;
  1008. if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
  1009. low_rec->ar_startext == high_rec->ar_startext)
  1010. return 0;
  1011. if (high_rec->ar_startext >= mp->m_sb.sb_rextents)
  1012. high_rec->ar_startext = mp->m_sb.sb_rextents - 1;
  1013. /* Iterate the bitmap, looking for discrepancies. */
  1014. rtstart = low_rec->ar_startext;
  1015. rem = high_rec->ar_startext - rtstart;
  1016. while (rem) {
  1017. /* Is the first block free? */
  1018. error = xfs_rtcheck_range(mp, tp, rtstart, 1, 1, &rtend,
  1019. &is_free);
  1020. if (error)
  1021. break;
  1022. /* How long does the extent go for? */
  1023. error = xfs_rtfind_forw(mp, tp, rtstart,
  1024. high_rec->ar_startext - 1, &rtend);
  1025. if (error)
  1026. break;
  1027. if (is_free) {
  1028. rec.ar_startext = rtstart;
  1029. rec.ar_extcount = rtend - rtstart + 1;
  1030. error = fn(tp, &rec, priv);
  1031. if (error)
  1032. break;
  1033. }
  1034. rem -= rtend - rtstart + 1;
  1035. rtstart = rtend + 1;
  1036. }
  1037. return error;
  1038. }
  1039. /* Find all the free records. */
  1040. int
  1041. xfs_rtalloc_query_all(
  1042. struct xfs_trans *tp,
  1043. xfs_rtalloc_query_range_fn fn,
  1044. void *priv)
  1045. {
  1046. struct xfs_rtalloc_rec keys[2];
  1047. keys[0].ar_startext = 0;
  1048. keys[1].ar_startext = tp->t_mountp->m_sb.sb_rextents - 1;
  1049. keys[0].ar_extcount = keys[1].ar_extcount = 0;
  1050. return xfs_rtalloc_query_range(tp, &keys[0], &keys[1], fn, priv);
  1051. }
  1052. /*
  1053. * Verify that an realtime block number pointer doesn't point off the
  1054. * end of the realtime device.
  1055. */
  1056. bool
  1057. xfs_verify_rtbno(
  1058. struct xfs_mount *mp,
  1059. xfs_rtblock_t rtbno)
  1060. {
  1061. return rtbno < mp->m_sb.sb_rblocks;
  1062. }
  1063. /* Is the given extent all free? */
  1064. int
  1065. xfs_rtalloc_extent_is_free(
  1066. struct xfs_mount *mp,
  1067. struct xfs_trans *tp,
  1068. xfs_rtblock_t start,
  1069. xfs_extlen_t len,
  1070. bool *is_free)
  1071. {
  1072. xfs_rtblock_t end;
  1073. int matches;
  1074. int error;
  1075. error = xfs_rtcheck_range(mp, tp, start, len, 1, &end, &matches);
  1076. if (error)
  1077. return error;
  1078. *is_free = matches;
  1079. return 0;
  1080. }