blob: 2ce48d4f24651920f9603097d2fed3d4e1bb1c47 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 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/**
434 * gfs2_ri_update - Pull in a new resource index from the disk
435 * @gl: The glock covering the rindex inode
436 *
437 * Returns: 0 on successful update, error code otherwise
438 */
439
440static int gfs2_ri_update(struct gfs2_inode *ip)
441{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400442 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
443 struct inode *inode = &ip->i_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 struct gfs2_rgrpd *rgd;
445 char buf[sizeof(struct gfs2_rindex)];
Steven Whitehousef42faf42006-01-30 18:34:10 +0000446 struct file_ra_state ra_state;
Steven Whitehousecd915492006-09-04 12:49:07 -0400447 u64 junk = ip->i_di.di_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448 int error;
449
450 if (do_div(junk, sizeof(struct gfs2_rindex))) {
451 gfs2_consist_inode(ip);
452 return -EIO;
453 }
454
455 clear_rgrpdi(sdp);
456
Steven Whitehousef42faf42006-01-30 18:34:10 +0000457 file_ra_state_init(&ra_state, inode->i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000459 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
460 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461 sizeof(struct gfs2_rindex));
462 if (!error)
463 break;
464 if (error != sizeof(struct gfs2_rindex)) {
465 if (error > 0)
466 error = -EIO;
467 goto fail;
468 }
469
Steven Whitehousedd894be2006-07-27 14:29:00 -0400470 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 error = -ENOMEM;
472 if (!rgd)
473 goto fail;
474
Steven Whitehousef55ab262006-02-21 12:51:39 +0000475 mutex_init(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
477 rgd->rd_sbd = sdp;
478
479 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
480 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
481
482 gfs2_rindex_in(&rgd->rd_ri, buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483 error = compute_bitstructs(rgd);
484 if (error)
485 goto fail;
486
487 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
488 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
489 if (error)
490 goto fail;
491
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500492 rgd->rd_gl->gl_object = rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
494 }
495
496 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 return 0;
498
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400499fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500 clear_rgrpdi(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501 return error;
502}
503
504/**
505 * gfs2_rindex_hold - Grab a lock on the rindex
506 * @sdp: The GFS2 superblock
507 * @ri_gh: the glock holder
508 *
509 * We grab a lock on the rindex inode to make sure that it doesn't
510 * change whilst we are performing an operation. We keep this lock
511 * for quite long periods of time compared to other locks. This
512 * doesn't matter, since it is shared and it is very, very rarely
513 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
514 *
515 * This makes sure that we're using the latest copy of the resource index
516 * special file, which might have been updated if someone expanded the
517 * filesystem (via gfs2_grow utility), which adds new resource groups.
518 *
519 * Returns: 0 on success, error code otherwise
520 */
521
522int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
523{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400524 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 struct gfs2_glock *gl = ip->i_gl;
526 int error;
527
528 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
529 if (error)
530 return error;
531
532 /* Read new copy from disk if we don't have the latest */
533 if (sdp->sd_rindex_vn != gl->gl_vn) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000534 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535 if (sdp->sd_rindex_vn != gl->gl_vn) {
536 error = gfs2_ri_update(ip);
537 if (error)
538 gfs2_glock_dq_uninit(ri_gh);
539 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000540 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 }
542
543 return error;
544}
545
546/**
547 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
548 * @rgd: the struct gfs2_rgrpd describing the RG to read in
549 *
550 * Read in all of a Resource Group's header and bitmap blocks.
551 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
552 *
553 * Returns: errno
554 */
555
556int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
557{
558 struct gfs2_sbd *sdp = rgd->rd_sbd;
559 struct gfs2_glock *gl = rgd->rd_gl;
560 unsigned int length = rgd->rd_ri.ri_length;
561 struct gfs2_bitmap *bi;
562 unsigned int x, y;
563 int error;
564
Steven Whitehousef55ab262006-02-21 12:51:39 +0000565 mutex_lock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566
567 spin_lock(&sdp->sd_rindex_spin);
568 if (rgd->rd_bh_count) {
569 rgd->rd_bh_count++;
570 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000571 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 return 0;
573 }
574 spin_unlock(&sdp->sd_rindex_spin);
575
576 for (x = 0; x < length; x++) {
577 bi = rgd->rd_bits + x;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400578 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 if (error)
580 goto fail;
581 }
582
583 for (y = length; y--;) {
584 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400585 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 if (error)
587 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400588 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 GFS2_METATYPE_RG)) {
590 error = -EIO;
591 goto fail;
592 }
593 }
594
595 if (rgd->rd_rg_vn != gl->gl_vn) {
596 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
597 rgd->rd_rg_vn = gl->gl_vn;
598 }
599
600 spin_lock(&sdp->sd_rindex_spin);
601 rgd->rd_free_clone = rgd->rd_rg.rg_free;
602 rgd->rd_bh_count++;
603 spin_unlock(&sdp->sd_rindex_spin);
604
Steven Whitehousef55ab262006-02-21 12:51:39 +0000605 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606
607 return 0;
608
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400609fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 while (x--) {
611 bi = rgd->rd_bits + x;
612 brelse(bi->bi_bh);
613 bi->bi_bh = NULL;
614 gfs2_assert_warn(sdp, !bi->bi_clone);
615 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000616 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617
618 return error;
619}
620
621void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
622{
623 struct gfs2_sbd *sdp = rgd->rd_sbd;
624
625 spin_lock(&sdp->sd_rindex_spin);
626 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
627 rgd->rd_bh_count++;
628 spin_unlock(&sdp->sd_rindex_spin);
629}
630
631/**
632 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
633 * @rgd: the struct gfs2_rgrpd describing the RG to read in
634 *
635 */
636
637void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
638{
639 struct gfs2_sbd *sdp = rgd->rd_sbd;
640 int x, length = rgd->rd_ri.ri_length;
641
642 spin_lock(&sdp->sd_rindex_spin);
643 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
644 if (--rgd->rd_bh_count) {
645 spin_unlock(&sdp->sd_rindex_spin);
646 return;
647 }
648
649 for (x = 0; x < length; x++) {
650 struct gfs2_bitmap *bi = rgd->rd_bits + x;
651 kfree(bi->bi_clone);
652 bi->bi_clone = NULL;
653 brelse(bi->bi_bh);
654 bi->bi_bh = NULL;
655 }
656
657 spin_unlock(&sdp->sd_rindex_spin);
658}
659
660void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
661{
662 struct gfs2_sbd *sdp = rgd->rd_sbd;
663 unsigned int length = rgd->rd_ri.ri_length;
664 unsigned int x;
665
666 for (x = 0; x < length; x++) {
667 struct gfs2_bitmap *bi = rgd->rd_bits + x;
668 if (!bi->bi_clone)
669 continue;
670 memcpy(bi->bi_clone + bi->bi_offset,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400671 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672 }
673
674 spin_lock(&sdp->sd_rindex_spin);
675 rgd->rd_free_clone = rgd->rd_rg.rg_free;
676 spin_unlock(&sdp->sd_rindex_spin);
677}
678
679/**
680 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
681 * @ip: the incore GFS2 inode structure
682 *
683 * Returns: the struct gfs2_alloc
684 */
685
686struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
687{
688 struct gfs2_alloc *al = &ip->i_alloc;
689
690 /* FIXME: Should assert that the correct locks are held here... */
691 memset(al, 0, sizeof(*al));
692 return al;
693}
694
695/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696 * try_rgrp_fit - See if a given reservation will fit in a given RG
697 * @rgd: the RG data
698 * @al: the struct gfs2_alloc structure describing the reservation
699 *
700 * If there's room for the requested blocks to be allocated from the RG:
701 * Sets the $al_reserved_data field in @al.
702 * Sets the $al_reserved_meta field in @al.
703 * Sets the $al_rgd field in @al.
704 *
705 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
706 */
707
708static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
709{
710 struct gfs2_sbd *sdp = rgd->rd_sbd;
711 int ret = 0;
712
713 spin_lock(&sdp->sd_rindex_spin);
714 if (rgd->rd_free_clone >= al->al_requested) {
715 al->al_rgd = rgd;
716 ret = 1;
717 }
718 spin_unlock(&sdp->sd_rindex_spin);
719
720 return ret;
721}
722
723/**
724 * recent_rgrp_first - get first RG from "recent" list
725 * @sdp: The GFS2 superblock
726 * @rglast: address of the rgrp used last
727 *
728 * Returns: The first rgrp in the recent list
729 */
730
731static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
Steven Whitehousecd915492006-09-04 12:49:07 -0400732 u64 rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733{
734 struct gfs2_rgrpd *rgd = NULL;
735
736 spin_lock(&sdp->sd_rindex_spin);
737
738 if (list_empty(&sdp->sd_rindex_recent_list))
739 goto out;
740
741 if (!rglast)
742 goto first;
743
744 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
745 if (rgd->rd_ri.ri_addr == rglast)
746 goto out;
747 }
748
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400749first:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
751 rd_recent);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400752out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 return rgd;
755}
756
757/**
758 * recent_rgrp_next - get next RG from "recent" list
759 * @cur_rgd: current rgrp
760 * @remove:
761 *
762 * Returns: The next rgrp in the recent list
763 */
764
765static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
766 int remove)
767{
768 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
769 struct list_head *head;
770 struct gfs2_rgrpd *rgd;
771
772 spin_lock(&sdp->sd_rindex_spin);
773
774 head = &sdp->sd_rindex_recent_list;
775
776 list_for_each_entry(rgd, head, rd_recent) {
777 if (rgd == cur_rgd) {
778 if (cur_rgd->rd_recent.next != head)
779 rgd = list_entry(cur_rgd->rd_recent.next,
780 struct gfs2_rgrpd, rd_recent);
781 else
782 rgd = NULL;
783
784 if (remove)
785 list_del(&cur_rgd->rd_recent);
786
787 goto out;
788 }
789 }
790
791 rgd = NULL;
792 if (!list_empty(head))
793 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
794
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400795out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 return rgd;
798}
799
800/**
801 * recent_rgrp_add - add an RG to tail of "recent" list
802 * @new_rgd: The rgrp to add
803 *
804 */
805
806static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
807{
808 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
809 struct gfs2_rgrpd *rgd;
810 unsigned int count = 0;
811 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
812
813 spin_lock(&sdp->sd_rindex_spin);
814
815 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
816 if (rgd == new_rgd)
817 goto out;
818
819 if (++count >= max)
820 goto out;
821 }
822 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
823
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400824out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 spin_unlock(&sdp->sd_rindex_spin);
826}
827
828/**
829 * forward_rgrp_get - get an rgrp to try next from full list
830 * @sdp: The GFS2 superblock
831 *
832 * Returns: The rgrp to try next
833 */
834
835static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
836{
837 struct gfs2_rgrpd *rgd;
838 unsigned int journals = gfs2_jindex_size(sdp);
839 unsigned int rg = 0, x;
840
841 spin_lock(&sdp->sd_rindex_spin);
842
843 rgd = sdp->sd_rindex_forward;
844 if (!rgd) {
845 if (sdp->sd_rgrps >= journals)
846 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
847
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400848 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 x++, rgd = gfs2_rgrpd_get_next(rgd))
850 /* Do Nothing */;
851
852 sdp->sd_rindex_forward = rgd;
853 }
854
855 spin_unlock(&sdp->sd_rindex_spin);
856
857 return rgd;
858}
859
860/**
861 * forward_rgrp_set - set the forward rgrp pointer
862 * @sdp: the filesystem
863 * @rgd: The new forward rgrp
864 *
865 */
866
867static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
868{
869 spin_lock(&sdp->sd_rindex_spin);
870 sdp->sd_rindex_forward = rgd;
871 spin_unlock(&sdp->sd_rindex_spin);
872}
873
874/**
875 * get_local_rgrp - Choose and lock a rgrp for allocation
876 * @ip: the inode to reserve space for
877 * @rgp: the chosen and locked rgrp
878 *
879 * Try to acquire rgrp in way which avoids contending with others.
880 *
881 * Returns: errno
882 */
883
884static int get_local_rgrp(struct gfs2_inode *ip)
885{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400886 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 struct gfs2_rgrpd *rgd, *begin = NULL;
888 struct gfs2_alloc *al = &ip->i_alloc;
889 int flags = LM_FLAG_TRY;
890 int skipped = 0;
891 int loops = 0;
892 int error;
893
894 /* Try recently successful rgrps */
895
896 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
897
898 while (rgd) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400899 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400900 LM_FLAG_TRY, &al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 switch (error) {
902 case 0:
903 if (try_rgrp_fit(rgd, al))
904 goto out;
905 gfs2_glock_dq_uninit(&al->al_rgd_gh);
906 rgd = recent_rgrp_next(rgd, 1);
907 break;
908
909 case GLR_TRYFAILED:
910 rgd = recent_rgrp_next(rgd, 0);
911 break;
912
913 default:
914 return error;
915 }
916 }
917
918 /* Go through full list of rgrps */
919
920 begin = rgd = forward_rgrp_get(sdp);
921
922 for (;;) {
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400923 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 &al->al_rgd_gh);
925 switch (error) {
926 case 0:
927 if (try_rgrp_fit(rgd, al))
928 goto out;
929 gfs2_glock_dq_uninit(&al->al_rgd_gh);
930 break;
931
932 case GLR_TRYFAILED:
933 skipped++;
934 break;
935
936 default:
937 return error;
938 }
939
940 rgd = gfs2_rgrpd_get_next(rgd);
941 if (!rgd)
942 rgd = gfs2_rgrpd_get_first(sdp);
943
944 if (rgd == begin) {
Benjamin Marzinski172e0452007-03-23 14:51:56 -0600945 if (++loops >= 3)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 return -ENOSPC;
Benjamin Marzinski172e0452007-03-23 14:51:56 -0600947 if (!skipped)
948 loops++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 flags = 0;
Benjamin Marzinski172e0452007-03-23 14:51:56 -0600950 if (loops == 2)
951 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 }
953 }
954
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400955out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
957
958 if (begin) {
959 recent_rgrp_add(rgd);
960 rgd = gfs2_rgrpd_get_next(rgd);
961 if (!rgd)
962 rgd = gfs2_rgrpd_get_first(sdp);
963 forward_rgrp_set(sdp, rgd);
964 }
965
966 return 0;
967}
968
969/**
970 * gfs2_inplace_reserve_i - Reserve space in the filesystem
971 * @ip: the inode to reserve space for
972 *
973 * Returns: errno
974 */
975
976int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
977{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400978 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 struct gfs2_alloc *al = &ip->i_alloc;
980 int error;
981
982 if (gfs2_assert_warn(sdp, al->al_requested))
983 return -EINVAL;
984
985 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
986 if (error)
987 return error;
988
989 error = get_local_rgrp(ip);
990 if (error) {
991 gfs2_glock_dq_uninit(&al->al_ri_gh);
992 return error;
993 }
994
995 al->al_file = file;
996 al->al_line = line;
997
998 return 0;
999}
1000
1001/**
1002 * gfs2_inplace_release - release an inplace reservation
1003 * @ip: the inode the reservation was taken out on
1004 *
1005 * Release a reservation made by gfs2_inplace_reserve().
1006 */
1007
1008void gfs2_inplace_release(struct gfs2_inode *ip)
1009{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001010 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011 struct gfs2_alloc *al = &ip->i_alloc;
1012
1013 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1014 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1015 "al_file = %s, al_line = %u\n",
1016 al->al_alloced, al->al_requested, al->al_file,
1017 al->al_line);
1018
1019 al->al_rgd = NULL;
1020 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1021 gfs2_glock_dq_uninit(&al->al_ri_gh);
1022}
1023
1024/**
1025 * gfs2_get_block_type - Check a block in a RG is of given type
1026 * @rgd: the resource group holding the block
1027 * @block: the block number
1028 *
1029 * Returns: The block type (GFS2_BLKST_*)
1030 */
1031
Steven Whitehousecd915492006-09-04 12:49:07 -04001032unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033{
1034 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001035 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 unsigned int buf;
1037 unsigned char type;
1038
1039 length = rgd->rd_ri.ri_length;
1040 rgrp_block = block - rgd->rd_ri.ri_data0;
1041
1042 for (buf = 0; buf < length; buf++) {
1043 bi = rgd->rd_bits + buf;
1044 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1045 break;
1046 }
1047
1048 gfs2_assert(rgd->rd_sbd, buf < length);
1049 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1050
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001051 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 bi->bi_len, buf_block);
1053
1054 return type;
1055}
1056
1057/**
1058 * rgblk_search - find a block in @old_state, change allocation
1059 * state to @new_state
1060 * @rgd: the resource group descriptor
1061 * @goal: the goal block within the RG (start here to search for avail block)
1062 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1063 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1064 *
1065 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1066 * Add the found bitmap buffer to the transaction.
1067 * Set the found bits to @new_state to change block's allocation state.
1068 *
1069 * This function never fails, because we wouldn't call it unless we
1070 * know (from reservation results, etc.) that a block is available.
1071 *
1072 * Scope of @goal and returned block is just within rgrp, not the whole
1073 * filesystem.
1074 *
1075 * Returns: the block number allocated
1076 */
1077
Steven Whitehousecd915492006-09-04 12:49:07 -04001078static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 unsigned char old_state, unsigned char new_state)
1080{
1081 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001082 u32 length = rgd->rd_ri.ri_length;
1083 u32 blk = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 unsigned int buf, x;
1085
1086 /* Find bitmap block that contains bits for goal block */
1087 for (buf = 0; buf < length; buf++) {
1088 bi = rgd->rd_bits + buf;
1089 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1090 break;
1091 }
1092
1093 gfs2_assert(rgd->rd_sbd, buf < length);
1094
1095 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1096 goal -= bi->bi_start * GFS2_NBBY;
1097
1098 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1099 "x <= length", instead of "x < length", because we typically start
1100 the search in the middle of a bit block, but if we can't find an
1101 allocatable block anywhere else, we want to be able wrap around and
1102 search in the first part of our first-searched bit block. */
1103 for (x = 0; x <= length; x++) {
1104 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001105 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 bi->bi_len, goal, old_state);
1107 else
1108 blk = gfs2_bitfit(rgd,
1109 bi->bi_bh->b_data + bi->bi_offset,
1110 bi->bi_len, goal, old_state);
1111 if (blk != BFITNOENT)
1112 break;
1113
1114 /* Try next bitmap block (wrap back to rgrp header if at end) */
1115 buf = (buf + 1) % length;
1116 bi = rgd->rd_bits + buf;
1117 goal = 0;
1118 }
1119
1120 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1121 blk = 0;
1122
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001123 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001124 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 bi->bi_len, blk, new_state);
1126 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001127 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128 bi->bi_len, blk, new_state);
1129
1130 return bi->bi_start * GFS2_NBBY + blk;
1131}
1132
1133/**
1134 * rgblk_free - Change alloc state of given block(s)
1135 * @sdp: the filesystem
1136 * @bstart: the start of a run of blocks to free
1137 * @blen: the length of the block run (all must lie within ONE RG!)
1138 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1139 *
1140 * Returns: Resource group containing the block(s)
1141 */
1142
Steven Whitehousecd915492006-09-04 12:49:07 -04001143static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1144 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145{
1146 struct gfs2_rgrpd *rgd;
1147 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001148 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 unsigned int buf;
1150
1151 rgd = gfs2_blk2rgrpd(sdp, bstart);
1152 if (!rgd) {
1153 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001154 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155 return NULL;
1156 }
1157
1158 length = rgd->rd_ri.ri_length;
1159
1160 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1161
1162 while (blen--) {
1163 for (buf = 0; buf < length; buf++) {
1164 bi = rgd->rd_bits + buf;
1165 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1166 break;
1167 }
1168
1169 gfs2_assert(rgd->rd_sbd, buf < length);
1170
1171 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1172 rgrp_blk++;
1173
1174 if (!bi->bi_clone) {
1175 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001176 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001177 memcpy(bi->bi_clone + bi->bi_offset,
1178 bi->bi_bh->b_data + bi->bi_offset,
1179 bi->bi_len);
1180 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001181 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousedd894be2006-07-27 14:29:00 -04001182 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183 bi->bi_len, buf_blk, new_state);
1184 }
1185
1186 return rgd;
1187}
1188
1189/**
1190 * gfs2_alloc_data - Allocate a data block
1191 * @ip: the inode to allocate the data block for
1192 *
1193 * Returns: the allocated block
1194 */
1195
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001196u64 gfs2_alloc_data(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001198 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199 struct gfs2_alloc *al = &ip->i_alloc;
1200 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001201 u32 goal, blk;
1202 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203
1204 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1205 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1206 else
1207 goal = rgd->rd_last_alloc_data;
1208
Steven Whitehousefd88de562006-05-05 16:59:11 -04001209 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001210 rgd->rd_last_alloc_data = blk;
1211
1212 block = rgd->rd_ri.ri_data0 + blk;
1213 ip->i_di.di_goal_data = block;
1214
1215 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1216 rgd->rd_rg.rg_free--;
1217
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001218 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001219 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1220
1221 al->al_alloced++;
1222
1223 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001224 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225
1226 spin_lock(&sdp->sd_rindex_spin);
1227 rgd->rd_free_clone--;
1228 spin_unlock(&sdp->sd_rindex_spin);
1229
1230 return block;
1231}
1232
1233/**
1234 * gfs2_alloc_meta - Allocate a metadata block
1235 * @ip: the inode to allocate the metadata block for
1236 *
1237 * Returns: the allocated block
1238 */
1239
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001240u64 gfs2_alloc_meta(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001242 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243 struct gfs2_alloc *al = &ip->i_alloc;
1244 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001245 u32 goal, blk;
1246 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247
1248 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1249 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1250 else
1251 goal = rgd->rd_last_alloc_meta;
1252
Steven Whitehousefd88de562006-05-05 16:59:11 -04001253 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254 rgd->rd_last_alloc_meta = blk;
1255
1256 block = rgd->rd_ri.ri_data0 + blk;
1257 ip->i_di.di_goal_meta = block;
1258
1259 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1260 rgd->rd_rg.rg_free--;
1261
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001262 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1264
1265 al->al_alloced++;
1266
1267 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001268 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001269 gfs2_trans_add_unrevoke(sdp, block);
1270
1271 spin_lock(&sdp->sd_rindex_spin);
1272 rgd->rd_free_clone--;
1273 spin_unlock(&sdp->sd_rindex_spin);
1274
1275 return block;
1276}
1277
1278/**
1279 * gfs2_alloc_di - Allocate a dinode
1280 * @dip: the directory that the inode is going in
1281 *
1282 * Returns: the block allocated
1283 */
1284
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001285u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001287 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288 struct gfs2_alloc *al = &dip->i_alloc;
1289 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001290 u32 blk;
1291 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292
1293 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1294 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1295
1296 rgd->rd_last_alloc_meta = blk;
1297
1298 block = rgd->rd_ri.ri_data0 + blk;
1299
1300 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1301 rgd->rd_rg.rg_free--;
1302 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001303 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001304 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001305 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1306
1307 al->al_alloced++;
1308
1309 gfs2_statfs_change(sdp, 0, -1, +1);
1310 gfs2_trans_add_unrevoke(sdp, block);
1311
1312 spin_lock(&sdp->sd_rindex_spin);
1313 rgd->rd_free_clone--;
1314 spin_unlock(&sdp->sd_rindex_spin);
1315
1316 return block;
1317}
1318
1319/**
1320 * gfs2_free_data - free a contiguous run of data block(s)
1321 * @ip: the inode these blocks are being freed from
1322 * @bstart: first block of a run of contiguous blocks
1323 * @blen: the length of the block run
1324 *
1325 */
1326
Steven Whitehousecd915492006-09-04 12:49:07 -04001327void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001328{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001329 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330 struct gfs2_rgrpd *rgd;
1331
1332 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1333 if (!rgd)
1334 return;
1335
1336 rgd->rd_rg.rg_free += blen;
1337
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001338 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1340
1341 gfs2_trans_add_rg(rgd);
1342
1343 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001344 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001345}
1346
1347/**
1348 * gfs2_free_meta - free a contiguous run of data block(s)
1349 * @ip: the inode these blocks are being freed from
1350 * @bstart: first block of a run of contiguous blocks
1351 * @blen: the length of the block run
1352 *
1353 */
1354
Steven Whitehousecd915492006-09-04 12:49:07 -04001355void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001356{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001357 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001358 struct gfs2_rgrpd *rgd;
1359
1360 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1361 if (!rgd)
1362 return;
1363
1364 rgd->rd_rg.rg_free += blen;
1365
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001366 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1368
1369 gfs2_trans_add_rg(rgd);
1370
1371 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001372 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373 gfs2_meta_wipe(ip, bstart, blen);
1374}
1375
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001376void gfs2_unlink_di(struct inode *inode)
1377{
1378 struct gfs2_inode *ip = GFS2_I(inode);
1379 struct gfs2_sbd *sdp = GFS2_SB(inode);
1380 struct gfs2_rgrpd *rgd;
1381 u64 blkno = ip->i_num.no_addr;
1382
1383 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1384 if (!rgd)
1385 return;
1386 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1387 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1388 gfs2_trans_add_rg(rgd);
1389}
1390
Steven Whitehousecd915492006-09-04 12:49:07 -04001391static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392{
1393 struct gfs2_sbd *sdp = rgd->rd_sbd;
1394 struct gfs2_rgrpd *tmp_rgd;
1395
1396 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1397 if (!tmp_rgd)
1398 return;
1399 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1400
1401 if (!rgd->rd_rg.rg_dinodes)
1402 gfs2_consist_rgrpd(rgd);
1403 rgd->rd_rg.rg_dinodes--;
1404 rgd->rd_rg.rg_free++;
1405
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001406 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001407 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1408
1409 gfs2_statfs_change(sdp, 0, +1, -1);
1410 gfs2_trans_add_rg(rgd);
1411}
1412
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413
1414void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1415{
1416 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001417 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001418 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1419}
1420
1421/**
1422 * gfs2_rlist_add - add a RG to a list of RGs
1423 * @sdp: the filesystem
1424 * @rlist: the list of resource groups
1425 * @block: the block
1426 *
1427 * Figure out what RG a block belongs to and add that RG to the list
1428 *
1429 * FIXME: Don't use NOFAIL
1430 *
1431 */
1432
1433void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001434 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001435{
1436 struct gfs2_rgrpd *rgd;
1437 struct gfs2_rgrpd **tmp;
1438 unsigned int new_space;
1439 unsigned int x;
1440
1441 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1442 return;
1443
1444 rgd = gfs2_blk2rgrpd(sdp, block);
1445 if (!rgd) {
1446 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001447 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448 return;
1449 }
1450
1451 for (x = 0; x < rlist->rl_rgrps; x++)
1452 if (rlist->rl_rgd[x] == rgd)
1453 return;
1454
1455 if (rlist->rl_rgrps == rlist->rl_space) {
1456 new_space = rlist->rl_space + 10;
1457
1458 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001459 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001460
1461 if (rlist->rl_rgd) {
1462 memcpy(tmp, rlist->rl_rgd,
1463 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1464 kfree(rlist->rl_rgd);
1465 }
1466
1467 rlist->rl_space = new_space;
1468 rlist->rl_rgd = tmp;
1469 }
1470
1471 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1472}
1473
1474/**
1475 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1476 * and initialize an array of glock holders for them
1477 * @rlist: the list of resource groups
1478 * @state: the lock state to acquire the RG lock in
1479 * @flags: the modifier flags for the holder structures
1480 *
1481 * FIXME: Don't use NOFAIL
1482 *
1483 */
1484
1485void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1486 int flags)
1487{
1488 unsigned int x;
1489
1490 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001491 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492 for (x = 0; x < rlist->rl_rgrps; x++)
1493 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1494 state, flags,
1495 &rlist->rl_ghs[x]);
1496}
1497
1498/**
1499 * gfs2_rlist_free - free a resource group list
1500 * @list: the list of resource groups
1501 *
1502 */
1503
1504void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1505{
1506 unsigned int x;
1507
1508 kfree(rlist->rl_rgd);
1509
1510 if (rlist->rl_ghs) {
1511 for (x = 0; x < rlist->rl_rgrps; x++)
1512 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1513 kfree(rlist->rl_ghs);
1514 }
1515}
1516