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>
31 #include "ops_address.h"
33 #define BFITNOENT ((u32)~0)
34 #define NO_BLOCK ((u64)~0)
37 * These routines are used by the resource group routines (rgrp.c)
38 * to keep track of block allocation. Each block is represented by two
39 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
42 * 1 = Used (not metadata)
43 * 2 = Unlinked (still in use) inode
47 static const char valid_change[16] = {
55 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
56 unsigned char old_state, unsigned char new_state);
59 * gfs2_setbit - Set a bit in the bitmaps
60 * @buffer: the buffer that holds the bitmaps
61 * @buflen: the length (in bytes) of the buffer
62 * @block: the block to set
63 * @new_state: the new state of the block
67 static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
68 unsigned int buflen, u32 block,
69 unsigned char new_state)
71 unsigned char *byte, *end, cur_state;
74 byte = buffer + (block / GFS2_NBBY);
75 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
76 end = buffer + buflen;
78 gfs2_assert(rgd->rd_sbd, byte < end);
80 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
82 if (valid_change[new_state * 4 + cur_state]) {
83 *byte ^= cur_state << bit;
84 *byte |= new_state << bit;
86 gfs2_consist_rgrpd(rgd);
90 * gfs2_testbit - test a bit in the bitmaps
91 * @buffer: the buffer that holds the bitmaps
92 * @buflen: the length (in bytes) of the buffer
93 * @block: the block to read
97 static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
98 unsigned int buflen, u32 block)
100 unsigned char *byte, *end, cur_state;
103 byte = buffer + (block / GFS2_NBBY);
104 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
105 end = buffer + buflen;
107 gfs2_assert(rgd->rd_sbd, byte < end);
109 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
115 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
116 * a block in a given allocation state.
117 * @buffer: the buffer that holds the bitmaps
118 * @buflen: the length (in bytes) of the buffer
119 * @goal: start search at this block's bit-pair (within @buffer)
120 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for.
122 * Scope of @goal and returned block number is only within this bitmap buffer,
123 * not entire rgrp or filesystem. @buffer will be offset from the actual
124 * beginning of a bitmap block buffer, skipping any header structures.
126 * Return: the block number (bitmap buffer scope) that was found
129 static u32 gfs2_bitfit(unsigned char *buffer, unsigned int buflen, u32 goal,
130 unsigned char old_state)
134 unsigned int bit, bitlong;
135 unsigned long *plong, plong55;
137 byte = buffer + (goal / GFS2_NBBY);
138 plong = (unsigned long *)(buffer + (goal / GFS2_NBBY));
139 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
141 #if BITS_PER_LONG == 32
142 plong55 = 0x55555555;
144 plong55 = 0x5555555555555555;
146 while (byte < buffer + buflen) {
148 if (bitlong == 0 && old_state == 0 && *plong == plong55) {
150 byte += sizeof(unsigned long);
151 blk += sizeof(unsigned long) * GFS2_NBBY;
154 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
156 bit += GFS2_BIT_SIZE;
161 bitlong += GFS2_BIT_SIZE;
162 if (bitlong >= sizeof(unsigned long) * 8) {
174 * gfs2_bitcount - count the number of bits in a certain state
175 * @buffer: the buffer that holds the bitmaps
176 * @buflen: the length (in bytes) of the buffer
177 * @state: the state of the block we're looking for
179 * Returns: The number of bits
182 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
183 unsigned int buflen, unsigned char state)
185 unsigned char *byte = buffer;
186 unsigned char *end = buffer + buflen;
187 unsigned char state1 = state << 2;
188 unsigned char state2 = state << 4;
189 unsigned char state3 = state << 6;
192 for (; byte < end; byte++) {
193 if (((*byte) & 0x03) == state)
195 if (((*byte) & 0x0C) == state1)
197 if (((*byte) & 0x30) == state2)
199 if (((*byte) & 0xC0) == state3)
207 * gfs2_rgrp_verify - Verify that a resource group is consistent
208 * @sdp: the filesystem
213 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
215 struct gfs2_sbd *sdp = rgd->rd_sbd;
216 struct gfs2_bitmap *bi = NULL;
217 u32 length = rgd->rd_length;
221 memset(count, 0, 4 * sizeof(u32));
223 /* Count # blocks in each of 4 possible allocation states */
224 for (buf = 0; buf < length; buf++) {
225 bi = rgd->rd_bits + buf;
226 for (x = 0; x < 4; x++)
227 count[x] += gfs2_bitcount(rgd,
233 if (count[0] != rgd->rd_rg.rg_free) {
234 if (gfs2_consist_rgrpd(rgd))
235 fs_err(sdp, "free data mismatch: %u != %u\n",
236 count[0], rgd->rd_rg.rg_free);
242 rgd->rd_rg.rg_dinodes;
243 if (count[1] + count[2] != tmp) {
244 if (gfs2_consist_rgrpd(rgd))
245 fs_err(sdp, "used data mismatch: %u != %u\n",
250 if (count[3] != rgd->rd_rg.rg_dinodes) {
251 if (gfs2_consist_rgrpd(rgd))
252 fs_err(sdp, "used metadata mismatch: %u != %u\n",
253 count[3], rgd->rd_rg.rg_dinodes);
257 if (count[2] > count[3]) {
258 if (gfs2_consist_rgrpd(rgd))
259 fs_err(sdp, "unlinked inodes > inodes: %u\n",
266 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
268 u64 first = rgd->rd_data0;
269 u64 last = first + rgd->rd_data;
270 return first <= block && block < last;
274 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
275 * @sdp: The GFS2 superblock
276 * @n: The data block number
278 * Returns: The resource group, or NULL if not found
281 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
283 struct gfs2_rgrpd *rgd;
285 spin_lock(&sdp->sd_rindex_spin);
287 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
288 if (rgrp_contains_block(rgd, blk)) {
289 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
290 spin_unlock(&sdp->sd_rindex_spin);
295 spin_unlock(&sdp->sd_rindex_spin);
301 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
302 * @sdp: The GFS2 superblock
304 * Returns: The first rgrp in the filesystem
307 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
309 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
310 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
314 * gfs2_rgrpd_get_next - get the next RG
317 * Returns: The next rgrp
320 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
322 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
324 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
327 static void clear_rgrpdi(struct gfs2_sbd *sdp)
329 struct list_head *head;
330 struct gfs2_rgrpd *rgd;
331 struct gfs2_glock *gl;
333 spin_lock(&sdp->sd_rindex_spin);
334 sdp->sd_rindex_forward = NULL;
335 head = &sdp->sd_rindex_recent_list;
336 while (!list_empty(head)) {
337 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
338 list_del(&rgd->rd_recent);
340 spin_unlock(&sdp->sd_rindex_spin);
342 head = &sdp->sd_rindex_list;
343 while (!list_empty(head)) {
344 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
347 list_del(&rgd->rd_list);
348 list_del(&rgd->rd_list_mru);
351 gl->gl_object = NULL;
360 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
362 mutex_lock(&sdp->sd_rindex_mutex);
364 mutex_unlock(&sdp->sd_rindex_mutex);
367 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
369 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
370 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
371 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
372 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
373 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
377 * gfs2_compute_bitstructs - Compute the bitmap sizes
378 * @rgd: The resource group descriptor
380 * Calculates bitmap descriptors, one for each block that contains bitmap data
385 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
387 struct gfs2_sbd *sdp = rgd->rd_sbd;
388 struct gfs2_bitmap *bi;
389 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
390 u32 bytes_left, bytes;
396 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
400 bytes_left = rgd->rd_bitbytes;
402 for (x = 0; x < length; x++) {
403 bi = rgd->rd_bits + x;
405 /* small rgrp; bitmap stored completely in header block */
408 bi->bi_offset = sizeof(struct gfs2_rgrp);
413 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
414 bi->bi_offset = sizeof(struct gfs2_rgrp);
418 } else if (x + 1 == length) {
420 bi->bi_offset = sizeof(struct gfs2_meta_header);
421 bi->bi_start = rgd->rd_bitbytes - bytes_left;
425 bytes = sdp->sd_sb.sb_bsize -
426 sizeof(struct gfs2_meta_header);
427 bi->bi_offset = sizeof(struct gfs2_meta_header);
428 bi->bi_start = rgd->rd_bitbytes - bytes_left;
436 gfs2_consist_rgrpd(rgd);
439 bi = rgd->rd_bits + (length - 1);
440 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
441 if (gfs2_consist_rgrpd(rgd)) {
442 gfs2_rindex_print(rgd);
443 fs_err(sdp, "start=%u len=%u offset=%u\n",
444 bi->bi_start, bi->bi_len, bi->bi_offset);
453 * gfs2_ri_total - Total up the file system space, according to the rindex.
456 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
459 struct inode *inode = sdp->sd_rindex;
460 struct gfs2_inode *ip = GFS2_I(inode);
461 char buf[sizeof(struct gfs2_rindex)];
462 struct file_ra_state ra_state;
465 mutex_lock(&sdp->sd_rindex_mutex);
466 file_ra_state_init(&ra_state, inode->i_mapping);
467 for (rgrps = 0;; rgrps++) {
468 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
470 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
472 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
473 sizeof(struct gfs2_rindex));
474 if (error != sizeof(struct gfs2_rindex))
476 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
478 mutex_unlock(&sdp->sd_rindex_mutex);
482 static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
484 const struct gfs2_rindex *str = buf;
486 rgd->rd_addr = be64_to_cpu(str->ri_addr);
487 rgd->rd_length = be32_to_cpu(str->ri_length);
488 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
489 rgd->rd_data = be32_to_cpu(str->ri_data);
490 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
494 * read_rindex_entry - Pull in a new resource index entry from the disk
495 * @gl: The glock covering the rindex inode
497 * Returns: 0 on success, error code otherwise
500 static int read_rindex_entry(struct gfs2_inode *ip,
501 struct file_ra_state *ra_state)
503 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
504 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
505 char buf[sizeof(struct gfs2_rindex)];
507 struct gfs2_rgrpd *rgd;
509 error = gfs2_internal_read(ip, ra_state, buf, &pos,
510 sizeof(struct gfs2_rindex));
513 if (error != sizeof(struct gfs2_rindex)) {
519 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
524 mutex_init(&rgd->rd_mutex);
525 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
528 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
529 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
531 gfs2_rindex_in(rgd, buf);
532 error = compute_bitstructs(rgd);
536 error = gfs2_glock_get(sdp, rgd->rd_addr,
537 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
541 rgd->rd_gl->gl_object = rgd;
542 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
543 rgd->rd_flags |= GFS2_RDF_CHECK;
548 * gfs2_ri_update - Pull in a new resource index from the disk
549 * @ip: pointer to the rindex inode
551 * Returns: 0 on successful update, error code otherwise
554 static int gfs2_ri_update(struct gfs2_inode *ip)
556 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
557 struct inode *inode = &ip->i_inode;
558 struct file_ra_state ra_state;
559 u64 rgrp_count = ip->i_di.di_size;
562 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
563 gfs2_consist_inode(ip);
569 file_ra_state_init(&ra_state, inode->i_mapping);
570 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
571 error = read_rindex_entry(ip, &ra_state);
578 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
583 * gfs2_ri_update_special - Pull in a new resource index from the disk
585 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
586 * In this case we know that we don't have any resource groups in memory yet.
588 * @ip: pointer to the rindex inode
590 * Returns: 0 on successful update, error code otherwise
592 static int gfs2_ri_update_special(struct gfs2_inode *ip)
594 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
595 struct inode *inode = &ip->i_inode;
596 struct file_ra_state ra_state;
599 file_ra_state_init(&ra_state, inode->i_mapping);
600 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
601 /* Ignore partials */
602 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
605 error = read_rindex_entry(ip, &ra_state);
612 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
617 * gfs2_rindex_hold - Grab a lock on the rindex
618 * @sdp: The GFS2 superblock
619 * @ri_gh: the glock holder
621 * We grab a lock on the rindex inode to make sure that it doesn't
622 * change whilst we are performing an operation. We keep this lock
623 * for quite long periods of time compared to other locks. This
624 * doesn't matter, since it is shared and it is very, very rarely
625 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
627 * This makes sure that we're using the latest copy of the resource index
628 * special file, which might have been updated if someone expanded the
629 * filesystem (via gfs2_grow utility), which adds new resource groups.
631 * Returns: 0 on success, error code otherwise
634 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
636 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
637 struct gfs2_glock *gl = ip->i_gl;
640 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
644 /* Read new copy from disk if we don't have the latest */
645 if (sdp->sd_rindex_vn != gl->gl_vn) {
646 mutex_lock(&sdp->sd_rindex_mutex);
647 if (sdp->sd_rindex_vn != gl->gl_vn) {
648 error = gfs2_ri_update(ip);
650 gfs2_glock_dq_uninit(ri_gh);
652 mutex_unlock(&sdp->sd_rindex_mutex);
658 static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
660 const struct gfs2_rgrp *str = buf;
662 rg->rg_flags = be32_to_cpu(str->rg_flags);
663 rg->rg_free = be32_to_cpu(str->rg_free);
664 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
665 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
668 static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
670 struct gfs2_rgrp *str = buf;
672 str->rg_flags = cpu_to_be32(rg->rg_flags);
673 str->rg_free = cpu_to_be32(rg->rg_free);
674 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
675 str->__pad = cpu_to_be32(0);
676 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
677 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
681 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
682 * @rgd: the struct gfs2_rgrpd describing the RG to read in
684 * Read in all of a Resource Group's header and bitmap blocks.
685 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
690 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
692 struct gfs2_sbd *sdp = rgd->rd_sbd;
693 struct gfs2_glock *gl = rgd->rd_gl;
694 unsigned int length = rgd->rd_length;
695 struct gfs2_bitmap *bi;
699 mutex_lock(&rgd->rd_mutex);
701 spin_lock(&sdp->sd_rindex_spin);
702 if (rgd->rd_bh_count) {
704 spin_unlock(&sdp->sd_rindex_spin);
705 mutex_unlock(&rgd->rd_mutex);
708 spin_unlock(&sdp->sd_rindex_spin);
710 for (x = 0; x < length; x++) {
711 bi = rgd->rd_bits + x;
712 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
717 for (y = length; y--;) {
718 bi = rgd->rd_bits + y;
719 error = gfs2_meta_wait(sdp, bi->bi_bh);
722 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
729 if (rgd->rd_rg_vn != gl->gl_vn) {
730 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
731 rgd->rd_rg_vn = gl->gl_vn;
734 spin_lock(&sdp->sd_rindex_spin);
735 rgd->rd_free_clone = rgd->rd_rg.rg_free;
737 spin_unlock(&sdp->sd_rindex_spin);
739 mutex_unlock(&rgd->rd_mutex);
745 bi = rgd->rd_bits + x;
748 gfs2_assert_warn(sdp, !bi->bi_clone);
750 mutex_unlock(&rgd->rd_mutex);
755 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
757 struct gfs2_sbd *sdp = rgd->rd_sbd;
759 spin_lock(&sdp->sd_rindex_spin);
760 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
762 spin_unlock(&sdp->sd_rindex_spin);
766 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
767 * @rgd: the struct gfs2_rgrpd describing the RG to read in
771 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
773 struct gfs2_sbd *sdp = rgd->rd_sbd;
774 int x, length = rgd->rd_length;
776 spin_lock(&sdp->sd_rindex_spin);
777 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
778 if (--rgd->rd_bh_count) {
779 spin_unlock(&sdp->sd_rindex_spin);
783 for (x = 0; x < length; x++) {
784 struct gfs2_bitmap *bi = rgd->rd_bits + x;
791 spin_unlock(&sdp->sd_rindex_spin);
794 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
796 struct gfs2_sbd *sdp = rgd->rd_sbd;
797 unsigned int length = rgd->rd_length;
800 for (x = 0; x < length; x++) {
801 struct gfs2_bitmap *bi = rgd->rd_bits + x;
804 memcpy(bi->bi_clone + bi->bi_offset,
805 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
808 spin_lock(&sdp->sd_rindex_spin);
809 rgd->rd_free_clone = rgd->rd_rg.rg_free;
810 spin_unlock(&sdp->sd_rindex_spin);
814 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
815 * @ip: the incore GFS2 inode structure
817 * Returns: the struct gfs2_alloc
820 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
822 BUG_ON(ip->i_alloc != NULL);
823 ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
828 * try_rgrp_fit - See if a given reservation will fit in a given RG
830 * @al: the struct gfs2_alloc structure describing the reservation
832 * If there's room for the requested blocks to be allocated from the RG:
833 * Sets the $al_rgd field in @al.
835 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
838 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
840 struct gfs2_sbd *sdp = rgd->rd_sbd;
843 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
846 spin_lock(&sdp->sd_rindex_spin);
847 if (rgd->rd_free_clone >= al->al_requested) {
851 spin_unlock(&sdp->sd_rindex_spin);
857 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
860 * Returns: The inode, if one has been found
863 static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
868 struct gfs2_sbd *sdp = rgd->rd_sbd;
871 if (goal >= rgd->rd_data)
873 down_write(&sdp->sd_log_flush_lock);
874 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
875 GFS2_BLKST_UNLINKED);
876 up_write(&sdp->sd_log_flush_lock);
877 if (block == BFITNOENT)
879 /* rgblk_search can return a block < goal, so we need to
880 keep it marching forward. */
881 no_addr = block + rgd->rd_data0;
883 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
885 *last_unlinked = no_addr;
886 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
892 rgd->rd_flags &= ~GFS2_RDF_CHECK;
897 * recent_rgrp_first - get first RG from "recent" list
898 * @sdp: The GFS2 superblock
899 * @rglast: address of the rgrp used last
901 * Returns: The first rgrp in the recent list
904 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
907 struct gfs2_rgrpd *rgd = NULL;
909 spin_lock(&sdp->sd_rindex_spin);
911 if (list_empty(&sdp->sd_rindex_recent_list))
917 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
918 if (rgd->rd_addr == rglast)
923 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
926 spin_unlock(&sdp->sd_rindex_spin);
931 * recent_rgrp_next - get next RG from "recent" list
932 * @cur_rgd: current rgrp
935 * Returns: The next rgrp in the recent list
938 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
941 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
942 struct list_head *head;
943 struct gfs2_rgrpd *rgd;
945 spin_lock(&sdp->sd_rindex_spin);
947 head = &sdp->sd_rindex_recent_list;
949 list_for_each_entry(rgd, head, rd_recent) {
950 if (rgd == cur_rgd) {
951 if (cur_rgd->rd_recent.next != head)
952 rgd = list_entry(cur_rgd->rd_recent.next,
953 struct gfs2_rgrpd, rd_recent);
958 list_del(&cur_rgd->rd_recent);
965 if (!list_empty(head))
966 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
969 spin_unlock(&sdp->sd_rindex_spin);
974 * recent_rgrp_add - add an RG to tail of "recent" list
975 * @new_rgd: The rgrp to add
979 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
981 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
982 struct gfs2_rgrpd *rgd;
983 unsigned int count = 0;
984 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
986 spin_lock(&sdp->sd_rindex_spin);
988 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
995 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
998 spin_unlock(&sdp->sd_rindex_spin);
1002 * forward_rgrp_get - get an rgrp to try next from full list
1003 * @sdp: The GFS2 superblock
1005 * Returns: The rgrp to try next
1008 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1010 struct gfs2_rgrpd *rgd;
1011 unsigned int journals = gfs2_jindex_size(sdp);
1012 unsigned int rg = 0, x;
1014 spin_lock(&sdp->sd_rindex_spin);
1016 rgd = sdp->sd_rindex_forward;
1018 if (sdp->sd_rgrps >= journals)
1019 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1021 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
1022 x++, rgd = gfs2_rgrpd_get_next(rgd))
1025 sdp->sd_rindex_forward = rgd;
1028 spin_unlock(&sdp->sd_rindex_spin);
1034 * forward_rgrp_set - set the forward rgrp pointer
1035 * @sdp: the filesystem
1036 * @rgd: The new forward rgrp
1040 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1042 spin_lock(&sdp->sd_rindex_spin);
1043 sdp->sd_rindex_forward = rgd;
1044 spin_unlock(&sdp->sd_rindex_spin);
1048 * get_local_rgrp - Choose and lock a rgrp for allocation
1049 * @ip: the inode to reserve space for
1050 * @rgp: the chosen and locked rgrp
1052 * Try to acquire rgrp in way which avoids contending with others.
1057 static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
1059 struct inode *inode = NULL;
1060 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1061 struct gfs2_rgrpd *rgd, *begin = NULL;
1062 struct gfs2_alloc *al = ip->i_alloc;
1063 int flags = LM_FLAG_TRY;
1066 int error, rg_locked;
1068 /* Try recently successful rgrps */
1070 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
1075 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1079 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1080 LM_FLAG_TRY, &al->al_rgd_gh);
1084 if (try_rgrp_fit(rgd, al))
1086 if (rgd->rd_flags & GFS2_RDF_CHECK)
1087 inode = try_rgrp_unlink(rgd, last_unlinked);
1089 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1092 rgd = recent_rgrp_next(rgd, 1);
1096 rgd = recent_rgrp_next(rgd, 0);
1100 return ERR_PTR(error);
1104 /* Go through full list of rgrps */
1106 begin = rgd = forward_rgrp_get(sdp);
1111 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1115 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1120 if (try_rgrp_fit(rgd, al))
1122 if (rgd->rd_flags & GFS2_RDF_CHECK)
1123 inode = try_rgrp_unlink(rgd, last_unlinked);
1125 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1135 return ERR_PTR(error);
1138 rgd = gfs2_rgrpd_get_next(rgd);
1140 rgd = gfs2_rgrpd_get_first(sdp);
1144 return ERR_PTR(-ENOSPC);
1149 gfs2_log_flush(sdp, NULL);
1154 ip->i_last_rg_alloc = rgd->rd_addr;
1157 recent_rgrp_add(rgd);
1158 rgd = gfs2_rgrpd_get_next(rgd);
1160 rgd = gfs2_rgrpd_get_first(sdp);
1161 forward_rgrp_set(sdp, rgd);
1168 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1169 * @ip: the inode to reserve space for
1174 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1176 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1177 struct gfs2_alloc *al = ip->i_alloc;
1178 struct inode *inode;
1180 u64 last_unlinked = NO_BLOCK;
1182 if (gfs2_assert_warn(sdp, al->al_requested))
1186 /* We need to hold the rindex unless the inode we're using is
1187 the rindex itself, in which case it's already held. */
1188 if (ip != GFS2_I(sdp->sd_rindex))
1189 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1190 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1191 error = gfs2_ri_update_special(ip);
1196 inode = get_local_rgrp(ip, &last_unlinked);
1198 if (ip != GFS2_I(sdp->sd_rindex))
1199 gfs2_glock_dq_uninit(&al->al_ri_gh);
1201 return PTR_ERR(inode);
1203 gfs2_log_flush(sdp, NULL);
1214 * gfs2_inplace_release - release an inplace reservation
1215 * @ip: the inode the reservation was taken out on
1217 * Release a reservation made by gfs2_inplace_reserve().
1220 void gfs2_inplace_release(struct gfs2_inode *ip)
1222 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1223 struct gfs2_alloc *al = ip->i_alloc;
1225 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1226 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1227 "al_file = %s, al_line = %u\n",
1228 al->al_alloced, al->al_requested, al->al_file,
1232 if (al->al_rgd_gh.gh_gl)
1233 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1234 if (ip != GFS2_I(sdp->sd_rindex))
1235 gfs2_glock_dq_uninit(&al->al_ri_gh);
1239 * gfs2_get_block_type - Check a block in a RG is of given type
1240 * @rgd: the resource group holding the block
1241 * @block: the block number
1243 * Returns: The block type (GFS2_BLKST_*)
1246 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1248 struct gfs2_bitmap *bi = NULL;
1249 u32 length, rgrp_block, buf_block;
1253 length = rgd->rd_length;
1254 rgrp_block = block - rgd->rd_data0;
1256 for (buf = 0; buf < length; buf++) {
1257 bi = rgd->rd_bits + buf;
1258 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1262 gfs2_assert(rgd->rd_sbd, buf < length);
1263 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1265 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1266 bi->bi_len, buf_block);
1272 * rgblk_search - find a block in @old_state, change allocation
1273 * state to @new_state
1274 * @rgd: the resource group descriptor
1275 * @goal: the goal block within the RG (start here to search for avail block)
1276 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1277 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1279 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1280 * Add the found bitmap buffer to the transaction.
1281 * Set the found bits to @new_state to change block's allocation state.
1283 * This function never fails, because we wouldn't call it unless we
1284 * know (from reservation results, etc.) that a block is available.
1286 * Scope of @goal and returned block is just within rgrp, not the whole
1289 * Returns: the block number allocated
1292 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1293 unsigned char old_state, unsigned char new_state)
1295 struct gfs2_bitmap *bi = NULL;
1296 u32 length = rgd->rd_length;
1298 unsigned int buf, x;
1300 /* Find bitmap block that contains bits for goal block */
1301 for (buf = 0; buf < length; buf++) {
1302 bi = rgd->rd_bits + buf;
1303 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1307 gfs2_assert(rgd->rd_sbd, buf < length);
1309 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1310 goal -= bi->bi_start * GFS2_NBBY;
1312 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1313 "x <= length", instead of "x < length", because we typically start
1314 the search in the middle of a bit block, but if we can't find an
1315 allocatable block anywhere else, we want to be able wrap around and
1316 search in the first part of our first-searched bit block. */
1317 for (x = 0; x <= length; x++) {
1318 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1319 bitmaps, so we must search the originals for that. */
1320 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
1321 blk = gfs2_bitfit(bi->bi_clone + bi->bi_offset,
1322 bi->bi_len, goal, old_state);
1324 blk = gfs2_bitfit(bi->bi_bh->b_data + bi->bi_offset,
1325 bi->bi_len, goal, old_state);
1326 if (blk != BFITNOENT)
1329 /* Try next bitmap block (wrap back to rgrp header if at end) */
1330 buf = (buf + 1) % length;
1331 bi = rgd->rd_bits + buf;
1335 if (blk != BFITNOENT && old_state != new_state) {
1336 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1337 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1338 bi->bi_len, blk, new_state);
1340 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1341 bi->bi_len, blk, new_state);
1344 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
1348 * rgblk_free - Change alloc state of given block(s)
1349 * @sdp: the filesystem
1350 * @bstart: the start of a run of blocks to free
1351 * @blen: the length of the block run (all must lie within ONE RG!)
1352 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1354 * Returns: Resource group containing the block(s)
1357 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1358 u32 blen, unsigned char new_state)
1360 struct gfs2_rgrpd *rgd;
1361 struct gfs2_bitmap *bi = NULL;
1362 u32 length, rgrp_blk, buf_blk;
1365 rgd = gfs2_blk2rgrpd(sdp, bstart);
1367 if (gfs2_consist(sdp))
1368 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1372 length = rgd->rd_length;
1374 rgrp_blk = bstart - rgd->rd_data0;
1377 for (buf = 0; buf < length; buf++) {
1378 bi = rgd->rd_bits + buf;
1379 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1383 gfs2_assert(rgd->rd_sbd, buf < length);
1385 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1388 if (!bi->bi_clone) {
1389 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1390 GFP_NOFS | __GFP_NOFAIL);
1391 memcpy(bi->bi_clone + bi->bi_offset,
1392 bi->bi_bh->b_data + bi->bi_offset,
1395 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1396 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1397 bi->bi_len, buf_blk, new_state);
1404 * gfs2_alloc_data - Allocate a data block
1405 * @ip: the inode to allocate the data block for
1407 * Returns: the allocated block
1410 u64 gfs2_alloc_data(struct gfs2_inode *ip)
1412 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1413 struct gfs2_alloc *al = ip->i_alloc;
1414 struct gfs2_rgrpd *rgd = al->al_rgd;
1418 if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
1419 goal = ip->i_di.di_goal_data - rgd->rd_data0;
1421 goal = rgd->rd_last_alloc_data;
1423 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1424 BUG_ON(blk == BFITNOENT);
1425 rgd->rd_last_alloc_data = blk;
1427 block = rgd->rd_data0 + blk;
1428 ip->i_di.di_goal_data = block;
1430 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1431 rgd->rd_rg.rg_free--;
1433 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1434 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1438 gfs2_statfs_change(sdp, 0, -1, 0);
1439 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1441 spin_lock(&sdp->sd_rindex_spin);
1442 rgd->rd_free_clone--;
1443 spin_unlock(&sdp->sd_rindex_spin);
1449 * gfs2_alloc_meta - Allocate a metadata block
1450 * @ip: the inode to allocate the metadata block for
1452 * Returns: the allocated block
1455 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
1457 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1458 struct gfs2_alloc *al = ip->i_alloc;
1459 struct gfs2_rgrpd *rgd = al->al_rgd;
1463 if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
1464 goal = ip->i_di.di_goal_meta - rgd->rd_data0;
1466 goal = rgd->rd_last_alloc_meta;
1468 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1469 BUG_ON(blk == BFITNOENT);
1470 rgd->rd_last_alloc_meta = blk;
1472 block = rgd->rd_data0 + blk;
1473 ip->i_di.di_goal_meta = block;
1475 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1476 rgd->rd_rg.rg_free--;
1478 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1479 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1483 gfs2_statfs_change(sdp, 0, -1, 0);
1484 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1485 gfs2_trans_add_unrevoke(sdp, block);
1487 spin_lock(&sdp->sd_rindex_spin);
1488 rgd->rd_free_clone--;
1489 spin_unlock(&sdp->sd_rindex_spin);
1495 * gfs2_alloc_di - Allocate a dinode
1496 * @dip: the directory that the inode is going in
1498 * Returns: the block allocated
1501 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1503 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1504 struct gfs2_alloc *al = dip->i_alloc;
1505 struct gfs2_rgrpd *rgd = al->al_rgd;
1509 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1510 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1511 BUG_ON(blk == BFITNOENT);
1513 rgd->rd_last_alloc_meta = blk;
1515 block = rgd->rd_data0 + blk;
1517 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1518 rgd->rd_rg.rg_free--;
1519 rgd->rd_rg.rg_dinodes++;
1520 *generation = rgd->rd_rg.rg_igeneration++;
1521 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1522 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1526 gfs2_statfs_change(sdp, 0, -1, +1);
1527 gfs2_trans_add_unrevoke(sdp, block);
1529 spin_lock(&sdp->sd_rindex_spin);
1530 rgd->rd_free_clone--;
1531 spin_unlock(&sdp->sd_rindex_spin);
1537 * gfs2_free_data - free a contiguous run of data block(s)
1538 * @ip: the inode these blocks are being freed from
1539 * @bstart: first block of a run of contiguous blocks
1540 * @blen: the length of the block run
1544 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1546 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1547 struct gfs2_rgrpd *rgd;
1549 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1553 rgd->rd_rg.rg_free += blen;
1555 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1556 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1558 gfs2_trans_add_rg(rgd);
1560 gfs2_statfs_change(sdp, 0, +blen, 0);
1561 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1565 * gfs2_free_meta - free a contiguous run of data block(s)
1566 * @ip: the inode these blocks are being freed from
1567 * @bstart: first block of a run of contiguous blocks
1568 * @blen: the length of the block run
1572 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1574 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1575 struct gfs2_rgrpd *rgd;
1577 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1581 rgd->rd_rg.rg_free += blen;
1583 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1584 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1586 gfs2_trans_add_rg(rgd);
1588 gfs2_statfs_change(sdp, 0, +blen, 0);
1589 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1590 gfs2_meta_wipe(ip, bstart, blen);
1593 void gfs2_unlink_di(struct inode *inode)
1595 struct gfs2_inode *ip = GFS2_I(inode);
1596 struct gfs2_sbd *sdp = GFS2_SB(inode);
1597 struct gfs2_rgrpd *rgd;
1598 u64 blkno = ip->i_no_addr;
1600 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1603 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1604 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1605 gfs2_trans_add_rg(rgd);
1608 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1610 struct gfs2_sbd *sdp = rgd->rd_sbd;
1611 struct gfs2_rgrpd *tmp_rgd;
1613 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1616 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1618 if (!rgd->rd_rg.rg_dinodes)
1619 gfs2_consist_rgrpd(rgd);
1620 rgd->rd_rg.rg_dinodes--;
1621 rgd->rd_rg.rg_free++;
1623 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1624 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1626 gfs2_statfs_change(sdp, 0, +1, -1);
1627 gfs2_trans_add_rg(rgd);
1631 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1633 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1634 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1635 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1639 * gfs2_rlist_add - add a RG to a list of RGs
1640 * @sdp: the filesystem
1641 * @rlist: the list of resource groups
1644 * Figure out what RG a block belongs to and add that RG to the list
1646 * FIXME: Don't use NOFAIL
1650 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1653 struct gfs2_rgrpd *rgd;
1654 struct gfs2_rgrpd **tmp;
1655 unsigned int new_space;
1658 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1661 rgd = gfs2_blk2rgrpd(sdp, block);
1663 if (gfs2_consist(sdp))
1664 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1668 for (x = 0; x < rlist->rl_rgrps; x++)
1669 if (rlist->rl_rgd[x] == rgd)
1672 if (rlist->rl_rgrps == rlist->rl_space) {
1673 new_space = rlist->rl_space + 10;
1675 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1676 GFP_NOFS | __GFP_NOFAIL);
1678 if (rlist->rl_rgd) {
1679 memcpy(tmp, rlist->rl_rgd,
1680 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1681 kfree(rlist->rl_rgd);
1684 rlist->rl_space = new_space;
1685 rlist->rl_rgd = tmp;
1688 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1692 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1693 * and initialize an array of glock holders for them
1694 * @rlist: the list of resource groups
1695 * @state: the lock state to acquire the RG lock in
1696 * @flags: the modifier flags for the holder structures
1698 * FIXME: Don't use NOFAIL
1702 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1707 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1708 GFP_NOFS | __GFP_NOFAIL);
1709 for (x = 0; x < rlist->rl_rgrps; x++)
1710 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1716 * gfs2_rlist_free - free a resource group list
1717 * @list: the list of resource groups
1721 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1725 kfree(rlist->rl_rgd);
1727 if (rlist->rl_ghs) {
1728 for (x = 0; x < rlist->rl_rgrps; x++)
1729 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1730 kfree(rlist->rl_ghs);