blob: 027f6ec5b0d93a5a63bf65d7ea5e1450df82be37 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Robert Peterson7ae8fa82007-05-09 09:37:57 -05003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
Steven Whitehousef42faf42006-01-30 18:34:10 +000014#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020016#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "glock.h"
21#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "lops.h"
23#include "meta_io.h"
24#include "quota.h"
25#include "rgrp.h"
26#include "super.h"
27#include "trans.h"
Steven Whitehousef42faf42006-01-30 18:34:10 +000028#include "ops_file.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
Benjamin Marzinski172e0452007-03-23 14:51:56 -060030#include "log.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031
Steven Whitehouse2c1e52a2006-09-05 15:41:57 -040032#define BFITNOENT ((u32)~0)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040033
34/*
35 * These routines are used by the resource group routines (rgrp.c)
36 * to keep track of block allocation. Each block is represented by two
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040037 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
38 *
39 * 0 = Free
40 * 1 = Used (not metadata)
41 * 2 = Unlinked (still in use) inode
42 * 3 = Used (metadata)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040043 */
44
45static const char valid_change[16] = {
46 /* current */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040047 /* n */ 0, 1, 1, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040048 /* e */ 1, 0, 0, 0,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040049 /* w */ 0, 0, 0, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040050 1, 0, 0, 0
51};
52
53/**
54 * gfs2_setbit - Set a bit in the bitmaps
55 * @buffer: the buffer that holds the bitmaps
56 * @buflen: the length (in bytes) of the buffer
57 * @block: the block to set
58 * @new_state: the new state of the block
59 *
60 */
61
Steven Whitehouse3efd7532006-05-18 14:02:52 -040062static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040063 unsigned int buflen, u32 block,
Steven Whitehouse3efd7532006-05-18 14:02:52 -040064 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040065{
66 unsigned char *byte, *end, cur_state;
67 unsigned int bit;
68
69 byte = buffer + (block / GFS2_NBBY);
70 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
71 end = buffer + buflen;
72
73 gfs2_assert(rgd->rd_sbd, byte < end);
74
75 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
76
77 if (valid_change[new_state * 4 + cur_state]) {
78 *byte ^= cur_state << bit;
79 *byte |= new_state << bit;
80 } else
81 gfs2_consist_rgrpd(rgd);
82}
83
84/**
85 * gfs2_testbit - test a bit in the bitmaps
86 * @buffer: the buffer that holds the bitmaps
87 * @buflen: the length (in bytes) of the buffer
88 * @block: the block to read
89 *
90 */
91
Steven Whitehouse3efd7532006-05-18 14:02:52 -040092static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040093 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040094{
95 unsigned char *byte, *end, cur_state;
96 unsigned int bit;
97
98 byte = buffer + (block / GFS2_NBBY);
99 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
100 end = buffer + buflen;
101
102 gfs2_assert(rgd->rd_sbd, byte < end);
103
104 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
105
106 return cur_state;
107}
108
109/**
110 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
111 * a block in a given allocation state.
112 * @buffer: the buffer that holds the bitmaps
113 * @buflen: the length (in bytes) of the buffer
114 * @goal: start search at this block's bit-pair (within @buffer)
115 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
116 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
117 *
118 * Scope of @goal and returned block number is only within this bitmap buffer,
119 * not entire rgrp or filesystem. @buffer will be offset from the actual
120 * beginning of a bitmap block buffer, skipping any header structures.
121 *
122 * Return: the block number (bitmap buffer scope) that was found
123 */
124
Steven Whitehousecd915492006-09-04 12:49:07 -0400125static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
126 unsigned int buflen, u32 goal,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400127 unsigned char old_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400128{
129 unsigned char *byte, *end, alloc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400130 u32 blk = goal;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400131 unsigned int bit;
132
133 byte = buffer + (goal / GFS2_NBBY);
134 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
135 end = buffer + buflen;
136 alloc = (old_state & 1) ? 0 : 0x55;
137
138 while (byte < end) {
139 if ((*byte & 0x55) == alloc) {
140 blk += (8 - bit) >> 1;
141
142 bit = 0;
143 byte++;
144
145 continue;
146 }
147
148 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
149 return blk;
150
151 bit += GFS2_BIT_SIZE;
152 if (bit >= 8) {
153 bit = 0;
154 byte++;
155 }
156
157 blk++;
158 }
159
160 return BFITNOENT;
161}
162
163/**
164 * gfs2_bitcount - count the number of bits in a certain state
165 * @buffer: the buffer that holds the bitmaps
166 * @buflen: the length (in bytes) of the buffer
167 * @state: the state of the block we're looking for
168 *
169 * Returns: The number of bits
170 */
171
Steven Whitehousecd915492006-09-04 12:49:07 -0400172static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400173 unsigned int buflen, unsigned char state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400174{
175 unsigned char *byte = buffer;
176 unsigned char *end = buffer + buflen;
177 unsigned char state1 = state << 2;
178 unsigned char state2 = state << 4;
179 unsigned char state3 = state << 6;
Steven Whitehousecd915492006-09-04 12:49:07 -0400180 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400181
182 for (; byte < end; byte++) {
183 if (((*byte) & 0x03) == state)
184 count++;
185 if (((*byte) & 0x0C) == state1)
186 count++;
187 if (((*byte) & 0x30) == state2)
188 count++;
189 if (((*byte) & 0xC0) == state3)
190 count++;
191 }
192
193 return count;
194}
195
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196/**
197 * gfs2_rgrp_verify - Verify that a resource group is consistent
198 * @sdp: the filesystem
199 * @rgd: the rgrp
200 *
201 */
202
203void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
204{
205 struct gfs2_sbd *sdp = rgd->rd_sbd;
206 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100207 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -0400208 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 int buf, x;
210
Steven Whitehousecd915492006-09-04 12:49:07 -0400211 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212
213 /* Count # blocks in each of 4 possible allocation states */
214 for (buf = 0; buf < length; buf++) {
215 bi = rgd->rd_bits + buf;
216 for (x = 0; x < 4; x++)
217 count[x] += gfs2_bitcount(rgd,
218 bi->bi_bh->b_data +
219 bi->bi_offset,
220 bi->bi_len, x);
221 }
222
223 if (count[0] != rgd->rd_rg.rg_free) {
224 if (gfs2_consist_rgrpd(rgd))
225 fs_err(sdp, "free data mismatch: %u != %u\n",
226 count[0], rgd->rd_rg.rg_free);
227 return;
228 }
229
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100230 tmp = rgd->rd_data -
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 rgd->rd_rg.rg_free -
232 rgd->rd_rg.rg_dinodes;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400233 if (count[1] + count[2] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 if (gfs2_consist_rgrpd(rgd))
235 fs_err(sdp, "used data mismatch: %u != %u\n",
236 count[1], tmp);
237 return;
238 }
239
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 if (count[3] != rgd->rd_rg.rg_dinodes) {
241 if (gfs2_consist_rgrpd(rgd))
242 fs_err(sdp, "used metadata mismatch: %u != %u\n",
243 count[3], rgd->rd_rg.rg_dinodes);
244 return;
245 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400246
247 if (count[2] > count[3]) {
248 if (gfs2_consist_rgrpd(rgd))
249 fs_err(sdp, "unlinked inodes > inodes: %u\n",
250 count[2]);
251 return;
252 }
253
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254}
255
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100256static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100258 u64 first = rgd->rd_data0;
259 u64 last = first + rgd->rd_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400260 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261}
262
263/**
264 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
265 * @sdp: The GFS2 superblock
266 * @n: The data block number
267 *
268 * Returns: The resource group, or NULL if not found
269 */
270
Steven Whitehousecd915492006-09-04 12:49:07 -0400271struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272{
273 struct gfs2_rgrpd *rgd;
274
275 spin_lock(&sdp->sd_rindex_spin);
276
277 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100278 if (rgrp_contains_block(rgd, blk)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
280 spin_unlock(&sdp->sd_rindex_spin);
281 return rgd;
282 }
283 }
284
285 spin_unlock(&sdp->sd_rindex_spin);
286
287 return NULL;
288}
289
290/**
291 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
292 * @sdp: The GFS2 superblock
293 *
294 * Returns: The first rgrp in the filesystem
295 */
296
297struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
298{
299 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
300 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
301}
302
303/**
304 * gfs2_rgrpd_get_next - get the next RG
305 * @rgd: A RG
306 *
307 * Returns: The next rgrp
308 */
309
310struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
311{
312 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
313 return NULL;
314 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
315}
316
317static void clear_rgrpdi(struct gfs2_sbd *sdp)
318{
319 struct list_head *head;
320 struct gfs2_rgrpd *rgd;
321 struct gfs2_glock *gl;
322
323 spin_lock(&sdp->sd_rindex_spin);
324 sdp->sd_rindex_forward = NULL;
325 head = &sdp->sd_rindex_recent_list;
326 while (!list_empty(head)) {
327 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
328 list_del(&rgd->rd_recent);
329 }
330 spin_unlock(&sdp->sd_rindex_spin);
331
332 head = &sdp->sd_rindex_list;
333 while (!list_empty(head)) {
334 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
335 gl = rgd->rd_gl;
336
337 list_del(&rgd->rd_list);
338 list_del(&rgd->rd_list_mru);
339
340 if (gl) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500341 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000342 gfs2_glock_put(gl);
343 }
344
345 kfree(rgd->rd_bits);
346 kfree(rgd);
347 }
348}
349
350void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
351{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000352 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 clear_rgrpdi(sdp);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000354 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355}
356
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100357static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
358{
359 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
360 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
361 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
362 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
363 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
364}
365
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366/**
367 * gfs2_compute_bitstructs - Compute the bitmap sizes
368 * @rgd: The resource group descriptor
369 *
370 * Calculates bitmap descriptors, one for each block that contains bitmap data
371 *
372 * Returns: errno
373 */
374
375static int compute_bitstructs(struct gfs2_rgrpd *rgd)
376{
377 struct gfs2_sbd *sdp = rgd->rd_sbd;
378 struct gfs2_bitmap *bi;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100379 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
Steven Whitehousecd915492006-09-04 12:49:07 -0400380 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 int x;
382
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400383 if (!length)
384 return -EINVAL;
385
Steven Whitehousedd894be2006-07-27 14:29:00 -0400386 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 if (!rgd->rd_bits)
388 return -ENOMEM;
389
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100390 bytes_left = rgd->rd_bitbytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391
392 for (x = 0; x < length; x++) {
393 bi = rgd->rd_bits + x;
394
395 /* small rgrp; bitmap stored completely in header block */
396 if (length == 1) {
397 bytes = bytes_left;
398 bi->bi_offset = sizeof(struct gfs2_rgrp);
399 bi->bi_start = 0;
400 bi->bi_len = bytes;
401 /* header block */
402 } else if (x == 0) {
403 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
404 bi->bi_offset = sizeof(struct gfs2_rgrp);
405 bi->bi_start = 0;
406 bi->bi_len = bytes;
407 /* last block */
408 } else if (x + 1 == length) {
409 bytes = bytes_left;
410 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100411 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 bi->bi_len = bytes;
413 /* other blocks */
414 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500415 bytes = sdp->sd_sb.sb_bsize -
416 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100418 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419 bi->bi_len = bytes;
420 }
421
422 bytes_left -= bytes;
423 }
424
425 if (bytes_left) {
426 gfs2_consist_rgrpd(rgd);
427 return -EIO;
428 }
429 bi = rgd->rd_bits + (length - 1);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100430 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 if (gfs2_consist_rgrpd(rgd)) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100432 gfs2_rindex_print(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433 fs_err(sdp, "start=%u len=%u offset=%u\n",
434 bi->bi_start, bi->bi_len, bi->bi_offset);
435 }
436 return -EIO;
437 }
438
439 return 0;
440}
441
442/**
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100443
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500444 * gfs2_ri_total - Total up the file system space, according to the rindex.
445 *
446 */
447u64 gfs2_ri_total(struct gfs2_sbd *sdp)
448{
449 u64 total_data = 0;
450 struct inode *inode = sdp->sd_rindex;
451 struct gfs2_inode *ip = GFS2_I(inode);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500452 char buf[sizeof(struct gfs2_rindex)];
453 struct file_ra_state ra_state;
454 int error, rgrps;
455
456 mutex_lock(&sdp->sd_rindex_mutex);
457 file_ra_state_init(&ra_state, inode->i_mapping);
458 for (rgrps = 0;; rgrps++) {
459 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
460
461 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
462 break;
463 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
464 sizeof(struct gfs2_rindex));
465 if (error != sizeof(struct gfs2_rindex))
466 break;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100467 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500468 }
469 mutex_unlock(&sdp->sd_rindex_mutex);
470 return total_data;
471}
472
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100473static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
474{
475 const struct gfs2_rindex *str = buf;
476
477 rgd->rd_addr = be64_to_cpu(str->ri_addr);
478 rgd->rd_length = be32_to_cpu(str->ri_length);
479 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
480 rgd->rd_data = be32_to_cpu(str->ri_data);
481 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
482}
483
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500484/**
Robert Peterson6c532672007-05-10 16:54:38 -0500485 * read_rindex_entry - Pull in a new resource index entry from the disk
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486 * @gl: The glock covering the rindex inode
487 *
Robert Peterson6c532672007-05-10 16:54:38 -0500488 * Returns: 0 on success, error code otherwise
489 */
490
491static int read_rindex_entry(struct gfs2_inode *ip,
492 struct file_ra_state *ra_state)
493{
494 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
495 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
496 char buf[sizeof(struct gfs2_rindex)];
497 int error;
498 struct gfs2_rgrpd *rgd;
499
500 error = gfs2_internal_read(ip, ra_state, buf, &pos,
501 sizeof(struct gfs2_rindex));
502 if (!error)
503 return 0;
504 if (error != sizeof(struct gfs2_rindex)) {
505 if (error > 0)
506 error = -EIO;
507 return error;
508 }
509
510 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
511 error = -ENOMEM;
512 if (!rgd)
513 return error;
514
515 mutex_init(&rgd->rd_mutex);
516 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
517 rgd->rd_sbd = sdp;
518
519 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
520 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
521
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100522 gfs2_rindex_in(rgd, buf);
Robert Peterson6c532672007-05-10 16:54:38 -0500523 error = compute_bitstructs(rgd);
524 if (error)
525 return error;
526
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100527 error = gfs2_glock_get(sdp, rgd->rd_addr,
Robert Peterson6c532672007-05-10 16:54:38 -0500528 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
529 if (error)
530 return error;
531
532 rgd->rd_gl->gl_object = rgd;
533 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
534 return error;
535}
536
537/**
538 * gfs2_ri_update - Pull in a new resource index from the disk
539 * @ip: pointer to the rindex inode
540 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 * Returns: 0 on successful update, error code otherwise
542 */
543
544static int gfs2_ri_update(struct gfs2_inode *ip)
545{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400546 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
547 struct inode *inode = &ip->i_inode;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000548 struct file_ra_state ra_state;
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500549 u64 rgrp_count = ip->i_di.di_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 int error;
551
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500552 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 gfs2_consist_inode(ip);
554 return -EIO;
555 }
556
557 clear_rgrpdi(sdp);
558
Steven Whitehousef42faf42006-01-30 18:34:10 +0000559 file_ra_state_init(&ra_state, inode->i_mapping);
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500560 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
Robert Peterson6c532672007-05-10 16:54:38 -0500561 error = read_rindex_entry(ip, &ra_state);
562 if (error) {
563 clear_rgrpdi(sdp);
564 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 }
567
568 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 return 0;
Robert Peterson6c532672007-05-10 16:54:38 -0500570}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571
Robert Peterson6c532672007-05-10 16:54:38 -0500572/**
573 * gfs2_ri_update_special - Pull in a new resource index from the disk
574 *
575 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
576 * In this case we know that we don't have any resource groups in memory yet.
577 *
578 * @ip: pointer to the rindex inode
579 *
580 * Returns: 0 on successful update, error code otherwise
581 */
582static int gfs2_ri_update_special(struct gfs2_inode *ip)
583{
584 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
585 struct inode *inode = &ip->i_inode;
586 struct file_ra_state ra_state;
587 int error;
588
589 file_ra_state_init(&ra_state, inode->i_mapping);
590 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
591 /* Ignore partials */
592 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
593 ip->i_di.di_size)
594 break;
595 error = read_rindex_entry(ip, &ra_state);
596 if (error) {
597 clear_rgrpdi(sdp);
598 return error;
599 }
600 }
601
602 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
603 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604}
605
606/**
607 * gfs2_rindex_hold - Grab a lock on the rindex
608 * @sdp: The GFS2 superblock
609 * @ri_gh: the glock holder
610 *
611 * We grab a lock on the rindex inode to make sure that it doesn't
612 * change whilst we are performing an operation. We keep this lock
613 * for quite long periods of time compared to other locks. This
614 * doesn't matter, since it is shared and it is very, very rarely
615 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
616 *
617 * This makes sure that we're using the latest copy of the resource index
618 * special file, which might have been updated if someone expanded the
619 * filesystem (via gfs2_grow utility), which adds new resource groups.
620 *
621 * Returns: 0 on success, error code otherwise
622 */
623
624int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
625{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400626 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627 struct gfs2_glock *gl = ip->i_gl;
628 int error;
629
630 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
631 if (error)
632 return error;
633
634 /* Read new copy from disk if we don't have the latest */
635 if (sdp->sd_rindex_vn != gl->gl_vn) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000636 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637 if (sdp->sd_rindex_vn != gl->gl_vn) {
638 error = gfs2_ri_update(ip);
639 if (error)
640 gfs2_glock_dq_uninit(ri_gh);
641 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000642 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 }
644
645 return error;
646}
647
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100648static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
649{
650 const struct gfs2_rgrp *str = buf;
651
652 rg->rg_flags = be32_to_cpu(str->rg_flags);
653 rg->rg_free = be32_to_cpu(str->rg_free);
654 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
655 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
656}
657
658static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
659{
660 struct gfs2_rgrp *str = buf;
661
662 str->rg_flags = cpu_to_be32(rg->rg_flags);
663 str->rg_free = cpu_to_be32(rg->rg_free);
664 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
665 str->__pad = cpu_to_be32(0);
666 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
667 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
668}
669
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670/**
671 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
672 * @rgd: the struct gfs2_rgrpd describing the RG to read in
673 *
674 * Read in all of a Resource Group's header and bitmap blocks.
675 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
676 *
677 * Returns: errno
678 */
679
680int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
681{
682 struct gfs2_sbd *sdp = rgd->rd_sbd;
683 struct gfs2_glock *gl = rgd->rd_gl;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100684 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 struct gfs2_bitmap *bi;
686 unsigned int x, y;
687 int error;
688
Steven Whitehousef55ab262006-02-21 12:51:39 +0000689 mutex_lock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690
691 spin_lock(&sdp->sd_rindex_spin);
692 if (rgd->rd_bh_count) {
693 rgd->rd_bh_count++;
694 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000695 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696 return 0;
697 }
698 spin_unlock(&sdp->sd_rindex_spin);
699
700 for (x = 0; x < length; x++) {
701 bi = rgd->rd_bits + x;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100702 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703 if (error)
704 goto fail;
705 }
706
707 for (y = length; y--;) {
708 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400709 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 if (error)
711 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400712 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 GFS2_METATYPE_RG)) {
714 error = -EIO;
715 goto fail;
716 }
717 }
718
719 if (rgd->rd_rg_vn != gl->gl_vn) {
720 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
721 rgd->rd_rg_vn = gl->gl_vn;
722 }
723
724 spin_lock(&sdp->sd_rindex_spin);
725 rgd->rd_free_clone = rgd->rd_rg.rg_free;
726 rgd->rd_bh_count++;
727 spin_unlock(&sdp->sd_rindex_spin);
728
Steven Whitehousef55ab262006-02-21 12:51:39 +0000729 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730
731 return 0;
732
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400733fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 while (x--) {
735 bi = rgd->rd_bits + x;
736 brelse(bi->bi_bh);
737 bi->bi_bh = NULL;
738 gfs2_assert_warn(sdp, !bi->bi_clone);
739 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000740 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741
742 return error;
743}
744
745void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
746{
747 struct gfs2_sbd *sdp = rgd->rd_sbd;
748
749 spin_lock(&sdp->sd_rindex_spin);
750 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
751 rgd->rd_bh_count++;
752 spin_unlock(&sdp->sd_rindex_spin);
753}
754
755/**
756 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
757 * @rgd: the struct gfs2_rgrpd describing the RG to read in
758 *
759 */
760
761void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
762{
763 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100764 int x, length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765
766 spin_lock(&sdp->sd_rindex_spin);
767 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
768 if (--rgd->rd_bh_count) {
769 spin_unlock(&sdp->sd_rindex_spin);
770 return;
771 }
772
773 for (x = 0; x < length; x++) {
774 struct gfs2_bitmap *bi = rgd->rd_bits + x;
775 kfree(bi->bi_clone);
776 bi->bi_clone = NULL;
777 brelse(bi->bi_bh);
778 bi->bi_bh = NULL;
779 }
780
781 spin_unlock(&sdp->sd_rindex_spin);
782}
783
784void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
785{
786 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100787 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 unsigned int x;
789
790 for (x = 0; x < length; x++) {
791 struct gfs2_bitmap *bi = rgd->rd_bits + x;
792 if (!bi->bi_clone)
793 continue;
794 memcpy(bi->bi_clone + bi->bi_offset,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400795 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 }
797
798 spin_lock(&sdp->sd_rindex_spin);
799 rgd->rd_free_clone = rgd->rd_rg.rg_free;
800 spin_unlock(&sdp->sd_rindex_spin);
801}
802
803/**
804 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
805 * @ip: the incore GFS2 inode structure
806 *
807 * Returns: the struct gfs2_alloc
808 */
809
810struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
811{
812 struct gfs2_alloc *al = &ip->i_alloc;
813
814 /* FIXME: Should assert that the correct locks are held here... */
815 memset(al, 0, sizeof(*al));
816 return al;
817}
818
819/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 * try_rgrp_fit - See if a given reservation will fit in a given RG
821 * @rgd: the RG data
822 * @al: the struct gfs2_alloc structure describing the reservation
823 *
824 * If there's room for the requested blocks to be allocated from the RG:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 * Sets the $al_rgd field in @al.
826 *
827 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
828 */
829
830static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
831{
832 struct gfs2_sbd *sdp = rgd->rd_sbd;
833 int ret = 0;
834
Steven Whitehousea43a4902007-04-02 10:48:17 +0100835 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
836 return 0;
837
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 spin_lock(&sdp->sd_rindex_spin);
839 if (rgd->rd_free_clone >= al->al_requested) {
840 al->al_rgd = rgd;
841 ret = 1;
842 }
843 spin_unlock(&sdp->sd_rindex_spin);
844
845 return ret;
846}
847
848/**
849 * recent_rgrp_first - get first RG from "recent" list
850 * @sdp: The GFS2 superblock
851 * @rglast: address of the rgrp used last
852 *
853 * Returns: The first rgrp in the recent list
854 */
855
856static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
Steven Whitehousecd915492006-09-04 12:49:07 -0400857 u64 rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858{
859 struct gfs2_rgrpd *rgd = NULL;
860
861 spin_lock(&sdp->sd_rindex_spin);
862
863 if (list_empty(&sdp->sd_rindex_recent_list))
864 goto out;
865
866 if (!rglast)
867 goto first;
868
869 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100870 if (rgd->rd_addr == rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 goto out;
872 }
873
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400874first:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
876 rd_recent);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400877out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 return rgd;
880}
881
882/**
883 * recent_rgrp_next - get next RG from "recent" list
884 * @cur_rgd: current rgrp
885 * @remove:
886 *
887 * Returns: The next rgrp in the recent list
888 */
889
890static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
891 int remove)
892{
893 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
894 struct list_head *head;
895 struct gfs2_rgrpd *rgd;
896
897 spin_lock(&sdp->sd_rindex_spin);
898
899 head = &sdp->sd_rindex_recent_list;
900
901 list_for_each_entry(rgd, head, rd_recent) {
902 if (rgd == cur_rgd) {
903 if (cur_rgd->rd_recent.next != head)
904 rgd = list_entry(cur_rgd->rd_recent.next,
905 struct gfs2_rgrpd, rd_recent);
906 else
907 rgd = NULL;
908
909 if (remove)
910 list_del(&cur_rgd->rd_recent);
911
912 goto out;
913 }
914 }
915
916 rgd = NULL;
917 if (!list_empty(head))
918 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
919
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400920out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 return rgd;
923}
924
925/**
926 * recent_rgrp_add - add an RG to tail of "recent" list
927 * @new_rgd: The rgrp to add
928 *
929 */
930
931static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
932{
933 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
934 struct gfs2_rgrpd *rgd;
935 unsigned int count = 0;
936 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
937
938 spin_lock(&sdp->sd_rindex_spin);
939
940 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
941 if (rgd == new_rgd)
942 goto out;
943
944 if (++count >= max)
945 goto out;
946 }
947 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
948
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400949out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950 spin_unlock(&sdp->sd_rindex_spin);
951}
952
953/**
954 * forward_rgrp_get - get an rgrp to try next from full list
955 * @sdp: The GFS2 superblock
956 *
957 * Returns: The rgrp to try next
958 */
959
960static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
961{
962 struct gfs2_rgrpd *rgd;
963 unsigned int journals = gfs2_jindex_size(sdp);
964 unsigned int rg = 0, x;
965
966 spin_lock(&sdp->sd_rindex_spin);
967
968 rgd = sdp->sd_rindex_forward;
969 if (!rgd) {
970 if (sdp->sd_rgrps >= journals)
971 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
972
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400973 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 x++, rgd = gfs2_rgrpd_get_next(rgd))
975 /* Do Nothing */;
976
977 sdp->sd_rindex_forward = rgd;
978 }
979
980 spin_unlock(&sdp->sd_rindex_spin);
981
982 return rgd;
983}
984
985/**
986 * forward_rgrp_set - set the forward rgrp pointer
987 * @sdp: the filesystem
988 * @rgd: The new forward rgrp
989 *
990 */
991
992static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
993{
994 spin_lock(&sdp->sd_rindex_spin);
995 sdp->sd_rindex_forward = rgd;
996 spin_unlock(&sdp->sd_rindex_spin);
997}
998
999/**
1000 * get_local_rgrp - Choose and lock a rgrp for allocation
1001 * @ip: the inode to reserve space for
1002 * @rgp: the chosen and locked rgrp
1003 *
1004 * Try to acquire rgrp in way which avoids contending with others.
1005 *
1006 * Returns: errno
1007 */
1008
1009static int get_local_rgrp(struct gfs2_inode *ip)
1010{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001011 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 struct gfs2_rgrpd *rgd, *begin = NULL;
1013 struct gfs2_alloc *al = &ip->i_alloc;
1014 int flags = LM_FLAG_TRY;
1015 int skipped = 0;
1016 int loops = 0;
1017 int error;
1018
1019 /* Try recently successful rgrps */
1020
1021 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
1022
1023 while (rgd) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001024 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -04001025 LM_FLAG_TRY, &al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 switch (error) {
1027 case 0:
1028 if (try_rgrp_fit(rgd, al))
1029 goto out;
1030 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1031 rgd = recent_rgrp_next(rgd, 1);
1032 break;
1033
1034 case GLR_TRYFAILED:
1035 rgd = recent_rgrp_next(rgd, 0);
1036 break;
1037
1038 default:
1039 return error;
1040 }
1041 }
1042
1043 /* Go through full list of rgrps */
1044
1045 begin = rgd = forward_rgrp_get(sdp);
1046
1047 for (;;) {
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -04001048 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 &al->al_rgd_gh);
1050 switch (error) {
1051 case 0:
1052 if (try_rgrp_fit(rgd, al))
1053 goto out;
1054 gfs2_glock_dq_uninit(&al->al_rgd_gh);
1055 break;
1056
1057 case GLR_TRYFAILED:
1058 skipped++;
1059 break;
1060
1061 default:
1062 return error;
1063 }
1064
1065 rgd = gfs2_rgrpd_get_next(rgd);
1066 if (!rgd)
1067 rgd = gfs2_rgrpd_get_first(sdp);
1068
1069 if (rgd == begin) {
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001070 if (++loops >= 3)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071 return -ENOSPC;
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001072 if (!skipped)
1073 loops++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074 flags = 0;
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001075 if (loops == 2)
1076 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 }
1078 }
1079
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001080out:
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001081 ip->i_last_rg_alloc = rgd->rd_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082
1083 if (begin) {
1084 recent_rgrp_add(rgd);
1085 rgd = gfs2_rgrpd_get_next(rgd);
1086 if (!rgd)
1087 rgd = gfs2_rgrpd_get_first(sdp);
1088 forward_rgrp_set(sdp, rgd);
1089 }
1090
1091 return 0;
1092}
1093
1094/**
1095 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1096 * @ip: the inode to reserve space for
1097 *
1098 * Returns: errno
1099 */
1100
1101int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1102{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001103 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 struct gfs2_alloc *al = &ip->i_alloc;
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001105 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106
1107 if (gfs2_assert_warn(sdp, al->al_requested))
1108 return -EINVAL;
1109
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001110 /* We need to hold the rindex unless the inode we're using is
1111 the rindex itself, in which case it's already held. */
1112 if (ip != GFS2_I(sdp->sd_rindex))
1113 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1114 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
Robert Peterson6c532672007-05-10 16:54:38 -05001115 error = gfs2_ri_update_special(ip);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001116
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117 if (error)
1118 return error;
1119
1120 error = get_local_rgrp(ip);
1121 if (error) {
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001122 if (ip != GFS2_I(sdp->sd_rindex))
1123 gfs2_glock_dq_uninit(&al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001124 return error;
1125 }
1126
1127 al->al_file = file;
1128 al->al_line = line;
1129
1130 return 0;
1131}
1132
1133/**
1134 * gfs2_inplace_release - release an inplace reservation
1135 * @ip: the inode the reservation was taken out on
1136 *
1137 * Release a reservation made by gfs2_inplace_reserve().
1138 */
1139
1140void gfs2_inplace_release(struct gfs2_inode *ip)
1141{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001142 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143 struct gfs2_alloc *al = &ip->i_alloc;
1144
1145 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1146 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1147 "al_file = %s, al_line = %u\n",
1148 al->al_alloced, al->al_requested, al->al_file,
1149 al->al_line);
1150
1151 al->al_rgd = NULL;
1152 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001153 if (ip != GFS2_I(sdp->sd_rindex))
1154 gfs2_glock_dq_uninit(&al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155}
1156
1157/**
1158 * gfs2_get_block_type - Check a block in a RG is of given type
1159 * @rgd: the resource group holding the block
1160 * @block: the block number
1161 *
1162 * Returns: The block type (GFS2_BLKST_*)
1163 */
1164
Steven Whitehousecd915492006-09-04 12:49:07 -04001165unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166{
1167 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001168 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169 unsigned int buf;
1170 unsigned char type;
1171
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001172 length = rgd->rd_length;
1173 rgrp_block = block - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174
1175 for (buf = 0; buf < length; buf++) {
1176 bi = rgd->rd_bits + buf;
1177 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1178 break;
1179 }
1180
1181 gfs2_assert(rgd->rd_sbd, buf < length);
1182 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1183
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001184 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001185 bi->bi_len, buf_block);
1186
1187 return type;
1188}
1189
1190/**
1191 * rgblk_search - find a block in @old_state, change allocation
1192 * state to @new_state
1193 * @rgd: the resource group descriptor
1194 * @goal: the goal block within the RG (start here to search for avail block)
1195 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1196 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1197 *
1198 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1199 * Add the found bitmap buffer to the transaction.
1200 * Set the found bits to @new_state to change block's allocation state.
1201 *
1202 * This function never fails, because we wouldn't call it unless we
1203 * know (from reservation results, etc.) that a block is available.
1204 *
1205 * Scope of @goal and returned block is just within rgrp, not the whole
1206 * filesystem.
1207 *
1208 * Returns: the block number allocated
1209 */
1210
Steven Whitehousecd915492006-09-04 12:49:07 -04001211static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212 unsigned char old_state, unsigned char new_state)
1213{
1214 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001215 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -04001216 u32 blk = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 unsigned int buf, x;
1218
1219 /* Find bitmap block that contains bits for goal block */
1220 for (buf = 0; buf < length; buf++) {
1221 bi = rgd->rd_bits + buf;
1222 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1223 break;
1224 }
1225
1226 gfs2_assert(rgd->rd_sbd, buf < length);
1227
1228 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1229 goal -= bi->bi_start * GFS2_NBBY;
1230
1231 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1232 "x <= length", instead of "x < length", because we typically start
1233 the search in the middle of a bit block, but if we can't find an
1234 allocatable block anywhere else, we want to be able wrap around and
1235 search in the first part of our first-searched bit block. */
1236 for (x = 0; x <= length; x++) {
1237 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001238 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001239 bi->bi_len, goal, old_state);
1240 else
1241 blk = gfs2_bitfit(rgd,
1242 bi->bi_bh->b_data + bi->bi_offset,
1243 bi->bi_len, goal, old_state);
1244 if (blk != BFITNOENT)
1245 break;
1246
1247 /* Try next bitmap block (wrap back to rgrp header if at end) */
1248 buf = (buf + 1) % length;
1249 bi = rgd->rd_bits + buf;
1250 goal = 0;
1251 }
1252
1253 if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
1254 blk = 0;
1255
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001256 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001257 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 bi->bi_len, blk, new_state);
1259 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001260 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001261 bi->bi_len, blk, new_state);
1262
1263 return bi->bi_start * GFS2_NBBY + blk;
1264}
1265
1266/**
1267 * rgblk_free - Change alloc state of given block(s)
1268 * @sdp: the filesystem
1269 * @bstart: the start of a run of blocks to free
1270 * @blen: the length of the block run (all must lie within ONE RG!)
1271 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1272 *
1273 * Returns: Resource group containing the block(s)
1274 */
1275
Steven Whitehousecd915492006-09-04 12:49:07 -04001276static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1277 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001278{
1279 struct gfs2_rgrpd *rgd;
1280 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001281 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001282 unsigned int buf;
1283
1284 rgd = gfs2_blk2rgrpd(sdp, bstart);
1285 if (!rgd) {
1286 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001287 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288 return NULL;
1289 }
1290
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001291 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001293 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001294
1295 while (blen--) {
1296 for (buf = 0; buf < length; buf++) {
1297 bi = rgd->rd_bits + buf;
1298 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1299 break;
1300 }
1301
1302 gfs2_assert(rgd->rd_sbd, buf < length);
1303
1304 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1305 rgrp_blk++;
1306
1307 if (!bi->bi_clone) {
1308 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001309 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310 memcpy(bi->bi_clone + bi->bi_offset,
1311 bi->bi_bh->b_data + bi->bi_offset,
1312 bi->bi_len);
1313 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001314 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousedd894be2006-07-27 14:29:00 -04001315 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001316 bi->bi_len, buf_blk, new_state);
1317 }
1318
1319 return rgd;
1320}
1321
1322/**
1323 * gfs2_alloc_data - Allocate a data block
1324 * @ip: the inode to allocate the data block for
1325 *
1326 * Returns: the allocated block
1327 */
1328
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001329u64 gfs2_alloc_data(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001331 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001332 struct gfs2_alloc *al = &ip->i_alloc;
1333 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001334 u32 goal, blk;
1335 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001336
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001337 if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
1338 goal = ip->i_di.di_goal_data - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339 else
1340 goal = rgd->rd_last_alloc_data;
1341
Steven Whitehousefd88de562006-05-05 16:59:11 -04001342 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001343 rgd->rd_last_alloc_data = blk;
1344
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001345 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346 ip->i_di.di_goal_data = block;
1347
1348 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1349 rgd->rd_rg.rg_free--;
1350
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001351 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1353
1354 al->al_alloced++;
1355
1356 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001357 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001358
1359 spin_lock(&sdp->sd_rindex_spin);
1360 rgd->rd_free_clone--;
1361 spin_unlock(&sdp->sd_rindex_spin);
1362
1363 return block;
1364}
1365
1366/**
1367 * gfs2_alloc_meta - Allocate a metadata block
1368 * @ip: the inode to allocate the metadata block for
1369 *
1370 * Returns: the allocated block
1371 */
1372
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001373u64 gfs2_alloc_meta(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001374{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001375 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 struct gfs2_alloc *al = &ip->i_alloc;
1377 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001378 u32 goal, blk;
1379 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001381 if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
1382 goal = ip->i_di.di_goal_meta - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383 else
1384 goal = rgd->rd_last_alloc_meta;
1385
Steven Whitehousefd88de562006-05-05 16:59:11 -04001386 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 rgd->rd_last_alloc_meta = blk;
1388
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001389 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001390 ip->i_di.di_goal_meta = block;
1391
1392 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1393 rgd->rd_rg.rg_free--;
1394
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001395 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1397
1398 al->al_alloced++;
1399
1400 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001401 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402 gfs2_trans_add_unrevoke(sdp, block);
1403
1404 spin_lock(&sdp->sd_rindex_spin);
1405 rgd->rd_free_clone--;
1406 spin_unlock(&sdp->sd_rindex_spin);
1407
1408 return block;
1409}
1410
1411/**
1412 * gfs2_alloc_di - Allocate a dinode
1413 * @dip: the directory that the inode is going in
1414 *
1415 * Returns: the block allocated
1416 */
1417
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001418u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001419{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001420 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001421 struct gfs2_alloc *al = &dip->i_alloc;
1422 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001423 u32 blk;
1424 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001425
1426 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1427 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
1428
1429 rgd->rd_last_alloc_meta = blk;
1430
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001431 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001432
1433 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1434 rgd->rd_rg.rg_free--;
1435 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001436 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001437 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001438 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1439
1440 al->al_alloced++;
1441
1442 gfs2_statfs_change(sdp, 0, -1, +1);
1443 gfs2_trans_add_unrevoke(sdp, block);
1444
1445 spin_lock(&sdp->sd_rindex_spin);
1446 rgd->rd_free_clone--;
1447 spin_unlock(&sdp->sd_rindex_spin);
1448
1449 return block;
1450}
1451
1452/**
1453 * gfs2_free_data - free a contiguous run of data block(s)
1454 * @ip: the inode these blocks are being freed from
1455 * @bstart: first block of a run of contiguous blocks
1456 * @blen: the length of the block run
1457 *
1458 */
1459
Steven Whitehousecd915492006-09-04 12:49:07 -04001460void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001462 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001463 struct gfs2_rgrpd *rgd;
1464
1465 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1466 if (!rgd)
1467 return;
1468
1469 rgd->rd_rg.rg_free += blen;
1470
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001471 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001472 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1473
1474 gfs2_trans_add_rg(rgd);
1475
1476 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001477 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001478}
1479
1480/**
1481 * gfs2_free_meta - free a contiguous run of data block(s)
1482 * @ip: the inode these blocks are being freed from
1483 * @bstart: first block of a run of contiguous blocks
1484 * @blen: the length of the block run
1485 *
1486 */
1487
Steven Whitehousecd915492006-09-04 12:49:07 -04001488void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001490 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001491 struct gfs2_rgrpd *rgd;
1492
1493 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1494 if (!rgd)
1495 return;
1496
1497 rgd->rd_rg.rg_free += blen;
1498
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001499 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1501
1502 gfs2_trans_add_rg(rgd);
1503
1504 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001505 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001506 gfs2_meta_wipe(ip, bstart, blen);
1507}
1508
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001509void gfs2_unlink_di(struct inode *inode)
1510{
1511 struct gfs2_inode *ip = GFS2_I(inode);
1512 struct gfs2_sbd *sdp = GFS2_SB(inode);
1513 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001514 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001515
1516 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1517 if (!rgd)
1518 return;
1519 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1520 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1521 gfs2_trans_add_rg(rgd);
1522}
1523
Steven Whitehousecd915492006-09-04 12:49:07 -04001524static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001525{
1526 struct gfs2_sbd *sdp = rgd->rd_sbd;
1527 struct gfs2_rgrpd *tmp_rgd;
1528
1529 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1530 if (!tmp_rgd)
1531 return;
1532 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1533
1534 if (!rgd->rd_rg.rg_dinodes)
1535 gfs2_consist_rgrpd(rgd);
1536 rgd->rd_rg.rg_dinodes--;
1537 rgd->rd_rg.rg_free++;
1538
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001539 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001540 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1541
1542 gfs2_statfs_change(sdp, 0, +1, -1);
1543 gfs2_trans_add_rg(rgd);
1544}
1545
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546
1547void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1548{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001549 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001550 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001551 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552}
1553
1554/**
1555 * gfs2_rlist_add - add a RG to a list of RGs
1556 * @sdp: the filesystem
1557 * @rlist: the list of resource groups
1558 * @block: the block
1559 *
1560 * Figure out what RG a block belongs to and add that RG to the list
1561 *
1562 * FIXME: Don't use NOFAIL
1563 *
1564 */
1565
1566void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001567 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001568{
1569 struct gfs2_rgrpd *rgd;
1570 struct gfs2_rgrpd **tmp;
1571 unsigned int new_space;
1572 unsigned int x;
1573
1574 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1575 return;
1576
1577 rgd = gfs2_blk2rgrpd(sdp, block);
1578 if (!rgd) {
1579 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001580 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001581 return;
1582 }
1583
1584 for (x = 0; x < rlist->rl_rgrps; x++)
1585 if (rlist->rl_rgd[x] == rgd)
1586 return;
1587
1588 if (rlist->rl_rgrps == rlist->rl_space) {
1589 new_space = rlist->rl_space + 10;
1590
1591 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001592 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001593
1594 if (rlist->rl_rgd) {
1595 memcpy(tmp, rlist->rl_rgd,
1596 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1597 kfree(rlist->rl_rgd);
1598 }
1599
1600 rlist->rl_space = new_space;
1601 rlist->rl_rgd = tmp;
1602 }
1603
1604 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1605}
1606
1607/**
1608 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1609 * and initialize an array of glock holders for them
1610 * @rlist: the list of resource groups
1611 * @state: the lock state to acquire the RG lock in
1612 * @flags: the modifier flags for the holder structures
1613 *
1614 * FIXME: Don't use NOFAIL
1615 *
1616 */
1617
1618void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1619 int flags)
1620{
1621 unsigned int x;
1622
1623 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001624 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001625 for (x = 0; x < rlist->rl_rgrps; x++)
1626 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1627 state, flags,
1628 &rlist->rl_ghs[x]);
1629}
1630
1631/**
1632 * gfs2_rlist_free - free a resource group list
1633 * @list: the list of resource groups
1634 *
1635 */
1636
1637void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1638{
1639 unsigned int x;
1640
1641 kfree(rlist->rl_rgd);
1642
1643 if (rlist->rl_ghs) {
1644 for (x = 0; x < rlist->rl_rgrps; x++)
1645 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1646 kfree(rlist->rl_ghs);
1647 }
1648}
1649