blob: 8d9c08b5c4b600bab2bee1cf635bb9849847740e [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"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
Steven Whitehouse2c1e52a2006-09-05 15:41:57 -040031#define BFITNOENT ((u32)~0)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040032
33/*
34 * These routines are used by the resource group routines (rgrp.c)
35 * to keep track of block allocation. Each block is represented by two
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040036 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
37 *
38 * 0 = Free
39 * 1 = Used (not metadata)
40 * 2 = Unlinked (still in use) inode
41 * 3 = Used (metadata)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040042 */
43
44static const char valid_change[16] = {
45 /* current */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040046 /* n */ 0, 1, 1, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040047 /* e */ 1, 0, 0, 0,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040048 /* w */ 0, 0, 0, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040049 1, 0, 0, 0
50};
51
52/**
53 * gfs2_setbit - Set a bit in the bitmaps
54 * @buffer: the buffer that holds the bitmaps
55 * @buflen: the length (in bytes) of the buffer
56 * @block: the block to set
57 * @new_state: the new state of the block
58 *
59 */
60
Steven Whitehouse3efd7532006-05-18 14:02:52 -040061static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040062 unsigned int buflen, u32 block,
Steven Whitehouse3efd7532006-05-18 14:02:52 -040063 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040064{
65 unsigned char *byte, *end, cur_state;
66 unsigned int bit;
67
68 byte = buffer + (block / GFS2_NBBY);
69 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
70 end = buffer + buflen;
71
72 gfs2_assert(rgd->rd_sbd, byte < end);
73
74 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
75
76 if (valid_change[new_state * 4 + cur_state]) {
77 *byte ^= cur_state << bit;
78 *byte |= new_state << bit;
79 } else
80 gfs2_consist_rgrpd(rgd);
81}
82
83/**
84 * gfs2_testbit - test a bit in the bitmaps
85 * @buffer: the buffer that holds the bitmaps
86 * @buflen: the length (in bytes) of the buffer
87 * @block: the block to read
88 *
89 */
90
Steven Whitehouse3efd7532006-05-18 14:02:52 -040091static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040092 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040093{
94 unsigned char *byte, *end, cur_state;
95 unsigned int bit;
96
97 byte = buffer + (block / GFS2_NBBY);
98 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
99 end = buffer + buflen;
100
101 gfs2_assert(rgd->rd_sbd, byte < end);
102
103 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
104
105 return cur_state;
106}
107
108/**
109 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
110 * a block in a given allocation state.
111 * @buffer: the buffer that holds the bitmaps
112 * @buflen: the length (in bytes) of the buffer
113 * @goal: start search at this block's bit-pair (within @buffer)
114 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
115 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
116 *
117 * Scope of @goal and returned block number is only within this bitmap buffer,
118 * not entire rgrp or filesystem. @buffer will be offset from the actual
119 * beginning of a bitmap block buffer, skipping any header structures.
120 *
121 * Return: the block number (bitmap buffer scope) that was found
122 */
123
Steven Whitehousecd915492006-09-04 12:49:07 -0400124static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
125 unsigned int buflen, u32 goal,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400126 unsigned char old_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400127{
128 unsigned char *byte, *end, alloc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400129 u32 blk = goal;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400130 unsigned int bit;
131
132 byte = buffer + (goal / GFS2_NBBY);
133 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
134 end = buffer + buflen;
135 alloc = (old_state & 1) ? 0 : 0x55;
136
137 while (byte < end) {
138 if ((*byte & 0x55) == alloc) {
139 blk += (8 - bit) >> 1;
140
141 bit = 0;
142 byte++;
143
144 continue;
145 }
146
147 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
148 return blk;
149
150 bit += GFS2_BIT_SIZE;
151 if (bit >= 8) {
152 bit = 0;
153 byte++;
154 }
155
156 blk++;
157 }
158
159 return BFITNOENT;
160}
161
162/**
163 * gfs2_bitcount - count the number of bits in a certain state
164 * @buffer: the buffer that holds the bitmaps
165 * @buflen: the length (in bytes) of the buffer
166 * @state: the state of the block we're looking for
167 *
168 * Returns: The number of bits
169 */
170
Steven Whitehousecd915492006-09-04 12:49:07 -0400171static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400172 unsigned int buflen, unsigned char state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400173{
174 unsigned char *byte = buffer;
175 unsigned char *end = buffer + buflen;
176 unsigned char state1 = state << 2;
177 unsigned char state2 = state << 4;
178 unsigned char state3 = state << 6;
Steven Whitehousecd915492006-09-04 12:49:07 -0400179 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400180
181 for (; byte < end; byte++) {
182 if (((*byte) & 0x03) == state)
183 count++;
184 if (((*byte) & 0x0C) == state1)
185 count++;
186 if (((*byte) & 0x30) == state2)
187 count++;
188 if (((*byte) & 0xC0) == state3)
189 count++;
190 }
191
192 return count;
193}
194
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195/**
196 * gfs2_rgrp_verify - Verify that a resource group is consistent
197 * @sdp: the filesystem
198 * @rgd: the rgrp
199 *
200 */
201
202void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
203{
204 struct gfs2_sbd *sdp = rgd->rd_sbd;
205 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -0400206 u32 length = rgd->rd_ri.ri_length;
207 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 int buf, x;
209
Steven Whitehousecd915492006-09-04 12:49:07 -0400210 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211
212 /* Count # blocks in each of 4 possible allocation states */
213 for (buf = 0; buf < length; buf++) {
214 bi = rgd->rd_bits + buf;
215 for (x = 0; x < 4; x++)
216 count[x] += gfs2_bitcount(rgd,
217 bi->bi_bh->b_data +
218 bi->bi_offset,
219 bi->bi_len, x);
220 }
221
222 if (count[0] != rgd->rd_rg.rg_free) {
223 if (gfs2_consist_rgrpd(rgd))
224 fs_err(sdp, "free data mismatch: %u != %u\n",
225 count[0], rgd->rd_rg.rg_free);
226 return;
227 }
228
229 tmp = rgd->rd_ri.ri_data -
230 rgd->rd_rg.rg_free -
231 rgd->rd_rg.rg_dinodes;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400232 if (count[1] + count[2] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233 if (gfs2_consist_rgrpd(rgd))
234 fs_err(sdp, "used data mismatch: %u != %u\n",
235 count[1], tmp);
236 return;
237 }
238
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239 if (count[3] != rgd->rd_rg.rg_dinodes) {
240 if (gfs2_consist_rgrpd(rgd))
241 fs_err(sdp, "used metadata mismatch: %u != %u\n",
242 count[3], rgd->rd_rg.rg_dinodes);
243 return;
244 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400245
246 if (count[2] > count[3]) {
247 if (gfs2_consist_rgrpd(rgd))
248 fs_err(sdp, "unlinked inodes > inodes: %u\n",
249 count[2]);
250 return;
251 }
252
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253}
254
Al Viro1e81c4c2006-10-13 22:51:24 -0400255static inline int rgrp_contains_block(struct gfs2_rindex_host *ri, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256{
Steven Whitehousecd915492006-09-04 12:49:07 -0400257 u64 first = ri->ri_data0;
258 u64 last = first + ri->ri_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400259 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260}
261
262/**
263 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
264 * @sdp: The GFS2 superblock
265 * @n: The data block number
266 *
267 * Returns: The resource group, or NULL if not found
268 */
269
Steven Whitehousecd915492006-09-04 12:49:07 -0400270struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271{
272 struct gfs2_rgrpd *rgd;
273
274 spin_lock(&sdp->sd_rindex_spin);
275
276 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
277 if (rgrp_contains_block(&rgd->rd_ri, blk)) {
278 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
279 spin_unlock(&sdp->sd_rindex_spin);
280 return rgd;
281 }
282 }
283
284 spin_unlock(&sdp->sd_rindex_spin);
285
286 return NULL;
287}
288
289/**
290 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
291 * @sdp: The GFS2 superblock
292 *
293 * Returns: The first rgrp in the filesystem
294 */
295
296struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
297{
298 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
299 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
300}
301
302/**
303 * gfs2_rgrpd_get_next - get the next RG
304 * @rgd: A RG
305 *
306 * Returns: The next rgrp
307 */
308
309struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
310{
311 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
312 return NULL;
313 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
314}
315
316static void clear_rgrpdi(struct gfs2_sbd *sdp)
317{
318 struct list_head *head;
319 struct gfs2_rgrpd *rgd;
320 struct gfs2_glock *gl;
321
322 spin_lock(&sdp->sd_rindex_spin);
323 sdp->sd_rindex_forward = NULL;
324 head = &sdp->sd_rindex_recent_list;
325 while (!list_empty(head)) {
326 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
327 list_del(&rgd->rd_recent);
328 }
329 spin_unlock(&sdp->sd_rindex_spin);
330
331 head = &sdp->sd_rindex_list;
332 while (!list_empty(head)) {
333 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
334 gl = rgd->rd_gl;
335
336 list_del(&rgd->rd_list);
337 list_del(&rgd->rd_list_mru);
338
339 if (gl) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500340 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 gfs2_glock_put(gl);
342 }
343
344 kfree(rgd->rd_bits);
345 kfree(rgd);
346 }
347}
348
349void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
350{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000351 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 clear_rgrpdi(sdp);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000353 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354}
355
356/**
357 * gfs2_compute_bitstructs - Compute the bitmap sizes
358 * @rgd: The resource group descriptor
359 *
360 * Calculates bitmap descriptors, one for each block that contains bitmap data
361 *
362 * Returns: errno
363 */
364
365static int compute_bitstructs(struct gfs2_rgrpd *rgd)
366{
367 struct gfs2_sbd *sdp = rgd->rd_sbd;
368 struct gfs2_bitmap *bi;
Steven Whitehousecd915492006-09-04 12:49:07 -0400369 u32 length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
370 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 int x;
372
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400373 if (!length)
374 return -EINVAL;
375
Steven Whitehousedd894be2006-07-27 14:29:00 -0400376 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000377 if (!rgd->rd_bits)
378 return -ENOMEM;
379
380 bytes_left = rgd->rd_ri.ri_bitbytes;
381
382 for (x = 0; x < length; x++) {
383 bi = rgd->rd_bits + x;
384
385 /* small rgrp; bitmap stored completely in header block */
386 if (length == 1) {
387 bytes = bytes_left;
388 bi->bi_offset = sizeof(struct gfs2_rgrp);
389 bi->bi_start = 0;
390 bi->bi_len = bytes;
391 /* header block */
392 } else if (x == 0) {
393 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
394 bi->bi_offset = sizeof(struct gfs2_rgrp);
395 bi->bi_start = 0;
396 bi->bi_len = bytes;
397 /* last block */
398 } else if (x + 1 == length) {
399 bytes = bytes_left;
400 bi->bi_offset = sizeof(struct gfs2_meta_header);
401 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
402 bi->bi_len = bytes;
403 /* other blocks */
404 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500405 bytes = sdp->sd_sb.sb_bsize -
406 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 bi->bi_offset = sizeof(struct gfs2_meta_header);
408 bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
409 bi->bi_len = bytes;
410 }
411
412 bytes_left -= bytes;
413 }
414
415 if (bytes_left) {
416 gfs2_consist_rgrpd(rgd);
417 return -EIO;
418 }
419 bi = rgd->rd_bits + (length - 1);
420 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
421 if (gfs2_consist_rgrpd(rgd)) {
422 gfs2_rindex_print(&rgd->rd_ri);
423 fs_err(sdp, "start=%u len=%u offset=%u\n",
424 bi->bi_start, bi->bi_len, bi->bi_offset);
425 }
426 return -EIO;
427 }
428
429 return 0;
430}
431
432/**
433 * gfs2_ri_update - Pull in a new resource index from the disk
434 * @gl: The glock covering the rindex inode
435 *
436 * Returns: 0 on successful update, error code otherwise
437 */
438
439static int gfs2_ri_update(struct gfs2_inode *ip)
440{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
442 struct inode *inode = &ip->i_inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443 struct gfs2_rgrpd *rgd;
444 char buf[sizeof(struct gfs2_rindex)];
Steven Whitehousef42faf42006-01-30 18:34:10 +0000445 struct file_ra_state ra_state;
Steven Whitehousecd915492006-09-04 12:49:07 -0400446 u64 junk = ip->i_di.di_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447 int error;
448
449 if (do_div(junk, sizeof(struct gfs2_rindex))) {
450 gfs2_consist_inode(ip);
451 return -EIO;
452 }
453
454 clear_rgrpdi(sdp);
455
Steven Whitehousef42faf42006-01-30 18:34:10 +0000456 file_ra_state_init(&ra_state, inode->i_mapping);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
Steven Whitehousef42faf42006-01-30 18:34:10 +0000458 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
459 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 sizeof(struct gfs2_rindex));
461 if (!error)
462 break;
463 if (error != sizeof(struct gfs2_rindex)) {
464 if (error > 0)
465 error = -EIO;
466 goto fail;
467 }
468
Steven Whitehousedd894be2006-07-27 14:29:00 -0400469 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000470 error = -ENOMEM;
471 if (!rgd)
472 goto fail;
473
Steven Whitehousef55ab262006-02-21 12:51:39 +0000474 mutex_init(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
476 rgd->rd_sbd = sdp;
477
478 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
479 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
480
481 gfs2_rindex_in(&rgd->rd_ri, buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482 error = compute_bitstructs(rgd);
483 if (error)
484 goto fail;
485
486 error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
487 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
488 if (error)
489 goto fail;
490
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500491 rgd->rd_gl->gl_object = rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
493 }
494
495 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496 return 0;
497
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400498fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 clear_rgrpdi(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500 return error;
501}
502
503/**
504 * gfs2_rindex_hold - Grab a lock on the rindex
505 * @sdp: The GFS2 superblock
506 * @ri_gh: the glock holder
507 *
508 * We grab a lock on the rindex inode to make sure that it doesn't
509 * change whilst we are performing an operation. We keep this lock
510 * for quite long periods of time compared to other locks. This
511 * doesn't matter, since it is shared and it is very, very rarely
512 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
513 *
514 * This makes sure that we're using the latest copy of the resource index
515 * special file, which might have been updated if someone expanded the
516 * filesystem (via gfs2_grow utility), which adds new resource groups.
517 *
518 * Returns: 0 on success, error code otherwise
519 */
520
521int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
522{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400523 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 struct gfs2_glock *gl = ip->i_gl;
525 int error;
526
527 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
528 if (error)
529 return error;
530
531 /* Read new copy from disk if we don't have the latest */
532 if (sdp->sd_rindex_vn != gl->gl_vn) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000533 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 if (sdp->sd_rindex_vn != gl->gl_vn) {
535 error = gfs2_ri_update(ip);
536 if (error)
537 gfs2_glock_dq_uninit(ri_gh);
538 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000539 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 }
541
542 return error;
543}
544
545/**
546 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
547 * @rgd: the struct gfs2_rgrpd describing the RG to read in
548 *
549 * Read in all of a Resource Group's header and bitmap blocks.
550 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
551 *
552 * Returns: errno
553 */
554
555int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
556{
557 struct gfs2_sbd *sdp = rgd->rd_sbd;
558 struct gfs2_glock *gl = rgd->rd_gl;
559 unsigned int length = rgd->rd_ri.ri_length;
560 struct gfs2_bitmap *bi;
561 unsigned int x, y;
562 int error;
563
Steven Whitehousef55ab262006-02-21 12:51:39 +0000564 mutex_lock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565
566 spin_lock(&sdp->sd_rindex_spin);
567 if (rgd->rd_bh_count) {
568 rgd->rd_bh_count++;
569 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000570 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571 return 0;
572 }
573 spin_unlock(&sdp->sd_rindex_spin);
574
575 for (x = 0; x < length; x++) {
576 bi = rgd->rd_bits + x;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400577 error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 if (error)
579 goto fail;
580 }
581
582 for (y = length; y--;) {
583 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400584 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 if (error)
586 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400587 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588 GFS2_METATYPE_RG)) {
589 error = -EIO;
590 goto fail;
591 }
592 }
593
594 if (rgd->rd_rg_vn != gl->gl_vn) {
595 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
596 rgd->rd_rg_vn = gl->gl_vn;
597 }
598
599 spin_lock(&sdp->sd_rindex_spin);
600 rgd->rd_free_clone = rgd->rd_rg.rg_free;
601 rgd->rd_bh_count++;
602 spin_unlock(&sdp->sd_rindex_spin);
603
Steven Whitehousef55ab262006-02-21 12:51:39 +0000604 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605
606 return 0;
607
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400608fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609 while (x--) {
610 bi = rgd->rd_bits + x;
611 brelse(bi->bi_bh);
612 bi->bi_bh = NULL;
613 gfs2_assert_warn(sdp, !bi->bi_clone);
614 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000615 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616
617 return error;
618}
619
620void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
621{
622 struct gfs2_sbd *sdp = rgd->rd_sbd;
623
624 spin_lock(&sdp->sd_rindex_spin);
625 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
626 rgd->rd_bh_count++;
627 spin_unlock(&sdp->sd_rindex_spin);
628}
629
630/**
631 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
632 * @rgd: the struct gfs2_rgrpd describing the RG to read in
633 *
634 */
635
636void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
637{
638 struct gfs2_sbd *sdp = rgd->rd_sbd;
639 int x, length = rgd->rd_ri.ri_length;
640
641 spin_lock(&sdp->sd_rindex_spin);
642 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
643 if (--rgd->rd_bh_count) {
644 spin_unlock(&sdp->sd_rindex_spin);
645 return;
646 }
647
648 for (x = 0; x < length; x++) {
649 struct gfs2_bitmap *bi = rgd->rd_bits + x;
650 kfree(bi->bi_clone);
651 bi->bi_clone = NULL;
652 brelse(bi->bi_bh);
653 bi->bi_bh = NULL;
654 }
655
656 spin_unlock(&sdp->sd_rindex_spin);
657}
658
659void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
660{
661 struct gfs2_sbd *sdp = rgd->rd_sbd;
662 unsigned int length = rgd->rd_ri.ri_length;
663 unsigned int x;
664
665 for (x = 0; x < length; x++) {
666 struct gfs2_bitmap *bi = rgd->rd_bits + x;
667 if (!bi->bi_clone)
668 continue;
669 memcpy(bi->bi_clone + bi->bi_offset,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400670 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 }
672
673 spin_lock(&sdp->sd_rindex_spin);
674 rgd->rd_free_clone = rgd->rd_rg.rg_free;
675 spin_unlock(&sdp->sd_rindex_spin);
676}
677
678/**
679 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
680 * @ip: the incore GFS2 inode structure
681 *
682 * Returns: the struct gfs2_alloc
683 */
684
685struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
686{
687 struct gfs2_alloc *al = &ip->i_alloc;
688
689 /* FIXME: Should assert that the correct locks are held here... */
690 memset(al, 0, sizeof(*al));
691 return al;
692}
693
694/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695 * try_rgrp_fit - See if a given reservation will fit in a given RG
696 * @rgd: the RG data
697 * @al: the struct gfs2_alloc structure describing the reservation
698 *
699 * If there's room for the requested blocks to be allocated from the RG:
700 * Sets the $al_reserved_data field in @al.
701 * Sets the $al_reserved_meta field in @al.
702 * Sets the $al_rgd field in @al.
703 *
704 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
705 */
706
707static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
708{
709 struct gfs2_sbd *sdp = rgd->rd_sbd;
710 int ret = 0;
711
712 spin_lock(&sdp->sd_rindex_spin);
713 if (rgd->rd_free_clone >= al->al_requested) {
714 al->al_rgd = rgd;
715 ret = 1;
716 }
717 spin_unlock(&sdp->sd_rindex_spin);
718
719 return ret;
720}
721
722/**
723 * recent_rgrp_first - get first RG from "recent" list
724 * @sdp: The GFS2 superblock
725 * @rglast: address of the rgrp used last
726 *
727 * Returns: The first rgrp in the recent list
728 */
729
730static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
Steven Whitehousecd915492006-09-04 12:49:07 -0400731 u64 rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732{
733 struct gfs2_rgrpd *rgd = NULL;
734
735 spin_lock(&sdp->sd_rindex_spin);
736
737 if (list_empty(&sdp->sd_rindex_recent_list))
738 goto out;
739
740 if (!rglast)
741 goto first;
742
743 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
744 if (rgd->rd_ri.ri_addr == rglast)
745 goto out;
746 }
747
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400748first:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
750 rd_recent);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400751out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 return rgd;
754}
755
756/**
757 * recent_rgrp_next - get next RG from "recent" list
758 * @cur_rgd: current rgrp
759 * @remove:
760 *
761 * Returns: The next rgrp in the recent list
762 */
763
764static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
765 int remove)
766{
767 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
768 struct list_head *head;
769 struct gfs2_rgrpd *rgd;
770
771 spin_lock(&sdp->sd_rindex_spin);
772
773 head = &sdp->sd_rindex_recent_list;
774
775 list_for_each_entry(rgd, head, rd_recent) {
776 if (rgd == cur_rgd) {
777 if (cur_rgd->rd_recent.next != head)
778 rgd = list_entry(cur_rgd->rd_recent.next,
779 struct gfs2_rgrpd, rd_recent);
780 else
781 rgd = NULL;
782
783 if (remove)
784 list_del(&cur_rgd->rd_recent);
785
786 goto out;
787 }
788 }
789
790 rgd = NULL;
791 if (!list_empty(head))
792 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
793
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400794out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 return rgd;
797}
798
799/**
800 * recent_rgrp_add - add an RG to tail of "recent" list
801 * @new_rgd: The rgrp to add
802 *
803 */
804
805static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
806{
807 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
808 struct gfs2_rgrpd *rgd;
809 unsigned int count = 0;
810 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
811
812 spin_lock(&sdp->sd_rindex_spin);
813
814 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
815 if (rgd == new_rgd)
816 goto out;
817
818 if (++count >= max)
819 goto out;
820 }
821 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
822
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400823out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824 spin_unlock(&sdp->sd_rindex_spin);
825}
826
827/**
828 * forward_rgrp_get - get an rgrp to try next from full list
829 * @sdp: The GFS2 superblock
830 *
831 * Returns: The rgrp to try next
832 */
833
834static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
835{
836 struct gfs2_rgrpd *rgd;
837 unsigned int journals = gfs2_jindex_size(sdp);
838 unsigned int rg = 0, x;
839
840 spin_lock(&sdp->sd_rindex_spin);
841
842 rgd = sdp->sd_rindex_forward;
843 if (!rgd) {
844 if (sdp->sd_rgrps >= journals)
845 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
846
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400847 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 x++, rgd = gfs2_rgrpd_get_next(rgd))
849 /* Do Nothing */;
850
851 sdp->sd_rindex_forward = rgd;
852 }
853
854 spin_unlock(&sdp->sd_rindex_spin);
855
856 return rgd;
857}
858
859/**
860 * forward_rgrp_set - set the forward rgrp pointer
861 * @sdp: the filesystem
862 * @rgd: The new forward rgrp
863 *
864 */
865
866static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
867{
868 spin_lock(&sdp->sd_rindex_spin);
869 sdp->sd_rindex_forward = rgd;
870 spin_unlock(&sdp->sd_rindex_spin);
871}
872
873/**
874 * get_local_rgrp - Choose and lock a rgrp for allocation
875 * @ip: the inode to reserve space for
876 * @rgp: the chosen and locked rgrp
877 *
878 * Try to acquire rgrp in way which avoids contending with others.
879 *
880 * Returns: errno
881 */
882
883static int get_local_rgrp(struct gfs2_inode *ip)
884{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400885 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886 struct gfs2_rgrpd *rgd, *begin = NULL;
887 struct gfs2_alloc *al = &ip->i_alloc;
888 int flags = LM_FLAG_TRY;
889 int skipped = 0;
890 int loops = 0;
891 int error;
892
893 /* Try recently successful rgrps */
894
895 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
896
897 while (rgd) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400898 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400899 LM_FLAG_TRY, &al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 switch (error) {
901 case 0:
902 if (try_rgrp_fit(rgd, al))
903 goto out;
904 gfs2_glock_dq_uninit(&al->al_rgd_gh);
905 rgd = recent_rgrp_next(rgd, 1);
906 break;
907
908 case GLR_TRYFAILED:
909 rgd = recent_rgrp_next(rgd, 0);
910 break;
911
912 default:
913 return error;
914 }
915 }
916
917 /* Go through full list of rgrps */
918
919 begin = rgd = forward_rgrp_get(sdp);
920
921 for (;;) {
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400922 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923 &al->al_rgd_gh);
924 switch (error) {
925 case 0:
926 if (try_rgrp_fit(rgd, al))
927 goto out;
928 gfs2_glock_dq_uninit(&al->al_rgd_gh);
929 break;
930
931 case GLR_TRYFAILED:
932 skipped++;
933 break;
934
935 default:
936 return error;
937 }
938
939 rgd = gfs2_rgrpd_get_next(rgd);
940 if (!rgd)
941 rgd = gfs2_rgrpd_get_first(sdp);
942
943 if (rgd == begin) {
944 if (++loops >= 2 || !skipped)
945 return -ENOSPC;
946 flags = 0;
947 }
948 }
949
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400950out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
952
953 if (begin) {
954 recent_rgrp_add(rgd);
955 rgd = gfs2_rgrpd_get_next(rgd);
956 if (!rgd)
957 rgd = gfs2_rgrpd_get_first(sdp);
958 forward_rgrp_set(sdp, rgd);
959 }
960
961 return 0;
962}
963
964/**
965 * gfs2_inplace_reserve_i - Reserve space in the filesystem
966 * @ip: the inode to reserve space for
967 *
968 * Returns: errno
969 */
970
971int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
972{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400973 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 struct gfs2_alloc *al = &ip->i_alloc;
975 int error;
976
977 if (gfs2_assert_warn(sdp, al->al_requested))
978 return -EINVAL;
979
980 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
981 if (error)
982 return error;
983
984 error = get_local_rgrp(ip);
985 if (error) {
986 gfs2_glock_dq_uninit(&al->al_ri_gh);
987 return error;
988 }
989
990 al->al_file = file;
991 al->al_line = line;
992
993 return 0;
994}
995
996/**
997 * gfs2_inplace_release - release an inplace reservation
998 * @ip: the inode the reservation was taken out on
999 *
1000 * Release a reservation made by gfs2_inplace_reserve().
1001 */
1002
1003void gfs2_inplace_release(struct gfs2_inode *ip)
1004{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001005 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006 struct gfs2_alloc *al = &ip->i_alloc;
1007
1008 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1009 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1010 "al_file = %s, al_line = %u\n",
1011 al->al_alloced, al->al_requested, al->al_file,
1012 al->al_line);
1013
1014 al->al_rgd = NULL;
1015 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1016 gfs2_glock_dq_uninit(&al->al_ri_gh);
1017}
1018
1019/**
1020 * gfs2_get_block_type - Check a block in a RG is of given type
1021 * @rgd: the resource group holding the block
1022 * @block: the block number
1023 *
1024 * Returns: The block type (GFS2_BLKST_*)
1025 */
1026
Steven Whitehousecd915492006-09-04 12:49:07 -04001027unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028{
1029 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001030 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 unsigned int buf;
1032 unsigned char type;
1033
1034 length = rgd->rd_ri.ri_length;
1035 rgrp_block = block - rgd->rd_ri.ri_data0;
1036
1037 for (buf = 0; buf < length; buf++) {
1038 bi = rgd->rd_bits + buf;
1039 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1040 break;
1041 }
1042
1043 gfs2_assert(rgd->rd_sbd, buf < length);
1044 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1045
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001046 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047 bi->bi_len, buf_block);
1048
1049 return type;
1050}
1051
1052/**
1053 * rgblk_search - find a block in @old_state, change allocation
1054 * state to @new_state
1055 * @rgd: the resource group descriptor
1056 * @goal: the goal block within the RG (start here to search for avail block)
1057 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1058 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1059 *
1060 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1061 * Add the found bitmap buffer to the transaction.
1062 * Set the found bits to @new_state to change block's allocation state.
1063 *
1064 * This function never fails, because we wouldn't call it unless we
1065 * know (from reservation results, etc.) that a block is available.
1066 *
1067 * Scope of @goal and returned block is just within rgrp, not the whole
1068 * filesystem.
1069 *
1070 * Returns: the block number allocated
1071 */
1072
Steven Whitehousecd915492006-09-04 12:49:07 -04001073static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074 unsigned char old_state, unsigned char new_state)
1075{
1076 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001077 u32 length = rgd->rd_ri.ri_length;
1078 u32 blk = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 unsigned int buf, x;
1080
1081 /* Find bitmap block that contains bits for goal block */
1082 for (buf = 0; buf < length; buf++) {
1083 bi = rgd->rd_bits + buf;
1084 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1085 break;
1086 }
1087
1088 gfs2_assert(rgd->rd_sbd, buf < length);
1089
1090 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1091 goal -= bi->bi_start * GFS2_NBBY;
1092
1093 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1094 "x <= length", instead of "x < length", because we typically start
1095 the search in the middle of a bit block, but if we can't find an
1096 allocatable block anywhere else, we want to be able wrap around and
1097 search in the first part of our first-searched bit block. */
1098 for (x = 0; x <= length; x++) {
1099 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001100 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 bi->bi_len, goal, old_state);
1102 else
1103 blk = gfs2_bitfit(rgd,
1104 bi->bi_bh->b_data + bi->bi_offset,
1105 bi->bi_len, goal, old_state);
1106 if (blk != BFITNOENT)
1107 break;
1108
1109 /* Try next bitmap block (wrap back to rgrp header if at end) */
1110 buf = (buf + 1) % length;
1111 bi = rgd->rd_bits + buf;
1112 goal = 0;
1113 }
1114
1115 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1116 blk = 0;
1117
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001118 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001119 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 bi->bi_len, blk, new_state);
1121 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001122 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 bi->bi_len, blk, new_state);
1124
1125 return bi->bi_start * GFS2_NBBY + blk;
1126}
1127
1128/**
1129 * rgblk_free - Change alloc state of given block(s)
1130 * @sdp: the filesystem
1131 * @bstart: the start of a run of blocks to free
1132 * @blen: the length of the block run (all must lie within ONE RG!)
1133 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1134 *
1135 * Returns: Resource group containing the block(s)
1136 */
1137
Steven Whitehousecd915492006-09-04 12:49:07 -04001138static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1139 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140{
1141 struct gfs2_rgrpd *rgd;
1142 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001143 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144 unsigned int buf;
1145
1146 rgd = gfs2_blk2rgrpd(sdp, bstart);
1147 if (!rgd) {
1148 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001149 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150 return NULL;
1151 }
1152
1153 length = rgd->rd_ri.ri_length;
1154
1155 rgrp_blk = bstart - rgd->rd_ri.ri_data0;
1156
1157 while (blen--) {
1158 for (buf = 0; buf < length; buf++) {
1159 bi = rgd->rd_bits + buf;
1160 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1161 break;
1162 }
1163
1164 gfs2_assert(rgd->rd_sbd, buf < length);
1165
1166 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1167 rgrp_blk++;
1168
1169 if (!bi->bi_clone) {
1170 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001171 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172 memcpy(bi->bi_clone + bi->bi_offset,
1173 bi->bi_bh->b_data + bi->bi_offset,
1174 bi->bi_len);
1175 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001176 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousedd894be2006-07-27 14:29:00 -04001177 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001178 bi->bi_len, buf_blk, new_state);
1179 }
1180
1181 return rgd;
1182}
1183
1184/**
1185 * gfs2_alloc_data - Allocate a data block
1186 * @ip: the inode to allocate the data block for
1187 *
1188 * Returns: the allocated block
1189 */
1190
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001191u64 gfs2_alloc_data(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001192{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001193 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 struct gfs2_alloc *al = &ip->i_alloc;
1195 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001196 u32 goal, blk;
1197 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198
1199 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
1200 goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
1201 else
1202 goal = rgd->rd_last_alloc_data;
1203
Steven Whitehousefd88de562006-05-05 16:59:11 -04001204 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205 rgd->rd_last_alloc_data = blk;
1206
1207 block = rgd->rd_ri.ri_data0 + blk;
1208 ip->i_di.di_goal_data = block;
1209
1210 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1211 rgd->rd_rg.rg_free--;
1212
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001213 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001214 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1215
1216 al->al_alloced++;
1217
1218 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001219 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001220
1221 spin_lock(&sdp->sd_rindex_spin);
1222 rgd->rd_free_clone--;
1223 spin_unlock(&sdp->sd_rindex_spin);
1224
1225 return block;
1226}
1227
1228/**
1229 * gfs2_alloc_meta - Allocate a metadata block
1230 * @ip: the inode to allocate the metadata block for
1231 *
1232 * Returns: the allocated block
1233 */
1234
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001235u64 gfs2_alloc_meta(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001236{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001237 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238 struct gfs2_alloc *al = &ip->i_alloc;
1239 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001240 u32 goal, blk;
1241 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001242
1243 if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
1244 goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
1245 else
1246 goal = rgd->rd_last_alloc_meta;
1247
Steven Whitehousefd88de562006-05-05 16:59:11 -04001248 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249 rgd->rd_last_alloc_meta = blk;
1250
1251 block = rgd->rd_ri.ri_data0 + blk;
1252 ip->i_di.di_goal_meta = block;
1253
1254 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1255 rgd->rd_rg.rg_free--;
1256
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001257 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1259
1260 al->al_alloced++;
1261
1262 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001263 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264 gfs2_trans_add_unrevoke(sdp, block);
1265
1266 spin_lock(&sdp->sd_rindex_spin);
1267 rgd->rd_free_clone--;
1268 spin_unlock(&sdp->sd_rindex_spin);
1269
1270 return block;
1271}
1272
1273/**
1274 * gfs2_alloc_di - Allocate a dinode
1275 * @dip: the directory that the inode is going in
1276 *
1277 * Returns: the block allocated
1278 */
1279
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001280u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001282 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283 struct gfs2_alloc *al = &dip->i_alloc;
1284 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001285 u32 blk;
1286 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287
1288 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1289 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1290
1291 rgd->rd_last_alloc_meta = blk;
1292
1293 block = rgd->rd_ri.ri_data0 + blk;
1294
1295 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1296 rgd->rd_rg.rg_free--;
1297 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001298 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001299 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1301
1302 al->al_alloced++;
1303
1304 gfs2_statfs_change(sdp, 0, -1, +1);
1305 gfs2_trans_add_unrevoke(sdp, block);
1306
1307 spin_lock(&sdp->sd_rindex_spin);
1308 rgd->rd_free_clone--;
1309 spin_unlock(&sdp->sd_rindex_spin);
1310
1311 return block;
1312}
1313
1314/**
1315 * gfs2_free_data - free a contiguous run of data block(s)
1316 * @ip: the inode these blocks are being freed from
1317 * @bstart: first block of a run of contiguous blocks
1318 * @blen: the length of the block run
1319 *
1320 */
1321
Steven Whitehousecd915492006-09-04 12:49:07 -04001322void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001324 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 struct gfs2_rgrpd *rgd;
1326
1327 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1328 if (!rgd)
1329 return;
1330
1331 rgd->rd_rg.rg_free += blen;
1332
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001333 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1335
1336 gfs2_trans_add_rg(rgd);
1337
1338 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001339 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340}
1341
1342/**
1343 * gfs2_free_meta - free a contiguous run of data block(s)
1344 * @ip: the inode these blocks are being freed from
1345 * @bstart: first block of a run of contiguous blocks
1346 * @blen: the length of the block run
1347 *
1348 */
1349
Steven Whitehousecd915492006-09-04 12:49:07 -04001350void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001351{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001352 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353 struct gfs2_rgrpd *rgd;
1354
1355 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1356 if (!rgd)
1357 return;
1358
1359 rgd->rd_rg.rg_free += blen;
1360
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001361 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001362 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1363
1364 gfs2_trans_add_rg(rgd);
1365
1366 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001367 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001368 gfs2_meta_wipe(ip, bstart, blen);
1369}
1370
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001371void gfs2_unlink_di(struct inode *inode)
1372{
1373 struct gfs2_inode *ip = GFS2_I(inode);
1374 struct gfs2_sbd *sdp = GFS2_SB(inode);
1375 struct gfs2_rgrpd *rgd;
1376 u64 blkno = ip->i_num.no_addr;
1377
1378 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1379 if (!rgd)
1380 return;
1381 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1382 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1383 gfs2_trans_add_rg(rgd);
1384}
1385
Steven Whitehousecd915492006-09-04 12:49:07 -04001386static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387{
1388 struct gfs2_sbd *sdp = rgd->rd_sbd;
1389 struct gfs2_rgrpd *tmp_rgd;
1390
1391 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1392 if (!tmp_rgd)
1393 return;
1394 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1395
1396 if (!rgd->rd_rg.rg_dinodes)
1397 gfs2_consist_rgrpd(rgd);
1398 rgd->rd_rg.rg_dinodes--;
1399 rgd->rd_rg.rg_free++;
1400
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001401 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1403
1404 gfs2_statfs_change(sdp, 0, +1, -1);
1405 gfs2_trans_add_rg(rgd);
1406}
1407
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408
1409void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1410{
1411 gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001412 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413 gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
1414}
1415
1416/**
1417 * gfs2_rlist_add - add a RG to a list of RGs
1418 * @sdp: the filesystem
1419 * @rlist: the list of resource groups
1420 * @block: the block
1421 *
1422 * Figure out what RG a block belongs to and add that RG to the list
1423 *
1424 * FIXME: Don't use NOFAIL
1425 *
1426 */
1427
1428void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001429 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001430{
1431 struct gfs2_rgrpd *rgd;
1432 struct gfs2_rgrpd **tmp;
1433 unsigned int new_space;
1434 unsigned int x;
1435
1436 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1437 return;
1438
1439 rgd = gfs2_blk2rgrpd(sdp, block);
1440 if (!rgd) {
1441 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001442 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001443 return;
1444 }
1445
1446 for (x = 0; x < rlist->rl_rgrps; x++)
1447 if (rlist->rl_rgd[x] == rgd)
1448 return;
1449
1450 if (rlist->rl_rgrps == rlist->rl_space) {
1451 new_space = rlist->rl_space + 10;
1452
1453 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001454 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455
1456 if (rlist->rl_rgd) {
1457 memcpy(tmp, rlist->rl_rgd,
1458 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1459 kfree(rlist->rl_rgd);
1460 }
1461
1462 rlist->rl_space = new_space;
1463 rlist->rl_rgd = tmp;
1464 }
1465
1466 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1467}
1468
1469/**
1470 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1471 * and initialize an array of glock holders for them
1472 * @rlist: the list of resource groups
1473 * @state: the lock state to acquire the RG lock in
1474 * @flags: the modifier flags for the holder structures
1475 *
1476 * FIXME: Don't use NOFAIL
1477 *
1478 */
1479
1480void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1481 int flags)
1482{
1483 unsigned int x;
1484
1485 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001486 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487 for (x = 0; x < rlist->rl_rgrps; x++)
1488 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1489 state, flags,
1490 &rlist->rl_ghs[x]);
1491}
1492
1493/**
1494 * gfs2_rlist_free - free a resource group list
1495 * @list: the list of resource groups
1496 *
1497 */
1498
1499void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1500{
1501 unsigned int x;
1502
1503 kfree(rlist->rl_rgd);
1504
1505 if (rlist->rl_ghs) {
1506 for (x = 0; x < rlist->rl_rgrps; x++)
1507 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1508 kfree(rlist->rl_ghs);
1509 }
1510}
1511