blob: 4291375cecc6e5f4700acbba719a7d1ead1ed6cf [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonfe6c9912008-01-28 11:13:02 -06003 * Copyright (C) 2004-2008 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,
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000056 unsigned char old_state, unsigned char new_state,
57 unsigned int *n);
Steven Whitehousec8cdf472007-06-08 10:05:33 +010058
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040059/**
60 * gfs2_setbit - Set a bit in the bitmaps
61 * @buffer: the buffer that holds the bitmaps
62 * @buflen: the length (in bytes) of the buffer
63 * @block: the block to set
64 * @new_state: the new state of the block
65 *
66 */
67
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000068static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
69 unsigned char *buf2, unsigned int offset,
70 unsigned int buflen, u32 block,
71 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040072{
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000073 unsigned char *byte1, *byte2, *end, cur_state;
74 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040075
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000076 byte1 = buf1 + offset + (block / GFS2_NBBY);
77 end = buf1 + offset + buflen;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040078
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000079 BUG_ON(byte1 >= end);
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040080
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000081 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040082
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000083 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040084 gfs2_consist_rgrpd(rgd);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000085 return;
86 }
87 *byte1 ^= (cur_state ^ new_state) << bit;
88
89 if (buf2) {
90 byte2 = buf2 + offset + (block / GFS2_NBBY);
91 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
92 *byte2 ^= (cur_state ^ new_state) << bit;
93 }
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040094}
95
96/**
97 * gfs2_testbit - test a bit in the bitmaps
98 * @buffer: the buffer that holds the bitmaps
99 * @buflen: the length (in bytes) of the buffer
100 * @block: the block to read
101 *
102 */
103
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000104static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
105 const unsigned char *buffer,
106 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400107{
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000108 const unsigned char *byte, *end;
109 unsigned char cur_state;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400110 unsigned int bit;
111
112 byte = buffer + (block / GFS2_NBBY);
113 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
114 end = buffer + buflen;
115
116 gfs2_assert(rgd->rd_sbd, byte < end);
117
118 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
119
120 return cur_state;
121}
122
123/**
124 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
125 * a block in a given allocation state.
126 * @buffer: the buffer that holds the bitmaps
127 * @buflen: the length (in bytes) of the buffer
128 * @goal: start search at this block's bit-pair (within @buffer)
Bob Peterson6760bdc2007-07-24 14:09:32 -0500129 * @old_state: GFS2_BLKST_XXX the state of the block we're looking for.
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400130 *
131 * Scope of @goal and returned block number is only within this bitmap buffer,
132 * not entire rgrp or filesystem. @buffer will be offset from the actual
133 * beginning of a bitmap block buffer, skipping any header structures.
134 *
135 * Return: the block number (bitmap buffer scope) that was found
136 */
137
Steven Whitehouse110acf32008-01-29 13:30:20 +0000138static u32 gfs2_bitfit(const u8 *buffer, unsigned int buflen, u32 goal,
139 u8 old_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400140{
Steven Whitehouse110acf32008-01-29 13:30:20 +0000141 const u8 *byte;
Steven Whitehousecd915492006-09-04 12:49:07 -0400142 u32 blk = goal;
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600143 unsigned int bit, bitlong;
Steven Whitehouse110acf32008-01-29 13:30:20 +0000144 const unsigned long *plong;
145#if BITS_PER_LONG == 32
146 const unsigned long plong55 = 0x55555555;
147#else
148 const unsigned long plong55 = 0x5555555555555555;
149#endif
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400150
151 byte = buffer + (goal / GFS2_NBBY);
Steven Whitehouse110acf32008-01-29 13:30:20 +0000152 plong = (const unsigned long *)(buffer + (goal / GFS2_NBBY));
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400153 bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600154 bitlong = bit;
Steven Whitehouse110acf32008-01-29 13:30:20 +0000155
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600156 while (byte < buffer + buflen) {
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400157
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600158 if (bitlong == 0 && old_state == 0 && *plong == plong55) {
159 plong++;
160 byte += sizeof(unsigned long);
161 blk += sizeof(unsigned long) * GFS2_NBBY;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400162 continue;
163 }
Bob Petersonb3513fc2007-12-12 09:24:08 -0600164 if (((*byte >> bit) & GFS2_BIT_MASK) == old_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400165 return blk;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400166 bit += GFS2_BIT_SIZE;
167 if (bit >= 8) {
168 bit = 0;
169 byte++;
170 }
Bob Peterson5fdc2ee2007-12-11 19:00:16 -0600171 bitlong += GFS2_BIT_SIZE;
172 if (bitlong >= sizeof(unsigned long) * 8) {
173 bitlong = 0;
174 plong++;
175 }
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400176
177 blk++;
178 }
179
180 return BFITNOENT;
181}
182
183/**
184 * gfs2_bitcount - count the number of bits in a certain state
185 * @buffer: the buffer that holds the bitmaps
186 * @buflen: the length (in bytes) of the buffer
187 * @state: the state of the block we're looking for
188 *
189 * Returns: The number of bits
190 */
191
Steven Whitehouse110acf32008-01-29 13:30:20 +0000192static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
193 unsigned int buflen, u8 state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400194{
Steven Whitehouse110acf32008-01-29 13:30:20 +0000195 const u8 *byte = buffer;
196 const u8 *end = buffer + buflen;
197 const u8 state1 = state << 2;
198 const u8 state2 = state << 4;
199 const u8 state3 = state << 6;
Steven Whitehousecd915492006-09-04 12:49:07 -0400200 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400201
202 for (; byte < end; byte++) {
203 if (((*byte) & 0x03) == state)
204 count++;
205 if (((*byte) & 0x0C) == state1)
206 count++;
207 if (((*byte) & 0x30) == state2)
208 count++;
209 if (((*byte) & 0xC0) == state3)
210 count++;
211 }
212
213 return count;
214}
215
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216/**
217 * gfs2_rgrp_verify - Verify that a resource group is consistent
218 * @sdp: the filesystem
219 * @rgd: the rgrp
220 *
221 */
222
223void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
224{
225 struct gfs2_sbd *sdp = rgd->rd_sbd;
226 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100227 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -0400228 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 int buf, x;
230
Steven Whitehousecd915492006-09-04 12:49:07 -0400231 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232
233 /* Count # blocks in each of 4 possible allocation states */
234 for (buf = 0; buf < length; buf++) {
235 bi = rgd->rd_bits + buf;
236 for (x = 0; x < 4; x++)
237 count[x] += gfs2_bitcount(rgd,
238 bi->bi_bh->b_data +
239 bi->bi_offset,
240 bi->bi_len, x);
241 }
242
243 if (count[0] != rgd->rd_rg.rg_free) {
244 if (gfs2_consist_rgrpd(rgd))
245 fs_err(sdp, "free data mismatch: %u != %u\n",
246 count[0], rgd->rd_rg.rg_free);
247 return;
248 }
249
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100250 tmp = rgd->rd_data -
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251 rgd->rd_rg.rg_free -
252 rgd->rd_rg.rg_dinodes;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400253 if (count[1] + count[2] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 if (gfs2_consist_rgrpd(rgd))
255 fs_err(sdp, "used data mismatch: %u != %u\n",
256 count[1], tmp);
257 return;
258 }
259
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 if (count[3] != rgd->rd_rg.rg_dinodes) {
261 if (gfs2_consist_rgrpd(rgd))
262 fs_err(sdp, "used metadata mismatch: %u != %u\n",
263 count[3], rgd->rd_rg.rg_dinodes);
264 return;
265 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400266
267 if (count[2] > count[3]) {
268 if (gfs2_consist_rgrpd(rgd))
269 fs_err(sdp, "unlinked inodes > inodes: %u\n",
270 count[2]);
271 return;
272 }
273
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274}
275
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100276static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100278 u64 first = rgd->rd_data0;
279 u64 last = first + rgd->rd_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400280 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281}
282
283/**
284 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
285 * @sdp: The GFS2 superblock
286 * @n: The data block number
287 *
288 * Returns: The resource group, or NULL if not found
289 */
290
Steven Whitehousecd915492006-09-04 12:49:07 -0400291struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292{
293 struct gfs2_rgrpd *rgd;
294
295 spin_lock(&sdp->sd_rindex_spin);
296
297 list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100298 if (rgrp_contains_block(rgd, blk)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
300 spin_unlock(&sdp->sd_rindex_spin);
301 return rgd;
302 }
303 }
304
305 spin_unlock(&sdp->sd_rindex_spin);
306
307 return NULL;
308}
309
310/**
311 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
312 * @sdp: The GFS2 superblock
313 *
314 * Returns: The first rgrp in the filesystem
315 */
316
317struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
318{
319 gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
320 return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
321}
322
323/**
324 * gfs2_rgrpd_get_next - get the next RG
325 * @rgd: A RG
326 *
327 * Returns: The next rgrp
328 */
329
330struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
331{
332 if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
333 return NULL;
334 return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
335}
336
337static void clear_rgrpdi(struct gfs2_sbd *sdp)
338{
339 struct list_head *head;
340 struct gfs2_rgrpd *rgd;
341 struct gfs2_glock *gl;
342
343 spin_lock(&sdp->sd_rindex_spin);
344 sdp->sd_rindex_forward = NULL;
345 head = &sdp->sd_rindex_recent_list;
346 while (!list_empty(head)) {
347 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
348 list_del(&rgd->rd_recent);
349 }
350 spin_unlock(&sdp->sd_rindex_spin);
351
352 head = &sdp->sd_rindex_list;
353 while (!list_empty(head)) {
354 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
355 gl = rgd->rd_gl;
356
357 list_del(&rgd->rd_list);
358 list_del(&rgd->rd_list_mru);
359
360 if (gl) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500361 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 gfs2_glock_put(gl);
363 }
364
365 kfree(rgd->rd_bits);
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600366 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 }
368}
369
370void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
371{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000372 mutex_lock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 clear_rgrpdi(sdp);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000374 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375}
376
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100377static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
378{
379 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
380 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
381 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
382 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
383 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
384}
385
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386/**
387 * gfs2_compute_bitstructs - Compute the bitmap sizes
388 * @rgd: The resource group descriptor
389 *
390 * Calculates bitmap descriptors, one for each block that contains bitmap data
391 *
392 * Returns: errno
393 */
394
395static int compute_bitstructs(struct gfs2_rgrpd *rgd)
396{
397 struct gfs2_sbd *sdp = rgd->rd_sbd;
398 struct gfs2_bitmap *bi;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100399 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
Steven Whitehousecd915492006-09-04 12:49:07 -0400400 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 int x;
402
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400403 if (!length)
404 return -EINVAL;
405
Steven Whitehousedd894be2006-07-27 14:29:00 -0400406 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 if (!rgd->rd_bits)
408 return -ENOMEM;
409
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100410 bytes_left = rgd->rd_bitbytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411
412 for (x = 0; x < length; x++) {
413 bi = rgd->rd_bits + x;
414
415 /* small rgrp; bitmap stored completely in header block */
416 if (length == 1) {
417 bytes = bytes_left;
418 bi->bi_offset = sizeof(struct gfs2_rgrp);
419 bi->bi_start = 0;
420 bi->bi_len = bytes;
421 /* header block */
422 } else if (x == 0) {
423 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
424 bi->bi_offset = sizeof(struct gfs2_rgrp);
425 bi->bi_start = 0;
426 bi->bi_len = bytes;
427 /* last block */
428 } else if (x + 1 == length) {
429 bytes = bytes_left;
430 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 /* other blocks */
434 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500435 bytes = sdp->sd_sb.sb_bsize -
436 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100438 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 bi->bi_len = bytes;
440 }
441
442 bytes_left -= bytes;
443 }
444
445 if (bytes_left) {
446 gfs2_consist_rgrpd(rgd);
447 return -EIO;
448 }
449 bi = rgd->rd_bits + (length - 1);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100450 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451 if (gfs2_consist_rgrpd(rgd)) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100452 gfs2_rindex_print(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453 fs_err(sdp, "start=%u len=%u offset=%u\n",
454 bi->bi_start, bi->bi_len, bi->bi_offset);
455 }
456 return -EIO;
457 }
458
459 return 0;
460}
461
462/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500463 * gfs2_ri_total - Total up the file system space, according to the rindex.
464 *
465 */
466u64 gfs2_ri_total(struct gfs2_sbd *sdp)
467{
468 u64 total_data = 0;
469 struct inode *inode = sdp->sd_rindex;
470 struct gfs2_inode *ip = GFS2_I(inode);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500471 char buf[sizeof(struct gfs2_rindex)];
472 struct file_ra_state ra_state;
473 int error, rgrps;
474
475 mutex_lock(&sdp->sd_rindex_mutex);
476 file_ra_state_init(&ra_state, inode->i_mapping);
477 for (rgrps = 0;; rgrps++) {
478 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
479
480 if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size)
481 break;
482 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
483 sizeof(struct gfs2_rindex));
484 if (error != sizeof(struct gfs2_rindex))
485 break;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100486 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500487 }
488 mutex_unlock(&sdp->sd_rindex_mutex);
489 return total_data;
490}
491
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100492static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
493{
494 const struct gfs2_rindex *str = buf;
495
496 rgd->rd_addr = be64_to_cpu(str->ri_addr);
497 rgd->rd_length = be32_to_cpu(str->ri_length);
498 rgd->rd_data0 = be64_to_cpu(str->ri_data0);
499 rgd->rd_data = be32_to_cpu(str->ri_data);
500 rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
501}
502
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500503/**
Robert Peterson6c532672007-05-10 16:54:38 -0500504 * read_rindex_entry - Pull in a new resource index entry from the disk
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 * @gl: The glock covering the rindex inode
506 *
Robert Peterson6c532672007-05-10 16:54:38 -0500507 * Returns: 0 on success, error code otherwise
508 */
509
510static int read_rindex_entry(struct gfs2_inode *ip,
511 struct file_ra_state *ra_state)
512{
513 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
514 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
515 char buf[sizeof(struct gfs2_rindex)];
516 int error;
517 struct gfs2_rgrpd *rgd;
518
519 error = gfs2_internal_read(ip, ra_state, buf, &pos,
520 sizeof(struct gfs2_rindex));
521 if (!error)
522 return 0;
523 if (error != sizeof(struct gfs2_rindex)) {
524 if (error > 0)
525 error = -EIO;
526 return error;
527 }
528
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600529 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
Robert Peterson6c532672007-05-10 16:54:38 -0500530 error = -ENOMEM;
531 if (!rgd)
532 return error;
533
534 mutex_init(&rgd->rd_mutex);
535 lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
536 rgd->rd_sbd = sdp;
537
538 list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
539 list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
540
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100541 gfs2_rindex_in(rgd, buf);
Robert Peterson6c532672007-05-10 16:54:38 -0500542 error = compute_bitstructs(rgd);
543 if (error)
544 return error;
545
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100546 error = gfs2_glock_get(sdp, rgd->rd_addr,
Robert Peterson6c532672007-05-10 16:54:38 -0500547 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
548 if (error)
549 return error;
550
551 rgd->rd_gl->gl_object = rgd;
Bob Petersoncf45b752008-01-31 10:31:39 -0600552 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100553 rgd->rd_flags |= GFS2_RDF_CHECK;
Robert Peterson6c532672007-05-10 16:54:38 -0500554 return error;
555}
556
557/**
558 * gfs2_ri_update - Pull in a new resource index from the disk
559 * @ip: pointer to the rindex inode
560 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561 * Returns: 0 on successful update, error code otherwise
562 */
563
564static int gfs2_ri_update(struct gfs2_inode *ip)
565{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400566 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
567 struct inode *inode = &ip->i_inode;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000568 struct file_ra_state ra_state;
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500569 u64 rgrp_count = ip->i_di.di_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570 int error;
571
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500572 if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573 gfs2_consist_inode(ip);
574 return -EIO;
575 }
576
577 clear_rgrpdi(sdp);
578
Steven Whitehousef42faf42006-01-30 18:34:10 +0000579 file_ra_state_init(&ra_state, inode->i_mapping);
Robert Petersoncd81a4b2007-05-14 12:42:18 -0500580 for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
Robert Peterson6c532672007-05-10 16:54:38 -0500581 error = read_rindex_entry(ip, &ra_state);
582 if (error) {
583 clear_rgrpdi(sdp);
584 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586 }
587
Bob Petersoncf45b752008-01-31 10:31:39 -0600588 sdp->sd_rindex_uptodate = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589 return 0;
Robert Peterson6c532672007-05-10 16:54:38 -0500590}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591
Robert Peterson6c532672007-05-10 16:54:38 -0500592/**
593 * gfs2_ri_update_special - Pull in a new resource index from the disk
594 *
595 * This is a special version that's safe to call from gfs2_inplace_reserve_i.
596 * In this case we know that we don't have any resource groups in memory yet.
597 *
598 * @ip: pointer to the rindex inode
599 *
600 * Returns: 0 on successful update, error code otherwise
601 */
602static int gfs2_ri_update_special(struct gfs2_inode *ip)
603{
604 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
605 struct inode *inode = &ip->i_inode;
606 struct file_ra_state ra_state;
607 int error;
608
609 file_ra_state_init(&ra_state, inode->i_mapping);
610 for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
611 /* Ignore partials */
612 if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
613 ip->i_di.di_size)
614 break;
615 error = read_rindex_entry(ip, &ra_state);
616 if (error) {
617 clear_rgrpdi(sdp);
618 return error;
619 }
620 }
621
Bob Petersoncf45b752008-01-31 10:31:39 -0600622 sdp->sd_rindex_uptodate = 1;
Robert Peterson6c532672007-05-10 16:54:38 -0500623 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624}
625
626/**
627 * gfs2_rindex_hold - Grab a lock on the rindex
628 * @sdp: The GFS2 superblock
629 * @ri_gh: the glock holder
630 *
631 * We grab a lock on the rindex inode to make sure that it doesn't
632 * change whilst we are performing an operation. We keep this lock
633 * for quite long periods of time compared to other locks. This
634 * doesn't matter, since it is shared and it is very, very rarely
635 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
636 *
637 * This makes sure that we're using the latest copy of the resource index
638 * special file, which might have been updated if someone expanded the
639 * filesystem (via gfs2_grow utility), which adds new resource groups.
640 *
641 * Returns: 0 on success, error code otherwise
642 */
643
644int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
645{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400646 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647 struct gfs2_glock *gl = ip->i_gl;
648 int error;
649
650 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
651 if (error)
652 return error;
653
654 /* Read new copy from disk if we don't have the latest */
Bob Petersoncf45b752008-01-31 10:31:39 -0600655 if (!sdp->sd_rindex_uptodate) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000656 mutex_lock(&sdp->sd_rindex_mutex);
Bob Petersoncf45b752008-01-31 10:31:39 -0600657 if (!sdp->sd_rindex_uptodate) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 error = gfs2_ri_update(ip);
659 if (error)
660 gfs2_glock_dq_uninit(ri_gh);
661 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000662 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 }
664
665 return error;
666}
667
Bob Peterson42d52e32008-01-28 18:38:07 -0600668static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100669{
670 const struct gfs2_rgrp *str = buf;
Bob Peterson42d52e32008-01-28 18:38:07 -0600671 struct gfs2_rgrp_host *rg = &rgd->rd_rg;
672 u32 rg_flags;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100673
Bob Peterson42d52e32008-01-28 18:38:07 -0600674 rg_flags = be32_to_cpu(str->rg_flags);
675 if (rg_flags & GFS2_RGF_NOALLOC)
676 rgd->rd_flags |= GFS2_RDF_NOALLOC;
677 else
678 rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100679 rg->rg_free = be32_to_cpu(str->rg_free);
680 rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
681 rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
682}
683
Bob Peterson42d52e32008-01-28 18:38:07 -0600684static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100685{
686 struct gfs2_rgrp *str = buf;
Bob Peterson42d52e32008-01-28 18:38:07 -0600687 struct gfs2_rgrp_host *rg = &rgd->rd_rg;
688 u32 rg_flags = 0;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100689
Bob Peterson42d52e32008-01-28 18:38:07 -0600690 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
691 rg_flags |= GFS2_RGF_NOALLOC;
692 str->rg_flags = cpu_to_be32(rg_flags);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100693 str->rg_free = cpu_to_be32(rg->rg_free);
694 str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
695 str->__pad = cpu_to_be32(0);
696 str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
697 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
698}
699
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700/**
701 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
702 * @rgd: the struct gfs2_rgrpd describing the RG to read in
703 *
704 * Read in all of a Resource Group's header and bitmap blocks.
705 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
706 *
707 * Returns: errno
708 */
709
710int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
711{
712 struct gfs2_sbd *sdp = rgd->rd_sbd;
713 struct gfs2_glock *gl = rgd->rd_gl;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100714 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 struct gfs2_bitmap *bi;
716 unsigned int x, y;
717 int error;
718
Steven Whitehousef55ab262006-02-21 12:51:39 +0000719 mutex_lock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720
721 spin_lock(&sdp->sd_rindex_spin);
722 if (rgd->rd_bh_count) {
723 rgd->rd_bh_count++;
724 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000725 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 return 0;
727 }
728 spin_unlock(&sdp->sd_rindex_spin);
729
730 for (x = 0; x < length; x++) {
731 bi = rgd->rd_bits + x;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100732 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 if (error)
734 goto fail;
735 }
736
737 for (y = length; y--;) {
738 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400739 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 if (error)
741 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400742 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 GFS2_METATYPE_RG)) {
744 error = -EIO;
745 goto fail;
746 }
747 }
748
Bob Petersoncf45b752008-01-31 10:31:39 -0600749 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
Bob Peterson42d52e32008-01-28 18:38:07 -0600750 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
Bob Petersoncf45b752008-01-31 10:31:39 -0600751 rgd->rd_flags |= GFS2_RDF_UPTODATE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752 }
753
754 spin_lock(&sdp->sd_rindex_spin);
755 rgd->rd_free_clone = rgd->rd_rg.rg_free;
756 rgd->rd_bh_count++;
757 spin_unlock(&sdp->sd_rindex_spin);
758
Steven Whitehousef55ab262006-02-21 12:51:39 +0000759 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760
761 return 0;
762
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400763fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 while (x--) {
765 bi = rgd->rd_bits + x;
766 brelse(bi->bi_bh);
767 bi->bi_bh = NULL;
768 gfs2_assert_warn(sdp, !bi->bi_clone);
769 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000770 mutex_unlock(&rgd->rd_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771
772 return error;
773}
774
775void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
776{
777 struct gfs2_sbd *sdp = rgd->rd_sbd;
778
779 spin_lock(&sdp->sd_rindex_spin);
780 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
781 rgd->rd_bh_count++;
782 spin_unlock(&sdp->sd_rindex_spin);
783}
784
785/**
786 * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
787 * @rgd: the struct gfs2_rgrpd describing the RG to read in
788 *
789 */
790
791void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
792{
793 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100794 int x, length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795
796 spin_lock(&sdp->sd_rindex_spin);
797 gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
798 if (--rgd->rd_bh_count) {
799 spin_unlock(&sdp->sd_rindex_spin);
800 return;
801 }
802
803 for (x = 0; x < length; x++) {
804 struct gfs2_bitmap *bi = rgd->rd_bits + x;
805 kfree(bi->bi_clone);
806 bi->bi_clone = NULL;
807 brelse(bi->bi_bh);
808 bi->bi_bh = NULL;
809 }
810
811 spin_unlock(&sdp->sd_rindex_spin);
812}
813
814void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
815{
816 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100817 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818 unsigned int x;
819
820 for (x = 0; x < length; x++) {
821 struct gfs2_bitmap *bi = rgd->rd_bits + x;
822 if (!bi->bi_clone)
823 continue;
824 memcpy(bi->bi_clone + bi->bi_offset,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400825 bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 }
827
828 spin_lock(&sdp->sd_rindex_spin);
829 rgd->rd_free_clone = rgd->rd_rg.rg_free;
830 spin_unlock(&sdp->sd_rindex_spin);
831}
832
833/**
834 * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
835 * @ip: the incore GFS2 inode structure
836 *
837 * Returns: the struct gfs2_alloc
838 */
839
840struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
841{
Steven Whitehouse6dbd8222008-01-10 15:18:55 +0000842 BUG_ON(ip->i_alloc != NULL);
843 ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
844 return ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845}
846
847/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 * try_rgrp_fit - See if a given reservation will fit in a given RG
849 * @rgd: the RG data
850 * @al: the struct gfs2_alloc structure describing the reservation
851 *
852 * If there's room for the requested blocks to be allocated from the RG:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 * Sets the $al_rgd field in @al.
854 *
855 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
856 */
857
858static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
859{
860 struct gfs2_sbd *sdp = rgd->rd_sbd;
861 int ret = 0;
862
Bob Peterson42d52e32008-01-28 18:38:07 -0600863 if (rgd->rd_flags & GFS2_RDF_NOALLOC)
Steven Whitehousea43a4902007-04-02 10:48:17 +0100864 return 0;
865
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 spin_lock(&sdp->sd_rindex_spin);
867 if (rgd->rd_free_clone >= al->al_requested) {
868 al->al_rgd = rgd;
869 ret = 1;
870 }
871 spin_unlock(&sdp->sd_rindex_spin);
872
873 return ret;
874}
875
876/**
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100877 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
878 * @rgd: The rgrp
879 *
880 * Returns: The inode, if one has been found
881 */
882
883static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
884{
885 struct inode *inode;
Bob Peterson6760bdc2007-07-24 14:09:32 -0500886 u32 goal = 0, block;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400887 u64 no_addr;
Bob Peterson5f3eae72007-08-08 16:52:09 -0500888 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000889 unsigned int n;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100890
891 for(;;) {
Bob Peterson24c73872007-07-12 16:58:50 -0500892 if (goal >= rgd->rd_data)
893 break;
Bob Peterson5f3eae72007-08-08 16:52:09 -0500894 down_write(&sdp->sd_log_flush_lock);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000895 n = 1;
Bob Peterson6760bdc2007-07-24 14:09:32 -0500896 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000897 GFS2_BLKST_UNLINKED, &n);
Bob Peterson5f3eae72007-08-08 16:52:09 -0500898 up_write(&sdp->sd_log_flush_lock);
Bob Peterson6760bdc2007-07-24 14:09:32 -0500899 if (block == BFITNOENT)
Bob Peterson24c73872007-07-12 16:58:50 -0500900 break;
Bob Peterson6760bdc2007-07-24 14:09:32 -0500901 /* rgblk_search can return a block < goal, so we need to
902 keep it marching forward. */
903 no_addr = block + rgd->rd_data0;
Bob Peterson24c73872007-07-12 16:58:50 -0500904 goal++;
Bob Peterson6760bdc2007-07-24 14:09:32 -0500905 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100906 continue;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400907 *last_unlinked = no_addr;
908 inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500909 no_addr, -1, 1);
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100910 if (!IS_ERR(inode))
911 return inode;
912 }
913
914 rgd->rd_flags &= ~GFS2_RDF_CHECK;
915 return NULL;
916}
917
918/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919 * recent_rgrp_first - get first RG from "recent" list
920 * @sdp: The GFS2 superblock
921 * @rglast: address of the rgrp used last
922 *
923 * Returns: The first rgrp in the recent list
924 */
925
926static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
Steven Whitehousecd915492006-09-04 12:49:07 -0400927 u64 rglast)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928{
Steven Whitehousece276b02008-02-06 09:25:45 +0000929 struct gfs2_rgrpd *rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930
931 spin_lock(&sdp->sd_rindex_spin);
932
Steven Whitehousece276b02008-02-06 09:25:45 +0000933 if (rglast) {
934 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
935 if (rgrp_contains_block(rgd, rglast))
936 goto out;
937 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 }
Steven Whitehousece276b02008-02-06 09:25:45 +0000939 rgd = NULL;
940 if (!list_empty(&sdp->sd_rindex_recent_list))
941 rgd = list_entry(sdp->sd_rindex_recent_list.next,
942 struct gfs2_rgrpd, rd_recent);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400943out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945 return rgd;
946}
947
948/**
949 * recent_rgrp_next - get next RG from "recent" list
950 * @cur_rgd: current rgrp
951 * @remove:
952 *
953 * Returns: The next rgrp in the recent list
954 */
955
956static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
957 int remove)
958{
959 struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
960 struct list_head *head;
961 struct gfs2_rgrpd *rgd;
962
963 spin_lock(&sdp->sd_rindex_spin);
964
965 head = &sdp->sd_rindex_recent_list;
966
967 list_for_each_entry(rgd, head, rd_recent) {
968 if (rgd == cur_rgd) {
969 if (cur_rgd->rd_recent.next != head)
970 rgd = list_entry(cur_rgd->rd_recent.next,
971 struct gfs2_rgrpd, rd_recent);
972 else
973 rgd = NULL;
974
975 if (remove)
976 list_del(&cur_rgd->rd_recent);
977
978 goto out;
979 }
980 }
981
982 rgd = NULL;
983 if (!list_empty(head))
984 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
985
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400986out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 return rgd;
989}
990
991/**
992 * recent_rgrp_add - add an RG to tail of "recent" list
993 * @new_rgd: The rgrp to add
994 *
995 */
996
997static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
998{
999 struct gfs2_sbd *sdp = new_rgd->rd_sbd;
1000 struct gfs2_rgrpd *rgd;
1001 unsigned int count = 0;
1002 unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
1003
1004 spin_lock(&sdp->sd_rindex_spin);
1005
1006 list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
1007 if (rgd == new_rgd)
1008 goto out;
1009
1010 if (++count >= max)
1011 goto out;
1012 }
1013 list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
1014
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001015out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 spin_unlock(&sdp->sd_rindex_spin);
1017}
1018
1019/**
1020 * forward_rgrp_get - get an rgrp to try next from full list
1021 * @sdp: The GFS2 superblock
1022 *
1023 * Returns: The rgrp to try next
1024 */
1025
1026static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
1027{
1028 struct gfs2_rgrpd *rgd;
1029 unsigned int journals = gfs2_jindex_size(sdp);
1030 unsigned int rg = 0, x;
1031
1032 spin_lock(&sdp->sd_rindex_spin);
1033
1034 rgd = sdp->sd_rindex_forward;
1035 if (!rgd) {
1036 if (sdp->sd_rgrps >= journals)
1037 rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
1038
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -04001039 for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 x++, rgd = gfs2_rgrpd_get_next(rgd))
1041 /* Do Nothing */;
1042
1043 sdp->sd_rindex_forward = rgd;
1044 }
1045
1046 spin_unlock(&sdp->sd_rindex_spin);
1047
1048 return rgd;
1049}
1050
1051/**
1052 * forward_rgrp_set - set the forward rgrp pointer
1053 * @sdp: the filesystem
1054 * @rgd: The new forward rgrp
1055 *
1056 */
1057
1058static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
1059{
1060 spin_lock(&sdp->sd_rindex_spin);
1061 sdp->sd_rindex_forward = rgd;
1062 spin_unlock(&sdp->sd_rindex_spin);
1063}
1064
1065/**
1066 * get_local_rgrp - Choose and lock a rgrp for allocation
1067 * @ip: the inode to reserve space for
1068 * @rgp: the chosen and locked rgrp
1069 *
1070 * Try to acquire rgrp in way which avoids contending with others.
1071 *
1072 * Returns: errno
1073 */
1074
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001075static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076{
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001077 struct inode *inode = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001078 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 struct gfs2_rgrpd *rgd, *begin = NULL;
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001080 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081 int flags = LM_FLAG_TRY;
1082 int skipped = 0;
1083 int loops = 0;
Abhijith Das292c8c12007-11-29 14:13:54 -06001084 int error, rg_locked;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001085
1086 /* Try recently successful rgrps */
1087
Steven Whitehousece276b02008-02-06 09:25:45 +00001088 rgd = recent_rgrp_first(sdp, ip->i_goal);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089
1090 while (rgd) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001091 rg_locked = 0;
1092
1093 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1094 rg_locked = 1;
1095 error = 0;
1096 } else {
1097 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1098 LM_FLAG_TRY, &al->al_rgd_gh);
1099 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 switch (error) {
1101 case 0:
1102 if (try_rgrp_fit(rgd, al))
1103 goto out;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001104 if (rgd->rd_flags & GFS2_RDF_CHECK)
1105 inode = try_rgrp_unlink(rgd, last_unlinked);
Abhijith Das292c8c12007-11-29 14:13:54 -06001106 if (!rg_locked)
1107 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001108 if (inode)
1109 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001110 rgd = recent_rgrp_next(rgd, 1);
1111 break;
1112
1113 case GLR_TRYFAILED:
1114 rgd = recent_rgrp_next(rgd, 0);
1115 break;
1116
1117 default:
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001118 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 }
1120 }
1121
1122 /* Go through full list of rgrps */
1123
1124 begin = rgd = forward_rgrp_get(sdp);
1125
1126 for (;;) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001127 rg_locked = 0;
1128
1129 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1130 rg_locked = 1;
1131 error = 0;
1132 } else {
1133 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
1134 &al->al_rgd_gh);
1135 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 switch (error) {
1137 case 0:
1138 if (try_rgrp_fit(rgd, al))
1139 goto out;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001140 if (rgd->rd_flags & GFS2_RDF_CHECK)
1141 inode = try_rgrp_unlink(rgd, last_unlinked);
Abhijith Das292c8c12007-11-29 14:13:54 -06001142 if (!rg_locked)
1143 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001144 if (inode)
1145 return inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146 break;
1147
1148 case GLR_TRYFAILED:
1149 skipped++;
1150 break;
1151
1152 default:
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001153 return ERR_PTR(error);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154 }
1155
1156 rgd = gfs2_rgrpd_get_next(rgd);
1157 if (!rgd)
1158 rgd = gfs2_rgrpd_get_first(sdp);
1159
1160 if (rgd == begin) {
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001161 if (++loops >= 3)
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001162 return ERR_PTR(-ENOSPC);
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001163 if (!skipped)
1164 loops++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 flags = 0;
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001166 if (loops == 2)
1167 gfs2_log_flush(sdp, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168 }
1169 }
1170
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001171out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172 if (begin) {
1173 recent_rgrp_add(rgd);
1174 rgd = gfs2_rgrpd_get_next(rgd);
1175 if (!rgd)
1176 rgd = gfs2_rgrpd_get_first(sdp);
1177 forward_rgrp_set(sdp, rgd);
1178 }
1179
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001180 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181}
1182
1183/**
1184 * gfs2_inplace_reserve_i - Reserve space in the filesystem
1185 * @ip: the inode to reserve space for
1186 *
1187 * Returns: errno
1188 */
1189
1190int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
1191{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001192 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001193 struct gfs2_alloc *al = ip->i_alloc;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001194 struct inode *inode;
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001195 int error = 0;
Bob Peterson6760bdc2007-07-24 14:09:32 -05001196 u64 last_unlinked = NO_BLOCK;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197
1198 if (gfs2_assert_warn(sdp, al->al_requested))
1199 return -EINVAL;
1200
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001201try_again:
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001202 /* We need to hold the rindex unless the inode we're using is
1203 the rindex itself, in which case it's already held. */
1204 if (ip != GFS2_I(sdp->sd_rindex))
1205 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
1206 else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
Robert Peterson6c532672007-05-10 16:54:38 -05001207 error = gfs2_ri_update_special(ip);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001208
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 if (error)
1210 return error;
1211
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001212 inode = get_local_rgrp(ip, &last_unlinked);
1213 if (inode) {
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001214 if (ip != GFS2_I(sdp->sd_rindex))
1215 gfs2_glock_dq_uninit(&al->al_ri_gh);
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001216 if (IS_ERR(inode))
1217 return PTR_ERR(inode);
1218 iput(inode);
1219 gfs2_log_flush(sdp, NULL);
1220 goto try_again;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221 }
1222
1223 al->al_file = file;
1224 al->al_line = line;
1225
1226 return 0;
1227}
1228
1229/**
1230 * gfs2_inplace_release - release an inplace reservation
1231 * @ip: the inode the reservation was taken out on
1232 *
1233 * Release a reservation made by gfs2_inplace_reserve().
1234 */
1235
1236void gfs2_inplace_release(struct gfs2_inode *ip)
1237{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001238 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001239 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001240
1241 if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
1242 fs_warn(sdp, "al_alloced = %u, al_requested = %u "
1243 "al_file = %s, al_line = %u\n",
1244 al->al_alloced, al->al_requested, al->al_file,
1245 al->al_line);
1246
1247 al->al_rgd = NULL;
Abhijith Das292c8c12007-11-29 14:13:54 -06001248 if (al->al_rgd_gh.gh_gl)
1249 gfs2_glock_dq_uninit(&al->al_rgd_gh);
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001250 if (ip != GFS2_I(sdp->sd_rindex))
1251 gfs2_glock_dq_uninit(&al->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252}
1253
1254/**
1255 * gfs2_get_block_type - Check a block in a RG is of given type
1256 * @rgd: the resource group holding the block
1257 * @block: the block number
1258 *
1259 * Returns: The block type (GFS2_BLKST_*)
1260 */
1261
Steven Whitehousecd915492006-09-04 12:49:07 -04001262unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263{
1264 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001265 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001266 unsigned int buf;
1267 unsigned char type;
1268
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001269 length = rgd->rd_length;
1270 rgrp_block = block - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271
1272 for (buf = 0; buf < length; buf++) {
1273 bi = rgd->rd_bits + buf;
1274 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1275 break;
1276 }
1277
1278 gfs2_assert(rgd->rd_sbd, buf < length);
1279 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1280
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001281 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001282 bi->bi_len, buf_block);
1283
1284 return type;
1285}
1286
1287/**
1288 * rgblk_search - find a block in @old_state, change allocation
1289 * state to @new_state
1290 * @rgd: the resource group descriptor
1291 * @goal: the goal block within the RG (start here to search for avail block)
1292 * @old_state: GFS2_BLKST_XXX the before-allocation state to find
1293 * @new_state: GFS2_BLKST_XXX the after-allocation block state
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001294 * @n: The extent length
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295 *
1296 * Walk rgrp's bitmap to find bits that represent a block in @old_state.
1297 * Add the found bitmap buffer to the transaction.
1298 * Set the found bits to @new_state to change block's allocation state.
1299 *
1300 * This function never fails, because we wouldn't call it unless we
1301 * know (from reservation results, etc.) that a block is available.
1302 *
1303 * Scope of @goal and returned block is just within rgrp, not the whole
1304 * filesystem.
1305 *
1306 * Returns: the block number allocated
1307 */
1308
Steven Whitehousecd915492006-09-04 12:49:07 -04001309static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001310 unsigned char old_state, unsigned char new_state,
1311 unsigned int *n)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312{
1313 struct gfs2_bitmap *bi = NULL;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001314 const u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -04001315 u32 blk = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001316 unsigned int buf, x;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001317 const unsigned int elen = *n;
1318 const u8 *buffer;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001320 *n = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001321 /* Find bitmap block that contains bits for goal block */
1322 for (buf = 0; buf < length; buf++) {
1323 bi = rgd->rd_bits + buf;
1324 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1325 break;
1326 }
1327
1328 gfs2_assert(rgd->rd_sbd, buf < length);
1329
1330 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1331 goal -= bi->bi_start * GFS2_NBBY;
1332
1333 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1334 "x <= length", instead of "x < length", because we typically start
1335 the search in the middle of a bit block, but if we can't find an
1336 allocatable block anywhere else, we want to be able wrap around and
1337 search in the first part of our first-searched bit block. */
1338 for (x = 0; x <= length; x++) {
Bob Peterson5f3eae72007-08-08 16:52:09 -05001339 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1340 bitmaps, so we must search the originals for that. */
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001341 buffer = bi->bi_bh->b_data + bi->bi_offset;
Bob Peterson5f3eae72007-08-08 16:52:09 -05001342 if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
Steven Whitehouse110acf32008-01-29 13:30:20 +00001343 buffer = bi->bi_clone + bi->bi_offset;
1344
1345 blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346 if (blk != BFITNOENT)
1347 break;
1348
1349 /* Try next bitmap block (wrap back to rgrp header if at end) */
1350 buf = (buf + 1) % length;
1351 bi = rgd->rd_bits + buf;
1352 goal = 0;
1353 }
1354
Bob Peterson6760bdc2007-07-24 14:09:32 -05001355 if (blk != BFITNOENT && old_state != new_state) {
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001356 *n = 1;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001357 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001358 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001359 bi->bi_len, blk, new_state);
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001360 goal = blk;
1361 while (*n < elen) {
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001362 goal++;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001363 if (goal >= (bi->bi_len * GFS2_NBBY))
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001364 break;
1365 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1366 GFS2_BLKST_FREE)
1367 break;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001368 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +00001369 bi->bi_offset, bi->bi_len, goal,
1370 new_state);
1371 (*n)++;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001372 }
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001373 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001374
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001375 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376}
1377
1378/**
1379 * rgblk_free - Change alloc state of given block(s)
1380 * @sdp: the filesystem
1381 * @bstart: the start of a run of blocks to free
1382 * @blen: the length of the block run (all must lie within ONE RG!)
1383 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1384 *
1385 * Returns: Resource group containing the block(s)
1386 */
1387
Steven Whitehousecd915492006-09-04 12:49:07 -04001388static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1389 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001390{
1391 struct gfs2_rgrpd *rgd;
1392 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001393 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394 unsigned int buf;
1395
1396 rgd = gfs2_blk2rgrpd(sdp, bstart);
1397 if (!rgd) {
1398 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001399 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400 return NULL;
1401 }
1402
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001403 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001405 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001406
1407 while (blen--) {
1408 for (buf = 0; buf < length; buf++) {
1409 bi = rgd->rd_bits + buf;
1410 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1411 break;
1412 }
1413
1414 gfs2_assert(rgd->rd_sbd, buf < length);
1415
1416 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1417 rgrp_blk++;
1418
1419 if (!bi->bi_clone) {
1420 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001421 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422 memcpy(bi->bi_clone + bi->bi_offset,
1423 bi->bi_bh->b_data + bi->bi_offset,
1424 bi->bi_len);
1425 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001426 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001427 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001428 bi->bi_len, buf_blk, new_state);
1429 }
1430
1431 return rgd;
1432}
1433
1434/**
Steven Whitehouse16394312008-02-01 14:52:30 +00001435 * gfs2_alloc_block - Allocate a block
1436 * @ip: the inode to allocate the block for
David Teiglandb3b94fa2006-01-16 16:50:04 +00001437 *
1438 * Returns: the allocated block
1439 */
1440
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001441u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001442{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001443 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001444 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001446 u32 goal, blk;
1447 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448
Steven Whitehousece276b02008-02-06 09:25:45 +00001449 if (rgrp_contains_block(rgd, ip->i_goal))
1450 goal = ip->i_goal - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001451 else
Steven Whitehouseac576cc2008-02-01 10:34:15 +00001452 goal = rgd->rd_last_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001453
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001454 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001455 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001457 rgd->rd_last_alloc = blk;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001458 block = rgd->rd_data0 + blk;
Steven Whitehousece276b02008-02-06 09:25:45 +00001459 ip->i_goal = block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001460
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001461 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free >= *n);
1462 rgd->rd_rg.rg_free -= *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001463
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001464 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001465 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001467 al->al_alloced += *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001468
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001469 gfs2_statfs_change(sdp, 0, -*n, 0);
1470 gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001471
1472 spin_lock(&sdp->sd_rindex_spin);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001473 rgd->rd_free_clone -= *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001474 spin_unlock(&sdp->sd_rindex_spin);
1475
1476 return block;
1477}
1478
1479/**
1480 * gfs2_alloc_di - Allocate a dinode
1481 * @dip: the directory that the inode is going in
1482 *
1483 * Returns: the block allocated
1484 */
1485
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001486u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001488 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001489 struct gfs2_alloc *al = dip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001490 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001491 u32 blk;
1492 u64 block;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001493 unsigned int n = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001494
Steven Whitehouseac576cc2008-02-01 10:34:15 +00001495 blk = rgblk_search(rgd, rgd->rd_last_alloc,
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001496 GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001497 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498
Steven Whitehouseac576cc2008-02-01 10:34:15 +00001499 rgd->rd_last_alloc = blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001501 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001502
1503 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1504 rgd->rd_rg.rg_free--;
1505 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001506 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001507 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001508 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001509
1510 al->al_alloced++;
1511
1512 gfs2_statfs_change(sdp, 0, -1, +1);
Steven Whitehouse5731be52008-02-01 13:16:55 +00001513 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001514
1515 spin_lock(&sdp->sd_rindex_spin);
1516 rgd->rd_free_clone--;
1517 spin_unlock(&sdp->sd_rindex_spin);
1518
1519 return block;
1520}
1521
1522/**
1523 * gfs2_free_data - free a contiguous run of data block(s)
1524 * @ip: the inode these blocks are being freed from
1525 * @bstart: first block of a run of contiguous blocks
1526 * @blen: the length of the block run
1527 *
1528 */
1529
Steven Whitehousecd915492006-09-04 12:49:07 -04001530void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001532 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533 struct gfs2_rgrpd *rgd;
1534
1535 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1536 if (!rgd)
1537 return;
1538
1539 rgd->rd_rg.rg_free += blen;
1540
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001541 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001542 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001543
1544 gfs2_trans_add_rg(rgd);
1545
1546 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001547 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548}
1549
1550/**
1551 * gfs2_free_meta - free a contiguous run of data block(s)
1552 * @ip: the inode these blocks are being freed from
1553 * @bstart: first block of a run of contiguous blocks
1554 * @blen: the length of the block run
1555 *
1556 */
1557
Steven Whitehousecd915492006-09-04 12:49:07 -04001558void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001559{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001560 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001561 struct gfs2_rgrpd *rgd;
1562
1563 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1564 if (!rgd)
1565 return;
1566
1567 rgd->rd_rg.rg_free += blen;
1568
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001569 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001570 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001571
1572 gfs2_trans_add_rg(rgd);
1573
1574 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001575 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001576 gfs2_meta_wipe(ip, bstart, blen);
1577}
1578
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001579void gfs2_unlink_di(struct inode *inode)
1580{
1581 struct gfs2_inode *ip = GFS2_I(inode);
1582 struct gfs2_sbd *sdp = GFS2_SB(inode);
1583 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001584 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001585
1586 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1587 if (!rgd)
1588 return;
1589 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001590 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001591 gfs2_trans_add_rg(rgd);
1592}
1593
Steven Whitehousecd915492006-09-04 12:49:07 -04001594static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001595{
1596 struct gfs2_sbd *sdp = rgd->rd_sbd;
1597 struct gfs2_rgrpd *tmp_rgd;
1598
1599 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1600 if (!tmp_rgd)
1601 return;
1602 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1603
1604 if (!rgd->rd_rg.rg_dinodes)
1605 gfs2_consist_rgrpd(rgd);
1606 rgd->rd_rg.rg_dinodes--;
1607 rgd->rd_rg.rg_free++;
1608
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001609 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001610 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001611
1612 gfs2_statfs_change(sdp, 0, +1, -1);
1613 gfs2_trans_add_rg(rgd);
1614}
1615
David Teiglandb3b94fa2006-01-16 16:50:04 +00001616
1617void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1618{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001619 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001620 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001621 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001622}
1623
1624/**
1625 * gfs2_rlist_add - add a RG to a list of RGs
1626 * @sdp: the filesystem
1627 * @rlist: the list of resource groups
1628 * @block: the block
1629 *
1630 * Figure out what RG a block belongs to and add that RG to the list
1631 *
1632 * FIXME: Don't use NOFAIL
1633 *
1634 */
1635
1636void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001637 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001638{
1639 struct gfs2_rgrpd *rgd;
1640 struct gfs2_rgrpd **tmp;
1641 unsigned int new_space;
1642 unsigned int x;
1643
1644 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1645 return;
1646
1647 rgd = gfs2_blk2rgrpd(sdp, block);
1648 if (!rgd) {
1649 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001650 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001651 return;
1652 }
1653
1654 for (x = 0; x < rlist->rl_rgrps; x++)
1655 if (rlist->rl_rgd[x] == rgd)
1656 return;
1657
1658 if (rlist->rl_rgrps == rlist->rl_space) {
1659 new_space = rlist->rl_space + 10;
1660
1661 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001662 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001663
1664 if (rlist->rl_rgd) {
1665 memcpy(tmp, rlist->rl_rgd,
1666 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1667 kfree(rlist->rl_rgd);
1668 }
1669
1670 rlist->rl_space = new_space;
1671 rlist->rl_rgd = tmp;
1672 }
1673
1674 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1675}
1676
1677/**
1678 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1679 * and initialize an array of glock holders for them
1680 * @rlist: the list of resource groups
1681 * @state: the lock state to acquire the RG lock in
1682 * @flags: the modifier flags for the holder structures
1683 *
1684 * FIXME: Don't use NOFAIL
1685 *
1686 */
1687
Bob Petersonfe6c9912008-01-28 11:13:02 -06001688void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001689{
1690 unsigned int x;
1691
1692 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001693 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001694 for (x = 0; x < rlist->rl_rgrps; x++)
1695 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
Bob Petersonfe6c9912008-01-28 11:13:02 -06001696 state, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001697 &rlist->rl_ghs[x]);
1698}
1699
1700/**
1701 * gfs2_rlist_free - free a resource group list
1702 * @list: the list of resource groups
1703 *
1704 */
1705
1706void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1707{
1708 unsigned int x;
1709
1710 kfree(rlist->rl_rgd);
1711
1712 if (rlist->rl_ghs) {
1713 for (x = 0; x < rlist->rl_rgrps; x++)
1714 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1715 kfree(rlist->rl_ghs);
1716 }
1717}
1718