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