Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
7ae8fa84 | 3 | * Copyright (C) 2004-2007 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> |
7d308590 | 16 | #include <linux/lm_interface.h> |
b3b94faa DT |
17 | |
18 | #include "gfs2.h" | |
5c676f6d | 19 | #include "incore.h" |
b3b94faa DT |
20 | #include "glock.h" |
21 | #include "glops.h" | |
b3b94faa DT |
22 | #include "lops.h" |
23 | #include "meta_io.h" | |
24 | #include "quota.h" | |
25 | #include "rgrp.h" | |
26 | #include "super.h" | |
27 | #include "trans.h" | |
f42faf4f | 28 | #include "ops_file.h" |
5c676f6d | 29 | #include "util.h" |
172e045a | 30 | #include "log.h" |
b3b94faa | 31 | |
2c1e52aa | 32 | #define BFITNOENT ((u32)~0) |
88c8ab1f SW |
33 | |
34 | /* | |
35 | * These routines are used by the resource group routines (rgrp.c) | |
36 | * to keep track of block allocation. Each block is represented by two | |
feaa7bba SW |
37 | * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks. |
38 | * | |
39 | * 0 = Free | |
40 | * 1 = Used (not metadata) | |
41 | * 2 = Unlinked (still in use) inode | |
42 | * 3 = Used (metadata) | |
88c8ab1f SW |
43 | */ |
44 | ||
45 | static const char valid_change[16] = { | |
46 | /* current */ | |
feaa7bba | 47 | /* n */ 0, 1, 1, 1, |
88c8ab1f | 48 | /* e */ 1, 0, 0, 0, |
feaa7bba | 49 | /* w */ 0, 0, 0, 1, |
88c8ab1f SW |
50 | 1, 0, 0, 0 |
51 | }; | |
52 | ||
53 | /** | |
54 | * gfs2_setbit - Set a bit in the bitmaps | |
55 | * @buffer: the buffer that holds the bitmaps | |
56 | * @buflen: the length (in bytes) of the buffer | |
57 | * @block: the block to set | |
58 | * @new_state: the new state of the block | |
59 | * | |
60 | */ | |
61 | ||
3efd7534 | 62 | static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer, |
cd915493 | 63 | unsigned int buflen, u32 block, |
3efd7534 | 64 | unsigned char new_state) |
88c8ab1f SW |
65 | { |
66 | unsigned char *byte, *end, cur_state; | |
67 | unsigned int bit; | |
68 | ||
69 | byte = buffer + (block / GFS2_NBBY); | |
70 | bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; | |
71 | end = buffer + buflen; | |
72 | ||
73 | gfs2_assert(rgd->rd_sbd, byte < end); | |
74 | ||
75 | cur_state = (*byte >> bit) & GFS2_BIT_MASK; | |
76 | ||
77 | if (valid_change[new_state * 4 + cur_state]) { | |
78 | *byte ^= cur_state << bit; | |
79 | *byte |= new_state << bit; | |
80 | } else | |
81 | gfs2_consist_rgrpd(rgd); | |
82 | } | |
83 | ||
84 | /** | |
85 | * gfs2_testbit - test a bit in the bitmaps | |
86 | * @buffer: the buffer that holds the bitmaps | |
87 | * @buflen: the length (in bytes) of the buffer | |
88 | * @block: the block to read | |
89 | * | |
90 | */ | |
91 | ||
3efd7534 | 92 | static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer, |
cd915493 | 93 | unsigned int buflen, u32 block) |
88c8ab1f SW |
94 | { |
95 | unsigned char *byte, *end, cur_state; | |
96 | unsigned int bit; | |
97 | ||
98 | byte = buffer + (block / GFS2_NBBY); | |
99 | bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; | |
100 | end = buffer + buflen; | |
101 | ||
102 | gfs2_assert(rgd->rd_sbd, byte < end); | |
103 | ||
104 | cur_state = (*byte >> bit) & GFS2_BIT_MASK; | |
105 | ||
106 | return cur_state; | |
107 | } | |
108 | ||
109 | /** | |
110 | * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing | |
111 | * a block in a given allocation state. | |
112 | * @buffer: the buffer that holds the bitmaps | |
113 | * @buflen: the length (in bytes) of the buffer | |
114 | * @goal: start search at this block's bit-pair (within @buffer) | |
115 | * @old_state: GFS2_BLKST_XXX the state of the block we're looking for; | |
116 | * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0) | |
117 | * | |
118 | * Scope of @goal and returned block number is only within this bitmap buffer, | |
119 | * not entire rgrp or filesystem. @buffer will be offset from the actual | |
120 | * beginning of a bitmap block buffer, skipping any header structures. | |
121 | * | |
122 | * Return: the block number (bitmap buffer scope) that was found | |
123 | */ | |
124 | ||
cd915493 SW |
125 | static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer, |
126 | unsigned int buflen, u32 goal, | |
3efd7534 | 127 | unsigned char old_state) |
88c8ab1f SW |
128 | { |
129 | unsigned char *byte, *end, alloc; | |
cd915493 | 130 | u32 blk = goal; |
88c8ab1f SW |
131 | unsigned int bit; |
132 | ||
133 | byte = buffer + (goal / GFS2_NBBY); | |
134 | bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE; | |
135 | end = buffer + buflen; | |
136 | alloc = (old_state & 1) ? 0 : 0x55; | |
137 | ||
138 | while (byte < end) { | |
139 | if ((*byte & 0x55) == alloc) { | |
140 | blk += (8 - bit) >> 1; | |
141 | ||
142 | bit = 0; | |
143 | byte++; | |
144 | ||
145 | continue; | |
146 | } | |
147 | ||
148 | if (((*byte >> bit) & GFS2_BIT_MASK) == old_state) | |
149 | return blk; | |
150 | ||
151 | bit += GFS2_BIT_SIZE; | |
152 | if (bit >= 8) { | |
153 | bit = 0; | |
154 | byte++; | |
155 | } | |
156 | ||
157 | blk++; | |
158 | } | |
159 | ||
160 | return BFITNOENT; | |
161 | } | |
162 | ||
163 | /** | |
164 | * gfs2_bitcount - count the number of bits in a certain state | |
165 | * @buffer: the buffer that holds the bitmaps | |
166 | * @buflen: the length (in bytes) of the buffer | |
167 | * @state: the state of the block we're looking for | |
168 | * | |
169 | * Returns: The number of bits | |
170 | */ | |
171 | ||
cd915493 | 172 | static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer, |
3efd7534 | 173 | unsigned int buflen, unsigned char state) |
88c8ab1f SW |
174 | { |
175 | unsigned char *byte = buffer; | |
176 | unsigned char *end = buffer + buflen; | |
177 | unsigned char state1 = state << 2; | |
178 | unsigned char state2 = state << 4; | |
179 | unsigned char state3 = state << 6; | |
cd915493 | 180 | u32 count = 0; |
88c8ab1f SW |
181 | |
182 | for (; byte < end; byte++) { | |
183 | if (((*byte) & 0x03) == state) | |
184 | count++; | |
185 | if (((*byte) & 0x0C) == state1) | |
186 | count++; | |
187 | if (((*byte) & 0x30) == state2) | |
188 | count++; | |
189 | if (((*byte) & 0xC0) == state3) | |
190 | count++; | |
191 | } | |
192 | ||
193 | return count; | |
194 | } | |
195 | ||
b3b94faa DT |
196 | /** |
197 | * gfs2_rgrp_verify - Verify that a resource group is consistent | |
198 | * @sdp: the filesystem | |
199 | * @rgd: the rgrp | |
200 | * | |
201 | */ | |
202 | ||
203 | void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd) | |
204 | { | |
205 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
206 | struct gfs2_bitmap *bi = NULL; | |
bb8d8a6f | 207 | u32 length = rgd->rd_length; |
cd915493 | 208 | u32 count[4], tmp; |
b3b94faa DT |
209 | int buf, x; |
210 | ||
cd915493 | 211 | memset(count, 0, 4 * sizeof(u32)); |
b3b94faa DT |
212 | |
213 | /* Count # blocks in each of 4 possible allocation states */ | |
214 | for (buf = 0; buf < length; buf++) { | |
215 | bi = rgd->rd_bits + buf; | |
216 | for (x = 0; x < 4; x++) | |
217 | count[x] += gfs2_bitcount(rgd, | |
218 | bi->bi_bh->b_data + | |
219 | bi->bi_offset, | |
220 | bi->bi_len, x); | |
221 | } | |
222 | ||
223 | if (count[0] != rgd->rd_rg.rg_free) { | |
224 | if (gfs2_consist_rgrpd(rgd)) | |
225 | fs_err(sdp, "free data mismatch: %u != %u\n", | |
226 | count[0], rgd->rd_rg.rg_free); | |
227 | return; | |
228 | } | |
229 | ||
bb8d8a6f | 230 | tmp = rgd->rd_data - |
b3b94faa DT |
231 | rgd->rd_rg.rg_free - |
232 | rgd->rd_rg.rg_dinodes; | |
feaa7bba | 233 | if (count[1] + count[2] != tmp) { |
b3b94faa DT |
234 | if (gfs2_consist_rgrpd(rgd)) |
235 | fs_err(sdp, "used data mismatch: %u != %u\n", | |
236 | count[1], tmp); | |
237 | return; | |
238 | } | |
239 | ||
feaa7bba | 240 | if (count[3] != rgd->rd_rg.rg_dinodes) { |
b3b94faa | 241 | if (gfs2_consist_rgrpd(rgd)) |
feaa7bba SW |
242 | fs_err(sdp, "used metadata mismatch: %u != %u\n", |
243 | count[3], rgd->rd_rg.rg_dinodes); | |
b3b94faa DT |
244 | return; |
245 | } | |
246 | ||
feaa7bba | 247 | if (count[2] > count[3]) { |
b3b94faa | 248 | if (gfs2_consist_rgrpd(rgd)) |
feaa7bba SW |
249 | fs_err(sdp, "unlinked inodes > inodes: %u\n", |
250 | count[2]); | |
b3b94faa DT |
251 | return; |
252 | } | |
feaa7bba | 253 | |
b3b94faa DT |
254 | } |
255 | ||
bb8d8a6f | 256 | static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block) |
b3b94faa | 257 | { |
bb8d8a6f SW |
258 | u64 first = rgd->rd_data0; |
259 | u64 last = first + rgd->rd_data; | |
16910427 | 260 | return first <= block && block < last; |
b3b94faa DT |
261 | } |
262 | ||
263 | /** | |
264 | * gfs2_blk2rgrpd - Find resource group for a given data/meta block number | |
265 | * @sdp: The GFS2 superblock | |
266 | * @n: The data block number | |
267 | * | |
268 | * Returns: The resource group, or NULL if not found | |
269 | */ | |
270 | ||
cd915493 | 271 | struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk) |
b3b94faa DT |
272 | { |
273 | struct gfs2_rgrpd *rgd; | |
274 | ||
275 | spin_lock(&sdp->sd_rindex_spin); | |
276 | ||
277 | list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) { | |
bb8d8a6f | 278 | if (rgrp_contains_block(rgd, blk)) { |
b3b94faa DT |
279 | list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list); |
280 | spin_unlock(&sdp->sd_rindex_spin); | |
281 | return rgd; | |
282 | } | |
283 | } | |
284 | ||
285 | spin_unlock(&sdp->sd_rindex_spin); | |
286 | ||
287 | return NULL; | |
288 | } | |
289 | ||
290 | /** | |
291 | * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem | |
292 | * @sdp: The GFS2 superblock | |
293 | * | |
294 | * Returns: The first rgrp in the filesystem | |
295 | */ | |
296 | ||
297 | struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp) | |
298 | { | |
299 | gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list)); | |
300 | return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list); | |
301 | } | |
302 | ||
303 | /** | |
304 | * gfs2_rgrpd_get_next - get the next RG | |
305 | * @rgd: A RG | |
306 | * | |
307 | * Returns: The next rgrp | |
308 | */ | |
309 | ||
310 | struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd) | |
311 | { | |
312 | if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list) | |
313 | return NULL; | |
314 | return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list); | |
315 | } | |
316 | ||
317 | static void clear_rgrpdi(struct gfs2_sbd *sdp) | |
318 | { | |
319 | struct list_head *head; | |
320 | struct gfs2_rgrpd *rgd; | |
321 | struct gfs2_glock *gl; | |
322 | ||
323 | spin_lock(&sdp->sd_rindex_spin); | |
324 | sdp->sd_rindex_forward = NULL; | |
325 | head = &sdp->sd_rindex_recent_list; | |
326 | while (!list_empty(head)) { | |
327 | rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent); | |
328 | list_del(&rgd->rd_recent); | |
329 | } | |
330 | spin_unlock(&sdp->sd_rindex_spin); | |
331 | ||
332 | head = &sdp->sd_rindex_list; | |
333 | while (!list_empty(head)) { | |
334 | rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list); | |
335 | gl = rgd->rd_gl; | |
336 | ||
337 | list_del(&rgd->rd_list); | |
338 | list_del(&rgd->rd_list_mru); | |
339 | ||
340 | if (gl) { | |
5c676f6d | 341 | gl->gl_object = NULL; |
b3b94faa DT |
342 | gfs2_glock_put(gl); |
343 | } | |
344 | ||
345 | kfree(rgd->rd_bits); | |
346 | kfree(rgd); | |
347 | } | |
348 | } | |
349 | ||
350 | void gfs2_clear_rgrpd(struct gfs2_sbd *sdp) | |
351 | { | |
f55ab26a | 352 | mutex_lock(&sdp->sd_rindex_mutex); |
b3b94faa | 353 | clear_rgrpdi(sdp); |
f55ab26a | 354 | mutex_unlock(&sdp->sd_rindex_mutex); |
b3b94faa DT |
355 | } |
356 | ||
bb8d8a6f SW |
357 | static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd) |
358 | { | |
359 | printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr); | |
360 | printk(KERN_INFO " ri_length = %u\n", rgd->rd_length); | |
361 | printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0); | |
362 | printk(KERN_INFO " ri_data = %u\n", rgd->rd_data); | |
363 | printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes); | |
364 | } | |
365 | ||
b3b94faa DT |
366 | /** |
367 | * gfs2_compute_bitstructs - Compute the bitmap sizes | |
368 | * @rgd: The resource group descriptor | |
369 | * | |
370 | * Calculates bitmap descriptors, one for each block that contains bitmap data | |
371 | * | |
372 | * Returns: errno | |
373 | */ | |
374 | ||
375 | static int compute_bitstructs(struct gfs2_rgrpd *rgd) | |
376 | { | |
377 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
378 | struct gfs2_bitmap *bi; | |
bb8d8a6f | 379 | u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */ |
cd915493 | 380 | u32 bytes_left, bytes; |
b3b94faa DT |
381 | int x; |
382 | ||
feaa7bba SW |
383 | if (!length) |
384 | return -EINVAL; | |
385 | ||
dd894be8 | 386 | rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS); |
b3b94faa DT |
387 | if (!rgd->rd_bits) |
388 | return -ENOMEM; | |
389 | ||
bb8d8a6f | 390 | bytes_left = rgd->rd_bitbytes; |
b3b94faa DT |
391 | |
392 | for (x = 0; x < length; x++) { | |
393 | bi = rgd->rd_bits + x; | |
394 | ||
395 | /* small rgrp; bitmap stored completely in header block */ | |
396 | if (length == 1) { | |
397 | bytes = bytes_left; | |
398 | bi->bi_offset = sizeof(struct gfs2_rgrp); | |
399 | bi->bi_start = 0; | |
400 | bi->bi_len = bytes; | |
401 | /* header block */ | |
402 | } else if (x == 0) { | |
403 | bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp); | |
404 | bi->bi_offset = sizeof(struct gfs2_rgrp); | |
405 | bi->bi_start = 0; | |
406 | bi->bi_len = bytes; | |
407 | /* last block */ | |
408 | } else if (x + 1 == length) { | |
409 | bytes = bytes_left; | |
410 | bi->bi_offset = sizeof(struct gfs2_meta_header); | |
bb8d8a6f | 411 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
b3b94faa DT |
412 | bi->bi_len = bytes; |
413 | /* other blocks */ | |
414 | } else { | |
568f4c96 SW |
415 | bytes = sdp->sd_sb.sb_bsize - |
416 | sizeof(struct gfs2_meta_header); | |
b3b94faa | 417 | bi->bi_offset = sizeof(struct gfs2_meta_header); |
bb8d8a6f | 418 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
b3b94faa DT |
419 | bi->bi_len = bytes; |
420 | } | |
421 | ||
422 | bytes_left -= bytes; | |
423 | } | |
424 | ||
425 | if (bytes_left) { | |
426 | gfs2_consist_rgrpd(rgd); | |
427 | return -EIO; | |
428 | } | |
429 | bi = rgd->rd_bits + (length - 1); | |
bb8d8a6f | 430 | if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) { |
b3b94faa | 431 | if (gfs2_consist_rgrpd(rgd)) { |
bb8d8a6f | 432 | gfs2_rindex_print(rgd); |
b3b94faa DT |
433 | fs_err(sdp, "start=%u len=%u offset=%u\n", |
434 | bi->bi_start, bi->bi_len, bi->bi_offset); | |
435 | } | |
436 | return -EIO; | |
437 | } | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
7ae8fa84 | 442 | /** |
bb8d8a6f | 443 | \0 |
7ae8fa84 RP |
444 | * gfs2_ri_total - Total up the file system space, according to the rindex. |
445 | * | |
446 | */ | |
447 | u64 gfs2_ri_total(struct gfs2_sbd *sdp) | |
448 | { | |
449 | u64 total_data = 0; | |
450 | struct inode *inode = sdp->sd_rindex; | |
451 | struct gfs2_inode *ip = GFS2_I(inode); | |
7ae8fa84 RP |
452 | char buf[sizeof(struct gfs2_rindex)]; |
453 | struct file_ra_state ra_state; | |
454 | int error, rgrps; | |
455 | ||
456 | mutex_lock(&sdp->sd_rindex_mutex); | |
457 | file_ra_state_init(&ra_state, inode->i_mapping); | |
458 | for (rgrps = 0;; rgrps++) { | |
459 | loff_t pos = rgrps * sizeof(struct gfs2_rindex); | |
460 | ||
461 | if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size) | |
462 | break; | |
463 | error = gfs2_internal_read(ip, &ra_state, buf, &pos, | |
464 | sizeof(struct gfs2_rindex)); | |
465 | if (error != sizeof(struct gfs2_rindex)) | |
466 | break; | |
bb8d8a6f | 467 | total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data); |
7ae8fa84 RP |
468 | } |
469 | mutex_unlock(&sdp->sd_rindex_mutex); | |
470 | return total_data; | |
471 | } | |
472 | ||
bb8d8a6f SW |
473 | static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf) |
474 | { | |
475 | const struct gfs2_rindex *str = buf; | |
476 | ||
477 | rgd->rd_addr = be64_to_cpu(str->ri_addr); | |
478 | rgd->rd_length = be32_to_cpu(str->ri_length); | |
479 | rgd->rd_data0 = be64_to_cpu(str->ri_data0); | |
480 | rgd->rd_data = be32_to_cpu(str->ri_data); | |
481 | rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes); | |
482 | } | |
483 | ||
b3b94faa | 484 | /** |
6c53267f | 485 | * read_rindex_entry - Pull in a new resource index entry from the disk |
b3b94faa DT |
486 | * @gl: The glock covering the rindex inode |
487 | * | |
6c53267f RP |
488 | * Returns: 0 on success, error code otherwise |
489 | */ | |
490 | ||
491 | static int read_rindex_entry(struct gfs2_inode *ip, | |
492 | struct file_ra_state *ra_state) | |
493 | { | |
494 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | |
495 | loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex); | |
496 | char buf[sizeof(struct gfs2_rindex)]; | |
497 | int error; | |
498 | struct gfs2_rgrpd *rgd; | |
499 | ||
500 | error = gfs2_internal_read(ip, ra_state, buf, &pos, | |
501 | sizeof(struct gfs2_rindex)); | |
502 | if (!error) | |
503 | return 0; | |
504 | if (error != sizeof(struct gfs2_rindex)) { | |
505 | if (error > 0) | |
506 | error = -EIO; | |
507 | return error; | |
508 | } | |
509 | ||
510 | rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS); | |
511 | error = -ENOMEM; | |
512 | if (!rgd) | |
513 | return error; | |
514 | ||
515 | mutex_init(&rgd->rd_mutex); | |
516 | lops_init_le(&rgd->rd_le, &gfs2_rg_lops); | |
517 | rgd->rd_sbd = sdp; | |
518 | ||
519 | list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list); | |
520 | list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list); | |
521 | ||
bb8d8a6f | 522 | gfs2_rindex_in(rgd, buf); |
6c53267f RP |
523 | error = compute_bitstructs(rgd); |
524 | if (error) | |
525 | return error; | |
526 | ||
bb8d8a6f | 527 | error = gfs2_glock_get(sdp, rgd->rd_addr, |
6c53267f RP |
528 | &gfs2_rgrp_glops, CREATE, &rgd->rd_gl); |
529 | if (error) | |
530 | return error; | |
531 | ||
532 | rgd->rd_gl->gl_object = rgd; | |
533 | rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1; | |
534 | return error; | |
535 | } | |
536 | ||
537 | /** | |
538 | * gfs2_ri_update - Pull in a new resource index from the disk | |
539 | * @ip: pointer to the rindex inode | |
540 | * | |
b3b94faa DT |
541 | * Returns: 0 on successful update, error code otherwise |
542 | */ | |
543 | ||
544 | static int gfs2_ri_update(struct gfs2_inode *ip) | |
545 | { | |
feaa7bba SW |
546 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
547 | struct inode *inode = &ip->i_inode; | |
f42faf4f | 548 | struct file_ra_state ra_state; |
cd81a4ba | 549 | u64 rgrp_count = ip->i_di.di_size; |
b3b94faa DT |
550 | int error; |
551 | ||
cd81a4ba | 552 | if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) { |
b3b94faa DT |
553 | gfs2_consist_inode(ip); |
554 | return -EIO; | |
555 | } | |
556 | ||
557 | clear_rgrpdi(sdp); | |
558 | ||
f42faf4f | 559 | file_ra_state_init(&ra_state, inode->i_mapping); |
cd81a4ba | 560 | for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) { |
6c53267f RP |
561 | error = read_rindex_entry(ip, &ra_state); |
562 | if (error) { | |
563 | clear_rgrpdi(sdp); | |
564 | return error; | |
b3b94faa | 565 | } |
6c53267f | 566 | } |
b3b94faa | 567 | |
6c53267f RP |
568 | sdp->sd_rindex_vn = ip->i_gl->gl_vn; |
569 | return 0; | |
570 | } | |
b3b94faa | 571 | |
6c53267f RP |
572 | /** |
573 | * gfs2_ri_update_special - Pull in a new resource index from the disk | |
574 | * | |
575 | * This is a special version that's safe to call from gfs2_inplace_reserve_i. | |
576 | * In this case we know that we don't have any resource groups in memory yet. | |
577 | * | |
578 | * @ip: pointer to the rindex inode | |
579 | * | |
580 | * Returns: 0 on successful update, error code otherwise | |
581 | */ | |
582 | static int gfs2_ri_update_special(struct gfs2_inode *ip) | |
583 | { | |
584 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | |
585 | struct inode *inode = &ip->i_inode; | |
586 | struct file_ra_state ra_state; | |
587 | int error; | |
b3b94faa | 588 | |
6c53267f RP |
589 | file_ra_state_init(&ra_state, inode->i_mapping); |
590 | for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) { | |
591 | /* Ignore partials */ | |
592 | if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) > | |
593 | ip->i_di.di_size) | |
594 | break; | |
595 | error = read_rindex_entry(ip, &ra_state); | |
596 | if (error) { | |
597 | clear_rgrpdi(sdp); | |
598 | return error; | |
599 | } | |
b3b94faa DT |
600 | } |
601 | ||
602 | sdp->sd_rindex_vn = ip->i_gl->gl_vn; | |
b3b94faa | 603 | return 0; |
b3b94faa DT |
604 | } |
605 | ||
606 | /** | |
607 | * gfs2_rindex_hold - Grab a lock on the rindex | |
608 | * @sdp: The GFS2 superblock | |
609 | * @ri_gh: the glock holder | |
610 | * | |
611 | * We grab a lock on the rindex inode to make sure that it doesn't | |
612 | * change whilst we are performing an operation. We keep this lock | |
613 | * for quite long periods of time compared to other locks. This | |
614 | * doesn't matter, since it is shared and it is very, very rarely | |
615 | * accessed in the exclusive mode (i.e. only when expanding the filesystem). | |
616 | * | |
617 | * This makes sure that we're using the latest copy of the resource index | |
618 | * special file, which might have been updated if someone expanded the | |
619 | * filesystem (via gfs2_grow utility), which adds new resource groups. | |
620 | * | |
621 | * Returns: 0 on success, error code otherwise | |
622 | */ | |
623 | ||
624 | int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh) | |
625 | { | |
feaa7bba | 626 | struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex); |
b3b94faa DT |
627 | struct gfs2_glock *gl = ip->i_gl; |
628 | int error; | |
629 | ||
630 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh); | |
631 | if (error) | |
632 | return error; | |
633 | ||
634 | /* Read new copy from disk if we don't have the latest */ | |
635 | if (sdp->sd_rindex_vn != gl->gl_vn) { | |
f55ab26a | 636 | mutex_lock(&sdp->sd_rindex_mutex); |
b3b94faa DT |
637 | if (sdp->sd_rindex_vn != gl->gl_vn) { |
638 | error = gfs2_ri_update(ip); | |
639 | if (error) | |
640 | gfs2_glock_dq_uninit(ri_gh); | |
641 | } | |
f55ab26a | 642 | mutex_unlock(&sdp->sd_rindex_mutex); |
b3b94faa DT |
643 | } |
644 | ||
645 | return error; | |
646 | } | |
647 | ||
bb8d8a6f SW |
648 | static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf) |
649 | { | |
650 | const struct gfs2_rgrp *str = buf; | |
651 | ||
652 | rg->rg_flags = be32_to_cpu(str->rg_flags); | |
653 | rg->rg_free = be32_to_cpu(str->rg_free); | |
654 | rg->rg_dinodes = be32_to_cpu(str->rg_dinodes); | |
655 | rg->rg_igeneration = be64_to_cpu(str->rg_igeneration); | |
656 | } | |
657 | ||
658 | static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf) | |
659 | { | |
660 | struct gfs2_rgrp *str = buf; | |
661 | ||
662 | str->rg_flags = cpu_to_be32(rg->rg_flags); | |
663 | str->rg_free = cpu_to_be32(rg->rg_free); | |
664 | str->rg_dinodes = cpu_to_be32(rg->rg_dinodes); | |
665 | str->__pad = cpu_to_be32(0); | |
666 | str->rg_igeneration = cpu_to_be64(rg->rg_igeneration); | |
667 | memset(&str->rg_reserved, 0, sizeof(str->rg_reserved)); | |
668 | } | |
669 | ||
b3b94faa DT |
670 | /** |
671 | * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps | |
672 | * @rgd: the struct gfs2_rgrpd describing the RG to read in | |
673 | * | |
674 | * Read in all of a Resource Group's header and bitmap blocks. | |
675 | * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps. | |
676 | * | |
677 | * Returns: errno | |
678 | */ | |
679 | ||
680 | int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd) | |
681 | { | |
682 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
683 | struct gfs2_glock *gl = rgd->rd_gl; | |
bb8d8a6f | 684 | unsigned int length = rgd->rd_length; |
b3b94faa DT |
685 | struct gfs2_bitmap *bi; |
686 | unsigned int x, y; | |
687 | int error; | |
688 | ||
f55ab26a | 689 | mutex_lock(&rgd->rd_mutex); |
b3b94faa DT |
690 | |
691 | spin_lock(&sdp->sd_rindex_spin); | |
692 | if (rgd->rd_bh_count) { | |
693 | rgd->rd_bh_count++; | |
694 | spin_unlock(&sdp->sd_rindex_spin); | |
f55ab26a | 695 | mutex_unlock(&rgd->rd_mutex); |
b3b94faa DT |
696 | return 0; |
697 | } | |
698 | spin_unlock(&sdp->sd_rindex_spin); | |
699 | ||
700 | for (x = 0; x < length; x++) { | |
701 | bi = rgd->rd_bits + x; | |
bb8d8a6f | 702 | error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh); |
b3b94faa DT |
703 | if (error) |
704 | goto fail; | |
705 | } | |
706 | ||
707 | for (y = length; y--;) { | |
708 | bi = rgd->rd_bits + y; | |
7276b3b0 | 709 | error = gfs2_meta_wait(sdp, bi->bi_bh); |
b3b94faa DT |
710 | if (error) |
711 | goto fail; | |
feaa7bba | 712 | if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB : |
b3b94faa DT |
713 | GFS2_METATYPE_RG)) { |
714 | error = -EIO; | |
715 | goto fail; | |
716 | } | |
717 | } | |
718 | ||
719 | if (rgd->rd_rg_vn != gl->gl_vn) { | |
720 | gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data); | |
721 | rgd->rd_rg_vn = gl->gl_vn; | |
722 | } | |
723 | ||
724 | spin_lock(&sdp->sd_rindex_spin); | |
725 | rgd->rd_free_clone = rgd->rd_rg.rg_free; | |
726 | rgd->rd_bh_count++; | |
727 | spin_unlock(&sdp->sd_rindex_spin); | |
728 | ||
f55ab26a | 729 | mutex_unlock(&rgd->rd_mutex); |
b3b94faa DT |
730 | |
731 | return 0; | |
732 | ||
feaa7bba | 733 | fail: |
b3b94faa DT |
734 | while (x--) { |
735 | bi = rgd->rd_bits + x; | |
736 | brelse(bi->bi_bh); | |
737 | bi->bi_bh = NULL; | |
738 | gfs2_assert_warn(sdp, !bi->bi_clone); | |
739 | } | |
f55ab26a | 740 | mutex_unlock(&rgd->rd_mutex); |
b3b94faa DT |
741 | |
742 | return error; | |
743 | } | |
744 | ||
745 | void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd) | |
746 | { | |
747 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
748 | ||
749 | spin_lock(&sdp->sd_rindex_spin); | |
750 | gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count); | |
751 | rgd->rd_bh_count++; | |
752 | spin_unlock(&sdp->sd_rindex_spin); | |
753 | } | |
754 | ||
755 | /** | |
756 | * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get() | |
757 | * @rgd: the struct gfs2_rgrpd describing the RG to read in | |
758 | * | |
759 | */ | |
760 | ||
761 | void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd) | |
762 | { | |
763 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
bb8d8a6f | 764 | int x, length = rgd->rd_length; |
b3b94faa DT |
765 | |
766 | spin_lock(&sdp->sd_rindex_spin); | |
767 | gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count); | |
768 | if (--rgd->rd_bh_count) { | |
769 | spin_unlock(&sdp->sd_rindex_spin); | |
770 | return; | |
771 | } | |
772 | ||
773 | for (x = 0; x < length; x++) { | |
774 | struct gfs2_bitmap *bi = rgd->rd_bits + x; | |
775 | kfree(bi->bi_clone); | |
776 | bi->bi_clone = NULL; | |
777 | brelse(bi->bi_bh); | |
778 | bi->bi_bh = NULL; | |
779 | } | |
780 | ||
781 | spin_unlock(&sdp->sd_rindex_spin); | |
782 | } | |
783 | ||
784 | void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd) | |
785 | { | |
786 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
bb8d8a6f | 787 | unsigned int length = rgd->rd_length; |
b3b94faa DT |
788 | unsigned int x; |
789 | ||
790 | for (x = 0; x < length; x++) { | |
791 | struct gfs2_bitmap *bi = rgd->rd_bits + x; | |
792 | if (!bi->bi_clone) | |
793 | continue; | |
794 | memcpy(bi->bi_clone + bi->bi_offset, | |
feaa7bba | 795 | bi->bi_bh->b_data + bi->bi_offset, bi->bi_len); |
b3b94faa DT |
796 | } |
797 | ||
798 | spin_lock(&sdp->sd_rindex_spin); | |
799 | rgd->rd_free_clone = rgd->rd_rg.rg_free; | |
800 | spin_unlock(&sdp->sd_rindex_spin); | |
801 | } | |
802 | ||
803 | /** | |
804 | * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode | |
805 | * @ip: the incore GFS2 inode structure | |
806 | * | |
807 | * Returns: the struct gfs2_alloc | |
808 | */ | |
809 | ||
810 | struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip) | |
811 | { | |
812 | struct gfs2_alloc *al = &ip->i_alloc; | |
813 | ||
814 | /* FIXME: Should assert that the correct locks are held here... */ | |
815 | memset(al, 0, sizeof(*al)); | |
816 | return al; | |
817 | } | |
818 | ||
b3b94faa DT |
819 | /** |
820 | * try_rgrp_fit - See if a given reservation will fit in a given RG | |
821 | * @rgd: the RG data | |
822 | * @al: the struct gfs2_alloc structure describing the reservation | |
823 | * | |
824 | * If there's room for the requested blocks to be allocated from the RG: | |
b3b94faa DT |
825 | * Sets the $al_rgd field in @al. |
826 | * | |
827 | * Returns: 1 on success (it fits), 0 on failure (it doesn't fit) | |
828 | */ | |
829 | ||
830 | static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al) | |
831 | { | |
832 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
833 | int ret = 0; | |
834 | ||
a43a4906 SW |
835 | if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC) |
836 | return 0; | |
837 | ||
b3b94faa DT |
838 | spin_lock(&sdp->sd_rindex_spin); |
839 | if (rgd->rd_free_clone >= al->al_requested) { | |
840 | al->al_rgd = rgd; | |
841 | ret = 1; | |
842 | } | |
843 | spin_unlock(&sdp->sd_rindex_spin); | |
844 | ||
845 | return ret; | |
846 | } | |
847 | ||
848 | /** | |
849 | * recent_rgrp_first - get first RG from "recent" list | |
850 | * @sdp: The GFS2 superblock | |
851 | * @rglast: address of the rgrp used last | |
852 | * | |
853 | * Returns: The first rgrp in the recent list | |
854 | */ | |
855 | ||
856 | static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp, | |
cd915493 | 857 | u64 rglast) |
b3b94faa DT |
858 | { |
859 | struct gfs2_rgrpd *rgd = NULL; | |
860 | ||
861 | spin_lock(&sdp->sd_rindex_spin); | |
862 | ||
863 | if (list_empty(&sdp->sd_rindex_recent_list)) | |
864 | goto out; | |
865 | ||
866 | if (!rglast) | |
867 | goto first; | |
868 | ||
869 | list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) { | |
bb8d8a6f | 870 | if (rgd->rd_addr == rglast) |
b3b94faa DT |
871 | goto out; |
872 | } | |
873 | ||
feaa7bba | 874 | first: |
b3b94faa DT |
875 | rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd, |
876 | rd_recent); | |
feaa7bba | 877 | out: |
b3b94faa | 878 | spin_unlock(&sdp->sd_rindex_spin); |
b3b94faa DT |
879 | return rgd; |
880 | } | |
881 | ||
882 | /** | |
883 | * recent_rgrp_next - get next RG from "recent" list | |
884 | * @cur_rgd: current rgrp | |
885 | * @remove: | |
886 | * | |
887 | * Returns: The next rgrp in the recent list | |
888 | */ | |
889 | ||
890 | static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd, | |
891 | int remove) | |
892 | { | |
893 | struct gfs2_sbd *sdp = cur_rgd->rd_sbd; | |
894 | struct list_head *head; | |
895 | struct gfs2_rgrpd *rgd; | |
896 | ||
897 | spin_lock(&sdp->sd_rindex_spin); | |
898 | ||
899 | head = &sdp->sd_rindex_recent_list; | |
900 | ||
901 | list_for_each_entry(rgd, head, rd_recent) { | |
902 | if (rgd == cur_rgd) { | |
903 | if (cur_rgd->rd_recent.next != head) | |
904 | rgd = list_entry(cur_rgd->rd_recent.next, | |
905 | struct gfs2_rgrpd, rd_recent); | |
906 | else | |
907 | rgd = NULL; | |
908 | ||
909 | if (remove) | |
910 | list_del(&cur_rgd->rd_recent); | |
911 | ||
912 | goto out; | |
913 | } | |
914 | } | |
915 | ||
916 | rgd = NULL; | |
917 | if (!list_empty(head)) | |
918 | rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent); | |
919 | ||
feaa7bba | 920 | out: |
b3b94faa | 921 | spin_unlock(&sdp->sd_rindex_spin); |
b3b94faa DT |
922 | return rgd; |
923 | } | |
924 | ||
925 | /** | |
926 | * recent_rgrp_add - add an RG to tail of "recent" list | |
927 | * @new_rgd: The rgrp to add | |
928 | * | |
929 | */ | |
930 | ||
931 | static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd) | |
932 | { | |
933 | struct gfs2_sbd *sdp = new_rgd->rd_sbd; | |
934 | struct gfs2_rgrpd *rgd; | |
935 | unsigned int count = 0; | |
936 | unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp); | |
937 | ||
938 | spin_lock(&sdp->sd_rindex_spin); | |
939 | ||
940 | list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) { | |
941 | if (rgd == new_rgd) | |
942 | goto out; | |
943 | ||
944 | if (++count >= max) | |
945 | goto out; | |
946 | } | |
947 | list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list); | |
948 | ||
feaa7bba | 949 | out: |
b3b94faa DT |
950 | spin_unlock(&sdp->sd_rindex_spin); |
951 | } | |
952 | ||
953 | /** | |
954 | * forward_rgrp_get - get an rgrp to try next from full list | |
955 | * @sdp: The GFS2 superblock | |
956 | * | |
957 | * Returns: The rgrp to try next | |
958 | */ | |
959 | ||
960 | static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp) | |
961 | { | |
962 | struct gfs2_rgrpd *rgd; | |
963 | unsigned int journals = gfs2_jindex_size(sdp); | |
964 | unsigned int rg = 0, x; | |
965 | ||
966 | spin_lock(&sdp->sd_rindex_spin); | |
967 | ||
968 | rgd = sdp->sd_rindex_forward; | |
969 | if (!rgd) { | |
970 | if (sdp->sd_rgrps >= journals) | |
971 | rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals; | |
972 | ||
b8e1aabf | 973 | for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg; |
b3b94faa DT |
974 | x++, rgd = gfs2_rgrpd_get_next(rgd)) |
975 | /* Do Nothing */; | |
976 | ||
977 | sdp->sd_rindex_forward = rgd; | |
978 | } | |
979 | ||
980 | spin_unlock(&sdp->sd_rindex_spin); | |
981 | ||
982 | return rgd; | |
983 | } | |
984 | ||
985 | /** | |
986 | * forward_rgrp_set - set the forward rgrp pointer | |
987 | * @sdp: the filesystem | |
988 | * @rgd: The new forward rgrp | |
989 | * | |
990 | */ | |
991 | ||
992 | static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd) | |
993 | { | |
994 | spin_lock(&sdp->sd_rindex_spin); | |
995 | sdp->sd_rindex_forward = rgd; | |
996 | spin_unlock(&sdp->sd_rindex_spin); | |
997 | } | |
998 | ||
999 | /** | |
1000 | * get_local_rgrp - Choose and lock a rgrp for allocation | |
1001 | * @ip: the inode to reserve space for | |
1002 | * @rgp: the chosen and locked rgrp | |
1003 | * | |
1004 | * Try to acquire rgrp in way which avoids contending with others. | |
1005 | * | |
1006 | * Returns: errno | |
1007 | */ | |
1008 | ||
1009 | static int get_local_rgrp(struct gfs2_inode *ip) | |
1010 | { | |
feaa7bba | 1011 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1012 | struct gfs2_rgrpd *rgd, *begin = NULL; |
1013 | struct gfs2_alloc *al = &ip->i_alloc; | |
1014 | int flags = LM_FLAG_TRY; | |
1015 | int skipped = 0; | |
1016 | int loops = 0; | |
1017 | int error; | |
1018 | ||
1019 | /* Try recently successful rgrps */ | |
1020 | ||
1021 | rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc); | |
1022 | ||
1023 | while (rgd) { | |
907b9bce | 1024 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, |
b8e1aabf | 1025 | LM_FLAG_TRY, &al->al_rgd_gh); |
b3b94faa DT |
1026 | switch (error) { |
1027 | case 0: | |
1028 | if (try_rgrp_fit(rgd, al)) | |
1029 | goto out; | |
1030 | gfs2_glock_dq_uninit(&al->al_rgd_gh); | |
1031 | rgd = recent_rgrp_next(rgd, 1); | |
1032 | break; | |
1033 | ||
1034 | case GLR_TRYFAILED: | |
1035 | rgd = recent_rgrp_next(rgd, 0); | |
1036 | break; | |
1037 | ||
1038 | default: | |
1039 | return error; | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | /* Go through full list of rgrps */ | |
1044 | ||
1045 | begin = rgd = forward_rgrp_get(sdp); | |
1046 | ||
1047 | for (;;) { | |
b8e1aabf | 1048 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags, |
b3b94faa DT |
1049 | &al->al_rgd_gh); |
1050 | switch (error) { | |
1051 | case 0: | |
1052 | if (try_rgrp_fit(rgd, al)) | |
1053 | goto out; | |
1054 | gfs2_glock_dq_uninit(&al->al_rgd_gh); | |
1055 | break; | |
1056 | ||
1057 | case GLR_TRYFAILED: | |
1058 | skipped++; | |
1059 | break; | |
1060 | ||
1061 | default: | |
1062 | return error; | |
1063 | } | |
1064 | ||
1065 | rgd = gfs2_rgrpd_get_next(rgd); | |
1066 | if (!rgd) | |
1067 | rgd = gfs2_rgrpd_get_first(sdp); | |
1068 | ||
1069 | if (rgd == begin) { | |
172e045a | 1070 | if (++loops >= 3) |
b3b94faa | 1071 | return -ENOSPC; |
172e045a BM |
1072 | if (!skipped) |
1073 | loops++; | |
b3b94faa | 1074 | flags = 0; |
172e045a BM |
1075 | if (loops == 2) |
1076 | gfs2_log_flush(sdp, NULL); | |
b3b94faa DT |
1077 | } |
1078 | } | |
1079 | ||
feaa7bba | 1080 | out: |
bb8d8a6f | 1081 | ip->i_last_rg_alloc = rgd->rd_addr; |
b3b94faa DT |
1082 | |
1083 | if (begin) { | |
1084 | recent_rgrp_add(rgd); | |
1085 | rgd = gfs2_rgrpd_get_next(rgd); | |
1086 | if (!rgd) | |
1087 | rgd = gfs2_rgrpd_get_first(sdp); | |
1088 | forward_rgrp_set(sdp, rgd); | |
1089 | } | |
1090 | ||
1091 | return 0; | |
1092 | } | |
1093 | ||
1094 | /** | |
1095 | * gfs2_inplace_reserve_i - Reserve space in the filesystem | |
1096 | * @ip: the inode to reserve space for | |
1097 | * | |
1098 | * Returns: errno | |
1099 | */ | |
1100 | ||
1101 | int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line) | |
1102 | { | |
feaa7bba | 1103 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa | 1104 | struct gfs2_alloc *al = &ip->i_alloc; |
7ae8fa84 | 1105 | int error = 0; |
b3b94faa DT |
1106 | |
1107 | if (gfs2_assert_warn(sdp, al->al_requested)) | |
1108 | return -EINVAL; | |
1109 | ||
7ae8fa84 RP |
1110 | /* We need to hold the rindex unless the inode we're using is |
1111 | the rindex itself, in which case it's already held. */ | |
1112 | if (ip != GFS2_I(sdp->sd_rindex)) | |
1113 | error = gfs2_rindex_hold(sdp, &al->al_ri_gh); | |
1114 | else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */ | |
6c53267f | 1115 | error = gfs2_ri_update_special(ip); |
7ae8fa84 | 1116 | |
b3b94faa DT |
1117 | if (error) |
1118 | return error; | |
1119 | ||
1120 | error = get_local_rgrp(ip); | |
1121 | if (error) { | |
7ae8fa84 RP |
1122 | if (ip != GFS2_I(sdp->sd_rindex)) |
1123 | gfs2_glock_dq_uninit(&al->al_ri_gh); | |
b3b94faa DT |
1124 | return error; |
1125 | } | |
1126 | ||
1127 | al->al_file = file; | |
1128 | al->al_line = line; | |
1129 | ||
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | /** | |
1134 | * gfs2_inplace_release - release an inplace reservation | |
1135 | * @ip: the inode the reservation was taken out on | |
1136 | * | |
1137 | * Release a reservation made by gfs2_inplace_reserve(). | |
1138 | */ | |
1139 | ||
1140 | void gfs2_inplace_release(struct gfs2_inode *ip) | |
1141 | { | |
feaa7bba | 1142 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1143 | struct gfs2_alloc *al = &ip->i_alloc; |
1144 | ||
1145 | if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1) | |
1146 | fs_warn(sdp, "al_alloced = %u, al_requested = %u " | |
1147 | "al_file = %s, al_line = %u\n", | |
1148 | al->al_alloced, al->al_requested, al->al_file, | |
1149 | al->al_line); | |
1150 | ||
1151 | al->al_rgd = NULL; | |
1152 | gfs2_glock_dq_uninit(&al->al_rgd_gh); | |
7ae8fa84 RP |
1153 | if (ip != GFS2_I(sdp->sd_rindex)) |
1154 | gfs2_glock_dq_uninit(&al->al_ri_gh); | |
b3b94faa DT |
1155 | } |
1156 | ||
1157 | /** | |
1158 | * gfs2_get_block_type - Check a block in a RG is of given type | |
1159 | * @rgd: the resource group holding the block | |
1160 | * @block: the block number | |
1161 | * | |
1162 | * Returns: The block type (GFS2_BLKST_*) | |
1163 | */ | |
1164 | ||
cd915493 | 1165 | unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block) |
b3b94faa DT |
1166 | { |
1167 | struct gfs2_bitmap *bi = NULL; | |
cd915493 | 1168 | u32 length, rgrp_block, buf_block; |
b3b94faa DT |
1169 | unsigned int buf; |
1170 | unsigned char type; | |
1171 | ||
bb8d8a6f SW |
1172 | length = rgd->rd_length; |
1173 | rgrp_block = block - rgd->rd_data0; | |
b3b94faa DT |
1174 | |
1175 | for (buf = 0; buf < length; buf++) { | |
1176 | bi = rgd->rd_bits + buf; | |
1177 | if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY) | |
1178 | break; | |
1179 | } | |
1180 | ||
1181 | gfs2_assert(rgd->rd_sbd, buf < length); | |
1182 | buf_block = rgrp_block - bi->bi_start * GFS2_NBBY; | |
1183 | ||
feaa7bba | 1184 | type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
b3b94faa DT |
1185 | bi->bi_len, buf_block); |
1186 | ||
1187 | return type; | |
1188 | } | |
1189 | ||
1190 | /** | |
1191 | * rgblk_search - find a block in @old_state, change allocation | |
1192 | * state to @new_state | |
1193 | * @rgd: the resource group descriptor | |
1194 | * @goal: the goal block within the RG (start here to search for avail block) | |
1195 | * @old_state: GFS2_BLKST_XXX the before-allocation state to find | |
1196 | * @new_state: GFS2_BLKST_XXX the after-allocation block state | |
1197 | * | |
1198 | * Walk rgrp's bitmap to find bits that represent a block in @old_state. | |
1199 | * Add the found bitmap buffer to the transaction. | |
1200 | * Set the found bits to @new_state to change block's allocation state. | |
1201 | * | |
1202 | * This function never fails, because we wouldn't call it unless we | |
1203 | * know (from reservation results, etc.) that a block is available. | |
1204 | * | |
1205 | * Scope of @goal and returned block is just within rgrp, not the whole | |
1206 | * filesystem. | |
1207 | * | |
1208 | * Returns: the block number allocated | |
1209 | */ | |
1210 | ||
cd915493 | 1211 | static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, |
b3b94faa DT |
1212 | unsigned char old_state, unsigned char new_state) |
1213 | { | |
1214 | struct gfs2_bitmap *bi = NULL; | |
bb8d8a6f | 1215 | u32 length = rgd->rd_length; |
cd915493 | 1216 | u32 blk = 0; |
b3b94faa DT |
1217 | unsigned int buf, x; |
1218 | ||
1219 | /* Find bitmap block that contains bits for goal block */ | |
1220 | for (buf = 0; buf < length; buf++) { | |
1221 | bi = rgd->rd_bits + buf; | |
1222 | if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) | |
1223 | break; | |
1224 | } | |
1225 | ||
1226 | gfs2_assert(rgd->rd_sbd, buf < length); | |
1227 | ||
1228 | /* Convert scope of "goal" from rgrp-wide to within found bit block */ | |
1229 | goal -= bi->bi_start * GFS2_NBBY; | |
1230 | ||
1231 | /* Search (up to entire) bitmap in this rgrp for allocatable block. | |
1232 | "x <= length", instead of "x < length", because we typically start | |
1233 | the search in the middle of a bit block, but if we can't find an | |
1234 | allocatable block anywhere else, we want to be able wrap around and | |
1235 | search in the first part of our first-searched bit block. */ | |
1236 | for (x = 0; x <= length; x++) { | |
1237 | if (bi->bi_clone) | |
fd88de56 | 1238 | blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset, |
b3b94faa DT |
1239 | bi->bi_len, goal, old_state); |
1240 | else | |
1241 | blk = gfs2_bitfit(rgd, | |
1242 | bi->bi_bh->b_data + bi->bi_offset, | |
1243 | bi->bi_len, goal, old_state); | |
1244 | if (blk != BFITNOENT) | |
1245 | break; | |
1246 | ||
1247 | /* Try next bitmap block (wrap back to rgrp header if at end) */ | |
1248 | buf = (buf + 1) % length; | |
1249 | bi = rgd->rd_bits + buf; | |
1250 | goal = 0; | |
1251 | } | |
1252 | ||
1253 | if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length)) | |
1254 | blk = 0; | |
1255 | ||
d4e9c4c3 | 1256 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
fd88de56 | 1257 | gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
b3b94faa DT |
1258 | bi->bi_len, blk, new_state); |
1259 | if (bi->bi_clone) | |
fd88de56 | 1260 | gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset, |
b3b94faa DT |
1261 | bi->bi_len, blk, new_state); |
1262 | ||
1263 | return bi->bi_start * GFS2_NBBY + blk; | |
1264 | } | |
1265 | ||
1266 | /** | |
1267 | * rgblk_free - Change alloc state of given block(s) | |
1268 | * @sdp: the filesystem | |
1269 | * @bstart: the start of a run of blocks to free | |
1270 | * @blen: the length of the block run (all must lie within ONE RG!) | |
1271 | * @new_state: GFS2_BLKST_XXX the after-allocation block state | |
1272 | * | |
1273 | * Returns: Resource group containing the block(s) | |
1274 | */ | |
1275 | ||
cd915493 SW |
1276 | static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart, |
1277 | u32 blen, unsigned char new_state) | |
b3b94faa DT |
1278 | { |
1279 | struct gfs2_rgrpd *rgd; | |
1280 | struct gfs2_bitmap *bi = NULL; | |
cd915493 | 1281 | u32 length, rgrp_blk, buf_blk; |
b3b94faa DT |
1282 | unsigned int buf; |
1283 | ||
1284 | rgd = gfs2_blk2rgrpd(sdp, bstart); | |
1285 | if (!rgd) { | |
1286 | if (gfs2_consist(sdp)) | |
382066da | 1287 | fs_err(sdp, "block = %llu\n", (unsigned long long)bstart); |
b3b94faa DT |
1288 | return NULL; |
1289 | } | |
1290 | ||
bb8d8a6f | 1291 | length = rgd->rd_length; |
b3b94faa | 1292 | |
bb8d8a6f | 1293 | rgrp_blk = bstart - rgd->rd_data0; |
b3b94faa DT |
1294 | |
1295 | while (blen--) { | |
1296 | for (buf = 0; buf < length; buf++) { | |
1297 | bi = rgd->rd_bits + buf; | |
1298 | if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY) | |
1299 | break; | |
1300 | } | |
1301 | ||
1302 | gfs2_assert(rgd->rd_sbd, buf < length); | |
1303 | ||
1304 | buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY; | |
1305 | rgrp_blk++; | |
1306 | ||
1307 | if (!bi->bi_clone) { | |
1308 | bi->bi_clone = kmalloc(bi->bi_bh->b_size, | |
dd894be8 | 1309 | GFP_NOFS | __GFP_NOFAIL); |
b3b94faa DT |
1310 | memcpy(bi->bi_clone + bi->bi_offset, |
1311 | bi->bi_bh->b_data + bi->bi_offset, | |
1312 | bi->bi_len); | |
1313 | } | |
d4e9c4c3 | 1314 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
dd894be8 | 1315 | gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
b3b94faa DT |
1316 | bi->bi_len, buf_blk, new_state); |
1317 | } | |
1318 | ||
1319 | return rgd; | |
1320 | } | |
1321 | ||
1322 | /** | |
1323 | * gfs2_alloc_data - Allocate a data block | |
1324 | * @ip: the inode to allocate the data block for | |
1325 | * | |
1326 | * Returns: the allocated block | |
1327 | */ | |
1328 | ||
4340fe62 | 1329 | u64 gfs2_alloc_data(struct gfs2_inode *ip) |
b3b94faa | 1330 | { |
feaa7bba | 1331 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1332 | struct gfs2_alloc *al = &ip->i_alloc; |
1333 | struct gfs2_rgrpd *rgd = al->al_rgd; | |
cd915493 SW |
1334 | u32 goal, blk; |
1335 | u64 block; | |
b3b94faa | 1336 | |
bb8d8a6f SW |
1337 | if (rgrp_contains_block(rgd, ip->i_di.di_goal_data)) |
1338 | goal = ip->i_di.di_goal_data - rgd->rd_data0; | |
b3b94faa DT |
1339 | else |
1340 | goal = rgd->rd_last_alloc_data; | |
1341 | ||
fd88de56 | 1342 | blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED); |
b3b94faa DT |
1343 | rgd->rd_last_alloc_data = blk; |
1344 | ||
bb8d8a6f | 1345 | block = rgd->rd_data0 + blk; |
b3b94faa DT |
1346 | ip->i_di.di_goal_data = block; |
1347 | ||
1348 | gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free); | |
1349 | rgd->rd_rg.rg_free--; | |
1350 | ||
d4e9c4c3 | 1351 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
b3b94faa DT |
1352 | gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data); |
1353 | ||
1354 | al->al_alloced++; | |
1355 | ||
1356 | gfs2_statfs_change(sdp, 0, -1, 0); | |
2933f925 | 1357 | gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid); |
b3b94faa DT |
1358 | |
1359 | spin_lock(&sdp->sd_rindex_spin); | |
1360 | rgd->rd_free_clone--; | |
1361 | spin_unlock(&sdp->sd_rindex_spin); | |
1362 | ||
1363 | return block; | |
1364 | } | |
1365 | ||
1366 | /** | |
1367 | * gfs2_alloc_meta - Allocate a metadata block | |
1368 | * @ip: the inode to allocate the metadata block for | |
1369 | * | |
1370 | * Returns: the allocated block | |
1371 | */ | |
1372 | ||
4340fe62 | 1373 | u64 gfs2_alloc_meta(struct gfs2_inode *ip) |
b3b94faa | 1374 | { |
feaa7bba | 1375 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1376 | struct gfs2_alloc *al = &ip->i_alloc; |
1377 | struct gfs2_rgrpd *rgd = al->al_rgd; | |
cd915493 SW |
1378 | u32 goal, blk; |
1379 | u64 block; | |
b3b94faa | 1380 | |
bb8d8a6f SW |
1381 | if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta)) |
1382 | goal = ip->i_di.di_goal_meta - rgd->rd_data0; | |
b3b94faa DT |
1383 | else |
1384 | goal = rgd->rd_last_alloc_meta; | |
1385 | ||
fd88de56 | 1386 | blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED); |
b3b94faa DT |
1387 | rgd->rd_last_alloc_meta = blk; |
1388 | ||
bb8d8a6f | 1389 | block = rgd->rd_data0 + blk; |
b3b94faa DT |
1390 | ip->i_di.di_goal_meta = block; |
1391 | ||
1392 | gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free); | |
1393 | rgd->rd_rg.rg_free--; | |
1394 | ||
d4e9c4c3 | 1395 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
b3b94faa DT |
1396 | gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data); |
1397 | ||
1398 | al->al_alloced++; | |
1399 | ||
1400 | gfs2_statfs_change(sdp, 0, -1, 0); | |
2933f925 | 1401 | gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid); |
b3b94faa DT |
1402 | gfs2_trans_add_unrevoke(sdp, block); |
1403 | ||
1404 | spin_lock(&sdp->sd_rindex_spin); | |
1405 | rgd->rd_free_clone--; | |
1406 | spin_unlock(&sdp->sd_rindex_spin); | |
1407 | ||
1408 | return block; | |
1409 | } | |
1410 | ||
1411 | /** | |
1412 | * gfs2_alloc_di - Allocate a dinode | |
1413 | * @dip: the directory that the inode is going in | |
1414 | * | |
1415 | * Returns: the block allocated | |
1416 | */ | |
1417 | ||
4340fe62 | 1418 | u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation) |
b3b94faa | 1419 | { |
feaa7bba | 1420 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
1421 | struct gfs2_alloc *al = &dip->i_alloc; |
1422 | struct gfs2_rgrpd *rgd = al->al_rgd; | |
4340fe62 SW |
1423 | u32 blk; |
1424 | u64 block; | |
b3b94faa DT |
1425 | |
1426 | blk = rgblk_search(rgd, rgd->rd_last_alloc_meta, | |
1427 | GFS2_BLKST_FREE, GFS2_BLKST_DINODE); | |
1428 | ||
1429 | rgd->rd_last_alloc_meta = blk; | |
1430 | ||
bb8d8a6f | 1431 | block = rgd->rd_data0 + blk; |
b3b94faa DT |
1432 | |
1433 | gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free); | |
1434 | rgd->rd_rg.rg_free--; | |
1435 | rgd->rd_rg.rg_dinodes++; | |
4340fe62 | 1436 | *generation = rgd->rd_rg.rg_igeneration++; |
d4e9c4c3 | 1437 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
b3b94faa DT |
1438 | gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data); |
1439 | ||
1440 | al->al_alloced++; | |
1441 | ||
1442 | gfs2_statfs_change(sdp, 0, -1, +1); | |
1443 | gfs2_trans_add_unrevoke(sdp, block); | |
1444 | ||
1445 | spin_lock(&sdp->sd_rindex_spin); | |
1446 | rgd->rd_free_clone--; | |
1447 | spin_unlock(&sdp->sd_rindex_spin); | |
1448 | ||
1449 | return block; | |
1450 | } | |
1451 | ||
1452 | /** | |
1453 | * gfs2_free_data - free a contiguous run of data block(s) | |
1454 | * @ip: the inode these blocks are being freed from | |
1455 | * @bstart: first block of a run of contiguous blocks | |
1456 | * @blen: the length of the block run | |
1457 | * | |
1458 | */ | |
1459 | ||
cd915493 | 1460 | void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen) |
b3b94faa | 1461 | { |
feaa7bba | 1462 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1463 | struct gfs2_rgrpd *rgd; |
1464 | ||
1465 | rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); | |
1466 | if (!rgd) | |
1467 | return; | |
1468 | ||
1469 | rgd->rd_rg.rg_free += blen; | |
1470 | ||
d4e9c4c3 | 1471 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
b3b94faa DT |
1472 | gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data); |
1473 | ||
1474 | gfs2_trans_add_rg(rgd); | |
1475 | ||
1476 | gfs2_statfs_change(sdp, 0, +blen, 0); | |
2933f925 | 1477 | gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); |
b3b94faa DT |
1478 | } |
1479 | ||
1480 | /** | |
1481 | * gfs2_free_meta - free a contiguous run of data block(s) | |
1482 | * @ip: the inode these blocks are being freed from | |
1483 | * @bstart: first block of a run of contiguous blocks | |
1484 | * @blen: the length of the block run | |
1485 | * | |
1486 | */ | |
1487 | ||
cd915493 | 1488 | void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen) |
b3b94faa | 1489 | { |
feaa7bba | 1490 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1491 | struct gfs2_rgrpd *rgd; |
1492 | ||
1493 | rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); | |
1494 | if (!rgd) | |
1495 | return; | |
1496 | ||
1497 | rgd->rd_rg.rg_free += blen; | |
1498 | ||
d4e9c4c3 | 1499 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
b3b94faa DT |
1500 | gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data); |
1501 | ||
1502 | gfs2_trans_add_rg(rgd); | |
1503 | ||
1504 | gfs2_statfs_change(sdp, 0, +blen, 0); | |
2933f925 | 1505 | gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); |
b3b94faa DT |
1506 | gfs2_meta_wipe(ip, bstart, blen); |
1507 | } | |
1508 | ||
feaa7bba SW |
1509 | void gfs2_unlink_di(struct inode *inode) |
1510 | { | |
1511 | struct gfs2_inode *ip = GFS2_I(inode); | |
1512 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
1513 | struct gfs2_rgrpd *rgd; | |
dbb7cae2 | 1514 | u64 blkno = ip->i_no_addr; |
feaa7bba SW |
1515 | |
1516 | rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED); | |
1517 | if (!rgd) | |
1518 | return; | |
1519 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); | |
1520 | gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data); | |
1521 | gfs2_trans_add_rg(rgd); | |
1522 | } | |
1523 | ||
cd915493 | 1524 | static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno) |
b3b94faa DT |
1525 | { |
1526 | struct gfs2_sbd *sdp = rgd->rd_sbd; | |
1527 | struct gfs2_rgrpd *tmp_rgd; | |
1528 | ||
1529 | tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE); | |
1530 | if (!tmp_rgd) | |
1531 | return; | |
1532 | gfs2_assert_withdraw(sdp, rgd == tmp_rgd); | |
1533 | ||
1534 | if (!rgd->rd_rg.rg_dinodes) | |
1535 | gfs2_consist_rgrpd(rgd); | |
1536 | rgd->rd_rg.rg_dinodes--; | |
1537 | rgd->rd_rg.rg_free++; | |
1538 | ||
d4e9c4c3 | 1539 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
b3b94faa DT |
1540 | gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data); |
1541 | ||
1542 | gfs2_statfs_change(sdp, 0, +1, -1); | |
1543 | gfs2_trans_add_rg(rgd); | |
1544 | } | |
1545 | ||
b3b94faa DT |
1546 | |
1547 | void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) | |
1548 | { | |
dbb7cae2 | 1549 | gfs2_free_uninit_di(rgd, ip->i_no_addr); |
2933f925 | 1550 | gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid); |
dbb7cae2 | 1551 | gfs2_meta_wipe(ip, ip->i_no_addr, 1); |
b3b94faa DT |
1552 | } |
1553 | ||
1554 | /** | |
1555 | * gfs2_rlist_add - add a RG to a list of RGs | |
1556 | * @sdp: the filesystem | |
1557 | * @rlist: the list of resource groups | |
1558 | * @block: the block | |
1559 | * | |
1560 | * Figure out what RG a block belongs to and add that RG to the list | |
1561 | * | |
1562 | * FIXME: Don't use NOFAIL | |
1563 | * | |
1564 | */ | |
1565 | ||
1566 | void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist, | |
cd915493 | 1567 | u64 block) |
b3b94faa DT |
1568 | { |
1569 | struct gfs2_rgrpd *rgd; | |
1570 | struct gfs2_rgrpd **tmp; | |
1571 | unsigned int new_space; | |
1572 | unsigned int x; | |
1573 | ||
1574 | if (gfs2_assert_warn(sdp, !rlist->rl_ghs)) | |
1575 | return; | |
1576 | ||
1577 | rgd = gfs2_blk2rgrpd(sdp, block); | |
1578 | if (!rgd) { | |
1579 | if (gfs2_consist(sdp)) | |
382066da | 1580 | fs_err(sdp, "block = %llu\n", (unsigned long long)block); |
b3b94faa DT |
1581 | return; |
1582 | } | |
1583 | ||
1584 | for (x = 0; x < rlist->rl_rgrps; x++) | |
1585 | if (rlist->rl_rgd[x] == rgd) | |
1586 | return; | |
1587 | ||
1588 | if (rlist->rl_rgrps == rlist->rl_space) { | |
1589 | new_space = rlist->rl_space + 10; | |
1590 | ||
1591 | tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *), | |
dd894be8 | 1592 | GFP_NOFS | __GFP_NOFAIL); |
b3b94faa DT |
1593 | |
1594 | if (rlist->rl_rgd) { | |
1595 | memcpy(tmp, rlist->rl_rgd, | |
1596 | rlist->rl_space * sizeof(struct gfs2_rgrpd *)); | |
1597 | kfree(rlist->rl_rgd); | |
1598 | } | |
1599 | ||
1600 | rlist->rl_space = new_space; | |
1601 | rlist->rl_rgd = tmp; | |
1602 | } | |
1603 | ||
1604 | rlist->rl_rgd[rlist->rl_rgrps++] = rgd; | |
1605 | } | |
1606 | ||
1607 | /** | |
1608 | * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate | |
1609 | * and initialize an array of glock holders for them | |
1610 | * @rlist: the list of resource groups | |
1611 | * @state: the lock state to acquire the RG lock in | |
1612 | * @flags: the modifier flags for the holder structures | |
1613 | * | |
1614 | * FIXME: Don't use NOFAIL | |
1615 | * | |
1616 | */ | |
1617 | ||
1618 | void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state, | |
1619 | int flags) | |
1620 | { | |
1621 | unsigned int x; | |
1622 | ||
1623 | rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder), | |
dd894be8 | 1624 | GFP_NOFS | __GFP_NOFAIL); |
b3b94faa DT |
1625 | for (x = 0; x < rlist->rl_rgrps; x++) |
1626 | gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, | |
1627 | state, flags, | |
1628 | &rlist->rl_ghs[x]); | |
1629 | } | |
1630 | ||
1631 | /** | |
1632 | * gfs2_rlist_free - free a resource group list | |
1633 | * @list: the list of resource groups | |
1634 | * | |
1635 | */ | |
1636 | ||
1637 | void gfs2_rlist_free(struct gfs2_rgrp_list *rlist) | |
1638 | { | |
1639 | unsigned int x; | |
1640 | ||
1641 | kfree(rlist->rl_rgd); | |
1642 | ||
1643 | if (rlist->rl_ghs) { | |
1644 | for (x = 0; x < rlist->rl_rgrps; x++) | |
1645 | gfs2_holder_uninit(&rlist->rl_ghs[x]); | |
1646 | kfree(rlist->rl_ghs); | |
1647 | } | |
1648 | } | |
1649 |