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 - sizeof(struct gfs2_meta_header);
237 bi->bi_offset = sizeof(struct gfs2_meta_header);
238 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
246 gfs2_consist_rgrpd(rgd);
249 bi = rgd->rd_bits + (length - 1);
250 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
251 if (gfs2_consist_rgrpd(rgd)) {
252 gfs2_rindex_print(&rgd->rd_ri);
253 fs_err(sdp, "start=%u len=%u offset=%u\n",
254 bi->bi_start, bi->bi_len, bi->bi_offset);
263 * gfs2_ri_update - Pull in a new resource index from the disk
264 * @gl: The glock covering the rindex inode
266 * Returns: 0 on successful update, error code otherwise
269 static int gfs2_ri_update(struct gfs2_inode *ip)
271 struct gfs2_sbd *sdp = ip->i_sbd;
272 struct inode *inode = ip->i_vnode;
273 struct gfs2_rgrpd *rgd;
274 char buf[sizeof(struct gfs2_rindex)];
275 struct file_ra_state ra_state;
276 uint64_t junk = ip->i_di.di_size;
279 if (do_div(junk, sizeof(struct gfs2_rindex))) {
280 gfs2_consist_inode(ip);
286 file_ra_state_init(&ra_state, inode->i_mapping);
287 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
288 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
289 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
290 sizeof(struct gfs2_rindex));
293 if (error != sizeof(struct gfs2_rindex)) {
299 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_KERNEL);
304 mutex_init(&rgd->rd_mutex);
305 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
308 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
309 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
311 gfs2_rindex_in(&rgd->rd_ri, buf);
313 error = compute_bitstructs(rgd);
317 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
318 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
322 set_gl2rgd(rgd->rd_gl, rgd);
323 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
326 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
337 * gfs2_rindex_hold - Grab a lock on the rindex
338 * @sdp: The GFS2 superblock
339 * @ri_gh: the glock holder
341 * We grab a lock on the rindex inode to make sure that it doesn't
342 * change whilst we are performing an operation. We keep this lock
343 * for quite long periods of time compared to other locks. This
344 * doesn't matter, since it is shared and it is very, very rarely
345 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
347 * This makes sure that we're using the latest copy of the resource index
348 * special file, which might have been updated if someone expanded the
349 * filesystem (via gfs2_grow utility), which adds new resource groups.
351 * Returns: 0 on success, error code otherwise
354 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
356 struct gfs2_inode *ip = get_v2ip(sdp->sd_rindex);
357 struct gfs2_glock *gl = ip->i_gl;
360 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
364 /* Read new copy from disk if we don't have the latest */
365 if (sdp->sd_rindex_vn != gl->gl_vn) {
366 mutex_lock(&sdp->sd_rindex_mutex);
367 if (sdp->sd_rindex_vn != gl->gl_vn) {
368 error = gfs2_ri_update(ip);
370 gfs2_glock_dq_uninit(ri_gh);
372 mutex_unlock(&sdp->sd_rindex_mutex);
379 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
380 * @rgd: the struct gfs2_rgrpd describing the RG to read in
382 * Read in all of a Resource Group's header and bitmap blocks.
383 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
388 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
390 struct gfs2_sbd *sdp = rgd->rd_sbd;
391 struct gfs2_glock *gl = rgd->rd_gl;
392 unsigned int length = rgd->rd_ri.ri_length;
393 struct gfs2_bitmap *bi;
397 mutex_lock(&rgd->rd_mutex);
399 spin_lock(&sdp->sd_rindex_spin);
400 if (rgd->rd_bh_count) {
402 spin_unlock(&sdp->sd_rindex_spin);
403 mutex_unlock(&rgd->rd_mutex);
406 spin_unlock(&sdp->sd_rindex_spin);
408 for (x = 0; x < length; x++) {
409 bi = rgd->rd_bits + x;
410 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, DIO_START,
416 for (y = length; y--;) {
417 bi = rgd->rd_bits + y;
418 error = gfs2_meta_reread(sdp, bi->bi_bh, DIO_WAIT);
421 if (gfs2_metatype_check(sdp, bi->bi_bh,
422 (y) ? GFS2_METATYPE_RB :
429 if (rgd->rd_rg_vn != gl->gl_vn) {
430 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
431 rgd->rd_rg_vn = gl->gl_vn;
434 spin_lock(&sdp->sd_rindex_spin);
435 rgd->rd_free_clone = rgd->rd_rg.rg_free;
437 spin_unlock(&sdp->sd_rindex_spin);
439 mutex_unlock(&rgd->rd_mutex);
445 bi = rgd->rd_bits + x;
448 gfs2_assert_warn(sdp, !bi->bi_clone);
450 mutex_unlock(&rgd->rd_mutex);
455 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
457 struct gfs2_sbd *sdp = rgd->rd_sbd;
459 spin_lock(&sdp->sd_rindex_spin);
460 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
462 spin_unlock(&sdp->sd_rindex_spin);
466 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
467 * @rgd: the struct gfs2_rgrpd describing the RG to read in
471 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
473 struct gfs2_sbd *sdp = rgd->rd_sbd;
474 int x, length = rgd->rd_ri.ri_length;
476 spin_lock(&sdp->sd_rindex_spin);
477 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
478 if (--rgd->rd_bh_count) {
479 spin_unlock(&sdp->sd_rindex_spin);
483 for (x = 0; x < length; x++) {
484 struct gfs2_bitmap *bi = rgd->rd_bits + x;
491 spin_unlock(&sdp->sd_rindex_spin);
494 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
496 struct gfs2_sbd *sdp = rgd->rd_sbd;
497 unsigned int length = rgd->rd_ri.ri_length;
500 for (x = 0; x < length; x++) {
501 struct gfs2_bitmap *bi = rgd->rd_bits + x;
504 memcpy(bi->bi_clone + bi->bi_offset,
505 bi->bi_bh->b_data + bi->bi_offset,
509 spin_lock(&sdp->sd_rindex_spin);
510 rgd->rd_free_clone = rgd->rd_rg.rg_free;
511 spin_unlock(&sdp->sd_rindex_spin);
515 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
516 * @ip: the incore GFS2 inode structure
518 * Returns: the struct gfs2_alloc
521 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
523 struct gfs2_alloc *al = &ip->i_alloc;
525 /* FIXME: Should assert that the correct locks are held here... */
526 memset(al, 0, sizeof(*al));
531 * gfs2_alloc_put - throw away the struct gfs2_alloc for an inode
536 void gfs2_alloc_put(struct gfs2_inode *ip)
542 * try_rgrp_fit - See if a given reservation will fit in a given RG
544 * @al: the struct gfs2_alloc structure describing the reservation
546 * If there's room for the requested blocks to be allocated from the RG:
547 * Sets the $al_reserved_data field in @al.
548 * Sets the $al_reserved_meta field in @al.
549 * Sets the $al_rgd field in @al.
551 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
554 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
556 struct gfs2_sbd *sdp = rgd->rd_sbd;
559 spin_lock(&sdp->sd_rindex_spin);
560 if (rgd->rd_free_clone >= al->al_requested) {
564 spin_unlock(&sdp->sd_rindex_spin);
570 * recent_rgrp_first - get first RG from "recent" list
571 * @sdp: The GFS2 superblock
572 * @rglast: address of the rgrp used last
574 * Returns: The first rgrp in the recent list
577 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
580 struct gfs2_rgrpd *rgd = NULL;
582 spin_lock(&sdp->sd_rindex_spin);
584 if (list_empty(&sdp->sd_rindex_recent_list))
590 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
591 if (rgd->rd_ri.ri_addr == rglast)
596 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
600 spin_unlock(&sdp->sd_rindex_spin);
606 * recent_rgrp_next - get next RG from "recent" list
607 * @cur_rgd: current rgrp
610 * Returns: The next rgrp in the recent list
613 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
616 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
617 struct list_head *head;
618 struct gfs2_rgrpd *rgd;
620 spin_lock(&sdp->sd_rindex_spin);
622 head = &sdp->sd_rindex_recent_list;
624 list_for_each_entry(rgd, head, rd_recent) {
625 if (rgd == cur_rgd) {
626 if (cur_rgd->rd_recent.next != head)
627 rgd = list_entry(cur_rgd->rd_recent.next,
628 struct gfs2_rgrpd, rd_recent);
633 list_del(&cur_rgd->rd_recent);
640 if (!list_empty(head))
641 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
644 spin_unlock(&sdp->sd_rindex_spin);
650 * recent_rgrp_add - add an RG to tail of "recent" list
651 * @new_rgd: The rgrp to add
655 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
657 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
658 struct gfs2_rgrpd *rgd;
659 unsigned int count = 0;
660 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
662 spin_lock(&sdp->sd_rindex_spin);
664 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
671 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
674 spin_unlock(&sdp->sd_rindex_spin);
678 * forward_rgrp_get - get an rgrp to try next from full list
679 * @sdp: The GFS2 superblock
681 * Returns: The rgrp to try next
684 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
686 struct gfs2_rgrpd *rgd;
687 unsigned int journals = gfs2_jindex_size(sdp);
688 unsigned int rg = 0, x;
690 spin_lock(&sdp->sd_rindex_spin);
692 rgd = sdp->sd_rindex_forward;
694 if (sdp->sd_rgrps >= journals)
695 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
697 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp);
699 x++, rgd = gfs2_rgrpd_get_next(rgd))
702 sdp->sd_rindex_forward = rgd;
705 spin_unlock(&sdp->sd_rindex_spin);
711 * forward_rgrp_set - set the forward rgrp pointer
712 * @sdp: the filesystem
713 * @rgd: The new forward rgrp
717 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
719 spin_lock(&sdp->sd_rindex_spin);
720 sdp->sd_rindex_forward = rgd;
721 spin_unlock(&sdp->sd_rindex_spin);
725 * get_local_rgrp - Choose and lock a rgrp for allocation
726 * @ip: the inode to reserve space for
727 * @rgp: the chosen and locked rgrp
729 * Try to acquire rgrp in way which avoids contending with others.
734 static int get_local_rgrp(struct gfs2_inode *ip)
736 struct gfs2_sbd *sdp = ip->i_sbd;
737 struct gfs2_rgrpd *rgd, *begin = NULL;
738 struct gfs2_alloc *al = &ip->i_alloc;
739 int flags = LM_FLAG_TRY;
744 /* Try recently successful rgrps */
746 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
749 error = gfs2_glock_nq_init(rgd->rd_gl,
750 LM_ST_EXCLUSIVE, LM_FLAG_TRY,
754 if (try_rgrp_fit(rgd, al))
756 gfs2_glock_dq_uninit(&al->al_rgd_gh);
757 rgd = recent_rgrp_next(rgd, 1);
761 rgd = recent_rgrp_next(rgd, 0);
769 /* Go through full list of rgrps */
771 begin = rgd = forward_rgrp_get(sdp);
774 error = gfs2_glock_nq_init(rgd->rd_gl,
775 LM_ST_EXCLUSIVE, flags,
779 if (try_rgrp_fit(rgd, al))
781 gfs2_glock_dq_uninit(&al->al_rgd_gh);
792 rgd = gfs2_rgrpd_get_next(rgd);
794 rgd = gfs2_rgrpd_get_first(sdp);
797 if (++loops >= 2 || !skipped)
804 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
807 recent_rgrp_add(rgd);
808 rgd = gfs2_rgrpd_get_next(rgd);
810 rgd = gfs2_rgrpd_get_first(sdp);
811 forward_rgrp_set(sdp, rgd);
818 * gfs2_inplace_reserve_i - Reserve space in the filesystem
819 * @ip: the inode to reserve space for
824 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
826 struct gfs2_sbd *sdp = ip->i_sbd;
827 struct gfs2_alloc *al = &ip->i_alloc;
830 if (gfs2_assert_warn(sdp, al->al_requested))
833 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
837 error = get_local_rgrp(ip);
839 gfs2_glock_dq_uninit(&al->al_ri_gh);
850 * gfs2_inplace_release - release an inplace reservation
851 * @ip: the inode the reservation was taken out on
853 * Release a reservation made by gfs2_inplace_reserve().
856 void gfs2_inplace_release(struct gfs2_inode *ip)
858 struct gfs2_sbd *sdp = ip->i_sbd;
859 struct gfs2_alloc *al = &ip->i_alloc;
861 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
862 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
863 "al_file = %s, al_line = %u\n",
864 al->al_alloced, al->al_requested, al->al_file,
868 gfs2_glock_dq_uninit(&al->al_rgd_gh);
869 gfs2_glock_dq_uninit(&al->al_ri_gh);
873 * gfs2_get_block_type - Check a block in a RG is of given type
874 * @rgd: the resource group holding the block
875 * @block: the block number
877 * Returns: The block type (GFS2_BLKST_*)
880 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, uint64_t block)
882 struct gfs2_bitmap *bi = NULL;
883 uint32_t length, rgrp_block, buf_block;
887 length = rgd->rd_ri.ri_length;
888 rgrp_block = block - rgd->rd_ri.ri_data0;
890 for (buf = 0; buf < length; buf++) {
891 bi = rgd->rd_bits + buf;
892 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
896 gfs2_assert(rgd->rd_sbd, buf < length);
897 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
899 type = gfs2_testbit(rgd,
900 bi->bi_bh->b_data + bi->bi_offset,
901 bi->bi_len, buf_block);
907 * rgblk_search - find a block in @old_state, change allocation
908 * state to @new_state
909 * @rgd: the resource group descriptor
910 * @goal: the goal block within the RG (start here to search for avail block)
911 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
912 * @new_state: GFS2_BLKST_XXX the after-allocation block state
914 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
915 * Add the found bitmap buffer to the transaction.
916 * Set the found bits to @new_state to change block's allocation state.
918 * This function never fails, because we wouldn't call it unless we
919 * know (from reservation results, etc.) that a block is available.
921 * Scope of @goal and returned block is just within rgrp, not the whole
924 * Returns: the block number allocated
927 static uint32_t rgblk_search(struct gfs2_rgrpd *rgd, uint32_t goal,
928 unsigned char old_state, unsigned char new_state)
930 struct gfs2_bitmap *bi = NULL;
931 uint32_t length = rgd->rd_ri.ri_length;
935 /* Find bitmap block that contains bits for goal block */
936 for (buf = 0; buf < length; buf++) {
937 bi = rgd->rd_bits + buf;
938 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
942 gfs2_assert(rgd->rd_sbd, buf < length);
944 /* Convert scope of "goal" from rgrp-wide to within found bit block */
945 goal -= bi->bi_start * GFS2_NBBY;
947 /* Search (up to entire) bitmap in this rgrp for allocatable block.
948 "x <= length", instead of "x < length", because we typically start
949 the search in the middle of a bit block, but if we can't find an
950 allocatable block anywhere else, we want to be able wrap around and
951 search in the first part of our first-searched bit block. */
952 for (x = 0; x <= length; x++) {
954 blk = gfs2_bitfit(rgd,
955 bi->bi_clone + bi->bi_offset,
956 bi->bi_len, goal, old_state);
958 blk = gfs2_bitfit(rgd,
959 bi->bi_bh->b_data + bi->bi_offset,
960 bi->bi_len, goal, old_state);
961 if (blk != BFITNOENT)
964 /* Try next bitmap block (wrap back to rgrp header if at end) */
965 buf = (buf + 1) % length;
966 bi = rgd->rd_bits + buf;
970 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
973 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
975 bi->bi_bh->b_data + bi->bi_offset,
976 bi->bi_len, blk, new_state);
979 bi->bi_clone + bi->bi_offset,
980 bi->bi_len, blk, new_state);
982 return bi->bi_start * GFS2_NBBY + blk;
986 * rgblk_free - Change alloc state of given block(s)
987 * @sdp: the filesystem
988 * @bstart: the start of a run of blocks to free
989 * @blen: the length of the block run (all must lie within ONE RG!)
990 * @new_state: GFS2_BLKST_XXX the after-allocation block state
992 * Returns: Resource group containing the block(s)
995 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
996 uint32_t blen, unsigned char new_state)
998 struct gfs2_rgrpd *rgd;
999 struct gfs2_bitmap *bi = NULL;
1000 uint32_t length, rgrp_blk, buf_blk;
1003 rgd = gfs2_blk2rgrpd(sdp, bstart);
1005 if (gfs2_consist(sdp))
1006 fs_err(sdp, "block = %llu\n", bstart);
1010 length = rgd->rd_ri.ri_length;
1012 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1015 for (buf = 0; buf < length; buf++) {
1016 bi = rgd->rd_bits + buf;
1017 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1021 gfs2_assert(rgd->rd_sbd, buf < length);
1023 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1026 if (!bi->bi_clone) {
1027 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1028 GFP_KERNEL | __GFP_NOFAIL);
1029 memcpy(bi->bi_clone + bi->bi_offset,
1030 bi->bi_bh->b_data + bi->bi_offset,
1033 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1035 bi->bi_bh->b_data + bi->bi_offset,
1036 bi->bi_len, buf_blk, new_state);
1043 * gfs2_alloc_data - Allocate a data block
1044 * @ip: the inode to allocate the data block for
1046 * Returns: the allocated block
1049 uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
1051 struct gfs2_sbd *sdp = ip->i_sbd;
1052 struct gfs2_alloc *al = &ip->i_alloc;
1053 struct gfs2_rgrpd *rgd = al->al_rgd;
1057 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1058 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1060 goal = rgd->rd_last_alloc_data;
1062 blk = rgblk_search(rgd, goal,
1063 GFS2_BLKST_FREE, GFS2_BLKST_USED);
1064 rgd->rd_last_alloc_data = blk;
1066 block = rgd->rd_ri.ri_data0 + blk;
1067 ip->i_di.di_goal_data = block;
1069 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1070 rgd->rd_rg.rg_free--;
1072 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1073 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1077 gfs2_statfs_change(sdp, 0, -1, 0);
1078 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1080 spin_lock(&sdp->sd_rindex_spin);
1081 rgd->rd_free_clone--;
1082 spin_unlock(&sdp->sd_rindex_spin);
1088 * gfs2_alloc_meta - Allocate a metadata block
1089 * @ip: the inode to allocate the metadata block for
1091 * Returns: the allocated block
1094 uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
1096 struct gfs2_sbd *sdp = ip->i_sbd;
1097 struct gfs2_alloc *al = &ip->i_alloc;
1098 struct gfs2_rgrpd *rgd = al->al_rgd;
1102 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1103 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1105 goal = rgd->rd_last_alloc_meta;
1107 blk = rgblk_search(rgd, goal,
1108 GFS2_BLKST_FREE, GFS2_BLKST_USED);
1109 rgd->rd_last_alloc_meta = blk;
1111 block = rgd->rd_ri.ri_data0 + blk;
1112 ip->i_di.di_goal_meta = block;
1114 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1115 rgd->rd_rg.rg_free--;
1117 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1118 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1122 gfs2_statfs_change(sdp, 0, -1, 0);
1123 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1124 gfs2_trans_add_unrevoke(sdp, block);
1126 spin_lock(&sdp->sd_rindex_spin);
1127 rgd->rd_free_clone--;
1128 spin_unlock(&sdp->sd_rindex_spin);
1134 * gfs2_alloc_di - Allocate a dinode
1135 * @dip: the directory that the inode is going in
1137 * Returns: the block allocated
1140 uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
1142 struct gfs2_sbd *sdp = dip->i_sbd;
1143 struct gfs2_alloc *al = &dip->i_alloc;
1144 struct gfs2_rgrpd *rgd = al->al_rgd;
1148 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1149 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1151 rgd->rd_last_alloc_meta = blk;
1153 block = rgd->rd_ri.ri_data0 + blk;
1155 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1156 rgd->rd_rg.rg_free--;
1157 rgd->rd_rg.rg_dinodes++;
1159 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1160 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1164 gfs2_statfs_change(sdp, 0, -1, +1);
1165 gfs2_trans_add_unrevoke(sdp, block);
1167 spin_lock(&sdp->sd_rindex_spin);
1168 rgd->rd_free_clone--;
1169 spin_unlock(&sdp->sd_rindex_spin);
1175 * gfs2_free_data - free a contiguous run of data block(s)
1176 * @ip: the inode these blocks are being freed from
1177 * @bstart: first block of a run of contiguous blocks
1178 * @blen: the length of the block run
1182 void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1184 struct gfs2_sbd *sdp = ip->i_sbd;
1185 struct gfs2_rgrpd *rgd;
1187 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1191 rgd->rd_rg.rg_free += blen;
1193 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1194 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1196 gfs2_trans_add_rg(rgd);
1198 gfs2_statfs_change(sdp, 0, +blen, 0);
1199 gfs2_quota_change(ip, -(int64_t)blen,
1200 ip->i_di.di_uid, ip->i_di.di_gid);
1204 * gfs2_free_meta - free a contiguous run of data block(s)
1205 * @ip: the inode these blocks are being freed from
1206 * @bstart: first block of a run of contiguous blocks
1207 * @blen: the length of the block run
1211 void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1213 struct gfs2_sbd *sdp = ip->i_sbd;
1214 struct gfs2_rgrpd *rgd;
1216 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1220 rgd->rd_rg.rg_free += blen;
1222 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1223 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1225 gfs2_trans_add_rg(rgd);
1227 gfs2_statfs_change(sdp, 0, +blen, 0);
1228 gfs2_quota_change(ip, -(int64_t)blen,
1229 ip->i_di.di_uid, ip->i_di.di_gid);
1230 gfs2_meta_wipe(ip, bstart, blen);
1233 void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
1235 struct gfs2_sbd *sdp = rgd->rd_sbd;
1236 struct gfs2_rgrpd *tmp_rgd;
1238 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1241 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1243 if (!rgd->rd_rg.rg_dinodes)
1244 gfs2_consist_rgrpd(rgd);
1245 rgd->rd_rg.rg_dinodes--;
1246 rgd->rd_rg.rg_free++;
1248 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1249 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1251 gfs2_statfs_change(sdp, 0, +1, -1);
1252 gfs2_trans_add_rg(rgd);
1256 * gfs2_free_uninit_di - free a dinode block
1257 * @rgd: the resource group that contains the dinode
1262 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1264 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
1265 gfs2_quota_change(ip, -1, ip->i_di.di_uid, ip->i_di.di_gid);
1266 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1270 * gfs2_rlist_add - add a RG to a list of RGs
1271 * @sdp: the filesystem
1272 * @rlist: the list of resource groups
1275 * Figure out what RG a block belongs to and add that RG to the list
1277 * FIXME: Don't use NOFAIL
1281 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1284 struct gfs2_rgrpd *rgd;
1285 struct gfs2_rgrpd **tmp;
1286 unsigned int new_space;
1289 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1292 rgd = gfs2_blk2rgrpd(sdp, block);
1294 if (gfs2_consist(sdp))
1295 fs_err(sdp, "block = %llu\n", block);
1299 for (x = 0; x < rlist->rl_rgrps; x++)
1300 if (rlist->rl_rgd[x] == rgd)
1303 if (rlist->rl_rgrps == rlist->rl_space) {
1304 new_space = rlist->rl_space + 10;
1306 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1307 GFP_KERNEL | __GFP_NOFAIL);
1309 if (rlist->rl_rgd) {
1310 memcpy(tmp, rlist->rl_rgd,
1311 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1312 kfree(rlist->rl_rgd);
1315 rlist->rl_space = new_space;
1316 rlist->rl_rgd = tmp;
1319 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1323 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1324 * and initialize an array of glock holders for them
1325 * @rlist: the list of resource groups
1326 * @state: the lock state to acquire the RG lock in
1327 * @flags: the modifier flags for the holder structures
1329 * FIXME: Don't use NOFAIL
1333 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1338 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1339 GFP_KERNEL | __GFP_NOFAIL);
1340 for (x = 0; x < rlist->rl_rgrps; x++)
1341 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1347 * gfs2_rlist_free - free a resource group list
1348 * @list: the list of resource groups
1352 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1356 kfree(rlist->rl_rgd);
1358 if (rlist->rl_ghs) {
1359 for (x = 0; x < rlist->rl_rgrps; x++)
1360 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1361 kfree(rlist->rl_ghs);