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 <linux/gfs2_ondisk.h>
17 #include <asm/semaphore.h>
20 #include "lm_interface.h"
33 #define BFITNOENT 0xFFFFFFFF
36 * These routines are used by the resource group routines (rgrp.c)
37 * to keep track of block allocation. Each block is represented by two
38 * bits. One bit indicates whether or not the block is used. (1=used,
39 * 0=free) The other bit indicates whether or not the block contains a
40 * dinode or not. (1=dinode, 0=not-dinode) So, each byte represents
41 * GFS2_NBBY (i.e. 4) blocks.
44 static const char valid_change[16] = {
53 * gfs2_setbit - Set a bit in the bitmaps
54 * @buffer: the buffer that holds the bitmaps
55 * @buflen: the length (in bytes) of the buffer
56 * @block: the block to set
57 * @new_state: the new state of the block
61 static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
62 unsigned int buflen, uint32_t block,
63 unsigned char new_state)
65 unsigned char *byte, *end, cur_state;
68 byte = buffer + (block / GFS2_NBBY);
69 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
70 end = buffer + buflen;
72 gfs2_assert(rgd->rd_sbd, byte < end);
74 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
76 if (valid_change[new_state * 4 + cur_state]) {
77 *byte ^= cur_state << bit;
78 *byte |= new_state << bit;
80 gfs2_consist_rgrpd(rgd);
84 * gfs2_testbit - test a bit in the bitmaps
85 * @buffer: the buffer that holds the bitmaps
86 * @buflen: the length (in bytes) of the buffer
87 * @block: the block to read
91 static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
92 unsigned int buflen, uint32_t block)
94 unsigned char *byte, *end, cur_state;
97 byte = buffer + (block / GFS2_NBBY);
98 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
99 end = buffer + buflen;
101 gfs2_assert(rgd->rd_sbd, byte < end);
103 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
109 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
110 * a block in a given allocation state.
111 * @buffer: the buffer that holds the bitmaps
112 * @buflen: the length (in bytes) of the buffer
113 * @goal: start search at this block's bit-pair (within @buffer)
114 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
115 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
117 * Scope of @goal and returned block number is only within this bitmap buffer,
118 * not entire rgrp or filesystem. @buffer will be offset from the actual
119 * beginning of a bitmap block buffer, skipping any header structures.
121 * Return: the block number (bitmap buffer scope) that was found
124 static uint32_t gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
125 unsigned int buflen, uint32_t goal,
126 unsigned char old_state)
128 unsigned char *byte, *end, alloc;
132 byte = buffer + (goal / GFS2_NBBY);
133 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
134 end = buffer + buflen;
135 alloc = (old_state & 1) ? 0 : 0x55;
138 if ((*byte & 0x55) == alloc) {
139 blk += (8 - bit) >> 1;
147 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
150 bit += GFS2_BIT_SIZE;
163 * gfs2_bitcount - count the number of bits in a certain state
164 * @buffer: the buffer that holds the bitmaps
165 * @buflen: the length (in bytes) of the buffer
166 * @state: the state of the block we're looking for
168 * Returns: The number of bits
171 static uint32_t gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
172 unsigned int buflen, unsigned char state)
174 unsigned char *byte = buffer;
175 unsigned char *end = buffer + buflen;
176 unsigned char state1 = state << 2;
177 unsigned char state2 = state << 4;
178 unsigned char state3 = state << 6;
181 for (; byte < end; byte++) {
182 if (((*byte) & 0x03) == state)
184 if (((*byte) & 0x0C) == state1)
186 if (((*byte) & 0x30) == state2)
188 if (((*byte) & 0xC0) == state3)
196 * gfs2_rgrp_verify - Verify that a resource group is consistent
197 * @sdp: the filesystem
202 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
204 struct gfs2_sbd *sdp = rgd->rd_sbd;
205 struct gfs2_bitmap *bi = NULL;
206 uint32_t length = rgd->rd_ri.ri_length;
207 uint32_t count[4], tmp;
210 memset(count, 0, 4 * sizeof(uint32_t));
212 /* Count # blocks in each of 4 possible allocation states */
213 for (buf = 0; buf < length; buf++) {
214 bi = rgd->rd_bits + buf;
215 for (x = 0; x < 4; x++)
216 count[x] += gfs2_bitcount(rgd,
222 if (count[0] != rgd->rd_rg.rg_free) {
223 if (gfs2_consist_rgrpd(rgd))
224 fs_err(sdp, "free data mismatch: %u != %u\n",
225 count[0], rgd->rd_rg.rg_free);
229 tmp = rgd->rd_ri.ri_data -
231 rgd->rd_rg.rg_dinodes;
232 if (count[1] != tmp) {
233 if (gfs2_consist_rgrpd(rgd))
234 fs_err(sdp, "used data mismatch: %u != %u\n",
240 if (gfs2_consist_rgrpd(rgd))
241 fs_err(sdp, "free metadata mismatch: %u != 0\n",
246 if (count[3] != rgd->rd_rg.rg_dinodes) {
247 if (gfs2_consist_rgrpd(rgd))
248 fs_err(sdp, "used metadata mismatch: %u != %u\n",
249 count[3], rgd->rd_rg.rg_dinodes);
254 static inline int rgrp_contains_block(struct gfs2_rindex *ri, uint64_t block)
256 uint64_t first = ri->ri_data0;
257 uint64_t last = first + ri->ri_data;
258 return !!(first <= block && block < last);
262 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
263 * @sdp: The GFS2 superblock
264 * @n: The data block number
266 * Returns: The resource group, or NULL if not found
269 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, uint64_t blk)
271 struct gfs2_rgrpd *rgd;
273 spin_lock(&sdp->sd_rindex_spin);
275 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
276 if (rgrp_contains_block(&rgd->rd_ri, blk)) {
277 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
278 spin_unlock(&sdp->sd_rindex_spin);
283 spin_unlock(&sdp->sd_rindex_spin);
289 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
290 * @sdp: The GFS2 superblock
292 * Returns: The first rgrp in the filesystem
295 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
297 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
298 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
302 * gfs2_rgrpd_get_next - get the next RG
305 * Returns: The next rgrp
308 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
310 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
312 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
315 static void clear_rgrpdi(struct gfs2_sbd *sdp)
317 struct list_head *head;
318 struct gfs2_rgrpd *rgd;
319 struct gfs2_glock *gl;
321 spin_lock(&sdp->sd_rindex_spin);
322 sdp->sd_rindex_forward = NULL;
323 head = &sdp->sd_rindex_recent_list;
324 while (!list_empty(head)) {
325 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
326 list_del(&rgd->rd_recent);
328 spin_unlock(&sdp->sd_rindex_spin);
330 head = &sdp->sd_rindex_list;
331 while (!list_empty(head)) {
332 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
335 list_del(&rgd->rd_list);
336 list_del(&rgd->rd_list_mru);
339 gl->gl_object = NULL;
348 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
350 mutex_lock(&sdp->sd_rindex_mutex);
352 mutex_unlock(&sdp->sd_rindex_mutex);
356 * gfs2_compute_bitstructs - Compute the bitmap sizes
357 * @rgd: The resource group descriptor
359 * Calculates bitmap descriptors, one for each block that contains bitmap data
364 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
366 struct gfs2_sbd *sdp = rgd->rd_sbd;
367 struct gfs2_bitmap *bi;
368 uint32_t length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
369 uint32_t bytes_left, bytes;
372 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_KERNEL);
376 bytes_left = rgd->rd_ri.ri_bitbytes;
378 for (x = 0; x < length; x++) {
379 bi = rgd->rd_bits + x;
381 /* small rgrp; bitmap stored completely in header block */
384 bi->bi_offset = sizeof(struct gfs2_rgrp);
389 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
390 bi->bi_offset = sizeof(struct gfs2_rgrp);
394 } else if (x + 1 == length) {
396 bi->bi_offset = sizeof(struct gfs2_meta_header);
397 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
401 bytes = sdp->sd_sb.sb_bsize -
402 sizeof(struct gfs2_meta_header);
403 bi->bi_offset = sizeof(struct gfs2_meta_header);
404 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
412 gfs2_consist_rgrpd(rgd);
415 bi = rgd->rd_bits + (length - 1);
416 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
417 if (gfs2_consist_rgrpd(rgd)) {
418 gfs2_rindex_print(&rgd->rd_ri);
419 fs_err(sdp, "start=%u len=%u offset=%u\n",
420 bi->bi_start, bi->bi_len, bi->bi_offset);
429 * gfs2_ri_update - Pull in a new resource index from the disk
430 * @gl: The glock covering the rindex inode
432 * Returns: 0 on successful update, error code otherwise
435 static int gfs2_ri_update(struct gfs2_inode *ip)
437 struct gfs2_sbd *sdp = ip->i_sbd;
438 struct inode *inode = ip->i_vnode;
439 struct gfs2_rgrpd *rgd;
440 char buf[sizeof(struct gfs2_rindex)];
441 struct file_ra_state ra_state;
442 uint64_t junk = ip->i_di.di_size;
445 if (do_div(junk, sizeof(struct gfs2_rindex))) {
446 gfs2_consist_inode(ip);
452 file_ra_state_init(&ra_state, inode->i_mapping);
453 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
454 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
455 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
456 sizeof(struct gfs2_rindex));
459 if (error != sizeof(struct gfs2_rindex)) {
465 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_KERNEL);
470 mutex_init(&rgd->rd_mutex);
471 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
474 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
475 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
477 gfs2_rindex_in(&rgd->rd_ri, buf);
479 error = compute_bitstructs(rgd);
483 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
484 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
488 rgd->rd_gl->gl_object = rgd;
489 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
492 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
503 * gfs2_rindex_hold - Grab a lock on the rindex
504 * @sdp: The GFS2 superblock
505 * @ri_gh: the glock holder
507 * We grab a lock on the rindex inode to make sure that it doesn't
508 * change whilst we are performing an operation. We keep this lock
509 * for quite long periods of time compared to other locks. This
510 * doesn't matter, since it is shared and it is very, very rarely
511 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
513 * This makes sure that we're using the latest copy of the resource index
514 * special file, which might have been updated if someone expanded the
515 * filesystem (via gfs2_grow utility), which adds new resource groups.
517 * Returns: 0 on success, error code otherwise
520 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
522 struct gfs2_inode *ip = sdp->sd_rindex->u.generic_ip;
523 struct gfs2_glock *gl = ip->i_gl;
526 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
530 /* Read new copy from disk if we don't have the latest */
531 if (sdp->sd_rindex_vn != gl->gl_vn) {
532 mutex_lock(&sdp->sd_rindex_mutex);
533 if (sdp->sd_rindex_vn != gl->gl_vn) {
534 error = gfs2_ri_update(ip);
536 gfs2_glock_dq_uninit(ri_gh);
538 mutex_unlock(&sdp->sd_rindex_mutex);
545 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
546 * @rgd: the struct gfs2_rgrpd describing the RG to read in
548 * Read in all of a Resource Group's header and bitmap blocks.
549 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
554 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
556 struct gfs2_sbd *sdp = rgd->rd_sbd;
557 struct gfs2_glock *gl = rgd->rd_gl;
558 unsigned int length = rgd->rd_ri.ri_length;
559 struct gfs2_bitmap *bi;
563 mutex_lock(&rgd->rd_mutex);
565 spin_lock(&sdp->sd_rindex_spin);
566 if (rgd->rd_bh_count) {
568 spin_unlock(&sdp->sd_rindex_spin);
569 mutex_unlock(&rgd->rd_mutex);
572 spin_unlock(&sdp->sd_rindex_spin);
574 for (x = 0; x < length; x++) {
575 bi = rgd->rd_bits + x;
576 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, DIO_START,
582 for (y = length; y--;) {
583 bi = rgd->rd_bits + y;
584 error = gfs2_meta_reread(sdp, bi->bi_bh, DIO_WAIT);
587 if (gfs2_metatype_check(sdp, bi->bi_bh,
588 (y) ? GFS2_METATYPE_RB :
595 if (rgd->rd_rg_vn != gl->gl_vn) {
596 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
597 rgd->rd_rg_vn = gl->gl_vn;
600 spin_lock(&sdp->sd_rindex_spin);
601 rgd->rd_free_clone = rgd->rd_rg.rg_free;
603 spin_unlock(&sdp->sd_rindex_spin);
605 mutex_unlock(&rgd->rd_mutex);
611 bi = rgd->rd_bits + x;
614 gfs2_assert_warn(sdp, !bi->bi_clone);
616 mutex_unlock(&rgd->rd_mutex);
621 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
623 struct gfs2_sbd *sdp = rgd->rd_sbd;
625 spin_lock(&sdp->sd_rindex_spin);
626 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
628 spin_unlock(&sdp->sd_rindex_spin);
632 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
633 * @rgd: the struct gfs2_rgrpd describing the RG to read in
637 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
639 struct gfs2_sbd *sdp = rgd->rd_sbd;
640 int x, length = rgd->rd_ri.ri_length;
642 spin_lock(&sdp->sd_rindex_spin);
643 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
644 if (--rgd->rd_bh_count) {
645 spin_unlock(&sdp->sd_rindex_spin);
649 for (x = 0; x < length; x++) {
650 struct gfs2_bitmap *bi = rgd->rd_bits + x;
657 spin_unlock(&sdp->sd_rindex_spin);
660 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
662 struct gfs2_sbd *sdp = rgd->rd_sbd;
663 unsigned int length = rgd->rd_ri.ri_length;
666 for (x = 0; x < length; x++) {
667 struct gfs2_bitmap *bi = rgd->rd_bits + x;
670 memcpy(bi->bi_clone + bi->bi_offset,
671 bi->bi_bh->b_data + bi->bi_offset,
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,
766 spin_unlock(&sdp->sd_rindex_spin);
772 * recent_rgrp_next - get next RG from "recent" list
773 * @cur_rgd: current rgrp
776 * Returns: The next rgrp in the recent list
779 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
782 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
783 struct list_head *head;
784 struct gfs2_rgrpd *rgd;
786 spin_lock(&sdp->sd_rindex_spin);
788 head = &sdp->sd_rindex_recent_list;
790 list_for_each_entry(rgd, head, rd_recent) {
791 if (rgd == cur_rgd) {
792 if (cur_rgd->rd_recent.next != head)
793 rgd = list_entry(cur_rgd->rd_recent.next,
794 struct gfs2_rgrpd, rd_recent);
799 list_del(&cur_rgd->rd_recent);
806 if (!list_empty(head))
807 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
810 spin_unlock(&sdp->sd_rindex_spin);
816 * recent_rgrp_add - add an RG to tail of "recent" list
817 * @new_rgd: The rgrp to add
821 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
823 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
824 struct gfs2_rgrpd *rgd;
825 unsigned int count = 0;
826 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
828 spin_lock(&sdp->sd_rindex_spin);
830 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
837 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
840 spin_unlock(&sdp->sd_rindex_spin);
844 * forward_rgrp_get - get an rgrp to try next from full list
845 * @sdp: The GFS2 superblock
847 * Returns: The rgrp to try next
850 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
852 struct gfs2_rgrpd *rgd;
853 unsigned int journals = gfs2_jindex_size(sdp);
854 unsigned int rg = 0, x;
856 spin_lock(&sdp->sd_rindex_spin);
858 rgd = sdp->sd_rindex_forward;
860 if (sdp->sd_rgrps >= journals)
861 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
863 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp);
865 x++, rgd = gfs2_rgrpd_get_next(rgd))
868 sdp->sd_rindex_forward = rgd;
871 spin_unlock(&sdp->sd_rindex_spin);
877 * forward_rgrp_set - set the forward rgrp pointer
878 * @sdp: the filesystem
879 * @rgd: The new forward rgrp
883 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
885 spin_lock(&sdp->sd_rindex_spin);
886 sdp->sd_rindex_forward = rgd;
887 spin_unlock(&sdp->sd_rindex_spin);
891 * get_local_rgrp - Choose and lock a rgrp for allocation
892 * @ip: the inode to reserve space for
893 * @rgp: the chosen and locked rgrp
895 * Try to acquire rgrp in way which avoids contending with others.
900 static int get_local_rgrp(struct gfs2_inode *ip)
902 struct gfs2_sbd *sdp = ip->i_sbd;
903 struct gfs2_rgrpd *rgd, *begin = NULL;
904 struct gfs2_alloc *al = &ip->i_alloc;
905 int flags = LM_FLAG_TRY;
910 /* Try recently successful rgrps */
912 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
915 error = gfs2_glock_nq_init(rgd->rd_gl,
916 LM_ST_EXCLUSIVE, LM_FLAG_TRY,
920 if (try_rgrp_fit(rgd, al))
922 gfs2_glock_dq_uninit(&al->al_rgd_gh);
923 rgd = recent_rgrp_next(rgd, 1);
927 rgd = recent_rgrp_next(rgd, 0);
935 /* Go through full list of rgrps */
937 begin = rgd = forward_rgrp_get(sdp);
940 error = gfs2_glock_nq_init(rgd->rd_gl,
941 LM_ST_EXCLUSIVE, flags,
945 if (try_rgrp_fit(rgd, al))
947 gfs2_glock_dq_uninit(&al->al_rgd_gh);
958 rgd = gfs2_rgrpd_get_next(rgd);
960 rgd = gfs2_rgrpd_get_first(sdp);
963 if (++loops >= 2 || !skipped)
970 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
973 recent_rgrp_add(rgd);
974 rgd = gfs2_rgrpd_get_next(rgd);
976 rgd = gfs2_rgrpd_get_first(sdp);
977 forward_rgrp_set(sdp, rgd);
984 * gfs2_inplace_reserve_i - Reserve space in the filesystem
985 * @ip: the inode to reserve space for
990 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
992 struct gfs2_sbd *sdp = ip->i_sbd;
993 struct gfs2_alloc *al = &ip->i_alloc;
996 if (gfs2_assert_warn(sdp, al->al_requested))
999 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1003 error = get_local_rgrp(ip);
1005 gfs2_glock_dq_uninit(&al->al_ri_gh);
1016 * gfs2_inplace_release - release an inplace reservation
1017 * @ip: the inode the reservation was taken out on
1019 * Release a reservation made by gfs2_inplace_reserve().
1022 void gfs2_inplace_release(struct gfs2_inode *ip)
1024 struct gfs2_sbd *sdp = ip->i_sbd;
1025 struct gfs2_alloc *al = &ip->i_alloc;
1027 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1028 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1029 "al_file = %s, al_line = %u\n",
1030 al->al_alloced, al->al_requested, al->al_file,
1034 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1035 gfs2_glock_dq_uninit(&al->al_ri_gh);
1039 * gfs2_get_block_type - Check a block in a RG is of given type
1040 * @rgd: the resource group holding the block
1041 * @block: the block number
1043 * Returns: The block type (GFS2_BLKST_*)
1046 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, uint64_t block)
1048 struct gfs2_bitmap *bi = NULL;
1049 uint32_t length, rgrp_block, buf_block;
1053 length = rgd->rd_ri.ri_length;
1054 rgrp_block = block - rgd->rd_ri.ri_data0;
1056 for (buf = 0; buf < length; buf++) {
1057 bi = rgd->rd_bits + buf;
1058 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1062 gfs2_assert(rgd->rd_sbd, buf < length);
1063 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1065 type = gfs2_testbit(rgd,
1066 bi->bi_bh->b_data + bi->bi_offset,
1067 bi->bi_len, buf_block);
1073 * rgblk_search - find a block in @old_state, change allocation
1074 * state to @new_state
1075 * @rgd: the resource group descriptor
1076 * @goal: the goal block within the RG (start here to search for avail block)
1077 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1078 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1080 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1081 * Add the found bitmap buffer to the transaction.
1082 * Set the found bits to @new_state to change block's allocation state.
1084 * This function never fails, because we wouldn't call it unless we
1085 * know (from reservation results, etc.) that a block is available.
1087 * Scope of @goal and returned block is just within rgrp, not the whole
1090 * Returns: the block number allocated
1093 static uint32_t rgblk_search(struct gfs2_rgrpd *rgd, uint32_t goal,
1094 unsigned char old_state, unsigned char new_state)
1096 struct gfs2_bitmap *bi = NULL;
1097 uint32_t length = rgd->rd_ri.ri_length;
1099 unsigned int buf, x;
1101 /* Find bitmap block that contains bits for goal block */
1102 for (buf = 0; buf < length; buf++) {
1103 bi = rgd->rd_bits + buf;
1104 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1108 gfs2_assert(rgd->rd_sbd, buf < length);
1110 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1111 goal -= bi->bi_start * GFS2_NBBY;
1113 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1114 "x <= length", instead of "x < length", because we typically start
1115 the search in the middle of a bit block, but if we can't find an
1116 allocatable block anywhere else, we want to be able wrap around and
1117 search in the first part of our first-searched bit block. */
1118 for (x = 0; x <= length; x++) {
1120 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
1121 bi->bi_len, goal, old_state);
1123 blk = gfs2_bitfit(rgd,
1124 bi->bi_bh->b_data + bi->bi_offset,
1125 bi->bi_len, goal, old_state);
1126 if (blk != BFITNOENT)
1129 /* Try next bitmap block (wrap back to rgrp header if at end) */
1130 buf = (buf + 1) % length;
1131 bi = rgd->rd_bits + buf;
1135 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1138 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1139 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1140 bi->bi_len, blk, new_state);
1142 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1143 bi->bi_len, blk, new_state);
1145 return bi->bi_start * GFS2_NBBY + blk;
1149 * rgblk_free - Change alloc state of given block(s)
1150 * @sdp: the filesystem
1151 * @bstart: the start of a run of blocks to free
1152 * @blen: the length of the block run (all must lie within ONE RG!)
1153 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1155 * Returns: Resource group containing the block(s)
1158 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
1159 uint32_t blen, unsigned char new_state)
1161 struct gfs2_rgrpd *rgd;
1162 struct gfs2_bitmap *bi = NULL;
1163 uint32_t length, rgrp_blk, buf_blk;
1166 rgd = gfs2_blk2rgrpd(sdp, bstart);
1168 if (gfs2_consist(sdp))
1169 fs_err(sdp, "block = %llu\n", bstart);
1173 length = rgd->rd_ri.ri_length;
1175 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1178 for (buf = 0; buf < length; buf++) {
1179 bi = rgd->rd_bits + buf;
1180 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1184 gfs2_assert(rgd->rd_sbd, buf < length);
1186 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1189 if (!bi->bi_clone) {
1190 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1191 GFP_KERNEL | __GFP_NOFAIL);
1192 memcpy(bi->bi_clone + bi->bi_offset,
1193 bi->bi_bh->b_data + bi->bi_offset,
1196 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1198 bi->bi_bh->b_data + bi->bi_offset,
1199 bi->bi_len, buf_blk, new_state);
1206 * gfs2_alloc_data - Allocate a data block
1207 * @ip: the inode to allocate the data block for
1209 * Returns: the allocated block
1212 uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
1214 struct gfs2_sbd *sdp = ip->i_sbd;
1215 struct gfs2_alloc *al = &ip->i_alloc;
1216 struct gfs2_rgrpd *rgd = al->al_rgd;
1220 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1221 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1223 goal = rgd->rd_last_alloc_data;
1225 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1226 rgd->rd_last_alloc_data = blk;
1228 block = rgd->rd_ri.ri_data0 + blk;
1229 ip->i_di.di_goal_data = block;
1231 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1232 rgd->rd_rg.rg_free--;
1234 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1235 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1239 gfs2_statfs_change(sdp, 0, -1, 0);
1240 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1242 spin_lock(&sdp->sd_rindex_spin);
1243 rgd->rd_free_clone--;
1244 spin_unlock(&sdp->sd_rindex_spin);
1250 * gfs2_alloc_meta - Allocate a metadata block
1251 * @ip: the inode to allocate the metadata block for
1253 * Returns: the allocated block
1256 uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
1258 struct gfs2_sbd *sdp = ip->i_sbd;
1259 struct gfs2_alloc *al = &ip->i_alloc;
1260 struct gfs2_rgrpd *rgd = al->al_rgd;
1264 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1265 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1267 goal = rgd->rd_last_alloc_meta;
1269 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1270 rgd->rd_last_alloc_meta = blk;
1272 block = rgd->rd_ri.ri_data0 + blk;
1273 ip->i_di.di_goal_meta = block;
1275 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1276 rgd->rd_rg.rg_free--;
1278 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1279 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1283 gfs2_statfs_change(sdp, 0, -1, 0);
1284 gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
1285 gfs2_trans_add_unrevoke(sdp, block);
1287 spin_lock(&sdp->sd_rindex_spin);
1288 rgd->rd_free_clone--;
1289 spin_unlock(&sdp->sd_rindex_spin);
1295 * gfs2_alloc_di - Allocate a dinode
1296 * @dip: the directory that the inode is going in
1298 * Returns: the block allocated
1301 uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
1303 struct gfs2_sbd *sdp = dip->i_sbd;
1304 struct gfs2_alloc *al = &dip->i_alloc;
1305 struct gfs2_rgrpd *rgd = al->al_rgd;
1309 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1310 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1312 rgd->rd_last_alloc_meta = blk;
1314 block = rgd->rd_ri.ri_data0 + blk;
1316 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1317 rgd->rd_rg.rg_free--;
1318 rgd->rd_rg.rg_dinodes++;
1320 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1321 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1325 gfs2_statfs_change(sdp, 0, -1, +1);
1326 gfs2_trans_add_unrevoke(sdp, block);
1328 spin_lock(&sdp->sd_rindex_spin);
1329 rgd->rd_free_clone--;
1330 spin_unlock(&sdp->sd_rindex_spin);
1336 * gfs2_free_data - free a contiguous run of data block(s)
1337 * @ip: the inode these blocks are being freed from
1338 * @bstart: first block of a run of contiguous blocks
1339 * @blen: the length of the block run
1343 void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1345 struct gfs2_sbd *sdp = ip->i_sbd;
1346 struct gfs2_rgrpd *rgd;
1348 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1352 rgd->rd_rg.rg_free += blen;
1354 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1355 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1357 gfs2_trans_add_rg(rgd);
1359 gfs2_statfs_change(sdp, 0, +blen, 0);
1360 gfs2_quota_change(ip, -(int64_t)blen,
1361 ip->i_di.di_uid, ip->i_di.di_gid);
1365 * gfs2_free_meta - free a contiguous run of data block(s)
1366 * @ip: the inode these blocks are being freed from
1367 * @bstart: first block of a run of contiguous blocks
1368 * @blen: the length of the block run
1372 void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
1374 struct gfs2_sbd *sdp = ip->i_sbd;
1375 struct gfs2_rgrpd *rgd;
1377 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1381 rgd->rd_rg.rg_free += blen;
1383 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1384 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1386 gfs2_trans_add_rg(rgd);
1388 gfs2_statfs_change(sdp, 0, +blen, 0);
1389 gfs2_quota_change(ip, -(int64_t)blen,
1390 ip->i_di.di_uid, ip->i_di.di_gid);
1391 gfs2_meta_wipe(ip, bstart, blen);
1394 void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
1396 struct gfs2_sbd *sdp = rgd->rd_sbd;
1397 struct gfs2_rgrpd *tmp_rgd;
1399 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1402 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1404 if (!rgd->rd_rg.rg_dinodes)
1405 gfs2_consist_rgrpd(rgd);
1406 rgd->rd_rg.rg_dinodes--;
1407 rgd->rd_rg.rg_free++;
1409 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1410 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1412 gfs2_statfs_change(sdp, 0, +1, -1);
1413 gfs2_trans_add_rg(rgd);
1417 * gfs2_free_uninit_di - free a dinode block
1418 * @rgd: the resource group that contains the dinode
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", 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_KERNEL | __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_KERNEL | __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);