2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
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.
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>
16 #include <asm/semaphore.h>
31 * gfs2_rgrp_verify - Verify that a resource group is consistent
32 * @sdp: the filesystem
37 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
39 struct gfs2_sbd *sdp = rgd->rd_sbd;
40 struct gfs2_bitmap *bi = NULL;
41 uint32_t length = rgd->rd_ri.ri_length;
42 uint32_t count[4], tmp;
45 memset(count, 0, 4 * sizeof(uint32_t));
47 /* Count # blocks in each of 4 possible allocation states */
48 for (buf = 0; buf < length; buf++) {
49 bi = rgd->rd_bits + buf;
50 for (x = 0; x < 4; x++)
51 count[x] += gfs2_bitcount(rgd,
57 if (count[0] != rgd->rd_rg.rg_free) {
58 if (gfs2_consist_rgrpd(rgd))
59 fs_err(sdp, "free data mismatch: %u != %u\n",
60 count[0], rgd->rd_rg.rg_free);
64 tmp = rgd->rd_ri.ri_data -
66 rgd->rd_rg.rg_dinodes;
67 if (count[1] != tmp) {
68 if (gfs2_consist_rgrpd(rgd))
69 fs_err(sdp, "used data mismatch: %u != %u\n",
75 if (gfs2_consist_rgrpd(rgd))
76 fs_err(sdp, "free metadata mismatch: %u != 0\n",
81 if (count[3] != rgd->rd_rg.rg_dinodes) {
82 if (gfs2_consist_rgrpd(rgd))
83 fs_err(sdp, "used metadata mismatch: %u != %u\n",
84 count[3], rgd->rd_rg.rg_dinodes);
89 static inline int rgrp_contains_block(struct gfs2_rindex *ri, uint64_t block)
91 uint64_t first = ri->ri_data0;
92 uint64_t last = first + ri->ri_data;
93 return !!(first <= block && block < last);
97 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
98 * @sdp: The GFS2 superblock
99 * @n: The data block number
101 * Returns: The resource group, or NULL if not found
104 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, uint64_t blk)
106 struct gfs2_rgrpd *rgd;
108 spin_lock(&sdp->sd_rindex_spin);
110 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
111 if (rgrp_contains_block(&rgd->rd_ri, blk)) {
112 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
113 spin_unlock(&sdp->sd_rindex_spin);
118 spin_unlock(&sdp->sd_rindex_spin);
124 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
125 * @sdp: The GFS2 superblock
127 * Returns: The first rgrp in the filesystem
130 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
132 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
133 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
137 * gfs2_rgrpd_get_next - get the next RG
140 * Returns: The next rgrp
143 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
145 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
147 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
150 static void clear_rgrpdi(struct gfs2_sbd *sdp)
152 struct list_head *head;
153 struct gfs2_rgrpd *rgd;
154 struct gfs2_glock *gl;
156 spin_lock(&sdp->sd_rindex_spin);
157 sdp->sd_rindex_forward = NULL;
158 head = &sdp->sd_rindex_recent_list;
159 while (!list_empty(head)) {
160 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
161 list_del(&rgd->rd_recent);
163 spin_unlock(&sdp->sd_rindex_spin);
165 head = &sdp->sd_rindex_list;
166 while (!list_empty(head)) {
167 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
170 list_del(&rgd->rd_list);
171 list_del(&rgd->rd_list_mru);
174 set_gl2rgd(gl, NULL);
183 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
185 mutex_lock(&sdp->sd_rindex_mutex);
187 mutex_unlock(&sdp->sd_rindex_mutex);
191 * gfs2_compute_bitstructs - Compute the bitmap sizes
192 * @rgd: The resource group descriptor
194 * Calculates bitmap descriptors, one for each block that contains bitmap data
199 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
201 struct gfs2_sbd *sdp = rgd->rd_sbd;
202 struct gfs2_bitmap *bi;
203 uint32_t length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
204 uint32_t bytes_left, bytes;
207 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_KERNEL);
211 bytes_left = rgd->rd_ri.ri_bitbytes;
213 for (x = 0; x < length; x++) {
214 bi = rgd->rd_bits + x;
216 /* small rgrp; bitmap stored completely in header block */
219 bi->bi_offset = sizeof(struct gfs2_rgrp);
224 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
225 bi->bi_offset = sizeof(struct gfs2_rgrp);
229 } else if (x + 1 == length) {
231 bi->bi_offset = sizeof(struct gfs2_meta_header);
232 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
236 bytes = sdp->sd_sb.sb_bsize -
237 sizeof(struct gfs2_meta_header);
238 bi->bi_offset = sizeof(struct gfs2_meta_header);
239 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
247 gfs2_consist_rgrpd(rgd);
250 bi = rgd->rd_bits + (length - 1);
251 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
252 if (gfs2_consist_rgrpd(rgd)) {
253 gfs2_rindex_print(&rgd->rd_ri);
254 fs_err(sdp, "start=%u len=%u offset=%u\n",
255 bi->bi_start, bi->bi_len, bi->bi_offset);
264 * gfs2_ri_update - Pull in a new resource index from the disk
265 * @gl: The glock covering the rindex inode
267 * Returns: 0 on successful update, error code otherwise
270 static int gfs2_ri_update(struct gfs2_inode *ip)
272 struct gfs2_sbd *sdp = ip->i_sbd;
273 struct inode *inode = ip->i_vnode;
274 struct gfs2_rgrpd *rgd;
275 char buf[sizeof(struct gfs2_rindex)];
276 struct file_ra_state ra_state;
277 uint64_t junk = ip->i_di.di_size;
280 if (do_div(junk, sizeof(struct gfs2_rindex))) {
281 gfs2_consist_inode(ip);
287 file_ra_state_init(&ra_state, inode->i_mapping);
288 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
289 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
290 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
291 sizeof(struct gfs2_rindex));
294 if (error != sizeof(struct gfs2_rindex)) {
300 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_KERNEL);
305 mutex_init(&rgd->rd_mutex);
306 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
309 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
310 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
312 gfs2_rindex_in(&rgd->rd_ri, buf);
314 error = compute_bitstructs(rgd);
318 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
319 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
323 set_gl2rgd(rgd->rd_gl, rgd);
324 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
327 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
338 * gfs2_rindex_hold - Grab a lock on the rindex
339 * @sdp: The GFS2 superblock
340 * @ri_gh: the glock holder
342 * We grab a lock on the rindex inode to make sure that it doesn't
343 * change whilst we are performing an operation. We keep this lock
344 * for quite long periods of time compared to other locks. This
345 * doesn't matter, since it is shared and it is very, very rarely
346 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
348 * This makes sure that we're using the latest copy of the resource index
349 * special file, which might have been updated if someone expanded the
350 * filesystem (via gfs2_grow utility), which adds new resource groups.
352 * Returns: 0 on success, error code otherwise
355 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
357 struct gfs2_inode *ip = get_v2ip(sdp->sd_rindex);
358 struct gfs2_glock *gl = ip->i_gl;
361 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
365 /* Read new copy from disk if we don't have the latest */
366 if (sdp->sd_rindex_vn != gl->gl_vn) {
367 mutex_lock(&sdp->sd_rindex_mutex);
368 if (sdp->sd_rindex_vn != gl->gl_vn) {
369 error = gfs2_ri_update(ip);
371 gfs2_glock_dq_uninit(ri_gh);
373 mutex_unlock(&sdp->sd_rindex_mutex);
380 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
381 * @rgd: the struct gfs2_rgrpd describing the RG to read in
383 * Read in all of a Resource Group's header and bitmap blocks.
384 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
389 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
391 struct gfs2_sbd *sdp = rgd->rd_sbd;
392 struct gfs2_glock *gl = rgd->rd_gl;
393 unsigned int length = rgd->rd_ri.ri_length;
394 struct gfs2_bitmap *bi;
398 mutex_lock(&rgd->rd_mutex);
400 spin_lock(&sdp->sd_rindex_spin);
401 if (rgd->rd_bh_count) {
403 spin_unlock(&sdp->sd_rindex_spin);
404 mutex_unlock(&rgd->rd_mutex);
407 spin_unlock(&sdp->sd_rindex_spin);
409 for (x = 0; x < length; x++) {
410 bi = rgd->rd_bits + x;
411 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, DIO_START,
417 for (y = length; y--;) {
418 bi = rgd->rd_bits + y;
419 error = gfs2_meta_reread(sdp, bi->bi_bh, DIO_WAIT);
422 if (gfs2_metatype_check(sdp, bi->bi_bh,
423 (y) ? GFS2_METATYPE_RB :
430 if (rgd->rd_rg_vn != gl->gl_vn) {
431 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
432 rgd->rd_rg_vn = gl->gl_vn;
435 spin_lock(&sdp->sd_rindex_spin);
436 rgd->rd_free_clone = rgd->rd_rg.rg_free;
438 spin_unlock(&sdp->sd_rindex_spin);
440 mutex_unlock(&rgd->rd_mutex);
446 bi = rgd->rd_bits + x;
449 gfs2_assert_warn(sdp, !bi->bi_clone);
451 mutex_unlock(&rgd->rd_mutex);
456 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
458 struct gfs2_sbd *sdp = rgd->rd_sbd;
460 spin_lock(&sdp->sd_rindex_spin);
461 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
463 spin_unlock(&sdp->sd_rindex_spin);
467 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
468 * @rgd: the struct gfs2_rgrpd describing the RG to read in
472 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
474 struct gfs2_sbd *sdp = rgd->rd_sbd;
475 int x, length = rgd->rd_ri.ri_length;
477 spin_lock(&sdp->sd_rindex_spin);
478 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
479 if (--rgd->rd_bh_count) {
480 spin_unlock(&sdp->sd_rindex_spin);
484 for (x = 0; x < length; x++) {
485 struct gfs2_bitmap *bi = rgd->rd_bits + x;
492 spin_unlock(&sdp->sd_rindex_spin);
495 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
497 struct gfs2_sbd *sdp = rgd->rd_sbd;
498 unsigned int length = rgd->rd_ri.ri_length;
501 for (x = 0; x < length; x++) {
502 struct gfs2_bitmap *bi = rgd->rd_bits + x;
505 memcpy(bi->bi_clone + bi->bi_offset,
506 bi->bi_bh->b_data + bi->bi_offset,
510 spin_lock(&sdp->sd_rindex_spin);
511 rgd->rd_free_clone = rgd->rd_rg.rg_free;
512 spin_unlock(&sdp->sd_rindex_spin);
516 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
517 * @ip: the incore GFS2 inode structure
519 * Returns: the struct gfs2_alloc
522 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
524 struct gfs2_alloc *al = &ip->i_alloc;
526 /* FIXME: Should assert that the correct locks are held here... */
527 memset(al, 0, sizeof(*al));
532 * gfs2_alloc_put - throw away the struct gfs2_alloc for an inode
537 void gfs2_alloc_put(struct gfs2_inode *ip)
543 * try_rgrp_fit - See if a given reservation will fit in a given RG
545 * @al: the struct gfs2_alloc structure describing the reservation
547 * If there's room for the requested blocks to be allocated from the RG:
548 * Sets the $al_reserved_data field in @al.
549 * Sets the $al_reserved_meta field in @al.
550 * Sets the $al_rgd field in @al.
552 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
555 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
557 struct gfs2_sbd *sdp = rgd->rd_sbd;
560 spin_lock(&sdp->sd_rindex_spin);
561 if (rgd->rd_free_clone >= al->al_requested) {
565 spin_unlock(&sdp->sd_rindex_spin);
571 * recent_rgrp_first - get first RG from "recent" list
572 * @sdp: The GFS2 superblock
573 * @rglast: address of the rgrp used last
575 * Returns: The first rgrp in the recent list
578 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
581 struct gfs2_rgrpd *rgd = NULL;
583 spin_lock(&sdp->sd_rindex_spin);
585 if (list_empty(&sdp->sd_rindex_recent_list))
591 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
592 if (rgd->rd_ri.ri_addr == rglast)
597 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
601 spin_unlock(&sdp->sd_rindex_spin);
607 * recent_rgrp_next - get next RG from "recent" list
608 * @cur_rgd: current rgrp
611 * Returns: The next rgrp in the recent list
614 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
617 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
618 struct list_head *head;
619 struct gfs2_rgrpd *rgd;
621 spin_lock(&sdp->sd_rindex_spin);
623 head = &sdp->sd_rindex_recent_list;
625 list_for_each_entry(rgd, head, rd_recent) {
626 if (rgd == cur_rgd) {
627 if (cur_rgd->rd_recent.next != head)
628 rgd = list_entry(cur_rgd->rd_recent.next,
629 struct gfs2_rgrpd, rd_recent);
634 list_del(&cur_rgd->rd_recent);
641 if (!list_empty(head))
642 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
645 spin_unlock(&sdp->sd_rindex_spin);
651 * recent_rgrp_add - add an RG to tail of "recent" list
652 * @new_rgd: The rgrp to add
656 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
658 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
659 struct gfs2_rgrpd *rgd;
660 unsigned int count = 0;
661 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
663 spin_lock(&sdp->sd_rindex_spin);
665 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
672 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
675 spin_unlock(&sdp->sd_rindex_spin);
679 * forward_rgrp_get - get an rgrp to try next from full list
680 * @sdp: The GFS2 superblock
682 * Returns: The rgrp to try next
685 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
687 struct gfs2_rgrpd *rgd;
688 unsigned int journals = gfs2_jindex_size(sdp);
689 unsigned int rg = 0, x;
691 spin_lock(&sdp->sd_rindex_spin);
693 rgd = sdp->sd_rindex_forward;
695 if (sdp->sd_rgrps >= journals)
696 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
698 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp);
700 x++, rgd = gfs2_rgrpd_get_next(rgd))
703 sdp->sd_rindex_forward = rgd;
706 spin_unlock(&sdp->sd_rindex_spin);
712 * forward_rgrp_set - set the forward rgrp pointer
713 * @sdp: the filesystem
714 * @rgd: The new forward rgrp
718 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
720 spin_lock(&sdp->sd_rindex_spin);
721 sdp->sd_rindex_forward = rgd;
722 spin_unlock(&sdp->sd_rindex_spin);
726 * get_local_rgrp - Choose and lock a rgrp for allocation
727 * @ip: the inode to reserve space for
728 * @rgp: the chosen and locked rgrp
730 * Try to acquire rgrp in way which avoids contending with others.
735 static int get_local_rgrp(struct gfs2_inode *ip)
737 struct gfs2_sbd *sdp = ip->i_sbd;
738 struct gfs2_rgrpd *rgd, *begin = NULL;
739 struct gfs2_alloc *al = &ip->i_alloc;
740 int flags = LM_FLAG_TRY;
745 /* Try recently successful rgrps */
747 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
750 error = gfs2_glock_nq_init(rgd->rd_gl,
751 LM_ST_EXCLUSIVE, LM_FLAG_TRY,
755 if (try_rgrp_fit(rgd, al))
757 gfs2_glock_dq_uninit(&al->al_rgd_gh);
758 rgd = recent_rgrp_next(rgd, 1);
762 rgd = recent_rgrp_next(rgd, 0);
770 /* Go through full list of rgrps */
772 begin = rgd = forward_rgrp_get(sdp);
775 error = gfs2_glock_nq_init(rgd->rd_gl,
776 LM_ST_EXCLUSIVE, flags,
780 if (try_rgrp_fit(rgd, al))
782 gfs2_glock_dq_uninit(&al->al_rgd_gh);
793 rgd = gfs2_rgrpd_get_next(rgd);
795 rgd = gfs2_rgrpd_get_first(sdp);
798 if (++loops >= 2 || !skipped)
805 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
808 recent_rgrp_add(rgd);
809 rgd = gfs2_rgrpd_get_next(rgd);
811 rgd = gfs2_rgrpd_get_first(sdp);
812 forward_rgrp_set(sdp, rgd);
819 * gfs2_inplace_reserve_i - Reserve space in the filesystem
820 * @ip: the inode to reserve space for
825 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
827 struct gfs2_sbd *sdp = ip->i_sbd;
828 struct gfs2_alloc *al = &ip->i_alloc;
831 if (gfs2_assert_warn(sdp, al->al_requested))
834 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
838 error = get_local_rgrp(ip);
840 gfs2_glock_dq_uninit(&al->al_ri_gh);
851 * gfs2_inplace_release - release an inplace reservation
852 * @ip: the inode the reservation was taken out on
854 * Release a reservation made by gfs2_inplace_reserve().
857 void gfs2_inplace_release(struct gfs2_inode *ip)
859 struct gfs2_sbd *sdp = ip->i_sbd;
860 struct gfs2_alloc *al = &ip->i_alloc;
862 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
863 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
864 "al_file = %s, al_line = %u\n",
865 al->al_alloced, al->al_requested, al->al_file,
869 gfs2_glock_dq_uninit(&al->al_rgd_gh);
870 gfs2_glock_dq_uninit(&al->al_ri_gh);
874 * gfs2_get_block_type - Check a block in a RG is of given type
875 * @rgd: the resource group holding the block
876 * @block: the block number
878 * Returns: The block type (GFS2_BLKST_*)
881 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, uint64_t block)
883 struct gfs2_bitmap *bi = NULL;
884 uint32_t length, rgrp_block, buf_block;
888 length = rgd->rd_ri.ri_length;
889 rgrp_block = block - rgd->rd_ri.ri_data0;
891 for (buf = 0; buf < length; buf++) {
892 bi = rgd->rd_bits + buf;
893 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
897 gfs2_assert(rgd->rd_sbd, buf < length);
898 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
900 type = gfs2_testbit(rgd,
901 bi->bi_bh->b_data + bi->bi_offset,
902 bi->bi_len, buf_block);
908 * rgblk_search - find a block in @old_state, change allocation
909 * state to @new_state
910 * @rgd: the resource group descriptor
911 * @goal: the goal block within the RG (start here to search for avail block)
912 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
913 * @new_state: GFS2_BLKST_XXX the after-allocation block state
915 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
916 * Add the found bitmap buffer to the transaction.
917 * Set the found bits to @new_state to change block's allocation state.
919 * This function never fails, because we wouldn't call it unless we
920 * know (from reservation results, etc.) that a block is available.
922 * Scope of @goal and returned block is just within rgrp, not the whole
925 * Returns: the block number allocated
928 static uint32_t rgblk_search(struct gfs2_rgrpd *rgd, uint32_t goal,
929 unsigned char old_state, unsigned char new_state)
931 struct gfs2_bitmap *bi = NULL;
932 uint32_t length = rgd->rd_ri.ri_length;
936 /* Find bitmap block that contains bits for goal block */
937 for (buf = 0; buf < length; buf++) {
938 bi = rgd->rd_bits + buf;
939 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
943 gfs2_assert(rgd->rd_sbd, buf < length);
945 /* Convert scope of "goal" from rgrp-wide to within found bit block */
946 goal -= bi->bi_start * GFS2_NBBY;
948 /* Search (up to entire) bitmap in this rgrp for allocatable block.
949 "x <= length", instead of "x < length", because we typically start
950 the search in the middle of a bit block, but if we can't find an
951 allocatable block anywhere else, we want to be able wrap around and
952 search in the first part of our first-searched bit block. */
953 for (x = 0; x <= length; x++) {
955 blk = gfs2_bitfit(rgd,
956 bi->bi_clone + bi->bi_offset,
957 bi->bi_len, goal, old_state);
959 blk = gfs2_bitfit(rgd,
960 bi->bi_bh->b_data + bi->bi_offset,
961 bi->bi_len, goal, old_state);
962 if (blk != BFITNOENT)
965 /* Try next bitmap block (wrap back to rgrp header if at end) */
966 buf = (buf + 1) % length;
967 bi = rgd->rd_bits + buf;
971 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
974 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
976 bi->bi_bh->b_data + bi->bi_offset,
977 bi->bi_len, blk, new_state);
980 bi->bi_clone + bi->bi_offset,
981 bi->bi_len, blk, new_state);
983 return bi->bi_start * GFS2_NBBY + blk;
987 * rgblk_free - Change alloc state of given block(s)
988 * @sdp: the filesystem
989 * @bstart: the start of a run of blocks to free
990 * @blen: the length of the block run (all must lie within ONE RG!)
991 * @new_state: GFS2_BLKST_XXX the after-allocation block state
993 * Returns: Resource group containing the block(s)
996 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
997 uint32_t blen, unsigned char new_state)
999 struct gfs2_rgrpd *rgd;
1000 struct gfs2_bitmap *bi = NULL;
1001 uint32_t length, rgrp_blk, buf_blk;
1004 rgd = gfs2_blk2rgrpd(sdp, bstart);
1006 if (gfs2_consist(sdp))
1007 fs_err(sdp, "block = %llu\n", bstart);
1011 length = rgd->rd_ri.ri_length;
1013 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1016 for (buf = 0; buf < length; buf++) {
1017 bi = rgd->rd_bits + buf;
1018 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1022 gfs2_assert(rgd->rd_sbd, buf < length);
1024 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1027 if (!bi->bi_clone) {
1028 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1029 GFP_KERNEL | __GFP_NOFAIL);
1030 memcpy(bi->bi_clone + bi->bi_offset,
1031 bi->bi_bh->b_data + bi->bi_offset,
1034 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1036 bi->bi_bh->b_data + bi->bi_offset,
1037 bi->bi_len, buf_blk, new_state);
1044 * gfs2_alloc_data - Allocate a data block
1045 * @ip: the inode to allocate the data block for
1047 * Returns: the allocated block
1050 uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
1052 struct gfs2_sbd *sdp = ip->i_sbd;
1053 struct gfs2_alloc *al = &ip->i_alloc;
1054 struct gfs2_rgrpd *rgd = al->al_rgd;
1058 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1059 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1061 goal = rgd->rd_last_alloc_data;
1063 blk = rgblk_search(rgd, goal,
1064 GFS2_BLKST_FREE, GFS2_BLKST_USED);
1065 rgd->rd_last_alloc_data = blk;
1067 block = rgd->rd_ri.ri_data0 + blk;
1068 ip->i_di.di_goal_data = block;
1070 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1071 rgd->rd_rg.rg_free--;
1073 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1074 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1078 gfs2_statfs_change(sdp, 0, -1, 0);
1079 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1081 spin_lock(&sdp->sd_rindex_spin);
1082 rgd->rd_free_clone--;
1083 spin_unlock(&sdp->sd_rindex_spin);
1089 * gfs2_alloc_meta - Allocate a metadata block
1090 * @ip: the inode to allocate the metadata block for
1092 * Returns: the allocated block
1095 uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
1097 struct gfs2_sbd *sdp = ip->i_sbd;
1098 struct gfs2_alloc *al = &ip->i_alloc;
1099 struct gfs2_rgrpd *rgd = al->al_rgd;
1103 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1104 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1106 goal = rgd->rd_last_alloc_meta;
1108 blk = rgblk_search(rgd, goal,
1109 GFS2_BLKST_FREE, GFS2_BLKST_USED);
1110 rgd->rd_last_alloc_meta = blk;
1112 block = rgd->rd_ri.ri_data0 + blk;
1113 ip->i_di.di_goal_meta = block;
1115 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1116 rgd->rd_rg.rg_free--;
1118 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1119 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1123 gfs2_statfs_change(sdp, 0, -1, 0);
1124 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1125 gfs2_trans_add_unrevoke(sdp, block);
1127 spin_lock(&sdp->sd_rindex_spin);
1128 rgd->rd_free_clone--;
1129 spin_unlock(&sdp->sd_rindex_spin);
1135 * gfs2_alloc_di - Allocate a dinode
1136 * @dip: the directory that the inode is going in
1138 * Returns: the block allocated
1141 uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
1143 struct gfs2_sbd *sdp = dip->i_sbd;
1144 struct gfs2_alloc *al = &dip->i_alloc;
1145 struct gfs2_rgrpd *rgd = al->al_rgd;
1149 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1150 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1152 rgd->rd_last_alloc_meta = blk;
1154 block = rgd->rd_ri.ri_data0 + blk;
1156 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1157 rgd->rd_rg.rg_free--;
1158 rgd->rd_rg.rg_dinodes++;
1160 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1161 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1165 gfs2_statfs_change(sdp, 0, -1, +1);
1166 gfs2_trans_add_unrevoke(sdp, block);
1168 spin_lock(&sdp->sd_rindex_spin);
1169 rgd->rd_free_clone--;
1170 spin_unlock(&sdp->sd_rindex_spin);
1176 * gfs2_free_data - free a contiguous run of data block(s)
1177 * @ip: the inode these blocks are being freed from
1178 * @bstart: first block of a run of contiguous blocks
1179 * @blen: the length of the block run
1183 void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1185 struct gfs2_sbd *sdp = ip->i_sbd;
1186 struct gfs2_rgrpd *rgd;
1188 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1192 rgd->rd_rg.rg_free += blen;
1194 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1195 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1197 gfs2_trans_add_rg(rgd);
1199 gfs2_statfs_change(sdp, 0, +blen, 0);
1200 gfs2_quota_change(ip, -(int64_t)blen,
1201 ip->i_di.di_uid, ip->i_di.di_gid);
1205 * gfs2_free_meta - free a contiguous run of data block(s)
1206 * @ip: the inode these blocks are being freed from
1207 * @bstart: first block of a run of contiguous blocks
1208 * @blen: the length of the block run
1212 void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1214 struct gfs2_sbd *sdp = ip->i_sbd;
1215 struct gfs2_rgrpd *rgd;
1217 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1221 rgd->rd_rg.rg_free += blen;
1223 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1224 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1226 gfs2_trans_add_rg(rgd);
1228 gfs2_statfs_change(sdp, 0, +blen, 0);
1229 gfs2_quota_change(ip, -(int64_t)blen,
1230 ip->i_di.di_uid, ip->i_di.di_gid);
1231 gfs2_meta_wipe(ip, bstart, blen);
1234 void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
1236 struct gfs2_sbd *sdp = rgd->rd_sbd;
1237 struct gfs2_rgrpd *tmp_rgd;
1239 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1242 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1244 if (!rgd->rd_rg.rg_dinodes)
1245 gfs2_consist_rgrpd(rgd);
1246 rgd->rd_rg.rg_dinodes--;
1247 rgd->rd_rg.rg_free++;
1249 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1250 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1252 gfs2_statfs_change(sdp, 0, +1, -1);
1253 gfs2_trans_add_rg(rgd);
1257 * gfs2_free_uninit_di - free a dinode block
1258 * @rgd: the resource group that contains the dinode
1263 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1265 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
1266 gfs2_quota_change(ip, -1, ip->i_di.di_uid, ip->i_di.di_gid);
1267 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1271 * gfs2_rlist_add - add a RG to a list of RGs
1272 * @sdp: the filesystem
1273 * @rlist: the list of resource groups
1276 * Figure out what RG a block belongs to and add that RG to the list
1278 * FIXME: Don't use NOFAIL
1282 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1285 struct gfs2_rgrpd *rgd;
1286 struct gfs2_rgrpd **tmp;
1287 unsigned int new_space;
1290 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1293 rgd = gfs2_blk2rgrpd(sdp, block);
1295 if (gfs2_consist(sdp))
1296 fs_err(sdp, "block = %llu\n", block);
1300 for (x = 0; x < rlist->rl_rgrps; x++)
1301 if (rlist->rl_rgd[x] == rgd)
1304 if (rlist->rl_rgrps == rlist->rl_space) {
1305 new_space = rlist->rl_space + 10;
1307 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1308 GFP_KERNEL | __GFP_NOFAIL);
1310 if (rlist->rl_rgd) {
1311 memcpy(tmp, rlist->rl_rgd,
1312 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1313 kfree(rlist->rl_rgd);
1316 rlist->rl_space = new_space;
1317 rlist->rl_rgd = tmp;
1320 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1324 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1325 * and initialize an array of glock holders for them
1326 * @rlist: the list of resource groups
1327 * @state: the lock state to acquire the RG lock in
1328 * @flags: the modifier flags for the holder structures
1330 * FIXME: Don't use NOFAIL
1334 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1339 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1340 GFP_KERNEL | __GFP_NOFAIL);
1341 for (x = 0; x < rlist->rl_rgrps; x++)
1342 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1348 * gfs2_rlist_free - free a resource group list
1349 * @list: the list of resource groups
1353 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1357 kfree(rlist->rl_rgd);
1359 if (rlist->rl_ghs) {
1360 for (x = 0; x < rlist->rl_rgrps; x++)
1361 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1362 kfree(rlist->rl_ghs);