GFS2: Rename glops go_xmote_th to go_sync
[deliverable/linux.git] / fs / gfs2 / rgrp.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
fe6c991c 3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
b3b94faa
DT
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
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
b3b94faa
DT
10#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
f42faf4f 14#include <linux/fs.h>
5c676f6d 15#include <linux/gfs2_ondisk.h>
1f466a47 16#include <linux/prefetch.h>
f15ab561 17#include <linux/blkdev.h>
7c9ca621 18#include <linux/rbtree.h>
b3b94faa
DT
19
20#include "gfs2.h"
5c676f6d 21#include "incore.h"
b3b94faa
DT
22#include "glock.h"
23#include "glops.h"
b3b94faa
DT
24#include "lops.h"
25#include "meta_io.h"
26#include "quota.h"
27#include "rgrp.h"
28#include "super.h"
29#include "trans.h"
5c676f6d 30#include "util.h"
172e045a 31#include "log.h"
c8cdf479 32#include "inode.h"
63997775 33#include "trace_gfs2.h"
b3b94faa 34
2c1e52aa 35#define BFITNOENT ((u32)~0)
6760bdcd 36#define NO_BLOCK ((u64)~0)
88c8ab1f 37
1f466a47
BP
38#if BITS_PER_LONG == 32
39#define LBITMASK (0x55555555UL)
40#define LBITSKIP55 (0x55555555UL)
41#define LBITSKIP00 (0x00000000UL)
42#else
43#define LBITMASK (0x5555555555555555UL)
44#define LBITSKIP55 (0x5555555555555555UL)
45#define LBITSKIP00 (0x0000000000000000UL)
46#endif
47
88c8ab1f
SW
48/*
49 * These routines are used by the resource group routines (rgrp.c)
50 * to keep track of block allocation. Each block is represented by two
feaa7bba
SW
51 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
52 *
53 * 0 = Free
54 * 1 = Used (not metadata)
55 * 2 = Unlinked (still in use) inode
56 * 3 = Used (metadata)
88c8ab1f
SW
57 */
58
59static const char valid_change[16] = {
60 /* current */
feaa7bba 61 /* n */ 0, 1, 1, 1,
88c8ab1f 62 /* e */ 1, 0, 0, 0,
feaa7bba 63 /* w */ 0, 0, 0, 1,
88c8ab1f
SW
64 1, 0, 0, 0
65};
66
ff7f4cb4
SW
67static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 minext,
68 const struct gfs2_inode *ip, bool nowrap);
69
70
88c8ab1f
SW
71/**
72 * gfs2_setbit - Set a bit in the bitmaps
3e6339dd
SW
73 * @rbm: The position of the bit to set
74 * @do_clone: Also set the clone bitmap, if it exists
88c8ab1f
SW
75 * @new_state: the new state of the block
76 *
77 */
78
3e6339dd 79static inline void gfs2_setbit(const struct gfs2_rbm *rbm, bool do_clone,
06344b91 80 unsigned char new_state)
88c8ab1f 81{
b45e41d7 82 unsigned char *byte1, *byte2, *end, cur_state;
3e6339dd
SW
83 unsigned int buflen = rbm->bi->bi_len;
84 const unsigned int bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
88c8ab1f 85
3e6339dd
SW
86 byte1 = rbm->bi->bi_bh->b_data + rbm->bi->bi_offset + (rbm->offset / GFS2_NBBY);
87 end = rbm->bi->bi_bh->b_data + rbm->bi->bi_offset + buflen;
88c8ab1f 88
b45e41d7 89 BUG_ON(byte1 >= end);
88c8ab1f 90
b45e41d7 91 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
88c8ab1f 92
b45e41d7 93 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
3e6339dd
SW
94 printk(KERN_WARNING "GFS2: buf_blk = 0x%x old_state=%d, "
95 "new_state=%d\n", rbm->offset, cur_state, new_state);
96 printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%x\n",
97 (unsigned long long)rbm->rgd->rd_addr,
98 rbm->bi->bi_start);
99 printk(KERN_WARNING "GFS2: bi_offset=0x%x bi_len=0x%x\n",
100 rbm->bi->bi_offset, rbm->bi->bi_len);
95c8e17f 101 dump_stack();
3e6339dd 102 gfs2_consist_rgrpd(rbm->rgd);
b45e41d7
SW
103 return;
104 }
105 *byte1 ^= (cur_state ^ new_state) << bit;
106
3e6339dd
SW
107 if (do_clone && rbm->bi->bi_clone) {
108 byte2 = rbm->bi->bi_clone + rbm->bi->bi_offset + (rbm->offset / GFS2_NBBY);
b45e41d7
SW
109 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
110 *byte2 ^= (cur_state ^ new_state) << bit;
111 }
88c8ab1f
SW
112}
113
114/**
115 * gfs2_testbit - test a bit in the bitmaps
c04a2ef3 116 * @rbm: The bit to test
88c8ab1f 117 *
c04a2ef3 118 * Returns: The two bit block state of the requested bit
88c8ab1f
SW
119 */
120
c04a2ef3 121static inline u8 gfs2_testbit(const struct gfs2_rbm *rbm)
88c8ab1f 122{
c04a2ef3
SW
123 const u8 *buffer = rbm->bi->bi_bh->b_data + rbm->bi->bi_offset;
124 const u8 *byte;
88c8ab1f
SW
125 unsigned int bit;
126
c04a2ef3
SW
127 byte = buffer + (rbm->offset / GFS2_NBBY);
128 bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
88c8ab1f 129
c04a2ef3 130 return (*byte >> bit) & GFS2_BIT_MASK;
88c8ab1f
SW
131}
132
223b2b88
SW
133/**
134 * gfs2_bit_search
135 * @ptr: Pointer to bitmap data
136 * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
137 * @state: The state we are searching for
138 *
139 * We xor the bitmap data with a patter which is the bitwise opposite
140 * of what we are looking for, this gives rise to a pattern of ones
141 * wherever there is a match. Since we have two bits per entry, we
142 * take this pattern, shift it down by one place and then and it with
143 * the original. All the even bit positions (0,2,4, etc) then represent
144 * successful matches, so we mask with 0x55555..... to remove the unwanted
145 * odd bit positions.
146 *
147 * This allows searching of a whole u64 at once (32 blocks) with a
148 * single test (on 64 bit arches).
149 */
150
151static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
152{
153 u64 tmp;
154 static const u64 search[] = {
075ac448
HE
155 [0] = 0xffffffffffffffffULL,
156 [1] = 0xaaaaaaaaaaaaaaaaULL,
157 [2] = 0x5555555555555555ULL,
158 [3] = 0x0000000000000000ULL,
223b2b88
SW
159 };
160 tmp = le64_to_cpu(*ptr) ^ search[state];
161 tmp &= (tmp >> 1);
162 tmp &= mask;
163 return tmp;
164}
165
8e2e0047
BP
166/**
167 * rs_cmp - multi-block reservation range compare
168 * @blk: absolute file system block number of the new reservation
169 * @len: number of blocks in the new reservation
170 * @rs: existing reservation to compare against
171 *
172 * returns: 1 if the block range is beyond the reach of the reservation
173 * -1 if the block range is before the start of the reservation
174 * 0 if the block range overlaps with the reservation
175 */
176static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs)
177{
4a993fb1 178 u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm);
8e2e0047
BP
179
180 if (blk >= startblk + rs->rs_free)
181 return 1;
182 if (blk + len - 1 < startblk)
183 return -1;
184 return 0;
185}
186
88c8ab1f
SW
187/**
188 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
189 * a block in a given allocation state.
886b1416 190 * @buf: the buffer that holds the bitmaps
223b2b88 191 * @len: the length (in bytes) of the buffer
88c8ab1f 192 * @goal: start search at this block's bit-pair (within @buffer)
223b2b88 193 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
88c8ab1f
SW
194 *
195 * Scope of @goal and returned block number is only within this bitmap buffer,
196 * not entire rgrp or filesystem. @buffer will be offset from the actual
223b2b88
SW
197 * beginning of a bitmap block buffer, skipping any header structures, but
198 * headers are always a multiple of 64 bits long so that the buffer is
199 * always aligned to a 64 bit boundary.
200 *
201 * The size of the buffer is in bytes, but is it assumed that it is
fd589a8f 202 * always ok to read a complete multiple of 64 bits at the end
223b2b88 203 * of the block in case the end is no aligned to a natural boundary.
88c8ab1f
SW
204 *
205 * Return: the block number (bitmap buffer scope) that was found
206 */
207
02ab1721
HE
208static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
209 u32 goal, u8 state)
88c8ab1f 210{
223b2b88
SW
211 u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
212 const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
213 const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
214 u64 tmp;
075ac448 215 u64 mask = 0x5555555555555555ULL;
223b2b88
SW
216 u32 bit;
217
223b2b88
SW
218 /* Mask off bits we don't care about at the start of the search */
219 mask <<= spoint;
220 tmp = gfs2_bit_search(ptr, mask, state);
221 ptr++;
222 while(tmp == 0 && ptr < end) {
075ac448 223 tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
223b2b88 224 ptr++;
1f466a47 225 }
223b2b88
SW
226 /* Mask off any bits which are more than len bytes from the start */
227 if (ptr == end && (len & (sizeof(u64) - 1)))
228 tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
229 /* Didn't find anything, so return */
230 if (tmp == 0)
231 return BFITNOENT;
232 ptr--;
d8bd504a 233 bit = __ffs64(tmp);
223b2b88
SW
234 bit /= 2; /* two bits per entry in the bitmap */
235 return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
88c8ab1f
SW
236}
237
ff7f4cb4
SW
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;
a68a0a35 254 u32 x;
ff7f4cb4
SW
255
256 if (WARN_ON_ONCE(rblock > UINT_MAX))
257 return -EINVAL;
258 if (block >= rbm->rgd->rd_data0 + rbm->rgd->rd_data)
259 return -E2BIG;
260
a68a0a35
BP
261 rbm->bi = rbm->rgd->rd_bits;
262 rbm->offset = (u32)(rblock);
263 /* Check if the block is within the first block */
264 if (rbm->offset < (rbm->bi->bi_start + rbm->bi->bi_len) * GFS2_NBBY)
265 return 0;
266
267 /* Adjust for the size diff between gfs2_meta_header and gfs2_rgrp */
268 rbm->offset += (sizeof(struct gfs2_rgrp) -
269 sizeof(struct gfs2_meta_header)) * GFS2_NBBY;
270 x = rbm->offset / rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
271 rbm->offset -= x * rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
272 rbm->bi += x;
ff7f4cb4
SW
273 return 0;
274}
275
276/**
277 * gfs2_unaligned_extlen - Look for free blocks which are not byte aligned
278 * @rbm: Position to search (value/result)
279 * @n_unaligned: Number of unaligned blocks to check
280 * @len: Decremented for each block found (terminate on zero)
281 *
282 * Returns: true if a non-free block is encountered
283 */
284
285static bool gfs2_unaligned_extlen(struct gfs2_rbm *rbm, u32 n_unaligned, u32 *len)
286{
287 u64 block;
288 u32 n;
289 u8 res;
290
291 for (n = 0; n < n_unaligned; n++) {
292 res = gfs2_testbit(rbm);
293 if (res != GFS2_BLKST_FREE)
294 return true;
295 (*len)--;
296 if (*len == 0)
297 return true;
298 block = gfs2_rbm_to_block(rbm);
299 if (gfs2_rbm_from_block(rbm, block + 1))
300 return true;
301 }
302
303 return false;
304}
305
306/**
307 * gfs2_free_extlen - Return extent length of free blocks
308 * @rbm: Starting position
309 * @len: Max length to check
310 *
311 * Starting at the block specified by the rbm, see how many free blocks
312 * there are, not reading more than len blocks ahead. This can be done
313 * using memchr_inv when the blocks are byte aligned, but has to be done
314 * on a block by block basis in case of unaligned blocks. Also this
315 * function can cope with bitmap boundaries (although it must stop on
316 * a resource group boundary)
317 *
318 * Returns: Number of free blocks in the extent
319 */
320
321static u32 gfs2_free_extlen(const struct gfs2_rbm *rrbm, u32 len)
322{
323 struct gfs2_rbm rbm = *rrbm;
324 u32 n_unaligned = rbm.offset & 3;
325 u32 size = len;
326 u32 bytes;
327 u32 chunk_size;
328 u8 *ptr, *start, *end;
329 u64 block;
330
331 if (n_unaligned &&
332 gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len))
333 goto out;
334
3701530a 335 n_unaligned = len & 3;
ff7f4cb4
SW
336 /* Start is now byte aligned */
337 while (len > 3) {
338 start = rbm.bi->bi_bh->b_data;
339 if (rbm.bi->bi_clone)
340 start = rbm.bi->bi_clone;
341 end = start + rbm.bi->bi_bh->b_size;
342 start += rbm.bi->bi_offset;
343 BUG_ON(rbm.offset & 3);
344 start += (rbm.offset / GFS2_NBBY);
345 bytes = min_t(u32, len / GFS2_NBBY, (end - start));
346 ptr = memchr_inv(start, 0, bytes);
347 chunk_size = ((ptr == NULL) ? bytes : (ptr - start));
348 chunk_size *= GFS2_NBBY;
349 BUG_ON(len < chunk_size);
350 len -= chunk_size;
351 block = gfs2_rbm_to_block(&rbm);
352 gfs2_rbm_from_block(&rbm, block + chunk_size);
353 n_unaligned = 3;
354 if (ptr)
355 break;
356 n_unaligned = len & 3;
357 }
358
359 /* Deal with any bits left over at the end */
360 if (n_unaligned)
361 gfs2_unaligned_extlen(&rbm, n_unaligned, &len);
362out:
363 return size - len;
364}
365
88c8ab1f
SW
366/**
367 * gfs2_bitcount - count the number of bits in a certain state
886b1416 368 * @rgd: the resource group descriptor
88c8ab1f
SW
369 * @buffer: the buffer that holds the bitmaps
370 * @buflen: the length (in bytes) of the buffer
371 * @state: the state of the block we're looking for
372 *
373 * Returns: The number of bits
374 */
375
110acf38
SW
376static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
377 unsigned int buflen, u8 state)
88c8ab1f 378{
110acf38
SW
379 const u8 *byte = buffer;
380 const u8 *end = buffer + buflen;
381 const u8 state1 = state << 2;
382 const u8 state2 = state << 4;
383 const u8 state3 = state << 6;
cd915493 384 u32 count = 0;
88c8ab1f
SW
385
386 for (; byte < end; byte++) {
387 if (((*byte) & 0x03) == state)
388 count++;
389 if (((*byte) & 0x0C) == state1)
390 count++;
391 if (((*byte) & 0x30) == state2)
392 count++;
393 if (((*byte) & 0xC0) == state3)
394 count++;
395 }
396
397 return count;
398}
399
b3b94faa
DT
400/**
401 * gfs2_rgrp_verify - Verify that a resource group is consistent
b3b94faa
DT
402 * @rgd: the rgrp
403 *
404 */
405
406void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
407{
408 struct gfs2_sbd *sdp = rgd->rd_sbd;
409 struct gfs2_bitmap *bi = NULL;
bb8d8a6f 410 u32 length = rgd->rd_length;
cd915493 411 u32 count[4], tmp;
b3b94faa
DT
412 int buf, x;
413
cd915493 414 memset(count, 0, 4 * sizeof(u32));
b3b94faa
DT
415
416 /* Count # blocks in each of 4 possible allocation states */
417 for (buf = 0; buf < length; buf++) {
418 bi = rgd->rd_bits + buf;
419 for (x = 0; x < 4; x++)
420 count[x] += gfs2_bitcount(rgd,
421 bi->bi_bh->b_data +
422 bi->bi_offset,
423 bi->bi_len, x);
424 }
425
cfc8b549 426 if (count[0] != rgd->rd_free) {
b3b94faa
DT
427 if (gfs2_consist_rgrpd(rgd))
428 fs_err(sdp, "free data mismatch: %u != %u\n",
cfc8b549 429 count[0], rgd->rd_free);
b3b94faa
DT
430 return;
431 }
432
73f74948 433 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
6b946170 434 if (count[1] != tmp) {
b3b94faa
DT
435 if (gfs2_consist_rgrpd(rgd))
436 fs_err(sdp, "used data mismatch: %u != %u\n",
437 count[1], tmp);
438 return;
439 }
440
6b946170 441 if (count[2] + count[3] != rgd->rd_dinodes) {
b3b94faa 442 if (gfs2_consist_rgrpd(rgd))
feaa7bba 443 fs_err(sdp, "used metadata mismatch: %u != %u\n",
6b946170 444 count[2] + count[3], rgd->rd_dinodes);
b3b94faa
DT
445 return;
446 }
b3b94faa
DT
447}
448
bb8d8a6f 449static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
b3b94faa 450{
bb8d8a6f
SW
451 u64 first = rgd->rd_data0;
452 u64 last = first + rgd->rd_data;
16910427 453 return first <= block && block < last;
b3b94faa
DT
454}
455
456/**
457 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
458 * @sdp: The GFS2 superblock
886b1416
BP
459 * @blk: The data block number
460 * @exact: True if this needs to be an exact match
b3b94faa
DT
461 *
462 * Returns: The resource group, or NULL if not found
463 */
464
66fc061b 465struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
b3b94faa 466{
66fc061b 467 struct rb_node *n, *next;
f75bbfb4 468 struct gfs2_rgrpd *cur;
b3b94faa
DT
469
470 spin_lock(&sdp->sd_rindex_spin);
66fc061b
SW
471 n = sdp->sd_rindex_tree.rb_node;
472 while (n) {
473 cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
474 next = NULL;
7c9ca621 475 if (blk < cur->rd_addr)
66fc061b 476 next = n->rb_left;
f75bbfb4 477 else if (blk >= cur->rd_data0 + cur->rd_data)
66fc061b
SW
478 next = n->rb_right;
479 if (next == NULL) {
b3b94faa 480 spin_unlock(&sdp->sd_rindex_spin);
66fc061b
SW
481 if (exact) {
482 if (blk < cur->rd_addr)
483 return NULL;
484 if (blk >= cur->rd_data0 + cur->rd_data)
485 return NULL;
486 }
7c9ca621 487 return cur;
b3b94faa 488 }
66fc061b 489 n = next;
b3b94faa 490 }
b3b94faa
DT
491 spin_unlock(&sdp->sd_rindex_spin);
492
493 return NULL;
494}
495
496/**
497 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
498 * @sdp: The GFS2 superblock
499 *
500 * Returns: The first rgrp in the filesystem
501 */
502
503struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
504{
7c9ca621
BP
505 const struct rb_node *n;
506 struct gfs2_rgrpd *rgd;
507
8339ee54 508 spin_lock(&sdp->sd_rindex_spin);
7c9ca621
BP
509 n = rb_first(&sdp->sd_rindex_tree);
510 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
8339ee54 511 spin_unlock(&sdp->sd_rindex_spin);
7c9ca621
BP
512
513 return rgd;
b3b94faa
DT
514}
515
516/**
517 * gfs2_rgrpd_get_next - get the next RG
886b1416 518 * @rgd: the resource group descriptor
b3b94faa
DT
519 *
520 * Returns: The next rgrp
521 */
522
523struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
524{
7c9ca621
BP
525 struct gfs2_sbd *sdp = rgd->rd_sbd;
526 const struct rb_node *n;
527
528 spin_lock(&sdp->sd_rindex_spin);
529 n = rb_next(&rgd->rd_node);
530 if (n == NULL)
531 n = rb_first(&sdp->sd_rindex_tree);
532
533 if (unlikely(&rgd->rd_node == n)) {
534 spin_unlock(&sdp->sd_rindex_spin);
b3b94faa 535 return NULL;
7c9ca621
BP
536 }
537 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
538 spin_unlock(&sdp->sd_rindex_spin);
539 return rgd;
b3b94faa
DT
540}
541
8339ee54
SW
542void gfs2_free_clones(struct gfs2_rgrpd *rgd)
543{
544 int x;
545
546 for (x = 0; x < rgd->rd_length; x++) {
547 struct gfs2_bitmap *bi = rgd->rd_bits + x;
548 kfree(bi->bi_clone);
549 bi->bi_clone = NULL;
550 }
551}
552
0a305e49
BP
553/**
554 * gfs2_rs_alloc - make sure we have a reservation assigned to the inode
555 * @ip: the inode for this reservation
556 */
557int gfs2_rs_alloc(struct gfs2_inode *ip)
558{
8e2e0047
BP
559 struct gfs2_blkreserv *res;
560
561 if (ip->i_res)
562 return 0;
563
564 res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS);
565 if (!res)
cd0ed19f 566 return -ENOMEM;
0a305e49 567
24d634e8 568 RB_CLEAR_NODE(&res->rs_node);
4a993fb1 569
0a305e49 570 down_write(&ip->i_rw_mutex);
8e2e0047
BP
571 if (ip->i_res)
572 kmem_cache_free(gfs2_rsrv_cachep, res);
573 else
574 ip->i_res = res;
0a305e49 575 up_write(&ip->i_rw_mutex);
cd0ed19f 576 return 0;
0a305e49
BP
577}
578
9e733d39 579static void dump_rs(struct seq_file *seq, const struct gfs2_blkreserv *rs)
8e2e0047 580{
9e733d39
SW
581 gfs2_print_dbg(seq, " B: n:%llu s:%llu b:%u f:%u\n",
582 (unsigned long long)rs->rs_inum,
583 (unsigned long long)gfs2_rbm_to_block(&rs->rs_rbm),
4a993fb1 584 rs->rs_rbm.offset, rs->rs_free);
8e2e0047
BP
585}
586
0a305e49 587/**
8e2e0047
BP
588 * __rs_deltree - remove a multi-block reservation from the rgd tree
589 * @rs: The reservation to remove
590 *
591 */
4a993fb1 592static void __rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
8e2e0047
BP
593{
594 struct gfs2_rgrpd *rgd;
595
596 if (!gfs2_rs_active(rs))
597 return;
598
4a993fb1 599 rgd = rs->rs_rbm.rgd;
9e733d39 600 trace_gfs2_rs(rs, TRACE_RS_TREEDEL);
4a993fb1 601 rb_erase(&rs->rs_node, &rgd->rd_rstree);
24d634e8 602 RB_CLEAR_NODE(&rs->rs_node);
8e2e0047
BP
603
604 if (rs->rs_free) {
605 /* return reserved blocks to the rgrp and the ip */
4a993fb1
SW
606 BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free);
607 rs->rs_rbm.rgd->rd_reserved -= rs->rs_free;
8e2e0047 608 rs->rs_free = 0;
4a993fb1 609 clear_bit(GBF_FULL, &rs->rs_rbm.bi->bi_flags);
8e2e0047
BP
610 smp_mb__after_clear_bit();
611 }
8e2e0047
BP
612}
613
614/**
615 * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
616 * @rs: The reservation to remove
617 *
618 */
4a993fb1 619void gfs2_rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
8e2e0047
BP
620{
621 struct gfs2_rgrpd *rgd;
622
4a993fb1
SW
623 rgd = rs->rs_rbm.rgd;
624 if (rgd) {
625 spin_lock(&rgd->rd_rsspin);
626 __rs_deltree(ip, rs);
627 spin_unlock(&rgd->rd_rsspin);
628 }
8e2e0047
BP
629}
630
631/**
632 * gfs2_rs_delete - delete a multi-block reservation
0a305e49
BP
633 * @ip: The inode for this reservation
634 *
635 */
636void gfs2_rs_delete(struct gfs2_inode *ip)
637{
638 down_write(&ip->i_rw_mutex);
639 if (ip->i_res) {
4a993fb1 640 gfs2_rs_deltree(ip, ip->i_res);
8e2e0047 641 BUG_ON(ip->i_res->rs_free);
0a305e49
BP
642 kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
643 ip->i_res = NULL;
644 }
645 up_write(&ip->i_rw_mutex);
646}
647
8e2e0047
BP
648/**
649 * return_all_reservations - return all reserved blocks back to the rgrp.
650 * @rgd: the rgrp that needs its space back
651 *
652 * We previously reserved a bunch of blocks for allocation. Now we need to
653 * give them back. This leave the reservation structures in tact, but removes
654 * all of their corresponding "no-fly zones".
655 */
656static void return_all_reservations(struct gfs2_rgrpd *rgd)
657{
658 struct rb_node *n;
659 struct gfs2_blkreserv *rs;
660
661 spin_lock(&rgd->rd_rsspin);
662 while ((n = rb_first(&rgd->rd_rstree))) {
663 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
4a993fb1 664 __rs_deltree(NULL, rs);
8e2e0047
BP
665 }
666 spin_unlock(&rgd->rd_rsspin);
667}
668
8339ee54 669void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
b3b94faa 670{
7c9ca621 671 struct rb_node *n;
b3b94faa
DT
672 struct gfs2_rgrpd *rgd;
673 struct gfs2_glock *gl;
674
7c9ca621
BP
675 while ((n = rb_first(&sdp->sd_rindex_tree))) {
676 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
b3b94faa
DT
677 gl = rgd->rd_gl;
678
7c9ca621 679 rb_erase(n, &sdp->sd_rindex_tree);
b3b94faa
DT
680
681 if (gl) {
8339ee54 682 spin_lock(&gl->gl_spin);
5c676f6d 683 gl->gl_object = NULL;
8339ee54 684 spin_unlock(&gl->gl_spin);
29687a2a 685 gfs2_glock_add_to_lru(gl);
b3b94faa
DT
686 gfs2_glock_put(gl);
687 }
688
8339ee54 689 gfs2_free_clones(rgd);
b3b94faa 690 kfree(rgd->rd_bits);
8e2e0047 691 return_all_reservations(rgd);
6bdd9be6 692 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
b3b94faa
DT
693 }
694}
695
bb8d8a6f
SW
696static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
697{
698 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
699 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
700 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
701 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
702 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
703}
704
b3b94faa
DT
705/**
706 * gfs2_compute_bitstructs - Compute the bitmap sizes
707 * @rgd: The resource group descriptor
708 *
709 * Calculates bitmap descriptors, one for each block that contains bitmap data
710 *
711 * Returns: errno
712 */
713
714static int compute_bitstructs(struct gfs2_rgrpd *rgd)
715{
716 struct gfs2_sbd *sdp = rgd->rd_sbd;
717 struct gfs2_bitmap *bi;
bb8d8a6f 718 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
cd915493 719 u32 bytes_left, bytes;
b3b94faa
DT
720 int x;
721
feaa7bba
SW
722 if (!length)
723 return -EINVAL;
724
dd894be8 725 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
b3b94faa
DT
726 if (!rgd->rd_bits)
727 return -ENOMEM;
728
bb8d8a6f 729 bytes_left = rgd->rd_bitbytes;
b3b94faa
DT
730
731 for (x = 0; x < length; x++) {
732 bi = rgd->rd_bits + x;
733
60a0b8f9 734 bi->bi_flags = 0;
b3b94faa
DT
735 /* small rgrp; bitmap stored completely in header block */
736 if (length == 1) {
737 bytes = bytes_left;
738 bi->bi_offset = sizeof(struct gfs2_rgrp);
739 bi->bi_start = 0;
740 bi->bi_len = bytes;
741 /* header block */
742 } else if (x == 0) {
743 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
744 bi->bi_offset = sizeof(struct gfs2_rgrp);
745 bi->bi_start = 0;
746 bi->bi_len = bytes;
747 /* last block */
748 } else if (x + 1 == length) {
749 bytes = bytes_left;
750 bi->bi_offset = sizeof(struct gfs2_meta_header);
bb8d8a6f 751 bi->bi_start = rgd->rd_bitbytes - bytes_left;
b3b94faa
DT
752 bi->bi_len = bytes;
753 /* other blocks */
754 } else {
568f4c96
SW
755 bytes = sdp->sd_sb.sb_bsize -
756 sizeof(struct gfs2_meta_header);
b3b94faa 757 bi->bi_offset = sizeof(struct gfs2_meta_header);
bb8d8a6f 758 bi->bi_start = rgd->rd_bitbytes - bytes_left;
b3b94faa
DT
759 bi->bi_len = bytes;
760 }
761
762 bytes_left -= bytes;
763 }
764
765 if (bytes_left) {
766 gfs2_consist_rgrpd(rgd);
767 return -EIO;
768 }
769 bi = rgd->rd_bits + (length - 1);
bb8d8a6f 770 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
b3b94faa 771 if (gfs2_consist_rgrpd(rgd)) {
bb8d8a6f 772 gfs2_rindex_print(rgd);
b3b94faa
DT
773 fs_err(sdp, "start=%u len=%u offset=%u\n",
774 bi->bi_start, bi->bi_len, bi->bi_offset);
775 }
776 return -EIO;
777 }
778
779 return 0;
780}
781
7ae8fa84
RP
782/**
783 * gfs2_ri_total - Total up the file system space, according to the rindex.
886b1416 784 * @sdp: the filesystem
7ae8fa84
RP
785 *
786 */
787u64 gfs2_ri_total(struct gfs2_sbd *sdp)
788{
789 u64 total_data = 0;
790 struct inode *inode = sdp->sd_rindex;
791 struct gfs2_inode *ip = GFS2_I(inode);
7ae8fa84 792 char buf[sizeof(struct gfs2_rindex)];
7ae8fa84
RP
793 int error, rgrps;
794
7ae8fa84
RP
795 for (rgrps = 0;; rgrps++) {
796 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
797
bcd7278d 798 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
7ae8fa84 799 break;
4306629e 800 error = gfs2_internal_read(ip, buf, &pos,
7ae8fa84
RP
801 sizeof(struct gfs2_rindex));
802 if (error != sizeof(struct gfs2_rindex))
803 break;
bb8d8a6f 804 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
7ae8fa84 805 }
7ae8fa84
RP
806 return total_data;
807}
808
6aad1c3d 809static int rgd_insert(struct gfs2_rgrpd *rgd)
7c9ca621
BP
810{
811 struct gfs2_sbd *sdp = rgd->rd_sbd;
812 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
813
814 /* Figure out where to put new node */
815 while (*newn) {
816 struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
817 rd_node);
818
819 parent = *newn;
820 if (rgd->rd_addr < cur->rd_addr)
821 newn = &((*newn)->rb_left);
822 else if (rgd->rd_addr > cur->rd_addr)
823 newn = &((*newn)->rb_right);
824 else
6aad1c3d 825 return -EEXIST;
7c9ca621
BP
826 }
827
828 rb_link_node(&rgd->rd_node, parent, newn);
829 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
6aad1c3d
BP
830 sdp->sd_rgrps++;
831 return 0;
7c9ca621
BP
832}
833
b3b94faa 834/**
6c53267f 835 * read_rindex_entry - Pull in a new resource index entry from the disk
4306629e 836 * @ip: Pointer to the rindex inode
b3b94faa 837 *
8339ee54 838 * Returns: 0 on success, > 0 on EOF, error code otherwise
6c53267f
RP
839 */
840
4306629e 841static int read_rindex_entry(struct gfs2_inode *ip)
6c53267f
RP
842{
843 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
844 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
8339ee54 845 struct gfs2_rindex buf;
6c53267f
RP
846 int error;
847 struct gfs2_rgrpd *rgd;
848
8339ee54
SW
849 if (pos >= i_size_read(&ip->i_inode))
850 return 1;
851
4306629e 852 error = gfs2_internal_read(ip, (char *)&buf, &pos,
6c53267f 853 sizeof(struct gfs2_rindex));
8339ee54
SW
854
855 if (error != sizeof(struct gfs2_rindex))
856 return (error == 0) ? 1 : error;
6c53267f 857
6bdd9be6 858 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
6c53267f
RP
859 error = -ENOMEM;
860 if (!rgd)
861 return error;
862
6c53267f 863 rgd->rd_sbd = sdp;
8339ee54
SW
864 rgd->rd_addr = be64_to_cpu(buf.ri_addr);
865 rgd->rd_length = be32_to_cpu(buf.ri_length);
866 rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
867 rgd->rd_data = be32_to_cpu(buf.ri_data);
868 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
8e2e0047 869 spin_lock_init(&rgd->rd_rsspin);
7c9ca621 870
6c53267f
RP
871 error = compute_bitstructs(rgd);
872 if (error)
8339ee54 873 goto fail;
6c53267f 874
bb8d8a6f 875 error = gfs2_glock_get(sdp, rgd->rd_addr,
6c53267f
RP
876 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
877 if (error)
8339ee54 878 goto fail;
6c53267f
RP
879
880 rgd->rd_gl->gl_object = rgd;
90306c41 881 rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lvb;
cf45b752 882 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
7c9ca621
BP
883 if (rgd->rd_data > sdp->sd_max_rg_data)
884 sdp->sd_max_rg_data = rgd->rd_data;
8339ee54 885 spin_lock(&sdp->sd_rindex_spin);
6aad1c3d 886 error = rgd_insert(rgd);
8339ee54 887 spin_unlock(&sdp->sd_rindex_spin);
6aad1c3d
BP
888 if (!error)
889 return 0;
890
891 error = 0; /* someone else read in the rgrp; free it and ignore it */
c1ac539e 892 gfs2_glock_put(rgd->rd_gl);
8339ee54
SW
893
894fail:
895 kfree(rgd->rd_bits);
896 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
6c53267f
RP
897 return error;
898}
899
900/**
901 * gfs2_ri_update - Pull in a new resource index from the disk
902 * @ip: pointer to the rindex inode
903 *
b3b94faa
DT
904 * Returns: 0 on successful update, error code otherwise
905 */
906
8339ee54 907static int gfs2_ri_update(struct gfs2_inode *ip)
b3b94faa 908{
feaa7bba 909 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
910 int error;
911
8339ee54 912 do {
4306629e 913 error = read_rindex_entry(ip);
8339ee54
SW
914 } while (error == 0);
915
916 if (error < 0)
917 return error;
b3b94faa 918
cf45b752 919 sdp->sd_rindex_uptodate = 1;
6c53267f
RP
920 return 0;
921}
b3b94faa 922
b3b94faa 923/**
8339ee54 924 * gfs2_rindex_update - Update the rindex if required
b3b94faa 925 * @sdp: The GFS2 superblock
b3b94faa
DT
926 *
927 * We grab a lock on the rindex inode to make sure that it doesn't
928 * change whilst we are performing an operation. We keep this lock
929 * for quite long periods of time compared to other locks. This
930 * doesn't matter, since it is shared and it is very, very rarely
931 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
932 *
933 * This makes sure that we're using the latest copy of the resource index
934 * special file, which might have been updated if someone expanded the
935 * filesystem (via gfs2_grow utility), which adds new resource groups.
936 *
8339ee54 937 * Returns: 0 on succeess, error code otherwise
b3b94faa
DT
938 */
939
8339ee54 940int gfs2_rindex_update(struct gfs2_sbd *sdp)
b3b94faa 941{
feaa7bba 942 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
b3b94faa 943 struct gfs2_glock *gl = ip->i_gl;
8339ee54
SW
944 struct gfs2_holder ri_gh;
945 int error = 0;
a365fbf3 946 int unlock_required = 0;
b3b94faa
DT
947
948 /* Read new copy from disk if we don't have the latest */
cf45b752 949 if (!sdp->sd_rindex_uptodate) {
a365fbf3
SW
950 if (!gfs2_glock_is_locked_by_me(gl)) {
951 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
952 if (error)
6aad1c3d 953 return error;
a365fbf3
SW
954 unlock_required = 1;
955 }
8339ee54 956 if (!sdp->sd_rindex_uptodate)
b3b94faa 957 error = gfs2_ri_update(ip);
a365fbf3
SW
958 if (unlock_required)
959 gfs2_glock_dq_uninit(&ri_gh);
b3b94faa
DT
960 }
961
962 return error;
963}
964
42d52e38 965static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
bb8d8a6f
SW
966{
967 const struct gfs2_rgrp *str = buf;
42d52e38 968 u32 rg_flags;
bb8d8a6f 969
42d52e38 970 rg_flags = be32_to_cpu(str->rg_flags);
09010978 971 rg_flags &= ~GFS2_RDF_MASK;
1ce97e56
SW
972 rgd->rd_flags &= GFS2_RDF_MASK;
973 rgd->rd_flags |= rg_flags;
cfc8b549 974 rgd->rd_free = be32_to_cpu(str->rg_free);
73f74948 975 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
d8b71f73 976 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
bb8d8a6f
SW
977}
978
42d52e38 979static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
bb8d8a6f
SW
980{
981 struct gfs2_rgrp *str = buf;
982
09010978 983 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
cfc8b549 984 str->rg_free = cpu_to_be32(rgd->rd_free);
73f74948 985 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
bb8d8a6f 986 str->__pad = cpu_to_be32(0);
d8b71f73 987 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
bb8d8a6f
SW
988 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
989}
990
90306c41
BM
991static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
992{
993 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
994 struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
995
996 if (rgl->rl_flags != str->rg_flags || rgl->rl_free != str->rg_free ||
997 rgl->rl_dinodes != str->rg_dinodes ||
998 rgl->rl_igeneration != str->rg_igeneration)
999 return 0;
1000 return 1;
1001}
1002
1003static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
1004{
1005 const struct gfs2_rgrp *str = buf;
1006
1007 rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
1008 rgl->rl_flags = str->rg_flags;
1009 rgl->rl_free = str->rg_free;
1010 rgl->rl_dinodes = str->rg_dinodes;
1011 rgl->rl_igeneration = str->rg_igeneration;
1012 rgl->__pad = 0UL;
1013}
1014
1015static void update_rgrp_lvb_unlinked(struct gfs2_rgrpd *rgd, u32 change)
1016{
1017 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
1018 u32 unlinked = be32_to_cpu(rgl->rl_unlinked) + change;
1019 rgl->rl_unlinked = cpu_to_be32(unlinked);
1020}
1021
1022static u32 count_unlinked(struct gfs2_rgrpd *rgd)
1023{
1024 struct gfs2_bitmap *bi;
1025 const u32 length = rgd->rd_length;
1026 const u8 *buffer = NULL;
1027 u32 i, goal, count = 0;
1028
1029 for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
1030 goal = 0;
1031 buffer = bi->bi_bh->b_data + bi->bi_offset;
1032 WARN_ON(!buffer_uptodate(bi->bi_bh));
1033 while (goal < bi->bi_len * GFS2_NBBY) {
1034 goal = gfs2_bitfit(buffer, bi->bi_len, goal,
1035 GFS2_BLKST_UNLINKED);
1036 if (goal == BFITNOENT)
1037 break;
1038 count++;
1039 goal++;
1040 }
1041 }
1042
1043 return count;
1044}
1045
1046
b3b94faa 1047/**
90306c41
BM
1048 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
1049 * @rgd: the struct gfs2_rgrpd describing the RG to read in
b3b94faa
DT
1050 *
1051 * Read in all of a Resource Group's header and bitmap blocks.
1052 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
1053 *
1054 * Returns: errno
1055 */
1056
90306c41 1057int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
b3b94faa
DT
1058{
1059 struct gfs2_sbd *sdp = rgd->rd_sbd;
1060 struct gfs2_glock *gl = rgd->rd_gl;
bb8d8a6f 1061 unsigned int length = rgd->rd_length;
b3b94faa
DT
1062 struct gfs2_bitmap *bi;
1063 unsigned int x, y;
1064 int error;
1065
90306c41
BM
1066 if (rgd->rd_bits[0].bi_bh != NULL)
1067 return 0;
1068
b3b94faa
DT
1069 for (x = 0; x < length; x++) {
1070 bi = rgd->rd_bits + x;
bb8d8a6f 1071 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
b3b94faa
DT
1072 if (error)
1073 goto fail;
1074 }
1075
1076 for (y = length; y--;) {
1077 bi = rgd->rd_bits + y;
7276b3b0 1078 error = gfs2_meta_wait(sdp, bi->bi_bh);
b3b94faa
DT
1079 if (error)
1080 goto fail;
feaa7bba 1081 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
b3b94faa
DT
1082 GFS2_METATYPE_RG)) {
1083 error = -EIO;
1084 goto fail;
1085 }
1086 }
1087
cf45b752 1088 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
60a0b8f9
SW
1089 for (x = 0; x < length; x++)
1090 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
42d52e38 1091 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
1ce97e56 1092 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
7c9ca621 1093 rgd->rd_free_clone = rgd->rd_free;
b3b94faa 1094 }
90306c41
BM
1095 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
1096 rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
1097 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
1098 rgd->rd_bits[0].bi_bh->b_data);
1099 }
1100 else if (sdp->sd_args.ar_rgrplvb) {
1101 if (!gfs2_rgrp_lvb_valid(rgd)){
1102 gfs2_consist_rgrpd(rgd);
1103 error = -EIO;
1104 goto fail;
1105 }
1106 if (rgd->rd_rgl->rl_unlinked == 0)
1107 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1108 }
b3b94faa
DT
1109 return 0;
1110
feaa7bba 1111fail:
b3b94faa
DT
1112 while (x--) {
1113 bi = rgd->rd_bits + x;
1114 brelse(bi->bi_bh);
1115 bi->bi_bh = NULL;
1116 gfs2_assert_warn(sdp, !bi->bi_clone);
1117 }
b3b94faa
DT
1118
1119 return error;
1120}
1121
90306c41
BM
1122int update_rgrp_lvb(struct gfs2_rgrpd *rgd)
1123{
1124 u32 rl_flags;
1125
1126 if (rgd->rd_flags & GFS2_RDF_UPTODATE)
1127 return 0;
1128
1129 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
1130 return gfs2_rgrp_bh_get(rgd);
1131
1132 rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
1133 rl_flags &= ~GFS2_RDF_MASK;
1134 rgd->rd_flags &= GFS2_RDF_MASK;
1135 rgd->rd_flags |= (rl_flags | GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
1136 if (rgd->rd_rgl->rl_unlinked == 0)
1137 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1138 rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
1139 rgd->rd_free_clone = rgd->rd_free;
1140 rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
1141 rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
1142 return 0;
1143}
1144
1145int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
1146{
1147 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
1148 struct gfs2_sbd *sdp = rgd->rd_sbd;
1149
1150 if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb)
1151 return 0;
1152 return gfs2_rgrp_bh_get((struct gfs2_rgrpd *)gh->gh_gl->gl_object);
1153}
1154
b3b94faa 1155/**
7c9ca621 1156 * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get()
886b1416 1157 * @gh: The glock holder for the resource group
b3b94faa
DT
1158 *
1159 */
1160
7c9ca621 1161void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
b3b94faa 1162{
7c9ca621 1163 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
bb8d8a6f 1164 int x, length = rgd->rd_length;
b3b94faa 1165
b3b94faa
DT
1166 for (x = 0; x < length; x++) {
1167 struct gfs2_bitmap *bi = rgd->rd_bits + x;
90306c41
BM
1168 if (bi->bi_bh) {
1169 brelse(bi->bi_bh);
1170 bi->bi_bh = NULL;
1171 }
b3b94faa
DT
1172 }
1173
b3b94faa
DT
1174}
1175
66fc061b 1176int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
7c9ca621 1177 struct buffer_head *bh,
66fc061b 1178 const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
f15ab561
SW
1179{
1180 struct super_block *sb = sdp->sd_vfs;
1181 struct block_device *bdev = sb->s_bdev;
1182 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
e1defc4f 1183 bdev_logical_block_size(sb->s_bdev);
f15ab561 1184 u64 blk;
64d576ba 1185 sector_t start = 0;
f15ab561
SW
1186 sector_t nr_sects = 0;
1187 int rv;
1188 unsigned int x;
66fc061b
SW
1189 u32 trimmed = 0;
1190 u8 diff;
f15ab561
SW
1191
1192 for (x = 0; x < bi->bi_len; x++) {
66fc061b
SW
1193 const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
1194 clone += bi->bi_offset;
1195 clone += x;
1196 if (bh) {
1197 const u8 *orig = bh->b_data + bi->bi_offset + x;
1198 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
1199 } else {
1200 diff = ~(*clone | (*clone >> 1));
1201 }
f15ab561
SW
1202 diff &= 0x55;
1203 if (diff == 0)
1204 continue;
1205 blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
1206 blk *= sects_per_blk; /* convert to sectors */
1207 while(diff) {
1208 if (diff & 1) {
1209 if (nr_sects == 0)
1210 goto start_new_extent;
1211 if ((start + nr_sects) != blk) {
66fc061b
SW
1212 if (nr_sects >= minlen) {
1213 rv = blkdev_issue_discard(bdev,
1214 start, nr_sects,
1215 GFP_NOFS, 0);
1216 if (rv)
1217 goto fail;
1218 trimmed += nr_sects;
1219 }
f15ab561
SW
1220 nr_sects = 0;
1221start_new_extent:
1222 start = blk;
1223 }
1224 nr_sects += sects_per_blk;
1225 }
1226 diff >>= 2;
1227 blk += sects_per_blk;
1228 }
1229 }
66fc061b 1230 if (nr_sects >= minlen) {
dd3932ed 1231 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
f15ab561
SW
1232 if (rv)
1233 goto fail;
66fc061b 1234 trimmed += nr_sects;
f15ab561 1235 }
66fc061b
SW
1236 if (ptrimmed)
1237 *ptrimmed = trimmed;
1238 return 0;
1239
f15ab561 1240fail:
66fc061b
SW
1241 if (sdp->sd_args.ar_discard)
1242 fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
f15ab561 1243 sdp->sd_args.ar_discard = 0;
66fc061b
SW
1244 return -EIO;
1245}
1246
1247/**
1248 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
1249 * @filp: Any file on the filesystem
1250 * @argp: Pointer to the arguments (also used to pass result)
1251 *
1252 * Returns: 0 on success, otherwise error code
1253 */
1254
1255int gfs2_fitrim(struct file *filp, void __user *argp)
1256{
1257 struct inode *inode = filp->f_dentry->d_inode;
1258 struct gfs2_sbd *sdp = GFS2_SB(inode);
1259 struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev);
1260 struct buffer_head *bh;
1261 struct gfs2_rgrpd *rgd;
1262 struct gfs2_rgrpd *rgd_end;
1263 struct gfs2_holder gh;
1264 struct fstrim_range r;
1265 int ret = 0;
1266 u64 amt;
1267 u64 trimmed = 0;
076f0faa 1268 u64 start, end, minlen;
66fc061b 1269 unsigned int x;
076f0faa 1270 unsigned bs_shift = sdp->sd_sb.sb_bsize_shift;
66fc061b
SW
1271
1272 if (!capable(CAP_SYS_ADMIN))
1273 return -EPERM;
1274
1275 if (!blk_queue_discard(q))
1276 return -EOPNOTSUPP;
1277
3a238ade 1278 if (copy_from_user(&r, argp, sizeof(r)))
66fc061b
SW
1279 return -EFAULT;
1280
5e2f7d61
BP
1281 ret = gfs2_rindex_update(sdp);
1282 if (ret)
1283 return ret;
1284
076f0faa
LC
1285 start = r.start >> bs_shift;
1286 end = start + (r.len >> bs_shift);
1287 minlen = max_t(u64, r.minlen,
1288 q->limits.discard_granularity) >> bs_shift;
1289
1290 rgd = gfs2_blk2rgrpd(sdp, start, 0);
1291 rgd_end = gfs2_blk2rgrpd(sdp, end - 1, 0);
1292
1293 if (end <= start ||
1294 minlen > sdp->sd_max_rg_data ||
1295 start > rgd_end->rd_data0 + rgd_end->rd_data)
1296 return -EINVAL;
66fc061b
SW
1297
1298 while (1) {
1299
1300 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1301 if (ret)
1302 goto out;
1303
1304 if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
1305 /* Trim each bitmap in the rgrp */
1306 for (x = 0; x < rgd->rd_length; x++) {
1307 struct gfs2_bitmap *bi = rgd->rd_bits + x;
076f0faa
LC
1308 ret = gfs2_rgrp_send_discards(sdp,
1309 rgd->rd_data0, NULL, bi, minlen,
1310 &amt);
66fc061b
SW
1311 if (ret) {
1312 gfs2_glock_dq_uninit(&gh);
1313 goto out;
1314 }
1315 trimmed += amt;
1316 }
1317
1318 /* Mark rgrp as having been trimmed */
1319 ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
1320 if (ret == 0) {
1321 bh = rgd->rd_bits[0].bi_bh;
1322 rgd->rd_flags |= GFS2_RGF_TRIMMED;
1323 gfs2_trans_add_bh(rgd->rd_gl, bh, 1);
1324 gfs2_rgrp_out(rgd, bh->b_data);
90306c41 1325 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, bh->b_data);
66fc061b
SW
1326 gfs2_trans_end(sdp);
1327 }
1328 }
1329 gfs2_glock_dq_uninit(&gh);
1330
1331 if (rgd == rgd_end)
1332 break;
1333
1334 rgd = gfs2_rgrpd_get_next(rgd);
1335 }
1336
1337out:
1338 r.len = trimmed << 9;
3a238ade 1339 if (copy_to_user(argp, &r, sizeof(r)))
66fc061b
SW
1340 return -EFAULT;
1341
1342 return ret;
f15ab561
SW
1343}
1344
8e2e0047
BP
1345/**
1346 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
8e2e0047 1347 * @ip: the inode structure
8e2e0047 1348 *
8e2e0047 1349 */
ff7f4cb4 1350static void rs_insert(struct gfs2_inode *ip)
8e2e0047
BP
1351{
1352 struct rb_node **newn, *parent = NULL;
1353 int rc;
1354 struct gfs2_blkreserv *rs = ip->i_res;
4a993fb1 1355 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd;
ff7f4cb4 1356 u64 fsblock = gfs2_rbm_to_block(&rs->rs_rbm);
8e2e0047 1357
8e2e0047 1358 BUG_ON(gfs2_rs_active(rs));
c743ffd0 1359
ff7f4cb4
SW
1360 spin_lock(&rgd->rd_rsspin);
1361 newn = &rgd->rd_rstree.rb_node;
8e2e0047
BP
1362 while (*newn) {
1363 struct gfs2_blkreserv *cur =
1364 rb_entry(*newn, struct gfs2_blkreserv, rs_node);
1365
1366 parent = *newn;
ff7f4cb4 1367 rc = rs_cmp(fsblock, rs->rs_free, cur);
8e2e0047
BP
1368 if (rc > 0)
1369 newn = &((*newn)->rb_right);
1370 else if (rc < 0)
1371 newn = &((*newn)->rb_left);
1372 else {
1373 spin_unlock(&rgd->rd_rsspin);
ff7f4cb4
SW
1374 WARN_ON(1);
1375 return;
8e2e0047
BP
1376 }
1377 }
1378
8e2e0047
BP
1379 rb_link_node(&rs->rs_node, parent, newn);
1380 rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
1381
8e2e0047 1382 /* Do our rgrp accounting for the reservation */
ff7f4cb4 1383 rgd->rd_reserved += rs->rs_free; /* blocks reserved */
8e2e0047 1384 spin_unlock(&rgd->rd_rsspin);
9e733d39 1385 trace_gfs2_rs(rs, TRACE_RS_INSERT);
8e2e0047
BP
1386}
1387
1388/**
ff7f4cb4 1389 * rg_mblk_search - find a group of multiple free blocks to form a reservation
8e2e0047 1390 * @rgd: the resource group descriptor
8e2e0047 1391 * @ip: pointer to the inode for which we're reserving blocks
c743ffd0 1392 * @requested: number of blocks required for this allocation
8e2e0047 1393 *
8e2e0047
BP
1394 */
1395
ff7f4cb4
SW
1396static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
1397 unsigned requested)
8e2e0047 1398{
ff7f4cb4
SW
1399 struct gfs2_rbm rbm = { .rgd = rgd, };
1400 u64 goal;
1401 struct gfs2_blkreserv *rs = ip->i_res;
1402 u32 extlen;
1403 u32 free_blocks = rgd->rd_free_clone - rgd->rd_reserved;
1404 int ret;
8e2e0047 1405
ff7f4cb4
SW
1406 extlen = max_t(u32, atomic_read(&rs->rs_sizehint), requested);
1407 extlen = clamp(extlen, RGRP_RSRV_MINBLKS, free_blocks);
1408 if ((rgd->rd_free_clone < rgd->rd_reserved) || (free_blocks < extlen))
c743ffd0
SW
1409 return;
1410
8e2e0047
BP
1411 /* Find bitmap block that contains bits for goal block */
1412 if (rgrp_contains_block(rgd, ip->i_goal))
ff7f4cb4 1413 goal = ip->i_goal;
8e2e0047 1414 else
ff7f4cb4 1415 goal = rgd->rd_last_alloc + rgd->rd_data0;
8e2e0047 1416
ff7f4cb4
SW
1417 if (WARN_ON(gfs2_rbm_from_block(&rbm, goal)))
1418 return;
8e2e0047 1419
ff7f4cb4
SW
1420 ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, extlen, ip, true);
1421 if (ret == 0) {
1422 rs->rs_rbm = rbm;
1423 rs->rs_free = extlen;
1424 rs->rs_inum = ip->i_no_addr;
1425 rs_insert(ip);
8e2e0047 1426 }
b3e47ca0
BP
1427}
1428
5b924ae2
SW
1429/**
1430 * gfs2_next_unreserved_block - Return next block that is not reserved
1431 * @rgd: The resource group
1432 * @block: The starting block
ff7f4cb4 1433 * @length: The required length
5b924ae2
SW
1434 * @ip: Ignore any reservations for this inode
1435 *
1436 * If the block does not appear in any reservation, then return the
1437 * block number unchanged. If it does appear in the reservation, then
1438 * keep looking through the tree of reservations in order to find the
1439 * first block number which is not reserved.
1440 */
1441
1442static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block,
ff7f4cb4 1443 u32 length,
5b924ae2
SW
1444 const struct gfs2_inode *ip)
1445{
1446 struct gfs2_blkreserv *rs;
1447 struct rb_node *n;
1448 int rc;
1449
1450 spin_lock(&rgd->rd_rsspin);
ff7f4cb4 1451 n = rgd->rd_rstree.rb_node;
5b924ae2
SW
1452 while (n) {
1453 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
ff7f4cb4 1454 rc = rs_cmp(block, length, rs);
5b924ae2
SW
1455 if (rc < 0)
1456 n = n->rb_left;
1457 else if (rc > 0)
1458 n = n->rb_right;
1459 else
1460 break;
1461 }
1462
1463 if (n) {
ff7f4cb4 1464 while ((rs_cmp(block, length, rs) == 0) && (ip->i_res != rs)) {
5b924ae2 1465 block = gfs2_rbm_to_block(&rs->rs_rbm) + rs->rs_free;
ff7f4cb4 1466 n = n->rb_right;
5b924ae2
SW
1467 if (n == NULL)
1468 break;
1469 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1470 }
1471 }
1472
1473 spin_unlock(&rgd->rd_rsspin);
1474 return block;
1475}
1476
5b924ae2
SW
1477/**
1478 * gfs2_reservation_check_and_update - Check for reservations during block alloc
1479 * @rbm: The current position in the resource group
ff7f4cb4
SW
1480 * @ip: The inode for which we are searching for blocks
1481 * @minext: The minimum extent length
5b924ae2
SW
1482 *
1483 * This checks the current position in the rgrp to see whether there is
1484 * a reservation covering this block. If not then this function is a
1485 * no-op. If there is, then the position is moved to the end of the
1486 * contiguous reservation(s) so that we are pointing at the first
1487 * non-reserved block.
1488 *
1489 * Returns: 0 if no reservation, 1 if @rbm has changed, otherwise an error
1490 */
1491
1492static int gfs2_reservation_check_and_update(struct gfs2_rbm *rbm,
ff7f4cb4
SW
1493 const struct gfs2_inode *ip,
1494 u32 minext)
5b924ae2
SW
1495{
1496 u64 block = gfs2_rbm_to_block(rbm);
ff7f4cb4 1497 u32 extlen = 1;
5b924ae2
SW
1498 u64 nblock;
1499 int ret;
1500
ff7f4cb4
SW
1501 /*
1502 * If we have a minimum extent length, then skip over any extent
1503 * which is less than the min extent length in size.
1504 */
1505 if (minext) {
1506 extlen = gfs2_free_extlen(rbm, minext);
1507 nblock = block + extlen;
1508 if (extlen < minext)
1509 goto fail;
1510 }
1511
1512 /*
1513 * Check the extent which has been found against the reservations
1514 * and skip if parts of it are already reserved
1515 */
1516 nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, ip);
5b924ae2
SW
1517 if (nblock == block)
1518 return 0;
ff7f4cb4 1519fail:
5b924ae2
SW
1520 ret = gfs2_rbm_from_block(rbm, nblock);
1521 if (ret < 0)
1522 return ret;
1523 return 1;
1524}
1525
1526/**
1527 * gfs2_rbm_find - Look for blocks of a particular state
1528 * @rbm: Value/result starting position and final position
1529 * @state: The state which we want to find
ff7f4cb4 1530 * @minext: The requested extent length (0 for a single block)
5b924ae2
SW
1531 * @ip: If set, check for reservations
1532 * @nowrap: Stop looking at the end of the rgrp, rather than wrapping
1533 * around until we've reached the starting point.
1534 *
1535 * Side effects:
1536 * - If looking for free blocks, we set GBF_FULL on each bitmap which
1537 * has no free blocks in it.
1538 *
1539 * Returns: 0 on success, -ENOSPC if there is no block of the requested state
1540 */
1541
ff7f4cb4 1542static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 minext,
5b924ae2
SW
1543 const struct gfs2_inode *ip, bool nowrap)
1544{
1545 struct buffer_head *bh;
1546 struct gfs2_bitmap *initial_bi;
1547 u32 initial_offset;
1548 u32 offset;
1549 u8 *buffer;
1550 int index;
1551 int n = 0;
1552 int iters = rbm->rgd->rd_length;
1553 int ret;
1554
1555 /* If we are not starting at the beginning of a bitmap, then we
1556 * need to add one to the bitmap count to ensure that we search
1557 * the starting bitmap twice.
1558 */
1559 if (rbm->offset != 0)
1560 iters++;
1561
1562 while(1) {
1563 if (test_bit(GBF_FULL, &rbm->bi->bi_flags) &&
1564 (state == GFS2_BLKST_FREE))
1565 goto next_bitmap;
1566
1567 bh = rbm->bi->bi_bh;
1568 buffer = bh->b_data + rbm->bi->bi_offset;
1569 WARN_ON(!buffer_uptodate(bh));
1570 if (state != GFS2_BLKST_UNLINKED && rbm->bi->bi_clone)
1571 buffer = rbm->bi->bi_clone + rbm->bi->bi_offset;
5b924ae2
SW
1572 initial_offset = rbm->offset;
1573 offset = gfs2_bitfit(buffer, rbm->bi->bi_len, rbm->offset, state);
1574 if (offset == BFITNOENT)
1575 goto bitmap_full;
1576 rbm->offset = offset;
1577 if (ip == NULL)
1578 return 0;
1579
1580 initial_bi = rbm->bi;
ff7f4cb4 1581 ret = gfs2_reservation_check_and_update(rbm, ip, minext);
5b924ae2
SW
1582 if (ret == 0)
1583 return 0;
1584 if (ret > 0) {
1585 n += (rbm->bi - initial_bi);
8d8b752a 1586 goto next_iter;
5b924ae2 1587 }
5d50d532
SW
1588 if (ret == -E2BIG) {
1589 index = 0;
1590 rbm->offset = 0;
1591 n += (rbm->bi - initial_bi);
1592 goto res_covered_end_of_rgrp;
1593 }
5b924ae2
SW
1594 return ret;
1595
1596bitmap_full: /* Mark bitmap as full and fall through */
1597 if ((state == GFS2_BLKST_FREE) && initial_offset == 0)
1598 set_bit(GBF_FULL, &rbm->bi->bi_flags);
1599
1600next_bitmap: /* Find next bitmap in the rgrp */
1601 rbm->offset = 0;
1602 index = rbm->bi - rbm->rgd->rd_bits;
1603 index++;
1604 if (index == rbm->rgd->rd_length)
1605 index = 0;
5d50d532 1606res_covered_end_of_rgrp:
5b924ae2
SW
1607 rbm->bi = &rbm->rgd->rd_bits[index];
1608 if ((index == 0) && nowrap)
1609 break;
1610 n++;
8d8b752a 1611next_iter:
5b924ae2
SW
1612 if (n >= iters)
1613 break;
1614 }
1615
1616 return -ENOSPC;
1617}
1618
c8cdf479
SW
1619/**
1620 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
1621 * @rgd: The rgrp
886b1416
BP
1622 * @last_unlinked: block address of the last dinode we unlinked
1623 * @skip: block address we should explicitly not unlink
c8cdf479 1624 *
1a0eae88
BP
1625 * Returns: 0 if no error
1626 * The inode, if one has been found, in inode.
c8cdf479
SW
1627 */
1628
044b9414 1629static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
c8cdf479 1630{
5b924ae2 1631 u64 block;
5f3eae75 1632 struct gfs2_sbd *sdp = rgd->rd_sbd;
044b9414
SW
1633 struct gfs2_glock *gl;
1634 struct gfs2_inode *ip;
1635 int error;
1636 int found = 0;
5b924ae2 1637 struct gfs2_rbm rbm = { .rgd = rgd, .bi = rgd->rd_bits, .offset = 0 };
c8cdf479 1638
5b924ae2 1639 while (1) {
5f3eae75 1640 down_write(&sdp->sd_log_flush_lock);
ff7f4cb4 1641 error = gfs2_rbm_find(&rbm, GFS2_BLKST_UNLINKED, 0, NULL, true);
5f3eae75 1642 up_write(&sdp->sd_log_flush_lock);
5b924ae2
SW
1643 if (error == -ENOSPC)
1644 break;
1645 if (WARN_ON_ONCE(error))
24c73873 1646 break;
b3e47ca0 1647
5b924ae2
SW
1648 block = gfs2_rbm_to_block(&rbm);
1649 if (gfs2_rbm_from_block(&rbm, block + 1))
1650 break;
1651 if (*last_unlinked != NO_BLOCK && block <= *last_unlinked)
c8cdf479 1652 continue;
5b924ae2 1653 if (block == skip)
1e19a195 1654 continue;
5b924ae2 1655 *last_unlinked = block;
044b9414 1656
5b924ae2 1657 error = gfs2_glock_get(sdp, block, &gfs2_inode_glops, CREATE, &gl);
044b9414
SW
1658 if (error)
1659 continue;
1660
1661 /* If the inode is already in cache, we can ignore it here
1662 * because the existing inode disposal code will deal with
1663 * it when all refs have gone away. Accessing gl_object like
1664 * this is not safe in general. Here it is ok because we do
1665 * not dereference the pointer, and we only need an approx
1666 * answer to whether it is NULL or not.
1667 */
1668 ip = gl->gl_object;
1669
1670 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
1671 gfs2_glock_put(gl);
1672 else
1673 found++;
1674
1675 /* Limit reclaim to sensible number of tasks */
44ad37d6 1676 if (found > NR_CPUS)
044b9414 1677 return;
c8cdf479
SW
1678 }
1679
1680 rgd->rd_flags &= ~GFS2_RDF_CHECK;
044b9414 1681 return;
c8cdf479
SW
1682}
1683
c743ffd0
SW
1684static bool gfs2_select_rgrp(struct gfs2_rgrpd **pos, const struct gfs2_rgrpd *begin)
1685{
1686 struct gfs2_rgrpd *rgd = *pos;
1687
1688 rgd = gfs2_rgrpd_get_next(rgd);
1689 if (rgd == NULL)
1690 rgd = gfs2_rgrpd_get_next(NULL);
1691 *pos = rgd;
1692 if (rgd != begin) /* If we didn't wrap */
1693 return true;
1694 return false;
1695}
1696
b3b94faa 1697/**
666d1d8a 1698 * gfs2_inplace_reserve - Reserve space in the filesystem
b3b94faa 1699 * @ip: the inode to reserve space for
666d1d8a 1700 * @requested: the number of blocks to be reserved
b3b94faa
DT
1701 *
1702 * Returns: errno
1703 */
1704
666d1d8a 1705int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
b3b94faa 1706{
feaa7bba 1707 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
8e2e0047 1708 struct gfs2_rgrpd *begin = NULL;
564e12b1 1709 struct gfs2_blkreserv *rs = ip->i_res;
666d1d8a
BP
1710 int error = 0, rg_locked, flags = LM_FLAG_TRY;
1711 u64 last_unlinked = NO_BLOCK;
7c9ca621 1712 int loops = 0;
b3b94faa 1713
90306c41
BM
1714 if (sdp->sd_args.ar_rgrplvb)
1715 flags |= GL_SKIP;
c743ffd0
SW
1716 if (gfs2_assert_warn(sdp, requested))
1717 return -EINVAL;
8e2e0047 1718 if (gfs2_rs_active(rs)) {
4a993fb1 1719 begin = rs->rs_rbm.rgd;
8e2e0047
BP
1720 flags = 0; /* Yoda: Do or do not. There is no try */
1721 } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) {
4a993fb1 1722 rs->rs_rbm.rgd = begin = ip->i_rgd;
8e2e0047 1723 } else {
4a993fb1 1724 rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
8e2e0047 1725 }
4a993fb1 1726 if (rs->rs_rbm.rgd == NULL)
7c9ca621
BP
1727 return -EBADSLT;
1728
1729 while (loops < 3) {
c743ffd0
SW
1730 rg_locked = 1;
1731
1732 if (!gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) {
1733 rg_locked = 0;
4a993fb1 1734 error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl,
8e2e0047
BP
1735 LM_ST_EXCLUSIVE, flags,
1736 &rs->rs_rgd_gh);
c743ffd0
SW
1737 if (error == GLR_TRYFAILED)
1738 goto next_rgrp;
1739 if (unlikely(error))
1740 return error;
1741 if (sdp->sd_args.ar_rgrplvb) {
4a993fb1 1742 error = update_rgrp_lvb(rs->rs_rbm.rgd);
c743ffd0 1743 if (unlikely(error)) {
90306c41
BM
1744 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1745 return error;
1746 }
1747 }
292c8c14 1748 }
666d1d8a 1749
c743ffd0
SW
1750 /* Skip unuseable resource groups */
1751 if (rs->rs_rbm.rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
1752 goto skip_rgrp;
1753
1754 if (sdp->sd_args.ar_rgrplvb)
1755 gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
1756
1757 /* Get a reservation if we don't already have one */
1758 if (!gfs2_rs_active(rs))
1759 rg_mblk_search(rs->rs_rbm.rgd, ip, requested);
1760
1761 /* Skip rgrps when we can't get a reservation on first pass */
1762 if (!gfs2_rs_active(rs) && (loops < 1))
1763 goto check_rgrp;
1764
1765 /* If rgrp has enough free space, use it */
1766 if (rs->rs_rbm.rgd->rd_free_clone >= requested) {
1767 ip->i_rgd = rs->rs_rbm.rgd;
1768 return 0;
b3b94faa 1769 }
c743ffd0
SW
1770
1771 /* Drop reservation, if we couldn't use reserved rgrp */
1772 if (gfs2_rs_active(rs))
1773 gfs2_rs_deltree(ip, rs);
1774check_rgrp:
1775 /* Check for unlinked inodes which can be reclaimed */
1776 if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK)
1777 try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked,
1778 ip->i_no_addr);
1779skip_rgrp:
1780 /* Unlock rgrp if required */
1781 if (!rg_locked)
1782 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1783next_rgrp:
1784 /* Find the next rgrp, and continue looking */
1785 if (gfs2_select_rgrp(&rs->rs_rbm.rgd, begin))
1786 continue;
1787
1788 /* If we've scanned all the rgrps, but found no free blocks
1789 * then this checks for some less likely conditions before
1790 * trying again.
1791 */
1792 flags &= ~LM_FLAG_TRY;
1793 loops++;
1794 /* Check that fs hasn't grown if writing to rindex */
1795 if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
1796 error = gfs2_ri_update(ip);
1797 if (error)
1798 return error;
1799 }
1800 /* Flushing the log may release space */
1801 if (loops == 2)
1802 gfs2_log_flush(sdp, NULL);
b3b94faa 1803 }
b3b94faa 1804
c743ffd0 1805 return -ENOSPC;
b3b94faa
DT
1806}
1807
1808/**
1809 * gfs2_inplace_release - release an inplace reservation
1810 * @ip: the inode the reservation was taken out on
1811 *
1812 * Release a reservation made by gfs2_inplace_reserve().
1813 */
1814
1815void gfs2_inplace_release(struct gfs2_inode *ip)
1816{
564e12b1 1817 struct gfs2_blkreserv *rs = ip->i_res;
b3b94faa 1818
564e12b1
BP
1819 if (rs->rs_rgd_gh.gh_gl)
1820 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
b3b94faa
DT
1821}
1822
1823/**
1824 * gfs2_get_block_type - Check a block in a RG is of given type
1825 * @rgd: the resource group holding the block
1826 * @block: the block number
1827 *
1828 * Returns: The block type (GFS2_BLKST_*)
1829 */
1830
acf7e244 1831static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
b3b94faa 1832{
3983903a
SW
1833 struct gfs2_rbm rbm = { .rgd = rgd, };
1834 int ret;
b3b94faa 1835
3983903a
SW
1836 ret = gfs2_rbm_from_block(&rbm, block);
1837 WARN_ON_ONCE(ret != 0);
b3b94faa 1838
c04a2ef3 1839 return gfs2_testbit(&rbm);
b3b94faa
DT
1840}
1841
60a0b8f9 1842
b3e47ca0
BP
1843/**
1844 * gfs2_alloc_extent - allocate an extent from a given bitmap
4a993fb1 1845 * @rbm: the resource group information
b3e47ca0 1846 * @dinode: TRUE if the first block we allocate is for a dinode
c04a2ef3 1847 * @n: The extent length (value/result)
b3e47ca0 1848 *
c04a2ef3 1849 * Add the bitmap buffer to the transaction.
b3e47ca0 1850 * Set the found bits to @new_state to change block's allocation state.
b3e47ca0 1851 */
c04a2ef3 1852static void gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
4a993fb1 1853 unsigned int *n)
b3e47ca0 1854{
c04a2ef3 1855 struct gfs2_rbm pos = { .rgd = rbm->rgd, };
b3e47ca0 1856 const unsigned int elen = *n;
c04a2ef3
SW
1857 u64 block;
1858 int ret;
b3e47ca0 1859
c04a2ef3
SW
1860 *n = 1;
1861 block = gfs2_rbm_to_block(rbm);
1862 gfs2_trans_add_bh(rbm->rgd->rd_gl, rbm->bi->bi_bh, 1);
3e6339dd 1863 gfs2_setbit(rbm, true, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
c04a2ef3 1864 block++;
60a0b8f9 1865 while (*n < elen) {
c04a2ef3 1866 ret = gfs2_rbm_from_block(&pos, block);
0688a5ec 1867 if (ret || gfs2_testbit(&pos) != GFS2_BLKST_FREE)
60a0b8f9 1868 break;
c04a2ef3 1869 gfs2_trans_add_bh(pos.rgd->rd_gl, pos.bi->bi_bh, 1);
3e6339dd 1870 gfs2_setbit(&pos, true, GFS2_BLKST_USED);
60a0b8f9 1871 (*n)++;
c04a2ef3 1872 block++;
c8cdf479 1873 }
b3b94faa
DT
1874}
1875
1876/**
1877 * rgblk_free - Change alloc state of given block(s)
1878 * @sdp: the filesystem
1879 * @bstart: the start of a run of blocks to free
1880 * @blen: the length of the block run (all must lie within ONE RG!)
1881 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1882 *
1883 * Returns: Resource group containing the block(s)
1884 */
1885
cd915493
SW
1886static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1887 u32 blen, unsigned char new_state)
b3b94faa 1888{
3b1d0b9d 1889 struct gfs2_rbm rbm;
b3b94faa 1890
3b1d0b9d
SW
1891 rbm.rgd = gfs2_blk2rgrpd(sdp, bstart, 1);
1892 if (!rbm.rgd) {
b3b94faa 1893 if (gfs2_consist(sdp))
382066da 1894 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
b3b94faa
DT
1895 return NULL;
1896 }
1897
b3b94faa 1898 while (blen--) {
3b1d0b9d
SW
1899 gfs2_rbm_from_block(&rbm, bstart);
1900 bstart++;
1901 if (!rbm.bi->bi_clone) {
1902 rbm.bi->bi_clone = kmalloc(rbm.bi->bi_bh->b_size,
1903 GFP_NOFS | __GFP_NOFAIL);
1904 memcpy(rbm.bi->bi_clone + rbm.bi->bi_offset,
1905 rbm.bi->bi_bh->b_data + rbm.bi->bi_offset,
1906 rbm.bi->bi_len);
b3b94faa 1907 }
3b1d0b9d 1908 gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.bi->bi_bh, 1);
3e6339dd 1909 gfs2_setbit(&rbm, false, new_state);
b3b94faa
DT
1910 }
1911
3b1d0b9d 1912 return rbm.rgd;
b3b94faa
DT
1913}
1914
1915/**
09010978
SW
1916 * gfs2_rgrp_dump - print out an rgrp
1917 * @seq: The iterator
1918 * @gl: The glock in question
1919 *
1920 */
1921
1922int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
1923{
8e2e0047
BP
1924 struct gfs2_rgrpd *rgd = gl->gl_object;
1925 struct gfs2_blkreserv *trs;
1926 const struct rb_node *n;
1927
09010978
SW
1928 if (rgd == NULL)
1929 return 0;
8e2e0047 1930 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u r:%u\n",
09010978 1931 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
8e2e0047
BP
1932 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
1933 rgd->rd_reserved);
1934 spin_lock(&rgd->rd_rsspin);
1935 for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
1936 trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1937 dump_rs(seq, trs);
1938 }
1939 spin_unlock(&rgd->rd_rsspin);
09010978
SW
1940 return 0;
1941}
1942
6050b9c7
SW
1943static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
1944{
1945 struct gfs2_sbd *sdp = rgd->rd_sbd;
1946 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
86d00636 1947 (unsigned long long)rgd->rd_addr);
6050b9c7
SW
1948 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
1949 gfs2_rgrp_dump(NULL, rgd->rd_gl);
1950 rgd->rd_flags |= GFS2_RDF_ERROR;
1951}
1952
8e2e0047 1953/**
5b924ae2
SW
1954 * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
1955 * @ip: The inode we have just allocated blocks for
1956 * @rbm: The start of the allocated blocks
1957 * @len: The extent length
8e2e0047 1958 *
5b924ae2
SW
1959 * Adjusts a reservation after an allocation has taken place. If the
1960 * reservation does not match the allocation, or if it is now empty
1961 * then it is removed.
8e2e0047 1962 */
5b924ae2
SW
1963
1964static void gfs2_adjust_reservation(struct gfs2_inode *ip,
1965 const struct gfs2_rbm *rbm, unsigned len)
8e2e0047
BP
1966{
1967 struct gfs2_blkreserv *rs = ip->i_res;
5b924ae2
SW
1968 struct gfs2_rgrpd *rgd = rbm->rgd;
1969 unsigned rlen;
1970 u64 block;
1971 int ret;
8e2e0047 1972
5b924ae2
SW
1973 spin_lock(&rgd->rd_rsspin);
1974 if (gfs2_rs_active(rs)) {
1975 if (gfs2_rbm_eq(&rs->rs_rbm, rbm)) {
1976 block = gfs2_rbm_to_block(rbm);
1977 ret = gfs2_rbm_from_block(&rs->rs_rbm, block + len);
1978 rlen = min(rs->rs_free, len);
1979 rs->rs_free -= rlen;
1980 rgd->rd_reserved -= rlen;
9e733d39 1981 trace_gfs2_rs(rs, TRACE_RS_CLAIM);
5b924ae2
SW
1982 if (rs->rs_free && !ret)
1983 goto out;
1984 }
1985 __rs_deltree(ip, rs);
8e2e0047 1986 }
5b924ae2
SW
1987out:
1988 spin_unlock(&rgd->rd_rsspin);
8e2e0047
BP
1989}
1990
09010978 1991/**
6e87ed0f 1992 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
1639431a 1993 * @ip: the inode to allocate the block for
09010978 1994 * @bn: Used to return the starting block number
8e2e0047 1995 * @nblocks: requested number of blocks/extent length (value/result)
6e87ed0f 1996 * @dinode: 1 if we're allocating a dinode block, else 0
3c5d785a 1997 * @generation: the generation number of the inode
b3b94faa 1998 *
09010978 1999 * Returns: 0 or error
b3b94faa
DT
2000 */
2001
6a8099ed 2002int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
6e87ed0f 2003 bool dinode, u64 *generation)
b3b94faa 2004{
feaa7bba 2005 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
d9ba7615 2006 struct buffer_head *dibh;
4a993fb1 2007 struct gfs2_rbm rbm = { .rgd = ip->i_rgd, };
6a8099ed 2008 unsigned int ndata;
5b924ae2 2009 u64 goal;
3c5d785a 2010 u64 block; /* block, within the file system scope */
d9ba7615 2011 int error;
b3b94faa 2012
5b924ae2
SW
2013 if (gfs2_rs_active(ip->i_res))
2014 goal = gfs2_rbm_to_block(&ip->i_res->rs_rbm);
2015 else if (!dinode && rgrp_contains_block(rbm.rgd, ip->i_goal))
2016 goal = ip->i_goal;
62e252ee 2017 else
5b924ae2 2018 goal = rbm.rgd->rd_last_alloc + rbm.rgd->rd_data0;
62e252ee 2019
5b924ae2 2020 gfs2_rbm_from_block(&rbm, goal);
ff7f4cb4 2021 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, 0, ip, false);
62e252ee 2022
137834a6
SW
2023 if (error == -ENOSPC) {
2024 gfs2_rbm_from_block(&rbm, goal);
ff7f4cb4 2025 error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, 0, NULL, false);
137834a6
SW
2026 }
2027
62e252ee 2028 /* Since all blocks are reserved in advance, this shouldn't happen */
5b924ae2 2029 if (error) {
9e733d39
SW
2030 fs_warn(sdp, "inum=%llu error=%d, nblocks=%u, full=%d\n",
2031 (unsigned long long)ip->i_no_addr, error, *nblocks,
5b924ae2 2032 test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags));
62e252ee 2033 goto rgrp_error;
8e2e0047 2034 }
62e252ee 2035
c04a2ef3
SW
2036 gfs2_alloc_extent(&rbm, dinode, nblocks);
2037 block = gfs2_rbm_to_block(&rbm);
c743ffd0 2038 rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0;
5b924ae2
SW
2039 if (gfs2_rs_active(ip->i_res))
2040 gfs2_adjust_reservation(ip, &rbm, *nblocks);
6a8099ed
SW
2041 ndata = *nblocks;
2042 if (dinode)
2043 ndata--;
b3e47ca0 2044
3c5d785a 2045 if (!dinode) {
6a8099ed 2046 ip->i_goal = block + ndata - 1;
3c5d785a
BP
2047 error = gfs2_meta_inode_buffer(ip, &dibh);
2048 if (error == 0) {
2049 struct gfs2_dinode *di =
2050 (struct gfs2_dinode *)dibh->b_data;
2051 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
2052 di->di_goal_meta = di->di_goal_data =
2053 cpu_to_be64(ip->i_goal);
2054 brelse(dibh);
2055 }
d9ba7615 2056 }
4a993fb1 2057 if (rbm.rgd->rd_free < *nblocks) {
8e2e0047 2058 printk(KERN_WARNING "nblocks=%u\n", *nblocks);
09010978 2059 goto rgrp_error;
8e2e0047 2060 }
09010978 2061
4a993fb1 2062 rbm.rgd->rd_free -= *nblocks;
3c5d785a 2063 if (dinode) {
4a993fb1
SW
2064 rbm.rgd->rd_dinodes++;
2065 *generation = rbm.rgd->rd_igeneration++;
3c5d785a 2066 if (*generation == 0)
4a993fb1 2067 *generation = rbm.rgd->rd_igeneration++;
3c5d785a 2068 }
b3b94faa 2069
4a993fb1
SW
2070 gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh, 1);
2071 gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
2072 gfs2_rgrp_ondisk2lvb(rbm.rgd->rd_rgl, rbm.rgd->rd_bits[0].bi_bh->b_data);
b3b94faa 2073
6a8099ed 2074 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
3c5d785a
BP
2075 if (dinode)
2076 gfs2_trans_add_unrevoke(sdp, block, 1);
6a8099ed
SW
2077
2078 /*
2079 * This needs reviewing to see why we cannot do the quota change
2080 * at this point in the dinode case.
2081 */
2082 if (ndata)
2083 gfs2_quota_change(ip, ndata, ip->i_inode.i_uid,
3c5d785a 2084 ip->i_inode.i_gid);
b3b94faa 2085
4a993fb1
SW
2086 rbm.rgd->rd_free_clone -= *nblocks;
2087 trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
6e87ed0f 2088 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
6050b9c7
SW
2089 *bn = block;
2090 return 0;
2091
2092rgrp_error:
4a993fb1 2093 gfs2_rgrp_error(rbm.rgd);
6050b9c7 2094 return -EIO;
b3b94faa
DT
2095}
2096
2097/**
46fcb2ed 2098 * __gfs2_free_blocks - free a contiguous run of block(s)
b3b94faa
DT
2099 * @ip: the inode these blocks are being freed from
2100 * @bstart: first block of a run of contiguous blocks
2101 * @blen: the length of the block run
46fcb2ed 2102 * @meta: 1 if the blocks represent metadata
b3b94faa
DT
2103 *
2104 */
2105
46fcb2ed 2106void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
b3b94faa 2107{
feaa7bba 2108 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
2109 struct gfs2_rgrpd *rgd;
2110
2111 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
2112 if (!rgd)
2113 return;
41db1ab9 2114 trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
cfc8b549 2115 rgd->rd_free += blen;
66fc061b 2116 rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
d4e9c4c3 2117 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 2118 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
90306c41 2119 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
b3b94faa 2120
6d3117b4 2121 /* Directories keep their data in the metadata address space */
46fcb2ed 2122 if (meta || ip->i_depth)
6d3117b4 2123 gfs2_meta_wipe(ip, bstart, blen);
4c16c36a 2124}
b3b94faa 2125
4c16c36a
BP
2126/**
2127 * gfs2_free_meta - free a contiguous run of data block(s)
2128 * @ip: the inode these blocks are being freed from
2129 * @bstart: first block of a run of contiguous blocks
2130 * @blen: the length of the block run
2131 *
2132 */
2133
2134void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
2135{
2136 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
2137
46fcb2ed 2138 __gfs2_free_blocks(ip, bstart, blen, 1);
b3b94faa 2139 gfs2_statfs_change(sdp, 0, +blen, 0);
2933f925 2140 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
b3b94faa
DT
2141}
2142
feaa7bba
SW
2143void gfs2_unlink_di(struct inode *inode)
2144{
2145 struct gfs2_inode *ip = GFS2_I(inode);
2146 struct gfs2_sbd *sdp = GFS2_SB(inode);
2147 struct gfs2_rgrpd *rgd;
dbb7cae2 2148 u64 blkno = ip->i_no_addr;
feaa7bba
SW
2149
2150 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
2151 if (!rgd)
2152 return;
41db1ab9 2153 trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
feaa7bba 2154 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 2155 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
90306c41
BM
2156 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2157 update_rgrp_lvb_unlinked(rgd, 1);
feaa7bba
SW
2158}
2159
cd915493 2160static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
b3b94faa
DT
2161{
2162 struct gfs2_sbd *sdp = rgd->rd_sbd;
2163 struct gfs2_rgrpd *tmp_rgd;
2164
2165 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
2166 if (!tmp_rgd)
2167 return;
2168 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
2169
73f74948 2170 if (!rgd->rd_dinodes)
b3b94faa 2171 gfs2_consist_rgrpd(rgd);
73f74948 2172 rgd->rd_dinodes--;
cfc8b549 2173 rgd->rd_free++;
b3b94faa 2174
d4e9c4c3 2175 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 2176 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
90306c41
BM
2177 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2178 update_rgrp_lvb_unlinked(rgd, -1);
b3b94faa
DT
2179
2180 gfs2_statfs_change(sdp, 0, +1, -1);
b3b94faa
DT
2181}
2182
b3b94faa
DT
2183
2184void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
2185{
dbb7cae2 2186 gfs2_free_uninit_di(rgd, ip->i_no_addr);
41db1ab9 2187 trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
2933f925 2188 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
dbb7cae2 2189 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
b3b94faa
DT
2190}
2191
acf7e244
SW
2192/**
2193 * gfs2_check_blk_type - Check the type of a block
2194 * @sdp: The superblock
2195 * @no_addr: The block number to check
2196 * @type: The block type we are looking for
2197 *
2198 * Returns: 0 if the block type matches the expected type
2199 * -ESTALE if it doesn't match
2200 * or -ve errno if something went wrong while checking
2201 */
2202
2203int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
2204{
2205 struct gfs2_rgrpd *rgd;
8339ee54 2206 struct gfs2_holder rgd_gh;
58884c4d 2207 int error = -EINVAL;
acf7e244 2208
66fc061b 2209 rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
acf7e244 2210 if (!rgd)
8339ee54 2211 goto fail;
acf7e244
SW
2212
2213 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
2214 if (error)
8339ee54 2215 goto fail;
acf7e244
SW
2216
2217 if (gfs2_get_block_type(rgd, no_addr) != type)
2218 error = -ESTALE;
2219
2220 gfs2_glock_dq_uninit(&rgd_gh);
acf7e244
SW
2221fail:
2222 return error;
2223}
2224
b3b94faa
DT
2225/**
2226 * gfs2_rlist_add - add a RG to a list of RGs
70b0c365 2227 * @ip: the inode
b3b94faa
DT
2228 * @rlist: the list of resource groups
2229 * @block: the block
2230 *
2231 * Figure out what RG a block belongs to and add that RG to the list
2232 *
2233 * FIXME: Don't use NOFAIL
2234 *
2235 */
2236
70b0c365 2237void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
cd915493 2238 u64 block)
b3b94faa 2239{
70b0c365 2240 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
2241 struct gfs2_rgrpd *rgd;
2242 struct gfs2_rgrpd **tmp;
2243 unsigned int new_space;
2244 unsigned int x;
2245
2246 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
2247 return;
2248
70b0c365
SW
2249 if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block))
2250 rgd = ip->i_rgd;
2251 else
66fc061b 2252 rgd = gfs2_blk2rgrpd(sdp, block, 1);
b3b94faa 2253 if (!rgd) {
70b0c365 2254 fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block);
b3b94faa
DT
2255 return;
2256 }
70b0c365 2257 ip->i_rgd = rgd;
b3b94faa
DT
2258
2259 for (x = 0; x < rlist->rl_rgrps; x++)
2260 if (rlist->rl_rgd[x] == rgd)
2261 return;
2262
2263 if (rlist->rl_rgrps == rlist->rl_space) {
2264 new_space = rlist->rl_space + 10;
2265
2266 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
dd894be8 2267 GFP_NOFS | __GFP_NOFAIL);
b3b94faa
DT
2268
2269 if (rlist->rl_rgd) {
2270 memcpy(tmp, rlist->rl_rgd,
2271 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
2272 kfree(rlist->rl_rgd);
2273 }
2274
2275 rlist->rl_space = new_space;
2276 rlist->rl_rgd = tmp;
2277 }
2278
2279 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
2280}
2281
2282/**
2283 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
2284 * and initialize an array of glock holders for them
2285 * @rlist: the list of resource groups
2286 * @state: the lock state to acquire the RG lock in
b3b94faa
DT
2287 *
2288 * FIXME: Don't use NOFAIL
2289 *
2290 */
2291
fe6c991c 2292void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
b3b94faa
DT
2293{
2294 unsigned int x;
2295
2296 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
dd894be8 2297 GFP_NOFS | __GFP_NOFAIL);
b3b94faa
DT
2298 for (x = 0; x < rlist->rl_rgrps; x++)
2299 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
fe6c991c 2300 state, 0,
b3b94faa
DT
2301 &rlist->rl_ghs[x]);
2302}
2303
2304/**
2305 * gfs2_rlist_free - free a resource group list
2306 * @list: the list of resource groups
2307 *
2308 */
2309
2310void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
2311{
2312 unsigned int x;
2313
2314 kfree(rlist->rl_rgd);
2315
2316 if (rlist->rl_ghs) {
2317 for (x = 0; x < rlist->rl_rgrps; x++)
2318 gfs2_holder_uninit(&rlist->rl_ghs[x]);
2319 kfree(rlist->rl_ghs);
8e2e0047 2320 rlist->rl_ghs = NULL;
b3b94faa
DT
2321 }
2322}
2323
This page took 0.62292 seconds and 5 git commands to generate.