2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 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>
33 #define BFITNOENT ((u32)~0)
36 * These routines are used by the resource group routines (rgrp.c)
37 * to keep track of block allocation. Each block is represented by two
38 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
41 * 1 = Used (not metadata)
42 * 2 = Unlinked (still in use) inode
46 static const char valid_change[16] = {
54 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
55 unsigned char old_state, unsigned char new_state);
58 * gfs2_setbit - Set a bit in the bitmaps
59 * @buffer: the buffer that holds the bitmaps
60 * @buflen: the length (in bytes) of the buffer
61 * @block: the block to set
62 * @new_state: the new state of the block
66 static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
67 unsigned int buflen, u32 block,
68 unsigned char new_state)
70 unsigned char *byte, *end, cur_state;
73 byte = buffer + (block / GFS2_NBBY);
74 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
75 end = buffer + buflen;
77 gfs2_assert(rgd->rd_sbd, byte < end);
79 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
81 if (valid_change[new_state * 4 + cur_state]) {
82 *byte ^= cur_state << bit;
83 *byte |= new_state << bit;
85 gfs2_consist_rgrpd(rgd);
89 * gfs2_testbit - test a bit in the bitmaps
90 * @buffer: the buffer that holds the bitmaps
91 * @buflen: the length (in bytes) of the buffer
92 * @block: the block to read
96 static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
97 unsigned int buflen, u32 block)
99 unsigned char *byte, *end, cur_state;
102 byte = buffer + (block / GFS2_NBBY);
103 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
104 end = buffer + buflen;
106 gfs2_assert(rgd->rd_sbd, byte < end);
108 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
114 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
115 * a block in a given allocation state.
116 * @buffer: the buffer that holds the bitmaps
117 * @buflen: the length (in bytes) of the buffer
118 * @goal: start search at this block's bit-pair (within @buffer)
119 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
120 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
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(struct gfs2_rgrpd *rgd, unsigned char *buffer,
130 unsigned int buflen, u32 goal,
131 unsigned char old_state)
133 unsigned char *byte, *end, alloc;
137 byte = buffer + (goal / GFS2_NBBY);
138 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
139 end = buffer + buflen;
140 alloc = (old_state & 1) ? 0 : 0x55;
143 if ((*byte & 0x55) == alloc) {
144 blk += (8 - bit) >> 1;
152 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
155 bit += GFS2_BIT_SIZE;
168 * gfs2_bitcount - count the number of bits in a certain state
169 * @buffer: the buffer that holds the bitmaps
170 * @buflen: the length (in bytes) of the buffer
171 * @state: the state of the block we're looking for
173 * Returns: The number of bits
176 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
177 unsigned int buflen, unsigned char state)
179 unsigned char *byte = buffer;
180 unsigned char *end = buffer + buflen;
181 unsigned char state1 = state << 2;
182 unsigned char state2 = state << 4;
183 unsigned char state3 = state << 6;
186 for (; byte < end; byte++) {
187 if (((*byte) & 0x03) == state)
189 if (((*byte) & 0x0C) == state1)
191 if (((*byte) & 0x30) == state2)
193 if (((*byte) & 0xC0) == state3)
201 * gfs2_rgrp_verify - Verify that a resource group is consistent
202 * @sdp: the filesystem
207 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
209 struct gfs2_sbd *sdp = rgd->rd_sbd;
210 struct gfs2_bitmap *bi = NULL;
211 u32 length = rgd->rd_length;
215 memset(count, 0, 4 * sizeof(u32));
217 /* Count # blocks in each of 4 possible allocation states */
218 for (buf = 0; buf < length; buf++) {
219 bi = rgd->rd_bits + buf;
220 for (x = 0; x < 4; x++)
221 count[x] += gfs2_bitcount(rgd,
227 if (count[0] != rgd->rd_rg.rg_free) {
228 if (gfs2_consist_rgrpd(rgd))
229 fs_err(sdp, "free data mismatch: %u != %u\n",
230 count[0], rgd->rd_rg.rg_free);
236 rgd->rd_rg.rg_dinodes;
237 if (count[1] + count[2] != tmp) {
238 if (gfs2_consist_rgrpd(rgd))
239 fs_err(sdp, "used data mismatch: %u != %u\n",
244 if (count[3] != rgd->rd_rg.rg_dinodes) {
245 if (gfs2_consist_rgrpd(rgd))
246 fs_err(sdp, "used metadata mismatch: %u != %u\n",
247 count[3], rgd->rd_rg.rg_dinodes);
251 if (count[2] > count[3]) {
252 if (gfs2_consist_rgrpd(rgd))
253 fs_err(sdp, "unlinked inodes > inodes: %u\n",
260 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
262 u64 first = rgd->rd_data0;
263 u64 last = first + rgd->rd_data;
264 return first <= block && block < last;
268 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
269 * @sdp: The GFS2 superblock
270 * @n: The data block number
272 * Returns: The resource group, or NULL if not found
275 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
277 struct gfs2_rgrpd *rgd;
279 spin_lock(&sdp->sd_rindex_spin);
281 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
282 if (rgrp_contains_block(rgd, blk)) {
283 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
284 spin_unlock(&sdp->sd_rindex_spin);
289 spin_unlock(&sdp->sd_rindex_spin);
295 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
296 * @sdp: The GFS2 superblock
298 * Returns: The first rgrp in the filesystem
301 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
303 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
304 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
308 * gfs2_rgrpd_get_next - get the next RG
311 * Returns: The next rgrp
314 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
316 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
318 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
321 static void clear_rgrpdi(struct gfs2_sbd *sdp)
323 struct list_head *head;
324 struct gfs2_rgrpd *rgd;
325 struct gfs2_glock *gl;
327 spin_lock(&sdp->sd_rindex_spin);
328 sdp->sd_rindex_forward = NULL;
329 head = &sdp->sd_rindex_recent_list;
330 while (!list_empty(head)) {
331 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
332 list_del(&rgd->rd_recent);
334 spin_unlock(&sdp->sd_rindex_spin);
336 head = &sdp->sd_rindex_list;
337 while (!list_empty(head)) {
338 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
341 list_del(&rgd->rd_list);
342 list_del(&rgd->rd_list_mru);
345 gl->gl_object = NULL;
354 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
356 mutex_lock(&sdp->sd_rindex_mutex);
358 mutex_unlock(&sdp->sd_rindex_mutex);
361 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
363 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
364 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
365 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
366 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
367 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
371 * gfs2_compute_bitstructs - Compute the bitmap sizes
372 * @rgd: The resource group descriptor
374 * Calculates bitmap descriptors, one for each block that contains bitmap data
379 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
381 struct gfs2_sbd *sdp = rgd->rd_sbd;
382 struct gfs2_bitmap *bi;
383 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
384 u32 bytes_left, bytes;
390 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
394 bytes_left = rgd->rd_bitbytes;
396 for (x = 0; x < length; x++) {
397 bi = rgd->rd_bits + x;
399 /* small rgrp; bitmap stored completely in header block */
402 bi->bi_offset = sizeof(struct gfs2_rgrp);
407 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
408 bi->bi_offset = sizeof(struct gfs2_rgrp);
412 } else if (x + 1 == length) {
414 bi->bi_offset = sizeof(struct gfs2_meta_header);
415 bi->bi_start = rgd->rd_bitbytes - bytes_left;
419 bytes = sdp->sd_sb.sb_bsize -
420 sizeof(struct gfs2_meta_header);
421 bi->bi_offset = sizeof(struct gfs2_meta_header);
422 bi->bi_start = rgd->rd_bitbytes - bytes_left;
430 gfs2_consist_rgrpd(rgd);
433 bi = rgd->rd_bits + (length - 1);
434 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
435 if (gfs2_consist_rgrpd(rgd)) {
436 gfs2_rindex_print(rgd);
437 fs_err(sdp, "start=%u len=%u offset=%u\n",
438 bi->bi_start, bi->bi_len, bi->bi_offset);
447 * gfs2_ri_total - Total up the file system space, according to the rindex.
450 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
453 struct inode *inode = sdp->sd_rindex;
454 struct gfs2_inode *ip = GFS2_I(inode);
455 char buf[sizeof(struct gfs2_rindex)];
456 struct file_ra_state ra_state;
459 mutex_lock(&sdp->sd_rindex_mutex);
460 file_ra_state_init(&ra_state, inode->i_mapping);
461 for (rgrps = 0;; rgrps++) {
462 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
464 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
466 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
467 sizeof(struct gfs2_rindex));
468 if (error != sizeof(struct gfs2_rindex))
470 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
472 mutex_unlock(&sdp->sd_rindex_mutex);
476 static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
478 const struct gfs2_rindex *str = buf;
480 rgd->rd_addr = be64_to_cpu(str->ri_addr);
481 rgd->rd_length = be32_to_cpu(str->ri_length);
482 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
483 rgd->rd_data = be32_to_cpu(str->ri_data);
484 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
488 * read_rindex_entry - Pull in a new resource index entry from the disk
489 * @gl: The glock covering the rindex inode
491 * Returns: 0 on success, error code otherwise
494 static int read_rindex_entry(struct gfs2_inode *ip,
495 struct file_ra_state *ra_state)
497 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
498 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
499 char buf[sizeof(struct gfs2_rindex)];
501 struct gfs2_rgrpd *rgd;
503 error = gfs2_internal_read(ip, ra_state, buf, &pos,
504 sizeof(struct gfs2_rindex));
507 if (error != sizeof(struct gfs2_rindex)) {
513 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
518 mutex_init(&rgd->rd_mutex);
519 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
522 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
523 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
525 gfs2_rindex_in(rgd, buf);
526 error = compute_bitstructs(rgd);
530 error = gfs2_glock_get(sdp, rgd->rd_addr,
531 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
535 rgd->rd_gl->gl_object = rgd;
536 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
537 rgd->rd_flags |= GFS2_RDF_CHECK;
542 * gfs2_ri_update - Pull in a new resource index from the disk
543 * @ip: pointer to the rindex inode
545 * Returns: 0 on successful update, error code otherwise
548 static int gfs2_ri_update(struct gfs2_inode *ip)
550 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
551 struct inode *inode = &ip->i_inode;
552 struct file_ra_state ra_state;
553 u64 rgrp_count = ip->i_di.di_size;
556 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
557 gfs2_consist_inode(ip);
563 file_ra_state_init(&ra_state, inode->i_mapping);
564 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
565 error = read_rindex_entry(ip, &ra_state);
572 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
577 * gfs2_ri_update_special - Pull in a new resource index from the disk
579 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
580 * In this case we know that we don't have any resource groups in memory yet.
582 * @ip: pointer to the rindex inode
584 * Returns: 0 on successful update, error code otherwise
586 static int gfs2_ri_update_special(struct gfs2_inode *ip)
588 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
589 struct inode *inode = &ip->i_inode;
590 struct file_ra_state ra_state;
593 file_ra_state_init(&ra_state, inode->i_mapping);
594 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
595 /* Ignore partials */
596 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
599 error = read_rindex_entry(ip, &ra_state);
606 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
611 * gfs2_rindex_hold - Grab a lock on the rindex
612 * @sdp: The GFS2 superblock
613 * @ri_gh: the glock holder
615 * We grab a lock on the rindex inode to make sure that it doesn't
616 * change whilst we are performing an operation. We keep this lock
617 * for quite long periods of time compared to other locks. This
618 * doesn't matter, since it is shared and it is very, very rarely
619 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
621 * This makes sure that we're using the latest copy of the resource index
622 * special file, which might have been updated if someone expanded the
623 * filesystem (via gfs2_grow utility), which adds new resource groups.
625 * Returns: 0 on success, error code otherwise
628 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
630 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
631 struct gfs2_glock *gl = ip->i_gl;
634 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
638 /* Read new copy from disk if we don't have the latest */
639 if (sdp->sd_rindex_vn != gl->gl_vn) {
640 mutex_lock(&sdp->sd_rindex_mutex);
641 if (sdp->sd_rindex_vn != gl->gl_vn) {
642 error = gfs2_ri_update(ip);
644 gfs2_glock_dq_uninit(ri_gh);
646 mutex_unlock(&sdp->sd_rindex_mutex);
652 static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
654 const struct gfs2_rgrp *str = buf;
656 rg->rg_flags = be32_to_cpu(str->rg_flags);
657 rg->rg_free = be32_to_cpu(str->rg_free);
658 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
659 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
662 static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
664 struct gfs2_rgrp *str = buf;
666 str->rg_flags = cpu_to_be32(rg->rg_flags);
667 str->rg_free = cpu_to_be32(rg->rg_free);
668 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
669 str->__pad = cpu_to_be32(0);
670 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
671 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
675 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
676 * @rgd: the struct gfs2_rgrpd describing the RG to read in
678 * Read in all of a Resource Group's header and bitmap blocks.
679 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
684 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
686 struct gfs2_sbd *sdp = rgd->rd_sbd;
687 struct gfs2_glock *gl = rgd->rd_gl;
688 unsigned int length = rgd->rd_length;
689 struct gfs2_bitmap *bi;
693 mutex_lock(&rgd->rd_mutex);
695 spin_lock(&sdp->sd_rindex_spin);
696 if (rgd->rd_bh_count) {
698 spin_unlock(&sdp->sd_rindex_spin);
699 mutex_unlock(&rgd->rd_mutex);
702 spin_unlock(&sdp->sd_rindex_spin);
704 for (x = 0; x < length; x++) {
705 bi = rgd->rd_bits + x;
706 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
711 for (y = length; y--;) {
712 bi = rgd->rd_bits + y;
713 error = gfs2_meta_wait(sdp, bi->bi_bh);
716 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
723 if (rgd->rd_rg_vn != gl->gl_vn) {
724 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
725 rgd->rd_rg_vn = gl->gl_vn;
728 spin_lock(&sdp->sd_rindex_spin);
729 rgd->rd_free_clone = rgd->rd_rg.rg_free;
731 spin_unlock(&sdp->sd_rindex_spin);
733 mutex_unlock(&rgd->rd_mutex);
739 bi = rgd->rd_bits + x;
742 gfs2_assert_warn(sdp, !bi->bi_clone);
744 mutex_unlock(&rgd->rd_mutex);
749 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
751 struct gfs2_sbd *sdp = rgd->rd_sbd;
753 spin_lock(&sdp->sd_rindex_spin);
754 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
756 spin_unlock(&sdp->sd_rindex_spin);
760 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
761 * @rgd: the struct gfs2_rgrpd describing the RG to read in
765 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
767 struct gfs2_sbd *sdp = rgd->rd_sbd;
768 int x, length = rgd->rd_length;
770 spin_lock(&sdp->sd_rindex_spin);
771 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
772 if (--rgd->rd_bh_count) {
773 spin_unlock(&sdp->sd_rindex_spin);
777 for (x = 0; x < length; x++) {
778 struct gfs2_bitmap *bi = rgd->rd_bits + x;
785 spin_unlock(&sdp->sd_rindex_spin);
788 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
790 struct gfs2_sbd *sdp = rgd->rd_sbd;
791 unsigned int length = rgd->rd_length;
794 for (x = 0; x < length; x++) {
795 struct gfs2_bitmap *bi = rgd->rd_bits + x;
798 memcpy(bi->bi_clone + bi->bi_offset,
799 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
802 spin_lock(&sdp->sd_rindex_spin);
803 rgd->rd_free_clone = rgd->rd_rg.rg_free;
804 spin_unlock(&sdp->sd_rindex_spin);
808 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
809 * @ip: the incore GFS2 inode structure
811 * Returns: the struct gfs2_alloc
814 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
816 struct gfs2_alloc *al = &ip->i_alloc;
818 /* FIXME: Should assert that the correct locks are held here... */
819 memset(al, 0, sizeof(*al));
824 * try_rgrp_fit - See if a given reservation will fit in a given RG
826 * @al: the struct gfs2_alloc structure describing the reservation
828 * If there's room for the requested blocks to be allocated from the RG:
829 * Sets the $al_rgd field in @al.
831 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
834 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
836 struct gfs2_sbd *sdp = rgd->rd_sbd;
839 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
842 spin_lock(&sdp->sd_rindex_spin);
843 if (rgd->rd_free_clone >= al->al_requested) {
847 spin_unlock(&sdp->sd_rindex_spin);
853 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
856 * Returns: The inode, if one has been found
859 static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
866 goal = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
867 GFS2_BLKST_UNLINKED);
870 ino = goal + rgd->rd_data0;
871 if (ino <= *last_unlinked)
873 *last_unlinked = ino;
874 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, ino, DT_UNKNOWN);
879 rgd->rd_flags &= ~GFS2_RDF_CHECK;
884 * recent_rgrp_first - get first RG from "recent" list
885 * @sdp: The GFS2 superblock
886 * @rglast: address of the rgrp used last
888 * Returns: The first rgrp in the recent list
891 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
894 struct gfs2_rgrpd *rgd = NULL;
896 spin_lock(&sdp->sd_rindex_spin);
898 if (list_empty(&sdp->sd_rindex_recent_list))
904 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
905 if (rgd->rd_addr == rglast)
910 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
913 spin_unlock(&sdp->sd_rindex_spin);
918 * recent_rgrp_next - get next RG from "recent" list
919 * @cur_rgd: current rgrp
922 * Returns: The next rgrp in the recent list
925 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
928 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
929 struct list_head *head;
930 struct gfs2_rgrpd *rgd;
932 spin_lock(&sdp->sd_rindex_spin);
934 head = &sdp->sd_rindex_recent_list;
936 list_for_each_entry(rgd, head, rd_recent) {
937 if (rgd == cur_rgd) {
938 if (cur_rgd->rd_recent.next != head)
939 rgd = list_entry(cur_rgd->rd_recent.next,
940 struct gfs2_rgrpd, rd_recent);
945 list_del(&cur_rgd->rd_recent);
952 if (!list_empty(head))
953 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
956 spin_unlock(&sdp->sd_rindex_spin);
961 * recent_rgrp_add - add an RG to tail of "recent" list
962 * @new_rgd: The rgrp to add
966 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
968 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
969 struct gfs2_rgrpd *rgd;
970 unsigned int count = 0;
971 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
973 spin_lock(&sdp->sd_rindex_spin);
975 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
982 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
985 spin_unlock(&sdp->sd_rindex_spin);
989 * forward_rgrp_get - get an rgrp to try next from full list
990 * @sdp: The GFS2 superblock
992 * Returns: The rgrp to try next
995 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
997 struct gfs2_rgrpd *rgd;
998 unsigned int journals = gfs2_jindex_size(sdp);
999 unsigned int rg = 0, x;
1001 spin_lock(&sdp->sd_rindex_spin);
1003 rgd = sdp->sd_rindex_forward;
1005 if (sdp->sd_rgrps >= journals)
1006 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1008 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
1009 x++, rgd = gfs2_rgrpd_get_next(rgd))
1012 sdp->sd_rindex_forward = rgd;
1015 spin_unlock(&sdp->sd_rindex_spin);
1021 * forward_rgrp_set - set the forward rgrp pointer
1022 * @sdp: the filesystem
1023 * @rgd: The new forward rgrp
1027 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1029 spin_lock(&sdp->sd_rindex_spin);
1030 sdp->sd_rindex_forward = rgd;
1031 spin_unlock(&sdp->sd_rindex_spin);
1035 * get_local_rgrp - Choose and lock a rgrp for allocation
1036 * @ip: the inode to reserve space for
1037 * @rgp: the chosen and locked rgrp
1039 * Try to acquire rgrp in way which avoids contending with others.
1044 static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
1046 struct inode *inode = NULL;
1047 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1048 struct gfs2_rgrpd *rgd, *begin = NULL;
1049 struct gfs2_alloc *al = &ip->i_alloc;
1050 int flags = LM_FLAG_TRY;
1055 /* Try recently successful rgrps */
1057 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
1060 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1061 LM_FLAG_TRY, &al->al_rgd_gh);
1064 if (try_rgrp_fit(rgd, al))
1066 if (rgd->rd_flags & GFS2_RDF_CHECK)
1067 inode = try_rgrp_unlink(rgd, last_unlinked);
1068 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1071 rgd = recent_rgrp_next(rgd, 1);
1075 rgd = recent_rgrp_next(rgd, 0);
1079 return ERR_PTR(error);
1083 /* Go through full list of rgrps */
1085 begin = rgd = forward_rgrp_get(sdp);
1088 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1092 if (try_rgrp_fit(rgd, al))
1094 if (rgd->rd_flags & GFS2_RDF_CHECK)
1095 inode = try_rgrp_unlink(rgd, last_unlinked);
1096 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1106 return ERR_PTR(error);
1109 rgd = gfs2_rgrpd_get_next(rgd);
1111 rgd = gfs2_rgrpd_get_first(sdp);
1115 return ERR_PTR(-ENOSPC);
1120 gfs2_log_flush(sdp, NULL);
1125 ip->i_last_rg_alloc = rgd->rd_addr;
1128 recent_rgrp_add(rgd);
1129 rgd = gfs2_rgrpd_get_next(rgd);
1131 rgd = gfs2_rgrpd_get_first(sdp);
1132 forward_rgrp_set(sdp, rgd);
1139 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1140 * @ip: the inode to reserve space for
1145 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1147 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1148 struct gfs2_alloc *al = &ip->i_alloc;
1149 struct inode *inode;
1151 u64 last_unlinked = 0;
1153 if (gfs2_assert_warn(sdp, al->al_requested))
1157 /* We need to hold the rindex unless the inode we're using is
1158 the rindex itself, in which case it's already held. */
1159 if (ip != GFS2_I(sdp->sd_rindex))
1160 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1161 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1162 error = gfs2_ri_update_special(ip);
1167 inode = get_local_rgrp(ip, &last_unlinked);
1169 if (ip != GFS2_I(sdp->sd_rindex))
1170 gfs2_glock_dq_uninit(&al->al_ri_gh);
1172 return PTR_ERR(inode);
1174 gfs2_log_flush(sdp, NULL);
1185 * gfs2_inplace_release - release an inplace reservation
1186 * @ip: the inode the reservation was taken out on
1188 * Release a reservation made by gfs2_inplace_reserve().
1191 void gfs2_inplace_release(struct gfs2_inode *ip)
1193 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1194 struct gfs2_alloc *al = &ip->i_alloc;
1196 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1197 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1198 "al_file = %s, al_line = %u\n",
1199 al->al_alloced, al->al_requested, al->al_file,
1203 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1204 if (ip != GFS2_I(sdp->sd_rindex))
1205 gfs2_glock_dq_uninit(&al->al_ri_gh);
1209 * gfs2_get_block_type - Check a block in a RG is of given type
1210 * @rgd: the resource group holding the block
1211 * @block: the block number
1213 * Returns: The block type (GFS2_BLKST_*)
1216 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1218 struct gfs2_bitmap *bi = NULL;
1219 u32 length, rgrp_block, buf_block;
1223 length = rgd->rd_length;
1224 rgrp_block = block - rgd->rd_data0;
1226 for (buf = 0; buf < length; buf++) {
1227 bi = rgd->rd_bits + buf;
1228 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1232 gfs2_assert(rgd->rd_sbd, buf < length);
1233 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1235 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1236 bi->bi_len, buf_block);
1242 * rgblk_search - find a block in @old_state, change allocation
1243 * state to @new_state
1244 * @rgd: the resource group descriptor
1245 * @goal: the goal block within the RG (start here to search for avail block)
1246 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1247 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1249 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1250 * Add the found bitmap buffer to the transaction.
1251 * Set the found bits to @new_state to change block's allocation state.
1253 * This function never fails, because we wouldn't call it unless we
1254 * know (from reservation results, etc.) that a block is available.
1256 * Scope of @goal and returned block is just within rgrp, not the whole
1259 * Returns: the block number allocated
1262 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1263 unsigned char old_state, unsigned char new_state)
1265 struct gfs2_bitmap *bi = NULL;
1266 u32 length = rgd->rd_length;
1268 unsigned int buf, x;
1270 /* Find bitmap block that contains bits for goal block */
1271 for (buf = 0; buf < length; buf++) {
1272 bi = rgd->rd_bits + buf;
1273 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1277 gfs2_assert(rgd->rd_sbd, buf < length);
1279 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1280 goal -= bi->bi_start * GFS2_NBBY;
1282 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1283 "x <= length", instead of "x < length", because we typically start
1284 the search in the middle of a bit block, but if we can't find an
1285 allocatable block anywhere else, we want to be able wrap around and
1286 search in the first part of our first-searched bit block. */
1287 for (x = 0; x <= length; x++) {
1289 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
1290 bi->bi_len, goal, old_state);
1292 blk = gfs2_bitfit(rgd,
1293 bi->bi_bh->b_data + bi->bi_offset,
1294 bi->bi_len, goal, old_state);
1295 if (blk != BFITNOENT)
1298 /* Try next bitmap block (wrap back to rgrp header if at end) */
1299 buf = (buf + 1) % length;
1300 bi = rgd->rd_bits + buf;
1304 if (old_state != new_state) {
1305 gfs2_assert_withdraw(rgd->rd_sbd, blk != BFITNOENT);
1307 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1308 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1309 bi->bi_len, blk, new_state);
1311 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1312 bi->bi_len, blk, new_state);
1315 return (blk == BFITNOENT) ? 0 : (bi->bi_start * GFS2_NBBY) + blk;
1319 * rgblk_free - Change alloc state of given block(s)
1320 * @sdp: the filesystem
1321 * @bstart: the start of a run of blocks to free
1322 * @blen: the length of the block run (all must lie within ONE RG!)
1323 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1325 * Returns: Resource group containing the block(s)
1328 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1329 u32 blen, unsigned char new_state)
1331 struct gfs2_rgrpd *rgd;
1332 struct gfs2_bitmap *bi = NULL;
1333 u32 length, rgrp_blk, buf_blk;
1336 rgd = gfs2_blk2rgrpd(sdp, bstart);
1338 if (gfs2_consist(sdp))
1339 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1343 length = rgd->rd_length;
1345 rgrp_blk = bstart - rgd->rd_data0;
1348 for (buf = 0; buf < length; buf++) {
1349 bi = rgd->rd_bits + buf;
1350 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1354 gfs2_assert(rgd->rd_sbd, buf < length);
1356 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1359 if (!bi->bi_clone) {
1360 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1361 GFP_NOFS | __GFP_NOFAIL);
1362 memcpy(bi->bi_clone + bi->bi_offset,
1363 bi->bi_bh->b_data + bi->bi_offset,
1366 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1367 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1368 bi->bi_len, buf_blk, new_state);
1375 * gfs2_alloc_data - Allocate a data block
1376 * @ip: the inode to allocate the data block for
1378 * Returns: the allocated block
1381 u64 gfs2_alloc_data(struct gfs2_inode *ip)
1383 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1384 struct gfs2_alloc *al = &ip->i_alloc;
1385 struct gfs2_rgrpd *rgd = al->al_rgd;
1389 if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
1390 goal = ip->i_di.di_goal_data - rgd->rd_data0;
1392 goal = rgd->rd_last_alloc_data;
1394 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1395 rgd->rd_last_alloc_data = blk;
1397 block = rgd->rd_data0 + blk;
1398 ip->i_di.di_goal_data = block;
1400 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1401 rgd->rd_rg.rg_free--;
1403 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1404 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1408 gfs2_statfs_change(sdp, 0, -1, 0);
1409 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1411 spin_lock(&sdp->sd_rindex_spin);
1412 rgd->rd_free_clone--;
1413 spin_unlock(&sdp->sd_rindex_spin);
1419 * gfs2_alloc_meta - Allocate a metadata block
1420 * @ip: the inode to allocate the metadata block for
1422 * Returns: the allocated block
1425 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
1427 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1428 struct gfs2_alloc *al = &ip->i_alloc;
1429 struct gfs2_rgrpd *rgd = al->al_rgd;
1433 if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
1434 goal = ip->i_di.di_goal_meta - rgd->rd_data0;
1436 goal = rgd->rd_last_alloc_meta;
1438 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1439 rgd->rd_last_alloc_meta = blk;
1441 block = rgd->rd_data0 + blk;
1442 ip->i_di.di_goal_meta = block;
1444 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1445 rgd->rd_rg.rg_free--;
1447 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1448 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1452 gfs2_statfs_change(sdp, 0, -1, 0);
1453 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1454 gfs2_trans_add_unrevoke(sdp, block);
1456 spin_lock(&sdp->sd_rindex_spin);
1457 rgd->rd_free_clone--;
1458 spin_unlock(&sdp->sd_rindex_spin);
1464 * gfs2_alloc_di - Allocate a dinode
1465 * @dip: the directory that the inode is going in
1467 * Returns: the block allocated
1470 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1472 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1473 struct gfs2_alloc *al = &dip->i_alloc;
1474 struct gfs2_rgrpd *rgd = al->al_rgd;
1478 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1479 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1481 rgd->rd_last_alloc_meta = blk;
1483 block = rgd->rd_data0 + blk;
1485 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1486 rgd->rd_rg.rg_free--;
1487 rgd->rd_rg.rg_dinodes++;
1488 *generation = rgd->rd_rg.rg_igeneration++;
1489 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1490 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1494 gfs2_statfs_change(sdp, 0, -1, +1);
1495 gfs2_trans_add_unrevoke(sdp, block);
1497 spin_lock(&sdp->sd_rindex_spin);
1498 rgd->rd_free_clone--;
1499 spin_unlock(&sdp->sd_rindex_spin);
1505 * gfs2_free_data - free a contiguous run of data block(s)
1506 * @ip: the inode these blocks are being freed from
1507 * @bstart: first block of a run of contiguous blocks
1508 * @blen: the length of the block run
1512 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1514 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1515 struct gfs2_rgrpd *rgd;
1517 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1521 rgd->rd_rg.rg_free += blen;
1523 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1524 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1526 gfs2_trans_add_rg(rgd);
1528 gfs2_statfs_change(sdp, 0, +blen, 0);
1529 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1533 * gfs2_free_meta - free a contiguous run of data block(s)
1534 * @ip: the inode these blocks are being freed from
1535 * @bstart: first block of a run of contiguous blocks
1536 * @blen: the length of the block run
1540 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1542 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1543 struct gfs2_rgrpd *rgd;
1545 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1549 rgd->rd_rg.rg_free += blen;
1551 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1552 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1554 gfs2_trans_add_rg(rgd);
1556 gfs2_statfs_change(sdp, 0, +blen, 0);
1557 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1558 gfs2_meta_wipe(ip, bstart, blen);
1561 void gfs2_unlink_di(struct inode *inode)
1563 struct gfs2_inode *ip = GFS2_I(inode);
1564 struct gfs2_sbd *sdp = GFS2_SB(inode);
1565 struct gfs2_rgrpd *rgd;
1566 u64 blkno = ip->i_no_addr;
1568 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1571 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1572 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1573 gfs2_trans_add_rg(rgd);
1576 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1578 struct gfs2_sbd *sdp = rgd->rd_sbd;
1579 struct gfs2_rgrpd *tmp_rgd;
1581 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1584 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1586 if (!rgd->rd_rg.rg_dinodes)
1587 gfs2_consist_rgrpd(rgd);
1588 rgd->rd_rg.rg_dinodes--;
1589 rgd->rd_rg.rg_free++;
1591 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1592 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1594 gfs2_statfs_change(sdp, 0, +1, -1);
1595 gfs2_trans_add_rg(rgd);
1599 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1601 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1602 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1603 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1607 * gfs2_rlist_add - add a RG to a list of RGs
1608 * @sdp: the filesystem
1609 * @rlist: the list of resource groups
1612 * Figure out what RG a block belongs to and add that RG to the list
1614 * FIXME: Don't use NOFAIL
1618 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1621 struct gfs2_rgrpd *rgd;
1622 struct gfs2_rgrpd **tmp;
1623 unsigned int new_space;
1626 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1629 rgd = gfs2_blk2rgrpd(sdp, block);
1631 if (gfs2_consist(sdp))
1632 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1636 for (x = 0; x < rlist->rl_rgrps; x++)
1637 if (rlist->rl_rgd[x] == rgd)
1640 if (rlist->rl_rgrps == rlist->rl_space) {
1641 new_space = rlist->rl_space + 10;
1643 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1644 GFP_NOFS | __GFP_NOFAIL);
1646 if (rlist->rl_rgd) {
1647 memcpy(tmp, rlist->rl_rgd,
1648 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1649 kfree(rlist->rl_rgd);
1652 rlist->rl_space = new_space;
1653 rlist->rl_rgd = tmp;
1656 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1660 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1661 * and initialize an array of glock holders for them
1662 * @rlist: the list of resource groups
1663 * @state: the lock state to acquire the RG lock in
1664 * @flags: the modifier flags for the holder structures
1666 * FIXME: Don't use NOFAIL
1670 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1675 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1676 GFP_NOFS | __GFP_NOFAIL);
1677 for (x = 0; x < rlist->rl_rgrps; x++)
1678 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1684 * gfs2_rlist_free - free a resource group list
1685 * @list: the list of resource groups
1689 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1693 kfree(rlist->rl_rgd);
1695 if (rlist->rl_ghs) {
1696 for (x = 0; x < rlist->rl_rgrps; x++)
1697 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1698 kfree(rlist->rl_ghs);