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