GFS2: Fix deadlock on journal flush
[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>
b3b94faa
DT
17
18#include "gfs2.h"
5c676f6d 19#include "incore.h"
b3b94faa
DT
20#include "glock.h"
21#include "glops.h"
b3b94faa
DT
22#include "lops.h"
23#include "meta_io.h"
24#include "quota.h"
25#include "rgrp.h"
26#include "super.h"
27#include "trans.h"
5c676f6d 28#include "util.h"
172e045a 29#include "log.h"
c8cdf479 30#include "inode.h"
51ff87bd 31#include "ops_address.h"
b3b94faa 32
2c1e52aa 33#define BFITNOENT ((u32)~0)
6760bdcd 34#define NO_BLOCK ((u64)~0)
88c8ab1f 35
1f466a47
BP
36#if BITS_PER_LONG == 32
37#define LBITMASK (0x55555555UL)
38#define LBITSKIP55 (0x55555555UL)
39#define LBITSKIP00 (0x00000000UL)
40#else
41#define LBITMASK (0x5555555555555555UL)
42#define LBITSKIP55 (0x5555555555555555UL)
43#define LBITSKIP00 (0x0000000000000000UL)
44#endif
45
88c8ab1f
SW
46/*
47 * These routines are used by the resource group routines (rgrp.c)
48 * to keep track of block allocation. Each block is represented by two
feaa7bba
SW
49 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
50 *
51 * 0 = Free
52 * 1 = Used (not metadata)
53 * 2 = Unlinked (still in use) inode
54 * 3 = Used (metadata)
88c8ab1f
SW
55 */
56
57static const char valid_change[16] = {
58 /* current */
feaa7bba 59 /* n */ 0, 1, 1, 1,
88c8ab1f 60 /* e */ 1, 0, 0, 0,
feaa7bba 61 /* w */ 0, 0, 0, 1,
88c8ab1f
SW
62 1, 0, 0, 0
63};
64
c8cdf479 65static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
b45e41d7
SW
66 unsigned char old_state, unsigned char new_state,
67 unsigned int *n);
c8cdf479 68
88c8ab1f
SW
69/**
70 * gfs2_setbit - Set a bit in the bitmaps
71 * @buffer: the buffer that holds the bitmaps
72 * @buflen: the length (in bytes) of the buffer
73 * @block: the block to set
74 * @new_state: the new state of the block
75 *
76 */
77
b45e41d7
SW
78static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
79 unsigned char *buf2, unsigned int offset,
80 unsigned int buflen, u32 block,
81 unsigned char new_state)
88c8ab1f 82{
b45e41d7
SW
83 unsigned char *byte1, *byte2, *end, cur_state;
84 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
88c8ab1f 85
b45e41d7
SW
86 byte1 = buf1 + offset + (block / GFS2_NBBY);
87 end = buf1 + 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])) {
88c8ab1f 94 gfs2_consist_rgrpd(rgd);
b45e41d7
SW
95 return;
96 }
97 *byte1 ^= (cur_state ^ new_state) << bit;
98
99 if (buf2) {
100 byte2 = buf2 + offset + (block / GFS2_NBBY);
101 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
102 *byte2 ^= (cur_state ^ new_state) << bit;
103 }
88c8ab1f
SW
104}
105
106/**
107 * gfs2_testbit - test a bit in the bitmaps
108 * @buffer: the buffer that holds the bitmaps
109 * @buflen: the length (in bytes) of the buffer
110 * @block: the block to read
111 *
112 */
113
b45e41d7
SW
114static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
115 const unsigned char *buffer,
116 unsigned int buflen, u32 block)
88c8ab1f 117{
b45e41d7
SW
118 const unsigned char *byte, *end;
119 unsigned char cur_state;
88c8ab1f
SW
120 unsigned int bit;
121
122 byte = buffer + (block / GFS2_NBBY);
123 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
124 end = buffer + buflen;
125
126 gfs2_assert(rgd->rd_sbd, byte < end);
127
128 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
129
130 return cur_state;
131}
132
133/**
134 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
135 * a block in a given allocation state.
136 * @buffer: the buffer that holds the bitmaps
137 * @buflen: the length (in bytes) of the buffer
138 * @goal: start search at this block's bit-pair (within @buffer)
6760bdcd 139 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for.
88c8ab1f
SW
140 *
141 * Scope of @goal and returned block number is only within this bitmap buffer,
142 * not entire rgrp or filesystem. @buffer will be offset from the actual
143 * beginning of a bitmap block buffer, skipping any header structures.
144 *
145 * Return: the block number (bitmap buffer scope) that was found
146 */
147
110acf38
SW
148static u32 gfs2_bitfit(const u8 *buffer, unsigned int buflen, u32 goal,
149 u8 old_state)
88c8ab1f 150{
1f466a47
BP
151 const u8 *byte, *start, *end;
152 int bit, startbit;
153 u32 g1, g2, misaligned;
154 unsigned long *plong;
155 unsigned long lskipval;
156
157 lskipval = (old_state & GFS2_BLKST_USED) ? LBITSKIP00 : LBITSKIP55;
158 g1 = (goal / GFS2_NBBY);
159 start = buffer + g1;
160 byte = start;
161 end = buffer + buflen;
162 g2 = ALIGN(g1, sizeof(unsigned long));
163 plong = (unsigned long *)(buffer + g2);
164 startbit = bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
165 misaligned = g2 - g1;
166 if (!misaligned)
167 goto ulong_aligned;
168/* parse the bitmap a byte at a time */
169misaligned:
170 while (byte < end) {
171 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state) {
172 return goal +
173 (((byte - start) * GFS2_NBBY) +
174 ((bit - startbit) >> 1));
88c8ab1f 175 }
88c8ab1f 176 bit += GFS2_BIT_SIZE;
1f466a47 177 if (bit >= GFS2_NBBY * GFS2_BIT_SIZE) {
88c8ab1f
SW
178 bit = 0;
179 byte++;
1f466a47
BP
180 misaligned--;
181 if (!misaligned) {
182 plong = (unsigned long *)byte;
183 goto ulong_aligned;
184 }
88c8ab1f 185 }
88c8ab1f 186 }
1f466a47 187 return BFITNOENT;
88c8ab1f 188
1f466a47
BP
189/* parse the bitmap a unsigned long at a time */
190ulong_aligned:
191 /* Stop at "end - 1" or else prefetch can go past the end and segfault.
192 We could "if" it but we'd lose some of the performance gained.
193 This way will only slow down searching the very last 4/8 bytes
194 depending on architecture. I've experimented with several ways
195 of writing this section such as using an else before the goto
196 but this one seems to be the fastest. */
17c15da0 197 while ((unsigned char *)plong < end - sizeof(unsigned long)) {
1f466a47
BP
198 prefetch(plong + 1);
199 if (((*plong) & LBITMASK) != lskipval)
200 break;
201 plong++;
202 }
203 if ((unsigned char *)plong < end) {
204 byte = (const u8 *)plong;
205 misaligned += sizeof(unsigned long) - 1;
206 goto misaligned;
207 }
88c8ab1f
SW
208 return BFITNOENT;
209}
210
211/**
212 * gfs2_bitcount - count the number of bits in a certain state
213 * @buffer: the buffer that holds the bitmaps
214 * @buflen: the length (in bytes) of the buffer
215 * @state: the state of the block we're looking for
216 *
217 * Returns: The number of bits
218 */
219
110acf38
SW
220static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
221 unsigned int buflen, u8 state)
88c8ab1f 222{
110acf38
SW
223 const u8 *byte = buffer;
224 const u8 *end = buffer + buflen;
225 const u8 state1 = state << 2;
226 const u8 state2 = state << 4;
227 const u8 state3 = state << 6;
cd915493 228 u32 count = 0;
88c8ab1f
SW
229
230 for (; byte < end; byte++) {
231 if (((*byte) & 0x03) == state)
232 count++;
233 if (((*byte) & 0x0C) == state1)
234 count++;
235 if (((*byte) & 0x30) == state2)
236 count++;
237 if (((*byte) & 0xC0) == state3)
238 count++;
239 }
240
241 return count;
242}
243
b3b94faa
DT
244/**
245 * gfs2_rgrp_verify - Verify that a resource group is consistent
246 * @sdp: the filesystem
247 * @rgd: the rgrp
248 *
249 */
250
251void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
252{
253 struct gfs2_sbd *sdp = rgd->rd_sbd;
254 struct gfs2_bitmap *bi = NULL;
bb8d8a6f 255 u32 length = rgd->rd_length;
cd915493 256 u32 count[4], tmp;
b3b94faa
DT
257 int buf, x;
258
cd915493 259 memset(count, 0, 4 * sizeof(u32));
b3b94faa
DT
260
261 /* Count # blocks in each of 4 possible allocation states */
262 for (buf = 0; buf < length; buf++) {
263 bi = rgd->rd_bits + buf;
264 for (x = 0; x < 4; x++)
265 count[x] += gfs2_bitcount(rgd,
266 bi->bi_bh->b_data +
267 bi->bi_offset,
268 bi->bi_len, x);
269 }
270
cfc8b549 271 if (count[0] != rgd->rd_free) {
b3b94faa
DT
272 if (gfs2_consist_rgrpd(rgd))
273 fs_err(sdp, "free data mismatch: %u != %u\n",
cfc8b549 274 count[0], rgd->rd_free);
b3b94faa
DT
275 return;
276 }
277
73f74948 278 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
feaa7bba 279 if (count[1] + count[2] != tmp) {
b3b94faa
DT
280 if (gfs2_consist_rgrpd(rgd))
281 fs_err(sdp, "used data mismatch: %u != %u\n",
282 count[1], tmp);
283 return;
284 }
285
73f74948 286 if (count[3] != rgd->rd_dinodes) {
b3b94faa 287 if (gfs2_consist_rgrpd(rgd))
feaa7bba 288 fs_err(sdp, "used metadata mismatch: %u != %u\n",
73f74948 289 count[3], rgd->rd_dinodes);
b3b94faa
DT
290 return;
291 }
292
feaa7bba 293 if (count[2] > count[3]) {
b3b94faa 294 if (gfs2_consist_rgrpd(rgd))
feaa7bba
SW
295 fs_err(sdp, "unlinked inodes > inodes: %u\n",
296 count[2]);
b3b94faa
DT
297 return;
298 }
feaa7bba 299
b3b94faa
DT
300}
301
bb8d8a6f 302static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
b3b94faa 303{
bb8d8a6f
SW
304 u64 first = rgd->rd_data0;
305 u64 last = first + rgd->rd_data;
16910427 306 return first <= block && block < last;
b3b94faa
DT
307}
308
309/**
310 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
311 * @sdp: The GFS2 superblock
312 * @n: The data block number
313 *
314 * Returns: The resource group, or NULL if not found
315 */
316
cd915493 317struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
b3b94faa
DT
318{
319 struct gfs2_rgrpd *rgd;
320
321 spin_lock(&sdp->sd_rindex_spin);
322
323 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
bb8d8a6f 324 if (rgrp_contains_block(rgd, blk)) {
b3b94faa
DT
325 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
326 spin_unlock(&sdp->sd_rindex_spin);
327 return rgd;
328 }
329 }
330
331 spin_unlock(&sdp->sd_rindex_spin);
332
333 return NULL;
334}
335
336/**
337 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
338 * @sdp: The GFS2 superblock
339 *
340 * Returns: The first rgrp in the filesystem
341 */
342
343struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
344{
345 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
346 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
347}
348
349/**
350 * gfs2_rgrpd_get_next - get the next RG
351 * @rgd: A RG
352 *
353 * Returns: The next rgrp
354 */
355
356struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
357{
358 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
359 return NULL;
360 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
361}
362
363static void clear_rgrpdi(struct gfs2_sbd *sdp)
364{
365 struct list_head *head;
366 struct gfs2_rgrpd *rgd;
367 struct gfs2_glock *gl;
368
369 spin_lock(&sdp->sd_rindex_spin);
370 sdp->sd_rindex_forward = NULL;
b3b94faa
DT
371 spin_unlock(&sdp->sd_rindex_spin);
372
373 head = &sdp->sd_rindex_list;
374 while (!list_empty(head)) {
375 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
376 gl = rgd->rd_gl;
377
378 list_del(&rgd->rd_list);
379 list_del(&rgd->rd_list_mru);
380
381 if (gl) {
5c676f6d 382 gl->gl_object = NULL;
b3b94faa
DT
383 gfs2_glock_put(gl);
384 }
385
386 kfree(rgd->rd_bits);
6bdd9be6 387 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
b3b94faa
DT
388 }
389}
390
391void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
392{
f55ab26a 393 mutex_lock(&sdp->sd_rindex_mutex);
b3b94faa 394 clear_rgrpdi(sdp);
f55ab26a 395 mutex_unlock(&sdp->sd_rindex_mutex);
b3b94faa
DT
396}
397
bb8d8a6f
SW
398static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
399{
400 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
401 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
402 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
403 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
404 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
405}
406
b3b94faa
DT
407/**
408 * gfs2_compute_bitstructs - Compute the bitmap sizes
409 * @rgd: The resource group descriptor
410 *
411 * Calculates bitmap descriptors, one for each block that contains bitmap data
412 *
413 * Returns: errno
414 */
415
416static int compute_bitstructs(struct gfs2_rgrpd *rgd)
417{
418 struct gfs2_sbd *sdp = rgd->rd_sbd;
419 struct gfs2_bitmap *bi;
bb8d8a6f 420 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
cd915493 421 u32 bytes_left, bytes;
b3b94faa
DT
422 int x;
423
feaa7bba
SW
424 if (!length)
425 return -EINVAL;
426
dd894be8 427 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
b3b94faa
DT
428 if (!rgd->rd_bits)
429 return -ENOMEM;
430
bb8d8a6f 431 bytes_left = rgd->rd_bitbytes;
b3b94faa
DT
432
433 for (x = 0; x < length; x++) {
434 bi = rgd->rd_bits + x;
435
436 /* small rgrp; bitmap stored completely in header block */
437 if (length == 1) {
438 bytes = bytes_left;
439 bi->bi_offset = sizeof(struct gfs2_rgrp);
440 bi->bi_start = 0;
441 bi->bi_len = bytes;
442 /* header block */
443 } else if (x == 0) {
444 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
445 bi->bi_offset = sizeof(struct gfs2_rgrp);
446 bi->bi_start = 0;
447 bi->bi_len = bytes;
448 /* last block */
449 } else if (x + 1 == length) {
450 bytes = bytes_left;
451 bi->bi_offset = sizeof(struct gfs2_meta_header);
bb8d8a6f 452 bi->bi_start = rgd->rd_bitbytes - bytes_left;
b3b94faa
DT
453 bi->bi_len = bytes;
454 /* other blocks */
455 } else {
568f4c96
SW
456 bytes = sdp->sd_sb.sb_bsize -
457 sizeof(struct gfs2_meta_header);
b3b94faa 458 bi->bi_offset = sizeof(struct gfs2_meta_header);
bb8d8a6f 459 bi->bi_start = rgd->rd_bitbytes - bytes_left;
b3b94faa
DT
460 bi->bi_len = bytes;
461 }
462
463 bytes_left -= bytes;
464 }
465
466 if (bytes_left) {
467 gfs2_consist_rgrpd(rgd);
468 return -EIO;
469 }
470 bi = rgd->rd_bits + (length - 1);
bb8d8a6f 471 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
b3b94faa 472 if (gfs2_consist_rgrpd(rgd)) {
bb8d8a6f 473 gfs2_rindex_print(rgd);
b3b94faa
DT
474 fs_err(sdp, "start=%u len=%u offset=%u\n",
475 bi->bi_start, bi->bi_len, bi->bi_offset);
476 }
477 return -EIO;
478 }
479
480 return 0;
481}
482
7ae8fa84
RP
483/**
484 * gfs2_ri_total - Total up the file system space, according to the rindex.
485 *
486 */
487u64 gfs2_ri_total(struct gfs2_sbd *sdp)
488{
489 u64 total_data = 0;
490 struct inode *inode = sdp->sd_rindex;
491 struct gfs2_inode *ip = GFS2_I(inode);
7ae8fa84
RP
492 char buf[sizeof(struct gfs2_rindex)];
493 struct file_ra_state ra_state;
494 int error, rgrps;
495
496 mutex_lock(&sdp->sd_rindex_mutex);
497 file_ra_state_init(&ra_state, inode->i_mapping);
498 for (rgrps = 0;; rgrps++) {
499 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
500
c9e98886 501 if (pos + sizeof(struct gfs2_rindex) >= ip->i_disksize)
7ae8fa84
RP
502 break;
503 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
504 sizeof(struct gfs2_rindex));
505 if (error != sizeof(struct gfs2_rindex))
506 break;
bb8d8a6f 507 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
7ae8fa84
RP
508 }
509 mutex_unlock(&sdp->sd_rindex_mutex);
510 return total_data;
511}
512
bb8d8a6f
SW
513static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
514{
515 const struct gfs2_rindex *str = buf;
516
517 rgd->rd_addr = be64_to_cpu(str->ri_addr);
518 rgd->rd_length = be32_to_cpu(str->ri_length);
519 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
520 rgd->rd_data = be32_to_cpu(str->ri_data);
521 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
522}
523
b3b94faa 524/**
6c53267f 525 * read_rindex_entry - Pull in a new resource index entry from the disk
b3b94faa
DT
526 * @gl: The glock covering the rindex inode
527 *
6c53267f
RP
528 * Returns: 0 on success, error code otherwise
529 */
530
531static int read_rindex_entry(struct gfs2_inode *ip,
532 struct file_ra_state *ra_state)
533{
534 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
535 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
536 char buf[sizeof(struct gfs2_rindex)];
537 int error;
538 struct gfs2_rgrpd *rgd;
539
540 error = gfs2_internal_read(ip, ra_state, buf, &pos,
541 sizeof(struct gfs2_rindex));
542 if (!error)
543 return 0;
544 if (error != sizeof(struct gfs2_rindex)) {
545 if (error > 0)
546 error = -EIO;
547 return error;
548 }
549
6bdd9be6 550 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
6c53267f
RP
551 error = -ENOMEM;
552 if (!rgd)
553 return error;
554
555 mutex_init(&rgd->rd_mutex);
556 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
557 rgd->rd_sbd = sdp;
558
559 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
560 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
561
bb8d8a6f 562 gfs2_rindex_in(rgd, buf);
6c53267f
RP
563 error = compute_bitstructs(rgd);
564 if (error)
565 return error;
566
bb8d8a6f 567 error = gfs2_glock_get(sdp, rgd->rd_addr,
6c53267f
RP
568 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
569 if (error)
570 return error;
571
572 rgd->rd_gl->gl_object = rgd;
cf45b752 573 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
c8cdf479 574 rgd->rd_flags |= GFS2_RDF_CHECK;
6c53267f
RP
575 return error;
576}
577
578/**
579 * gfs2_ri_update - Pull in a new resource index from the disk
580 * @ip: pointer to the rindex inode
581 *
b3b94faa
DT
582 * Returns: 0 on successful update, error code otherwise
583 */
584
585static int gfs2_ri_update(struct gfs2_inode *ip)
586{
feaa7bba
SW
587 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
588 struct inode *inode = &ip->i_inode;
f42faf4f 589 struct file_ra_state ra_state;
c9e98886 590 u64 rgrp_count = ip->i_disksize;
b3b94faa
DT
591 int error;
592
cd81a4ba 593 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
b3b94faa
DT
594 gfs2_consist_inode(ip);
595 return -EIO;
596 }
597
598 clear_rgrpdi(sdp);
599
f42faf4f 600 file_ra_state_init(&ra_state, inode->i_mapping);
cd81a4ba 601 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
6c53267f
RP
602 error = read_rindex_entry(ip, &ra_state);
603 if (error) {
604 clear_rgrpdi(sdp);
605 return error;
b3b94faa 606 }
6c53267f 607 }
b3b94faa 608
cf45b752 609 sdp->sd_rindex_uptodate = 1;
6c53267f
RP
610 return 0;
611}
b3b94faa 612
6c53267f
RP
613/**
614 * gfs2_ri_update_special - Pull in a new resource index from the disk
615 *
616 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
617 * In this case we know that we don't have any resource groups in memory yet.
618 *
619 * @ip: pointer to the rindex inode
620 *
621 * Returns: 0 on successful update, error code otherwise
622 */
623static int gfs2_ri_update_special(struct gfs2_inode *ip)
624{
625 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
626 struct inode *inode = &ip->i_inode;
627 struct file_ra_state ra_state;
628 int error;
b3b94faa 629
6c53267f
RP
630 file_ra_state_init(&ra_state, inode->i_mapping);
631 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
632 /* Ignore partials */
633 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
c9e98886 634 ip->i_disksize)
6c53267f
RP
635 break;
636 error = read_rindex_entry(ip, &ra_state);
637 if (error) {
638 clear_rgrpdi(sdp);
639 return error;
640 }
b3b94faa
DT
641 }
642
cf45b752 643 sdp->sd_rindex_uptodate = 1;
b3b94faa 644 return 0;
b3b94faa
DT
645}
646
647/**
648 * gfs2_rindex_hold - Grab a lock on the rindex
649 * @sdp: The GFS2 superblock
650 * @ri_gh: the glock holder
651 *
652 * We grab a lock on the rindex inode to make sure that it doesn't
653 * change whilst we are performing an operation. We keep this lock
654 * for quite long periods of time compared to other locks. This
655 * doesn't matter, since it is shared and it is very, very rarely
656 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
657 *
658 * This makes sure that we're using the latest copy of the resource index
659 * special file, which might have been updated if someone expanded the
660 * filesystem (via gfs2_grow utility), which adds new resource groups.
661 *
662 * Returns: 0 on success, error code otherwise
663 */
664
665int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
666{
feaa7bba 667 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
b3b94faa
DT
668 struct gfs2_glock *gl = ip->i_gl;
669 int error;
670
671 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
672 if (error)
673 return error;
674
675 /* Read new copy from disk if we don't have the latest */
cf45b752 676 if (!sdp->sd_rindex_uptodate) {
f55ab26a 677 mutex_lock(&sdp->sd_rindex_mutex);
cf45b752 678 if (!sdp->sd_rindex_uptodate) {
b3b94faa
DT
679 error = gfs2_ri_update(ip);
680 if (error)
681 gfs2_glock_dq_uninit(ri_gh);
682 }
f55ab26a 683 mutex_unlock(&sdp->sd_rindex_mutex);
b3b94faa
DT
684 }
685
686 return error;
687}
688
42d52e38 689static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
bb8d8a6f
SW
690{
691 const struct gfs2_rgrp *str = buf;
42d52e38 692 u32 rg_flags;
bb8d8a6f 693
42d52e38
BP
694 rg_flags = be32_to_cpu(str->rg_flags);
695 if (rg_flags & GFS2_RGF_NOALLOC)
696 rgd->rd_flags |= GFS2_RDF_NOALLOC;
697 else
698 rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
cfc8b549 699 rgd->rd_free = be32_to_cpu(str->rg_free);
73f74948 700 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
d8b71f73 701 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
bb8d8a6f
SW
702}
703
42d52e38 704static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
bb8d8a6f
SW
705{
706 struct gfs2_rgrp *str = buf;
42d52e38 707 u32 rg_flags = 0;
bb8d8a6f 708
42d52e38
BP
709 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
710 rg_flags |= GFS2_RGF_NOALLOC;
711 str->rg_flags = cpu_to_be32(rg_flags);
cfc8b549 712 str->rg_free = cpu_to_be32(rgd->rd_free);
73f74948 713 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
bb8d8a6f 714 str->__pad = cpu_to_be32(0);
d8b71f73 715 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
bb8d8a6f
SW
716 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
717}
718
b3b94faa
DT
719/**
720 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
721 * @rgd: the struct gfs2_rgrpd describing the RG to read in
722 *
723 * Read in all of a Resource Group's header and bitmap blocks.
724 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
725 *
726 * Returns: errno
727 */
728
729int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
730{
731 struct gfs2_sbd *sdp = rgd->rd_sbd;
732 struct gfs2_glock *gl = rgd->rd_gl;
bb8d8a6f 733 unsigned int length = rgd->rd_length;
b3b94faa
DT
734 struct gfs2_bitmap *bi;
735 unsigned int x, y;
736 int error;
737
f55ab26a 738 mutex_lock(&rgd->rd_mutex);
b3b94faa
DT
739
740 spin_lock(&sdp->sd_rindex_spin);
741 if (rgd->rd_bh_count) {
742 rgd->rd_bh_count++;
743 spin_unlock(&sdp->sd_rindex_spin);
f55ab26a 744 mutex_unlock(&rgd->rd_mutex);
b3b94faa
DT
745 return 0;
746 }
747 spin_unlock(&sdp->sd_rindex_spin);
748
749 for (x = 0; x < length; x++) {
750 bi = rgd->rd_bits + x;
bb8d8a6f 751 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
b3b94faa
DT
752 if (error)
753 goto fail;
754 }
755
756 for (y = length; y--;) {
757 bi = rgd->rd_bits + y;
7276b3b0 758 error = gfs2_meta_wait(sdp, bi->bi_bh);
b3b94faa
DT
759 if (error)
760 goto fail;
feaa7bba 761 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
b3b94faa
DT
762 GFS2_METATYPE_RG)) {
763 error = -EIO;
764 goto fail;
765 }
766 }
767
cf45b752 768 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
42d52e38 769 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
cf45b752 770 rgd->rd_flags |= GFS2_RDF_UPTODATE;
b3b94faa
DT
771 }
772
773 spin_lock(&sdp->sd_rindex_spin);
cfc8b549 774 rgd->rd_free_clone = rgd->rd_free;
b3b94faa
DT
775 rgd->rd_bh_count++;
776 spin_unlock(&sdp->sd_rindex_spin);
777
f55ab26a 778 mutex_unlock(&rgd->rd_mutex);
b3b94faa
DT
779
780 return 0;
781
feaa7bba 782fail:
b3b94faa
DT
783 while (x--) {
784 bi = rgd->rd_bits + x;
785 brelse(bi->bi_bh);
786 bi->bi_bh = NULL;
787 gfs2_assert_warn(sdp, !bi->bi_clone);
788 }
f55ab26a 789 mutex_unlock(&rgd->rd_mutex);
b3b94faa
DT
790
791 return error;
792}
793
794void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
795{
796 struct gfs2_sbd *sdp = rgd->rd_sbd;
797
798 spin_lock(&sdp->sd_rindex_spin);
799 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
800 rgd->rd_bh_count++;
801 spin_unlock(&sdp->sd_rindex_spin);
802}
803
804/**
805 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
806 * @rgd: the struct gfs2_rgrpd describing the RG to read in
807 *
808 */
809
810void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
811{
812 struct gfs2_sbd *sdp = rgd->rd_sbd;
bb8d8a6f 813 int x, length = rgd->rd_length;
b3b94faa
DT
814
815 spin_lock(&sdp->sd_rindex_spin);
816 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
817 if (--rgd->rd_bh_count) {
818 spin_unlock(&sdp->sd_rindex_spin);
819 return;
820 }
821
822 for (x = 0; x < length; x++) {
823 struct gfs2_bitmap *bi = rgd->rd_bits + x;
824 kfree(bi->bi_clone);
825 bi->bi_clone = NULL;
826 brelse(bi->bi_bh);
827 bi->bi_bh = NULL;
828 }
829
830 spin_unlock(&sdp->sd_rindex_spin);
831}
832
833void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
834{
835 struct gfs2_sbd *sdp = rgd->rd_sbd;
bb8d8a6f 836 unsigned int length = rgd->rd_length;
b3b94faa
DT
837 unsigned int x;
838
839 for (x = 0; x < length; x++) {
840 struct gfs2_bitmap *bi = rgd->rd_bits + x;
841 if (!bi->bi_clone)
842 continue;
843 memcpy(bi->bi_clone + bi->bi_offset,
feaa7bba 844 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
b3b94faa
DT
845 }
846
847 spin_lock(&sdp->sd_rindex_spin);
cfc8b549 848 rgd->rd_free_clone = rgd->rd_free;
b3b94faa
DT
849 spin_unlock(&sdp->sd_rindex_spin);
850}
851
852/**
853 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
854 * @ip: the incore GFS2 inode structure
855 *
856 * Returns: the struct gfs2_alloc
857 */
858
859struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
860{
6dbd8224
SW
861 BUG_ON(ip->i_alloc != NULL);
862 ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
863 return ip->i_alloc;
b3b94faa
DT
864}
865
b3b94faa
DT
866/**
867 * try_rgrp_fit - See if a given reservation will fit in a given RG
868 * @rgd: the RG data
869 * @al: the struct gfs2_alloc structure describing the reservation
870 *
871 * If there's room for the requested blocks to be allocated from the RG:
b3b94faa
DT
872 * Sets the $al_rgd field in @al.
873 *
874 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
875 */
876
877static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
878{
879 struct gfs2_sbd *sdp = rgd->rd_sbd;
880 int ret = 0;
881
42d52e38 882 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
a43a4906
SW
883 return 0;
884
b3b94faa
DT
885 spin_lock(&sdp->sd_rindex_spin);
886 if (rgd->rd_free_clone >= al->al_requested) {
887 al->al_rgd = rgd;
888 ret = 1;
889 }
890 spin_unlock(&sdp->sd_rindex_spin);
891
892 return ret;
893}
894
c8cdf479
SW
895/**
896 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
897 * @rgd: The rgrp
898 *
899 * Returns: The inode, if one has been found
900 */
901
902static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
903{
904 struct inode *inode;
6760bdcd 905 u32 goal = 0, block;
bb9bcf06 906 u64 no_addr;
5f3eae75 907 struct gfs2_sbd *sdp = rgd->rd_sbd;
b45e41d7 908 unsigned int n;
c8cdf479
SW
909
910 for(;;) {
24c73873
BP
911 if (goal >= rgd->rd_data)
912 break;
5f3eae75 913 down_write(&sdp->sd_log_flush_lock);
b45e41d7 914 n = 1;
6760bdcd 915 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
b45e41d7 916 GFS2_BLKST_UNLINKED, &n);
5f3eae75 917 up_write(&sdp->sd_log_flush_lock);
6760bdcd 918 if (block == BFITNOENT)
24c73873 919 break;
6760bdcd
BP
920 /* rgblk_search can return a block < goal, so we need to
921 keep it marching forward. */
922 no_addr = block + rgd->rd_data0;
24c73873 923 goal++;
6760bdcd 924 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
c8cdf479 925 continue;
bb9bcf06
WC
926 *last_unlinked = no_addr;
927 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
7a9f53b3 928 no_addr, -1, 1);
c8cdf479
SW
929 if (!IS_ERR(inode))
930 return inode;
931 }
932
933 rgd->rd_flags &= ~GFS2_RDF_CHECK;
934 return NULL;
935}
936
b3b94faa
DT
937/**
938 * recent_rgrp_next - get next RG from "recent" list
939 * @cur_rgd: current rgrp
b3b94faa
DT
940 *
941 * Returns: The next rgrp in the recent list
942 */
943
9cabcdbd 944static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd)
b3b94faa
DT
945{
946 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
947 struct list_head *head;
948 struct gfs2_rgrpd *rgd;
949
950 spin_lock(&sdp->sd_rindex_spin);
9cabcdbd
SW
951 head = &sdp->sd_rindex_mru_list;
952 if (unlikely(cur_rgd->rd_list_mru.next == head)) {
953 spin_unlock(&sdp->sd_rindex_spin);
954 return NULL;
b3b94faa 955 }
9cabcdbd 956 rgd = list_entry(cur_rgd->rd_list_mru.next, struct gfs2_rgrpd, rd_list_mru);
b3b94faa 957 spin_unlock(&sdp->sd_rindex_spin);
b3b94faa
DT
958 return rgd;
959}
960
b3b94faa
DT
961/**
962 * forward_rgrp_get - get an rgrp to try next from full list
963 * @sdp: The GFS2 superblock
964 *
965 * Returns: The rgrp to try next
966 */
967
968static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
969{
970 struct gfs2_rgrpd *rgd;
971 unsigned int journals = gfs2_jindex_size(sdp);
972 unsigned int rg = 0, x;
973
974 spin_lock(&sdp->sd_rindex_spin);
975
976 rgd = sdp->sd_rindex_forward;
977 if (!rgd) {
978 if (sdp->sd_rgrps >= journals)
979 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
980
b8e1aabf 981 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
b3b94faa
DT
982 x++, rgd = gfs2_rgrpd_get_next(rgd))
983 /* Do Nothing */;
984
985 sdp->sd_rindex_forward = rgd;
986 }
987
988 spin_unlock(&sdp->sd_rindex_spin);
989
990 return rgd;
991}
992
993/**
994 * forward_rgrp_set - set the forward rgrp pointer
995 * @sdp: the filesystem
996 * @rgd: The new forward rgrp
997 *
998 */
999
1000static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1001{
1002 spin_lock(&sdp->sd_rindex_spin);
1003 sdp->sd_rindex_forward = rgd;
1004 spin_unlock(&sdp->sd_rindex_spin);
1005}
1006
1007/**
1008 * get_local_rgrp - Choose and lock a rgrp for allocation
1009 * @ip: the inode to reserve space for
1010 * @rgp: the chosen and locked rgrp
1011 *
1012 * Try to acquire rgrp in way which avoids contending with others.
1013 *
1014 * Returns: errno
1015 */
1016
c8cdf479 1017static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
b3b94faa 1018{
c8cdf479 1019 struct inode *inode = NULL;
feaa7bba 1020 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa 1021 struct gfs2_rgrpd *rgd, *begin = NULL;
6dbd8224 1022 struct gfs2_alloc *al = ip->i_alloc;
b3b94faa
DT
1023 int flags = LM_FLAG_TRY;
1024 int skipped = 0;
1025 int loops = 0;
292c8c14 1026 int error, rg_locked;
b3b94faa 1027
9cabcdbd 1028 rgd = gfs2_blk2rgrpd(sdp, ip->i_goal);
b3b94faa
DT
1029
1030 while (rgd) {
292c8c14
AD
1031 rg_locked = 0;
1032
1033 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1034 rg_locked = 1;
1035 error = 0;
1036 } else {
1037 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1038 LM_FLAG_TRY, &al->al_rgd_gh);
1039 }
b3b94faa
DT
1040 switch (error) {
1041 case 0:
1042 if (try_rgrp_fit(rgd, al))
1043 goto out;
c8cdf479
SW
1044 if (rgd->rd_flags & GFS2_RDF_CHECK)
1045 inode = try_rgrp_unlink(rgd, last_unlinked);
292c8c14
AD
1046 if (!rg_locked)
1047 gfs2_glock_dq_uninit(&al->al_rgd_gh);
c8cdf479
SW
1048 if (inode)
1049 return inode;
9cabcdbd 1050 /* fall through */
b3b94faa 1051 case GLR_TRYFAILED:
9cabcdbd 1052 rgd = recent_rgrp_next(rgd);
b3b94faa
DT
1053 break;
1054
1055 default:
c8cdf479 1056 return ERR_PTR(error);
b3b94faa
DT
1057 }
1058 }
1059
1060 /* Go through full list of rgrps */
1061
1062 begin = rgd = forward_rgrp_get(sdp);
1063
1064 for (;;) {
292c8c14
AD
1065 rg_locked = 0;
1066
1067 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1068 rg_locked = 1;
1069 error = 0;
1070 } else {
1071 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1072 &al->al_rgd_gh);
1073 }
b3b94faa
DT
1074 switch (error) {
1075 case 0:
1076 if (try_rgrp_fit(rgd, al))
1077 goto out;
c8cdf479
SW
1078 if (rgd->rd_flags & GFS2_RDF_CHECK)
1079 inode = try_rgrp_unlink(rgd, last_unlinked);
292c8c14
AD
1080 if (!rg_locked)
1081 gfs2_glock_dq_uninit(&al->al_rgd_gh);
c8cdf479
SW
1082 if (inode)
1083 return inode;
b3b94faa
DT
1084 break;
1085
1086 case GLR_TRYFAILED:
1087 skipped++;
1088 break;
1089
1090 default:
c8cdf479 1091 return ERR_PTR(error);
b3b94faa
DT
1092 }
1093
1094 rgd = gfs2_rgrpd_get_next(rgd);
1095 if (!rgd)
1096 rgd = gfs2_rgrpd_get_first(sdp);
1097
1098 if (rgd == begin) {
172e045a 1099 if (++loops >= 3)
c8cdf479 1100 return ERR_PTR(-ENOSPC);
172e045a
BM
1101 if (!skipped)
1102 loops++;
b3b94faa 1103 flags = 0;
172e045a
BM
1104 if (loops == 2)
1105 gfs2_log_flush(sdp, NULL);
b3b94faa
DT
1106 }
1107 }
1108
feaa7bba 1109out:
b3b94faa 1110 if (begin) {
9cabcdbd
SW
1111 spin_lock(&sdp->sd_rindex_spin);
1112 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
1113 spin_unlock(&sdp->sd_rindex_spin);
b3b94faa
DT
1114 rgd = gfs2_rgrpd_get_next(rgd);
1115 if (!rgd)
1116 rgd = gfs2_rgrpd_get_first(sdp);
1117 forward_rgrp_set(sdp, rgd);
1118 }
1119
c8cdf479 1120 return NULL;
b3b94faa
DT
1121}
1122
1123/**
1124 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1125 * @ip: the inode to reserve space for
1126 *
1127 * Returns: errno
1128 */
1129
1130int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1131{
feaa7bba 1132 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
6dbd8224 1133 struct gfs2_alloc *al = ip->i_alloc;
c8cdf479 1134 struct inode *inode;
7ae8fa84 1135 int error = 0;
6760bdcd 1136 u64 last_unlinked = NO_BLOCK;
b3b94faa
DT
1137
1138 if (gfs2_assert_warn(sdp, al->al_requested))
1139 return -EINVAL;
1140
c8cdf479 1141try_again:
7ae8fa84
RP
1142 /* We need to hold the rindex unless the inode we're using is
1143 the rindex itself, in which case it's already held. */
1144 if (ip != GFS2_I(sdp->sd_rindex))
1145 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1146 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
6c53267f 1147 error = gfs2_ri_update_special(ip);
7ae8fa84 1148
b3b94faa
DT
1149 if (error)
1150 return error;
1151
c8cdf479
SW
1152 inode = get_local_rgrp(ip, &last_unlinked);
1153 if (inode) {
7ae8fa84
RP
1154 if (ip != GFS2_I(sdp->sd_rindex))
1155 gfs2_glock_dq_uninit(&al->al_ri_gh);
c8cdf479
SW
1156 if (IS_ERR(inode))
1157 return PTR_ERR(inode);
1158 iput(inode);
1159 gfs2_log_flush(sdp, NULL);
1160 goto try_again;
b3b94faa
DT
1161 }
1162
1163 al->al_file = file;
1164 al->al_line = line;
1165
1166 return 0;
1167}
1168
1169/**
1170 * gfs2_inplace_release - release an inplace reservation
1171 * @ip: the inode the reservation was taken out on
1172 *
1173 * Release a reservation made by gfs2_inplace_reserve().
1174 */
1175
1176void gfs2_inplace_release(struct gfs2_inode *ip)
1177{
feaa7bba 1178 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
6dbd8224 1179 struct gfs2_alloc *al = ip->i_alloc;
b3b94faa
DT
1180
1181 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1182 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1183 "al_file = %s, al_line = %u\n",
1184 al->al_alloced, al->al_requested, al->al_file,
1185 al->al_line);
1186
1187 al->al_rgd = NULL;
292c8c14
AD
1188 if (al->al_rgd_gh.gh_gl)
1189 gfs2_glock_dq_uninit(&al->al_rgd_gh);
7ae8fa84
RP
1190 if (ip != GFS2_I(sdp->sd_rindex))
1191 gfs2_glock_dq_uninit(&al->al_ri_gh);
b3b94faa
DT
1192}
1193
1194/**
1195 * gfs2_get_block_type - Check a block in a RG is of given type
1196 * @rgd: the resource group holding the block
1197 * @block: the block number
1198 *
1199 * Returns: The block type (GFS2_BLKST_*)
1200 */
1201
cd915493 1202unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
b3b94faa
DT
1203{
1204 struct gfs2_bitmap *bi = NULL;
cd915493 1205 u32 length, rgrp_block, buf_block;
b3b94faa
DT
1206 unsigned int buf;
1207 unsigned char type;
1208
bb8d8a6f
SW
1209 length = rgd->rd_length;
1210 rgrp_block = block - rgd->rd_data0;
b3b94faa
DT
1211
1212 for (buf = 0; buf < length; buf++) {
1213 bi = rgd->rd_bits + buf;
1214 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1215 break;
1216 }
1217
1218 gfs2_assert(rgd->rd_sbd, buf < length);
1219 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1220
feaa7bba 1221 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
b3b94faa
DT
1222 bi->bi_len, buf_block);
1223
1224 return type;
1225}
1226
1227/**
1228 * rgblk_search - find a block in @old_state, change allocation
1229 * state to @new_state
1230 * @rgd: the resource group descriptor
1231 * @goal: the goal block within the RG (start here to search for avail block)
1232 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1233 * @new_state: GFS2_BLKST_XXX the after-allocation block state
b45e41d7 1234 * @n: The extent length
b3b94faa
DT
1235 *
1236 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1237 * Add the found bitmap buffer to the transaction.
1238 * Set the found bits to @new_state to change block's allocation state.
1239 *
1240 * This function never fails, because we wouldn't call it unless we
1241 * know (from reservation results, etc.) that a block is available.
1242 *
1243 * Scope of @goal and returned block is just within rgrp, not the whole
1244 * filesystem.
1245 *
1246 * Returns: the block number allocated
1247 */
1248
cd915493 1249static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
b45e41d7
SW
1250 unsigned char old_state, unsigned char new_state,
1251 unsigned int *n)
b3b94faa
DT
1252{
1253 struct gfs2_bitmap *bi = NULL;
b45e41d7 1254 const u32 length = rgd->rd_length;
cd915493 1255 u32 blk = 0;
b3b94faa 1256 unsigned int buf, x;
b45e41d7
SW
1257 const unsigned int elen = *n;
1258 const u8 *buffer;
b3b94faa 1259
b45e41d7 1260 *n = 0;
b3b94faa
DT
1261 /* Find bitmap block that contains bits for goal block */
1262 for (buf = 0; buf < length; buf++) {
1263 bi = rgd->rd_bits + buf;
1264 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1265 break;
1266 }
1267
1268 gfs2_assert(rgd->rd_sbd, buf < length);
1269
1270 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1271 goal -= bi->bi_start * GFS2_NBBY;
1272
1273 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1274 "x <= length", instead of "x < length", because we typically start
1275 the search in the middle of a bit block, but if we can't find an
1276 allocatable block anywhere else, we want to be able wrap around and
1277 search in the first part of our first-searched bit block. */
1278 for (x = 0; x <= length; x++) {
5f3eae75
BP
1279 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1280 bitmaps, so we must search the originals for that. */
b45e41d7 1281 buffer = bi->bi_bh->b_data + bi->bi_offset;
5f3eae75 1282 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
110acf38
SW
1283 buffer = bi->bi_clone + bi->bi_offset;
1284
1285 blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
b3b94faa
DT
1286 if (blk != BFITNOENT)
1287 break;
1288
1289 /* Try next bitmap block (wrap back to rgrp header if at end) */
1290 buf = (buf + 1) % length;
1291 bi = rgd->rd_bits + buf;
1292 goal = 0;
1293 }
1294
6760bdcd 1295 if (blk != BFITNOENT && old_state != new_state) {
b45e41d7 1296 *n = 1;
c8cdf479 1297 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
b45e41d7 1298 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
b3b94faa 1299 bi->bi_len, blk, new_state);
9b8c81d1
SW
1300 goal = blk;
1301 while (*n < elen) {
b45e41d7 1302 goal++;
9b8c81d1 1303 if (goal >= (bi->bi_len * GFS2_NBBY))
b45e41d7
SW
1304 break;
1305 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1306 GFS2_BLKST_FREE)
1307 break;
b45e41d7 1308 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
9b8c81d1
SW
1309 bi->bi_offset, bi->bi_len, goal,
1310 new_state);
1311 (*n)++;
b45e41d7 1312 }
c8cdf479 1313 }
b3b94faa 1314
6eefaf61 1315 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
b3b94faa
DT
1316}
1317
1318/**
1319 * rgblk_free - Change alloc state of given block(s)
1320 * @sdp: the filesystem
1321 * @bstart: the start of a run of blocks to free
1322 * @blen: the length of the block run (all must lie within ONE RG!)
1323 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1324 *
1325 * Returns: Resource group containing the block(s)
1326 */
1327
cd915493
SW
1328static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1329 u32 blen, unsigned char new_state)
b3b94faa
DT
1330{
1331 struct gfs2_rgrpd *rgd;
1332 struct gfs2_bitmap *bi = NULL;
cd915493 1333 u32 length, rgrp_blk, buf_blk;
b3b94faa
DT
1334 unsigned int buf;
1335
1336 rgd = gfs2_blk2rgrpd(sdp, bstart);
1337 if (!rgd) {
1338 if (gfs2_consist(sdp))
382066da 1339 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
b3b94faa
DT
1340 return NULL;
1341 }
1342
bb8d8a6f 1343 length = rgd->rd_length;
b3b94faa 1344
bb8d8a6f 1345 rgrp_blk = bstart - rgd->rd_data0;
b3b94faa
DT
1346
1347 while (blen--) {
1348 for (buf = 0; buf < length; buf++) {
1349 bi = rgd->rd_bits + buf;
1350 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1351 break;
1352 }
1353
1354 gfs2_assert(rgd->rd_sbd, buf < length);
1355
1356 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1357 rgrp_blk++;
1358
1359 if (!bi->bi_clone) {
1360 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
dd894be8 1361 GFP_NOFS | __GFP_NOFAIL);
b3b94faa
DT
1362 memcpy(bi->bi_clone + bi->bi_offset,
1363 bi->bi_bh->b_data + bi->bi_offset,
1364 bi->bi_len);
1365 }
d4e9c4c3 1366 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
b45e41d7 1367 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
b3b94faa
DT
1368 bi->bi_len, buf_blk, new_state);
1369 }
1370
1371 return rgd;
1372}
1373
1374/**
1639431a
SW
1375 * gfs2_alloc_block - Allocate a block
1376 * @ip: the inode to allocate the block for
b3b94faa
DT
1377 *
1378 * Returns: the allocated block
1379 */
1380
b45e41d7 1381u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
b3b94faa 1382{
feaa7bba 1383 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
6dbd8224 1384 struct gfs2_alloc *al = ip->i_alloc;
b3b94faa 1385 struct gfs2_rgrpd *rgd = al->al_rgd;
cd915493
SW
1386 u32 goal, blk;
1387 u64 block;
b3b94faa 1388
ce276b06
SW
1389 if (rgrp_contains_block(rgd, ip->i_goal))
1390 goal = ip->i_goal - rgd->rd_data0;
b3b94faa 1391 else
ac576cc5 1392 goal = rgd->rd_last_alloc;
b3b94faa 1393
b45e41d7 1394 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
6eefaf61 1395 BUG_ON(blk == BFITNOENT);
b3b94faa 1396
b45e41d7 1397 rgd->rd_last_alloc = blk;
bb8d8a6f 1398 block = rgd->rd_data0 + blk;
ce276b06 1399 ip->i_goal = block;
b3b94faa 1400
cfc8b549
SW
1401 gfs2_assert_withdraw(sdp, rgd->rd_free >= *n);
1402 rgd->rd_free -= *n;
b3b94faa 1403
d4e9c4c3 1404 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 1405 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
b3b94faa 1406
b45e41d7 1407 al->al_alloced += *n;
b3b94faa 1408
ad99f777 1409 gfs2_statfs_change(sdp, 0, -(s64)*n, 0);
b45e41d7 1410 gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
b3b94faa
DT
1411
1412 spin_lock(&sdp->sd_rindex_spin);
b45e41d7 1413 rgd->rd_free_clone -= *n;
b3b94faa
DT
1414 spin_unlock(&sdp->sd_rindex_spin);
1415
1416 return block;
1417}
1418
1419/**
1420 * gfs2_alloc_di - Allocate a dinode
1421 * @dip: the directory that the inode is going in
1422 *
1423 * Returns: the block allocated
1424 */
1425
4340fe62 1426u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
b3b94faa 1427{
feaa7bba 1428 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
6dbd8224 1429 struct gfs2_alloc *al = dip->i_alloc;
b3b94faa 1430 struct gfs2_rgrpd *rgd = al->al_rgd;
4340fe62
SW
1431 u32 blk;
1432 u64 block;
b45e41d7 1433 unsigned int n = 1;
b3b94faa 1434
ac576cc5 1435 blk = rgblk_search(rgd, rgd->rd_last_alloc,
b45e41d7 1436 GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
6eefaf61 1437 BUG_ON(blk == BFITNOENT);
b3b94faa 1438
ac576cc5 1439 rgd->rd_last_alloc = blk;
b3b94faa 1440
bb8d8a6f 1441 block = rgd->rd_data0 + blk;
b3b94faa 1442
cfc8b549
SW
1443 gfs2_assert_withdraw(sdp, rgd->rd_free);
1444 rgd->rd_free--;
73f74948 1445 rgd->rd_dinodes++;
d8b71f73 1446 *generation = rgd->rd_igeneration++;
d4e9c4c3 1447 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 1448 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
b3b94faa
DT
1449
1450 al->al_alloced++;
1451
1452 gfs2_statfs_change(sdp, 0, -1, +1);
5731be53 1453 gfs2_trans_add_unrevoke(sdp, block, 1);
b3b94faa
DT
1454
1455 spin_lock(&sdp->sd_rindex_spin);
1456 rgd->rd_free_clone--;
1457 spin_unlock(&sdp->sd_rindex_spin);
1458
1459 return block;
1460}
1461
1462/**
1463 * gfs2_free_data - free a contiguous run of data block(s)
1464 * @ip: the inode these blocks are being freed from
1465 * @bstart: first block of a run of contiguous blocks
1466 * @blen: the length of the block run
1467 *
1468 */
1469
cd915493 1470void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
b3b94faa 1471{
feaa7bba 1472 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
1473 struct gfs2_rgrpd *rgd;
1474
1475 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1476 if (!rgd)
1477 return;
1478
cfc8b549 1479 rgd->rd_free += blen;
b3b94faa 1480
d4e9c4c3 1481 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 1482 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
b3b94faa
DT
1483
1484 gfs2_trans_add_rg(rgd);
1485
1486 gfs2_statfs_change(sdp, 0, +blen, 0);
2933f925 1487 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
b3b94faa
DT
1488}
1489
1490/**
1491 * gfs2_free_meta - free a contiguous run of data block(s)
1492 * @ip: the inode these blocks are being freed from
1493 * @bstart: first block of a run of contiguous blocks
1494 * @blen: the length of the block run
1495 *
1496 */
1497
cd915493 1498void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
b3b94faa 1499{
feaa7bba 1500 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
1501 struct gfs2_rgrpd *rgd;
1502
1503 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1504 if (!rgd)
1505 return;
1506
cfc8b549 1507 rgd->rd_free += blen;
b3b94faa 1508
d4e9c4c3 1509 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 1510 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
b3b94faa
DT
1511
1512 gfs2_trans_add_rg(rgd);
1513
1514 gfs2_statfs_change(sdp, 0, +blen, 0);
2933f925 1515 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
b3b94faa
DT
1516 gfs2_meta_wipe(ip, bstart, blen);
1517}
1518
feaa7bba
SW
1519void gfs2_unlink_di(struct inode *inode)
1520{
1521 struct gfs2_inode *ip = GFS2_I(inode);
1522 struct gfs2_sbd *sdp = GFS2_SB(inode);
1523 struct gfs2_rgrpd *rgd;
dbb7cae2 1524 u64 blkno = ip->i_no_addr;
feaa7bba
SW
1525
1526 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1527 if (!rgd)
1528 return;
1529 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 1530 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
feaa7bba
SW
1531 gfs2_trans_add_rg(rgd);
1532}
1533
cd915493 1534static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
b3b94faa
DT
1535{
1536 struct gfs2_sbd *sdp = rgd->rd_sbd;
1537 struct gfs2_rgrpd *tmp_rgd;
1538
1539 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1540 if (!tmp_rgd)
1541 return;
1542 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1543
73f74948 1544 if (!rgd->rd_dinodes)
b3b94faa 1545 gfs2_consist_rgrpd(rgd);
73f74948 1546 rgd->rd_dinodes--;
cfc8b549 1547 rgd->rd_free++;
b3b94faa 1548
d4e9c4c3 1549 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
42d52e38 1550 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
b3b94faa
DT
1551
1552 gfs2_statfs_change(sdp, 0, +1, -1);
1553 gfs2_trans_add_rg(rgd);
1554}
1555
b3b94faa
DT
1556
1557void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1558{
dbb7cae2 1559 gfs2_free_uninit_di(rgd, ip->i_no_addr);
2933f925 1560 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
dbb7cae2 1561 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
b3b94faa
DT
1562}
1563
1564/**
1565 * gfs2_rlist_add - add a RG to a list of RGs
1566 * @sdp: the filesystem
1567 * @rlist: the list of resource groups
1568 * @block: the block
1569 *
1570 * Figure out what RG a block belongs to and add that RG to the list
1571 *
1572 * FIXME: Don't use NOFAIL
1573 *
1574 */
1575
1576void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
cd915493 1577 u64 block)
b3b94faa
DT
1578{
1579 struct gfs2_rgrpd *rgd;
1580 struct gfs2_rgrpd **tmp;
1581 unsigned int new_space;
1582 unsigned int x;
1583
1584 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1585 return;
1586
1587 rgd = gfs2_blk2rgrpd(sdp, block);
1588 if (!rgd) {
1589 if (gfs2_consist(sdp))
382066da 1590 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
b3b94faa
DT
1591 return;
1592 }
1593
1594 for (x = 0; x < rlist->rl_rgrps; x++)
1595 if (rlist->rl_rgd[x] == rgd)
1596 return;
1597
1598 if (rlist->rl_rgrps == rlist->rl_space) {
1599 new_space = rlist->rl_space + 10;
1600
1601 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
dd894be8 1602 GFP_NOFS | __GFP_NOFAIL);
b3b94faa
DT
1603
1604 if (rlist->rl_rgd) {
1605 memcpy(tmp, rlist->rl_rgd,
1606 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1607 kfree(rlist->rl_rgd);
1608 }
1609
1610 rlist->rl_space = new_space;
1611 rlist->rl_rgd = tmp;
1612 }
1613
1614 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1615}
1616
1617/**
1618 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1619 * and initialize an array of glock holders for them
1620 * @rlist: the list of resource groups
1621 * @state: the lock state to acquire the RG lock in
1622 * @flags: the modifier flags for the holder structures
1623 *
1624 * FIXME: Don't use NOFAIL
1625 *
1626 */
1627
fe6c991c 1628void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
b3b94faa
DT
1629{
1630 unsigned int x;
1631
1632 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
dd894be8 1633 GFP_NOFS | __GFP_NOFAIL);
b3b94faa
DT
1634 for (x = 0; x < rlist->rl_rgrps; x++)
1635 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
fe6c991c 1636 state, 0,
b3b94faa
DT
1637 &rlist->rl_ghs[x]);
1638}
1639
1640/**
1641 * gfs2_rlist_free - free a resource group list
1642 * @list: the list of resource groups
1643 *
1644 */
1645
1646void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1647{
1648 unsigned int x;
1649
1650 kfree(rlist->rl_rgd);
1651
1652 if (rlist->rl_ghs) {
1653 for (x = 0; x < rlist->rl_rgrps; x++)
1654 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1655 kfree(rlist->rl_ghs);
1656 }
1657}
1658
This page took 0.387533 seconds and 5 git commands to generate.