blob: 449d0cb45a845711954f16ba6c107744b0bb893a [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/completion.h>
15#include <linux/buffer_head.h>
16#include <linux/fs.h>
17#include <linux/gfs2_ondisk.h>
18#include <linux/prefetch.h>
19#include <linux/blkdev.h>
20#include <linux/rbtree.h>
21#include <linux/random.h>
22
23#include "gfs2.h"
24#include "incore.h"
25#include "glock.h"
26#include "glops.h"
27#include "lops.h"
28#include "meta_io.h"
29#include "quota.h"
30#include "rgrp.h"
31#include "super.h"
32#include "trans.h"
33#include "util.h"
34#include "log.h"
35#include "inode.h"
36#include "trace_gfs2.h"
37#include "dir.h"
38
39#define BFITNOENT ((u32)~0)
40#define NO_BLOCK ((u64)~0)
41
42#if BITS_PER_LONG == 32
43#define LBITMASK (0x55555555UL)
44#define LBITSKIP55 (0x55555555UL)
45#define LBITSKIP00 (0x00000000UL)
46#else
47#define LBITMASK (0x5555555555555555UL)
48#define LBITSKIP55 (0x5555555555555555UL)
49#define LBITSKIP00 (0x0000000000000000UL)
50#endif
51
52/*
53 * These routines are used by the resource group routines (rgrp.c)
54 * to keep track of block allocation. Each block is represented by two
55 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
56 *
57 * 0 = Free
58 * 1 = Used (not metadata)
59 * 2 = Unlinked (still in use) inode
60 * 3 = Used (metadata)
61 */
62
63struct gfs2_extent {
64 struct gfs2_rbm rbm;
65 u32 len;
66};
67
68static const char valid_change[16] = {
69 /* current */
70 /* n */ 0, 1, 1, 1,
71 /* e */ 1, 0, 0, 0,
72 /* w */ 0, 0, 0, 1,
73 1, 0, 0, 0
74};
75
76static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
77 const struct gfs2_inode *ip, bool nowrap);
78
79
80/**
81 * gfs2_setbit - Set a bit in the bitmaps
82 * @rbm: The position of the bit to set
83 * @do_clone: Also set the clone bitmap, if it exists
84 * @new_state: the new state of the block
85 *
86 */
87
88static inline void gfs2_setbit(const struct gfs2_rbm *rbm, bool do_clone,
89 unsigned char new_state)
90{
91 unsigned char *byte1, *byte2, *end, cur_state;
92 struct gfs2_bitmap *bi = rbm_bi(rbm);
93 unsigned int buflen = bi->bi_len;
94 const unsigned int bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
95
96 byte1 = bi->bi_bh->b_data + bi->bi_offset + (rbm->offset / GFS2_NBBY);
97 end = bi->bi_bh->b_data + bi->bi_offset + buflen;
98
99 BUG_ON(byte1 >= end);
100
101 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
102
103 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
104 pr_warn("buf_blk = 0x%x old_state=%d, new_state=%d\n",
105 rbm->offset, cur_state, new_state);
106 pr_warn("rgrp=0x%llx bi_start=0x%x\n",
107 (unsigned long long)rbm->rgd->rd_addr, bi->bi_start);
108 pr_warn("bi_offset=0x%x bi_len=0x%x\n",
109 bi->bi_offset, bi->bi_len);
110 dump_stack();
111 gfs2_consist_rgrpd(rbm->rgd);
112 return;
113 }
114 *byte1 ^= (cur_state ^ new_state) << bit;
115
116 if (do_clone && bi->bi_clone) {
117 byte2 = bi->bi_clone + bi->bi_offset + (rbm->offset / GFS2_NBBY);
118 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
119 *byte2 ^= (cur_state ^ new_state) << bit;
120 }
121}
122
123/**
124 * gfs2_testbit - test a bit in the bitmaps
125 * @rbm: The bit to test
126 * @use_clone: If true, test the clone bitmap, not the official bitmap.
127 *
128 * Some callers like gfs2_unaligned_extlen need to test the clone bitmaps,
129 * not the "real" bitmaps, to avoid allocating recently freed blocks.
130 *
131 * Returns: The two bit block state of the requested bit
132 */
133
134static inline u8 gfs2_testbit(const struct gfs2_rbm *rbm, bool use_clone)
135{
136 struct gfs2_bitmap *bi = rbm_bi(rbm);
137 const u8 *buffer;
138 const u8 *byte;
139 unsigned int bit;
140
141 if (use_clone && bi->bi_clone)
142 buffer = bi->bi_clone;
143 else
144 buffer = bi->bi_bh->b_data;
145 buffer += bi->bi_offset;
146 byte = buffer + (rbm->offset / GFS2_NBBY);
147 bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
148
149 return (*byte >> bit) & GFS2_BIT_MASK;
150}
151
152/**
153 * gfs2_bit_search
154 * @ptr: Pointer to bitmap data
155 * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
156 * @state: The state we are searching for
157 *
158 * We xor the bitmap data with a patter which is the bitwise opposite
159 * of what we are looking for, this gives rise to a pattern of ones
160 * wherever there is a match. Since we have two bits per entry, we
161 * take this pattern, shift it down by one place and then and it with
162 * the original. All the even bit positions (0,2,4, etc) then represent
163 * successful matches, so we mask with 0x55555..... to remove the unwanted
164 * odd bit positions.
165 *
166 * This allows searching of a whole u64 at once (32 blocks) with a
167 * single test (on 64 bit arches).
168 */
169
170static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
171{
172 u64 tmp;
173 static const u64 search[] = {
174 [0] = 0xffffffffffffffffULL,
175 [1] = 0xaaaaaaaaaaaaaaaaULL,
176 [2] = 0x5555555555555555ULL,
177 [3] = 0x0000000000000000ULL,
178 };
179 tmp = le64_to_cpu(*ptr) ^ search[state];
180 tmp &= (tmp >> 1);
181 tmp &= mask;
182 return tmp;
183}
184
185/**
186 * rs_cmp - multi-block reservation range compare
187 * @blk: absolute file system block number of the new reservation
188 * @len: number of blocks in the new reservation
189 * @rs: existing reservation to compare against
190 *
191 * returns: 1 if the block range is beyond the reach of the reservation
192 * -1 if the block range is before the start of the reservation
193 * 0 if the block range overlaps with the reservation
194 */
195static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
196{
197 u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm);
198
199 if (blk >= startblk + rs->rs_free)
200 return 1;
201 if (blk + len - 1 < startblk)
202 return -1;
203 return 0;
204}
205
206/**
207 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
208 * a block in a given allocation state.
209 * @buf: the buffer that holds the bitmaps
210 * @len: the length (in bytes) of the buffer
211 * @goal: start search at this block's bit-pair (within @buffer)
212 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
213 *
214 * Scope of @goal and returned block number is only within this bitmap buffer,
215 * not entire rgrp or filesystem. @buffer will be offset from the actual
216 * beginning of a bitmap block buffer, skipping any header structures, but
217 * headers are always a multiple of 64 bits long so that the buffer is
218 * always aligned to a 64 bit boundary.
219 *
220 * The size of the buffer is in bytes, but is it assumed that it is
221 * always ok to read a complete multiple of 64 bits at the end
222 * of the block in case the end is no aligned to a natural boundary.
223 *
224 * Return: the block number (bitmap buffer scope) that was found
225 */
226
227static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
228 u32 goal, u8 state)
229{
230 u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
231 const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
232 const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
233 u64 tmp;
234 u64 mask = 0x5555555555555555ULL;
235 u32 bit;
236
237 /* Mask off bits we don't care about at the start of the search */
238 mask <<= spoint;
239 tmp = gfs2_bit_search(ptr, mask, state);
240 ptr++;
241 while(tmp == 0 && ptr < end) {
242 tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
243 ptr++;
244 }
245 /* Mask off any bits which are more than len bytes from the start */
246 if (ptr == end && (len & (sizeof(u64) - 1)))
247 tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
248 /* Didn't find anything, so return */
249 if (tmp == 0)
250 return BFITNOENT;
251 ptr--;
252 bit = __ffs64(tmp);
253 bit /= 2; /* two bits per entry in the bitmap */
254 return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
255}
256
257/**
258 * gfs2_rbm_from_block - Set the rbm based upon rgd and block number
259 * @rbm: The rbm with rgd already set correctly
260 * @block: The block number (filesystem relative)
261 *
262 * This sets the bi and offset members of an rbm based on a
263 * resource group and a filesystem relative block number. The
264 * resource group must be set in the rbm on entry, the bi and
265 * offset members will be set by this function.
266 *
267 * Returns: 0 on success, or an error code
268 */
269
270static int gfs2_rbm_from_block(struct gfs2_rbm *rbm, u64 block)
271{
272 u64 rblock = block - rbm->rgd->rd_data0;
273
274 if (WARN_ON_ONCE(rblock > UINT_MAX))
275 return -EINVAL;
276 if (block >= rbm->rgd->rd_data0 + rbm->rgd->rd_data)
277 return -E2BIG;
278
279 rbm->bii = 0;
280 rbm->offset = (u32)(rblock);
281 /* Check if the block is within the first block */
282 if (rbm->offset < rbm_bi(rbm)->bi_blocks)
283 return 0;
284
285 /* Adjust for the size diff between gfs2_meta_header and gfs2_rgrp */
286 rbm->offset += (sizeof(struct gfs2_rgrp) -
287 sizeof(struct gfs2_meta_header)) * GFS2_NBBY;
288 rbm->bii = rbm->offset / rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
289 rbm->offset -= rbm->bii * rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
290 return 0;
291}
292
293/**
294 * gfs2_rbm_incr - increment an rbm structure
295 * @rbm: The rbm with rgd already set correctly
296 *
297 * This function takes an existing rbm structure and increments it to the next
298 * viable block offset.
299 *
300 * Returns: If incrementing the offset would cause the rbm to go past the
301 * end of the rgrp, true is returned, otherwise false.
302 *
303 */
304
305static bool gfs2_rbm_incr(struct gfs2_rbm *rbm)
306{
307 if (rbm->offset + 1 < rbm_bi(rbm)->bi_blocks) { /* in the same bitmap */
308 rbm->offset++;
309 return false;
310 }
311 if (rbm->bii == rbm->rgd->rd_length - 1) /* at the last bitmap */
312 return true;
313
314 rbm->offset = 0;
315 rbm->bii++;
316 return false;
317}
318
319/**
320 * gfs2_unaligned_extlen - Look for free blocks which are not byte aligned
321 * @rbm: Position to search (value/result)
322 * @n_unaligned: Number of unaligned blocks to check
323 * @len: Decremented for each block found (terminate on zero)
324 *
325 * Returns: true if a non-free block is encountered
326 */
327
328static bool gfs2_unaligned_extlen(struct gfs2_rbm *rbm, u32 n_unaligned, u32 *len)
329{
330 u32 n;
331 u8 res;
332
333 for (n = 0; n < n_unaligned; n++) {
334 res = gfs2_testbit(rbm, true);
335 if (res != GFS2_BLKST_FREE)
336 return true;
337 (*len)--;
338 if (*len == 0)
339 return true;
340 if (gfs2_rbm_incr(rbm))
341 return true;
342 }
343
344 return false;
345}
346
347/**
348 * gfs2_free_extlen - Return extent length of free blocks
349 * @rrbm: Starting position
350 * @len: Max length to check
351 *
352 * Starting at the block specified by the rbm, see how many free blocks
353 * there are, not reading more than len blocks ahead. This can be done
354 * using memchr_inv when the blocks are byte aligned, but has to be done
355 * on a block by block basis in case of unaligned blocks. Also this
356 * function can cope with bitmap boundaries (although it must stop on
357 * a resource group boundary)
358 *
359 * Returns: Number of free blocks in the extent
360 */
361
362static u32 gfs2_free_extlen(const struct gfs2_rbm *rrbm, u32 len)
363{
364 struct gfs2_rbm rbm = *rrbm;
365 u32 n_unaligned = rbm.offset & 3;
366 u32 size = len;
367 u32 bytes;
368 u32 chunk_size;
369 u8 *ptr, *start, *end;
370 u64 block;
371 struct gfs2_bitmap *bi;
372
373 if (n_unaligned &&
374 gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len))
375 goto out;
376
377 n_unaligned = len & 3;
378 /* Start is now byte aligned */
379 while (len > 3) {
380 bi = rbm_bi(&rbm);
381 start = bi->bi_bh->b_data;
382 if (bi->bi_clone)
383 start = bi->bi_clone;
384 start += bi->bi_offset;
385 end = start + bi->bi_len;
386 BUG_ON(rbm.offset & 3);
387 start += (rbm.offset / GFS2_NBBY);
388 bytes = min_t(u32, len / GFS2_NBBY, (end - start));
389 ptr = memchr_inv(start, 0, bytes);
390 chunk_size = ((ptr == NULL) ? bytes : (ptr - start));
391 chunk_size *= GFS2_NBBY;
392 BUG_ON(len < chunk_size);
393 len -= chunk_size;
394 block = gfs2_rbm_to_block(&rbm);
395 if (gfs2_rbm_from_block(&rbm, block + chunk_size)) {
396 n_unaligned = 0;
397 break;
398 }
399 if (ptr) {
400 n_unaligned = 3;
401 break;
402 }
403 n_unaligned = len & 3;
404 }
405
406 /* Deal with any bits left over at the end */
407 if (n_unaligned)
408 gfs2_unaligned_extlen(&rbm, n_unaligned, &len);
409out:
410 return size - len;
411}
412
413/**
414 * gfs2_bitcount - count the number of bits in a certain state
415 * @rgd: the resource group descriptor
416 * @buffer: the buffer that holds the bitmaps
417 * @buflen: the length (in bytes) of the buffer
418 * @state: the state of the block we're looking for
419 *
420 * Returns: The number of bits
421 */
422
423static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
424 unsigned int buflen, u8 state)
425{
426 const u8 *byte = buffer;
427 const u8 *end = buffer + buflen;
428 const u8 state1 = state << 2;
429 const u8 state2 = state << 4;
430 const u8 state3 = state << 6;
431 u32 count = 0;
432
433 for (; byte < end; byte++) {
434 if (((*byte) & 0x03) == state)
435 count++;
436 if (((*byte) & 0x0C) == state1)
437 count++;
438 if (((*byte) & 0x30) == state2)
439 count++;
440 if (((*byte) & 0xC0) == state3)
441 count++;
442 }
443
444 return count;
445}
446
447/**
448 * gfs2_rgrp_verify - Verify that a resource group is consistent
449 * @rgd: the rgrp
450 *
451 */
452
453void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
454{
455 struct gfs2_sbd *sdp = rgd->rd_sbd;
456 struct gfs2_bitmap *bi = NULL;
457 u32 length = rgd->rd_length;
458 u32 count[4], tmp;
459 int buf, x;
460
461 memset(count, 0, 4 * sizeof(u32));
462
463 /* Count # blocks in each of 4 possible allocation states */
464 for (buf = 0; buf < length; buf++) {
465 bi = rgd->rd_bits + buf;
466 for (x = 0; x < 4; x++)
467 count[x] += gfs2_bitcount(rgd,
468 bi->bi_bh->b_data +
469 bi->bi_offset,
470 bi->bi_len, x);
471 }
472
473 if (count[0] != rgd->rd_free) {
474 if (gfs2_consist_rgrpd(rgd))
475 fs_err(sdp, "free data mismatch: %u != %u\n",
476 count[0], rgd->rd_free);
477 return;
478 }
479
480 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
481 if (count[1] != tmp) {
482 if (gfs2_consist_rgrpd(rgd))
483 fs_err(sdp, "used data mismatch: %u != %u\n",
484 count[1], tmp);
485 return;
486 }
487
488 if (count[2] + count[3] != rgd->rd_dinodes) {
489 if (gfs2_consist_rgrpd(rgd))
490 fs_err(sdp, "used metadata mismatch: %u != %u\n",
491 count[2] + count[3], rgd->rd_dinodes);
492 return;
493 }
494}
495
496/**
497 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
498 * @sdp: The GFS2 superblock
499 * @blk: The data block number
500 * @exact: True if this needs to be an exact match
501 *
502 * The @exact argument should be set to true by most callers. The exception
503 * is when we need to match blocks which are not represented by the rgrp
504 * bitmap, but which are part of the rgrp (i.e. padding blocks) which are
505 * there for alignment purposes. Another way of looking at it is that @exact
506 * matches only valid data/metadata blocks, but with @exact false, it will
507 * match any block within the extent of the rgrp.
508 *
509 * Returns: The resource group, or NULL if not found
510 */
511
512struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
513{
514 struct rb_node *n, *next;
515 struct gfs2_rgrpd *cur;
516
517 spin_lock(&sdp->sd_rindex_spin);
518 n = sdp->sd_rindex_tree.rb_node;
519 while (n) {
520 cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
521 next = NULL;
522 if (blk < cur->rd_addr)
523 next = n->rb_left;
524 else if (blk >= cur->rd_data0 + cur->rd_data)
525 next = n->rb_right;
526 if (next == NULL) {
527 spin_unlock(&sdp->sd_rindex_spin);
528 if (exact) {
529 if (blk < cur->rd_addr)
530 return NULL;
531 if (blk >= cur->rd_data0 + cur->rd_data)
532 return NULL;
533 }
534 return cur;
535 }
536 n = next;
537 }
538 spin_unlock(&sdp->sd_rindex_spin);
539
540 return NULL;
541}
542
543/**
544 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
545 * @sdp: The GFS2 superblock
546 *
547 * Returns: The first rgrp in the filesystem
548 */
549
550struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
551{
552 const struct rb_node *n;
553 struct gfs2_rgrpd *rgd;
554
555 spin_lock(&sdp->sd_rindex_spin);
556 n = rb_first(&sdp->sd_rindex_tree);
557 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
558 spin_unlock(&sdp->sd_rindex_spin);
559
560 return rgd;
561}
562
563/**
564 * gfs2_rgrpd_get_next - get the next RG
565 * @rgd: the resource group descriptor
566 *
567 * Returns: The next rgrp
568 */
569
570struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
571{
572 struct gfs2_sbd *sdp = rgd->rd_sbd;
573 const struct rb_node *n;
574
575 spin_lock(&sdp->sd_rindex_spin);
576 n = rb_next(&rgd->rd_node);
577 if (n == NULL)
578 n = rb_first(&sdp->sd_rindex_tree);
579
580 if (unlikely(&rgd->rd_node == n)) {
581 spin_unlock(&sdp->sd_rindex_spin);
582 return NULL;
583 }
584 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
585 spin_unlock(&sdp->sd_rindex_spin);
586 return rgd;
587}
588
589void check_and_update_goal(struct gfs2_inode *ip)
590{
591 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
592 if (!ip->i_goal || gfs2_blk2rgrpd(sdp, ip->i_goal, 1) == NULL)
593 ip->i_goal = ip->i_no_addr;
594}
595
596void gfs2_free_clones(struct gfs2_rgrpd *rgd)
597{
598 int x;
599
600 for (x = 0; x < rgd->rd_length; x++) {
601 struct gfs2_bitmap *bi = rgd->rd_bits + x;
602 kfree(bi->bi_clone);
603 bi->bi_clone = NULL;
604 }
605}
606
607/**
608 * gfs2_rsqa_alloc - make sure we have a reservation assigned to the inode
609 * plus a quota allocations data structure, if necessary
610 * @ip: the inode for this reservation
611 */
612int gfs2_rsqa_alloc(struct gfs2_inode *ip)
613{
614 return gfs2_qa_alloc(ip);
615}
616
617static void dump_rs(struct seq_file *seq, const struct gfs2_blkreserv *rs)
618{
619 struct gfs2_inode *ip = container_of(rs, struct gfs2_inode, i_res);
620
621 gfs2_print_dbg(seq, " B: n:%llu s:%llu b:%u f:%u\n",
622 (unsigned long long)ip->i_no_addr,
623 (unsigned long long)gfs2_rbm_to_block(&rs->rs_rbm),
624 rs->rs_rbm.offset, rs->rs_free);
625}
626
627/**
628 * __rs_deltree - remove a multi-block reservation from the rgd tree
629 * @rs: The reservation to remove
630 *
631 */
632static void __rs_deltree(struct gfs2_blkreserv *rs)
633{
634 struct gfs2_rgrpd *rgd;
635
636 if (!gfs2_rs_active(rs))
637 return;
638
639 rgd = rs->rs_rbm.rgd;
640 trace_gfs2_rs(rs, TRACE_RS_TREEDEL);
641 rb_erase(&rs->rs_node, &rgd->rd_rstree);
642 RB_CLEAR_NODE(&rs->rs_node);
643
644 if (rs->rs_free) {
645 struct gfs2_bitmap *bi = rbm_bi(&rs->rs_rbm);
646
647 /* return reserved blocks to the rgrp */
648 BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free);
649 rs->rs_rbm.rgd->rd_reserved -= rs->rs_free;
650 /* The rgrp extent failure point is likely not to increase;
651 it will only do so if the freed blocks are somehow
652 contiguous with a span of free blocks that follows. Still,
653 it will force the number to be recalculated later. */
654 rgd->rd_extfail_pt += rs->rs_free;
655 rs->rs_free = 0;
656 clear_bit(GBF_FULL, &bi->bi_flags);
657 }
658}
659
660/**
661 * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
662 * @rs: The reservation to remove
663 *
664 */
665void gfs2_rs_deltree(struct gfs2_blkreserv *rs)
666{
667 struct gfs2_rgrpd *rgd;
668
669 rgd = rs->rs_rbm.rgd;
670 if (rgd) {
671 spin_lock(&rgd->rd_rsspin);
672 __rs_deltree(rs);
673 BUG_ON(rs->rs_free);
674 spin_unlock(&rgd->rd_rsspin);
675 }
676}
677
678/**
679 * gfs2_rsqa_delete - delete a multi-block reservation and quota allocation
680 * @ip: The inode for this reservation
681 * @wcount: The inode's write count, or NULL
682 *
683 */
684void gfs2_rsqa_delete(struct gfs2_inode *ip, atomic_t *wcount)
685{
686 down_write(&ip->i_rw_mutex);
687 if ((wcount == NULL) || (atomic_read(wcount) <= 1))
688 gfs2_rs_deltree(&ip->i_res);
689 up_write(&ip->i_rw_mutex);
690 gfs2_qa_delete(ip, wcount);
691}
692
693/**
694 * return_all_reservations - return all reserved blocks back to the rgrp.
695 * @rgd: the rgrp that needs its space back
696 *
697 * We previously reserved a bunch of blocks for allocation. Now we need to
698 * give them back. This leave the reservation structures in tact, but removes
699 * all of their corresponding "no-fly zones".
700 */
701static void return_all_reservations(struct gfs2_rgrpd *rgd)
702{
703 struct rb_node *n;
704 struct gfs2_blkreserv *rs;
705
706 spin_lock(&rgd->rd_rsspin);
707 while ((n = rb_first(&rgd->rd_rstree))) {
708 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
709 __rs_deltree(rs);
710 }
711 spin_unlock(&rgd->rd_rsspin);
712}
713
714void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
715{
716 struct rb_node *n;
717 struct gfs2_rgrpd *rgd;
718 struct gfs2_glock *gl;
719
720 while ((n = rb_first(&sdp->sd_rindex_tree))) {
721 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
722 gl = rgd->rd_gl;
723
724 rb_erase(n, &sdp->sd_rindex_tree);
725
726 if (gl) {
727 glock_clear_object(gl, rgd);
728 gfs2_rgrp_brelse(rgd);
729 gfs2_glock_put(gl);
730 }
731
732 gfs2_free_clones(rgd);
733 kfree(rgd->rd_bits);
734 rgd->rd_bits = NULL;
735 return_all_reservations(rgd);
736 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
737 }
738}
739
740static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
741{
742 pr_info("ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
743 pr_info("ri_length = %u\n", rgd->rd_length);
744 pr_info("ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
745 pr_info("ri_data = %u\n", rgd->rd_data);
746 pr_info("ri_bitbytes = %u\n", rgd->rd_bitbytes);
747}
748
749/**
750 * gfs2_compute_bitstructs - Compute the bitmap sizes
751 * @rgd: The resource group descriptor
752 *
753 * Calculates bitmap descriptors, one for each block that contains bitmap data
754 *
755 * Returns: errno
756 */
757
758static int compute_bitstructs(struct gfs2_rgrpd *rgd)
759{
760 struct gfs2_sbd *sdp = rgd->rd_sbd;
761 struct gfs2_bitmap *bi;
762 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
763 u32 bytes_left, bytes;
764 int x;
765
766 if (!length)
767 return -EINVAL;
768
769 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
770 if (!rgd->rd_bits)
771 return -ENOMEM;
772
773 bytes_left = rgd->rd_bitbytes;
774
775 for (x = 0; x < length; x++) {
776 bi = rgd->rd_bits + x;
777
778 bi->bi_flags = 0;
779 /* small rgrp; bitmap stored completely in header block */
780 if (length == 1) {
781 bytes = bytes_left;
782 bi->bi_offset = sizeof(struct gfs2_rgrp);
783 bi->bi_start = 0;
784 bi->bi_len = bytes;
785 bi->bi_blocks = bytes * GFS2_NBBY;
786 /* header block */
787 } else if (x == 0) {
788 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
789 bi->bi_offset = sizeof(struct gfs2_rgrp);
790 bi->bi_start = 0;
791 bi->bi_len = bytes;
792 bi->bi_blocks = bytes * GFS2_NBBY;
793 /* last block */
794 } else if (x + 1 == length) {
795 bytes = bytes_left;
796 bi->bi_offset = sizeof(struct gfs2_meta_header);
797 bi->bi_start = rgd->rd_bitbytes - bytes_left;
798 bi->bi_len = bytes;
799 bi->bi_blocks = bytes * GFS2_NBBY;
800 /* other blocks */
801 } else {
802 bytes = sdp->sd_sb.sb_bsize -
803 sizeof(struct gfs2_meta_header);
804 bi->bi_offset = sizeof(struct gfs2_meta_header);
805 bi->bi_start = rgd->rd_bitbytes - bytes_left;
806 bi->bi_len = bytes;
807 bi->bi_blocks = bytes * GFS2_NBBY;
808 }
809
810 bytes_left -= bytes;
811 }
812
813 if (bytes_left) {
814 gfs2_consist_rgrpd(rgd);
815 return -EIO;
816 }
817 bi = rgd->rd_bits + (length - 1);
818 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
819 if (gfs2_consist_rgrpd(rgd)) {
820 gfs2_rindex_print(rgd);
821 fs_err(sdp, "start=%u len=%u offset=%u\n",
822 bi->bi_start, bi->bi_len, bi->bi_offset);
823 }
824 return -EIO;
825 }
826
827 return 0;
828}
829
830/**
831 * gfs2_ri_total - Total up the file system space, according to the rindex.
832 * @sdp: the filesystem
833 *
834 */
835u64 gfs2_ri_total(struct gfs2_sbd *sdp)
836{
837 u64 total_data = 0;
838 struct inode *inode = sdp->sd_rindex;
839 struct gfs2_inode *ip = GFS2_I(inode);
840 char buf[sizeof(struct gfs2_rindex)];
841 int error, rgrps;
842
843 for (rgrps = 0;; rgrps++) {
844 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
845
846 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
847 break;
848 error = gfs2_internal_read(ip, buf, &pos,
849 sizeof(struct gfs2_rindex));
850 if (error != sizeof(struct gfs2_rindex))
851 break;
852 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
853 }
854 return total_data;
855}
856
857static int rgd_insert(struct gfs2_rgrpd *rgd)
858{
859 struct gfs2_sbd *sdp = rgd->rd_sbd;
860 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
861
862 /* Figure out where to put new node */
863 while (*newn) {
864 struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
865 rd_node);
866
867 parent = *newn;
868 if (rgd->rd_addr < cur->rd_addr)
869 newn = &((*newn)->rb_left);
870 else if (rgd->rd_addr > cur->rd_addr)
871 newn = &((*newn)->rb_right);
872 else
873 return -EEXIST;
874 }
875
876 rb_link_node(&rgd->rd_node, parent, newn);
877 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
878 sdp->sd_rgrps++;
879 return 0;
880}
881
882/**
883 * read_rindex_entry - Pull in a new resource index entry from the disk
884 * @ip: Pointer to the rindex inode
885 *
886 * Returns: 0 on success, > 0 on EOF, error code otherwise
887 */
888
889static int read_rindex_entry(struct gfs2_inode *ip)
890{
891 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
892 const unsigned bsize = sdp->sd_sb.sb_bsize;
893 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
894 struct gfs2_rindex buf;
895 int error;
896 struct gfs2_rgrpd *rgd;
897
898 if (pos >= i_size_read(&ip->i_inode))
899 return 1;
900
901 error = gfs2_internal_read(ip, (char *)&buf, &pos,
902 sizeof(struct gfs2_rindex));
903
904 if (error != sizeof(struct gfs2_rindex))
905 return (error == 0) ? 1 : error;
906
907 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
908 error = -ENOMEM;
909 if (!rgd)
910 return error;
911
912 rgd->rd_sbd = sdp;
913 rgd->rd_addr = be64_to_cpu(buf.ri_addr);
914 rgd->rd_length = be32_to_cpu(buf.ri_length);
915 rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
916 rgd->rd_data = be32_to_cpu(buf.ri_data);
917 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
918 spin_lock_init(&rgd->rd_rsspin);
919
920 error = compute_bitstructs(rgd);
921 if (error)
922 goto fail;
923
924 error = gfs2_glock_get(sdp, rgd->rd_addr,
925 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
926 if (error)
927 goto fail;
928
929 rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lksb.sb_lvbptr;
930 rgd->rd_flags &= ~(GFS2_RDF_UPTODATE | GFS2_RDF_PREFERRED);
931 if (rgd->rd_data > sdp->sd_max_rg_data)
932 sdp->sd_max_rg_data = rgd->rd_data;
933 spin_lock(&sdp->sd_rindex_spin);
934 error = rgd_insert(rgd);
935 spin_unlock(&sdp->sd_rindex_spin);
936 if (!error) {
937 glock_set_object(rgd->rd_gl, rgd);
938 rgd->rd_gl->gl_vm.start = (rgd->rd_addr * bsize) & PAGE_MASK;
939 rgd->rd_gl->gl_vm.end = PAGE_ALIGN((rgd->rd_addr +
940 rgd->rd_length) * bsize) - 1;
941 return 0;
942 }
943
944 error = 0; /* someone else read in the rgrp; free it and ignore it */
945 gfs2_glock_put(rgd->rd_gl);
946
947fail:
948 kfree(rgd->rd_bits);
949 rgd->rd_bits = NULL;
950 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
951 return error;
952}
953
954/**
955 * set_rgrp_preferences - Run all the rgrps, selecting some we prefer to use
956 * @sdp: the GFS2 superblock
957 *
958 * The purpose of this function is to select a subset of the resource groups
959 * and mark them as PREFERRED. We do it in such a way that each node prefers
960 * to use a unique set of rgrps to minimize glock contention.
961 */
962static void set_rgrp_preferences(struct gfs2_sbd *sdp)
963{
964 struct gfs2_rgrpd *rgd, *first;
965 int i;
966
967 /* Skip an initial number of rgrps, based on this node's journal ID.
968 That should start each node out on its own set. */
969 rgd = gfs2_rgrpd_get_first(sdp);
970 for (i = 0; i < sdp->sd_lockstruct.ls_jid; i++)
971 rgd = gfs2_rgrpd_get_next(rgd);
972 first = rgd;
973
974 do {
975 rgd->rd_flags |= GFS2_RDF_PREFERRED;
976 for (i = 0; i < sdp->sd_journals; i++) {
977 rgd = gfs2_rgrpd_get_next(rgd);
978 if (!rgd || rgd == first)
979 break;
980 }
981 } while (rgd && rgd != first);
982}
983
984/**
985 * gfs2_ri_update - Pull in a new resource index from the disk
986 * @ip: pointer to the rindex inode
987 *
988 * Returns: 0 on successful update, error code otherwise
989 */
990
991static int gfs2_ri_update(struct gfs2_inode *ip)
992{
993 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
994 int error;
995
996 do {
997 error = read_rindex_entry(ip);
998 } while (error == 0);
999
1000 if (error < 0)
1001 return error;
1002
1003 set_rgrp_preferences(sdp);
1004
1005 sdp->sd_rindex_uptodate = 1;
1006 return 0;
1007}
1008
1009/**
1010 * gfs2_rindex_update - Update the rindex if required
1011 * @sdp: The GFS2 superblock
1012 *
1013 * We grab a lock on the rindex inode to make sure that it doesn't
1014 * change whilst we are performing an operation. We keep this lock
1015 * for quite long periods of time compared to other locks. This
1016 * doesn't matter, since it is shared and it is very, very rarely
1017 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
1018 *
1019 * This makes sure that we're using the latest copy of the resource index
1020 * special file, which might have been updated if someone expanded the
1021 * filesystem (via gfs2_grow utility), which adds new resource groups.
1022 *
1023 * Returns: 0 on succeess, error code otherwise
1024 */
1025
1026int gfs2_rindex_update(struct gfs2_sbd *sdp)
1027{
1028 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
1029 struct gfs2_glock *gl = ip->i_gl;
1030 struct gfs2_holder ri_gh;
1031 int error = 0;
1032 int unlock_required = 0;
1033
1034 /* Read new copy from disk if we don't have the latest */
1035 if (!sdp->sd_rindex_uptodate) {
1036 if (!gfs2_glock_is_locked_by_me(gl)) {
1037 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
1038 if (error)
1039 return error;
1040 unlock_required = 1;
1041 }
1042 if (!sdp->sd_rindex_uptodate)
1043 error = gfs2_ri_update(ip);
1044 if (unlock_required)
1045 gfs2_glock_dq_uninit(&ri_gh);
1046 }
1047
1048 return error;
1049}
1050
1051static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
1052{
1053 const struct gfs2_rgrp *str = buf;
1054 u32 rg_flags;
1055
1056 rg_flags = be32_to_cpu(str->rg_flags);
1057 rg_flags &= ~GFS2_RDF_MASK;
1058 rgd->rd_flags &= GFS2_RDF_MASK;
1059 rgd->rd_flags |= rg_flags;
1060 rgd->rd_free = be32_to_cpu(str->rg_free);
1061 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
1062 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
1063 /* rd_data0, rd_data and rd_bitbytes already set from rindex */
1064}
1065
1066static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
1067{
1068 const struct gfs2_rgrp *str = buf;
1069
1070 rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
1071 rgl->rl_flags = str->rg_flags;
1072 rgl->rl_free = str->rg_free;
1073 rgl->rl_dinodes = str->rg_dinodes;
1074 rgl->rl_igeneration = str->rg_igeneration;
1075 rgl->__pad = 0UL;
1076}
1077
1078static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
1079{
1080 struct gfs2_rgrpd *next = gfs2_rgrpd_get_next(rgd);
1081 struct gfs2_rgrp *str = buf;
1082 u32 crc;
1083
1084 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
1085 str->rg_free = cpu_to_be32(rgd->rd_free);
1086 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
1087 if (next == NULL)
1088 str->rg_skip = 0;
1089 else if (next->rd_addr > rgd->rd_addr)
1090 str->rg_skip = cpu_to_be32(next->rd_addr - rgd->rd_addr);
1091 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
1092 str->rg_data0 = cpu_to_be64(rgd->rd_data0);
1093 str->rg_data = cpu_to_be32(rgd->rd_data);
1094 str->rg_bitbytes = cpu_to_be32(rgd->rd_bitbytes);
1095 str->rg_crc = 0;
1096 crc = gfs2_disk_hash(buf, sizeof(struct gfs2_rgrp));
1097 str->rg_crc = cpu_to_be32(crc);
1098
1099 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
1100 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, buf);
1101}
1102
1103static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
1104{
1105 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
1106 struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
1107
1108 if (rgl->rl_flags != str->rg_flags || rgl->rl_free != str->rg_free ||
1109 rgl->rl_dinodes != str->rg_dinodes ||
1110 rgl->rl_igeneration != str->rg_igeneration)
1111 return 0;
1112 return 1;
1113}
1114
1115static u32 count_unlinked(struct gfs2_rgrpd *rgd)
1116{
1117 struct gfs2_bitmap *bi;
1118 const u32 length = rgd->rd_length;
1119 const u8 *buffer = NULL;
1120 u32 i, goal, count = 0;
1121
1122 for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
1123 goal = 0;
1124 buffer = bi->bi_bh->b_data + bi->bi_offset;
1125 WARN_ON(!buffer_uptodate(bi->bi_bh));
1126 while (goal < bi->bi_len * GFS2_NBBY) {
1127 goal = gfs2_bitfit(buffer, bi->bi_len, goal,
1128 GFS2_BLKST_UNLINKED);
1129 if (goal == BFITNOENT)
1130 break;
1131 count++;
1132 goal++;
1133 }
1134 }
1135
1136 return count;
1137}
1138
1139
1140/**
1141 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
1142 * @rgd: the struct gfs2_rgrpd describing the RG to read in
1143 *
1144 * Read in all of a Resource Group's header and bitmap blocks.
1145 * Caller must eventually call gfs2_rgrp_brelse() to free the bitmaps.
1146 *
1147 * Returns: errno
1148 */
1149
1150static int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
1151{
1152 struct gfs2_sbd *sdp = rgd->rd_sbd;
1153 struct gfs2_glock *gl = rgd->rd_gl;
1154 unsigned int length = rgd->rd_length;
1155 struct gfs2_bitmap *bi;
1156 unsigned int x, y;
1157 int error;
1158
1159 if (rgd->rd_bits[0].bi_bh != NULL)
1160 return 0;
1161
1162 for (x = 0; x < length; x++) {
1163 bi = rgd->rd_bits + x;
1164 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, 0, &bi->bi_bh);
1165 if (error)
1166 goto fail;
1167 }
1168
1169 for (y = length; y--;) {
1170 bi = rgd->rd_bits + y;
1171 error = gfs2_meta_wait(sdp, bi->bi_bh);
1172 if (error)
1173 goto fail;
1174 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
1175 GFS2_METATYPE_RG)) {
1176 error = -EIO;
1177 goto fail;
1178 }
1179 }
1180
1181 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
1182 for (x = 0; x < length; x++)
1183 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
1184 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
1185 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
1186 rgd->rd_free_clone = rgd->rd_free;
1187 /* max out the rgrp allocation failure point */
1188 rgd->rd_extfail_pt = rgd->rd_free;
1189 }
1190 if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
1191 rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
1192 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
1193 rgd->rd_bits[0].bi_bh->b_data);
1194 }
1195 else if (sdp->sd_args.ar_rgrplvb) {
1196 if (!gfs2_rgrp_lvb_valid(rgd)){
1197 gfs2_consist_rgrpd(rgd);
1198 error = -EIO;
1199 goto fail;
1200 }
1201 if (rgd->rd_rgl->rl_unlinked == 0)
1202 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1203 }
1204 return 0;
1205
1206fail:
1207 while (x--) {
1208 bi = rgd->rd_bits + x;
1209 brelse(bi->bi_bh);
1210 bi->bi_bh = NULL;
1211 gfs2_assert_warn(sdp, !bi->bi_clone);
1212 }
1213
1214 return error;
1215}
1216
1217static int update_rgrp_lvb(struct gfs2_rgrpd *rgd)
1218{
1219 u32 rl_flags;
1220
1221 if (rgd->rd_flags & GFS2_RDF_UPTODATE)
1222 return 0;
1223
1224 if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
1225 return gfs2_rgrp_bh_get(rgd);
1226
1227 rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
1228 rl_flags &= ~GFS2_RDF_MASK;
1229 rgd->rd_flags &= GFS2_RDF_MASK;
1230 rgd->rd_flags |= (rl_flags | GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
1231 if (rgd->rd_rgl->rl_unlinked == 0)
1232 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1233 rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
1234 rgd->rd_free_clone = rgd->rd_free;
1235 rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
1236 rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
1237 return 0;
1238}
1239
1240int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
1241{
1242 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
1243 struct gfs2_sbd *sdp = rgd->rd_sbd;
1244
1245 if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb)
1246 return 0;
1247 return gfs2_rgrp_bh_get(rgd);
1248}
1249
1250/**
1251 * gfs2_rgrp_brelse - Release RG bitmaps read in with gfs2_rgrp_bh_get()
1252 * @rgd: The resource group
1253 *
1254 */
1255
1256void gfs2_rgrp_brelse(struct gfs2_rgrpd *rgd)
1257{
1258 int x, length = rgd->rd_length;
1259
1260 for (x = 0; x < length; x++) {
1261 struct gfs2_bitmap *bi = rgd->rd_bits + x;
1262 if (bi->bi_bh) {
1263 brelse(bi->bi_bh);
1264 bi->bi_bh = NULL;
1265 }
1266 }
1267
1268}
1269
1270/**
1271 * gfs2_rgrp_go_unlock - Unlock a rgrp glock
1272 * @gh: The glock holder for the resource group
1273 *
1274 */
1275
1276void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
1277{
1278 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
1279 int demote_requested = test_bit(GLF_DEMOTE, &gh->gh_gl->gl_flags) |
1280 test_bit(GLF_PENDING_DEMOTE, &gh->gh_gl->gl_flags);
1281
1282 if (rgd && demote_requested)
1283 gfs2_rgrp_brelse(rgd);
1284}
1285
1286int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
1287 struct buffer_head *bh,
1288 const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
1289{
1290 struct super_block *sb = sdp->sd_vfs;
1291 u64 blk;
1292 sector_t start = 0;
1293 sector_t nr_blks = 0;
1294 int rv;
1295 unsigned int x;
1296 u32 trimmed = 0;
1297 u8 diff;
1298
1299 for (x = 0; x < bi->bi_len; x++) {
1300 const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
1301 clone += bi->bi_offset;
1302 clone += x;
1303 if (bh) {
1304 const u8 *orig = bh->b_data + bi->bi_offset + x;
1305 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
1306 } else {
1307 diff = ~(*clone | (*clone >> 1));
1308 }
1309 diff &= 0x55;
1310 if (diff == 0)
1311 continue;
1312 blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
1313 while(diff) {
1314 if (diff & 1) {
1315 if (nr_blks == 0)
1316 goto start_new_extent;
1317 if ((start + nr_blks) != blk) {
1318 if (nr_blks >= minlen) {
1319 rv = sb_issue_discard(sb,
1320 start, nr_blks,
1321 GFP_NOFS, 0);
1322 if (rv)
1323 goto fail;
1324 trimmed += nr_blks;
1325 }
1326 nr_blks = 0;
1327start_new_extent:
1328 start = blk;
1329 }
1330 nr_blks++;
1331 }
1332 diff >>= 2;
1333 blk++;
1334 }
1335 }
1336 if (nr_blks >= minlen) {
1337 rv = sb_issue_discard(sb, start, nr_blks, GFP_NOFS, 0);
1338 if (rv)
1339 goto fail;
1340 trimmed += nr_blks;
1341 }
1342 if (ptrimmed)
1343 *ptrimmed = trimmed;
1344 return 0;
1345
1346fail:
1347 if (sdp->sd_args.ar_discard)
1348 fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem\n", rv);
1349 sdp->sd_args.ar_discard = 0;
1350 return -EIO;
1351}
1352
1353/**
1354 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
1355 * @filp: Any file on the filesystem
1356 * @argp: Pointer to the arguments (also used to pass result)
1357 *
1358 * Returns: 0 on success, otherwise error code
1359 */
1360
1361int gfs2_fitrim(struct file *filp, void __user *argp)
1362{
1363 struct inode *inode = file_inode(filp);
1364 struct gfs2_sbd *sdp = GFS2_SB(inode);
1365 struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev);
1366 struct buffer_head *bh;
1367 struct gfs2_rgrpd *rgd;
1368 struct gfs2_rgrpd *rgd_end;
1369 struct gfs2_holder gh;
1370 struct fstrim_range r;
1371 int ret = 0;
1372 u64 amt;
1373 u64 trimmed = 0;
1374 u64 start, end, minlen;
1375 unsigned int x;
1376 unsigned bs_shift = sdp->sd_sb.sb_bsize_shift;
1377
1378 if (!capable(CAP_SYS_ADMIN))
1379 return -EPERM;
1380
1381 if (!blk_queue_discard(q))
1382 return -EOPNOTSUPP;
1383
1384 if (copy_from_user(&r, argp, sizeof(r)))
1385 return -EFAULT;
1386
1387 ret = gfs2_rindex_update(sdp);
1388 if (ret)
1389 return ret;
1390
1391 start = r.start >> bs_shift;
1392 end = start + (r.len >> bs_shift);
1393 minlen = max_t(u64, r.minlen,
1394 q->limits.discard_granularity) >> bs_shift;
1395
1396 if (end <= start || minlen > sdp->sd_max_rg_data)
1397 return -EINVAL;
1398
1399 rgd = gfs2_blk2rgrpd(sdp, start, 0);
1400 rgd_end = gfs2_blk2rgrpd(sdp, end, 0);
1401
1402 if ((gfs2_rgrpd_get_first(sdp) == gfs2_rgrpd_get_next(rgd_end))
1403 && (start > rgd_end->rd_data0 + rgd_end->rd_data))
1404 return -EINVAL; /* start is beyond the end of the fs */
1405
1406 while (1) {
1407
1408 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1409 if (ret)
1410 goto out;
1411
1412 if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
1413 /* Trim each bitmap in the rgrp */
1414 for (x = 0; x < rgd->rd_length; x++) {
1415 struct gfs2_bitmap *bi = rgd->rd_bits + x;
1416 ret = gfs2_rgrp_send_discards(sdp,
1417 rgd->rd_data0, NULL, bi, minlen,
1418 &amt);
1419 if (ret) {
1420 gfs2_glock_dq_uninit(&gh);
1421 goto out;
1422 }
1423 trimmed += amt;
1424 }
1425
1426 /* Mark rgrp as having been trimmed */
1427 ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
1428 if (ret == 0) {
1429 bh = rgd->rd_bits[0].bi_bh;
1430 rgd->rd_flags |= GFS2_RGF_TRIMMED;
1431 gfs2_trans_add_meta(rgd->rd_gl, bh);
1432 gfs2_rgrp_out(rgd, bh->b_data);
1433 gfs2_trans_end(sdp);
1434 }
1435 }
1436 gfs2_glock_dq_uninit(&gh);
1437
1438 if (rgd == rgd_end)
1439 break;
1440
1441 rgd = gfs2_rgrpd_get_next(rgd);
1442 }
1443
1444out:
1445 r.len = trimmed << bs_shift;
1446 if (copy_to_user(argp, &r, sizeof(r)))
1447 return -EFAULT;
1448
1449 return ret;
1450}
1451
1452/**
1453 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
1454 * @ip: the inode structure
1455 *
1456 */
1457static void rs_insert(struct gfs2_inode *ip)
1458{
1459 struct rb_node **newn, *parent = NULL;
1460 int rc;
1461 struct gfs2_blkreserv *rs = &ip->i_res;
1462 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd;
1463 u64 fsblock = gfs2_rbm_to_block(&rs->rs_rbm);
1464
1465 BUG_ON(gfs2_rs_active(rs));
1466
1467 spin_lock(&rgd->rd_rsspin);
1468 newn = &rgd->rd_rstree.rb_node;
1469 while (*newn) {
1470 struct gfs2_blkreserv *cur =
1471 rb_entry(*newn, struct gfs2_blkreserv, rs_node);
1472
1473 parent = *newn;
1474 rc = rs_cmp(fsblock, rs->rs_free, cur);
1475 if (rc > 0)
1476 newn = &((*newn)->rb_right);
1477 else if (rc < 0)
1478 newn = &((*newn)->rb_left);
1479 else {
1480 spin_unlock(&rgd->rd_rsspin);
1481 WARN_ON(1);
1482 return;
1483 }
1484 }
1485
1486 rb_link_node(&rs->rs_node, parent, newn);
1487 rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
1488
1489 /* Do our rgrp accounting for the reservation */
1490 rgd->rd_reserved += rs->rs_free; /* blocks reserved */
1491 spin_unlock(&rgd->rd_rsspin);
1492 trace_gfs2_rs(rs, TRACE_RS_INSERT);
1493}
1494
1495/**
1496 * rgd_free - return the number of free blocks we can allocate.
1497 * @rgd: the resource group
1498 *
1499 * This function returns the number of free blocks for an rgrp.
1500 * That's the clone-free blocks (blocks that are free, not including those
1501 * still being used for unlinked files that haven't been deleted.)
1502 *
1503 * It also subtracts any blocks reserved by someone else, but does not
1504 * include free blocks that are still part of our current reservation,
1505 * because obviously we can (and will) allocate them.
1506 */
1507static inline u32 rgd_free(struct gfs2_rgrpd *rgd, struct gfs2_blkreserv *rs)
1508{
1509 u32 tot_reserved, tot_free;
1510
1511 if (WARN_ON_ONCE(rgd->rd_reserved < rs->rs_free))
1512 return 0;
1513 tot_reserved = rgd->rd_reserved - rs->rs_free;
1514
1515 if (rgd->rd_free_clone < tot_reserved)
1516 tot_reserved = 0;
1517
1518 tot_free = rgd->rd_free_clone - tot_reserved;
1519
1520 return tot_free;
1521}
1522
1523/**
1524 * rg_mblk_search - find a group of multiple free blocks to form a reservation
1525 * @rgd: the resource group descriptor
1526 * @ip: pointer to the inode for which we're reserving blocks
1527 * @ap: the allocation parameters
1528 *
1529 */
1530
1531static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
1532 const struct gfs2_alloc_parms *ap)
1533{
1534 struct gfs2_rbm rbm = { .rgd = rgd, };
1535 u64 goal;
1536 struct gfs2_blkreserv *rs = &ip->i_res;
1537 u32 extlen;
1538 u32 free_blocks = rgd_free(rgd, rs);
1539 int ret;
1540 struct inode *inode = &ip->i_inode;
1541
1542 if (S_ISDIR(inode->i_mode))
1543 extlen = 1;
1544 else {
1545 extlen = max_t(u32, atomic_read(&rs->rs_sizehint), ap->target);
1546 extlen = clamp(extlen, RGRP_RSRV_MINBLKS, free_blocks);
1547 }
1548 if ((rgd->rd_free_clone < rgd->rd_reserved) || (free_blocks < extlen))
1549 return;
1550
1551 /* Find bitmap block that contains bits for goal block */
1552 if (rgrp_contains_block(rgd, ip->i_goal))
1553 goal = ip->i_goal;
1554 else
1555 goal = rgd->rd_last_alloc + rgd->rd_data0;
1556
1557 if (WARN_ON(gfs2_rbm_from_block(&rbm, goal)))
1558 return;
1559
1560 ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &extlen, ip, true);
1561 if (ret == 0) {
1562 rs->rs_rbm = rbm;
1563 rs->rs_free = extlen;
1564 rs_insert(ip);
1565 } else {
1566 if (goal == rgd->rd_last_alloc + rgd->rd_data0)
1567 rgd->rd_last_alloc = 0;
1568 }
1569}
1570
1571/**
1572 * gfs2_next_unreserved_block - Return next block that is not reserved
1573 * @rgd: The resource group
1574 * @block: The starting block
1575 * @length: The required length
1576 * @ip: Ignore any reservations for this inode
1577 *
1578 * If the block does not appear in any reservation, then return the
1579 * block number unchanged. If it does appear in the reservation, then
1580 * keep looking through the tree of reservations in order to find the
1581 * first block number which is not reserved.
1582 */
1583
1584static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block,
1585 u32 length,
1586 const struct gfs2_inode *ip)
1587{
1588 struct gfs2_blkreserv *rs;
1589 struct rb_node *n;
1590 int rc;
1591
1592 spin_lock(&rgd->rd_rsspin);
1593 n = rgd->rd_rstree.rb_node;
1594 while (n) {
1595 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1596 rc = rs_cmp(block, length, rs);
1597 if (rc < 0)
1598 n = n->rb_left;
1599 else if (rc > 0)
1600 n = n->rb_right;
1601 else
1602 break;
1603 }
1604
1605 if (n) {
1606 while ((rs_cmp(block, length, rs) == 0) && (&ip->i_res != rs)) {
1607 block = gfs2_rbm_to_block(&rs->rs_rbm) + rs->rs_free;
1608 n = n->rb_right;
1609 if (n == NULL)
1610 break;
1611 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1612 }
1613 }
1614
1615 spin_unlock(&rgd->rd_rsspin);
1616 return block;
1617}
1618
1619/**
1620 * gfs2_reservation_check_and_update - Check for reservations during block alloc
1621 * @rbm: The current position in the resource group
1622 * @ip: The inode for which we are searching for blocks
1623 * @minext: The minimum extent length
1624 * @maxext: A pointer to the maximum extent structure
1625 *
1626 * This checks the current position in the rgrp to see whether there is
1627 * a reservation covering this block. If not then this function is a
1628 * no-op. If there is, then the position is moved to the end of the
1629 * contiguous reservation(s) so that we are pointing at the first
1630 * non-reserved block.
1631 *
1632 * Returns: 0 if no reservation, 1 if @rbm has changed, otherwise an error
1633 */
1634
1635static int gfs2_reservation_check_and_update(struct gfs2_rbm *rbm,
1636 const struct gfs2_inode *ip,
1637 u32 minext,
1638 struct gfs2_extent *maxext)
1639{
1640 u64 block = gfs2_rbm_to_block(rbm);
1641 u32 extlen = 1;
1642 u64 nblock;
1643 int ret;
1644
1645 /*
1646 * If we have a minimum extent length, then skip over any extent
1647 * which is less than the min extent length in size.
1648 */
1649 if (minext) {
1650 extlen = gfs2_free_extlen(rbm, minext);
1651 if (extlen <= maxext->len)
1652 goto fail;
1653 }
1654
1655 /*
1656 * Check the extent which has been found against the reservations
1657 * and skip if parts of it are already reserved
1658 */
1659 nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, ip);
1660 if (nblock == block) {
1661 if (!minext || extlen >= minext)
1662 return 0;
1663
1664 if (extlen > maxext->len) {
1665 maxext->len = extlen;
1666 maxext->rbm = *rbm;
1667 }
1668fail:
1669 nblock = block + extlen;
1670 }
1671 ret = gfs2_rbm_from_block(rbm, nblock);
1672 if (ret < 0)
1673 return ret;
1674 return 1;
1675}
1676
1677/**
1678 * gfs2_rbm_find - Look for blocks of a particular state
1679 * @rbm: Value/result starting position and final position
1680 * @state: The state which we want to find
1681 * @minext: Pointer to the requested extent length (NULL for a single block)
1682 * This is updated to be the actual reservation size.
1683 * @ip: If set, check for reservations
1684 * @nowrap: Stop looking at the end of the rgrp, rather than wrapping
1685 * around until we've reached the starting point.
1686 *
1687 * Side effects:
1688 * - If looking for free blocks, we set GBF_FULL on each bitmap which
1689 * has no free blocks in it.
1690 * - If looking for free blocks, we set rd_extfail_pt on each rgrp which
1691 * has come up short on a free block search.
1692 *
1693 * Returns: 0 on success, -ENOSPC if there is no block of the requested state
1694 */
1695
1696static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
1697 const struct gfs2_inode *ip, bool nowrap)
1698{
1699 struct buffer_head *bh;
1700 int initial_bii;
1701 u32 initial_offset;
1702 int first_bii = rbm->bii;
1703 u32 first_offset = rbm->offset;
1704 u32 offset;
1705 u8 *buffer;
1706 int n = 0;
1707 int iters = rbm->rgd->rd_length;
1708 int ret;
1709 struct gfs2_bitmap *bi;
1710 struct gfs2_extent maxext = { .rbm.rgd = rbm->rgd, };
1711
1712 /* If we are not starting at the beginning of a bitmap, then we
1713 * need to add one to the bitmap count to ensure that we search
1714 * the starting bitmap twice.
1715 */
1716 if (rbm->offset != 0)
1717 iters++;
1718
1719 while(1) {
1720 bi = rbm_bi(rbm);
1721 if ((ip == NULL || !gfs2_rs_active(&ip->i_res)) &&
1722 test_bit(GBF_FULL, &bi->bi_flags) &&
1723 (state == GFS2_BLKST_FREE))
1724 goto next_bitmap;
1725
1726 bh = bi->bi_bh;
1727 buffer = bh->b_data + bi->bi_offset;
1728 WARN_ON(!buffer_uptodate(bh));
1729 if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
1730 buffer = bi->bi_clone + bi->bi_offset;
1731 initial_offset = rbm->offset;
1732 offset = gfs2_bitfit(buffer, bi->bi_len, rbm->offset, state);
1733 if (offset == BFITNOENT)
1734 goto bitmap_full;
1735 rbm->offset = offset;
1736 if (ip == NULL)
1737 return 0;
1738
1739 initial_bii = rbm->bii;
1740 ret = gfs2_reservation_check_and_update(rbm, ip,
1741 minext ? *minext : 0,
1742 &maxext);
1743 if (ret == 0)
1744 return 0;
1745 if (ret > 0) {
1746 n += (rbm->bii - initial_bii);
1747 goto next_iter;
1748 }
1749 if (ret == -E2BIG) {
1750 rbm->bii = 0;
1751 rbm->offset = 0;
1752 n += (rbm->bii - initial_bii);
1753 goto res_covered_end_of_rgrp;
1754 }
1755 return ret;
1756
1757bitmap_full: /* Mark bitmap as full and fall through */
1758 if ((state == GFS2_BLKST_FREE) && initial_offset == 0)
1759 set_bit(GBF_FULL, &bi->bi_flags);
1760
1761next_bitmap: /* Find next bitmap in the rgrp */
1762 rbm->offset = 0;
1763 rbm->bii++;
1764 if (rbm->bii == rbm->rgd->rd_length)
1765 rbm->bii = 0;
1766res_covered_end_of_rgrp:
1767 if ((rbm->bii == 0) && nowrap)
1768 break;
1769 n++;
1770next_iter:
1771 if (n >= iters)
1772 break;
1773 }
1774
1775 if (minext == NULL || state != GFS2_BLKST_FREE)
1776 return -ENOSPC;
1777
1778 /* If the extent was too small, and it's smaller than the smallest
1779 to have failed before, remember for future reference that it's
1780 useless to search this rgrp again for this amount or more. */
1781 if ((first_offset == 0) && (first_bii == 0) &&
1782 (*minext < rbm->rgd->rd_extfail_pt))
1783 rbm->rgd->rd_extfail_pt = *minext;
1784
1785 /* If the maximum extent we found is big enough to fulfill the
1786 minimum requirements, use it anyway. */
1787 if (maxext.len) {
1788 *rbm = maxext.rbm;
1789 *minext = maxext.len;
1790 return 0;
1791 }
1792
1793 return -ENOSPC;
1794}
1795
1796/**
1797 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
1798 * @rgd: The rgrp
1799 * @last_unlinked: block address of the last dinode we unlinked
1800 * @skip: block address we should explicitly not unlink
1801 *
1802 * Returns: 0 if no error
1803 * The inode, if one has been found, in inode.
1804 */
1805
1806static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
1807{
1808 u64 block;
1809 struct gfs2_sbd *sdp = rgd->rd_sbd;
1810 struct gfs2_glock *gl;
1811 struct gfs2_inode *ip;
1812 int error;
1813 int found = 0;
1814 struct gfs2_rbm rbm = { .rgd = rgd, .bii = 0, .offset = 0 };
1815
1816 while (1) {
1817 down_write(&sdp->sd_log_flush_lock);
1818 error = gfs2_rbm_find(&rbm, GFS2_BLKST_UNLINKED, NULL, NULL,
1819 true);
1820 up_write(&sdp->sd_log_flush_lock);
1821 if (error == -ENOSPC)
1822 break;
1823 if (WARN_ON_ONCE(error))
1824 break;
1825
1826 block = gfs2_rbm_to_block(&rbm);
1827 if (gfs2_rbm_from_block(&rbm, block + 1))
1828 break;
1829 if (*last_unlinked != NO_BLOCK && block <= *last_unlinked)
1830 continue;
1831 if (block == skip)
1832 continue;
1833 *last_unlinked = block;
1834
1835 error = gfs2_glock_get(sdp, block, &gfs2_iopen_glops, CREATE, &gl);
1836 if (error)
1837 continue;
1838
1839 /* If the inode is already in cache, we can ignore it here
1840 * because the existing inode disposal code will deal with
1841 * it when all refs have gone away. Accessing gl_object like
1842 * this is not safe in general. Here it is ok because we do
1843 * not dereference the pointer, and we only need an approx
1844 * answer to whether it is NULL or not.
1845 */
1846 ip = gl->gl_object;
1847
1848 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
1849 gfs2_glock_put(gl);
1850 else
1851 found++;
1852
1853 /* Limit reclaim to sensible number of tasks */
1854 if (found > NR_CPUS)
1855 return;
1856 }
1857
1858 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1859 return;
1860}
1861
1862/**
1863 * gfs2_rgrp_congested - Use stats to figure out whether an rgrp is congested
1864 * @rgd: The rgrp in question
1865 * @loops: An indication of how picky we can be (0=very, 1=less so)
1866 *
1867 * This function uses the recently added glock statistics in order to
1868 * figure out whether a parciular resource group is suffering from
1869 * contention from multiple nodes. This is done purely on the basis
1870 * of timings, since this is the only data we have to work with and
1871 * our aim here is to reject a resource group which is highly contended
1872 * but (very important) not to do this too often in order to ensure that
1873 * we do not land up introducing fragmentation by changing resource
1874 * groups when not actually required.
1875 *
1876 * The calculation is fairly simple, we want to know whether the SRTTB
1877 * (i.e. smoothed round trip time for blocking operations) to acquire
1878 * the lock for this rgrp's glock is significantly greater than the
1879 * time taken for resource groups on average. We introduce a margin in
1880 * the form of the variable @var which is computed as the sum of the two
1881 * respective variences, and multiplied by a factor depending on @loops
1882 * and whether we have a lot of data to base the decision on. This is
1883 * then tested against the square difference of the means in order to
1884 * decide whether the result is statistically significant or not.
1885 *
1886 * Returns: A boolean verdict on the congestion status
1887 */
1888
1889static bool gfs2_rgrp_congested(const struct gfs2_rgrpd *rgd, int loops)
1890{
1891 const struct gfs2_glock *gl = rgd->rd_gl;
1892 const struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1893 struct gfs2_lkstats *st;
1894 u64 r_dcount, l_dcount;
1895 u64 l_srttb, a_srttb = 0;
1896 s64 srttb_diff;
1897 u64 sqr_diff;
1898 u64 var;
1899 int cpu, nonzero = 0;
1900
1901 preempt_disable();
1902 for_each_present_cpu(cpu) {
1903 st = &per_cpu_ptr(sdp->sd_lkstats, cpu)->lkstats[LM_TYPE_RGRP];
1904 if (st->stats[GFS2_LKS_SRTTB]) {
1905 a_srttb += st->stats[GFS2_LKS_SRTTB];
1906 nonzero++;
1907 }
1908 }
1909 st = &this_cpu_ptr(sdp->sd_lkstats)->lkstats[LM_TYPE_RGRP];
1910 if (nonzero)
1911 do_div(a_srttb, nonzero);
1912 r_dcount = st->stats[GFS2_LKS_DCOUNT];
1913 var = st->stats[GFS2_LKS_SRTTVARB] +
1914 gl->gl_stats.stats[GFS2_LKS_SRTTVARB];
1915 preempt_enable();
1916
1917 l_srttb = gl->gl_stats.stats[GFS2_LKS_SRTTB];
1918 l_dcount = gl->gl_stats.stats[GFS2_LKS_DCOUNT];
1919
1920 if ((l_dcount < 1) || (r_dcount < 1) || (a_srttb == 0))
1921 return false;
1922
1923 srttb_diff = a_srttb - l_srttb;
1924 sqr_diff = srttb_diff * srttb_diff;
1925
1926 var *= 2;
1927 if (l_dcount < 8 || r_dcount < 8)
1928 var *= 2;
1929 if (loops == 1)
1930 var *= 2;
1931
1932 return ((srttb_diff < 0) && (sqr_diff > var));
1933}
1934
1935/**
1936 * gfs2_rgrp_used_recently
1937 * @rs: The block reservation with the rgrp to test
1938 * @msecs: The time limit in milliseconds
1939 *
1940 * Returns: True if the rgrp glock has been used within the time limit
1941 */
1942static bool gfs2_rgrp_used_recently(const struct gfs2_blkreserv *rs,
1943 u64 msecs)
1944{
1945 u64 tdiff;
1946
1947 tdiff = ktime_to_ns(ktime_sub(ktime_get_real(),
1948 rs->rs_rbm.rgd->rd_gl->gl_dstamp));
1949
1950 return tdiff > (msecs * 1000 * 1000);
1951}
1952
1953static u32 gfs2_orlov_skip(const struct gfs2_inode *ip)
1954{
1955 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1956 u32 skip;
1957
1958 get_random_bytes(&skip, sizeof(skip));
1959 return skip % sdp->sd_rgrps;
1960}
1961
1962static bool gfs2_select_rgrp(struct gfs2_rgrpd **pos, const struct gfs2_rgrpd *begin)
1963{
1964 struct gfs2_rgrpd *rgd = *pos;
1965 struct gfs2_sbd *sdp = rgd->rd_sbd;
1966
1967 rgd = gfs2_rgrpd_get_next(rgd);
1968 if (rgd == NULL)
1969 rgd = gfs2_rgrpd_get_first(sdp);
1970 *pos = rgd;
1971 if (rgd != begin) /* If we didn't wrap */
1972 return true;
1973 return false;
1974}
1975
1976/**
1977 * fast_to_acquire - determine if a resource group will be fast to acquire
1978 *
1979 * If this is one of our preferred rgrps, it should be quicker to acquire,
1980 * because we tried to set ourselves up as dlm lock master.
1981 */
1982static inline int fast_to_acquire(struct gfs2_rgrpd *rgd)
1983{
1984 struct gfs2_glock *gl = rgd->rd_gl;
1985
1986 if (gl->gl_state != LM_ST_UNLOCKED && list_empty(&gl->gl_holders) &&
1987 !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
1988 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1989 return 1;
1990 if (rgd->rd_flags & GFS2_RDF_PREFERRED)
1991 return 1;
1992 return 0;
1993}
1994
1995/**
1996 * gfs2_inplace_reserve - Reserve space in the filesystem
1997 * @ip: the inode to reserve space for
1998 * @ap: the allocation parameters
1999 *
2000 * We try our best to find an rgrp that has at least ap->target blocks
2001 * available. After a couple of passes (loops == 2), the prospects of finding
2002 * such an rgrp diminish. At this stage, we return the first rgrp that has
2003 * atleast ap->min_target blocks available. Either way, we set ap->allowed to
2004 * the number of blocks available in the chosen rgrp.
2005 *
2006 * Returns: 0 on success,
2007 * -ENOMEM if a suitable rgrp can't be found
2008 * errno otherwise
2009 */
2010
2011int gfs2_inplace_reserve(struct gfs2_inode *ip, struct gfs2_alloc_parms *ap)
2012{
2013 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2014 struct gfs2_rgrpd *begin = NULL;
2015 struct gfs2_blkreserv *rs = &ip->i_res;
2016 int error = 0, rg_locked, flags = 0;
2017 u64 last_unlinked = NO_BLOCK;
2018 int loops = 0;
2019 u32 free_blocks, skip = 0;
2020
2021 if (sdp->sd_args.ar_rgrplvb)
2022 flags |= GL_SKIP;
2023 if (gfs2_assert_warn(sdp, ap->target))
2024 return -EINVAL;
2025 if (gfs2_rs_active(rs)) {
2026 begin = rs->rs_rbm.rgd;
2027 } else if (rs->rs_rbm.rgd &&
2028 rgrp_contains_block(rs->rs_rbm.rgd, ip->i_goal)) {
2029 begin = rs->rs_rbm.rgd;
2030 } else {
2031 check_and_update_goal(ip);
2032 rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
2033 }
2034 if (S_ISDIR(ip->i_inode.i_mode) && (ap->aflags & GFS2_AF_ORLOV))
2035 skip = gfs2_orlov_skip(ip);
2036 if (rs->rs_rbm.rgd == NULL)
2037 return -EBADSLT;
2038
2039 while (loops < 3) {
2040 rg_locked = 1;
2041
2042 if (!gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) {
2043 rg_locked = 0;
2044 if (skip && skip--)
2045 goto next_rgrp;
2046 if (!gfs2_rs_active(rs)) {
2047 if (loops == 0 &&
2048 !fast_to_acquire(rs->rs_rbm.rgd))
2049 goto next_rgrp;
2050 if ((loops < 2) &&
2051 gfs2_rgrp_used_recently(rs, 1000) &&
2052 gfs2_rgrp_congested(rs->rs_rbm.rgd, loops))
2053 goto next_rgrp;
2054 }
2055 error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl,
2056 LM_ST_EXCLUSIVE, flags,
2057 &rs->rs_rgd_gh);
2058 if (unlikely(error))
2059 return error;
2060 if (!gfs2_rs_active(rs) && (loops < 2) &&
2061 gfs2_rgrp_congested(rs->rs_rbm.rgd, loops))
2062 goto skip_rgrp;
2063 if (sdp->sd_args.ar_rgrplvb) {
2064 error = update_rgrp_lvb(rs->rs_rbm.rgd);
2065 if (unlikely(error)) {
2066 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
2067 return error;
2068 }
2069 }
2070 }
2071
2072 /* Skip unuseable resource groups */
2073 if ((rs->rs_rbm.rgd->rd_flags & (GFS2_RGF_NOALLOC |
2074 GFS2_RDF_ERROR)) ||
2075 (loops == 0 && ap->target > rs->rs_rbm.rgd->rd_extfail_pt))
2076 goto skip_rgrp;
2077
2078 if (sdp->sd_args.ar_rgrplvb)
2079 gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
2080
2081 /* Get a reservation if we don't already have one */
2082 if (!gfs2_rs_active(rs))
2083 rg_mblk_search(rs->rs_rbm.rgd, ip, ap);
2084
2085 /* Skip rgrps when we can't get a reservation on first pass */
2086 if (!gfs2_rs_active(rs) && (loops < 1))
2087 goto check_rgrp;
2088
2089 /* If rgrp has enough free space, use it */
2090 free_blocks = rgd_free(rs->rs_rbm.rgd, rs);
2091 if (free_blocks >= ap->target ||
2092 (loops == 2 && ap->min_target &&
2093 free_blocks >= ap->min_target)) {
2094 ap->allowed = free_blocks;
2095 return 0;
2096 }
2097check_rgrp:
2098 /* Check for unlinked inodes which can be reclaimed */
2099 if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK)
2100 try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked,
2101 ip->i_no_addr);
2102skip_rgrp:
2103 /* Drop reservation, if we couldn't use reserved rgrp */
2104 if (gfs2_rs_active(rs))
2105 gfs2_rs_deltree(rs);
2106
2107 /* Unlock rgrp if required */
2108 if (!rg_locked)
2109 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
2110next_rgrp:
2111 /* Find the next rgrp, and continue looking */
2112 if (gfs2_select_rgrp(&rs->rs_rbm.rgd, begin))
2113 continue;
2114 if (skip)
2115 continue;
2116
2117 /* If we've scanned all the rgrps, but found no free blocks
2118 * then this checks for some less likely conditions before
2119 * trying again.
2120 */
2121 loops++;
2122 /* Check that fs hasn't grown if writing to rindex */
2123 if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
2124 error = gfs2_ri_update(ip);
2125 if (error)
2126 return error;
2127 }
2128 /* Flushing the log may release space */
2129 if (loops == 2)
2130 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
2131 GFS2_LFC_INPLACE_RESERVE);
2132 }
2133
2134 return -ENOSPC;
2135}
2136
2137/**
2138 * gfs2_inplace_release - release an inplace reservation
2139 * @ip: the inode the reservation was taken out on
2140 *
2141 * Release a reservation made by gfs2_inplace_reserve().
2142 */
2143
2144void gfs2_inplace_release(struct gfs2_inode *ip)
2145{
2146 struct gfs2_blkreserv *rs = &ip->i_res;
2147
2148 if (gfs2_holder_initialized(&rs->rs_rgd_gh))
2149 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
2150}
2151
2152/**
2153 * gfs2_alloc_extent - allocate an extent from a given bitmap
2154 * @rbm: the resource group information
2155 * @dinode: TRUE if the first block we allocate is for a dinode
2156 * @n: The extent length (value/result)
2157 *
2158 * Add the bitmap buffer to the transaction.
2159 * Set the found bits to @new_state to change block's allocation state.
2160 */
2161static void gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
2162 unsigned int *n)
2163{
2164 struct gfs2_rbm pos = { .rgd = rbm->rgd, };
2165 const unsigned int elen = *n;
2166 u64 block;
2167 int ret;
2168
2169 *n = 1;
2170 block = gfs2_rbm_to_block(rbm);
2171 gfs2_trans_add_meta(rbm->rgd->rd_gl, rbm_bi(rbm)->bi_bh);
2172 gfs2_setbit(rbm, true, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
2173 block++;
2174 while (*n < elen) {
2175 ret = gfs2_rbm_from_block(&pos, block);
2176 if (ret || gfs2_testbit(&pos, true) != GFS2_BLKST_FREE)
2177 break;
2178 gfs2_trans_add_meta(pos.rgd->rd_gl, rbm_bi(&pos)->bi_bh);
2179 gfs2_setbit(&pos, true, GFS2_BLKST_USED);
2180 (*n)++;
2181 block++;
2182 }
2183}
2184
2185/**
2186 * rgblk_free - Change alloc state of given block(s)
2187 * @sdp: the filesystem
2188 * @bstart: the start of a run of blocks to free
2189 * @blen: the length of the block run (all must lie within ONE RG!)
2190 * @new_state: GFS2_BLKST_XXX the after-allocation block state
2191 *
2192 * Returns: Resource group containing the block(s)
2193 */
2194
2195static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
2196 u32 blen, unsigned char new_state)
2197{
2198 struct gfs2_rbm rbm;
2199 struct gfs2_bitmap *bi, *bi_prev = NULL;
2200
2201 rbm.rgd = gfs2_blk2rgrpd(sdp, bstart, 1);
2202 if (!rbm.rgd) {
2203 if (gfs2_consist(sdp))
2204 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
2205 return NULL;
2206 }
2207
2208 gfs2_rbm_from_block(&rbm, bstart);
2209 while (blen--) {
2210 bi = rbm_bi(&rbm);
2211 if (bi != bi_prev) {
2212 if (!bi->bi_clone) {
2213 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
2214 GFP_NOFS | __GFP_NOFAIL);
2215 memcpy(bi->bi_clone + bi->bi_offset,
2216 bi->bi_bh->b_data + bi->bi_offset,
2217 bi->bi_len);
2218 }
2219 gfs2_trans_add_meta(rbm.rgd->rd_gl, bi->bi_bh);
2220 bi_prev = bi;
2221 }
2222 gfs2_setbit(&rbm, false, new_state);
2223 gfs2_rbm_incr(&rbm);
2224 }
2225
2226 return rbm.rgd;
2227}
2228
2229/**
2230 * gfs2_rgrp_dump - print out an rgrp
2231 * @seq: The iterator
2232 * @gl: The glock in question
2233 *
2234 */
2235
2236void gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
2237{
2238 struct gfs2_rgrpd *rgd = gl->gl_object;
2239 struct gfs2_blkreserv *trs;
2240 const struct rb_node *n;
2241
2242 if (rgd == NULL)
2243 return;
2244 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u r:%u e:%u\n",
2245 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
2246 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
2247 rgd->rd_reserved, rgd->rd_extfail_pt);
2248 spin_lock(&rgd->rd_rsspin);
2249 for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
2250 trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
2251 dump_rs(seq, trs);
2252 }
2253 spin_unlock(&rgd->rd_rsspin);
2254}
2255
2256static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
2257{
2258 struct gfs2_sbd *sdp = rgd->rd_sbd;
2259 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
2260 (unsigned long long)rgd->rd_addr);
2261 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
2262 gfs2_rgrp_dump(NULL, rgd->rd_gl);
2263 rgd->rd_flags |= GFS2_RDF_ERROR;
2264}
2265
2266/**
2267 * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
2268 * @ip: The inode we have just allocated blocks for
2269 * @rbm: The start of the allocated blocks
2270 * @len: The extent length
2271 *
2272 * Adjusts a reservation after an allocation has taken place. If the
2273 * reservation does not match the allocation, or if it is now empty
2274 * then it is removed.
2275 */
2276
2277static void gfs2_adjust_reservation(struct gfs2_inode *ip,
2278 const struct gfs2_rbm *rbm, unsigned len)
2279{
2280 struct gfs2_blkreserv *rs = &ip->i_res;
2281 struct gfs2_rgrpd *rgd = rbm->rgd;
2282 unsigned rlen;
2283 u64 block;
2284 int ret;
2285
2286 spin_lock(&rgd->rd_rsspin);
2287 if (gfs2_rs_active(rs)) {
2288 if (gfs2_rbm_eq(&rs->rs_rbm, rbm)) {
2289 block = gfs2_rbm_to_block(rbm);
2290 ret = gfs2_rbm_from_block(&rs->rs_rbm, block + len);
2291 rlen = min(rs->rs_free, len);
2292 rs->rs_free -= rlen;
2293 rgd->rd_reserved -= rlen;
2294 trace_gfs2_rs(rs, TRACE_RS_CLAIM);
2295 if (rs->rs_free && !ret)
2296 goto out;
2297 /* We used up our block reservation, so we should
2298 reserve more blocks next time. */
2299 atomic_add(RGRP_RSRV_ADDBLKS, &rs->rs_sizehint);
2300 }
2301 __rs_deltree(rs);
2302 }
2303out:
2304 spin_unlock(&rgd->rd_rsspin);
2305}
2306
2307/**
2308 * gfs2_set_alloc_start - Set starting point for block allocation
2309 * @rbm: The rbm which will be set to the required location
2310 * @ip: The gfs2 inode
2311 * @dinode: Flag to say if allocation includes a new inode
2312 *
2313 * This sets the starting point from the reservation if one is active
2314 * otherwise it falls back to guessing a start point based on the
2315 * inode's goal block or the last allocation point in the rgrp.
2316 */
2317
2318static void gfs2_set_alloc_start(struct gfs2_rbm *rbm,
2319 const struct gfs2_inode *ip, bool dinode)
2320{
2321 u64 goal;
2322
2323 if (gfs2_rs_active(&ip->i_res)) {
2324 *rbm = ip->i_res.rs_rbm;
2325 return;
2326 }
2327
2328 if (!dinode && rgrp_contains_block(rbm->rgd, ip->i_goal))
2329 goal = ip->i_goal;
2330 else
2331 goal = rbm->rgd->rd_last_alloc + rbm->rgd->rd_data0;
2332
2333 gfs2_rbm_from_block(rbm, goal);
2334}
2335
2336/**
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
2340 * @nblocks: requested number of blocks/extent length (value/result)
2341 * @dinode: 1 if we're allocating a dinode block, else 0
2342 * @generation: the generation number of the inode
2343 *
2344 * Returns: 0 or error
2345 */
2346
2347int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
2348 bool dinode, u64 *generation)
2349{
2350 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2351 struct buffer_head *dibh;
2352 struct gfs2_rbm rbm = { .rgd = ip->i_res.rs_rbm.rgd, };
2353 unsigned int ndata;
2354 u64 block; /* block, within the file system scope */
2355 int error;
2356
2357 gfs2_set_alloc_start(&rbm, ip, dinode);
2358 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, NULL, ip, false);
2359
2360 if (error == -ENOSPC) {
2361 gfs2_set_alloc_start(&rbm, ip, dinode);
2362 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, NULL, NULL, false);
2363 }
2364
2365 /* Since all blocks are reserved in advance, this shouldn't happen */
2366 if (error) {
2367 fs_warn(sdp, "inum=%llu error=%d, nblocks=%u, full=%d fail_pt=%d\n",
2368 (unsigned long long)ip->i_no_addr, error, *nblocks,
2369 test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags),
2370 rbm.rgd->rd_extfail_pt);
2371 goto rgrp_error;
2372 }
2373
2374 gfs2_alloc_extent(&rbm, dinode, nblocks);
2375 block = gfs2_rbm_to_block(&rbm);
2376 rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0;
2377 if (gfs2_rs_active(&ip->i_res))
2378 gfs2_adjust_reservation(ip, &rbm, *nblocks);
2379 ndata = *nblocks;
2380 if (dinode)
2381 ndata--;
2382
2383 if (!dinode) {
2384 ip->i_goal = block + ndata - 1;
2385 error = gfs2_meta_inode_buffer(ip, &dibh);
2386 if (error == 0) {
2387 struct gfs2_dinode *di =
2388 (struct gfs2_dinode *)dibh->b_data;
2389 gfs2_trans_add_meta(ip->i_gl, dibh);
2390 di->di_goal_meta = di->di_goal_data =
2391 cpu_to_be64(ip->i_goal);
2392 brelse(dibh);
2393 }
2394 }
2395 if (rbm.rgd->rd_free < *nblocks) {
2396 pr_warn("nblocks=%u\n", *nblocks);
2397 goto rgrp_error;
2398 }
2399
2400 rbm.rgd->rd_free -= *nblocks;
2401 if (dinode) {
2402 rbm.rgd->rd_dinodes++;
2403 *generation = rbm.rgd->rd_igeneration++;
2404 if (*generation == 0)
2405 *generation = rbm.rgd->rd_igeneration++;
2406 }
2407
2408 gfs2_trans_add_meta(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh);
2409 gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
2410
2411 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
2412 if (dinode)
2413 gfs2_trans_add_unrevoke(sdp, block, *nblocks);
2414
2415 gfs2_quota_change(ip, *nblocks, ip->i_inode.i_uid, ip->i_inode.i_gid);
2416
2417 rbm.rgd->rd_free_clone -= *nblocks;
2418 trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
2419 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
2420 *bn = block;
2421 return 0;
2422
2423rgrp_error:
2424 gfs2_rgrp_error(rbm.rgd);
2425 return -EIO;
2426}
2427
2428/**
2429 * __gfs2_free_blocks - free a contiguous run of block(s)
2430 * @ip: the inode these blocks are being freed from
2431 * @bstart: first block of a run of contiguous blocks
2432 * @blen: the length of the block run
2433 * @meta: 1 if the blocks represent metadata
2434 *
2435 */
2436
2437void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
2438{
2439 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2440 struct gfs2_rgrpd *rgd;
2441
2442 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
2443 if (!rgd)
2444 return;
2445 trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
2446 rgd->rd_free += blen;
2447 rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
2448 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
2449 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2450
2451 /* Directories keep their data in the metadata address space */
2452 if (meta || ip->i_depth)
2453 gfs2_meta_wipe(ip, bstart, blen);
2454}
2455
2456/**
2457 * gfs2_free_meta - free a contiguous run of data block(s)
2458 * @ip: the inode these blocks are being freed from
2459 * @bstart: first block of a run of contiguous blocks
2460 * @blen: the length of the block run
2461 *
2462 */
2463
2464void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
2465{
2466 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2467
2468 __gfs2_free_blocks(ip, bstart, blen, 1);
2469 gfs2_statfs_change(sdp, 0, +blen, 0);
2470 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
2471}
2472
2473void gfs2_unlink_di(struct inode *inode)
2474{
2475 struct gfs2_inode *ip = GFS2_I(inode);
2476 struct gfs2_sbd *sdp = GFS2_SB(inode);
2477 struct gfs2_rgrpd *rgd;
2478 u64 blkno = ip->i_no_addr;
2479
2480 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
2481 if (!rgd)
2482 return;
2483 trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
2484 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
2485 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2486 be32_add_cpu(&rgd->rd_rgl->rl_unlinked, 1);
2487}
2488
2489void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
2490{
2491 struct gfs2_sbd *sdp = rgd->rd_sbd;
2492 struct gfs2_rgrpd *tmp_rgd;
2493
2494 tmp_rgd = rgblk_free(sdp, ip->i_no_addr, 1, GFS2_BLKST_FREE);
2495 if (!tmp_rgd)
2496 return;
2497 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
2498
2499 if (!rgd->rd_dinodes)
2500 gfs2_consist_rgrpd(rgd);
2501 rgd->rd_dinodes--;
2502 rgd->rd_free++;
2503
2504 gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
2505 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
2506 be32_add_cpu(&rgd->rd_rgl->rl_unlinked, -1);
2507
2508 gfs2_statfs_change(sdp, 0, +1, -1);
2509 trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
2510 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
2511 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
2512}
2513
2514/**
2515 * gfs2_check_blk_type - Check the type of a block
2516 * @sdp: The superblock
2517 * @no_addr: The block number to check
2518 * @type: The block type we are looking for
2519 *
2520 * Returns: 0 if the block type matches the expected type
2521 * -ESTALE if it doesn't match
2522 * or -ve errno if something went wrong while checking
2523 */
2524
2525int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
2526{
2527 struct gfs2_rgrpd *rgd;
2528 struct gfs2_holder rgd_gh;
2529 struct gfs2_rbm rbm;
2530 int error = -EINVAL;
2531
2532 rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
2533 if (!rgd)
2534 goto fail;
2535
2536 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
2537 if (error)
2538 goto fail;
2539
2540 rbm.rgd = rgd;
2541 error = gfs2_rbm_from_block(&rbm, no_addr);
2542 WARN_ON_ONCE(error != 0);
2543
2544 if (gfs2_testbit(&rbm, false) != type)
2545 error = -ESTALE;
2546
2547 gfs2_glock_dq_uninit(&rgd_gh);
2548fail:
2549 return error;
2550}
2551
2552/**
2553 * gfs2_rlist_add - add a RG to a list of RGs
2554 * @ip: the inode
2555 * @rlist: the list of resource groups
2556 * @block: the block
2557 *
2558 * Figure out what RG a block belongs to and add that RG to the list
2559 *
2560 * FIXME: Don't use NOFAIL
2561 *
2562 */
2563
2564void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
2565 u64 block)
2566{
2567 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2568 struct gfs2_rgrpd *rgd;
2569 struct gfs2_rgrpd **tmp;
2570 unsigned int new_space;
2571 unsigned int x;
2572
2573 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
2574 return;
2575
2576 /*
2577 * The resource group last accessed is kept in the last position.
2578 */
2579
2580 if (rlist->rl_rgrps) {
2581 rgd = rlist->rl_rgd[rlist->rl_rgrps - 1];
2582 if (rgrp_contains_block(rgd, block))
2583 return;
2584 rgd = gfs2_blk2rgrpd(sdp, block, 1);
2585 } else {
2586 rgd = ip->i_res.rs_rbm.rgd;
2587 if (!rgd || !rgrp_contains_block(rgd, block))
2588 rgd = gfs2_blk2rgrpd(sdp, block, 1);
2589 }
2590
2591 if (!rgd) {
2592 fs_err(sdp, "rlist_add: no rgrp for block %llu\n",
2593 (unsigned long long)block);
2594 return;
2595 }
2596
2597 for (x = 0; x < rlist->rl_rgrps; x++) {
2598 if (rlist->rl_rgd[x] == rgd) {
2599 swap(rlist->rl_rgd[x],
2600 rlist->rl_rgd[rlist->rl_rgrps - 1]);
2601 return;
2602 }
2603 }
2604
2605 if (rlist->rl_rgrps == rlist->rl_space) {
2606 new_space = rlist->rl_space + 10;
2607
2608 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
2609 GFP_NOFS | __GFP_NOFAIL);
2610
2611 if (rlist->rl_rgd) {
2612 memcpy(tmp, rlist->rl_rgd,
2613 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
2614 kfree(rlist->rl_rgd);
2615 }
2616
2617 rlist->rl_space = new_space;
2618 rlist->rl_rgd = tmp;
2619 }
2620
2621 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
2622}
2623
2624/**
2625 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
2626 * and initialize an array of glock holders for them
2627 * @rlist: the list of resource groups
2628 * @state: the lock state to acquire the RG lock in
2629 *
2630 * FIXME: Don't use NOFAIL
2631 *
2632 */
2633
2634void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
2635{
2636 unsigned int x;
2637
2638 rlist->rl_ghs = kmalloc_array(rlist->rl_rgrps,
2639 sizeof(struct gfs2_holder),
2640 GFP_NOFS | __GFP_NOFAIL);
2641 for (x = 0; x < rlist->rl_rgrps; x++)
2642 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
2643 state, 0,
2644 &rlist->rl_ghs[x]);
2645}
2646
2647/**
2648 * gfs2_rlist_free - free a resource group list
2649 * @rlist: the list of resource groups
2650 *
2651 */
2652
2653void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
2654{
2655 unsigned int x;
2656
2657 kfree(rlist->rl_rgd);
2658
2659 if (rlist->rl_ghs) {
2660 for (x = 0; x < rlist->rl_rgrps; x++)
2661 gfs2_holder_uninit(&rlist->rl_ghs[x]);
2662 kfree(rlist->rl_ghs);
2663 rlist->rl_ghs = NULL;
2664 }
2665}
2666