2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 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 version 2.
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/lm_interface.h>
32 #define BFITNOENT ((u32)~0)
35 * These routines are used by the resource group routines (rgrp.c)
36 * to keep track of block allocation. Each block is represented by two
37 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
40 * 1 = Used (not metadata)
41 * 2 = Unlinked (still in use) inode
45 static const char valid_change[16] = {
54 * gfs2_setbit - Set a bit in the bitmaps
55 * @buffer: the buffer that holds the bitmaps
56 * @buflen: the length (in bytes) of the buffer
57 * @block: the block to set
58 * @new_state: the new state of the block
62 static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
63 unsigned int buflen, u32 block,
64 unsigned char new_state)
66 unsigned char *byte, *end, cur_state;
69 byte = buffer + (block / GFS2_NBBY);
70 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
71 end = buffer + buflen;
73 gfs2_assert(rgd->rd_sbd, byte < end);
75 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
77 if (valid_change[new_state * 4 + cur_state]) {
78 *byte ^= cur_state << bit;
79 *byte |= new_state << bit;
81 gfs2_consist_rgrpd(rgd);
85 * gfs2_testbit - test a bit in the bitmaps
86 * @buffer: the buffer that holds the bitmaps
87 * @buflen: the length (in bytes) of the buffer
88 * @block: the block to read
92 static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
93 unsigned int buflen, u32 block)
95 unsigned char *byte, *end, cur_state;
98 byte = buffer + (block / GFS2_NBBY);
99 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
100 end = buffer + buflen;
102 gfs2_assert(rgd->rd_sbd, byte < end);
104 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
110 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
111 * a block in a given allocation state.
112 * @buffer: the buffer that holds the bitmaps
113 * @buflen: the length (in bytes) of the buffer
114 * @goal: start search at this block's bit-pair (within @buffer)
115 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
116 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
118 * Scope of @goal and returned block number is only within this bitmap buffer,
119 * not entire rgrp or filesystem. @buffer will be offset from the actual
120 * beginning of a bitmap block buffer, skipping any header structures.
122 * Return: the block number (bitmap buffer scope) that was found
125 static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
126 unsigned int buflen, u32 goal,
127 unsigned char old_state)
129 unsigned char *byte, *end, alloc;
133 byte = buffer + (goal / GFS2_NBBY);
134 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
135 end = buffer + buflen;
136 alloc = (old_state & 1) ? 0 : 0x55;
139 if ((*byte & 0x55) == alloc) {
140 blk += (8 - bit) >> 1;
148 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
151 bit += GFS2_BIT_SIZE;
164 * gfs2_bitcount - count the number of bits in a certain state
165 * @buffer: the buffer that holds the bitmaps
166 * @buflen: the length (in bytes) of the buffer
167 * @state: the state of the block we're looking for
169 * Returns: The number of bits
172 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
173 unsigned int buflen, unsigned char state)
175 unsigned char *byte = buffer;
176 unsigned char *end = buffer + buflen;
177 unsigned char state1 = state << 2;
178 unsigned char state2 = state << 4;
179 unsigned char state3 = state << 6;
182 for (; byte < end; byte++) {
183 if (((*byte) & 0x03) == state)
185 if (((*byte) & 0x0C) == state1)
187 if (((*byte) & 0x30) == state2)
189 if (((*byte) & 0xC0) == state3)
197 * gfs2_rgrp_verify - Verify that a resource group is consistent
198 * @sdp: the filesystem
203 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
205 struct gfs2_sbd *sdp = rgd->rd_sbd;
206 struct gfs2_bitmap *bi = NULL;
207 u32 length = rgd->rd_ri.ri_length;
211 memset(count, 0, 4 * sizeof(u32));
213 /* Count # blocks in each of 4 possible allocation states */
214 for (buf = 0; buf < length; buf++) {
215 bi = rgd->rd_bits + buf;
216 for (x = 0; x < 4; x++)
217 count[x] += gfs2_bitcount(rgd,
223 if (count[0] != rgd->rd_rg.rg_free) {
224 if (gfs2_consist_rgrpd(rgd))
225 fs_err(sdp, "free data mismatch: %u != %u\n",
226 count[0], rgd->rd_rg.rg_free);
230 tmp = rgd->rd_ri.ri_data -
232 rgd->rd_rg.rg_dinodes;
233 if (count[1] + count[2] != tmp) {
234 if (gfs2_consist_rgrpd(rgd))
235 fs_err(sdp, "used data mismatch: %u != %u\n",
240 if (count[3] != rgd->rd_rg.rg_dinodes) {
241 if (gfs2_consist_rgrpd(rgd))
242 fs_err(sdp, "used metadata mismatch: %u != %u\n",
243 count[3], rgd->rd_rg.rg_dinodes);
247 if (count[2] > count[3]) {
248 if (gfs2_consist_rgrpd(rgd))
249 fs_err(sdp, "unlinked inodes > inodes: %u\n",
256 static inline int rgrp_contains_block(struct gfs2_rindex_host *ri, u64 block)
258 u64 first = ri->ri_data0;
259 u64 last = first + ri->ri_data;
260 return first <= block && block < last;
264 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
265 * @sdp: The GFS2 superblock
266 * @n: The data block number
268 * Returns: The resource group, or NULL if not found
271 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
273 struct gfs2_rgrpd *rgd;
275 spin_lock(&sdp->sd_rindex_spin);
277 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
278 if (rgrp_contains_block(&rgd->rd_ri, blk)) {
279 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
280 spin_unlock(&sdp->sd_rindex_spin);
285 spin_unlock(&sdp->sd_rindex_spin);
291 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
292 * @sdp: The GFS2 superblock
294 * Returns: The first rgrp in the filesystem
297 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
299 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
300 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
304 * gfs2_rgrpd_get_next - get the next RG
307 * Returns: The next rgrp
310 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
312 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
314 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
317 static void clear_rgrpdi(struct gfs2_sbd *sdp)
319 struct list_head *head;
320 struct gfs2_rgrpd *rgd;
321 struct gfs2_glock *gl;
323 spin_lock(&sdp->sd_rindex_spin);
324 sdp->sd_rindex_forward = NULL;
325 head = &sdp->sd_rindex_recent_list;
326 while (!list_empty(head)) {
327 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
328 list_del(&rgd->rd_recent);
330 spin_unlock(&sdp->sd_rindex_spin);
332 head = &sdp->sd_rindex_list;
333 while (!list_empty(head)) {
334 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
337 list_del(&rgd->rd_list);
338 list_del(&rgd->rd_list_mru);
341 gl->gl_object = NULL;
350 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
352 mutex_lock(&sdp->sd_rindex_mutex);
354 mutex_unlock(&sdp->sd_rindex_mutex);
358 * gfs2_compute_bitstructs - Compute the bitmap sizes
359 * @rgd: The resource group descriptor
361 * Calculates bitmap descriptors, one for each block that contains bitmap data
366 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
368 struct gfs2_sbd *sdp = rgd->rd_sbd;
369 struct gfs2_bitmap *bi;
370 u32 length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
371 u32 bytes_left, bytes;
377 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
381 bytes_left = rgd->rd_ri.ri_bitbytes;
383 for (x = 0; x < length; x++) {
384 bi = rgd->rd_bits + x;
386 /* small rgrp; bitmap stored completely in header block */
389 bi->bi_offset = sizeof(struct gfs2_rgrp);
394 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
395 bi->bi_offset = sizeof(struct gfs2_rgrp);
399 } else if (x + 1 == length) {
401 bi->bi_offset = sizeof(struct gfs2_meta_header);
402 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
406 bytes = sdp->sd_sb.sb_bsize -
407 sizeof(struct gfs2_meta_header);
408 bi->bi_offset = sizeof(struct gfs2_meta_header);
409 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
417 gfs2_consist_rgrpd(rgd);
420 bi = rgd->rd_bits + (length - 1);
421 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
422 if (gfs2_consist_rgrpd(rgd)) {
423 gfs2_rindex_print(&rgd->rd_ri);
424 fs_err(sdp, "start=%u len=%u offset=%u\n",
425 bi->bi_start, bi->bi_len, bi->bi_offset);
434 * gfs2_ri_total - Total up the file system space, according to the rindex.
437 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
440 struct inode *inode = sdp->sd_rindex;
441 struct gfs2_inode *ip = GFS2_I(inode);
442 struct gfs2_rindex_host ri;
443 char buf[sizeof(struct gfs2_rindex)];
444 struct file_ra_state ra_state;
447 mutex_lock(&sdp->sd_rindex_mutex);
448 file_ra_state_init(&ra_state, inode->i_mapping);
449 for (rgrps = 0;; rgrps++) {
450 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
452 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
454 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
455 sizeof(struct gfs2_rindex));
456 if (error != sizeof(struct gfs2_rindex))
458 gfs2_rindex_in(&ri, buf);
459 total_data += ri.ri_data;
461 mutex_unlock(&sdp->sd_rindex_mutex);
466 * gfs2_ri_update - Pull in a new resource index from the disk
467 * @gl: The glock covering the rindex inode
469 * Returns: 0 on successful update, error code otherwise
472 static int gfs2_ri_update(struct gfs2_inode *ip)
474 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
475 struct inode *inode = &ip->i_inode;
476 struct gfs2_rgrpd *rgd;
477 char buf[sizeof(struct gfs2_rindex)];
478 struct file_ra_state ra_state;
479 u64 junk = ip->i_di.di_size;
482 /* If someone is holding the rindex file with a glock, they must
483 be updating it, in which case we may have partial entries.
484 In this case, we ignore the partials. */
485 if (!gfs2_glock_is_held_excl(ip->i_gl) &&
486 !gfs2_glock_is_held_shrd(ip->i_gl) &&
487 do_div(junk, sizeof(struct gfs2_rindex))) {
488 gfs2_consist_inode(ip);
494 file_ra_state_init(&ra_state, inode->i_mapping);
495 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
496 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
498 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
500 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
501 sizeof(struct gfs2_rindex));
504 if (error != sizeof(struct gfs2_rindex)) {
510 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
515 mutex_init(&rgd->rd_mutex);
516 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
519 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
520 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
522 gfs2_rindex_in(&rgd->rd_ri, buf);
523 error = compute_bitstructs(rgd);
527 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
528 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
532 rgd->rd_gl->gl_object = rgd;
533 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
536 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
545 * gfs2_rindex_hold - Grab a lock on the rindex
546 * @sdp: The GFS2 superblock
547 * @ri_gh: the glock holder
549 * We grab a lock on the rindex inode to make sure that it doesn't
550 * change whilst we are performing an operation. We keep this lock
551 * for quite long periods of time compared to other locks. This
552 * doesn't matter, since it is shared and it is very, very rarely
553 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
555 * This makes sure that we're using the latest copy of the resource index
556 * special file, which might have been updated if someone expanded the
557 * filesystem (via gfs2_grow utility), which adds new resource groups.
559 * Returns: 0 on success, error code otherwise
562 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
564 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
565 struct gfs2_glock *gl = ip->i_gl;
568 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
572 /* Read new copy from disk if we don't have the latest */
573 if (sdp->sd_rindex_vn != gl->gl_vn) {
574 mutex_lock(&sdp->sd_rindex_mutex);
575 if (sdp->sd_rindex_vn != gl->gl_vn) {
576 error = gfs2_ri_update(ip);
578 gfs2_glock_dq_uninit(ri_gh);
580 mutex_unlock(&sdp->sd_rindex_mutex);
587 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
588 * @rgd: the struct gfs2_rgrpd describing the RG to read in
590 * Read in all of a Resource Group's header and bitmap blocks.
591 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
596 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
598 struct gfs2_sbd *sdp = rgd->rd_sbd;
599 struct gfs2_glock *gl = rgd->rd_gl;
600 unsigned int length = rgd->rd_ri.ri_length;
601 struct gfs2_bitmap *bi;
605 mutex_lock(&rgd->rd_mutex);
607 spin_lock(&sdp->sd_rindex_spin);
608 if (rgd->rd_bh_count) {
610 spin_unlock(&sdp->sd_rindex_spin);
611 mutex_unlock(&rgd->rd_mutex);
614 spin_unlock(&sdp->sd_rindex_spin);
616 for (x = 0; x < length; x++) {
617 bi = rgd->rd_bits + x;
618 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, 0, &bi->bi_bh);
623 for (y = length; y--;) {
624 bi = rgd->rd_bits + y;
625 error = gfs2_meta_wait(sdp, bi->bi_bh);
628 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
635 if (rgd->rd_rg_vn != gl->gl_vn) {
636 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
637 rgd->rd_rg_vn = gl->gl_vn;
640 spin_lock(&sdp->sd_rindex_spin);
641 rgd->rd_free_clone = rgd->rd_rg.rg_free;
643 spin_unlock(&sdp->sd_rindex_spin);
645 mutex_unlock(&rgd->rd_mutex);
651 bi = rgd->rd_bits + x;
654 gfs2_assert_warn(sdp, !bi->bi_clone);
656 mutex_unlock(&rgd->rd_mutex);
661 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
663 struct gfs2_sbd *sdp = rgd->rd_sbd;
665 spin_lock(&sdp->sd_rindex_spin);
666 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
668 spin_unlock(&sdp->sd_rindex_spin);
672 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
673 * @rgd: the struct gfs2_rgrpd describing the RG to read in
677 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
679 struct gfs2_sbd *sdp = rgd->rd_sbd;
680 int x, length = rgd->rd_ri.ri_length;
682 spin_lock(&sdp->sd_rindex_spin);
683 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
684 if (--rgd->rd_bh_count) {
685 spin_unlock(&sdp->sd_rindex_spin);
689 for (x = 0; x < length; x++) {
690 struct gfs2_bitmap *bi = rgd->rd_bits + x;
697 spin_unlock(&sdp->sd_rindex_spin);
700 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
702 struct gfs2_sbd *sdp = rgd->rd_sbd;
703 unsigned int length = rgd->rd_ri.ri_length;
706 for (x = 0; x < length; x++) {
707 struct gfs2_bitmap *bi = rgd->rd_bits + x;
710 memcpy(bi->bi_clone + bi->bi_offset,
711 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
714 spin_lock(&sdp->sd_rindex_spin);
715 rgd->rd_free_clone = rgd->rd_rg.rg_free;
716 spin_unlock(&sdp->sd_rindex_spin);
720 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
721 * @ip: the incore GFS2 inode structure
723 * Returns: the struct gfs2_alloc
726 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
728 struct gfs2_alloc *al = &ip->i_alloc;
730 /* FIXME: Should assert that the correct locks are held here... */
731 memset(al, 0, sizeof(*al));
736 * try_rgrp_fit - See if a given reservation will fit in a given RG
738 * @al: the struct gfs2_alloc structure describing the reservation
740 * If there's room for the requested blocks to be allocated from the RG:
741 * Sets the $al_rgd field in @al.
743 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
746 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
748 struct gfs2_sbd *sdp = rgd->rd_sbd;
751 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
754 spin_lock(&sdp->sd_rindex_spin);
755 if (rgd->rd_free_clone >= al->al_requested) {
759 spin_unlock(&sdp->sd_rindex_spin);
765 * recent_rgrp_first - get first RG from "recent" list
766 * @sdp: The GFS2 superblock
767 * @rglast: address of the rgrp used last
769 * Returns: The first rgrp in the recent list
772 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
775 struct gfs2_rgrpd *rgd = NULL;
777 spin_lock(&sdp->sd_rindex_spin);
779 if (list_empty(&sdp->sd_rindex_recent_list))
785 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
786 if (rgd->rd_ri.ri_addr == rglast)
791 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
794 spin_unlock(&sdp->sd_rindex_spin);
799 * recent_rgrp_next - get next RG from "recent" list
800 * @cur_rgd: current rgrp
803 * Returns: The next rgrp in the recent list
806 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
809 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
810 struct list_head *head;
811 struct gfs2_rgrpd *rgd;
813 spin_lock(&sdp->sd_rindex_spin);
815 head = &sdp->sd_rindex_recent_list;
817 list_for_each_entry(rgd, head, rd_recent) {
818 if (rgd == cur_rgd) {
819 if (cur_rgd->rd_recent.next != head)
820 rgd = list_entry(cur_rgd->rd_recent.next,
821 struct gfs2_rgrpd, rd_recent);
826 list_del(&cur_rgd->rd_recent);
833 if (!list_empty(head))
834 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
837 spin_unlock(&sdp->sd_rindex_spin);
842 * recent_rgrp_add - add an RG to tail of "recent" list
843 * @new_rgd: The rgrp to add
847 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
849 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
850 struct gfs2_rgrpd *rgd;
851 unsigned int count = 0;
852 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
854 spin_lock(&sdp->sd_rindex_spin);
856 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
863 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
866 spin_unlock(&sdp->sd_rindex_spin);
870 * forward_rgrp_get - get an rgrp to try next from full list
871 * @sdp: The GFS2 superblock
873 * Returns: The rgrp to try next
876 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
878 struct gfs2_rgrpd *rgd;
879 unsigned int journals = gfs2_jindex_size(sdp);
880 unsigned int rg = 0, x;
882 spin_lock(&sdp->sd_rindex_spin);
884 rgd = sdp->sd_rindex_forward;
886 if (sdp->sd_rgrps >= journals)
887 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
889 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
890 x++, rgd = gfs2_rgrpd_get_next(rgd))
893 sdp->sd_rindex_forward = rgd;
896 spin_unlock(&sdp->sd_rindex_spin);
902 * forward_rgrp_set - set the forward rgrp pointer
903 * @sdp: the filesystem
904 * @rgd: The new forward rgrp
908 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
910 spin_lock(&sdp->sd_rindex_spin);
911 sdp->sd_rindex_forward = rgd;
912 spin_unlock(&sdp->sd_rindex_spin);
916 * get_local_rgrp - Choose and lock a rgrp for allocation
917 * @ip: the inode to reserve space for
918 * @rgp: the chosen and locked rgrp
920 * Try to acquire rgrp in way which avoids contending with others.
925 static int get_local_rgrp(struct gfs2_inode *ip)
927 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
928 struct gfs2_rgrpd *rgd, *begin = NULL;
929 struct gfs2_alloc *al = &ip->i_alloc;
930 int flags = LM_FLAG_TRY;
935 /* Try recently successful rgrps */
937 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
940 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
941 LM_FLAG_TRY, &al->al_rgd_gh);
944 if (try_rgrp_fit(rgd, al))
946 gfs2_glock_dq_uninit(&al->al_rgd_gh);
947 rgd = recent_rgrp_next(rgd, 1);
951 rgd = recent_rgrp_next(rgd, 0);
959 /* Go through full list of rgrps */
961 begin = rgd = forward_rgrp_get(sdp);
964 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
968 if (try_rgrp_fit(rgd, al))
970 gfs2_glock_dq_uninit(&al->al_rgd_gh);
981 rgd = gfs2_rgrpd_get_next(rgd);
983 rgd = gfs2_rgrpd_get_first(sdp);
992 gfs2_log_flush(sdp, NULL);
997 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
1000 recent_rgrp_add(rgd);
1001 rgd = gfs2_rgrpd_get_next(rgd);
1003 rgd = gfs2_rgrpd_get_first(sdp);
1004 forward_rgrp_set(sdp, rgd);
1011 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1012 * @ip: the inode to reserve space for
1017 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1019 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1020 struct gfs2_alloc *al = &ip->i_alloc;
1023 if (gfs2_assert_warn(sdp, al->al_requested))
1026 /* We need to hold the rindex unless the inode we're using is
1027 the rindex itself, in which case it's already held. */
1028 if (ip != GFS2_I(sdp->sd_rindex))
1029 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1030 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1031 error = gfs2_ri_update(ip);
1036 error = get_local_rgrp(ip);
1038 if (ip != GFS2_I(sdp->sd_rindex))
1039 gfs2_glock_dq_uninit(&al->al_ri_gh);
1050 * gfs2_inplace_release - release an inplace reservation
1051 * @ip: the inode the reservation was taken out on
1053 * Release a reservation made by gfs2_inplace_reserve().
1056 void gfs2_inplace_release(struct gfs2_inode *ip)
1058 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1059 struct gfs2_alloc *al = &ip->i_alloc;
1061 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1062 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1063 "al_file = %s, al_line = %u\n",
1064 al->al_alloced, al->al_requested, al->al_file,
1068 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1069 if (ip != GFS2_I(sdp->sd_rindex))
1070 gfs2_glock_dq_uninit(&al->al_ri_gh);
1074 * gfs2_get_block_type - Check a block in a RG is of given type
1075 * @rgd: the resource group holding the block
1076 * @block: the block number
1078 * Returns: The block type (GFS2_BLKST_*)
1081 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1083 struct gfs2_bitmap *bi = NULL;
1084 u32 length, rgrp_block, buf_block;
1088 length = rgd->rd_ri.ri_length;
1089 rgrp_block = block - rgd->rd_ri.ri_data0;
1091 for (buf = 0; buf < length; buf++) {
1092 bi = rgd->rd_bits + buf;
1093 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1097 gfs2_assert(rgd->rd_sbd, buf < length);
1098 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1100 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1101 bi->bi_len, buf_block);
1107 * rgblk_search - find a block in @old_state, change allocation
1108 * state to @new_state
1109 * @rgd: the resource group descriptor
1110 * @goal: the goal block within the RG (start here to search for avail block)
1111 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1112 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1114 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1115 * Add the found bitmap buffer to the transaction.
1116 * Set the found bits to @new_state to change block's allocation state.
1118 * This function never fails, because we wouldn't call it unless we
1119 * know (from reservation results, etc.) that a block is available.
1121 * Scope of @goal and returned block is just within rgrp, not the whole
1124 * Returns: the block number allocated
1127 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1128 unsigned char old_state, unsigned char new_state)
1130 struct gfs2_bitmap *bi = NULL;
1131 u32 length = rgd->rd_ri.ri_length;
1133 unsigned int buf, x;
1135 /* Find bitmap block that contains bits for goal block */
1136 for (buf = 0; buf < length; buf++) {
1137 bi = rgd->rd_bits + buf;
1138 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1142 gfs2_assert(rgd->rd_sbd, buf < length);
1144 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1145 goal -= bi->bi_start * GFS2_NBBY;
1147 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1148 "x <= length", instead of "x < length", because we typically start
1149 the search in the middle of a bit block, but if we can't find an
1150 allocatable block anywhere else, we want to be able wrap around and
1151 search in the first part of our first-searched bit block. */
1152 for (x = 0; x <= length; x++) {
1154 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
1155 bi->bi_len, goal, old_state);
1157 blk = gfs2_bitfit(rgd,
1158 bi->bi_bh->b_data + bi->bi_offset,
1159 bi->bi_len, goal, old_state);
1160 if (blk != BFITNOENT)
1163 /* Try next bitmap block (wrap back to rgrp header if at end) */
1164 buf = (buf + 1) % length;
1165 bi = rgd->rd_bits + buf;
1169 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1172 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1173 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1174 bi->bi_len, blk, new_state);
1176 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1177 bi->bi_len, blk, new_state);
1179 return bi->bi_start * GFS2_NBBY + blk;
1183 * rgblk_free - Change alloc state of given block(s)
1184 * @sdp: the filesystem
1185 * @bstart: the start of a run of blocks to free
1186 * @blen: the length of the block run (all must lie within ONE RG!)
1187 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1189 * Returns: Resource group containing the block(s)
1192 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1193 u32 blen, unsigned char new_state)
1195 struct gfs2_rgrpd *rgd;
1196 struct gfs2_bitmap *bi = NULL;
1197 u32 length, rgrp_blk, buf_blk;
1200 rgd = gfs2_blk2rgrpd(sdp, bstart);
1202 if (gfs2_consist(sdp))
1203 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1207 length = rgd->rd_ri.ri_length;
1209 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1212 for (buf = 0; buf < length; buf++) {
1213 bi = rgd->rd_bits + buf;
1214 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1218 gfs2_assert(rgd->rd_sbd, buf < length);
1220 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1223 if (!bi->bi_clone) {
1224 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1225 GFP_NOFS | __GFP_NOFAIL);
1226 memcpy(bi->bi_clone + bi->bi_offset,
1227 bi->bi_bh->b_data + bi->bi_offset,
1230 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1231 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1232 bi->bi_len, buf_blk, new_state);
1239 * gfs2_alloc_data - Allocate a data block
1240 * @ip: the inode to allocate the data block for
1242 * Returns: the allocated block
1245 u64 gfs2_alloc_data(struct gfs2_inode *ip)
1247 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1248 struct gfs2_alloc *al = &ip->i_alloc;
1249 struct gfs2_rgrpd *rgd = al->al_rgd;
1253 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1254 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1256 goal = rgd->rd_last_alloc_data;
1258 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1259 rgd->rd_last_alloc_data = blk;
1261 block = rgd->rd_ri.ri_data0 + blk;
1262 ip->i_di.di_goal_data = block;
1264 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1265 rgd->rd_rg.rg_free--;
1267 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1268 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1272 gfs2_statfs_change(sdp, 0, -1, 0);
1273 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1275 spin_lock(&sdp->sd_rindex_spin);
1276 rgd->rd_free_clone--;
1277 spin_unlock(&sdp->sd_rindex_spin);
1283 * gfs2_alloc_meta - Allocate a metadata block
1284 * @ip: the inode to allocate the metadata block for
1286 * Returns: the allocated block
1289 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
1291 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1292 struct gfs2_alloc *al = &ip->i_alloc;
1293 struct gfs2_rgrpd *rgd = al->al_rgd;
1297 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1298 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1300 goal = rgd->rd_last_alloc_meta;
1302 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1303 rgd->rd_last_alloc_meta = blk;
1305 block = rgd->rd_ri.ri_data0 + blk;
1306 ip->i_di.di_goal_meta = block;
1308 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1309 rgd->rd_rg.rg_free--;
1311 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1312 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1316 gfs2_statfs_change(sdp, 0, -1, 0);
1317 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1318 gfs2_trans_add_unrevoke(sdp, block);
1320 spin_lock(&sdp->sd_rindex_spin);
1321 rgd->rd_free_clone--;
1322 spin_unlock(&sdp->sd_rindex_spin);
1328 * gfs2_alloc_di - Allocate a dinode
1329 * @dip: the directory that the inode is going in
1331 * Returns: the block allocated
1334 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1336 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1337 struct gfs2_alloc *al = &dip->i_alloc;
1338 struct gfs2_rgrpd *rgd = al->al_rgd;
1342 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1343 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1345 rgd->rd_last_alloc_meta = blk;
1347 block = rgd->rd_ri.ri_data0 + blk;
1349 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1350 rgd->rd_rg.rg_free--;
1351 rgd->rd_rg.rg_dinodes++;
1352 *generation = rgd->rd_rg.rg_igeneration++;
1353 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1354 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1358 gfs2_statfs_change(sdp, 0, -1, +1);
1359 gfs2_trans_add_unrevoke(sdp, block);
1361 spin_lock(&sdp->sd_rindex_spin);
1362 rgd->rd_free_clone--;
1363 spin_unlock(&sdp->sd_rindex_spin);
1369 * gfs2_free_data - free a contiguous run of data block(s)
1370 * @ip: the inode these blocks are being freed from
1371 * @bstart: first block of a run of contiguous blocks
1372 * @blen: the length of the block run
1376 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1378 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1379 struct gfs2_rgrpd *rgd;
1381 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1385 rgd->rd_rg.rg_free += blen;
1387 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1388 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1390 gfs2_trans_add_rg(rgd);
1392 gfs2_statfs_change(sdp, 0, +blen, 0);
1393 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1397 * gfs2_free_meta - free a contiguous run of data block(s)
1398 * @ip: the inode these blocks are being freed from
1399 * @bstart: first block of a run of contiguous blocks
1400 * @blen: the length of the block run
1404 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1406 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1407 struct gfs2_rgrpd *rgd;
1409 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1413 rgd->rd_rg.rg_free += blen;
1415 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1416 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1418 gfs2_trans_add_rg(rgd);
1420 gfs2_statfs_change(sdp, 0, +blen, 0);
1421 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1422 gfs2_meta_wipe(ip, bstart, blen);
1425 void gfs2_unlink_di(struct inode *inode)
1427 struct gfs2_inode *ip = GFS2_I(inode);
1428 struct gfs2_sbd *sdp = GFS2_SB(inode);
1429 struct gfs2_rgrpd *rgd;
1430 u64 blkno = ip->i_num.no_addr;
1432 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1435 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1436 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1437 gfs2_trans_add_rg(rgd);
1440 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1442 struct gfs2_sbd *sdp = rgd->rd_sbd;
1443 struct gfs2_rgrpd *tmp_rgd;
1445 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1448 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1450 if (!rgd->rd_rg.rg_dinodes)
1451 gfs2_consist_rgrpd(rgd);
1452 rgd->rd_rg.rg_dinodes--;
1453 rgd->rd_rg.rg_free++;
1455 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1456 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1458 gfs2_statfs_change(sdp, 0, +1, -1);
1459 gfs2_trans_add_rg(rgd);
1463 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1465 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
1466 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1467 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1471 * gfs2_rlist_add - add a RG to a list of RGs
1472 * @sdp: the filesystem
1473 * @rlist: the list of resource groups
1476 * Figure out what RG a block belongs to and add that RG to the list
1478 * FIXME: Don't use NOFAIL
1482 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1485 struct gfs2_rgrpd *rgd;
1486 struct gfs2_rgrpd **tmp;
1487 unsigned int new_space;
1490 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1493 rgd = gfs2_blk2rgrpd(sdp, block);
1495 if (gfs2_consist(sdp))
1496 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1500 for (x = 0; x < rlist->rl_rgrps; x++)
1501 if (rlist->rl_rgd[x] == rgd)
1504 if (rlist->rl_rgrps == rlist->rl_space) {
1505 new_space = rlist->rl_space + 10;
1507 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1508 GFP_NOFS | __GFP_NOFAIL);
1510 if (rlist->rl_rgd) {
1511 memcpy(tmp, rlist->rl_rgd,
1512 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1513 kfree(rlist->rl_rgd);
1516 rlist->rl_space = new_space;
1517 rlist->rl_rgd = tmp;
1520 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1524 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1525 * and initialize an array of glock holders for them
1526 * @rlist: the list of resource groups
1527 * @state: the lock state to acquire the RG lock in
1528 * @flags: the modifier flags for the holder structures
1530 * FIXME: Don't use NOFAIL
1534 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1539 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1540 GFP_NOFS | __GFP_NOFAIL);
1541 for (x = 0; x < rlist->rl_rgrps; x++)
1542 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1548 * gfs2_rlist_free - free a resource group list
1549 * @list: the list of resource groups
1553 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1557 kfree(rlist->rl_rgd);
1559 if (rlist->rl_ghs) {
1560 for (x = 0; x < rlist->rl_rgrps; x++)
1561 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1562 kfree(rlist->rl_ghs);