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
669 void gfs2_rs_delete(struct gfs2_inode *ip) in gfs2_rs_delete() argument
671 struct inode *inode = &ip->i_inode; in gfs2_rs_delete()
673 down_write(&ip->i_rw_mutex); in gfs2_rs_delete()
674 if (atomic_read(&inode->i_writecount) <= 1) in gfs2_rs_delete()
675 gfs2_rs_deltree(&ip->i_res); in gfs2_rs_delete()
676 up_write(&ip->i_rw_mutex); in gfs2_rs_delete()
680 * return_all_reservations - return all reserved blocks back to the rgrp.
685 * all of their corresponding "no-fly zones".
692 spin_lock(&rgd->rd_rsspin); in return_all_reservations()
693 while ((n = rb_first(&rgd->rd_rstree))) { in return_all_reservations()
697 spin_unlock(&rgd->rd_rsspin); in return_all_reservations()
706 while ((n = rb_first(&sdp->sd_rindex_tree))) { in gfs2_clear_rgrpd()
708 gl = rgd->rd_gl; in gfs2_clear_rgrpd()
710 rb_erase(n, &sdp->sd_rindex_tree); in gfs2_clear_rgrpd()
713 if (gl->gl_state != LM_ST_UNLOCKED) { in gfs2_clear_rgrpd()
715 flush_delayed_work(&gl->gl_work); in gfs2_clear_rgrpd()
724 kfree(rgd->rd_bits); in gfs2_clear_rgrpd()
725 rgd->rd_bits = NULL; in gfs2_clear_rgrpd()
731 * gfs2_compute_bitstructs - Compute the bitmap sizes
734 * Calculates bitmap descriptors, one for each block that contains bitmap data
741 struct gfs2_sbd *sdp = rgd->rd_sbd; in compute_bitstructs()
743 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */ in compute_bitstructs()
748 return -EINVAL; in compute_bitstructs()
750 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS); in compute_bitstructs()
751 if (!rgd->rd_bits) in compute_bitstructs()
752 return -ENOMEM; in compute_bitstructs()
754 bytes_left = rgd->rd_bitbytes; in compute_bitstructs()
757 bi = rgd->rd_bits + x; in compute_bitstructs()
759 bi->bi_flags = 0; in compute_bitstructs()
760 /* small rgrp; bitmap stored completely in header block */ in compute_bitstructs()
763 bi->bi_offset = sizeof(struct gfs2_rgrp); in compute_bitstructs()
764 bi->bi_start = 0; in compute_bitstructs()
765 bi->bi_bytes = bytes; in compute_bitstructs()
766 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
767 /* header block */ in compute_bitstructs()
769 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp); in compute_bitstructs()
770 bi->bi_offset = sizeof(struct gfs2_rgrp); in compute_bitstructs()
771 bi->bi_start = 0; in compute_bitstructs()
772 bi->bi_bytes = bytes; in compute_bitstructs()
773 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
774 /* last block */ in compute_bitstructs()
777 bi->bi_offset = sizeof(struct gfs2_meta_header); in compute_bitstructs()
778 bi->bi_start = rgd->rd_bitbytes - bytes_left; in compute_bitstructs()
779 bi->bi_bytes = bytes; in compute_bitstructs()
780 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
783 bytes = sdp->sd_sb.sb_bsize - in compute_bitstructs()
785 bi->bi_offset = sizeof(struct gfs2_meta_header); in compute_bitstructs()
786 bi->bi_start = rgd->rd_bitbytes - bytes_left; in compute_bitstructs()
787 bi->bi_bytes = bytes; in compute_bitstructs()
788 bi->bi_blocks = bytes * GFS2_NBBY; in compute_bitstructs()
791 bytes_left -= bytes; in compute_bitstructs()
796 return -EIO; in compute_bitstructs()
798 bi = rgd->rd_bits + (length - 1); in compute_bitstructs()
799 if ((bi->bi_start + bi->bi_bytes) * GFS2_NBBY != rgd->rd_data) { in compute_bitstructs()
807 (unsigned long long)rgd->rd_addr, in compute_bitstructs()
808 rgd->rd_length, in compute_bitstructs()
809 (unsigned long long)rgd->rd_data0, in compute_bitstructs()
810 rgd->rd_data, in compute_bitstructs()
811 rgd->rd_bitbytes, in compute_bitstructs()
812 bi->bi_start, bi->bi_bytes, bi->bi_offset); in compute_bitstructs()
814 return -EIO; in compute_bitstructs()
821 * gfs2_ri_total - Total up the file system space, according to the rindex.
828 struct inode *inode = sdp->sd_rindex; in gfs2_ri_total()
829 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_ri_total() local
838 error = gfs2_internal_read(ip, buf, &pos, in gfs2_ri_total()
842 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data); in gfs2_ri_total()
849 struct gfs2_sbd *sdp = rgd->rd_sbd; in rgd_insert()
850 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL; in rgd_insert()
858 if (rgd->rd_addr < cur->rd_addr) in rgd_insert()
859 newn = &((*newn)->rb_left); in rgd_insert()
860 else if (rgd->rd_addr > cur->rd_addr) in rgd_insert()
861 newn = &((*newn)->rb_right); in rgd_insert()
863 return -EEXIST; in rgd_insert()
866 rb_link_node(&rgd->rd_node, parent, newn); in rgd_insert()
867 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree); in rgd_insert()
868 sdp->sd_rgrps++; in rgd_insert()
873 * read_rindex_entry - Pull in a new resource index entry from the disk
874 * @ip: Pointer to the rindex inode
879 static int read_rindex_entry(struct gfs2_inode *ip) in read_rindex_entry() argument
881 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in read_rindex_entry()
882 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex); in read_rindex_entry()
887 if (pos >= i_size_read(&ip->i_inode)) in read_rindex_entry()
890 error = gfs2_internal_read(ip, (char *)&buf, &pos, in read_rindex_entry()
897 error = -ENOMEM; in read_rindex_entry()
901 rgd->rd_sbd = sdp; in read_rindex_entry()
902 rgd->rd_addr = be64_to_cpu(buf.ri_addr); in read_rindex_entry()
903 rgd->rd_length = be32_to_cpu(buf.ri_length); in read_rindex_entry()
904 rgd->rd_data0 = be64_to_cpu(buf.ri_data0); in read_rindex_entry()
905 rgd->rd_data = be32_to_cpu(buf.ri_data); in read_rindex_entry()
906 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes); in read_rindex_entry()
907 spin_lock_init(&rgd->rd_rsspin); in read_rindex_entry()
909 error = gfs2_glock_get(sdp, rgd->rd_addr, in read_rindex_entry()
910 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl); in read_rindex_entry()
918 rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lksb.sb_lvbptr; in read_rindex_entry()
919 rgd->rd_flags &= ~(GFS2_RDF_UPTODATE | GFS2_RDF_PREFERRED); in read_rindex_entry()
920 if (rgd->rd_data > sdp->sd_max_rg_data) in read_rindex_entry()
921 sdp->sd_max_rg_data = rgd->rd_data; in read_rindex_entry()
922 spin_lock(&sdp->sd_rindex_spin); in read_rindex_entry()
924 spin_unlock(&sdp->sd_rindex_spin); in read_rindex_entry()
926 glock_set_object(rgd->rd_gl, rgd); in read_rindex_entry()
932 gfs2_glock_put(rgd->rd_gl); in read_rindex_entry()
935 kfree(rgd->rd_bits); in read_rindex_entry()
936 rgd->rd_bits = NULL; in read_rindex_entry()
942 * set_rgrp_preferences - Run all the rgrps, selecting some we prefer to use
957 for (i = 0; i < sdp->sd_lockstruct.ls_jid; i++) in set_rgrp_preferences()
962 rgd->rd_flags |= GFS2_RDF_PREFERRED; in set_rgrp_preferences()
963 for (i = 0; i < sdp->sd_journals; i++) { in set_rgrp_preferences()
972 * gfs2_ri_update - Pull in a new resource index from the disk
973 * @ip: pointer to the rindex inode
978 static int gfs2_ri_update(struct gfs2_inode *ip) in gfs2_ri_update() argument
980 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_ri_update()
984 error = read_rindex_entry(ip); in gfs2_ri_update()
990 if (RB_EMPTY_ROOT(&sdp->sd_rindex_tree)) { in gfs2_ri_update()
992 return -ENOENT; in gfs2_ri_update()
996 sdp->sd_rindex_uptodate = 1; in gfs2_ri_update()
1001 * gfs2_rindex_update - Update the rindex if required
1019 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex); in gfs2_rindex_update() local
1020 struct gfs2_glock *gl = ip->i_gl; in gfs2_rindex_update()
1026 if (!sdp->sd_rindex_uptodate) { in gfs2_rindex_update()
1033 if (!sdp->sd_rindex_uptodate) in gfs2_rindex_update()
1034 error = gfs2_ri_update(ip); in gfs2_rindex_update()
1047 rg_flags = be32_to_cpu(str->rg_flags); in gfs2_rgrp_in()
1049 rgd->rd_flags &= GFS2_RDF_MASK; in gfs2_rgrp_in()
1050 rgd->rd_flags |= rg_flags; in gfs2_rgrp_in()
1051 rgd->rd_free = be32_to_cpu(str->rg_free); in gfs2_rgrp_in()
1052 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes); in gfs2_rgrp_in()
1053 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration); in gfs2_rgrp_in()
1061 rgl->rl_magic = cpu_to_be32(GFS2_MAGIC); in gfs2_rgrp_ondisk2lvb()
1062 rgl->rl_flags = str->rg_flags; in gfs2_rgrp_ondisk2lvb()
1063 rgl->rl_free = str->rg_free; in gfs2_rgrp_ondisk2lvb()
1064 rgl->rl_dinodes = str->rg_dinodes; in gfs2_rgrp_ondisk2lvb()
1065 rgl->rl_igeneration = str->rg_igeneration; in gfs2_rgrp_ondisk2lvb()
1066 rgl->__pad = 0UL; in gfs2_rgrp_ondisk2lvb()
1075 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK); in gfs2_rgrp_out()
1076 str->rg_free = cpu_to_be32(rgd->rd_free); in gfs2_rgrp_out()
1077 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes); in gfs2_rgrp_out()
1079 str->rg_skip = 0; in gfs2_rgrp_out()
1080 else if (next->rd_addr > rgd->rd_addr) in gfs2_rgrp_out()
1081 str->rg_skip = cpu_to_be32(next->rd_addr - rgd->rd_addr); in gfs2_rgrp_out()
1082 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration); in gfs2_rgrp_out()
1083 str->rg_data0 = cpu_to_be64(rgd->rd_data0); in gfs2_rgrp_out()
1084 str->rg_data = cpu_to_be32(rgd->rd_data); in gfs2_rgrp_out()
1085 str->rg_bitbytes = cpu_to_be32(rgd->rd_bitbytes); in gfs2_rgrp_out()
1086 str->rg_crc = 0; in gfs2_rgrp_out()
1088 str->rg_crc = cpu_to_be32(crc); in gfs2_rgrp_out()
1090 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved)); in gfs2_rgrp_out()
1091 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, buf); in gfs2_rgrp_out()
1096 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; in gfs2_rgrp_lvb_valid()
1097 struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data; in gfs2_rgrp_lvb_valid()
1098 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_lvb_valid()
1101 if (rgl->rl_flags != str->rg_flags) { in gfs2_rgrp_lvb_valid()
1103 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1104 be32_to_cpu(rgl->rl_flags), be32_to_cpu(str->rg_flags)); in gfs2_rgrp_lvb_valid()
1107 if (rgl->rl_free != str->rg_free) { in gfs2_rgrp_lvb_valid()
1109 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1110 be32_to_cpu(rgl->rl_free), be32_to_cpu(str->rg_free)); in gfs2_rgrp_lvb_valid()
1113 if (rgl->rl_dinodes != str->rg_dinodes) { in gfs2_rgrp_lvb_valid()
1115 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1116 be32_to_cpu(rgl->rl_dinodes), in gfs2_rgrp_lvb_valid()
1117 be32_to_cpu(str->rg_dinodes)); in gfs2_rgrp_lvb_valid()
1120 if (rgl->rl_igeneration != str->rg_igeneration) { in gfs2_rgrp_lvb_valid()
1122 (unsigned long long)rgd->rd_addr, in gfs2_rgrp_lvb_valid()
1123 (unsigned long long)be64_to_cpu(rgl->rl_igeneration), in gfs2_rgrp_lvb_valid()
1124 (unsigned long long)be64_to_cpu(str->rg_igeneration)); in gfs2_rgrp_lvb_valid()
1133 const u32 length = rgd->rd_length; in count_unlinked()
1137 for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) { in count_unlinked()
1139 buffer = bi->bi_bh->b_data + bi->bi_offset; in count_unlinked()
1140 WARN_ON(!buffer_uptodate(bi->bi_bh)); in count_unlinked()
1141 while (goal < bi->bi_blocks) { in count_unlinked()
1142 goal = gfs2_bitfit(buffer, bi->bi_bytes, goal, in count_unlinked()
1156 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
1167 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_bh_get()
1168 struct gfs2_glock *gl = rgd->rd_gl; in gfs2_rgrp_bh_get()
1169 unsigned int length = rgd->rd_length; in gfs2_rgrp_bh_get()
1174 if (rgd->rd_bits[0].bi_bh != NULL) in gfs2_rgrp_bh_get()
1178 bi = rgd->rd_bits + x; in gfs2_rgrp_bh_get()
1179 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, 0, &bi->bi_bh); in gfs2_rgrp_bh_get()
1184 for (y = length; y--;) { in gfs2_rgrp_bh_get()
1185 bi = rgd->rd_bits + y; in gfs2_rgrp_bh_get()
1186 error = gfs2_meta_wait(sdp, bi->bi_bh); in gfs2_rgrp_bh_get()
1189 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB : in gfs2_rgrp_bh_get()
1191 error = -EIO; in gfs2_rgrp_bh_get()
1196 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) { in gfs2_rgrp_bh_get()
1198 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags); in gfs2_rgrp_bh_get()
1199 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data); in gfs2_rgrp_bh_get()
1200 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK); in gfs2_rgrp_bh_get()
1201 rgd->rd_free_clone = rgd->rd_free; in gfs2_rgrp_bh_get()
1203 rgd->rd_extfail_pt = rgd->rd_free; in gfs2_rgrp_bh_get()
1205 if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) { in gfs2_rgrp_bh_get()
1206 rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd)); in gfs2_rgrp_bh_get()
1207 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, in gfs2_rgrp_bh_get()
1208 rgd->rd_bits[0].bi_bh->b_data); in gfs2_rgrp_bh_get()
1210 else if (sdp->sd_args.ar_rgrplvb) { in gfs2_rgrp_bh_get()
1213 error = -EIO; in gfs2_rgrp_bh_get()
1216 if (rgd->rd_rgl->rl_unlinked == 0) in gfs2_rgrp_bh_get()
1217 rgd->rd_flags &= ~GFS2_RDF_CHECK; in gfs2_rgrp_bh_get()
1222 while (x--) { in gfs2_rgrp_bh_get()
1223 bi = rgd->rd_bits + x; in gfs2_rgrp_bh_get()
1224 brelse(bi->bi_bh); in gfs2_rgrp_bh_get()
1225 bi->bi_bh = NULL; in gfs2_rgrp_bh_get()
1226 gfs2_assert_warn(sdp, !bi->bi_clone); in gfs2_rgrp_bh_get()
1236 if (rgd->rd_flags & GFS2_RDF_UPTODATE) in update_rgrp_lvb()
1239 if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) in update_rgrp_lvb()
1242 rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags); in update_rgrp_lvb()
1244 rgd->rd_flags &= GFS2_RDF_MASK; in update_rgrp_lvb()
1245 rgd->rd_flags |= (rl_flags | GFS2_RDF_CHECK); in update_rgrp_lvb()
1246 if (rgd->rd_rgl->rl_unlinked == 0) in update_rgrp_lvb()
1247 rgd->rd_flags &= ~GFS2_RDF_CHECK; in update_rgrp_lvb()
1248 rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free); in update_rgrp_lvb()
1249 rgd->rd_free_clone = rgd->rd_free; in update_rgrp_lvb()
1251 rgd->rd_extfail_pt = rgd->rd_free; in update_rgrp_lvb()
1252 rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes); in update_rgrp_lvb()
1253 rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration); in update_rgrp_lvb()
1259 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object; in gfs2_rgrp_go_lock()
1260 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_go_lock()
1262 if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb) in gfs2_rgrp_go_lock()
1268 * gfs2_rgrp_brelse - Release RG bitmaps read in with gfs2_rgrp_bh_get()
1275 int x, length = rgd->rd_length; in gfs2_rgrp_brelse()
1278 struct gfs2_bitmap *bi = rgd->rd_bits + x; in gfs2_rgrp_brelse()
1279 if (bi->bi_bh) { in gfs2_rgrp_brelse()
1280 brelse(bi->bi_bh); in gfs2_rgrp_brelse()
1281 bi->bi_bh = NULL; in gfs2_rgrp_brelse()
1290 struct super_block *sb = sdp->sd_vfs; in gfs2_rgrp_send_discards()
1299 for (x = 0; x < bi->bi_bytes; x++) { in gfs2_rgrp_send_discards()
1300 const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data; in gfs2_rgrp_send_discards()
1301 clone += bi->bi_offset; in gfs2_rgrp_send_discards()
1304 const u8 *orig = bh->b_data + bi->bi_offset + x; in gfs2_rgrp_send_discards()
1312 blk = offset + ((bi->bi_start + x) * GFS2_NBBY); in gfs2_rgrp_send_discards()
1347 if (sdp->sd_args.ar_discard) in gfs2_rgrp_send_discards()
1349 sdp->sd_args.ar_discard = 0; in gfs2_rgrp_send_discards()
1350 return -EIO; in gfs2_rgrp_send_discards()
1354 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
1365 struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev); in gfs2_fitrim()
1376 unsigned bs_shift = sdp->sd_sb.sb_bsize_shift; in gfs2_fitrim()
1379 return -EPERM; in gfs2_fitrim()
1381 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) in gfs2_fitrim()
1382 return -EROFS; in gfs2_fitrim()
1385 return -EOPNOTSUPP; in gfs2_fitrim()
1388 return -EFAULT; in gfs2_fitrim()
1396 minlen = max_t(u64, r.minlen, sdp->sd_sb.sb_bsize); in gfs2_fitrim()
1398 q->limits.discard_granularity) >> bs_shift; in gfs2_fitrim()
1400 if (end <= start || minlen > sdp->sd_max_rg_data) in gfs2_fitrim()
1401 return -EINVAL; in gfs2_fitrim()
1407 && (start > rgd_end->rd_data0 + rgd_end->rd_data)) in gfs2_fitrim()
1408 return -EINVAL; /* start is beyond the end of the fs */ in gfs2_fitrim()
1412 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh); in gfs2_fitrim()
1416 if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) { in gfs2_fitrim()
1418 for (x = 0; x < rgd->rd_length; x++) { in gfs2_fitrim()
1419 struct gfs2_bitmap *bi = rgd->rd_bits + x; in gfs2_fitrim()
1421 rgd->rd_data0, NULL, bi, minlen, in gfs2_fitrim()
1433 bh = rgd->rd_bits[0].bi_bh; in gfs2_fitrim()
1434 rgd->rd_flags |= GFS2_RGF_TRIMMED; in gfs2_fitrim()
1435 gfs2_trans_add_meta(rgd->rd_gl, bh); in gfs2_fitrim()
1436 gfs2_rgrp_out(rgd, bh->b_data); in gfs2_fitrim()
1451 return -EFAULT; in gfs2_fitrim()
1457 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
1458 * @ip: the inode structure
1461 static void rs_insert(struct gfs2_inode *ip) in rs_insert() argument
1465 struct gfs2_blkreserv *rs = &ip->i_res; in rs_insert()
1466 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd; in rs_insert()
1467 u64 fsblock = gfs2_rbm_to_block(&rs->rs_rbm); in rs_insert()
1471 spin_lock(&rgd->rd_rsspin); in rs_insert()
1472 newn = &rgd->rd_rstree.rb_node; in rs_insert()
1478 rc = rs_cmp(fsblock, rs->rs_free, cur); in rs_insert()
1480 newn = &((*newn)->rb_right); in rs_insert()
1482 newn = &((*newn)->rb_left); in rs_insert()
1484 spin_unlock(&rgd->rd_rsspin); in rs_insert()
1490 rb_link_node(&rs->rs_node, parent, newn); in rs_insert()
1491 rb_insert_color(&rs->rs_node, &rgd->rd_rstree); in rs_insert()
1494 rgd->rd_reserved += rs->rs_free; /* blocks reserved */ in rs_insert()
1495 spin_unlock(&rgd->rd_rsspin); in rs_insert()
1500 * rgd_free - return the number of free blocks we can allocate.
1504 * That's the clone-free blocks (blocks that are free, not including those
1515 if (WARN_ON_ONCE(rgd->rd_reserved < rs->rs_free)) in rgd_free()
1517 tot_reserved = rgd->rd_reserved - rs->rs_free; in rgd_free()
1519 if (rgd->rd_free_clone < tot_reserved) in rgd_free()
1522 tot_free = rgd->rd_free_clone - tot_reserved; in rgd_free()
1528 * rg_mblk_search - find a group of multiple free blocks to form a reservation
1530 * @ip: pointer to the inode for which we're reserving blocks
1535 static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip, in rg_mblk_search() argument
1540 struct gfs2_blkreserv *rs = &ip->i_res; in rg_mblk_search()
1544 struct inode *inode = &ip->i_inode; in rg_mblk_search()
1546 if (S_ISDIR(inode->i_mode)) in rg_mblk_search()
1549 extlen = max_t(u32, atomic_read(&ip->i_sizehint), ap->target); in rg_mblk_search()
1552 if ((rgd->rd_free_clone < rgd->rd_reserved) || (free_blocks < extlen)) in rg_mblk_search()
1555 /* Find bitmap block that contains bits for goal block */ in rg_mblk_search()
1556 if (rgrp_contains_block(rgd, ip->i_goal)) in rg_mblk_search()
1557 goal = ip->i_goal; in rg_mblk_search()
1559 goal = rgd->rd_last_alloc + rgd->rd_data0; in rg_mblk_search()
1564 ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &extlen, ip, true); in rg_mblk_search()
1566 rs->rs_rbm = rbm; in rg_mblk_search()
1567 rs->rs_free = extlen; in rg_mblk_search()
1568 rs_insert(ip); in rg_mblk_search()
1570 if (goal == rgd->rd_last_alloc + rgd->rd_data0) in rg_mblk_search()
1571 rgd->rd_last_alloc = 0; in rg_mblk_search()
1576 * gfs2_next_unreserved_block - Return next block that is not reserved
1578 * @block: The starting block
1580 * @ip: Ignore any reservations for this inode
1582 * If the block does not appear in any reservation, then return the
1583 * block number unchanged. If it does appear in the reservation, then
1585 * first block number which is not reserved.
1588 static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block, in gfs2_next_unreserved_block() argument
1590 const struct gfs2_inode *ip) in gfs2_next_unreserved_block() argument
1596 spin_lock(&rgd->rd_rsspin); in gfs2_next_unreserved_block()
1597 n = rgd->rd_rstree.rb_node; in gfs2_next_unreserved_block()
1600 rc = rs_cmp(block, length, rs); in gfs2_next_unreserved_block()
1602 n = n->rb_left; in gfs2_next_unreserved_block()
1604 n = n->rb_right; in gfs2_next_unreserved_block()
1610 while ((rs_cmp(block, length, rs) == 0) && (&ip->i_res != rs)) { in gfs2_next_unreserved_block()
1611 block = gfs2_rbm_to_block(&rs->rs_rbm) + rs->rs_free; in gfs2_next_unreserved_block()
1612 n = n->rb_right; in gfs2_next_unreserved_block()
1619 spin_unlock(&rgd->rd_rsspin); in gfs2_next_unreserved_block()
1620 return block; in gfs2_next_unreserved_block()
1624 * gfs2_reservation_check_and_update - Check for reservations during block alloc
1626 * @ip: The inode for which we are searching for blocks
1631 * a reservation covering this block. If not then this function is a
1632 * no-op. If there is, then the position is moved to the end of the
1634 * non-reserved block.
1640 const struct gfs2_inode *ip, in gfs2_reservation_check_and_update() argument
1644 u64 block = gfs2_rbm_to_block(rbm); in gfs2_reservation_check_and_update() local
1655 if (extlen <= maxext->len) in gfs2_reservation_check_and_update()
1663 nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, ip); in gfs2_reservation_check_and_update()
1664 if (nblock == block) { in gfs2_reservation_check_and_update()
1668 if (extlen > maxext->len) { in gfs2_reservation_check_and_update()
1669 maxext->len = extlen; in gfs2_reservation_check_and_update()
1670 maxext->rbm = *rbm; in gfs2_reservation_check_and_update()
1673 nblock = block + extlen; in gfs2_reservation_check_and_update()
1682 * gfs2_rbm_find - Look for blocks of a particular state
1687 * @ip: If set, check for reservations
1692 * - If looking for free blocks, we set GBF_FULL on each bitmap which
1694 * - If looking for free blocks, we set rd_extfail_pt on each rgrp which
1695 * has come up short on a free block search.
1697 * Returns: 0 on success, -ENOSPC if there is no block of the requested state
1701 const struct gfs2_inode *ip, bool nowrap) in gfs2_rbm_find() argument
1703 bool scan_from_start = rbm->bii == 0 && rbm->offset == 0; in gfs2_rbm_find()
1711 struct gfs2_extent maxext = { .rbm.rgd = rbm->rgd, }; in gfs2_rbm_find()
1718 last_bii = rbm->bii - (rbm->offset == 0); in gfs2_rbm_find()
1722 if (test_bit(GBF_FULL, &bi->bi_flags) && in gfs2_rbm_find()
1726 bh = bi->bi_bh; in gfs2_rbm_find()
1727 buffer = bh->b_data + bi->bi_offset; in gfs2_rbm_find()
1729 if (state != GFS2_BLKST_UNLINKED && bi->bi_clone) in gfs2_rbm_find()
1730 buffer = bi->bi_clone + bi->bi_offset; in gfs2_rbm_find()
1731 offset = gfs2_bitfit(buffer, bi->bi_bytes, rbm->offset, state); in gfs2_rbm_find()
1733 if (state == GFS2_BLKST_FREE && rbm->offset == 0) in gfs2_rbm_find()
1734 set_bit(GBF_FULL, &bi->bi_flags); in gfs2_rbm_find()
1737 rbm->offset = offset; in gfs2_rbm_find()
1738 if (ip == NULL) in gfs2_rbm_find()
1741 ret = gfs2_reservation_check_and_update(rbm, ip, *minext, in gfs2_rbm_find()
1747 if (ret == -E2BIG) { in gfs2_rbm_find()
1748 rbm->bii = 0; in gfs2_rbm_find()
1749 rbm->offset = 0; in gfs2_rbm_find()
1755 rbm->offset = 0; in gfs2_rbm_find()
1756 rbm->bii++; in gfs2_rbm_find()
1757 if (rbm->bii == rbm->rgd->rd_length) in gfs2_rbm_find()
1758 rbm->bii = 0; in gfs2_rbm_find()
1760 if (rbm->bii == 0) { in gfs2_rbm_find()
1769 if (wrapped && rbm->bii > last_bii) in gfs2_rbm_find()
1774 return -ENOSPC; in gfs2_rbm_find()
1779 if (wrapped && (scan_from_start || rbm->bii > last_bii) && in gfs2_rbm_find()
1780 *minext < rbm->rgd->rd_extfail_pt) in gfs2_rbm_find()
1781 rbm->rgd->rd_extfail_pt = *minext - 1; in gfs2_rbm_find()
1791 return -ENOSPC; in gfs2_rbm_find()
1795 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
1797 * @last_unlinked: block address of the last dinode we unlinked
1798 * @skip: block address we should explicitly not unlink
1806 u64 block; in try_rgrp_unlink() local
1807 struct gfs2_sbd *sdp = rgd->rd_sbd; in try_rgrp_unlink()
1809 struct gfs2_inode *ip; in try_rgrp_unlink() local
1817 if (error == -ENOSPC) in try_rgrp_unlink()
1822 block = gfs2_rbm_to_block(&rbm); in try_rgrp_unlink()
1823 if (gfs2_rbm_from_block(&rbm, block + 1)) in try_rgrp_unlink()
1825 if (*last_unlinked != NO_BLOCK && block <= *last_unlinked) in try_rgrp_unlink()
1827 if (block == skip) in try_rgrp_unlink()
1829 *last_unlinked = block; in try_rgrp_unlink()
1831 error = gfs2_glock_get(sdp, block, &gfs2_iopen_glops, CREATE, &gl); in try_rgrp_unlink()
1842 ip = gl->gl_object; in try_rgrp_unlink()
1844 if (ip || !gfs2_queue_delete_work(gl, 0)) in try_rgrp_unlink()
1854 rgd->rd_flags &= ~GFS2_RDF_CHECK; in try_rgrp_unlink()
1859 * gfs2_rgrp_congested - Use stats to figure out whether an rgrp is congested
1887 const struct gfs2_glock *gl = rgd->rd_gl; in gfs2_rgrp_congested()
1888 const struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; in gfs2_rgrp_congested()
1899 st = &per_cpu_ptr(sdp->sd_lkstats, cpu)->lkstats[LM_TYPE_RGRP]; in gfs2_rgrp_congested()
1900 if (st->stats[GFS2_LKS_SRTTB]) { in gfs2_rgrp_congested()
1901 a_srttb += st->stats[GFS2_LKS_SRTTB]; in gfs2_rgrp_congested()
1905 st = &this_cpu_ptr(sdp->sd_lkstats)->lkstats[LM_TYPE_RGRP]; in gfs2_rgrp_congested()
1908 r_dcount = st->stats[GFS2_LKS_DCOUNT]; in gfs2_rgrp_congested()
1909 var = st->stats[GFS2_LKS_SRTTVARB] + in gfs2_rgrp_congested()
1910 gl->gl_stats.stats[GFS2_LKS_SRTTVARB]; in gfs2_rgrp_congested()
1913 l_srttb = gl->gl_stats.stats[GFS2_LKS_SRTTB]; in gfs2_rgrp_congested()
1914 l_dcount = gl->gl_stats.stats[GFS2_LKS_DCOUNT]; in gfs2_rgrp_congested()
1919 srttb_diff = a_srttb - l_srttb; in gfs2_rgrp_congested()
1933 * @rs: The block reservation with the rgrp to test
1944 rs->rs_rbm.rgd->rd_gl->gl_dstamp)); in gfs2_rgrp_used_recently()
1949 static u32 gfs2_orlov_skip(const struct gfs2_inode *ip) in gfs2_orlov_skip() argument
1951 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_orlov_skip()
1955 return skip % sdp->sd_rgrps; in gfs2_orlov_skip()
1961 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_select_rgrp()
1973 * fast_to_acquire - determine if a resource group will be fast to acquire
1980 struct gfs2_glock *gl = rgd->rd_gl; in fast_to_acquire()
1982 if (gl->gl_state != LM_ST_UNLOCKED && list_empty(&gl->gl_holders) && in fast_to_acquire()
1983 !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) && in fast_to_acquire()
1984 !test_bit(GLF_DEMOTE, &gl->gl_flags)) in fast_to_acquire()
1986 if (rgd->rd_flags & GFS2_RDF_PREFERRED) in fast_to_acquire()
1992 * gfs2_inplace_reserve - Reserve space in the filesystem
1993 * @ip: the inode to reserve space for
1996 * We try our best to find an rgrp that has at least ap->target blocks
1999 * at least ap->min_target blocks available. Either way, we set ap->allowed to
2003 * -ENOMEM if a suitable rgrp can't be found
2007 int gfs2_inplace_reserve(struct gfs2_inode *ip, struct gfs2_alloc_parms *ap) in gfs2_inplace_reserve() argument
2009 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_inplace_reserve()
2011 struct gfs2_blkreserv *rs = &ip->i_res; in gfs2_inplace_reserve()
2017 if (sdp->sd_args.ar_rgrplvb) in gfs2_inplace_reserve()
2019 if (gfs2_assert_warn(sdp, ap->target)) in gfs2_inplace_reserve()
2020 return -EINVAL; in gfs2_inplace_reserve()
2022 begin = rs->rs_rbm.rgd; in gfs2_inplace_reserve()
2023 } else if (rs->rs_rbm.rgd && in gfs2_inplace_reserve()
2024 rgrp_contains_block(rs->rs_rbm.rgd, ip->i_goal)) { in gfs2_inplace_reserve()
2025 begin = rs->rs_rbm.rgd; in gfs2_inplace_reserve()
2027 check_and_update_goal(ip); in gfs2_inplace_reserve()
2028 rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1); in gfs2_inplace_reserve()
2030 if (S_ISDIR(ip->i_inode.i_mode) && (ap->aflags & GFS2_AF_ORLOV)) in gfs2_inplace_reserve()
2031 skip = gfs2_orlov_skip(ip); in gfs2_inplace_reserve()
2032 if (rs->rs_rbm.rgd == NULL) in gfs2_inplace_reserve()
2033 return -EBADSLT; in gfs2_inplace_reserve()
2038 if (!gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) { in gfs2_inplace_reserve()
2040 if (skip && skip--) in gfs2_inplace_reserve()
2044 !fast_to_acquire(rs->rs_rbm.rgd)) in gfs2_inplace_reserve()
2048 gfs2_rgrp_congested(rs->rs_rbm.rgd, loops)) in gfs2_inplace_reserve()
2051 error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl, in gfs2_inplace_reserve()
2053 &ip->i_rgd_gh); in gfs2_inplace_reserve()
2057 gfs2_rgrp_congested(rs->rs_rbm.rgd, loops)) in gfs2_inplace_reserve()
2059 if (sdp->sd_args.ar_rgrplvb) { in gfs2_inplace_reserve()
2060 error = update_rgrp_lvb(rs->rs_rbm.rgd); in gfs2_inplace_reserve()
2062 gfs2_glock_dq_uninit(&ip->i_rgd_gh); in gfs2_inplace_reserve()
2069 if ((rs->rs_rbm.rgd->rd_flags & (GFS2_RGF_NOALLOC | in gfs2_inplace_reserve()
2071 (loops == 0 && ap->target > rs->rs_rbm.rgd->rd_extfail_pt)) in gfs2_inplace_reserve()
2074 if (sdp->sd_args.ar_rgrplvb) in gfs2_inplace_reserve()
2075 gfs2_rgrp_bh_get(rs->rs_rbm.rgd); in gfs2_inplace_reserve()
2079 rg_mblk_search(rs->rs_rbm.rgd, ip, ap); in gfs2_inplace_reserve()
2086 free_blocks = rgd_free(rs->rs_rbm.rgd, rs); in gfs2_inplace_reserve()
2087 if (free_blocks >= ap->target || in gfs2_inplace_reserve()
2088 (loops == 2 && ap->min_target && in gfs2_inplace_reserve()
2089 free_blocks >= ap->min_target)) { in gfs2_inplace_reserve()
2090 ap->allowed = free_blocks; in gfs2_inplace_reserve()
2095 if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK) in gfs2_inplace_reserve()
2096 try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked, in gfs2_inplace_reserve()
2097 ip->i_no_addr); in gfs2_inplace_reserve()
2105 gfs2_glock_dq_uninit(&ip->i_rgd_gh); in gfs2_inplace_reserve()
2108 if (gfs2_select_rgrp(&rs->rs_rbm.rgd, begin)) in gfs2_inplace_reserve()
2119 if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) { in gfs2_inplace_reserve()
2120 error = gfs2_ri_update(ip); in gfs2_inplace_reserve()
2130 return -ENOSPC; in gfs2_inplace_reserve()
2134 * gfs2_inplace_release - release an inplace reservation
2135 * @ip: the inode the reservation was taken out on
2140 void gfs2_inplace_release(struct gfs2_inode *ip) in gfs2_inplace_release() argument
2142 if (gfs2_holder_initialized(&ip->i_rgd_gh)) in gfs2_inplace_release()
2143 gfs2_glock_dq_uninit(&ip->i_rgd_gh); in gfs2_inplace_release()
2147 * gfs2_alloc_extent - allocate an extent from a given bitmap
2149 * @dinode: TRUE if the first block we allocate is for a dinode
2153 * Set the found bits to @new_state to change block's allocation state.
2158 struct gfs2_rbm pos = { .rgd = rbm->rgd, }; in gfs2_alloc_extent()
2160 u64 block; in gfs2_alloc_extent() local
2164 block = gfs2_rbm_to_block(rbm); in gfs2_alloc_extent()
2165 gfs2_trans_add_meta(rbm->rgd->rd_gl, rbm_bi(rbm)->bi_bh); in gfs2_alloc_extent()
2167 block++; in gfs2_alloc_extent()
2169 ret = gfs2_rbm_from_block(&pos, block); in gfs2_alloc_extent()
2172 gfs2_trans_add_meta(pos.rgd->rd_gl, rbm_bi(&pos)->bi_bh); in gfs2_alloc_extent()
2175 block++; in gfs2_alloc_extent()
2180 * rgblk_free - Change alloc state of given block(s)
2184 * @blen: the length of the block run (all must lie within ONE RG!)
2185 * @new_state: GFS2_BLKST_XXX the after-allocation block state
2197 while (blen--) { in rgblk_free()
2200 if (!bi->bi_clone) { in rgblk_free()
2201 bi->bi_clone = kmalloc(bi->bi_bh->b_size, in rgblk_free()
2203 memcpy(bi->bi_clone + bi->bi_offset, in rgblk_free()
2204 bi->bi_bh->b_data + bi->bi_offset, in rgblk_free()
2205 bi->bi_bytes); in rgblk_free()
2207 gfs2_trans_add_meta(rbm.rgd->rd_gl, bi->bi_bh); in rgblk_free()
2216 * gfs2_rgrp_dump - print out an rgrp
2231 (unsigned long long)rgd->rd_addr, rgd->rd_flags, in gfs2_rgrp_dump()
2232 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes, in gfs2_rgrp_dump()
2233 rgd->rd_reserved, rgd->rd_extfail_pt); in gfs2_rgrp_dump()
2234 if (rgd->rd_sbd->sd_args.ar_rgrplvb && rgd->rd_rgl) { in gfs2_rgrp_dump()
2235 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; in gfs2_rgrp_dump()
2238 be32_to_cpu(rgl->rl_flags), in gfs2_rgrp_dump()
2239 be32_to_cpu(rgl->rl_free), in gfs2_rgrp_dump()
2240 be32_to_cpu(rgl->rl_dinodes)); in gfs2_rgrp_dump()
2242 spin_lock(&rgd->rd_rsspin); in gfs2_rgrp_dump()
2243 for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) { in gfs2_rgrp_dump()
2247 spin_unlock(&rgd->rd_rsspin); in gfs2_rgrp_dump()
2252 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_rgrp_error()
2253 char fs_id_buf[sizeof(sdp->sd_fsname) + 7]; in gfs2_rgrp_error()
2256 (unsigned long long)rgd->rd_addr); in gfs2_rgrp_error()
2258 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname); in gfs2_rgrp_error()
2260 rgd->rd_flags |= GFS2_RDF_ERROR; in gfs2_rgrp_error()
2264 * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
2265 * @ip: The inode we have just allocated blocks for
2274 static void gfs2_adjust_reservation(struct gfs2_inode *ip, in gfs2_adjust_reservation() argument
2277 struct gfs2_blkreserv *rs = &ip->i_res; in gfs2_adjust_reservation()
2278 struct gfs2_rgrpd *rgd = rbm->rgd; in gfs2_adjust_reservation()
2280 u64 block; in gfs2_adjust_reservation() local
2283 spin_lock(&rgd->rd_rsspin); in gfs2_adjust_reservation()
2285 if (gfs2_rbm_eq(&rs->rs_rbm, rbm)) { in gfs2_adjust_reservation()
2286 block = gfs2_rbm_to_block(rbm); in gfs2_adjust_reservation()
2287 ret = gfs2_rbm_from_block(&rs->rs_rbm, block + len); in gfs2_adjust_reservation()
2288 rlen = min(rs->rs_free, len); in gfs2_adjust_reservation()
2289 rs->rs_free -= rlen; in gfs2_adjust_reservation()
2290 rgd->rd_reserved -= rlen; in gfs2_adjust_reservation()
2292 if (rs->rs_free && !ret) in gfs2_adjust_reservation()
2294 /* We used up our block reservation, so we should in gfs2_adjust_reservation()
2296 atomic_add(RGRP_RSRV_ADDBLKS, &ip->i_sizehint); in gfs2_adjust_reservation()
2301 spin_unlock(&rgd->rd_rsspin); in gfs2_adjust_reservation()
2305 * gfs2_set_alloc_start - Set starting point for block allocation
2307 * @ip: The gfs2 inode
2312 * inode's goal block or the last allocation point in the rgrp.
2316 const struct gfs2_inode *ip, bool dinode) in gfs2_set_alloc_start() argument
2320 if (gfs2_rs_active(&ip->i_res)) { in gfs2_set_alloc_start()
2321 *rbm = ip->i_res.rs_rbm; in gfs2_set_alloc_start()
2325 if (!dinode && rgrp_contains_block(rbm->rgd, ip->i_goal)) in gfs2_set_alloc_start()
2326 goal = ip->i_goal; in gfs2_set_alloc_start()
2328 goal = rbm->rgd->rd_last_alloc + rbm->rgd->rd_data0; in gfs2_set_alloc_start()
2331 rbm->bii = 0; in gfs2_set_alloc_start()
2332 rbm->offset = 0; in gfs2_set_alloc_start()
2337 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
2338 * @ip: the inode to allocate the block for
2339 * @bn: Used to return the starting block number
2341 * @dinode: 1 if we're allocating a dinode block, else 0
2347 int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks, in gfs2_alloc_blocks() argument
2350 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_alloc_blocks()
2352 struct gfs2_rbm rbm = { .rgd = ip->i_res.rs_rbm.rgd, }; in gfs2_alloc_blocks()
2354 u64 block; /* block, within the file system scope */ in gfs2_alloc_blocks() local
2358 gfs2_set_alloc_start(&rbm, ip, dinode); in gfs2_alloc_blocks()
2359 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &minext, ip, false); in gfs2_alloc_blocks()
2361 if (error == -ENOSPC) { in gfs2_alloc_blocks()
2362 gfs2_set_alloc_start(&rbm, ip, dinode); in gfs2_alloc_blocks()
2369 (unsigned long long)ip->i_no_addr, error, *nblocks, in gfs2_alloc_blocks()
2370 test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags), in gfs2_alloc_blocks()
2371 rbm.rgd->rd_extfail_pt); in gfs2_alloc_blocks()
2376 block = gfs2_rbm_to_block(&rbm); in gfs2_alloc_blocks()
2377 rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0; in gfs2_alloc_blocks()
2378 if (gfs2_rs_active(&ip->i_res)) in gfs2_alloc_blocks()
2379 gfs2_adjust_reservation(ip, &rbm, *nblocks); in gfs2_alloc_blocks()
2382 ndata--; in gfs2_alloc_blocks()
2385 ip->i_goal = block + ndata - 1; in gfs2_alloc_blocks()
2386 error = gfs2_meta_inode_buffer(ip, &dibh); in gfs2_alloc_blocks()
2389 (struct gfs2_dinode *)dibh->b_data; in gfs2_alloc_blocks()
2390 gfs2_trans_add_meta(ip->i_gl, dibh); in gfs2_alloc_blocks()
2391 di->di_goal_meta = di->di_goal_data = in gfs2_alloc_blocks()
2392 cpu_to_be64(ip->i_goal); in gfs2_alloc_blocks()
2396 if (rbm.rgd->rd_free < *nblocks) { in gfs2_alloc_blocks()
2401 rbm.rgd->rd_free -= *nblocks; in gfs2_alloc_blocks()
2403 rbm.rgd->rd_dinodes++; in gfs2_alloc_blocks()
2404 *generation = rbm.rgd->rd_igeneration++; in gfs2_alloc_blocks()
2406 *generation = rbm.rgd->rd_igeneration++; in gfs2_alloc_blocks()
2409 gfs2_trans_add_meta(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh); in gfs2_alloc_blocks()
2410 gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data); in gfs2_alloc_blocks()
2412 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0); in gfs2_alloc_blocks()
2414 gfs2_trans_remove_revoke(sdp, block, *nblocks); in gfs2_alloc_blocks()
2416 gfs2_quota_change(ip, *nblocks, ip->i_inode.i_uid, ip->i_inode.i_gid); in gfs2_alloc_blocks()
2418 rbm.rgd->rd_free_clone -= *nblocks; in gfs2_alloc_blocks()
2419 trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks, in gfs2_alloc_blocks()
2421 *bn = block; in gfs2_alloc_blocks()
2426 return -EIO; in gfs2_alloc_blocks()
2430 * __gfs2_free_blocks - free a contiguous run of block(s)
2431 * @ip: the inode these blocks are being freed from
2433 * @bstart: first block of a run of contiguous blocks
2434 * @blen: the length of the block run
2439 void __gfs2_free_blocks(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd, in __gfs2_free_blocks() argument
2442 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in __gfs2_free_blocks()
2445 trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE); in __gfs2_free_blocks()
2446 rgd->rd_free += blen; in __gfs2_free_blocks()
2447 rgd->rd_flags &= ~GFS2_RGF_TRIMMED; in __gfs2_free_blocks()
2448 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh); in __gfs2_free_blocks()
2449 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); in __gfs2_free_blocks()
2452 if (meta || ip->i_depth || gfs2_is_jdata(ip)) in __gfs2_free_blocks()
2453 gfs2_journal_wipe(ip, bstart, blen); in __gfs2_free_blocks()
2457 * gfs2_free_meta - free a contiguous run of data block(s)
2458 * @ip: the inode these blocks are being freed from
2460 * @bstart: first block of a run of contiguous blocks
2461 * @blen: the length of the block run
2465 void gfs2_free_meta(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd, in gfs2_free_meta() argument
2468 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_free_meta()
2470 __gfs2_free_blocks(ip, rgd, bstart, blen, 1); in gfs2_free_meta()
2472 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); in gfs2_free_meta()
2477 struct gfs2_inode *ip = GFS2_I(inode); in gfs2_unlink_di() local
2480 u64 blkno = ip->i_no_addr; in gfs2_unlink_di()
2486 trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED); in gfs2_unlink_di()
2487 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh); in gfs2_unlink_di()
2488 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); in gfs2_unlink_di()
2489 be32_add_cpu(&rgd->rd_rgl->rl_unlinked, 1); in gfs2_unlink_di()
2492 void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) in gfs2_free_di() argument
2494 struct gfs2_sbd *sdp = rgd->rd_sbd; in gfs2_free_di()
2496 rgblk_free(sdp, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE); in gfs2_free_di()
2497 if (!rgd->rd_dinodes) in gfs2_free_di()
2499 rgd->rd_dinodes--; in gfs2_free_di()
2500 rgd->rd_free++; in gfs2_free_di()
2502 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh); in gfs2_free_di()
2503 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); in gfs2_free_di()
2504 be32_add_cpu(&rgd->rd_rgl->rl_unlinked, -1); in gfs2_free_di()
2506 gfs2_statfs_change(sdp, 0, +1, -1); in gfs2_free_di()
2507 trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE); in gfs2_free_di()
2508 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid); in gfs2_free_di()
2509 gfs2_journal_wipe(ip, ip->i_no_addr, 1); in gfs2_free_di()
2513 * gfs2_check_blk_type - Check the type of a block
2515 * @no_addr: The block number to check
2516 * @type: The block type we are looking for
2518 * Returns: 0 if the block type matches the expected type
2519 * -ESTALE if it doesn't match
2520 * or -ve errno if something went wrong while checking
2528 int error = -EINVAL; in gfs2_check_blk_type()
2534 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh); in gfs2_check_blk_type()
2542 error = -ESTALE; in gfs2_check_blk_type()
2552 * gfs2_rlist_add - add a RG to a list of RGs
2553 * @ip: the inode
2555 * @block: the block
2557 * Figure out what RG a block belongs to and add that RG to the list
2563 void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist, in gfs2_rlist_add() argument
2564 u64 block) in gfs2_rlist_add() argument
2566 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); in gfs2_rlist_add()
2572 if (gfs2_assert_warn(sdp, !rlist->rl_ghs)) in gfs2_rlist_add()
2579 if (rlist->rl_rgrps) { in gfs2_rlist_add()
2580 rgd = rlist->rl_rgd[rlist->rl_rgrps - 1]; in gfs2_rlist_add()
2581 if (rgrp_contains_block(rgd, block)) in gfs2_rlist_add()
2583 rgd = gfs2_blk2rgrpd(sdp, block, 1); in gfs2_rlist_add()
2585 rgd = ip->i_res.rs_rbm.rgd; in gfs2_rlist_add()
2586 if (!rgd || !rgrp_contains_block(rgd, block)) in gfs2_rlist_add()
2587 rgd = gfs2_blk2rgrpd(sdp, block, 1); in gfs2_rlist_add()
2591 fs_err(sdp, "rlist_add: no rgrp for block %llu\n", in gfs2_rlist_add()
2592 (unsigned long long)block); in gfs2_rlist_add()
2596 for (x = 0; x < rlist->rl_rgrps; x++) { in gfs2_rlist_add()
2597 if (rlist->rl_rgd[x] == rgd) { in gfs2_rlist_add()
2598 swap(rlist->rl_rgd[x], in gfs2_rlist_add()
2599 rlist->rl_rgd[rlist->rl_rgrps - 1]); in gfs2_rlist_add()
2604 if (rlist->rl_rgrps == rlist->rl_space) { in gfs2_rlist_add()
2605 new_space = rlist->rl_space + 10; in gfs2_rlist_add()
2610 if (rlist->rl_rgd) { in gfs2_rlist_add()
2611 memcpy(tmp, rlist->rl_rgd, in gfs2_rlist_add()
2612 rlist->rl_space * sizeof(struct gfs2_rgrpd *)); in gfs2_rlist_add()
2613 kfree(rlist->rl_rgd); in gfs2_rlist_add()
2616 rlist->rl_space = new_space; in gfs2_rlist_add()
2617 rlist->rl_rgd = tmp; in gfs2_rlist_add()
2620 rlist->rl_rgd[rlist->rl_rgrps++] = rgd; in gfs2_rlist_add()
2624 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
2636 rlist->rl_ghs = kmalloc_array(rlist->rl_rgrps, in gfs2_rlist_alloc()
2639 for (x = 0; x < rlist->rl_rgrps; x++) in gfs2_rlist_alloc()
2640 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, in gfs2_rlist_alloc()
2642 &rlist->rl_ghs[x]); in gfs2_rlist_alloc()
2646 * gfs2_rlist_free - free a resource group list
2655 kfree(rlist->rl_rgd); in gfs2_rlist_free()
2657 if (rlist->rl_ghs) { in gfs2_rlist_free()
2658 for (x = 0; x < rlist->rl_rgrps; x++) in gfs2_rlist_free()
2659 gfs2_holder_uninit(&rlist->rl_ghs[x]); in gfs2_rlist_free()
2660 kfree(rlist->rl_ghs); in gfs2_rlist_free()
2661 rlist->rl_ghs = NULL; in gfs2_rlist_free()