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>
32 #define BFITNOENT ((u32)~0)
35 * These routines are used by the resource group routines (rgrp.c)
36 * to keep track of block allocation. Each block is represented by two
37 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
40 * 1 = Used (not metadata)
41 * 2 = Unlinked (still in use) inode
45 static const char valid_change[16] = {
54 * gfs2_setbit - Set a bit in the bitmaps
55 * @buffer: the buffer that holds the bitmaps
56 * @buflen: the length (in bytes) of the buffer
57 * @block: the block to set
58 * @new_state: the new state of the block
62 static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
63 unsigned int buflen, u32 block,
64 unsigned char new_state)
66 unsigned char *byte, *end, cur_state;
69 byte = buffer + (block / GFS2_NBBY);
70 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
71 end = buffer + buflen;
73 gfs2_assert(rgd->rd_sbd, byte < end);
75 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
77 if (valid_change[new_state * 4 + cur_state]) {
78 *byte ^= cur_state << bit;
79 *byte |= new_state << bit;
81 gfs2_consist_rgrpd(rgd);
85 * gfs2_testbit - test a bit in the bitmaps
86 * @buffer: the buffer that holds the bitmaps
87 * @buflen: the length (in bytes) of the buffer
88 * @block: the block to read
92 static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
93 unsigned int buflen, u32 block)
95 unsigned char *byte, *end, cur_state;
98 byte = buffer + (block / GFS2_NBBY);
99 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
100 end = buffer + buflen;
102 gfs2_assert(rgd->rd_sbd, byte < end);
104 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
110 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
111 * a block in a given allocation state.
112 * @buffer: the buffer that holds the bitmaps
113 * @buflen: the length (in bytes) of the buffer
114 * @goal: start search at this block's bit-pair (within @buffer)
115 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
116 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
118 * Scope of @goal and returned block number is only within this bitmap buffer,
119 * not entire rgrp or filesystem. @buffer will be offset from the actual
120 * beginning of a bitmap block buffer, skipping any header structures.
122 * Return: the block number (bitmap buffer scope) that was found
125 static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
126 unsigned int buflen, u32 goal,
127 unsigned char old_state)
129 unsigned char *byte, *end, alloc;
133 byte = buffer + (goal / GFS2_NBBY);
134 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
135 end = buffer + buflen;
136 alloc = (old_state & 1) ? 0 : 0x55;
139 if ((*byte & 0x55) == alloc) {
140 blk += (8 - bit) >> 1;
148 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
151 bit += GFS2_BIT_SIZE;
164 * gfs2_bitcount - count the number of bits in a certain state
165 * @buffer: the buffer that holds the bitmaps
166 * @buflen: the length (in bytes) of the buffer
167 * @state: the state of the block we're looking for
169 * Returns: The number of bits
172 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
173 unsigned int buflen, unsigned char state)
175 unsigned char *byte = buffer;
176 unsigned char *end = buffer + buflen;
177 unsigned char state1 = state << 2;
178 unsigned char state2 = state << 4;
179 unsigned char state3 = state << 6;
182 for (; byte < end; byte++) {
183 if (((*byte) & 0x03) == state)
185 if (((*byte) & 0x0C) == state1)
187 if (((*byte) & 0x30) == state2)
189 if (((*byte) & 0xC0) == state3)
197 * gfs2_rgrp_verify - Verify that a resource group is consistent
198 * @sdp: the filesystem
203 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
205 struct gfs2_sbd *sdp = rgd->rd_sbd;
206 struct gfs2_bitmap *bi = NULL;
207 u32 length = rgd->rd_length;
211 memset(count, 0, 4 * sizeof(u32));
213 /* Count # blocks in each of 4 possible allocation states */
214 for (buf = 0; buf < length; buf++) {
215 bi = rgd->rd_bits + buf;
216 for (x = 0; x < 4; x++)
217 count[x] += gfs2_bitcount(rgd,
223 if (count[0] != rgd->rd_rg.rg_free) {
224 if (gfs2_consist_rgrpd(rgd))
225 fs_err(sdp, "free data mismatch: %u != %u\n",
226 count[0], rgd->rd_rg.rg_free);
232 rgd->rd_rg.rg_dinodes;
233 if (count[1] + count[2] != tmp) {
234 if (gfs2_consist_rgrpd(rgd))
235 fs_err(sdp, "used data mismatch: %u != %u\n",
240 if (count[3] != rgd->rd_rg.rg_dinodes) {
241 if (gfs2_consist_rgrpd(rgd))
242 fs_err(sdp, "used metadata mismatch: %u != %u\n",
243 count[3], rgd->rd_rg.rg_dinodes);
247 if (count[2] > count[3]) {
248 if (gfs2_consist_rgrpd(rgd))
249 fs_err(sdp, "unlinked inodes > inodes: %u\n",
256 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
258 u64 first = rgd->rd_data0;
259 u64 last = first + rgd->rd_data;
260 return first <= block && block < last;
264 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
265 * @sdp: The GFS2 superblock
266 * @n: The data block number
268 * Returns: The resource group, or NULL if not found
271 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
273 struct gfs2_rgrpd *rgd;
275 spin_lock(&sdp->sd_rindex_spin);
277 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
278 if (rgrp_contains_block(rgd, blk)) {
279 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
280 spin_unlock(&sdp->sd_rindex_spin);
285 spin_unlock(&sdp->sd_rindex_spin);
291 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
292 * @sdp: The GFS2 superblock
294 * Returns: The first rgrp in the filesystem
297 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
299 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
300 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
304 * gfs2_rgrpd_get_next - get the next RG
307 * Returns: The next rgrp
310 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
312 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
314 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
317 static void clear_rgrpdi(struct gfs2_sbd *sdp)
319 struct list_head *head;
320 struct gfs2_rgrpd *rgd;
321 struct gfs2_glock *gl;
323 spin_lock(&sdp->sd_rindex_spin);
324 sdp->sd_rindex_forward = NULL;
325 head = &sdp->sd_rindex_recent_list;
326 while (!list_empty(head)) {
327 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
328 list_del(&rgd->rd_recent);
330 spin_unlock(&sdp->sd_rindex_spin);
332 head = &sdp->sd_rindex_list;
333 while (!list_empty(head)) {
334 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
337 list_del(&rgd->rd_list);
338 list_del(&rgd->rd_list_mru);
341 gl->gl_object = NULL;
350 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
352 mutex_lock(&sdp->sd_rindex_mutex);
354 mutex_unlock(&sdp->sd_rindex_mutex);
357 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
359 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
360 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
361 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
362 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
363 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
367 * gfs2_compute_bitstructs - Compute the bitmap sizes
368 * @rgd: The resource group descriptor
370 * Calculates bitmap descriptors, one for each block that contains bitmap data
375 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
377 struct gfs2_sbd *sdp = rgd->rd_sbd;
378 struct gfs2_bitmap *bi;
379 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
380 u32 bytes_left, bytes;
386 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
390 bytes_left = rgd->rd_bitbytes;
392 for (x = 0; x < length; x++) {
393 bi = rgd->rd_bits + x;
395 /* small rgrp; bitmap stored completely in header block */
398 bi->bi_offset = sizeof(struct gfs2_rgrp);
403 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
404 bi->bi_offset = sizeof(struct gfs2_rgrp);
408 } else if (x + 1 == length) {
410 bi->bi_offset = sizeof(struct gfs2_meta_header);
411 bi->bi_start = rgd->rd_bitbytes - bytes_left;
415 bytes = sdp->sd_sb.sb_bsize -
416 sizeof(struct gfs2_meta_header);
417 bi->bi_offset = sizeof(struct gfs2_meta_header);
418 bi->bi_start = rgd->rd_bitbytes - bytes_left;
426 gfs2_consist_rgrpd(rgd);
429 bi = rgd->rd_bits + (length - 1);
430 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
431 if (gfs2_consist_rgrpd(rgd)) {
432 gfs2_rindex_print(rgd);
433 fs_err(sdp, "start=%u len=%u offset=%u\n",
434 bi->bi_start, bi->bi_len, bi->bi_offset);
444 * gfs2_ri_total - Total up the file system space, according to the rindex.
447 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
450 struct inode *inode = sdp->sd_rindex;
451 struct gfs2_inode *ip = GFS2_I(inode);
452 char buf[sizeof(struct gfs2_rindex)];
453 struct file_ra_state ra_state;
456 mutex_lock(&sdp->sd_rindex_mutex);
457 file_ra_state_init(&ra_state, inode->i_mapping);
458 for (rgrps = 0;; rgrps++) {
459 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
461 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
463 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
464 sizeof(struct gfs2_rindex));
465 if (error != sizeof(struct gfs2_rindex))
467 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
469 mutex_unlock(&sdp->sd_rindex_mutex);
473 static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
475 const struct gfs2_rindex *str = buf;
477 rgd->rd_addr = be64_to_cpu(str->ri_addr);
478 rgd->rd_length = be32_to_cpu(str->ri_length);
479 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
480 rgd->rd_data = be32_to_cpu(str->ri_data);
481 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
485 * read_rindex_entry - Pull in a new resource index entry from the disk
486 * @gl: The glock covering the rindex inode
488 * Returns: 0 on success, error code otherwise
491 static int read_rindex_entry(struct gfs2_inode *ip,
492 struct file_ra_state *ra_state)
494 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
495 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
496 char buf[sizeof(struct gfs2_rindex)];
498 struct gfs2_rgrpd *rgd;
500 error = gfs2_internal_read(ip, ra_state, buf, &pos,
501 sizeof(struct gfs2_rindex));
504 if (error != sizeof(struct gfs2_rindex)) {
510 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
515 mutex_init(&rgd->rd_mutex);
516 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
519 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
520 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
522 gfs2_rindex_in(rgd, buf);
523 error = compute_bitstructs(rgd);
527 error = gfs2_glock_get(sdp, rgd->rd_addr,
528 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
532 rgd->rd_gl->gl_object = rgd;
533 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
538 * gfs2_ri_update - Pull in a new resource index from the disk
539 * @ip: pointer to the rindex inode
541 * Returns: 0 on successful update, error code otherwise
544 static int gfs2_ri_update(struct gfs2_inode *ip)
546 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
547 struct inode *inode = &ip->i_inode;
548 struct file_ra_state ra_state;
549 u64 rgrp_count = ip->i_di.di_size;
552 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
553 gfs2_consist_inode(ip);
559 file_ra_state_init(&ra_state, inode->i_mapping);
560 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
561 error = read_rindex_entry(ip, &ra_state);
568 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
573 * gfs2_ri_update_special - Pull in a new resource index from the disk
575 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
576 * In this case we know that we don't have any resource groups in memory yet.
578 * @ip: pointer to the rindex inode
580 * Returns: 0 on successful update, error code otherwise
582 static int gfs2_ri_update_special(struct gfs2_inode *ip)
584 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
585 struct inode *inode = &ip->i_inode;
586 struct file_ra_state ra_state;
589 file_ra_state_init(&ra_state, inode->i_mapping);
590 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
591 /* Ignore partials */
592 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
595 error = read_rindex_entry(ip, &ra_state);
602 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
607 * gfs2_rindex_hold - Grab a lock on the rindex
608 * @sdp: The GFS2 superblock
609 * @ri_gh: the glock holder
611 * We grab a lock on the rindex inode to make sure that it doesn't
612 * change whilst we are performing an operation. We keep this lock
613 * for quite long periods of time compared to other locks. This
614 * doesn't matter, since it is shared and it is very, very rarely
615 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
617 * This makes sure that we're using the latest copy of the resource index
618 * special file, which might have been updated if someone expanded the
619 * filesystem (via gfs2_grow utility), which adds new resource groups.
621 * Returns: 0 on success, error code otherwise
624 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
626 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
627 struct gfs2_glock *gl = ip->i_gl;
630 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
634 /* Read new copy from disk if we don't have the latest */
635 if (sdp->sd_rindex_vn != gl->gl_vn) {
636 mutex_lock(&sdp->sd_rindex_mutex);
637 if (sdp->sd_rindex_vn != gl->gl_vn) {
638 error = gfs2_ri_update(ip);
640 gfs2_glock_dq_uninit(ri_gh);
642 mutex_unlock(&sdp->sd_rindex_mutex);
648 static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
650 const struct gfs2_rgrp *str = buf;
652 rg->rg_flags = be32_to_cpu(str->rg_flags);
653 rg->rg_free = be32_to_cpu(str->rg_free);
654 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
655 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
658 static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
660 struct gfs2_rgrp *str = buf;
662 str->rg_flags = cpu_to_be32(rg->rg_flags);
663 str->rg_free = cpu_to_be32(rg->rg_free);
664 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
665 str->__pad = cpu_to_be32(0);
666 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
667 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
671 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
672 * @rgd: the struct gfs2_rgrpd describing the RG to read in
674 * Read in all of a Resource Group's header and bitmap blocks.
675 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
680 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
682 struct gfs2_sbd *sdp = rgd->rd_sbd;
683 struct gfs2_glock *gl = rgd->rd_gl;
684 unsigned int length = rgd->rd_length;
685 struct gfs2_bitmap *bi;
689 mutex_lock(&rgd->rd_mutex);
691 spin_lock(&sdp->sd_rindex_spin);
692 if (rgd->rd_bh_count) {
694 spin_unlock(&sdp->sd_rindex_spin);
695 mutex_unlock(&rgd->rd_mutex);
698 spin_unlock(&sdp->sd_rindex_spin);
700 for (x = 0; x < length; x++) {
701 bi = rgd->rd_bits + x;
702 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
707 for (y = length; y--;) {
708 bi = rgd->rd_bits + y;
709 error = gfs2_meta_wait(sdp, bi->bi_bh);
712 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
719 if (rgd->rd_rg_vn != gl->gl_vn) {
720 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
721 rgd->rd_rg_vn = gl->gl_vn;
724 spin_lock(&sdp->sd_rindex_spin);
725 rgd->rd_free_clone = rgd->rd_rg.rg_free;
727 spin_unlock(&sdp->sd_rindex_spin);
729 mutex_unlock(&rgd->rd_mutex);
735 bi = rgd->rd_bits + x;
738 gfs2_assert_warn(sdp, !bi->bi_clone);
740 mutex_unlock(&rgd->rd_mutex);
745 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
747 struct gfs2_sbd *sdp = rgd->rd_sbd;
749 spin_lock(&sdp->sd_rindex_spin);
750 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
752 spin_unlock(&sdp->sd_rindex_spin);
756 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
757 * @rgd: the struct gfs2_rgrpd describing the RG to read in
761 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
763 struct gfs2_sbd *sdp = rgd->rd_sbd;
764 int x, length = rgd->rd_length;
766 spin_lock(&sdp->sd_rindex_spin);
767 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
768 if (--rgd->rd_bh_count) {
769 spin_unlock(&sdp->sd_rindex_spin);
773 for (x = 0; x < length; x++) {
774 struct gfs2_bitmap *bi = rgd->rd_bits + x;
781 spin_unlock(&sdp->sd_rindex_spin);
784 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
786 struct gfs2_sbd *sdp = rgd->rd_sbd;
787 unsigned int length = rgd->rd_length;
790 for (x = 0; x < length; x++) {
791 struct gfs2_bitmap *bi = rgd->rd_bits + x;
794 memcpy(bi->bi_clone + bi->bi_offset,
795 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
798 spin_lock(&sdp->sd_rindex_spin);
799 rgd->rd_free_clone = rgd->rd_rg.rg_free;
800 spin_unlock(&sdp->sd_rindex_spin);
804 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
805 * @ip: the incore GFS2 inode structure
807 * Returns: the struct gfs2_alloc
810 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
812 struct gfs2_alloc *al = &ip->i_alloc;
814 /* FIXME: Should assert that the correct locks are held here... */
815 memset(al, 0, sizeof(*al));
820 * try_rgrp_fit - See if a given reservation will fit in a given RG
822 * @al: the struct gfs2_alloc structure describing the reservation
824 * If there's room for the requested blocks to be allocated from the RG:
825 * Sets the $al_rgd field in @al.
827 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
830 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
832 struct gfs2_sbd *sdp = rgd->rd_sbd;
835 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
838 spin_lock(&sdp->sd_rindex_spin);
839 if (rgd->rd_free_clone >= al->al_requested) {
843 spin_unlock(&sdp->sd_rindex_spin);
849 * recent_rgrp_first - get first RG from "recent" list
850 * @sdp: The GFS2 superblock
851 * @rglast: address of the rgrp used last
853 * Returns: The first rgrp in the recent list
856 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
859 struct gfs2_rgrpd *rgd = NULL;
861 spin_lock(&sdp->sd_rindex_spin);
863 if (list_empty(&sdp->sd_rindex_recent_list))
869 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
870 if (rgd->rd_addr == rglast)
875 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
878 spin_unlock(&sdp->sd_rindex_spin);
883 * recent_rgrp_next - get next RG from "recent" list
884 * @cur_rgd: current rgrp
887 * Returns: The next rgrp in the recent list
890 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
893 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
894 struct list_head *head;
895 struct gfs2_rgrpd *rgd;
897 spin_lock(&sdp->sd_rindex_spin);
899 head = &sdp->sd_rindex_recent_list;
901 list_for_each_entry(rgd, head, rd_recent) {
902 if (rgd == cur_rgd) {
903 if (cur_rgd->rd_recent.next != head)
904 rgd = list_entry(cur_rgd->rd_recent.next,
905 struct gfs2_rgrpd, rd_recent);
910 list_del(&cur_rgd->rd_recent);
917 if (!list_empty(head))
918 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
921 spin_unlock(&sdp->sd_rindex_spin);
926 * recent_rgrp_add - add an RG to tail of "recent" list
927 * @new_rgd: The rgrp to add
931 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
933 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
934 struct gfs2_rgrpd *rgd;
935 unsigned int count = 0;
936 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
938 spin_lock(&sdp->sd_rindex_spin);
940 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
947 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
950 spin_unlock(&sdp->sd_rindex_spin);
954 * forward_rgrp_get - get an rgrp to try next from full list
955 * @sdp: The GFS2 superblock
957 * Returns: The rgrp to try next
960 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
962 struct gfs2_rgrpd *rgd;
963 unsigned int journals = gfs2_jindex_size(sdp);
964 unsigned int rg = 0, x;
966 spin_lock(&sdp->sd_rindex_spin);
968 rgd = sdp->sd_rindex_forward;
970 if (sdp->sd_rgrps >= journals)
971 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
973 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
974 x++, rgd = gfs2_rgrpd_get_next(rgd))
977 sdp->sd_rindex_forward = rgd;
980 spin_unlock(&sdp->sd_rindex_spin);
986 * forward_rgrp_set - set the forward rgrp pointer
987 * @sdp: the filesystem
988 * @rgd: The new forward rgrp
992 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
994 spin_lock(&sdp->sd_rindex_spin);
995 sdp->sd_rindex_forward = rgd;
996 spin_unlock(&sdp->sd_rindex_spin);
1000 * get_local_rgrp - Choose and lock a rgrp for allocation
1001 * @ip: the inode to reserve space for
1002 * @rgp: the chosen and locked rgrp
1004 * Try to acquire rgrp in way which avoids contending with others.
1009 static int get_local_rgrp(struct gfs2_inode *ip)
1011 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1012 struct gfs2_rgrpd *rgd, *begin = NULL;
1013 struct gfs2_alloc *al = &ip->i_alloc;
1014 int flags = LM_FLAG_TRY;
1019 /* Try recently successful rgrps */
1021 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
1024 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1025 LM_FLAG_TRY, &al->al_rgd_gh);
1028 if (try_rgrp_fit(rgd, al))
1030 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1031 rgd = recent_rgrp_next(rgd, 1);
1035 rgd = recent_rgrp_next(rgd, 0);
1043 /* Go through full list of rgrps */
1045 begin = rgd = forward_rgrp_get(sdp);
1048 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1052 if (try_rgrp_fit(rgd, al))
1054 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1065 rgd = gfs2_rgrpd_get_next(rgd);
1067 rgd = gfs2_rgrpd_get_first(sdp);
1076 gfs2_log_flush(sdp, NULL);
1081 ip->i_last_rg_alloc = rgd->rd_addr;
1084 recent_rgrp_add(rgd);
1085 rgd = gfs2_rgrpd_get_next(rgd);
1087 rgd = gfs2_rgrpd_get_first(sdp);
1088 forward_rgrp_set(sdp, rgd);
1095 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1096 * @ip: the inode to reserve space for
1101 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1103 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1104 struct gfs2_alloc *al = &ip->i_alloc;
1107 if (gfs2_assert_warn(sdp, al->al_requested))
1110 /* We need to hold the rindex unless the inode we're using is
1111 the rindex itself, in which case it's already held. */
1112 if (ip != GFS2_I(sdp->sd_rindex))
1113 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1114 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1115 error = gfs2_ri_update_special(ip);
1120 error = get_local_rgrp(ip);
1122 if (ip != GFS2_I(sdp->sd_rindex))
1123 gfs2_glock_dq_uninit(&al->al_ri_gh);
1134 * gfs2_inplace_release - release an inplace reservation
1135 * @ip: the inode the reservation was taken out on
1137 * Release a reservation made by gfs2_inplace_reserve().
1140 void gfs2_inplace_release(struct gfs2_inode *ip)
1142 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1143 struct gfs2_alloc *al = &ip->i_alloc;
1145 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1146 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1147 "al_file = %s, al_line = %u\n",
1148 al->al_alloced, al->al_requested, al->al_file,
1152 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1153 if (ip != GFS2_I(sdp->sd_rindex))
1154 gfs2_glock_dq_uninit(&al->al_ri_gh);
1158 * gfs2_get_block_type - Check a block in a RG is of given type
1159 * @rgd: the resource group holding the block
1160 * @block: the block number
1162 * Returns: The block type (GFS2_BLKST_*)
1165 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1167 struct gfs2_bitmap *bi = NULL;
1168 u32 length, rgrp_block, buf_block;
1172 length = rgd->rd_length;
1173 rgrp_block = block - rgd->rd_data0;
1175 for (buf = 0; buf < length; buf++) {
1176 bi = rgd->rd_bits + buf;
1177 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1181 gfs2_assert(rgd->rd_sbd, buf < length);
1182 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1184 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1185 bi->bi_len, buf_block);
1191 * rgblk_search - find a block in @old_state, change allocation
1192 * state to @new_state
1193 * @rgd: the resource group descriptor
1194 * @goal: the goal block within the RG (start here to search for avail block)
1195 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1196 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1198 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1199 * Add the found bitmap buffer to the transaction.
1200 * Set the found bits to @new_state to change block's allocation state.
1202 * This function never fails, because we wouldn't call it unless we
1203 * know (from reservation results, etc.) that a block is available.
1205 * Scope of @goal and returned block is just within rgrp, not the whole
1208 * Returns: the block number allocated
1211 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1212 unsigned char old_state, unsigned char new_state)
1214 struct gfs2_bitmap *bi = NULL;
1215 u32 length = rgd->rd_length;
1217 unsigned int buf, x;
1219 /* Find bitmap block that contains bits for goal block */
1220 for (buf = 0; buf < length; buf++) {
1221 bi = rgd->rd_bits + buf;
1222 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1226 gfs2_assert(rgd->rd_sbd, buf < length);
1228 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1229 goal -= bi->bi_start * GFS2_NBBY;
1231 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1232 "x <= length", instead of "x < length", because we typically start
1233 the search in the middle of a bit block, but if we can't find an
1234 allocatable block anywhere else, we want to be able wrap around and
1235 search in the first part of our first-searched bit block. */
1236 for (x = 0; x <= length; x++) {
1238 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
1239 bi->bi_len, goal, old_state);
1241 blk = gfs2_bitfit(rgd,
1242 bi->bi_bh->b_data + bi->bi_offset,
1243 bi->bi_len, goal, old_state);
1244 if (blk != BFITNOENT)
1247 /* Try next bitmap block (wrap back to rgrp header if at end) */
1248 buf = (buf + 1) % length;
1249 bi = rgd->rd_bits + buf;
1253 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1256 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1257 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1258 bi->bi_len, blk, new_state);
1260 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1261 bi->bi_len, blk, new_state);
1263 return bi->bi_start * GFS2_NBBY + blk;
1267 * rgblk_free - Change alloc state of given block(s)
1268 * @sdp: the filesystem
1269 * @bstart: the start of a run of blocks to free
1270 * @blen: the length of the block run (all must lie within ONE RG!)
1271 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1273 * Returns: Resource group containing the block(s)
1276 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1277 u32 blen, unsigned char new_state)
1279 struct gfs2_rgrpd *rgd;
1280 struct gfs2_bitmap *bi = NULL;
1281 u32 length, rgrp_blk, buf_blk;
1284 rgd = gfs2_blk2rgrpd(sdp, bstart);
1286 if (gfs2_consist(sdp))
1287 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1291 length = rgd->rd_length;
1293 rgrp_blk = bstart - rgd->rd_data0;
1296 for (buf = 0; buf < length; buf++) {
1297 bi = rgd->rd_bits + buf;
1298 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1302 gfs2_assert(rgd->rd_sbd, buf < length);
1304 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1307 if (!bi->bi_clone) {
1308 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1309 GFP_NOFS | __GFP_NOFAIL);
1310 memcpy(bi->bi_clone + bi->bi_offset,
1311 bi->bi_bh->b_data + bi->bi_offset,
1314 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1315 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1316 bi->bi_len, buf_blk, new_state);
1323 * gfs2_alloc_data - Allocate a data block
1324 * @ip: the inode to allocate the data block for
1326 * Returns: the allocated block
1329 u64 gfs2_alloc_data(struct gfs2_inode *ip)
1331 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1332 struct gfs2_alloc *al = &ip->i_alloc;
1333 struct gfs2_rgrpd *rgd = al->al_rgd;
1337 if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
1338 goal = ip->i_di.di_goal_data - rgd->rd_data0;
1340 goal = rgd->rd_last_alloc_data;
1342 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1343 rgd->rd_last_alloc_data = blk;
1345 block = rgd->rd_data0 + blk;
1346 ip->i_di.di_goal_data = block;
1348 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1349 rgd->rd_rg.rg_free--;
1351 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1352 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1356 gfs2_statfs_change(sdp, 0, -1, 0);
1357 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1359 spin_lock(&sdp->sd_rindex_spin);
1360 rgd->rd_free_clone--;
1361 spin_unlock(&sdp->sd_rindex_spin);
1367 * gfs2_alloc_meta - Allocate a metadata block
1368 * @ip: the inode to allocate the metadata block for
1370 * Returns: the allocated block
1373 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
1375 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1376 struct gfs2_alloc *al = &ip->i_alloc;
1377 struct gfs2_rgrpd *rgd = al->al_rgd;
1381 if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
1382 goal = ip->i_di.di_goal_meta - rgd->rd_data0;
1384 goal = rgd->rd_last_alloc_meta;
1386 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1387 rgd->rd_last_alloc_meta = blk;
1389 block = rgd->rd_data0 + blk;
1390 ip->i_di.di_goal_meta = block;
1392 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1393 rgd->rd_rg.rg_free--;
1395 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1396 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1400 gfs2_statfs_change(sdp, 0, -1, 0);
1401 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1402 gfs2_trans_add_unrevoke(sdp, block);
1404 spin_lock(&sdp->sd_rindex_spin);
1405 rgd->rd_free_clone--;
1406 spin_unlock(&sdp->sd_rindex_spin);
1412 * gfs2_alloc_di - Allocate a dinode
1413 * @dip: the directory that the inode is going in
1415 * Returns: the block allocated
1418 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1420 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1421 struct gfs2_alloc *al = &dip->i_alloc;
1422 struct gfs2_rgrpd *rgd = al->al_rgd;
1426 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1427 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1429 rgd->rd_last_alloc_meta = blk;
1431 block = rgd->rd_data0 + blk;
1433 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1434 rgd->rd_rg.rg_free--;
1435 rgd->rd_rg.rg_dinodes++;
1436 *generation = rgd->rd_rg.rg_igeneration++;
1437 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1438 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1442 gfs2_statfs_change(sdp, 0, -1, +1);
1443 gfs2_trans_add_unrevoke(sdp, block);
1445 spin_lock(&sdp->sd_rindex_spin);
1446 rgd->rd_free_clone--;
1447 spin_unlock(&sdp->sd_rindex_spin);
1453 * gfs2_free_data - free a contiguous run of data block(s)
1454 * @ip: the inode these blocks are being freed from
1455 * @bstart: first block of a run of contiguous blocks
1456 * @blen: the length of the block run
1460 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1462 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1463 struct gfs2_rgrpd *rgd;
1465 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1469 rgd->rd_rg.rg_free += blen;
1471 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1472 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1474 gfs2_trans_add_rg(rgd);
1476 gfs2_statfs_change(sdp, 0, +blen, 0);
1477 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1481 * gfs2_free_meta - free a contiguous run of data block(s)
1482 * @ip: the inode these blocks are being freed from
1483 * @bstart: first block of a run of contiguous blocks
1484 * @blen: the length of the block run
1488 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1490 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1491 struct gfs2_rgrpd *rgd;
1493 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1497 rgd->rd_rg.rg_free += blen;
1499 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1500 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1502 gfs2_trans_add_rg(rgd);
1504 gfs2_statfs_change(sdp, 0, +blen, 0);
1505 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1506 gfs2_meta_wipe(ip, bstart, blen);
1509 void gfs2_unlink_di(struct inode *inode)
1511 struct gfs2_inode *ip = GFS2_I(inode);
1512 struct gfs2_sbd *sdp = GFS2_SB(inode);
1513 struct gfs2_rgrpd *rgd;
1514 u64 blkno = ip->i_no_addr;
1516 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1519 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1520 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1521 gfs2_trans_add_rg(rgd);
1524 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1526 struct gfs2_sbd *sdp = rgd->rd_sbd;
1527 struct gfs2_rgrpd *tmp_rgd;
1529 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1532 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1534 if (!rgd->rd_rg.rg_dinodes)
1535 gfs2_consist_rgrpd(rgd);
1536 rgd->rd_rg.rg_dinodes--;
1537 rgd->rd_rg.rg_free++;
1539 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1540 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1542 gfs2_statfs_change(sdp, 0, +1, -1);
1543 gfs2_trans_add_rg(rgd);
1547 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1549 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1550 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1551 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1555 * gfs2_rlist_add - add a RG to a list of RGs
1556 * @sdp: the filesystem
1557 * @rlist: the list of resource groups
1560 * Figure out what RG a block belongs to and add that RG to the list
1562 * FIXME: Don't use NOFAIL
1566 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1569 struct gfs2_rgrpd *rgd;
1570 struct gfs2_rgrpd **tmp;
1571 unsigned int new_space;
1574 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1577 rgd = gfs2_blk2rgrpd(sdp, block);
1579 if (gfs2_consist(sdp))
1580 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1584 for (x = 0; x < rlist->rl_rgrps; x++)
1585 if (rlist->rl_rgd[x] == rgd)
1588 if (rlist->rl_rgrps == rlist->rl_space) {
1589 new_space = rlist->rl_space + 10;
1591 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1592 GFP_NOFS | __GFP_NOFAIL);
1594 if (rlist->rl_rgd) {
1595 memcpy(tmp, rlist->rl_rgd,
1596 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1597 kfree(rlist->rl_rgd);
1600 rlist->rl_space = new_space;
1601 rlist->rl_rgd = tmp;
1604 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1608 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1609 * and initialize an array of glock holders for them
1610 * @rlist: the list of resource groups
1611 * @state: the lock state to acquire the RG lock in
1612 * @flags: the modifier flags for the holder structures
1614 * FIXME: Don't use NOFAIL
1618 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1623 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1624 GFP_NOFS | __GFP_NOFAIL);
1625 for (x = 0; x < rlist->rl_rgrps; x++)
1626 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1632 * gfs2_rlist_free - free a resource group list
1633 * @list: the list of resource groups
1637 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1641 kfree(rlist->rl_rgd);
1643 if (rlist->rl_ghs) {
1644 for (x = 0; x < rlist->rl_rgrps; x++)
1645 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1646 kfree(rlist->rl_ghs);