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>
16 #include <linux/gfs2_ondisk.h>
17 #include <asm/semaphore.h>
20 #include "lm_interface.h"
35 * gfs2_rgrp_verify - Verify that a resource group is consistent
36 * @sdp: the filesystem
41 void gfs2_rgrp_verify(struct gfs2_rgrpd
*rgd
)
43 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
44 struct gfs2_bitmap
*bi
= NULL
;
45 uint32_t length
= rgd
->rd_ri
.ri_length
;
46 uint32_t count
[4], tmp
;
49 memset(count
, 0, 4 * sizeof(uint32_t));
51 /* Count # blocks in each of 4 possible allocation states */
52 for (buf
= 0; buf
< length
; buf
++) {
53 bi
= rgd
->rd_bits
+ buf
;
54 for (x
= 0; x
< 4; x
++)
55 count
[x
] += gfs2_bitcount(rgd
,
61 if (count
[0] != rgd
->rd_rg
.rg_free
) {
62 if (gfs2_consist_rgrpd(rgd
))
63 fs_err(sdp
, "free data mismatch: %u != %u\n",
64 count
[0], rgd
->rd_rg
.rg_free
);
68 tmp
= rgd
->rd_ri
.ri_data
-
70 rgd
->rd_rg
.rg_dinodes
;
71 if (count
[1] != tmp
) {
72 if (gfs2_consist_rgrpd(rgd
))
73 fs_err(sdp
, "used data mismatch: %u != %u\n",
79 if (gfs2_consist_rgrpd(rgd
))
80 fs_err(sdp
, "free metadata mismatch: %u != 0\n",
85 if (count
[3] != rgd
->rd_rg
.rg_dinodes
) {
86 if (gfs2_consist_rgrpd(rgd
))
87 fs_err(sdp
, "used metadata mismatch: %u != %u\n",
88 count
[3], rgd
->rd_rg
.rg_dinodes
);
93 static inline int rgrp_contains_block(struct gfs2_rindex
*ri
, uint64_t block
)
95 uint64_t first
= ri
->ri_data0
;
96 uint64_t last
= first
+ ri
->ri_data
;
97 return !!(first
<= block
&& block
< last
);
101 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
102 * @sdp: The GFS2 superblock
103 * @n: The data block number
105 * Returns: The resource group, or NULL if not found
108 struct gfs2_rgrpd
*gfs2_blk2rgrpd(struct gfs2_sbd
*sdp
, uint64_t blk
)
110 struct gfs2_rgrpd
*rgd
;
112 spin_lock(&sdp
->sd_rindex_spin
);
114 list_for_each_entry(rgd
, &sdp
->sd_rindex_mru_list
, rd_list_mru
) {
115 if (rgrp_contains_block(&rgd
->rd_ri
, blk
)) {
116 list_move(&rgd
->rd_list_mru
, &sdp
->sd_rindex_mru_list
);
117 spin_unlock(&sdp
->sd_rindex_spin
);
122 spin_unlock(&sdp
->sd_rindex_spin
);
128 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
129 * @sdp: The GFS2 superblock
131 * Returns: The first rgrp in the filesystem
134 struct gfs2_rgrpd
*gfs2_rgrpd_get_first(struct gfs2_sbd
*sdp
)
136 gfs2_assert(sdp
, !list_empty(&sdp
->sd_rindex_list
));
137 return list_entry(sdp
->sd_rindex_list
.next
, struct gfs2_rgrpd
, rd_list
);
141 * gfs2_rgrpd_get_next - get the next RG
144 * Returns: The next rgrp
147 struct gfs2_rgrpd
*gfs2_rgrpd_get_next(struct gfs2_rgrpd
*rgd
)
149 if (rgd
->rd_list
.next
== &rgd
->rd_sbd
->sd_rindex_list
)
151 return list_entry(rgd
->rd_list
.next
, struct gfs2_rgrpd
, rd_list
);
154 static void clear_rgrpdi(struct gfs2_sbd
*sdp
)
156 struct list_head
*head
;
157 struct gfs2_rgrpd
*rgd
;
158 struct gfs2_glock
*gl
;
160 spin_lock(&sdp
->sd_rindex_spin
);
161 sdp
->sd_rindex_forward
= NULL
;
162 head
= &sdp
->sd_rindex_recent_list
;
163 while (!list_empty(head
)) {
164 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_recent
);
165 list_del(&rgd
->rd_recent
);
167 spin_unlock(&sdp
->sd_rindex_spin
);
169 head
= &sdp
->sd_rindex_list
;
170 while (!list_empty(head
)) {
171 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_list
);
174 list_del(&rgd
->rd_list
);
175 list_del(&rgd
->rd_list_mru
);
178 gl
->gl_object
= NULL
;
187 void gfs2_clear_rgrpd(struct gfs2_sbd
*sdp
)
189 mutex_lock(&sdp
->sd_rindex_mutex
);
191 mutex_unlock(&sdp
->sd_rindex_mutex
);
195 * gfs2_compute_bitstructs - Compute the bitmap sizes
196 * @rgd: The resource group descriptor
198 * Calculates bitmap descriptors, one for each block that contains bitmap data
203 static int compute_bitstructs(struct gfs2_rgrpd
*rgd
)
205 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
206 struct gfs2_bitmap
*bi
;
207 uint32_t length
= rgd
->rd_ri
.ri_length
; /* # blocks in hdr & bitmap */
208 uint32_t bytes_left
, bytes
;
211 rgd
->rd_bits
= kcalloc(length
, sizeof(struct gfs2_bitmap
), GFP_KERNEL
);
215 bytes_left
= rgd
->rd_ri
.ri_bitbytes
;
217 for (x
= 0; x
< length
; x
++) {
218 bi
= rgd
->rd_bits
+ x
;
220 /* small rgrp; bitmap stored completely in header block */
223 bi
->bi_offset
= sizeof(struct gfs2_rgrp
);
228 bytes
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_rgrp
);
229 bi
->bi_offset
= sizeof(struct gfs2_rgrp
);
233 } else if (x
+ 1 == length
) {
235 bi
->bi_offset
= sizeof(struct gfs2_meta_header
);
236 bi
->bi_start
= rgd
->rd_ri
.ri_bitbytes
- bytes_left
;
240 bytes
= sdp
->sd_sb
.sb_bsize
-
241 sizeof(struct gfs2_meta_header
);
242 bi
->bi_offset
= sizeof(struct gfs2_meta_header
);
243 bi
->bi_start
= rgd
->rd_ri
.ri_bitbytes
- bytes_left
;
251 gfs2_consist_rgrpd(rgd
);
254 bi
= rgd
->rd_bits
+ (length
- 1);
255 if ((bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
!= rgd
->rd_ri
.ri_data
) {
256 if (gfs2_consist_rgrpd(rgd
)) {
257 gfs2_rindex_print(&rgd
->rd_ri
);
258 fs_err(sdp
, "start=%u len=%u offset=%u\n",
259 bi
->bi_start
, bi
->bi_len
, bi
->bi_offset
);
268 * gfs2_ri_update - Pull in a new resource index from the disk
269 * @gl: The glock covering the rindex inode
271 * Returns: 0 on successful update, error code otherwise
274 static int gfs2_ri_update(struct gfs2_inode
*ip
)
276 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
277 struct inode
*inode
= ip
->i_vnode
;
278 struct gfs2_rgrpd
*rgd
;
279 char buf
[sizeof(struct gfs2_rindex
)];
280 struct file_ra_state ra_state
;
281 uint64_t junk
= ip
->i_di
.di_size
;
284 if (do_div(junk
, sizeof(struct gfs2_rindex
))) {
285 gfs2_consist_inode(ip
);
291 file_ra_state_init(&ra_state
, inode
->i_mapping
);
292 for (sdp
->sd_rgrps
= 0;; sdp
->sd_rgrps
++) {
293 loff_t pos
= sdp
->sd_rgrps
* sizeof(struct gfs2_rindex
);
294 error
= gfs2_internal_read(ip
, &ra_state
, buf
, &pos
,
295 sizeof(struct gfs2_rindex
));
298 if (error
!= sizeof(struct gfs2_rindex
)) {
304 rgd
= kzalloc(sizeof(struct gfs2_rgrpd
), GFP_KERNEL
);
309 mutex_init(&rgd
->rd_mutex
);
310 lops_init_le(&rgd
->rd_le
, &gfs2_rg_lops
);
313 list_add_tail(&rgd
->rd_list
, &sdp
->sd_rindex_list
);
314 list_add_tail(&rgd
->rd_list_mru
, &sdp
->sd_rindex_mru_list
);
316 gfs2_rindex_in(&rgd
->rd_ri
, buf
);
318 error
= compute_bitstructs(rgd
);
322 error
= gfs2_glock_get(sdp
, rgd
->rd_ri
.ri_addr
,
323 &gfs2_rgrp_glops
, CREATE
, &rgd
->rd_gl
);
327 rgd
->rd_gl
->gl_object
= rgd
;
328 rgd
->rd_rg_vn
= rgd
->rd_gl
->gl_vn
- 1;
331 sdp
->sd_rindex_vn
= ip
->i_gl
->gl_vn
;
342 * gfs2_rindex_hold - Grab a lock on the rindex
343 * @sdp: The GFS2 superblock
344 * @ri_gh: the glock holder
346 * We grab a lock on the rindex inode to make sure that it doesn't
347 * change whilst we are performing an operation. We keep this lock
348 * for quite long periods of time compared to other locks. This
349 * doesn't matter, since it is shared and it is very, very rarely
350 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
352 * This makes sure that we're using the latest copy of the resource index
353 * special file, which might have been updated if someone expanded the
354 * filesystem (via gfs2_grow utility), which adds new resource groups.
356 * Returns: 0 on success, error code otherwise
359 int gfs2_rindex_hold(struct gfs2_sbd
*sdp
, struct gfs2_holder
*ri_gh
)
361 struct gfs2_inode
*ip
= sdp
->sd_rindex
->u
.generic_ip
;
362 struct gfs2_glock
*gl
= ip
->i_gl
;
365 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, 0, ri_gh
);
369 /* Read new copy from disk if we don't have the latest */
370 if (sdp
->sd_rindex_vn
!= gl
->gl_vn
) {
371 mutex_lock(&sdp
->sd_rindex_mutex
);
372 if (sdp
->sd_rindex_vn
!= gl
->gl_vn
) {
373 error
= gfs2_ri_update(ip
);
375 gfs2_glock_dq_uninit(ri_gh
);
377 mutex_unlock(&sdp
->sd_rindex_mutex
);
384 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
385 * @rgd: the struct gfs2_rgrpd describing the RG to read in
387 * Read in all of a Resource Group's header and bitmap blocks.
388 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
393 int gfs2_rgrp_bh_get(struct gfs2_rgrpd
*rgd
)
395 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
396 struct gfs2_glock
*gl
= rgd
->rd_gl
;
397 unsigned int length
= rgd
->rd_ri
.ri_length
;
398 struct gfs2_bitmap
*bi
;
402 mutex_lock(&rgd
->rd_mutex
);
404 spin_lock(&sdp
->sd_rindex_spin
);
405 if (rgd
->rd_bh_count
) {
407 spin_unlock(&sdp
->sd_rindex_spin
);
408 mutex_unlock(&rgd
->rd_mutex
);
411 spin_unlock(&sdp
->sd_rindex_spin
);
413 for (x
= 0; x
< length
; x
++) {
414 bi
= rgd
->rd_bits
+ x
;
415 error
= gfs2_meta_read(gl
, rgd
->rd_ri
.ri_addr
+ x
, DIO_START
,
421 for (y
= length
; y
--;) {
422 bi
= rgd
->rd_bits
+ y
;
423 error
= gfs2_meta_reread(sdp
, bi
->bi_bh
, DIO_WAIT
);
426 if (gfs2_metatype_check(sdp
, bi
->bi_bh
,
427 (y
) ? GFS2_METATYPE_RB
:
434 if (rgd
->rd_rg_vn
!= gl
->gl_vn
) {
435 gfs2_rgrp_in(&rgd
->rd_rg
, (rgd
->rd_bits
[0].bi_bh
)->b_data
);
436 rgd
->rd_rg_vn
= gl
->gl_vn
;
439 spin_lock(&sdp
->sd_rindex_spin
);
440 rgd
->rd_free_clone
= rgd
->rd_rg
.rg_free
;
442 spin_unlock(&sdp
->sd_rindex_spin
);
444 mutex_unlock(&rgd
->rd_mutex
);
450 bi
= rgd
->rd_bits
+ x
;
453 gfs2_assert_warn(sdp
, !bi
->bi_clone
);
455 mutex_unlock(&rgd
->rd_mutex
);
460 void gfs2_rgrp_bh_hold(struct gfs2_rgrpd
*rgd
)
462 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
464 spin_lock(&sdp
->sd_rindex_spin
);
465 gfs2_assert_warn(rgd
->rd_sbd
, rgd
->rd_bh_count
);
467 spin_unlock(&sdp
->sd_rindex_spin
);
471 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
472 * @rgd: the struct gfs2_rgrpd describing the RG to read in
476 void gfs2_rgrp_bh_put(struct gfs2_rgrpd
*rgd
)
478 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
479 int x
, length
= rgd
->rd_ri
.ri_length
;
481 spin_lock(&sdp
->sd_rindex_spin
);
482 gfs2_assert_warn(rgd
->rd_sbd
, rgd
->rd_bh_count
);
483 if (--rgd
->rd_bh_count
) {
484 spin_unlock(&sdp
->sd_rindex_spin
);
488 for (x
= 0; x
< length
; x
++) {
489 struct gfs2_bitmap
*bi
= rgd
->rd_bits
+ x
;
496 spin_unlock(&sdp
->sd_rindex_spin
);
499 void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd
*rgd
)
501 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
502 unsigned int length
= rgd
->rd_ri
.ri_length
;
505 for (x
= 0; x
< length
; x
++) {
506 struct gfs2_bitmap
*bi
= rgd
->rd_bits
+ x
;
509 memcpy(bi
->bi_clone
+ bi
->bi_offset
,
510 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
514 spin_lock(&sdp
->sd_rindex_spin
);
515 rgd
->rd_free_clone
= rgd
->rd_rg
.rg_free
;
516 spin_unlock(&sdp
->sd_rindex_spin
);
520 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
521 * @ip: the incore GFS2 inode structure
523 * Returns: the struct gfs2_alloc
526 struct gfs2_alloc
*gfs2_alloc_get(struct gfs2_inode
*ip
)
528 struct gfs2_alloc
*al
= &ip
->i_alloc
;
530 /* FIXME: Should assert that the correct locks are held here... */
531 memset(al
, 0, sizeof(*al
));
536 * gfs2_alloc_put - throw away the struct gfs2_alloc for an inode
541 void gfs2_alloc_put(struct gfs2_inode
*ip
)
547 * try_rgrp_fit - See if a given reservation will fit in a given RG
549 * @al: the struct gfs2_alloc structure describing the reservation
551 * If there's room for the requested blocks to be allocated from the RG:
552 * Sets the $al_reserved_data field in @al.
553 * Sets the $al_reserved_meta field in @al.
554 * Sets the $al_rgd field in @al.
556 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
559 static int try_rgrp_fit(struct gfs2_rgrpd
*rgd
, struct gfs2_alloc
*al
)
561 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
564 spin_lock(&sdp
->sd_rindex_spin
);
565 if (rgd
->rd_free_clone
>= al
->al_requested
) {
569 spin_unlock(&sdp
->sd_rindex_spin
);
575 * recent_rgrp_first - get first RG from "recent" list
576 * @sdp: The GFS2 superblock
577 * @rglast: address of the rgrp used last
579 * Returns: The first rgrp in the recent list
582 static struct gfs2_rgrpd
*recent_rgrp_first(struct gfs2_sbd
*sdp
,
585 struct gfs2_rgrpd
*rgd
= NULL
;
587 spin_lock(&sdp
->sd_rindex_spin
);
589 if (list_empty(&sdp
->sd_rindex_recent_list
))
595 list_for_each_entry(rgd
, &sdp
->sd_rindex_recent_list
, rd_recent
) {
596 if (rgd
->rd_ri
.ri_addr
== rglast
)
601 rgd
= list_entry(sdp
->sd_rindex_recent_list
.next
, struct gfs2_rgrpd
,
605 spin_unlock(&sdp
->sd_rindex_spin
);
611 * recent_rgrp_next - get next RG from "recent" list
612 * @cur_rgd: current rgrp
615 * Returns: The next rgrp in the recent list
618 static struct gfs2_rgrpd
*recent_rgrp_next(struct gfs2_rgrpd
*cur_rgd
,
621 struct gfs2_sbd
*sdp
= cur_rgd
->rd_sbd
;
622 struct list_head
*head
;
623 struct gfs2_rgrpd
*rgd
;
625 spin_lock(&sdp
->sd_rindex_spin
);
627 head
= &sdp
->sd_rindex_recent_list
;
629 list_for_each_entry(rgd
, head
, rd_recent
) {
630 if (rgd
== cur_rgd
) {
631 if (cur_rgd
->rd_recent
.next
!= head
)
632 rgd
= list_entry(cur_rgd
->rd_recent
.next
,
633 struct gfs2_rgrpd
, rd_recent
);
638 list_del(&cur_rgd
->rd_recent
);
645 if (!list_empty(head
))
646 rgd
= list_entry(head
->next
, struct gfs2_rgrpd
, rd_recent
);
649 spin_unlock(&sdp
->sd_rindex_spin
);
655 * recent_rgrp_add - add an RG to tail of "recent" list
656 * @new_rgd: The rgrp to add
660 static void recent_rgrp_add(struct gfs2_rgrpd
*new_rgd
)
662 struct gfs2_sbd
*sdp
= new_rgd
->rd_sbd
;
663 struct gfs2_rgrpd
*rgd
;
664 unsigned int count
= 0;
665 unsigned int max
= sdp
->sd_rgrps
/ gfs2_jindex_size(sdp
);
667 spin_lock(&sdp
->sd_rindex_spin
);
669 list_for_each_entry(rgd
, &sdp
->sd_rindex_recent_list
, rd_recent
) {
676 list_add_tail(&new_rgd
->rd_recent
, &sdp
->sd_rindex_recent_list
);
679 spin_unlock(&sdp
->sd_rindex_spin
);
683 * forward_rgrp_get - get an rgrp to try next from full list
684 * @sdp: The GFS2 superblock
686 * Returns: The rgrp to try next
689 static struct gfs2_rgrpd
*forward_rgrp_get(struct gfs2_sbd
*sdp
)
691 struct gfs2_rgrpd
*rgd
;
692 unsigned int journals
= gfs2_jindex_size(sdp
);
693 unsigned int rg
= 0, x
;
695 spin_lock(&sdp
->sd_rindex_spin
);
697 rgd
= sdp
->sd_rindex_forward
;
699 if (sdp
->sd_rgrps
>= journals
)
700 rg
= sdp
->sd_rgrps
* sdp
->sd_jdesc
->jd_jid
/ journals
;
702 for (x
= 0, rgd
= gfs2_rgrpd_get_first(sdp
);
704 x
++, rgd
= gfs2_rgrpd_get_next(rgd
))
707 sdp
->sd_rindex_forward
= rgd
;
710 spin_unlock(&sdp
->sd_rindex_spin
);
716 * forward_rgrp_set - set the forward rgrp pointer
717 * @sdp: the filesystem
718 * @rgd: The new forward rgrp
722 static void forward_rgrp_set(struct gfs2_sbd
*sdp
, struct gfs2_rgrpd
*rgd
)
724 spin_lock(&sdp
->sd_rindex_spin
);
725 sdp
->sd_rindex_forward
= rgd
;
726 spin_unlock(&sdp
->sd_rindex_spin
);
730 * get_local_rgrp - Choose and lock a rgrp for allocation
731 * @ip: the inode to reserve space for
732 * @rgp: the chosen and locked rgrp
734 * Try to acquire rgrp in way which avoids contending with others.
739 static int get_local_rgrp(struct gfs2_inode
*ip
)
741 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
742 struct gfs2_rgrpd
*rgd
, *begin
= NULL
;
743 struct gfs2_alloc
*al
= &ip
->i_alloc
;
744 int flags
= LM_FLAG_TRY
;
749 /* Try recently successful rgrps */
751 rgd
= recent_rgrp_first(sdp
, ip
->i_last_rg_alloc
);
754 error
= gfs2_glock_nq_init(rgd
->rd_gl
,
755 LM_ST_EXCLUSIVE
, LM_FLAG_TRY
,
759 if (try_rgrp_fit(rgd
, al
))
761 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
762 rgd
= recent_rgrp_next(rgd
, 1);
766 rgd
= recent_rgrp_next(rgd
, 0);
774 /* Go through full list of rgrps */
776 begin
= rgd
= forward_rgrp_get(sdp
);
779 error
= gfs2_glock_nq_init(rgd
->rd_gl
,
780 LM_ST_EXCLUSIVE
, flags
,
784 if (try_rgrp_fit(rgd
, al
))
786 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
797 rgd
= gfs2_rgrpd_get_next(rgd
);
799 rgd
= gfs2_rgrpd_get_first(sdp
);
802 if (++loops
>= 2 || !skipped
)
809 ip
->i_last_rg_alloc
= rgd
->rd_ri
.ri_addr
;
812 recent_rgrp_add(rgd
);
813 rgd
= gfs2_rgrpd_get_next(rgd
);
815 rgd
= gfs2_rgrpd_get_first(sdp
);
816 forward_rgrp_set(sdp
, rgd
);
823 * gfs2_inplace_reserve_i - Reserve space in the filesystem
824 * @ip: the inode to reserve space for
829 int gfs2_inplace_reserve_i(struct gfs2_inode
*ip
, char *file
, unsigned int line
)
831 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
832 struct gfs2_alloc
*al
= &ip
->i_alloc
;
835 if (gfs2_assert_warn(sdp
, al
->al_requested
))
838 error
= gfs2_rindex_hold(sdp
, &al
->al_ri_gh
);
842 error
= get_local_rgrp(ip
);
844 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
855 * gfs2_inplace_release - release an inplace reservation
856 * @ip: the inode the reservation was taken out on
858 * Release a reservation made by gfs2_inplace_reserve().
861 void gfs2_inplace_release(struct gfs2_inode
*ip
)
863 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
864 struct gfs2_alloc
*al
= &ip
->i_alloc
;
866 if (gfs2_assert_warn(sdp
, al
->al_alloced
<= al
->al_requested
) == -1)
867 fs_warn(sdp
, "al_alloced = %u, al_requested = %u "
868 "al_file = %s, al_line = %u\n",
869 al
->al_alloced
, al
->al_requested
, al
->al_file
,
873 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
874 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
878 * gfs2_get_block_type - Check a block in a RG is of given type
879 * @rgd: the resource group holding the block
880 * @block: the block number
882 * Returns: The block type (GFS2_BLKST_*)
885 unsigned char gfs2_get_block_type(struct gfs2_rgrpd
*rgd
, uint64_t block
)
887 struct gfs2_bitmap
*bi
= NULL
;
888 uint32_t length
, rgrp_block
, buf_block
;
892 length
= rgd
->rd_ri
.ri_length
;
893 rgrp_block
= block
- rgd
->rd_ri
.ri_data0
;
895 for (buf
= 0; buf
< length
; buf
++) {
896 bi
= rgd
->rd_bits
+ buf
;
897 if (rgrp_block
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
901 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
902 buf_block
= rgrp_block
- bi
->bi_start
* GFS2_NBBY
;
904 type
= gfs2_testbit(rgd
,
905 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
906 bi
->bi_len
, buf_block
);
912 * rgblk_search - find a block in @old_state, change allocation
913 * state to @new_state
914 * @rgd: the resource group descriptor
915 * @goal: the goal block within the RG (start here to search for avail block)
916 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
917 * @new_state: GFS2_BLKST_XXX the after-allocation block state
919 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
920 * Add the found bitmap buffer to the transaction.
921 * Set the found bits to @new_state to change block's allocation state.
923 * This function never fails, because we wouldn't call it unless we
924 * know (from reservation results, etc.) that a block is available.
926 * Scope of @goal and returned block is just within rgrp, not the whole
929 * Returns: the block number allocated
932 static uint32_t rgblk_search(struct gfs2_rgrpd
*rgd
, uint32_t goal
,
933 unsigned char old_state
, unsigned char new_state
)
935 struct gfs2_bitmap
*bi
= NULL
;
936 uint32_t length
= rgd
->rd_ri
.ri_length
;
940 /* Find bitmap block that contains bits for goal block */
941 for (buf
= 0; buf
< length
; buf
++) {
942 bi
= rgd
->rd_bits
+ buf
;
943 if (goal
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
947 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
949 /* Convert scope of "goal" from rgrp-wide to within found bit block */
950 goal
-= bi
->bi_start
* GFS2_NBBY
;
952 /* Search (up to entire) bitmap in this rgrp for allocatable block.
953 "x <= length", instead of "x < length", because we typically start
954 the search in the middle of a bit block, but if we can't find an
955 allocatable block anywhere else, we want to be able wrap around and
956 search in the first part of our first-searched bit block. */
957 for (x
= 0; x
<= length
; x
++) {
959 blk
= gfs2_bitfit(rgd
,
960 bi
->bi_clone
+ bi
->bi_offset
,
961 bi
->bi_len
, goal
, old_state
);
963 blk
= gfs2_bitfit(rgd
,
964 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
965 bi
->bi_len
, goal
, old_state
);
966 if (blk
!= BFITNOENT
)
969 /* Try next bitmap block (wrap back to rgrp header if at end) */
970 buf
= (buf
+ 1) % length
;
971 bi
= rgd
->rd_bits
+ buf
;
975 if (gfs2_assert_withdraw(rgd
->rd_sbd
, x
<= length
))
978 gfs2_trans_add_bh(rgd
->rd_gl
, bi
->bi_bh
, 1);
980 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
981 bi
->bi_len
, blk
, new_state
);
984 bi
->bi_clone
+ bi
->bi_offset
,
985 bi
->bi_len
, blk
, new_state
);
987 return bi
->bi_start
* GFS2_NBBY
+ blk
;
991 * rgblk_free - Change alloc state of given block(s)
992 * @sdp: the filesystem
993 * @bstart: the start of a run of blocks to free
994 * @blen: the length of the block run (all must lie within ONE RG!)
995 * @new_state: GFS2_BLKST_XXX the after-allocation block state
997 * Returns: Resource group containing the block(s)
1000 static struct gfs2_rgrpd
*rgblk_free(struct gfs2_sbd
*sdp
, uint64_t bstart
,
1001 uint32_t blen
, unsigned char new_state
)
1003 struct gfs2_rgrpd
*rgd
;
1004 struct gfs2_bitmap
*bi
= NULL
;
1005 uint32_t length
, rgrp_blk
, buf_blk
;
1008 rgd
= gfs2_blk2rgrpd(sdp
, bstart
);
1010 if (gfs2_consist(sdp
))
1011 fs_err(sdp
, "block = %llu\n", bstart
);
1015 length
= rgd
->rd_ri
.ri_length
;
1017 rgrp_blk
= bstart
- rgd
->rd_ri
.ri_data0
;
1020 for (buf
= 0; buf
< length
; buf
++) {
1021 bi
= rgd
->rd_bits
+ buf
;
1022 if (rgrp_blk
< (bi
->bi_start
+ bi
->bi_len
) * GFS2_NBBY
)
1026 gfs2_assert(rgd
->rd_sbd
, buf
< length
);
1028 buf_blk
= rgrp_blk
- bi
->bi_start
* GFS2_NBBY
;
1031 if (!bi
->bi_clone
) {
1032 bi
->bi_clone
= kmalloc(bi
->bi_bh
->b_size
,
1033 GFP_KERNEL
| __GFP_NOFAIL
);
1034 memcpy(bi
->bi_clone
+ bi
->bi_offset
,
1035 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1038 gfs2_trans_add_bh(rgd
->rd_gl
, bi
->bi_bh
, 1);
1040 bi
->bi_bh
->b_data
+ bi
->bi_offset
,
1041 bi
->bi_len
, buf_blk
, new_state
);
1048 * gfs2_alloc_data - Allocate a data block
1049 * @ip: the inode to allocate the data block for
1051 * Returns: the allocated block
1054 uint64_t gfs2_alloc_data(struct gfs2_inode
*ip
)
1056 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1057 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1058 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1062 if (rgrp_contains_block(&rgd
->rd_ri
, ip
->i_di
.di_goal_data
))
1063 goal
= ip
->i_di
.di_goal_data
- rgd
->rd_ri
.ri_data0
;
1065 goal
= rgd
->rd_last_alloc_data
;
1067 blk
= rgblk_search(rgd
, goal
,
1068 GFS2_BLKST_FREE
, GFS2_BLKST_USED
);
1069 rgd
->rd_last_alloc_data
= blk
;
1071 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1072 ip
->i_di
.di_goal_data
= block
;
1074 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1075 rgd
->rd_rg
.rg_free
--;
1077 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1078 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1082 gfs2_statfs_change(sdp
, 0, -1, 0);
1083 gfs2_quota_change(ip
, +1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1085 spin_lock(&sdp
->sd_rindex_spin
);
1086 rgd
->rd_free_clone
--;
1087 spin_unlock(&sdp
->sd_rindex_spin
);
1093 * gfs2_alloc_meta - Allocate a metadata block
1094 * @ip: the inode to allocate the metadata block for
1096 * Returns: the allocated block
1099 uint64_t gfs2_alloc_meta(struct gfs2_inode
*ip
)
1101 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1102 struct gfs2_alloc
*al
= &ip
->i_alloc
;
1103 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1107 if (rgrp_contains_block(&rgd
->rd_ri
, ip
->i_di
.di_goal_meta
))
1108 goal
= ip
->i_di
.di_goal_meta
- rgd
->rd_ri
.ri_data0
;
1110 goal
= rgd
->rd_last_alloc_meta
;
1112 blk
= rgblk_search(rgd
, goal
,
1113 GFS2_BLKST_FREE
, GFS2_BLKST_USED
);
1114 rgd
->rd_last_alloc_meta
= blk
;
1116 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1117 ip
->i_di
.di_goal_meta
= block
;
1119 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1120 rgd
->rd_rg
.rg_free
--;
1122 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1123 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1127 gfs2_statfs_change(sdp
, 0, -1, 0);
1128 gfs2_quota_change(ip
, +1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1129 gfs2_trans_add_unrevoke(sdp
, block
);
1131 spin_lock(&sdp
->sd_rindex_spin
);
1132 rgd
->rd_free_clone
--;
1133 spin_unlock(&sdp
->sd_rindex_spin
);
1139 * gfs2_alloc_di - Allocate a dinode
1140 * @dip: the directory that the inode is going in
1142 * Returns: the block allocated
1145 uint64_t gfs2_alloc_di(struct gfs2_inode
*dip
)
1147 struct gfs2_sbd
*sdp
= dip
->i_sbd
;
1148 struct gfs2_alloc
*al
= &dip
->i_alloc
;
1149 struct gfs2_rgrpd
*rgd
= al
->al_rgd
;
1153 blk
= rgblk_search(rgd
, rgd
->rd_last_alloc_meta
,
1154 GFS2_BLKST_FREE
, GFS2_BLKST_DINODE
);
1156 rgd
->rd_last_alloc_meta
= blk
;
1158 block
= rgd
->rd_ri
.ri_data0
+ blk
;
1160 gfs2_assert_withdraw(sdp
, rgd
->rd_rg
.rg_free
);
1161 rgd
->rd_rg
.rg_free
--;
1162 rgd
->rd_rg
.rg_dinodes
++;
1164 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1165 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1169 gfs2_statfs_change(sdp
, 0, -1, +1);
1170 gfs2_trans_add_unrevoke(sdp
, block
);
1172 spin_lock(&sdp
->sd_rindex_spin
);
1173 rgd
->rd_free_clone
--;
1174 spin_unlock(&sdp
->sd_rindex_spin
);
1180 * gfs2_free_data - free a contiguous run of data block(s)
1181 * @ip: the inode these blocks are being freed from
1182 * @bstart: first block of a run of contiguous blocks
1183 * @blen: the length of the block run
1187 void gfs2_free_data(struct gfs2_inode
*ip
, uint64_t bstart
, uint32_t blen
)
1189 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1190 struct gfs2_rgrpd
*rgd
;
1192 rgd
= rgblk_free(sdp
, bstart
, blen
, GFS2_BLKST_FREE
);
1196 rgd
->rd_rg
.rg_free
+= blen
;
1198 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1199 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1201 gfs2_trans_add_rg(rgd
);
1203 gfs2_statfs_change(sdp
, 0, +blen
, 0);
1204 gfs2_quota_change(ip
, -(int64_t)blen
,
1205 ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1209 * gfs2_free_meta - free a contiguous run of data block(s)
1210 * @ip: the inode these blocks are being freed from
1211 * @bstart: first block of a run of contiguous blocks
1212 * @blen: the length of the block run
1216 void gfs2_free_meta(struct gfs2_inode
*ip
, uint64_t bstart
, uint32_t blen
)
1218 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1219 struct gfs2_rgrpd
*rgd
;
1221 rgd
= rgblk_free(sdp
, bstart
, blen
, GFS2_BLKST_FREE
);
1225 rgd
->rd_rg
.rg_free
+= blen
;
1227 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1228 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1230 gfs2_trans_add_rg(rgd
);
1232 gfs2_statfs_change(sdp
, 0, +blen
, 0);
1233 gfs2_quota_change(ip
, -(int64_t)blen
,
1234 ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1235 gfs2_meta_wipe(ip
, bstart
, blen
);
1238 void gfs2_free_uninit_di(struct gfs2_rgrpd
*rgd
, uint64_t blkno
)
1240 struct gfs2_sbd
*sdp
= rgd
->rd_sbd
;
1241 struct gfs2_rgrpd
*tmp_rgd
;
1243 tmp_rgd
= rgblk_free(sdp
, blkno
, 1, GFS2_BLKST_FREE
);
1246 gfs2_assert_withdraw(sdp
, rgd
== tmp_rgd
);
1248 if (!rgd
->rd_rg
.rg_dinodes
)
1249 gfs2_consist_rgrpd(rgd
);
1250 rgd
->rd_rg
.rg_dinodes
--;
1251 rgd
->rd_rg
.rg_free
++;
1253 gfs2_trans_add_bh(rgd
->rd_gl
, rgd
->rd_bits
[0].bi_bh
, 1);
1254 gfs2_rgrp_out(&rgd
->rd_rg
, rgd
->rd_bits
[0].bi_bh
->b_data
);
1256 gfs2_statfs_change(sdp
, 0, +1, -1);
1257 gfs2_trans_add_rg(rgd
);
1261 * gfs2_free_uninit_di - free a dinode block
1262 * @rgd: the resource group that contains the dinode
1267 void gfs2_free_di(struct gfs2_rgrpd
*rgd
, struct gfs2_inode
*ip
)
1269 gfs2_free_uninit_di(rgd
, ip
->i_num
.no_addr
);
1270 gfs2_quota_change(ip
, -1, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
1271 gfs2_meta_wipe(ip
, ip
->i_num
.no_addr
, 1);
1275 * gfs2_rlist_add - add a RG to a list of RGs
1276 * @sdp: the filesystem
1277 * @rlist: the list of resource groups
1280 * Figure out what RG a block belongs to and add that RG to the list
1282 * FIXME: Don't use NOFAIL
1286 void gfs2_rlist_add(struct gfs2_sbd
*sdp
, struct gfs2_rgrp_list
*rlist
,
1289 struct gfs2_rgrpd
*rgd
;
1290 struct gfs2_rgrpd
**tmp
;
1291 unsigned int new_space
;
1294 if (gfs2_assert_warn(sdp
, !rlist
->rl_ghs
))
1297 rgd
= gfs2_blk2rgrpd(sdp
, block
);
1299 if (gfs2_consist(sdp
))
1300 fs_err(sdp
, "block = %llu\n", block
);
1304 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1305 if (rlist
->rl_rgd
[x
] == rgd
)
1308 if (rlist
->rl_rgrps
== rlist
->rl_space
) {
1309 new_space
= rlist
->rl_space
+ 10;
1311 tmp
= kcalloc(new_space
, sizeof(struct gfs2_rgrpd
*),
1312 GFP_KERNEL
| __GFP_NOFAIL
);
1314 if (rlist
->rl_rgd
) {
1315 memcpy(tmp
, rlist
->rl_rgd
,
1316 rlist
->rl_space
* sizeof(struct gfs2_rgrpd
*));
1317 kfree(rlist
->rl_rgd
);
1320 rlist
->rl_space
= new_space
;
1321 rlist
->rl_rgd
= tmp
;
1324 rlist
->rl_rgd
[rlist
->rl_rgrps
++] = rgd
;
1328 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1329 * and initialize an array of glock holders for them
1330 * @rlist: the list of resource groups
1331 * @state: the lock state to acquire the RG lock in
1332 * @flags: the modifier flags for the holder structures
1334 * FIXME: Don't use NOFAIL
1338 void gfs2_rlist_alloc(struct gfs2_rgrp_list
*rlist
, unsigned int state
,
1343 rlist
->rl_ghs
= kcalloc(rlist
->rl_rgrps
, sizeof(struct gfs2_holder
),
1344 GFP_KERNEL
| __GFP_NOFAIL
);
1345 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1346 gfs2_holder_init(rlist
->rl_rgd
[x
]->rd_gl
,
1352 * gfs2_rlist_free - free a resource group list
1353 * @list: the list of resource groups
1357 void gfs2_rlist_free(struct gfs2_rgrp_list
*rlist
)
1361 kfree(rlist
->rl_rgd
);
1363 if (rlist
->rl_ghs
) {
1364 for (x
= 0; x
< rlist
->rl_rgrps
; x
++)
1365 gfs2_holder_uninit(&rlist
->rl_ghs
[x
]);
1366 kfree(rlist
->rl_ghs
);
This page took 0.089401 seconds and 5 git commands to generate.