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