2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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 v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
16 #include <linux/gfs2_ondisk.h>
19 #include "lm_interface.h"
32 #define BFITNOENT 0xFFFFFFFF
35 * These routines are used by the resource group routines (rgrp.c)
36 * to keep track of block allocation. Each block is represented by two
37 * bits. One bit indicates whether or not the block is used. (1=used,
38 * 0=free) The other bit indicates whether or not the block contains a
39 * dinode or not. (1=dinode, 0=not-dinode) So, each byte represents
40 * GFS2_NBBY (i.e. 4) blocks.
43 static const char valid_change
[16] = {
52 * gfs2_setbit - Set a bit in the bitmaps
53 * @buffer: the buffer that holds the bitmaps
54 * @buflen: the length (in bytes) of the buffer
55 * @block: the block to set
56 * @new_state: the new state of the block
60 static void gfs2_setbit(struct gfs2_rgrpd
*rgd
, unsigned char *buffer
,
61 unsigned int buflen
, uint32_t block
,
62 unsigned char new_state
)
64 unsigned char *byte
, *end
, cur_state
;
67 byte
= buffer
+ (block
/ GFS2_NBBY
);
68 bit
= (block
% GFS2_NBBY
) * GFS2_BIT_SIZE
;
69 end
= buffer
+ buflen
;
71 gfs2_assert(rgd
->rd_sbd
, byte
< end
);
73 cur_state
= (*byte
>> bit
) & GFS2_BIT_MASK
;
75 if (valid_change
[new_state
* 4 + cur_state
]) {
76 *byte
^= cur_state
<< bit
;
77 *byte
|= new_state
<< bit
;
79 gfs2_consist_rgrpd(rgd
);
83 * gfs2_testbit - test a bit in the bitmaps
84 * @buffer: the buffer that holds the bitmaps
85 * @buflen: the length (in bytes) of the buffer
86 * @block: the block to read
90 static unsigned char gfs2_testbit(struct gfs2_rgrpd
*rgd
, unsigned char *buffer
,
91 unsigned int buflen
, uint32_t block
)
93 unsigned char *byte
, *end
, cur_state
;
96 byte
= buffer
+ (block
/ GFS2_NBBY
);
97 bit
= (block
% GFS2_NBBY
) * GFS2_BIT_SIZE
;
98 end
= buffer
+ buflen
;
100 gfs2_assert(rgd
->rd_sbd
, byte
< end
);
102 cur_state
= (*byte
>> bit
) & GFS2_BIT_MASK
;
108 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
109 * a block in a given allocation state.
110 * @buffer: the buffer that holds the bitmaps
111 * @buflen: the length (in bytes) of the buffer
112 * @goal: start search at this block's bit-pair (within @buffer)
113 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
114 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
116 * Scope of @goal and returned block number is only within this bitmap buffer,
117 * not entire rgrp or filesystem. @buffer will be offset from the actual
118 * beginning of a bitmap block buffer, skipping any header structures.
120 * Return: the block number (bitmap buffer scope) that was found
123 static uint32_t gfs2_bitfit(struct gfs2_rgrpd
*rgd
, unsigned char *buffer
,
124 unsigned int buflen
, uint32_t goal
,
125 unsigned char old_state
)
127 unsigned char *byte
, *end
, alloc
;
131 byte
= buffer
+ (goal
/ GFS2_NBBY
);
132 bit
= (goal
% GFS2_NBBY
) * GFS2_BIT_SIZE
;
133 end
= buffer
+ buflen
;
134 alloc
= (old_state
& 1) ? 0 : 0x55;
137 if ((*byte
& 0x55) == alloc
) {
138 blk
+= (8 - bit
) >> 1;
146 if (((*byte
>> bit
) & GFS2_BIT_MASK
) == old_state
)
149 bit
+= GFS2_BIT_SIZE
;
162 * gfs2_bitcount - count the number of bits in a certain state
163 * @buffer: the buffer that holds the bitmaps
164 * @buflen: the length (in bytes) of the buffer
165 * @state: the state of the block we're looking for
167 * Returns: The number of bits
170 static uint32_t gfs2_bitcount(struct gfs2_rgrpd
*rgd
, unsigned char *buffer
,
171 unsigned int buflen
, unsigned char state
)
173 unsigned char *byte
= buffer
;
174 unsigned char *end
= buffer
+ buflen
;
175 unsigned char state1
= state
<< 2;
176 unsigned char state2
= state
<< 4;
177 unsigned char state3
= state
<< 6;
180 for (; byte
< end
; byte
++) {
181 if (((*byte
) & 0x03) == state
)
183 if (((*byte
) & 0x0C) == state1
)
185 if (((*byte
) & 0x30) == state2
)
187 if (((*byte
) & 0xC0) == state3
)
195 * gfs2_rgrp_verify - Verify that a resource group is consistent
196 * @sdp: the filesystem
201 void gfs2_rgrp_verify(struct gfs2_rgrpd
*rgd
)
203 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
204 struct gfs2_bitmap
*bi
= NULL
;
205 uint32_t length
= rgd
->rd_ri
.ri_length
;
206 uint32_t count
[4], tmp
;
209 memset(count
, 0, 4 * sizeof(uint32_t));
211 /* Count # blocks in each of 4 possible allocation states */
212 for (buf
= 0; buf
< length
; buf
++) {
213 bi
= rgd
->rd_bits
+ buf
;
214 for (x
= 0; x
< 4; x
++)
215 count
[x
] += gfs2_bitcount(rgd
,
221 if (count
[0] != rgd
->rd_rg
.rg_free
) {
222 if (gfs2_consist_rgrpd(rgd
))
223 fs_err(sdp
, "free data mismatch: %u != %u\n",
224 count
[0], rgd
->rd_rg
.rg_free
);
228 tmp
= rgd
->rd_ri
.ri_data
-
230 rgd
->rd_rg
.rg_dinodes
;
231 if (count
[1] != tmp
) {
232 if (gfs2_consist_rgrpd(rgd
))
233 fs_err(sdp
, "used data mismatch: %u != %u\n",
239 if (gfs2_consist_rgrpd(rgd
))
240 fs_err(sdp
, "free metadata mismatch: %u != 0\n",
245 if (count
[3] != rgd
->rd_rg
.rg_dinodes
) {
246 if (gfs2_consist_rgrpd(rgd
))
247 fs_err(sdp
, "used metadata mismatch: %u != %u\n",
248 count
[3], rgd
->rd_rg
.rg_dinodes
);
253 static inline int rgrp_contains_block(struct gfs2_rindex
*ri
, uint64_t block
)
255 uint64_t first
= ri
->ri_data0
;
256 uint64_t last
= first
+ ri
->ri_data
;
257 return !!(first
<= block
&& block
< last
);
261 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
262 * @sdp: The GFS2 superblock
263 * @n: The data block number
265 * Returns: The resource group, or NULL if not found
268 struct gfs2_rgrpd
*gfs2_blk2rgrpd(struct gfs2_sbd
*sdp
, uint64_t blk
)
270 struct gfs2_rgrpd
*rgd
;
272 spin_lock(&sdp
->sd_rindex_spin
);
274 list_for_each_entry(rgd
, &sdp
->sd_rindex_mru_list
, rd_list_mru
) {
275 if (rgrp_contains_block(&rgd
->rd_ri
, blk
)) {
276 list_move(&rgd
->rd_list_mru
, &sdp
->sd_rindex_mru_list
);
277 spin_unlock(&sdp
->sd_rindex_spin
);
282 spin_unlock(&sdp
->sd_rindex_spin
);
288 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
289 * @sdp: The GFS2 superblock
291 * Returns: The first rgrp in the filesystem
294 struct gfs2_rgrpd
*gfs2_rgrpd_get_first(struct gfs2_sbd
*sdp
)
296 gfs2_assert(sdp
, !list_empty(&sdp
->sd_rindex_list
));
297 return list_entry(sdp
->sd_rindex_list
.next
, struct gfs2_rgrpd
, rd_list
);
301 * gfs2_rgrpd_get_next - get the next RG
304 * Returns: The next rgrp
307 struct gfs2_rgrpd
*gfs2_rgrpd_get_next(struct gfs2_rgrpd
*rgd
)
309 if (rgd
->rd_list
.next
== &rgd
->rd_sbd
->sd_rindex_list
)
311 return list_entry(rgd
->rd_list
.next
, struct gfs2_rgrpd
, rd_list
);
314 static void clear_rgrpdi(struct gfs2_sbd
*sdp
)
316 struct list_head
*head
;
317 struct gfs2_rgrpd
*rgd
;
318 struct gfs2_glock
*gl
;
320 spin_lock(&sdp
->sd_rindex_spin
);
321 sdp
->sd_rindex_forward
= NULL
;
322 head
= &sdp
->sd_rindex_recent_list
;
323 while (!list_empty(head
)) {
324 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_recent
);
325 list_del(&rgd
->rd_recent
);
327 spin_unlock(&sdp
->sd_rindex_spin
);
329 head
= &sdp
->sd_rindex_list
;
330 while (!list_empty(head
)) {
331 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_list
);
334 list_del(&rgd
->rd_list
);
335 list_del(&rgd
->rd_list_mru
);
338 gl
->gl_object
= NULL
;
347 void gfs2_clear_rgrpd(struct gfs2_sbd
*sdp
)
349 mutex_lock(&sdp
->sd_rindex_mutex
);
351 mutex_unlock(&sdp
->sd_rindex_mutex
);
355 * gfs2_compute_bitstructs - Compute the bitmap sizes
356 * @rgd: The resource group descriptor
358 * Calculates bitmap descriptors, one for each block that contains bitmap data
363 static int compute_bitstructs(struct gfs2_rgrpd
*rgd
)
365 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
366 struct gfs2_bitmap
*bi
;
367 uint32_t length
= rgd
->rd_ri
.ri_length
; /* # blocks in hdr & bitmap */
368 uint32_t bytes_left
, bytes
;
371 rgd
->rd_bits
= kcalloc(length
, sizeof(struct gfs2_bitmap
), GFP_KERNEL
);
375 bytes_left
= rgd
->rd_ri
.ri_bitbytes
;
377 for (x
= 0; x
< length
; x
++) {
378 bi
= rgd
->rd_bits
+ x
;
380 /* small rgrp; bitmap stored completely in header block */
383 bi
->bi_offset
= sizeof(struct gfs2_rgrp
);
388 bytes
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_rgrp
);
389 bi
->bi_offset
= sizeof(struct gfs2_rgrp
);
393 } else if (x
+ 1 == length
) {
395 bi
->bi_offset
= sizeof(struct gfs2_meta_header
);
396 bi
->bi_start
= rgd
->rd_ri
.ri_bitbytes
- bytes_left
;
400 bytes
= sdp
->sd_sb
.sb_bsize
-
401 sizeof(struct gfs2_meta_header
);
402 bi
->bi_offset
= sizeof(struct gfs2_meta_header
);
403 bi
->bi_start
= rgd
->rd_ri
.ri_bitbytes
- bytes_left
;
411 gfs2_consist_rgrpd(rgd
);
414 bi
= rgd
->rd_bits
+ (length
- 1);
415 if ((bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
!= rgd
->rd_ri
.ri_data
) {
416 if (gfs2_consist_rgrpd(rgd
)) {
417 gfs2_rindex_print(&rgd
->rd_ri
);
418 fs_err(sdp
, "start=%u len=%u offset=%u\n",
419 bi
->bi_start
, bi
->bi_len
, bi
->bi_offset
);
428 * gfs2_ri_update - Pull in a new resource index from the disk
429 * @gl: The glock covering the rindex inode
431 * Returns: 0 on successful update, error code otherwise
434 static int gfs2_ri_update(struct gfs2_inode
*ip
)
436 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
437 struct inode
*inode
= ip
->i_vnode
;
438 struct gfs2_rgrpd
*rgd
;
439 char buf
[sizeof(struct gfs2_rindex
)];
440 struct file_ra_state ra_state
;
441 uint64_t junk
= ip
->i_di
.di_size
;
444 if (do_div(junk
, sizeof(struct gfs2_rindex
))) {
445 gfs2_consist_inode(ip
);
451 file_ra_state_init(&ra_state
, inode
->i_mapping
);
452 for (sdp
->sd_rgrps
= 0;; sdp
->sd_rgrps
++) {
453 loff_t pos
= sdp
->sd_rgrps
* sizeof(struct gfs2_rindex
);
454 error
= gfs2_internal_read(ip
, &ra_state
, buf
, &pos
,
455 sizeof(struct gfs2_rindex
));
458 if (error
!= sizeof(struct gfs2_rindex
)) {
464 rgd
= kzalloc(sizeof(struct gfs2_rgrpd
), GFP_KERNEL
);
469 mutex_init(&rgd
->rd_mutex
);
470 lops_init_le(&rgd
->rd_le
, &gfs2_rg_lops
);
473 list_add_tail(&rgd
->rd_list
, &sdp
->sd_rindex_list
);
474 list_add_tail(&rgd
->rd_list_mru
, &sdp
->sd_rindex_mru_list
);
476 gfs2_rindex_in(&rgd
->rd_ri
, buf
);
478 error
= compute_bitstructs(rgd
);
482 error
= gfs2_glock_get(sdp
, rgd
->rd_ri
.ri_addr
,
483 &gfs2_rgrp_glops
, CREATE
, &rgd
->rd_gl
);
487 rgd
->rd_gl
->gl_object
= rgd
;
488 rgd
->rd_rg_vn
= rgd
->rd_gl
->gl_vn
- 1;
491 sdp
->sd_rindex_vn
= ip
->i_gl
->gl_vn
;
502 * gfs2_rindex_hold - Grab a lock on the rindex
503 * @sdp: The GFS2 superblock
504 * @ri_gh: the glock holder
506 * We grab a lock on the rindex inode to make sure that it doesn't
507 * change whilst we are performing an operation. We keep this lock
508 * for quite long periods of time compared to other locks. This
509 * doesn't matter, since it is shared and it is very, very rarely
510 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
512 * This makes sure that we're using the latest copy of the resource index
513 * special file, which might have been updated if someone expanded the
514 * filesystem (via gfs2_grow utility), which adds new resource groups.
516 * Returns: 0 on success, error code otherwise
519 int gfs2_rindex_hold(struct gfs2_sbd
*sdp
, struct gfs2_holder
*ri_gh
)
521 struct gfs2_inode
*ip
= sdp
->sd_rindex
->u
.generic_ip
;
522 struct gfs2_glock
*gl
= ip
->i_gl
;
525 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, 0, ri_gh
);
529 /* Read new copy from disk if we don't have the latest */
530 if (sdp
->sd_rindex_vn
!= gl
->gl_vn
) {
531 mutex_lock(&sdp
->sd_rindex_mutex
);
532 if (sdp
->sd_rindex_vn
!= gl
->gl_vn
) {
533 error
= gfs2_ri_update(ip
);
535 gfs2_glock_dq_uninit(ri_gh
);
537 mutex_unlock(&sdp
->sd_rindex_mutex
);
544 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
545 * @rgd: the struct gfs2_rgrpd describing the RG to read in
547 * Read in all of a Resource Group's header and bitmap blocks.
548 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
553 int gfs2_rgrp_bh_get(struct gfs2_rgrpd
*rgd
)
555 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
556 struct gfs2_glock
*gl
= rgd
->rd_gl
;
557 unsigned int length
= rgd
->rd_ri
.ri_length
;
558 struct gfs2_bitmap
*bi
;
562 mutex_lock(&rgd
->rd_mutex
);
564 spin_lock(&sdp
->sd_rindex_spin
);
565 if (rgd
->rd_bh_count
) {
567 spin_unlock(&sdp
->sd_rindex_spin
);
568 mutex_unlock(&rgd
->rd_mutex
);
571 spin_unlock(&sdp
->sd_rindex_spin
);
573 for (x
= 0; x
< length
; x
++) {
574 bi
= rgd
->rd_bits
+ x
;
575 error
= gfs2_meta_read(gl
, rgd
->rd_ri
.ri_addr
+ x
, DIO_START
,
581 for (y
= length
; y
--;) {
582 bi
= rgd
->rd_bits
+ y
;
583 error
= gfs2_meta_reread(sdp
, bi
->bi_bh
, DIO_WAIT
);
586 if (gfs2_metatype_check(sdp
, bi
->bi_bh
,
587 (y
) ? GFS2_METATYPE_RB
:
594 if (rgd
->rd_rg_vn
!= gl
->gl_vn
) {
595 gfs2_rgrp_in(&rgd
->rd_rg
, (rgd
->rd_bits
[0].bi_bh
)->b_data
);
596 rgd
->rd_rg_vn
= gl
->gl_vn
;
599 spin_lock(&sdp
->sd_rindex_spin
);
600 rgd
->rd_free_clone
= rgd
->rd_rg
.rg_free
;
602 spin_unlock(&sdp
->sd_rindex_spin
);
604 mutex_unlock(&rgd
->rd_mutex
);
610 bi
= rgd
->rd_bits
+ x
;
613 gfs2_assert_warn(sdp
, !bi
->bi_clone
);
615 mutex_unlock(&rgd
->rd_mutex
);
620 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd
*rgd
)
622 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
624 spin_lock(&sdp
->sd_rindex_spin
);
625 gfs2_assert_warn(rgd
->rd_sbd
, rgd
->rd_bh_count
);
627 spin_unlock(&sdp
->sd_rindex_spin
);
631 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
632 * @rgd: the struct gfs2_rgrpd describing the RG to read in
636 void gfs2_rgrp_bh_put(struct gfs2_rgrpd
*rgd
)
638 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
639 int x
, length
= rgd
->rd_ri
.ri_length
;
641 spin_lock(&sdp
->sd_rindex_spin
);
642 gfs2_assert_warn(rgd
->rd_sbd
, rgd
->rd_bh_count
);
643 if (--rgd
->rd_bh_count
) {
644 spin_unlock(&sdp
->sd_rindex_spin
);
648 for (x
= 0; x
< length
; x
++) {
649 struct gfs2_bitmap
*bi
= rgd
->rd_bits
+ x
;
656 spin_unlock(&sdp
->sd_rindex_spin
);
659 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd
*rgd
)
661 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
662 unsigned int length
= rgd
->rd_ri
.ri_length
;
665 for (x
= 0; x
< length
; x
++) {
666 struct gfs2_bitmap
*bi
= rgd
->rd_bits
+ x
;
669 memcpy(bi
->bi_clone
+ bi
->bi_offset
,
670 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
674 spin_lock(&sdp
->sd_rindex_spin
);
675 rgd
->rd_free_clone
= rgd
->rd_rg
.rg_free
;
676 spin_unlock(&sdp
->sd_rindex_spin
);
680 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
681 * @ip: the incore GFS2 inode structure
683 * Returns: the struct gfs2_alloc
686 struct gfs2_alloc
*gfs2_alloc_get(struct gfs2_inode
*ip
)
688 struct gfs2_alloc
*al
= &ip
->i_alloc
;
690 /* FIXME: Should assert that the correct locks are held here... */
691 memset(al
, 0, sizeof(*al
));
696 * gfs2_alloc_put - throw away the struct gfs2_alloc for an inode
701 void gfs2_alloc_put(struct gfs2_inode
*ip
)
707 * try_rgrp_fit - See if a given reservation will fit in a given RG
709 * @al: the struct gfs2_alloc structure describing the reservation
711 * If there's room for the requested blocks to be allocated from the RG:
712 * Sets the $al_reserved_data field in @al.
713 * Sets the $al_reserved_meta field in @al.
714 * Sets the $al_rgd field in @al.
716 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
719 static int try_rgrp_fit(struct gfs2_rgrpd
*rgd
, struct gfs2_alloc
*al
)
721 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
724 spin_lock(&sdp
->sd_rindex_spin
);
725 if (rgd
->rd_free_clone
>= al
->al_requested
) {
729 spin_unlock(&sdp
->sd_rindex_spin
);
735 * recent_rgrp_first - get first RG from "recent" list
736 * @sdp: The GFS2 superblock
737 * @rglast: address of the rgrp used last
739 * Returns: The first rgrp in the recent list
742 static struct gfs2_rgrpd
*recent_rgrp_first(struct gfs2_sbd
*sdp
,
745 struct gfs2_rgrpd
*rgd
= NULL
;
747 spin_lock(&sdp
->sd_rindex_spin
);
749 if (list_empty(&sdp
->sd_rindex_recent_list
))
755 list_for_each_entry(rgd
, &sdp
->sd_rindex_recent_list
, rd_recent
) {
756 if (rgd
->rd_ri
.ri_addr
== rglast
)
761 rgd
= list_entry(sdp
->sd_rindex_recent_list
.next
, struct gfs2_rgrpd
,
765 spin_unlock(&sdp
->sd_rindex_spin
);
771 * recent_rgrp_next - get next RG from "recent" list
772 * @cur_rgd: current rgrp
775 * Returns: The next rgrp in the recent list
778 static struct gfs2_rgrpd
*recent_rgrp_next(struct gfs2_rgrpd
*cur_rgd
,
781 struct gfs2_sbd
*sdp
= cur_rgd
->rd_sbd
;
782 struct list_head
*head
;
783 struct gfs2_rgrpd
*rgd
;
785 spin_lock(&sdp
->sd_rindex_spin
);
787 head
= &sdp
->sd_rindex_recent_list
;
789 list_for_each_entry(rgd
, head
, rd_recent
) {
790 if (rgd
== cur_rgd
) {
791 if (cur_rgd
->rd_recent
.next
!= head
)
792 rgd
= list_entry(cur_rgd
->rd_recent
.next
,
793 struct gfs2_rgrpd
, rd_recent
);
798 list_del(&cur_rgd
->rd_recent
);
805 if (!list_empty(head
))
806 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_recent
);
809 spin_unlock(&sdp
->sd_rindex_spin
);
815 * recent_rgrp_add - add an RG to tail of "recent" list
816 * @new_rgd: The rgrp to add
820 static void recent_rgrp_add(struct gfs2_rgrpd
*new_rgd
)
822 struct gfs2_sbd
*sdp
= new_rgd
->rd_sbd
;
823 struct gfs2_rgrpd
*rgd
;
824 unsigned int count
= 0;
825 unsigned int max
= sdp
->sd_rgrps
/ gfs2_jindex_size(sdp
);
827 spin_lock(&sdp
->sd_rindex_spin
);
829 list_for_each_entry(rgd
, &sdp
->sd_rindex_recent_list
, rd_recent
) {
836 list_add_tail(&new_rgd
->rd_recent
, &sdp
->sd_rindex_recent_list
);
839 spin_unlock(&sdp
->sd_rindex_spin
);
843 * forward_rgrp_get - get an rgrp to try next from full list
844 * @sdp: The GFS2 superblock
846 * Returns: The rgrp to try next
849 static struct gfs2_rgrpd
*forward_rgrp_get(struct gfs2_sbd
*sdp
)
851 struct gfs2_rgrpd
*rgd
;
852 unsigned int journals
= gfs2_jindex_size(sdp
);
853 unsigned int rg
= 0, x
;
855 spin_lock(&sdp
->sd_rindex_spin
);
857 rgd
= sdp
->sd_rindex_forward
;
859 if (sdp
->sd_rgrps
>= journals
)
860 rg
= sdp
->sd_rgrps
* sdp
->sd_jdesc
->jd_jid
/ journals
;
862 for (x
= 0, rgd
= gfs2_rgrpd_get_first(sdp
);
864 x
++, rgd
= gfs2_rgrpd_get_next(rgd
))
867 sdp
->sd_rindex_forward
= rgd
;
870 spin_unlock(&sdp
->sd_rindex_spin
);
876 * forward_rgrp_set - set the forward rgrp pointer
877 * @sdp: the filesystem
878 * @rgd: The new forward rgrp
882 static void forward_rgrp_set(struct gfs2_sbd
*sdp
, struct gfs2_rgrpd
*rgd
)
884 spin_lock(&sdp
->sd_rindex_spin
);
885 sdp
->sd_rindex_forward
= rgd
;
886 spin_unlock(&sdp
->sd_rindex_spin
);
890 * get_local_rgrp - Choose and lock a rgrp for allocation
891 * @ip: the inode to reserve space for
892 * @rgp: the chosen and locked rgrp
894 * Try to acquire rgrp in way which avoids contending with others.
899 static int get_local_rgrp(struct gfs2_inode
*ip
)
901 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
902 struct gfs2_rgrpd
*rgd
, *begin
= NULL
;
903 struct gfs2_alloc
*al
= &ip
->i_alloc
;
904 int flags
= LM_FLAG_TRY
;
909 /* Try recently successful rgrps */
911 rgd
= recent_rgrp_first(sdp
, ip
->i_last_rg_alloc
);
914 error
= gfs2_glock_nq_init(rgd
->rd_gl
,
915 LM_ST_EXCLUSIVE
, LM_FLAG_TRY
,
919 if (try_rgrp_fit(rgd
, al
))
921 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
922 rgd
= recent_rgrp_next(rgd
, 1);
926 rgd
= recent_rgrp_next(rgd
, 0);
934 /* Go through full list of rgrps */
936 begin
= rgd
= forward_rgrp_get(sdp
);
939 error
= gfs2_glock_nq_init(rgd
->rd_gl
,
940 LM_ST_EXCLUSIVE
, flags
,
944 if (try_rgrp_fit(rgd
, al
))
946 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
957 rgd
= gfs2_rgrpd_get_next(rgd
);
959 rgd
= gfs2_rgrpd_get_first(sdp
);
962 if (++loops
>= 2 || !skipped
)
969 ip
->i_last_rg_alloc
= rgd
->rd_ri
.ri_addr
;
972 recent_rgrp_add(rgd
);
973 rgd
= gfs2_rgrpd_get_next(rgd
);
975 rgd
= gfs2_rgrpd_get_first(sdp
);
976 forward_rgrp_set(sdp
, rgd
);
983 * gfs2_inplace_reserve_i - Reserve space in the filesystem
984 * @ip: the inode to reserve space for
989 int gfs2_inplace_reserve_i(struct gfs2_inode
*ip
, char *file
, unsigned int line
)
991 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
992 struct gfs2_alloc
*al
= &ip
->i_alloc
;
995 if (gfs2_assert_warn(sdp
, al
->al_requested
))
998 error
= gfs2_rindex_hold(sdp
, &al
->al_ri_gh
);
1002 error
= get_local_rgrp(ip
);
1004 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
1015 * gfs2_inplace_release - release an inplace reservation
1016 * @ip: the inode the reservation was taken out on
1018 * Release a reservation made by gfs2_inplace_reserve().
1021 void gfs2_inplace_release(struct gfs2_inode
*ip
)
1023 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1024 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1026 if (gfs2_assert_warn(sdp
, al
->al_alloced
<= al
->al_requested
) == -1)
1027 fs_warn(sdp
, "al_alloced = %u, al_requested = %u "
1028 "al_file = %s, al_line = %u\n",
1029 al
->al_alloced
, al
->al_requested
, al
->al_file
,
1033 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
1034 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
1038 * gfs2_get_block_type - Check a block in a RG is of given type
1039 * @rgd: the resource group holding the block
1040 * @block: the block number
1042 * Returns: The block type (GFS2_BLKST_*)
1045 unsigned char gfs2_get_block_type(struct gfs2_rgrpd
*rgd
, uint64_t block
)
1047 struct gfs2_bitmap
*bi
= NULL
;
1048 uint32_t length
, rgrp_block
, buf_block
;
1052 length
= rgd
->rd_ri
.ri_length
;
1053 rgrp_block
= block
- rgd
->rd_ri
.ri_data0
;
1055 for (buf
= 0; buf
< length
; buf
++) {
1056 bi
= rgd
->rd_bits
+ buf
;
1057 if (rgrp_block
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
1061 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
1062 buf_block
= rgrp_block
- bi
->bi_start
* GFS2_NBBY
;
1064 type
= gfs2_testbit(rgd
,
1065 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1066 bi
->bi_len
, buf_block
);
1072 * rgblk_search - find a block in @old_state, change allocation
1073 * state to @new_state
1074 * @rgd: the resource group descriptor
1075 * @goal: the goal block within the RG (start here to search for avail block)
1076 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1077 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1079 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1080 * Add the found bitmap buffer to the transaction.
1081 * Set the found bits to @new_state to change block's allocation state.
1083 * This function never fails, because we wouldn't call it unless we
1084 * know (from reservation results, etc.) that a block is available.
1086 * Scope of @goal and returned block is just within rgrp, not the whole
1089 * Returns: the block number allocated
1092 static uint32_t rgblk_search(struct gfs2_rgrpd
*rgd
, uint32_t goal
,
1093 unsigned char old_state
, unsigned char new_state
)
1095 struct gfs2_bitmap
*bi
= NULL
;
1096 uint32_t length
= rgd
->rd_ri
.ri_length
;
1098 unsigned int buf
, x
;
1100 /* Find bitmap block that contains bits for goal block */
1101 for (buf
= 0; buf
< length
; buf
++) {
1102 bi
= rgd
->rd_bits
+ buf
;
1103 if (goal
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
1107 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
1109 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1110 goal
-= bi
->bi_start
* GFS2_NBBY
;
1112 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1113 "x <= length", instead of "x < length", because we typically start
1114 the search in the middle of a bit block, but if we can't find an
1115 allocatable block anywhere else, we want to be able wrap around and
1116 search in the first part of our first-searched bit block. */
1117 for (x
= 0; x
<= length
; x
++) {
1119 blk
= gfs2_bitfit(rgd
, bi
->bi_clone
+ bi
->bi_offset
,
1120 bi
->bi_len
, goal
, old_state
);
1122 blk
= gfs2_bitfit(rgd
,
1123 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1124 bi
->bi_len
, goal
, old_state
);
1125 if (blk
!= BFITNOENT
)
1128 /* Try next bitmap block (wrap back to rgrp header if at end) */
1129 buf
= (buf
+ 1) % length
;
1130 bi
= rgd
->rd_bits
+ buf
;
1134 if (gfs2_assert_withdraw(rgd
->rd_sbd
, x
<= length
))
1137 gfs2_trans_add_bh(rgd
->rd_gl
, bi
->bi_bh
, 1);
1138 gfs2_setbit(rgd
, bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1139 bi
->bi_len
, blk
, new_state
);
1141 gfs2_setbit(rgd
, bi
->bi_clone
+ bi
->bi_offset
,
1142 bi
->bi_len
, blk
, new_state
);
1144 return bi
->bi_start
* GFS2_NBBY
+ blk
;
1148 * rgblk_free - Change alloc state of given block(s)
1149 * @sdp: the filesystem
1150 * @bstart: the start of a run of blocks to free
1151 * @blen: the length of the block run (all must lie within ONE RG!)
1152 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1154 * Returns: Resource group containing the block(s)
1157 static struct gfs2_rgrpd
*rgblk_free(struct gfs2_sbd
*sdp
, uint64_t bstart
,
1158 uint32_t blen
, unsigned char new_state
)
1160 struct gfs2_rgrpd
*rgd
;
1161 struct gfs2_bitmap
*bi
= NULL
;
1162 uint32_t length
, rgrp_blk
, buf_blk
;
1165 rgd
= gfs2_blk2rgrpd(sdp
, bstart
);
1167 if (gfs2_consist(sdp
))
1168 fs_err(sdp
, "block = %llu\n", (unsigned long long)bstart
);
1172 length
= rgd
->rd_ri
.ri_length
;
1174 rgrp_blk
= bstart
- rgd
->rd_ri
.ri_data0
;
1177 for (buf
= 0; buf
< length
; buf
++) {
1178 bi
= rgd
->rd_bits
+ buf
;
1179 if (rgrp_blk
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
1183 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
1185 buf_blk
= rgrp_blk
- bi
->bi_start
* GFS2_NBBY
;
1188 if (!bi
->bi_clone
) {
1189 bi
->bi_clone
= kmalloc(bi
->bi_bh
->b_size
,
1190 GFP_KERNEL
| __GFP_NOFAIL
);
1191 memcpy(bi
->bi_clone
+ bi
->bi_offset
,
1192 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1195 gfs2_trans_add_bh(rgd
->rd_gl
, bi
->bi_bh
, 1);
1197 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1198 bi
->bi_len
, buf_blk
, new_state
);
1205 * gfs2_alloc_data - Allocate a data block
1206 * @ip: the inode to allocate the data block for
1208 * Returns: the allocated block
1211 uint64_t gfs2_alloc_data(struct gfs2_inode
*ip
)
1213 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1214 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1215 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1219 if (rgrp_contains_block(&rgd
->rd_ri
, ip
->i_di
.di_goal_data
))
1220 goal
= ip
->i_di
.di_goal_data
- rgd
->rd_ri
.ri_data0
;
1222 goal
= rgd
->rd_last_alloc_data
;
1224 blk
= rgblk_search(rgd
, goal
, GFS2_BLKST_FREE
, GFS2_BLKST_USED
);
1225 rgd
->rd_last_alloc_data
= blk
;
1227 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1228 ip
->i_di
.di_goal_data
= block
;
1230 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1231 rgd
->rd_rg
.rg_free
--;
1233 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1234 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1238 gfs2_statfs_change(sdp
, 0, -1, 0);
1239 gfs2_quota_change(ip
, +1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1241 spin_lock(&sdp
->sd_rindex_spin
);
1242 rgd
->rd_free_clone
--;
1243 spin_unlock(&sdp
->sd_rindex_spin
);
1249 * gfs2_alloc_meta - Allocate a metadata block
1250 * @ip: the inode to allocate the metadata block for
1252 * Returns: the allocated block
1255 uint64_t gfs2_alloc_meta(struct gfs2_inode
*ip
)
1257 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1258 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1259 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1263 if (rgrp_contains_block(&rgd
->rd_ri
, ip
->i_di
.di_goal_meta
))
1264 goal
= ip
->i_di
.di_goal_meta
- rgd
->rd_ri
.ri_data0
;
1266 goal
= rgd
->rd_last_alloc_meta
;
1268 blk
= rgblk_search(rgd
, goal
, GFS2_BLKST_FREE
, GFS2_BLKST_USED
);
1269 rgd
->rd_last_alloc_meta
= blk
;
1271 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1272 ip
->i_di
.di_goal_meta
= block
;
1274 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1275 rgd
->rd_rg
.rg_free
--;
1277 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1278 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1282 gfs2_statfs_change(sdp
, 0, -1, 0);
1283 gfs2_quota_change(ip
, +1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1284 gfs2_trans_add_unrevoke(sdp
, block
);
1286 spin_lock(&sdp
->sd_rindex_spin
);
1287 rgd
->rd_free_clone
--;
1288 spin_unlock(&sdp
->sd_rindex_spin
);
1294 * gfs2_alloc_di - Allocate a dinode
1295 * @dip: the directory that the inode is going in
1297 * Returns: the block allocated
1300 uint64_t gfs2_alloc_di(struct gfs2_inode
*dip
)
1302 struct gfs2_sbd
*sdp
= dip
->i_sbd
;
1303 struct gfs2_alloc
*al
= &dip
->i_alloc
;
1304 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1308 blk
= rgblk_search(rgd
, rgd
->rd_last_alloc_meta
,
1309 GFS2_BLKST_FREE
, GFS2_BLKST_DINODE
);
1311 rgd
->rd_last_alloc_meta
= blk
;
1313 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1315 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1316 rgd
->rd_rg
.rg_free
--;
1317 rgd
->rd_rg
.rg_dinodes
++;
1319 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1320 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1324 gfs2_statfs_change(sdp
, 0, -1, +1);
1325 gfs2_trans_add_unrevoke(sdp
, block
);
1327 spin_lock(&sdp
->sd_rindex_spin
);
1328 rgd
->rd_free_clone
--;
1329 spin_unlock(&sdp
->sd_rindex_spin
);
1335 * gfs2_free_data - free a contiguous run of data block(s)
1336 * @ip: the inode these blocks are being freed from
1337 * @bstart: first block of a run of contiguous blocks
1338 * @blen: the length of the block run
1342 void gfs2_free_data(struct gfs2_inode
*ip
, uint64_t bstart
, uint32_t blen
)
1344 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1345 struct gfs2_rgrpd
*rgd
;
1347 rgd
= rgblk_free(sdp
, bstart
, blen
, GFS2_BLKST_FREE
);
1351 rgd
->rd_rg
.rg_free
+= blen
;
1353 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1354 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1356 gfs2_trans_add_rg(rgd
);
1358 gfs2_statfs_change(sdp
, 0, +blen
, 0);
1359 gfs2_quota_change(ip
, -(int64_t)blen
,
1360 ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1364 * gfs2_free_meta - free a contiguous run of data block(s)
1365 * @ip: the inode these blocks are being freed from
1366 * @bstart: first block of a run of contiguous blocks
1367 * @blen: the length of the block run
1371 void gfs2_free_meta(struct gfs2_inode
*ip
, uint64_t bstart
, uint32_t blen
)
1373 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1374 struct gfs2_rgrpd
*rgd
;
1376 rgd
= rgblk_free(sdp
, bstart
, blen
, GFS2_BLKST_FREE
);
1380 rgd
->rd_rg
.rg_free
+= blen
;
1382 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1383 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1385 gfs2_trans_add_rg(rgd
);
1387 gfs2_statfs_change(sdp
, 0, +blen
, 0);
1388 gfs2_quota_change(ip
, -(int64_t)blen
,
1389 ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1390 gfs2_meta_wipe(ip
, bstart
, blen
);
1393 void gfs2_free_uninit_di(struct gfs2_rgrpd
*rgd
, uint64_t blkno
)
1395 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
1396 struct gfs2_rgrpd
*tmp_rgd
;
1398 tmp_rgd
= rgblk_free(sdp
, blkno
, 1, GFS2_BLKST_FREE
);
1401 gfs2_assert_withdraw(sdp
, rgd
== tmp_rgd
);
1403 if (!rgd
->rd_rg
.rg_dinodes
)
1404 gfs2_consist_rgrpd(rgd
);
1405 rgd
->rd_rg
.rg_dinodes
--;
1406 rgd
->rd_rg
.rg_free
++;
1408 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1409 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1411 gfs2_statfs_change(sdp
, 0, +1, -1);
1412 gfs2_trans_add_rg(rgd
);
1416 * gfs2_free_uninit_di - free a dinode block
1417 * @rgd: the resource group that contains the dinode
1422 void gfs2_free_di(struct gfs2_rgrpd
*rgd
, struct gfs2_inode
*ip
)
1424 gfs2_free_uninit_di(rgd
, ip
->i_num
.no_addr
);
1425 gfs2_quota_change(ip
, -1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1426 gfs2_meta_wipe(ip
, ip
->i_num
.no_addr
, 1);
1430 * gfs2_rlist_add - add a RG to a list of RGs
1431 * @sdp: the filesystem
1432 * @rlist: the list of resource groups
1435 * Figure out what RG a block belongs to and add that RG to the list
1437 * FIXME: Don't use NOFAIL
1441 void gfs2_rlist_add(struct gfs2_sbd
*sdp
, struct gfs2_rgrp_list
*rlist
,
1444 struct gfs2_rgrpd
*rgd
;
1445 struct gfs2_rgrpd
**tmp
;
1446 unsigned int new_space
;
1449 if (gfs2_assert_warn(sdp
, !rlist
->rl_ghs
))
1452 rgd
= gfs2_blk2rgrpd(sdp
, block
);
1454 if (gfs2_consist(sdp
))
1455 fs_err(sdp
, "block = %llu\n", (unsigned long long)block
);
1459 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1460 if (rlist
->rl_rgd
[x
] == rgd
)
1463 if (rlist
->rl_rgrps
== rlist
->rl_space
) {
1464 new_space
= rlist
->rl_space
+ 10;
1466 tmp
= kcalloc(new_space
, sizeof(struct gfs2_rgrpd
*),
1467 GFP_KERNEL
| __GFP_NOFAIL
);
1469 if (rlist
->rl_rgd
) {
1470 memcpy(tmp
, rlist
->rl_rgd
,
1471 rlist
->rl_space
* sizeof(struct gfs2_rgrpd
*));
1472 kfree(rlist
->rl_rgd
);
1475 rlist
->rl_space
= new_space
;
1476 rlist
->rl_rgd
= tmp
;
1479 rlist
->rl_rgd
[rlist
->rl_rgrps
++] = rgd
;
1483 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1484 * and initialize an array of glock holders for them
1485 * @rlist: the list of resource groups
1486 * @state: the lock state to acquire the RG lock in
1487 * @flags: the modifier flags for the holder structures
1489 * FIXME: Don't use NOFAIL
1493 void gfs2_rlist_alloc(struct gfs2_rgrp_list
*rlist
, unsigned int state
,
1498 rlist
->rl_ghs
= kcalloc(rlist
->rl_rgrps
, sizeof(struct gfs2_holder
),
1499 GFP_KERNEL
| __GFP_NOFAIL
);
1500 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1501 gfs2_holder_init(rlist
->rl_rgd
[x
]->rd_gl
,
1507 * gfs2_rlist_free - free a resource group list
1508 * @list: the list of resource groups
1512 void gfs2_rlist_free(struct gfs2_rgrp_list
*rlist
)
1516 kfree(rlist
->rl_rgd
);
1518 if (rlist
->rl_ghs
) {
1519 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1520 gfs2_holder_uninit(&rlist
->rl_ghs
[x
]);
1521 kfree(rlist
->rl_ghs
);