blob: ce48c4594ec88f9dc9fb64eb8d0413c5a04cc272 [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"
Steven Whitehousec8cdf472007-06-08 10:05:33 +010031#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
Steven Whitehouse2c1e52a2006-09-05 15:41:57 -040033#define BFITNOENT ((u32)~0)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040034
35/*
36 * These routines are used by the resource group routines (rgrp.c)
37 * to keep track of block allocation. Each block is represented by two
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040038 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
39 *
40 * 0 = Free
41 * 1 = Used (not metadata)
42 * 2 = Unlinked (still in use) inode
43 * 3 = Used (metadata)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040044 */
45
46static const char valid_change[16] = {
47 /* current */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040048 /* n */ 0, 1, 1, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040049 /* e */ 1, 0, 0, 0,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040050 /* w */ 0, 0, 0, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040051 1, 0, 0, 0
52};
53
Steven Whitehousec8cdf472007-06-08 10:05:33 +010054static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
55 unsigned char old_state, unsigned char new_state);
56
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040057/**
58 * gfs2_setbit - Set a bit in the bitmaps
59 * @buffer: the buffer that holds the bitmaps
60 * @buflen: the length (in bytes) of the buffer
61 * @block: the block to set
62 * @new_state: the new state of the block
63 *
64 */
65
Steven Whitehouse3efd7532006-05-18 14:02:52 -040066static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040067 unsigned int buflen, u32 block,
Steven Whitehouse3efd7532006-05-18 14:02:52 -040068 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040069{
70 unsigned char *byte, *end, cur_state;
71 unsigned int bit;
72
73 byte = buffer + (block / GFS2_NBBY);
74 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
75 end = buffer + buflen;
76
77 gfs2_assert(rgd->rd_sbd, byte < end);
78
79 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
80
81 if (valid_change[new_state * 4 + cur_state]) {
82 *byte ^= cur_state << bit;
83 *byte |= new_state << bit;
84 } else
85 gfs2_consist_rgrpd(rgd);
86}
87
88/**
89 * gfs2_testbit - test a bit in the bitmaps
90 * @buffer: the buffer that holds the bitmaps
91 * @buflen: the length (in bytes) of the buffer
92 * @block: the block to read
93 *
94 */
95
Steven Whitehouse3efd7532006-05-18 14:02:52 -040096static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehousecd915492006-09-04 12:49:07 -040097 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040098{
99 unsigned char *byte, *end, cur_state;
100 unsigned int bit;
101
102 byte = buffer + (block / GFS2_NBBY);
103 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
104 end = buffer + buflen;
105
106 gfs2_assert(rgd->rd_sbd, byte < end);
107
108 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
109
110 return cur_state;
111}
112
113/**
114 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
115 * a block in a given allocation state.
116 * @buffer: the buffer that holds the bitmaps
117 * @buflen: the length (in bytes) of the buffer
118 * @goal: start search at this block's bit-pair (within @buffer)
119 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for;
120 * bit 0 = alloc(1)/free(0), bit 1 = meta(1)/data(0)
121 *
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
Steven Whitehousecd915492006-09-04 12:49:07 -0400129static u32 gfs2_bitfit(struct gfs2_rgrpd *rgd, unsigned char *buffer,
130 unsigned int buflen, u32 goal,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400131 unsigned char old_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400132{
133 unsigned char *byte, *end, alloc;
Steven Whitehousecd915492006-09-04 12:49:07 -0400134 u32 blk = goal;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400135 unsigned int bit;
136
137 byte = buffer + (goal / GFS2_NBBY);
138 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
139 end = buffer + buflen;
140 alloc = (old_state & 1) ? 0 : 0x55;
141
142 while (byte < end) {
143 if ((*byte & 0x55) == alloc) {
144 blk += (8 - bit) >> 1;
145
146 bit = 0;
147 byte++;
148
149 continue;
150 }
151
152 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
153 return blk;
154
155 bit += GFS2_BIT_SIZE;
156 if (bit >= 8) {
157 bit = 0;
158 byte++;
159 }
160
161 blk++;
162 }
163
164 return BFITNOENT;
165}
166
167/**
168 * gfs2_bitcount - count the number of bits in a certain state
169 * @buffer: the buffer that holds the bitmaps
170 * @buflen: the length (in bytes) of the buffer
171 * @state: the state of the block we're looking for
172 *
173 * Returns: The number of bits
174 */
175
Steven Whitehousecd915492006-09-04 12:49:07 -0400176static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, unsigned char *buffer,
Steven Whitehouse3efd7532006-05-18 14:02:52 -0400177 unsigned int buflen, unsigned char state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400178{
179 unsigned char *byte = buffer;
180 unsigned char *end = buffer + buflen;
181 unsigned char state1 = state << 2;
182 unsigned char state2 = state << 4;
183 unsigned char state3 = state << 6;
Steven Whitehousecd915492006-09-04 12:49:07 -0400184 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400185
186 for (; byte < end; byte++) {
187 if (((*byte) & 0x03) == state)
188 count++;
189 if (((*byte) & 0x0C) == state1)
190 count++;
191 if (((*byte) & 0x30) == state2)
192 count++;
193 if (((*byte) & 0xC0) == state3)
194 count++;
195 }
196
197 return count;
198}
199
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200/**
201 * gfs2_rgrp_verify - Verify that a resource group is consistent
202 * @sdp: the filesystem
203 * @rgd: the rgrp
204 *
205 */
206
207void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
208{
209 struct gfs2_sbd *sdp = rgd->rd_sbd;
210 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100211 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -0400212 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 int buf, x;
214
Steven Whitehousecd915492006-09-04 12:49:07 -0400215 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216
217 /* Count # blocks in each of 4 possible allocation states */
218 for (buf = 0; buf < length; buf++) {
219 bi = rgd->rd_bits + buf;
220 for (x = 0; x < 4; x++)
221 count[x] += gfs2_bitcount(rgd,
222 bi->bi_bh->b_data +
223 bi->bi_offset,
224 bi->bi_len, x);
225 }
226
227 if (count[0] != rgd->rd_rg.rg_free) {
228 if (gfs2_consist_rgrpd(rgd))
229 fs_err(sdp, "free data mismatch: %u != %u\n",
230 count[0], rgd->rd_rg.rg_free);
231 return;
232 }
233
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100234 tmp = rgd->rd_data -
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 rgd->rd_rg.rg_free -
236 rgd->rd_rg.rg_dinodes;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400237 if (count[1] + count[2] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238 if (gfs2_consist_rgrpd(rgd))
239 fs_err(sdp, "used data mismatch: %u != %u\n",
240 count[1], tmp);
241 return;
242 }
243
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 if (count[3] != rgd->rd_rg.rg_dinodes) {
245 if (gfs2_consist_rgrpd(rgd))
246 fs_err(sdp, "used metadata mismatch: %u != %u\n",
247 count[3], rgd->rd_rg.rg_dinodes);
248 return;
249 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400250
251 if (count[2] > count[3]) {
252 if (gfs2_consist_rgrpd(rgd))
253 fs_err(sdp, "unlinked inodes > inodes: %u\n",
254 count[2]);
255 return;
256 }
257
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258}
259
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100260static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100262 u64 first = rgd->rd_data0;
263 u64 last = first + rgd->rd_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400264 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265}
266
267/**
268 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
269 * @sdp: The GFS2 superblock
270 * @n: The data block number
271 *
272 * Returns: The resource group, or NULL if not found
273 */
274
Steven Whitehousecd915492006-09-04 12:49:07 -0400275struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276{
277 struct gfs2_rgrpd *rgd;
278
279 spin_lock(&sdp->sd_rindex_spin);
280
281 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100282 if (rgrp_contains_block(rgd, blk)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
284 spin_unlock(&sdp->sd_rindex_spin);
285 return rgd;
286 }
287 }
288
289 spin_unlock(&sdp->sd_rindex_spin);
290
291 return NULL;
292}
293
294/**
295 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
296 * @sdp: The GFS2 superblock
297 *
298 * Returns: The first rgrp in the filesystem
299 */
300
301struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
302{
303 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
304 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
305}
306
307/**
308 * gfs2_rgrpd_get_next - get the next RG
309 * @rgd: A RG
310 *
311 * Returns: The next rgrp
312 */
313
314struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
315{
316 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
317 return NULL;
318 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
319}
320
321static void clear_rgrpdi(struct gfs2_sbd *sdp)
322{
323 struct list_head *head;
324 struct gfs2_rgrpd *rgd;
325 struct gfs2_glock *gl;
326
327 spin_lock(&sdp->sd_rindex_spin);
328 sdp->sd_rindex_forward = NULL;
329 head = &sdp->sd_rindex_recent_list;
330 while (!list_empty(head)) {
331 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
332 list_del(&rgd->rd_recent);
333 }
334 spin_unlock(&sdp->sd_rindex_spin);
335
336 head = &sdp->sd_rindex_list;
337 while (!list_empty(head)) {
338 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
339 gl = rgd->rd_gl;
340
341 list_del(&rgd->rd_list);
342 list_del(&rgd->rd_list_mru);
343
344 if (gl) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500345 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 gfs2_glock_put(gl);
347 }
348
349 kfree(rgd->rd_bits);
350 kfree(rgd);
351 }
352}
353
354void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
355{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000356 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357 clear_rgrpdi(sdp);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000358 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359}
360
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100361static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
362{
363 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
364 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
365 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
366 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
367 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
368}
369
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370/**
371 * gfs2_compute_bitstructs - Compute the bitmap sizes
372 * @rgd: The resource group descriptor
373 *
374 * Calculates bitmap descriptors, one for each block that contains bitmap data
375 *
376 * Returns: errno
377 */
378
379static int compute_bitstructs(struct gfs2_rgrpd *rgd)
380{
381 struct gfs2_sbd *sdp = rgd->rd_sbd;
382 struct gfs2_bitmap *bi;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100383 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
Steven Whitehousecd915492006-09-04 12:49:07 -0400384 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 int x;
386
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400387 if (!length)
388 return -EINVAL;
389
Steven Whitehousedd894be2006-07-27 14:29:00 -0400390 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000391 if (!rgd->rd_bits)
392 return -ENOMEM;
393
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100394 bytes_left = rgd->rd_bitbytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395
396 for (x = 0; x < length; x++) {
397 bi = rgd->rd_bits + x;
398
399 /* small rgrp; bitmap stored completely in header block */
400 if (length == 1) {
401 bytes = bytes_left;
402 bi->bi_offset = sizeof(struct gfs2_rgrp);
403 bi->bi_start = 0;
404 bi->bi_len = bytes;
405 /* header block */
406 } else if (x == 0) {
407 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
408 bi->bi_offset = sizeof(struct gfs2_rgrp);
409 bi->bi_start = 0;
410 bi->bi_len = bytes;
411 /* last block */
412 } else if (x + 1 == length) {
413 bytes = bytes_left;
414 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100415 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 bi->bi_len = bytes;
417 /* other blocks */
418 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500419 bytes = sdp->sd_sb.sb_bsize -
420 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100422 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 bi->bi_len = bytes;
424 }
425
426 bytes_left -= bytes;
427 }
428
429 if (bytes_left) {
430 gfs2_consist_rgrpd(rgd);
431 return -EIO;
432 }
433 bi = rgd->rd_bits + (length - 1);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100434 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435 if (gfs2_consist_rgrpd(rgd)) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100436 gfs2_rindex_print(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 fs_err(sdp, "start=%u len=%u offset=%u\n",
438 bi->bi_start, bi->bi_len, bi->bi_offset);
439 }
440 return -EIO;
441 }
442
443 return 0;
444}
445
446/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500447 * gfs2_ri_total - Total up the file system space, according to the rindex.
448 *
449 */
450u64 gfs2_ri_total(struct gfs2_sbd *sdp)
451{
452 u64 total_data = 0;
453 struct inode *inode = sdp->sd_rindex;
454 struct gfs2_inode *ip = GFS2_I(inode);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500455 char buf[sizeof(struct gfs2_rindex)];
456 struct file_ra_state ra_state;
457 int error, rgrps;
458
459 mutex_lock(&sdp->sd_rindex_mutex);
460 file_ra_state_init(&ra_state, inode->i_mapping);
461 for (rgrps = 0;; rgrps++) {
462 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
463
464 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
465 break;
466 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
467 sizeof(struct gfs2_rindex));
468 if (error != sizeof(struct gfs2_rindex))
469 break;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100470 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500471 }
472 mutex_unlock(&sdp->sd_rindex_mutex);
473 return total_data;
474}
475
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100476static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
477{
478 const struct gfs2_rindex *str = buf;
479
480 rgd->rd_addr = be64_to_cpu(str->ri_addr);
481 rgd->rd_length = be32_to_cpu(str->ri_length);
482 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
483 rgd->rd_data = be32_to_cpu(str->ri_data);
484 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
485}
486
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500487/**
Robert Peterson6c532672007-05-10 16:54:38 -0500488 * read_rindex_entry - Pull in a new resource index entry from the disk
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 * @gl: The glock covering the rindex inode
490 *
Robert Peterson6c532672007-05-10 16:54:38 -0500491 * Returns: 0 on success, error code otherwise
492 */
493
494static int read_rindex_entry(struct gfs2_inode *ip,
495 struct file_ra_state *ra_state)
496{
497 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
498 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
499 char buf[sizeof(struct gfs2_rindex)];
500 int error;
501 struct gfs2_rgrpd *rgd;
502
503 error = gfs2_internal_read(ip, ra_state, buf, &pos,
504 sizeof(struct gfs2_rindex));
505 if (!error)
506 return 0;
507 if (error != sizeof(struct gfs2_rindex)) {
508 if (error > 0)
509 error = -EIO;
510 return error;
511 }
512
513 rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_NOFS);
514 error = -ENOMEM;
515 if (!rgd)
516 return error;
517
518 mutex_init(&rgd->rd_mutex);
519 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
520 rgd->rd_sbd = sdp;
521
522 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
523 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
524
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100525 gfs2_rindex_in(rgd, buf);
Robert Peterson6c532672007-05-10 16:54:38 -0500526 error = compute_bitstructs(rgd);
527 if (error)
528 return error;
529
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100530 error = gfs2_glock_get(sdp, rgd->rd_addr,
Robert Peterson6c532672007-05-10 16:54:38 -0500531 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
532 if (error)
533 return error;
534
535 rgd->rd_gl->gl_object = rgd;
536 rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100537 rgd->rd_flags |= GFS2_RDF_CHECK;
Robert Peterson6c532672007-05-10 16:54:38 -0500538 return error;
539}
540
541/**
542 * gfs2_ri_update - Pull in a new resource index from the disk
543 * @ip: pointer to the rindex inode
544 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000545 * Returns: 0 on successful update, error code otherwise
546 */
547
548static int gfs2_ri_update(struct gfs2_inode *ip)
549{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400550 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
551 struct inode *inode = &ip->i_inode;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000552 struct file_ra_state ra_state;
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500553 u64 rgrp_count = ip->i_di.di_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554 int error;
555
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500556 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557 gfs2_consist_inode(ip);
558 return -EIO;
559 }
560
561 clear_rgrpdi(sdp);
562
Steven Whitehousef42faf42006-01-30 18:34:10 +0000563 file_ra_state_init(&ra_state, inode->i_mapping);
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500564 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
Robert Peterson6c532672007-05-10 16:54:38 -0500565 error = read_rindex_entry(ip, &ra_state);
566 if (error) {
567 clear_rgrpdi(sdp);
568 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570 }
571
572 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 return 0;
Robert Peterson6c532672007-05-10 16:54:38 -0500574}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575
Robert Peterson6c532672007-05-10 16:54:38 -0500576/**
577 * gfs2_ri_update_special - Pull in a new resource index from the disk
578 *
579 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
580 * In this case we know that we don't have any resource groups in memory yet.
581 *
582 * @ip: pointer to the rindex inode
583 *
584 * Returns: 0 on successful update, error code otherwise
585 */
586static int gfs2_ri_update_special(struct gfs2_inode *ip)
587{
588 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
589 struct inode *inode = &ip->i_inode;
590 struct file_ra_state ra_state;
591 int error;
592
593 file_ra_state_init(&ra_state, inode->i_mapping);
594 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
595 /* Ignore partials */
596 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
597 ip->i_di.di_size)
598 break;
599 error = read_rindex_entry(ip, &ra_state);
600 if (error) {
601 clear_rgrpdi(sdp);
602 return error;
603 }
604 }
605
606 sdp->sd_rindex_vn = ip->i_gl->gl_vn;
607 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608}
609
610/**
611 * gfs2_rindex_hold - Grab a lock on the rindex
612 * @sdp: The GFS2 superblock
613 * @ri_gh: the glock holder
614 *
615 * We grab a lock on the rindex inode to make sure that it doesn't
616 * change whilst we are performing an operation. We keep this lock
617 * for quite long periods of time compared to other locks. This
618 * doesn't matter, since it is shared and it is very, very rarely
619 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
620 *
621 * This makes sure that we're using the latest copy of the resource index
622 * special file, which might have been updated if someone expanded the
623 * filesystem (via gfs2_grow utility), which adds new resource groups.
624 *
625 * Returns: 0 on success, error code otherwise
626 */
627
628int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
629{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400630 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000631 struct gfs2_glock *gl = ip->i_gl;
632 int error;
633
634 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
635 if (error)
636 return error;
637
638 /* Read new copy from disk if we don't have the latest */
639 if (sdp->sd_rindex_vn != gl->gl_vn) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000640 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 if (sdp->sd_rindex_vn != gl->gl_vn) {
642 error = gfs2_ri_update(ip);
643 if (error)
644 gfs2_glock_dq_uninit(ri_gh);
645 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000646 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647 }
648
649 return error;
650}
651
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100652static void gfs2_rgrp_in(struct gfs2_rgrp_host *rg, const void *buf)
653{
654 const struct gfs2_rgrp *str = buf;
655
656 rg->rg_flags = be32_to_cpu(str->rg_flags);
657 rg->rg_free = be32_to_cpu(str->rg_free);
658 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
659 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
660}
661
662static void gfs2_rgrp_out(const struct gfs2_rgrp_host *rg, void *buf)
663{
664 struct gfs2_rgrp *str = buf;
665
666 str->rg_flags = cpu_to_be32(rg->rg_flags);
667 str->rg_free = cpu_to_be32(rg->rg_free);
668 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
669 str->__pad = cpu_to_be32(0);
670 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
671 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
672}
673
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674/**
675 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
676 * @rgd: the struct gfs2_rgrpd describing the RG to read in
677 *
678 * Read in all of a Resource Group's header and bitmap blocks.
679 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
680 *
681 * Returns: errno
682 */
683
684int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
685{
686 struct gfs2_sbd *sdp = rgd->rd_sbd;
687 struct gfs2_glock *gl = rgd->rd_gl;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100688 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 struct gfs2_bitmap *bi;
690 unsigned int x, y;
691 int error;
692
Steven Whitehousef55ab262006-02-21 12:51:39 +0000693 mutex_lock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694
695 spin_lock(&sdp->sd_rindex_spin);
696 if (rgd->rd_bh_count) {
697 rgd->rd_bh_count++;
698 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000699 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700 return 0;
701 }
702 spin_unlock(&sdp->sd_rindex_spin);
703
704 for (x = 0; x < length; x++) {
705 bi = rgd->rd_bits + x;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100706 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 if (error)
708 goto fail;
709 }
710
711 for (y = length; y--;) {
712 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400713 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 if (error)
715 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400716 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 GFS2_METATYPE_RG)) {
718 error = -EIO;
719 goto fail;
720 }
721 }
722
723 if (rgd->rd_rg_vn != gl->gl_vn) {
724 gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
725 rgd->rd_rg_vn = gl->gl_vn;
726 }
727
728 spin_lock(&sdp->sd_rindex_spin);
729 rgd->rd_free_clone = rgd->rd_rg.rg_free;
730 rgd->rd_bh_count++;
731 spin_unlock(&sdp->sd_rindex_spin);
732
Steven Whitehousef55ab262006-02-21 12:51:39 +0000733 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734
735 return 0;
736
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400737fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 while (x--) {
739 bi = rgd->rd_bits + x;
740 brelse(bi->bi_bh);
741 bi->bi_bh = NULL;
742 gfs2_assert_warn(sdp, !bi->bi_clone);
743 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000744 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745
746 return error;
747}
748
749void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
750{
751 struct gfs2_sbd *sdp = rgd->rd_sbd;
752
753 spin_lock(&sdp->sd_rindex_spin);
754 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
755 rgd->rd_bh_count++;
756 spin_unlock(&sdp->sd_rindex_spin);
757}
758
759/**
760 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
761 * @rgd: the struct gfs2_rgrpd describing the RG to read in
762 *
763 */
764
765void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
766{
767 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100768 int x, length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769
770 spin_lock(&sdp->sd_rindex_spin);
771 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
772 if (--rgd->rd_bh_count) {
773 spin_unlock(&sdp->sd_rindex_spin);
774 return;
775 }
776
777 for (x = 0; x < length; x++) {
778 struct gfs2_bitmap *bi = rgd->rd_bits + x;
779 kfree(bi->bi_clone);
780 bi->bi_clone = NULL;
781 brelse(bi->bi_bh);
782 bi->bi_bh = NULL;
783 }
784
785 spin_unlock(&sdp->sd_rindex_spin);
786}
787
788void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
789{
790 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100791 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 unsigned int x;
793
794 for (x = 0; x < length; x++) {
795 struct gfs2_bitmap *bi = rgd->rd_bits + x;
796 if (!bi->bi_clone)
797 continue;
798 memcpy(bi->bi_clone + bi->bi_offset,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400799 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 }
801
802 spin_lock(&sdp->sd_rindex_spin);
803 rgd->rd_free_clone = rgd->rd_rg.rg_free;
804 spin_unlock(&sdp->sd_rindex_spin);
805}
806
807/**
808 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
809 * @ip: the incore GFS2 inode structure
810 *
811 * Returns: the struct gfs2_alloc
812 */
813
814struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
815{
816 struct gfs2_alloc *al = &ip->i_alloc;
817
818 /* FIXME: Should assert that the correct locks are held here... */
819 memset(al, 0, sizeof(*al));
820 return al;
821}
822
823/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824 * try_rgrp_fit - See if a given reservation will fit in a given RG
825 * @rgd: the RG data
826 * @al: the struct gfs2_alloc structure describing the reservation
827 *
828 * If there's room for the requested blocks to be allocated from the RG:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 * Sets the $al_rgd field in @al.
830 *
831 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
832 */
833
834static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
835{
836 struct gfs2_sbd *sdp = rgd->rd_sbd;
837 int ret = 0;
838
Steven Whitehousea43a4902007-04-02 10:48:17 +0100839 if (rgd->rd_rg.rg_flags & GFS2_RGF_NOALLOC)
840 return 0;
841
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 spin_lock(&sdp->sd_rindex_spin);
843 if (rgd->rd_free_clone >= al->al_requested) {
844 al->al_rgd = rgd;
845 ret = 1;
846 }
847 spin_unlock(&sdp->sd_rindex_spin);
848
849 return ret;
850}
851
852/**
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100853 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
854 * @rgd: The rgrp
855 *
856 * Returns: The inode, if one has been found
857 */
858
859static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
860{
861 struct inode *inode;
862 u32 goal = 0;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400863 u64 no_addr;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100864
865 for(;;) {
Bob Peterson24c73872007-07-12 16:58:50 -0500866 if (goal >= rgd->rd_data)
867 break;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100868 goal = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
869 GFS2_BLKST_UNLINKED);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +0100870 if (goal == BFITNOENT)
Bob Peterson24c73872007-07-12 16:58:50 -0500871 break;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400872 no_addr = goal + rgd->rd_data0;
Bob Peterson24c73872007-07-12 16:58:50 -0500873 goal++;
874 if (no_addr < *last_unlinked)
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100875 continue;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400876 *last_unlinked = no_addr;
877 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
Bob Peterson24c73872007-07-12 16:58:50 -0500878 no_addr, -1);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100879 if (!IS_ERR(inode))
880 return inode;
881 }
882
883 rgd->rd_flags &= ~GFS2_RDF_CHECK;
884 return NULL;
885}
886
887/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888 * recent_rgrp_first - get first RG from "recent" list
889 * @sdp: The GFS2 superblock
890 * @rglast: address of the rgrp used last
891 *
892 * Returns: The first rgrp in the recent list
893 */
894
895static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
Steven Whitehousecd915492006-09-04 12:49:07 -0400896 u64 rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897{
898 struct gfs2_rgrpd *rgd = NULL;
899
900 spin_lock(&sdp->sd_rindex_spin);
901
902 if (list_empty(&sdp->sd_rindex_recent_list))
903 goto out;
904
905 if (!rglast)
906 goto first;
907
908 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100909 if (rgd->rd_addr == rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 goto out;
911 }
912
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400913first:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
915 rd_recent);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400916out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 return rgd;
919}
920
921/**
922 * recent_rgrp_next - get next RG from "recent" list
923 * @cur_rgd: current rgrp
924 * @remove:
925 *
926 * Returns: The next rgrp in the recent list
927 */
928
929static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
930 int remove)
931{
932 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
933 struct list_head *head;
934 struct gfs2_rgrpd *rgd;
935
936 spin_lock(&sdp->sd_rindex_spin);
937
938 head = &sdp->sd_rindex_recent_list;
939
940 list_for_each_entry(rgd, head, rd_recent) {
941 if (rgd == cur_rgd) {
942 if (cur_rgd->rd_recent.next != head)
943 rgd = list_entry(cur_rgd->rd_recent.next,
944 struct gfs2_rgrpd, rd_recent);
945 else
946 rgd = NULL;
947
948 if (remove)
949 list_del(&cur_rgd->rd_recent);
950
951 goto out;
952 }
953 }
954
955 rgd = NULL;
956 if (!list_empty(head))
957 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
958
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400959out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 return rgd;
962}
963
964/**
965 * recent_rgrp_add - add an RG to tail of "recent" list
966 * @new_rgd: The rgrp to add
967 *
968 */
969
970static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
971{
972 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
973 struct gfs2_rgrpd *rgd;
974 unsigned int count = 0;
975 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
976
977 spin_lock(&sdp->sd_rindex_spin);
978
979 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
980 if (rgd == new_rgd)
981 goto out;
982
983 if (++count >= max)
984 goto out;
985 }
986 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
987
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400988out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 spin_unlock(&sdp->sd_rindex_spin);
990}
991
992/**
993 * forward_rgrp_get - get an rgrp to try next from full list
994 * @sdp: The GFS2 superblock
995 *
996 * Returns: The rgrp to try next
997 */
998
999static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1000{
1001 struct gfs2_rgrpd *rgd;
1002 unsigned int journals = gfs2_jindex_size(sdp);
1003 unsigned int rg = 0, x;
1004
1005 spin_lock(&sdp->sd_rindex_spin);
1006
1007 rgd = sdp->sd_rindex_forward;
1008 if (!rgd) {
1009 if (sdp->sd_rgrps >= journals)
1010 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1011
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -04001012 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 x++, rgd = gfs2_rgrpd_get_next(rgd))
1014 /* Do Nothing */;
1015
1016 sdp->sd_rindex_forward = rgd;
1017 }
1018
1019 spin_unlock(&sdp->sd_rindex_spin);
1020
1021 return rgd;
1022}
1023
1024/**
1025 * forward_rgrp_set - set the forward rgrp pointer
1026 * @sdp: the filesystem
1027 * @rgd: The new forward rgrp
1028 *
1029 */
1030
1031static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1032{
1033 spin_lock(&sdp->sd_rindex_spin);
1034 sdp->sd_rindex_forward = rgd;
1035 spin_unlock(&sdp->sd_rindex_spin);
1036}
1037
1038/**
1039 * get_local_rgrp - Choose and lock a rgrp for allocation
1040 * @ip: the inode to reserve space for
1041 * @rgp: the chosen and locked rgrp
1042 *
1043 * Try to acquire rgrp in way which avoids contending with others.
1044 *
1045 * Returns: errno
1046 */
1047
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001048static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049{
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001050 struct inode *inode = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001051 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 struct gfs2_rgrpd *rgd, *begin = NULL;
1053 struct gfs2_alloc *al = &ip->i_alloc;
1054 int flags = LM_FLAG_TRY;
1055 int skipped = 0;
1056 int loops = 0;
1057 int error;
1058
1059 /* Try recently successful rgrps */
1060
1061 rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
1062
1063 while (rgd) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001064 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -04001065 LM_FLAG_TRY, &al->al_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066 switch (error) {
1067 case 0:
1068 if (try_rgrp_fit(rgd, al))
1069 goto out;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001070 if (rgd->rd_flags & GFS2_RDF_CHECK)
1071 inode = try_rgrp_unlink(rgd, last_unlinked);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001073 if (inode)
1074 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 rgd = recent_rgrp_next(rgd, 1);
1076 break;
1077
1078 case GLR_TRYFAILED:
1079 rgd = recent_rgrp_next(rgd, 0);
1080 break;
1081
1082 default:
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001083 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 }
1085 }
1086
1087 /* Go through full list of rgrps */
1088
1089 begin = rgd = forward_rgrp_get(sdp);
1090
1091 for (;;) {
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -04001092 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 &al->al_rgd_gh);
1094 switch (error) {
1095 case 0:
1096 if (try_rgrp_fit(rgd, al))
1097 goto out;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001098 if (rgd->rd_flags & GFS2_RDF_CHECK)
1099 inode = try_rgrp_unlink(rgd, last_unlinked);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001101 if (inode)
1102 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 break;
1104
1105 case GLR_TRYFAILED:
1106 skipped++;
1107 break;
1108
1109 default:
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001110 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111 }
1112
1113 rgd = gfs2_rgrpd_get_next(rgd);
1114 if (!rgd)
1115 rgd = gfs2_rgrpd_get_first(sdp);
1116
1117 if (rgd == begin) {
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001118 if (++loops >= 3)
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001119 return ERR_PTR(-ENOSPC);
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001120 if (!skipped)
1121 loops++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122 flags = 0;
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001123 if (loops == 2)
1124 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 }
1126 }
1127
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001128out:
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001129 ip->i_last_rg_alloc = rgd->rd_addr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130
1131 if (begin) {
1132 recent_rgrp_add(rgd);
1133 rgd = gfs2_rgrpd_get_next(rgd);
1134 if (!rgd)
1135 rgd = gfs2_rgrpd_get_first(sdp);
1136 forward_rgrp_set(sdp, rgd);
1137 }
1138
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001139 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140}
1141
1142/**
1143 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1144 * @ip: the inode to reserve space for
1145 *
1146 * Returns: errno
1147 */
1148
1149int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1150{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001151 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 struct gfs2_alloc *al = &ip->i_alloc;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001153 struct inode *inode;
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001154 int error = 0;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001155 u64 last_unlinked = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156
1157 if (gfs2_assert_warn(sdp, al->al_requested))
1158 return -EINVAL;
1159
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001160try_again:
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001161 /* We need to hold the rindex unless the inode we're using is
1162 the rindex itself, in which case it's already held. */
1163 if (ip != GFS2_I(sdp->sd_rindex))
1164 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1165 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
Robert Peterson6c532672007-05-10 16:54:38 -05001166 error = gfs2_ri_update_special(ip);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001167
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168 if (error)
1169 return error;
1170
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001171 inode = get_local_rgrp(ip, &last_unlinked);
1172 if (inode) {
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001173 if (ip != GFS2_I(sdp->sd_rindex))
1174 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001175 if (IS_ERR(inode))
1176 return PTR_ERR(inode);
1177 iput(inode);
1178 gfs2_log_flush(sdp, NULL);
1179 goto try_again;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 }
1181
1182 al->al_file = file;
1183 al->al_line = line;
1184
1185 return 0;
1186}
1187
1188/**
1189 * gfs2_inplace_release - release an inplace reservation
1190 * @ip: the inode the reservation was taken out on
1191 *
1192 * Release a reservation made by gfs2_inplace_reserve().
1193 */
1194
1195void gfs2_inplace_release(struct gfs2_inode *ip)
1196{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001197 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198 struct gfs2_alloc *al = &ip->i_alloc;
1199
1200 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1201 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1202 "al_file = %s, al_line = %u\n",
1203 al->al_alloced, al->al_requested, al->al_file,
1204 al->al_line);
1205
1206 al->al_rgd = NULL;
1207 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001208 if (ip != GFS2_I(sdp->sd_rindex))
1209 gfs2_glock_dq_uninit(&al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001210}
1211
1212/**
1213 * gfs2_get_block_type - Check a block in a RG is of given type
1214 * @rgd: the resource group holding the block
1215 * @block: the block number
1216 *
1217 * Returns: The block type (GFS2_BLKST_*)
1218 */
1219
Steven Whitehousecd915492006-09-04 12:49:07 -04001220unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221{
1222 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001223 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224 unsigned int buf;
1225 unsigned char type;
1226
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001227 length = rgd->rd_length;
1228 rgrp_block = block - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001229
1230 for (buf = 0; buf < length; buf++) {
1231 bi = rgd->rd_bits + buf;
1232 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1233 break;
1234 }
1235
1236 gfs2_assert(rgd->rd_sbd, buf < length);
1237 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1238
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001239 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001240 bi->bi_len, buf_block);
1241
1242 return type;
1243}
1244
1245/**
1246 * rgblk_search - find a block in @old_state, change allocation
1247 * state to @new_state
1248 * @rgd: the resource group descriptor
1249 * @goal: the goal block within the RG (start here to search for avail block)
1250 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1251 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1252 *
1253 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1254 * Add the found bitmap buffer to the transaction.
1255 * Set the found bits to @new_state to change block's allocation state.
1256 *
1257 * This function never fails, because we wouldn't call it unless we
1258 * know (from reservation results, etc.) that a block is available.
1259 *
1260 * Scope of @goal and returned block is just within rgrp, not the whole
1261 * filesystem.
1262 *
1263 * Returns: the block number allocated
1264 */
1265
Steven Whitehousecd915492006-09-04 12:49:07 -04001266static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001267 unsigned char old_state, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268{
1269 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001270 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -04001271 u32 blk = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272 unsigned int buf, x;
1273
1274 /* Find bitmap block that contains bits for goal block */
1275 for (buf = 0; buf < length; buf++) {
1276 bi = rgd->rd_bits + buf;
1277 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1278 break;
1279 }
1280
1281 gfs2_assert(rgd->rd_sbd, buf < length);
1282
1283 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1284 goal -= bi->bi_start * GFS2_NBBY;
1285
1286 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1287 "x <= length", instead of "x < length", because we typically start
1288 the search in the middle of a bit block, but if we can't find an
1289 allocatable block anywhere else, we want to be able wrap around and
1290 search in the first part of our first-searched bit block. */
1291 for (x = 0; x <= length; x++) {
1292 if (bi->bi_clone)
Steven Whitehousefd88de562006-05-05 16:59:11 -04001293 blk = gfs2_bitfit(rgd, bi->bi_clone + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001294 bi->bi_len, goal, old_state);
1295 else
1296 blk = gfs2_bitfit(rgd,
1297 bi->bi_bh->b_data + bi->bi_offset,
1298 bi->bi_len, goal, old_state);
1299 if (blk != BFITNOENT)
1300 break;
1301
1302 /* Try next bitmap block (wrap back to rgrp header if at end) */
1303 buf = (buf + 1) % length;
1304 bi = rgd->rd_bits + buf;
1305 goal = 0;
1306 }
1307
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001308 if (old_state != new_state) {
1309 gfs2_assert_withdraw(rgd->rd_sbd, blk != BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001311 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1312 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001313 bi->bi_len, blk, new_state);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001314 if (bi->bi_clone)
1315 gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
1316 bi->bi_len, blk, new_state);
1317 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001319 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001320}
1321
1322/**
1323 * rgblk_free - Change alloc state of given block(s)
1324 * @sdp: the filesystem
1325 * @bstart: the start of a run of blocks to free
1326 * @blen: the length of the block run (all must lie within ONE RG!)
1327 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1328 *
1329 * Returns: Resource group containing the block(s)
1330 */
1331
Steven Whitehousecd915492006-09-04 12:49:07 -04001332static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1333 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334{
1335 struct gfs2_rgrpd *rgd;
1336 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001337 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338 unsigned int buf;
1339
1340 rgd = gfs2_blk2rgrpd(sdp, bstart);
1341 if (!rgd) {
1342 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001343 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344 return NULL;
1345 }
1346
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001347 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001349 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001350
1351 while (blen--) {
1352 for (buf = 0; buf < length; buf++) {
1353 bi = rgd->rd_bits + buf;
1354 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1355 break;
1356 }
1357
1358 gfs2_assert(rgd->rd_sbd, buf < length);
1359
1360 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1361 rgrp_blk++;
1362
1363 if (!bi->bi_clone) {
1364 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001365 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001366 memcpy(bi->bi_clone + bi->bi_offset,
1367 bi->bi_bh->b_data + bi->bi_offset,
1368 bi->bi_len);
1369 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001370 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehousedd894be2006-07-27 14:29:00 -04001371 gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001372 bi->bi_len, buf_blk, new_state);
1373 }
1374
1375 return rgd;
1376}
1377
1378/**
1379 * gfs2_alloc_data - Allocate a data block
1380 * @ip: the inode to allocate the data block for
1381 *
1382 * Returns: the allocated block
1383 */
1384
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001385u64 gfs2_alloc_data(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001386{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001387 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388 struct gfs2_alloc *al = &ip->i_alloc;
1389 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001390 u32 goal, blk;
1391 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001393 if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
1394 goal = ip->i_di.di_goal_data - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395 else
1396 goal = rgd->rd_last_alloc_data;
1397
Steven Whitehousefd88de562006-05-05 16:59:11 -04001398 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001399 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400 rgd->rd_last_alloc_data = blk;
1401
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001402 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403 ip->i_di.di_goal_data = block;
1404
1405 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1406 rgd->rd_rg.rg_free--;
1407
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001408 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001409 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1410
1411 al->al_alloced++;
1412
1413 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001414 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001415
1416 spin_lock(&sdp->sd_rindex_spin);
1417 rgd->rd_free_clone--;
1418 spin_unlock(&sdp->sd_rindex_spin);
1419
1420 return block;
1421}
1422
1423/**
1424 * gfs2_alloc_meta - Allocate a metadata block
1425 * @ip: the inode to allocate the metadata block for
1426 *
1427 * Returns: the allocated block
1428 */
1429
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001430u64 gfs2_alloc_meta(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001431{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001432 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001433 struct gfs2_alloc *al = &ip->i_alloc;
1434 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001435 u32 goal, blk;
1436 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001437
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001438 if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
1439 goal = ip->i_di.di_goal_meta - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001440 else
1441 goal = rgd->rd_last_alloc_meta;
1442
Steven Whitehousefd88de562006-05-05 16:59:11 -04001443 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001444 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445 rgd->rd_last_alloc_meta = blk;
1446
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001447 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448 ip->i_di.di_goal_meta = block;
1449
1450 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1451 rgd->rd_rg.rg_free--;
1452
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001453 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001454 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1455
1456 al->al_alloced++;
1457
1458 gfs2_statfs_change(sdp, 0, -1, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001459 gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001460 gfs2_trans_add_unrevoke(sdp, block);
1461
1462 spin_lock(&sdp->sd_rindex_spin);
1463 rgd->rd_free_clone--;
1464 spin_unlock(&sdp->sd_rindex_spin);
1465
1466 return block;
1467}
1468
1469/**
1470 * gfs2_alloc_di - Allocate a dinode
1471 * @dip: the directory that the inode is going in
1472 *
1473 * Returns: the block allocated
1474 */
1475
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001476u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001477{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001478 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001479 struct gfs2_alloc *al = &dip->i_alloc;
1480 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001481 u32 blk;
1482 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001483
1484 blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
1485 GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001486 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487
1488 rgd->rd_last_alloc_meta = blk;
1489
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001490 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001491
1492 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1493 rgd->rd_rg.rg_free--;
1494 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001495 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001496 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001497 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1498
1499 al->al_alloced++;
1500
1501 gfs2_statfs_change(sdp, 0, -1, +1);
1502 gfs2_trans_add_unrevoke(sdp, block);
1503
1504 spin_lock(&sdp->sd_rindex_spin);
1505 rgd->rd_free_clone--;
1506 spin_unlock(&sdp->sd_rindex_spin);
1507
1508 return block;
1509}
1510
1511/**
1512 * gfs2_free_data - free a contiguous run of data block(s)
1513 * @ip: the inode these blocks are being freed from
1514 * @bstart: first block of a run of contiguous blocks
1515 * @blen: the length of the block run
1516 *
1517 */
1518
Steven Whitehousecd915492006-09-04 12:49:07 -04001519void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001520{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001521 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001522 struct gfs2_rgrpd *rgd;
1523
1524 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1525 if (!rgd)
1526 return;
1527
1528 rgd->rd_rg.rg_free += blen;
1529
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001530 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1532
1533 gfs2_trans_add_rg(rgd);
1534
1535 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001536 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001537}
1538
1539/**
1540 * gfs2_free_meta - free a contiguous run of data block(s)
1541 * @ip: the inode these blocks are being freed from
1542 * @bstart: first block of a run of contiguous blocks
1543 * @blen: the length of the block run
1544 *
1545 */
1546
Steven Whitehousecd915492006-09-04 12:49:07 -04001547void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001549 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001550 struct gfs2_rgrpd *rgd;
1551
1552 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1553 if (!rgd)
1554 return;
1555
1556 rgd->rd_rg.rg_free += blen;
1557
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001558 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001559 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1560
1561 gfs2_trans_add_rg(rgd);
1562
1563 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001564 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001565 gfs2_meta_wipe(ip, bstart, blen);
1566}
1567
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001568void gfs2_unlink_di(struct inode *inode)
1569{
1570 struct gfs2_inode *ip = GFS2_I(inode);
1571 struct gfs2_sbd *sdp = GFS2_SB(inode);
1572 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001573 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001574
1575 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1576 if (!rgd)
1577 return;
1578 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
1579 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1580 gfs2_trans_add_rg(rgd);
1581}
1582
Steven Whitehousecd915492006-09-04 12:49:07 -04001583static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001584{
1585 struct gfs2_sbd *sdp = rgd->rd_sbd;
1586 struct gfs2_rgrpd *tmp_rgd;
1587
1588 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1589 if (!tmp_rgd)
1590 return;
1591 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1592
1593 if (!rgd->rd_rg.rg_dinodes)
1594 gfs2_consist_rgrpd(rgd);
1595 rgd->rd_rg.rg_dinodes--;
1596 rgd->rd_rg.rg_free++;
1597
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001598 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001599 gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
1600
1601 gfs2_statfs_change(sdp, 0, +1, -1);
1602 gfs2_trans_add_rg(rgd);
1603}
1604
David Teiglandb3b94fa2006-01-16 16:50:04 +00001605
1606void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1607{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001608 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001609 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001610 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001611}
1612
1613/**
1614 * gfs2_rlist_add - add a RG to a list of RGs
1615 * @sdp: the filesystem
1616 * @rlist: the list of resource groups
1617 * @block: the block
1618 *
1619 * Figure out what RG a block belongs to and add that RG to the list
1620 *
1621 * FIXME: Don't use NOFAIL
1622 *
1623 */
1624
1625void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001626 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001627{
1628 struct gfs2_rgrpd *rgd;
1629 struct gfs2_rgrpd **tmp;
1630 unsigned int new_space;
1631 unsigned int x;
1632
1633 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1634 return;
1635
1636 rgd = gfs2_blk2rgrpd(sdp, block);
1637 if (!rgd) {
1638 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001639 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001640 return;
1641 }
1642
1643 for (x = 0; x < rlist->rl_rgrps; x++)
1644 if (rlist->rl_rgd[x] == rgd)
1645 return;
1646
1647 if (rlist->rl_rgrps == rlist->rl_space) {
1648 new_space = rlist->rl_space + 10;
1649
1650 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001651 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001652
1653 if (rlist->rl_rgd) {
1654 memcpy(tmp, rlist->rl_rgd,
1655 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1656 kfree(rlist->rl_rgd);
1657 }
1658
1659 rlist->rl_space = new_space;
1660 rlist->rl_rgd = tmp;
1661 }
1662
1663 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1664}
1665
1666/**
1667 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1668 * and initialize an array of glock holders for them
1669 * @rlist: the list of resource groups
1670 * @state: the lock state to acquire the RG lock in
1671 * @flags: the modifier flags for the holder structures
1672 *
1673 * FIXME: Don't use NOFAIL
1674 *
1675 */
1676
1677void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
1678 int flags)
1679{
1680 unsigned int x;
1681
1682 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001683 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001684 for (x = 0; x < rlist->rl_rgrps; x++)
1685 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
1686 state, flags,
1687 &rlist->rl_ghs[x]);
1688}
1689
1690/**
1691 * gfs2_rlist_free - free a resource group list
1692 * @list: the list of resource groups
1693 *
1694 */
1695
1696void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1697{
1698 unsigned int x;
1699
1700 kfree(rlist->rl_rgd);
1701
1702 if (rlist->rl_ghs) {
1703 for (x = 0; x < rlist->rl_rgrps; x++)
1704 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1705 kfree(rlist->rl_ghs);
1706 }
1707}
1708