blob: e857f405353b7a8f655119cb38fcb2e6ae675a01 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Robert Peterson7ae8fa82007-05-09 09:37:57 -05003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
Steven Whitehousef42faf42006-01-30 18:34:10 +000014#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020016#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "glock.h"
21#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "lops.h"
23#include "meta_io.h"
24#include "quota.h"
25#include "rgrp.h"
26#include "super.h"
27#include "trans.h"
Steven Whitehousef42faf42006-01-30 18:34:10 +000028#include "ops_file.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
Benjamin Marzinski172e0452007-03-23 14:51:56 -060030#include "log.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031
Steven Whitehouse2c1e52a2006-09-05 15:41:57 -040032#define BFITNOENT ((u32)~0)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040033
34/*
35 * These routines are used by the resource group routines (rgrp.c)
36 * to keep track of block allocation. Each block is represented by two
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040037 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
38 *
39 * 0 = Free
40 * 1 = Used (not metadata)
41 * 2 = Unlinked (still in use) inode
42 * 3 = Used (metadata)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040043 */
44
45static const char valid_change[16] = {
46 /* current */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040047 /* n */ 0, 1, 1, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040048 /* e */ 1, 0, 0, 0,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040049 /* w */ 0, 0, 0, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040050 1, 0, 0, 0
51};
52
53/**
54 * gfs2_setbit - Set a bit in the bitmaps
55 * @buffer: the buffer that holds the bitmaps
56 * @buflen: the length (in bytes) of the buffer
57 * @block: the block to set
58 * @new_state: the new state of the block
59 *
60 */
61
Steven Whitehouse3efd7532006-05-18 14:02:52 -040062static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040063 unsigned int buflen, u32 block,
Steven Whitehouse3efd7532006-05-18 14:02:52 -040064 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040065{
66 unsigned char *byte, *end, cur_state;
67 unsigned int bit;
68
69 byte = buffer + (block / GFS2_NBBY);
70 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
71 end = buffer + buflen;
72
73 gfs2_assert(rgd->rd_sbd, byte < end);
74
75 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
76
77 if (valid_change[new_state * 4 + cur_state]) {
78 *byte ^= cur_state << bit;
79 *byte |= new_state << bit;
80 } else
81 gfs2_consist_rgrpd(rgd);
82}
83
84/**
85 * gfs2_testbit - test a bit in the bitmaps
86 * @buffer: the buffer that holds the bitmaps
87 * @buflen: the length (in bytes) of the buffer
88 * @block: the block to read
89 *
90 */
91
Steven Whitehouse3efd7532006-05-18 14:02:52 -040092static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040093 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040094{
95 unsigned char *byte, *end, cur_state;
96 unsigned int bit;
97
98 byte = buffer + (block / GFS2_NBBY);
99 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
100 end = buffer + buflen;
101
102 gfs2_assert(rgd->rd_sbd, byte < end);
103
104 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
105
106 return cur_state;
107}
108
109/**
110 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
111 * a block in a given allocation state.
112 * @buffer: the buffer that holds the bitmaps
113 * @buflen: the length (in bytes) of the buffer
114 * @goal: start search at this block's bit-pair (within @buffer)
115 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
116 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
117 *
118 * Scope of @goal and returned block number is only within this bitmap buffer,
119 * not entire rgrp or filesystem. @buffer will be offset from the actual
120 * beginning of a bitmap block buffer, skipping any header structures.
121 *
122 * Return: the block number (bitmap buffer scope) that was found
123 */
124
Steven Whitehousecd915492006-09-04 12:49:07 -0400125static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
126 unsigned int buflen, u32 goal,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400127 unsigned char old_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400128{
129 unsigned char *byte, *end, alloc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400130 u32 blk = goal;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400131 unsigned int bit;
132
133 byte = buffer + (goal / GFS2_NBBY);
134 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
135 end = buffer + buflen;
136 alloc = (old_state & 1) ? 0 : 0x55;
137
138 while (byte < end) {
139 if ((*byte & 0x55) == alloc) {
140 blk += (8 - bit) >> 1;
141
142 bit = 0;
143 byte++;
144
145 continue;
146 }
147
148 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
149 return blk;
150
151 bit += GFS2_BIT_SIZE;
152 if (bit >= 8) {
153 bit = 0;
154 byte++;
155 }
156
157 blk++;
158 }
159
160 return BFITNOENT;
161}
162
163/**
164 * gfs2_bitcount - count the number of bits in a certain state
165 * @buffer: the buffer that holds the bitmaps
166 * @buflen: the length (in bytes) of the buffer
167 * @state: the state of the block we're looking for
168 *
169 * Returns: The number of bits
170 */
171
Steven Whitehousecd915492006-09-04 12:49:07 -0400172static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400173 unsigned int buflen, unsigned char state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400174{
175 unsigned char *byte = buffer;
176 unsigned char *end = buffer + buflen;
177 unsigned char state1 = state << 2;
178 unsigned char state2 = state << 4;
179 unsigned char state3 = state << 6;
Steven Whitehousecd915492006-09-04 12:49:07 -0400180 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400181
182 for (; byte < end; byte++) {
183 if (((*byte) & 0x03) == state)
184 count++;
185 if (((*byte) & 0x0C) == state1)
186 count++;
187 if (((*byte) & 0x30) == state2)
188 count++;
189 if (((*byte) & 0xC0) == state3)
190 count++;
191 }
192
193 return count;
194}
195
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196/**
197 * gfs2_rgrp_verify - Verify that a resource group is consistent
198 * @sdp: the filesystem
199 * @rgd: the rgrp
200 *
201 */
202
203void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
204{
205 struct gfs2_sbd *sdp = rgd->rd_sbd;
206 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -0400207 u32 length = rgd->rd_ri.ri_length;
208 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 int buf, x;
210
Steven Whitehousecd915492006-09-04 12:49:07 -0400211 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212
213 /* Count # blocks in each of 4 possible allocation states */
214 for (buf = 0; buf < length; buf++) {
215 bi = rgd->rd_bits + buf;
216 for (x = 0; x < 4; x++)
217 count[x] += gfs2_bitcount(rgd,
218 bi->bi_bh->b_data +
219 bi->bi_offset,
220 bi->bi_len, x);
221 }
222
223 if (count[0] != rgd->rd_rg.rg_free) {
224 if (gfs2_consist_rgrpd(rgd))
225 fs_err(sdp, "free data mismatch: %u != %u\n",
226 count[0], rgd->rd_rg.rg_free);
227 return;
228 }
229
230 tmp = rgd->rd_ri.ri_data -
231 rgd->rd_rg.rg_free -
232 rgd->rd_rg.rg_dinodes;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400233 if (count[1] + count[2] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 if (gfs2_consist_rgrpd(rgd))
235 fs_err(sdp, "used data mismatch: %u != %u\n",
236 count[1], tmp);
237 return;
238 }
239
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 if (count[3] != rgd->rd_rg.rg_dinodes) {
241 if (gfs2_consist_rgrpd(rgd))
242 fs_err(sdp, "used metadata mismatch: %u != %u\n",
243 count[3], rgd->rd_rg.rg_dinodes);
244 return;
245 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400246
247 if (count[2] > count[3]) {
248 if (gfs2_consist_rgrpd(rgd))
249 fs_err(sdp, "unlinked inodes > inodes: %u\n",
250 count[2]);
251 return;
252 }
253
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254}
255
Al Viro1e81c4c2006-10-13 22:51:24 -0400256static inline int rgrp_contains_block(struct gfs2_rindex_host *ri, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257{
Steven Whitehousecd915492006-09-04 12:49:07 -0400258 u64 first = ri->ri_data0;
259 u64 last = first + ri->ri_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400260 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261}
262
263/**
264 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
265 * @sdp: The GFS2 superblock
266 * @n: The data block number
267 *
268 * Returns: The resource group, or NULL if not found
269 */
270
Steven Whitehousecd915492006-09-04 12:49:07 -0400271struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272{
273 struct gfs2_rgrpd *rgd;
274
275 spin_lock(&sdp->sd_rindex_spin);
276
277 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
278 if (rgrp_contains_block(&rgd->rd_ri, blk)) {
279 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
280 spin_unlock(&sdp->sd_rindex_spin);
281 return rgd;
282 }
283 }
284
285 spin_unlock(&sdp->sd_rindex_spin);
286
287 return NULL;
288}
289
290/**
291 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
292 * @sdp: The GFS2 superblock
293 *
294 * Returns: The first rgrp in the filesystem
295 */
296
297struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
298{
299 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
300 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
301}
302
303/**
304 * gfs2_rgrpd_get_next - get the next RG
305 * @rgd: A RG
306 *
307 * Returns: The next rgrp
308 */
309
310struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
311{
312 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
313 return NULL;
314 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
315}
316
317static void clear_rgrpdi(struct gfs2_sbd *sdp)
318{
319 struct list_head *head;
320 struct gfs2_rgrpd *rgd;
321 struct gfs2_glock *gl;
322
323 spin_lock(&sdp->sd_rindex_spin);
324 sdp->sd_rindex_forward = NULL;
325 head = &sdp->sd_rindex_recent_list;
326 while (!list_empty(head)) {
327 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
328 list_del(&rgd->rd_recent);
329 }
330 spin_unlock(&sdp->sd_rindex_spin);
331
332 head = &sdp->sd_rindex_list;
333 while (!list_empty(head)) {
334 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
335 gl = rgd->rd_gl;
336
337 list_del(&rgd->rd_list);
338 list_del(&rgd->rd_list_mru);
339
340 if (gl) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500341 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342 gfs2_glock_put(gl);
343 }
344
345 kfree(rgd->rd_bits);
346 kfree(rgd);
347 }
348}
349
350void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
351{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000352 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 clear_rgrpdi(sdp);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000354 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355}
356
357/**
358 * gfs2_compute_bitstructs - Compute the bitmap sizes
359 * @rgd: The resource group descriptor
360 *
361 * Calculates bitmap descriptors, one for each block that contains bitmap data
362 *
363 * Returns: errno
364 */
365
366static int compute_bitstructs(struct gfs2_rgrpd *rgd)
367{
368 struct gfs2_sbd *sdp = rgd->rd_sbd;
369 struct gfs2_bitmap *bi;
Steven Whitehousecd915492006-09-04 12:49:07 -0400370 u32 length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
371 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 int x;
373
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400374 if (!length)
375 return -EINVAL;
376
Steven Whitehousedd894be2006-07-27 14:29:00 -0400377 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000378 if (!rgd->rd_bits)
379 return -ENOMEM;
380
381 bytes_left = rgd->rd_ri.ri_bitbytes;
382
383 for (x = 0; x < length; x++) {
384 bi = rgd->rd_bits + x;
385
386 /* small rgrp; bitmap stored completely in header block */
387 if (length == 1) {
388 bytes = bytes_left;
389 bi->bi_offset = sizeof(struct gfs2_rgrp);
390 bi->bi_start = 0;
391 bi->bi_len = bytes;
392 /* header block */
393 } else if (x == 0) {
394 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
395 bi->bi_offset = sizeof(struct gfs2_rgrp);
396 bi->bi_start = 0;
397 bi->bi_len = bytes;
398 /* last block */
399 } else if (x + 1 == length) {
400 bytes = bytes_left;
401 bi->bi_offset = sizeof(struct gfs2_meta_header);
402 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
403 bi->bi_len = bytes;
404 /* other blocks */
405 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500406 bytes = sdp->sd_sb.sb_bsize -
407 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 bi->bi_offset = sizeof(struct gfs2_meta_header);
409 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
410 bi->bi_len = bytes;
411 }
412
413 bytes_left -= bytes;
414 }
415
416 if (bytes_left) {
417 gfs2_consist_rgrpd(rgd);
418 return -EIO;
419 }
420 bi = rgd->rd_bits + (length - 1);
421 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
422 if (gfs2_consist_rgrpd(rgd)) {
423 gfs2_rindex_print(&rgd->rd_ri);
424 fs_err(sdp, "start=%u len=%u offset=%u\n",
425 bi->bi_start, bi->bi_len, bi->bi_offset);
426 }
427 return -EIO;
428 }
429
430 return 0;
431}
432
433/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500434 * gfs2_ri_total - Total up the file system space, according to the rindex.
435 *
436 */
437u64 gfs2_ri_total(struct gfs2_sbd *sdp)
438{
439 u64 total_data = 0;
440 struct inode *inode = sdp->sd_rindex;
441 struct gfs2_inode *ip = GFS2_I(inode);
442 struct gfs2_rindex_host ri;
443 char buf[sizeof(struct gfs2_rindex)];
444 struct file_ra_state ra_state;
445 int error, rgrps;
446
447 mutex_lock(&sdp->sd_rindex_mutex);
448 file_ra_state_init(&ra_state, inode->i_mapping);
449 for (rgrps = 0;; rgrps++) {
450 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
451
452 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
453 break;
454 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
455 sizeof(struct gfs2_rindex));
456 if (error != sizeof(struct gfs2_rindex))
457 break;
458 gfs2_rindex_in(&ri, buf);
459 total_data += ri.ri_data;
460 }
461 mutex_unlock(&sdp->sd_rindex_mutex);
462 return total_data;
463}
464
465/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466 * gfs2_ri_update - Pull in a new resource index from the disk
467 * @gl: The glock covering the rindex inode
468 *
469 * Returns: 0 on successful update, error code otherwise
470 */
471
472static int gfs2_ri_update(struct gfs2_inode *ip)
473{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400474 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
475 struct inode *inode = &ip->i_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 struct gfs2_rgrpd *rgd;
477 char buf[sizeof(struct gfs2_rindex)];
Steven Whitehousef42faf42006-01-30 18:34:10 +0000478 struct file_ra_state ra_state;
Steven Whitehousecd915492006-09-04 12:49:07 -0400479 u64 junk = ip->i_di.di_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 int error;
481
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500482 /* If someone is holding the rindex file with a glock, they must
483 be updating it, in which case we may have partial entries.
484 In this case, we ignore the partials. */
485 if (!gfs2_glock_is_held_excl(ip->i_gl) &&
486 !gfs2_glock_is_held_shrd(ip->i_gl) &&
487 do_div(junk, sizeof(struct gfs2_rindex))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488 gfs2_consist_inode(ip);
489 return -EIO;
490 }
491
492 clear_rgrpdi(sdp);
493
Steven Whitehousef42faf42006-01-30 18:34:10 +0000494 file_ra_state_init(&ra_state, inode->i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000496 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500497
498 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
499 break;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000500 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501 sizeof(struct gfs2_rindex));
502 if (!error)
503 break;
504 if (error != sizeof(struct gfs2_rindex)) {
505 if (error > 0)
506 error = -EIO;
507 goto fail;
508 }
509
Steven Whitehousedd894be2006-07-27 14:29:00 -0400510 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 error = -ENOMEM;
512 if (!rgd)
513 goto fail;
514
Steven Whitehousef55ab262006-02-21 12:51:39 +0000515 mutex_init(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
517 rgd->rd_sbd = sdp;
518
519 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
520 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
521
522 gfs2_rindex_in(&rgd->rd_ri, buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 error = compute_bitstructs(rgd);
524 if (error)
525 goto fail;
526
527 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
528 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
529 if (error)
530 goto fail;
531
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500532 rgd->rd_gl->gl_object = rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
534 }
535
536 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537 return 0;
538
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400539fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 clear_rgrpdi(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 return error;
542}
543
544/**
545 * gfs2_rindex_hold - Grab a lock on the rindex
546 * @sdp: The GFS2 superblock
547 * @ri_gh: the glock holder
548 *
549 * We grab a lock on the rindex inode to make sure that it doesn't
550 * change whilst we are performing an operation. We keep this lock
551 * for quite long periods of time compared to other locks. This
552 * doesn't matter, since it is shared and it is very, very rarely
553 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
554 *
555 * This makes sure that we're using the latest copy of the resource index
556 * special file, which might have been updated if someone expanded the
557 * filesystem (via gfs2_grow utility), which adds new resource groups.
558 *
559 * Returns: 0 on success, error code otherwise
560 */
561
562int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
563{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400564 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 struct gfs2_glock *gl = ip->i_gl;
566 int error;
567
568 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
569 if (error)
570 return error;
571
572 /* Read new copy from disk if we don't have the latest */
573 if (sdp->sd_rindex_vn != gl->gl_vn) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000574 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575 if (sdp->sd_rindex_vn != gl->gl_vn) {
576 error = gfs2_ri_update(ip);
577 if (error)
578 gfs2_glock_dq_uninit(ri_gh);
579 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000580 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581 }
582
583 return error;
584}
585
586/**
587 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
588 * @rgd: the struct gfs2_rgrpd describing the RG to read in
589 *
590 * Read in all of a Resource Group's header and bitmap blocks.
591 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
592 *
593 * Returns: errno
594 */
595
596int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
597{
598 struct gfs2_sbd *sdp = rgd->rd_sbd;
599 struct gfs2_glock *gl = rgd->rd_gl;
600 unsigned int length = rgd->rd_ri.ri_length;
601 struct gfs2_bitmap *bi;
602 unsigned int x, y;
603 int error;
604
Steven Whitehousef55ab262006-02-21 12:51:39 +0000605 mutex_lock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606
607 spin_lock(&sdp->sd_rindex_spin);
608 if (rgd->rd_bh_count) {
609 rgd->rd_bh_count++;
610 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000611 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 return 0;
613 }
614 spin_unlock(&sdp->sd_rindex_spin);
615
616 for (x = 0; x < length; x++) {
617 bi = rgd->rd_bits + x;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400618 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 if (error)
620 goto fail;
621 }
622
623 for (y = length; y--;) {
624 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400625 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626 if (error)
627 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400628 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 GFS2_METATYPE_RG)) {
630 error = -EIO;
631 goto fail;
632 }
633 }
634
635 if (rgd->rd_rg_vn != gl->gl_vn) {
636 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
637 rgd->rd_rg_vn = gl->gl_vn;
638 }
639
640 spin_lock(&sdp->sd_rindex_spin);
641 rgd->rd_free_clone = rgd->rd_rg.rg_free;
642 rgd->rd_bh_count++;
643 spin_unlock(&sdp->sd_rindex_spin);
644
Steven Whitehousef55ab262006-02-21 12:51:39 +0000645 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646
647 return 0;
648
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400649fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 while (x--) {
651 bi = rgd->rd_bits + x;
652 brelse(bi->bi_bh);
653 bi->bi_bh = NULL;
654 gfs2_assert_warn(sdp, !bi->bi_clone);
655 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000656 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657
658 return error;
659}
660
661void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
662{
663 struct gfs2_sbd *sdp = rgd->rd_sbd;
664
665 spin_lock(&sdp->sd_rindex_spin);
666 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
667 rgd->rd_bh_count++;
668 spin_unlock(&sdp->sd_rindex_spin);
669}
670
671/**
672 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
673 * @rgd: the struct gfs2_rgrpd describing the RG to read in
674 *
675 */
676
677void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
678{
679 struct gfs2_sbd *sdp = rgd->rd_sbd;
680 int x, length = rgd->rd_ri.ri_length;
681
682 spin_lock(&sdp->sd_rindex_spin);
683 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
684 if (--rgd->rd_bh_count) {
685 spin_unlock(&sdp->sd_rindex_spin);
686 return;
687 }
688
689 for (x = 0; x < length; x++) {
690 struct gfs2_bitmap *bi = rgd->rd_bits + x;
691 kfree(bi->bi_clone);
692 bi->bi_clone = NULL;
693 brelse(bi->bi_bh);
694 bi->bi_bh = NULL;
695 }
696
697 spin_unlock(&sdp->sd_rindex_spin);
698}
699
700void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
701{
702 struct gfs2_sbd *sdp = rgd->rd_sbd;
703 unsigned int length = rgd->rd_ri.ri_length;
704 unsigned int x;
705
706 for (x = 0; x < length; x++) {
707 struct gfs2_bitmap *bi = rgd->rd_bits + x;
708 if (!bi->bi_clone)
709 continue;
710 memcpy(bi->bi_clone + bi->bi_offset,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400711 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 }
713
714 spin_lock(&sdp->sd_rindex_spin);
715 rgd->rd_free_clone = rgd->rd_rg.rg_free;
716 spin_unlock(&sdp->sd_rindex_spin);
717}
718
719/**
720 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
721 * @ip: the incore GFS2 inode structure
722 *
723 * Returns: the struct gfs2_alloc
724 */
725
726struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
727{
728 struct gfs2_alloc *al = &ip->i_alloc;
729
730 /* FIXME: Should assert that the correct locks are held here... */
731 memset(al, 0, sizeof(*al));
732 return al;
733}
734
735/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 * try_rgrp_fit - See if a given reservation will fit in a given RG
737 * @rgd: the RG data
738 * @al: the struct gfs2_alloc structure describing the reservation
739 *
740 * If there's room for the requested blocks to be allocated from the RG:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 * Sets the $al_rgd field in @al.
742 *
743 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
744 */
745
746static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
747{
748 struct gfs2_sbd *sdp = rgd->rd_sbd;
749 int ret = 0;
750
Steven Whitehousea43a4902007-04-02 10:48:17 +0100751 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
752 return 0;
753
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 spin_lock(&sdp->sd_rindex_spin);
755 if (rgd->rd_free_clone >= al->al_requested) {
756 al->al_rgd = rgd;
757 ret = 1;
758 }
759 spin_unlock(&sdp->sd_rindex_spin);
760
761 return ret;
762}
763
764/**
765 * recent_rgrp_first - get first RG from "recent" list
766 * @sdp: The GFS2 superblock
767 * @rglast: address of the rgrp used last
768 *
769 * Returns: The first rgrp in the recent list
770 */
771
772static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
Steven Whitehousecd915492006-09-04 12:49:07 -0400773 u64 rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774{
775 struct gfs2_rgrpd *rgd = NULL;
776
777 spin_lock(&sdp->sd_rindex_spin);
778
779 if (list_empty(&sdp->sd_rindex_recent_list))
780 goto out;
781
782 if (!rglast)
783 goto first;
784
785 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
786 if (rgd->rd_ri.ri_addr == rglast)
787 goto out;
788 }
789
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400790first:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
792 rd_recent);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400793out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 return rgd;
796}
797
798/**
799 * recent_rgrp_next - get next RG from "recent" list
800 * @cur_rgd: current rgrp
801 * @remove:
802 *
803 * Returns: The next rgrp in the recent list
804 */
805
806static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
807 int remove)
808{
809 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
810 struct list_head *head;
811 struct gfs2_rgrpd *rgd;
812
813 spin_lock(&sdp->sd_rindex_spin);
814
815 head = &sdp->sd_rindex_recent_list;
816
817 list_for_each_entry(rgd, head, rd_recent) {
818 if (rgd == cur_rgd) {
819 if (cur_rgd->rd_recent.next != head)
820 rgd = list_entry(cur_rgd->rd_recent.next,
821 struct gfs2_rgrpd, rd_recent);
822 else
823 rgd = NULL;
824
825 if (remove)
826 list_del(&cur_rgd->rd_recent);
827
828 goto out;
829 }
830 }
831
832 rgd = NULL;
833 if (!list_empty(head))
834 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
835
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400836out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 return rgd;
839}
840
841/**
842 * recent_rgrp_add - add an RG to tail of "recent" list
843 * @new_rgd: The rgrp to add
844 *
845 */
846
847static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
848{
849 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
850 struct gfs2_rgrpd *rgd;
851 unsigned int count = 0;
852 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
853
854 spin_lock(&sdp->sd_rindex_spin);
855
856 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
857 if (rgd == new_rgd)
858 goto out;
859
860 if (++count >= max)
861 goto out;
862 }
863 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
864
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400865out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 spin_unlock(&sdp->sd_rindex_spin);
867}
868
869/**
870 * forward_rgrp_get - get an rgrp to try next from full list
871 * @sdp: The GFS2 superblock
872 *
873 * Returns: The rgrp to try next
874 */
875
876static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
877{
878 struct gfs2_rgrpd *rgd;
879 unsigned int journals = gfs2_jindex_size(sdp);
880 unsigned int rg = 0, x;
881
882 spin_lock(&sdp->sd_rindex_spin);
883
884 rgd = sdp->sd_rindex_forward;
885 if (!rgd) {
886 if (sdp->sd_rgrps >= journals)
887 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
888
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400889 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890 x++, rgd = gfs2_rgrpd_get_next(rgd))
891 /* Do Nothing */;
892
893 sdp->sd_rindex_forward = rgd;
894 }
895
896 spin_unlock(&sdp->sd_rindex_spin);
897
898 return rgd;
899}
900
901/**
902 * forward_rgrp_set - set the forward rgrp pointer
903 * @sdp: the filesystem
904 * @rgd: The new forward rgrp
905 *
906 */
907
908static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
909{
910 spin_lock(&sdp->sd_rindex_spin);
911 sdp->sd_rindex_forward = rgd;
912 spin_unlock(&sdp->sd_rindex_spin);
913}
914
915/**
916 * get_local_rgrp - Choose and lock a rgrp for allocation
917 * @ip: the inode to reserve space for
918 * @rgp: the chosen and locked rgrp
919 *
920 * Try to acquire rgrp in way which avoids contending with others.
921 *
922 * Returns: errno
923 */
924
925static int get_local_rgrp(struct gfs2_inode *ip)
926{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400927 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 struct gfs2_rgrpd *rgd, *begin = NULL;
929 struct gfs2_alloc *al = &ip->i_alloc;
930 int flags = LM_FLAG_TRY;
931 int skipped = 0;
932 int loops = 0;
933 int error;
934
935 /* Try recently successful rgrps */
936
937 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
938
939 while (rgd) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400940 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400941 LM_FLAG_TRY, &al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942 switch (error) {
943 case 0:
944 if (try_rgrp_fit(rgd, al))
945 goto out;
946 gfs2_glock_dq_uninit(&al->al_rgd_gh);
947 rgd = recent_rgrp_next(rgd, 1);
948 break;
949
950 case GLR_TRYFAILED:
951 rgd = recent_rgrp_next(rgd, 0);
952 break;
953
954 default:
955 return error;
956 }
957 }
958
959 /* Go through full list of rgrps */
960
961 begin = rgd = forward_rgrp_get(sdp);
962
963 for (;;) {
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400964 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 &al->al_rgd_gh);
966 switch (error) {
967 case 0:
968 if (try_rgrp_fit(rgd, al))
969 goto out;
970 gfs2_glock_dq_uninit(&al->al_rgd_gh);
971 break;
972
973 case GLR_TRYFAILED:
974 skipped++;
975 break;
976
977 default:
978 return error;
979 }
980
981 rgd = gfs2_rgrpd_get_next(rgd);
982 if (!rgd)
983 rgd = gfs2_rgrpd_get_first(sdp);
984
985 if (rgd == begin) {
Benjamin Marzinski172e0452007-03-23 14:51:56 -0600986 if (++loops >= 3)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 return -ENOSPC;
Benjamin Marzinski172e0452007-03-23 14:51:56 -0600988 if (!skipped)
989 loops++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 flags = 0;
Benjamin Marzinski172e0452007-03-23 14:51:56 -0600991 if (loops == 2)
992 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 }
994 }
995
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400996out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
998
999 if (begin) {
1000 recent_rgrp_add(rgd);
1001 rgd = gfs2_rgrpd_get_next(rgd);
1002 if (!rgd)
1003 rgd = gfs2_rgrpd_get_first(sdp);
1004 forward_rgrp_set(sdp, rgd);
1005 }
1006
1007 return 0;
1008}
1009
1010/**
1011 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1012 * @ip: the inode to reserve space for
1013 *
1014 * Returns: errno
1015 */
1016
1017int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1018{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001019 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 struct gfs2_alloc *al = &ip->i_alloc;
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001021 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022
1023 if (gfs2_assert_warn(sdp, al->al_requested))
1024 return -EINVAL;
1025
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001026 /* We need to hold the rindex unless the inode we're using is
1027 the rindex itself, in which case it's already held. */
1028 if (ip != GFS2_I(sdp->sd_rindex))
1029 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1030 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
1031 error = gfs2_ri_update(ip);
1032
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 if (error)
1034 return error;
1035
1036 error = get_local_rgrp(ip);
1037 if (error) {
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001038 if (ip != GFS2_I(sdp->sd_rindex))
1039 gfs2_glock_dq_uninit(&al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 return error;
1041 }
1042
1043 al->al_file = file;
1044 al->al_line = line;
1045
1046 return 0;
1047}
1048
1049/**
1050 * gfs2_inplace_release - release an inplace reservation
1051 * @ip: the inode the reservation was taken out on
1052 *
1053 * Release a reservation made by gfs2_inplace_reserve().
1054 */
1055
1056void gfs2_inplace_release(struct gfs2_inode *ip)
1057{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001058 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 struct gfs2_alloc *al = &ip->i_alloc;
1060
1061 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1062 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1063 "al_file = %s, al_line = %u\n",
1064 al->al_alloced, al->al_requested, al->al_file,
1065 al->al_line);
1066
1067 al->al_rgd = NULL;
1068 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001069 if (ip != GFS2_I(sdp->sd_rindex))
1070 gfs2_glock_dq_uninit(&al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071}
1072
1073/**
1074 * gfs2_get_block_type - Check a block in a RG is of given type
1075 * @rgd: the resource group holding the block
1076 * @block: the block number
1077 *
1078 * Returns: The block type (GFS2_BLKST_*)
1079 */
1080
Steven Whitehousecd915492006-09-04 12:49:07 -04001081unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082{
1083 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001084 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001085 unsigned int buf;
1086 unsigned char type;
1087
1088 length = rgd->rd_ri.ri_length;
1089 rgrp_block = block - rgd->rd_ri.ri_data0;
1090
1091 for (buf = 0; buf < length; buf++) {
1092 bi = rgd->rd_bits + buf;
1093 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1094 break;
1095 }
1096
1097 gfs2_assert(rgd->rd_sbd, buf < length);
1098 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1099
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001100 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 bi->bi_len, buf_block);
1102
1103 return type;
1104}
1105
1106/**
1107 * rgblk_search - find a block in @old_state, change allocation
1108 * state to @new_state
1109 * @rgd: the resource group descriptor
1110 * @goal: the goal block within the RG (start here to search for avail block)
1111 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1112 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1113 *
1114 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1115 * Add the found bitmap buffer to the transaction.
1116 * Set the found bits to @new_state to change block's allocation state.
1117 *
1118 * This function never fails, because we wouldn't call it unless we
1119 * know (from reservation results, etc.) that a block is available.
1120 *
1121 * Scope of @goal and returned block is just within rgrp, not the whole
1122 * filesystem.
1123 *
1124 * Returns: the block number allocated
1125 */
1126
Steven Whitehousecd915492006-09-04 12:49:07 -04001127static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128 unsigned char old_state, unsigned char new_state)
1129{
1130 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001131 u32 length = rgd->rd_ri.ri_length;
1132 u32 blk = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133 unsigned int buf, x;
1134
1135 /* Find bitmap block that contains bits for goal block */
1136 for (buf = 0; buf < length; buf++) {
1137 bi = rgd->rd_bits + buf;
1138 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1139 break;
1140 }
1141
1142 gfs2_assert(rgd->rd_sbd, buf < length);
1143
1144 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1145 goal -= bi->bi_start * GFS2_NBBY;
1146
1147 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1148 "x <= length", instead of "x < length", because we typically start
1149 the search in the middle of a bit block, but if we can't find an
1150 allocatable block anywhere else, we want to be able wrap around and
1151 search in the first part of our first-searched bit block. */
1152 for (x = 0; x <= length; x++) {
1153 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001154 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155 bi->bi_len, goal, old_state);
1156 else
1157 blk = gfs2_bitfit(rgd,
1158 bi->bi_bh->b_data + bi->bi_offset,
1159 bi->bi_len, goal, old_state);
1160 if (blk != BFITNOENT)
1161 break;
1162
1163 /* Try next bitmap block (wrap back to rgrp header if at end) */
1164 buf = (buf + 1) % length;
1165 bi = rgd->rd_bits + buf;
1166 goal = 0;
1167 }
1168
1169 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1170 blk = 0;
1171
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001172 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001173 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174 bi->bi_len, blk, new_state);
1175 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001176 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001177 bi->bi_len, blk, new_state);
1178
1179 return bi->bi_start * GFS2_NBBY + blk;
1180}
1181
1182/**
1183 * rgblk_free - Change alloc state of given block(s)
1184 * @sdp: the filesystem
1185 * @bstart: the start of a run of blocks to free
1186 * @blen: the length of the block run (all must lie within ONE RG!)
1187 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1188 *
1189 * Returns: Resource group containing the block(s)
1190 */
1191
Steven Whitehousecd915492006-09-04 12:49:07 -04001192static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1193 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194{
1195 struct gfs2_rgrpd *rgd;
1196 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001197 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198 unsigned int buf;
1199
1200 rgd = gfs2_blk2rgrpd(sdp, bstart);
1201 if (!rgd) {
1202 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001203 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 return NULL;
1205 }
1206
1207 length = rgd->rd_ri.ri_length;
1208
1209 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1210
1211 while (blen--) {
1212 for (buf = 0; buf < length; buf++) {
1213 bi = rgd->rd_bits + buf;
1214 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1215 break;
1216 }
1217
1218 gfs2_assert(rgd->rd_sbd, buf < length);
1219
1220 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1221 rgrp_blk++;
1222
1223 if (!bi->bi_clone) {
1224 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001225 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001226 memcpy(bi->bi_clone + bi->bi_offset,
1227 bi->bi_bh->b_data + bi->bi_offset,
1228 bi->bi_len);
1229 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001230 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousedd894be2006-07-27 14:29:00 -04001231 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232 bi->bi_len, buf_blk, new_state);
1233 }
1234
1235 return rgd;
1236}
1237
1238/**
1239 * gfs2_alloc_data - Allocate a data block
1240 * @ip: the inode to allocate the data block for
1241 *
1242 * Returns: the allocated block
1243 */
1244
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001245u64 gfs2_alloc_data(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001247 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248 struct gfs2_alloc *al = &ip->i_alloc;
1249 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001250 u32 goal, blk;
1251 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252
1253 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1254 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1255 else
1256 goal = rgd->rd_last_alloc_data;
1257
Steven Whitehousefd88de562006-05-05 16:59:11 -04001258 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001259 rgd->rd_last_alloc_data = blk;
1260
1261 block = rgd->rd_ri.ri_data0 + blk;
1262 ip->i_di.di_goal_data = block;
1263
1264 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1265 rgd->rd_rg.rg_free--;
1266
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001267 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1269
1270 al->al_alloced++;
1271
1272 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001273 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001274
1275 spin_lock(&sdp->sd_rindex_spin);
1276 rgd->rd_free_clone--;
1277 spin_unlock(&sdp->sd_rindex_spin);
1278
1279 return block;
1280}
1281
1282/**
1283 * gfs2_alloc_meta - Allocate a metadata block
1284 * @ip: the inode to allocate the metadata block for
1285 *
1286 * Returns: the allocated block
1287 */
1288
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001289u64 gfs2_alloc_meta(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001291 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292 struct gfs2_alloc *al = &ip->i_alloc;
1293 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001294 u32 goal, blk;
1295 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296
1297 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1298 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1299 else
1300 goal = rgd->rd_last_alloc_meta;
1301
Steven Whitehousefd88de562006-05-05 16:59:11 -04001302 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303 rgd->rd_last_alloc_meta = blk;
1304
1305 block = rgd->rd_ri.ri_data0 + blk;
1306 ip->i_di.di_goal_meta = block;
1307
1308 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1309 rgd->rd_rg.rg_free--;
1310
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001311 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1313
1314 al->al_alloced++;
1315
1316 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001317 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318 gfs2_trans_add_unrevoke(sdp, block);
1319
1320 spin_lock(&sdp->sd_rindex_spin);
1321 rgd->rd_free_clone--;
1322 spin_unlock(&sdp->sd_rindex_spin);
1323
1324 return block;
1325}
1326
1327/**
1328 * gfs2_alloc_di - Allocate a dinode
1329 * @dip: the directory that the inode is going in
1330 *
1331 * Returns: the block allocated
1332 */
1333
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001334u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001336 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337 struct gfs2_alloc *al = &dip->i_alloc;
1338 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001339 u32 blk;
1340 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001341
1342 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1343 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1344
1345 rgd->rd_last_alloc_meta = blk;
1346
1347 block = rgd->rd_ri.ri_data0 + blk;
1348
1349 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1350 rgd->rd_rg.rg_free--;
1351 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001352 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001353 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1355
1356 al->al_alloced++;
1357
1358 gfs2_statfs_change(sdp, 0, -1, +1);
1359 gfs2_trans_add_unrevoke(sdp, block);
1360
1361 spin_lock(&sdp->sd_rindex_spin);
1362 rgd->rd_free_clone--;
1363 spin_unlock(&sdp->sd_rindex_spin);
1364
1365 return block;
1366}
1367
1368/**
1369 * gfs2_free_data - free a contiguous run of data block(s)
1370 * @ip: the inode these blocks are being freed from
1371 * @bstart: first block of a run of contiguous blocks
1372 * @blen: the length of the block run
1373 *
1374 */
1375
Steven Whitehousecd915492006-09-04 12:49:07 -04001376void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001377{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001378 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001379 struct gfs2_rgrpd *rgd;
1380
1381 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1382 if (!rgd)
1383 return;
1384
1385 rgd->rd_rg.rg_free += blen;
1386
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001387 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1389
1390 gfs2_trans_add_rg(rgd);
1391
1392 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001393 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394}
1395
1396/**
1397 * gfs2_free_meta - free a contiguous run of data block(s)
1398 * @ip: the inode these blocks are being freed from
1399 * @bstart: first block of a run of contiguous blocks
1400 * @blen: the length of the block run
1401 *
1402 */
1403
Steven Whitehousecd915492006-09-04 12:49:07 -04001404void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001405{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001406 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001407 struct gfs2_rgrpd *rgd;
1408
1409 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1410 if (!rgd)
1411 return;
1412
1413 rgd->rd_rg.rg_free += blen;
1414
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001415 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001416 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1417
1418 gfs2_trans_add_rg(rgd);
1419
1420 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001421 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422 gfs2_meta_wipe(ip, bstart, blen);
1423}
1424
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001425void gfs2_unlink_di(struct inode *inode)
1426{
1427 struct gfs2_inode *ip = GFS2_I(inode);
1428 struct gfs2_sbd *sdp = GFS2_SB(inode);
1429 struct gfs2_rgrpd *rgd;
1430 u64 blkno = ip->i_num.no_addr;
1431
1432 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1433 if (!rgd)
1434 return;
1435 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1436 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1437 gfs2_trans_add_rg(rgd);
1438}
1439
Steven Whitehousecd915492006-09-04 12:49:07 -04001440static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001441{
1442 struct gfs2_sbd *sdp = rgd->rd_sbd;
1443 struct gfs2_rgrpd *tmp_rgd;
1444
1445 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1446 if (!tmp_rgd)
1447 return;
1448 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1449
1450 if (!rgd->rd_rg.rg_dinodes)
1451 gfs2_consist_rgrpd(rgd);
1452 rgd->rd_rg.rg_dinodes--;
1453 rgd->rd_rg.rg_free++;
1454
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001455 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1457
1458 gfs2_statfs_change(sdp, 0, +1, -1);
1459 gfs2_trans_add_rg(rgd);
1460}
1461
David Teiglandb3b94fa2006-01-16 16:50:04 +00001462
1463void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1464{
1465 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001466 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001467 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1468}
1469
1470/**
1471 * gfs2_rlist_add - add a RG to a list of RGs
1472 * @sdp: the filesystem
1473 * @rlist: the list of resource groups
1474 * @block: the block
1475 *
1476 * Figure out what RG a block belongs to and add that RG to the list
1477 *
1478 * FIXME: Don't use NOFAIL
1479 *
1480 */
1481
1482void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001483 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001484{
1485 struct gfs2_rgrpd *rgd;
1486 struct gfs2_rgrpd **tmp;
1487 unsigned int new_space;
1488 unsigned int x;
1489
1490 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1491 return;
1492
1493 rgd = gfs2_blk2rgrpd(sdp, block);
1494 if (!rgd) {
1495 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001496 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001497 return;
1498 }
1499
1500 for (x = 0; x < rlist->rl_rgrps; x++)
1501 if (rlist->rl_rgd[x] == rgd)
1502 return;
1503
1504 if (rlist->rl_rgrps == rlist->rl_space) {
1505 new_space = rlist->rl_space + 10;
1506
1507 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001508 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001509
1510 if (rlist->rl_rgd) {
1511 memcpy(tmp, rlist->rl_rgd,
1512 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1513 kfree(rlist->rl_rgd);
1514 }
1515
1516 rlist->rl_space = new_space;
1517 rlist->rl_rgd = tmp;
1518 }
1519
1520 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1521}
1522
1523/**
1524 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1525 * and initialize an array of glock holders for them
1526 * @rlist: the list of resource groups
1527 * @state: the lock state to acquire the RG lock in
1528 * @flags: the modifier flags for the holder structures
1529 *
1530 * FIXME: Don't use NOFAIL
1531 *
1532 */
1533
1534void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1535 int flags)
1536{
1537 unsigned int x;
1538
1539 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001540 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541 for (x = 0; x < rlist->rl_rgrps; x++)
1542 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1543 state, flags,
1544 &rlist->rl_ghs[x]);
1545}
1546
1547/**
1548 * gfs2_rlist_free - free a resource group list
1549 * @list: the list of resource groups
1550 *
1551 */
1552
1553void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1554{
1555 unsigned int x;
1556
1557 kfree(rlist->rl_rgd);
1558
1559 if (rlist->rl_ghs) {
1560 for (x = 0; x < rlist->rl_rgrps; x++)
1561 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1562 kfree(rlist->rl_ghs);
1563 }
1564}
1565