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