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