blob: d7ff9cf6653fa823fe9c02054f0c6238691e8856 [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 Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
Benjamin Marzinski172e0452007-03-23 14:51:56 -060029#include "log.h"
Steven Whitehousec8cdf472007-06-08 10:05:33 +010030#include "inode.h"
Steven Whitehouse51ff87b2007-10-15 14:42:35 +010031#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
Steven Whitehouse2c1e52a2006-09-05 15:41:57 -040033#define BFITNOENT ((u32)~0)
Bob Peterson6760bdc2007-07-24 14:09:32 -050034#define NO_BLOCK ((u64)~0)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040035
36/*
37 * These routines are used by the resource group routines (rgrp.c)
38 * to keep track of block allocation. Each block is represented by two
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040039 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
40 *
41 * 0 = Free
42 * 1 = Used (not metadata)
43 * 2 = Unlinked (still in use) inode
44 * 3 = Used (metadata)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040045 */
46
47static const char valid_change[16] = {
48 /* current */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040049 /* n */ 0, 1, 1, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040050 /* e */ 1, 0, 0, 0,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040051 /* w */ 0, 0, 0, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040052 1, 0, 0, 0
53};
54
Steven Whitehousec8cdf472007-06-08 10:05:33 +010055static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
56 unsigned char old_state, unsigned char new_state);
57
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040058/**
59 * gfs2_setbit - Set a bit in the bitmaps
60 * @buffer: the buffer that holds the bitmaps
61 * @buflen: the length (in bytes) of the buffer
62 * @block: the block to set
63 * @new_state: the new state of the block
64 *
65 */
66
Steven Whitehouse3efd7532006-05-18 14:02:52 -040067static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040068 unsigned int buflen, u32 block,
Steven Whitehouse3efd7532006-05-18 14:02:52 -040069 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040070{
71 unsigned char *byte, *end, cur_state;
72 unsigned int bit;
73
74 byte = buffer + (block / GFS2_NBBY);
75 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
76 end = buffer + buflen;
77
78 gfs2_assert(rgd->rd_sbd, byte < end);
79
80 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
81
82 if (valid_change[new_state * 4 + cur_state]) {
83 *byte ^= cur_state << bit;
84 *byte |= new_state << bit;
85 } else
86 gfs2_consist_rgrpd(rgd);
87}
88
89/**
90 * gfs2_testbit - test a bit in the bitmaps
91 * @buffer: the buffer that holds the bitmaps
92 * @buflen: the length (in bytes) of the buffer
93 * @block: the block to read
94 *
95 */
96
Steven Whitehouse3efd7532006-05-18 14:02:52 -040097static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040098 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040099{
100 unsigned char *byte, *end, cur_state;
101 unsigned int bit;
102
103 byte = buffer + (block / GFS2_NBBY);
104 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
105 end = buffer + buflen;
106
107 gfs2_assert(rgd->rd_sbd, byte < end);
108
109 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
110
111 return cur_state;
112}
113
114/**
115 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
116 * a block in a given allocation state.
117 * @buffer: the buffer that holds the bitmaps
118 * @buflen: the length (in bytes) of the buffer
119 * @goal: start search at this block's bit-pair (within @buffer)
Bob Peterson6760bdc2007-07-24 14:09:32 -0500120 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for.
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400121 *
122 * Scope of @goal and returned block number is only within this bitmap buffer,
123 * not entire rgrp or filesystem. @buffer will be offset from the actual
124 * beginning of a bitmap block buffer, skipping any header structures.
125 *
126 * Return: the block number (bitmap buffer scope) that was found
127 */
128
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600129static u32 gfs2_bitfit(unsigned char *buffer, unsigned int buflen, u32 goal,
130 unsigned char old_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400131{
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600132 unsigned char *byte;
Steven Whitehousecd915492006-09-04 12:49:07 -0400133 u32 blk = goal;
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600134 unsigned int bit, bitlong;
135 unsigned long *plong, plong55;
136 static int c = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400137
138 byte = buffer + (goal / GFS2_NBBY);
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600139 plong = buffer + (goal / GFS2_NBBY);
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400140 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600141 bitlong = bit;
142#if BITS_PER_LONG == 32
143 plong55 = 0x55555555;
144#else
145 plong55 = 0x5555555555555555;
146#endif
147 while (byte < buffer + buflen) {
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400148
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600149 if (bitlong == 0 && old_state == 0 && *plong == plong55) {
150 plong++;
151 byte += sizeof(unsigned long);
152 blk += sizeof(unsigned long) * GFS2_NBBY;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400153 continue;
154 }
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600155 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state) {
156 c++;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400157 return blk;
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600158 }
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400159 bit += GFS2_BIT_SIZE;
160 if (bit >= 8) {
161 bit = 0;
162 byte++;
163 }
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600164 bitlong += GFS2_BIT_SIZE;
165 if (bitlong >= sizeof(unsigned long) * 8) {
166 bitlong = 0;
167 plong++;
168 }
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400169
170 blk++;
171 }
172
173 return BFITNOENT;
174}
175
176/**
177 * gfs2_bitcount - count the number of bits in a certain state
178 * @buffer: the buffer that holds the bitmaps
179 * @buflen: the length (in bytes) of the buffer
180 * @state: the state of the block we're looking for
181 *
182 * Returns: The number of bits
183 */
184
Steven Whitehousecd915492006-09-04 12:49:07 -0400185static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400186 unsigned int buflen, unsigned char state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400187{
188 unsigned char *byte = buffer;
189 unsigned char *end = buffer + buflen;
190 unsigned char state1 = state << 2;
191 unsigned char state2 = state << 4;
192 unsigned char state3 = state << 6;
Steven Whitehousecd915492006-09-04 12:49:07 -0400193 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400194
195 for (; byte < end; byte++) {
196 if (((*byte) & 0x03) == state)
197 count++;
198 if (((*byte) & 0x0C) == state1)
199 count++;
200 if (((*byte) & 0x30) == state2)
201 count++;
202 if (((*byte) & 0xC0) == state3)
203 count++;
204 }
205
206 return count;
207}
208
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209/**
210 * gfs2_rgrp_verify - Verify that a resource group is consistent
211 * @sdp: the filesystem
212 * @rgd: the rgrp
213 *
214 */
215
216void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
217{
218 struct gfs2_sbd *sdp = rgd->rd_sbd;
219 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100220 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -0400221 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 int buf, x;
223
Steven Whitehousecd915492006-09-04 12:49:07 -0400224 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225
226 /* Count # blocks in each of 4 possible allocation states */
227 for (buf = 0; buf < length; buf++) {
228 bi = rgd->rd_bits + buf;
229 for (x = 0; x < 4; x++)
230 count[x] += gfs2_bitcount(rgd,
231 bi->bi_bh->b_data +
232 bi->bi_offset,
233 bi->bi_len, x);
234 }
235
236 if (count[0] != rgd->rd_rg.rg_free) {
237 if (gfs2_consist_rgrpd(rgd))
238 fs_err(sdp, "free data mismatch: %u != %u\n",
239 count[0], rgd->rd_rg.rg_free);
240 return;
241 }
242
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100243 tmp = rgd->rd_data -
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 rgd->rd_rg.rg_free -
245 rgd->rd_rg.rg_dinodes;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400246 if (count[1] + count[2] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247 if (gfs2_consist_rgrpd(rgd))
248 fs_err(sdp, "used data mismatch: %u != %u\n",
249 count[1], tmp);
250 return;
251 }
252
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253 if (count[3] != rgd->rd_rg.rg_dinodes) {
254 if (gfs2_consist_rgrpd(rgd))
255 fs_err(sdp, "used metadata mismatch: %u != %u\n",
256 count[3], rgd->rd_rg.rg_dinodes);
257 return;
258 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400259
260 if (count[2] > count[3]) {
261 if (gfs2_consist_rgrpd(rgd))
262 fs_err(sdp, "unlinked inodes > inodes: %u\n",
263 count[2]);
264 return;
265 }
266
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267}
268
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100269static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100271 u64 first = rgd->rd_data0;
272 u64 last = first + rgd->rd_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400273 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274}
275
276/**
277 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
278 * @sdp: The GFS2 superblock
279 * @n: The data block number
280 *
281 * Returns: The resource group, or NULL if not found
282 */
283
Steven Whitehousecd915492006-09-04 12:49:07 -0400284struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285{
286 struct gfs2_rgrpd *rgd;
287
288 spin_lock(&sdp->sd_rindex_spin);
289
290 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100291 if (rgrp_contains_block(rgd, blk)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
293 spin_unlock(&sdp->sd_rindex_spin);
294 return rgd;
295 }
296 }
297
298 spin_unlock(&sdp->sd_rindex_spin);
299
300 return NULL;
301}
302
303/**
304 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
305 * @sdp: The GFS2 superblock
306 *
307 * Returns: The first rgrp in the filesystem
308 */
309
310struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
311{
312 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
313 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
314}
315
316/**
317 * gfs2_rgrpd_get_next - get the next RG
318 * @rgd: A RG
319 *
320 * Returns: The next rgrp
321 */
322
323struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
324{
325 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
326 return NULL;
327 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
328}
329
330static void clear_rgrpdi(struct gfs2_sbd *sdp)
331{
332 struct list_head *head;
333 struct gfs2_rgrpd *rgd;
334 struct gfs2_glock *gl;
335
336 spin_lock(&sdp->sd_rindex_spin);
337 sdp->sd_rindex_forward = NULL;
338 head = &sdp->sd_rindex_recent_list;
339 while (!list_empty(head)) {
340 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
341 list_del(&rgd->rd_recent);
342 }
343 spin_unlock(&sdp->sd_rindex_spin);
344
345 head = &sdp->sd_rindex_list;
346 while (!list_empty(head)) {
347 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
348 gl = rgd->rd_gl;
349
350 list_del(&rgd->rd_list);
351 list_del(&rgd->rd_list_mru);
352
353 if (gl) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500354 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355 gfs2_glock_put(gl);
356 }
357
358 kfree(rgd->rd_bits);
359 kfree(rgd);
360 }
361}
362
363void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
364{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000365 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 clear_rgrpdi(sdp);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000367 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368}
369
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100370static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
371{
372 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
373 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
374 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
375 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
376 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
377}
378
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379/**
380 * gfs2_compute_bitstructs - Compute the bitmap sizes
381 * @rgd: The resource group descriptor
382 *
383 * Calculates bitmap descriptors, one for each block that contains bitmap data
384 *
385 * Returns: errno
386 */
387
388static int compute_bitstructs(struct gfs2_rgrpd *rgd)
389{
390 struct gfs2_sbd *sdp = rgd->rd_sbd;
391 struct gfs2_bitmap *bi;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100392 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
Steven Whitehousecd915492006-09-04 12:49:07 -0400393 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394 int x;
395
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400396 if (!length)
397 return -EINVAL;
398
Steven Whitehousedd894be2006-07-27 14:29:00 -0400399 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400 if (!rgd->rd_bits)
401 return -ENOMEM;
402
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100403 bytes_left = rgd->rd_bitbytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404
405 for (x = 0; x < length; x++) {
406 bi = rgd->rd_bits + x;
407
408 /* small rgrp; bitmap stored completely in header block */
409 if (length == 1) {
410 bytes = bytes_left;
411 bi->bi_offset = sizeof(struct gfs2_rgrp);
412 bi->bi_start = 0;
413 bi->bi_len = bytes;
414 /* header block */
415 } else if (x == 0) {
416 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
417 bi->bi_offset = sizeof(struct gfs2_rgrp);
418 bi->bi_start = 0;
419 bi->bi_len = bytes;
420 /* last block */
421 } else if (x + 1 == length) {
422 bytes = bytes_left;
423 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100424 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 bi->bi_len = bytes;
426 /* other blocks */
427 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500428 bytes = sdp->sd_sb.sb_bsize -
429 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100431 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 bi->bi_len = bytes;
433 }
434
435 bytes_left -= bytes;
436 }
437
438 if (bytes_left) {
439 gfs2_consist_rgrpd(rgd);
440 return -EIO;
441 }
442 bi = rgd->rd_bits + (length - 1);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100443 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000444 if (gfs2_consist_rgrpd(rgd)) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100445 gfs2_rindex_print(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446 fs_err(sdp, "start=%u len=%u offset=%u\n",
447 bi->bi_start, bi->bi_len, bi->bi_offset);
448 }
449 return -EIO;
450 }
451
452 return 0;
453}
454
455/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500456 * gfs2_ri_total - Total up the file system space, according to the rindex.
457 *
458 */
459u64 gfs2_ri_total(struct gfs2_sbd *sdp)
460{
461 u64 total_data = 0;
462 struct inode *inode = sdp->sd_rindex;
463 struct gfs2_inode *ip = GFS2_I(inode);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500464 char buf[sizeof(struct gfs2_rindex)];
465 struct file_ra_state ra_state;
466 int error, rgrps;
467
468 mutex_lock(&sdp->sd_rindex_mutex);
469 file_ra_state_init(&ra_state, inode->i_mapping);
470 for (rgrps = 0;; rgrps++) {
471 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
472
473 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
474 break;
475 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
476 sizeof(struct gfs2_rindex));
477 if (error != sizeof(struct gfs2_rindex))
478 break;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100479 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500480 }
481 mutex_unlock(&sdp->sd_rindex_mutex);
482 return total_data;
483}
484
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100485static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
486{
487 const struct gfs2_rindex *str = buf;
488
489 rgd->rd_addr = be64_to_cpu(str->ri_addr);
490 rgd->rd_length = be32_to_cpu(str->ri_length);
491 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
492 rgd->rd_data = be32_to_cpu(str->ri_data);
493 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
494}
495
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500496/**
Robert Peterson6c532672007-05-10 16:54:38 -0500497 * read_rindex_entry - Pull in a new resource index entry from the disk
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498 * @gl: The glock covering the rindex inode
499 *
Robert Peterson6c532672007-05-10 16:54:38 -0500500 * Returns: 0 on success, error code otherwise
501 */
502
503static int read_rindex_entry(struct gfs2_inode *ip,
504 struct file_ra_state *ra_state)
505{
506 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
507 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
508 char buf[sizeof(struct gfs2_rindex)];
509 int error;
510 struct gfs2_rgrpd *rgd;
511
512 error = gfs2_internal_read(ip, ra_state, buf, &pos,
513 sizeof(struct gfs2_rindex));
514 if (!error)
515 return 0;
516 if (error != sizeof(struct gfs2_rindex)) {
517 if (error > 0)
518 error = -EIO;
519 return error;
520 }
521
522 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
523 error = -ENOMEM;
524 if (!rgd)
525 return error;
526
527 mutex_init(&rgd->rd_mutex);
528 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
529 rgd->rd_sbd = sdp;
530
531 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
532 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
533
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100534 gfs2_rindex_in(rgd, buf);
Robert Peterson6c532672007-05-10 16:54:38 -0500535 error = compute_bitstructs(rgd);
536 if (error)
537 return error;
538
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100539 error = gfs2_glock_get(sdp, rgd->rd_addr,
Robert Peterson6c532672007-05-10 16:54:38 -0500540 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
541 if (error)
542 return error;
543
544 rgd->rd_gl->gl_object = rgd;
545 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100546 rgd->rd_flags |= GFS2_RDF_CHECK;
Robert Peterson6c532672007-05-10 16:54:38 -0500547 return error;
548}
549
550/**
551 * gfs2_ri_update - Pull in a new resource index from the disk
552 * @ip: pointer to the rindex inode
553 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 * Returns: 0 on successful update, error code otherwise
555 */
556
557static int gfs2_ri_update(struct gfs2_inode *ip)
558{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400559 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
560 struct inode *inode = &ip->i_inode;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000561 struct file_ra_state ra_state;
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500562 u64 rgrp_count = ip->i_di.di_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 int error;
564
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500565 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 gfs2_consist_inode(ip);
567 return -EIO;
568 }
569
570 clear_rgrpdi(sdp);
571
Steven Whitehousef42faf42006-01-30 18:34:10 +0000572 file_ra_state_init(&ra_state, inode->i_mapping);
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500573 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
Robert Peterson6c532672007-05-10 16:54:38 -0500574 error = read_rindex_entry(ip, &ra_state);
575 if (error) {
576 clear_rgrpdi(sdp);
577 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 }
580
581 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 return 0;
Robert Peterson6c532672007-05-10 16:54:38 -0500583}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584
Robert Peterson6c532672007-05-10 16:54:38 -0500585/**
586 * gfs2_ri_update_special - Pull in a new resource index from the disk
587 *
588 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
589 * In this case we know that we don't have any resource groups in memory yet.
590 *
591 * @ip: pointer to the rindex inode
592 *
593 * Returns: 0 on successful update, error code otherwise
594 */
595static int gfs2_ri_update_special(struct gfs2_inode *ip)
596{
597 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
598 struct inode *inode = &ip->i_inode;
599 struct file_ra_state ra_state;
600 int error;
601
602 file_ra_state_init(&ra_state, inode->i_mapping);
603 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
604 /* Ignore partials */
605 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
606 ip->i_di.di_size)
607 break;
608 error = read_rindex_entry(ip, &ra_state);
609 if (error) {
610 clear_rgrpdi(sdp);
611 return error;
612 }
613 }
614
615 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
616 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617}
618
619/**
620 * gfs2_rindex_hold - Grab a lock on the rindex
621 * @sdp: The GFS2 superblock
622 * @ri_gh: the glock holder
623 *
624 * We grab a lock on the rindex inode to make sure that it doesn't
625 * change whilst we are performing an operation. We keep this lock
626 * for quite long periods of time compared to other locks. This
627 * doesn't matter, since it is shared and it is very, very rarely
628 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
629 *
630 * This makes sure that we're using the latest copy of the resource index
631 * special file, which might have been updated if someone expanded the
632 * filesystem (via gfs2_grow utility), which adds new resource groups.
633 *
634 * Returns: 0 on success, error code otherwise
635 */
636
637int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
638{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400639 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 struct gfs2_glock *gl = ip->i_gl;
641 int error;
642
643 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
644 if (error)
645 return error;
646
647 /* Read new copy from disk if we don't have the latest */
648 if (sdp->sd_rindex_vn != gl->gl_vn) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000649 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 if (sdp->sd_rindex_vn != gl->gl_vn) {
651 error = gfs2_ri_update(ip);
652 if (error)
653 gfs2_glock_dq_uninit(ri_gh);
654 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000655 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 }
657
658 return error;
659}
660
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100661static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
662{
663 const struct gfs2_rgrp *str = buf;
664
665 rg->rg_flags = be32_to_cpu(str->rg_flags);
666 rg->rg_free = be32_to_cpu(str->rg_free);
667 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
668 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
669}
670
671static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
672{
673 struct gfs2_rgrp *str = buf;
674
675 str->rg_flags = cpu_to_be32(rg->rg_flags);
676 str->rg_free = cpu_to_be32(rg->rg_free);
677 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
678 str->__pad = cpu_to_be32(0);
679 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
680 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
681}
682
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683/**
684 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
685 * @rgd: the struct gfs2_rgrpd describing the RG to read in
686 *
687 * Read in all of a Resource Group's header and bitmap blocks.
688 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
689 *
690 * Returns: errno
691 */
692
693int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
694{
695 struct gfs2_sbd *sdp = rgd->rd_sbd;
696 struct gfs2_glock *gl = rgd->rd_gl;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100697 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698 struct gfs2_bitmap *bi;
699 unsigned int x, y;
700 int error;
701
Steven Whitehousef55ab262006-02-21 12:51:39 +0000702 mutex_lock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703
704 spin_lock(&sdp->sd_rindex_spin);
705 if (rgd->rd_bh_count) {
706 rgd->rd_bh_count++;
707 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000708 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709 return 0;
710 }
711 spin_unlock(&sdp->sd_rindex_spin);
712
713 for (x = 0; x < length; x++) {
714 bi = rgd->rd_bits + x;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100715 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 if (error)
717 goto fail;
718 }
719
720 for (y = length; y--;) {
721 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400722 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 if (error)
724 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400725 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 GFS2_METATYPE_RG)) {
727 error = -EIO;
728 goto fail;
729 }
730 }
731
732 if (rgd->rd_rg_vn != gl->gl_vn) {
733 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
734 rgd->rd_rg_vn = gl->gl_vn;
735 }
736
737 spin_lock(&sdp->sd_rindex_spin);
738 rgd->rd_free_clone = rgd->rd_rg.rg_free;
739 rgd->rd_bh_count++;
740 spin_unlock(&sdp->sd_rindex_spin);
741
Steven Whitehousef55ab262006-02-21 12:51:39 +0000742 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743
744 return 0;
745
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400746fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 while (x--) {
748 bi = rgd->rd_bits + x;
749 brelse(bi->bi_bh);
750 bi->bi_bh = NULL;
751 gfs2_assert_warn(sdp, !bi->bi_clone);
752 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000753 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754
755 return error;
756}
757
758void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
759{
760 struct gfs2_sbd *sdp = rgd->rd_sbd;
761
762 spin_lock(&sdp->sd_rindex_spin);
763 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
764 rgd->rd_bh_count++;
765 spin_unlock(&sdp->sd_rindex_spin);
766}
767
768/**
769 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
770 * @rgd: the struct gfs2_rgrpd describing the RG to read in
771 *
772 */
773
774void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
775{
776 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100777 int x, length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778
779 spin_lock(&sdp->sd_rindex_spin);
780 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
781 if (--rgd->rd_bh_count) {
782 spin_unlock(&sdp->sd_rindex_spin);
783 return;
784 }
785
786 for (x = 0; x < length; x++) {
787 struct gfs2_bitmap *bi = rgd->rd_bits + x;
788 kfree(bi->bi_clone);
789 bi->bi_clone = NULL;
790 brelse(bi->bi_bh);
791 bi->bi_bh = NULL;
792 }
793
794 spin_unlock(&sdp->sd_rindex_spin);
795}
796
797void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
798{
799 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100800 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801 unsigned int x;
802
803 for (x = 0; x < length; x++) {
804 struct gfs2_bitmap *bi = rgd->rd_bits + x;
805 if (!bi->bi_clone)
806 continue;
807 memcpy(bi->bi_clone + bi->bi_offset,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400808 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 }
810
811 spin_lock(&sdp->sd_rindex_spin);
812 rgd->rd_free_clone = rgd->rd_rg.rg_free;
813 spin_unlock(&sdp->sd_rindex_spin);
814}
815
816/**
817 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
818 * @ip: the incore GFS2 inode structure
819 *
820 * Returns: the struct gfs2_alloc
821 */
822
823struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
824{
825 struct gfs2_alloc *al = &ip->i_alloc;
826
827 /* FIXME: Should assert that the correct locks are held here... */
828 memset(al, 0, sizeof(*al));
829 return al;
830}
831
832/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 * try_rgrp_fit - See if a given reservation will fit in a given RG
834 * @rgd: the RG data
835 * @al: the struct gfs2_alloc structure describing the reservation
836 *
837 * If there's room for the requested blocks to be allocated from the RG:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 * Sets the $al_rgd field in @al.
839 *
840 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
841 */
842
843static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
844{
845 struct gfs2_sbd *sdp = rgd->rd_sbd;
846 int ret = 0;
847
Steven Whitehousea43a4902007-04-02 10:48:17 +0100848 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
849 return 0;
850
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851 spin_lock(&sdp->sd_rindex_spin);
852 if (rgd->rd_free_clone >= al->al_requested) {
853 al->al_rgd = rgd;
854 ret = 1;
855 }
856 spin_unlock(&sdp->sd_rindex_spin);
857
858 return ret;
859}
860
861/**
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100862 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
863 * @rgd: The rgrp
864 *
865 * Returns: The inode, if one has been found
866 */
867
868static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
869{
870 struct inode *inode;
Bob Peterson6760bdc2007-07-24 14:09:32 -0500871 u32 goal = 0, block;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400872 u64 no_addr;
Bob Peterson5f3eae72007-08-08 16:52:09 -0500873 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100874
875 for(;;) {
Bob Peterson24c73872007-07-12 16:58:50 -0500876 if (goal >= rgd->rd_data)
877 break;
Bob Peterson5f3eae72007-08-08 16:52:09 -0500878 down_write(&sdp->sd_log_flush_lock);
Bob Peterson6760bdc2007-07-24 14:09:32 -0500879 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
880 GFS2_BLKST_UNLINKED);
Bob Peterson5f3eae72007-08-08 16:52:09 -0500881 up_write(&sdp->sd_log_flush_lock);
Bob Peterson6760bdc2007-07-24 14:09:32 -0500882 if (block == BFITNOENT)
Bob Peterson24c73872007-07-12 16:58:50 -0500883 break;
Bob Peterson6760bdc2007-07-24 14:09:32 -0500884 /* rgblk_search can return a block < goal, so we need to
885 keep it marching forward. */
886 no_addr = block + rgd->rd_data0;
Bob Peterson24c73872007-07-12 16:58:50 -0500887 goal++;
Bob Peterson6760bdc2007-07-24 14:09:32 -0500888 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100889 continue;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400890 *last_unlinked = no_addr;
891 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500892 no_addr, -1, 1);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100893 if (!IS_ERR(inode))
894 return inode;
895 }
896
897 rgd->rd_flags &= ~GFS2_RDF_CHECK;
898 return NULL;
899}
900
901/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000902 * recent_rgrp_first - get first RG from "recent" list
903 * @sdp: The GFS2 superblock
904 * @rglast: address of the rgrp used last
905 *
906 * Returns: The first rgrp in the recent list
907 */
908
909static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
Steven Whitehousecd915492006-09-04 12:49:07 -0400910 u64 rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911{
912 struct gfs2_rgrpd *rgd = NULL;
913
914 spin_lock(&sdp->sd_rindex_spin);
915
916 if (list_empty(&sdp->sd_rindex_recent_list))
917 goto out;
918
919 if (!rglast)
920 goto first;
921
922 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100923 if (rgd->rd_addr == rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 goto out;
925 }
926
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400927first:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
929 rd_recent);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400930out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 return rgd;
933}
934
935/**
936 * recent_rgrp_next - get next RG from "recent" list
937 * @cur_rgd: current rgrp
938 * @remove:
939 *
940 * Returns: The next rgrp in the recent list
941 */
942
943static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
944 int remove)
945{
946 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
947 struct list_head *head;
948 struct gfs2_rgrpd *rgd;
949
950 spin_lock(&sdp->sd_rindex_spin);
951
952 head = &sdp->sd_rindex_recent_list;
953
954 list_for_each_entry(rgd, head, rd_recent) {
955 if (rgd == cur_rgd) {
956 if (cur_rgd->rd_recent.next != head)
957 rgd = list_entry(cur_rgd->rd_recent.next,
958 struct gfs2_rgrpd, rd_recent);
959 else
960 rgd = NULL;
961
962 if (remove)
963 list_del(&cur_rgd->rd_recent);
964
965 goto out;
966 }
967 }
968
969 rgd = NULL;
970 if (!list_empty(head))
971 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
972
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400973out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000975 return rgd;
976}
977
978/**
979 * recent_rgrp_add - add an RG to tail of "recent" list
980 * @new_rgd: The rgrp to add
981 *
982 */
983
984static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
985{
986 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
987 struct gfs2_rgrpd *rgd;
988 unsigned int count = 0;
989 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
990
991 spin_lock(&sdp->sd_rindex_spin);
992
993 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
994 if (rgd == new_rgd)
995 goto out;
996
997 if (++count >= max)
998 goto out;
999 }
1000 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
1001
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001002out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 spin_unlock(&sdp->sd_rindex_spin);
1004}
1005
1006/**
1007 * forward_rgrp_get - get an rgrp to try next from full list
1008 * @sdp: The GFS2 superblock
1009 *
1010 * Returns: The rgrp to try next
1011 */
1012
1013static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1014{
1015 struct gfs2_rgrpd *rgd;
1016 unsigned int journals = gfs2_jindex_size(sdp);
1017 unsigned int rg = 0, x;
1018
1019 spin_lock(&sdp->sd_rindex_spin);
1020
1021 rgd = sdp->sd_rindex_forward;
1022 if (!rgd) {
1023 if (sdp->sd_rgrps >= journals)
1024 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1025
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -04001026 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 x++, rgd = gfs2_rgrpd_get_next(rgd))
1028 /* Do Nothing */;
1029
1030 sdp->sd_rindex_forward = rgd;
1031 }
1032
1033 spin_unlock(&sdp->sd_rindex_spin);
1034
1035 return rgd;
1036}
1037
1038/**
1039 * forward_rgrp_set - set the forward rgrp pointer
1040 * @sdp: the filesystem
1041 * @rgd: The new forward rgrp
1042 *
1043 */
1044
1045static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1046{
1047 spin_lock(&sdp->sd_rindex_spin);
1048 sdp->sd_rindex_forward = rgd;
1049 spin_unlock(&sdp->sd_rindex_spin);
1050}
1051
1052/**
1053 * get_local_rgrp - Choose and lock a rgrp for allocation
1054 * @ip: the inode to reserve space for
1055 * @rgp: the chosen and locked rgrp
1056 *
1057 * Try to acquire rgrp in way which avoids contending with others.
1058 *
1059 * Returns: errno
1060 */
1061
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001062static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063{
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001064 struct inode *inode = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001065 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066 struct gfs2_rgrpd *rgd, *begin = NULL;
1067 struct gfs2_alloc *al = &ip->i_alloc;
1068 int flags = LM_FLAG_TRY;
1069 int skipped = 0;
1070 int loops = 0;
Abhijith Das292c8c12007-11-29 14:13:54 -06001071 int error, rg_locked;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072
1073 /* Try recently successful rgrps */
1074
1075 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
1076
1077 while (rgd) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001078 rg_locked = 0;
1079
1080 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1081 rg_locked = 1;
1082 error = 0;
1083 } else {
1084 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1085 LM_FLAG_TRY, &al->al_rgd_gh);
1086 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 switch (error) {
1088 case 0:
1089 if (try_rgrp_fit(rgd, al))
1090 goto out;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001091 if (rgd->rd_flags & GFS2_RDF_CHECK)
1092 inode = try_rgrp_unlink(rgd, last_unlinked);
Abhijith Das292c8c12007-11-29 14:13:54 -06001093 if (!rg_locked)
1094 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001095 if (inode)
1096 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 rgd = recent_rgrp_next(rgd, 1);
1098 break;
1099
1100 case GLR_TRYFAILED:
1101 rgd = recent_rgrp_next(rgd, 0);
1102 break;
1103
1104 default:
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001105 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 }
1107 }
1108
1109 /* Go through full list of rgrps */
1110
1111 begin = rgd = forward_rgrp_get(sdp);
1112
1113 for (;;) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001114 rg_locked = 0;
1115
1116 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1117 rg_locked = 1;
1118 error = 0;
1119 } else {
1120 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1121 &al->al_rgd_gh);
1122 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 switch (error) {
1124 case 0:
1125 if (try_rgrp_fit(rgd, al))
1126 goto out;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001127 if (rgd->rd_flags & GFS2_RDF_CHECK)
1128 inode = try_rgrp_unlink(rgd, last_unlinked);
Abhijith Das292c8c12007-11-29 14:13:54 -06001129 if (!rg_locked)
1130 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001131 if (inode)
1132 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133 break;
1134
1135 case GLR_TRYFAILED:
1136 skipped++;
1137 break;
1138
1139 default:
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001140 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141 }
1142
1143 rgd = gfs2_rgrpd_get_next(rgd);
1144 if (!rgd)
1145 rgd = gfs2_rgrpd_get_first(sdp);
1146
1147 if (rgd == begin) {
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001148 if (++loops >= 3)
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001149 return ERR_PTR(-ENOSPC);
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001150 if (!skipped)
1151 loops++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 flags = 0;
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001153 if (loops == 2)
1154 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155 }
1156 }
1157
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001158out:
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001159 ip->i_last_rg_alloc = rgd->rd_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160
1161 if (begin) {
1162 recent_rgrp_add(rgd);
1163 rgd = gfs2_rgrpd_get_next(rgd);
1164 if (!rgd)
1165 rgd = gfs2_rgrpd_get_first(sdp);
1166 forward_rgrp_set(sdp, rgd);
1167 }
1168
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001169 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170}
1171
1172/**
1173 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1174 * @ip: the inode to reserve space for
1175 *
1176 * Returns: errno
1177 */
1178
1179int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1180{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001181 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182 struct gfs2_alloc *al = &ip->i_alloc;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001183 struct inode *inode;
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001184 int error = 0;
Bob Peterson6760bdc2007-07-24 14:09:32 -05001185 u64 last_unlinked = NO_BLOCK;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186
1187 if (gfs2_assert_warn(sdp, al->al_requested))
1188 return -EINVAL;
1189
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001190try_again:
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001191 /* We need to hold the rindex unless the inode we're using is
1192 the rindex itself, in which case it's already held. */
1193 if (ip != GFS2_I(sdp->sd_rindex))
1194 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1195 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
Robert Peterson6c532672007-05-10 16:54:38 -05001196 error = gfs2_ri_update_special(ip);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001197
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198 if (error)
1199 return error;
1200
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001201 inode = get_local_rgrp(ip, &last_unlinked);
1202 if (inode) {
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001203 if (ip != GFS2_I(sdp->sd_rindex))
1204 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001205 if (IS_ERR(inode))
1206 return PTR_ERR(inode);
1207 iput(inode);
1208 gfs2_log_flush(sdp, NULL);
1209 goto try_again;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001210 }
1211
1212 al->al_file = file;
1213 al->al_line = line;
1214
1215 return 0;
1216}
1217
1218/**
1219 * gfs2_inplace_release - release an inplace reservation
1220 * @ip: the inode the reservation was taken out on
1221 *
1222 * Release a reservation made by gfs2_inplace_reserve().
1223 */
1224
1225void gfs2_inplace_release(struct gfs2_inode *ip)
1226{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001227 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228 struct gfs2_alloc *al = &ip->i_alloc;
1229
1230 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1231 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1232 "al_file = %s, al_line = %u\n",
1233 al->al_alloced, al->al_requested, al->al_file,
1234 al->al_line);
1235
1236 al->al_rgd = NULL;
Abhijith Das292c8c12007-11-29 14:13:54 -06001237 if (al->al_rgd_gh.gh_gl)
1238 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001239 if (ip != GFS2_I(sdp->sd_rindex))
1240 gfs2_glock_dq_uninit(&al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241}
1242
1243/**
1244 * gfs2_get_block_type - Check a block in a RG is of given type
1245 * @rgd: the resource group holding the block
1246 * @block: the block number
1247 *
1248 * Returns: The block type (GFS2_BLKST_*)
1249 */
1250
Steven Whitehousecd915492006-09-04 12:49:07 -04001251unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252{
1253 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001254 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255 unsigned int buf;
1256 unsigned char type;
1257
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001258 length = rgd->rd_length;
1259 rgrp_block = block - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260
1261 for (buf = 0; buf < length; buf++) {
1262 bi = rgd->rd_bits + buf;
1263 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1264 break;
1265 }
1266
1267 gfs2_assert(rgd->rd_sbd, buf < length);
1268 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1269
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001270 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271 bi->bi_len, buf_block);
1272
1273 return type;
1274}
1275
1276/**
1277 * rgblk_search - find a block in @old_state, change allocation
1278 * state to @new_state
1279 * @rgd: the resource group descriptor
1280 * @goal: the goal block within the RG (start here to search for avail block)
1281 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1282 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1283 *
1284 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1285 * Add the found bitmap buffer to the transaction.
1286 * Set the found bits to @new_state to change block's allocation state.
1287 *
1288 * This function never fails, because we wouldn't call it unless we
1289 * know (from reservation results, etc.) that a block is available.
1290 *
1291 * Scope of @goal and returned block is just within rgrp, not the whole
1292 * filesystem.
1293 *
1294 * Returns: the block number allocated
1295 */
1296
Steven Whitehousecd915492006-09-04 12:49:07 -04001297static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001298 unsigned char old_state, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001299{
1300 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001301 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -04001302 u32 blk = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303 unsigned int buf, x;
1304
1305 /* Find bitmap block that contains bits for goal block */
1306 for (buf = 0; buf < length; buf++) {
1307 bi = rgd->rd_bits + buf;
1308 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1309 break;
1310 }
1311
1312 gfs2_assert(rgd->rd_sbd, buf < length);
1313
1314 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1315 goal -= bi->bi_start * GFS2_NBBY;
1316
1317 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1318 "x <= length", instead of "x < length", because we typically start
1319 the search in the middle of a bit block, but if we can't find an
1320 allocatable block anywhere else, we want to be able wrap around and
1321 search in the first part of our first-searched bit block. */
1322 for (x = 0; x <= length; x++) {
Bob Peterson5f3eae72007-08-08 16:52:09 -05001323 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1324 bitmaps, so we must search the originals for that. */
1325 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
Bob Peterson5fdc2ee2007-12-11 19:00:16 -06001326 blk = gfs2_bitfit(bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327 bi->bi_len, goal, old_state);
1328 else
Bob Peterson5fdc2ee2007-12-11 19:00:16 -06001329 blk = gfs2_bitfit(bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330 bi->bi_len, goal, old_state);
1331 if (blk != BFITNOENT)
1332 break;
1333
1334 /* Try next bitmap block (wrap back to rgrp header if at end) */
1335 buf = (buf + 1) % length;
1336 bi = rgd->rd_bits + buf;
1337 goal = 0;
1338 }
1339
Bob Peterson6760bdc2007-07-24 14:09:32 -05001340 if (blk != BFITNOENT && old_state != new_state) {
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001341 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1342 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001343 bi->bi_len, blk, new_state);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001344 if (bi->bi_clone)
1345 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1346 bi->bi_len, blk, new_state);
1347 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001349 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001350}
1351
1352/**
1353 * rgblk_free - Change alloc state of given block(s)
1354 * @sdp: the filesystem
1355 * @bstart: the start of a run of blocks to free
1356 * @blen: the length of the block run (all must lie within ONE RG!)
1357 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1358 *
1359 * Returns: Resource group containing the block(s)
1360 */
1361
Steven Whitehousecd915492006-09-04 12:49:07 -04001362static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1363 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001364{
1365 struct gfs2_rgrpd *rgd;
1366 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001367 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001368 unsigned int buf;
1369
1370 rgd = gfs2_blk2rgrpd(sdp, bstart);
1371 if (!rgd) {
1372 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001373 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001374 return NULL;
1375 }
1376
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001377 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001378
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001379 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380
1381 while (blen--) {
1382 for (buf = 0; buf < length; buf++) {
1383 bi = rgd->rd_bits + buf;
1384 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1385 break;
1386 }
1387
1388 gfs2_assert(rgd->rd_sbd, buf < length);
1389
1390 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1391 rgrp_blk++;
1392
1393 if (!bi->bi_clone) {
1394 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001395 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396 memcpy(bi->bi_clone + bi->bi_offset,
1397 bi->bi_bh->b_data + bi->bi_offset,
1398 bi->bi_len);
1399 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001400 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousedd894be2006-07-27 14:29:00 -04001401 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402 bi->bi_len, buf_blk, new_state);
1403 }
1404
1405 return rgd;
1406}
1407
1408/**
1409 * gfs2_alloc_data - Allocate a data block
1410 * @ip: the inode to allocate the data block for
1411 *
1412 * Returns: the allocated block
1413 */
1414
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001415u64 gfs2_alloc_data(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001416{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001417 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001418 struct gfs2_alloc *al = &ip->i_alloc;
1419 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001420 u32 goal, blk;
1421 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001423 if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
1424 goal = ip->i_di.di_goal_data - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001425 else
1426 goal = rgd->rd_last_alloc_data;
1427
Steven Whitehousefd88de562006-05-05 16:59:11 -04001428 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001429 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001430 rgd->rd_last_alloc_data = blk;
1431
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001432 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001433 ip->i_di.di_goal_data = block;
1434
1435 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1436 rgd->rd_rg.rg_free--;
1437
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001438 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001439 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1440
1441 al->al_alloced++;
1442
1443 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001444 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445
1446 spin_lock(&sdp->sd_rindex_spin);
1447 rgd->rd_free_clone--;
1448 spin_unlock(&sdp->sd_rindex_spin);
1449
1450 return block;
1451}
1452
1453/**
1454 * gfs2_alloc_meta - Allocate a metadata block
1455 * @ip: the inode to allocate the metadata block for
1456 *
1457 * Returns: the allocated block
1458 */
1459
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001460u64 gfs2_alloc_meta(struct gfs2_inode *ip)
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_alloc *al = &ip->i_alloc;
1464 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001465 u32 goal, blk;
1466 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001467
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001468 if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
1469 goal = ip->i_di.di_goal_meta - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470 else
1471 goal = rgd->rd_last_alloc_meta;
1472
Steven Whitehousefd88de562006-05-05 16:59:11 -04001473 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001474 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001475 rgd->rd_last_alloc_meta = blk;
1476
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001477 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001478 ip->i_di.di_goal_meta = block;
1479
1480 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1481 rgd->rd_rg.rg_free--;
1482
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001483 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001484 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1485
1486 al->al_alloced++;
1487
1488 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001489 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001490 gfs2_trans_add_unrevoke(sdp, block);
1491
1492 spin_lock(&sdp->sd_rindex_spin);
1493 rgd->rd_free_clone--;
1494 spin_unlock(&sdp->sd_rindex_spin);
1495
1496 return block;
1497}
1498
1499/**
1500 * gfs2_alloc_di - Allocate a dinode
1501 * @dip: the directory that the inode is going in
1502 *
1503 * Returns: the block allocated
1504 */
1505
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001506u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001507{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001508 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001509 struct gfs2_alloc *al = &dip->i_alloc;
1510 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001511 u32 blk;
1512 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001513
1514 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1515 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001516 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001517
1518 rgd->rd_last_alloc_meta = blk;
1519
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001520 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001521
1522 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1523 rgd->rd_rg.rg_free--;
1524 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001525 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001526 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001527 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1528
1529 al->al_alloced++;
1530
1531 gfs2_statfs_change(sdp, 0, -1, +1);
1532 gfs2_trans_add_unrevoke(sdp, block);
1533
1534 spin_lock(&sdp->sd_rindex_spin);
1535 rgd->rd_free_clone--;
1536 spin_unlock(&sdp->sd_rindex_spin);
1537
1538 return block;
1539}
1540
1541/**
1542 * gfs2_free_data - free a contiguous run of data block(s)
1543 * @ip: the inode these blocks are being freed from
1544 * @bstart: first block of a run of contiguous blocks
1545 * @blen: the length of the block run
1546 *
1547 */
1548
Steven Whitehousecd915492006-09-04 12:49:07 -04001549void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001550{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001551 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552 struct gfs2_rgrpd *rgd;
1553
1554 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1555 if (!rgd)
1556 return;
1557
1558 rgd->rd_rg.rg_free += blen;
1559
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001560 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001561 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1562
1563 gfs2_trans_add_rg(rgd);
1564
1565 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001566 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001567}
1568
1569/**
1570 * gfs2_free_meta - free a contiguous run of data block(s)
1571 * @ip: the inode these blocks are being freed from
1572 * @bstart: first block of a run of contiguous blocks
1573 * @blen: the length of the block run
1574 *
1575 */
1576
Steven Whitehousecd915492006-09-04 12:49:07 -04001577void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001578{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001579 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001580 struct gfs2_rgrpd *rgd;
1581
1582 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1583 if (!rgd)
1584 return;
1585
1586 rgd->rd_rg.rg_free += blen;
1587
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001588 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001589 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1590
1591 gfs2_trans_add_rg(rgd);
1592
1593 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001594 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001595 gfs2_meta_wipe(ip, bstart, blen);
1596}
1597
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001598void gfs2_unlink_di(struct inode *inode)
1599{
1600 struct gfs2_inode *ip = GFS2_I(inode);
1601 struct gfs2_sbd *sdp = GFS2_SB(inode);
1602 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001603 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001604
1605 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1606 if (!rgd)
1607 return;
1608 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1609 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1610 gfs2_trans_add_rg(rgd);
1611}
1612
Steven Whitehousecd915492006-09-04 12:49:07 -04001613static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001614{
1615 struct gfs2_sbd *sdp = rgd->rd_sbd;
1616 struct gfs2_rgrpd *tmp_rgd;
1617
1618 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1619 if (!tmp_rgd)
1620 return;
1621 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1622
1623 if (!rgd->rd_rg.rg_dinodes)
1624 gfs2_consist_rgrpd(rgd);
1625 rgd->rd_rg.rg_dinodes--;
1626 rgd->rd_rg.rg_free++;
1627
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001628 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001629 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1630
1631 gfs2_statfs_change(sdp, 0, +1, -1);
1632 gfs2_trans_add_rg(rgd);
1633}
1634
David Teiglandb3b94fa2006-01-16 16:50:04 +00001635
1636void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1637{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001638 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001639 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001640 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001641}
1642
1643/**
1644 * gfs2_rlist_add - add a RG to a list of RGs
1645 * @sdp: the filesystem
1646 * @rlist: the list of resource groups
1647 * @block: the block
1648 *
1649 * Figure out what RG a block belongs to and add that RG to the list
1650 *
1651 * FIXME: Don't use NOFAIL
1652 *
1653 */
1654
1655void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001656 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001657{
1658 struct gfs2_rgrpd *rgd;
1659 struct gfs2_rgrpd **tmp;
1660 unsigned int new_space;
1661 unsigned int x;
1662
1663 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1664 return;
1665
1666 rgd = gfs2_blk2rgrpd(sdp, block);
1667 if (!rgd) {
1668 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001669 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001670 return;
1671 }
1672
1673 for (x = 0; x < rlist->rl_rgrps; x++)
1674 if (rlist->rl_rgd[x] == rgd)
1675 return;
1676
1677 if (rlist->rl_rgrps == rlist->rl_space) {
1678 new_space = rlist->rl_space + 10;
1679
1680 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001681 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001682
1683 if (rlist->rl_rgd) {
1684 memcpy(tmp, rlist->rl_rgd,
1685 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1686 kfree(rlist->rl_rgd);
1687 }
1688
1689 rlist->rl_space = new_space;
1690 rlist->rl_rgd = tmp;
1691 }
1692
1693 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1694}
1695
1696/**
1697 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1698 * and initialize an array of glock holders for them
1699 * @rlist: the list of resource groups
1700 * @state: the lock state to acquire the RG lock in
1701 * @flags: the modifier flags for the holder structures
1702 *
1703 * FIXME: Don't use NOFAIL
1704 *
1705 */
1706
1707void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1708 int flags)
1709{
1710 unsigned int x;
1711
1712 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001713 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001714 for (x = 0; x < rlist->rl_rgrps; x++)
1715 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1716 state, flags,
1717 &rlist->rl_ghs[x]);
1718}
1719
1720/**
1721 * gfs2_rlist_free - free a resource group list
1722 * @list: the list of resource groups
1723 *
1724 */
1725
1726void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1727{
1728 unsigned int x;
1729
1730 kfree(rlist->rl_rgd);
1731
1732 if (rlist->rl_ghs) {
1733 for (x = 0; x < rlist->rl_rgrps; x++)
1734 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1735 kfree(rlist->rl_ghs);
1736 }
1737}
1738