blob: 77eba0a38040a14877fcf0d4277cacde24b7130b [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 Whitehouseb45e41d2008-02-06 10:11:15 +00001360 while(*n < elen) {
1361 goal++;
1362 if (goal >= (bi->bi_len / GFS2_NBBY))
1363 break;
1364 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1365 GFS2_BLKST_FREE)
1366 break;
1367 (*n)++;
1368 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
1369 bi->bi_offset, bi->bi_len, blk, new_state);
1370 }
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001371 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001372
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001373 return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001374}
1375
1376/**
1377 * rgblk_free - Change alloc state of given block(s)
1378 * @sdp: the filesystem
1379 * @bstart: the start of a run of blocks to free
1380 * @blen: the length of the block run (all must lie within ONE RG!)
1381 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1382 *
1383 * Returns: Resource group containing the block(s)
1384 */
1385
Steven Whitehousecd915492006-09-04 12:49:07 -04001386static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1387 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388{
1389 struct gfs2_rgrpd *rgd;
1390 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001391 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392 unsigned int buf;
1393
1394 rgd = gfs2_blk2rgrpd(sdp, bstart);
1395 if (!rgd) {
1396 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001397 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 return NULL;
1399 }
1400
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001401 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001403 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404
1405 while (blen--) {
1406 for (buf = 0; buf < length; buf++) {
1407 bi = rgd->rd_bits + buf;
1408 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1409 break;
1410 }
1411
1412 gfs2_assert(rgd->rd_sbd, buf < length);
1413
1414 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1415 rgrp_blk++;
1416
1417 if (!bi->bi_clone) {
1418 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001419 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001420 memcpy(bi->bi_clone + bi->bi_offset,
1421 bi->bi_bh->b_data + bi->bi_offset,
1422 bi->bi_len);
1423 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001424 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001425 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001426 bi->bi_len, buf_blk, new_state);
1427 }
1428
1429 return rgd;
1430}
1431
1432/**
Steven Whitehouse16394312008-02-01 14:52:30 +00001433 * gfs2_alloc_block - Allocate a block
1434 * @ip: the inode to allocate the block for
David Teiglandb3b94fa2006-01-16 16:50:04 +00001435 *
1436 * Returns: the allocated block
1437 */
1438
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001439u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001440{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001442 struct gfs2_alloc *al = ip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001443 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehousecd915492006-09-04 12:49:07 -04001444 u32 goal, blk;
1445 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001446
Steven Whitehousece276b02008-02-06 09:25:45 +00001447 if (rgrp_contains_block(rgd, ip->i_goal))
1448 goal = ip->i_goal - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001449 else
Steven Whitehouseac576cc2008-02-01 10:34:15 +00001450 goal = rgd->rd_last_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001451
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001452 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001453 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001454
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001455 rgd->rd_last_alloc = blk;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001456 block = rgd->rd_data0 + blk;
Steven Whitehousece276b02008-02-06 09:25:45 +00001457 ip->i_goal = block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001458
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001459 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free >= *n);
1460 rgd->rd_rg.rg_free -= *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001462 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001463 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001464
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001465 al->al_alloced += *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001467 gfs2_statfs_change(sdp, 0, -*n, 0);
1468 gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001469
1470 spin_lock(&sdp->sd_rindex_spin);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001471 rgd->rd_free_clone -= *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001472 spin_unlock(&sdp->sd_rindex_spin);
1473
1474 return block;
1475}
1476
1477/**
1478 * gfs2_alloc_di - Allocate a dinode
1479 * @dip: the directory that the inode is going in
1480 *
1481 * Returns: the block allocated
1482 */
1483
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001484u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001486 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001487 struct gfs2_alloc *al = dip->i_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001488 struct gfs2_rgrpd *rgd = al->al_rgd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001489 u32 blk;
1490 u64 block;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001491 unsigned int n = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492
Steven Whitehouseac576cc2008-02-01 10:34:15 +00001493 blk = rgblk_search(rgd, rgd->rd_last_alloc,
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001494 GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
Steven Whitehouse6eefaf62007-07-17 10:26:56 +01001495 BUG_ON(blk == BFITNOENT);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001496
Steven Whitehouseac576cc2008-02-01 10:34:15 +00001497 rgd->rd_last_alloc = blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001499 block = rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500
1501 gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
1502 rgd->rd_rg.rg_free--;
1503 rgd->rd_rg.rg_dinodes++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001504 *generation = rgd->rd_rg.rg_igeneration++;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001505 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001506 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001507
1508 al->al_alloced++;
1509
1510 gfs2_statfs_change(sdp, 0, -1, +1);
Steven Whitehouse5731be52008-02-01 13:16:55 +00001511 gfs2_trans_add_unrevoke(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001512
1513 spin_lock(&sdp->sd_rindex_spin);
1514 rgd->rd_free_clone--;
1515 spin_unlock(&sdp->sd_rindex_spin);
1516
1517 return block;
1518}
1519
1520/**
1521 * gfs2_free_data - free a contiguous run of data block(s)
1522 * @ip: the inode these blocks are being freed from
1523 * @bstart: first block of a run of contiguous blocks
1524 * @blen: the length of the block run
1525 *
1526 */
1527
Steven Whitehousecd915492006-09-04 12:49:07 -04001528void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001529{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001530 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531 struct gfs2_rgrpd *rgd;
1532
1533 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1534 if (!rgd)
1535 return;
1536
1537 rgd->rd_rg.rg_free += blen;
1538
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001539 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001540 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541
1542 gfs2_trans_add_rg(rgd);
1543
1544 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001545 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546}
1547
1548/**
1549 * gfs2_free_meta - free a contiguous run of data block(s)
1550 * @ip: the inode these blocks are being freed from
1551 * @bstart: first block of a run of contiguous blocks
1552 * @blen: the length of the block run
1553 *
1554 */
1555
Steven Whitehousecd915492006-09-04 12:49:07 -04001556void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001557{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001558 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001559 struct gfs2_rgrpd *rgd;
1560
1561 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1562 if (!rgd)
1563 return;
1564
1565 rgd->rd_rg.rg_free += blen;
1566
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001567 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001568 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001569
1570 gfs2_trans_add_rg(rgd);
1571
1572 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001573 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001574 gfs2_meta_wipe(ip, bstart, blen);
1575}
1576
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001577void gfs2_unlink_di(struct inode *inode)
1578{
1579 struct gfs2_inode *ip = GFS2_I(inode);
1580 struct gfs2_sbd *sdp = GFS2_SB(inode);
1581 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001582 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001583
1584 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1585 if (!rgd)
1586 return;
1587 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001588 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001589 gfs2_trans_add_rg(rgd);
1590}
1591
Steven Whitehousecd915492006-09-04 12:49:07 -04001592static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001593{
1594 struct gfs2_sbd *sdp = rgd->rd_sbd;
1595 struct gfs2_rgrpd *tmp_rgd;
1596
1597 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1598 if (!tmp_rgd)
1599 return;
1600 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1601
1602 if (!rgd->rd_rg.rg_dinodes)
1603 gfs2_consist_rgrpd(rgd);
1604 rgd->rd_rg.rg_dinodes--;
1605 rgd->rd_rg.rg_free++;
1606
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001607 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001608 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001609
1610 gfs2_statfs_change(sdp, 0, +1, -1);
1611 gfs2_trans_add_rg(rgd);
1612}
1613
David Teiglandb3b94fa2006-01-16 16:50:04 +00001614
1615void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1616{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001617 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001618 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001619 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001620}
1621
1622/**
1623 * gfs2_rlist_add - add a RG to a list of RGs
1624 * @sdp: the filesystem
1625 * @rlist: the list of resource groups
1626 * @block: the block
1627 *
1628 * Figure out what RG a block belongs to and add that RG to the list
1629 *
1630 * FIXME: Don't use NOFAIL
1631 *
1632 */
1633
1634void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001635 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001636{
1637 struct gfs2_rgrpd *rgd;
1638 struct gfs2_rgrpd **tmp;
1639 unsigned int new_space;
1640 unsigned int x;
1641
1642 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1643 return;
1644
1645 rgd = gfs2_blk2rgrpd(sdp, block);
1646 if (!rgd) {
1647 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001648 fs_err(sdp, "block = %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001649 return;
1650 }
1651
1652 for (x = 0; x < rlist->rl_rgrps; x++)
1653 if (rlist->rl_rgd[x] == rgd)
1654 return;
1655
1656 if (rlist->rl_rgrps == rlist->rl_space) {
1657 new_space = rlist->rl_space + 10;
1658
1659 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001660 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001661
1662 if (rlist->rl_rgd) {
1663 memcpy(tmp, rlist->rl_rgd,
1664 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1665 kfree(rlist->rl_rgd);
1666 }
1667
1668 rlist->rl_space = new_space;
1669 rlist->rl_rgd = tmp;
1670 }
1671
1672 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1673}
1674
1675/**
1676 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1677 * and initialize an array of glock holders for them
1678 * @rlist: the list of resource groups
1679 * @state: the lock state to acquire the RG lock in
1680 * @flags: the modifier flags for the holder structures
1681 *
1682 * FIXME: Don't use NOFAIL
1683 *
1684 */
1685
Bob Petersonfe6c9912008-01-28 11:13:02 -06001686void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001687{
1688 unsigned int x;
1689
1690 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001691 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001692 for (x = 0; x < rlist->rl_rgrps; x++)
1693 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
Bob Petersonfe6c9912008-01-28 11:13:02 -06001694 state, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001695 &rlist->rl_ghs[x]);
1696}
1697
1698/**
1699 * gfs2_rlist_free - free a resource group list
1700 * @list: the list of resource groups
1701 *
1702 */
1703
1704void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1705{
1706 unsigned int x;
1707
1708 kfree(rlist->rl_rgd);
1709
1710 if (rlist->rl_ghs) {
1711 for (x = 0; x < rlist->rl_rgrps; x++)
1712 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1713 kfree(rlist->rl_ghs);
1714 }
1715}
1716