2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 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 #include "ops_address.h"
33 #define BFITNOENT ((u32)~0)
34 #define NO_BLOCK ((u64)~0)
37 * These routines are used by the resource group routines (rgrp.c)
38 * to keep track of block allocation. Each block is represented by two
39 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
42 * 1 = Used (not metadata)
43 * 2 = Unlinked (still in use) inode
47 static const char valid_change[16] = {
55 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
56 unsigned char old_state, unsigned char new_state,
60 * gfs2_setbit - Set a bit in the bitmaps
61 * @buffer: the buffer that holds the bitmaps
62 * @buflen: the length (in bytes) of the buffer
63 * @block: the block to set
64 * @new_state: the new state of the block
68 static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
69 unsigned char *buf2, unsigned int offset,
70 unsigned int buflen, u32 block,
71 unsigned char new_state)
73 unsigned char *byte1, *byte2, *end, cur_state;
74 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
76 byte1 = buf1 + offset + (block / GFS2_NBBY);
77 end = buf1 + offset + buflen;
81 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
83 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
84 gfs2_consist_rgrpd(rgd);
87 *byte1 ^= (cur_state ^ new_state) << bit;
90 byte2 = buf2 + offset + (block / GFS2_NBBY);
91 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
92 *byte2 ^= (cur_state ^ new_state) << bit;
97 * gfs2_testbit - test a bit in the bitmaps
98 * @buffer: the buffer that holds the bitmaps
99 * @buflen: the length (in bytes) of the buffer
100 * @block: the block to read
104 static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
105 const unsigned char *buffer,
106 unsigned int buflen, u32 block)
108 const unsigned char *byte, *end;
109 unsigned char cur_state;
112 byte = buffer + (block / GFS2_NBBY);
113 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
114 end = buffer + buflen;
116 gfs2_assert(rgd->rd_sbd, byte < end);
118 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
124 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
125 * a block in a given allocation state.
126 * @buffer: the buffer that holds the bitmaps
127 * @buflen: the length (in bytes) of the buffer
128 * @goal: start search at this block's bit-pair (within @buffer)
129 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for.
131 * Scope of @goal and returned block number is only within this bitmap buffer,
132 * not entire rgrp or filesystem. @buffer will be offset from the actual
133 * beginning of a bitmap block buffer, skipping any header structures.
135 * Return: the block number (bitmap buffer scope) that was found
138 static u32 gfs2_bitfit(const u8 *buffer, unsigned int buflen, u32 goal,
143 unsigned int bit, bitlong;
144 const unsigned long *plong;
145 #if BITS_PER_LONG == 32
146 const unsigned long plong55 = 0x55555555;
148 const unsigned long plong55 = 0x5555555555555555;
151 byte = buffer + (goal / GFS2_NBBY);
152 plong = (const unsigned long *)(buffer + (goal / GFS2_NBBY));
153 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
156 while (byte < buffer + buflen) {
158 if (bitlong == 0 && old_state == 0 && *plong == plong55) {
160 byte += sizeof(unsigned long);
161 blk += sizeof(unsigned long) * GFS2_NBBY;
164 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
166 bit += GFS2_BIT_SIZE;
171 bitlong += GFS2_BIT_SIZE;
172 if (bitlong >= sizeof(unsigned long) * 8) {
184 * gfs2_bitcount - count the number of bits in a certain state
185 * @buffer: the buffer that holds the bitmaps
186 * @buflen: the length (in bytes) of the buffer
187 * @state: the state of the block we're looking for
189 * Returns: The number of bits
192 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
193 unsigned int buflen, u8 state)
195 const u8 *byte = buffer;
196 const u8 *end = buffer + buflen;
197 const u8 state1 = state << 2;
198 const u8 state2 = state << 4;
199 const u8 state3 = state << 6;
202 for (; byte < end; byte++) {
203 if (((*byte) & 0x03) == state)
205 if (((*byte) & 0x0C) == state1)
207 if (((*byte) & 0x30) == state2)
209 if (((*byte) & 0xC0) == state3)
217 * gfs2_rgrp_verify - Verify that a resource group is consistent
218 * @sdp: the filesystem
223 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
225 struct gfs2_sbd *sdp = rgd->rd_sbd;
226 struct gfs2_bitmap *bi = NULL;
227 u32 length = rgd->rd_length;
231 memset(count, 0, 4 * sizeof(u32));
233 /* Count # blocks in each of 4 possible allocation states */
234 for (buf = 0; buf < length; buf++) {
235 bi = rgd->rd_bits + buf;
236 for (x = 0; x < 4; x++)
237 count[x] += gfs2_bitcount(rgd,
243 if (count[0] != rgd->rd_rg.rg_free) {
244 if (gfs2_consist_rgrpd(rgd))
245 fs_err(sdp, "free data mismatch: %u != %u\n",
246 count[0], rgd->rd_rg.rg_free);
252 rgd->rd_rg.rg_dinodes;
253 if (count[1] + count[2] != tmp) {
254 if (gfs2_consist_rgrpd(rgd))
255 fs_err(sdp, "used data mismatch: %u != %u\n",
260 if (count[3] != rgd->rd_rg.rg_dinodes) {
261 if (gfs2_consist_rgrpd(rgd))
262 fs_err(sdp, "used metadata mismatch: %u != %u\n",
263 count[3], rgd->rd_rg.rg_dinodes);
267 if (count[2] > count[3]) {
268 if (gfs2_consist_rgrpd(rgd))
269 fs_err(sdp, "unlinked inodes > inodes: %u\n",
276 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
278 u64 first = rgd->rd_data0;
279 u64 last = first + rgd->rd_data;
280 return first <= block && block < last;
284 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
285 * @sdp: The GFS2 superblock
286 * @n: The data block number
288 * Returns: The resource group, or NULL if not found
291 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
293 struct gfs2_rgrpd *rgd;
295 spin_lock(&sdp->sd_rindex_spin);
297 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
298 if (rgrp_contains_block(rgd, blk)) {
299 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
300 spin_unlock(&sdp->sd_rindex_spin);
305 spin_unlock(&sdp->sd_rindex_spin);
311 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
312 * @sdp: The GFS2 superblock
314 * Returns: The first rgrp in the filesystem
317 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
319 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
320 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
324 * gfs2_rgrpd_get_next - get the next RG
327 * Returns: The next rgrp
330 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
332 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
334 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
337 static void clear_rgrpdi(struct gfs2_sbd *sdp)
339 struct list_head *head;
340 struct gfs2_rgrpd *rgd;
341 struct gfs2_glock *gl;
343 spin_lock(&sdp->sd_rindex_spin);
344 sdp->sd_rindex_forward = NULL;
345 head = &sdp->sd_rindex_recent_list;
346 while (!list_empty(head)) {
347 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
348 list_del(&rgd->rd_recent);
350 spin_unlock(&sdp->sd_rindex_spin);
352 head = &sdp->sd_rindex_list;
353 while (!list_empty(head)) {
354 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
357 list_del(&rgd->rd_list);
358 list_del(&rgd->rd_list_mru);
361 gl->gl_object = NULL;
366 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
370 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
372 mutex_lock(&sdp->sd_rindex_mutex);
374 mutex_unlock(&sdp->sd_rindex_mutex);
377 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
379 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
380 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
381 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
382 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
383 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
387 * gfs2_compute_bitstructs - Compute the bitmap sizes
388 * @rgd: The resource group descriptor
390 * Calculates bitmap descriptors, one for each block that contains bitmap data
395 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
397 struct gfs2_sbd *sdp = rgd->rd_sbd;
398 struct gfs2_bitmap *bi;
399 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
400 u32 bytes_left, bytes;
406 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
410 bytes_left = rgd->rd_bitbytes;
412 for (x = 0; x < length; x++) {
413 bi = rgd->rd_bits + x;
415 /* small rgrp; bitmap stored completely in header block */
418 bi->bi_offset = sizeof(struct gfs2_rgrp);
423 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
424 bi->bi_offset = sizeof(struct gfs2_rgrp);
428 } else if (x + 1 == length) {
430 bi->bi_offset = sizeof(struct gfs2_meta_header);
431 bi->bi_start = rgd->rd_bitbytes - bytes_left;
435 bytes = sdp->sd_sb.sb_bsize -
436 sizeof(struct gfs2_meta_header);
437 bi->bi_offset = sizeof(struct gfs2_meta_header);
438 bi->bi_start = rgd->rd_bitbytes - bytes_left;
446 gfs2_consist_rgrpd(rgd);
449 bi = rgd->rd_bits + (length - 1);
450 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
451 if (gfs2_consist_rgrpd(rgd)) {
452 gfs2_rindex_print(rgd);
453 fs_err(sdp, "start=%u len=%u offset=%u\n",
454 bi->bi_start, bi->bi_len, bi->bi_offset);
463 * gfs2_ri_total - Total up the file system space, according to the rindex.
466 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
469 struct inode *inode = sdp->sd_rindex;
470 struct gfs2_inode *ip = GFS2_I(inode);
471 char buf[sizeof(struct gfs2_rindex)];
472 struct file_ra_state ra_state;
475 mutex_lock(&sdp->sd_rindex_mutex);
476 file_ra_state_init(&ra_state, inode->i_mapping);
477 for (rgrps = 0;; rgrps++) {
478 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
480 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
482 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
483 sizeof(struct gfs2_rindex));
484 if (error != sizeof(struct gfs2_rindex))
486 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
488 mutex_unlock(&sdp->sd_rindex_mutex);
492 static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
494 const struct gfs2_rindex *str = buf;
496 rgd->rd_addr = be64_to_cpu(str->ri_addr);
497 rgd->rd_length = be32_to_cpu(str->ri_length);
498 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
499 rgd->rd_data = be32_to_cpu(str->ri_data);
500 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
504 * read_rindex_entry - Pull in a new resource index entry from the disk
505 * @gl: The glock covering the rindex inode
507 * Returns: 0 on success, error code otherwise
510 static int read_rindex_entry(struct gfs2_inode *ip,
511 struct file_ra_state *ra_state)
513 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
514 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
515 char buf[sizeof(struct gfs2_rindex)];
517 struct gfs2_rgrpd *rgd;
519 error = gfs2_internal_read(ip, ra_state, buf, &pos,
520 sizeof(struct gfs2_rindex));
523 if (error != sizeof(struct gfs2_rindex)) {
529 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
534 mutex_init(&rgd->rd_mutex);
535 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
538 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
539 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
541 gfs2_rindex_in(rgd, buf);
542 error = compute_bitstructs(rgd);
546 error = gfs2_glock_get(sdp, rgd->rd_addr,
547 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
551 rgd->rd_gl->gl_object = rgd;
552 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
553 rgd->rd_flags |= GFS2_RDF_CHECK;
558 * gfs2_ri_update - Pull in a new resource index from the disk
559 * @ip: pointer to the rindex inode
561 * Returns: 0 on successful update, error code otherwise
564 static int gfs2_ri_update(struct gfs2_inode *ip)
566 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
567 struct inode *inode = &ip->i_inode;
568 struct file_ra_state ra_state;
569 u64 rgrp_count = ip->i_di.di_size;
572 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
573 gfs2_consist_inode(ip);
579 file_ra_state_init(&ra_state, inode->i_mapping);
580 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
581 error = read_rindex_entry(ip, &ra_state);
588 sdp->sd_rindex_uptodate = 1;
593 * gfs2_ri_update_special - Pull in a new resource index from the disk
595 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
596 * In this case we know that we don't have any resource groups in memory yet.
598 * @ip: pointer to the rindex inode
600 * Returns: 0 on successful update, error code otherwise
602 static int gfs2_ri_update_special(struct gfs2_inode *ip)
604 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
605 struct inode *inode = &ip->i_inode;
606 struct file_ra_state ra_state;
609 file_ra_state_init(&ra_state, inode->i_mapping);
610 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
611 /* Ignore partials */
612 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
615 error = read_rindex_entry(ip, &ra_state);
622 sdp->sd_rindex_uptodate = 1;
627 * gfs2_rindex_hold - Grab a lock on the rindex
628 * @sdp: The GFS2 superblock
629 * @ri_gh: the glock holder
631 * We grab a lock on the rindex inode to make sure that it doesn't
632 * change whilst we are performing an operation. We keep this lock
633 * for quite long periods of time compared to other locks. This
634 * doesn't matter, since it is shared and it is very, very rarely
635 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
637 * This makes sure that we're using the latest copy of the resource index
638 * special file, which might have been updated if someone expanded the
639 * filesystem (via gfs2_grow utility), which adds new resource groups.
641 * Returns: 0 on success, error code otherwise
644 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
646 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
647 struct gfs2_glock *gl = ip->i_gl;
650 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
654 /* Read new copy from disk if we don't have the latest */
655 if (!sdp->sd_rindex_uptodate) {
656 mutex_lock(&sdp->sd_rindex_mutex);
657 if (!sdp->sd_rindex_uptodate) {
658 error = gfs2_ri_update(ip);
660 gfs2_glock_dq_uninit(ri_gh);
662 mutex_unlock(&sdp->sd_rindex_mutex);
668 static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
670 const struct gfs2_rgrp *str = buf;
671 struct gfs2_rgrp_host *rg = &rgd->rd_rg;
674 rg_flags = be32_to_cpu(str->rg_flags);
675 if (rg_flags & GFS2_RGF_NOALLOC)
676 rgd->rd_flags |= GFS2_RDF_NOALLOC;
678 rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
679 rg->rg_free = be32_to_cpu(str->rg_free);
680 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
681 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
684 static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
686 struct gfs2_rgrp *str = buf;
687 struct gfs2_rgrp_host *rg = &rgd->rd_rg;
690 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
691 rg_flags |= GFS2_RGF_NOALLOC;
692 str->rg_flags = cpu_to_be32(rg_flags);
693 str->rg_free = cpu_to_be32(rg->rg_free);
694 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
695 str->__pad = cpu_to_be32(0);
696 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
697 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
701 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
702 * @rgd: the struct gfs2_rgrpd describing the RG to read in
704 * Read in all of a Resource Group's header and bitmap blocks.
705 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
710 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
712 struct gfs2_sbd *sdp = rgd->rd_sbd;
713 struct gfs2_glock *gl = rgd->rd_gl;
714 unsigned int length = rgd->rd_length;
715 struct gfs2_bitmap *bi;
719 mutex_lock(&rgd->rd_mutex);
721 spin_lock(&sdp->sd_rindex_spin);
722 if (rgd->rd_bh_count) {
724 spin_unlock(&sdp->sd_rindex_spin);
725 mutex_unlock(&rgd->rd_mutex);
728 spin_unlock(&sdp->sd_rindex_spin);
730 for (x = 0; x < length; x++) {
731 bi = rgd->rd_bits + x;
732 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
737 for (y = length; y--;) {
738 bi = rgd->rd_bits + y;
739 error = gfs2_meta_wait(sdp, bi->bi_bh);
742 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
749 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
750 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
751 rgd->rd_flags |= GFS2_RDF_UPTODATE;
754 spin_lock(&sdp->sd_rindex_spin);
755 rgd->rd_free_clone = rgd->rd_rg.rg_free;
757 spin_unlock(&sdp->sd_rindex_spin);
759 mutex_unlock(&rgd->rd_mutex);
765 bi = rgd->rd_bits + x;
768 gfs2_assert_warn(sdp, !bi->bi_clone);
770 mutex_unlock(&rgd->rd_mutex);
775 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
777 struct gfs2_sbd *sdp = rgd->rd_sbd;
779 spin_lock(&sdp->sd_rindex_spin);
780 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
782 spin_unlock(&sdp->sd_rindex_spin);
786 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
787 * @rgd: the struct gfs2_rgrpd describing the RG to read in
791 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
793 struct gfs2_sbd *sdp = rgd->rd_sbd;
794 int x, length = rgd->rd_length;
796 spin_lock(&sdp->sd_rindex_spin);
797 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
798 if (--rgd->rd_bh_count) {
799 spin_unlock(&sdp->sd_rindex_spin);
803 for (x = 0; x < length; x++) {
804 struct gfs2_bitmap *bi = rgd->rd_bits + x;
811 spin_unlock(&sdp->sd_rindex_spin);
814 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
816 struct gfs2_sbd *sdp = rgd->rd_sbd;
817 unsigned int length = rgd->rd_length;
820 for (x = 0; x < length; x++) {
821 struct gfs2_bitmap *bi = rgd->rd_bits + x;
824 memcpy(bi->bi_clone + bi->bi_offset,
825 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
828 spin_lock(&sdp->sd_rindex_spin);
829 rgd->rd_free_clone = rgd->rd_rg.rg_free;
830 spin_unlock(&sdp->sd_rindex_spin);
834 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
835 * @ip: the incore GFS2 inode structure
837 * Returns: the struct gfs2_alloc
840 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
842 BUG_ON(ip->i_alloc != NULL);
843 ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
848 * try_rgrp_fit - See if a given reservation will fit in a given RG
850 * @al: the struct gfs2_alloc structure describing the reservation
852 * If there's room for the requested blocks to be allocated from the RG:
853 * Sets the $al_rgd field in @al.
855 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
858 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
860 struct gfs2_sbd *sdp = rgd->rd_sbd;
863 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
866 spin_lock(&sdp->sd_rindex_spin);
867 if (rgd->rd_free_clone >= al->al_requested) {
871 spin_unlock(&sdp->sd_rindex_spin);
877 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
880 * Returns: The inode, if one has been found
883 static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
888 struct gfs2_sbd *sdp = rgd->rd_sbd;
892 if (goal >= rgd->rd_data)
894 down_write(&sdp->sd_log_flush_lock);
896 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
897 GFS2_BLKST_UNLINKED, &n);
898 up_write(&sdp->sd_log_flush_lock);
899 if (block == BFITNOENT)
901 /* rgblk_search can return a block < goal, so we need to
902 keep it marching forward. */
903 no_addr = block + rgd->rd_data0;
905 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
907 *last_unlinked = no_addr;
908 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
914 rgd->rd_flags &= ~GFS2_RDF_CHECK;
919 * recent_rgrp_first - get first RG from "recent" list
920 * @sdp: The GFS2 superblock
921 * @rglast: address of the rgrp used last
923 * Returns: The first rgrp in the recent list
926 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
929 struct gfs2_rgrpd *rgd;
931 spin_lock(&sdp->sd_rindex_spin);
934 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
935 if (rgrp_contains_block(rgd, rglast))
940 if (!list_empty(&sdp->sd_rindex_recent_list))
941 rgd = list_entry(sdp->sd_rindex_recent_list.next,
942 struct gfs2_rgrpd, rd_recent);
944 spin_unlock(&sdp->sd_rindex_spin);
949 * recent_rgrp_next - get next RG from "recent" list
950 * @cur_rgd: current rgrp
953 * Returns: The next rgrp in the recent list
956 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
959 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
960 struct list_head *head;
961 struct gfs2_rgrpd *rgd;
963 spin_lock(&sdp->sd_rindex_spin);
965 head = &sdp->sd_rindex_recent_list;
967 list_for_each_entry(rgd, head, rd_recent) {
968 if (rgd == cur_rgd) {
969 if (cur_rgd->rd_recent.next != head)
970 rgd = list_entry(cur_rgd->rd_recent.next,
971 struct gfs2_rgrpd, rd_recent);
976 list_del(&cur_rgd->rd_recent);
983 if (!list_empty(head))
984 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
987 spin_unlock(&sdp->sd_rindex_spin);
992 * recent_rgrp_add - add an RG to tail of "recent" list
993 * @new_rgd: The rgrp to add
997 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
999 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
1000 struct gfs2_rgrpd *rgd;
1001 unsigned int count = 0;
1002 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
1004 spin_lock(&sdp->sd_rindex_spin);
1006 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
1013 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
1016 spin_unlock(&sdp->sd_rindex_spin);
1020 * forward_rgrp_get - get an rgrp to try next from full list
1021 * @sdp: The GFS2 superblock
1023 * Returns: The rgrp to try next
1026 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1028 struct gfs2_rgrpd *rgd;
1029 unsigned int journals = gfs2_jindex_size(sdp);
1030 unsigned int rg = 0, x;
1032 spin_lock(&sdp->sd_rindex_spin);
1034 rgd = sdp->sd_rindex_forward;
1036 if (sdp->sd_rgrps >= journals)
1037 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1039 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
1040 x++, rgd = gfs2_rgrpd_get_next(rgd))
1043 sdp->sd_rindex_forward = rgd;
1046 spin_unlock(&sdp->sd_rindex_spin);
1052 * forward_rgrp_set - set the forward rgrp pointer
1053 * @sdp: the filesystem
1054 * @rgd: The new forward rgrp
1058 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1060 spin_lock(&sdp->sd_rindex_spin);
1061 sdp->sd_rindex_forward = rgd;
1062 spin_unlock(&sdp->sd_rindex_spin);
1066 * get_local_rgrp - Choose and lock a rgrp for allocation
1067 * @ip: the inode to reserve space for
1068 * @rgp: the chosen and locked rgrp
1070 * Try to acquire rgrp in way which avoids contending with others.
1075 static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
1077 struct inode *inode = NULL;
1078 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1079 struct gfs2_rgrpd *rgd, *begin = NULL;
1080 struct gfs2_alloc *al = ip->i_alloc;
1081 int flags = LM_FLAG_TRY;
1084 int error, rg_locked;
1086 /* Try recently successful rgrps */
1088 rgd = recent_rgrp_first(sdp, ip->i_goal);
1093 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1097 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1098 LM_FLAG_TRY, &al->al_rgd_gh);
1102 if (try_rgrp_fit(rgd, al))
1104 if (rgd->rd_flags & GFS2_RDF_CHECK)
1105 inode = try_rgrp_unlink(rgd, last_unlinked);
1107 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1110 rgd = recent_rgrp_next(rgd, 1);
1114 rgd = recent_rgrp_next(rgd, 0);
1118 return ERR_PTR(error);
1122 /* Go through full list of rgrps */
1124 begin = rgd = forward_rgrp_get(sdp);
1129 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1133 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1138 if (try_rgrp_fit(rgd, al))
1140 if (rgd->rd_flags & GFS2_RDF_CHECK)
1141 inode = try_rgrp_unlink(rgd, last_unlinked);
1143 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1153 return ERR_PTR(error);
1156 rgd = gfs2_rgrpd_get_next(rgd);
1158 rgd = gfs2_rgrpd_get_first(sdp);
1162 return ERR_PTR(-ENOSPC);
1167 gfs2_log_flush(sdp, NULL);
1173 recent_rgrp_add(rgd);
1174 rgd = gfs2_rgrpd_get_next(rgd);
1176 rgd = gfs2_rgrpd_get_first(sdp);
1177 forward_rgrp_set(sdp, rgd);
1184 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1185 * @ip: the inode to reserve space for
1190 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1192 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1193 struct gfs2_alloc *al = ip->i_alloc;
1194 struct inode *inode;
1196 u64 last_unlinked = NO_BLOCK;
1198 if (gfs2_assert_warn(sdp, al->al_requested))
1202 /* We need to hold the rindex unless the inode we're using is
1203 the rindex itself, in which case it's already held. */
1204 if (ip != GFS2_I(sdp->sd_rindex))
1205 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1206 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1207 error = gfs2_ri_update_special(ip);
1212 inode = get_local_rgrp(ip, &last_unlinked);
1214 if (ip != GFS2_I(sdp->sd_rindex))
1215 gfs2_glock_dq_uninit(&al->al_ri_gh);
1217 return PTR_ERR(inode);
1219 gfs2_log_flush(sdp, NULL);
1230 * gfs2_inplace_release - release an inplace reservation
1231 * @ip: the inode the reservation was taken out on
1233 * Release a reservation made by gfs2_inplace_reserve().
1236 void gfs2_inplace_release(struct gfs2_inode *ip)
1238 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1239 struct gfs2_alloc *al = ip->i_alloc;
1241 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1242 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1243 "al_file = %s, al_line = %u\n",
1244 al->al_alloced, al->al_requested, al->al_file,
1248 if (al->al_rgd_gh.gh_gl)
1249 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1250 if (ip != GFS2_I(sdp->sd_rindex))
1251 gfs2_glock_dq_uninit(&al->al_ri_gh);
1255 * gfs2_get_block_type - Check a block in a RG is of given type
1256 * @rgd: the resource group holding the block
1257 * @block: the block number
1259 * Returns: The block type (GFS2_BLKST_*)
1262 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1264 struct gfs2_bitmap *bi = NULL;
1265 u32 length, rgrp_block, buf_block;
1269 length = rgd->rd_length;
1270 rgrp_block = block - rgd->rd_data0;
1272 for (buf = 0; buf < length; buf++) {
1273 bi = rgd->rd_bits + buf;
1274 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1278 gfs2_assert(rgd->rd_sbd, buf < length);
1279 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1281 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1282 bi->bi_len, buf_block);
1288 * rgblk_search - find a block in @old_state, change allocation
1289 * state to @new_state
1290 * @rgd: the resource group descriptor
1291 * @goal: the goal block within the RG (start here to search for avail block)
1292 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1293 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1294 * @n: The extent length
1296 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1297 * Add the found bitmap buffer to the transaction.
1298 * Set the found bits to @new_state to change block's allocation state.
1300 * This function never fails, because we wouldn't call it unless we
1301 * know (from reservation results, etc.) that a block is available.
1303 * Scope of @goal and returned block is just within rgrp, not the whole
1306 * Returns: the block number allocated
1309 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1310 unsigned char old_state, unsigned char new_state,
1313 struct gfs2_bitmap *bi = NULL;
1314 const u32 length = rgd->rd_length;
1316 unsigned int buf, x;
1317 const unsigned int elen = *n;
1321 /* Find bitmap block that contains bits for goal block */
1322 for (buf = 0; buf < length; buf++) {
1323 bi = rgd->rd_bits + buf;
1324 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1328 gfs2_assert(rgd->rd_sbd, buf < length);
1330 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1331 goal -= bi->bi_start * GFS2_NBBY;
1333 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1334 "x <= length", instead of "x < length", because we typically start
1335 the search in the middle of a bit block, but if we can't find an
1336 allocatable block anywhere else, we want to be able wrap around and
1337 search in the first part of our first-searched bit block. */
1338 for (x = 0; x <= length; x++) {
1339 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1340 bitmaps, so we must search the originals for that. */
1341 buffer = bi->bi_bh->b_data + bi->bi_offset;
1342 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
1343 buffer = bi->bi_clone + bi->bi_offset;
1345 blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
1346 if (blk != BFITNOENT)
1349 /* Try next bitmap block (wrap back to rgrp header if at end) */
1350 buf = (buf + 1) % length;
1351 bi = rgd->rd_bits + buf;
1355 if (blk != BFITNOENT && old_state != new_state) {
1357 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1358 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1359 bi->bi_len, blk, new_state);
1362 if (goal >= (bi->bi_len / GFS2_NBBY))
1364 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1368 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
1369 bi->bi_offset, bi->bi_len, blk, new_state);
1373 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
1377 * rgblk_free - Change alloc state of given block(s)
1378 * @sdp: the filesystem
1379 * @bstart: the start of a run of blocks to free
1380 * @blen: the length of the block run (all must lie within ONE RG!)
1381 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1383 * Returns: Resource group containing the block(s)
1386 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1387 u32 blen, unsigned char new_state)
1389 struct gfs2_rgrpd *rgd;
1390 struct gfs2_bitmap *bi = NULL;
1391 u32 length, rgrp_blk, buf_blk;
1394 rgd = gfs2_blk2rgrpd(sdp, bstart);
1396 if (gfs2_consist(sdp))
1397 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1401 length = rgd->rd_length;
1403 rgrp_blk = bstart - rgd->rd_data0;
1406 for (buf = 0; buf < length; buf++) {
1407 bi = rgd->rd_bits + buf;
1408 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1412 gfs2_assert(rgd->rd_sbd, buf < length);
1414 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1417 if (!bi->bi_clone) {
1418 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1419 GFP_NOFS | __GFP_NOFAIL);
1420 memcpy(bi->bi_clone + bi->bi_offset,
1421 bi->bi_bh->b_data + bi->bi_offset,
1424 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1425 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
1426 bi->bi_len, buf_blk, new_state);
1433 * gfs2_alloc_block - Allocate a block
1434 * @ip: the inode to allocate the block for
1436 * Returns: the allocated block
1439 u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
1441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1442 struct gfs2_alloc *al = ip->i_alloc;
1443 struct gfs2_rgrpd *rgd = al->al_rgd;
1447 if (rgrp_contains_block(rgd, ip->i_goal))
1448 goal = ip->i_goal - rgd->rd_data0;
1450 goal = rgd->rd_last_alloc;
1452 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
1453 BUG_ON(blk == BFITNOENT);
1455 rgd->rd_last_alloc = blk;
1456 block = rgd->rd_data0 + blk;
1459 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free >= *n);
1460 rgd->rd_rg.rg_free -= *n;
1462 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1463 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1465 al->al_alloced += *n;
1467 gfs2_statfs_change(sdp, 0, -*n, 0);
1468 gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
1470 spin_lock(&sdp->sd_rindex_spin);
1471 rgd->rd_free_clone -= *n;
1472 spin_unlock(&sdp->sd_rindex_spin);
1478 * gfs2_alloc_di - Allocate a dinode
1479 * @dip: the directory that the inode is going in
1481 * Returns: the block allocated
1484 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1486 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1487 struct gfs2_alloc *al = dip->i_alloc;
1488 struct gfs2_rgrpd *rgd = al->al_rgd;
1493 blk = rgblk_search(rgd, rgd->rd_last_alloc,
1494 GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
1495 BUG_ON(blk == BFITNOENT);
1497 rgd->rd_last_alloc = blk;
1499 block = rgd->rd_data0 + blk;
1501 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1502 rgd->rd_rg.rg_free--;
1503 rgd->rd_rg.rg_dinodes++;
1504 *generation = rgd->rd_rg.rg_igeneration++;
1505 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1506 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1510 gfs2_statfs_change(sdp, 0, -1, +1);
1511 gfs2_trans_add_unrevoke(sdp, block, 1);
1513 spin_lock(&sdp->sd_rindex_spin);
1514 rgd->rd_free_clone--;
1515 spin_unlock(&sdp->sd_rindex_spin);
1521 * gfs2_free_data - free a contiguous run of data block(s)
1522 * @ip: the inode these blocks are being freed from
1523 * @bstart: first block of a run of contiguous blocks
1524 * @blen: the length of the block run
1528 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1530 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1531 struct gfs2_rgrpd *rgd;
1533 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1537 rgd->rd_rg.rg_free += blen;
1539 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1540 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1542 gfs2_trans_add_rg(rgd);
1544 gfs2_statfs_change(sdp, 0, +blen, 0);
1545 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1549 * gfs2_free_meta - free a contiguous run of data block(s)
1550 * @ip: the inode these blocks are being freed from
1551 * @bstart: first block of a run of contiguous blocks
1552 * @blen: the length of the block run
1556 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1558 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1559 struct gfs2_rgrpd *rgd;
1561 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1565 rgd->rd_rg.rg_free += blen;
1567 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1568 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1570 gfs2_trans_add_rg(rgd);
1572 gfs2_statfs_change(sdp, 0, +blen, 0);
1573 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1574 gfs2_meta_wipe(ip, bstart, blen);
1577 void gfs2_unlink_di(struct inode *inode)
1579 struct gfs2_inode *ip = GFS2_I(inode);
1580 struct gfs2_sbd *sdp = GFS2_SB(inode);
1581 struct gfs2_rgrpd *rgd;
1582 u64 blkno = ip->i_no_addr;
1584 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1587 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1588 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1589 gfs2_trans_add_rg(rgd);
1592 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1594 struct gfs2_sbd *sdp = rgd->rd_sbd;
1595 struct gfs2_rgrpd *tmp_rgd;
1597 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1600 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1602 if (!rgd->rd_rg.rg_dinodes)
1603 gfs2_consist_rgrpd(rgd);
1604 rgd->rd_rg.rg_dinodes--;
1605 rgd->rd_rg.rg_free++;
1607 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1608 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1610 gfs2_statfs_change(sdp, 0, +1, -1);
1611 gfs2_trans_add_rg(rgd);
1615 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1617 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1618 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1619 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1623 * gfs2_rlist_add - add a RG to a list of RGs
1624 * @sdp: the filesystem
1625 * @rlist: the list of resource groups
1628 * Figure out what RG a block belongs to and add that RG to the list
1630 * FIXME: Don't use NOFAIL
1634 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1637 struct gfs2_rgrpd *rgd;
1638 struct gfs2_rgrpd **tmp;
1639 unsigned int new_space;
1642 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1645 rgd = gfs2_blk2rgrpd(sdp, block);
1647 if (gfs2_consist(sdp))
1648 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1652 for (x = 0; x < rlist->rl_rgrps; x++)
1653 if (rlist->rl_rgd[x] == rgd)
1656 if (rlist->rl_rgrps == rlist->rl_space) {
1657 new_space = rlist->rl_space + 10;
1659 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1660 GFP_NOFS | __GFP_NOFAIL);
1662 if (rlist->rl_rgd) {
1663 memcpy(tmp, rlist->rl_rgd,
1664 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1665 kfree(rlist->rl_rgd);
1668 rlist->rl_space = new_space;
1669 rlist->rl_rgd = tmp;
1672 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1676 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1677 * and initialize an array of glock holders for them
1678 * @rlist: the list of resource groups
1679 * @state: the lock state to acquire the RG lock in
1680 * @flags: the modifier flags for the holder structures
1682 * FIXME: Don't use NOFAIL
1686 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
1690 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1691 GFP_NOFS | __GFP_NOFAIL);
1692 for (x = 0; x < rlist->rl_rgrps; x++)
1693 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1699 * gfs2_rlist_free - free a resource group list
1700 * @list: the list of resource groups
1704 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1708 kfree(rlist->rl_rgd);
1710 if (rlist->rl_ghs) {
1711 for (x = 0; x < rlist->rl_rgrps; x++)
1712 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1713 kfree(rlist->rl_ghs);