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);
59 * gfs2_setbit - Set a bit in the bitmaps
60 * @buffer: the buffer that holds the bitmaps
61 * @buflen: the length (in bytes) of the buffer
62 * @block: the block to set
63 * @new_state: the new state of the block
67 static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
68 unsigned int buflen, u32 block,
69 unsigned char new_state)
71 unsigned char *byte, *end, cur_state;
74 byte = buffer + (block / GFS2_NBBY);
75 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
76 end = buffer + buflen;
78 gfs2_assert(rgd->rd_sbd, byte < end);
80 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
82 if (valid_change[new_state * 4 + cur_state]) {
83 *byte ^= cur_state << bit;
84 *byte |= new_state << bit;
86 gfs2_consist_rgrpd(rgd);
90 * gfs2_testbit - test a bit in the bitmaps
91 * @buffer: the buffer that holds the bitmaps
92 * @buflen: the length (in bytes) of the buffer
93 * @block: the block to read
97 static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
98 unsigned int buflen, u32 block)
100 unsigned char *byte, *end, cur_state;
103 byte = buffer + (block / GFS2_NBBY);
104 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
105 end = buffer + buflen;
107 gfs2_assert(rgd->rd_sbd, byte < end);
109 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
115 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
116 * a block in a given allocation state.
117 * @buffer: the buffer that holds the bitmaps
118 * @buflen: the length (in bytes) of the buffer
119 * @goal: start search at this block's bit-pair (within @buffer)
120 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for.
122 * Scope of @goal and returned block number is only within this bitmap buffer,
123 * not entire rgrp or filesystem. @buffer will be offset from the actual
124 * beginning of a bitmap block buffer, skipping any header structures.
126 * Return: the block number (bitmap buffer scope) that was found
129 static u32 gfs2_bitfit(const u8 *buffer, unsigned int buflen, u32 goal,
134 unsigned int bit, bitlong;
135 const unsigned long *plong;
136 #if BITS_PER_LONG == 32
137 const unsigned long plong55 = 0x55555555;
139 const unsigned long plong55 = 0x5555555555555555;
142 byte = buffer + (goal / GFS2_NBBY);
143 plong = (const unsigned long *)(buffer + (goal / GFS2_NBBY));
144 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
147 while (byte < buffer + buflen) {
149 if (bitlong == 0 && old_state == 0 && *plong == plong55) {
151 byte += sizeof(unsigned long);
152 blk += sizeof(unsigned long) * GFS2_NBBY;
155 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
157 bit += GFS2_BIT_SIZE;
162 bitlong += GFS2_BIT_SIZE;
163 if (bitlong >= sizeof(unsigned long) * 8) {
175 * gfs2_bitcount - count the number of bits in a certain state
176 * @buffer: the buffer that holds the bitmaps
177 * @buflen: the length (in bytes) of the buffer
178 * @state: the state of the block we're looking for
180 * Returns: The number of bits
183 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
184 unsigned int buflen, u8 state)
186 const u8 *byte = buffer;
187 const u8 *end = buffer + buflen;
188 const u8 state1 = state << 2;
189 const u8 state2 = state << 4;
190 const u8 state3 = state << 6;
193 for (; byte < end; byte++) {
194 if (((*byte) & 0x03) == state)
196 if (((*byte) & 0x0C) == state1)
198 if (((*byte) & 0x30) == state2)
200 if (((*byte) & 0xC0) == state3)
208 * gfs2_rgrp_verify - Verify that a resource group is consistent
209 * @sdp: the filesystem
214 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
216 struct gfs2_sbd *sdp = rgd->rd_sbd;
217 struct gfs2_bitmap *bi = NULL;
218 u32 length = rgd->rd_length;
222 memset(count, 0, 4 * sizeof(u32));
224 /* Count # blocks in each of 4 possible allocation states */
225 for (buf = 0; buf < length; buf++) {
226 bi = rgd->rd_bits + buf;
227 for (x = 0; x < 4; x++)
228 count[x] += gfs2_bitcount(rgd,
234 if (count[0] != rgd->rd_rg.rg_free) {
235 if (gfs2_consist_rgrpd(rgd))
236 fs_err(sdp, "free data mismatch: %u != %u\n",
237 count[0], rgd->rd_rg.rg_free);
243 rgd->rd_rg.rg_dinodes;
244 if (count[1] + count[2] != tmp) {
245 if (gfs2_consist_rgrpd(rgd))
246 fs_err(sdp, "used data mismatch: %u != %u\n",
251 if (count[3] != rgd->rd_rg.rg_dinodes) {
252 if (gfs2_consist_rgrpd(rgd))
253 fs_err(sdp, "used metadata mismatch: %u != %u\n",
254 count[3], rgd->rd_rg.rg_dinodes);
258 if (count[2] > count[3]) {
259 if (gfs2_consist_rgrpd(rgd))
260 fs_err(sdp, "unlinked inodes > inodes: %u\n",
267 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
269 u64 first = rgd->rd_data0;
270 u64 last = first + rgd->rd_data;
271 return first <= block && block < last;
275 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
276 * @sdp: The GFS2 superblock
277 * @n: The data block number
279 * Returns: The resource group, or NULL if not found
282 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
284 struct gfs2_rgrpd *rgd;
286 spin_lock(&sdp->sd_rindex_spin);
288 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
289 if (rgrp_contains_block(rgd, blk)) {
290 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
291 spin_unlock(&sdp->sd_rindex_spin);
296 spin_unlock(&sdp->sd_rindex_spin);
302 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
303 * @sdp: The GFS2 superblock
305 * Returns: The first rgrp in the filesystem
308 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
310 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
311 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
315 * gfs2_rgrpd_get_next - get the next RG
318 * Returns: The next rgrp
321 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
323 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
325 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
328 static void clear_rgrpdi(struct gfs2_sbd *sdp)
330 struct list_head *head;
331 struct gfs2_rgrpd *rgd;
332 struct gfs2_glock *gl;
334 spin_lock(&sdp->sd_rindex_spin);
335 sdp->sd_rindex_forward = NULL;
336 head = &sdp->sd_rindex_recent_list;
337 while (!list_empty(head)) {
338 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
339 list_del(&rgd->rd_recent);
341 spin_unlock(&sdp->sd_rindex_spin);
343 head = &sdp->sd_rindex_list;
344 while (!list_empty(head)) {
345 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
348 list_del(&rgd->rd_list);
349 list_del(&rgd->rd_list_mru);
352 gl->gl_object = NULL;
357 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
361 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
363 mutex_lock(&sdp->sd_rindex_mutex);
365 mutex_unlock(&sdp->sd_rindex_mutex);
368 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
370 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
371 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
372 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
373 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
374 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
378 * gfs2_compute_bitstructs - Compute the bitmap sizes
379 * @rgd: The resource group descriptor
381 * Calculates bitmap descriptors, one for each block that contains bitmap data
386 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
388 struct gfs2_sbd *sdp = rgd->rd_sbd;
389 struct gfs2_bitmap *bi;
390 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
391 u32 bytes_left, bytes;
397 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
401 bytes_left = rgd->rd_bitbytes;
403 for (x = 0; x < length; x++) {
404 bi = rgd->rd_bits + x;
406 /* small rgrp; bitmap stored completely in header block */
409 bi->bi_offset = sizeof(struct gfs2_rgrp);
414 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
415 bi->bi_offset = sizeof(struct gfs2_rgrp);
419 } else if (x + 1 == length) {
421 bi->bi_offset = sizeof(struct gfs2_meta_header);
422 bi->bi_start = rgd->rd_bitbytes - bytes_left;
426 bytes = sdp->sd_sb.sb_bsize -
427 sizeof(struct gfs2_meta_header);
428 bi->bi_offset = sizeof(struct gfs2_meta_header);
429 bi->bi_start = rgd->rd_bitbytes - bytes_left;
437 gfs2_consist_rgrpd(rgd);
440 bi = rgd->rd_bits + (length - 1);
441 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
442 if (gfs2_consist_rgrpd(rgd)) {
443 gfs2_rindex_print(rgd);
444 fs_err(sdp, "start=%u len=%u offset=%u\n",
445 bi->bi_start, bi->bi_len, bi->bi_offset);
454 * gfs2_ri_total - Total up the file system space, according to the rindex.
457 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
460 struct inode *inode = sdp->sd_rindex;
461 struct gfs2_inode *ip = GFS2_I(inode);
462 char buf[sizeof(struct gfs2_rindex)];
463 struct file_ra_state ra_state;
466 mutex_lock(&sdp->sd_rindex_mutex);
467 file_ra_state_init(&ra_state, inode->i_mapping);
468 for (rgrps = 0;; rgrps++) {
469 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
471 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
473 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
474 sizeof(struct gfs2_rindex));
475 if (error != sizeof(struct gfs2_rindex))
477 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
479 mutex_unlock(&sdp->sd_rindex_mutex);
483 static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
485 const struct gfs2_rindex *str = buf;
487 rgd->rd_addr = be64_to_cpu(str->ri_addr);
488 rgd->rd_length = be32_to_cpu(str->ri_length);
489 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
490 rgd->rd_data = be32_to_cpu(str->ri_data);
491 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
495 * read_rindex_entry - Pull in a new resource index entry from the disk
496 * @gl: The glock covering the rindex inode
498 * Returns: 0 on success, error code otherwise
501 static int read_rindex_entry(struct gfs2_inode *ip,
502 struct file_ra_state *ra_state)
504 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
505 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
506 char buf[sizeof(struct gfs2_rindex)];
508 struct gfs2_rgrpd *rgd;
510 error = gfs2_internal_read(ip, ra_state, buf, &pos,
511 sizeof(struct gfs2_rindex));
514 if (error != sizeof(struct gfs2_rindex)) {
520 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
525 mutex_init(&rgd->rd_mutex);
526 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
529 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
530 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
532 gfs2_rindex_in(rgd, buf);
533 error = compute_bitstructs(rgd);
537 error = gfs2_glock_get(sdp, rgd->rd_addr,
538 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
542 rgd->rd_gl->gl_object = rgd;
543 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
544 rgd->rd_flags |= GFS2_RDF_CHECK;
549 * gfs2_ri_update - Pull in a new resource index from the disk
550 * @ip: pointer to the rindex inode
552 * Returns: 0 on successful update, error code otherwise
555 static int gfs2_ri_update(struct gfs2_inode *ip)
557 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
558 struct inode *inode = &ip->i_inode;
559 struct file_ra_state ra_state;
560 u64 rgrp_count = ip->i_di.di_size;
563 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
564 gfs2_consist_inode(ip);
570 file_ra_state_init(&ra_state, inode->i_mapping);
571 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
572 error = read_rindex_entry(ip, &ra_state);
579 sdp->sd_rindex_uptodate = 1;
584 * gfs2_ri_update_special - Pull in a new resource index from the disk
586 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
587 * In this case we know that we don't have any resource groups in memory yet.
589 * @ip: pointer to the rindex inode
591 * Returns: 0 on successful update, error code otherwise
593 static int gfs2_ri_update_special(struct gfs2_inode *ip)
595 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
596 struct inode *inode = &ip->i_inode;
597 struct file_ra_state ra_state;
600 file_ra_state_init(&ra_state, inode->i_mapping);
601 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
602 /* Ignore partials */
603 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
606 error = read_rindex_entry(ip, &ra_state);
613 sdp->sd_rindex_uptodate = 1;
618 * gfs2_rindex_hold - Grab a lock on the rindex
619 * @sdp: The GFS2 superblock
620 * @ri_gh: the glock holder
622 * We grab a lock on the rindex inode to make sure that it doesn't
623 * change whilst we are performing an operation. We keep this lock
624 * for quite long periods of time compared to other locks. This
625 * doesn't matter, since it is shared and it is very, very rarely
626 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
628 * This makes sure that we're using the latest copy of the resource index
629 * special file, which might have been updated if someone expanded the
630 * filesystem (via gfs2_grow utility), which adds new resource groups.
632 * Returns: 0 on success, error code otherwise
635 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
637 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
638 struct gfs2_glock *gl = ip->i_gl;
641 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
645 /* Read new copy from disk if we don't have the latest */
646 if (!sdp->sd_rindex_uptodate) {
647 mutex_lock(&sdp->sd_rindex_mutex);
648 if (!sdp->sd_rindex_uptodate) {
649 error = gfs2_ri_update(ip);
651 gfs2_glock_dq_uninit(ri_gh);
653 mutex_unlock(&sdp->sd_rindex_mutex);
659 static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
661 const struct gfs2_rgrp *str = buf;
662 struct gfs2_rgrp_host *rg = &rgd->rd_rg;
665 rg_flags = be32_to_cpu(str->rg_flags);
666 if (rg_flags & GFS2_RGF_NOALLOC)
667 rgd->rd_flags |= GFS2_RDF_NOALLOC;
669 rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
670 rg->rg_free = be32_to_cpu(str->rg_free);
671 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
672 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
675 static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
677 struct gfs2_rgrp *str = buf;
678 struct gfs2_rgrp_host *rg = &rgd->rd_rg;
681 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
682 rg_flags |= GFS2_RGF_NOALLOC;
683 str->rg_flags = cpu_to_be32(rg_flags);
684 str->rg_free = cpu_to_be32(rg->rg_free);
685 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
686 str->__pad = cpu_to_be32(0);
687 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
688 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
692 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
693 * @rgd: the struct gfs2_rgrpd describing the RG to read in
695 * Read in all of a Resource Group's header and bitmap blocks.
696 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
701 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
703 struct gfs2_sbd *sdp = rgd->rd_sbd;
704 struct gfs2_glock *gl = rgd->rd_gl;
705 unsigned int length = rgd->rd_length;
706 struct gfs2_bitmap *bi;
710 mutex_lock(&rgd->rd_mutex);
712 spin_lock(&sdp->sd_rindex_spin);
713 if (rgd->rd_bh_count) {
715 spin_unlock(&sdp->sd_rindex_spin);
716 mutex_unlock(&rgd->rd_mutex);
719 spin_unlock(&sdp->sd_rindex_spin);
721 for (x = 0; x < length; x++) {
722 bi = rgd->rd_bits + x;
723 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
728 for (y = length; y--;) {
729 bi = rgd->rd_bits + y;
730 error = gfs2_meta_wait(sdp, bi->bi_bh);
733 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
740 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
741 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
742 rgd->rd_flags |= GFS2_RDF_UPTODATE;
745 spin_lock(&sdp->sd_rindex_spin);
746 rgd->rd_free_clone = rgd->rd_rg.rg_free;
748 spin_unlock(&sdp->sd_rindex_spin);
750 mutex_unlock(&rgd->rd_mutex);
756 bi = rgd->rd_bits + x;
759 gfs2_assert_warn(sdp, !bi->bi_clone);
761 mutex_unlock(&rgd->rd_mutex);
766 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
768 struct gfs2_sbd *sdp = rgd->rd_sbd;
770 spin_lock(&sdp->sd_rindex_spin);
771 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
773 spin_unlock(&sdp->sd_rindex_spin);
777 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
778 * @rgd: the struct gfs2_rgrpd describing the RG to read in
782 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
784 struct gfs2_sbd *sdp = rgd->rd_sbd;
785 int x, length = rgd->rd_length;
787 spin_lock(&sdp->sd_rindex_spin);
788 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
789 if (--rgd->rd_bh_count) {
790 spin_unlock(&sdp->sd_rindex_spin);
794 for (x = 0; x < length; x++) {
795 struct gfs2_bitmap *bi = rgd->rd_bits + x;
802 spin_unlock(&sdp->sd_rindex_spin);
805 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
807 struct gfs2_sbd *sdp = rgd->rd_sbd;
808 unsigned int length = rgd->rd_length;
811 for (x = 0; x < length; x++) {
812 struct gfs2_bitmap *bi = rgd->rd_bits + x;
815 memcpy(bi->bi_clone + bi->bi_offset,
816 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
819 spin_lock(&sdp->sd_rindex_spin);
820 rgd->rd_free_clone = rgd->rd_rg.rg_free;
821 spin_unlock(&sdp->sd_rindex_spin);
825 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
826 * @ip: the incore GFS2 inode structure
828 * Returns: the struct gfs2_alloc
831 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
833 BUG_ON(ip->i_alloc != NULL);
834 ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
839 * try_rgrp_fit - See if a given reservation will fit in a given RG
841 * @al: the struct gfs2_alloc structure describing the reservation
843 * If there's room for the requested blocks to be allocated from the RG:
844 * Sets the $al_rgd field in @al.
846 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
849 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
851 struct gfs2_sbd *sdp = rgd->rd_sbd;
854 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
857 spin_lock(&sdp->sd_rindex_spin);
858 if (rgd->rd_free_clone >= al->al_requested) {
862 spin_unlock(&sdp->sd_rindex_spin);
868 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
871 * Returns: The inode, if one has been found
874 static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
879 struct gfs2_sbd *sdp = rgd->rd_sbd;
882 if (goal >= rgd->rd_data)
884 down_write(&sdp->sd_log_flush_lock);
885 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
886 GFS2_BLKST_UNLINKED);
887 up_write(&sdp->sd_log_flush_lock);
888 if (block == BFITNOENT)
890 /* rgblk_search can return a block < goal, so we need to
891 keep it marching forward. */
892 no_addr = block + rgd->rd_data0;
894 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
896 *last_unlinked = no_addr;
897 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
903 rgd->rd_flags &= ~GFS2_RDF_CHECK;
908 * recent_rgrp_first - get first RG from "recent" list
909 * @sdp: The GFS2 superblock
910 * @rglast: address of the rgrp used last
912 * Returns: The first rgrp in the recent list
915 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
918 struct gfs2_rgrpd *rgd;
920 spin_lock(&sdp->sd_rindex_spin);
923 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
924 if (rgrp_contains_block(rgd, rglast))
929 if (!list_empty(&sdp->sd_rindex_recent_list))
930 rgd = list_entry(sdp->sd_rindex_recent_list.next,
931 struct gfs2_rgrpd, rd_recent);
933 spin_unlock(&sdp->sd_rindex_spin);
938 * recent_rgrp_next - get next RG from "recent" list
939 * @cur_rgd: current rgrp
942 * Returns: The next rgrp in the recent list
945 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
948 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
949 struct list_head *head;
950 struct gfs2_rgrpd *rgd;
952 spin_lock(&sdp->sd_rindex_spin);
954 head = &sdp->sd_rindex_recent_list;
956 list_for_each_entry(rgd, head, rd_recent) {
957 if (rgd == cur_rgd) {
958 if (cur_rgd->rd_recent.next != head)
959 rgd = list_entry(cur_rgd->rd_recent.next,
960 struct gfs2_rgrpd, rd_recent);
965 list_del(&cur_rgd->rd_recent);
972 if (!list_empty(head))
973 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
976 spin_unlock(&sdp->sd_rindex_spin);
981 * recent_rgrp_add - add an RG to tail of "recent" list
982 * @new_rgd: The rgrp to add
986 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
988 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
989 struct gfs2_rgrpd *rgd;
990 unsigned int count = 0;
991 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
993 spin_lock(&sdp->sd_rindex_spin);
995 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
1002 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
1005 spin_unlock(&sdp->sd_rindex_spin);
1009 * forward_rgrp_get - get an rgrp to try next from full list
1010 * @sdp: The GFS2 superblock
1012 * Returns: The rgrp to try next
1015 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1017 struct gfs2_rgrpd *rgd;
1018 unsigned int journals = gfs2_jindex_size(sdp);
1019 unsigned int rg = 0, x;
1021 spin_lock(&sdp->sd_rindex_spin);
1023 rgd = sdp->sd_rindex_forward;
1025 if (sdp->sd_rgrps >= journals)
1026 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1028 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
1029 x++, rgd = gfs2_rgrpd_get_next(rgd))
1032 sdp->sd_rindex_forward = rgd;
1035 spin_unlock(&sdp->sd_rindex_spin);
1041 * forward_rgrp_set - set the forward rgrp pointer
1042 * @sdp: the filesystem
1043 * @rgd: The new forward rgrp
1047 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1049 spin_lock(&sdp->sd_rindex_spin);
1050 sdp->sd_rindex_forward = rgd;
1051 spin_unlock(&sdp->sd_rindex_spin);
1055 * get_local_rgrp - Choose and lock a rgrp for allocation
1056 * @ip: the inode to reserve space for
1057 * @rgp: the chosen and locked rgrp
1059 * Try to acquire rgrp in way which avoids contending with others.
1064 static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
1066 struct inode *inode = NULL;
1067 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1068 struct gfs2_rgrpd *rgd, *begin = NULL;
1069 struct gfs2_alloc *al = ip->i_alloc;
1070 int flags = LM_FLAG_TRY;
1073 int error, rg_locked;
1075 /* Try recently successful rgrps */
1077 rgd = recent_rgrp_first(sdp, ip->i_goal);
1082 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1086 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1087 LM_FLAG_TRY, &al->al_rgd_gh);
1091 if (try_rgrp_fit(rgd, al))
1093 if (rgd->rd_flags & GFS2_RDF_CHECK)
1094 inode = try_rgrp_unlink(rgd, last_unlinked);
1096 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1099 rgd = recent_rgrp_next(rgd, 1);
1103 rgd = recent_rgrp_next(rgd, 0);
1107 return ERR_PTR(error);
1111 /* Go through full list of rgrps */
1113 begin = rgd = forward_rgrp_get(sdp);
1118 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1122 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1127 if (try_rgrp_fit(rgd, al))
1129 if (rgd->rd_flags & GFS2_RDF_CHECK)
1130 inode = try_rgrp_unlink(rgd, last_unlinked);
1132 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1142 return ERR_PTR(error);
1145 rgd = gfs2_rgrpd_get_next(rgd);
1147 rgd = gfs2_rgrpd_get_first(sdp);
1151 return ERR_PTR(-ENOSPC);
1156 gfs2_log_flush(sdp, NULL);
1162 recent_rgrp_add(rgd);
1163 rgd = gfs2_rgrpd_get_next(rgd);
1165 rgd = gfs2_rgrpd_get_first(sdp);
1166 forward_rgrp_set(sdp, rgd);
1173 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1174 * @ip: the inode to reserve space for
1179 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1181 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1182 struct gfs2_alloc *al = ip->i_alloc;
1183 struct inode *inode;
1185 u64 last_unlinked = NO_BLOCK;
1187 if (gfs2_assert_warn(sdp, al->al_requested))
1191 /* We need to hold the rindex unless the inode we're using is
1192 the rindex itself, in which case it's already held. */
1193 if (ip != GFS2_I(sdp->sd_rindex))
1194 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1195 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1196 error = gfs2_ri_update_special(ip);
1201 inode = get_local_rgrp(ip, &last_unlinked);
1203 if (ip != GFS2_I(sdp->sd_rindex))
1204 gfs2_glock_dq_uninit(&al->al_ri_gh);
1206 return PTR_ERR(inode);
1208 gfs2_log_flush(sdp, NULL);
1219 * gfs2_inplace_release - release an inplace reservation
1220 * @ip: the inode the reservation was taken out on
1222 * Release a reservation made by gfs2_inplace_reserve().
1225 void gfs2_inplace_release(struct gfs2_inode *ip)
1227 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1228 struct gfs2_alloc *al = ip->i_alloc;
1230 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1231 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1232 "al_file = %s, al_line = %u\n",
1233 al->al_alloced, al->al_requested, al->al_file,
1237 if (al->al_rgd_gh.gh_gl)
1238 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1239 if (ip != GFS2_I(sdp->sd_rindex))
1240 gfs2_glock_dq_uninit(&al->al_ri_gh);
1244 * gfs2_get_block_type - Check a block in a RG is of given type
1245 * @rgd: the resource group holding the block
1246 * @block: the block number
1248 * Returns: The block type (GFS2_BLKST_*)
1251 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1253 struct gfs2_bitmap *bi = NULL;
1254 u32 length, rgrp_block, buf_block;
1258 length = rgd->rd_length;
1259 rgrp_block = block - rgd->rd_data0;
1261 for (buf = 0; buf < length; buf++) {
1262 bi = rgd->rd_bits + buf;
1263 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1267 gfs2_assert(rgd->rd_sbd, buf < length);
1268 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1270 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1271 bi->bi_len, buf_block);
1277 * rgblk_search - find a block in @old_state, change allocation
1278 * state to @new_state
1279 * @rgd: the resource group descriptor
1280 * @goal: the goal block within the RG (start here to search for avail block)
1281 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1282 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1284 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1285 * Add the found bitmap buffer to the transaction.
1286 * Set the found bits to @new_state to change block's allocation state.
1288 * This function never fails, because we wouldn't call it unless we
1289 * know (from reservation results, etc.) that a block is available.
1291 * Scope of @goal and returned block is just within rgrp, not the whole
1294 * Returns: the block number allocated
1297 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1298 unsigned char old_state, unsigned char new_state)
1300 struct gfs2_bitmap *bi = NULL;
1301 u32 length = rgd->rd_length;
1303 unsigned int buf, x;
1305 /* Find bitmap block that contains bits for goal block */
1306 for (buf = 0; buf < length; buf++) {
1307 bi = rgd->rd_bits + buf;
1308 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1312 gfs2_assert(rgd->rd_sbd, buf < length);
1314 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1315 goal -= bi->bi_start * GFS2_NBBY;
1317 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1318 "x <= length", instead of "x < length", because we typically start
1319 the search in the middle of a bit block, but if we can't find an
1320 allocatable block anywhere else, we want to be able wrap around and
1321 search in the first part of our first-searched bit block. */
1322 for (x = 0; x <= length; x++) {
1323 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1324 bitmaps, so we must search the originals for that. */
1325 const u8 *buffer = bi->bi_bh->b_data + bi->bi_offset;
1326 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
1327 buffer = bi->bi_clone + bi->bi_offset;
1329 blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
1330 if (blk != BFITNOENT)
1333 /* Try next bitmap block (wrap back to rgrp header if at end) */
1334 buf = (buf + 1) % length;
1335 bi = rgd->rd_bits + buf;
1339 if (blk != BFITNOENT && old_state != new_state) {
1340 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1341 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1342 bi->bi_len, blk, new_state);
1344 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1345 bi->bi_len, blk, new_state);
1348 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
1352 * rgblk_free - Change alloc state of given block(s)
1353 * @sdp: the filesystem
1354 * @bstart: the start of a run of blocks to free
1355 * @blen: the length of the block run (all must lie within ONE RG!)
1356 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1358 * Returns: Resource group containing the block(s)
1361 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1362 u32 blen, unsigned char new_state)
1364 struct gfs2_rgrpd *rgd;
1365 struct gfs2_bitmap *bi = NULL;
1366 u32 length, rgrp_blk, buf_blk;
1369 rgd = gfs2_blk2rgrpd(sdp, bstart);
1371 if (gfs2_consist(sdp))
1372 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1376 length = rgd->rd_length;
1378 rgrp_blk = bstart - rgd->rd_data0;
1381 for (buf = 0; buf < length; buf++) {
1382 bi = rgd->rd_bits + buf;
1383 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1387 gfs2_assert(rgd->rd_sbd, buf < length);
1389 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1392 if (!bi->bi_clone) {
1393 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1394 GFP_NOFS | __GFP_NOFAIL);
1395 memcpy(bi->bi_clone + bi->bi_offset,
1396 bi->bi_bh->b_data + bi->bi_offset,
1399 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1400 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1401 bi->bi_len, buf_blk, new_state);
1408 * gfs2_alloc_block - Allocate a block
1409 * @ip: the inode to allocate the block for
1411 * Returns: the allocated block
1414 u64 gfs2_alloc_block(struct gfs2_inode *ip)
1416 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1417 struct gfs2_alloc *al = ip->i_alloc;
1418 struct gfs2_rgrpd *rgd = al->al_rgd;
1422 if (rgrp_contains_block(rgd, ip->i_goal))
1423 goal = ip->i_goal - rgd->rd_data0;
1425 goal = rgd->rd_last_alloc;
1427 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1428 BUG_ON(blk == BFITNOENT);
1429 rgd->rd_last_alloc = blk;
1431 block = rgd->rd_data0 + blk;
1434 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1435 rgd->rd_rg.rg_free--;
1437 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1438 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1442 gfs2_statfs_change(sdp, 0, -1, 0);
1443 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1445 spin_lock(&sdp->sd_rindex_spin);
1446 rgd->rd_free_clone--;
1447 spin_unlock(&sdp->sd_rindex_spin);
1453 * gfs2_alloc_di - Allocate a dinode
1454 * @dip: the directory that the inode is going in
1456 * Returns: the block allocated
1459 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1461 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1462 struct gfs2_alloc *al = dip->i_alloc;
1463 struct gfs2_rgrpd *rgd = al->al_rgd;
1467 blk = rgblk_search(rgd, rgd->rd_last_alloc,
1468 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1469 BUG_ON(blk == BFITNOENT);
1471 rgd->rd_last_alloc = blk;
1473 block = rgd->rd_data0 + blk;
1475 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1476 rgd->rd_rg.rg_free--;
1477 rgd->rd_rg.rg_dinodes++;
1478 *generation = rgd->rd_rg.rg_igeneration++;
1479 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1480 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1484 gfs2_statfs_change(sdp, 0, -1, +1);
1485 gfs2_trans_add_unrevoke(sdp, block, 1);
1487 spin_lock(&sdp->sd_rindex_spin);
1488 rgd->rd_free_clone--;
1489 spin_unlock(&sdp->sd_rindex_spin);
1495 * gfs2_free_data - free a contiguous run of data block(s)
1496 * @ip: the inode these blocks are being freed from
1497 * @bstart: first block of a run of contiguous blocks
1498 * @blen: the length of the block run
1502 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1504 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1505 struct gfs2_rgrpd *rgd;
1507 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1511 rgd->rd_rg.rg_free += blen;
1513 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1514 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1516 gfs2_trans_add_rg(rgd);
1518 gfs2_statfs_change(sdp, 0, +blen, 0);
1519 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1523 * gfs2_free_meta - free a contiguous run of data block(s)
1524 * @ip: the inode these blocks are being freed from
1525 * @bstart: first block of a run of contiguous blocks
1526 * @blen: the length of the block run
1530 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1532 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1533 struct gfs2_rgrpd *rgd;
1535 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1539 rgd->rd_rg.rg_free += blen;
1541 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1542 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1544 gfs2_trans_add_rg(rgd);
1546 gfs2_statfs_change(sdp, 0, +blen, 0);
1547 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1548 gfs2_meta_wipe(ip, bstart, blen);
1551 void gfs2_unlink_di(struct inode *inode)
1553 struct gfs2_inode *ip = GFS2_I(inode);
1554 struct gfs2_sbd *sdp = GFS2_SB(inode);
1555 struct gfs2_rgrpd *rgd;
1556 u64 blkno = ip->i_no_addr;
1558 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1561 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1562 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1563 gfs2_trans_add_rg(rgd);
1566 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1568 struct gfs2_sbd *sdp = rgd->rd_sbd;
1569 struct gfs2_rgrpd *tmp_rgd;
1571 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1574 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1576 if (!rgd->rd_rg.rg_dinodes)
1577 gfs2_consist_rgrpd(rgd);
1578 rgd->rd_rg.rg_dinodes--;
1579 rgd->rd_rg.rg_free++;
1581 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1582 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1584 gfs2_statfs_change(sdp, 0, +1, -1);
1585 gfs2_trans_add_rg(rgd);
1589 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1591 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1592 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1593 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1597 * gfs2_rlist_add - add a RG to a list of RGs
1598 * @sdp: the filesystem
1599 * @rlist: the list of resource groups
1602 * Figure out what RG a block belongs to and add that RG to the list
1604 * FIXME: Don't use NOFAIL
1608 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1611 struct gfs2_rgrpd *rgd;
1612 struct gfs2_rgrpd **tmp;
1613 unsigned int new_space;
1616 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1619 rgd = gfs2_blk2rgrpd(sdp, block);
1621 if (gfs2_consist(sdp))
1622 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1626 for (x = 0; x < rlist->rl_rgrps; x++)
1627 if (rlist->rl_rgd[x] == rgd)
1630 if (rlist->rl_rgrps == rlist->rl_space) {
1631 new_space = rlist->rl_space + 10;
1633 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1634 GFP_NOFS | __GFP_NOFAIL);
1636 if (rlist->rl_rgd) {
1637 memcpy(tmp, rlist->rl_rgd,
1638 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1639 kfree(rlist->rl_rgd);
1642 rlist->rl_space = new_space;
1643 rlist->rl_rgd = tmp;
1646 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1650 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1651 * and initialize an array of glock holders for them
1652 * @rlist: the list of resource groups
1653 * @state: the lock state to acquire the RG lock in
1654 * @flags: the modifier flags for the holder structures
1656 * FIXME: Don't use NOFAIL
1660 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
1664 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1665 GFP_NOFS | __GFP_NOFAIL);
1666 for (x = 0; x < rlist->rl_rgrps; x++)
1667 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1673 * gfs2_rlist_free - free a resource group list
1674 * @list: the list of resource groups
1678 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1682 kfree(rlist->rl_rgd);
1684 if (rlist->rl_ghs) {
1685 for (x = 0; x < rlist->rl_rgrps; x++)
1686 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1687 kfree(rlist->rl_ghs);