aboutsummaryrefslogtreecommitdiffstats
path: root/fs/gfs2/rgrp.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/gfs2/rgrp.c')
-rw-r--r--fs/gfs2/rgrp.c1203
1 files changed, 587 insertions, 616 deletions
diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
index c9ed814eeb6f..3cc402ce6fea 100644
--- a/fs/gfs2/rgrp.c
+++ b/fs/gfs2/rgrp.c
@@ -35,9 +35,6 @@
35#define BFITNOENT ((u32)~0) 35#define BFITNOENT ((u32)~0)
36#define NO_BLOCK ((u64)~0) 36#define NO_BLOCK ((u64)~0)
37 37
38#define RSRV_CONTENTION_FACTOR 4
39#define RGRP_RSRV_MAX_CONTENDERS 2
40
41#if BITS_PER_LONG == 32 38#if BITS_PER_LONG == 32
42#define LBITMASK (0x55555555UL) 39#define LBITMASK (0x55555555UL)
43#define LBITSKIP55 (0x55555555UL) 40#define LBITSKIP55 (0x55555555UL)
@@ -67,53 +64,48 @@ static const char valid_change[16] = {
67 1, 0, 0, 0 64 1, 0, 0, 0
68}; 65};
69 66
70static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, 67static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 minext,
71 unsigned char old_state, 68 const struct gfs2_inode *ip, bool nowrap);
72 struct gfs2_bitmap **rbi); 69
73 70
74/** 71/**
75 * gfs2_setbit - Set a bit in the bitmaps 72 * gfs2_setbit - Set a bit in the bitmaps
76 * @rgd: the resource group descriptor 73 * @rbm: The position of the bit to set
77 * @buf2: the clone buffer that holds the bitmaps 74 * @do_clone: Also set the clone bitmap, if it exists
78 * @bi: the bitmap structure
79 * @block: the block to set
80 * @new_state: the new state of the block 75 * @new_state: the new state of the block
81 * 76 *
82 */ 77 */
83 78
84static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf2, 79static inline void gfs2_setbit(const struct gfs2_rbm *rbm, bool do_clone,
85 struct gfs2_bitmap *bi, u32 block,
86 unsigned char new_state) 80 unsigned char new_state)
87{ 81{
88 unsigned char *byte1, *byte2, *end, cur_state; 82 unsigned char *byte1, *byte2, *end, cur_state;
89 unsigned int buflen = bi->bi_len; 83 unsigned int buflen = rbm->bi->bi_len;
90 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; 84 const unsigned int bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
91 85
92 byte1 = bi->bi_bh->b_data + bi->bi_offset + (block / GFS2_NBBY); 86 byte1 = rbm->bi->bi_bh->b_data + rbm->bi->bi_offset + (rbm->offset / GFS2_NBBY);
93 end = bi->bi_bh->b_data + bi->bi_offset + buflen; 87 end = rbm->bi->bi_bh->b_data + rbm->bi->bi_offset + buflen;
94 88
95 BUG_ON(byte1 >= end); 89 BUG_ON(byte1 >= end);
96 90
97 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK; 91 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
98 92
99 if (unlikely(!valid_change[new_state * 4 + cur_state])) { 93 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
100 printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, " 94 printk(KERN_WARNING "GFS2: buf_blk = 0x%x old_state=%d, "
101 "new_state=%d\n", 95 "new_state=%d\n", rbm->offset, cur_state, new_state);
102 (unsigned long long)block, cur_state, new_state); 96 printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%x\n",
103 printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n", 97 (unsigned long long)rbm->rgd->rd_addr,
104 (unsigned long long)rgd->rd_addr, 98 rbm->bi->bi_start);
105 (unsigned long)bi->bi_start); 99 printk(KERN_WARNING "GFS2: bi_offset=0x%x bi_len=0x%x\n",
106 printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n", 100 rbm->bi->bi_offset, rbm->bi->bi_len);
107 (unsigned long)bi->bi_offset,
108 (unsigned long)bi->bi_len);
109 dump_stack(); 101 dump_stack();
110 gfs2_consist_rgrpd(rgd); 102 gfs2_consist_rgrpd(rbm->rgd);
111 return; 103 return;
112 } 104 }
113 *byte1 ^= (cur_state ^ new_state) << bit; 105 *byte1 ^= (cur_state ^ new_state) << bit;
114 106
115 if (buf2) { 107 if (do_clone && rbm->bi->bi_clone) {
116 byte2 = buf2 + bi->bi_offset + (block / GFS2_NBBY); 108 byte2 = rbm->bi->bi_clone + rbm->bi->bi_offset + (rbm->offset / GFS2_NBBY);
117 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK; 109 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
118 *byte2 ^= (cur_state ^ new_state) << bit; 110 *byte2 ^= (cur_state ^ new_state) << bit;
119 } 111 }
@@ -121,30 +113,21 @@ static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf2,
121 113
122/** 114/**
123 * gfs2_testbit - test a bit in the bitmaps 115 * gfs2_testbit - test a bit in the bitmaps
124 * @rgd: the resource group descriptor 116 * @rbm: The bit to test
125 * @buffer: the buffer that holds the bitmaps
126 * @buflen: the length (in bytes) of the buffer
127 * @block: the block to read
128 * 117 *
118 * Returns: The two bit block state of the requested bit
129 */ 119 */
130 120
131static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, 121static inline u8 gfs2_testbit(const struct gfs2_rbm *rbm)
132 const unsigned char *buffer,
133 unsigned int buflen, u32 block)
134{ 122{
135 const unsigned char *byte, *end; 123 const u8 *buffer = rbm->bi->bi_bh->b_data + rbm->bi->bi_offset;
136 unsigned char cur_state; 124 const u8 *byte;
137 unsigned int bit; 125 unsigned int bit;
138 126
139 byte = buffer + (block / GFS2_NBBY); 127 byte = buffer + (rbm->offset / GFS2_NBBY);
140 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; 128 bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
141 end = buffer + buflen;
142
143 gfs2_assert(rgd->rd_sbd, byte < end);
144 129
145 cur_state = (*byte >> bit) & GFS2_BIT_MASK; 130 return (*byte >> bit) & GFS2_BIT_MASK;
146
147 return cur_state;
148} 131}
149 132
150/** 133/**
@@ -192,7 +175,7 @@ static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
192 */ 175 */
193static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs) 176static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
194{ 177{
195 u64 startblk = gfs2_rs_startblk(rs); 178 u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm);
196 179
197 if (blk >= startblk + rs->rs_free) 180 if (blk >= startblk + rs->rs_free)
198 return 1; 181 return 1;
@@ -202,36 +185,6 @@ static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
202} 185}
203 186
204/** 187/**
205 * rs_find - Find a rgrp multi-block reservation that contains a given block
206 * @rgd: The rgrp
207 * @rgblk: The block we're looking for, relative to the rgrp
208 */
209static struct gfs2_blkreserv *rs_find(struct gfs2_rgrpd *rgd, u32 rgblk)
210{
211 struct rb_node **newn;
212 int rc;
213 u64 fsblk = rgblk + rgd->rd_data0;
214
215 spin_lock(&rgd->rd_rsspin);
216 newn = &rgd->rd_rstree.rb_node;
217 while (*newn) {
218 struct gfs2_blkreserv *cur =
219 rb_entry(*newn, struct gfs2_blkreserv, rs_node);
220 rc = rs_cmp(fsblk, 1, cur);
221 if (rc < 0)
222 newn = &((*newn)->rb_left);
223 else if (rc > 0)
224 newn = &((*newn)->rb_right);
225 else {
226 spin_unlock(&rgd->rd_rsspin);
227 return cur;
228 }
229 }
230 spin_unlock(&rgd->rd_rsspin);
231 return NULL;
232}
233
234/**
235 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing 188 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
236 * a block in a given allocation state. 189 * a block in a given allocation state.
237 * @buf: the buffer that holds the bitmaps 190 * @buf: the buffer that holds the bitmaps
@@ -262,8 +215,6 @@ static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
262 u64 mask = 0x5555555555555555ULL; 215 u64 mask = 0x5555555555555555ULL;
263 u32 bit; 216 u32 bit;
264 217
265 BUG_ON(state > 3);
266
267 /* Mask off bits we don't care about at the start of the search */ 218 /* Mask off bits we don't care about at the start of the search */
268 mask <<= spoint; 219 mask <<= spoint;
269 tmp = gfs2_bit_search(ptr, mask, state); 220 tmp = gfs2_bit_search(ptr, mask, state);
@@ -285,6 +236,131 @@ static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
285} 236}
286 237
287/** 238/**
239 * gfs2_rbm_from_block - Set the rbm based upon rgd and block number
240 * @rbm: The rbm with rgd already set correctly
241 * @block: The block number (filesystem relative)
242 *
243 * This sets the bi and offset members of an rbm based on a
244 * resource group and a filesystem relative block number. The
245 * resource group must be set in the rbm on entry, the bi and
246 * offset members will be set by this function.
247 *
248 * Returns: 0 on success, or an error code
249 */
250
251static int gfs2_rbm_from_block(struct gfs2_rbm *rbm, u64 block)
252{
253 u64 rblock = block - rbm->rgd->rd_data0;
254 u32 goal = (u32)rblock;
255 int x;
256
257 if (WARN_ON_ONCE(rblock > UINT_MAX))
258 return -EINVAL;
259 if (block >= rbm->rgd->rd_data0 + rbm->rgd->rd_data)
260 return -E2BIG;
261
262 for (x = 0; x < rbm->rgd->rd_length; x++) {
263 rbm->bi = rbm->rgd->rd_bits + x;
264 if (goal < (rbm->bi->bi_start + rbm->bi->bi_len) * GFS2_NBBY) {
265 rbm->offset = goal - (rbm->bi->bi_start * GFS2_NBBY);
266 break;
267 }
268 }
269
270 return 0;
271}
272
273/**
274 * gfs2_unaligned_extlen - Look for free blocks which are not byte aligned
275 * @rbm: Position to search (value/result)
276 * @n_unaligned: Number of unaligned blocks to check
277 * @len: Decremented for each block found (terminate on zero)
278 *
279 * Returns: true if a non-free block is encountered
280 */
281
282static bool gfs2_unaligned_extlen(struct gfs2_rbm *rbm, u32 n_unaligned, u32 *len)
283{
284 u64 block;
285 u32 n;
286 u8 res;
287
288 for (n = 0; n < n_unaligned; n++) {
289 res = gfs2_testbit(rbm);
290 if (res != GFS2_BLKST_FREE)
291 return true;
292 (*len)--;
293 if (*len == 0)
294 return true;
295 block = gfs2_rbm_to_block(rbm);
296 if (gfs2_rbm_from_block(rbm, block + 1))
297 return true;
298 }
299
300 return false;
301}
302
303/**
304 * gfs2_free_extlen - Return extent length of free blocks
305 * @rbm: Starting position
306 * @len: Max length to check
307 *
308 * Starting at the block specified by the rbm, see how many free blocks
309 * there are, not reading more than len blocks ahead. This can be done
310 * using memchr_inv when the blocks are byte aligned, but has to be done
311 * on a block by block basis in case of unaligned blocks. Also this
312 * function can cope with bitmap boundaries (although it must stop on
313 * a resource group boundary)
314 *
315 * Returns: Number of free blocks in the extent
316 */
317
318static u32 gfs2_free_extlen(const struct gfs2_rbm *rrbm, u32 len)
319{
320 struct gfs2_rbm rbm = *rrbm;
321 u32 n_unaligned = rbm.offset & 3;
322 u32 size = len;
323 u32 bytes;
324 u32 chunk_size;
325 u8 *ptr, *start, *end;
326 u64 block;
327
328 if (n_unaligned &&
329 gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len))
330 goto out;
331
332 n_unaligned = len & 3;
333 /* Start is now byte aligned */
334 while (len > 3) {
335 start = rbm.bi->bi_bh->b_data;
336 if (rbm.bi->bi_clone)
337 start = rbm.bi->bi_clone;
338 end = start + rbm.bi->bi_bh->b_size;
339 start += rbm.bi->bi_offset;
340 BUG_ON(rbm.offset & 3);
341 start += (rbm.offset / GFS2_NBBY);
342 bytes = min_t(u32, len / GFS2_NBBY, (end - start));
343 ptr = memchr_inv(start, 0, bytes);
344 chunk_size = ((ptr == NULL) ? bytes : (ptr - start));
345 chunk_size *= GFS2_NBBY;
346 BUG_ON(len < chunk_size);
347 len -= chunk_size;
348 block = gfs2_rbm_to_block(&rbm);
349 gfs2_rbm_from_block(&rbm, block + chunk_size);
350 n_unaligned = 3;
351 if (ptr)
352 break;
353 n_unaligned = len & 3;
354 }
355
356 /* Deal with any bits left over at the end */
357 if (n_unaligned)
358 gfs2_unaligned_extlen(&rbm, n_unaligned, &len);
359out:
360 return size - len;
361}
362
363/**
288 * gfs2_bitcount - count the number of bits in a certain state 364 * gfs2_bitcount - count the number of bits in a certain state
289 * @rgd: the resource group descriptor 365 * @rgd: the resource group descriptor
290 * @buffer: the buffer that holds the bitmaps 366 * @buffer: the buffer that holds the bitmaps
@@ -487,6 +563,8 @@ int gfs2_rs_alloc(struct gfs2_inode *ip)
487 if (!res) 563 if (!res)
488 error = -ENOMEM; 564 error = -ENOMEM;
489 565
566 RB_CLEAR_NODE(&res->rs_node);
567
490 down_write(&ip->i_rw_mutex); 568 down_write(&ip->i_rw_mutex);
491 if (ip->i_res) 569 if (ip->i_res)
492 kmem_cache_free(gfs2_rsrv_cachep, res); 570 kmem_cache_free(gfs2_rsrv_cachep, res);
@@ -496,11 +574,12 @@ int gfs2_rs_alloc(struct gfs2_inode *ip)
496 return error; 574 return error;
497} 575}
498 576
499static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs) 577static void dump_rs(struct seq_file *seq, const struct gfs2_blkreserv *rs)
500{ 578{
501 gfs2_print_dbg(seq, " r: %llu s:%llu b:%u f:%u\n", 579 gfs2_print_dbg(seq, " B: n:%llu s:%llu b:%u f:%u\n",
502 rs->rs_rgd->rd_addr, gfs2_rs_startblk(rs), rs->rs_biblk, 580 (unsigned long long)rs->rs_inum,
503 rs->rs_free); 581 (unsigned long long)gfs2_rbm_to_block(&rs->rs_rbm),
582 rs->rs_rbm.offset, rs->rs_free);
504} 583}
505 584
506/** 585/**
@@ -508,41 +587,26 @@ static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs)
508 * @rs: The reservation to remove 587 * @rs: The reservation to remove
509 * 588 *
510 */ 589 */
511static void __rs_deltree(struct gfs2_blkreserv *rs) 590static void __rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
512{ 591{
513 struct gfs2_rgrpd *rgd; 592 struct gfs2_rgrpd *rgd;
514 593
515 if (!gfs2_rs_active(rs)) 594 if (!gfs2_rs_active(rs))
516 return; 595 return;
517 596
518 rgd = rs->rs_rgd; 597 rgd = rs->rs_rbm.rgd;
519 /* We can't do this: The reason is that when the rgrp is invalidated, 598 trace_gfs2_rs(rs, TRACE_RS_TREEDEL);
520 it's in the "middle" of acquiring the glock, but the HOLDER bit 599 rb_erase(&rs->rs_node, &rgd->rd_rstree);
521 isn't set yet: 600 RB_CLEAR_NODE(&rs->rs_node);
522 BUG_ON(!gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl));*/
523 trace_gfs2_rs(NULL, rs, TRACE_RS_TREEDEL);
524
525 if (!RB_EMPTY_ROOT(&rgd->rd_rstree))
526 rb_erase(&rs->rs_node, &rgd->rd_rstree);
527 BUG_ON(!rgd->rd_rs_cnt);
528 rgd->rd_rs_cnt--;
529 601
530 if (rs->rs_free) { 602 if (rs->rs_free) {
531 /* return reserved blocks to the rgrp and the ip */ 603 /* return reserved blocks to the rgrp and the ip */
532 BUG_ON(rs->rs_rgd->rd_reserved < rs->rs_free); 604 BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free);
533 rs->rs_rgd->rd_reserved -= rs->rs_free; 605 rs->rs_rbm.rgd->rd_reserved -= rs->rs_free;
534 rs->rs_free = 0; 606 rs->rs_free = 0;
535 clear_bit(GBF_FULL, &rs->rs_bi->bi_flags); 607 clear_bit(GBF_FULL, &rs->rs_rbm.bi->bi_flags);
536 smp_mb__after_clear_bit(); 608 smp_mb__after_clear_bit();
537 } 609 }
538 /* We can't change any of the step 1 or step 2 components of the rs.
539 E.g. We can't set rs_rgd to NULL because the rgd glock is held and
540 dequeued through this pointer.
541 Can't: atomic_set(&rs->rs_sizehint, 0);
542 Can't: rs->rs_requested = 0;
543 Can't: rs->rs_rgd = NULL;*/
544 rs->rs_bi = NULL;
545 rs->rs_biblk = 0;
546} 610}
547 611
548/** 612/**
@@ -550,17 +614,16 @@ static void __rs_deltree(struct gfs2_blkreserv *rs)
550 * @rs: The reservation to remove 614 * @rs: The reservation to remove
551 * 615 *
552 */ 616 */
553void gfs2_rs_deltree(struct gfs2_blkreserv *rs) 617void gfs2_rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
554{ 618{
555 struct gfs2_rgrpd *rgd; 619 struct gfs2_rgrpd *rgd;
556 620
557 if (!gfs2_rs_active(rs)) 621 rgd = rs->rs_rbm.rgd;
558 return; 622 if (rgd) {
559 623 spin_lock(&rgd->rd_rsspin);
560 rgd = rs->rs_rgd; 624 __rs_deltree(ip, rs);
561 spin_lock(&rgd->rd_rsspin); 625 spin_unlock(&rgd->rd_rsspin);
562 __rs_deltree(rs); 626 }
563 spin_unlock(&rgd->rd_rsspin);
564} 627}
565 628
566/** 629/**
@@ -572,8 +635,7 @@ void gfs2_rs_delete(struct gfs2_inode *ip)
572{ 635{
573 down_write(&ip->i_rw_mutex); 636 down_write(&ip->i_rw_mutex);
574 if (ip->i_res) { 637 if (ip->i_res) {
575 gfs2_rs_deltree(ip->i_res); 638 gfs2_rs_deltree(ip, ip->i_res);
576 trace_gfs2_rs(ip, ip->i_res, TRACE_RS_DELETE);
577 BUG_ON(ip->i_res->rs_free); 639 BUG_ON(ip->i_res->rs_free);
578 kmem_cache_free(gfs2_rsrv_cachep, ip->i_res); 640 kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
579 ip->i_res = NULL; 641 ip->i_res = NULL;
@@ -597,7 +659,7 @@ static void return_all_reservations(struct gfs2_rgrpd *rgd)
597 spin_lock(&rgd->rd_rsspin); 659 spin_lock(&rgd->rd_rsspin);
598 while ((n = rb_first(&rgd->rd_rstree))) { 660 while ((n = rb_first(&rgd->rd_rstree))) {
599 rs = rb_entry(n, struct gfs2_blkreserv, rs_node); 661 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
600 __rs_deltree(rs); 662 __rs_deltree(NULL, rs);
601 } 663 }
602 spin_unlock(&rgd->rd_rsspin); 664 spin_unlock(&rgd->rd_rsspin);
603} 665}
@@ -1270,211 +1332,276 @@ out:
1270 1332
1271/** 1333/**
1272 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree 1334 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
1273 * @bi: the bitmap with the blocks
1274 * @ip: the inode structure 1335 * @ip: the inode structure
1275 * @biblk: the 32-bit block number relative to the start of the bitmap
1276 * @amount: the number of blocks to reserve
1277 * 1336 *
1278 * Returns: NULL - reservation was already taken, so not inserted
1279 * pointer to the inserted reservation
1280 */ 1337 */
1281static struct gfs2_blkreserv *rs_insert(struct gfs2_bitmap *bi, 1338static void rs_insert(struct gfs2_inode *ip)
1282 struct gfs2_inode *ip, u32 biblk,
1283 int amount)
1284{ 1339{
1285 struct rb_node **newn, *parent = NULL; 1340 struct rb_node **newn, *parent = NULL;
1286 int rc; 1341 int rc;
1287 struct gfs2_blkreserv *rs = ip->i_res; 1342 struct gfs2_blkreserv *rs = ip->i_res;
1288 struct gfs2_rgrpd *rgd = rs->rs_rgd; 1343 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd;
1289 u64 fsblock = gfs2_bi2rgd_blk(bi, biblk) + rgd->rd_data0; 1344 u64 fsblock = gfs2_rbm_to_block(&rs->rs_rbm);
1345
1346 BUG_ON(gfs2_rs_active(rs));
1290 1347
1291 spin_lock(&rgd->rd_rsspin); 1348 spin_lock(&rgd->rd_rsspin);
1292 newn = &rgd->rd_rstree.rb_node; 1349 newn = &rgd->rd_rstree.rb_node;
1293 BUG_ON(!ip->i_res);
1294 BUG_ON(gfs2_rs_active(rs));
1295 /* Figure out where to put new node */
1296 /*BUG_ON(!gfs2_glock_is_locked_by_me(rgd->rd_gl));*/
1297 while (*newn) { 1350 while (*newn) {
1298 struct gfs2_blkreserv *cur = 1351 struct gfs2_blkreserv *cur =
1299 rb_entry(*newn, struct gfs2_blkreserv, rs_node); 1352 rb_entry(*newn, struct gfs2_blkreserv, rs_node);
1300 1353
1301 parent = *newn; 1354 parent = *newn;
1302 rc = rs_cmp(fsblock, amount, cur); 1355 rc = rs_cmp(fsblock, rs->rs_free, cur);
1303 if (rc > 0) 1356 if (rc > 0)
1304 newn = &((*newn)->rb_right); 1357 newn = &((*newn)->rb_right);
1305 else if (rc < 0) 1358 else if (rc < 0)
1306 newn = &((*newn)->rb_left); 1359 newn = &((*newn)->rb_left);
1307 else { 1360 else {
1308 spin_unlock(&rgd->rd_rsspin); 1361 spin_unlock(&rgd->rd_rsspin);
1309 return NULL; /* reservation already in use */ 1362 WARN_ON(1);
1363 return;
1310 } 1364 }
1311 } 1365 }
1312 1366
1313 /* Do our reservation work */
1314 rs = ip->i_res;
1315 rs->rs_free = amount;
1316 rs->rs_biblk = biblk;
1317 rs->rs_bi = bi;
1318 rb_link_node(&rs->rs_node, parent, newn); 1367 rb_link_node(&rs->rs_node, parent, newn);
1319 rb_insert_color(&rs->rs_node, &rgd->rd_rstree); 1368 rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
1320 1369
1321 /* Do our inode accounting for the reservation */
1322 /*BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));*/
1323
1324 /* Do our rgrp accounting for the reservation */ 1370 /* Do our rgrp accounting for the reservation */
1325 rgd->rd_reserved += amount; /* blocks reserved */ 1371 rgd->rd_reserved += rs->rs_free; /* blocks reserved */
1326 rgd->rd_rs_cnt++; /* number of in-tree reservations */
1327 spin_unlock(&rgd->rd_rsspin); 1372 spin_unlock(&rgd->rd_rsspin);
1328 trace_gfs2_rs(ip, rs, TRACE_RS_INSERT); 1373 trace_gfs2_rs(rs, TRACE_RS_INSERT);
1329 return rs;
1330}
1331
1332/**
1333 * unclaimed_blocks - return number of blocks that aren't spoken for
1334 */
1335static u32 unclaimed_blocks(struct gfs2_rgrpd *rgd)
1336{
1337 return rgd->rd_free_clone - rgd->rd_reserved;
1338} 1374}
1339 1375
1340/** 1376/**
1341 * rg_mblk_search - find a group of multiple free blocks 1377 * rg_mblk_search - find a group of multiple free blocks to form a reservation
1342 * @rgd: the resource group descriptor 1378 * @rgd: the resource group descriptor
1343 * @rs: the block reservation
1344 * @ip: pointer to the inode for which we're reserving blocks 1379 * @ip: pointer to the inode for which we're reserving blocks
1380 * @requested: number of blocks required for this allocation
1345 * 1381 *
1346 * This is very similar to rgblk_search, except we're looking for whole
1347 * 64-bit words that represent a chunk of 32 free blocks. I'm only focusing
1348 * on aligned dwords for speed's sake.
1349 *
1350 * Returns: 0 if successful or BFITNOENT if there isn't enough free space
1351 */ 1382 */
1352 1383
1353static int rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) 1384static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
1385 unsigned requested)
1354{ 1386{
1355 struct gfs2_bitmap *bi = rgd->rd_bits; 1387 struct gfs2_rbm rbm = { .rgd = rgd, };
1356 const u32 length = rgd->rd_length; 1388 u64 goal;
1357 u32 blk; 1389 struct gfs2_blkreserv *rs = ip->i_res;
1358 unsigned int buf, x, search_bytes; 1390 u32 extlen;
1359 u8 *buffer = NULL; 1391 u32 free_blocks = rgd->rd_free_clone - rgd->rd_reserved;
1360 u8 *ptr, *end, *nonzero; 1392 int ret;
1361 u32 goal, rsv_bytes; 1393
1362 struct gfs2_blkreserv *rs; 1394 extlen = max_t(u32, atomic_read(&rs->rs_sizehint), requested);
1363 u32 best_rs_bytes, unclaimed; 1395 extlen = clamp(extlen, RGRP_RSRV_MINBLKS, free_blocks);
1364 int best_rs_blocks; 1396 if ((rgd->rd_free_clone < rgd->rd_reserved) || (free_blocks < extlen))
1397 return;
1365 1398
1366 /* Find bitmap block that contains bits for goal block */ 1399 /* Find bitmap block that contains bits for goal block */
1367 if (rgrp_contains_block(rgd, ip->i_goal)) 1400 if (rgrp_contains_block(rgd, ip->i_goal))
1368 goal = ip->i_goal - rgd->rd_data0; 1401 goal = ip->i_goal;
1369 else 1402 else
1370 goal = rgd->rd_last_alloc; 1403 goal = rgd->rd_last_alloc + rgd->rd_data0;
1371 for (buf = 0; buf < length; buf++) { 1404
1372 bi = rgd->rd_bits + buf; 1405 if (WARN_ON(gfs2_rbm_from_block(&rbm, goal)))
1373 /* Convert scope of "goal" from rgrp-wide to within 1406 return;
1374 found bit block */ 1407
1375 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) { 1408 ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, extlen, ip, true);
1376 goal -= bi->bi_start * GFS2_NBBY; 1409 if (ret == 0) {
1377 goto do_search; 1410 rs->rs_rbm = rbm;
1378 } 1411 rs->rs_free = extlen;
1412 rs->rs_inum = ip->i_no_addr;
1413 rs_insert(ip);
1379 } 1414 }
1380 buf = 0; 1415}
1381 goal = 0; 1416
1382 1417/**
1383do_search: 1418 * gfs2_next_unreserved_block - Return next block that is not reserved
1384 best_rs_blocks = max_t(int, atomic_read(&ip->i_res->rs_sizehint), 1419 * @rgd: The resource group
1385 (RGRP_RSRV_MINBLKS * rgd->rd_length)); 1420 * @block: The starting block
1386 best_rs_bytes = (best_rs_blocks * 1421 * @length: The required length
1387 (1 + (RSRV_CONTENTION_FACTOR * rgd->rd_rs_cnt))) / 1422 * @ip: Ignore any reservations for this inode
1388 GFS2_NBBY; /* 1 + is for our not-yet-created reservation */ 1423 *
1389 best_rs_bytes = ALIGN(best_rs_bytes, sizeof(u64)); 1424 * If the block does not appear in any reservation, then return the
1390 unclaimed = unclaimed_blocks(rgd); 1425 * block number unchanged. If it does appear in the reservation, then
1391 if (best_rs_bytes * GFS2_NBBY > unclaimed) 1426 * keep looking through the tree of reservations in order to find the
1392 best_rs_bytes = unclaimed >> GFS2_BIT_SIZE; 1427 * first block number which is not reserved.
1393 1428 */
1394 for (x = 0; x <= length; x++) {
1395 bi = rgd->rd_bits + buf;
1396 1429
1397 if (test_bit(GBF_FULL, &bi->bi_flags)) 1430static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block,
1398 goto skip; 1431 u32 length,
1432 const struct gfs2_inode *ip)
1433{
1434 struct gfs2_blkreserv *rs;
1435 struct rb_node *n;
1436 int rc;
1399 1437
1400 WARN_ON(!buffer_uptodate(bi->bi_bh)); 1438 spin_lock(&rgd->rd_rsspin);
1401 if (bi->bi_clone) 1439 n = rgd->rd_rstree.rb_node;
1402 buffer = bi->bi_clone + bi->bi_offset; 1440 while (n) {
1441 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1442 rc = rs_cmp(block, length, rs);
1443 if (rc < 0)
1444 n = n->rb_left;
1445 else if (rc > 0)
1446 n = n->rb_right;
1403 else 1447 else
1404 buffer = bi->bi_bh->b_data + bi->bi_offset; 1448 break;
1405 1449 }
1406 /* We have to keep the reservations aligned on u64 boundaries 1450
1407 otherwise we could get situations where a byte can't be 1451 if (n) {
1408 used because it's after a reservation, but a free bit still 1452 while ((rs_cmp(block, length, rs) == 0) && (ip->i_res != rs)) {
1409 is within the reservation's area. */ 1453 block = gfs2_rbm_to_block(&rs->rs_rbm) + rs->rs_free;
1410 ptr = buffer + ALIGN(goal >> GFS2_BIT_SIZE, sizeof(u64)); 1454 n = n->rb_right;
1411 end = (buffer + bi->bi_len); 1455 if (n == NULL)
1412 while (ptr < end) { 1456 break;
1413 rsv_bytes = 0; 1457 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1414 if ((ptr + best_rs_bytes) <= end)
1415 search_bytes = best_rs_bytes;
1416 else
1417 search_bytes = end - ptr;
1418 BUG_ON(!search_bytes);
1419 nonzero = memchr_inv(ptr, 0, search_bytes);
1420 /* If the lot is all zeroes, reserve the whole size. If
1421 there's enough zeroes to satisfy the request, use
1422 what we can. If there's not enough, keep looking. */
1423 if (nonzero == NULL)
1424 rsv_bytes = search_bytes;
1425 else if ((nonzero - ptr) * GFS2_NBBY >=
1426 ip->i_res->rs_requested)
1427 rsv_bytes = (nonzero - ptr);
1428
1429 if (rsv_bytes) {
1430 blk = ((ptr - buffer) * GFS2_NBBY);
1431 BUG_ON(blk >= bi->bi_len * GFS2_NBBY);
1432 rs = rs_insert(bi, ip, blk,
1433 rsv_bytes * GFS2_NBBY);
1434 if (IS_ERR(rs))
1435 return PTR_ERR(rs);
1436 if (rs)
1437 return 0;
1438 }
1439 ptr += ALIGN(search_bytes, sizeof(u64));
1440 } 1458 }
1441skip:
1442 /* Try next bitmap block (wrap back to rgrp header
1443 if at end) */
1444 buf++;
1445 buf %= length;
1446 goal = 0;
1447 } 1459 }
1448 1460
1449 return BFITNOENT; 1461 spin_unlock(&rgd->rd_rsspin);
1462 return block;
1450} 1463}
1451 1464
1452/** 1465/**
1453 * try_rgrp_fit - See if a given reservation will fit in a given RG 1466 * gfs2_reservation_check_and_update - Check for reservations during block alloc
1454 * @rgd: the RG data 1467 * @rbm: The current position in the resource group
1455 * @ip: the inode 1468 * @ip: The inode for which we are searching for blocks
1469 * @minext: The minimum extent length
1456 * 1470 *
1457 * If there's room for the requested blocks to be allocated from the RG: 1471 * This checks the current position in the rgrp to see whether there is
1458 * This will try to get a multi-block reservation first, and if that doesn't 1472 * a reservation covering this block. If not then this function is a
1459 * fit, it will take what it can. 1473 * no-op. If there is, then the position is moved to the end of the
1474 * contiguous reservation(s) so that we are pointing at the first
1475 * non-reserved block.
1460 * 1476 *
1461 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit) 1477 * Returns: 0 if no reservation, 1 if @rbm has changed, otherwise an error
1462 */ 1478 */
1463 1479
1464static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) 1480static int gfs2_reservation_check_and_update(struct gfs2_rbm *rbm,
1481 const struct gfs2_inode *ip,
1482 u32 minext)
1465{ 1483{
1466 struct gfs2_blkreserv *rs = ip->i_res; 1484 u64 block = gfs2_rbm_to_block(rbm);
1485 u32 extlen = 1;
1486 u64 nblock;
1487 int ret;
1467 1488
1468 if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR)) 1489 /*
1490 * If we have a minimum extent length, then skip over any extent
1491 * which is less than the min extent length in size.
1492 */
1493 if (minext) {
1494 extlen = gfs2_free_extlen(rbm, minext);
1495 nblock = block + extlen;
1496 if (extlen < minext)
1497 goto fail;
1498 }
1499
1500 /*
1501 * Check the extent which has been found against the reservations
1502 * and skip if parts of it are already reserved
1503 */
1504 nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, ip);
1505 if (nblock == block)
1469 return 0; 1506 return 0;
1470 /* Look for a multi-block reservation. */ 1507fail:
1471 if (unclaimed_blocks(rgd) >= RGRP_RSRV_MINBLKS && 1508 ret = gfs2_rbm_from_block(rbm, nblock);
1472 rg_mblk_search(rgd, ip) != BFITNOENT) 1509 if (ret < 0)
1473 return 1; 1510 return ret;
1474 if (unclaimed_blocks(rgd) >= rs->rs_requested) 1511 return 1;
1475 return 1; 1512}
1476 1513
1477 return 0; 1514/**
1515 * gfs2_rbm_find - Look for blocks of a particular state
1516 * @rbm: Value/result starting position and final position
1517 * @state: The state which we want to find
1518 * @minext: The requested extent length (0 for a single block)
1519 * @ip: If set, check for reservations
1520 * @nowrap: Stop looking at the end of the rgrp, rather than wrapping
1521 * around until we've reached the starting point.
1522 *
1523 * Side effects:
1524 * - If looking for free blocks, we set GBF_FULL on each bitmap which
1525 * has no free blocks in it.
1526 *
1527 * Returns: 0 on success, -ENOSPC if there is no block of the requested state
1528 */
1529
1530static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 minext,
1531 const struct gfs2_inode *ip, bool nowrap)
1532{
1533 struct buffer_head *bh;
1534 struct gfs2_bitmap *initial_bi;
1535 u32 initial_offset;
1536 u32 offset;
1537 u8 *buffer;
1538 int index;
1539 int n = 0;
1540 int iters = rbm->rgd->rd_length;
1541 int ret;
1542
1543 /* If we are not starting at the beginning of a bitmap, then we
1544 * need to add one to the bitmap count to ensure that we search
1545 * the starting bitmap twice.
1546 */
1547 if (rbm->offset != 0)
1548 iters++;
1549
1550 while(1) {
1551 if (test_bit(GBF_FULL, &rbm->bi->bi_flags) &&
1552 (state == GFS2_BLKST_FREE))
1553 goto next_bitmap;
1554
1555 bh = rbm->bi->bi_bh;
1556 buffer = bh->b_data + rbm->bi->bi_offset;
1557 WARN_ON(!buffer_uptodate(bh));
1558 if (state != GFS2_BLKST_UNLINKED && rbm->bi->bi_clone)
1559 buffer = rbm->bi->bi_clone + rbm->bi->bi_offset;
1560 initial_offset = rbm->offset;
1561 offset = gfs2_bitfit(buffer, rbm->bi->bi_len, rbm->offset, state);
1562 if (offset == BFITNOENT)
1563 goto bitmap_full;
1564 rbm->offset = offset;
1565 if (ip == NULL)
1566 return 0;
1567
1568 initial_bi = rbm->bi;
1569 ret = gfs2_reservation_check_and_update(rbm, ip, minext);
1570 if (ret == 0)
1571 return 0;
1572 if (ret > 0) {
1573 n += (rbm->bi - initial_bi);
1574 goto next_iter;
1575 }
1576 if (ret == -E2BIG) {
1577 index = 0;
1578 rbm->offset = 0;
1579 n += (rbm->bi - initial_bi);
1580 goto res_covered_end_of_rgrp;
1581 }
1582 return ret;
1583
1584bitmap_full: /* Mark bitmap as full and fall through */
1585 if ((state == GFS2_BLKST_FREE) && initial_offset == 0)
1586 set_bit(GBF_FULL, &rbm->bi->bi_flags);
1587
1588next_bitmap: /* Find next bitmap in the rgrp */
1589 rbm->offset = 0;
1590 index = rbm->bi - rbm->rgd->rd_bits;
1591 index++;
1592 if (index == rbm->rgd->rd_length)
1593 index = 0;
1594res_covered_end_of_rgrp:
1595 rbm->bi = &rbm->rgd->rd_bits[index];
1596 if ((index == 0) && nowrap)
1597 break;
1598 n++;
1599next_iter:
1600 if (n >= iters)
1601 break;
1602 }
1603
1604 return -ENOSPC;
1478} 1605}
1479 1606
1480/** 1607/**
@@ -1489,34 +1616,33 @@ static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1489 1616
1490static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip) 1617static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
1491{ 1618{
1492 u32 goal = 0, block; 1619 u64 block;
1493 u64 no_addr;
1494 struct gfs2_sbd *sdp = rgd->rd_sbd; 1620 struct gfs2_sbd *sdp = rgd->rd_sbd;
1495 struct gfs2_glock *gl; 1621 struct gfs2_glock *gl;
1496 struct gfs2_inode *ip; 1622 struct gfs2_inode *ip;
1497 int error; 1623 int error;
1498 int found = 0; 1624 int found = 0;
1499 struct gfs2_bitmap *bi; 1625 struct gfs2_rbm rbm = { .rgd = rgd, .bi = rgd->rd_bits, .offset = 0 };
1500 1626
1501 while (goal < rgd->rd_data) { 1627 while (1) {
1502 down_write(&sdp->sd_log_flush_lock); 1628 down_write(&sdp->sd_log_flush_lock);
1503 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, &bi); 1629 error = gfs2_rbm_find(&rbm, GFS2_BLKST_UNLINKED, 0, NULL, true);
1504 up_write(&sdp->sd_log_flush_lock); 1630 up_write(&sdp->sd_log_flush_lock);
1505 if (block == BFITNOENT) 1631 if (error == -ENOSPC)
1632 break;
1633 if (WARN_ON_ONCE(error))
1506 break; 1634 break;
1507 1635
1508 block = gfs2_bi2rgd_blk(bi, block); 1636 block = gfs2_rbm_to_block(&rbm);
1509 /* rgblk_search can return a block < goal, so we need to 1637 if (gfs2_rbm_from_block(&rbm, block + 1))
1510 keep it marching forward. */ 1638 break;
1511 no_addr = block + rgd->rd_data0; 1639 if (*last_unlinked != NO_BLOCK && block <= *last_unlinked)
1512 goal = max(block + 1, goal + 1);
1513 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
1514 continue; 1640 continue;
1515 if (no_addr == skip) 1641 if (block == skip)
1516 continue; 1642 continue;
1517 *last_unlinked = no_addr; 1643 *last_unlinked = block;
1518 1644
1519 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl); 1645 error = gfs2_glock_get(sdp, block, &gfs2_inode_glops, CREATE, &gl);
1520 if (error) 1646 if (error)
1521 continue; 1647 continue;
1522 1648
@@ -1543,6 +1669,19 @@ static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip
1543 return; 1669 return;
1544} 1670}
1545 1671
1672static bool gfs2_select_rgrp(struct gfs2_rgrpd **pos, const struct gfs2_rgrpd *begin)
1673{
1674 struct gfs2_rgrpd *rgd = *pos;
1675
1676 rgd = gfs2_rgrpd_get_next(rgd);
1677 if (rgd == NULL)
1678 rgd = gfs2_rgrpd_get_next(NULL);
1679 *pos = rgd;
1680 if (rgd != begin) /* If we didn't wrap */
1681 return true;
1682 return false;
1683}
1684
1546/** 1685/**
1547 * gfs2_inplace_reserve - Reserve space in the filesystem 1686 * gfs2_inplace_reserve - Reserve space in the filesystem
1548 * @ip: the inode to reserve space for 1687 * @ip: the inode to reserve space for
@@ -1562,103 +1701,96 @@ int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
1562 1701
1563 if (sdp->sd_args.ar_rgrplvb) 1702 if (sdp->sd_args.ar_rgrplvb)
1564 flags |= GL_SKIP; 1703 flags |= GL_SKIP;
1565 rs->rs_requested = requested; 1704 if (gfs2_assert_warn(sdp, requested))
1566 if (gfs2_assert_warn(sdp, requested)) { 1705 return -EINVAL;
1567 error = -EINVAL;
1568 goto out;
1569 }
1570 if (gfs2_rs_active(rs)) { 1706 if (gfs2_rs_active(rs)) {
1571 begin = rs->rs_rgd; 1707 begin = rs->rs_rbm.rgd;
1572 flags = 0; /* Yoda: Do or do not. There is no try */ 1708 flags = 0; /* Yoda: Do or do not. There is no try */
1573 } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) { 1709 } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) {
1574 rs->rs_rgd = begin = ip->i_rgd; 1710 rs->rs_rbm.rgd = begin = ip->i_rgd;
1575 } else { 1711 } else {
1576 rs->rs_rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1); 1712 rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
1577 } 1713 }
1578 if (rs->rs_rgd == NULL) 1714 if (rs->rs_rbm.rgd == NULL)
1579 return -EBADSLT; 1715 return -EBADSLT;
1580 1716
1581 while (loops < 3) { 1717 while (loops < 3) {
1582 rg_locked = 0; 1718 rg_locked = 1;
1583 1719
1584 if (gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl)) { 1720 if (!gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) {
1585 rg_locked = 1; 1721 rg_locked = 0;
1586 error = 0; 1722 error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl,
1587 } else if (!loops && !gfs2_rs_active(rs) &&
1588 rs->rs_rgd->rd_rs_cnt > RGRP_RSRV_MAX_CONTENDERS) {
1589 /* If the rgrp already is maxed out for contenders,
1590 we can eliminate it as a "first pass" without even
1591 requesting the rgrp glock. */
1592 error = GLR_TRYFAILED;
1593 } else {
1594 error = gfs2_glock_nq_init(rs->rs_rgd->rd_gl,
1595 LM_ST_EXCLUSIVE, flags, 1723 LM_ST_EXCLUSIVE, flags,
1596 &rs->rs_rgd_gh); 1724 &rs->rs_rgd_gh);
1597 if (!error && sdp->sd_args.ar_rgrplvb) { 1725 if (error == GLR_TRYFAILED)
1598 error = update_rgrp_lvb(rs->rs_rgd); 1726 goto next_rgrp;
1599 if (error) { 1727 if (unlikely(error))
1728 return error;
1729 if (sdp->sd_args.ar_rgrplvb) {
1730 error = update_rgrp_lvb(rs->rs_rbm.rgd);
1731 if (unlikely(error)) {
1600 gfs2_glock_dq_uninit(&rs->rs_rgd_gh); 1732 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1601 return error; 1733 return error;
1602 } 1734 }
1603 } 1735 }
1604 } 1736 }
1605 switch (error) {
1606 case 0:
1607 if (gfs2_rs_active(rs)) {
1608 if (unclaimed_blocks(rs->rs_rgd) +
1609 rs->rs_free >= rs->rs_requested) {
1610 ip->i_rgd = rs->rs_rgd;
1611 return 0;
1612 }
1613 /* We have a multi-block reservation, but the
1614 rgrp doesn't have enough free blocks to
1615 satisfy the request. Free the reservation
1616 and look for a suitable rgrp. */
1617 gfs2_rs_deltree(rs);
1618 }
1619 if (try_rgrp_fit(rs->rs_rgd, ip)) {
1620 if (sdp->sd_args.ar_rgrplvb)
1621 gfs2_rgrp_bh_get(rs->rs_rgd);
1622 ip->i_rgd = rs->rs_rgd;
1623 return 0;
1624 }
1625 if (rs->rs_rgd->rd_flags & GFS2_RDF_CHECK) {
1626 if (sdp->sd_args.ar_rgrplvb)
1627 gfs2_rgrp_bh_get(rs->rs_rgd);
1628 try_rgrp_unlink(rs->rs_rgd, &last_unlinked,
1629 ip->i_no_addr);
1630 }
1631 if (!rg_locked)
1632 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1633 /* fall through */
1634 case GLR_TRYFAILED:
1635 rs->rs_rgd = gfs2_rgrpd_get_next(rs->rs_rgd);
1636 rs->rs_rgd = rs->rs_rgd ? : begin; /* if NULL, wrap */
1637 if (rs->rs_rgd != begin) /* If we didn't wrap */
1638 break;
1639 1737
1640 flags &= ~LM_FLAG_TRY; 1738 /* Skip unuseable resource groups */
1641 loops++; 1739 if (rs->rs_rbm.rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
1642 /* Check that fs hasn't grown if writing to rindex */ 1740 goto skip_rgrp;
1643 if (ip == GFS2_I(sdp->sd_rindex) && 1741
1644 !sdp->sd_rindex_uptodate) { 1742 if (sdp->sd_args.ar_rgrplvb)
1645 error = gfs2_ri_update(ip); 1743 gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
1646 if (error) 1744
1647 goto out; 1745 /* Get a reservation if we don't already have one */
1648 } else if (loops == 2) 1746 if (!gfs2_rs_active(rs))
1649 /* Flushing the log may release space */ 1747 rg_mblk_search(rs->rs_rbm.rgd, ip, requested);
1650 gfs2_log_flush(sdp, NULL); 1748
1651 break; 1749 /* Skip rgrps when we can't get a reservation on first pass */
1652 default: 1750 if (!gfs2_rs_active(rs) && (loops < 1))
1653 goto out; 1751 goto check_rgrp;
1752
1753 /* If rgrp has enough free space, use it */
1754 if (rs->rs_rbm.rgd->rd_free_clone >= requested) {
1755 ip->i_rgd = rs->rs_rbm.rgd;
1756 return 0;
1757 }
1758
1759 /* Drop reservation, if we couldn't use reserved rgrp */
1760 if (gfs2_rs_active(rs))
1761 gfs2_rs_deltree(ip, rs);
1762check_rgrp:
1763 /* Check for unlinked inodes which can be reclaimed */
1764 if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK)
1765 try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked,
1766 ip->i_no_addr);
1767skip_rgrp:
1768 /* Unlock rgrp if required */
1769 if (!rg_locked)
1770 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1771next_rgrp:
1772 /* Find the next rgrp, and continue looking */
1773 if (gfs2_select_rgrp(&rs->rs_rbm.rgd, begin))
1774 continue;
1775
1776 /* If we've scanned all the rgrps, but found no free blocks
1777 * then this checks for some less likely conditions before
1778 * trying again.
1779 */
1780 flags &= ~LM_FLAG_TRY;
1781 loops++;
1782 /* Check that fs hasn't grown if writing to rindex */
1783 if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
1784 error = gfs2_ri_update(ip);
1785 if (error)
1786 return error;
1654 } 1787 }
1788 /* Flushing the log may release space */
1789 if (loops == 2)
1790 gfs2_log_flush(sdp, NULL);
1655 } 1791 }
1656 error = -ENOSPC;
1657 1792
1658out: 1793 return -ENOSPC;
1659 if (error)
1660 rs->rs_requested = 0;
1661 return error;
1662} 1794}
1663 1795
1664/** 1796/**
@@ -1672,15 +1804,8 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
1672{ 1804{
1673 struct gfs2_blkreserv *rs = ip->i_res; 1805 struct gfs2_blkreserv *rs = ip->i_res;
1674 1806
1675 if (!rs)
1676 return;
1677
1678 if (!rs->rs_free)
1679 gfs2_rs_deltree(rs);
1680
1681 if (rs->rs_rgd_gh.gh_gl) 1807 if (rs->rs_rgd_gh.gh_gl)
1682 gfs2_glock_dq_uninit(&rs->rs_rgd_gh); 1808 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1683 rs->rs_requested = 0;
1684} 1809}
1685 1810
1686/** 1811/**
@@ -1693,173 +1818,47 @@ void gfs2_inplace_release(struct gfs2_inode *ip)
1693 1818
1694static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block) 1819static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
1695{ 1820{
1696 struct gfs2_bitmap *bi = NULL; 1821 struct gfs2_rbm rbm = { .rgd = rgd, };
1697 u32 length, rgrp_block, buf_block; 1822 int ret;
1698 unsigned int buf;
1699 unsigned char type;
1700
1701 length = rgd->rd_length;
1702 rgrp_block = block - rgd->rd_data0;
1703
1704 for (buf = 0; buf < length; buf++) {
1705 bi = rgd->rd_bits + buf;
1706 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1707 break;
1708 }
1709
1710 gfs2_assert(rgd->rd_sbd, buf < length);
1711 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1712 1823
1713 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, 1824 ret = gfs2_rbm_from_block(&rbm, block);
1714 bi->bi_len, buf_block); 1825 WARN_ON_ONCE(ret != 0);
1715 1826
1716 return type; 1827 return gfs2_testbit(&rbm);
1717} 1828}
1718 1829
1719/**
1720 * rgblk_search - find a block in @state
1721 * @rgd: the resource group descriptor
1722 * @goal: the goal block within the RG (start here to search for avail block)
1723 * @state: GFS2_BLKST_XXX the before-allocation state to find
1724 * @rbi: address of the pointer to the bitmap containing the block found
1725 *
1726 * Walk rgrp's bitmap to find bits that represent a block in @state.
1727 *
1728 * This function never fails, because we wouldn't call it unless we
1729 * know (from reservation results, etc.) that a block is available.
1730 *
1731 * Scope of @goal is just within rgrp, not the whole filesystem.
1732 * Scope of @returned block is just within bitmap, not the whole filesystem.
1733 *
1734 * Returns: the block number found relative to the bitmap rbi
1735 */
1736
1737static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, unsigned char state,
1738 struct gfs2_bitmap **rbi)
1739{
1740 struct gfs2_bitmap *bi = NULL;
1741 const u32 length = rgd->rd_length;
1742 u32 biblk = BFITNOENT;
1743 unsigned int buf, x;
1744 const u8 *buffer = NULL;
1745
1746 *rbi = NULL;
1747 /* Find bitmap block that contains bits for goal block */
1748 for (buf = 0; buf < length; buf++) {
1749 bi = rgd->rd_bits + buf;
1750 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1751 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1752 goal -= bi->bi_start * GFS2_NBBY;
1753 goto do_search;
1754 }
1755 }
1756 buf = 0;
1757 goal = 0;
1758
1759do_search:
1760 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1761 "x <= length", instead of "x < length", because we typically start
1762 the search in the middle of a bit block, but if we can't find an
1763 allocatable block anywhere else, we want to be able wrap around and
1764 search in the first part of our first-searched bit block. */
1765 for (x = 0; x <= length; x++) {
1766 bi = rgd->rd_bits + buf;
1767
1768 if (test_bit(GBF_FULL, &bi->bi_flags) &&
1769 (state == GFS2_BLKST_FREE))
1770 goto skip;
1771
1772 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1773 bitmaps, so we must search the originals for that. */
1774 buffer = bi->bi_bh->b_data + bi->bi_offset;
1775 WARN_ON(!buffer_uptodate(bi->bi_bh));
1776 if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
1777 buffer = bi->bi_clone + bi->bi_offset;
1778
1779 while (1) {
1780 struct gfs2_blkreserv *rs;
1781 u32 rgblk;
1782
1783 biblk = gfs2_bitfit(buffer, bi->bi_len, goal, state);
1784 if (biblk == BFITNOENT)
1785 break;
1786 /* Check if this block is reserved() */
1787 rgblk = gfs2_bi2rgd_blk(bi, biblk);
1788 rs = rs_find(rgd, rgblk);
1789 if (rs == NULL)
1790 break;
1791
1792 BUG_ON(rs->rs_bi != bi);
1793 biblk = BFITNOENT;
1794 /* This should jump to the first block after the
1795 reservation. */
1796 goal = rs->rs_biblk + rs->rs_free;
1797 if (goal >= bi->bi_len * GFS2_NBBY)
1798 break;
1799 }
1800 if (biblk != BFITNOENT)
1801 break;
1802
1803 if ((goal == 0) && (state == GFS2_BLKST_FREE))
1804 set_bit(GBF_FULL, &bi->bi_flags);
1805
1806 /* Try next bitmap block (wrap back to rgrp header if at end) */
1807skip:
1808 buf++;
1809 buf %= length;
1810 goal = 0;
1811 }
1812
1813 if (biblk != BFITNOENT)
1814 *rbi = bi;
1815
1816 return biblk;
1817}
1818 1830
1819/** 1831/**
1820 * gfs2_alloc_extent - allocate an extent from a given bitmap 1832 * gfs2_alloc_extent - allocate an extent from a given bitmap
1821 * @rgd: the resource group descriptor 1833 * @rbm: the resource group information
1822 * @bi: the bitmap within the rgrp
1823 * @blk: the block within the bitmap
1824 * @dinode: TRUE if the first block we allocate is for a dinode 1834 * @dinode: TRUE if the first block we allocate is for a dinode
1825 * @n: The extent length 1835 * @n: The extent length (value/result)
1826 * 1836 *
1827 * Add the found bitmap buffer to the transaction. 1837 * Add the bitmap buffer to the transaction.
1828 * Set the found bits to @new_state to change block's allocation state. 1838 * Set the found bits to @new_state to change block's allocation state.
1829 * Returns: starting block number of the extent (fs scope)
1830 */ 1839 */
1831static u64 gfs2_alloc_extent(struct gfs2_rgrpd *rgd, struct gfs2_bitmap *bi, 1840static void gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
1832 u32 blk, bool dinode, unsigned int *n) 1841 unsigned int *n)
1833{ 1842{
1843 struct gfs2_rbm pos = { .rgd = rbm->rgd, };
1834 const unsigned int elen = *n; 1844 const unsigned int elen = *n;
1835 u32 goal, rgblk; 1845 u64 block;
1836 const u8 *buffer = NULL; 1846 int ret;
1837 struct gfs2_blkreserv *rs; 1847
1838 1848 *n = 1;
1839 *n = 0; 1849 block = gfs2_rbm_to_block(rbm);
1840 buffer = bi->bi_bh->b_data + bi->bi_offset; 1850 gfs2_trans_add_bh(rbm->rgd->rd_gl, rbm->bi->bi_bh, 1);
1841 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); 1851 gfs2_setbit(rbm, true, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
1842 gfs2_setbit(rgd, bi->bi_clone, bi, blk, 1852 block++;
1843 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
1844 (*n)++;
1845 goal = blk;
1846 while (*n < elen) { 1853 while (*n < elen) {
1847 goal++; 1854 ret = gfs2_rbm_from_block(&pos, block);
1848 if (goal >= (bi->bi_len * GFS2_NBBY)) 1855 if (ret || gfs2_testbit(&pos) != GFS2_BLKST_FREE)
1849 break;
1850 rgblk = gfs2_bi2rgd_blk(bi, goal);
1851 rs = rs_find(rgd, rgblk);
1852 if (rs) /* Oops, we bumped into someone's reservation */
1853 break;
1854 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1855 GFS2_BLKST_FREE)
1856 break; 1856 break;
1857 gfs2_setbit(rgd, bi->bi_clone, bi, goal, GFS2_BLKST_USED); 1857 gfs2_trans_add_bh(pos.rgd->rd_gl, pos.bi->bi_bh, 1);
1858 gfs2_setbit(&pos, true, GFS2_BLKST_USED);
1858 (*n)++; 1859 (*n)++;
1860 block++;
1859 } 1861 }
1860 blk = gfs2_bi2rgd_blk(bi, blk);
1861 rgd->rd_last_alloc = blk + *n - 1;
1862 return rgd->rd_data0 + blk;
1863} 1862}
1864 1863
1865/** 1864/**
@@ -1875,46 +1874,30 @@ static u64 gfs2_alloc_extent(struct gfs2_rgrpd *rgd, struct gfs2_bitmap *bi,
1875static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart, 1874static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1876 u32 blen, unsigned char new_state) 1875 u32 blen, unsigned char new_state)
1877{ 1876{
1878 struct gfs2_rgrpd *rgd; 1877 struct gfs2_rbm rbm;
1879 struct gfs2_bitmap *bi = NULL;
1880 u32 length, rgrp_blk, buf_blk;
1881 unsigned int buf;
1882 1878
1883 rgd = gfs2_blk2rgrpd(sdp, bstart, 1); 1879 rbm.rgd = gfs2_blk2rgrpd(sdp, bstart, 1);
1884 if (!rgd) { 1880 if (!rbm.rgd) {
1885 if (gfs2_consist(sdp)) 1881 if (gfs2_consist(sdp))
1886 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart); 1882 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
1887 return NULL; 1883 return NULL;
1888 } 1884 }
1889 1885
1890 length = rgd->rd_length;
1891
1892 rgrp_blk = bstart - rgd->rd_data0;
1893
1894 while (blen--) { 1886 while (blen--) {
1895 for (buf = 0; buf < length; buf++) { 1887 gfs2_rbm_from_block(&rbm, bstart);
1896 bi = rgd->rd_bits + buf; 1888 bstart++;
1897 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY) 1889 if (!rbm.bi->bi_clone) {
1898 break; 1890 rbm.bi->bi_clone = kmalloc(rbm.bi->bi_bh->b_size,
1891 GFP_NOFS | __GFP_NOFAIL);
1892 memcpy(rbm.bi->bi_clone + rbm.bi->bi_offset,
1893 rbm.bi->bi_bh->b_data + rbm.bi->bi_offset,
1894 rbm.bi->bi_len);
1899 } 1895 }
1900 1896 gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.bi->bi_bh, 1);
1901 gfs2_assert(rgd->rd_sbd, buf < length); 1897 gfs2_setbit(&rbm, false, new_state);
1902
1903 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1904 rgrp_blk++;
1905
1906 if (!bi->bi_clone) {
1907 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
1908 GFP_NOFS | __GFP_NOFAIL);
1909 memcpy(bi->bi_clone + bi->bi_offset,
1910 bi->bi_bh->b_data + bi->bi_offset,
1911 bi->bi_len);
1912 }
1913 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1914 gfs2_setbit(rgd, NULL, bi, buf_blk, new_state);
1915 } 1898 }
1916 1899
1917 return rgd; 1900 return rbm.rgd;
1918} 1901}
1919 1902
1920/** 1903/**
@@ -1956,46 +1939,41 @@ static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
1956} 1939}
1957 1940
1958/** 1941/**
1959 * claim_reserved_blks - Claim previously reserved blocks 1942 * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
1960 * @ip: the inode that's claiming the reservation 1943 * @ip: The inode we have just allocated blocks for
1961 * @dinode: 1 if this block is a dinode block, otherwise data block 1944 * @rbm: The start of the allocated blocks
1962 * @nblocks: desired extent length 1945 * @len: The extent length
1963 * 1946 *
1964 * Lay claim to previously reserved blocks. 1947 * Adjusts a reservation after an allocation has taken place. If the
1965 * Returns: Starting block number of the blocks claimed. 1948 * reservation does not match the allocation, or if it is now empty
1966 * Sets *nblocks to the actual extent length allocated. 1949 * then it is removed.
1967 */ 1950 */
1968static u64 claim_reserved_blks(struct gfs2_inode *ip, bool dinode, 1951
1969 unsigned int *nblocks) 1952static void gfs2_adjust_reservation(struct gfs2_inode *ip,
1953 const struct gfs2_rbm *rbm, unsigned len)
1970{ 1954{
1971 struct gfs2_blkreserv *rs = ip->i_res; 1955 struct gfs2_blkreserv *rs = ip->i_res;
1972 struct gfs2_rgrpd *rgd = rs->rs_rgd; 1956 struct gfs2_rgrpd *rgd = rbm->rgd;
1973 struct gfs2_bitmap *bi; 1957 unsigned rlen;
1974 u64 start_block = gfs2_rs_startblk(rs); 1958 u64 block;
1975 const unsigned int elen = *nblocks; 1959 int ret;
1976 1960
1977 bi = rs->rs_bi; 1961 spin_lock(&rgd->rd_rsspin);
1978 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); 1962 if (gfs2_rs_active(rs)) {
1979 1963 if (gfs2_rbm_eq(&rs->rs_rbm, rbm)) {
1980 for (*nblocks = 0; *nblocks < elen && rs->rs_free; (*nblocks)++) { 1964 block = gfs2_rbm_to_block(rbm);
1981 if (gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, 1965 ret = gfs2_rbm_from_block(&rs->rs_rbm, block + len);
1982 bi->bi_len, rs->rs_biblk) != GFS2_BLKST_FREE) 1966 rlen = min(rs->rs_free, len);
1983 break; 1967 rs->rs_free -= rlen;
1984 gfs2_setbit(rgd, bi->bi_clone, bi, rs->rs_biblk, 1968 rgd->rd_reserved -= rlen;
1985 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); 1969 trace_gfs2_rs(rs, TRACE_RS_CLAIM);
1986 rs->rs_biblk++; 1970 if (rs->rs_free && !ret)
1987 rs->rs_free--; 1971 goto out;
1988 1972 }
1989 BUG_ON(!rgd->rd_reserved); 1973 __rs_deltree(ip, rs);
1990 rgd->rd_reserved--;
1991 dinode = false;
1992 } 1974 }
1993 1975out:
1994 trace_gfs2_rs(ip, rs, TRACE_RS_CLAIM); 1976 spin_unlock(&rgd->rd_rsspin);
1995 if (!rs->rs_free || *nblocks != elen)
1996 gfs2_rs_deltree(rs);
1997
1998 return start_block;
1999} 1977}
2000 1978
2001/** 1979/**
@@ -2014,47 +1992,40 @@ int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
2014{ 1992{
2015 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 1993 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2016 struct buffer_head *dibh; 1994 struct buffer_head *dibh;
2017 struct gfs2_rgrpd *rgd; 1995 struct gfs2_rbm rbm = { .rgd = ip->i_rgd, };
2018 unsigned int ndata; 1996 unsigned int ndata;
2019 u32 goal, blk; /* block, within the rgrp scope */ 1997 u64 goal;
2020 u64 block; /* block, within the file system scope */ 1998 u64 block; /* block, within the file system scope */
2021 int error; 1999 int error;
2022 struct gfs2_bitmap *bi;
2023 2000
2024 /* Only happens if there is a bug in gfs2, return something distinctive 2001 if (gfs2_rs_active(ip->i_res))
2025 * to ensure that it is noticed. 2002 goal = gfs2_rbm_to_block(&ip->i_res->rs_rbm);
2026 */ 2003 else if (!dinode && rgrp_contains_block(rbm.rgd, ip->i_goal))
2027 if (ip->i_res->rs_requested == 0) 2004 goal = ip->i_goal;
2028 return -ECANCELED;
2029
2030 /* If we have a reservation, claim blocks from it. */
2031 if (gfs2_rs_active(ip->i_res)) {
2032 BUG_ON(!ip->i_res->rs_free);
2033 rgd = ip->i_res->rs_rgd;
2034 block = claim_reserved_blks(ip, dinode, nblocks);
2035 if (*nblocks)
2036 goto found_blocks;
2037 }
2038
2039 rgd = ip->i_rgd;
2040
2041 if (!dinode && rgrp_contains_block(rgd, ip->i_goal))
2042 goal = ip->i_goal - rgd->rd_data0;
2043 else 2005 else
2044 goal = rgd->rd_last_alloc; 2006 goal = rbm.rgd->rd_last_alloc + rbm.rgd->rd_data0;
2045 2007
2046 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, &bi); 2008 gfs2_rbm_from_block(&rbm, goal);
2009 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, 0, ip, false);
2010
2011 if (error == -ENOSPC) {
2012 gfs2_rbm_from_block(&rbm, goal);
2013 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, 0, NULL, false);
2014 }
2047 2015
2048 /* Since all blocks are reserved in advance, this shouldn't happen */ 2016 /* Since all blocks are reserved in advance, this shouldn't happen */
2049 if (blk == BFITNOENT) { 2017 if (error) {
2050 printk(KERN_WARNING "BFITNOENT, nblocks=%u\n", *nblocks); 2018 fs_warn(sdp, "inum=%llu error=%d, nblocks=%u, full=%d\n",
2051 printk(KERN_WARNING "FULL=%d\n", 2019 (unsigned long long)ip->i_no_addr, error, *nblocks,
2052 test_bit(GBF_FULL, &rgd->rd_bits->bi_flags)); 2020 test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags));
2053 goto rgrp_error; 2021 goto rgrp_error;
2054 } 2022 }
2055 2023
2056 block = gfs2_alloc_extent(rgd, bi, blk, dinode, nblocks); 2024 gfs2_alloc_extent(&rbm, dinode, nblocks);
2057found_blocks: 2025 block = gfs2_rbm_to_block(&rbm);
2026 rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0;
2027 if (gfs2_rs_active(ip->i_res))
2028 gfs2_adjust_reservation(ip, &rbm, *nblocks);
2058 ndata = *nblocks; 2029 ndata = *nblocks;
2059 if (dinode) 2030 if (dinode)
2060 ndata--; 2031 ndata--;
@@ -2071,22 +2042,22 @@ found_blocks:
2071 brelse(dibh); 2042 brelse(dibh);
2072 } 2043 }
2073 } 2044 }
2074 if (rgd->rd_free < *nblocks) { 2045 if (rbm.rgd->rd_free < *nblocks) {
2075 printk(KERN_WARNING "nblocks=%u\n", *nblocks); 2046 printk(KERN_WARNING "nblocks=%u\n", *nblocks);
2076 goto rgrp_error; 2047 goto rgrp_error;
2077 } 2048 }
2078 2049
2079 rgd->rd_free -= *nblocks; 2050 rbm.rgd->rd_free -= *nblocks;
2080 if (dinode) { 2051 if (dinode) {
2081 rgd->rd_dinodes++; 2052 rbm.rgd->rd_dinodes++;
2082 *generation = rgd->rd_igeneration++; 2053 *generation = rbm.rgd->rd_igeneration++;
2083 if (*generation == 0) 2054 if (*generation == 0)
2084 *generation = rgd->rd_igeneration++; 2055 *generation = rbm.rgd->rd_igeneration++;
2085 } 2056 }
2086 2057
2087 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); 2058 gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh, 1);
2088 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); 2059 gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
2089 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); 2060 gfs2_rgrp_ondisk2lvb(rbm.rgd->rd_rgl, rbm.rgd->rd_bits[0].bi_bh->b_data);
2090 2061
2091 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0); 2062 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
2092 if (dinode) 2063 if (dinode)
@@ -2100,14 +2071,14 @@ found_blocks:
2100 gfs2_quota_change(ip, ndata, ip->i_inode.i_uid, 2071 gfs2_quota_change(ip, ndata, ip->i_inode.i_uid,
2101 ip->i_inode.i_gid); 2072 ip->i_inode.i_gid);
2102 2073
2103 rgd->rd_free_clone -= *nblocks; 2074 rbm.rgd->rd_free_clone -= *nblocks;
2104 trace_gfs2_block_alloc(ip, rgd, block, *nblocks, 2075 trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
2105 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); 2076 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
2106 *bn = block; 2077 *bn = block;
2107 return 0; 2078 return 0;
2108 2079
2109rgrp_error: 2080rgrp_error:
2110 gfs2_rgrp_error(rgd); 2081 gfs2_rgrp_error(rbm.rgd);
2111 return -EIO; 2082 return -EIO;
2112} 2083}
2113 2084