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 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>
31 #define BFITNOENT ((u32)~0)
34 * These routines are used by the resource group routines (rgrp.c)
35 * to keep track of block allocation. Each block is represented by two
36 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
39 * 1 = Used (not metadata)
40 * 2 = Unlinked (still in use) inode
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, u32 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, u32 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 u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
125 unsigned int buflen, u32 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 u32 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 u32 length = rgd->rd_ri.ri_length;
210 memset(count, 0, 4 * sizeof(u32));
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] + count[2] != tmp) {
233 if (gfs2_consist_rgrpd(rgd))
234 fs_err(sdp, "used data mismatch: %u != %u\n",
239 if (count[3] != rgd->rd_rg.rg_dinodes) {
240 if (gfs2_consist_rgrpd(rgd))
241 fs_err(sdp, "used metadata mismatch: %u != %u\n",
242 count[3], rgd->rd_rg.rg_dinodes);
246 if (count[2] > count[3]) {
247 if (gfs2_consist_rgrpd(rgd))
248 fs_err(sdp, "unlinked inodes > inodes: %u\n",
255 static inline int rgrp_contains_block(struct gfs2_rindex_host *ri, u64 block)
257 u64 first = ri->ri_data0;
258 u64 last = first + ri->ri_data;
259 return first <= block && block < last;
263 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
264 * @sdp: The GFS2 superblock
265 * @n: The data block number
267 * Returns: The resource group, or NULL if not found
270 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
272 struct gfs2_rgrpd *rgd;
274 spin_lock(&sdp->sd_rindex_spin);
276 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
277 if (rgrp_contains_block(&rgd->rd_ri, blk)) {
278 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
279 spin_unlock(&sdp->sd_rindex_spin);
284 spin_unlock(&sdp->sd_rindex_spin);
290 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
291 * @sdp: The GFS2 superblock
293 * Returns: The first rgrp in the filesystem
296 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
298 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
299 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
303 * gfs2_rgrpd_get_next - get the next RG
306 * Returns: The next rgrp
309 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
311 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
313 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
316 static void clear_rgrpdi(struct gfs2_sbd *sdp)
318 struct list_head *head;
319 struct gfs2_rgrpd *rgd;
320 struct gfs2_glock *gl;
322 spin_lock(&sdp->sd_rindex_spin);
323 sdp->sd_rindex_forward = NULL;
324 head = &sdp->sd_rindex_recent_list;
325 while (!list_empty(head)) {
326 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
327 list_del(&rgd->rd_recent);
329 spin_unlock(&sdp->sd_rindex_spin);
331 head = &sdp->sd_rindex_list;
332 while (!list_empty(head)) {
333 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
336 list_del(&rgd->rd_list);
337 list_del(&rgd->rd_list_mru);
340 gl->gl_object = NULL;
349 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
351 mutex_lock(&sdp->sd_rindex_mutex);
353 mutex_unlock(&sdp->sd_rindex_mutex);
357 * gfs2_compute_bitstructs - Compute the bitmap sizes
358 * @rgd: The resource group descriptor
360 * Calculates bitmap descriptors, one for each block that contains bitmap data
365 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
367 struct gfs2_sbd *sdp = rgd->rd_sbd;
368 struct gfs2_bitmap *bi;
369 u32 length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
370 u32 bytes_left, bytes;
376 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
380 bytes_left = rgd->rd_ri.ri_bitbytes;
382 for (x = 0; x < length; x++) {
383 bi = rgd->rd_bits + x;
385 /* small rgrp; bitmap stored completely in header block */
388 bi->bi_offset = sizeof(struct gfs2_rgrp);
393 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
394 bi->bi_offset = sizeof(struct gfs2_rgrp);
398 } else if (x + 1 == length) {
400 bi->bi_offset = sizeof(struct gfs2_meta_header);
401 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
405 bytes = sdp->sd_sb.sb_bsize -
406 sizeof(struct gfs2_meta_header);
407 bi->bi_offset = sizeof(struct gfs2_meta_header);
408 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
416 gfs2_consist_rgrpd(rgd);
419 bi = rgd->rd_bits + (length - 1);
420 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
421 if (gfs2_consist_rgrpd(rgd)) {
422 gfs2_rindex_print(&rgd->rd_ri);
423 fs_err(sdp, "start=%u len=%u offset=%u\n",
424 bi->bi_start, bi->bi_len, bi->bi_offset);
433 * gfs2_ri_update - Pull in a new resource index from the disk
434 * @gl: The glock covering the rindex inode
436 * Returns: 0 on successful update, error code otherwise
439 static int gfs2_ri_update(struct gfs2_inode *ip)
441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
442 struct inode *inode = &ip->i_inode;
443 struct gfs2_rgrpd *rgd;
444 char buf[sizeof(struct gfs2_rindex)];
445 struct file_ra_state ra_state;
446 u64 junk = ip->i_di.di_size;
449 if (do_div(junk, sizeof(struct gfs2_rindex))) {
450 gfs2_consist_inode(ip);
456 file_ra_state_init(&ra_state, inode->i_mapping);
457 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
458 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
459 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
460 sizeof(struct gfs2_rindex));
463 if (error != sizeof(struct gfs2_rindex)) {
469 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
474 mutex_init(&rgd->rd_mutex);
475 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
478 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
479 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
481 gfs2_rindex_in(&rgd->rd_ri, buf);
482 error = compute_bitstructs(rgd);
486 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
487 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
491 rgd->rd_gl->gl_object = rgd;
492 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
495 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
504 * gfs2_rindex_hold - Grab a lock on the rindex
505 * @sdp: The GFS2 superblock
506 * @ri_gh: the glock holder
508 * We grab a lock on the rindex inode to make sure that it doesn't
509 * change whilst we are performing an operation. We keep this lock
510 * for quite long periods of time compared to other locks. This
511 * doesn't matter, since it is shared and it is very, very rarely
512 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
514 * This makes sure that we're using the latest copy of the resource index
515 * special file, which might have been updated if someone expanded the
516 * filesystem (via gfs2_grow utility), which adds new resource groups.
518 * Returns: 0 on success, error code otherwise
521 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
523 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
524 struct gfs2_glock *gl = ip->i_gl;
527 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
531 /* Read new copy from disk if we don't have the latest */
532 if (sdp->sd_rindex_vn != gl->gl_vn) {
533 mutex_lock(&sdp->sd_rindex_mutex);
534 if (sdp->sd_rindex_vn != gl->gl_vn) {
535 error = gfs2_ri_update(ip);
537 gfs2_glock_dq_uninit(ri_gh);
539 mutex_unlock(&sdp->sd_rindex_mutex);
546 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
547 * @rgd: the struct gfs2_rgrpd describing the RG to read in
549 * Read in all of a Resource Group's header and bitmap blocks.
550 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
555 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
557 struct gfs2_sbd *sdp = rgd->rd_sbd;
558 struct gfs2_glock *gl = rgd->rd_gl;
559 unsigned int length = rgd->rd_ri.ri_length;
560 struct gfs2_bitmap *bi;
564 mutex_lock(&rgd->rd_mutex);
566 spin_lock(&sdp->sd_rindex_spin);
567 if (rgd->rd_bh_count) {
569 spin_unlock(&sdp->sd_rindex_spin);
570 mutex_unlock(&rgd->rd_mutex);
573 spin_unlock(&sdp->sd_rindex_spin);
575 for (x = 0; x < length; x++) {
576 bi = rgd->rd_bits + x;
577 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, 0, &bi->bi_bh);
582 for (y = length; y--;) {
583 bi = rgd->rd_bits + y;
584 error = gfs2_meta_wait(sdp, bi->bi_bh);
587 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
594 if (rgd->rd_rg_vn != gl->gl_vn) {
595 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
596 rgd->rd_rg_vn = gl->gl_vn;
599 spin_lock(&sdp->sd_rindex_spin);
600 rgd->rd_free_clone = rgd->rd_rg.rg_free;
602 spin_unlock(&sdp->sd_rindex_spin);
604 mutex_unlock(&rgd->rd_mutex);
610 bi = rgd->rd_bits + x;
613 gfs2_assert_warn(sdp, !bi->bi_clone);
615 mutex_unlock(&rgd->rd_mutex);
620 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
622 struct gfs2_sbd *sdp = rgd->rd_sbd;
624 spin_lock(&sdp->sd_rindex_spin);
625 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
627 spin_unlock(&sdp->sd_rindex_spin);
631 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
632 * @rgd: the struct gfs2_rgrpd describing the RG to read in
636 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
638 struct gfs2_sbd *sdp = rgd->rd_sbd;
639 int x, length = rgd->rd_ri.ri_length;
641 spin_lock(&sdp->sd_rindex_spin);
642 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
643 if (--rgd->rd_bh_count) {
644 spin_unlock(&sdp->sd_rindex_spin);
648 for (x = 0; x < length; x++) {
649 struct gfs2_bitmap *bi = rgd->rd_bits + x;
656 spin_unlock(&sdp->sd_rindex_spin);
659 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
661 struct gfs2_sbd *sdp = rgd->rd_sbd;
662 unsigned int length = rgd->rd_ri.ri_length;
665 for (x = 0; x < length; x++) {
666 struct gfs2_bitmap *bi = rgd->rd_bits + x;
669 memcpy(bi->bi_clone + bi->bi_offset,
670 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
673 spin_lock(&sdp->sd_rindex_spin);
674 rgd->rd_free_clone = rgd->rd_rg.rg_free;
675 spin_unlock(&sdp->sd_rindex_spin);
679 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
680 * @ip: the incore GFS2 inode structure
682 * Returns: the struct gfs2_alloc
685 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
687 struct gfs2_alloc *al = &ip->i_alloc;
689 /* FIXME: Should assert that the correct locks are held here... */
690 memset(al, 0, sizeof(*al));
695 * try_rgrp_fit - See if a given reservation will fit in a given RG
697 * @al: the struct gfs2_alloc structure describing the reservation
699 * If there's room for the requested blocks to be allocated from the RG:
700 * Sets the $al_reserved_data field in @al.
701 * Sets the $al_reserved_meta field in @al.
702 * Sets the $al_rgd field in @al.
704 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
707 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
709 struct gfs2_sbd *sdp = rgd->rd_sbd;
712 spin_lock(&sdp->sd_rindex_spin);
713 if (rgd->rd_free_clone >= al->al_requested) {
717 spin_unlock(&sdp->sd_rindex_spin);
723 * recent_rgrp_first - get first RG from "recent" list
724 * @sdp: The GFS2 superblock
725 * @rglast: address of the rgrp used last
727 * Returns: The first rgrp in the recent list
730 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
733 struct gfs2_rgrpd *rgd = NULL;
735 spin_lock(&sdp->sd_rindex_spin);
737 if (list_empty(&sdp->sd_rindex_recent_list))
743 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
744 if (rgd->rd_ri.ri_addr == rglast)
749 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
752 spin_unlock(&sdp->sd_rindex_spin);
757 * recent_rgrp_next - get next RG from "recent" list
758 * @cur_rgd: current rgrp
761 * Returns: The next rgrp in the recent list
764 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
767 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
768 struct list_head *head;
769 struct gfs2_rgrpd *rgd;
771 spin_lock(&sdp->sd_rindex_spin);
773 head = &sdp->sd_rindex_recent_list;
775 list_for_each_entry(rgd, head, rd_recent) {
776 if (rgd == cur_rgd) {
777 if (cur_rgd->rd_recent.next != head)
778 rgd = list_entry(cur_rgd->rd_recent.next,
779 struct gfs2_rgrpd, rd_recent);
784 list_del(&cur_rgd->rd_recent);
791 if (!list_empty(head))
792 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
795 spin_unlock(&sdp->sd_rindex_spin);
800 * recent_rgrp_add - add an RG to tail of "recent" list
801 * @new_rgd: The rgrp to add
805 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
807 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
808 struct gfs2_rgrpd *rgd;
809 unsigned int count = 0;
810 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
812 spin_lock(&sdp->sd_rindex_spin);
814 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
821 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
824 spin_unlock(&sdp->sd_rindex_spin);
828 * forward_rgrp_get - get an rgrp to try next from full list
829 * @sdp: The GFS2 superblock
831 * Returns: The rgrp to try next
834 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
836 struct gfs2_rgrpd *rgd;
837 unsigned int journals = gfs2_jindex_size(sdp);
838 unsigned int rg = 0, x;
840 spin_lock(&sdp->sd_rindex_spin);
842 rgd = sdp->sd_rindex_forward;
844 if (sdp->sd_rgrps >= journals)
845 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
847 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
848 x++, rgd = gfs2_rgrpd_get_next(rgd))
851 sdp->sd_rindex_forward = rgd;
854 spin_unlock(&sdp->sd_rindex_spin);
860 * forward_rgrp_set - set the forward rgrp pointer
861 * @sdp: the filesystem
862 * @rgd: The new forward rgrp
866 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
868 spin_lock(&sdp->sd_rindex_spin);
869 sdp->sd_rindex_forward = rgd;
870 spin_unlock(&sdp->sd_rindex_spin);
874 * get_local_rgrp - Choose and lock a rgrp for allocation
875 * @ip: the inode to reserve space for
876 * @rgp: the chosen and locked rgrp
878 * Try to acquire rgrp in way which avoids contending with others.
883 static int get_local_rgrp(struct gfs2_inode *ip)
885 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
886 struct gfs2_rgrpd *rgd, *begin = NULL;
887 struct gfs2_alloc *al = &ip->i_alloc;
888 int flags = LM_FLAG_TRY;
893 /* Try recently successful rgrps */
895 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
898 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
899 LM_FLAG_TRY, &al->al_rgd_gh);
902 if (try_rgrp_fit(rgd, al))
904 gfs2_glock_dq_uninit(&al->al_rgd_gh);
905 rgd = recent_rgrp_next(rgd, 1);
909 rgd = recent_rgrp_next(rgd, 0);
917 /* Go through full list of rgrps */
919 begin = rgd = forward_rgrp_get(sdp);
922 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
926 if (try_rgrp_fit(rgd, al))
928 gfs2_glock_dq_uninit(&al->al_rgd_gh);
939 rgd = gfs2_rgrpd_get_next(rgd);
941 rgd = gfs2_rgrpd_get_first(sdp);
944 if (++loops >= 2 || !skipped)
951 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
954 recent_rgrp_add(rgd);
955 rgd = gfs2_rgrpd_get_next(rgd);
957 rgd = gfs2_rgrpd_get_first(sdp);
958 forward_rgrp_set(sdp, rgd);
965 * gfs2_inplace_reserve_i - Reserve space in the filesystem
966 * @ip: the inode to reserve space for
971 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
973 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
974 struct gfs2_alloc *al = &ip->i_alloc;
977 if (gfs2_assert_warn(sdp, al->al_requested))
980 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
984 error = get_local_rgrp(ip);
986 gfs2_glock_dq_uninit(&al->al_ri_gh);
997 * gfs2_inplace_release - release an inplace reservation
998 * @ip: the inode the reservation was taken out on
1000 * Release a reservation made by gfs2_inplace_reserve().
1003 void gfs2_inplace_release(struct gfs2_inode *ip)
1005 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1006 struct gfs2_alloc *al = &ip->i_alloc;
1008 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1009 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1010 "al_file = %s, al_line = %u\n",
1011 al->al_alloced, al->al_requested, al->al_file,
1015 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1016 gfs2_glock_dq_uninit(&al->al_ri_gh);
1020 * gfs2_get_block_type - Check a block in a RG is of given type
1021 * @rgd: the resource group holding the block
1022 * @block: the block number
1024 * Returns: The block type (GFS2_BLKST_*)
1027 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1029 struct gfs2_bitmap *bi = NULL;
1030 u32 length, rgrp_block, buf_block;
1034 length = rgd->rd_ri.ri_length;
1035 rgrp_block = block - rgd->rd_ri.ri_data0;
1037 for (buf = 0; buf < length; buf++) {
1038 bi = rgd->rd_bits + buf;
1039 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1043 gfs2_assert(rgd->rd_sbd, buf < length);
1044 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1046 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1047 bi->bi_len, buf_block);
1053 * rgblk_search - find a block in @old_state, change allocation
1054 * state to @new_state
1055 * @rgd: the resource group descriptor
1056 * @goal: the goal block within the RG (start here to search for avail block)
1057 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1058 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1060 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1061 * Add the found bitmap buffer to the transaction.
1062 * Set the found bits to @new_state to change block's allocation state.
1064 * This function never fails, because we wouldn't call it unless we
1065 * know (from reservation results, etc.) that a block is available.
1067 * Scope of @goal and returned block is just within rgrp, not the whole
1070 * Returns: the block number allocated
1073 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1074 unsigned char old_state, unsigned char new_state)
1076 struct gfs2_bitmap *bi = NULL;
1077 u32 length = rgd->rd_ri.ri_length;
1079 unsigned int buf, x;
1081 /* Find bitmap block that contains bits for goal block */
1082 for (buf = 0; buf < length; buf++) {
1083 bi = rgd->rd_bits + buf;
1084 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1088 gfs2_assert(rgd->rd_sbd, buf < length);
1090 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1091 goal -= bi->bi_start * GFS2_NBBY;
1093 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1094 "x <= length", instead of "x < length", because we typically start
1095 the search in the middle of a bit block, but if we can't find an
1096 allocatable block anywhere else, we want to be able wrap around and
1097 search in the first part of our first-searched bit block. */
1098 for (x = 0; x <= length; x++) {
1100 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
1101 bi->bi_len, goal, old_state);
1103 blk = gfs2_bitfit(rgd,
1104 bi->bi_bh->b_data + bi->bi_offset,
1105 bi->bi_len, goal, old_state);
1106 if (blk != BFITNOENT)
1109 /* Try next bitmap block (wrap back to rgrp header if at end) */
1110 buf = (buf + 1) % length;
1111 bi = rgd->rd_bits + buf;
1115 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1118 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1119 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1120 bi->bi_len, blk, new_state);
1122 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1123 bi->bi_len, blk, new_state);
1125 return bi->bi_start * GFS2_NBBY + blk;
1129 * rgblk_free - Change alloc state of given block(s)
1130 * @sdp: the filesystem
1131 * @bstart: the start of a run of blocks to free
1132 * @blen: the length of the block run (all must lie within ONE RG!)
1133 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1135 * Returns: Resource group containing the block(s)
1138 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1139 u32 blen, unsigned char new_state)
1141 struct gfs2_rgrpd *rgd;
1142 struct gfs2_bitmap *bi = NULL;
1143 u32 length, rgrp_blk, buf_blk;
1146 rgd = gfs2_blk2rgrpd(sdp, bstart);
1148 if (gfs2_consist(sdp))
1149 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1153 length = rgd->rd_ri.ri_length;
1155 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1158 for (buf = 0; buf < length; buf++) {
1159 bi = rgd->rd_bits + buf;
1160 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1164 gfs2_assert(rgd->rd_sbd, buf < length);
1166 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1169 if (!bi->bi_clone) {
1170 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1171 GFP_NOFS | __GFP_NOFAIL);
1172 memcpy(bi->bi_clone + bi->bi_offset,
1173 bi->bi_bh->b_data + bi->bi_offset,
1176 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1177 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1178 bi->bi_len, buf_blk, new_state);
1185 * gfs2_alloc_data - Allocate a data block
1186 * @ip: the inode to allocate the data block for
1188 * Returns: the allocated block
1191 u64 gfs2_alloc_data(struct gfs2_inode *ip)
1193 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1194 struct gfs2_alloc *al = &ip->i_alloc;
1195 struct gfs2_rgrpd *rgd = al->al_rgd;
1199 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1200 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1202 goal = rgd->rd_last_alloc_data;
1204 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1205 rgd->rd_last_alloc_data = blk;
1207 block = rgd->rd_ri.ri_data0 + blk;
1208 ip->i_di.di_goal_data = block;
1210 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1211 rgd->rd_rg.rg_free--;
1213 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1214 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1218 gfs2_statfs_change(sdp, 0, -1, 0);
1219 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1221 spin_lock(&sdp->sd_rindex_spin);
1222 rgd->rd_free_clone--;
1223 spin_unlock(&sdp->sd_rindex_spin);
1229 * gfs2_alloc_meta - Allocate a metadata block
1230 * @ip: the inode to allocate the metadata block for
1232 * Returns: the allocated block
1235 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
1237 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1238 struct gfs2_alloc *al = &ip->i_alloc;
1239 struct gfs2_rgrpd *rgd = al->al_rgd;
1243 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1244 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1246 goal = rgd->rd_last_alloc_meta;
1248 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1249 rgd->rd_last_alloc_meta = blk;
1251 block = rgd->rd_ri.ri_data0 + blk;
1252 ip->i_di.di_goal_meta = block;
1254 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1255 rgd->rd_rg.rg_free--;
1257 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1258 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1262 gfs2_statfs_change(sdp, 0, -1, 0);
1263 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1264 gfs2_trans_add_unrevoke(sdp, block);
1266 spin_lock(&sdp->sd_rindex_spin);
1267 rgd->rd_free_clone--;
1268 spin_unlock(&sdp->sd_rindex_spin);
1274 * gfs2_alloc_di - Allocate a dinode
1275 * @dip: the directory that the inode is going in
1277 * Returns: the block allocated
1280 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1282 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1283 struct gfs2_alloc *al = &dip->i_alloc;
1284 struct gfs2_rgrpd *rgd = al->al_rgd;
1288 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1289 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1291 rgd->rd_last_alloc_meta = blk;
1293 block = rgd->rd_ri.ri_data0 + blk;
1295 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1296 rgd->rd_rg.rg_free--;
1297 rgd->rd_rg.rg_dinodes++;
1298 *generation = rgd->rd_rg.rg_igeneration++;
1299 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1300 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1304 gfs2_statfs_change(sdp, 0, -1, +1);
1305 gfs2_trans_add_unrevoke(sdp, block);
1307 spin_lock(&sdp->sd_rindex_spin);
1308 rgd->rd_free_clone--;
1309 spin_unlock(&sdp->sd_rindex_spin);
1315 * gfs2_free_data - free a contiguous run of data block(s)
1316 * @ip: the inode these blocks are being freed from
1317 * @bstart: first block of a run of contiguous blocks
1318 * @blen: the length of the block run
1322 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1324 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1325 struct gfs2_rgrpd *rgd;
1327 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1331 rgd->rd_rg.rg_free += blen;
1333 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1334 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1336 gfs2_trans_add_rg(rgd);
1338 gfs2_statfs_change(sdp, 0, +blen, 0);
1339 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1343 * gfs2_free_meta - free a contiguous run of data block(s)
1344 * @ip: the inode these blocks are being freed from
1345 * @bstart: first block of a run of contiguous blocks
1346 * @blen: the length of the block run
1350 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1352 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1353 struct gfs2_rgrpd *rgd;
1355 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1359 rgd->rd_rg.rg_free += blen;
1361 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1362 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1364 gfs2_trans_add_rg(rgd);
1366 gfs2_statfs_change(sdp, 0, +blen, 0);
1367 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1368 gfs2_meta_wipe(ip, bstart, blen);
1371 void gfs2_unlink_di(struct inode *inode)
1373 struct gfs2_inode *ip = GFS2_I(inode);
1374 struct gfs2_sbd *sdp = GFS2_SB(inode);
1375 struct gfs2_rgrpd *rgd;
1376 u64 blkno = ip->i_num.no_addr;
1378 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1381 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1382 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1383 gfs2_trans_add_rg(rgd);
1386 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1388 struct gfs2_sbd *sdp = rgd->rd_sbd;
1389 struct gfs2_rgrpd *tmp_rgd;
1391 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1394 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1396 if (!rgd->rd_rg.rg_dinodes)
1397 gfs2_consist_rgrpd(rgd);
1398 rgd->rd_rg.rg_dinodes--;
1399 rgd->rd_rg.rg_free++;
1401 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1402 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1404 gfs2_statfs_change(sdp, 0, +1, -1);
1405 gfs2_trans_add_rg(rgd);
1409 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1411 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
1412 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1413 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1417 * gfs2_rlist_add - add a RG to a list of RGs
1418 * @sdp: the filesystem
1419 * @rlist: the list of resource groups
1422 * Figure out what RG a block belongs to and add that RG to the list
1424 * FIXME: Don't use NOFAIL
1428 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1431 struct gfs2_rgrpd *rgd;
1432 struct gfs2_rgrpd **tmp;
1433 unsigned int new_space;
1436 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1439 rgd = gfs2_blk2rgrpd(sdp, block);
1441 if (gfs2_consist(sdp))
1442 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1446 for (x = 0; x < rlist->rl_rgrps; x++)
1447 if (rlist->rl_rgd[x] == rgd)
1450 if (rlist->rl_rgrps == rlist->rl_space) {
1451 new_space = rlist->rl_space + 10;
1453 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1454 GFP_NOFS | __GFP_NOFAIL);
1456 if (rlist->rl_rgd) {
1457 memcpy(tmp, rlist->rl_rgd,
1458 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1459 kfree(rlist->rl_rgd);
1462 rlist->rl_space = new_space;
1463 rlist->rl_rgd = tmp;
1466 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1470 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1471 * and initialize an array of glock holders for them
1472 * @rlist: the list of resource groups
1473 * @state: the lock state to acquire the RG lock in
1474 * @flags: the modifier flags for the holder structures
1476 * FIXME: Don't use NOFAIL
1480 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1485 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1486 GFP_NOFS | __GFP_NOFAIL);
1487 for (x = 0; x < rlist->rl_rgrps; x++)
1488 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1494 * gfs2_rlist_free - free a resource group list
1495 * @list: the list of resource groups
1499 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1503 kfree(rlist->rl_rgd);
1505 if (rlist->rl_ghs) {
1506 for (x = 0; x < rlist->rl_rgrps; x++)
1507 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1508 kfree(rlist->rl_ghs);