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/prefetch.h>
17 #include <linux/blkdev.h>
18 #include <linux/rbtree.h>
33 #include "trace_gfs2.h"
35 #define BFITNOENT ((u32)~0)
36 #define NO_BLOCK ((u64)~0)
38 #define RSRV_CONTENTION_FACTOR 4
39 #define RGRP_RSRV_MAX_CONTENDERS 2
41 #if BITS_PER_LONG == 32
42 #define LBITMASK (0x55555555UL)
43 #define LBITSKIP55 (0x55555555UL)
44 #define LBITSKIP00 (0x00000000UL)
46 #define LBITMASK (0x5555555555555555UL)
47 #define LBITSKIP55 (0x5555555555555555UL)
48 #define LBITSKIP00 (0x0000000000000000UL)
52 * These routines are used by the resource group routines (rgrp.c)
53 * to keep track of block allocation. Each block is represented by two
54 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
57 * 1 = Used (not metadata)
58 * 2 = Unlinked (still in use) inode
62 static const char valid_change[16] = {
71 * gfs2_setbit - Set a bit in the bitmaps
72 * @rgd: the resource group descriptor
73 * @buf2: the clone buffer that holds the bitmaps
74 * @bi: the bitmap structure
75 * @block: the block to set
76 * @new_state: the new state of the block
80 static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf2,
81 struct gfs2_bitmap *bi, u32 block,
82 unsigned char new_state)
84 unsigned char *byte1, *byte2, *end, cur_state;
85 unsigned int buflen = bi->bi_len;
86 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
88 byte1 = bi->bi_bh->b_data + bi->bi_offset + (block / GFS2_NBBY);
89 end = bi->bi_bh->b_data + bi->bi_offset + buflen;
93 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
95 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
96 printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, "
98 (unsigned long long)block, cur_state, new_state);
99 printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n",
100 (unsigned long long)rgd->rd_addr,
101 (unsigned long)bi->bi_start);
102 printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n",
103 (unsigned long)bi->bi_offset,
104 (unsigned long)bi->bi_len);
106 gfs2_consist_rgrpd(rgd);
109 *byte1 ^= (cur_state ^ new_state) << bit;
112 byte2 = buf2 + bi->bi_offset + (block / GFS2_NBBY);
113 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
114 *byte2 ^= (cur_state ^ new_state) << bit;
119 * gfs2_testbit - test a bit in the bitmaps
120 * @rgd: the resource group descriptor
121 * @buffer: the buffer that holds the bitmaps
122 * @buflen: the length (in bytes) of the buffer
123 * @block: the block to read
127 static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
128 const unsigned char *buffer,
129 unsigned int buflen, u32 block)
131 const unsigned char *byte, *end;
132 unsigned char cur_state;
135 byte = buffer + (block / GFS2_NBBY);
136 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
137 end = buffer + buflen;
139 gfs2_assert(rgd->rd_sbd, byte < end);
141 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
148 * @ptr: Pointer to bitmap data
149 * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
150 * @state: The state we are searching for
152 * We xor the bitmap data with a patter which is the bitwise opposite
153 * of what we are looking for, this gives rise to a pattern of ones
154 * wherever there is a match. Since we have two bits per entry, we
155 * take this pattern, shift it down by one place and then and it with
156 * the original. All the even bit positions (0,2,4, etc) then represent
157 * successful matches, so we mask with 0x55555..... to remove the unwanted
160 * This allows searching of a whole u64 at once (32 blocks) with a
161 * single test (on 64 bit arches).
164 static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
167 static const u64 search[] = {
168 [0] = 0xffffffffffffffffULL,
169 [1] = 0xaaaaaaaaaaaaaaaaULL,
170 [2] = 0x5555555555555555ULL,
171 [3] = 0x0000000000000000ULL,
173 tmp = le64_to_cpu(*ptr) ^ search[state];
180 * rs_cmp - multi-block reservation range compare
181 * @blk: absolute file system block number of the new reservation
182 * @len: number of blocks in the new reservation
183 * @rs: existing reservation to compare against
185 * returns: 1 if the block range is beyond the reach of the reservation
186 * -1 if the block range is before the start of the reservation
187 * 0 if the block range overlaps with the reservation
189 static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
191 u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm);
193 if (blk >= startblk + rs->rs_free)
195 if (blk + len - 1 < startblk)
201 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
202 * a block in a given allocation state.
203 * @buf: the buffer that holds the bitmaps
204 * @len: the length (in bytes) of the buffer
205 * @goal: start search at this block's bit-pair (within @buffer)
206 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
208 * Scope of @goal and returned block number is only within this bitmap buffer,
209 * not entire rgrp or filesystem. @buffer will be offset from the actual
210 * beginning of a bitmap block buffer, skipping any header structures, but
211 * headers are always a multiple of 64 bits long so that the buffer is
212 * always aligned to a 64 bit boundary.
214 * The size of the buffer is in bytes, but is it assumed that it is
215 * always ok to read a complete multiple of 64 bits at the end
216 * of the block in case the end is no aligned to a natural boundary.
218 * Return: the block number (bitmap buffer scope) that was found
221 static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
224 u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
225 const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
226 const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
228 u64 mask = 0x5555555555555555ULL;
231 /* Mask off bits we don't care about at the start of the search */
233 tmp = gfs2_bit_search(ptr, mask, state);
235 while(tmp == 0 && ptr < end) {
236 tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
239 /* Mask off any bits which are more than len bytes from the start */
240 if (ptr == end && (len & (sizeof(u64) - 1)))
241 tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
242 /* Didn't find anything, so return */
247 bit /= 2; /* two bits per entry in the bitmap */
248 return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
252 * gfs2_bitcount - count the number of bits in a certain state
253 * @rgd: the resource group descriptor
254 * @buffer: the buffer that holds the bitmaps
255 * @buflen: the length (in bytes) of the buffer
256 * @state: the state of the block we're looking for
258 * Returns: The number of bits
261 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
262 unsigned int buflen, u8 state)
264 const u8 *byte = buffer;
265 const u8 *end = buffer + buflen;
266 const u8 state1 = state << 2;
267 const u8 state2 = state << 4;
268 const u8 state3 = state << 6;
271 for (; byte < end; byte++) {
272 if (((*byte) & 0x03) == state)
274 if (((*byte) & 0x0C) == state1)
276 if (((*byte) & 0x30) == state2)
278 if (((*byte) & 0xC0) == state3)
286 * gfs2_rgrp_verify - Verify that a resource group is consistent
291 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
293 struct gfs2_sbd *sdp = rgd->rd_sbd;
294 struct gfs2_bitmap *bi = NULL;
295 u32 length = rgd->rd_length;
299 memset(count, 0, 4 * sizeof(u32));
301 /* Count # blocks in each of 4 possible allocation states */
302 for (buf = 0; buf < length; buf++) {
303 bi = rgd->rd_bits + buf;
304 for (x = 0; x < 4; x++)
305 count[x] += gfs2_bitcount(rgd,
311 if (count[0] != rgd->rd_free) {
312 if (gfs2_consist_rgrpd(rgd))
313 fs_err(sdp, "free data mismatch: %u != %u\n",
314 count[0], rgd->rd_free);
318 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
319 if (count[1] != tmp) {
320 if (gfs2_consist_rgrpd(rgd))
321 fs_err(sdp, "used data mismatch: %u != %u\n",
326 if (count[2] + count[3] != rgd->rd_dinodes) {
327 if (gfs2_consist_rgrpd(rgd))
328 fs_err(sdp, "used metadata mismatch: %u != %u\n",
329 count[2] + count[3], rgd->rd_dinodes);
334 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
336 u64 first = rgd->rd_data0;
337 u64 last = first + rgd->rd_data;
338 return first <= block && block < last;
342 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
343 * @sdp: The GFS2 superblock
344 * @blk: The data block number
345 * @exact: True if this needs to be an exact match
347 * Returns: The resource group, or NULL if not found
350 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
352 struct rb_node *n, *next;
353 struct gfs2_rgrpd *cur;
355 spin_lock(&sdp->sd_rindex_spin);
356 n = sdp->sd_rindex_tree.rb_node;
358 cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
360 if (blk < cur->rd_addr)
362 else if (blk >= cur->rd_data0 + cur->rd_data)
365 spin_unlock(&sdp->sd_rindex_spin);
367 if (blk < cur->rd_addr)
369 if (blk >= cur->rd_data0 + cur->rd_data)
376 spin_unlock(&sdp->sd_rindex_spin);
382 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
383 * @sdp: The GFS2 superblock
385 * Returns: The first rgrp in the filesystem
388 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
390 const struct rb_node *n;
391 struct gfs2_rgrpd *rgd;
393 spin_lock(&sdp->sd_rindex_spin);
394 n = rb_first(&sdp->sd_rindex_tree);
395 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
396 spin_unlock(&sdp->sd_rindex_spin);
402 * gfs2_rgrpd_get_next - get the next RG
403 * @rgd: the resource group descriptor
405 * Returns: The next rgrp
408 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
410 struct gfs2_sbd *sdp = rgd->rd_sbd;
411 const struct rb_node *n;
413 spin_lock(&sdp->sd_rindex_spin);
414 n = rb_next(&rgd->rd_node);
416 n = rb_first(&sdp->sd_rindex_tree);
418 if (unlikely(&rgd->rd_node == n)) {
419 spin_unlock(&sdp->sd_rindex_spin);
422 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
423 spin_unlock(&sdp->sd_rindex_spin);
427 void gfs2_free_clones(struct gfs2_rgrpd *rgd)
431 for (x = 0; x < rgd->rd_length; x++) {
432 struct gfs2_bitmap *bi = rgd->rd_bits + x;
439 * gfs2_rs_alloc - make sure we have a reservation assigned to the inode
440 * @ip: the inode for this reservation
442 int gfs2_rs_alloc(struct gfs2_inode *ip)
445 struct gfs2_blkreserv *res;
450 res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS);
454 RB_CLEAR_NODE(&res->rs_node);
456 down_write(&ip->i_rw_mutex);
458 kmem_cache_free(gfs2_rsrv_cachep, res);
461 up_write(&ip->i_rw_mutex);
465 static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs)
467 gfs2_print_dbg(seq, " r: %llu s:%llu b:%u f:%u\n",
468 rs->rs_rbm.rgd->rd_addr, gfs2_rbm_to_block(&rs->rs_rbm),
469 rs->rs_rbm.offset, rs->rs_free);
473 * __rs_deltree - remove a multi-block reservation from the rgd tree
474 * @rs: The reservation to remove
477 static void __rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
479 struct gfs2_rgrpd *rgd;
481 if (!gfs2_rs_active(rs))
484 rgd = rs->rs_rbm.rgd;
485 trace_gfs2_rs(ip, rs, TRACE_RS_TREEDEL);
486 rb_erase(&rs->rs_node, &rgd->rd_rstree);
487 RB_CLEAR_NODE(&rs->rs_node);
488 BUG_ON(!rgd->rd_rs_cnt);
492 /* return reserved blocks to the rgrp and the ip */
493 BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free);
494 rs->rs_rbm.rgd->rd_reserved -= rs->rs_free;
496 clear_bit(GBF_FULL, &rs->rs_rbm.bi->bi_flags);
497 smp_mb__after_clear_bit();
502 * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
503 * @rs: The reservation to remove
506 void gfs2_rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
508 struct gfs2_rgrpd *rgd;
510 rgd = rs->rs_rbm.rgd;
512 spin_lock(&rgd->rd_rsspin);
513 __rs_deltree(ip, rs);
514 spin_unlock(&rgd->rd_rsspin);
519 * gfs2_rs_delete - delete a multi-block reservation
520 * @ip: The inode for this reservation
523 void gfs2_rs_delete(struct gfs2_inode *ip)
525 down_write(&ip->i_rw_mutex);
527 gfs2_rs_deltree(ip, ip->i_res);
528 trace_gfs2_rs(ip, ip->i_res, TRACE_RS_DELETE);
529 BUG_ON(ip->i_res->rs_free);
530 kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
533 up_write(&ip->i_rw_mutex);
537 * return_all_reservations - return all reserved blocks back to the rgrp.
538 * @rgd: the rgrp that needs its space back
540 * We previously reserved a bunch of blocks for allocation. Now we need to
541 * give them back. This leave the reservation structures in tact, but removes
542 * all of their corresponding "no-fly zones".
544 static void return_all_reservations(struct gfs2_rgrpd *rgd)
547 struct gfs2_blkreserv *rs;
549 spin_lock(&rgd->rd_rsspin);
550 while ((n = rb_first(&rgd->rd_rstree))) {
551 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
552 __rs_deltree(NULL, rs);
554 spin_unlock(&rgd->rd_rsspin);
557 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
560 struct gfs2_rgrpd *rgd;
561 struct gfs2_glock *gl;
563 while ((n = rb_first(&sdp->sd_rindex_tree))) {
564 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
567 rb_erase(n, &sdp->sd_rindex_tree);
570 spin_lock(&gl->gl_spin);
571 gl->gl_object = NULL;
572 spin_unlock(&gl->gl_spin);
573 gfs2_glock_add_to_lru(gl);
577 gfs2_free_clones(rgd);
579 return_all_reservations(rgd);
580 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
584 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
586 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
587 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
588 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
589 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
590 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
594 * gfs2_compute_bitstructs - Compute the bitmap sizes
595 * @rgd: The resource group descriptor
597 * Calculates bitmap descriptors, one for each block that contains bitmap data
602 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
604 struct gfs2_sbd *sdp = rgd->rd_sbd;
605 struct gfs2_bitmap *bi;
606 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
607 u32 bytes_left, bytes;
613 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
617 bytes_left = rgd->rd_bitbytes;
619 for (x = 0; x < length; x++) {
620 bi = rgd->rd_bits + x;
623 /* small rgrp; bitmap stored completely in header block */
626 bi->bi_offset = sizeof(struct gfs2_rgrp);
631 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
632 bi->bi_offset = sizeof(struct gfs2_rgrp);
636 } else if (x + 1 == length) {
638 bi->bi_offset = sizeof(struct gfs2_meta_header);
639 bi->bi_start = rgd->rd_bitbytes - bytes_left;
643 bytes = sdp->sd_sb.sb_bsize -
644 sizeof(struct gfs2_meta_header);
645 bi->bi_offset = sizeof(struct gfs2_meta_header);
646 bi->bi_start = rgd->rd_bitbytes - bytes_left;
654 gfs2_consist_rgrpd(rgd);
657 bi = rgd->rd_bits + (length - 1);
658 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
659 if (gfs2_consist_rgrpd(rgd)) {
660 gfs2_rindex_print(rgd);
661 fs_err(sdp, "start=%u len=%u offset=%u\n",
662 bi->bi_start, bi->bi_len, bi->bi_offset);
671 * gfs2_ri_total - Total up the file system space, according to the rindex.
672 * @sdp: the filesystem
675 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
678 struct inode *inode = sdp->sd_rindex;
679 struct gfs2_inode *ip = GFS2_I(inode);
680 char buf[sizeof(struct gfs2_rindex)];
683 for (rgrps = 0;; rgrps++) {
684 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
686 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
688 error = gfs2_internal_read(ip, buf, &pos,
689 sizeof(struct gfs2_rindex));
690 if (error != sizeof(struct gfs2_rindex))
692 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
697 static int rgd_insert(struct gfs2_rgrpd *rgd)
699 struct gfs2_sbd *sdp = rgd->rd_sbd;
700 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
702 /* Figure out where to put new node */
704 struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
708 if (rgd->rd_addr < cur->rd_addr)
709 newn = &((*newn)->rb_left);
710 else if (rgd->rd_addr > cur->rd_addr)
711 newn = &((*newn)->rb_right);
716 rb_link_node(&rgd->rd_node, parent, newn);
717 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
723 * read_rindex_entry - Pull in a new resource index entry from the disk
724 * @ip: Pointer to the rindex inode
726 * Returns: 0 on success, > 0 on EOF, error code otherwise
729 static int read_rindex_entry(struct gfs2_inode *ip)
731 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
732 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
733 struct gfs2_rindex buf;
735 struct gfs2_rgrpd *rgd;
737 if (pos >= i_size_read(&ip->i_inode))
740 error = gfs2_internal_read(ip, (char *)&buf, &pos,
741 sizeof(struct gfs2_rindex));
743 if (error != sizeof(struct gfs2_rindex))
744 return (error == 0) ? 1 : error;
746 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
752 rgd->rd_addr = be64_to_cpu(buf.ri_addr);
753 rgd->rd_length = be32_to_cpu(buf.ri_length);
754 rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
755 rgd->rd_data = be32_to_cpu(buf.ri_data);
756 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
757 spin_lock_init(&rgd->rd_rsspin);
759 error = compute_bitstructs(rgd);
763 error = gfs2_glock_get(sdp, rgd->rd_addr,
764 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
768 rgd->rd_gl->gl_object = rgd;
769 rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lvb;
770 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
771 if (rgd->rd_data > sdp->sd_max_rg_data)
772 sdp->sd_max_rg_data = rgd->rd_data;
773 spin_lock(&sdp->sd_rindex_spin);
774 error = rgd_insert(rgd);
775 spin_unlock(&sdp->sd_rindex_spin);
779 error = 0; /* someone else read in the rgrp; free it and ignore it */
780 gfs2_glock_put(rgd->rd_gl);
784 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
789 * gfs2_ri_update - Pull in a new resource index from the disk
790 * @ip: pointer to the rindex inode
792 * Returns: 0 on successful update, error code otherwise
795 static int gfs2_ri_update(struct gfs2_inode *ip)
797 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
801 error = read_rindex_entry(ip);
802 } while (error == 0);
807 sdp->sd_rindex_uptodate = 1;
812 * gfs2_rindex_update - Update the rindex if required
813 * @sdp: The GFS2 superblock
815 * We grab a lock on the rindex inode to make sure that it doesn't
816 * change whilst we are performing an operation. We keep this lock
817 * for quite long periods of time compared to other locks. This
818 * doesn't matter, since it is shared and it is very, very rarely
819 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
821 * This makes sure that we're using the latest copy of the resource index
822 * special file, which might have been updated if someone expanded the
823 * filesystem (via gfs2_grow utility), which adds new resource groups.
825 * Returns: 0 on succeess, error code otherwise
828 int gfs2_rindex_update(struct gfs2_sbd *sdp)
830 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
831 struct gfs2_glock *gl = ip->i_gl;
832 struct gfs2_holder ri_gh;
834 int unlock_required = 0;
836 /* Read new copy from disk if we don't have the latest */
837 if (!sdp->sd_rindex_uptodate) {
838 if (!gfs2_glock_is_locked_by_me(gl)) {
839 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
844 if (!sdp->sd_rindex_uptodate)
845 error = gfs2_ri_update(ip);
847 gfs2_glock_dq_uninit(&ri_gh);
853 static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
855 const struct gfs2_rgrp *str = buf;
858 rg_flags = be32_to_cpu(str->rg_flags);
859 rg_flags &= ~GFS2_RDF_MASK;
860 rgd->rd_flags &= GFS2_RDF_MASK;
861 rgd->rd_flags |= rg_flags;
862 rgd->rd_free = be32_to_cpu(str->rg_free);
863 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
864 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
867 static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
869 struct gfs2_rgrp *str = buf;
871 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
872 str->rg_free = cpu_to_be32(rgd->rd_free);
873 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
874 str->__pad = cpu_to_be32(0);
875 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
876 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
879 static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
881 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
882 struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
884 if (rgl->rl_flags != str->rg_flags || rgl->rl_free != str->rg_free ||
885 rgl->rl_dinodes != str->rg_dinodes ||
886 rgl->rl_igeneration != str->rg_igeneration)
891 static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
893 const struct gfs2_rgrp *str = buf;
895 rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
896 rgl->rl_flags = str->rg_flags;
897 rgl->rl_free = str->rg_free;
898 rgl->rl_dinodes = str->rg_dinodes;
899 rgl->rl_igeneration = str->rg_igeneration;
903 static void update_rgrp_lvb_unlinked(struct gfs2_rgrpd *rgd, u32 change)
905 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
906 u32 unlinked = be32_to_cpu(rgl->rl_unlinked) + change;
907 rgl->rl_unlinked = cpu_to_be32(unlinked);
910 static u32 count_unlinked(struct gfs2_rgrpd *rgd)
912 struct gfs2_bitmap *bi;
913 const u32 length = rgd->rd_length;
914 const u8 *buffer = NULL;
915 u32 i, goal, count = 0;
917 for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
919 buffer = bi->bi_bh->b_data + bi->bi_offset;
920 WARN_ON(!buffer_uptodate(bi->bi_bh));
921 while (goal < bi->bi_len * GFS2_NBBY) {
922 goal = gfs2_bitfit(buffer, bi->bi_len, goal,
923 GFS2_BLKST_UNLINKED);
924 if (goal == BFITNOENT)
936 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
937 * @rgd: the struct gfs2_rgrpd describing the RG to read in
939 * Read in all of a Resource Group's header and bitmap blocks.
940 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
945 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
947 struct gfs2_sbd *sdp = rgd->rd_sbd;
948 struct gfs2_glock *gl = rgd->rd_gl;
949 unsigned int length = rgd->rd_length;
950 struct gfs2_bitmap *bi;
954 if (rgd->rd_bits[0].bi_bh != NULL)
957 for (x = 0; x < length; x++) {
958 bi = rgd->rd_bits + x;
959 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
964 for (y = length; y--;) {
965 bi = rgd->rd_bits + y;
966 error = gfs2_meta_wait(sdp, bi->bi_bh);
969 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
976 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
977 for (x = 0; x < length; x++)
978 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
979 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
980 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
981 rgd->rd_free_clone = rgd->rd_free;
983 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
984 rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
985 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
986 rgd->rd_bits[0].bi_bh->b_data);
988 else if (sdp->sd_args.ar_rgrplvb) {
989 if (!gfs2_rgrp_lvb_valid(rgd)){
990 gfs2_consist_rgrpd(rgd);
994 if (rgd->rd_rgl->rl_unlinked == 0)
995 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1001 bi = rgd->rd_bits + x;
1004 gfs2_assert_warn(sdp, !bi->bi_clone);
1010 int update_rgrp_lvb(struct gfs2_rgrpd *rgd)
1014 if (rgd->rd_flags & GFS2_RDF_UPTODATE)
1017 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
1018 return gfs2_rgrp_bh_get(rgd);
1020 rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
1021 rl_flags &= ~GFS2_RDF_MASK;
1022 rgd->rd_flags &= GFS2_RDF_MASK;
1023 rgd->rd_flags |= (rl_flags | GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
1024 if (rgd->rd_rgl->rl_unlinked == 0)
1025 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1026 rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
1027 rgd->rd_free_clone = rgd->rd_free;
1028 rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
1029 rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
1033 int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
1035 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
1036 struct gfs2_sbd *sdp = rgd->rd_sbd;
1038 if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb)
1040 return gfs2_rgrp_bh_get((struct gfs2_rgrpd *)gh->gh_gl->gl_object);
1044 * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get()
1045 * @gh: The glock holder for the resource group
1049 void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
1051 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
1052 int x, length = rgd->rd_length;
1054 for (x = 0; x < length; x++) {
1055 struct gfs2_bitmap *bi = rgd->rd_bits + x;
1064 int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
1065 struct buffer_head *bh,
1066 const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
1068 struct super_block *sb = sdp->sd_vfs;
1069 struct block_device *bdev = sb->s_bdev;
1070 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
1071 bdev_logical_block_size(sb->s_bdev);
1074 sector_t nr_sects = 0;
1080 for (x = 0; x < bi->bi_len; x++) {
1081 const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
1082 clone += bi->bi_offset;
1085 const u8 *orig = bh->b_data + bi->bi_offset + x;
1086 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
1088 diff = ~(*clone | (*clone >> 1));
1093 blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
1094 blk *= sects_per_blk; /* convert to sectors */
1098 goto start_new_extent;
1099 if ((start + nr_sects) != blk) {
1100 if (nr_sects >= minlen) {
1101 rv = blkdev_issue_discard(bdev,
1106 trimmed += nr_sects;
1112 nr_sects += sects_per_blk;
1115 blk += sects_per_blk;
1118 if (nr_sects >= minlen) {
1119 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
1122 trimmed += nr_sects;
1125 *ptrimmed = trimmed;
1129 if (sdp->sd_args.ar_discard)
1130 fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
1131 sdp->sd_args.ar_discard = 0;
1136 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
1137 * @filp: Any file on the filesystem
1138 * @argp: Pointer to the arguments (also used to pass result)
1140 * Returns: 0 on success, otherwise error code
1143 int gfs2_fitrim(struct file *filp, void __user *argp)
1145 struct inode *inode = filp->f_dentry->d_inode;
1146 struct gfs2_sbd *sdp = GFS2_SB(inode);
1147 struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev);
1148 struct buffer_head *bh;
1149 struct gfs2_rgrpd *rgd;
1150 struct gfs2_rgrpd *rgd_end;
1151 struct gfs2_holder gh;
1152 struct fstrim_range r;
1158 if (!capable(CAP_SYS_ADMIN))
1161 if (!blk_queue_discard(q))
1168 } else if (copy_from_user(&r, argp, sizeof(r)))
1171 ret = gfs2_rindex_update(sdp);
1175 rgd = gfs2_blk2rgrpd(sdp, r.start, 0);
1176 rgd_end = gfs2_blk2rgrpd(sdp, r.start + r.len, 0);
1180 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1184 if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
1185 /* Trim each bitmap in the rgrp */
1186 for (x = 0; x < rgd->rd_length; x++) {
1187 struct gfs2_bitmap *bi = rgd->rd_bits + x;
1188 ret = gfs2_rgrp_send_discards(sdp, rgd->rd_data0, NULL, bi, r.minlen, &amt);
1190 gfs2_glock_dq_uninit(&gh);
1196 /* Mark rgrp as having been trimmed */
1197 ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
1199 bh = rgd->rd_bits[0].bi_bh;
1200 rgd->rd_flags |= GFS2_RGF_TRIMMED;
1201 gfs2_trans_add_bh(rgd->rd_gl, bh, 1);
1202 gfs2_rgrp_out(rgd, bh->b_data);
1203 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, bh->b_data);
1204 gfs2_trans_end(sdp);
1207 gfs2_glock_dq_uninit(&gh);
1212 rgd = gfs2_rgrpd_get_next(rgd);
1216 r.len = trimmed << 9;
1217 if (argp && copy_to_user(argp, &r, sizeof(r)))
1224 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
1225 * @bi: the bitmap with the blocks
1226 * @ip: the inode structure
1227 * @biblk: the 32-bit block number relative to the start of the bitmap
1228 * @amount: the number of blocks to reserve
1230 * Returns: NULL - reservation was already taken, so not inserted
1231 * pointer to the inserted reservation
1233 static struct gfs2_blkreserv *rs_insert(struct gfs2_bitmap *bi,
1234 struct gfs2_inode *ip, u32 biblk,
1237 struct rb_node **newn, *parent = NULL;
1239 struct gfs2_blkreserv *rs = ip->i_res;
1240 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd;
1241 u64 fsblock = gfs2_bi2rgd_blk(bi, biblk) + rgd->rd_data0;
1243 spin_lock(&rgd->rd_rsspin);
1244 newn = &rgd->rd_rstree.rb_node;
1246 BUG_ON(gfs2_rs_active(rs));
1247 /* Figure out where to put new node */
1248 /*BUG_ON(!gfs2_glock_is_locked_by_me(rgd->rd_gl));*/
1250 struct gfs2_blkreserv *cur =
1251 rb_entry(*newn, struct gfs2_blkreserv, rs_node);
1254 rc = rs_cmp(fsblock, amount, cur);
1256 newn = &((*newn)->rb_right);
1258 newn = &((*newn)->rb_left);
1260 spin_unlock(&rgd->rd_rsspin);
1261 return NULL; /* reservation already in use */
1265 /* Do our reservation work */
1267 rs->rs_free = amount;
1268 rs->rs_rbm.offset = biblk;
1270 rb_link_node(&rs->rs_node, parent, newn);
1271 rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
1273 /* Do our rgrp accounting for the reservation */
1274 rgd->rd_reserved += amount; /* blocks reserved */
1275 rgd->rd_rs_cnt++; /* number of in-tree reservations */
1276 spin_unlock(&rgd->rd_rsspin);
1277 trace_gfs2_rs(ip, rs, TRACE_RS_INSERT);
1282 * unclaimed_blocks - return number of blocks that aren't spoken for
1284 static u32 unclaimed_blocks(struct gfs2_rgrpd *rgd)
1286 return rgd->rd_free_clone - rgd->rd_reserved;
1290 * rg_mblk_search - find a group of multiple free blocks
1291 * @rgd: the resource group descriptor
1292 * @rs: the block reservation
1293 * @ip: pointer to the inode for which we're reserving blocks
1295 * This is very similar to rgblk_search, except we're looking for whole
1296 * 64-bit words that represent a chunk of 32 free blocks. I'm only focusing
1297 * on aligned dwords for speed's sake.
1299 * Returns: 0 if successful or BFITNOENT if there isn't enough free space
1302 static int rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip, unsigned requested)
1304 struct gfs2_bitmap *bi = rgd->rd_bits;
1305 const u32 length = rgd->rd_length;
1307 unsigned int buf, x, search_bytes;
1309 u8 *ptr, *end, *nonzero;
1310 u32 goal, rsv_bytes;
1311 struct gfs2_blkreserv *rs;
1312 u32 best_rs_bytes, unclaimed;
1315 /* Find bitmap block that contains bits for goal block */
1316 if (rgrp_contains_block(rgd, ip->i_goal))
1317 goal = ip->i_goal - rgd->rd_data0;
1319 goal = rgd->rd_last_alloc;
1320 for (buf = 0; buf < length; buf++) {
1321 bi = rgd->rd_bits + buf;
1322 /* Convert scope of "goal" from rgrp-wide to within
1324 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1325 goal -= bi->bi_start * GFS2_NBBY;
1333 best_rs_blocks = max_t(int, atomic_read(&ip->i_res->rs_sizehint),
1334 (RGRP_RSRV_MINBLKS * rgd->rd_length));
1335 best_rs_bytes = (best_rs_blocks *
1336 (1 + (RSRV_CONTENTION_FACTOR * rgd->rd_rs_cnt))) /
1337 GFS2_NBBY; /* 1 + is for our not-yet-created reservation */
1338 best_rs_bytes = ALIGN(best_rs_bytes, sizeof(u64));
1339 unclaimed = unclaimed_blocks(rgd);
1340 if (best_rs_bytes * GFS2_NBBY > unclaimed)
1341 best_rs_bytes = unclaimed >> GFS2_BIT_SIZE;
1343 for (x = 0; x <= length; x++) {
1344 bi = rgd->rd_bits + buf;
1346 if (test_bit(GBF_FULL, &bi->bi_flags))
1349 WARN_ON(!buffer_uptodate(bi->bi_bh));
1351 buffer = bi->bi_clone + bi->bi_offset;
1353 buffer = bi->bi_bh->b_data + bi->bi_offset;
1355 /* We have to keep the reservations aligned on u64 boundaries
1356 otherwise we could get situations where a byte can't be
1357 used because it's after a reservation, but a free bit still
1358 is within the reservation's area. */
1359 ptr = buffer + ALIGN(goal >> GFS2_BIT_SIZE, sizeof(u64));
1360 end = (buffer + bi->bi_len);
1363 if ((ptr + best_rs_bytes) <= end)
1364 search_bytes = best_rs_bytes;
1366 search_bytes = end - ptr;
1367 BUG_ON(!search_bytes);
1368 nonzero = memchr_inv(ptr, 0, search_bytes);
1369 /* If the lot is all zeroes, reserve the whole size. If
1370 there's enough zeroes to satisfy the request, use
1371 what we can. If there's not enough, keep looking. */
1372 if (nonzero == NULL)
1373 rsv_bytes = search_bytes;
1374 else if ((nonzero - ptr) * GFS2_NBBY >= requested)
1375 rsv_bytes = (nonzero - ptr);
1378 blk = ((ptr - buffer) * GFS2_NBBY);
1379 BUG_ON(blk >= bi->bi_len * GFS2_NBBY);
1380 rs = rs_insert(bi, ip, blk,
1381 rsv_bytes * GFS2_NBBY);
1387 ptr += ALIGN(search_bytes, sizeof(u64));
1390 /* Try next bitmap block (wrap back to rgrp header
1401 * try_rgrp_fit - See if a given reservation will fit in a given RG
1405 * If there's room for the requested blocks to be allocated from the RG:
1406 * This will try to get a multi-block reservation first, and if that doesn't
1407 * fit, it will take what it can.
1409 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
1412 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
1415 if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
1417 /* Look for a multi-block reservation. */
1418 if (unclaimed_blocks(rgd) >= RGRP_RSRV_MINBLKS &&
1419 rg_mblk_search(rgd, ip, requested) != BFITNOENT)
1421 if (unclaimed_blocks(rgd) >= requested)
1428 * gfs2_next_unreserved_block - Return next block that is not reserved
1429 * @rgd: The resource group
1430 * @block: The starting block
1431 * @ip: Ignore any reservations for this inode
1433 * If the block does not appear in any reservation, then return the
1434 * block number unchanged. If it does appear in the reservation, then
1435 * keep looking through the tree of reservations in order to find the
1436 * first block number which is not reserved.
1439 static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block,
1440 const struct gfs2_inode *ip)
1442 struct gfs2_blkreserv *rs;
1446 spin_lock(&rgd->rd_rsspin);
1447 n = rb_first(&rgd->rd_rstree);
1449 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1450 rc = rs_cmp(block, 1, rs);
1460 while ((rs_cmp(block, 1, rs) == 0) && (ip->i_res != rs)) {
1461 block = gfs2_rbm_to_block(&rs->rs_rbm) + rs->rs_free;
1462 n = rb_next(&rs->rs_node);
1465 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1469 spin_unlock(&rgd->rd_rsspin);
1474 * gfs2_rbm_from_block - Set the rbm based upon rgd and block number
1475 * @rbm: The rbm with rgd already set correctly
1476 * @block: The block number (filesystem relative)
1478 * This sets the bi and offset members of an rbm based on a
1479 * resource group and a filesystem relative block number. The
1480 * resource group must be set in the rbm on entry, the bi and
1481 * offset members will be set by this function.
1483 * Returns: 0 on success, or an error code
1486 static int gfs2_rbm_from_block(struct gfs2_rbm *rbm, u64 block)
1488 u64 rblock = block - rbm->rgd->rd_data0;
1489 u32 goal = (u32)rblock;
1492 if (WARN_ON_ONCE(rblock > UINT_MAX))
1494 if (block >= rbm->rgd->rd_data0 + rbm->rgd->rd_data)
1497 for (x = 0; x < rbm->rgd->rd_length; x++) {
1498 rbm->bi = rbm->rgd->rd_bits + x;
1499 if (goal < (rbm->bi->bi_start + rbm->bi->bi_len) * GFS2_NBBY) {
1500 rbm->offset = goal - (rbm->bi->bi_start * GFS2_NBBY);
1509 * gfs2_reservation_check_and_update - Check for reservations during block alloc
1510 * @rbm: The current position in the resource group
1512 * This checks the current position in the rgrp to see whether there is
1513 * a reservation covering this block. If not then this function is a
1514 * no-op. If there is, then the position is moved to the end of the
1515 * contiguous reservation(s) so that we are pointing at the first
1516 * non-reserved block.
1518 * Returns: 0 if no reservation, 1 if @rbm has changed, otherwise an error
1521 static int gfs2_reservation_check_and_update(struct gfs2_rbm *rbm,
1522 const struct gfs2_inode *ip)
1524 u64 block = gfs2_rbm_to_block(rbm);
1528 nblock = gfs2_next_unreserved_block(rbm->rgd, block, ip);
1529 if (nblock == block)
1531 ret = gfs2_rbm_from_block(rbm, nblock);
1538 * gfs2_rbm_find - Look for blocks of a particular state
1539 * @rbm: Value/result starting position and final position
1540 * @state: The state which we want to find
1541 * @ip: If set, check for reservations
1542 * @nowrap: Stop looking at the end of the rgrp, rather than wrapping
1543 * around until we've reached the starting point.
1546 * - If looking for free blocks, we set GBF_FULL on each bitmap which
1547 * has no free blocks in it.
1549 * Returns: 0 on success, -ENOSPC if there is no block of the requested state
1552 static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state,
1553 const struct gfs2_inode *ip, bool nowrap)
1555 struct buffer_head *bh;
1556 struct gfs2_bitmap *initial_bi;
1562 int iters = rbm->rgd->rd_length;
1565 /* If we are not starting at the beginning of a bitmap, then we
1566 * need to add one to the bitmap count to ensure that we search
1567 * the starting bitmap twice.
1569 if (rbm->offset != 0)
1573 if (test_bit(GBF_FULL, &rbm->bi->bi_flags) &&
1574 (state == GFS2_BLKST_FREE))
1577 bh = rbm->bi->bi_bh;
1578 buffer = bh->b_data + rbm->bi->bi_offset;
1579 WARN_ON(!buffer_uptodate(bh));
1580 if (state != GFS2_BLKST_UNLINKED && rbm->bi->bi_clone)
1581 buffer = rbm->bi->bi_clone + rbm->bi->bi_offset;
1582 initial_offset = rbm->offset;
1583 offset = gfs2_bitfit(buffer, rbm->bi->bi_len, rbm->offset, state);
1584 if (offset == BFITNOENT)
1586 rbm->offset = offset;
1590 initial_bi = rbm->bi;
1591 ret = gfs2_reservation_check_and_update(rbm, ip);
1595 n += (rbm->bi - initial_bi);
1598 if (ret == -E2BIG) {
1601 n += (rbm->bi - initial_bi);
1602 goto res_covered_end_of_rgrp;
1606 bitmap_full: /* Mark bitmap as full and fall through */
1607 if ((state == GFS2_BLKST_FREE) && initial_offset == 0)
1608 set_bit(GBF_FULL, &rbm->bi->bi_flags);
1610 next_bitmap: /* Find next bitmap in the rgrp */
1612 index = rbm->bi - rbm->rgd->rd_bits;
1614 if (index == rbm->rgd->rd_length)
1616 res_covered_end_of_rgrp:
1617 rbm->bi = &rbm->rgd->rd_bits[index];
1618 if ((index == 0) && nowrap)
1630 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
1632 * @last_unlinked: block address of the last dinode we unlinked
1633 * @skip: block address we should explicitly not unlink
1635 * Returns: 0 if no error
1636 * The inode, if one has been found, in inode.
1639 static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
1642 struct gfs2_sbd *sdp = rgd->rd_sbd;
1643 struct gfs2_glock *gl;
1644 struct gfs2_inode *ip;
1647 struct gfs2_rbm rbm = { .rgd = rgd, .bi = rgd->rd_bits, .offset = 0 };
1650 down_write(&sdp->sd_log_flush_lock);
1651 error = gfs2_rbm_find(&rbm, GFS2_BLKST_UNLINKED, NULL, true);
1652 up_write(&sdp->sd_log_flush_lock);
1653 if (error == -ENOSPC)
1655 if (WARN_ON_ONCE(error))
1658 block = gfs2_rbm_to_block(&rbm);
1659 if (gfs2_rbm_from_block(&rbm, block + 1))
1661 if (*last_unlinked != NO_BLOCK && block <= *last_unlinked)
1665 *last_unlinked = block;
1667 error = gfs2_glock_get(sdp, block, &gfs2_inode_glops, CREATE, &gl);
1671 /* If the inode is already in cache, we can ignore it here
1672 * because the existing inode disposal code will deal with
1673 * it when all refs have gone away. Accessing gl_object like
1674 * this is not safe in general. Here it is ok because we do
1675 * not dereference the pointer, and we only need an approx
1676 * answer to whether it is NULL or not.
1680 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
1685 /* Limit reclaim to sensible number of tasks */
1686 if (found > NR_CPUS)
1690 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1695 * gfs2_inplace_reserve - Reserve space in the filesystem
1696 * @ip: the inode to reserve space for
1697 * @requested: the number of blocks to be reserved
1702 int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
1704 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1705 struct gfs2_rgrpd *begin = NULL;
1706 struct gfs2_blkreserv *rs = ip->i_res;
1707 int error = 0, rg_locked, flags = LM_FLAG_TRY;
1708 u64 last_unlinked = NO_BLOCK;
1711 if (sdp->sd_args.ar_rgrplvb)
1713 if (gfs2_assert_warn(sdp, requested)) {
1717 if (gfs2_rs_active(rs)) {
1718 begin = rs->rs_rbm.rgd;
1719 flags = 0; /* Yoda: Do or do not. There is no try */
1720 } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) {
1721 rs->rs_rbm.rgd = begin = ip->i_rgd;
1723 rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
1725 if (rs->rs_rbm.rgd == NULL)
1731 if (gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) {
1734 } else if (!loops && !gfs2_rs_active(rs) &&
1735 rs->rs_rbm.rgd->rd_rs_cnt > RGRP_RSRV_MAX_CONTENDERS) {
1736 /* If the rgrp already is maxed out for contenders,
1737 we can eliminate it as a "first pass" without even
1738 requesting the rgrp glock. */
1739 error = GLR_TRYFAILED;
1741 error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl,
1742 LM_ST_EXCLUSIVE, flags,
1744 if (!error && sdp->sd_args.ar_rgrplvb) {
1745 error = update_rgrp_lvb(rs->rs_rbm.rgd);
1747 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1754 if (gfs2_rs_active(rs)) {
1755 if (unclaimed_blocks(rs->rs_rbm.rgd) +
1756 rs->rs_free >= requested) {
1757 ip->i_rgd = rs->rs_rbm.rgd;
1760 /* We have a multi-block reservation, but the
1761 rgrp doesn't have enough free blocks to
1762 satisfy the request. Free the reservation
1763 and look for a suitable rgrp. */
1764 gfs2_rs_deltree(ip, rs);
1766 if (try_rgrp_fit(rs->rs_rbm.rgd, ip, requested)) {
1767 if (sdp->sd_args.ar_rgrplvb)
1768 gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
1769 ip->i_rgd = rs->rs_rbm.rgd;
1772 if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK) {
1773 if (sdp->sd_args.ar_rgrplvb)
1774 gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
1775 try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked,
1779 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1782 rs->rs_rbm.rgd = gfs2_rgrpd_get_next(rs->rs_rbm.rgd);
1783 rs->rs_rbm.rgd = rs->rs_rbm.rgd ? : begin; /* if NULL, wrap */
1784 if (rs->rs_rbm.rgd != begin) /* If we didn't wrap */
1787 flags &= ~LM_FLAG_TRY;
1789 /* Check that fs hasn't grown if writing to rindex */
1790 if (ip == GFS2_I(sdp->sd_rindex) &&
1791 !sdp->sd_rindex_uptodate) {
1792 error = gfs2_ri_update(ip);
1795 } else if (loops == 2)
1796 /* Flushing the log may release space */
1797 gfs2_log_flush(sdp, NULL);
1810 * gfs2_inplace_release - release an inplace reservation
1811 * @ip: the inode the reservation was taken out on
1813 * Release a reservation made by gfs2_inplace_reserve().
1816 void gfs2_inplace_release(struct gfs2_inode *ip)
1818 struct gfs2_blkreserv *rs = ip->i_res;
1820 if (rs->rs_rgd_gh.gh_gl)
1821 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1825 * gfs2_get_block_type - Check a block in a RG is of given type
1826 * @rgd: the resource group holding the block
1827 * @block: the block number
1829 * Returns: The block type (GFS2_BLKST_*)
1832 static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1834 struct gfs2_rbm rbm = { .rgd = rgd, };
1837 ret = gfs2_rbm_from_block(&rbm, block);
1838 WARN_ON_ONCE(ret != 0);
1840 return gfs2_testbit(rgd, rbm.bi->bi_bh->b_data + rbm.bi->bi_offset,
1841 rbm.bi->bi_len, rbm.offset);
1846 * gfs2_alloc_extent - allocate an extent from a given bitmap
1847 * @rbm: the resource group information
1848 * @dinode: TRUE if the first block we allocate is for a dinode
1849 * @n: The extent length
1851 * Add the found bitmap buffer to the transaction.
1852 * Set the found bits to @new_state to change block's allocation state.
1853 * Returns: starting block number of the extent (fs scope)
1855 static u64 gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
1858 struct gfs2_rgrpd *rgd = rbm->rgd;
1859 struct gfs2_bitmap *bi = rbm->bi;
1860 u32 blk = rbm->offset;
1861 const unsigned int elen = *n;
1863 const u8 *buffer = NULL;
1866 buffer = bi->bi_bh->b_data + bi->bi_offset;
1867 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1868 gfs2_setbit(rgd, bi->bi_clone, bi, blk,
1869 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
1874 if (goal >= (bi->bi_len * GFS2_NBBY))
1876 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1879 gfs2_setbit(rgd, bi->bi_clone, bi, goal, GFS2_BLKST_USED);
1882 blk = gfs2_bi2rgd_blk(bi, blk);
1883 rgd->rd_last_alloc = blk + *n - 1;
1884 return rgd->rd_data0 + blk;
1888 * rgblk_free - Change alloc state of given block(s)
1889 * @sdp: the filesystem
1890 * @bstart: the start of a run of blocks to free
1891 * @blen: the length of the block run (all must lie within ONE RG!)
1892 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1894 * Returns: Resource group containing the block(s)
1897 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1898 u32 blen, unsigned char new_state)
1900 struct gfs2_rbm rbm;
1902 rbm.rgd = gfs2_blk2rgrpd(sdp, bstart, 1);
1904 if (gfs2_consist(sdp))
1905 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1910 gfs2_rbm_from_block(&rbm, bstart);
1912 if (!rbm.bi->bi_clone) {
1913 rbm.bi->bi_clone = kmalloc(rbm.bi->bi_bh->b_size,
1914 GFP_NOFS | __GFP_NOFAIL);
1915 memcpy(rbm.bi->bi_clone + rbm.bi->bi_offset,
1916 rbm.bi->bi_bh->b_data + rbm.bi->bi_offset,
1919 gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.bi->bi_bh, 1);
1920 gfs2_setbit(rbm.rgd, NULL, rbm.bi, rbm.offset, new_state);
1927 * gfs2_rgrp_dump - print out an rgrp
1928 * @seq: The iterator
1929 * @gl: The glock in question
1933 int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
1935 struct gfs2_rgrpd *rgd = gl->gl_object;
1936 struct gfs2_blkreserv *trs;
1937 const struct rb_node *n;
1941 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u r:%u\n",
1942 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
1943 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
1945 spin_lock(&rgd->rd_rsspin);
1946 for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
1947 trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1950 spin_unlock(&rgd->rd_rsspin);
1954 static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
1956 struct gfs2_sbd *sdp = rgd->rd_sbd;
1957 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
1958 (unsigned long long)rgd->rd_addr);
1959 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
1960 gfs2_rgrp_dump(NULL, rgd->rd_gl);
1961 rgd->rd_flags |= GFS2_RDF_ERROR;
1965 * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
1966 * @ip: The inode we have just allocated blocks for
1967 * @rbm: The start of the allocated blocks
1968 * @len: The extent length
1970 * Adjusts a reservation after an allocation has taken place. If the
1971 * reservation does not match the allocation, or if it is now empty
1972 * then it is removed.
1975 static void gfs2_adjust_reservation(struct gfs2_inode *ip,
1976 const struct gfs2_rbm *rbm, unsigned len)
1978 struct gfs2_blkreserv *rs = ip->i_res;
1979 struct gfs2_rgrpd *rgd = rbm->rgd;
1984 spin_lock(&rgd->rd_rsspin);
1985 if (gfs2_rs_active(rs)) {
1986 if (gfs2_rbm_eq(&rs->rs_rbm, rbm)) {
1987 block = gfs2_rbm_to_block(rbm);
1988 ret = gfs2_rbm_from_block(&rs->rs_rbm, block + len);
1989 rlen = min(rs->rs_free, len);
1990 rs->rs_free -= rlen;
1991 rgd->rd_reserved -= rlen;
1992 trace_gfs2_rs(ip, rs, TRACE_RS_CLAIM);
1993 if (rs->rs_free && !ret)
1996 __rs_deltree(ip, rs);
1999 spin_unlock(&rgd->rd_rsspin);
2003 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
2004 * @ip: the inode to allocate the block for
2005 * @bn: Used to return the starting block number
2006 * @nblocks: requested number of blocks/extent length (value/result)
2007 * @dinode: 1 if we're allocating a dinode block, else 0
2008 * @generation: the generation number of the inode
2010 * Returns: 0 or error
2013 int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
2014 bool dinode, u64 *generation)
2016 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2017 struct buffer_head *dibh;
2018 struct gfs2_rbm rbm = { .rgd = ip->i_rgd, };
2021 u64 block; /* block, within the file system scope */
2024 if (gfs2_rs_active(ip->i_res))
2025 goal = gfs2_rbm_to_block(&ip->i_res->rs_rbm);
2026 else if (!dinode && rgrp_contains_block(rbm.rgd, ip->i_goal))
2029 goal = rbm.rgd->rd_last_alloc + rbm.rgd->rd_data0;
2031 if ((goal < rbm.rgd->rd_data0) ||
2032 (goal >= rbm.rgd->rd_data0 + rbm.rgd->rd_data))
2033 rbm.rgd = gfs2_blk2rgrpd(sdp, goal, 1);
2035 gfs2_rbm_from_block(&rbm, goal);
2036 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, ip, false);
2038 /* Since all blocks are reserved in advance, this shouldn't happen */
2040 fs_warn(sdp, "error=%d, nblocks=%u, full=%d\n", error, *nblocks,
2041 test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags));
2045 block = gfs2_alloc_extent(&rbm, dinode, nblocks);
2046 if (gfs2_rs_active(ip->i_res))
2047 gfs2_adjust_reservation(ip, &rbm, *nblocks);
2053 ip->i_goal = block + ndata - 1;
2054 error = gfs2_meta_inode_buffer(ip, &dibh);
2056 struct gfs2_dinode *di =
2057 (struct gfs2_dinode *)dibh->b_data;
2058 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
2059 di->di_goal_meta = di->di_goal_data =
2060 cpu_to_be64(ip->i_goal);
2064 if (rbm.rgd->rd_free < *nblocks) {
2065 printk(KERN_WARNING "nblocks=%u\n", *nblocks);
2069 rbm.rgd->rd_free -= *nblocks;
2071 rbm.rgd->rd_dinodes++;
2072 *generation = rbm.rgd->rd_igeneration++;
2073 if (*generation == 0)
2074 *generation = rbm.rgd->rd_igeneration++;
2077 gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh, 1);
2078 gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
2079 gfs2_rgrp_ondisk2lvb(rbm.rgd->rd_rgl, rbm.rgd->rd_bits[0].bi_bh->b_data);
2081 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
2083 gfs2_trans_add_unrevoke(sdp, block, 1);
2086 * This needs reviewing to see why we cannot do the quota change
2087 * at this point in the dinode case.
2090 gfs2_quota_change(ip, ndata, ip->i_inode.i_uid,
2093 rbm.rgd->rd_free_clone -= *nblocks;
2094 trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
2095 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
2100 gfs2_rgrp_error(rbm.rgd);
2105 * __gfs2_free_blocks - free a contiguous run of block(s)
2106 * @ip: the inode these blocks are being freed from
2107 * @bstart: first block of a run of contiguous blocks
2108 * @blen: the length of the block run
2109 * @meta: 1 if the blocks represent metadata
2113 void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
2115 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2116 struct gfs2_rgrpd *rgd;
2118 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
2121 trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
2122 rgd->rd_free += blen;
2123 rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
2124 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
2125 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2126 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2128 /* Directories keep their data in the metadata address space */
2129 if (meta || ip->i_depth)
2130 gfs2_meta_wipe(ip, bstart, blen);
2134 * gfs2_free_meta - free a contiguous run of data block(s)
2135 * @ip: the inode these blocks are being freed from
2136 * @bstart: first block of a run of contiguous blocks
2137 * @blen: the length of the block run
2141 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
2143 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2145 __gfs2_free_blocks(ip, bstart, blen, 1);
2146 gfs2_statfs_change(sdp, 0, +blen, 0);
2147 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
2150 void gfs2_unlink_di(struct inode *inode)
2152 struct gfs2_inode *ip = GFS2_I(inode);
2153 struct gfs2_sbd *sdp = GFS2_SB(inode);
2154 struct gfs2_rgrpd *rgd;
2155 u64 blkno = ip->i_no_addr;
2157 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
2160 trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
2161 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
2162 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2163 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2164 update_rgrp_lvb_unlinked(rgd, 1);
2167 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
2169 struct gfs2_sbd *sdp = rgd->rd_sbd;
2170 struct gfs2_rgrpd *tmp_rgd;
2172 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
2175 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
2177 if (!rgd->rd_dinodes)
2178 gfs2_consist_rgrpd(rgd);
2182 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
2183 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2184 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2185 update_rgrp_lvb_unlinked(rgd, -1);
2187 gfs2_statfs_change(sdp, 0, +1, -1);
2191 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
2193 gfs2_free_uninit_di(rgd, ip->i_no_addr);
2194 trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
2195 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
2196 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
2200 * gfs2_check_blk_type - Check the type of a block
2201 * @sdp: The superblock
2202 * @no_addr: The block number to check
2203 * @type: The block type we are looking for
2205 * Returns: 0 if the block type matches the expected type
2206 * -ESTALE if it doesn't match
2207 * or -ve errno if something went wrong while checking
2210 int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
2212 struct gfs2_rgrpd *rgd;
2213 struct gfs2_holder rgd_gh;
2214 int error = -EINVAL;
2216 rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
2220 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
2224 if (gfs2_get_block_type(rgd, no_addr) != type)
2227 gfs2_glock_dq_uninit(&rgd_gh);
2233 * gfs2_rlist_add - add a RG to a list of RGs
2235 * @rlist: the list of resource groups
2238 * Figure out what RG a block belongs to and add that RG to the list
2240 * FIXME: Don't use NOFAIL
2244 void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
2247 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2248 struct gfs2_rgrpd *rgd;
2249 struct gfs2_rgrpd **tmp;
2250 unsigned int new_space;
2253 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
2256 if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block))
2259 rgd = gfs2_blk2rgrpd(sdp, block, 1);
2261 fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block);
2266 for (x = 0; x < rlist->rl_rgrps; x++)
2267 if (rlist->rl_rgd[x] == rgd)
2270 if (rlist->rl_rgrps == rlist->rl_space) {
2271 new_space = rlist->rl_space + 10;
2273 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
2274 GFP_NOFS | __GFP_NOFAIL);
2276 if (rlist->rl_rgd) {
2277 memcpy(tmp, rlist->rl_rgd,
2278 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
2279 kfree(rlist->rl_rgd);
2282 rlist->rl_space = new_space;
2283 rlist->rl_rgd = tmp;
2286 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
2290 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
2291 * and initialize an array of glock holders for them
2292 * @rlist: the list of resource groups
2293 * @state: the lock state to acquire the RG lock in
2295 * FIXME: Don't use NOFAIL
2299 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
2303 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
2304 GFP_NOFS | __GFP_NOFAIL);
2305 for (x = 0; x < rlist->rl_rgrps; x++)
2306 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
2312 * gfs2_rlist_free - free a resource group list
2313 * @list: the list of resource groups
2317 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
2321 kfree(rlist->rl_rgd);
2323 if (rlist->rl_ghs) {
2324 for (x = 0; x < rlist->rl_rgrps; x++)
2325 gfs2_holder_uninit(&rlist->rl_ghs[x]);
2326 kfree(rlist->rl_ghs);
2327 rlist->rl_ghs = NULL;