2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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 <linux/gfs2_ondisk.h>
19 #include "lm_interface.h"
32 #define BFITNOENT 0xFFFFFFFF
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, uint32_t 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, uint32_t 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 uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
126 unsigned int buflen, uint32_t 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 uint32_t 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 uint32_t length = rgd->rd_ri.ri_length;
208 uint32_t count[4], tmp;
211 memset(count, 0, 4 * sizeof(uint32_t));
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 *ri, uint64_t block)
258 uint64_t first = ri->ri_data0;
259 uint64_t 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, uint64_t 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 uint32_t length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
371 uint32_t 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_update - Pull in a new resource index from the disk
435 * @gl: The glock covering the rindex inode
437 * Returns: 0 on successful update, error code otherwise
440 static int gfs2_ri_update(struct gfs2_inode *ip)
442 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
443 struct inode *inode = &ip->i_inode;
444 struct gfs2_rgrpd *rgd;
445 char buf[sizeof(struct gfs2_rindex)];
446 struct file_ra_state ra_state;
447 uint64_t junk = ip->i_di.di_size;
450 if (do_div(junk, sizeof(struct gfs2_rindex))) {
451 gfs2_consist_inode(ip);
457 file_ra_state_init(&ra_state, inode->i_mapping);
458 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
459 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
460 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
461 sizeof(struct gfs2_rindex));
464 if (error != sizeof(struct gfs2_rindex)) {
470 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
475 mutex_init(&rgd->rd_mutex);
476 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
479 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
480 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
482 gfs2_rindex_in(&rgd->rd_ri, buf);
483 error = compute_bitstructs(rgd);
487 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
488 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
492 rgd->rd_gl->gl_object = rgd;
493 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
496 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
505 * gfs2_rindex_hold - Grab a lock on the rindex
506 * @sdp: The GFS2 superblock
507 * @ri_gh: the glock holder
509 * We grab a lock on the rindex inode to make sure that it doesn't
510 * change whilst we are performing an operation. We keep this lock
511 * for quite long periods of time compared to other locks. This
512 * doesn't matter, since it is shared and it is very, very rarely
513 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
515 * This makes sure that we're using the latest copy of the resource index
516 * special file, which might have been updated if someone expanded the
517 * filesystem (via gfs2_grow utility), which adds new resource groups.
519 * Returns: 0 on success, error code otherwise
522 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
524 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
525 struct gfs2_glock *gl = ip->i_gl;
528 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
532 /* Read new copy from disk if we don't have the latest */
533 if (sdp->sd_rindex_vn != gl->gl_vn) {
534 mutex_lock(&sdp->sd_rindex_mutex);
535 if (sdp->sd_rindex_vn != gl->gl_vn) {
536 error = gfs2_ri_update(ip);
538 gfs2_glock_dq_uninit(ri_gh);
540 mutex_unlock(&sdp->sd_rindex_mutex);
547 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
548 * @rgd: the struct gfs2_rgrpd describing the RG to read in
550 * Read in all of a Resource Group's header and bitmap blocks.
551 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
556 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
558 struct gfs2_sbd *sdp = rgd->rd_sbd;
559 struct gfs2_glock *gl = rgd->rd_gl;
560 unsigned int length = rgd->rd_ri.ri_length;
561 struct gfs2_bitmap *bi;
565 mutex_lock(&rgd->rd_mutex);
567 spin_lock(&sdp->sd_rindex_spin);
568 if (rgd->rd_bh_count) {
570 spin_unlock(&sdp->sd_rindex_spin);
571 mutex_unlock(&rgd->rd_mutex);
574 spin_unlock(&sdp->sd_rindex_spin);
576 for (x = 0; x < length; x++) {
577 bi = rgd->rd_bits + x;
578 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, DIO_START,
584 for (y = length; y--;) {
585 bi = rgd->rd_bits + y;
586 error = gfs2_meta_reread(sdp, bi->bi_bh, DIO_WAIT);
589 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
596 if (rgd->rd_rg_vn != gl->gl_vn) {
597 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
598 rgd->rd_rg_vn = gl->gl_vn;
601 spin_lock(&sdp->sd_rindex_spin);
602 rgd->rd_free_clone = rgd->rd_rg.rg_free;
604 spin_unlock(&sdp->sd_rindex_spin);
606 mutex_unlock(&rgd->rd_mutex);
612 bi = rgd->rd_bits + x;
615 gfs2_assert_warn(sdp, !bi->bi_clone);
617 mutex_unlock(&rgd->rd_mutex);
622 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
624 struct gfs2_sbd *sdp = rgd->rd_sbd;
626 spin_lock(&sdp->sd_rindex_spin);
627 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
629 spin_unlock(&sdp->sd_rindex_spin);
633 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
634 * @rgd: the struct gfs2_rgrpd describing the RG to read in
638 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
640 struct gfs2_sbd *sdp = rgd->rd_sbd;
641 int x, length = rgd->rd_ri.ri_length;
643 spin_lock(&sdp->sd_rindex_spin);
644 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
645 if (--rgd->rd_bh_count) {
646 spin_unlock(&sdp->sd_rindex_spin);
650 for (x = 0; x < length; x++) {
651 struct gfs2_bitmap *bi = rgd->rd_bits + x;
658 spin_unlock(&sdp->sd_rindex_spin);
661 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
663 struct gfs2_sbd *sdp = rgd->rd_sbd;
664 unsigned int length = rgd->rd_ri.ri_length;
667 for (x = 0; x < length; x++) {
668 struct gfs2_bitmap *bi = rgd->rd_bits + x;
671 memcpy(bi->bi_clone + bi->bi_offset,
672 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
675 spin_lock(&sdp->sd_rindex_spin);
676 rgd->rd_free_clone = rgd->rd_rg.rg_free;
677 spin_unlock(&sdp->sd_rindex_spin);
681 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
682 * @ip: the incore GFS2 inode structure
684 * Returns: the struct gfs2_alloc
687 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
689 struct gfs2_alloc *al = &ip->i_alloc;
691 /* FIXME: Should assert that the correct locks are held here... */
692 memset(al, 0, sizeof(*al));
697 * gfs2_alloc_put - throw away the struct gfs2_alloc for an inode
702 void gfs2_alloc_put(struct gfs2_inode *ip)
708 * try_rgrp_fit - See if a given reservation will fit in a given RG
710 * @al: the struct gfs2_alloc structure describing the reservation
712 * If there's room for the requested blocks to be allocated from the RG:
713 * Sets the $al_reserved_data field in @al.
714 * Sets the $al_reserved_meta field in @al.
715 * Sets the $al_rgd field in @al.
717 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
720 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
722 struct gfs2_sbd *sdp = rgd->rd_sbd;
725 spin_lock(&sdp->sd_rindex_spin);
726 if (rgd->rd_free_clone >= al->al_requested) {
730 spin_unlock(&sdp->sd_rindex_spin);
736 * recent_rgrp_first - get first RG from "recent" list
737 * @sdp: The GFS2 superblock
738 * @rglast: address of the rgrp used last
740 * Returns: The first rgrp in the recent list
743 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
746 struct gfs2_rgrpd *rgd = NULL;
748 spin_lock(&sdp->sd_rindex_spin);
750 if (list_empty(&sdp->sd_rindex_recent_list))
756 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
757 if (rgd->rd_ri.ri_addr == rglast)
762 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
765 spin_unlock(&sdp->sd_rindex_spin);
770 * recent_rgrp_next - get next RG from "recent" list
771 * @cur_rgd: current rgrp
774 * Returns: The next rgrp in the recent list
777 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
780 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
781 struct list_head *head;
782 struct gfs2_rgrpd *rgd;
784 spin_lock(&sdp->sd_rindex_spin);
786 head = &sdp->sd_rindex_recent_list;
788 list_for_each_entry(rgd, head, rd_recent) {
789 if (rgd == cur_rgd) {
790 if (cur_rgd->rd_recent.next != head)
791 rgd = list_entry(cur_rgd->rd_recent.next,
792 struct gfs2_rgrpd, rd_recent);
797 list_del(&cur_rgd->rd_recent);
804 if (!list_empty(head))
805 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
808 spin_unlock(&sdp->sd_rindex_spin);
813 * recent_rgrp_add - add an RG to tail of "recent" list
814 * @new_rgd: The rgrp to add
818 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
820 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
821 struct gfs2_rgrpd *rgd;
822 unsigned int count = 0;
823 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
825 spin_lock(&sdp->sd_rindex_spin);
827 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
834 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
837 spin_unlock(&sdp->sd_rindex_spin);
841 * forward_rgrp_get - get an rgrp to try next from full list
842 * @sdp: The GFS2 superblock
844 * Returns: The rgrp to try next
847 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
849 struct gfs2_rgrpd *rgd;
850 unsigned int journals = gfs2_jindex_size(sdp);
851 unsigned int rg = 0, x;
853 spin_lock(&sdp->sd_rindex_spin);
855 rgd = sdp->sd_rindex_forward;
857 if (sdp->sd_rgrps >= journals)
858 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
860 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
861 x++, rgd = gfs2_rgrpd_get_next(rgd))
864 sdp->sd_rindex_forward = rgd;
867 spin_unlock(&sdp->sd_rindex_spin);
873 * forward_rgrp_set - set the forward rgrp pointer
874 * @sdp: the filesystem
875 * @rgd: The new forward rgrp
879 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
881 spin_lock(&sdp->sd_rindex_spin);
882 sdp->sd_rindex_forward = rgd;
883 spin_unlock(&sdp->sd_rindex_spin);
887 * get_local_rgrp - Choose and lock a rgrp for allocation
888 * @ip: the inode to reserve space for
889 * @rgp: the chosen and locked rgrp
891 * Try to acquire rgrp in way which avoids contending with others.
896 static int get_local_rgrp(struct gfs2_inode *ip)
898 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
899 struct gfs2_rgrpd *rgd, *begin = NULL;
900 struct gfs2_alloc *al = &ip->i_alloc;
901 int flags = LM_FLAG_TRY;
906 /* Try recently successful rgrps */
908 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
911 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
912 LM_FLAG_TRY, &al->al_rgd_gh);
915 if (try_rgrp_fit(rgd, al))
917 gfs2_glock_dq_uninit(&al->al_rgd_gh);
918 rgd = recent_rgrp_next(rgd, 1);
922 rgd = recent_rgrp_next(rgd, 0);
930 /* Go through full list of rgrps */
932 begin = rgd = forward_rgrp_get(sdp);
935 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
939 if (try_rgrp_fit(rgd, al))
941 gfs2_glock_dq_uninit(&al->al_rgd_gh);
952 rgd = gfs2_rgrpd_get_next(rgd);
954 rgd = gfs2_rgrpd_get_first(sdp);
957 if (++loops >= 2 || !skipped)
964 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
967 recent_rgrp_add(rgd);
968 rgd = gfs2_rgrpd_get_next(rgd);
970 rgd = gfs2_rgrpd_get_first(sdp);
971 forward_rgrp_set(sdp, rgd);
978 * gfs2_inplace_reserve_i - Reserve space in the filesystem
979 * @ip: the inode to reserve space for
984 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
986 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
987 struct gfs2_alloc *al = &ip->i_alloc;
990 if (gfs2_assert_warn(sdp, al->al_requested))
993 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
997 error = get_local_rgrp(ip);
999 gfs2_glock_dq_uninit(&al->al_ri_gh);
1010 * gfs2_inplace_release - release an inplace reservation
1011 * @ip: the inode the reservation was taken out on
1013 * Release a reservation made by gfs2_inplace_reserve().
1016 void gfs2_inplace_release(struct gfs2_inode *ip)
1018 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1019 struct gfs2_alloc *al = &ip->i_alloc;
1021 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1022 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1023 "al_file = %s, al_line = %u\n",
1024 al->al_alloced, al->al_requested, al->al_file,
1028 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1029 gfs2_glock_dq_uninit(&al->al_ri_gh);
1033 * gfs2_get_block_type - Check a block in a RG is of given type
1034 * @rgd: the resource group holding the block
1035 * @block: the block number
1037 * Returns: The block type (GFS2_BLKST_*)
1040 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, uint64_t block)
1042 struct gfs2_bitmap *bi = NULL;
1043 uint32_t length, rgrp_block, buf_block;
1047 length = rgd->rd_ri.ri_length;
1048 rgrp_block = block - rgd->rd_ri.ri_data0;
1050 for (buf = 0; buf < length; buf++) {
1051 bi = rgd->rd_bits + buf;
1052 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1056 gfs2_assert(rgd->rd_sbd, buf < length);
1057 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1059 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1060 bi->bi_len, buf_block);
1066 * rgblk_search - find a block in @old_state, change allocation
1067 * state to @new_state
1068 * @rgd: the resource group descriptor
1069 * @goal: the goal block within the RG (start here to search for avail block)
1070 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1071 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1073 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1074 * Add the found bitmap buffer to the transaction.
1075 * Set the found bits to @new_state to change block's allocation state.
1077 * This function never fails, because we wouldn't call it unless we
1078 * know (from reservation results, etc.) that a block is available.
1080 * Scope of @goal and returned block is just within rgrp, not the whole
1083 * Returns: the block number allocated
1086 static uint32_t rgblk_search(struct gfs2_rgrpd *rgd, uint32_t goal,
1087 unsigned char old_state, unsigned char new_state)
1089 struct gfs2_bitmap *bi = NULL;
1090 uint32_t length = rgd->rd_ri.ri_length;
1092 unsigned int buf, x;
1094 /* Find bitmap block that contains bits for goal block */
1095 for (buf = 0; buf < length; buf++) {
1096 bi = rgd->rd_bits + buf;
1097 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1101 gfs2_assert(rgd->rd_sbd, buf < length);
1103 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1104 goal -= bi->bi_start * GFS2_NBBY;
1106 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1107 "x <= length", instead of "x < length", because we typically start
1108 the search in the middle of a bit block, but if we can't find an
1109 allocatable block anywhere else, we want to be able wrap around and
1110 search in the first part of our first-searched bit block. */
1111 for (x = 0; x <= length; x++) {
1113 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
1114 bi->bi_len, goal, old_state);
1116 blk = gfs2_bitfit(rgd,
1117 bi->bi_bh->b_data + bi->bi_offset,
1118 bi->bi_len, goal, old_state);
1119 if (blk != BFITNOENT)
1122 /* Try next bitmap block (wrap back to rgrp header if at end) */
1123 buf = (buf + 1) % length;
1124 bi = rgd->rd_bits + buf;
1128 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1131 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1132 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1133 bi->bi_len, blk, new_state);
1135 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1136 bi->bi_len, blk, new_state);
1138 return bi->bi_start * GFS2_NBBY + blk;
1142 * rgblk_free - Change alloc state of given block(s)
1143 * @sdp: the filesystem
1144 * @bstart: the start of a run of blocks to free
1145 * @blen: the length of the block run (all must lie within ONE RG!)
1146 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1148 * Returns: Resource group containing the block(s)
1151 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
1152 uint32_t blen, unsigned char new_state)
1154 struct gfs2_rgrpd *rgd;
1155 struct gfs2_bitmap *bi = NULL;
1156 uint32_t length, rgrp_blk, buf_blk;
1159 rgd = gfs2_blk2rgrpd(sdp, bstart);
1161 if (gfs2_consist(sdp))
1162 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1166 length = rgd->rd_ri.ri_length;
1168 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1171 for (buf = 0; buf < length; buf++) {
1172 bi = rgd->rd_bits + buf;
1173 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1177 gfs2_assert(rgd->rd_sbd, buf < length);
1179 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1182 if (!bi->bi_clone) {
1183 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1184 GFP_NOFS | __GFP_NOFAIL);
1185 memcpy(bi->bi_clone + bi->bi_offset,
1186 bi->bi_bh->b_data + bi->bi_offset,
1189 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1190 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1191 bi->bi_len, buf_blk, new_state);
1198 * gfs2_alloc_data - Allocate a data block
1199 * @ip: the inode to allocate the data block for
1201 * Returns: the allocated block
1204 u64 gfs2_alloc_data(struct gfs2_inode *ip)
1206 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1207 struct gfs2_alloc *al = &ip->i_alloc;
1208 struct gfs2_rgrpd *rgd = al->al_rgd;
1212 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1213 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1215 goal = rgd->rd_last_alloc_data;
1217 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1218 rgd->rd_last_alloc_data = blk;
1220 block = rgd->rd_ri.ri_data0 + blk;
1221 ip->i_di.di_goal_data = block;
1223 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1224 rgd->rd_rg.rg_free--;
1226 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1227 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1231 gfs2_statfs_change(sdp, 0, -1, 0);
1232 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1234 spin_lock(&sdp->sd_rindex_spin);
1235 rgd->rd_free_clone--;
1236 spin_unlock(&sdp->sd_rindex_spin);
1242 * gfs2_alloc_meta - Allocate a metadata block
1243 * @ip: the inode to allocate the metadata block for
1245 * Returns: the allocated block
1248 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
1250 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1251 struct gfs2_alloc *al = &ip->i_alloc;
1252 struct gfs2_rgrpd *rgd = al->al_rgd;
1256 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1257 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1259 goal = rgd->rd_last_alloc_meta;
1261 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1262 rgd->rd_last_alloc_meta = blk;
1264 block = rgd->rd_ri.ri_data0 + blk;
1265 ip->i_di.di_goal_meta = block;
1267 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1268 rgd->rd_rg.rg_free--;
1270 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1271 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1275 gfs2_statfs_change(sdp, 0, -1, 0);
1276 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1277 gfs2_trans_add_unrevoke(sdp, block);
1279 spin_lock(&sdp->sd_rindex_spin);
1280 rgd->rd_free_clone--;
1281 spin_unlock(&sdp->sd_rindex_spin);
1287 * gfs2_alloc_di - Allocate a dinode
1288 * @dip: the directory that the inode is going in
1290 * Returns: the block allocated
1293 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1295 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1296 struct gfs2_alloc *al = &dip->i_alloc;
1297 struct gfs2_rgrpd *rgd = al->al_rgd;
1301 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1302 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1304 rgd->rd_last_alloc_meta = blk;
1306 block = rgd->rd_ri.ri_data0 + blk;
1308 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1309 rgd->rd_rg.rg_free--;
1310 rgd->rd_rg.rg_dinodes++;
1311 *generation = rgd->rd_rg.rg_igeneration++;
1312 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1313 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1317 gfs2_statfs_change(sdp, 0, -1, +1);
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_free_data - free a contiguous run of data block(s)
1329 * @ip: the inode these blocks are being freed from
1330 * @bstart: first block of a run of contiguous blocks
1331 * @blen: the length of the block run
1335 void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1337 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1338 struct gfs2_rgrpd *rgd;
1340 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1344 rgd->rd_rg.rg_free += blen;
1346 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1347 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1349 gfs2_trans_add_rg(rgd);
1351 gfs2_statfs_change(sdp, 0, +blen, 0);
1352 gfs2_quota_change(ip, -(int64_t)blen,
1353 ip->i_di.di_uid, ip->i_di.di_gid);
1357 * gfs2_free_meta - free a contiguous run of data block(s)
1358 * @ip: the inode these blocks are being freed from
1359 * @bstart: first block of a run of contiguous blocks
1360 * @blen: the length of the block run
1364 void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1366 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1367 struct gfs2_rgrpd *rgd;
1369 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1373 rgd->rd_rg.rg_free += blen;
1375 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1376 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1378 gfs2_trans_add_rg(rgd);
1380 gfs2_statfs_change(sdp, 0, +blen, 0);
1381 gfs2_quota_change(ip, -(int64_t)blen, ip->i_di.di_uid, ip->i_di.di_gid);
1382 gfs2_meta_wipe(ip, bstart, blen);
1385 void gfs2_unlink_di(struct inode *inode)
1387 struct gfs2_inode *ip = GFS2_I(inode);
1388 struct gfs2_sbd *sdp = GFS2_SB(inode);
1389 struct gfs2_rgrpd *rgd;
1390 u64 blkno = ip->i_num.no_addr;
1392 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1395 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1396 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1397 gfs2_trans_add_rg(rgd);
1400 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
1402 struct gfs2_sbd *sdp = rgd->rd_sbd;
1403 struct gfs2_rgrpd *tmp_rgd;
1405 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1408 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1410 if (!rgd->rd_rg.rg_dinodes)
1411 gfs2_consist_rgrpd(rgd);
1412 rgd->rd_rg.rg_dinodes--;
1413 rgd->rd_rg.rg_free++;
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_statfs_change(sdp, 0, +1, -1);
1419 gfs2_trans_add_rg(rgd);
1423 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1425 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
1426 gfs2_quota_change(ip, -1, ip->i_di.di_uid, ip->i_di.di_gid);
1427 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1431 * gfs2_rlist_add - add a RG to a list of RGs
1432 * @sdp: the filesystem
1433 * @rlist: the list of resource groups
1436 * Figure out what RG a block belongs to and add that RG to the list
1438 * FIXME: Don't use NOFAIL
1442 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1445 struct gfs2_rgrpd *rgd;
1446 struct gfs2_rgrpd **tmp;
1447 unsigned int new_space;
1450 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1453 rgd = gfs2_blk2rgrpd(sdp, block);
1455 if (gfs2_consist(sdp))
1456 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1460 for (x = 0; x < rlist->rl_rgrps; x++)
1461 if (rlist->rl_rgd[x] == rgd)
1464 if (rlist->rl_rgrps == rlist->rl_space) {
1465 new_space = rlist->rl_space + 10;
1467 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1468 GFP_NOFS | __GFP_NOFAIL);
1470 if (rlist->rl_rgd) {
1471 memcpy(tmp, rlist->rl_rgd,
1472 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1473 kfree(rlist->rl_rgd);
1476 rlist->rl_space = new_space;
1477 rlist->rl_rgd = tmp;
1480 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1484 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1485 * and initialize an array of glock holders for them
1486 * @rlist: the list of resource groups
1487 * @state: the lock state to acquire the RG lock in
1488 * @flags: the modifier flags for the holder structures
1490 * FIXME: Don't use NOFAIL
1494 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1499 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1500 GFP_NOFS | __GFP_NOFAIL);
1501 for (x = 0; x < rlist->rl_rgrps; x++)
1502 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1508 * gfs2_rlist_free - free a resource group list
1509 * @list: the list of resource groups
1513 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1517 kfree(rlist->rl_rgd);
1519 if (rlist->rl_ghs) {
1520 for (x = 0; x < rlist->rl_rgrps; x++)
1521 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1522 kfree(rlist->rl_ghs);