2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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>
15 #include <asm/semaphore.h>
30 * gfs2_rgrp_verify - Verify that a resource group is consistent
31 * @sdp: the filesystem
36 void gfs2_rgrp_verify(struct gfs2_rgrpd
*rgd
)
38 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
39 struct gfs2_bitmap
*bi
= NULL
;
40 uint32_t length
= rgd
->rd_ri
.ri_length
;
41 uint32_t count
[4], tmp
;
44 memset(count
, 0, 4 * sizeof(uint32_t));
46 /* Count # blocks in each of 4 possible allocation states */
47 for (buf
= 0; buf
< length
; buf
++) {
48 bi
= rgd
->rd_bits
+ buf
;
49 for (x
= 0; x
< 4; x
++)
50 count
[x
] += gfs2_bitcount(rgd
,
56 if (count
[0] != rgd
->rd_rg
.rg_free
) {
57 if (gfs2_consist_rgrpd(rgd
))
58 fs_err(sdp
, "free data mismatch: %u != %u\n",
59 count
[0], rgd
->rd_rg
.rg_free
);
63 tmp
= rgd
->rd_ri
.ri_data
-
65 rgd
->rd_rg
.rg_dinodes
;
66 if (count
[1] != tmp
) {
67 if (gfs2_consist_rgrpd(rgd
))
68 fs_err(sdp
, "used data mismatch: %u != %u\n",
74 if (gfs2_consist_rgrpd(rgd
))
75 fs_err(sdp
, "free metadata mismatch: %u != 0\n",
80 if (count
[3] != rgd
->rd_rg
.rg_dinodes
) {
81 if (gfs2_consist_rgrpd(rgd
))
82 fs_err(sdp
, "used metadata mismatch: %u != %u\n",
83 count
[3], rgd
->rd_rg
.rg_dinodes
);
88 static inline int rgrp_contains_block(struct gfs2_rindex
*ri
, uint64_t block
)
90 uint64_t first
= ri
->ri_data0
;
91 uint64_t last
= first
+ ri
->ri_data
;
92 return !!(first
<= block
&& block
< last
);
96 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
97 * @sdp: The GFS2 superblock
98 * @n: The data block number
100 * Returns: The resource group, or NULL if not found
103 struct gfs2_rgrpd
*gfs2_blk2rgrpd(struct gfs2_sbd
*sdp
, uint64_t blk
)
105 struct gfs2_rgrpd
*rgd
;
107 spin_lock(&sdp
->sd_rindex_spin
);
109 list_for_each_entry(rgd
, &sdp
->sd_rindex_mru_list
, rd_list_mru
) {
110 if (rgrp_contains_block(&rgd
->rd_ri
, blk
)) {
111 list_move(&rgd
->rd_list_mru
, &sdp
->sd_rindex_mru_list
);
112 spin_unlock(&sdp
->sd_rindex_spin
);
117 spin_unlock(&sdp
->sd_rindex_spin
);
123 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
124 * @sdp: The GFS2 superblock
126 * Returns: The first rgrp in the filesystem
129 struct gfs2_rgrpd
*gfs2_rgrpd_get_first(struct gfs2_sbd
*sdp
)
131 gfs2_assert(sdp
, !list_empty(&sdp
->sd_rindex_list
));
132 return list_entry(sdp
->sd_rindex_list
.next
, struct gfs2_rgrpd
, rd_list
);
136 * gfs2_rgrpd_get_next - get the next RG
139 * Returns: The next rgrp
142 struct gfs2_rgrpd
*gfs2_rgrpd_get_next(struct gfs2_rgrpd
*rgd
)
144 if (rgd
->rd_list
.next
== &rgd
->rd_sbd
->sd_rindex_list
)
146 return list_entry(rgd
->rd_list
.next
, struct gfs2_rgrpd
, rd_list
);
149 static void clear_rgrpdi(struct gfs2_sbd
*sdp
)
151 struct list_head
*head
;
152 struct gfs2_rgrpd
*rgd
;
153 struct gfs2_glock
*gl
;
155 spin_lock(&sdp
->sd_rindex_spin
);
156 sdp
->sd_rindex_forward
= NULL
;
157 head
= &sdp
->sd_rindex_recent_list
;
158 while (!list_empty(head
)) {
159 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_recent
);
160 list_del(&rgd
->rd_recent
);
162 spin_unlock(&sdp
->sd_rindex_spin
);
164 head
= &sdp
->sd_rindex_list
;
165 while (!list_empty(head
)) {
166 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_list
);
169 list_del(&rgd
->rd_list
);
170 list_del(&rgd
->rd_list_mru
);
173 set_gl2rgd(gl
, NULL
);
182 void gfs2_clear_rgrpd(struct gfs2_sbd
*sdp
)
184 down(&sdp
->sd_rindex_mutex
);
186 up(&sdp
->sd_rindex_mutex
);
190 * gfs2_compute_bitstructs - Compute the bitmap sizes
191 * @rgd: The resource group descriptor
193 * Calculates bitmap descriptors, one for each block that contains bitmap data
198 static int compute_bitstructs(struct gfs2_rgrpd
*rgd
)
200 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
201 struct gfs2_bitmap
*bi
;
202 uint32_t length
= rgd
->rd_ri
.ri_length
; /* # blocks in hdr & bitmap */
203 uint32_t bytes_left
, bytes
;
206 rgd
->rd_bits
= kcalloc(length
, sizeof(struct gfs2_bitmap
), GFP_KERNEL
);
210 bytes_left
= rgd
->rd_ri
.ri_bitbytes
;
212 for (x
= 0; x
< length
; x
++) {
213 bi
= rgd
->rd_bits
+ x
;
215 /* small rgrp; bitmap stored completely in header block */
218 bi
->bi_offset
= sizeof(struct gfs2_rgrp
);
223 bytes
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_rgrp
);
224 bi
->bi_offset
= sizeof(struct gfs2_rgrp
);
228 } else if (x
+ 1 == length
) {
230 bi
->bi_offset
= sizeof(struct gfs2_meta_header
);
231 bi
->bi_start
= rgd
->rd_ri
.ri_bitbytes
- bytes_left
;
235 bytes
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_meta_header
);
236 bi
->bi_offset
= sizeof(struct gfs2_meta_header
);
237 bi
->bi_start
= rgd
->rd_ri
.ri_bitbytes
- bytes_left
;
245 gfs2_consist_rgrpd(rgd
);
248 bi
= rgd
->rd_bits
+ (length
- 1);
249 if ((bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
!= rgd
->rd_ri
.ri_data
) {
250 if (gfs2_consist_rgrpd(rgd
)) {
251 gfs2_rindex_print(&rgd
->rd_ri
);
252 fs_err(sdp
, "start=%u len=%u offset=%u\n",
253 bi
->bi_start
, bi
->bi_len
, bi
->bi_offset
);
262 * gfs2_ri_update - Pull in a new resource index from the disk
263 * @gl: The glock covering the rindex inode
265 * Returns: 0 on successful update, error code otherwise
268 static int gfs2_ri_update(struct gfs2_inode
*ip
)
270 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
271 struct gfs2_rgrpd
*rgd
;
272 char buf
[sizeof(struct gfs2_rindex
)];
273 uint64_t junk
= ip
->i_di
.di_size
;
276 if (do_div(junk
, sizeof(struct gfs2_rindex
))) {
277 gfs2_consist_inode(ip
);
283 for (sdp
->sd_rgrps
= 0;; sdp
->sd_rgrps
++) {
284 error
= gfs2_jdata_read_mem(ip
, buf
,
286 sizeof(struct gfs2_rindex
),
287 sizeof(struct gfs2_rindex
));
290 if (error
!= sizeof(struct gfs2_rindex
)) {
296 rgd
= kzalloc(sizeof(struct gfs2_rgrpd
), GFP_KERNEL
);
301 init_MUTEX(&rgd
->rd_mutex
);
302 lops_init_le(&rgd
->rd_le
, &gfs2_rg_lops
);
305 list_add_tail(&rgd
->rd_list
, &sdp
->sd_rindex_list
);
306 list_add_tail(&rgd
->rd_list_mru
, &sdp
->sd_rindex_mru_list
);
308 gfs2_rindex_in(&rgd
->rd_ri
, buf
);
310 error
= compute_bitstructs(rgd
);
314 error
= gfs2_glock_get(sdp
, rgd
->rd_ri
.ri_addr
,
315 &gfs2_rgrp_glops
, CREATE
, &rgd
->rd_gl
);
319 set_gl2rgd(rgd
->rd_gl
, rgd
);
320 rgd
->rd_rg_vn
= rgd
->rd_gl
->gl_vn
- 1;
323 sdp
->sd_rindex_vn
= ip
->i_gl
->gl_vn
;
334 * gfs2_rindex_hold - Grab a lock on the rindex
335 * @sdp: The GFS2 superblock
336 * @ri_gh: the glock holder
338 * We grab a lock on the rindex inode to make sure that it doesn't
339 * change whilst we are performing an operation. We keep this lock
340 * for quite long periods of time compared to other locks. This
341 * doesn't matter, since it is shared and it is very, very rarely
342 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
344 * This makes sure that we're using the latest copy of the resource index
345 * special file, which might have been updated if someone expanded the
346 * filesystem (via gfs2_grow utility), which adds new resource groups.
348 * Returns: 0 on success, error code otherwise
351 int gfs2_rindex_hold(struct gfs2_sbd
*sdp
, struct gfs2_holder
*ri_gh
)
353 struct gfs2_inode
*ip
= sdp
->sd_rindex
;
354 struct gfs2_glock
*gl
= ip
->i_gl
;
357 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, 0, ri_gh
);
361 /* Read new copy from disk if we don't have the latest */
362 if (sdp
->sd_rindex_vn
!= gl
->gl_vn
) {
363 down(&sdp
->sd_rindex_mutex
);
364 if (sdp
->sd_rindex_vn
!= gl
->gl_vn
) {
365 error
= gfs2_ri_update(ip
);
367 gfs2_glock_dq_uninit(ri_gh
);
369 up(&sdp
->sd_rindex_mutex
);
376 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
377 * @rgd: the struct gfs2_rgrpd describing the RG to read in
379 * Read in all of a Resource Group's header and bitmap blocks.
380 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
385 int gfs2_rgrp_bh_get(struct gfs2_rgrpd
*rgd
)
387 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
388 struct gfs2_glock
*gl
= rgd
->rd_gl
;
389 unsigned int length
= rgd
->rd_ri
.ri_length
;
390 struct gfs2_bitmap
*bi
;
394 down(&rgd
->rd_mutex
);
396 spin_lock(&sdp
->sd_rindex_spin
);
397 if (rgd
->rd_bh_count
) {
399 spin_unlock(&sdp
->sd_rindex_spin
);
403 spin_unlock(&sdp
->sd_rindex_spin
);
405 for (x
= 0; x
< length
; x
++) {
406 bi
= rgd
->rd_bits
+ x
;
407 error
= gfs2_meta_read(gl
, rgd
->rd_ri
.ri_addr
+ x
, DIO_START
,
413 for (y
= length
; y
--;) {
414 bi
= rgd
->rd_bits
+ y
;
415 error
= gfs2_meta_reread(sdp
, bi
->bi_bh
, DIO_WAIT
);
418 if (gfs2_metatype_check(sdp
, bi
->bi_bh
,
419 (y
) ? GFS2_METATYPE_RB
:
426 if (rgd
->rd_rg_vn
!= gl
->gl_vn
) {
427 gfs2_rgrp_in(&rgd
->rd_rg
, (rgd
->rd_bits
[0].bi_bh
)->b_data
);
428 rgd
->rd_rg_vn
= gl
->gl_vn
;
431 spin_lock(&sdp
->sd_rindex_spin
);
432 rgd
->rd_free_clone
= rgd
->rd_rg
.rg_free
;
434 spin_unlock(&sdp
->sd_rindex_spin
);
442 bi
= rgd
->rd_bits
+ x
;
445 gfs2_assert_warn(sdp
, !bi
->bi_clone
);
452 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd
*rgd
)
454 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
456 spin_lock(&sdp
->sd_rindex_spin
);
457 gfs2_assert_warn(rgd
->rd_sbd
, rgd
->rd_bh_count
);
459 spin_unlock(&sdp
->sd_rindex_spin
);
463 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
464 * @rgd: the struct gfs2_rgrpd describing the RG to read in
468 void gfs2_rgrp_bh_put(struct gfs2_rgrpd
*rgd
)
470 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
471 int x
, length
= rgd
->rd_ri
.ri_length
;
473 spin_lock(&sdp
->sd_rindex_spin
);
474 gfs2_assert_warn(rgd
->rd_sbd
, rgd
->rd_bh_count
);
475 if (--rgd
->rd_bh_count
) {
476 spin_unlock(&sdp
->sd_rindex_spin
);
480 for (x
= 0; x
< length
; x
++) {
481 struct gfs2_bitmap
*bi
= rgd
->rd_bits
+ x
;
488 spin_unlock(&sdp
->sd_rindex_spin
);
491 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd
*rgd
)
493 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
494 unsigned int length
= rgd
->rd_ri
.ri_length
;
497 for (x
= 0; x
< length
; x
++) {
498 struct gfs2_bitmap
*bi
= rgd
->rd_bits
+ x
;
501 memcpy(bi
->bi_clone
+ bi
->bi_offset
,
502 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
506 spin_lock(&sdp
->sd_rindex_spin
);
507 rgd
->rd_free_clone
= rgd
->rd_rg
.rg_free
;
508 spin_unlock(&sdp
->sd_rindex_spin
);
512 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
513 * @ip: the incore GFS2 inode structure
515 * Returns: the struct gfs2_alloc
518 struct gfs2_alloc
*gfs2_alloc_get(struct gfs2_inode
*ip
)
520 struct gfs2_alloc
*al
= &ip
->i_alloc
;
522 /* FIXME: Should assert that the correct locks are held here... */
523 memset(al
, 0, sizeof(*al
));
528 * gfs2_alloc_put - throw away the struct gfs2_alloc for an inode
533 void gfs2_alloc_put(struct gfs2_inode
*ip
)
539 * try_rgrp_fit - See if a given reservation will fit in a given RG
541 * @al: the struct gfs2_alloc structure describing the reservation
543 * If there's room for the requested blocks to be allocated from the RG:
544 * Sets the $al_reserved_data field in @al.
545 * Sets the $al_reserved_meta field in @al.
546 * Sets the $al_rgd field in @al.
548 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
551 static int try_rgrp_fit(struct gfs2_rgrpd
*rgd
, struct gfs2_alloc
*al
)
553 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
556 spin_lock(&sdp
->sd_rindex_spin
);
557 if (rgd
->rd_free_clone
>= al
->al_requested
) {
561 spin_unlock(&sdp
->sd_rindex_spin
);
567 * recent_rgrp_first - get first RG from "recent" list
568 * @sdp: The GFS2 superblock
569 * @rglast: address of the rgrp used last
571 * Returns: The first rgrp in the recent list
574 static struct gfs2_rgrpd
*recent_rgrp_first(struct gfs2_sbd
*sdp
,
577 struct gfs2_rgrpd
*rgd
= NULL
;
579 spin_lock(&sdp
->sd_rindex_spin
);
581 if (list_empty(&sdp
->sd_rindex_recent_list
))
587 list_for_each_entry(rgd
, &sdp
->sd_rindex_recent_list
, rd_recent
) {
588 if (rgd
->rd_ri
.ri_addr
== rglast
)
593 rgd
= list_entry(sdp
->sd_rindex_recent_list
.next
, struct gfs2_rgrpd
,
597 spin_unlock(&sdp
->sd_rindex_spin
);
603 * recent_rgrp_next - get next RG from "recent" list
604 * @cur_rgd: current rgrp
607 * Returns: The next rgrp in the recent list
610 static struct gfs2_rgrpd
*recent_rgrp_next(struct gfs2_rgrpd
*cur_rgd
,
613 struct gfs2_sbd
*sdp
= cur_rgd
->rd_sbd
;
614 struct list_head
*head
;
615 struct gfs2_rgrpd
*rgd
;
617 spin_lock(&sdp
->sd_rindex_spin
);
619 head
= &sdp
->sd_rindex_recent_list
;
621 list_for_each_entry(rgd
, head
, rd_recent
) {
622 if (rgd
== cur_rgd
) {
623 if (cur_rgd
->rd_recent
.next
!= head
)
624 rgd
= list_entry(cur_rgd
->rd_recent
.next
,
625 struct gfs2_rgrpd
, rd_recent
);
630 list_del(&cur_rgd
->rd_recent
);
637 if (!list_empty(head
))
638 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_recent
);
641 spin_unlock(&sdp
->sd_rindex_spin
);
647 * recent_rgrp_add - add an RG to tail of "recent" list
648 * @new_rgd: The rgrp to add
652 static void recent_rgrp_add(struct gfs2_rgrpd
*new_rgd
)
654 struct gfs2_sbd
*sdp
= new_rgd
->rd_sbd
;
655 struct gfs2_rgrpd
*rgd
;
656 unsigned int count
= 0;
657 unsigned int max
= sdp
->sd_rgrps
/ gfs2_jindex_size(sdp
);
659 spin_lock(&sdp
->sd_rindex_spin
);
661 list_for_each_entry(rgd
, &sdp
->sd_rindex_recent_list
, rd_recent
) {
668 list_add_tail(&new_rgd
->rd_recent
, &sdp
->sd_rindex_recent_list
);
671 spin_unlock(&sdp
->sd_rindex_spin
);
675 * forward_rgrp_get - get an rgrp to try next from full list
676 * @sdp: The GFS2 superblock
678 * Returns: The rgrp to try next
681 static struct gfs2_rgrpd
*forward_rgrp_get(struct gfs2_sbd
*sdp
)
683 struct gfs2_rgrpd
*rgd
;
684 unsigned int journals
= gfs2_jindex_size(sdp
);
685 unsigned int rg
= 0, x
;
687 spin_lock(&sdp
->sd_rindex_spin
);
689 rgd
= sdp
->sd_rindex_forward
;
691 if (sdp
->sd_rgrps
>= journals
)
692 rg
= sdp
->sd_rgrps
* sdp
->sd_jdesc
->jd_jid
/ journals
;
694 for (x
= 0, rgd
= gfs2_rgrpd_get_first(sdp
);
696 x
++, rgd
= gfs2_rgrpd_get_next(rgd
))
699 sdp
->sd_rindex_forward
= rgd
;
702 spin_unlock(&sdp
->sd_rindex_spin
);
708 * forward_rgrp_set - set the forward rgrp pointer
709 * @sdp: the filesystem
710 * @rgd: The new forward rgrp
714 static void forward_rgrp_set(struct gfs2_sbd
*sdp
, struct gfs2_rgrpd
*rgd
)
716 spin_lock(&sdp
->sd_rindex_spin
);
717 sdp
->sd_rindex_forward
= rgd
;
718 spin_unlock(&sdp
->sd_rindex_spin
);
722 * get_local_rgrp - Choose and lock a rgrp for allocation
723 * @ip: the inode to reserve space for
724 * @rgp: the chosen and locked rgrp
726 * Try to acquire rgrp in way which avoids contending with others.
731 static int get_local_rgrp(struct gfs2_inode
*ip
)
733 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
734 struct gfs2_rgrpd
*rgd
, *begin
= NULL
;
735 struct gfs2_alloc
*al
= &ip
->i_alloc
;
736 int flags
= LM_FLAG_TRY
;
741 /* Try recently successful rgrps */
743 rgd
= recent_rgrp_first(sdp
, ip
->i_last_rg_alloc
);
746 error
= gfs2_glock_nq_init(rgd
->rd_gl
,
747 LM_ST_EXCLUSIVE
, LM_FLAG_TRY
,
751 if (try_rgrp_fit(rgd
, al
))
753 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
754 rgd
= recent_rgrp_next(rgd
, 1);
758 rgd
= recent_rgrp_next(rgd
, 0);
766 /* Go through full list of rgrps */
768 begin
= rgd
= forward_rgrp_get(sdp
);
771 error
= gfs2_glock_nq_init(rgd
->rd_gl
,
772 LM_ST_EXCLUSIVE
, flags
,
776 if (try_rgrp_fit(rgd
, al
))
778 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
789 rgd
= gfs2_rgrpd_get_next(rgd
);
791 rgd
= gfs2_rgrpd_get_first(sdp
);
794 if (++loops
>= 2 || !skipped
)
801 ip
->i_last_rg_alloc
= rgd
->rd_ri
.ri_addr
;
804 recent_rgrp_add(rgd
);
805 rgd
= gfs2_rgrpd_get_next(rgd
);
807 rgd
= gfs2_rgrpd_get_first(sdp
);
808 forward_rgrp_set(sdp
, rgd
);
815 * gfs2_inplace_reserve_i - Reserve space in the filesystem
816 * @ip: the inode to reserve space for
821 int gfs2_inplace_reserve_i(struct gfs2_inode
*ip
, char *file
, unsigned int line
)
823 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
824 struct gfs2_alloc
*al
= &ip
->i_alloc
;
827 if (gfs2_assert_warn(sdp
, al
->al_requested
))
830 error
= gfs2_rindex_hold(sdp
, &al
->al_ri_gh
);
834 error
= get_local_rgrp(ip
);
836 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
847 * gfs2_inplace_release - release an inplace reservation
848 * @ip: the inode the reservation was taken out on
850 * Release a reservation made by gfs2_inplace_reserve().
853 void gfs2_inplace_release(struct gfs2_inode
*ip
)
855 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
856 struct gfs2_alloc
*al
= &ip
->i_alloc
;
858 if (gfs2_assert_warn(sdp
, al
->al_alloced
<= al
->al_requested
) == -1)
859 fs_warn(sdp
, "al_alloced = %u, al_requested = %u "
860 "al_file = %s, al_line = %u\n",
861 al
->al_alloced
, al
->al_requested
, al
->al_file
,
865 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
866 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
870 * gfs2_get_block_type - Check a block in a RG is of given type
871 * @rgd: the resource group holding the block
872 * @block: the block number
874 * Returns: The block type (GFS2_BLKST_*)
877 unsigned char gfs2_get_block_type(struct gfs2_rgrpd
*rgd
, uint64_t block
)
879 struct gfs2_bitmap
*bi
= NULL
;
880 uint32_t length
, rgrp_block
, buf_block
;
884 length
= rgd
->rd_ri
.ri_length
;
885 rgrp_block
= block
- rgd
->rd_ri
.ri_data0
;
887 for (buf
= 0; buf
< length
; buf
++) {
888 bi
= rgd
->rd_bits
+ buf
;
889 if (rgrp_block
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
893 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
894 buf_block
= rgrp_block
- bi
->bi_start
* GFS2_NBBY
;
896 type
= gfs2_testbit(rgd
,
897 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
898 bi
->bi_len
, buf_block
);
904 * rgblk_search - find a block in @old_state, change allocation
905 * state to @new_state
906 * @rgd: the resource group descriptor
907 * @goal: the goal block within the RG (start here to search for avail block)
908 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
909 * @new_state: GFS2_BLKST_XXX the after-allocation block state
911 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
912 * Add the found bitmap buffer to the transaction.
913 * Set the found bits to @new_state to change block's allocation state.
915 * This function never fails, because we wouldn't call it unless we
916 * know (from reservation results, etc.) that a block is available.
918 * Scope of @goal and returned block is just within rgrp, not the whole
921 * Returns: the block number allocated
924 static uint32_t rgblk_search(struct gfs2_rgrpd
*rgd
, uint32_t goal
,
925 unsigned char old_state
, unsigned char new_state
)
927 struct gfs2_bitmap
*bi
= NULL
;
928 uint32_t length
= rgd
->rd_ri
.ri_length
;
932 /* Find bitmap block that contains bits for goal block */
933 for (buf
= 0; buf
< length
; buf
++) {
934 bi
= rgd
->rd_bits
+ buf
;
935 if (goal
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
939 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
941 /* Convert scope of "goal" from rgrp-wide to within found bit block */
942 goal
-= bi
->bi_start
* GFS2_NBBY
;
944 /* Search (up to entire) bitmap in this rgrp for allocatable block.
945 "x <= length", instead of "x < length", because we typically start
946 the search in the middle of a bit block, but if we can't find an
947 allocatable block anywhere else, we want to be able wrap around and
948 search in the first part of our first-searched bit block. */
949 for (x
= 0; x
<= length
; x
++) {
951 blk
= gfs2_bitfit(rgd
,
952 bi
->bi_clone
+ bi
->bi_offset
,
953 bi
->bi_len
, goal
, old_state
);
955 blk
= gfs2_bitfit(rgd
,
956 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
957 bi
->bi_len
, goal
, old_state
);
958 if (blk
!= BFITNOENT
)
961 /* Try next bitmap block (wrap back to rgrp header if at end) */
962 buf
= (buf
+ 1) % length
;
963 bi
= rgd
->rd_bits
+ buf
;
967 if (gfs2_assert_withdraw(rgd
->rd_sbd
, x
<= length
))
970 gfs2_trans_add_bh(rgd
->rd_gl
, bi
->bi_bh
, 1);
972 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
973 bi
->bi_len
, blk
, new_state
);
976 bi
->bi_clone
+ bi
->bi_offset
,
977 bi
->bi_len
, blk
, new_state
);
979 return bi
->bi_start
* GFS2_NBBY
+ blk
;
983 * rgblk_free - Change alloc state of given block(s)
984 * @sdp: the filesystem
985 * @bstart: the start of a run of blocks to free
986 * @blen: the length of the block run (all must lie within ONE RG!)
987 * @new_state: GFS2_BLKST_XXX the after-allocation block state
989 * Returns: Resource group containing the block(s)
992 static struct gfs2_rgrpd
*rgblk_free(struct gfs2_sbd
*sdp
, uint64_t bstart
,
993 uint32_t blen
, unsigned char new_state
)
995 struct gfs2_rgrpd
*rgd
;
996 struct gfs2_bitmap
*bi
= NULL
;
997 uint32_t length
, rgrp_blk
, buf_blk
;
1000 rgd
= gfs2_blk2rgrpd(sdp
, bstart
);
1002 if (gfs2_consist(sdp
))
1003 fs_err(sdp
, "block = %llu\n", bstart
);
1007 length
= rgd
->rd_ri
.ri_length
;
1009 rgrp_blk
= bstart
- rgd
->rd_ri
.ri_data0
;
1012 for (buf
= 0; buf
< length
; buf
++) {
1013 bi
= rgd
->rd_bits
+ buf
;
1014 if (rgrp_blk
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
1018 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
1020 buf_blk
= rgrp_blk
- bi
->bi_start
* GFS2_NBBY
;
1023 if (!bi
->bi_clone
) {
1024 bi
->bi_clone
= kmalloc(bi
->bi_bh
->b_size
,
1025 GFP_KERNEL
| __GFP_NOFAIL
);
1026 memcpy(bi
->bi_clone
+ bi
->bi_offset
,
1027 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1030 gfs2_trans_add_bh(rgd
->rd_gl
, bi
->bi_bh
, 1);
1032 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1033 bi
->bi_len
, buf_blk
, new_state
);
1040 * gfs2_alloc_data - Allocate a data block
1041 * @ip: the inode to allocate the data block for
1043 * Returns: the allocated block
1046 uint64_t gfs2_alloc_data(struct gfs2_inode
*ip
)
1048 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1049 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1050 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1054 if (rgrp_contains_block(&rgd
->rd_ri
, ip
->i_di
.di_goal_data
))
1055 goal
= ip
->i_di
.di_goal_data
- rgd
->rd_ri
.ri_data0
;
1057 goal
= rgd
->rd_last_alloc_data
;
1059 blk
= rgblk_search(rgd
, goal
,
1060 GFS2_BLKST_FREE
, GFS2_BLKST_USED
);
1061 rgd
->rd_last_alloc_data
= blk
;
1063 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1064 ip
->i_di
.di_goal_data
= block
;
1066 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1067 rgd
->rd_rg
.rg_free
--;
1069 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1070 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1074 gfs2_statfs_change(sdp
, 0, -1, 0);
1075 gfs2_quota_change(ip
, +1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1077 spin_lock(&sdp
->sd_rindex_spin
);
1078 rgd
->rd_free_clone
--;
1079 spin_unlock(&sdp
->sd_rindex_spin
);
1085 * gfs2_alloc_meta - Allocate a metadata block
1086 * @ip: the inode to allocate the metadata block for
1088 * Returns: the allocated block
1091 uint64_t gfs2_alloc_meta(struct gfs2_inode
*ip
)
1093 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1094 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1095 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1099 if (rgrp_contains_block(&rgd
->rd_ri
, ip
->i_di
.di_goal_meta
))
1100 goal
= ip
->i_di
.di_goal_meta
- rgd
->rd_ri
.ri_data0
;
1102 goal
= rgd
->rd_last_alloc_meta
;
1104 blk
= rgblk_search(rgd
, goal
,
1105 GFS2_BLKST_FREE
, GFS2_BLKST_USED
);
1106 rgd
->rd_last_alloc_meta
= blk
;
1108 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1109 ip
->i_di
.di_goal_meta
= block
;
1111 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1112 rgd
->rd_rg
.rg_free
--;
1114 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1115 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1119 gfs2_statfs_change(sdp
, 0, -1, 0);
1120 gfs2_quota_change(ip
, +1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1121 gfs2_trans_add_unrevoke(sdp
, block
);
1123 spin_lock(&sdp
->sd_rindex_spin
);
1124 rgd
->rd_free_clone
--;
1125 spin_unlock(&sdp
->sd_rindex_spin
);
1131 * gfs2_alloc_di - Allocate a dinode
1132 * @dip: the directory that the inode is going in
1134 * Returns: the block allocated
1137 uint64_t gfs2_alloc_di(struct gfs2_inode
*dip
)
1139 struct gfs2_sbd
*sdp
= dip
->i_sbd
;
1140 struct gfs2_alloc
*al
= &dip
->i_alloc
;
1141 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1145 blk
= rgblk_search(rgd
, rgd
->rd_last_alloc_meta
,
1146 GFS2_BLKST_FREE
, GFS2_BLKST_DINODE
);
1148 rgd
->rd_last_alloc_meta
= blk
;
1150 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1152 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1153 rgd
->rd_rg
.rg_free
--;
1154 rgd
->rd_rg
.rg_dinodes
++;
1156 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1157 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1161 gfs2_statfs_change(sdp
, 0, -1, +1);
1162 gfs2_trans_add_unrevoke(sdp
, block
);
1164 spin_lock(&sdp
->sd_rindex_spin
);
1165 rgd
->rd_free_clone
--;
1166 spin_unlock(&sdp
->sd_rindex_spin
);
1172 * gfs2_free_data - free a contiguous run of data block(s)
1173 * @ip: the inode these blocks are being freed from
1174 * @bstart: first block of a run of contiguous blocks
1175 * @blen: the length of the block run
1179 void gfs2_free_data(struct gfs2_inode
*ip
, uint64_t bstart
, uint32_t blen
)
1181 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1182 struct gfs2_rgrpd
*rgd
;
1184 rgd
= rgblk_free(sdp
, bstart
, blen
, GFS2_BLKST_FREE
);
1188 rgd
->rd_rg
.rg_free
+= blen
;
1190 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1191 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1193 gfs2_trans_add_rg(rgd
);
1195 gfs2_statfs_change(sdp
, 0, +blen
, 0);
1196 gfs2_quota_change(ip
, -(int64_t)blen
,
1197 ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1201 * gfs2_free_meta - free a contiguous run of data block(s)
1202 * @ip: the inode these blocks are being freed from
1203 * @bstart: first block of a run of contiguous blocks
1204 * @blen: the length of the block run
1208 void gfs2_free_meta(struct gfs2_inode
*ip
, uint64_t bstart
, uint32_t blen
)
1210 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1211 struct gfs2_rgrpd
*rgd
;
1213 rgd
= rgblk_free(sdp
, bstart
, blen
, GFS2_BLKST_FREE
);
1217 rgd
->rd_rg
.rg_free
+= blen
;
1219 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1220 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1222 gfs2_trans_add_rg(rgd
);
1224 gfs2_statfs_change(sdp
, 0, +blen
, 0);
1225 gfs2_quota_change(ip
, -(int64_t)blen
,
1226 ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1227 gfs2_meta_wipe(ip
, bstart
, blen
);
1230 void gfs2_free_uninit_di(struct gfs2_rgrpd
*rgd
, uint64_t blkno
)
1232 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
1233 struct gfs2_rgrpd
*tmp_rgd
;
1235 tmp_rgd
= rgblk_free(sdp
, blkno
, 1, GFS2_BLKST_FREE
);
1238 gfs2_assert_withdraw(sdp
, rgd
== tmp_rgd
);
1240 if (!rgd
->rd_rg
.rg_dinodes
)
1241 gfs2_consist_rgrpd(rgd
);
1242 rgd
->rd_rg
.rg_dinodes
--;
1243 rgd
->rd_rg
.rg_free
++;
1245 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1246 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1248 gfs2_statfs_change(sdp
, 0, +1, -1);
1249 gfs2_trans_add_rg(rgd
);
1253 * gfs2_free_uninit_di - free a dinode block
1254 * @rgd: the resource group that contains the dinode
1259 void gfs2_free_di(struct gfs2_rgrpd
*rgd
, struct gfs2_inode
*ip
)
1261 gfs2_free_uninit_di(rgd
, ip
->i_num
.no_addr
);
1262 gfs2_quota_change(ip
, -1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1263 gfs2_meta_wipe(ip
, ip
->i_num
.no_addr
, 1);
1267 * gfs2_rlist_add - add a RG to a list of RGs
1268 * @sdp: the filesystem
1269 * @rlist: the list of resource groups
1272 * Figure out what RG a block belongs to and add that RG to the list
1274 * FIXME: Don't use NOFAIL
1278 void gfs2_rlist_add(struct gfs2_sbd
*sdp
, struct gfs2_rgrp_list
*rlist
,
1281 struct gfs2_rgrpd
*rgd
;
1282 struct gfs2_rgrpd
**tmp
;
1283 unsigned int new_space
;
1286 if (gfs2_assert_warn(sdp
, !rlist
->rl_ghs
))
1289 rgd
= gfs2_blk2rgrpd(sdp
, block
);
1291 if (gfs2_consist(sdp
))
1292 fs_err(sdp
, "block = %llu\n", block
);
1296 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1297 if (rlist
->rl_rgd
[x
] == rgd
)
1300 if (rlist
->rl_rgrps
== rlist
->rl_space
) {
1301 new_space
= rlist
->rl_space
+ 10;
1303 tmp
= kcalloc(new_space
, sizeof(struct gfs2_rgrpd
*),
1304 GFP_KERNEL
| __GFP_NOFAIL
);
1306 if (rlist
->rl_rgd
) {
1307 memcpy(tmp
, rlist
->rl_rgd
,
1308 rlist
->rl_space
* sizeof(struct gfs2_rgrpd
*));
1309 kfree(rlist
->rl_rgd
);
1312 rlist
->rl_space
= new_space
;
1313 rlist
->rl_rgd
= tmp
;
1316 rlist
->rl_rgd
[rlist
->rl_rgrps
++] = rgd
;
1320 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1321 * and initialize an array of glock holders for them
1322 * @rlist: the list of resource groups
1323 * @state: the lock state to acquire the RG lock in
1324 * @flags: the modifier flags for the holder structures
1326 * FIXME: Don't use NOFAIL
1330 void gfs2_rlist_alloc(struct gfs2_rgrp_list
*rlist
, unsigned int state
,
1335 rlist
->rl_ghs
= kcalloc(rlist
->rl_rgrps
, sizeof(struct gfs2_holder
),
1336 GFP_KERNEL
| __GFP_NOFAIL
);
1337 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1338 gfs2_holder_init(rlist
->rl_rgd
[x
]->rd_gl
,
1344 * gfs2_rlist_free - free a resource group list
1345 * @list: the list of resource groups
1349 void gfs2_rlist_free(struct gfs2_rgrp_list
*rlist
)
1353 kfree(rlist
->rl_rgd
);
1355 if (rlist
->rl_ghs
) {
1356 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1357 gfs2_holder_uninit(&rlist
->rl_ghs
[x
]);
1358 kfree(rlist
->rl_ghs
);