Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
fe6c991c | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
9 | ||
b3b94faa DT |
10 | #include <linux/slab.h> |
11 | #include <linux/spinlock.h> | |
12 | #include <linux/completion.h> | |
13 | #include <linux/buffer_head.h> | |
f42faf4f | 14 | #include <linux/fs.h> |
5c676f6d | 15 | #include <linux/gfs2_ondisk.h> |
1f466a47 | 16 | #include <linux/prefetch.h> |
f15ab561 | 17 | #include <linux/blkdev.h> |
7c9ca621 | 18 | #include <linux/rbtree.h> |
b3b94faa DT |
19 | |
20 | #include "gfs2.h" | |
5c676f6d | 21 | #include "incore.h" |
b3b94faa DT |
22 | #include "glock.h" |
23 | #include "glops.h" | |
b3b94faa DT |
24 | #include "lops.h" |
25 | #include "meta_io.h" | |
26 | #include "quota.h" | |
27 | #include "rgrp.h" | |
28 | #include "super.h" | |
29 | #include "trans.h" | |
5c676f6d | 30 | #include "util.h" |
172e045a | 31 | #include "log.h" |
c8cdf479 | 32 | #include "inode.h" |
63997775 | 33 | #include "trace_gfs2.h" |
b3b94faa | 34 | |
2c1e52aa | 35 | #define BFITNOENT ((u32)~0) |
6760bdcd | 36 | #define NO_BLOCK ((u64)~0) |
88c8ab1f | 37 | |
8e2e0047 BP |
38 | #define RSRV_CONTENTION_FACTOR 4 |
39 | #define RGRP_RSRV_MAX_CONTENDERS 2 | |
40 | ||
1f466a47 BP |
41 | #if BITS_PER_LONG == 32 |
42 | #define LBITMASK (0x55555555UL) | |
43 | #define LBITSKIP55 (0x55555555UL) | |
44 | #define LBITSKIP00 (0x00000000UL) | |
45 | #else | |
46 | #define LBITMASK (0x5555555555555555UL) | |
47 | #define LBITSKIP55 (0x5555555555555555UL) | |
48 | #define LBITSKIP00 (0x0000000000000000UL) | |
49 | #endif | |
50 | ||
88c8ab1f SW |
51 | /* |
52 | * These routines are used by the resource group routines (rgrp.c) | |
53 | * to keep track of block allocation. Each block is represented by two | |
feaa7bba SW |
54 | * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks. |
55 | * | |
56 | * 0 = Free | |
57 | * 1 = Used (not metadata) | |
58 | * 2 = Unlinked (still in use) inode | |
59 | * 3 = Used (metadata) | |
88c8ab1f SW |
60 | */ |
61 | ||
62 | static const char valid_change[16] = { | |
63 | /* current */ | |
feaa7bba | 64 | /* n */ 0, 1, 1, 1, |
88c8ab1f | 65 | /* e */ 1, 0, 0, 0, |
feaa7bba | 66 | /* w */ 0, 0, 0, 1, |
88c8ab1f SW |
67 | 1, 0, 0, 0 |
68 | }; | |
69 | ||
c8cdf479 | 70 | static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, |
6a8099ed | 71 | unsigned char old_state, |
b3e47ca0 | 72 | struct gfs2_bitmap **rbi); |
c8cdf479 | 73 | |
88c8ab1f SW |
74 | /** |
75 | * gfs2_setbit - Set a bit in the bitmaps | |
29c578f5 | 76 | * @rgd: the resource group descriptor |
29c578f5 BP |
77 | * @buf2: the clone buffer that holds the bitmaps |
78 | * @bi: the bitmap structure | |
88c8ab1f SW |
79 | * @block: the block to set |
80 | * @new_state: the new state of the block | |
81 | * | |
82 | */ | |
83 | ||
06344b91 BP |
84 | static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf2, |
85 | struct gfs2_bitmap *bi, u32 block, | |
86 | unsigned char new_state) | |
88c8ab1f | 87 | { |
b45e41d7 | 88 | unsigned char *byte1, *byte2, *end, cur_state; |
95c8e17f | 89 | unsigned int buflen = bi->bi_len; |
b45e41d7 | 90 | const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; |
88c8ab1f | 91 | |
06344b91 BP |
92 | byte1 = bi->bi_bh->b_data + bi->bi_offset + (block / GFS2_NBBY); |
93 | end = bi->bi_bh->b_data + bi->bi_offset + buflen; | |
88c8ab1f | 94 | |
b45e41d7 | 95 | BUG_ON(byte1 >= end); |
88c8ab1f | 96 | |
b45e41d7 | 97 | cur_state = (*byte1 >> bit) & GFS2_BIT_MASK; |
88c8ab1f | 98 | |
b45e41d7 | 99 | if (unlikely(!valid_change[new_state * 4 + cur_state])) { |
95c8e17f BP |
100 | printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, " |
101 | "new_state=%d\n", | |
102 | (unsigned long long)block, cur_state, new_state); | |
103 | printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n", | |
104 | (unsigned long long)rgd->rd_addr, | |
105 | (unsigned long)bi->bi_start); | |
106 | printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n", | |
107 | (unsigned long)bi->bi_offset, | |
108 | (unsigned long)bi->bi_len); | |
109 | dump_stack(); | |
88c8ab1f | 110 | gfs2_consist_rgrpd(rgd); |
b45e41d7 SW |
111 | return; |
112 | } | |
113 | *byte1 ^= (cur_state ^ new_state) << bit; | |
114 | ||
115 | if (buf2) { | |
29c578f5 | 116 | byte2 = buf2 + bi->bi_offset + (block / GFS2_NBBY); |
b45e41d7 SW |
117 | cur_state = (*byte2 >> bit) & GFS2_BIT_MASK; |
118 | *byte2 ^= (cur_state ^ new_state) << bit; | |
119 | } | |
88c8ab1f SW |
120 | } |
121 | ||
122 | /** | |
123 | * gfs2_testbit - test a bit in the bitmaps | |
886b1416 | 124 | * @rgd: the resource group descriptor |
88c8ab1f SW |
125 | * @buffer: the buffer that holds the bitmaps |
126 | * @buflen: the length (in bytes) of the buffer | |
127 | * @block: the block to read | |
128 | * | |
129 | */ | |
130 | ||
b45e41d7 SW |
131 | static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, |
132 | const unsigned char *buffer, | |
133 | unsigned int buflen, u32 block) | |
88c8ab1f | 134 | { |
b45e41d7 SW |
135 | const unsigned char *byte, *end; |
136 | unsigned char cur_state; | |
88c8ab1f SW |
137 | unsigned int bit; |
138 | ||
139 | byte = buffer + (block / GFS2_NBBY); | |
140 | bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; | |
141 | end = buffer + buflen; | |
142 | ||
143 | gfs2_assert(rgd->rd_sbd, byte < end); | |
144 | ||
145 | cur_state = (*byte >> bit) & GFS2_BIT_MASK; | |
146 | ||
147 | return cur_state; | |
148 | } | |
149 | ||
223b2b88 SW |
150 | /** |
151 | * gfs2_bit_search | |
152 | * @ptr: Pointer to bitmap data | |
153 | * @mask: Mask to use (normally 0x55555.... but adjusted for search start) | |
154 | * @state: The state we are searching for | |
155 | * | |
156 | * We xor the bitmap data with a patter which is the bitwise opposite | |
157 | * of what we are looking for, this gives rise to a pattern of ones | |
158 | * wherever there is a match. Since we have two bits per entry, we | |
159 | * take this pattern, shift it down by one place and then and it with | |
160 | * the original. All the even bit positions (0,2,4, etc) then represent | |
161 | * successful matches, so we mask with 0x55555..... to remove the unwanted | |
162 | * odd bit positions. | |
163 | * | |
164 | * This allows searching of a whole u64 at once (32 blocks) with a | |
165 | * single test (on 64 bit arches). | |
166 | */ | |
167 | ||
168 | static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state) | |
169 | { | |
170 | u64 tmp; | |
171 | static const u64 search[] = { | |
075ac448 HE |
172 | [0] = 0xffffffffffffffffULL, |
173 | [1] = 0xaaaaaaaaaaaaaaaaULL, | |
174 | [2] = 0x5555555555555555ULL, | |
175 | [3] = 0x0000000000000000ULL, | |
223b2b88 SW |
176 | }; |
177 | tmp = le64_to_cpu(*ptr) ^ search[state]; | |
178 | tmp &= (tmp >> 1); | |
179 | tmp &= mask; | |
180 | return tmp; | |
181 | } | |
182 | ||
8e2e0047 BP |
183 | /** |
184 | * rs_cmp - multi-block reservation range compare | |
185 | * @blk: absolute file system block number of the new reservation | |
186 | * @len: number of blocks in the new reservation | |
187 | * @rs: existing reservation to compare against | |
188 | * | |
189 | * returns: 1 if the block range is beyond the reach of the reservation | |
190 | * -1 if the block range is before the start of the reservation | |
191 | * 0 if the block range overlaps with the reservation | |
192 | */ | |
193 | static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs) | |
194 | { | |
4a993fb1 | 195 | u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm); |
8e2e0047 BP |
196 | |
197 | if (blk >= startblk + rs->rs_free) | |
198 | return 1; | |
199 | if (blk + len - 1 < startblk) | |
200 | return -1; | |
201 | return 0; | |
202 | } | |
203 | ||
204 | /** | |
205 | * rs_find - Find a rgrp multi-block reservation that contains a given block | |
206 | * @rgd: The rgrp | |
207 | * @rgblk: The block we're looking for, relative to the rgrp | |
208 | */ | |
209 | static struct gfs2_blkreserv *rs_find(struct gfs2_rgrpd *rgd, u32 rgblk) | |
210 | { | |
211 | struct rb_node **newn; | |
212 | int rc; | |
213 | u64 fsblk = rgblk + rgd->rd_data0; | |
214 | ||
215 | spin_lock(&rgd->rd_rsspin); | |
216 | newn = &rgd->rd_rstree.rb_node; | |
217 | while (*newn) { | |
218 | struct gfs2_blkreserv *cur = | |
219 | rb_entry(*newn, struct gfs2_blkreserv, rs_node); | |
220 | rc = rs_cmp(fsblk, 1, cur); | |
221 | if (rc < 0) | |
222 | newn = &((*newn)->rb_left); | |
223 | else if (rc > 0) | |
224 | newn = &((*newn)->rb_right); | |
225 | else { | |
226 | spin_unlock(&rgd->rd_rsspin); | |
227 | return cur; | |
228 | } | |
229 | } | |
230 | spin_unlock(&rgd->rd_rsspin); | |
231 | return NULL; | |
232 | } | |
233 | ||
88c8ab1f SW |
234 | /** |
235 | * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing | |
236 | * a block in a given allocation state. | |
886b1416 | 237 | * @buf: the buffer that holds the bitmaps |
223b2b88 | 238 | * @len: the length (in bytes) of the buffer |
88c8ab1f | 239 | * @goal: start search at this block's bit-pair (within @buffer) |
223b2b88 | 240 | * @state: GFS2_BLKST_XXX the state of the block we're looking for. |
88c8ab1f SW |
241 | * |
242 | * Scope of @goal and returned block number is only within this bitmap buffer, | |
243 | * not entire rgrp or filesystem. @buffer will be offset from the actual | |
223b2b88 SW |
244 | * beginning of a bitmap block buffer, skipping any header structures, but |
245 | * headers are always a multiple of 64 bits long so that the buffer is | |
246 | * always aligned to a 64 bit boundary. | |
247 | * | |
248 | * The size of the buffer is in bytes, but is it assumed that it is | |
fd589a8f | 249 | * always ok to read a complete multiple of 64 bits at the end |
223b2b88 | 250 | * of the block in case the end is no aligned to a natural boundary. |
88c8ab1f SW |
251 | * |
252 | * Return: the block number (bitmap buffer scope) that was found | |
253 | */ | |
254 | ||
02ab1721 HE |
255 | static u32 gfs2_bitfit(const u8 *buf, const unsigned int len, |
256 | u32 goal, u8 state) | |
88c8ab1f | 257 | { |
223b2b88 SW |
258 | u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1); |
259 | const __le64 *ptr = ((__le64 *)buf) + (goal >> 5); | |
260 | const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64))); | |
261 | u64 tmp; | |
075ac448 | 262 | u64 mask = 0x5555555555555555ULL; |
223b2b88 SW |
263 | u32 bit; |
264 | ||
265 | BUG_ON(state > 3); | |
266 | ||
267 | /* Mask off bits we don't care about at the start of the search */ | |
268 | mask <<= spoint; | |
269 | tmp = gfs2_bit_search(ptr, mask, state); | |
270 | ptr++; | |
271 | while(tmp == 0 && ptr < end) { | |
075ac448 | 272 | tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state); |
223b2b88 | 273 | ptr++; |
1f466a47 | 274 | } |
223b2b88 SW |
275 | /* Mask off any bits which are more than len bytes from the start */ |
276 | if (ptr == end && (len & (sizeof(u64) - 1))) | |
277 | tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1)))); | |
278 | /* Didn't find anything, so return */ | |
279 | if (tmp == 0) | |
280 | return BFITNOENT; | |
281 | ptr--; | |
d8bd504a | 282 | bit = __ffs64(tmp); |
223b2b88 SW |
283 | bit /= 2; /* two bits per entry in the bitmap */ |
284 | return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit; | |
88c8ab1f SW |
285 | } |
286 | ||
287 | /** | |
288 | * gfs2_bitcount - count the number of bits in a certain state | |
886b1416 | 289 | * @rgd: the resource group descriptor |
88c8ab1f SW |
290 | * @buffer: the buffer that holds the bitmaps |
291 | * @buflen: the length (in bytes) of the buffer | |
292 | * @state: the state of the block we're looking for | |
293 | * | |
294 | * Returns: The number of bits | |
295 | */ | |
296 | ||
110acf38 SW |
297 | static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer, |
298 | unsigned int buflen, u8 state) | |
88c8ab1f | 299 | { |
110acf38 SW |
300 | const u8 *byte = buffer; |
301 | const u8 *end = buffer + buflen; | |
302 | const u8 state1 = state << 2; | |
303 | const u8 state2 = state << 4; | |
304 | const u8 state3 = state << 6; | |
cd915493 | 305 | u32 count = 0; |
88c8ab1f SW |
306 | |
307 | for (; byte < end; byte++) { | |
308 | if (((*byte) & 0x03) == state) | |
309 | count++; | |
310 | if (((*byte) & 0x0C) == state1) | |
311 | count++; | |
312 | if (((*byte) & 0x30) == state2) | |
313 | count++; | |
314 | if (((*byte) & 0xC0) == state3) | |
315 | count++; | |
316 | } | |
317 | ||
318 | return count; | |
319 | } | |
320 | ||
b3b94faa DT |
321 | /** |
322 | * gfs2_rgrp_verify - Verify that a resource group is consistent | |
b3b94faa DT |
323 | * @rgd: the rgrp |
324 | * | |
325 | */ | |
326 | ||
327 | void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd) | |
328 | { | |
329 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
330 | struct gfs2_bitmap *bi = NULL; | |
bb8d8a6f | 331 | u32 length = rgd->rd_length; |
cd915493 | 332 | u32 count[4], tmp; |
b3b94faa DT |
333 | int buf, x; |
334 | ||
cd915493 | 335 | memset(count, 0, 4 * sizeof(u32)); |
b3b94faa DT |
336 | |
337 | /* Count # blocks in each of 4 possible allocation states */ | |
338 | for (buf = 0; buf < length; buf++) { | |
339 | bi = rgd->rd_bits + buf; | |
340 | for (x = 0; x < 4; x++) | |
341 | count[x] += gfs2_bitcount(rgd, | |
342 | bi->bi_bh->b_data + | |
343 | bi->bi_offset, | |
344 | bi->bi_len, x); | |
345 | } | |
346 | ||
cfc8b549 | 347 | if (count[0] != rgd->rd_free) { |
b3b94faa DT |
348 | if (gfs2_consist_rgrpd(rgd)) |
349 | fs_err(sdp, "free data mismatch: %u != %u\n", | |
cfc8b549 | 350 | count[0], rgd->rd_free); |
b3b94faa DT |
351 | return; |
352 | } | |
353 | ||
73f74948 | 354 | tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes; |
6b946170 | 355 | if (count[1] != tmp) { |
b3b94faa DT |
356 | if (gfs2_consist_rgrpd(rgd)) |
357 | fs_err(sdp, "used data mismatch: %u != %u\n", | |
358 | count[1], tmp); | |
359 | return; | |
360 | } | |
361 | ||
6b946170 | 362 | if (count[2] + count[3] != rgd->rd_dinodes) { |
b3b94faa | 363 | if (gfs2_consist_rgrpd(rgd)) |
feaa7bba | 364 | fs_err(sdp, "used metadata mismatch: %u != %u\n", |
6b946170 | 365 | count[2] + count[3], rgd->rd_dinodes); |
b3b94faa DT |
366 | return; |
367 | } | |
b3b94faa DT |
368 | } |
369 | ||
bb8d8a6f | 370 | static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block) |
b3b94faa | 371 | { |
bb8d8a6f SW |
372 | u64 first = rgd->rd_data0; |
373 | u64 last = first + rgd->rd_data; | |
16910427 | 374 | return first <= block && block < last; |
b3b94faa DT |
375 | } |
376 | ||
377 | /** | |
378 | * gfs2_blk2rgrpd - Find resource group for a given data/meta block number | |
379 | * @sdp: The GFS2 superblock | |
886b1416 BP |
380 | * @blk: The data block number |
381 | * @exact: True if this needs to be an exact match | |
b3b94faa DT |
382 | * |
383 | * Returns: The resource group, or NULL if not found | |
384 | */ | |
385 | ||
66fc061b | 386 | struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact) |
b3b94faa | 387 | { |
66fc061b | 388 | struct rb_node *n, *next; |
f75bbfb4 | 389 | struct gfs2_rgrpd *cur; |
b3b94faa DT |
390 | |
391 | spin_lock(&sdp->sd_rindex_spin); | |
66fc061b SW |
392 | n = sdp->sd_rindex_tree.rb_node; |
393 | while (n) { | |
394 | cur = rb_entry(n, struct gfs2_rgrpd, rd_node); | |
395 | next = NULL; | |
7c9ca621 | 396 | if (blk < cur->rd_addr) |
66fc061b | 397 | next = n->rb_left; |
f75bbfb4 | 398 | else if (blk >= cur->rd_data0 + cur->rd_data) |
66fc061b SW |
399 | next = n->rb_right; |
400 | if (next == NULL) { | |
b3b94faa | 401 | spin_unlock(&sdp->sd_rindex_spin); |
66fc061b SW |
402 | if (exact) { |
403 | if (blk < cur->rd_addr) | |
404 | return NULL; | |
405 | if (blk >= cur->rd_data0 + cur->rd_data) | |
406 | return NULL; | |
407 | } | |
7c9ca621 | 408 | return cur; |
b3b94faa | 409 | } |
66fc061b | 410 | n = next; |
b3b94faa | 411 | } |
b3b94faa DT |
412 | spin_unlock(&sdp->sd_rindex_spin); |
413 | ||
414 | return NULL; | |
415 | } | |
416 | ||
417 | /** | |
418 | * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem | |
419 | * @sdp: The GFS2 superblock | |
420 | * | |
421 | * Returns: The first rgrp in the filesystem | |
422 | */ | |
423 | ||
424 | struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp) | |
425 | { | |
7c9ca621 BP |
426 | const struct rb_node *n; |
427 | struct gfs2_rgrpd *rgd; | |
428 | ||
8339ee54 | 429 | spin_lock(&sdp->sd_rindex_spin); |
7c9ca621 BP |
430 | n = rb_first(&sdp->sd_rindex_tree); |
431 | rgd = rb_entry(n, struct gfs2_rgrpd, rd_node); | |
8339ee54 | 432 | spin_unlock(&sdp->sd_rindex_spin); |
7c9ca621 BP |
433 | |
434 | return rgd; | |
b3b94faa DT |
435 | } |
436 | ||
437 | /** | |
438 | * gfs2_rgrpd_get_next - get the next RG | |
886b1416 | 439 | * @rgd: the resource group descriptor |
b3b94faa DT |
440 | * |
441 | * Returns: The next rgrp | |
442 | */ | |
443 | ||
444 | struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd) | |
445 | { | |
7c9ca621 BP |
446 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
447 | const struct rb_node *n; | |
448 | ||
449 | spin_lock(&sdp->sd_rindex_spin); | |
450 | n = rb_next(&rgd->rd_node); | |
451 | if (n == NULL) | |
452 | n = rb_first(&sdp->sd_rindex_tree); | |
453 | ||
454 | if (unlikely(&rgd->rd_node == n)) { | |
455 | spin_unlock(&sdp->sd_rindex_spin); | |
b3b94faa | 456 | return NULL; |
7c9ca621 BP |
457 | } |
458 | rgd = rb_entry(n, struct gfs2_rgrpd, rd_node); | |
459 | spin_unlock(&sdp->sd_rindex_spin); | |
460 | return rgd; | |
b3b94faa DT |
461 | } |
462 | ||
8339ee54 SW |
463 | void gfs2_free_clones(struct gfs2_rgrpd *rgd) |
464 | { | |
465 | int x; | |
466 | ||
467 | for (x = 0; x < rgd->rd_length; x++) { | |
468 | struct gfs2_bitmap *bi = rgd->rd_bits + x; | |
469 | kfree(bi->bi_clone); | |
470 | bi->bi_clone = NULL; | |
471 | } | |
472 | } | |
473 | ||
0a305e49 BP |
474 | /** |
475 | * gfs2_rs_alloc - make sure we have a reservation assigned to the inode | |
476 | * @ip: the inode for this reservation | |
477 | */ | |
478 | int gfs2_rs_alloc(struct gfs2_inode *ip) | |
479 | { | |
480 | int error = 0; | |
8e2e0047 BP |
481 | struct gfs2_blkreserv *res; |
482 | ||
483 | if (ip->i_res) | |
484 | return 0; | |
485 | ||
486 | res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS); | |
487 | if (!res) | |
488 | error = -ENOMEM; | |
0a305e49 | 489 | |
4a993fb1 SW |
490 | rb_init_node(&res->rs_node); |
491 | ||
0a305e49 | 492 | down_write(&ip->i_rw_mutex); |
8e2e0047 BP |
493 | if (ip->i_res) |
494 | kmem_cache_free(gfs2_rsrv_cachep, res); | |
495 | else | |
496 | ip->i_res = res; | |
0a305e49 BP |
497 | up_write(&ip->i_rw_mutex); |
498 | return error; | |
499 | } | |
500 | ||
8e2e0047 BP |
501 | static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs) |
502 | { | |
503 | gfs2_print_dbg(seq, " r: %llu s:%llu b:%u f:%u\n", | |
4a993fb1 SW |
504 | rs->rs_rbm.rgd->rd_addr, gfs2_rbm_to_block(&rs->rs_rbm), |
505 | rs->rs_rbm.offset, rs->rs_free); | |
8e2e0047 BP |
506 | } |
507 | ||
0a305e49 | 508 | /** |
8e2e0047 BP |
509 | * __rs_deltree - remove a multi-block reservation from the rgd tree |
510 | * @rs: The reservation to remove | |
511 | * | |
512 | */ | |
4a993fb1 | 513 | static void __rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs) |
8e2e0047 BP |
514 | { |
515 | struct gfs2_rgrpd *rgd; | |
516 | ||
517 | if (!gfs2_rs_active(rs)) | |
518 | return; | |
519 | ||
4a993fb1 SW |
520 | rgd = rs->rs_rbm.rgd; |
521 | trace_gfs2_rs(ip, rs, TRACE_RS_TREEDEL); | |
522 | rb_erase(&rs->rs_node, &rgd->rd_rstree); | |
523 | rb_init_node(&rs->rs_node); | |
8e2e0047 BP |
524 | BUG_ON(!rgd->rd_rs_cnt); |
525 | rgd->rd_rs_cnt--; | |
526 | ||
527 | if (rs->rs_free) { | |
528 | /* return reserved blocks to the rgrp and the ip */ | |
4a993fb1 SW |
529 | BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free); |
530 | rs->rs_rbm.rgd->rd_reserved -= rs->rs_free; | |
8e2e0047 | 531 | rs->rs_free = 0; |
4a993fb1 | 532 | clear_bit(GBF_FULL, &rs->rs_rbm.bi->bi_flags); |
8e2e0047 BP |
533 | smp_mb__after_clear_bit(); |
534 | } | |
8e2e0047 BP |
535 | } |
536 | ||
537 | /** | |
538 | * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree | |
539 | * @rs: The reservation to remove | |
540 | * | |
541 | */ | |
4a993fb1 | 542 | void gfs2_rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs) |
8e2e0047 BP |
543 | { |
544 | struct gfs2_rgrpd *rgd; | |
545 | ||
4a993fb1 SW |
546 | rgd = rs->rs_rbm.rgd; |
547 | if (rgd) { | |
548 | spin_lock(&rgd->rd_rsspin); | |
549 | __rs_deltree(ip, rs); | |
550 | spin_unlock(&rgd->rd_rsspin); | |
551 | } | |
8e2e0047 BP |
552 | } |
553 | ||
554 | /** | |
555 | * gfs2_rs_delete - delete a multi-block reservation | |
0a305e49 BP |
556 | * @ip: The inode for this reservation |
557 | * | |
558 | */ | |
559 | void gfs2_rs_delete(struct gfs2_inode *ip) | |
560 | { | |
561 | down_write(&ip->i_rw_mutex); | |
562 | if (ip->i_res) { | |
4a993fb1 | 563 | gfs2_rs_deltree(ip, ip->i_res); |
8e2e0047 BP |
564 | trace_gfs2_rs(ip, ip->i_res, TRACE_RS_DELETE); |
565 | BUG_ON(ip->i_res->rs_free); | |
0a305e49 BP |
566 | kmem_cache_free(gfs2_rsrv_cachep, ip->i_res); |
567 | ip->i_res = NULL; | |
568 | } | |
569 | up_write(&ip->i_rw_mutex); | |
570 | } | |
571 | ||
8e2e0047 BP |
572 | /** |
573 | * return_all_reservations - return all reserved blocks back to the rgrp. | |
574 | * @rgd: the rgrp that needs its space back | |
575 | * | |
576 | * We previously reserved a bunch of blocks for allocation. Now we need to | |
577 | * give them back. This leave the reservation structures in tact, but removes | |
578 | * all of their corresponding "no-fly zones". | |
579 | */ | |
580 | static void return_all_reservations(struct gfs2_rgrpd *rgd) | |
581 | { | |
582 | struct rb_node *n; | |
583 | struct gfs2_blkreserv *rs; | |
584 | ||
585 | spin_lock(&rgd->rd_rsspin); | |
586 | while ((n = rb_first(&rgd->rd_rstree))) { | |
587 | rs = rb_entry(n, struct gfs2_blkreserv, rs_node); | |
4a993fb1 | 588 | __rs_deltree(NULL, rs); |
8e2e0047 BP |
589 | } |
590 | spin_unlock(&rgd->rd_rsspin); | |
591 | } | |
592 | ||
8339ee54 | 593 | void gfs2_clear_rgrpd(struct gfs2_sbd *sdp) |
b3b94faa | 594 | { |
7c9ca621 | 595 | struct rb_node *n; |
b3b94faa DT |
596 | struct gfs2_rgrpd *rgd; |
597 | struct gfs2_glock *gl; | |
598 | ||
7c9ca621 BP |
599 | while ((n = rb_first(&sdp->sd_rindex_tree))) { |
600 | rgd = rb_entry(n, struct gfs2_rgrpd, rd_node); | |
b3b94faa DT |
601 | gl = rgd->rd_gl; |
602 | ||
7c9ca621 | 603 | rb_erase(n, &sdp->sd_rindex_tree); |
b3b94faa DT |
604 | |
605 | if (gl) { | |
8339ee54 | 606 | spin_lock(&gl->gl_spin); |
5c676f6d | 607 | gl->gl_object = NULL; |
8339ee54 | 608 | spin_unlock(&gl->gl_spin); |
29687a2a | 609 | gfs2_glock_add_to_lru(gl); |
b3b94faa DT |
610 | gfs2_glock_put(gl); |
611 | } | |
612 | ||
8339ee54 | 613 | gfs2_free_clones(rgd); |
b3b94faa | 614 | kfree(rgd->rd_bits); |
8e2e0047 | 615 | return_all_reservations(rgd); |
6bdd9be6 | 616 | kmem_cache_free(gfs2_rgrpd_cachep, rgd); |
b3b94faa DT |
617 | } |
618 | } | |
619 | ||
bb8d8a6f SW |
620 | static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd) |
621 | { | |
622 | printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr); | |
623 | printk(KERN_INFO " ri_length = %u\n", rgd->rd_length); | |
624 | printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0); | |
625 | printk(KERN_INFO " ri_data = %u\n", rgd->rd_data); | |
626 | printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes); | |
627 | } | |
628 | ||
b3b94faa DT |
629 | /** |
630 | * gfs2_compute_bitstructs - Compute the bitmap sizes | |
631 | * @rgd: The resource group descriptor | |
632 | * | |
633 | * Calculates bitmap descriptors, one for each block that contains bitmap data | |
634 | * | |
635 | * Returns: errno | |
636 | */ | |
637 | ||
638 | static int compute_bitstructs(struct gfs2_rgrpd *rgd) | |
639 | { | |
640 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
641 | struct gfs2_bitmap *bi; | |
bb8d8a6f | 642 | u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */ |
cd915493 | 643 | u32 bytes_left, bytes; |
b3b94faa DT |
644 | int x; |
645 | ||
feaa7bba SW |
646 | if (!length) |
647 | return -EINVAL; | |
648 | ||
dd894be8 | 649 | rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS); |
b3b94faa DT |
650 | if (!rgd->rd_bits) |
651 | return -ENOMEM; | |
652 | ||
bb8d8a6f | 653 | bytes_left = rgd->rd_bitbytes; |
b3b94faa DT |
654 | |
655 | for (x = 0; x < length; x++) { | |
656 | bi = rgd->rd_bits + x; | |
657 | ||
60a0b8f9 | 658 | bi->bi_flags = 0; |
b3b94faa DT |
659 | /* small rgrp; bitmap stored completely in header block */ |
660 | if (length == 1) { | |
661 | bytes = bytes_left; | |
662 | bi->bi_offset = sizeof(struct gfs2_rgrp); | |
663 | bi->bi_start = 0; | |
664 | bi->bi_len = bytes; | |
665 | /* header block */ | |
666 | } else if (x == 0) { | |
667 | bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp); | |
668 | bi->bi_offset = sizeof(struct gfs2_rgrp); | |
669 | bi->bi_start = 0; | |
670 | bi->bi_len = bytes; | |
671 | /* last block */ | |
672 | } else if (x + 1 == length) { | |
673 | bytes = bytes_left; | |
674 | bi->bi_offset = sizeof(struct gfs2_meta_header); | |
bb8d8a6f | 675 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
b3b94faa DT |
676 | bi->bi_len = bytes; |
677 | /* other blocks */ | |
678 | } else { | |
568f4c96 SW |
679 | bytes = sdp->sd_sb.sb_bsize - |
680 | sizeof(struct gfs2_meta_header); | |
b3b94faa | 681 | bi->bi_offset = sizeof(struct gfs2_meta_header); |
bb8d8a6f | 682 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
b3b94faa DT |
683 | bi->bi_len = bytes; |
684 | } | |
685 | ||
686 | bytes_left -= bytes; | |
687 | } | |
688 | ||
689 | if (bytes_left) { | |
690 | gfs2_consist_rgrpd(rgd); | |
691 | return -EIO; | |
692 | } | |
693 | bi = rgd->rd_bits + (length - 1); | |
bb8d8a6f | 694 | if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) { |
b3b94faa | 695 | if (gfs2_consist_rgrpd(rgd)) { |
bb8d8a6f | 696 | gfs2_rindex_print(rgd); |
b3b94faa DT |
697 | fs_err(sdp, "start=%u len=%u offset=%u\n", |
698 | bi->bi_start, bi->bi_len, bi->bi_offset); | |
699 | } | |
700 | return -EIO; | |
701 | } | |
702 | ||
703 | return 0; | |
704 | } | |
705 | ||
7ae8fa84 RP |
706 | /** |
707 | * gfs2_ri_total - Total up the file system space, according to the rindex. | |
886b1416 | 708 | * @sdp: the filesystem |
7ae8fa84 RP |
709 | * |
710 | */ | |
711 | u64 gfs2_ri_total(struct gfs2_sbd *sdp) | |
712 | { | |
713 | u64 total_data = 0; | |
714 | struct inode *inode = sdp->sd_rindex; | |
715 | struct gfs2_inode *ip = GFS2_I(inode); | |
7ae8fa84 | 716 | char buf[sizeof(struct gfs2_rindex)]; |
7ae8fa84 RP |
717 | int error, rgrps; |
718 | ||
7ae8fa84 RP |
719 | for (rgrps = 0;; rgrps++) { |
720 | loff_t pos = rgrps * sizeof(struct gfs2_rindex); | |
721 | ||
bcd7278d | 722 | if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode)) |
7ae8fa84 | 723 | break; |
4306629e | 724 | error = gfs2_internal_read(ip, buf, &pos, |
7ae8fa84 RP |
725 | sizeof(struct gfs2_rindex)); |
726 | if (error != sizeof(struct gfs2_rindex)) | |
727 | break; | |
bb8d8a6f | 728 | total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data); |
7ae8fa84 | 729 | } |
7ae8fa84 RP |
730 | return total_data; |
731 | } | |
732 | ||
6aad1c3d | 733 | static int rgd_insert(struct gfs2_rgrpd *rgd) |
7c9ca621 BP |
734 | { |
735 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
736 | struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL; | |
737 | ||
738 | /* Figure out where to put new node */ | |
739 | while (*newn) { | |
740 | struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd, | |
741 | rd_node); | |
742 | ||
743 | parent = *newn; | |
744 | if (rgd->rd_addr < cur->rd_addr) | |
745 | newn = &((*newn)->rb_left); | |
746 | else if (rgd->rd_addr > cur->rd_addr) | |
747 | newn = &((*newn)->rb_right); | |
748 | else | |
6aad1c3d | 749 | return -EEXIST; |
7c9ca621 BP |
750 | } |
751 | ||
752 | rb_link_node(&rgd->rd_node, parent, newn); | |
753 | rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree); | |
6aad1c3d BP |
754 | sdp->sd_rgrps++; |
755 | return 0; | |
7c9ca621 BP |
756 | } |
757 | ||
b3b94faa | 758 | /** |
6c53267f | 759 | * read_rindex_entry - Pull in a new resource index entry from the disk |
4306629e | 760 | * @ip: Pointer to the rindex inode |
b3b94faa | 761 | * |
8339ee54 | 762 | * Returns: 0 on success, > 0 on EOF, error code otherwise |
6c53267f RP |
763 | */ |
764 | ||
4306629e | 765 | static int read_rindex_entry(struct gfs2_inode *ip) |
6c53267f RP |
766 | { |
767 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | |
768 | loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex); | |
8339ee54 | 769 | struct gfs2_rindex buf; |
6c53267f RP |
770 | int error; |
771 | struct gfs2_rgrpd *rgd; | |
772 | ||
8339ee54 SW |
773 | if (pos >= i_size_read(&ip->i_inode)) |
774 | return 1; | |
775 | ||
4306629e | 776 | error = gfs2_internal_read(ip, (char *)&buf, &pos, |
6c53267f | 777 | sizeof(struct gfs2_rindex)); |
8339ee54 SW |
778 | |
779 | if (error != sizeof(struct gfs2_rindex)) | |
780 | return (error == 0) ? 1 : error; | |
6c53267f | 781 | |
6bdd9be6 | 782 | rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS); |
6c53267f RP |
783 | error = -ENOMEM; |
784 | if (!rgd) | |
785 | return error; | |
786 | ||
6c53267f | 787 | rgd->rd_sbd = sdp; |
8339ee54 SW |
788 | rgd->rd_addr = be64_to_cpu(buf.ri_addr); |
789 | rgd->rd_length = be32_to_cpu(buf.ri_length); | |
790 | rgd->rd_data0 = be64_to_cpu(buf.ri_data0); | |
791 | rgd->rd_data = be32_to_cpu(buf.ri_data); | |
792 | rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes); | |
8e2e0047 | 793 | spin_lock_init(&rgd->rd_rsspin); |
7c9ca621 | 794 | |
6c53267f RP |
795 | error = compute_bitstructs(rgd); |
796 | if (error) | |
8339ee54 | 797 | goto fail; |
6c53267f | 798 | |
bb8d8a6f | 799 | error = gfs2_glock_get(sdp, rgd->rd_addr, |
6c53267f RP |
800 | &gfs2_rgrp_glops, CREATE, &rgd->rd_gl); |
801 | if (error) | |
8339ee54 | 802 | goto fail; |
6c53267f RP |
803 | |
804 | rgd->rd_gl->gl_object = rgd; | |
90306c41 | 805 | rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lvb; |
cf45b752 | 806 | rgd->rd_flags &= ~GFS2_RDF_UPTODATE; |
7c9ca621 BP |
807 | if (rgd->rd_data > sdp->sd_max_rg_data) |
808 | sdp->sd_max_rg_data = rgd->rd_data; | |
8339ee54 | 809 | spin_lock(&sdp->sd_rindex_spin); |
6aad1c3d | 810 | error = rgd_insert(rgd); |
8339ee54 | 811 | spin_unlock(&sdp->sd_rindex_spin); |
6aad1c3d BP |
812 | if (!error) |
813 | return 0; | |
814 | ||
815 | error = 0; /* someone else read in the rgrp; free it and ignore it */ | |
c1ac539e | 816 | gfs2_glock_put(rgd->rd_gl); |
8339ee54 SW |
817 | |
818 | fail: | |
819 | kfree(rgd->rd_bits); | |
820 | kmem_cache_free(gfs2_rgrpd_cachep, rgd); | |
6c53267f RP |
821 | return error; |
822 | } | |
823 | ||
824 | /** | |
825 | * gfs2_ri_update - Pull in a new resource index from the disk | |
826 | * @ip: pointer to the rindex inode | |
827 | * | |
b3b94faa DT |
828 | * Returns: 0 on successful update, error code otherwise |
829 | */ | |
830 | ||
8339ee54 | 831 | static int gfs2_ri_update(struct gfs2_inode *ip) |
b3b94faa | 832 | { |
feaa7bba | 833 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
834 | int error; |
835 | ||
8339ee54 | 836 | do { |
4306629e | 837 | error = read_rindex_entry(ip); |
8339ee54 SW |
838 | } while (error == 0); |
839 | ||
840 | if (error < 0) | |
841 | return error; | |
b3b94faa | 842 | |
cf45b752 | 843 | sdp->sd_rindex_uptodate = 1; |
6c53267f RP |
844 | return 0; |
845 | } | |
b3b94faa | 846 | |
b3b94faa | 847 | /** |
8339ee54 | 848 | * gfs2_rindex_update - Update the rindex if required |
b3b94faa | 849 | * @sdp: The GFS2 superblock |
b3b94faa DT |
850 | * |
851 | * We grab a lock on the rindex inode to make sure that it doesn't | |
852 | * change whilst we are performing an operation. We keep this lock | |
853 | * for quite long periods of time compared to other locks. This | |
854 | * doesn't matter, since it is shared and it is very, very rarely | |
855 | * accessed in the exclusive mode (i.e. only when expanding the filesystem). | |
856 | * | |
857 | * This makes sure that we're using the latest copy of the resource index | |
858 | * special file, which might have been updated if someone expanded the | |
859 | * filesystem (via gfs2_grow utility), which adds new resource groups. | |
860 | * | |
8339ee54 | 861 | * Returns: 0 on succeess, error code otherwise |
b3b94faa DT |
862 | */ |
863 | ||
8339ee54 | 864 | int gfs2_rindex_update(struct gfs2_sbd *sdp) |
b3b94faa | 865 | { |
feaa7bba | 866 | struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex); |
b3b94faa | 867 | struct gfs2_glock *gl = ip->i_gl; |
8339ee54 SW |
868 | struct gfs2_holder ri_gh; |
869 | int error = 0; | |
a365fbf3 | 870 | int unlock_required = 0; |
b3b94faa DT |
871 | |
872 | /* Read new copy from disk if we don't have the latest */ | |
cf45b752 | 873 | if (!sdp->sd_rindex_uptodate) { |
a365fbf3 SW |
874 | if (!gfs2_glock_is_locked_by_me(gl)) { |
875 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh); | |
876 | if (error) | |
6aad1c3d | 877 | return error; |
a365fbf3 SW |
878 | unlock_required = 1; |
879 | } | |
8339ee54 | 880 | if (!sdp->sd_rindex_uptodate) |
b3b94faa | 881 | error = gfs2_ri_update(ip); |
a365fbf3 SW |
882 | if (unlock_required) |
883 | gfs2_glock_dq_uninit(&ri_gh); | |
b3b94faa DT |
884 | } |
885 | ||
886 | return error; | |
887 | } | |
888 | ||
42d52e38 | 889 | static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf) |
bb8d8a6f SW |
890 | { |
891 | const struct gfs2_rgrp *str = buf; | |
42d52e38 | 892 | u32 rg_flags; |
bb8d8a6f | 893 | |
42d52e38 | 894 | rg_flags = be32_to_cpu(str->rg_flags); |
09010978 | 895 | rg_flags &= ~GFS2_RDF_MASK; |
1ce97e56 SW |
896 | rgd->rd_flags &= GFS2_RDF_MASK; |
897 | rgd->rd_flags |= rg_flags; | |
cfc8b549 | 898 | rgd->rd_free = be32_to_cpu(str->rg_free); |
73f74948 | 899 | rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes); |
d8b71f73 | 900 | rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration); |
bb8d8a6f SW |
901 | } |
902 | ||
42d52e38 | 903 | static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf) |
bb8d8a6f SW |
904 | { |
905 | struct gfs2_rgrp *str = buf; | |
906 | ||
09010978 | 907 | str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK); |
cfc8b549 | 908 | str->rg_free = cpu_to_be32(rgd->rd_free); |
73f74948 | 909 | str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes); |
bb8d8a6f | 910 | str->__pad = cpu_to_be32(0); |
d8b71f73 | 911 | str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration); |
bb8d8a6f SW |
912 | memset(&str->rg_reserved, 0, sizeof(str->rg_reserved)); |
913 | } | |
914 | ||
90306c41 BM |
915 | static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd) |
916 | { | |
917 | struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; | |
918 | struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data; | |
919 | ||
920 | if (rgl->rl_flags != str->rg_flags || rgl->rl_free != str->rg_free || | |
921 | rgl->rl_dinodes != str->rg_dinodes || | |
922 | rgl->rl_igeneration != str->rg_igeneration) | |
923 | return 0; | |
924 | return 1; | |
925 | } | |
926 | ||
927 | static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf) | |
928 | { | |
929 | const struct gfs2_rgrp *str = buf; | |
930 | ||
931 | rgl->rl_magic = cpu_to_be32(GFS2_MAGIC); | |
932 | rgl->rl_flags = str->rg_flags; | |
933 | rgl->rl_free = str->rg_free; | |
934 | rgl->rl_dinodes = str->rg_dinodes; | |
935 | rgl->rl_igeneration = str->rg_igeneration; | |
936 | rgl->__pad = 0UL; | |
937 | } | |
938 | ||
939 | static void update_rgrp_lvb_unlinked(struct gfs2_rgrpd *rgd, u32 change) | |
940 | { | |
941 | struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; | |
942 | u32 unlinked = be32_to_cpu(rgl->rl_unlinked) + change; | |
943 | rgl->rl_unlinked = cpu_to_be32(unlinked); | |
944 | } | |
945 | ||
946 | static u32 count_unlinked(struct gfs2_rgrpd *rgd) | |
947 | { | |
948 | struct gfs2_bitmap *bi; | |
949 | const u32 length = rgd->rd_length; | |
950 | const u8 *buffer = NULL; | |
951 | u32 i, goal, count = 0; | |
952 | ||
953 | for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) { | |
954 | goal = 0; | |
955 | buffer = bi->bi_bh->b_data + bi->bi_offset; | |
956 | WARN_ON(!buffer_uptodate(bi->bi_bh)); | |
957 | while (goal < bi->bi_len * GFS2_NBBY) { | |
958 | goal = gfs2_bitfit(buffer, bi->bi_len, goal, | |
959 | GFS2_BLKST_UNLINKED); | |
960 | if (goal == BFITNOENT) | |
961 | break; | |
962 | count++; | |
963 | goal++; | |
964 | } | |
965 | } | |
966 | ||
967 | return count; | |
968 | } | |
969 | ||
970 | ||
b3b94faa | 971 | /** |
90306c41 BM |
972 | * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps |
973 | * @rgd: the struct gfs2_rgrpd describing the RG to read in | |
b3b94faa DT |
974 | * |
975 | * Read in all of a Resource Group's header and bitmap blocks. | |
976 | * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps. | |
977 | * | |
978 | * Returns: errno | |
979 | */ | |
980 | ||
90306c41 | 981 | int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd) |
b3b94faa DT |
982 | { |
983 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
984 | struct gfs2_glock *gl = rgd->rd_gl; | |
bb8d8a6f | 985 | unsigned int length = rgd->rd_length; |
b3b94faa DT |
986 | struct gfs2_bitmap *bi; |
987 | unsigned int x, y; | |
988 | int error; | |
989 | ||
90306c41 BM |
990 | if (rgd->rd_bits[0].bi_bh != NULL) |
991 | return 0; | |
992 | ||
b3b94faa DT |
993 | for (x = 0; x < length; x++) { |
994 | bi = rgd->rd_bits + x; | |
bb8d8a6f | 995 | error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh); |
b3b94faa DT |
996 | if (error) |
997 | goto fail; | |
998 | } | |
999 | ||
1000 | for (y = length; y--;) { | |
1001 | bi = rgd->rd_bits + y; | |
7276b3b0 | 1002 | error = gfs2_meta_wait(sdp, bi->bi_bh); |
b3b94faa DT |
1003 | if (error) |
1004 | goto fail; | |
feaa7bba | 1005 | if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB : |
b3b94faa DT |
1006 | GFS2_METATYPE_RG)) { |
1007 | error = -EIO; | |
1008 | goto fail; | |
1009 | } | |
1010 | } | |
1011 | ||
cf45b752 | 1012 | if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) { |
60a0b8f9 SW |
1013 | for (x = 0; x < length; x++) |
1014 | clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags); | |
42d52e38 | 1015 | gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data); |
1ce97e56 | 1016 | rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK); |
7c9ca621 | 1017 | rgd->rd_free_clone = rgd->rd_free; |
b3b94faa | 1018 | } |
90306c41 BM |
1019 | if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) { |
1020 | rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd)); | |
1021 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, | |
1022 | rgd->rd_bits[0].bi_bh->b_data); | |
1023 | } | |
1024 | else if (sdp->sd_args.ar_rgrplvb) { | |
1025 | if (!gfs2_rgrp_lvb_valid(rgd)){ | |
1026 | gfs2_consist_rgrpd(rgd); | |
1027 | error = -EIO; | |
1028 | goto fail; | |
1029 | } | |
1030 | if (rgd->rd_rgl->rl_unlinked == 0) | |
1031 | rgd->rd_flags &= ~GFS2_RDF_CHECK; | |
1032 | } | |
b3b94faa DT |
1033 | return 0; |
1034 | ||
feaa7bba | 1035 | fail: |
b3b94faa DT |
1036 | while (x--) { |
1037 | bi = rgd->rd_bits + x; | |
1038 | brelse(bi->bi_bh); | |
1039 | bi->bi_bh = NULL; | |
1040 | gfs2_assert_warn(sdp, !bi->bi_clone); | |
1041 | } | |
b3b94faa DT |
1042 | |
1043 | return error; | |
1044 | } | |
1045 | ||
90306c41 BM |
1046 | int update_rgrp_lvb(struct gfs2_rgrpd *rgd) |
1047 | { | |
1048 | u32 rl_flags; | |
1049 | ||
1050 | if (rgd->rd_flags & GFS2_RDF_UPTODATE) | |
1051 | return 0; | |
1052 | ||
1053 | if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) | |
1054 | return gfs2_rgrp_bh_get(rgd); | |
1055 | ||
1056 | rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags); | |
1057 | rl_flags &= ~GFS2_RDF_MASK; | |
1058 | rgd->rd_flags &= GFS2_RDF_MASK; | |
1059 | rgd->rd_flags |= (rl_flags | GFS2_RDF_UPTODATE | GFS2_RDF_CHECK); | |
1060 | if (rgd->rd_rgl->rl_unlinked == 0) | |
1061 | rgd->rd_flags &= ~GFS2_RDF_CHECK; | |
1062 | rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free); | |
1063 | rgd->rd_free_clone = rgd->rd_free; | |
1064 | rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes); | |
1065 | rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration); | |
1066 | return 0; | |
1067 | } | |
1068 | ||
1069 | int gfs2_rgrp_go_lock(struct gfs2_holder *gh) | |
1070 | { | |
1071 | struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object; | |
1072 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
1073 | ||
1074 | if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb) | |
1075 | return 0; | |
1076 | return gfs2_rgrp_bh_get((struct gfs2_rgrpd *)gh->gh_gl->gl_object); | |
1077 | } | |
1078 | ||
b3b94faa | 1079 | /** |
7c9ca621 | 1080 | * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get() |
886b1416 | 1081 | * @gh: The glock holder for the resource group |
b3b94faa DT |
1082 | * |
1083 | */ | |
1084 | ||
7c9ca621 | 1085 | void gfs2_rgrp_go_unlock(struct gfs2_holder *gh) |
b3b94faa | 1086 | { |
7c9ca621 | 1087 | struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object; |
bb8d8a6f | 1088 | int x, length = rgd->rd_length; |
b3b94faa | 1089 | |
b3b94faa DT |
1090 | for (x = 0; x < length; x++) { |
1091 | struct gfs2_bitmap *bi = rgd->rd_bits + x; | |
90306c41 BM |
1092 | if (bi->bi_bh) { |
1093 | brelse(bi->bi_bh); | |
1094 | bi->bi_bh = NULL; | |
1095 | } | |
b3b94faa DT |
1096 | } |
1097 | ||
b3b94faa DT |
1098 | } |
1099 | ||
66fc061b | 1100 | int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset, |
7c9ca621 | 1101 | struct buffer_head *bh, |
66fc061b | 1102 | const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed) |
f15ab561 SW |
1103 | { |
1104 | struct super_block *sb = sdp->sd_vfs; | |
1105 | struct block_device *bdev = sb->s_bdev; | |
1106 | const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize / | |
e1defc4f | 1107 | bdev_logical_block_size(sb->s_bdev); |
f15ab561 | 1108 | u64 blk; |
64d576ba | 1109 | sector_t start = 0; |
f15ab561 SW |
1110 | sector_t nr_sects = 0; |
1111 | int rv; | |
1112 | unsigned int x; | |
66fc061b SW |
1113 | u32 trimmed = 0; |
1114 | u8 diff; | |
f15ab561 SW |
1115 | |
1116 | for (x = 0; x < bi->bi_len; x++) { | |
66fc061b SW |
1117 | const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data; |
1118 | clone += bi->bi_offset; | |
1119 | clone += x; | |
1120 | if (bh) { | |
1121 | const u8 *orig = bh->b_data + bi->bi_offset + x; | |
1122 | diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1)); | |
1123 | } else { | |
1124 | diff = ~(*clone | (*clone >> 1)); | |
1125 | } | |
f15ab561 SW |
1126 | diff &= 0x55; |
1127 | if (diff == 0) | |
1128 | continue; | |
1129 | blk = offset + ((bi->bi_start + x) * GFS2_NBBY); | |
1130 | blk *= sects_per_blk; /* convert to sectors */ | |
1131 | while(diff) { | |
1132 | if (diff & 1) { | |
1133 | if (nr_sects == 0) | |
1134 | goto start_new_extent; | |
1135 | if ((start + nr_sects) != blk) { | |
66fc061b SW |
1136 | if (nr_sects >= minlen) { |
1137 | rv = blkdev_issue_discard(bdev, | |
1138 | start, nr_sects, | |
1139 | GFP_NOFS, 0); | |
1140 | if (rv) | |
1141 | goto fail; | |
1142 | trimmed += nr_sects; | |
1143 | } | |
f15ab561 SW |
1144 | nr_sects = 0; |
1145 | start_new_extent: | |
1146 | start = blk; | |
1147 | } | |
1148 | nr_sects += sects_per_blk; | |
1149 | } | |
1150 | diff >>= 2; | |
1151 | blk += sects_per_blk; | |
1152 | } | |
1153 | } | |
66fc061b | 1154 | if (nr_sects >= minlen) { |
dd3932ed | 1155 | rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0); |
f15ab561 SW |
1156 | if (rv) |
1157 | goto fail; | |
66fc061b | 1158 | trimmed += nr_sects; |
f15ab561 | 1159 | } |
66fc061b SW |
1160 | if (ptrimmed) |
1161 | *ptrimmed = trimmed; | |
1162 | return 0; | |
1163 | ||
f15ab561 | 1164 | fail: |
66fc061b SW |
1165 | if (sdp->sd_args.ar_discard) |
1166 | fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv); | |
f15ab561 | 1167 | sdp->sd_args.ar_discard = 0; |
66fc061b SW |
1168 | return -EIO; |
1169 | } | |
1170 | ||
1171 | /** | |
1172 | * gfs2_fitrim - Generate discard requests for unused bits of the filesystem | |
1173 | * @filp: Any file on the filesystem | |
1174 | * @argp: Pointer to the arguments (also used to pass result) | |
1175 | * | |
1176 | * Returns: 0 on success, otherwise error code | |
1177 | */ | |
1178 | ||
1179 | int gfs2_fitrim(struct file *filp, void __user *argp) | |
1180 | { | |
1181 | struct inode *inode = filp->f_dentry->d_inode; | |
1182 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
1183 | struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev); | |
1184 | struct buffer_head *bh; | |
1185 | struct gfs2_rgrpd *rgd; | |
1186 | struct gfs2_rgrpd *rgd_end; | |
1187 | struct gfs2_holder gh; | |
1188 | struct fstrim_range r; | |
1189 | int ret = 0; | |
1190 | u64 amt; | |
1191 | u64 trimmed = 0; | |
1192 | unsigned int x; | |
1193 | ||
1194 | if (!capable(CAP_SYS_ADMIN)) | |
1195 | return -EPERM; | |
1196 | ||
1197 | if (!blk_queue_discard(q)) | |
1198 | return -EOPNOTSUPP; | |
1199 | ||
66fc061b SW |
1200 | if (argp == NULL) { |
1201 | r.start = 0; | |
1202 | r.len = ULLONG_MAX; | |
1203 | r.minlen = 0; | |
1204 | } else if (copy_from_user(&r, argp, sizeof(r))) | |
1205 | return -EFAULT; | |
1206 | ||
5e2f7d61 BP |
1207 | ret = gfs2_rindex_update(sdp); |
1208 | if (ret) | |
1209 | return ret; | |
1210 | ||
66fc061b SW |
1211 | rgd = gfs2_blk2rgrpd(sdp, r.start, 0); |
1212 | rgd_end = gfs2_blk2rgrpd(sdp, r.start + r.len, 0); | |
1213 | ||
1214 | while (1) { | |
1215 | ||
1216 | ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh); | |
1217 | if (ret) | |
1218 | goto out; | |
1219 | ||
1220 | if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) { | |
1221 | /* Trim each bitmap in the rgrp */ | |
1222 | for (x = 0; x < rgd->rd_length; x++) { | |
1223 | struct gfs2_bitmap *bi = rgd->rd_bits + x; | |
1224 | ret = gfs2_rgrp_send_discards(sdp, rgd->rd_data0, NULL, bi, r.minlen, &amt); | |
1225 | if (ret) { | |
1226 | gfs2_glock_dq_uninit(&gh); | |
1227 | goto out; | |
1228 | } | |
1229 | trimmed += amt; | |
1230 | } | |
1231 | ||
1232 | /* Mark rgrp as having been trimmed */ | |
1233 | ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0); | |
1234 | if (ret == 0) { | |
1235 | bh = rgd->rd_bits[0].bi_bh; | |
1236 | rgd->rd_flags |= GFS2_RGF_TRIMMED; | |
1237 | gfs2_trans_add_bh(rgd->rd_gl, bh, 1); | |
1238 | gfs2_rgrp_out(rgd, bh->b_data); | |
90306c41 | 1239 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, bh->b_data); |
66fc061b SW |
1240 | gfs2_trans_end(sdp); |
1241 | } | |
1242 | } | |
1243 | gfs2_glock_dq_uninit(&gh); | |
1244 | ||
1245 | if (rgd == rgd_end) | |
1246 | break; | |
1247 | ||
1248 | rgd = gfs2_rgrpd_get_next(rgd); | |
1249 | } | |
1250 | ||
1251 | out: | |
1252 | r.len = trimmed << 9; | |
1253 | if (argp && copy_to_user(argp, &r, sizeof(r))) | |
1254 | return -EFAULT; | |
1255 | ||
1256 | return ret; | |
f15ab561 SW |
1257 | } |
1258 | ||
8e2e0047 BP |
1259 | /** |
1260 | * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree | |
1261 | * @bi: the bitmap with the blocks | |
1262 | * @ip: the inode structure | |
1263 | * @biblk: the 32-bit block number relative to the start of the bitmap | |
1264 | * @amount: the number of blocks to reserve | |
1265 | * | |
1266 | * Returns: NULL - reservation was already taken, so not inserted | |
1267 | * pointer to the inserted reservation | |
1268 | */ | |
1269 | static struct gfs2_blkreserv *rs_insert(struct gfs2_bitmap *bi, | |
1270 | struct gfs2_inode *ip, u32 biblk, | |
1271 | int amount) | |
1272 | { | |
1273 | struct rb_node **newn, *parent = NULL; | |
1274 | int rc; | |
1275 | struct gfs2_blkreserv *rs = ip->i_res; | |
4a993fb1 | 1276 | struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd; |
8e2e0047 BP |
1277 | u64 fsblock = gfs2_bi2rgd_blk(bi, biblk) + rgd->rd_data0; |
1278 | ||
1279 | spin_lock(&rgd->rd_rsspin); | |
1280 | newn = &rgd->rd_rstree.rb_node; | |
1281 | BUG_ON(!ip->i_res); | |
1282 | BUG_ON(gfs2_rs_active(rs)); | |
1283 | /* Figure out where to put new node */ | |
1284 | /*BUG_ON(!gfs2_glock_is_locked_by_me(rgd->rd_gl));*/ | |
1285 | while (*newn) { | |
1286 | struct gfs2_blkreserv *cur = | |
1287 | rb_entry(*newn, struct gfs2_blkreserv, rs_node); | |
1288 | ||
1289 | parent = *newn; | |
1290 | rc = rs_cmp(fsblock, amount, cur); | |
1291 | if (rc > 0) | |
1292 | newn = &((*newn)->rb_right); | |
1293 | else if (rc < 0) | |
1294 | newn = &((*newn)->rb_left); | |
1295 | else { | |
1296 | spin_unlock(&rgd->rd_rsspin); | |
1297 | return NULL; /* reservation already in use */ | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | /* Do our reservation work */ | |
1302 | rs = ip->i_res; | |
1303 | rs->rs_free = amount; | |
4a993fb1 SW |
1304 | rs->rs_rbm.offset = biblk; |
1305 | rs->rs_rbm.bi = bi; | |
8e2e0047 BP |
1306 | rb_link_node(&rs->rs_node, parent, newn); |
1307 | rb_insert_color(&rs->rs_node, &rgd->rd_rstree); | |
1308 | ||
1309 | /* Do our inode accounting for the reservation */ | |
1310 | /*BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));*/ | |
1311 | ||
1312 | /* Do our rgrp accounting for the reservation */ | |
1313 | rgd->rd_reserved += amount; /* blocks reserved */ | |
1314 | rgd->rd_rs_cnt++; /* number of in-tree reservations */ | |
1315 | spin_unlock(&rgd->rd_rsspin); | |
1316 | trace_gfs2_rs(ip, rs, TRACE_RS_INSERT); | |
1317 | return rs; | |
1318 | } | |
1319 | ||
1320 | /** | |
1321 | * unclaimed_blocks - return number of blocks that aren't spoken for | |
1322 | */ | |
1323 | static u32 unclaimed_blocks(struct gfs2_rgrpd *rgd) | |
1324 | { | |
1325 | return rgd->rd_free_clone - rgd->rd_reserved; | |
1326 | } | |
1327 | ||
1328 | /** | |
1329 | * rg_mblk_search - find a group of multiple free blocks | |
1330 | * @rgd: the resource group descriptor | |
1331 | * @rs: the block reservation | |
1332 | * @ip: pointer to the inode for which we're reserving blocks | |
1333 | * | |
1334 | * This is very similar to rgblk_search, except we're looking for whole | |
1335 | * 64-bit words that represent a chunk of 32 free blocks. I'm only focusing | |
1336 | * on aligned dwords for speed's sake. | |
1337 | * | |
1338 | * Returns: 0 if successful or BFITNOENT if there isn't enough free space | |
1339 | */ | |
1340 | ||
71f890f7 | 1341 | static int rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip, unsigned requested) |
8e2e0047 BP |
1342 | { |
1343 | struct gfs2_bitmap *bi = rgd->rd_bits; | |
1344 | const u32 length = rgd->rd_length; | |
1345 | u32 blk; | |
1346 | unsigned int buf, x, search_bytes; | |
1347 | u8 *buffer = NULL; | |
1348 | u8 *ptr, *end, *nonzero; | |
1349 | u32 goal, rsv_bytes; | |
1350 | struct gfs2_blkreserv *rs; | |
1351 | u32 best_rs_bytes, unclaimed; | |
1352 | int best_rs_blocks; | |
1353 | ||
1354 | /* Find bitmap block that contains bits for goal block */ | |
1355 | if (rgrp_contains_block(rgd, ip->i_goal)) | |
1356 | goal = ip->i_goal - rgd->rd_data0; | |
1357 | else | |
1358 | goal = rgd->rd_last_alloc; | |
1359 | for (buf = 0; buf < length; buf++) { | |
1360 | bi = rgd->rd_bits + buf; | |
1361 | /* Convert scope of "goal" from rgrp-wide to within | |
1362 | found bit block */ | |
1363 | if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) { | |
1364 | goal -= bi->bi_start * GFS2_NBBY; | |
1365 | goto do_search; | |
1366 | } | |
1367 | } | |
1368 | buf = 0; | |
1369 | goal = 0; | |
1370 | ||
1371 | do_search: | |
1372 | best_rs_blocks = max_t(int, atomic_read(&ip->i_res->rs_sizehint), | |
1373 | (RGRP_RSRV_MINBLKS * rgd->rd_length)); | |
1374 | best_rs_bytes = (best_rs_blocks * | |
1375 | (1 + (RSRV_CONTENTION_FACTOR * rgd->rd_rs_cnt))) / | |
1376 | GFS2_NBBY; /* 1 + is for our not-yet-created reservation */ | |
1377 | best_rs_bytes = ALIGN(best_rs_bytes, sizeof(u64)); | |
1378 | unclaimed = unclaimed_blocks(rgd); | |
1379 | if (best_rs_bytes * GFS2_NBBY > unclaimed) | |
1380 | best_rs_bytes = unclaimed >> GFS2_BIT_SIZE; | |
1381 | ||
1382 | for (x = 0; x <= length; x++) { | |
1383 | bi = rgd->rd_bits + buf; | |
1384 | ||
1385 | if (test_bit(GBF_FULL, &bi->bi_flags)) | |
1386 | goto skip; | |
1387 | ||
1388 | WARN_ON(!buffer_uptodate(bi->bi_bh)); | |
1389 | if (bi->bi_clone) | |
1390 | buffer = bi->bi_clone + bi->bi_offset; | |
1391 | else | |
1392 | buffer = bi->bi_bh->b_data + bi->bi_offset; | |
1393 | ||
1394 | /* We have to keep the reservations aligned on u64 boundaries | |
1395 | otherwise we could get situations where a byte can't be | |
1396 | used because it's after a reservation, but a free bit still | |
1397 | is within the reservation's area. */ | |
1398 | ptr = buffer + ALIGN(goal >> GFS2_BIT_SIZE, sizeof(u64)); | |
1399 | end = (buffer + bi->bi_len); | |
1400 | while (ptr < end) { | |
1401 | rsv_bytes = 0; | |
1402 | if ((ptr + best_rs_bytes) <= end) | |
1403 | search_bytes = best_rs_bytes; | |
1404 | else | |
1405 | search_bytes = end - ptr; | |
1406 | BUG_ON(!search_bytes); | |
1407 | nonzero = memchr_inv(ptr, 0, search_bytes); | |
1408 | /* If the lot is all zeroes, reserve the whole size. If | |
1409 | there's enough zeroes to satisfy the request, use | |
1410 | what we can. If there's not enough, keep looking. */ | |
1411 | if (nonzero == NULL) | |
1412 | rsv_bytes = search_bytes; | |
71f890f7 | 1413 | else if ((nonzero - ptr) * GFS2_NBBY >= requested) |
8e2e0047 BP |
1414 | rsv_bytes = (nonzero - ptr); |
1415 | ||
1416 | if (rsv_bytes) { | |
1417 | blk = ((ptr - buffer) * GFS2_NBBY); | |
1418 | BUG_ON(blk >= bi->bi_len * GFS2_NBBY); | |
1419 | rs = rs_insert(bi, ip, blk, | |
1420 | rsv_bytes * GFS2_NBBY); | |
1421 | if (IS_ERR(rs)) | |
1422 | return PTR_ERR(rs); | |
1423 | if (rs) | |
1424 | return 0; | |
1425 | } | |
1426 | ptr += ALIGN(search_bytes, sizeof(u64)); | |
1427 | } | |
1428 | skip: | |
1429 | /* Try next bitmap block (wrap back to rgrp header | |
1430 | if at end) */ | |
1431 | buf++; | |
1432 | buf %= length; | |
1433 | goal = 0; | |
1434 | } | |
1435 | ||
1436 | return BFITNOENT; | |
1437 | } | |
1438 | ||
b3b94faa DT |
1439 | /** |
1440 | * try_rgrp_fit - See if a given reservation will fit in a given RG | |
1441 | * @rgd: the RG data | |
54335b1f | 1442 | * @ip: the inode |
b3b94faa DT |
1443 | * |
1444 | * If there's room for the requested blocks to be allocated from the RG: | |
8e2e0047 BP |
1445 | * This will try to get a multi-block reservation first, and if that doesn't |
1446 | * fit, it will take what it can. | |
b3b94faa DT |
1447 | * |
1448 | * Returns: 1 on success (it fits), 0 on failure (it doesn't fit) | |
1449 | */ | |
1450 | ||
71f890f7 SW |
1451 | static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip, |
1452 | unsigned requested) | |
b3b94faa | 1453 | { |
09010978 | 1454 | if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR)) |
a43a4906 | 1455 | return 0; |
8e2e0047 BP |
1456 | /* Look for a multi-block reservation. */ |
1457 | if (unclaimed_blocks(rgd) >= RGRP_RSRV_MINBLKS && | |
71f890f7 | 1458 | rg_mblk_search(rgd, ip, requested) != BFITNOENT) |
8e2e0047 | 1459 | return 1; |
71f890f7 | 1460 | if (unclaimed_blocks(rgd) >= requested) |
7c9ca621 | 1461 | return 1; |
b3b94faa | 1462 | |
8e2e0047 | 1463 | return 0; |
b3e47ca0 BP |
1464 | } |
1465 | ||
c8cdf479 SW |
1466 | /** |
1467 | * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes | |
1468 | * @rgd: The rgrp | |
886b1416 BP |
1469 | * @last_unlinked: block address of the last dinode we unlinked |
1470 | * @skip: block address we should explicitly not unlink | |
c8cdf479 | 1471 | * |
1a0eae88 BP |
1472 | * Returns: 0 if no error |
1473 | * The inode, if one has been found, in inode. | |
c8cdf479 SW |
1474 | */ |
1475 | ||
044b9414 | 1476 | static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip) |
c8cdf479 | 1477 | { |
6760bdcd | 1478 | u32 goal = 0, block; |
bb9bcf06 | 1479 | u64 no_addr; |
5f3eae75 | 1480 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
044b9414 SW |
1481 | struct gfs2_glock *gl; |
1482 | struct gfs2_inode *ip; | |
1483 | int error; | |
1484 | int found = 0; | |
b3e47ca0 | 1485 | struct gfs2_bitmap *bi; |
c8cdf479 | 1486 | |
044b9414 | 1487 | while (goal < rgd->rd_data) { |
5f3eae75 | 1488 | down_write(&sdp->sd_log_flush_lock); |
6a8099ed | 1489 | block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, &bi); |
5f3eae75 | 1490 | up_write(&sdp->sd_log_flush_lock); |
6760bdcd | 1491 | if (block == BFITNOENT) |
24c73873 | 1492 | break; |
b3e47ca0 BP |
1493 | |
1494 | block = gfs2_bi2rgd_blk(bi, block); | |
6760bdcd BP |
1495 | /* rgblk_search can return a block < goal, so we need to |
1496 | keep it marching forward. */ | |
1497 | no_addr = block + rgd->rd_data0; | |
44ad37d6 | 1498 | goal = max(block + 1, goal + 1); |
6760bdcd | 1499 | if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked) |
c8cdf479 | 1500 | continue; |
1e19a195 SW |
1501 | if (no_addr == skip) |
1502 | continue; | |
bb9bcf06 | 1503 | *last_unlinked = no_addr; |
044b9414 SW |
1504 | |
1505 | error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl); | |
1506 | if (error) | |
1507 | continue; | |
1508 | ||
1509 | /* If the inode is already in cache, we can ignore it here | |
1510 | * because the existing inode disposal code will deal with | |
1511 | * it when all refs have gone away. Accessing gl_object like | |
1512 | * this is not safe in general. Here it is ok because we do | |
1513 | * not dereference the pointer, and we only need an approx | |
1514 | * answer to whether it is NULL or not. | |
1515 | */ | |
1516 | ip = gl->gl_object; | |
1517 | ||
1518 | if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0) | |
1519 | gfs2_glock_put(gl); | |
1520 | else | |
1521 | found++; | |
1522 | ||
1523 | /* Limit reclaim to sensible number of tasks */ | |
44ad37d6 | 1524 | if (found > NR_CPUS) |
044b9414 | 1525 | return; |
c8cdf479 SW |
1526 | } |
1527 | ||
1528 | rgd->rd_flags &= ~GFS2_RDF_CHECK; | |
044b9414 | 1529 | return; |
c8cdf479 SW |
1530 | } |
1531 | ||
b3b94faa | 1532 | /** |
666d1d8a | 1533 | * gfs2_inplace_reserve - Reserve space in the filesystem |
b3b94faa | 1534 | * @ip: the inode to reserve space for |
666d1d8a | 1535 | * @requested: the number of blocks to be reserved |
b3b94faa DT |
1536 | * |
1537 | * Returns: errno | |
1538 | */ | |
1539 | ||
666d1d8a | 1540 | int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested) |
b3b94faa | 1541 | { |
feaa7bba | 1542 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
8e2e0047 | 1543 | struct gfs2_rgrpd *begin = NULL; |
564e12b1 | 1544 | struct gfs2_blkreserv *rs = ip->i_res; |
666d1d8a BP |
1545 | int error = 0, rg_locked, flags = LM_FLAG_TRY; |
1546 | u64 last_unlinked = NO_BLOCK; | |
7c9ca621 | 1547 | int loops = 0; |
b3b94faa | 1548 | |
90306c41 BM |
1549 | if (sdp->sd_args.ar_rgrplvb) |
1550 | flags |= GL_SKIP; | |
666d1d8a BP |
1551 | if (gfs2_assert_warn(sdp, requested)) { |
1552 | error = -EINVAL; | |
1553 | goto out; | |
1554 | } | |
8e2e0047 | 1555 | if (gfs2_rs_active(rs)) { |
4a993fb1 | 1556 | begin = rs->rs_rbm.rgd; |
8e2e0047 BP |
1557 | flags = 0; /* Yoda: Do or do not. There is no try */ |
1558 | } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) { | |
4a993fb1 | 1559 | rs->rs_rbm.rgd = begin = ip->i_rgd; |
8e2e0047 | 1560 | } else { |
4a993fb1 | 1561 | rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1); |
8e2e0047 | 1562 | } |
4a993fb1 | 1563 | if (rs->rs_rbm.rgd == NULL) |
7c9ca621 BP |
1564 | return -EBADSLT; |
1565 | ||
1566 | while (loops < 3) { | |
292c8c14 AD |
1567 | rg_locked = 0; |
1568 | ||
4a993fb1 | 1569 | if (gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) { |
292c8c14 AD |
1570 | rg_locked = 1; |
1571 | error = 0; | |
8e2e0047 | 1572 | } else if (!loops && !gfs2_rs_active(rs) && |
4a993fb1 | 1573 | rs->rs_rbm.rgd->rd_rs_cnt > RGRP_RSRV_MAX_CONTENDERS) { |
8e2e0047 BP |
1574 | /* If the rgrp already is maxed out for contenders, |
1575 | we can eliminate it as a "first pass" without even | |
1576 | requesting the rgrp glock. */ | |
1577 | error = GLR_TRYFAILED; | |
292c8c14 | 1578 | } else { |
4a993fb1 | 1579 | error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl, |
8e2e0047 BP |
1580 | LM_ST_EXCLUSIVE, flags, |
1581 | &rs->rs_rgd_gh); | |
90306c41 | 1582 | if (!error && sdp->sd_args.ar_rgrplvb) { |
4a993fb1 | 1583 | error = update_rgrp_lvb(rs->rs_rbm.rgd); |
90306c41 BM |
1584 | if (error) { |
1585 | gfs2_glock_dq_uninit(&rs->rs_rgd_gh); | |
1586 | return error; | |
1587 | } | |
1588 | } | |
292c8c14 | 1589 | } |
b3b94faa DT |
1590 | switch (error) { |
1591 | case 0: | |
8e2e0047 | 1592 | if (gfs2_rs_active(rs)) { |
4a993fb1 | 1593 | if (unclaimed_blocks(rs->rs_rbm.rgd) + |
71f890f7 | 1594 | rs->rs_free >= requested) { |
4a993fb1 | 1595 | ip->i_rgd = rs->rs_rbm.rgd; |
8e2e0047 BP |
1596 | return 0; |
1597 | } | |
1598 | /* We have a multi-block reservation, but the | |
1599 | rgrp doesn't have enough free blocks to | |
1600 | satisfy the request. Free the reservation | |
1601 | and look for a suitable rgrp. */ | |
4a993fb1 | 1602 | gfs2_rs_deltree(ip, rs); |
8e2e0047 | 1603 | } |
4a993fb1 | 1604 | if (try_rgrp_fit(rs->rs_rbm.rgd, ip, requested)) { |
90306c41 | 1605 | if (sdp->sd_args.ar_rgrplvb) |
4a993fb1 SW |
1606 | gfs2_rgrp_bh_get(rs->rs_rbm.rgd); |
1607 | ip->i_rgd = rs->rs_rbm.rgd; | |
7c9ca621 | 1608 | return 0; |
54335b1f | 1609 | } |
4a993fb1 | 1610 | if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK) { |
90306c41 | 1611 | if (sdp->sd_args.ar_rgrplvb) |
4a993fb1 SW |
1612 | gfs2_rgrp_bh_get(rs->rs_rbm.rgd); |
1613 | try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked, | |
666d1d8a | 1614 | ip->i_no_addr); |
90306c41 | 1615 | } |
292c8c14 | 1616 | if (!rg_locked) |
564e12b1 | 1617 | gfs2_glock_dq_uninit(&rs->rs_rgd_gh); |
9cabcdbd | 1618 | /* fall through */ |
b3b94faa | 1619 | case GLR_TRYFAILED: |
4a993fb1 SW |
1620 | rs->rs_rbm.rgd = gfs2_rgrpd_get_next(rs->rs_rbm.rgd); |
1621 | rs->rs_rbm.rgd = rs->rs_rbm.rgd ? : begin; /* if NULL, wrap */ | |
1622 | if (rs->rs_rbm.rgd != begin) /* If we didn't wrap */ | |
666d1d8a BP |
1623 | break; |
1624 | ||
1625 | flags &= ~LM_FLAG_TRY; | |
1626 | loops++; | |
1627 | /* Check that fs hasn't grown if writing to rindex */ | |
1628 | if (ip == GFS2_I(sdp->sd_rindex) && | |
1629 | !sdp->sd_rindex_uptodate) { | |
1630 | error = gfs2_ri_update(ip); | |
1631 | if (error) | |
1632 | goto out; | |
1633 | } else if (loops == 2) | |
1634 | /* Flushing the log may release space */ | |
1635 | gfs2_log_flush(sdp, NULL); | |
b3b94faa | 1636 | break; |
b3b94faa | 1637 | default: |
666d1d8a | 1638 | goto out; |
b3b94faa | 1639 | } |
b3b94faa | 1640 | } |
666d1d8a | 1641 | error = -ENOSPC; |
b3b94faa | 1642 | |
564e12b1 | 1643 | out: |
9ae32429 | 1644 | return error; |
b3b94faa DT |
1645 | } |
1646 | ||
1647 | /** | |
1648 | * gfs2_inplace_release - release an inplace reservation | |
1649 | * @ip: the inode the reservation was taken out on | |
1650 | * | |
1651 | * Release a reservation made by gfs2_inplace_reserve(). | |
1652 | */ | |
1653 | ||
1654 | void gfs2_inplace_release(struct gfs2_inode *ip) | |
1655 | { | |
564e12b1 | 1656 | struct gfs2_blkreserv *rs = ip->i_res; |
b3b94faa | 1657 | |
564e12b1 BP |
1658 | if (rs->rs_rgd_gh.gh_gl) |
1659 | gfs2_glock_dq_uninit(&rs->rs_rgd_gh); | |
b3b94faa DT |
1660 | } |
1661 | ||
1662 | /** | |
1663 | * gfs2_get_block_type - Check a block in a RG is of given type | |
1664 | * @rgd: the resource group holding the block | |
1665 | * @block: the block number | |
1666 | * | |
1667 | * Returns: The block type (GFS2_BLKST_*) | |
1668 | */ | |
1669 | ||
acf7e244 | 1670 | static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block) |
b3b94faa DT |
1671 | { |
1672 | struct gfs2_bitmap *bi = NULL; | |
cd915493 | 1673 | u32 length, rgrp_block, buf_block; |
b3b94faa DT |
1674 | unsigned int buf; |
1675 | unsigned char type; | |
1676 | ||
bb8d8a6f SW |
1677 | length = rgd->rd_length; |
1678 | rgrp_block = block - rgd->rd_data0; | |
b3b94faa DT |
1679 | |
1680 | for (buf = 0; buf < length; buf++) { | |
1681 | bi = rgd->rd_bits + buf; | |
1682 | if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY) | |
1683 | break; | |
1684 | } | |
1685 | ||
1686 | gfs2_assert(rgd->rd_sbd, buf < length); | |
1687 | buf_block = rgrp_block - bi->bi_start * GFS2_NBBY; | |
1688 | ||
feaa7bba | 1689 | type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
b3b94faa DT |
1690 | bi->bi_len, buf_block); |
1691 | ||
1692 | return type; | |
1693 | } | |
1694 | ||
1695 | /** | |
6a8099ed | 1696 | * rgblk_search - find a block in @state |
b3b94faa DT |
1697 | * @rgd: the resource group descriptor |
1698 | * @goal: the goal block within the RG (start here to search for avail block) | |
6a8099ed | 1699 | * @state: GFS2_BLKST_XXX the before-allocation state to find |
b3e47ca0 | 1700 | * @rbi: address of the pointer to the bitmap containing the block found |
b3b94faa | 1701 | * |
6a8099ed | 1702 | * Walk rgrp's bitmap to find bits that represent a block in @state. |
b3b94faa DT |
1703 | * |
1704 | * This function never fails, because we wouldn't call it unless we | |
1705 | * know (from reservation results, etc.) that a block is available. | |
1706 | * | |
b3e47ca0 BP |
1707 | * Scope of @goal is just within rgrp, not the whole filesystem. |
1708 | * Scope of @returned block is just within bitmap, not the whole filesystem. | |
b3b94faa | 1709 | * |
b3e47ca0 | 1710 | * Returns: the block number found relative to the bitmap rbi |
b3b94faa DT |
1711 | */ |
1712 | ||
886b1416 | 1713 | static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, unsigned char state, |
b3e47ca0 | 1714 | struct gfs2_bitmap **rbi) |
b3b94faa DT |
1715 | { |
1716 | struct gfs2_bitmap *bi = NULL; | |
b45e41d7 | 1717 | const u32 length = rgd->rd_length; |
9598d25e | 1718 | u32 biblk = BFITNOENT; |
b3b94faa | 1719 | unsigned int buf, x; |
60a0b8f9 | 1720 | const u8 *buffer = NULL; |
b3b94faa | 1721 | |
b3e47ca0 | 1722 | *rbi = NULL; |
b3b94faa DT |
1723 | /* Find bitmap block that contains bits for goal block */ |
1724 | for (buf = 0; buf < length; buf++) { | |
1725 | bi = rgd->rd_bits + buf; | |
60a0b8f9 SW |
1726 | /* Convert scope of "goal" from rgrp-wide to within found bit block */ |
1727 | if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) { | |
1728 | goal -= bi->bi_start * GFS2_NBBY; | |
1729 | goto do_search; | |
1730 | } | |
b3b94faa | 1731 | } |
60a0b8f9 SW |
1732 | buf = 0; |
1733 | goal = 0; | |
b3b94faa | 1734 | |
60a0b8f9 | 1735 | do_search: |
b3b94faa DT |
1736 | /* Search (up to entire) bitmap in this rgrp for allocatable block. |
1737 | "x <= length", instead of "x < length", because we typically start | |
1738 | the search in the middle of a bit block, but if we can't find an | |
1739 | allocatable block anywhere else, we want to be able wrap around and | |
1740 | search in the first part of our first-searched bit block. */ | |
1741 | for (x = 0; x <= length; x++) { | |
60a0b8f9 SW |
1742 | bi = rgd->rd_bits + buf; |
1743 | ||
1744 | if (test_bit(GBF_FULL, &bi->bi_flags) && | |
6a8099ed | 1745 | (state == GFS2_BLKST_FREE)) |
60a0b8f9 SW |
1746 | goto skip; |
1747 | ||
5f3eae75 BP |
1748 | /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone |
1749 | bitmaps, so we must search the originals for that. */ | |
b45e41d7 | 1750 | buffer = bi->bi_bh->b_data + bi->bi_offset; |
7c9ca621 | 1751 | WARN_ON(!buffer_uptodate(bi->bi_bh)); |
6a8099ed | 1752 | if (state != GFS2_BLKST_UNLINKED && bi->bi_clone) |
110acf38 SW |
1753 | buffer = bi->bi_clone + bi->bi_offset; |
1754 | ||
8e2e0047 BP |
1755 | while (1) { |
1756 | struct gfs2_blkreserv *rs; | |
1757 | u32 rgblk; | |
1758 | ||
1759 | biblk = gfs2_bitfit(buffer, bi->bi_len, goal, state); | |
1760 | if (biblk == BFITNOENT) | |
1761 | break; | |
1762 | /* Check if this block is reserved() */ | |
1763 | rgblk = gfs2_bi2rgd_blk(bi, biblk); | |
1764 | rs = rs_find(rgd, rgblk); | |
1765 | if (rs == NULL) | |
1766 | break; | |
1767 | ||
4a993fb1 | 1768 | BUG_ON(rs->rs_rbm.bi != bi); |
8e2e0047 BP |
1769 | biblk = BFITNOENT; |
1770 | /* This should jump to the first block after the | |
1771 | reservation. */ | |
4a993fb1 | 1772 | goal = rs->rs_rbm.offset + rs->rs_free; |
8e2e0047 BP |
1773 | if (goal >= bi->bi_len * GFS2_NBBY) |
1774 | break; | |
1775 | } | |
9598d25e | 1776 | if (biblk != BFITNOENT) |
b3b94faa DT |
1777 | break; |
1778 | ||
6a8099ed | 1779 | if ((goal == 0) && (state == GFS2_BLKST_FREE)) |
60a0b8f9 SW |
1780 | set_bit(GBF_FULL, &bi->bi_flags); |
1781 | ||
b3b94faa | 1782 | /* Try next bitmap block (wrap back to rgrp header if at end) */ |
60a0b8f9 SW |
1783 | skip: |
1784 | buf++; | |
1785 | buf %= length; | |
b3b94faa DT |
1786 | goal = 0; |
1787 | } | |
1788 | ||
9598d25e | 1789 | if (biblk != BFITNOENT) |
b3e47ca0 | 1790 | *rbi = bi; |
7c9ca621 | 1791 | |
9598d25e | 1792 | return biblk; |
b3e47ca0 | 1793 | } |
60a0b8f9 | 1794 | |
b3e47ca0 BP |
1795 | /** |
1796 | * gfs2_alloc_extent - allocate an extent from a given bitmap | |
4a993fb1 | 1797 | * @rbm: the resource group information |
b3e47ca0 BP |
1798 | * @dinode: TRUE if the first block we allocate is for a dinode |
1799 | * @n: The extent length | |
1800 | * | |
1801 | * Add the found bitmap buffer to the transaction. | |
1802 | * Set the found bits to @new_state to change block's allocation state. | |
1803 | * Returns: starting block number of the extent (fs scope) | |
1804 | */ | |
4a993fb1 SW |
1805 | static u64 gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode, |
1806 | unsigned int *n) | |
b3e47ca0 | 1807 | { |
4a993fb1 SW |
1808 | struct gfs2_rgrpd *rgd = rbm->rgd; |
1809 | struct gfs2_bitmap *bi = rbm->bi; | |
1810 | u32 blk = rbm->offset; | |
b3e47ca0 | 1811 | const unsigned int elen = *n; |
8e2e0047 | 1812 | u32 goal, rgblk; |
b3e47ca0 | 1813 | const u8 *buffer = NULL; |
8e2e0047 | 1814 | struct gfs2_blkreserv *rs; |
b3e47ca0 | 1815 | |
6a8099ed | 1816 | *n = 0; |
b3e47ca0 | 1817 | buffer = bi->bi_bh->b_data + bi->bi_offset; |
60a0b8f9 | 1818 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
06344b91 BP |
1819 | gfs2_setbit(rgd, bi->bi_clone, bi, blk, |
1820 | dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); | |
6a8099ed | 1821 | (*n)++; |
60a0b8f9 SW |
1822 | goal = blk; |
1823 | while (*n < elen) { | |
1824 | goal++; | |
1825 | if (goal >= (bi->bi_len * GFS2_NBBY)) | |
1826 | break; | |
8e2e0047 BP |
1827 | rgblk = gfs2_bi2rgd_blk(bi, goal); |
1828 | rs = rs_find(rgd, rgblk); | |
1829 | if (rs) /* Oops, we bumped into someone's reservation */ | |
1830 | break; | |
60a0b8f9 SW |
1831 | if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) != |
1832 | GFS2_BLKST_FREE) | |
1833 | break; | |
06344b91 | 1834 | gfs2_setbit(rgd, bi->bi_clone, bi, goal, GFS2_BLKST_USED); |
60a0b8f9 | 1835 | (*n)++; |
c8cdf479 | 1836 | } |
b3e47ca0 | 1837 | blk = gfs2_bi2rgd_blk(bi, blk); |
6a8099ed | 1838 | rgd->rd_last_alloc = blk + *n - 1; |
b3e47ca0 | 1839 | return rgd->rd_data0 + blk; |
b3b94faa DT |
1840 | } |
1841 | ||
1842 | /** | |
1843 | * rgblk_free - Change alloc state of given block(s) | |
1844 | * @sdp: the filesystem | |
1845 | * @bstart: the start of a run of blocks to free | |
1846 | * @blen: the length of the block run (all must lie within ONE RG!) | |
1847 | * @new_state: GFS2_BLKST_XXX the after-allocation block state | |
1848 | * | |
1849 | * Returns: Resource group containing the block(s) | |
1850 | */ | |
1851 | ||
cd915493 SW |
1852 | static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart, |
1853 | u32 blen, unsigned char new_state) | |
b3b94faa DT |
1854 | { |
1855 | struct gfs2_rgrpd *rgd; | |
1856 | struct gfs2_bitmap *bi = NULL; | |
cd915493 | 1857 | u32 length, rgrp_blk, buf_blk; |
b3b94faa DT |
1858 | unsigned int buf; |
1859 | ||
66fc061b | 1860 | rgd = gfs2_blk2rgrpd(sdp, bstart, 1); |
b3b94faa DT |
1861 | if (!rgd) { |
1862 | if (gfs2_consist(sdp)) | |
382066da | 1863 | fs_err(sdp, "block = %llu\n", (unsigned long long)bstart); |
b3b94faa DT |
1864 | return NULL; |
1865 | } | |
1866 | ||
bb8d8a6f | 1867 | length = rgd->rd_length; |
b3b94faa | 1868 | |
bb8d8a6f | 1869 | rgrp_blk = bstart - rgd->rd_data0; |
b3b94faa DT |
1870 | |
1871 | while (blen--) { | |
1872 | for (buf = 0; buf < length; buf++) { | |
1873 | bi = rgd->rd_bits + buf; | |
1874 | if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY) | |
1875 | break; | |
1876 | } | |
1877 | ||
1878 | gfs2_assert(rgd->rd_sbd, buf < length); | |
1879 | ||
1880 | buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY; | |
1881 | rgrp_blk++; | |
1882 | ||
1883 | if (!bi->bi_clone) { | |
1884 | bi->bi_clone = kmalloc(bi->bi_bh->b_size, | |
dd894be8 | 1885 | GFP_NOFS | __GFP_NOFAIL); |
b3b94faa DT |
1886 | memcpy(bi->bi_clone + bi->bi_offset, |
1887 | bi->bi_bh->b_data + bi->bi_offset, | |
1888 | bi->bi_len); | |
1889 | } | |
d4e9c4c3 | 1890 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
06344b91 | 1891 | gfs2_setbit(rgd, NULL, bi, buf_blk, new_state); |
b3b94faa DT |
1892 | } |
1893 | ||
1894 | return rgd; | |
1895 | } | |
1896 | ||
1897 | /** | |
09010978 SW |
1898 | * gfs2_rgrp_dump - print out an rgrp |
1899 | * @seq: The iterator | |
1900 | * @gl: The glock in question | |
1901 | * | |
1902 | */ | |
1903 | ||
1904 | int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl) | |
1905 | { | |
8e2e0047 BP |
1906 | struct gfs2_rgrpd *rgd = gl->gl_object; |
1907 | struct gfs2_blkreserv *trs; | |
1908 | const struct rb_node *n; | |
1909 | ||
09010978 SW |
1910 | if (rgd == NULL) |
1911 | return 0; | |
8e2e0047 | 1912 | gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u r:%u\n", |
09010978 | 1913 | (unsigned long long)rgd->rd_addr, rgd->rd_flags, |
8e2e0047 BP |
1914 | rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes, |
1915 | rgd->rd_reserved); | |
1916 | spin_lock(&rgd->rd_rsspin); | |
1917 | for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) { | |
1918 | trs = rb_entry(n, struct gfs2_blkreserv, rs_node); | |
1919 | dump_rs(seq, trs); | |
1920 | } | |
1921 | spin_unlock(&rgd->rd_rsspin); | |
09010978 SW |
1922 | return 0; |
1923 | } | |
1924 | ||
6050b9c7 SW |
1925 | static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd) |
1926 | { | |
1927 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
1928 | fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n", | |
86d00636 | 1929 | (unsigned long long)rgd->rd_addr); |
6050b9c7 SW |
1930 | fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n"); |
1931 | gfs2_rgrp_dump(NULL, rgd->rd_gl); | |
1932 | rgd->rd_flags |= GFS2_RDF_ERROR; | |
1933 | } | |
1934 | ||
8e2e0047 BP |
1935 | /** |
1936 | * claim_reserved_blks - Claim previously reserved blocks | |
1937 | * @ip: the inode that's claiming the reservation | |
1938 | * @dinode: 1 if this block is a dinode block, otherwise data block | |
1939 | * @nblocks: desired extent length | |
1940 | * | |
62e252ee | 1941 | * Lay claim to previously reserved blocks. |
8e2e0047 BP |
1942 | * Returns: Starting block number of the blocks claimed. |
1943 | * Sets *nblocks to the actual extent length allocated. | |
1944 | */ | |
1945 | static u64 claim_reserved_blks(struct gfs2_inode *ip, bool dinode, | |
1946 | unsigned int *nblocks) | |
1947 | { | |
1948 | struct gfs2_blkreserv *rs = ip->i_res; | |
4a993fb1 | 1949 | struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd; |
8e2e0047 | 1950 | struct gfs2_bitmap *bi; |
4a993fb1 | 1951 | u64 start_block = gfs2_rbm_to_block(&rs->rs_rbm); |
8e2e0047 BP |
1952 | const unsigned int elen = *nblocks; |
1953 | ||
4a993fb1 | 1954 | bi = rs->rs_rbm.bi; |
8e2e0047 BP |
1955 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
1956 | ||
1957 | for (*nblocks = 0; *nblocks < elen && rs->rs_free; (*nblocks)++) { | |
62e252ee | 1958 | if (gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
4a993fb1 | 1959 | bi->bi_len, rs->rs_rbm.offset) != GFS2_BLKST_FREE) |
62e252ee | 1960 | break; |
4a993fb1 | 1961 | gfs2_setbit(rgd, bi->bi_clone, bi, rs->rs_rbm.offset, |
8e2e0047 | 1962 | dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); |
4a993fb1 | 1963 | rs->rs_rbm.offset++; |
8e2e0047 BP |
1964 | rs->rs_free--; |
1965 | ||
1966 | BUG_ON(!rgd->rd_reserved); | |
1967 | rgd->rd_reserved--; | |
1968 | dinode = false; | |
8e2e0047 BP |
1969 | } |
1970 | ||
62e252ee SW |
1971 | trace_gfs2_rs(ip, rs, TRACE_RS_CLAIM); |
1972 | if (!rs->rs_free || *nblocks != elen) | |
4a993fb1 | 1973 | gfs2_rs_deltree(ip, rs); |
62e252ee | 1974 | |
8e2e0047 BP |
1975 | return start_block; |
1976 | } | |
1977 | ||
09010978 | 1978 | /** |
6e87ed0f | 1979 | * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode |
1639431a | 1980 | * @ip: the inode to allocate the block for |
09010978 | 1981 | * @bn: Used to return the starting block number |
8e2e0047 | 1982 | * @nblocks: requested number of blocks/extent length (value/result) |
6e87ed0f | 1983 | * @dinode: 1 if we're allocating a dinode block, else 0 |
3c5d785a | 1984 | * @generation: the generation number of the inode |
b3b94faa | 1985 | * |
09010978 | 1986 | * Returns: 0 or error |
b3b94faa DT |
1987 | */ |
1988 | ||
6a8099ed | 1989 | int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks, |
6e87ed0f | 1990 | bool dinode, u64 *generation) |
b3b94faa | 1991 | { |
feaa7bba | 1992 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
d9ba7615 | 1993 | struct buffer_head *dibh; |
4a993fb1 | 1994 | struct gfs2_rbm rbm = { .rgd = ip->i_rgd, }; |
6a8099ed | 1995 | unsigned int ndata; |
4a993fb1 | 1996 | u32 goal; /* block, within the rgrp scope */ |
3c5d785a | 1997 | u64 block; /* block, within the file system scope */ |
d9ba7615 | 1998 | int error; |
b3b94faa | 1999 | |
62e252ee | 2000 | /* If we have a reservation, claim blocks from it. */ |
8e2e0047 BP |
2001 | if (gfs2_rs_active(ip->i_res)) { |
2002 | BUG_ON(!ip->i_res->rs_free); | |
4a993fb1 | 2003 | rbm.rgd = ip->i_res->rs_rbm.rgd; |
8e2e0047 | 2004 | block = claim_reserved_blks(ip, dinode, nblocks); |
62e252ee SW |
2005 | if (*nblocks) |
2006 | goto found_blocks; | |
2007 | } | |
09010978 | 2008 | |
4a993fb1 SW |
2009 | if (!dinode && rgrp_contains_block(rbm.rgd, ip->i_goal)) |
2010 | goal = ip->i_goal - rbm.rgd->rd_data0; | |
62e252ee | 2011 | else |
4a993fb1 | 2012 | goal = rbm.rgd->rd_last_alloc; |
62e252ee | 2013 | |
4a993fb1 | 2014 | rbm.offset = rgblk_search(rbm.rgd, goal, GFS2_BLKST_FREE, &rbm.bi); |
62e252ee SW |
2015 | |
2016 | /* Since all blocks are reserved in advance, this shouldn't happen */ | |
4a993fb1 | 2017 | if (rbm.offset == BFITNOENT) { |
62e252ee SW |
2018 | printk(KERN_WARNING "BFITNOENT, nblocks=%u\n", *nblocks); |
2019 | printk(KERN_WARNING "FULL=%d\n", | |
4a993fb1 | 2020 | test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags)); |
62e252ee | 2021 | goto rgrp_error; |
8e2e0047 | 2022 | } |
62e252ee | 2023 | |
4a993fb1 | 2024 | block = gfs2_alloc_extent(&rbm, dinode, nblocks); |
62e252ee | 2025 | found_blocks: |
6a8099ed SW |
2026 | ndata = *nblocks; |
2027 | if (dinode) | |
2028 | ndata--; | |
b3e47ca0 | 2029 | |
3c5d785a | 2030 | if (!dinode) { |
6a8099ed | 2031 | ip->i_goal = block + ndata - 1; |
3c5d785a BP |
2032 | error = gfs2_meta_inode_buffer(ip, &dibh); |
2033 | if (error == 0) { | |
2034 | struct gfs2_dinode *di = | |
2035 | (struct gfs2_dinode *)dibh->b_data; | |
2036 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
2037 | di->di_goal_meta = di->di_goal_data = | |
2038 | cpu_to_be64(ip->i_goal); | |
2039 | brelse(dibh); | |
2040 | } | |
d9ba7615 | 2041 | } |
4a993fb1 | 2042 | if (rbm.rgd->rd_free < *nblocks) { |
8e2e0047 | 2043 | printk(KERN_WARNING "nblocks=%u\n", *nblocks); |
09010978 | 2044 | goto rgrp_error; |
8e2e0047 | 2045 | } |
09010978 | 2046 | |
4a993fb1 | 2047 | rbm.rgd->rd_free -= *nblocks; |
3c5d785a | 2048 | if (dinode) { |
4a993fb1 SW |
2049 | rbm.rgd->rd_dinodes++; |
2050 | *generation = rbm.rgd->rd_igeneration++; | |
3c5d785a | 2051 | if (*generation == 0) |
4a993fb1 | 2052 | *generation = rbm.rgd->rd_igeneration++; |
3c5d785a | 2053 | } |
b3b94faa | 2054 | |
4a993fb1 SW |
2055 | gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh, 1); |
2056 | gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data); | |
2057 | gfs2_rgrp_ondisk2lvb(rbm.rgd->rd_rgl, rbm.rgd->rd_bits[0].bi_bh->b_data); | |
b3b94faa | 2058 | |
6a8099ed | 2059 | gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0); |
3c5d785a BP |
2060 | if (dinode) |
2061 | gfs2_trans_add_unrevoke(sdp, block, 1); | |
6a8099ed SW |
2062 | |
2063 | /* | |
2064 | * This needs reviewing to see why we cannot do the quota change | |
2065 | * at this point in the dinode case. | |
2066 | */ | |
2067 | if (ndata) | |
2068 | gfs2_quota_change(ip, ndata, ip->i_inode.i_uid, | |
3c5d785a | 2069 | ip->i_inode.i_gid); |
b3b94faa | 2070 | |
4a993fb1 SW |
2071 | rbm.rgd->rd_free_clone -= *nblocks; |
2072 | trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks, | |
6e87ed0f | 2073 | dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); |
6050b9c7 SW |
2074 | *bn = block; |
2075 | return 0; | |
2076 | ||
2077 | rgrp_error: | |
4a993fb1 | 2078 | gfs2_rgrp_error(rbm.rgd); |
6050b9c7 | 2079 | return -EIO; |
b3b94faa DT |
2080 | } |
2081 | ||
2082 | /** | |
46fcb2ed | 2083 | * __gfs2_free_blocks - free a contiguous run of block(s) |
b3b94faa DT |
2084 | * @ip: the inode these blocks are being freed from |
2085 | * @bstart: first block of a run of contiguous blocks | |
2086 | * @blen: the length of the block run | |
46fcb2ed | 2087 | * @meta: 1 if the blocks represent metadata |
b3b94faa DT |
2088 | * |
2089 | */ | |
2090 | ||
46fcb2ed | 2091 | void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta) |
b3b94faa | 2092 | { |
feaa7bba | 2093 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
2094 | struct gfs2_rgrpd *rgd; |
2095 | ||
2096 | rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); | |
2097 | if (!rgd) | |
2098 | return; | |
41db1ab9 | 2099 | trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE); |
cfc8b549 | 2100 | rgd->rd_free += blen; |
66fc061b | 2101 | rgd->rd_flags &= ~GFS2_RGF_TRIMMED; |
d4e9c4c3 | 2102 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
42d52e38 | 2103 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
90306c41 | 2104 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); |
b3b94faa | 2105 | |
6d3117b4 | 2106 | /* Directories keep their data in the metadata address space */ |
46fcb2ed | 2107 | if (meta || ip->i_depth) |
6d3117b4 | 2108 | gfs2_meta_wipe(ip, bstart, blen); |
4c16c36a | 2109 | } |
b3b94faa | 2110 | |
4c16c36a BP |
2111 | /** |
2112 | * gfs2_free_meta - free a contiguous run of data block(s) | |
2113 | * @ip: the inode these blocks are being freed from | |
2114 | * @bstart: first block of a run of contiguous blocks | |
2115 | * @blen: the length of the block run | |
2116 | * | |
2117 | */ | |
2118 | ||
2119 | void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen) | |
2120 | { | |
2121 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | |
2122 | ||
46fcb2ed | 2123 | __gfs2_free_blocks(ip, bstart, blen, 1); |
b3b94faa | 2124 | gfs2_statfs_change(sdp, 0, +blen, 0); |
2933f925 | 2125 | gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); |
b3b94faa DT |
2126 | } |
2127 | ||
feaa7bba SW |
2128 | void gfs2_unlink_di(struct inode *inode) |
2129 | { | |
2130 | struct gfs2_inode *ip = GFS2_I(inode); | |
2131 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
2132 | struct gfs2_rgrpd *rgd; | |
dbb7cae2 | 2133 | u64 blkno = ip->i_no_addr; |
feaa7bba SW |
2134 | |
2135 | rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED); | |
2136 | if (!rgd) | |
2137 | return; | |
41db1ab9 | 2138 | trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED); |
feaa7bba | 2139 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
42d52e38 | 2140 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
90306c41 BM |
2141 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); |
2142 | update_rgrp_lvb_unlinked(rgd, 1); | |
feaa7bba SW |
2143 | } |
2144 | ||
cd915493 | 2145 | static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno) |
b3b94faa DT |
2146 | { |
2147 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
2148 | struct gfs2_rgrpd *tmp_rgd; | |
2149 | ||
2150 | tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE); | |
2151 | if (!tmp_rgd) | |
2152 | return; | |
2153 | gfs2_assert_withdraw(sdp, rgd == tmp_rgd); | |
2154 | ||
73f74948 | 2155 | if (!rgd->rd_dinodes) |
b3b94faa | 2156 | gfs2_consist_rgrpd(rgd); |
73f74948 | 2157 | rgd->rd_dinodes--; |
cfc8b549 | 2158 | rgd->rd_free++; |
b3b94faa | 2159 | |
d4e9c4c3 | 2160 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
42d52e38 | 2161 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
90306c41 BM |
2162 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); |
2163 | update_rgrp_lvb_unlinked(rgd, -1); | |
b3b94faa DT |
2164 | |
2165 | gfs2_statfs_change(sdp, 0, +1, -1); | |
b3b94faa DT |
2166 | } |
2167 | ||
b3b94faa DT |
2168 | |
2169 | void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) | |
2170 | { | |
dbb7cae2 | 2171 | gfs2_free_uninit_di(rgd, ip->i_no_addr); |
41db1ab9 | 2172 | trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE); |
2933f925 | 2173 | gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid); |
dbb7cae2 | 2174 | gfs2_meta_wipe(ip, ip->i_no_addr, 1); |
b3b94faa DT |
2175 | } |
2176 | ||
acf7e244 SW |
2177 | /** |
2178 | * gfs2_check_blk_type - Check the type of a block | |
2179 | * @sdp: The superblock | |
2180 | * @no_addr: The block number to check | |
2181 | * @type: The block type we are looking for | |
2182 | * | |
2183 | * Returns: 0 if the block type matches the expected type | |
2184 | * -ESTALE if it doesn't match | |
2185 | * or -ve errno if something went wrong while checking | |
2186 | */ | |
2187 | ||
2188 | int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type) | |
2189 | { | |
2190 | struct gfs2_rgrpd *rgd; | |
8339ee54 | 2191 | struct gfs2_holder rgd_gh; |
58884c4d | 2192 | int error = -EINVAL; |
acf7e244 | 2193 | |
66fc061b | 2194 | rgd = gfs2_blk2rgrpd(sdp, no_addr, 1); |
acf7e244 | 2195 | if (!rgd) |
8339ee54 | 2196 | goto fail; |
acf7e244 SW |
2197 | |
2198 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh); | |
2199 | if (error) | |
8339ee54 | 2200 | goto fail; |
acf7e244 SW |
2201 | |
2202 | if (gfs2_get_block_type(rgd, no_addr) != type) | |
2203 | error = -ESTALE; | |
2204 | ||
2205 | gfs2_glock_dq_uninit(&rgd_gh); | |
acf7e244 SW |
2206 | fail: |
2207 | return error; | |
2208 | } | |
2209 | ||
b3b94faa DT |
2210 | /** |
2211 | * gfs2_rlist_add - add a RG to a list of RGs | |
70b0c365 | 2212 | * @ip: the inode |
b3b94faa DT |
2213 | * @rlist: the list of resource groups |
2214 | * @block: the block | |
2215 | * | |
2216 | * Figure out what RG a block belongs to and add that RG to the list | |
2217 | * | |
2218 | * FIXME: Don't use NOFAIL | |
2219 | * | |
2220 | */ | |
2221 | ||
70b0c365 | 2222 | void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist, |
cd915493 | 2223 | u64 block) |
b3b94faa | 2224 | { |
70b0c365 | 2225 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
2226 | struct gfs2_rgrpd *rgd; |
2227 | struct gfs2_rgrpd **tmp; | |
2228 | unsigned int new_space; | |
2229 | unsigned int x; | |
2230 | ||
2231 | if (gfs2_assert_warn(sdp, !rlist->rl_ghs)) | |
2232 | return; | |
2233 | ||
70b0c365 SW |
2234 | if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block)) |
2235 | rgd = ip->i_rgd; | |
2236 | else | |
66fc061b | 2237 | rgd = gfs2_blk2rgrpd(sdp, block, 1); |
b3b94faa | 2238 | if (!rgd) { |
70b0c365 | 2239 | fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block); |
b3b94faa DT |
2240 | return; |
2241 | } | |
70b0c365 | 2242 | ip->i_rgd = rgd; |
b3b94faa DT |
2243 | |
2244 | for (x = 0; x < rlist->rl_rgrps; x++) | |
2245 | if (rlist->rl_rgd[x] == rgd) | |
2246 | return; | |
2247 | ||
2248 | if (rlist->rl_rgrps == rlist->rl_space) { | |
2249 | new_space = rlist->rl_space + 10; | |
2250 | ||
2251 | tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *), | |
dd894be8 | 2252 | GFP_NOFS | __GFP_NOFAIL); |
b3b94faa DT |
2253 | |
2254 | if (rlist->rl_rgd) { | |
2255 | memcpy(tmp, rlist->rl_rgd, | |
2256 | rlist->rl_space * sizeof(struct gfs2_rgrpd *)); | |
2257 | kfree(rlist->rl_rgd); | |
2258 | } | |
2259 | ||
2260 | rlist->rl_space = new_space; | |
2261 | rlist->rl_rgd = tmp; | |
2262 | } | |
2263 | ||
2264 | rlist->rl_rgd[rlist->rl_rgrps++] = rgd; | |
2265 | } | |
2266 | ||
2267 | /** | |
2268 | * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate | |
2269 | * and initialize an array of glock holders for them | |
2270 | * @rlist: the list of resource groups | |
2271 | * @state: the lock state to acquire the RG lock in | |
b3b94faa DT |
2272 | * |
2273 | * FIXME: Don't use NOFAIL | |
2274 | * | |
2275 | */ | |
2276 | ||
fe6c991c | 2277 | void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state) |
b3b94faa DT |
2278 | { |
2279 | unsigned int x; | |
2280 | ||
2281 | rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder), | |
dd894be8 | 2282 | GFP_NOFS | __GFP_NOFAIL); |
b3b94faa DT |
2283 | for (x = 0; x < rlist->rl_rgrps; x++) |
2284 | gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, | |
fe6c991c | 2285 | state, 0, |
b3b94faa DT |
2286 | &rlist->rl_ghs[x]); |
2287 | } | |
2288 | ||
2289 | /** | |
2290 | * gfs2_rlist_free - free a resource group list | |
2291 | * @list: the list of resource groups | |
2292 | * | |
2293 | */ | |
2294 | ||
2295 | void gfs2_rlist_free(struct gfs2_rgrp_list *rlist) | |
2296 | { | |
2297 | unsigned int x; | |
2298 | ||
2299 | kfree(rlist->rl_rgd); | |
2300 | ||
2301 | if (rlist->rl_ghs) { | |
2302 | for (x = 0; x < rlist->rl_rgrps; x++) | |
2303 | gfs2_holder_uninit(&rlist->rl_ghs[x]); | |
2304 | kfree(rlist->rl_ghs); | |
8e2e0047 | 2305 | rlist->rl_ghs = NULL; |
b3b94faa DT |
2306 | } |
2307 | } | |
2308 |