Lines Matching +full:ip +full:- +full:block
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41 * to keep track of block allocation. Each block is represented by two
64 const struct gfs2_inode *ip, bool nowrap);
68 * gfs2_setbit - Set a bit in the bitmaps
71 * @new_state: the new state of the block
80 unsigned int buflen = bi->bi_bytes; in gfs2_setbit()
81 const unsigned int bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE; in gfs2_setbit()
83 byte1 = bi->bi_bh->b_data + bi->bi_offset + (rbm->offset / GFS2_NBBY); in gfs2_setbit()
84 end = bi->bi_bh->b_data + bi->bi_offset + buflen; in gfs2_setbit()
91 struct gfs2_sbd *sdp = rbm->rgd->rd_sbd; in gfs2_setbit()
94 rbm->offset, cur_state, new_state); in gfs2_setbit()
96 (unsigned long long)rbm->rgd->rd_addr, bi->bi_start, in gfs2_setbit()
97 (unsigned long long)bi->bi_bh->b_blocknr); in gfs2_setbit()
98 fs_warn(sdp, "bi_offset=0x%x bi_bytes=0x%x block=0x%llx\n", in gfs2_setbit()
99 bi->bi_offset, bi->bi_bytes, in gfs2_setbit()
102 gfs2_consist_rgrpd(rbm->rgd); in gfs2_setbit()
107 if (do_clone && bi->bi_clone) { in gfs2_setbit()
108 byte2 = bi->bi_clone + bi->bi_offset + (rbm->offset / GFS2_NBBY); in gfs2_setbit()
115 * gfs2_testbit - test a bit in the bitmaps
122 * Returns: The two bit block state of the requested bit
132 if (use_clone && bi->bi_clone) in gfs2_testbit()
133 buffer = bi->bi_clone; in gfs2_testbit()
135 buffer = bi->bi_bh->b_data; in gfs2_testbit()
136 buffer += bi->bi_offset; in gfs2_testbit()
137 byte = buffer + (rbm->offset / GFS2_NBBY); in gfs2_testbit()
138 bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE; in gfs2_testbit()
177 * rs_cmp - multi-block reservation range compare
178 * @blk: absolute file system block number of the new reservation
182 * returns: 1 if the block range is beyond the reach of the reservation
183 * -1 if the block range is before the start of the reservation
184 * 0 if the block range overlaps with the reservation
188 u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm); in rs_cmp()
190 if (blk >= startblk + rs->rs_free) in rs_cmp()
192 if (blk + len - 1 < startblk) in rs_cmp()
193 return -1; in rs_cmp()
198 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
199 * a block in a given allocation state.
202 * @goal: start search at this block's bit-pair (within @buffer)
203 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
205 * Scope of @goal and returned block number is only within this bitmap buffer,
207 * beginning of a bitmap block buffer, skipping any header structures, but
213 * of the block in case the end is no aligned to a natural boundary.
215 * Return: the block number (bitmap buffer scope) that was found
221 u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1); in gfs2_bitfit()
237 if (ptr == end && (len & (sizeof(u64) - 1))) in gfs2_bitfit()
238 tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1)))); in gfs2_bitfit()
242 ptr--; in gfs2_bitfit()
245 return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit; in gfs2_bitfit()
249 * gfs2_rbm_from_block - Set the rbm based upon rgd and block number
251 * @block: The block number (filesystem relative)
254 * resource group and a filesystem relative block number. The
261 static int gfs2_rbm_from_block(struct gfs2_rbm *rbm, u64 block) in gfs2_rbm_from_block() argument
263 if (!rgrp_contains_block(rbm->rgd, block)) in gfs2_rbm_from_block()
264 return -E2BIG; in gfs2_rbm_from_block()
265 rbm->bii = 0; in gfs2_rbm_from_block()
266 rbm->offset = block - rbm->rgd->rd_data0; in gfs2_rbm_from_block()
267 /* Check if the block is within the first block */ in gfs2_rbm_from_block()
268 if (rbm->offset < rbm_bi(rbm)->bi_blocks) in gfs2_rbm_from_block()
272 rbm->offset += (sizeof(struct gfs2_rgrp) - in gfs2_rbm_from_block()
274 rbm->bii = rbm->offset / rbm->rgd->rd_sbd->sd_blocks_per_bitmap; in gfs2_rbm_from_block()
275 rbm->offset -= rbm->bii * rbm->rgd->rd_sbd->sd_blocks_per_bitmap; in gfs2_rbm_from_block()
280 * gfs2_rbm_incr - increment an rbm structure
284 * viable block offset.
293 if (rbm->offset + 1 < rbm_bi(rbm)->bi_blocks) { /* in the same bitmap */ in gfs2_rbm_incr()
294 rbm->offset++; in gfs2_rbm_incr()
297 if (rbm->bii == rbm->rgd->rd_length - 1) /* at the last bitmap */ in gfs2_rbm_incr()
300 rbm->offset = 0; in gfs2_rbm_incr()
301 rbm->bii++; in gfs2_rbm_incr()
306 * gfs2_unaligned_extlen - Look for free blocks which are not byte aligned
309 * @len: Decremented for each block found (terminate on zero)
311 * Returns: true if a non-free block is encountered
323 (*len)--; in gfs2_unaligned_extlen()
334 * gfs2_free_extlen - Return extent length of free blocks
338 * Starting at the block specified by the rbm, see how many free blocks
341 * on a block by block basis in case of unaligned blocks. Also this
356 u64 block; in gfs2_free_extlen() local
360 gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len)) in gfs2_free_extlen()
367 start = bi->bi_bh->b_data; in gfs2_free_extlen()
368 if (bi->bi_clone) in gfs2_free_extlen()
369 start = bi->bi_clone; in gfs2_free_extlen()
370 start += bi->bi_offset; in gfs2_free_extlen()
371 end = start + bi->bi_bytes; in gfs2_free_extlen()
374 bytes = min_t(u32, len / GFS2_NBBY, (end - start)); in gfs2_free_extlen()
376 chunk_size = ((ptr == NULL) ? bytes : (ptr - start)); in gfs2_free_extlen()
379 len -= chunk_size; in gfs2_free_extlen()
380 block = gfs2_rbm_to_block(&rbm); in gfs2_free_extlen()
381 if (gfs2_rbm_from_block(&rbm, block + chunk_size)) { in gfs2_free_extlen()
396 return size - len; in gfs2_free_extlen()
400 * gfs2_bitcount - count the number of bits in a certain state
404 * @state: the state of the block we're looking for
434 * gfs2_rgrp_verify - Verify that a resource group is consistent
441 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_verify()
443 u32 length = rgd->rd_length; in gfs2_rgrp_verify()
451 bi = rgd->rd_bits + buf; in gfs2_rgrp_verify()
454 bi->bi_bh->b_data + in gfs2_rgrp_verify()
455 bi->bi_offset, in gfs2_rgrp_verify()
456 bi->bi_bytes, x); in gfs2_rgrp_verify()
459 if (count[0] != rgd->rd_free) { in gfs2_rgrp_verify()
461 count[0], rgd->rd_free); in gfs2_rgrp_verify()
466 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes; in gfs2_rgrp_verify()
474 if (count[2] + count[3] != rgd->rd_dinodes) { in gfs2_rgrp_verify()
476 count[2] + count[3], rgd->rd_dinodes); in gfs2_rgrp_verify()
483 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
485 * @blk: The data block number
493 * match any block within the extent of the rgrp.
503 spin_lock(&sdp->sd_rindex_spin); in gfs2_blk2rgrpd()
504 n = sdp->sd_rindex_tree.rb_node; in gfs2_blk2rgrpd()
508 if (blk < cur->rd_addr) in gfs2_blk2rgrpd()
509 next = n->rb_left; in gfs2_blk2rgrpd()
510 else if (blk >= cur->rd_data0 + cur->rd_data) in gfs2_blk2rgrpd()
511 next = n->rb_right; in gfs2_blk2rgrpd()
513 spin_unlock(&sdp->sd_rindex_spin); in gfs2_blk2rgrpd()
515 if (blk < cur->rd_addr) in gfs2_blk2rgrpd()
517 if (blk >= cur->rd_data0 + cur->rd_data) in gfs2_blk2rgrpd()
524 spin_unlock(&sdp->sd_rindex_spin); in gfs2_blk2rgrpd()
530 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
541 spin_lock(&sdp->sd_rindex_spin); in gfs2_rgrpd_get_first()
542 n = rb_first(&sdp->sd_rindex_tree); in gfs2_rgrpd_get_first()
544 spin_unlock(&sdp->sd_rindex_spin); in gfs2_rgrpd_get_first()
550 * gfs2_rgrpd_get_next - get the next RG
558 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrpd_get_next()
561 spin_lock(&sdp->sd_rindex_spin); in gfs2_rgrpd_get_next()
562 n = rb_next(&rgd->rd_node); in gfs2_rgrpd_get_next()
564 n = rb_first(&sdp->sd_rindex_tree); in gfs2_rgrpd_get_next()
566 if (unlikely(&rgd->rd_node == n)) { in gfs2_rgrpd_get_next()
567 spin_unlock(&sdp->sd_rindex_spin); in gfs2_rgrpd_get_next()
571 spin_unlock(&sdp->sd_rindex_spin); in gfs2_rgrpd_get_next()
575 void check_and_update_goal(struct gfs2_inode *ip) in check_and_update_goal() argument
577 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in check_and_update_goal()
578 if (!ip->i_goal || gfs2_blk2rgrpd(sdp, ip->i_goal, 1) == NULL) in check_and_update_goal()
579 ip->i_goal = ip->i_no_addr; in check_and_update_goal()
586 for (x = 0; x < rgd->rd_length; x++) { in gfs2_free_clones()
587 struct gfs2_bitmap *bi = rgd->rd_bits + x; in gfs2_free_clones()
588 kfree(bi->bi_clone); in gfs2_free_clones()
589 bi->bi_clone = NULL; in gfs2_free_clones()
596 struct gfs2_inode *ip = container_of(rs, struct gfs2_inode, i_res); in dump_rs() local
599 (unsigned long long)ip->i_no_addr, in dump_rs()
600 (unsigned long long)gfs2_rbm_to_block(&rs->rs_rbm), in dump_rs()
601 rs->rs_rbm.offset, rs->rs_free); in dump_rs()
605 * __rs_deltree - remove a multi-block reservation from the rgd tree
616 rgd = rs->rs_rbm.rgd; in __rs_deltree()
618 rb_erase(&rs->rs_node, &rgd->rd_rstree); in __rs_deltree()
619 RB_CLEAR_NODE(&rs->rs_node); in __rs_deltree()
621 if (rs->rs_free) { in __rs_deltree()
622 u64 last_block = gfs2_rbm_to_block(&rs->rs_rbm) + in __rs_deltree()
623 rs->rs_free - 1; in __rs_deltree()
624 struct gfs2_rbm last_rbm = { .rgd = rs->rs_rbm.rgd, }; in __rs_deltree()
628 BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free); in __rs_deltree()
629 rs->rs_rbm.rgd->rd_reserved -= rs->rs_free; in __rs_deltree()
634 rgd->rd_extfail_pt += rs->rs_free; in __rs_deltree()
635 rs->rs_free = 0; in __rs_deltree()
638 start = rbm_bi(&rs->rs_rbm); in __rs_deltree()
641 clear_bit(GBF_FULL, &start->bi_flags); in __rs_deltree()
647 * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
655 rgd = rs->rs_rbm.rgd; in gfs2_rs_deltree()
657 spin_lock(&rgd->rd_rsspin); in gfs2_rs_deltree()
659 BUG_ON(rs->rs_free); in gfs2_rs_deltree()
660 spin_unlock(&rgd->rd_rsspin); in gfs2_rs_deltree()
665 * gfs2_rs_delete - delete a multi-block reservation
666 * @ip: The inode for this reservation
670 void gfs2_rs_delete(struct gfs2_inode *ip, atomic_t *wcount) in gfs2_rs_delete() argument
672 down_write(&ip->i_rw_mutex); in gfs2_rs_delete()
674 gfs2_rs_deltree(&ip->i_res); in gfs2_rs_delete()
675 up_write(&ip->i_rw_mutex); in gfs2_rs_delete()
679 * return_all_reservations - return all reserved blocks back to the rgrp.
684 * all of their corresponding "no-fly zones".
691 spin_lock(&rgd->rd_rsspin); in return_all_reservations()
692 while ((n = rb_first(&rgd->rd_rstree))) { in return_all_reservations()
696 spin_unlock(&rgd->rd_rsspin); in return_all_reservations()
705 while ((n = rb_first(&sdp->sd_rindex_tree))) { in gfs2_clear_rgrpd()
707 gl = rgd->rd_gl; in gfs2_clear_rgrpd()
709 rb_erase(n, &sdp->sd_rindex_tree); in gfs2_clear_rgrpd()
712 if (gl->gl_state != LM_ST_UNLOCKED) { in gfs2_clear_rgrpd()
714 flush_delayed_work(&gl->gl_work); in gfs2_clear_rgrpd()
723 kfree(rgd->rd_bits); in gfs2_clear_rgrpd()
724 rgd->rd_bits = NULL; in gfs2_clear_rgrpd()
730 * gfs2_compute_bitstructs - Compute the bitmap sizes
733 * Calculates bitmap descriptors, one for each block that contains bitmap data
740 struct gfs2_sbd *sdp = rgd->rd_sbd; in compute_bitstructs()
742 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */ in compute_bitstructs()
747 return -EINVAL; in compute_bitstructs()
749 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS); in compute_bitstructs()
750 if (!rgd->rd_bits) in compute_bitstructs()
751 return -ENOMEM; in compute_bitstructs()
753 bytes_left = rgd->rd_bitbytes; in compute_bitstructs()
756 bi = rgd->rd_bits + x; in compute_bitstructs()
758 bi->bi_flags = 0; in compute_bitstructs()
759 /* small rgrp; bitmap stored completely in header block */ in compute_bitstructs()
762 bi->bi_offset = sizeof(struct gfs2_rgrp); in compute_bitstructs()
763 bi->bi_start = 0; in compute_bitstructs()
764 bi->bi_bytes = bytes; in compute_bitstructs()
765 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
766 /* header block */ in compute_bitstructs()
768 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp); in compute_bitstructs()
769 bi->bi_offset = sizeof(struct gfs2_rgrp); in compute_bitstructs()
770 bi->bi_start = 0; in compute_bitstructs()
771 bi->bi_bytes = bytes; in compute_bitstructs()
772 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
773 /* last block */ in compute_bitstructs()
776 bi->bi_offset = sizeof(struct gfs2_meta_header); in compute_bitstructs()
777 bi->bi_start = rgd->rd_bitbytes - bytes_left; in compute_bitstructs()
778 bi->bi_bytes = bytes; in compute_bitstructs()
779 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
782 bytes = sdp->sd_sb.sb_bsize - in compute_bitstructs()
784 bi->bi_offset = sizeof(struct gfs2_meta_header); in compute_bitstructs()
785 bi->bi_start = rgd->rd_bitbytes - bytes_left; in compute_bitstructs()
786 bi->bi_bytes = bytes; in compute_bitstructs()
787 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
790 bytes_left -= bytes; in compute_bitstructs()
795 return -EIO; in compute_bitstructs()
797 bi = rgd->rd_bits + (length - 1); in compute_bitstructs()
798 if ((bi->bi_start + bi->bi_bytes) * GFS2_NBBY != rgd->rd_data) { in compute_bitstructs()
806 (unsigned long long)rgd->rd_addr, in compute_bitstructs()
807 rgd->rd_length, in compute_bitstructs()
808 (unsigned long long)rgd->rd_data0, in compute_bitstructs()
809 rgd->rd_data, in compute_bitstructs()
810 rgd->rd_bitbytes, in compute_bitstructs()
811 bi->bi_start, bi->bi_bytes, bi->bi_offset); in compute_bitstructs()
813 return -EIO; in compute_bitstructs()
820 * gfs2_ri_total - Total up the file system space, according to the rindex.
827 struct inode *inode = sdp->sd_rindex; in gfs2_ri_total()
828 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_ri_total() local
837 error = gfs2_internal_read(ip, buf, &pos, in gfs2_ri_total()
841 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data); in gfs2_ri_total()
848 struct gfs2_sbd *sdp = rgd->rd_sbd; in rgd_insert()
849 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL; in rgd_insert()
857 if (rgd->rd_addr < cur->rd_addr) in rgd_insert()
858 newn = &((*newn)->rb_left); in rgd_insert()
859 else if (rgd->rd_addr > cur->rd_addr) in rgd_insert()
860 newn = &((*newn)->rb_right); in rgd_insert()
862 return -EEXIST; in rgd_insert()
865 rb_link_node(&rgd->rd_node, parent, newn); in rgd_insert()
866 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree); in rgd_insert()
867 sdp->sd_rgrps++; in rgd_insert()
872 * read_rindex_entry - Pull in a new resource index entry from the disk
873 * @ip: Pointer to the rindex inode
878 static int read_rindex_entry(struct gfs2_inode *ip) in read_rindex_entry() argument
880 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in read_rindex_entry()
881 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex); in read_rindex_entry()
886 if (pos >= i_size_read(&ip->i_inode)) in read_rindex_entry()
889 error = gfs2_internal_read(ip, (char *)&buf, &pos, in read_rindex_entry()
896 error = -ENOMEM; in read_rindex_entry()
900 rgd->rd_sbd = sdp; in read_rindex_entry()
901 rgd->rd_addr = be64_to_cpu(buf.ri_addr); in read_rindex_entry()
902 rgd->rd_length = be32_to_cpu(buf.ri_length); in read_rindex_entry()
903 rgd->rd_data0 = be64_to_cpu(buf.ri_data0); in read_rindex_entry()
904 rgd->rd_data = be32_to_cpu(buf.ri_data); in read_rindex_entry()
905 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes); in read_rindex_entry()
906 spin_lock_init(&rgd->rd_rsspin); in read_rindex_entry()
912 error = gfs2_glock_get(sdp, rgd->rd_addr, in read_rindex_entry()
913 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl); in read_rindex_entry()
917 rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lksb.sb_lvbptr; in read_rindex_entry()
918 rgd->rd_flags &= ~(GFS2_RDF_UPTODATE | GFS2_RDF_PREFERRED); in read_rindex_entry()
919 if (rgd->rd_data > sdp->sd_max_rg_data) in read_rindex_entry()
920 sdp->sd_max_rg_data = rgd->rd_data; in read_rindex_entry()
921 spin_lock(&sdp->sd_rindex_spin); in read_rindex_entry()
923 spin_unlock(&sdp->sd_rindex_spin); in read_rindex_entry()
925 glock_set_object(rgd->rd_gl, rgd); in read_rindex_entry()
930 gfs2_glock_put(rgd->rd_gl); in read_rindex_entry()
933 kfree(rgd->rd_bits); in read_rindex_entry()
934 rgd->rd_bits = NULL; in read_rindex_entry()
940 * set_rgrp_preferences - Run all the rgrps, selecting some we prefer to use
955 for (i = 0; i < sdp->sd_lockstruct.ls_jid; i++) in set_rgrp_preferences()
960 rgd->rd_flags |= GFS2_RDF_PREFERRED; in set_rgrp_preferences()
961 for (i = 0; i < sdp->sd_journals; i++) { in set_rgrp_preferences()
970 * gfs2_ri_update - Pull in a new resource index from the disk
971 * @ip: pointer to the rindex inode
976 static int gfs2_ri_update(struct gfs2_inode *ip) in gfs2_ri_update() argument
978 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_ri_update()
982 error = read_rindex_entry(ip); in gfs2_ri_update()
988 if (RB_EMPTY_ROOT(&sdp->sd_rindex_tree)) { in gfs2_ri_update()
990 return -ENOENT; in gfs2_ri_update()
994 sdp->sd_rindex_uptodate = 1; in gfs2_ri_update()
999 * gfs2_rindex_update - Update the rindex if required
1017 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex); in gfs2_rindex_update() local
1018 struct gfs2_glock *gl = ip->i_gl; in gfs2_rindex_update()
1024 if (!sdp->sd_rindex_uptodate) { in gfs2_rindex_update()
1031 if (!sdp->sd_rindex_uptodate) in gfs2_rindex_update()
1032 error = gfs2_ri_update(ip); in gfs2_rindex_update()
1045 rg_flags = be32_to_cpu(str->rg_flags); in gfs2_rgrp_in()
1047 rgd->rd_flags &= GFS2_RDF_MASK; in gfs2_rgrp_in()
1048 rgd->rd_flags |= rg_flags; in gfs2_rgrp_in()
1049 rgd->rd_free = be32_to_cpu(str->rg_free); in gfs2_rgrp_in()
1050 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes); in gfs2_rgrp_in()
1051 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration); in gfs2_rgrp_in()
1059 rgl->rl_magic = cpu_to_be32(GFS2_MAGIC); in gfs2_rgrp_ondisk2lvb()
1060 rgl->rl_flags = str->rg_flags; in gfs2_rgrp_ondisk2lvb()
1061 rgl->rl_free = str->rg_free; in gfs2_rgrp_ondisk2lvb()
1062 rgl->rl_dinodes = str->rg_dinodes; in gfs2_rgrp_ondisk2lvb()
1063 rgl->rl_igeneration = str->rg_igeneration; in gfs2_rgrp_ondisk2lvb()
1064 rgl->__pad = 0UL; in gfs2_rgrp_ondisk2lvb()
1073 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK); in gfs2_rgrp_out()
1074 str->rg_free = cpu_to_be32(rgd->rd_free); in gfs2_rgrp_out()
1075 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes); in gfs2_rgrp_out()
1077 str->rg_skip = 0; in gfs2_rgrp_out()
1078 else if (next->rd_addr > rgd->rd_addr) in gfs2_rgrp_out()
1079 str->rg_skip = cpu_to_be32(next->rd_addr - rgd->rd_addr); in gfs2_rgrp_out()
1080 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration); in gfs2_rgrp_out()
1081 str->rg_data0 = cpu_to_be64(rgd->rd_data0); in gfs2_rgrp_out()
1082 str->rg_data = cpu_to_be32(rgd->rd_data); in gfs2_rgrp_out()
1083 str->rg_bitbytes = cpu_to_be32(rgd->rd_bitbytes); in gfs2_rgrp_out()
1084 str->rg_crc = 0; in gfs2_rgrp_out()
1086 str->rg_crc = cpu_to_be32(crc); in gfs2_rgrp_out()
1088 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved)); in gfs2_rgrp_out()
1089 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, buf); in gfs2_rgrp_out()
1094 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; in gfs2_rgrp_lvb_valid()
1095 struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data; in gfs2_rgrp_lvb_valid()
1096 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_lvb_valid()
1099 if (rgl->rl_flags != str->rg_flags) { in gfs2_rgrp_lvb_valid()
1101 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1102 be32_to_cpu(rgl->rl_flags), be32_to_cpu(str->rg_flags)); in gfs2_rgrp_lvb_valid()
1105 if (rgl->rl_free != str->rg_free) { in gfs2_rgrp_lvb_valid()
1107 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1108 be32_to_cpu(rgl->rl_free), be32_to_cpu(str->rg_free)); in gfs2_rgrp_lvb_valid()
1111 if (rgl->rl_dinodes != str->rg_dinodes) { in gfs2_rgrp_lvb_valid()
1113 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1114 be32_to_cpu(rgl->rl_dinodes), in gfs2_rgrp_lvb_valid()
1115 be32_to_cpu(str->rg_dinodes)); in gfs2_rgrp_lvb_valid()
1118 if (rgl->rl_igeneration != str->rg_igeneration) { in gfs2_rgrp_lvb_valid()
1120 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1121 (unsigned long long)be64_to_cpu(rgl->rl_igeneration), in gfs2_rgrp_lvb_valid()
1122 (unsigned long long)be64_to_cpu(str->rg_igeneration)); in gfs2_rgrp_lvb_valid()
1131 const u32 length = rgd->rd_length; in count_unlinked()
1135 for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) { in count_unlinked()
1137 buffer = bi->bi_bh->b_data + bi->bi_offset; in count_unlinked()
1138 WARN_ON(!buffer_uptodate(bi->bi_bh)); in count_unlinked()
1139 while (goal < bi->bi_blocks) { in count_unlinked()
1140 goal = gfs2_bitfit(buffer, bi->bi_bytes, goal, in count_unlinked()
1154 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
1165 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_bh_get()
1166 struct gfs2_glock *gl = rgd->rd_gl; in gfs2_rgrp_bh_get()
1167 unsigned int length = rgd->rd_length; in gfs2_rgrp_bh_get()
1172 if (rgd->rd_bits[0].bi_bh != NULL) in gfs2_rgrp_bh_get()
1176 bi = rgd->rd_bits + x; in gfs2_rgrp_bh_get()
1177 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, 0, &bi->bi_bh); in gfs2_rgrp_bh_get()
1182 for (y = length; y--;) { in gfs2_rgrp_bh_get()
1183 bi = rgd->rd_bits + y; in gfs2_rgrp_bh_get()
1184 error = gfs2_meta_wait(sdp, bi->bi_bh); in gfs2_rgrp_bh_get()
1187 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB : in gfs2_rgrp_bh_get()
1189 error = -EIO; in gfs2_rgrp_bh_get()
1194 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) { in gfs2_rgrp_bh_get()
1196 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags); in gfs2_rgrp_bh_get()
1197 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data); in gfs2_rgrp_bh_get()
1198 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK); in gfs2_rgrp_bh_get()
1199 rgd->rd_free_clone = rgd->rd_free; in gfs2_rgrp_bh_get()
1201 rgd->rd_extfail_pt = rgd->rd_free; in gfs2_rgrp_bh_get()
1203 if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) { in gfs2_rgrp_bh_get()
1204 rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd)); in gfs2_rgrp_bh_get()
1205 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, in gfs2_rgrp_bh_get()
1206 rgd->rd_bits[0].bi_bh->b_data); in gfs2_rgrp_bh_get()
1208 else if (sdp->sd_args.ar_rgrplvb) { in gfs2_rgrp_bh_get()
1211 error = -EIO; in gfs2_rgrp_bh_get()
1214 if (rgd->rd_rgl->rl_unlinked == 0) in gfs2_rgrp_bh_get()
1215 rgd->rd_flags &= ~GFS2_RDF_CHECK; in gfs2_rgrp_bh_get()
1220 while (x--) { in gfs2_rgrp_bh_get()
1221 bi = rgd->rd_bits + x; in gfs2_rgrp_bh_get()
1222 brelse(bi->bi_bh); in gfs2_rgrp_bh_get()
1223 bi->bi_bh = NULL; in gfs2_rgrp_bh_get()
1224 gfs2_assert_warn(sdp, !bi->bi_clone); in gfs2_rgrp_bh_get()
1234 if (rgd->rd_flags & GFS2_RDF_UPTODATE) in update_rgrp_lvb()
1237 if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) in update_rgrp_lvb()
1240 rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags); in update_rgrp_lvb()
1242 rgd->rd_flags &= GFS2_RDF_MASK; in update_rgrp_lvb()
1243 rgd->rd_flags |= (rl_flags | GFS2_RDF_CHECK); in update_rgrp_lvb()
1244 if (rgd->rd_rgl->rl_unlinked == 0) in update_rgrp_lvb()
1245 rgd->rd_flags &= ~GFS2_RDF_CHECK; in update_rgrp_lvb()
1246 rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free); in update_rgrp_lvb()
1247 rgd->rd_free_clone = rgd->rd_free; in update_rgrp_lvb()
1248 rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes); in update_rgrp_lvb()
1249 rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration); in update_rgrp_lvb()
1255 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object; in gfs2_rgrp_go_lock()
1256 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_go_lock()
1258 if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb) in gfs2_rgrp_go_lock()
1264 * gfs2_rgrp_brelse - Release RG bitmaps read in with gfs2_rgrp_bh_get()
1271 int x, length = rgd->rd_length; in gfs2_rgrp_brelse()
1274 struct gfs2_bitmap *bi = rgd->rd_bits + x; in gfs2_rgrp_brelse()
1275 if (bi->bi_bh) { in gfs2_rgrp_brelse()
1276 brelse(bi->bi_bh); in gfs2_rgrp_brelse()
1277 bi->bi_bh = NULL; in gfs2_rgrp_brelse()
1286 struct super_block *sb = sdp->sd_vfs; in gfs2_rgrp_send_discards()
1295 for (x = 0; x < bi->bi_bytes; x++) { in gfs2_rgrp_send_discards()
1296 const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data; in gfs2_rgrp_send_discards()
1297 clone += bi->bi_offset; in gfs2_rgrp_send_discards()
1300 const u8 *orig = bh->b_data + bi->bi_offset + x; in gfs2_rgrp_send_discards()
1308 blk = offset + ((bi->bi_start + x) * GFS2_NBBY); in gfs2_rgrp_send_discards()
1343 if (sdp->sd_args.ar_discard) in gfs2_rgrp_send_discards()
1345 sdp->sd_args.ar_discard = 0; in gfs2_rgrp_send_discards()
1346 return -EIO; in gfs2_rgrp_send_discards()
1350 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
1361 struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev); in gfs2_fitrim()
1372 unsigned bs_shift = sdp->sd_sb.sb_bsize_shift; in gfs2_fitrim()
1375 return -EPERM; in gfs2_fitrim()
1377 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) in gfs2_fitrim()
1378 return -EROFS; in gfs2_fitrim()
1381 return -EOPNOTSUPP; in gfs2_fitrim()
1384 return -EFAULT; in gfs2_fitrim()
1393 q->limits.discard_granularity) >> bs_shift; in gfs2_fitrim()
1395 if (end <= start || minlen > sdp->sd_max_rg_data) in gfs2_fitrim()
1396 return -EINVAL; in gfs2_fitrim()
1402 && (start > rgd_end->rd_data0 + rgd_end->rd_data)) in gfs2_fitrim()
1403 return -EINVAL; /* start is beyond the end of the fs */ in gfs2_fitrim()
1407 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh); in gfs2_fitrim()
1411 if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) { in gfs2_fitrim()
1413 for (x = 0; x < rgd->rd_length; x++) { in gfs2_fitrim()
1414 struct gfs2_bitmap *bi = rgd->rd_bits + x; in gfs2_fitrim()
1416 rgd->rd_data0, NULL, bi, minlen, in gfs2_fitrim()
1428 bh = rgd->rd_bits[0].bi_bh; in gfs2_fitrim()
1429 rgd->rd_flags |= GFS2_RGF_TRIMMED; in gfs2_fitrim()
1430 gfs2_trans_add_meta(rgd->rd_gl, bh); in gfs2_fitrim()
1431 gfs2_rgrp_out(rgd, bh->b_data); in gfs2_fitrim()
1446 return -EFAULT; in gfs2_fitrim()
1452 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
1453 * @ip: the inode structure
1456 static void rs_insert(struct gfs2_inode *ip) in rs_insert() argument
1460 struct gfs2_blkreserv *rs = &ip->i_res; in rs_insert()
1461 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd; in rs_insert()
1462 u64 fsblock = gfs2_rbm_to_block(&rs->rs_rbm); in rs_insert()
1466 spin_lock(&rgd->rd_rsspin); in rs_insert()
1467 newn = &rgd->rd_rstree.rb_node; in rs_insert()
1473 rc = rs_cmp(fsblock, rs->rs_free, cur); in rs_insert()
1475 newn = &((*newn)->rb_right); in rs_insert()
1477 newn = &((*newn)->rb_left); in rs_insert()
1479 spin_unlock(&rgd->rd_rsspin); in rs_insert()
1485 rb_link_node(&rs->rs_node, parent, newn); in rs_insert()
1486 rb_insert_color(&rs->rs_node, &rgd->rd_rstree); in rs_insert()
1489 rgd->rd_reserved += rs->rs_free; /* blocks reserved */ in rs_insert()
1490 spin_unlock(&rgd->rd_rsspin); in rs_insert()
1495 * rgd_free - return the number of free blocks we can allocate.
1499 * That's the clone-free blocks (blocks that are free, not including those
1510 if (WARN_ON_ONCE(rgd->rd_reserved < rs->rs_free)) in rgd_free()
1512 tot_reserved = rgd->rd_reserved - rs->rs_free; in rgd_free()
1514 if (rgd->rd_free_clone < tot_reserved) in rgd_free()
1517 tot_free = rgd->rd_free_clone - tot_reserved; in rgd_free()
1523 * rg_mblk_search - find a group of multiple free blocks to form a reservation
1525 * @ip: pointer to the inode for which we're reserving blocks
1530 static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip, in rg_mblk_search() argument
1535 struct gfs2_blkreserv *rs = &ip->i_res; in rg_mblk_search()
1539 struct inode *inode = &ip->i_inode; in rg_mblk_search()
1541 if (S_ISDIR(inode->i_mode)) in rg_mblk_search()
1544 extlen = max_t(u32, atomic_read(&ip->i_sizehint), ap->target); in rg_mblk_search()
1547 if ((rgd->rd_free_clone < rgd->rd_reserved) || (free_blocks < extlen)) in rg_mblk_search()
1550 /* Find bitmap block that contains bits for goal block */ in rg_mblk_search()
1551 if (rgrp_contains_block(rgd, ip->i_goal)) in rg_mblk_search()
1552 goal = ip->i_goal; in rg_mblk_search()
1554 goal = rgd->rd_last_alloc + rgd->rd_data0; in rg_mblk_search()
1559 ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &extlen, ip, true); in rg_mblk_search()
1561 rs->rs_rbm = rbm; in rg_mblk_search()
1562 rs->rs_free = extlen; in rg_mblk_search()
1563 rs_insert(ip); in rg_mblk_search()
1565 if (goal == rgd->rd_last_alloc + rgd->rd_data0) in rg_mblk_search()
1566 rgd->rd_last_alloc = 0; in rg_mblk_search()
1571 * gfs2_next_unreserved_block - Return next block that is not reserved
1573 * @block: The starting block
1575 * @ip: Ignore any reservations for this inode
1577 * If the block does not appear in any reservation, then return the
1578 * block number unchanged. If it does appear in the reservation, then
1580 * first block number which is not reserved.
1583 static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block, in gfs2_next_unreserved_block() argument
1585 const struct gfs2_inode *ip) in gfs2_next_unreserved_block() argument
1591 spin_lock(&rgd->rd_rsspin); in gfs2_next_unreserved_block()
1592 n = rgd->rd_rstree.rb_node; in gfs2_next_unreserved_block()
1595 rc = rs_cmp(block, length, rs); in gfs2_next_unreserved_block()
1597 n = n->rb_left; in gfs2_next_unreserved_block()
1599 n = n->rb_right; in gfs2_next_unreserved_block()
1605 while ((rs_cmp(block, length, rs) == 0) && (&ip->i_res != rs)) { in gfs2_next_unreserved_block()
1606 block = gfs2_rbm_to_block(&rs->rs_rbm) + rs->rs_free; in gfs2_next_unreserved_block()
1607 n = n->rb_right; in gfs2_next_unreserved_block()
1614 spin_unlock(&rgd->rd_rsspin); in gfs2_next_unreserved_block()
1615 return block; in gfs2_next_unreserved_block()
1619 * gfs2_reservation_check_and_update - Check for reservations during block alloc
1621 * @ip: The inode for which we are searching for blocks
1626 * a reservation covering this block. If not then this function is a
1627 * no-op. If there is, then the position is moved to the end of the
1629 * non-reserved block.
1635 const struct gfs2_inode *ip, in gfs2_reservation_check_and_update() argument
1639 u64 block = gfs2_rbm_to_block(rbm); in gfs2_reservation_check_and_update() local
1650 if (extlen <= maxext->len) in gfs2_reservation_check_and_update()
1658 nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, ip); in gfs2_reservation_check_and_update()
1659 if (nblock == block) { in gfs2_reservation_check_and_update()
1663 if (extlen > maxext->len) { in gfs2_reservation_check_and_update()
1664 maxext->len = extlen; in gfs2_reservation_check_and_update()
1665 maxext->rbm = *rbm; in gfs2_reservation_check_and_update()
1668 nblock = block + extlen; in gfs2_reservation_check_and_update()
1677 * gfs2_rbm_find - Look for blocks of a particular state
1680 * @minext: Pointer to the requested extent length (NULL for a single block)
1682 * @ip: If set, check for reservations
1687 * - If looking for free blocks, we set GBF_FULL on each bitmap which
1689 * - If looking for free blocks, we set rd_extfail_pt on each rgrp which
1690 * has come up short on a free block search.
1692 * Returns: 0 on success, -ENOSPC if there is no block of the requested state
1696 const struct gfs2_inode *ip, bool nowrap) in gfs2_rbm_find() argument
1698 bool scan_from_start = rbm->bii == 0 && rbm->offset == 0; in gfs2_rbm_find()
1706 struct gfs2_extent maxext = { .rbm.rgd = rbm->rgd, }; in gfs2_rbm_find()
1713 last_bii = rbm->bii - (rbm->offset == 0); in gfs2_rbm_find()
1717 if ((ip == NULL || !gfs2_rs_active(&ip->i_res)) && in gfs2_rbm_find()
1718 test_bit(GBF_FULL, &bi->bi_flags) && in gfs2_rbm_find()
1722 bh = bi->bi_bh; in gfs2_rbm_find()
1723 buffer = bh->b_data + bi->bi_offset; in gfs2_rbm_find()
1725 if (state != GFS2_BLKST_UNLINKED && bi->bi_clone) in gfs2_rbm_find()
1726 buffer = bi->bi_clone + bi->bi_offset; in gfs2_rbm_find()
1727 offset = gfs2_bitfit(buffer, bi->bi_bytes, rbm->offset, state); in gfs2_rbm_find()
1729 if (state == GFS2_BLKST_FREE && rbm->offset == 0) in gfs2_rbm_find()
1730 set_bit(GBF_FULL, &bi->bi_flags); in gfs2_rbm_find()
1733 rbm->offset = offset; in gfs2_rbm_find()
1734 if (ip == NULL) in gfs2_rbm_find()
1737 ret = gfs2_reservation_check_and_update(rbm, ip, in gfs2_rbm_find()
1744 if (ret == -E2BIG) { in gfs2_rbm_find()
1745 rbm->bii = 0; in gfs2_rbm_find()
1746 rbm->offset = 0; in gfs2_rbm_find()
1752 rbm->offset = 0; in gfs2_rbm_find()
1753 rbm->bii++; in gfs2_rbm_find()
1754 if (rbm->bii == rbm->rgd->rd_length) in gfs2_rbm_find()
1755 rbm->bii = 0; in gfs2_rbm_find()
1757 if (rbm->bii == 0) { in gfs2_rbm_find()
1766 if (wrapped && rbm->bii > last_bii) in gfs2_rbm_find()
1771 return -ENOSPC; in gfs2_rbm_find()
1776 if (wrapped && (scan_from_start || rbm->bii > last_bii) && in gfs2_rbm_find()
1777 *minext < rbm->rgd->rd_extfail_pt) in gfs2_rbm_find()
1778 rbm->rgd->rd_extfail_pt = *minext; in gfs2_rbm_find()
1788 return -ENOSPC; in gfs2_rbm_find()
1792 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
1794 * @last_unlinked: block address of the last dinode we unlinked
1795 * @skip: block address we should explicitly not unlink
1803 u64 block; in try_rgrp_unlink() local
1804 struct gfs2_sbd *sdp = rgd->rd_sbd; in try_rgrp_unlink()
1806 struct gfs2_inode *ip; in try_rgrp_unlink() local
1814 if (error == -ENOSPC) in try_rgrp_unlink()
1819 block = gfs2_rbm_to_block(&rbm); in try_rgrp_unlink()
1820 if (gfs2_rbm_from_block(&rbm, block + 1)) in try_rgrp_unlink()
1822 if (*last_unlinked != NO_BLOCK && block <= *last_unlinked) in try_rgrp_unlink()
1824 if (block == skip) in try_rgrp_unlink()
1826 *last_unlinked = block; in try_rgrp_unlink()
1828 error = gfs2_glock_get(sdp, block, &gfs2_iopen_glops, CREATE, &gl); in try_rgrp_unlink()
1839 ip = gl->gl_object; in try_rgrp_unlink()
1841 if (ip || !gfs2_queue_delete_work(gl, 0)) in try_rgrp_unlink()
1851 rgd->rd_flags &= ~GFS2_RDF_CHECK; in try_rgrp_unlink()
1856 * gfs2_rgrp_congested - Use stats to figure out whether an rgrp is congested
1884 const struct gfs2_glock *gl = rgd->rd_gl; in gfs2_rgrp_congested()
1885 const struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; in gfs2_rgrp_congested()
1896 st = &per_cpu_ptr(sdp->sd_lkstats, cpu)->lkstats[LM_TYPE_RGRP]; in gfs2_rgrp_congested()
1897 if (st->stats[GFS2_LKS_SRTTB]) { in gfs2_rgrp_congested()
1898 a_srttb += st->stats[GFS2_LKS_SRTTB]; in gfs2_rgrp_congested()
1902 st = &this_cpu_ptr(sdp->sd_lkstats)->lkstats[LM_TYPE_RGRP]; in gfs2_rgrp_congested()
1905 r_dcount = st->stats[GFS2_LKS_DCOUNT]; in gfs2_rgrp_congested()
1906 var = st->stats[GFS2_LKS_SRTTVARB] + in gfs2_rgrp_congested()
1907 gl->gl_stats.stats[GFS2_LKS_SRTTVARB]; in gfs2_rgrp_congested()
1910 l_srttb = gl->gl_stats.stats[GFS2_LKS_SRTTB]; in gfs2_rgrp_congested()
1911 l_dcount = gl->gl_stats.stats[GFS2_LKS_DCOUNT]; in gfs2_rgrp_congested()
1916 srttb_diff = a_srttb - l_srttb; in gfs2_rgrp_congested()
1930 * @rs: The block reservation with the rgrp to test
1941 rs->rs_rbm.rgd->rd_gl->gl_dstamp)); in gfs2_rgrp_used_recently()
1946 static u32 gfs2_orlov_skip(const struct gfs2_inode *ip) in gfs2_orlov_skip() argument
1948 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_orlov_skip()
1952 return skip % sdp->sd_rgrps; in gfs2_orlov_skip()
1958 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_select_rgrp()
1970 * fast_to_acquire - determine if a resource group will be fast to acquire
1977 struct gfs2_glock *gl = rgd->rd_gl; in fast_to_acquire()
1979 if (gl->gl_state != LM_ST_UNLOCKED && list_empty(&gl->gl_holders) && in fast_to_acquire()
1980 !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) && in fast_to_acquire()
1981 !test_bit(GLF_DEMOTE, &gl->gl_flags)) in fast_to_acquire()
1983 if (rgd->rd_flags & GFS2_RDF_PREFERRED) in fast_to_acquire()
1989 * gfs2_inplace_reserve - Reserve space in the filesystem
1990 * @ip: the inode to reserve space for
1993 * We try our best to find an rgrp that has at least ap->target blocks
1996 * at least ap->min_target blocks available. Either way, we set ap->allowed to
2000 * -ENOMEM if a suitable rgrp can't be found
2004 int gfs2_inplace_reserve(struct gfs2_inode *ip, struct gfs2_alloc_parms *ap) in gfs2_inplace_reserve() argument
2006 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_inplace_reserve()
2008 struct gfs2_blkreserv *rs = &ip->i_res; in gfs2_inplace_reserve()
2014 if (sdp->sd_args.ar_rgrplvb) in gfs2_inplace_reserve()
2016 if (gfs2_assert_warn(sdp, ap->target)) in gfs2_inplace_reserve()
2017 return -EINVAL; in gfs2_inplace_reserve()
2019 begin = rs->rs_rbm.rgd; in gfs2_inplace_reserve()
2020 } else if (rs->rs_rbm.rgd && in gfs2_inplace_reserve()
2021 rgrp_contains_block(rs->rs_rbm.rgd, ip->i_goal)) { in gfs2_inplace_reserve()
2022 begin = rs->rs_rbm.rgd; in gfs2_inplace_reserve()
2024 check_and_update_goal(ip); in gfs2_inplace_reserve()
2025 rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1); in gfs2_inplace_reserve()
2027 if (S_ISDIR(ip->i_inode.i_mode) && (ap->aflags & GFS2_AF_ORLOV)) in gfs2_inplace_reserve()
2028 skip = gfs2_orlov_skip(ip); in gfs2_inplace_reserve()
2029 if (rs->rs_rbm.rgd == NULL) in gfs2_inplace_reserve()
2030 return -EBADSLT; in gfs2_inplace_reserve()
2035 if (!gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) { in gfs2_inplace_reserve()
2037 if (skip && skip--) in gfs2_inplace_reserve()
2041 !fast_to_acquire(rs->rs_rbm.rgd)) in gfs2_inplace_reserve()
2045 gfs2_rgrp_congested(rs->rs_rbm.rgd, loops)) in gfs2_inplace_reserve()
2048 error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl, in gfs2_inplace_reserve()
2050 &ip->i_rgd_gh); in gfs2_inplace_reserve()
2054 gfs2_rgrp_congested(rs->rs_rbm.rgd, loops)) in gfs2_inplace_reserve()
2056 if (sdp->sd_args.ar_rgrplvb) { in gfs2_inplace_reserve()
2057 error = update_rgrp_lvb(rs->rs_rbm.rgd); in gfs2_inplace_reserve()
2059 gfs2_glock_dq_uninit(&ip->i_rgd_gh); in gfs2_inplace_reserve()
2066 if ((rs->rs_rbm.rgd->rd_flags & (GFS2_RGF_NOALLOC | in gfs2_inplace_reserve()
2068 (loops == 0 && ap->target > rs->rs_rbm.rgd->rd_extfail_pt)) in gfs2_inplace_reserve()
2071 if (sdp->sd_args.ar_rgrplvb) in gfs2_inplace_reserve()
2072 gfs2_rgrp_bh_get(rs->rs_rbm.rgd); in gfs2_inplace_reserve()
2076 rg_mblk_search(rs->rs_rbm.rgd, ip, ap); in gfs2_inplace_reserve()
2083 free_blocks = rgd_free(rs->rs_rbm.rgd, rs); in gfs2_inplace_reserve()
2084 if (free_blocks >= ap->target || in gfs2_inplace_reserve()
2085 (loops == 2 && ap->min_target && in gfs2_inplace_reserve()
2086 free_blocks >= ap->min_target)) { in gfs2_inplace_reserve()
2087 ap->allowed = free_blocks; in gfs2_inplace_reserve()
2092 if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK) in gfs2_inplace_reserve()
2093 try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked, in gfs2_inplace_reserve()
2094 ip->i_no_addr); in gfs2_inplace_reserve()
2102 gfs2_glock_dq_uninit(&ip->i_rgd_gh); in gfs2_inplace_reserve()
2105 if (gfs2_select_rgrp(&rs->rs_rbm.rgd, begin)) in gfs2_inplace_reserve()
2116 if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) { in gfs2_inplace_reserve()
2117 error = gfs2_ri_update(ip); in gfs2_inplace_reserve()
2127 return -ENOSPC; in gfs2_inplace_reserve()
2131 * gfs2_inplace_release - release an inplace reservation
2132 * @ip: the inode the reservation was taken out on
2137 void gfs2_inplace_release(struct gfs2_inode *ip) in gfs2_inplace_release() argument
2139 if (gfs2_holder_initialized(&ip->i_rgd_gh)) in gfs2_inplace_release()
2140 gfs2_glock_dq_uninit(&ip->i_rgd_gh); in gfs2_inplace_release()
2144 * gfs2_alloc_extent - allocate an extent from a given bitmap
2146 * @dinode: TRUE if the first block we allocate is for a dinode
2150 * Set the found bits to @new_state to change block's allocation state.
2155 struct gfs2_rbm pos = { .rgd = rbm->rgd, }; in gfs2_alloc_extent()
2157 u64 block; in gfs2_alloc_extent() local
2161 block = gfs2_rbm_to_block(rbm); in gfs2_alloc_extent()
2162 gfs2_trans_add_meta(rbm->rgd->rd_gl, rbm_bi(rbm)->bi_bh); in gfs2_alloc_extent()
2164 block++; in gfs2_alloc_extent()
2166 ret = gfs2_rbm_from_block(&pos, block); in gfs2_alloc_extent()
2169 gfs2_trans_add_meta(pos.rgd->rd_gl, rbm_bi(&pos)->bi_bh); in gfs2_alloc_extent()
2172 block++; in gfs2_alloc_extent()
2177 * rgblk_free - Change alloc state of given block(s)
2181 * @blen: the length of the block run (all must lie within ONE RG!)
2182 * @new_state: GFS2_BLKST_XXX the after-allocation block state
2194 while (blen--) { in rgblk_free()
2197 if (!bi->bi_clone) { in rgblk_free()
2198 bi->bi_clone = kmalloc(bi->bi_bh->b_size, in rgblk_free()
2200 memcpy(bi->bi_clone + bi->bi_offset, in rgblk_free()
2201 bi->bi_bh->b_data + bi->bi_offset, in rgblk_free()
2202 bi->bi_bytes); in rgblk_free()
2204 gfs2_trans_add_meta(rbm.rgd->rd_gl, bi->bi_bh); in rgblk_free()
2213 * gfs2_rgrp_dump - print out an rgrp
2228 (unsigned long long)rgd->rd_addr, rgd->rd_flags, in gfs2_rgrp_dump()
2229 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes, in gfs2_rgrp_dump()
2230 rgd->rd_reserved, rgd->rd_extfail_pt); in gfs2_rgrp_dump()
2231 if (rgd->rd_sbd->sd_args.ar_rgrplvb) { in gfs2_rgrp_dump()
2232 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; in gfs2_rgrp_dump()
2235 be32_to_cpu(rgl->rl_flags), in gfs2_rgrp_dump()
2236 be32_to_cpu(rgl->rl_free), in gfs2_rgrp_dump()
2237 be32_to_cpu(rgl->rl_dinodes)); in gfs2_rgrp_dump()
2239 spin_lock(&rgd->rd_rsspin); in gfs2_rgrp_dump()
2240 for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) { in gfs2_rgrp_dump()
2244 spin_unlock(&rgd->rd_rsspin); in gfs2_rgrp_dump()
2249 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_error()
2250 char fs_id_buf[sizeof(sdp->sd_fsname) + 7]; in gfs2_rgrp_error()
2253 (unsigned long long)rgd->rd_addr); in gfs2_rgrp_error()
2255 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname); in gfs2_rgrp_error()
2257 rgd->rd_flags |= GFS2_RDF_ERROR; in gfs2_rgrp_error()
2261 * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
2262 * @ip: The inode we have just allocated blocks for
2271 static void gfs2_adjust_reservation(struct gfs2_inode *ip, in gfs2_adjust_reservation() argument
2274 struct gfs2_blkreserv *rs = &ip->i_res; in gfs2_adjust_reservation()
2275 struct gfs2_rgrpd *rgd = rbm->rgd; in gfs2_adjust_reservation()
2277 u64 block; in gfs2_adjust_reservation() local
2280 spin_lock(&rgd->rd_rsspin); in gfs2_adjust_reservation()
2282 if (gfs2_rbm_eq(&rs->rs_rbm, rbm)) { in gfs2_adjust_reservation()
2283 block = gfs2_rbm_to_block(rbm); in gfs2_adjust_reservation()
2284 ret = gfs2_rbm_from_block(&rs->rs_rbm, block + len); in gfs2_adjust_reservation()
2285 rlen = min(rs->rs_free, len); in gfs2_adjust_reservation()
2286 rs->rs_free -= rlen; in gfs2_adjust_reservation()
2287 rgd->rd_reserved -= rlen; in gfs2_adjust_reservation()
2289 if (rs->rs_free && !ret) in gfs2_adjust_reservation()
2291 /* We used up our block reservation, so we should in gfs2_adjust_reservation()
2293 atomic_add(RGRP_RSRV_ADDBLKS, &ip->i_sizehint); in gfs2_adjust_reservation()
2298 spin_unlock(&rgd->rd_rsspin); in gfs2_adjust_reservation()
2302 * gfs2_set_alloc_start - Set starting point for block allocation
2304 * @ip: The gfs2 inode
2309 * inode's goal block or the last allocation point in the rgrp.
2313 const struct gfs2_inode *ip, bool dinode) in gfs2_set_alloc_start() argument
2317 if (gfs2_rs_active(&ip->i_res)) { in gfs2_set_alloc_start()
2318 *rbm = ip->i_res.rs_rbm; in gfs2_set_alloc_start()
2322 if (!dinode && rgrp_contains_block(rbm->rgd, ip->i_goal)) in gfs2_set_alloc_start()
2323 goal = ip->i_goal; in gfs2_set_alloc_start()
2325 goal = rbm->rgd->rd_last_alloc + rbm->rgd->rd_data0; in gfs2_set_alloc_start()
2328 rbm->bii = 0; in gfs2_set_alloc_start()
2329 rbm->offset = 0; in gfs2_set_alloc_start()
2334 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
2335 * @ip: the inode to allocate the block for
2336 * @bn: Used to return the starting block number
2338 * @dinode: 1 if we're allocating a dinode block, else 0
2344 int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks, in gfs2_alloc_blocks() argument
2347 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_alloc_blocks()
2349 struct gfs2_rbm rbm = { .rgd = ip->i_res.rs_rbm.rgd, }; in gfs2_alloc_blocks()
2351 u64 block; /* block, within the file system scope */ in gfs2_alloc_blocks() local
2354 gfs2_set_alloc_start(&rbm, ip, dinode); in gfs2_alloc_blocks()
2355 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, NULL, ip, false); in gfs2_alloc_blocks()
2357 if (error == -ENOSPC) { in gfs2_alloc_blocks()
2358 gfs2_set_alloc_start(&rbm, ip, dinode); in gfs2_alloc_blocks()
2365 (unsigned long long)ip->i_no_addr, error, *nblocks, in gfs2_alloc_blocks()
2366 test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags), in gfs2_alloc_blocks()
2367 rbm.rgd->rd_extfail_pt); in gfs2_alloc_blocks()
2372 block = gfs2_rbm_to_block(&rbm); in gfs2_alloc_blocks()
2373 rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0; in gfs2_alloc_blocks()
2374 if (gfs2_rs_active(&ip->i_res)) in gfs2_alloc_blocks()
2375 gfs2_adjust_reservation(ip, &rbm, *nblocks); in gfs2_alloc_blocks()
2378 ndata--; in gfs2_alloc_blocks()
2381 ip->i_goal = block + ndata - 1; in gfs2_alloc_blocks()
2382 error = gfs2_meta_inode_buffer(ip, &dibh); in gfs2_alloc_blocks()
2385 (struct gfs2_dinode *)dibh->b_data; in gfs2_alloc_blocks()
2386 gfs2_trans_add_meta(ip->i_gl, dibh); in gfs2_alloc_blocks()
2387 di->di_goal_meta = di->di_goal_data = in gfs2_alloc_blocks()
2388 cpu_to_be64(ip->i_goal); in gfs2_alloc_blocks()
2392 if (rbm.rgd->rd_free < *nblocks) { in gfs2_alloc_blocks()
2397 rbm.rgd->rd_free -= *nblocks; in gfs2_alloc_blocks()
2399 rbm.rgd->rd_dinodes++; in gfs2_alloc_blocks()
2400 *generation = rbm.rgd->rd_igeneration++; in gfs2_alloc_blocks()
2402 *generation = rbm.rgd->rd_igeneration++; in gfs2_alloc_blocks()
2405 gfs2_trans_add_meta(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh); in gfs2_alloc_blocks()
2406 gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data); in gfs2_alloc_blocks()
2408 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0); in gfs2_alloc_blocks()
2410 gfs2_trans_remove_revoke(sdp, block, *nblocks); in gfs2_alloc_blocks()
2412 gfs2_quota_change(ip, *nblocks, ip->i_inode.i_uid, ip->i_inode.i_gid); in gfs2_alloc_blocks()
2414 rbm.rgd->rd_free_clone -= *nblocks; in gfs2_alloc_blocks()
2415 trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks, in gfs2_alloc_blocks()
2417 *bn = block; in gfs2_alloc_blocks()
2422 return -EIO; in gfs2_alloc_blocks()
2426 * __gfs2_free_blocks - free a contiguous run of block(s)
2427 * @ip: the inode these blocks are being freed from
2429 * @bstart: first block of a run of contiguous blocks
2430 * @blen: the length of the block run
2435 void __gfs2_free_blocks(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd, in __gfs2_free_blocks() argument
2438 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in __gfs2_free_blocks()
2441 trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE); in __gfs2_free_blocks()
2442 rgd->rd_free += blen; in __gfs2_free_blocks()
2443 rgd->rd_flags &= ~GFS2_RGF_TRIMMED; in __gfs2_free_blocks()
2444 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh); in __gfs2_free_blocks()
2445 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); in __gfs2_free_blocks()
2448 if (meta || ip->i_depth || gfs2_is_jdata(ip)) in __gfs2_free_blocks()
2449 gfs2_journal_wipe(ip, bstart, blen); in __gfs2_free_blocks()
2453 * gfs2_free_meta - free a contiguous run of data block(s)
2454 * @ip: the inode these blocks are being freed from
2456 * @bstart: first block of a run of contiguous blocks
2457 * @blen: the length of the block run
2461 void gfs2_free_meta(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd, in gfs2_free_meta() argument
2464 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_free_meta()
2466 __gfs2_free_blocks(ip, rgd, bstart, blen, 1); in gfs2_free_meta()
2468 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); in gfs2_free_meta()
2473 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_unlink_di() local
2476 u64 blkno = ip->i_no_addr; in gfs2_unlink_di()
2482 trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED); in gfs2_unlink_di()
2483 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh); in gfs2_unlink_di()
2484 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); in gfs2_unlink_di()
2485 be32_add_cpu(&rgd->rd_rgl->rl_unlinked, 1); in gfs2_unlink_di()
2488 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) in gfs2_free_di() argument
2490 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_free_di()
2492 rgblk_free(sdp, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE); in gfs2_free_di()
2493 if (!rgd->rd_dinodes) in gfs2_free_di()
2495 rgd->rd_dinodes--; in gfs2_free_di()
2496 rgd->rd_free++; in gfs2_free_di()
2498 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh); in gfs2_free_di()
2499 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); in gfs2_free_di()
2500 be32_add_cpu(&rgd->rd_rgl->rl_unlinked, -1); in gfs2_free_di()
2502 gfs2_statfs_change(sdp, 0, +1, -1); in gfs2_free_di()
2503 trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE); in gfs2_free_di()
2504 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid); in gfs2_free_di()
2505 gfs2_journal_wipe(ip, ip->i_no_addr, 1); in gfs2_free_di()
2509 * gfs2_check_blk_type - Check the type of a block
2511 * @no_addr: The block number to check
2512 * @type: The block type we are looking for
2514 * Returns: 0 if the block type matches the expected type
2515 * -ESTALE if it doesn't match
2516 * or -ve errno if something went wrong while checking
2524 int error = -EINVAL; in gfs2_check_blk_type()
2530 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh); in gfs2_check_blk_type()
2538 error = -ESTALE; in gfs2_check_blk_type()
2548 * gfs2_rlist_add - add a RG to a list of RGs
2549 * @ip: the inode
2551 * @block: the block
2553 * Figure out what RG a block belongs to and add that RG to the list
2559 void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist, in gfs2_rlist_add() argument
2560 u64 block) in gfs2_rlist_add() argument
2562 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_rlist_add()
2568 if (gfs2_assert_warn(sdp, !rlist->rl_ghs)) in gfs2_rlist_add()
2575 if (rlist->rl_rgrps) { in gfs2_rlist_add()
2576 rgd = rlist->rl_rgd[rlist->rl_rgrps - 1]; in gfs2_rlist_add()
2577 if (rgrp_contains_block(rgd, block)) in gfs2_rlist_add()
2579 rgd = gfs2_blk2rgrpd(sdp, block, 1); in gfs2_rlist_add()
2581 rgd = ip->i_res.rs_rbm.rgd; in gfs2_rlist_add()
2582 if (!rgd || !rgrp_contains_block(rgd, block)) in gfs2_rlist_add()
2583 rgd = gfs2_blk2rgrpd(sdp, block, 1); in gfs2_rlist_add()
2587 fs_err(sdp, "rlist_add: no rgrp for block %llu\n", in gfs2_rlist_add()
2588 (unsigned long long)block); in gfs2_rlist_add()
2592 for (x = 0; x < rlist->rl_rgrps; x++) { in gfs2_rlist_add()
2593 if (rlist->rl_rgd[x] == rgd) { in gfs2_rlist_add()
2594 swap(rlist->rl_rgd[x], in gfs2_rlist_add()
2595 rlist->rl_rgd[rlist->rl_rgrps - 1]); in gfs2_rlist_add()
2600 if (rlist->rl_rgrps == rlist->rl_space) { in gfs2_rlist_add()
2601 new_space = rlist->rl_space + 10; in gfs2_rlist_add()
2606 if (rlist->rl_rgd) { in gfs2_rlist_add()
2607 memcpy(tmp, rlist->rl_rgd, in gfs2_rlist_add()
2608 rlist->rl_space * sizeof(struct gfs2_rgrpd *)); in gfs2_rlist_add()
2609 kfree(rlist->rl_rgd); in gfs2_rlist_add()
2612 rlist->rl_space = new_space; in gfs2_rlist_add()
2613 rlist->rl_rgd = tmp; in gfs2_rlist_add()
2616 rlist->rl_rgd[rlist->rl_rgrps++] = rgd; in gfs2_rlist_add()
2620 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
2632 rlist->rl_ghs = kmalloc_array(rlist->rl_rgrps, in gfs2_rlist_alloc()
2635 for (x = 0; x < rlist->rl_rgrps; x++) in gfs2_rlist_alloc()
2636 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, in gfs2_rlist_alloc()
2638 &rlist->rl_ghs[x]); in gfs2_rlist_alloc()
2642 * gfs2_rlist_free - free a resource group list
2651 kfree(rlist->rl_rgd); in gfs2_rlist_free()
2653 if (rlist->rl_ghs) { in gfs2_rlist_free()
2654 for (x = 0; x < rlist->rl_rgrps; x++) in gfs2_rlist_free()
2655 gfs2_holder_uninit(&rlist->rl_ghs[x]); in gfs2_rlist_free()
2656 kfree(rlist->rl_ghs); in gfs2_rlist_free()
2657 rlist->rl_ghs = NULL; in gfs2_rlist_free()