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>
32 #include "trace_gfs2.h"
34 #define BFITNOENT ((u32)~0)
35 #define NO_BLOCK ((u64)~0)
37 #if BITS_PER_LONG == 32
38 #define LBITMASK (0x55555555UL)
39 #define LBITSKIP55 (0x55555555UL)
40 #define LBITSKIP00 (0x00000000UL)
42 #define LBITMASK (0x5555555555555555UL)
43 #define LBITSKIP55 (0x5555555555555555UL)
44 #define LBITSKIP00 (0x0000000000000000UL)
48 * These routines are used by the resource group routines (rgrp.c)
49 * to keep track of block allocation. Each block is represented by two
50 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
53 * 1 = Used (not metadata)
54 * 2 = Unlinked (still in use) inode
58 static const char valid_change[16] = {
66 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
67 unsigned char old_state, unsigned char new_state,
71 * gfs2_setbit - Set a bit in the bitmaps
72 * @buffer: the buffer that holds the bitmaps
73 * @buflen: the length (in bytes) of the buffer
74 * @block: the block to set
75 * @new_state: the new state of the block
79 static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
80 unsigned char *buf2, unsigned int offset,
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 = buf1 + offset + (block / GFS2_NBBY);
89 end = buf1 + 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 + 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 * @buffer: the buffer that holds the bitmaps
121 * @buflen: the length (in bytes) of the buffer
122 * @block: the block to read
126 static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
127 const unsigned char *buffer,
128 unsigned int buflen, u32 block)
130 const unsigned char *byte, *end;
131 unsigned char cur_state;
134 byte = buffer + (block / GFS2_NBBY);
135 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
136 end = buffer + buflen;
138 gfs2_assert(rgd->rd_sbd, byte < end);
140 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
147 * @ptr: Pointer to bitmap data
148 * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
149 * @state: The state we are searching for
151 * We xor the bitmap data with a patter which is the bitwise opposite
152 * of what we are looking for, this gives rise to a pattern of ones
153 * wherever there is a match. Since we have two bits per entry, we
154 * take this pattern, shift it down by one place and then and it with
155 * the original. All the even bit positions (0,2,4, etc) then represent
156 * successful matches, so we mask with 0x55555..... to remove the unwanted
159 * This allows searching of a whole u64 at once (32 blocks) with a
160 * single test (on 64 bit arches).
163 static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
166 static const u64 search[] = {
167 [0] = 0xffffffffffffffffULL,
168 [1] = 0xaaaaaaaaaaaaaaaaULL,
169 [2] = 0x5555555555555555ULL,
170 [3] = 0x0000000000000000ULL,
172 tmp = le64_to_cpu(*ptr) ^ search[state];
179 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
180 * a block in a given allocation state.
181 * @buffer: the buffer that holds the bitmaps
182 * @len: the length (in bytes) of the buffer
183 * @goal: start search at this block's bit-pair (within @buffer)
184 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
186 * Scope of @goal and returned block number is only within this bitmap buffer,
187 * not entire rgrp or filesystem. @buffer will be offset from the actual
188 * beginning of a bitmap block buffer, skipping any header structures, but
189 * headers are always a multiple of 64 bits long so that the buffer is
190 * always aligned to a 64 bit boundary.
192 * The size of the buffer is in bytes, but is it assumed that it is
193 * always ok to read a complete multiple of 64 bits at the end
194 * of the block in case the end is no aligned to a natural boundary.
196 * Return: the block number (bitmap buffer scope) that was found
199 static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
202 u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
203 const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
204 const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
206 u64 mask = 0x5555555555555555ULL;
211 /* Mask off bits we don't care about at the start of the search */
213 tmp = gfs2_bit_search(ptr, mask, state);
215 while(tmp == 0 && ptr < end) {
216 tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
219 /* Mask off any bits which are more than len bytes from the start */
220 if (ptr == end && (len & (sizeof(u64) - 1)))
221 tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
222 /* Didn't find anything, so return */
227 bit /= 2; /* two bits per entry in the bitmap */
228 return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
232 * gfs2_bitcount - count the number of bits in a certain state
233 * @buffer: the buffer that holds the bitmaps
234 * @buflen: the length (in bytes) of the buffer
235 * @state: the state of the block we're looking for
237 * Returns: The number of bits
240 static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
241 unsigned int buflen, u8 state)
243 const u8 *byte = buffer;
244 const u8 *end = buffer + buflen;
245 const u8 state1 = state << 2;
246 const u8 state2 = state << 4;
247 const u8 state3 = state << 6;
250 for (; byte < end; byte++) {
251 if (((*byte) & 0x03) == state)
253 if (((*byte) & 0x0C) == state1)
255 if (((*byte) & 0x30) == state2)
257 if (((*byte) & 0xC0) == state3)
265 * gfs2_rgrp_verify - Verify that a resource group is consistent
266 * @sdp: the filesystem
271 void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
273 struct gfs2_sbd *sdp = rgd->rd_sbd;
274 struct gfs2_bitmap *bi = NULL;
275 u32 length = rgd->rd_length;
279 memset(count, 0, 4 * sizeof(u32));
281 /* Count # blocks in each of 4 possible allocation states */
282 for (buf = 0; buf < length; buf++) {
283 bi = rgd->rd_bits + buf;
284 for (x = 0; x < 4; x++)
285 count[x] += gfs2_bitcount(rgd,
291 if (count[0] != rgd->rd_free) {
292 if (gfs2_consist_rgrpd(rgd))
293 fs_err(sdp, "free data mismatch: %u != %u\n",
294 count[0], rgd->rd_free);
298 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
299 if (count[1] != tmp) {
300 if (gfs2_consist_rgrpd(rgd))
301 fs_err(sdp, "used data mismatch: %u != %u\n",
306 if (count[2] + count[3] != rgd->rd_dinodes) {
307 if (gfs2_consist_rgrpd(rgd))
308 fs_err(sdp, "used metadata mismatch: %u != %u\n",
309 count[2] + count[3], rgd->rd_dinodes);
314 static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
316 u64 first = rgd->rd_data0;
317 u64 last = first + rgd->rd_data;
318 return first <= block && block < last;
322 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
323 * @sdp: The GFS2 superblock
324 * @n: The data block number
326 * Returns: The resource group, or NULL if not found
329 struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
331 struct gfs2_rgrpd *rgd;
333 spin_lock(&sdp->sd_rindex_spin);
335 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
336 if (rgrp_contains_block(rgd, blk)) {
337 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
338 spin_unlock(&sdp->sd_rindex_spin);
343 spin_unlock(&sdp->sd_rindex_spin);
349 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
350 * @sdp: The GFS2 superblock
352 * Returns: The first rgrp in the filesystem
355 struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
357 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
358 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
362 * gfs2_rgrpd_get_next - get the next RG
365 * Returns: The next rgrp
368 struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
370 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
372 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
375 static void clear_rgrpdi(struct gfs2_sbd *sdp)
377 struct list_head *head;
378 struct gfs2_rgrpd *rgd;
379 struct gfs2_glock *gl;
381 spin_lock(&sdp->sd_rindex_spin);
382 sdp->sd_rindex_forward = NULL;
383 spin_unlock(&sdp->sd_rindex_spin);
385 head = &sdp->sd_rindex_list;
386 while (!list_empty(head)) {
387 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
390 list_del(&rgd->rd_list);
391 list_del(&rgd->rd_list_mru);
394 gl->gl_object = NULL;
399 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
403 void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
405 mutex_lock(&sdp->sd_rindex_mutex);
407 mutex_unlock(&sdp->sd_rindex_mutex);
410 static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
412 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
413 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
414 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
415 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
416 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
420 * gfs2_compute_bitstructs - Compute the bitmap sizes
421 * @rgd: The resource group descriptor
423 * Calculates bitmap descriptors, one for each block that contains bitmap data
428 static int compute_bitstructs(struct gfs2_rgrpd *rgd)
430 struct gfs2_sbd *sdp = rgd->rd_sbd;
431 struct gfs2_bitmap *bi;
432 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
433 u32 bytes_left, bytes;
439 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
443 bytes_left = rgd->rd_bitbytes;
445 for (x = 0; x < length; x++) {
446 bi = rgd->rd_bits + x;
449 /* small rgrp; bitmap stored completely in header block */
452 bi->bi_offset = sizeof(struct gfs2_rgrp);
457 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
458 bi->bi_offset = sizeof(struct gfs2_rgrp);
462 } else if (x + 1 == length) {
464 bi->bi_offset = sizeof(struct gfs2_meta_header);
465 bi->bi_start = rgd->rd_bitbytes - bytes_left;
469 bytes = sdp->sd_sb.sb_bsize -
470 sizeof(struct gfs2_meta_header);
471 bi->bi_offset = sizeof(struct gfs2_meta_header);
472 bi->bi_start = rgd->rd_bitbytes - bytes_left;
480 gfs2_consist_rgrpd(rgd);
483 bi = rgd->rd_bits + (length - 1);
484 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
485 if (gfs2_consist_rgrpd(rgd)) {
486 gfs2_rindex_print(rgd);
487 fs_err(sdp, "start=%u len=%u offset=%u\n",
488 bi->bi_start, bi->bi_len, bi->bi_offset);
497 * gfs2_ri_total - Total up the file system space, according to the rindex.
500 u64 gfs2_ri_total(struct gfs2_sbd *sdp)
503 struct inode *inode = sdp->sd_rindex;
504 struct gfs2_inode *ip = GFS2_I(inode);
505 char buf[sizeof(struct gfs2_rindex)];
506 struct file_ra_state ra_state;
509 mutex_lock(&sdp->sd_rindex_mutex);
510 file_ra_state_init(&ra_state, inode->i_mapping);
511 for (rgrps = 0;; rgrps++) {
512 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
514 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
516 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
517 sizeof(struct gfs2_rindex));
518 if (error != sizeof(struct gfs2_rindex))
520 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
522 mutex_unlock(&sdp->sd_rindex_mutex);
526 static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
528 const struct gfs2_rindex *str = buf;
530 rgd->rd_addr = be64_to_cpu(str->ri_addr);
531 rgd->rd_length = be32_to_cpu(str->ri_length);
532 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
533 rgd->rd_data = be32_to_cpu(str->ri_data);
534 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
538 * read_rindex_entry - Pull in a new resource index entry from the disk
539 * @gl: The glock covering the rindex inode
541 * Returns: 0 on success, error code otherwise
544 static int read_rindex_entry(struct gfs2_inode *ip,
545 struct file_ra_state *ra_state)
547 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
548 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
549 char buf[sizeof(struct gfs2_rindex)];
551 struct gfs2_rgrpd *rgd;
553 error = gfs2_internal_read(ip, ra_state, buf, &pos,
554 sizeof(struct gfs2_rindex));
557 if (error != sizeof(struct gfs2_rindex)) {
563 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
568 mutex_init(&rgd->rd_mutex);
569 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
572 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
573 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
575 gfs2_rindex_in(rgd, buf);
576 error = compute_bitstructs(rgd);
580 error = gfs2_glock_get(sdp, rgd->rd_addr,
581 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
585 rgd->rd_gl->gl_object = rgd;
586 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
591 * gfs2_ri_update - Pull in a new resource index from the disk
592 * @ip: pointer to the rindex inode
594 * Returns: 0 on successful update, error code otherwise
597 int gfs2_ri_update(struct gfs2_inode *ip)
599 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
600 struct inode *inode = &ip->i_inode;
601 struct file_ra_state ra_state;
602 u64 rgrp_count = i_size_read(inode);
603 struct gfs2_rgrpd *rgd;
604 unsigned int max_data = 0;
607 do_div(rgrp_count, sizeof(struct gfs2_rindex));
610 file_ra_state_init(&ra_state, inode->i_mapping);
611 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
612 error = read_rindex_entry(ip, &ra_state);
619 list_for_each_entry(rgd, &sdp->sd_rindex_list, rd_list)
620 if (rgd->rd_data > max_data)
621 max_data = rgd->rd_data;
622 sdp->sd_max_rg_data = max_data;
623 sdp->sd_rindex_uptodate = 1;
628 * gfs2_rindex_hold - Grab a lock on the rindex
629 * @sdp: The GFS2 superblock
630 * @ri_gh: the glock holder
632 * We grab a lock on the rindex inode to make sure that it doesn't
633 * change whilst we are performing an operation. We keep this lock
634 * for quite long periods of time compared to other locks. This
635 * doesn't matter, since it is shared and it is very, very rarely
636 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
638 * This makes sure that we're using the latest copy of the resource index
639 * special file, which might have been updated if someone expanded the
640 * filesystem (via gfs2_grow utility), which adds new resource groups.
642 * Returns: 0 on success, error code otherwise
645 int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
647 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
648 struct gfs2_glock *gl = ip->i_gl;
651 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
655 /* Read new copy from disk if we don't have the latest */
656 if (!sdp->sd_rindex_uptodate) {
657 mutex_lock(&sdp->sd_rindex_mutex);
658 if (!sdp->sd_rindex_uptodate) {
659 error = gfs2_ri_update(ip);
661 gfs2_glock_dq_uninit(ri_gh);
663 mutex_unlock(&sdp->sd_rindex_mutex);
669 static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
671 const struct gfs2_rgrp *str = buf;
674 rg_flags = be32_to_cpu(str->rg_flags);
675 rg_flags &= ~GFS2_RDF_MASK;
676 rgd->rd_flags &= GFS2_RDF_MASK;
677 rgd->rd_flags |= rg_flags;
678 rgd->rd_free = be32_to_cpu(str->rg_free);
679 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
680 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
683 static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
685 struct gfs2_rgrp *str = buf;
687 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
688 str->rg_free = cpu_to_be32(rgd->rd_free);
689 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
690 str->__pad = cpu_to_be32(0);
691 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
692 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
696 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
697 * @rgd: the struct gfs2_rgrpd describing the RG to read in
699 * Read in all of a Resource Group's header and bitmap blocks.
700 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
705 int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
707 struct gfs2_sbd *sdp = rgd->rd_sbd;
708 struct gfs2_glock *gl = rgd->rd_gl;
709 unsigned int length = rgd->rd_length;
710 struct gfs2_bitmap *bi;
714 mutex_lock(&rgd->rd_mutex);
716 spin_lock(&sdp->sd_rindex_spin);
717 if (rgd->rd_bh_count) {
719 spin_unlock(&sdp->sd_rindex_spin);
720 mutex_unlock(&rgd->rd_mutex);
723 spin_unlock(&sdp->sd_rindex_spin);
725 for (x = 0; x < length; x++) {
726 bi = rgd->rd_bits + x;
727 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
732 for (y = length; y--;) {
733 bi = rgd->rd_bits + y;
734 error = gfs2_meta_wait(sdp, bi->bi_bh);
737 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
744 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
745 for (x = 0; x < length; x++)
746 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
747 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
748 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
751 spin_lock(&sdp->sd_rindex_spin);
752 rgd->rd_free_clone = rgd->rd_free;
754 spin_unlock(&sdp->sd_rindex_spin);
756 mutex_unlock(&rgd->rd_mutex);
762 bi = rgd->rd_bits + x;
765 gfs2_assert_warn(sdp, !bi->bi_clone);
767 mutex_unlock(&rgd->rd_mutex);
772 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
774 struct gfs2_sbd *sdp = rgd->rd_sbd;
776 spin_lock(&sdp->sd_rindex_spin);
777 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
779 spin_unlock(&sdp->sd_rindex_spin);
783 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
784 * @rgd: the struct gfs2_rgrpd describing the RG to read in
788 void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
790 struct gfs2_sbd *sdp = rgd->rd_sbd;
791 int x, length = rgd->rd_length;
793 spin_lock(&sdp->sd_rindex_spin);
794 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
795 if (--rgd->rd_bh_count) {
796 spin_unlock(&sdp->sd_rindex_spin);
800 for (x = 0; x < length; x++) {
801 struct gfs2_bitmap *bi = rgd->rd_bits + x;
808 spin_unlock(&sdp->sd_rindex_spin);
811 static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
812 const struct gfs2_bitmap *bi)
814 struct super_block *sb = sdp->sd_vfs;
815 struct block_device *bdev = sb->s_bdev;
816 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
817 bdev_logical_block_size(sb->s_bdev);
820 sector_t nr_sects = 0;
824 for (x = 0; x < bi->bi_len; x++) {
825 const u8 *orig = bi->bi_bh->b_data + bi->bi_offset + x;
826 const u8 *clone = bi->bi_clone + bi->bi_offset + x;
827 u8 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
831 blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
832 blk *= sects_per_blk; /* convert to sectors */
836 goto start_new_extent;
837 if ((start + nr_sects) != blk) {
838 rv = blkdev_issue_discard(bdev, start,
847 nr_sects += sects_per_blk;
850 blk += sects_per_blk;
854 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
860 fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
861 sdp->sd_args.ar_discard = 0;
864 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
866 struct gfs2_sbd *sdp = rgd->rd_sbd;
867 unsigned int length = rgd->rd_length;
870 for (x = 0; x < length; x++) {
871 struct gfs2_bitmap *bi = rgd->rd_bits + x;
874 if (sdp->sd_args.ar_discard)
875 gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bi);
876 clear_bit(GBF_FULL, &bi->bi_flags);
877 memcpy(bi->bi_clone + bi->bi_offset,
878 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
881 spin_lock(&sdp->sd_rindex_spin);
882 rgd->rd_free_clone = rgd->rd_free;
883 spin_unlock(&sdp->sd_rindex_spin);
887 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
888 * @ip: the incore GFS2 inode structure
890 * Returns: the struct gfs2_alloc
893 struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
895 BUG_ON(ip->i_alloc != NULL);
896 ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_NOFS);
901 * try_rgrp_fit - See if a given reservation will fit in a given RG
903 * @al: the struct gfs2_alloc structure describing the reservation
905 * If there's room for the requested blocks to be allocated from the RG:
906 * Sets the $al_rgd field in @al.
908 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
911 static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
913 struct gfs2_sbd *sdp = rgd->rd_sbd;
916 if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
919 spin_lock(&sdp->sd_rindex_spin);
920 if (rgd->rd_free_clone >= al->al_requested) {
924 spin_unlock(&sdp->sd_rindex_spin);
930 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
933 * Returns: 0 if no error
934 * The inode, if one has been found, in inode.
937 static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
941 struct gfs2_sbd *sdp = rgd->rd_sbd;
943 struct gfs2_glock *gl;
944 struct gfs2_inode *ip;
948 while (goal < rgd->rd_data) {
949 down_write(&sdp->sd_log_flush_lock);
951 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
952 GFS2_BLKST_UNLINKED, &n);
953 up_write(&sdp->sd_log_flush_lock);
954 if (block == BFITNOENT)
956 /* rgblk_search can return a block < goal, so we need to
957 keep it marching forward. */
958 no_addr = block + rgd->rd_data0;
959 goal = max(block + 1, goal + 1);
960 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
964 *last_unlinked = no_addr;
966 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
970 /* If the inode is already in cache, we can ignore it here
971 * because the existing inode disposal code will deal with
972 * it when all refs have gone away. Accessing gl_object like
973 * this is not safe in general. Here it is ok because we do
974 * not dereference the pointer, and we only need an approx
975 * answer to whether it is NULL or not.
979 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
984 /* Limit reclaim to sensible number of tasks */
989 rgd->rd_flags &= ~GFS2_RDF_CHECK;
994 * recent_rgrp_next - get next RG from "recent" list
995 * @cur_rgd: current rgrp
997 * Returns: The next rgrp in the recent list
1000 static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd)
1002 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
1003 struct list_head *head;
1004 struct gfs2_rgrpd *rgd;
1006 spin_lock(&sdp->sd_rindex_spin);
1007 head = &sdp->sd_rindex_mru_list;
1008 if (unlikely(cur_rgd->rd_list_mru.next == head)) {
1009 spin_unlock(&sdp->sd_rindex_spin);
1012 rgd = list_entry(cur_rgd->rd_list_mru.next, struct gfs2_rgrpd, rd_list_mru);
1013 spin_unlock(&sdp->sd_rindex_spin);
1018 * forward_rgrp_get - get an rgrp to try next from full list
1019 * @sdp: The GFS2 superblock
1021 * Returns: The rgrp to try next
1024 static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1026 struct gfs2_rgrpd *rgd;
1027 unsigned int journals = gfs2_jindex_size(sdp);
1028 unsigned int rg = 0, x;
1030 spin_lock(&sdp->sd_rindex_spin);
1032 rgd = sdp->sd_rindex_forward;
1034 if (sdp->sd_rgrps >= journals)
1035 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1037 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
1038 x++, rgd = gfs2_rgrpd_get_next(rgd))
1041 sdp->sd_rindex_forward = rgd;
1044 spin_unlock(&sdp->sd_rindex_spin);
1050 * forward_rgrp_set - set the forward rgrp pointer
1051 * @sdp: the filesystem
1052 * @rgd: The new forward rgrp
1056 static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1058 spin_lock(&sdp->sd_rindex_spin);
1059 sdp->sd_rindex_forward = rgd;
1060 spin_unlock(&sdp->sd_rindex_spin);
1064 * get_local_rgrp - Choose and lock a rgrp for allocation
1065 * @ip: the inode to reserve space for
1066 * @rgp: the chosen and locked rgrp
1068 * Try to acquire rgrp in way which avoids contending with others.
1073 static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
1075 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1076 struct gfs2_rgrpd *rgd, *begin = NULL;
1077 struct gfs2_alloc *al = ip->i_alloc;
1078 int flags = LM_FLAG_TRY;
1081 int error, rg_locked;
1083 rgd = gfs2_blk2rgrpd(sdp, ip->i_goal);
1088 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1092 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1093 LM_FLAG_TRY, &al->al_rgd_gh);
1097 if (try_rgrp_fit(rgd, al))
1099 if (rgd->rd_flags & GFS2_RDF_CHECK)
1100 try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
1102 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1105 rgd = recent_rgrp_next(rgd);
1113 /* Go through full list of rgrps */
1115 begin = rgd = forward_rgrp_get(sdp);
1120 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1124 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1129 if (try_rgrp_fit(rgd, al))
1131 if (rgd->rd_flags & GFS2_RDF_CHECK)
1132 try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
1134 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1145 rgd = gfs2_rgrpd_get_next(rgd);
1147 rgd = gfs2_rgrpd_get_first(sdp);
1156 gfs2_log_flush(sdp, NULL);
1162 spin_lock(&sdp->sd_rindex_spin);
1163 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
1164 spin_unlock(&sdp->sd_rindex_spin);
1165 rgd = gfs2_rgrpd_get_next(rgd);
1167 rgd = gfs2_rgrpd_get_first(sdp);
1168 forward_rgrp_set(sdp, rgd);
1175 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1176 * @ip: the inode to reserve space for
1181 int gfs2_inplace_reserve_i(struct gfs2_inode *ip, int hold_rindex,
1182 char *file, unsigned int line)
1184 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1185 struct gfs2_alloc *al = ip->i_alloc;
1187 u64 last_unlinked = NO_BLOCK;
1190 if (gfs2_assert_warn(sdp, al->al_requested))
1194 /* We need to hold the rindex unless the inode we're using is
1195 the rindex itself, in which case it's already held. */
1196 if (ip != GFS2_I(sdp->sd_rindex))
1197 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1198 else if (!sdp->sd_rgrps) /* We may not have the rindex read
1200 error = gfs2_ri_update(ip);
1207 error = get_local_rgrp(ip, &last_unlinked);
1208 /* If there is no space, flushing the log may release some */
1210 if (ip == GFS2_I(sdp->sd_rindex) &&
1211 !sdp->sd_rindex_uptodate) {
1212 error = gfs2_ri_update(ip);
1217 gfs2_log_flush(sdp, NULL);
1219 } while (error && tries++ < 3);
1222 if (hold_rindex && ip != GFS2_I(sdp->sd_rindex))
1223 gfs2_glock_dq_uninit(&al->al_ri_gh);
1227 /* no error, so we have the rgrp set in the inode's allocation. */
1235 * gfs2_inplace_release - release an inplace reservation
1236 * @ip: the inode the reservation was taken out on
1238 * Release a reservation made by gfs2_inplace_reserve().
1241 void gfs2_inplace_release(struct gfs2_inode *ip)
1243 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1244 struct gfs2_alloc *al = ip->i_alloc;
1246 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1247 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1248 "al_file = %s, al_line = %u\n",
1249 al->al_alloced, al->al_requested, al->al_file,
1253 if (al->al_rgd_gh.gh_gl)
1254 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1255 if (ip != GFS2_I(sdp->sd_rindex) && al->al_ri_gh.gh_gl)
1256 gfs2_glock_dq_uninit(&al->al_ri_gh);
1260 * gfs2_get_block_type - Check a block in a RG is of given type
1261 * @rgd: the resource group holding the block
1262 * @block: the block number
1264 * Returns: The block type (GFS2_BLKST_*)
1267 static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1269 struct gfs2_bitmap *bi = NULL;
1270 u32 length, rgrp_block, buf_block;
1274 length = rgd->rd_length;
1275 rgrp_block = block - rgd->rd_data0;
1277 for (buf = 0; buf < length; buf++) {
1278 bi = rgd->rd_bits + buf;
1279 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1283 gfs2_assert(rgd->rd_sbd, buf < length);
1284 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1286 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1287 bi->bi_len, buf_block);
1293 * rgblk_search - find a block in @old_state, change allocation
1294 * state to @new_state
1295 * @rgd: the resource group descriptor
1296 * @goal: the goal block within the RG (start here to search for avail block)
1297 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1298 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1299 * @n: The extent length
1301 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1302 * Add the found bitmap buffer to the transaction.
1303 * Set the found bits to @new_state to change block's allocation state.
1305 * This function never fails, because we wouldn't call it unless we
1306 * know (from reservation results, etc.) that a block is available.
1308 * Scope of @goal and returned block is just within rgrp, not the whole
1311 * Returns: the block number allocated
1314 static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
1315 unsigned char old_state, unsigned char new_state,
1318 struct gfs2_bitmap *bi = NULL;
1319 const u32 length = rgd->rd_length;
1320 u32 blk = BFITNOENT;
1321 unsigned int buf, x;
1322 const unsigned int elen = *n;
1323 const u8 *buffer = NULL;
1326 /* Find bitmap block that contains bits for goal block */
1327 for (buf = 0; buf < length; buf++) {
1328 bi = rgd->rd_bits + buf;
1329 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1330 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1331 goal -= bi->bi_start * GFS2_NBBY;
1339 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1340 "x <= length", instead of "x < length", because we typically start
1341 the search in the middle of a bit block, but if we can't find an
1342 allocatable block anywhere else, we want to be able wrap around and
1343 search in the first part of our first-searched bit block. */
1344 for (x = 0; x <= length; x++) {
1345 bi = rgd->rd_bits + buf;
1347 if (test_bit(GBF_FULL, &bi->bi_flags) &&
1348 (old_state == GFS2_BLKST_FREE))
1351 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1352 bitmaps, so we must search the originals for that. */
1353 buffer = bi->bi_bh->b_data + bi->bi_offset;
1354 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
1355 buffer = bi->bi_clone + bi->bi_offset;
1357 blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
1358 if (blk != BFITNOENT)
1361 if ((goal == 0) && (old_state == GFS2_BLKST_FREE))
1362 set_bit(GBF_FULL, &bi->bi_flags);
1364 /* Try next bitmap block (wrap back to rgrp header if at end) */
1371 if (blk == BFITNOENT)
1374 if (old_state == new_state)
1377 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1378 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1379 bi, blk, new_state);
1383 if (goal >= (bi->bi_len * GFS2_NBBY))
1385 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1388 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
1389 bi, goal, new_state);
1393 return (bi->bi_start * GFS2_NBBY) + blk;
1397 * rgblk_free - Change alloc state of given block(s)
1398 * @sdp: the filesystem
1399 * @bstart: the start of a run of blocks to free
1400 * @blen: the length of the block run (all must lie within ONE RG!)
1401 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1403 * Returns: Resource group containing the block(s)
1406 static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1407 u32 blen, unsigned char new_state)
1409 struct gfs2_rgrpd *rgd;
1410 struct gfs2_bitmap *bi = NULL;
1411 u32 length, rgrp_blk, buf_blk;
1414 rgd = gfs2_blk2rgrpd(sdp, bstart);
1416 if (gfs2_consist(sdp))
1417 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1421 length = rgd->rd_length;
1423 rgrp_blk = bstart - rgd->rd_data0;
1426 for (buf = 0; buf < length; buf++) {
1427 bi = rgd->rd_bits + buf;
1428 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1432 gfs2_assert(rgd->rd_sbd, buf < length);
1434 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1437 if (!bi->bi_clone) {
1438 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1439 GFP_NOFS | __GFP_NOFAIL);
1440 memcpy(bi->bi_clone + bi->bi_offset,
1441 bi->bi_bh->b_data + bi->bi_offset,
1444 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1445 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
1446 bi, buf_blk, new_state);
1453 * gfs2_rgrp_dump - print out an rgrp
1454 * @seq: The iterator
1455 * @gl: The glock in question
1459 int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
1461 const struct gfs2_rgrpd *rgd = gl->gl_object;
1464 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
1465 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
1466 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
1470 static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
1472 struct gfs2_sbd *sdp = rgd->rd_sbd;
1473 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
1474 (unsigned long long)rgd->rd_addr);
1475 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
1476 gfs2_rgrp_dump(NULL, rgd->rd_gl);
1477 rgd->rd_flags |= GFS2_RDF_ERROR;
1481 * gfs2_alloc_block - Allocate one or more blocks
1482 * @ip: the inode to allocate the block for
1483 * @bn: Used to return the starting block number
1484 * @n: requested number of blocks/extent length (value/result)
1486 * Returns: 0 or error
1489 int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
1491 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1492 struct buffer_head *dibh;
1493 struct gfs2_alloc *al = ip->i_alloc;
1494 struct gfs2_rgrpd *rgd;
1499 /* Only happens if there is a bug in gfs2, return something distinctive
1500 * to ensure that it is noticed.
1507 if (rgrp_contains_block(rgd, ip->i_goal))
1508 goal = ip->i_goal - rgd->rd_data0;
1510 goal = rgd->rd_last_alloc;
1512 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
1514 /* Since all blocks are reserved in advance, this shouldn't happen */
1515 if (blk == BFITNOENT)
1518 rgd->rd_last_alloc = blk;
1519 block = rgd->rd_data0 + blk;
1521 error = gfs2_meta_inode_buffer(ip, &dibh);
1523 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
1524 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1525 di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal);
1528 if (rgd->rd_free < *n)
1533 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1534 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1536 al->al_alloced += *n;
1538 gfs2_statfs_change(sdp, 0, -(s64)*n, 0);
1539 gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
1541 spin_lock(&sdp->sd_rindex_spin);
1542 rgd->rd_free_clone -= *n;
1543 spin_unlock(&sdp->sd_rindex_spin);
1544 trace_gfs2_block_alloc(ip, block, *n, GFS2_BLKST_USED);
1549 gfs2_rgrp_error(rgd);
1554 * gfs2_alloc_di - Allocate a dinode
1555 * @dip: the directory that the inode is going in
1556 * @bn: the block number which is allocated
1557 * @generation: the generation number of the inode
1559 * Returns: 0 on success or error
1562 int gfs2_alloc_di(struct gfs2_inode *dip, u64 *bn, u64 *generation)
1564 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1565 struct gfs2_alloc *al = dip->i_alloc;
1566 struct gfs2_rgrpd *rgd = al->al_rgd;
1571 blk = rgblk_search(rgd, rgd->rd_last_alloc,
1572 GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
1574 /* Since all blocks are reserved in advance, this shouldn't happen */
1575 if (blk == BFITNOENT)
1578 rgd->rd_last_alloc = blk;
1579 block = rgd->rd_data0 + blk;
1580 if (rgd->rd_free == 0)
1585 *generation = rgd->rd_igeneration++;
1586 if (*generation == 0)
1587 *generation = rgd->rd_igeneration++;
1588 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1589 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1593 gfs2_statfs_change(sdp, 0, -1, +1);
1594 gfs2_trans_add_unrevoke(sdp, block, 1);
1596 spin_lock(&sdp->sd_rindex_spin);
1597 rgd->rd_free_clone--;
1598 spin_unlock(&sdp->sd_rindex_spin);
1599 trace_gfs2_block_alloc(dip, block, 1, GFS2_BLKST_DINODE);
1604 gfs2_rgrp_error(rgd);
1609 * gfs2_free_data - free a contiguous run of data block(s)
1610 * @ip: the inode these blocks are being freed from
1611 * @bstart: first block of a run of contiguous blocks
1612 * @blen: the length of the block run
1616 void __gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1618 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1619 struct gfs2_rgrpd *rgd;
1621 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1624 trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
1625 rgd->rd_free += blen;
1627 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1628 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1630 gfs2_trans_add_rg(rgd);
1634 * gfs2_free_data - free a contiguous run of data block(s)
1635 * @ip: the inode these blocks are being freed from
1636 * @bstart: first block of a run of contiguous blocks
1637 * @blen: the length of the block run
1641 void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
1643 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1645 __gfs2_free_data(ip, bstart, blen);
1646 gfs2_statfs_change(sdp, 0, +blen, 0);
1647 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1651 * gfs2_free_meta - free a contiguous run of data block(s)
1652 * @ip: the inode these blocks are being freed from
1653 * @bstart: first block of a run of contiguous blocks
1654 * @blen: the length of the block run
1658 void __gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1660 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1661 struct gfs2_rgrpd *rgd;
1663 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1666 trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
1667 rgd->rd_free += blen;
1669 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1670 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1672 gfs2_trans_add_rg(rgd);
1673 gfs2_meta_wipe(ip, bstart, blen);
1677 * gfs2_free_meta - free a contiguous run of data block(s)
1678 * @ip: the inode these blocks are being freed from
1679 * @bstart: first block of a run of contiguous blocks
1680 * @blen: the length of the block run
1684 void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
1686 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1688 __gfs2_free_meta(ip, bstart, blen);
1689 gfs2_statfs_change(sdp, 0, +blen, 0);
1690 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
1693 void gfs2_unlink_di(struct inode *inode)
1695 struct gfs2_inode *ip = GFS2_I(inode);
1696 struct gfs2_sbd *sdp = GFS2_SB(inode);
1697 struct gfs2_rgrpd *rgd;
1698 u64 blkno = ip->i_no_addr;
1700 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1703 trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
1704 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1705 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1706 gfs2_trans_add_rg(rgd);
1709 static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
1711 struct gfs2_sbd *sdp = rgd->rd_sbd;
1712 struct gfs2_rgrpd *tmp_rgd;
1714 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1717 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1719 if (!rgd->rd_dinodes)
1720 gfs2_consist_rgrpd(rgd);
1724 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1725 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
1727 gfs2_statfs_change(sdp, 0, +1, -1);
1728 gfs2_trans_add_rg(rgd);
1732 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1734 gfs2_free_uninit_di(rgd, ip->i_no_addr);
1735 trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
1736 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
1737 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
1741 * gfs2_check_blk_type - Check the type of a block
1742 * @sdp: The superblock
1743 * @no_addr: The block number to check
1744 * @type: The block type we are looking for
1746 * Returns: 0 if the block type matches the expected type
1747 * -ESTALE if it doesn't match
1748 * or -ve errno if something went wrong while checking
1751 int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
1753 struct gfs2_rgrpd *rgd;
1754 struct gfs2_holder ri_gh, rgd_gh;
1755 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
1759 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
1760 error = gfs2_rindex_hold(sdp, &ri_gh);
1767 rgd = gfs2_blk2rgrpd(sdp, no_addr);
1771 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
1775 if (gfs2_get_block_type(rgd, no_addr) != type)
1778 gfs2_glock_dq_uninit(&rgd_gh);
1781 gfs2_glock_dq_uninit(&ri_gh);
1787 * gfs2_rlist_add - add a RG to a list of RGs
1788 * @sdp: the filesystem
1789 * @rlist: the list of resource groups
1792 * Figure out what RG a block belongs to and add that RG to the list
1794 * FIXME: Don't use NOFAIL
1798 void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
1801 struct gfs2_rgrpd *rgd;
1802 struct gfs2_rgrpd **tmp;
1803 unsigned int new_space;
1806 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1809 rgd = gfs2_blk2rgrpd(sdp, block);
1811 if (gfs2_consist(sdp))
1812 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
1816 for (x = 0; x < rlist->rl_rgrps; x++)
1817 if (rlist->rl_rgd[x] == rgd)
1820 if (rlist->rl_rgrps == rlist->rl_space) {
1821 new_space = rlist->rl_space + 10;
1823 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
1824 GFP_NOFS | __GFP_NOFAIL);
1826 if (rlist->rl_rgd) {
1827 memcpy(tmp, rlist->rl_rgd,
1828 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1829 kfree(rlist->rl_rgd);
1832 rlist->rl_space = new_space;
1833 rlist->rl_rgd = tmp;
1836 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1840 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1841 * and initialize an array of glock holders for them
1842 * @rlist: the list of resource groups
1843 * @state: the lock state to acquire the RG lock in
1844 * @flags: the modifier flags for the holder structures
1846 * FIXME: Don't use NOFAIL
1850 void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
1854 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
1855 GFP_NOFS | __GFP_NOFAIL);
1856 for (x = 0; x < rlist->rl_rgrps; x++)
1857 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1863 * gfs2_rlist_free - free a resource group list
1864 * @list: the list of resource groups
1868 void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1872 kfree(rlist->rl_rgd);
1874 if (rlist->rl_ghs) {
1875 for (x = 0; x < rlist->rl_rgrps; x++)
1876 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1877 kfree(rlist->rl_ghs);