[GFS2] Run through full bitmaps quicker in gfs2_bitfit
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / gfs2 / rgrp.c
blobd7ff9cf6653fa823fe9c02054f0c6238691e8856
1 /*
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.
8 */
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/fs.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/lm_interface.h>
18 #include "gfs2.h"
19 #include "incore.h"
20 #include "glock.h"
21 #include "glops.h"
22 #include "lops.h"
23 #include "meta_io.h"
24 #include "quota.h"
25 #include "rgrp.h"
26 #include "super.h"
27 #include "trans.h"
28 #include "util.h"
29 #include "log.h"
30 #include "inode.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.
41 * 0 = Free
42 * 1 = Used (not metadata)
43 * 2 = Unlinked (still in use) inode
44 * 3 = Used (metadata)
47 static const char valid_change[16] = {
48 /* current */
49 /* n */ 0, 1, 1, 1,
50 /* e */ 1, 0, 0, 0,
51 /* w */ 0, 0, 0, 1,
52 1, 0, 0, 0
55 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
56 unsigned char old_state, unsigned char new_state);
58 /**
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;
72 unsigned int bit;
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;
85 } else
86 gfs2_consist_rgrpd(rgd);
89 /**
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;
101 unsigned int bit;
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;
111 return cur_state;
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)
132 unsigned char *byte;
133 u32 blk = goal;
134 unsigned int bit, bitlong;
135 unsigned long *plong, plong55;
136 static int c = 0;
138 byte = buffer + (goal / GFS2_NBBY);
139 plong = buffer + (goal / GFS2_NBBY);
140 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
141 bitlong = bit;
142 #if BITS_PER_LONG == 32
143 plong55 = 0x55555555;
144 #else
145 plong55 = 0x5555555555555555;
146 #endif
147 while (byte < buffer + buflen) {
149 if (bitlong == 0 && old_state == 0 && *plong == plong55) {
150 plong++;
151 byte += sizeof(unsigned long);
152 blk += sizeof(unsigned long) * GFS2_NBBY;
153 continue;
155 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state) {
156 c++;
157 return blk;
159 bit += GFS2_BIT_SIZE;
160 if (bit >= 8) {
161 bit = 0;
162 byte++;
164 bitlong += GFS2_BIT_SIZE;
165 if (bitlong >= sizeof(unsigned long) * 8) {
166 bitlong = 0;
167 plong++;
170 blk++;
173 return BFITNOENT;
177 * gfs2_bitcount - count the number of bits in a certain state
178 * @buffer: the buffer that holds the bitmaps
179 * @buflen: the length (in bytes) of the buffer
180 * @state: the state of the block we're looking for
182 * Returns: The number of bits
185 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
186 unsigned int buflen, unsigned char state)
188 unsigned char *byte = buffer;
189 unsigned char *end = buffer + buflen;
190 unsigned char state1 = state << 2;
191 unsigned char state2 = state << 4;
192 unsigned char state3 = state << 6;
193 u32 count = 0;
195 for (; byte < end; byte++) {
196 if (((*byte) & 0x03) == state)
197 count++;
198 if (((*byte) & 0x0C) == state1)
199 count++;
200 if (((*byte) & 0x30) == state2)
201 count++;
202 if (((*byte) & 0xC0) == state3)
203 count++;
206 return count;
210 * gfs2_rgrp_verify - Verify that a resource group is consistent
211 * @sdp: the filesystem
212 * @rgd: the rgrp
216 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
218 struct gfs2_sbd *sdp = rgd->rd_sbd;
219 struct gfs2_bitmap *bi = NULL;
220 u32 length = rgd->rd_length;
221 u32 count[4], tmp;
222 int buf, x;
224 memset(count, 0, 4 * sizeof(u32));
226 /* Count # blocks in each of 4 possible allocation states */
227 for (buf = 0; buf < length; buf++) {
228 bi = rgd->rd_bits + buf;
229 for (x = 0; x < 4; x++)
230 count[x] += gfs2_bitcount(rgd,
231 bi->bi_bh->b_data +
232 bi->bi_offset,
233 bi->bi_len, x);
236 if (count[0] != rgd->rd_rg.rg_free) {
237 if (gfs2_consist_rgrpd(rgd))
238 fs_err(sdp, "free data mismatch: %u != %u\n",
239 count[0], rgd->rd_rg.rg_free);
240 return;
243 tmp = rgd->rd_data -
244 rgd->rd_rg.rg_free -
245 rgd->rd_rg.rg_dinodes;
246 if (count[1] + count[2] != tmp) {
247 if (gfs2_consist_rgrpd(rgd))
248 fs_err(sdp, "used data mismatch: %u != %u\n",
249 count[1], tmp);
250 return;
253 if (count[3] != rgd->rd_rg.rg_dinodes) {
254 if (gfs2_consist_rgrpd(rgd))
255 fs_err(sdp, "used metadata mismatch: %u != %u\n",
256 count[3], rgd->rd_rg.rg_dinodes);
257 return;
260 if (count[2] > count[3]) {
261 if (gfs2_consist_rgrpd(rgd))
262 fs_err(sdp, "unlinked inodes > inodes: %u\n",
263 count[2]);
264 return;
269 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
271 u64 first = rgd->rd_data0;
272 u64 last = first + rgd->rd_data;
273 return first <= block && block < last;
277 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
278 * @sdp: The GFS2 superblock
279 * @n: The data block number
281 * Returns: The resource group, or NULL if not found
284 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
286 struct gfs2_rgrpd *rgd;
288 spin_lock(&sdp->sd_rindex_spin);
290 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
291 if (rgrp_contains_block(rgd, blk)) {
292 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
293 spin_unlock(&sdp->sd_rindex_spin);
294 return rgd;
298 spin_unlock(&sdp->sd_rindex_spin);
300 return NULL;
304 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
305 * @sdp: The GFS2 superblock
307 * Returns: The first rgrp in the filesystem
310 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
312 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
313 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
317 * gfs2_rgrpd_get_next - get the next RG
318 * @rgd: A RG
320 * Returns: The next rgrp
323 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
325 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
326 return NULL;
327 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
330 static void clear_rgrpdi(struct gfs2_sbd *sdp)
332 struct list_head *head;
333 struct gfs2_rgrpd *rgd;
334 struct gfs2_glock *gl;
336 spin_lock(&sdp->sd_rindex_spin);
337 sdp->sd_rindex_forward = NULL;
338 head = &sdp->sd_rindex_recent_list;
339 while (!list_empty(head)) {
340 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
341 list_del(&rgd->rd_recent);
343 spin_unlock(&sdp->sd_rindex_spin);
345 head = &sdp->sd_rindex_list;
346 while (!list_empty(head)) {
347 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
348 gl = rgd->rd_gl;
350 list_del(&rgd->rd_list);
351 list_del(&rgd->rd_list_mru);
353 if (gl) {
354 gl->gl_object = NULL;
355 gfs2_glock_put(gl);
358 kfree(rgd->rd_bits);
359 kfree(rgd);
363 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
365 mutex_lock(&sdp->sd_rindex_mutex);
366 clear_rgrpdi(sdp);
367 mutex_unlock(&sdp->sd_rindex_mutex);
370 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
372 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
373 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
374 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
375 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
376 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
380 * gfs2_compute_bitstructs - Compute the bitmap sizes
381 * @rgd: The resource group descriptor
383 * Calculates bitmap descriptors, one for each block that contains bitmap data
385 * Returns: errno
388 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
390 struct gfs2_sbd *sdp = rgd->rd_sbd;
391 struct gfs2_bitmap *bi;
392 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
393 u32 bytes_left, bytes;
394 int x;
396 if (!length)
397 return -EINVAL;
399 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
400 if (!rgd->rd_bits)
401 return -ENOMEM;
403 bytes_left = rgd->rd_bitbytes;
405 for (x = 0; x < length; x++) {
406 bi = rgd->rd_bits + x;
408 /* small rgrp; bitmap stored completely in header block */
409 if (length == 1) {
410 bytes = bytes_left;
411 bi->bi_offset = sizeof(struct gfs2_rgrp);
412 bi->bi_start = 0;
413 bi->bi_len = bytes;
414 /* header block */
415 } else if (x == 0) {
416 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
417 bi->bi_offset = sizeof(struct gfs2_rgrp);
418 bi->bi_start = 0;
419 bi->bi_len = bytes;
420 /* last block */
421 } else if (x + 1 == length) {
422 bytes = bytes_left;
423 bi->bi_offset = sizeof(struct gfs2_meta_header);
424 bi->bi_start = rgd->rd_bitbytes - bytes_left;
425 bi->bi_len = bytes;
426 /* other blocks */
427 } else {
428 bytes = sdp->sd_sb.sb_bsize -
429 sizeof(struct gfs2_meta_header);
430 bi->bi_offset = sizeof(struct gfs2_meta_header);
431 bi->bi_start = rgd->rd_bitbytes - bytes_left;
432 bi->bi_len = bytes;
435 bytes_left -= bytes;
438 if (bytes_left) {
439 gfs2_consist_rgrpd(rgd);
440 return -EIO;
442 bi = rgd->rd_bits + (length - 1);
443 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
444 if (gfs2_consist_rgrpd(rgd)) {
445 gfs2_rindex_print(rgd);
446 fs_err(sdp, "start=%u len=%u offset=%u\n",
447 bi->bi_start, bi->bi_len, bi->bi_offset);
449 return -EIO;
452 return 0;
456 * gfs2_ri_total - Total up the file system space, according to the rindex.
459 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
461 u64 total_data = 0;
462 struct inode *inode = sdp->sd_rindex;
463 struct gfs2_inode *ip = GFS2_I(inode);
464 char buf[sizeof(struct gfs2_rindex)];
465 struct file_ra_state ra_state;
466 int error, rgrps;
468 mutex_lock(&sdp->sd_rindex_mutex);
469 file_ra_state_init(&ra_state, inode->i_mapping);
470 for (rgrps = 0;; rgrps++) {
471 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
473 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
474 break;
475 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
476 sizeof(struct gfs2_rindex));
477 if (error != sizeof(struct gfs2_rindex))
478 break;
479 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
481 mutex_unlock(&sdp->sd_rindex_mutex);
482 return total_data;
485 static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
487 const struct gfs2_rindex *str = buf;
489 rgd->rd_addr = be64_to_cpu(str->ri_addr);
490 rgd->rd_length = be32_to_cpu(str->ri_length);
491 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
492 rgd->rd_data = be32_to_cpu(str->ri_data);
493 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
497 * read_rindex_entry - Pull in a new resource index entry from the disk
498 * @gl: The glock covering the rindex inode
500 * Returns: 0 on success, error code otherwise
503 static int read_rindex_entry(struct gfs2_inode *ip,
504 struct file_ra_state *ra_state)
506 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
507 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
508 char buf[sizeof(struct gfs2_rindex)];
509 int error;
510 struct gfs2_rgrpd *rgd;
512 error = gfs2_internal_read(ip, ra_state, buf, &pos,
513 sizeof(struct gfs2_rindex));
514 if (!error)
515 return 0;
516 if (error != sizeof(struct gfs2_rindex)) {
517 if (error > 0)
518 error = -EIO;
519 return error;
522 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
523 error = -ENOMEM;
524 if (!rgd)
525 return error;
527 mutex_init(&rgd->rd_mutex);
528 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
529 rgd->rd_sbd = sdp;
531 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
532 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
534 gfs2_rindex_in(rgd, buf);
535 error = compute_bitstructs(rgd);
536 if (error)
537 return error;
539 error = gfs2_glock_get(sdp, rgd->rd_addr,
540 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
541 if (error)
542 return error;
544 rgd->rd_gl->gl_object = rgd;
545 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
546 rgd->rd_flags |= GFS2_RDF_CHECK;
547 return error;
551 * gfs2_ri_update - Pull in a new resource index from the disk
552 * @ip: pointer to the rindex inode
554 * Returns: 0 on successful update, error code otherwise
557 static int gfs2_ri_update(struct gfs2_inode *ip)
559 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
560 struct inode *inode = &ip->i_inode;
561 struct file_ra_state ra_state;
562 u64 rgrp_count = ip->i_di.di_size;
563 int error;
565 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
566 gfs2_consist_inode(ip);
567 return -EIO;
570 clear_rgrpdi(sdp);
572 file_ra_state_init(&ra_state, inode->i_mapping);
573 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
574 error = read_rindex_entry(ip, &ra_state);
575 if (error) {
576 clear_rgrpdi(sdp);
577 return error;
581 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
582 return 0;
586 * gfs2_ri_update_special - Pull in a new resource index from the disk
588 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
589 * In this case we know that we don't have any resource groups in memory yet.
591 * @ip: pointer to the rindex inode
593 * Returns: 0 on successful update, error code otherwise
595 static int gfs2_ri_update_special(struct gfs2_inode *ip)
597 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
598 struct inode *inode = &ip->i_inode;
599 struct file_ra_state ra_state;
600 int error;
602 file_ra_state_init(&ra_state, inode->i_mapping);
603 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
604 /* Ignore partials */
605 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
606 ip->i_di.di_size)
607 break;
608 error = read_rindex_entry(ip, &ra_state);
609 if (error) {
610 clear_rgrpdi(sdp);
611 return error;
615 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
616 return 0;
620 * gfs2_rindex_hold - Grab a lock on the rindex
621 * @sdp: The GFS2 superblock
622 * @ri_gh: the glock holder
624 * We grab a lock on the rindex inode to make sure that it doesn't
625 * change whilst we are performing an operation. We keep this lock
626 * for quite long periods of time compared to other locks. This
627 * doesn't matter, since it is shared and it is very, very rarely
628 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
630 * This makes sure that we're using the latest copy of the resource index
631 * special file, which might have been updated if someone expanded the
632 * filesystem (via gfs2_grow utility), which adds new resource groups.
634 * Returns: 0 on success, error code otherwise
637 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
639 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
640 struct gfs2_glock *gl = ip->i_gl;
641 int error;
643 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
644 if (error)
645 return error;
647 /* Read new copy from disk if we don't have the latest */
648 if (sdp->sd_rindex_vn != gl->gl_vn) {
649 mutex_lock(&sdp->sd_rindex_mutex);
650 if (sdp->sd_rindex_vn != gl->gl_vn) {
651 error = gfs2_ri_update(ip);
652 if (error)
653 gfs2_glock_dq_uninit(ri_gh);
655 mutex_unlock(&sdp->sd_rindex_mutex);
658 return error;
661 static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
663 const struct gfs2_rgrp *str = buf;
665 rg->rg_flags = be32_to_cpu(str->rg_flags);
666 rg->rg_free = be32_to_cpu(str->rg_free);
667 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
668 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
671 static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
673 struct gfs2_rgrp *str = buf;
675 str->rg_flags = cpu_to_be32(rg->rg_flags);
676 str->rg_free = cpu_to_be32(rg->rg_free);
677 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
678 str->__pad = cpu_to_be32(0);
679 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
680 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
684 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
685 * @rgd: the struct gfs2_rgrpd describing the RG to read in
687 * Read in all of a Resource Group's header and bitmap blocks.
688 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
690 * Returns: errno
693 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
695 struct gfs2_sbd *sdp = rgd->rd_sbd;
696 struct gfs2_glock *gl = rgd->rd_gl;
697 unsigned int length = rgd->rd_length;
698 struct gfs2_bitmap *bi;
699 unsigned int x, y;
700 int error;
702 mutex_lock(&rgd->rd_mutex);
704 spin_lock(&sdp->sd_rindex_spin);
705 if (rgd->rd_bh_count) {
706 rgd->rd_bh_count++;
707 spin_unlock(&sdp->sd_rindex_spin);
708 mutex_unlock(&rgd->rd_mutex);
709 return 0;
711 spin_unlock(&sdp->sd_rindex_spin);
713 for (x = 0; x < length; x++) {
714 bi = rgd->rd_bits + x;
715 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
716 if (error)
717 goto fail;
720 for (y = length; y--;) {
721 bi = rgd->rd_bits + y;
722 error = gfs2_meta_wait(sdp, bi->bi_bh);
723 if (error)
724 goto fail;
725 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
726 GFS2_METATYPE_RG)) {
727 error = -EIO;
728 goto fail;
732 if (rgd->rd_rg_vn != gl->gl_vn) {
733 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
734 rgd->rd_rg_vn = gl->gl_vn;
737 spin_lock(&sdp->sd_rindex_spin);
738 rgd->rd_free_clone = rgd->rd_rg.rg_free;
739 rgd->rd_bh_count++;
740 spin_unlock(&sdp->sd_rindex_spin);
742 mutex_unlock(&rgd->rd_mutex);
744 return 0;
746 fail:
747 while (x--) {
748 bi = rgd->rd_bits + x;
749 brelse(bi->bi_bh);
750 bi->bi_bh = NULL;
751 gfs2_assert_warn(sdp, !bi->bi_clone);
753 mutex_unlock(&rgd->rd_mutex);
755 return error;
758 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
760 struct gfs2_sbd *sdp = rgd->rd_sbd;
762 spin_lock(&sdp->sd_rindex_spin);
763 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
764 rgd->rd_bh_count++;
765 spin_unlock(&sdp->sd_rindex_spin);
769 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
770 * @rgd: the struct gfs2_rgrpd describing the RG to read in
774 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
776 struct gfs2_sbd *sdp = rgd->rd_sbd;
777 int x, length = rgd->rd_length;
779 spin_lock(&sdp->sd_rindex_spin);
780 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
781 if (--rgd->rd_bh_count) {
782 spin_unlock(&sdp->sd_rindex_spin);
783 return;
786 for (x = 0; x < length; x++) {
787 struct gfs2_bitmap *bi = rgd->rd_bits + x;
788 kfree(bi->bi_clone);
789 bi->bi_clone = NULL;
790 brelse(bi->bi_bh);
791 bi->bi_bh = NULL;
794 spin_unlock(&sdp->sd_rindex_spin);
797 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
799 struct gfs2_sbd *sdp = rgd->rd_sbd;
800 unsigned int length = rgd->rd_length;
801 unsigned int x;
803 for (x = 0; x < length; x++) {
804 struct gfs2_bitmap *bi = rgd->rd_bits + x;
805 if (!bi->bi_clone)
806 continue;
807 memcpy(bi->bi_clone + bi->bi_offset,
808 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
811 spin_lock(&sdp->sd_rindex_spin);
812 rgd->rd_free_clone = rgd->rd_rg.rg_free;
813 spin_unlock(&sdp->sd_rindex_spin);
817 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
818 * @ip: the incore GFS2 inode structure
820 * Returns: the struct gfs2_alloc
823 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
825 struct gfs2_alloc *al = &ip->i_alloc;
827 /* FIXME: Should assert that the correct locks are held here... */
828 memset(al, 0, sizeof(*al));
829 return al;
833 * try_rgrp_fit - See if a given reservation will fit in a given RG
834 * @rgd: the RG data
835 * @al: the struct gfs2_alloc structure describing the reservation
837 * If there's room for the requested blocks to be allocated from the RG:
838 * Sets the $al_rgd field in @al.
840 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
843 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
845 struct gfs2_sbd *sdp = rgd->rd_sbd;
846 int ret = 0;
848 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
849 return 0;
851 spin_lock(&sdp->sd_rindex_spin);
852 if (rgd->rd_free_clone >= al->al_requested) {
853 al->al_rgd = rgd;
854 ret = 1;
856 spin_unlock(&sdp->sd_rindex_spin);
858 return ret;
862 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
863 * @rgd: The rgrp
865 * Returns: The inode, if one has been found
868 static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
870 struct inode *inode;
871 u32 goal = 0, block;
872 u64 no_addr;
873 struct gfs2_sbd *sdp = rgd->rd_sbd;
875 for(;;) {
876 if (goal >= rgd->rd_data)
877 break;
878 down_write(&sdp->sd_log_flush_lock);
879 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
880 GFS2_BLKST_UNLINKED);
881 up_write(&sdp->sd_log_flush_lock);
882 if (block == BFITNOENT)
883 break;
884 /* rgblk_search can return a block < goal, so we need to
885 keep it marching forward. */
886 no_addr = block + rgd->rd_data0;
887 goal++;
888 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
889 continue;
890 *last_unlinked = no_addr;
891 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
892 no_addr, -1, 1);
893 if (!IS_ERR(inode))
894 return inode;
897 rgd->rd_flags &= ~GFS2_RDF_CHECK;
898 return NULL;
902 * recent_rgrp_first - get first RG from "recent" list
903 * @sdp: The GFS2 superblock
904 * @rglast: address of the rgrp used last
906 * Returns: The first rgrp in the recent list
909 static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
910 u64 rglast)
912 struct gfs2_rgrpd *rgd = NULL;
914 spin_lock(&sdp->sd_rindex_spin);
916 if (list_empty(&sdp->sd_rindex_recent_list))
917 goto out;
919 if (!rglast)
920 goto first;
922 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
923 if (rgd->rd_addr == rglast)
924 goto out;
927 first:
928 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
929 rd_recent);
930 out:
931 spin_unlock(&sdp->sd_rindex_spin);
932 return rgd;
936 * recent_rgrp_next - get next RG from "recent" list
937 * @cur_rgd: current rgrp
938 * @remove:
940 * Returns: The next rgrp in the recent list
943 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
944 int remove)
946 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
947 struct list_head *head;
948 struct gfs2_rgrpd *rgd;
950 spin_lock(&sdp->sd_rindex_spin);
952 head = &sdp->sd_rindex_recent_list;
954 list_for_each_entry(rgd, head, rd_recent) {
955 if (rgd == cur_rgd) {
956 if (cur_rgd->rd_recent.next != head)
957 rgd = list_entry(cur_rgd->rd_recent.next,
958 struct gfs2_rgrpd, rd_recent);
959 else
960 rgd = NULL;
962 if (remove)
963 list_del(&cur_rgd->rd_recent);
965 goto out;
969 rgd = NULL;
970 if (!list_empty(head))
971 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
973 out:
974 spin_unlock(&sdp->sd_rindex_spin);
975 return rgd;
979 * recent_rgrp_add - add an RG to tail of "recent" list
980 * @new_rgd: The rgrp to add
984 static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
986 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
987 struct gfs2_rgrpd *rgd;
988 unsigned int count = 0;
989 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
991 spin_lock(&sdp->sd_rindex_spin);
993 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
994 if (rgd == new_rgd)
995 goto out;
997 if (++count >= max)
998 goto out;
1000 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
1002 out:
1003 spin_unlock(&sdp->sd_rindex_spin);
1007 * forward_rgrp_get - get an rgrp to try next from full list
1008 * @sdp: The GFS2 superblock
1010 * Returns: The rgrp to try next
1013 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1015 struct gfs2_rgrpd *rgd;
1016 unsigned int journals = gfs2_jindex_size(sdp);
1017 unsigned int rg = 0, x;
1019 spin_lock(&sdp->sd_rindex_spin);
1021 rgd = sdp->sd_rindex_forward;
1022 if (!rgd) {
1023 if (sdp->sd_rgrps >= journals)
1024 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1026 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
1027 x++, rgd = gfs2_rgrpd_get_next(rgd))
1028 /* Do Nothing */;
1030 sdp->sd_rindex_forward = rgd;
1033 spin_unlock(&sdp->sd_rindex_spin);
1035 return rgd;
1039 * forward_rgrp_set - set the forward rgrp pointer
1040 * @sdp: the filesystem
1041 * @rgd: The new forward rgrp
1045 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1047 spin_lock(&sdp->sd_rindex_spin);
1048 sdp->sd_rindex_forward = rgd;
1049 spin_unlock(&sdp->sd_rindex_spin);
1053 * get_local_rgrp - Choose and lock a rgrp for allocation
1054 * @ip: the inode to reserve space for
1055 * @rgp: the chosen and locked rgrp
1057 * Try to acquire rgrp in way which avoids contending with others.
1059 * Returns: errno
1062 static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
1064 struct inode *inode = NULL;
1065 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1066 struct gfs2_rgrpd *rgd, *begin = NULL;
1067 struct gfs2_alloc *al = &ip->i_alloc;
1068 int flags = LM_FLAG_TRY;
1069 int skipped = 0;
1070 int loops = 0;
1071 int error, rg_locked;
1073 /* Try recently successful rgrps */
1075 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
1077 while (rgd) {
1078 rg_locked = 0;
1080 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1081 rg_locked = 1;
1082 error = 0;
1083 } else {
1084 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1085 LM_FLAG_TRY, &al->al_rgd_gh);
1087 switch (error) {
1088 case 0:
1089 if (try_rgrp_fit(rgd, al))
1090 goto out;
1091 if (rgd->rd_flags & GFS2_RDF_CHECK)
1092 inode = try_rgrp_unlink(rgd, last_unlinked);
1093 if (!rg_locked)
1094 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1095 if (inode)
1096 return inode;
1097 rgd = recent_rgrp_next(rgd, 1);
1098 break;
1100 case GLR_TRYFAILED:
1101 rgd = recent_rgrp_next(rgd, 0);
1102 break;
1104 default:
1105 return ERR_PTR(error);
1109 /* Go through full list of rgrps */
1111 begin = rgd = forward_rgrp_get(sdp);
1113 for (;;) {
1114 rg_locked = 0;
1116 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1117 rg_locked = 1;
1118 error = 0;
1119 } else {
1120 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1121 &al->al_rgd_gh);
1123 switch (error) {
1124 case 0:
1125 if (try_rgrp_fit(rgd, al))
1126 goto out;
1127 if (rgd->rd_flags & GFS2_RDF_CHECK)
1128 inode = try_rgrp_unlink(rgd, last_unlinked);
1129 if (!rg_locked)
1130 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1131 if (inode)
1132 return inode;
1133 break;
1135 case GLR_TRYFAILED:
1136 skipped++;
1137 break;
1139 default:
1140 return ERR_PTR(error);
1143 rgd = gfs2_rgrpd_get_next(rgd);
1144 if (!rgd)
1145 rgd = gfs2_rgrpd_get_first(sdp);
1147 if (rgd == begin) {
1148 if (++loops >= 3)
1149 return ERR_PTR(-ENOSPC);
1150 if (!skipped)
1151 loops++;
1152 flags = 0;
1153 if (loops == 2)
1154 gfs2_log_flush(sdp, NULL);
1158 out:
1159 ip->i_last_rg_alloc = rgd->rd_addr;
1161 if (begin) {
1162 recent_rgrp_add(rgd);
1163 rgd = gfs2_rgrpd_get_next(rgd);
1164 if (!rgd)
1165 rgd = gfs2_rgrpd_get_first(sdp);
1166 forward_rgrp_set(sdp, rgd);
1169 return NULL;
1173 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1174 * @ip: the inode to reserve space for
1176 * Returns: errno
1179 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1181 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1182 struct gfs2_alloc *al = &ip->i_alloc;
1183 struct inode *inode;
1184 int error = 0;
1185 u64 last_unlinked = NO_BLOCK;
1187 if (gfs2_assert_warn(sdp, al->al_requested))
1188 return -EINVAL;
1190 try_again:
1191 /* We need to hold the rindex unless the inode we're using is
1192 the rindex itself, in which case it's already held. */
1193 if (ip != GFS2_I(sdp->sd_rindex))
1194 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1195 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1196 error = gfs2_ri_update_special(ip);
1198 if (error)
1199 return error;
1201 inode = get_local_rgrp(ip, &last_unlinked);
1202 if (inode) {
1203 if (ip != GFS2_I(sdp->sd_rindex))
1204 gfs2_glock_dq_uninit(&al->al_ri_gh);
1205 if (IS_ERR(inode))
1206 return PTR_ERR(inode);
1207 iput(inode);
1208 gfs2_log_flush(sdp, NULL);
1209 goto try_again;
1212 al->al_file = file;
1213 al->al_line = line;
1215 return 0;
1219 * gfs2_inplace_release - release an inplace reservation
1220 * @ip: the inode the reservation was taken out on
1222 * Release a reservation made by gfs2_inplace_reserve().
1225 void gfs2_inplace_release(struct gfs2_inode *ip)
1227 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1228 struct gfs2_alloc *al = &ip->i_alloc;
1230 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1231 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1232 "al_file = %s, al_line = %u\n",
1233 al->al_alloced, al->al_requested, al->al_file,
1234 al->al_line);
1236 al->al_rgd = NULL;
1237 if (al->al_rgd_gh.gh_gl)
1238 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1239 if (ip != GFS2_I(sdp->sd_rindex))
1240 gfs2_glock_dq_uninit(&al->al_ri_gh);
1244 * gfs2_get_block_type - Check a block in a RG is of given type
1245 * @rgd: the resource group holding the block
1246 * @block: the block number
1248 * Returns: The block type (GFS2_BLKST_*)
1251 unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1253 struct gfs2_bitmap *bi = NULL;
1254 u32 length, rgrp_block, buf_block;
1255 unsigned int buf;
1256 unsigned char type;
1258 length = rgd->rd_length;
1259 rgrp_block = block - rgd->rd_data0;
1261 for (buf = 0; buf < length; buf++) {
1262 bi = rgd->rd_bits + buf;
1263 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1264 break;
1267 gfs2_assert(rgd->rd_sbd, buf < length);
1268 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1270 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1271 bi->bi_len, buf_block);
1273 return type;
1277 * rgblk_search - find a block in @old_state, change allocation
1278 * state to @new_state
1279 * @rgd: the resource group descriptor
1280 * @goal: the goal block within the RG (start here to search for avail block)
1281 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1282 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1284 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1285 * Add the found bitmap buffer to the transaction.
1286 * Set the found bits to @new_state to change block's allocation state.
1288 * This function never fails, because we wouldn't call it unless we
1289 * know (from reservation results, etc.) that a block is available.
1291 * Scope of @goal and returned block is just within rgrp, not the whole
1292 * filesystem.
1294 * Returns: the block number allocated
1297 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1298 unsigned char old_state, unsigned char new_state)
1300 struct gfs2_bitmap *bi = NULL;
1301 u32 length = rgd->rd_length;
1302 u32 blk = 0;
1303 unsigned int buf, x;
1305 /* Find bitmap block that contains bits for goal block */
1306 for (buf = 0; buf < length; buf++) {
1307 bi = rgd->rd_bits + buf;
1308 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1309 break;
1312 gfs2_assert(rgd->rd_sbd, buf < length);
1314 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1315 goal -= bi->bi_start * GFS2_NBBY;
1317 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1318 "x <= length", instead of "x < length", because we typically start
1319 the search in the middle of a bit block, but if we can't find an
1320 allocatable block anywhere else, we want to be able wrap around and
1321 search in the first part of our first-searched bit block. */
1322 for (x = 0; x <= length; x++) {
1323 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1324 bitmaps, so we must search the originals for that. */
1325 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
1326 blk = gfs2_bitfit(bi->bi_clone + bi->bi_offset,
1327 bi->bi_len, goal, old_state);
1328 else
1329 blk = gfs2_bitfit(bi->bi_bh->b_data + bi->bi_offset,
1330 bi->bi_len, goal, old_state);
1331 if (blk != BFITNOENT)
1332 break;
1334 /* Try next bitmap block (wrap back to rgrp header if at end) */
1335 buf = (buf + 1) % length;
1336 bi = rgd->rd_bits + buf;
1337 goal = 0;
1340 if (blk != BFITNOENT && old_state != new_state) {
1341 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1342 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1343 bi->bi_len, blk, new_state);
1344 if (bi->bi_clone)
1345 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1346 bi->bi_len, blk, new_state);
1349 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
1353 * rgblk_free - Change alloc state of given block(s)
1354 * @sdp: the filesystem
1355 * @bstart: the start of a run of blocks to free
1356 * @blen: the length of the block run (all must lie within ONE RG!)
1357 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1359 * Returns: Resource group containing the block(s)
1362 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1363 u32 blen, unsigned char new_state)
1365 struct gfs2_rgrpd *rgd;
1366 struct gfs2_bitmap *bi = NULL;
1367 u32 length, rgrp_blk, buf_blk;
1368 unsigned int buf;
1370 rgd = gfs2_blk2rgrpd(sdp, bstart);
1371 if (!rgd) {
1372 if (gfs2_consist(sdp))
1373 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1374 return NULL;
1377 length = rgd->rd_length;
1379 rgrp_blk = bstart - rgd->rd_data0;
1381 while (blen--) {
1382 for (buf = 0; buf < length; buf++) {
1383 bi = rgd->rd_bits + buf;
1384 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1385 break;
1388 gfs2_assert(rgd->rd_sbd, buf < length);
1390 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1391 rgrp_blk++;
1393 if (!bi->bi_clone) {
1394 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1395 GFP_NOFS | __GFP_NOFAIL);
1396 memcpy(bi->bi_clone + bi->bi_offset,
1397 bi->bi_bh->b_data + bi->bi_offset,
1398 bi->bi_len);
1400 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1401 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1402 bi->bi_len, buf_blk, new_state);
1405 return rgd;
1409 * gfs2_alloc_data - Allocate a data block
1410 * @ip: the inode to allocate the data block for
1412 * Returns: the allocated block
1415 u64 gfs2_alloc_data(struct gfs2_inode *ip)
1417 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1418 struct gfs2_alloc *al = &ip->i_alloc;
1419 struct gfs2_rgrpd *rgd = al->al_rgd;
1420 u32 goal, blk;
1421 u64 block;
1423 if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
1424 goal = ip->i_di.di_goal_data - rgd->rd_data0;
1425 else
1426 goal = rgd->rd_last_alloc_data;
1428 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1429 BUG_ON(blk == BFITNOENT);
1430 rgd->rd_last_alloc_data = blk;
1432 block = rgd->rd_data0 + blk;
1433 ip->i_di.di_goal_data = block;
1435 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1436 rgd->rd_rg.rg_free--;
1438 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1439 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1441 al->al_alloced++;
1443 gfs2_statfs_change(sdp, 0, -1, 0);
1444 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1446 spin_lock(&sdp->sd_rindex_spin);
1447 rgd->rd_free_clone--;
1448 spin_unlock(&sdp->sd_rindex_spin);
1450 return block;
1454 * gfs2_alloc_meta - Allocate a metadata block
1455 * @ip: the inode to allocate the metadata block for
1457 * Returns: the allocated block
1460 u64 gfs2_alloc_meta(struct gfs2_inode *ip)
1462 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1463 struct gfs2_alloc *al = &ip->i_alloc;
1464 struct gfs2_rgrpd *rgd = al->al_rgd;
1465 u32 goal, blk;
1466 u64 block;
1468 if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
1469 goal = ip->i_di.di_goal_meta - rgd->rd_data0;
1470 else
1471 goal = rgd->rd_last_alloc_meta;
1473 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
1474 BUG_ON(blk == BFITNOENT);
1475 rgd->rd_last_alloc_meta = blk;
1477 block = rgd->rd_data0 + blk;
1478 ip->i_di.di_goal_meta = block;
1480 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1481 rgd->rd_rg.rg_free--;
1483 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1484 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1486 al->al_alloced++;
1488 gfs2_statfs_change(sdp, 0, -1, 0);
1489 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1490 gfs2_trans_add_unrevoke(sdp, block);
1492 spin_lock(&sdp->sd_rindex_spin);
1493 rgd->rd_free_clone--;
1494 spin_unlock(&sdp->sd_rindex_spin);
1496 return block;
1500 * gfs2_alloc_di - Allocate a dinode
1501 * @dip: the directory that the inode is going in
1503 * Returns: the block allocated
1506 u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
1508 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1509 struct gfs2_alloc *al = &dip->i_alloc;
1510 struct gfs2_rgrpd *rgd = al->al_rgd;
1511 u32 blk;
1512 u64 block;
1514 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1515 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1516 BUG_ON(blk == BFITNOENT);
1518 rgd->rd_last_alloc_meta = blk;
1520 block = rgd->rd_data0 + blk;
1522 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1523 rgd->rd_rg.rg_free--;
1524 rgd->rd_rg.rg_dinodes++;
1525 *generation = rgd->rd_rg.rg_igeneration++;
1526 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1527 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1529 al->al_alloced++;
1531 gfs2_statfs_change(sdp, 0, -1, +1);
1532 gfs2_trans_add_unrevoke(sdp, block);
1534 spin_lock(&sdp->sd_rindex_spin);
1535 rgd->rd_free_clone--;
1536 spin_unlock(&sdp->sd_rindex_spin);
1538 return block;
1542 * gfs2_free_data - free a contiguous run of data block(s)
1543 * @ip: the inode these blocks are being freed from
1544 * @bstart: first block of a run of contiguous blocks
1545 * @blen: the length of the block run
1549 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1551 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1552 struct gfs2_rgrpd *rgd;
1554 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1555 if (!rgd)
1556 return;
1558 rgd->rd_rg.rg_free += blen;
1560 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1561 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1563 gfs2_trans_add_rg(rgd);
1565 gfs2_statfs_change(sdp, 0, +blen, 0);
1566 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1570 * gfs2_free_meta - free a contiguous run of data block(s)
1571 * @ip: the inode these blocks are being freed from
1572 * @bstart: first block of a run of contiguous blocks
1573 * @blen: the length of the block run
1577 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1579 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1580 struct gfs2_rgrpd *rgd;
1582 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1583 if (!rgd)
1584 return;
1586 rgd->rd_rg.rg_free += blen;
1588 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1589 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1591 gfs2_trans_add_rg(rgd);
1593 gfs2_statfs_change(sdp, 0, +blen, 0);
1594 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1595 gfs2_meta_wipe(ip, bstart, blen);
1598 void gfs2_unlink_di(struct inode *inode)
1600 struct gfs2_inode *ip = GFS2_I(inode);
1601 struct gfs2_sbd *sdp = GFS2_SB(inode);
1602 struct gfs2_rgrpd *rgd;
1603 u64 blkno = ip->i_no_addr;
1605 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1606 if (!rgd)
1607 return;
1608 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1609 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1610 gfs2_trans_add_rg(rgd);
1613 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1615 struct gfs2_sbd *sdp = rgd->rd_sbd;
1616 struct gfs2_rgrpd *tmp_rgd;
1618 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1619 if (!tmp_rgd)
1620 return;
1621 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1623 if (!rgd->rd_rg.rg_dinodes)
1624 gfs2_consist_rgrpd(rgd);
1625 rgd->rd_rg.rg_dinodes--;
1626 rgd->rd_rg.rg_free++;
1628 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1629 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1631 gfs2_statfs_change(sdp, 0, +1, -1);
1632 gfs2_trans_add_rg(rgd);
1636 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1638 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1639 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1640 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1644 * gfs2_rlist_add - add a RG to a list of RGs
1645 * @sdp: the filesystem
1646 * @rlist: the list of resource groups
1647 * @block: the block
1649 * Figure out what RG a block belongs to and add that RG to the list
1651 * FIXME: Don't use NOFAIL
1655 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1656 u64 block)
1658 struct gfs2_rgrpd *rgd;
1659 struct gfs2_rgrpd **tmp;
1660 unsigned int new_space;
1661 unsigned int x;
1663 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1664 return;
1666 rgd = gfs2_blk2rgrpd(sdp, block);
1667 if (!rgd) {
1668 if (gfs2_consist(sdp))
1669 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1670 return;
1673 for (x = 0; x < rlist->rl_rgrps; x++)
1674 if (rlist->rl_rgd[x] == rgd)
1675 return;
1677 if (rlist->rl_rgrps == rlist->rl_space) {
1678 new_space = rlist->rl_space + 10;
1680 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1681 GFP_NOFS | __GFP_NOFAIL);
1683 if (rlist->rl_rgd) {
1684 memcpy(tmp, rlist->rl_rgd,
1685 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1686 kfree(rlist->rl_rgd);
1689 rlist->rl_space = new_space;
1690 rlist->rl_rgd = tmp;
1693 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1697 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1698 * and initialize an array of glock holders for them
1699 * @rlist: the list of resource groups
1700 * @state: the lock state to acquire the RG lock in
1701 * @flags: the modifier flags for the holder structures
1703 * FIXME: Don't use NOFAIL
1707 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1708 int flags)
1710 unsigned int x;
1712 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1713 GFP_NOFS | __GFP_NOFAIL);
1714 for (x = 0; x < rlist->rl_rgrps; x++)
1715 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1716 state, flags,
1717 &rlist->rl_ghs[x]);
1721 * gfs2_rlist_free - free a resource group list
1722 * @list: the list of resource groups
1726 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1728 unsigned int x;
1730 kfree(rlist->rl_rgd);
1732 if (rlist->rl_ghs) {
1733 for (x = 0; x < rlist->rl_rgrps; x++)
1734 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1735 kfree(rlist->rl_ghs);