David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Bob Peterson | fe6c991 | 2008-01-28 11:13:02 -0600 | [diff] [blame] | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 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 Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/completion.h> |
| 13 | #include <linux/buffer_head.h> |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 14 | #include <linux/fs.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 15 | #include <linux/gfs2_ondisk.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 16 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 17 | |
| 18 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 20 | #include "glock.h" |
| 21 | #include "glops.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #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 Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 28 | #include "util.h" |
Benjamin Marzinski | 172e045 | 2007-03-23 14:51:56 -0600 | [diff] [blame] | 29 | #include "log.h" |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 30 | #include "inode.h" |
Steven Whitehouse | 51ff87b | 2007-10-15 14:42:35 +0100 | [diff] [blame] | 31 | #include "ops_address.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | |
Steven Whitehouse | 2c1e52a | 2006-09-05 15:41:57 -0400 | [diff] [blame] | 33 | #define BFITNOENT ((u32)~0) |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 34 | #define NO_BLOCK ((u64)~0) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 35 | |
| 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 Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 39 | * 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 Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | static const char valid_change[16] = { |
| 48 | /* current */ |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 49 | /* n */ 0, 1, 1, 1, |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 50 | /* e */ 1, 0, 0, 0, |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 51 | /* w */ 0, 0, 0, 1, |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 52 | 1, 0, 0, 0 |
| 53 | }; |
| 54 | |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 55 | static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, |
| 56 | unsigned char old_state, unsigned char new_state); |
| 57 | |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 58 | /** |
| 59 | * gfs2_setbit - Set a bit in the bitmaps |
| 60 | * @buffer: the buffer that holds the bitmaps |
| 61 | * @buflen: the length (in bytes) of the buffer |
| 62 | * @block: the block to set |
| 63 | * @new_state: the new state of the block |
| 64 | * |
| 65 | */ |
| 66 | |
Steven Whitehouse | 3efd753 | 2006-05-18 14:02:52 -0400 | [diff] [blame] | 67 | static void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buffer, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 68 | unsigned int buflen, u32 block, |
Steven Whitehouse | 3efd753 | 2006-05-18 14:02:52 -0400 | [diff] [blame] | 69 | unsigned char new_state) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 70 | { |
| 71 | unsigned char *byte, *end, cur_state; |
| 72 | unsigned int bit; |
| 73 | |
| 74 | byte = buffer + (block / GFS2_NBBY); |
| 75 | bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; |
| 76 | end = buffer + buflen; |
| 77 | |
| 78 | gfs2_assert(rgd->rd_sbd, byte < end); |
| 79 | |
| 80 | cur_state = (*byte >> bit) & GFS2_BIT_MASK; |
| 81 | |
| 82 | if (valid_change[new_state * 4 + cur_state]) { |
| 83 | *byte ^= cur_state << bit; |
| 84 | *byte |= new_state << bit; |
| 85 | } else |
| 86 | gfs2_consist_rgrpd(rgd); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * gfs2_testbit - test a bit in the bitmaps |
| 91 | * @buffer: the buffer that holds the bitmaps |
| 92 | * @buflen: the length (in bytes) of the buffer |
| 93 | * @block: the block to read |
| 94 | * |
| 95 | */ |
| 96 | |
Steven Whitehouse | 3efd753 | 2006-05-18 14:02:52 -0400 | [diff] [blame] | 97 | static unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, unsigned char *buffer, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 98 | unsigned int buflen, u32 block) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 99 | { |
| 100 | unsigned char *byte, *end, cur_state; |
| 101 | unsigned int bit; |
| 102 | |
| 103 | byte = buffer + (block / GFS2_NBBY); |
| 104 | bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; |
| 105 | end = buffer + buflen; |
| 106 | |
| 107 | gfs2_assert(rgd->rd_sbd, byte < end); |
| 108 | |
| 109 | cur_state = (*byte >> bit) & GFS2_BIT_MASK; |
| 110 | |
| 111 | return cur_state; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing |
| 116 | * a block in a given allocation state. |
| 117 | * @buffer: the buffer that holds the bitmaps |
| 118 | * @buflen: the length (in bytes) of the buffer |
| 119 | * @goal: start search at this block's bit-pair (within @buffer) |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 120 | * @old_state: GFS2_BLKST_XXX the state of the block we're looking for. |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 121 | * |
| 122 | * Scope of @goal and returned block number is only within this bitmap buffer, |
| 123 | * not entire rgrp or filesystem. @buffer will be offset from the actual |
| 124 | * beginning of a bitmap block buffer, skipping any header structures. |
| 125 | * |
| 126 | * Return: the block number (bitmap buffer scope) that was found |
| 127 | */ |
| 128 | |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 129 | static u32 gfs2_bitfit(const u8 *buffer, unsigned int buflen, u32 goal, |
| 130 | u8 old_state) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 131 | { |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 132 | const u8 *byte; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 133 | u32 blk = goal; |
Bob Peterson | 5fdc2ee | 2007-12-11 19:00:16 -0600 | [diff] [blame] | 134 | unsigned int bit, bitlong; |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 135 | const unsigned long *plong; |
| 136 | #if BITS_PER_LONG == 32 |
| 137 | const unsigned long plong55 = 0x55555555; |
| 138 | #else |
| 139 | const unsigned long plong55 = 0x5555555555555555; |
| 140 | #endif |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 141 | |
| 142 | byte = buffer + (goal / GFS2_NBBY); |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 143 | plong = (const unsigned long *)(buffer + (goal / GFS2_NBBY)); |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 144 | bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE; |
Bob Peterson | 5fdc2ee | 2007-12-11 19:00:16 -0600 | [diff] [blame] | 145 | bitlong = bit; |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 146 | |
Bob Peterson | 5fdc2ee | 2007-12-11 19:00:16 -0600 | [diff] [blame] | 147 | while (byte < buffer + buflen) { |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 148 | |
Bob Peterson | 5fdc2ee | 2007-12-11 19:00:16 -0600 | [diff] [blame] | 149 | if (bitlong == 0 && old_state == 0 && *plong == plong55) { |
| 150 | plong++; |
| 151 | byte += sizeof(unsigned long); |
| 152 | blk += sizeof(unsigned long) * GFS2_NBBY; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 153 | continue; |
| 154 | } |
Bob Peterson | b3513fc | 2007-12-12 09:24:08 -0600 | [diff] [blame] | 155 | if (((*byte >> bit) & GFS2_BIT_MASK) == old_state) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 156 | return blk; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 157 | bit += GFS2_BIT_SIZE; |
| 158 | if (bit >= 8) { |
| 159 | bit = 0; |
| 160 | byte++; |
| 161 | } |
Bob Peterson | 5fdc2ee | 2007-12-11 19:00:16 -0600 | [diff] [blame] | 162 | bitlong += GFS2_BIT_SIZE; |
| 163 | if (bitlong >= sizeof(unsigned long) * 8) { |
| 164 | bitlong = 0; |
| 165 | plong++; |
| 166 | } |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 167 | |
| 168 | blk++; |
| 169 | } |
| 170 | |
| 171 | return BFITNOENT; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * gfs2_bitcount - count the number of bits in a certain state |
| 176 | * @buffer: the buffer that holds the bitmaps |
| 177 | * @buflen: the length (in bytes) of the buffer |
| 178 | * @state: the state of the block we're looking for |
| 179 | * |
| 180 | * Returns: The number of bits |
| 181 | */ |
| 182 | |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 183 | static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer, |
| 184 | unsigned int buflen, u8 state) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 185 | { |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 186 | const u8 *byte = buffer; |
| 187 | const u8 *end = buffer + buflen; |
| 188 | const u8 state1 = state << 2; |
| 189 | const u8 state2 = state << 4; |
| 190 | const u8 state3 = state << 6; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 191 | u32 count = 0; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 192 | |
| 193 | for (; byte < end; byte++) { |
| 194 | if (((*byte) & 0x03) == state) |
| 195 | count++; |
| 196 | if (((*byte) & 0x0C) == state1) |
| 197 | count++; |
| 198 | if (((*byte) & 0x30) == state2) |
| 199 | count++; |
| 200 | if (((*byte) & 0xC0) == state3) |
| 201 | count++; |
| 202 | } |
| 203 | |
| 204 | return count; |
| 205 | } |
| 206 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 207 | /** |
| 208 | * gfs2_rgrp_verify - Verify that a resource group is consistent |
| 209 | * @sdp: the filesystem |
| 210 | * @rgd: the rgrp |
| 211 | * |
| 212 | */ |
| 213 | |
| 214 | void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd) |
| 215 | { |
| 216 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 217 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 218 | u32 length = rgd->rd_length; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 219 | u32 count[4], tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 220 | int buf, x; |
| 221 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 222 | memset(count, 0, 4 * sizeof(u32)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 223 | |
| 224 | /* Count # blocks in each of 4 possible allocation states */ |
| 225 | for (buf = 0; buf < length; buf++) { |
| 226 | bi = rgd->rd_bits + buf; |
| 227 | for (x = 0; x < 4; x++) |
| 228 | count[x] += gfs2_bitcount(rgd, |
| 229 | bi->bi_bh->b_data + |
| 230 | bi->bi_offset, |
| 231 | bi->bi_len, x); |
| 232 | } |
| 233 | |
| 234 | if (count[0] != rgd->rd_rg.rg_free) { |
| 235 | if (gfs2_consist_rgrpd(rgd)) |
| 236 | fs_err(sdp, "free data mismatch: %u != %u\n", |
| 237 | count[0], rgd->rd_rg.rg_free); |
| 238 | return; |
| 239 | } |
| 240 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 241 | tmp = rgd->rd_data - |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 242 | rgd->rd_rg.rg_free - |
| 243 | rgd->rd_rg.rg_dinodes; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 244 | if (count[1] + count[2] != tmp) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 245 | if (gfs2_consist_rgrpd(rgd)) |
| 246 | fs_err(sdp, "used data mismatch: %u != %u\n", |
| 247 | count[1], tmp); |
| 248 | return; |
| 249 | } |
| 250 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 251 | if (count[3] != rgd->rd_rg.rg_dinodes) { |
| 252 | if (gfs2_consist_rgrpd(rgd)) |
| 253 | fs_err(sdp, "used metadata mismatch: %u != %u\n", |
| 254 | count[3], rgd->rd_rg.rg_dinodes); |
| 255 | return; |
| 256 | } |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 257 | |
| 258 | if (count[2] > count[3]) { |
| 259 | if (gfs2_consist_rgrpd(rgd)) |
| 260 | fs_err(sdp, "unlinked inodes > inodes: %u\n", |
| 261 | count[2]); |
| 262 | return; |
| 263 | } |
| 264 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 267 | static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 268 | { |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 269 | u64 first = rgd->rd_data0; |
| 270 | u64 last = first + rgd->rd_data; |
Steven Whitehouse | 1691042 | 2006-09-05 11:15:45 -0400 | [diff] [blame] | 271 | return first <= block && block < last; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * gfs2_blk2rgrpd - Find resource group for a given data/meta block number |
| 276 | * @sdp: The GFS2 superblock |
| 277 | * @n: The data block number |
| 278 | * |
| 279 | * Returns: The resource group, or NULL if not found |
| 280 | */ |
| 281 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 282 | struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 283 | { |
| 284 | struct gfs2_rgrpd *rgd; |
| 285 | |
| 286 | spin_lock(&sdp->sd_rindex_spin); |
| 287 | |
| 288 | list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) { |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 289 | if (rgrp_contains_block(rgd, blk)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 290 | list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list); |
| 291 | spin_unlock(&sdp->sd_rindex_spin); |
| 292 | return rgd; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | spin_unlock(&sdp->sd_rindex_spin); |
| 297 | |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem |
| 303 | * @sdp: The GFS2 superblock |
| 304 | * |
| 305 | * Returns: The first rgrp in the filesystem |
| 306 | */ |
| 307 | |
| 308 | struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp) |
| 309 | { |
| 310 | gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list)); |
| 311 | return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * gfs2_rgrpd_get_next - get the next RG |
| 316 | * @rgd: A RG |
| 317 | * |
| 318 | * Returns: The next rgrp |
| 319 | */ |
| 320 | |
| 321 | struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd) |
| 322 | { |
| 323 | if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list) |
| 324 | return NULL; |
| 325 | return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list); |
| 326 | } |
| 327 | |
| 328 | static void clear_rgrpdi(struct gfs2_sbd *sdp) |
| 329 | { |
| 330 | struct list_head *head; |
| 331 | struct gfs2_rgrpd *rgd; |
| 332 | struct gfs2_glock *gl; |
| 333 | |
| 334 | spin_lock(&sdp->sd_rindex_spin); |
| 335 | sdp->sd_rindex_forward = NULL; |
| 336 | head = &sdp->sd_rindex_recent_list; |
| 337 | while (!list_empty(head)) { |
| 338 | rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent); |
| 339 | list_del(&rgd->rd_recent); |
| 340 | } |
| 341 | spin_unlock(&sdp->sd_rindex_spin); |
| 342 | |
| 343 | head = &sdp->sd_rindex_list; |
| 344 | while (!list_empty(head)) { |
| 345 | rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list); |
| 346 | gl = rgd->rd_gl; |
| 347 | |
| 348 | list_del(&rgd->rd_list); |
| 349 | list_del(&rgd->rd_list_mru); |
| 350 | |
| 351 | if (gl) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 352 | gl->gl_object = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 353 | gfs2_glock_put(gl); |
| 354 | } |
| 355 | |
| 356 | kfree(rgd->rd_bits); |
Bob Peterson | 6bdd9be | 2008-01-28 17:20:26 -0600 | [diff] [blame] | 357 | kmem_cache_free(gfs2_rgrpd_cachep, rgd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | void gfs2_clear_rgrpd(struct gfs2_sbd *sdp) |
| 362 | { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 363 | mutex_lock(&sdp->sd_rindex_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 364 | clear_rgrpdi(sdp); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 365 | mutex_unlock(&sdp->sd_rindex_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 368 | static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd) |
| 369 | { |
| 370 | printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr); |
| 371 | printk(KERN_INFO " ri_length = %u\n", rgd->rd_length); |
| 372 | printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0); |
| 373 | printk(KERN_INFO " ri_data = %u\n", rgd->rd_data); |
| 374 | printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes); |
| 375 | } |
| 376 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 377 | /** |
| 378 | * gfs2_compute_bitstructs - Compute the bitmap sizes |
| 379 | * @rgd: The resource group descriptor |
| 380 | * |
| 381 | * Calculates bitmap descriptors, one for each block that contains bitmap data |
| 382 | * |
| 383 | * Returns: errno |
| 384 | */ |
| 385 | |
| 386 | static int compute_bitstructs(struct gfs2_rgrpd *rgd) |
| 387 | { |
| 388 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 389 | struct gfs2_bitmap *bi; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 390 | u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */ |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 391 | u32 bytes_left, bytes; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 392 | int x; |
| 393 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 394 | if (!length) |
| 395 | return -EINVAL; |
| 396 | |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 397 | rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 398 | if (!rgd->rd_bits) |
| 399 | return -ENOMEM; |
| 400 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 401 | bytes_left = rgd->rd_bitbytes; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 402 | |
| 403 | for (x = 0; x < length; x++) { |
| 404 | bi = rgd->rd_bits + x; |
| 405 | |
| 406 | /* small rgrp; bitmap stored completely in header block */ |
| 407 | if (length == 1) { |
| 408 | bytes = bytes_left; |
| 409 | bi->bi_offset = sizeof(struct gfs2_rgrp); |
| 410 | bi->bi_start = 0; |
| 411 | bi->bi_len = bytes; |
| 412 | /* header block */ |
| 413 | } else if (x == 0) { |
| 414 | bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp); |
| 415 | bi->bi_offset = sizeof(struct gfs2_rgrp); |
| 416 | bi->bi_start = 0; |
| 417 | bi->bi_len = bytes; |
| 418 | /* last block */ |
| 419 | } else if (x + 1 == length) { |
| 420 | bytes = bytes_left; |
| 421 | bi->bi_offset = sizeof(struct gfs2_meta_header); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 422 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | bi->bi_len = bytes; |
| 424 | /* other blocks */ |
| 425 | } else { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 426 | bytes = sdp->sd_sb.sb_bsize - |
| 427 | sizeof(struct gfs2_meta_header); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 428 | bi->bi_offset = sizeof(struct gfs2_meta_header); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 429 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | bi->bi_len = bytes; |
| 431 | } |
| 432 | |
| 433 | bytes_left -= bytes; |
| 434 | } |
| 435 | |
| 436 | if (bytes_left) { |
| 437 | gfs2_consist_rgrpd(rgd); |
| 438 | return -EIO; |
| 439 | } |
| 440 | bi = rgd->rd_bits + (length - 1); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 441 | if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 442 | if (gfs2_consist_rgrpd(rgd)) { |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 443 | gfs2_rindex_print(rgd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 444 | fs_err(sdp, "start=%u len=%u offset=%u\n", |
| 445 | bi->bi_start, bi->bi_len, bi->bi_offset); |
| 446 | } |
| 447 | return -EIO; |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | /** |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 454 | * gfs2_ri_total - Total up the file system space, according to the rindex. |
| 455 | * |
| 456 | */ |
| 457 | u64 gfs2_ri_total(struct gfs2_sbd *sdp) |
| 458 | { |
| 459 | u64 total_data = 0; |
| 460 | struct inode *inode = sdp->sd_rindex; |
| 461 | struct gfs2_inode *ip = GFS2_I(inode); |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 462 | char buf[sizeof(struct gfs2_rindex)]; |
| 463 | struct file_ra_state ra_state; |
| 464 | int error, rgrps; |
| 465 | |
| 466 | mutex_lock(&sdp->sd_rindex_mutex); |
| 467 | file_ra_state_init(&ra_state, inode->i_mapping); |
| 468 | for (rgrps = 0;; rgrps++) { |
| 469 | loff_t pos = rgrps * sizeof(struct gfs2_rindex); |
| 470 | |
| 471 | if (pos + sizeof(struct gfs2_rindex) >= ip->i_di.di_size) |
| 472 | break; |
| 473 | error = gfs2_internal_read(ip, &ra_state, buf, &pos, |
| 474 | sizeof(struct gfs2_rindex)); |
| 475 | if (error != sizeof(struct gfs2_rindex)) |
| 476 | break; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 477 | total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data); |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 478 | } |
| 479 | mutex_unlock(&sdp->sd_rindex_mutex); |
| 480 | return total_data; |
| 481 | } |
| 482 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 483 | static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf) |
| 484 | { |
| 485 | const struct gfs2_rindex *str = buf; |
| 486 | |
| 487 | rgd->rd_addr = be64_to_cpu(str->ri_addr); |
| 488 | rgd->rd_length = be32_to_cpu(str->ri_length); |
| 489 | rgd->rd_data0 = be64_to_cpu(str->ri_data0); |
| 490 | rgd->rd_data = be32_to_cpu(str->ri_data); |
| 491 | rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes); |
| 492 | } |
| 493 | |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 494 | /** |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 495 | * read_rindex_entry - Pull in a new resource index entry from the disk |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 496 | * @gl: The glock covering the rindex inode |
| 497 | * |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 498 | * Returns: 0 on success, error code otherwise |
| 499 | */ |
| 500 | |
| 501 | static int read_rindex_entry(struct gfs2_inode *ip, |
| 502 | struct file_ra_state *ra_state) |
| 503 | { |
| 504 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 505 | loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex); |
| 506 | char buf[sizeof(struct gfs2_rindex)]; |
| 507 | int error; |
| 508 | struct gfs2_rgrpd *rgd; |
| 509 | |
| 510 | error = gfs2_internal_read(ip, ra_state, buf, &pos, |
| 511 | sizeof(struct gfs2_rindex)); |
| 512 | if (!error) |
| 513 | return 0; |
| 514 | if (error != sizeof(struct gfs2_rindex)) { |
| 515 | if (error > 0) |
| 516 | error = -EIO; |
| 517 | return error; |
| 518 | } |
| 519 | |
Bob Peterson | 6bdd9be | 2008-01-28 17:20:26 -0600 | [diff] [blame] | 520 | rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS); |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 521 | error = -ENOMEM; |
| 522 | if (!rgd) |
| 523 | return error; |
| 524 | |
| 525 | mutex_init(&rgd->rd_mutex); |
| 526 | lops_init_le(&rgd->rd_le, &gfs2_rg_lops); |
| 527 | rgd->rd_sbd = sdp; |
| 528 | |
| 529 | list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list); |
| 530 | list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list); |
| 531 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 532 | gfs2_rindex_in(rgd, buf); |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 533 | error = compute_bitstructs(rgd); |
| 534 | if (error) |
| 535 | return error; |
| 536 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 537 | error = gfs2_glock_get(sdp, rgd->rd_addr, |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 538 | &gfs2_rgrp_glops, CREATE, &rgd->rd_gl); |
| 539 | if (error) |
| 540 | return error; |
| 541 | |
| 542 | rgd->rd_gl->gl_object = rgd; |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 543 | rgd->rd_flags &= ~GFS2_RDF_UPTODATE; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 544 | rgd->rd_flags |= GFS2_RDF_CHECK; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 545 | return error; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * gfs2_ri_update - Pull in a new resource index from the disk |
| 550 | * @ip: pointer to the rindex inode |
| 551 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 552 | * Returns: 0 on successful update, error code otherwise |
| 553 | */ |
| 554 | |
| 555 | static int gfs2_ri_update(struct gfs2_inode *ip) |
| 556 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 557 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 558 | struct inode *inode = &ip->i_inode; |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 559 | struct file_ra_state ra_state; |
Robert Peterson | cd81a4b | 2007-05-14 12:42:18 -0500 | [diff] [blame] | 560 | u64 rgrp_count = ip->i_di.di_size; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 561 | int error; |
| 562 | |
Robert Peterson | cd81a4b | 2007-05-14 12:42:18 -0500 | [diff] [blame] | 563 | if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 564 | gfs2_consist_inode(ip); |
| 565 | return -EIO; |
| 566 | } |
| 567 | |
| 568 | clear_rgrpdi(sdp); |
| 569 | |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 570 | file_ra_state_init(&ra_state, inode->i_mapping); |
Robert Peterson | cd81a4b | 2007-05-14 12:42:18 -0500 | [diff] [blame] | 571 | for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) { |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 572 | error = read_rindex_entry(ip, &ra_state); |
| 573 | if (error) { |
| 574 | clear_rgrpdi(sdp); |
| 575 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 576 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 579 | sdp->sd_rindex_uptodate = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 580 | return 0; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 581 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 582 | |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 583 | /** |
| 584 | * gfs2_ri_update_special - Pull in a new resource index from the disk |
| 585 | * |
| 586 | * This is a special version that's safe to call from gfs2_inplace_reserve_i. |
| 587 | * In this case we know that we don't have any resource groups in memory yet. |
| 588 | * |
| 589 | * @ip: pointer to the rindex inode |
| 590 | * |
| 591 | * Returns: 0 on successful update, error code otherwise |
| 592 | */ |
| 593 | static int gfs2_ri_update_special(struct gfs2_inode *ip) |
| 594 | { |
| 595 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 596 | struct inode *inode = &ip->i_inode; |
| 597 | struct file_ra_state ra_state; |
| 598 | int error; |
| 599 | |
| 600 | file_ra_state_init(&ra_state, inode->i_mapping); |
| 601 | for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) { |
| 602 | /* Ignore partials */ |
| 603 | if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) > |
| 604 | ip->i_di.di_size) |
| 605 | break; |
| 606 | error = read_rindex_entry(ip, &ra_state); |
| 607 | if (error) { |
| 608 | clear_rgrpdi(sdp); |
| 609 | return error; |
| 610 | } |
| 611 | } |
| 612 | |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 613 | sdp->sd_rindex_uptodate = 1; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 614 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /** |
| 618 | * gfs2_rindex_hold - Grab a lock on the rindex |
| 619 | * @sdp: The GFS2 superblock |
| 620 | * @ri_gh: the glock holder |
| 621 | * |
| 622 | * We grab a lock on the rindex inode to make sure that it doesn't |
| 623 | * change whilst we are performing an operation. We keep this lock |
| 624 | * for quite long periods of time compared to other locks. This |
| 625 | * doesn't matter, since it is shared and it is very, very rarely |
| 626 | * accessed in the exclusive mode (i.e. only when expanding the filesystem). |
| 627 | * |
| 628 | * This makes sure that we're using the latest copy of the resource index |
| 629 | * special file, which might have been updated if someone expanded the |
| 630 | * filesystem (via gfs2_grow utility), which adds new resource groups. |
| 631 | * |
| 632 | * Returns: 0 on success, error code otherwise |
| 633 | */ |
| 634 | |
| 635 | int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh) |
| 636 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 637 | struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 638 | struct gfs2_glock *gl = ip->i_gl; |
| 639 | int error; |
| 640 | |
| 641 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh); |
| 642 | if (error) |
| 643 | return error; |
| 644 | |
| 645 | /* Read new copy from disk if we don't have the latest */ |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 646 | if (!sdp->sd_rindex_uptodate) { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 647 | mutex_lock(&sdp->sd_rindex_mutex); |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 648 | if (!sdp->sd_rindex_uptodate) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 649 | error = gfs2_ri_update(ip); |
| 650 | if (error) |
| 651 | gfs2_glock_dq_uninit(ri_gh); |
| 652 | } |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 653 | mutex_unlock(&sdp->sd_rindex_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | return error; |
| 657 | } |
| 658 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 659 | static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf) |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 660 | { |
| 661 | const struct gfs2_rgrp *str = buf; |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 662 | struct gfs2_rgrp_host *rg = &rgd->rd_rg; |
| 663 | u32 rg_flags; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 664 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 665 | rg_flags = be32_to_cpu(str->rg_flags); |
| 666 | if (rg_flags & GFS2_RGF_NOALLOC) |
| 667 | rgd->rd_flags |= GFS2_RDF_NOALLOC; |
| 668 | else |
| 669 | rgd->rd_flags &= ~GFS2_RDF_NOALLOC; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 670 | rg->rg_free = be32_to_cpu(str->rg_free); |
| 671 | rg->rg_dinodes = be32_to_cpu(str->rg_dinodes); |
| 672 | rg->rg_igeneration = be64_to_cpu(str->rg_igeneration); |
| 673 | } |
| 674 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 675 | static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf) |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 676 | { |
| 677 | struct gfs2_rgrp *str = buf; |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 678 | struct gfs2_rgrp_host *rg = &rgd->rd_rg; |
| 679 | u32 rg_flags = 0; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 680 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 681 | if (rgd->rd_flags & GFS2_RDF_NOALLOC) |
| 682 | rg_flags |= GFS2_RGF_NOALLOC; |
| 683 | str->rg_flags = cpu_to_be32(rg_flags); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 684 | str->rg_free = cpu_to_be32(rg->rg_free); |
| 685 | str->rg_dinodes = cpu_to_be32(rg->rg_dinodes); |
| 686 | str->__pad = cpu_to_be32(0); |
| 687 | str->rg_igeneration = cpu_to_be64(rg->rg_igeneration); |
| 688 | memset(&str->rg_reserved, 0, sizeof(str->rg_reserved)); |
| 689 | } |
| 690 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 691 | /** |
| 692 | * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps |
| 693 | * @rgd: the struct gfs2_rgrpd describing the RG to read in |
| 694 | * |
| 695 | * Read in all of a Resource Group's header and bitmap blocks. |
| 696 | * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps. |
| 697 | * |
| 698 | * Returns: errno |
| 699 | */ |
| 700 | |
| 701 | int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd) |
| 702 | { |
| 703 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 704 | struct gfs2_glock *gl = rgd->rd_gl; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 705 | unsigned int length = rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 706 | struct gfs2_bitmap *bi; |
| 707 | unsigned int x, y; |
| 708 | int error; |
| 709 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 710 | mutex_lock(&rgd->rd_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 711 | |
| 712 | spin_lock(&sdp->sd_rindex_spin); |
| 713 | if (rgd->rd_bh_count) { |
| 714 | rgd->rd_bh_count++; |
| 715 | spin_unlock(&sdp->sd_rindex_spin); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 716 | mutex_unlock(&rgd->rd_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | spin_unlock(&sdp->sd_rindex_spin); |
| 720 | |
| 721 | for (x = 0; x < length; x++) { |
| 722 | bi = rgd->rd_bits + x; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 723 | error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 724 | if (error) |
| 725 | goto fail; |
| 726 | } |
| 727 | |
| 728 | for (y = length; y--;) { |
| 729 | bi = rgd->rd_bits + y; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 730 | error = gfs2_meta_wait(sdp, bi->bi_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 731 | if (error) |
| 732 | goto fail; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 733 | if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB : |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 734 | GFS2_METATYPE_RG)) { |
| 735 | error = -EIO; |
| 736 | goto fail; |
| 737 | } |
| 738 | } |
| 739 | |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 740 | if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) { |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 741 | gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data); |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 742 | rgd->rd_flags |= GFS2_RDF_UPTODATE; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | spin_lock(&sdp->sd_rindex_spin); |
| 746 | rgd->rd_free_clone = rgd->rd_rg.rg_free; |
| 747 | rgd->rd_bh_count++; |
| 748 | spin_unlock(&sdp->sd_rindex_spin); |
| 749 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 750 | mutex_unlock(&rgd->rd_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 751 | |
| 752 | return 0; |
| 753 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 754 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 755 | while (x--) { |
| 756 | bi = rgd->rd_bits + x; |
| 757 | brelse(bi->bi_bh); |
| 758 | bi->bi_bh = NULL; |
| 759 | gfs2_assert_warn(sdp, !bi->bi_clone); |
| 760 | } |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 761 | mutex_unlock(&rgd->rd_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 762 | |
| 763 | return error; |
| 764 | } |
| 765 | |
| 766 | void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd) |
| 767 | { |
| 768 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 769 | |
| 770 | spin_lock(&sdp->sd_rindex_spin); |
| 771 | gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count); |
| 772 | rgd->rd_bh_count++; |
| 773 | spin_unlock(&sdp->sd_rindex_spin); |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get() |
| 778 | * @rgd: the struct gfs2_rgrpd describing the RG to read in |
| 779 | * |
| 780 | */ |
| 781 | |
| 782 | void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd) |
| 783 | { |
| 784 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 785 | int x, length = rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 786 | |
| 787 | spin_lock(&sdp->sd_rindex_spin); |
| 788 | gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count); |
| 789 | if (--rgd->rd_bh_count) { |
| 790 | spin_unlock(&sdp->sd_rindex_spin); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | for (x = 0; x < length; x++) { |
| 795 | struct gfs2_bitmap *bi = rgd->rd_bits + x; |
| 796 | kfree(bi->bi_clone); |
| 797 | bi->bi_clone = NULL; |
| 798 | brelse(bi->bi_bh); |
| 799 | bi->bi_bh = NULL; |
| 800 | } |
| 801 | |
| 802 | spin_unlock(&sdp->sd_rindex_spin); |
| 803 | } |
| 804 | |
| 805 | void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd) |
| 806 | { |
| 807 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 808 | unsigned int length = rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 809 | unsigned int x; |
| 810 | |
| 811 | for (x = 0; x < length; x++) { |
| 812 | struct gfs2_bitmap *bi = rgd->rd_bits + x; |
| 813 | if (!bi->bi_clone) |
| 814 | continue; |
| 815 | memcpy(bi->bi_clone + bi->bi_offset, |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 816 | bi->bi_bh->b_data + bi->bi_offset, bi->bi_len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | spin_lock(&sdp->sd_rindex_spin); |
| 820 | rgd->rd_free_clone = rgd->rd_rg.rg_free; |
| 821 | spin_unlock(&sdp->sd_rindex_spin); |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode |
| 826 | * @ip: the incore GFS2 inode structure |
| 827 | * |
| 828 | * Returns: the struct gfs2_alloc |
| 829 | */ |
| 830 | |
| 831 | struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip) |
| 832 | { |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 833 | BUG_ON(ip->i_alloc != NULL); |
| 834 | ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL); |
| 835 | return ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 839 | * try_rgrp_fit - See if a given reservation will fit in a given RG |
| 840 | * @rgd: the RG data |
| 841 | * @al: the struct gfs2_alloc structure describing the reservation |
| 842 | * |
| 843 | * If there's room for the requested blocks to be allocated from the RG: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 844 | * Sets the $al_rgd field in @al. |
| 845 | * |
| 846 | * Returns: 1 on success (it fits), 0 on failure (it doesn't fit) |
| 847 | */ |
| 848 | |
| 849 | static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al) |
| 850 | { |
| 851 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 852 | int ret = 0; |
| 853 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 854 | if (rgd->rd_flags & GFS2_RDF_NOALLOC) |
Steven Whitehouse | a43a490 | 2007-04-02 10:48:17 +0100 | [diff] [blame] | 855 | return 0; |
| 856 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 857 | spin_lock(&sdp->sd_rindex_spin); |
| 858 | if (rgd->rd_free_clone >= al->al_requested) { |
| 859 | al->al_rgd = rgd; |
| 860 | ret = 1; |
| 861 | } |
| 862 | spin_unlock(&sdp->sd_rindex_spin); |
| 863 | |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | /** |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 868 | * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes |
| 869 | * @rgd: The rgrp |
| 870 | * |
| 871 | * Returns: The inode, if one has been found |
| 872 | */ |
| 873 | |
| 874 | static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked) |
| 875 | { |
| 876 | struct inode *inode; |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 877 | u32 goal = 0, block; |
Wendy Cheng | bb9bcf0 | 2007-06-27 17:07:08 -0400 | [diff] [blame] | 878 | u64 no_addr; |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 879 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 880 | |
| 881 | for(;;) { |
Bob Peterson | 24c7387 | 2007-07-12 16:58:50 -0500 | [diff] [blame] | 882 | if (goal >= rgd->rd_data) |
| 883 | break; |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 884 | down_write(&sdp->sd_log_flush_lock); |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 885 | block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, |
| 886 | GFS2_BLKST_UNLINKED); |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 887 | up_write(&sdp->sd_log_flush_lock); |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 888 | if (block == BFITNOENT) |
Bob Peterson | 24c7387 | 2007-07-12 16:58:50 -0500 | [diff] [blame] | 889 | break; |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 890 | /* rgblk_search can return a block < goal, so we need to |
| 891 | keep it marching forward. */ |
| 892 | no_addr = block + rgd->rd_data0; |
Bob Peterson | 24c7387 | 2007-07-12 16:58:50 -0500 | [diff] [blame] | 893 | goal++; |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 894 | if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked) |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 895 | continue; |
Wendy Cheng | bb9bcf0 | 2007-06-27 17:07:08 -0400 | [diff] [blame] | 896 | *last_unlinked = no_addr; |
| 897 | inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN, |
Benjamin Marzinski | 7a9f53b | 2007-09-18 13:33:18 -0500 | [diff] [blame] | 898 | no_addr, -1, 1); |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 899 | if (!IS_ERR(inode)) |
| 900 | return inode; |
| 901 | } |
| 902 | |
| 903 | rgd->rd_flags &= ~GFS2_RDF_CHECK; |
| 904 | return NULL; |
| 905 | } |
| 906 | |
| 907 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 908 | * recent_rgrp_first - get first RG from "recent" list |
| 909 | * @sdp: The GFS2 superblock |
| 910 | * @rglast: address of the rgrp used last |
| 911 | * |
| 912 | * Returns: The first rgrp in the recent list |
| 913 | */ |
| 914 | |
| 915 | static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 916 | u64 rglast) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 917 | { |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 918 | struct gfs2_rgrpd *rgd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 919 | |
| 920 | spin_lock(&sdp->sd_rindex_spin); |
| 921 | |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 922 | if (rglast) { |
| 923 | list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) { |
| 924 | if (rgrp_contains_block(rgd, rglast)) |
| 925 | goto out; |
| 926 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 927 | } |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 928 | rgd = NULL; |
| 929 | if (!list_empty(&sdp->sd_rindex_recent_list)) |
| 930 | rgd = list_entry(sdp->sd_rindex_recent_list.next, |
| 931 | struct gfs2_rgrpd, rd_recent); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 932 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 933 | spin_unlock(&sdp->sd_rindex_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 934 | return rgd; |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * recent_rgrp_next - get next RG from "recent" list |
| 939 | * @cur_rgd: current rgrp |
| 940 | * @remove: |
| 941 | * |
| 942 | * Returns: The next rgrp in the recent list |
| 943 | */ |
| 944 | |
| 945 | static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd, |
| 946 | int remove) |
| 947 | { |
| 948 | struct gfs2_sbd *sdp = cur_rgd->rd_sbd; |
| 949 | struct list_head *head; |
| 950 | struct gfs2_rgrpd *rgd; |
| 951 | |
| 952 | spin_lock(&sdp->sd_rindex_spin); |
| 953 | |
| 954 | head = &sdp->sd_rindex_recent_list; |
| 955 | |
| 956 | list_for_each_entry(rgd, head, rd_recent) { |
| 957 | if (rgd == cur_rgd) { |
| 958 | if (cur_rgd->rd_recent.next != head) |
| 959 | rgd = list_entry(cur_rgd->rd_recent.next, |
| 960 | struct gfs2_rgrpd, rd_recent); |
| 961 | else |
| 962 | rgd = NULL; |
| 963 | |
| 964 | if (remove) |
| 965 | list_del(&cur_rgd->rd_recent); |
| 966 | |
| 967 | goto out; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | rgd = NULL; |
| 972 | if (!list_empty(head)) |
| 973 | rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent); |
| 974 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 975 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 976 | spin_unlock(&sdp->sd_rindex_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 977 | return rgd; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * recent_rgrp_add - add an RG to tail of "recent" list |
| 982 | * @new_rgd: The rgrp to add |
| 983 | * |
| 984 | */ |
| 985 | |
| 986 | static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd) |
| 987 | { |
| 988 | struct gfs2_sbd *sdp = new_rgd->rd_sbd; |
| 989 | struct gfs2_rgrpd *rgd; |
| 990 | unsigned int count = 0; |
| 991 | unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp); |
| 992 | |
| 993 | spin_lock(&sdp->sd_rindex_spin); |
| 994 | |
| 995 | list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) { |
| 996 | if (rgd == new_rgd) |
| 997 | goto out; |
| 998 | |
| 999 | if (++count >= max) |
| 1000 | goto out; |
| 1001 | } |
| 1002 | list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list); |
| 1003 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1004 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1005 | spin_unlock(&sdp->sd_rindex_spin); |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * forward_rgrp_get - get an rgrp to try next from full list |
| 1010 | * @sdp: The GFS2 superblock |
| 1011 | * |
| 1012 | * Returns: The rgrp to try next |
| 1013 | */ |
| 1014 | |
| 1015 | static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp) |
| 1016 | { |
| 1017 | struct gfs2_rgrpd *rgd; |
| 1018 | unsigned int journals = gfs2_jindex_size(sdp); |
| 1019 | unsigned int rg = 0, x; |
| 1020 | |
| 1021 | spin_lock(&sdp->sd_rindex_spin); |
| 1022 | |
| 1023 | rgd = sdp->sd_rindex_forward; |
| 1024 | if (!rgd) { |
| 1025 | if (sdp->sd_rgrps >= journals) |
| 1026 | rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals; |
| 1027 | |
Steven Whitehouse | b8e1aab | 2006-08-22 16:25:50 -0400 | [diff] [blame] | 1028 | for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1029 | x++, rgd = gfs2_rgrpd_get_next(rgd)) |
| 1030 | /* Do Nothing */; |
| 1031 | |
| 1032 | sdp->sd_rindex_forward = rgd; |
| 1033 | } |
| 1034 | |
| 1035 | spin_unlock(&sdp->sd_rindex_spin); |
| 1036 | |
| 1037 | return rgd; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * forward_rgrp_set - set the forward rgrp pointer |
| 1042 | * @sdp: the filesystem |
| 1043 | * @rgd: The new forward rgrp |
| 1044 | * |
| 1045 | */ |
| 1046 | |
| 1047 | static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd) |
| 1048 | { |
| 1049 | spin_lock(&sdp->sd_rindex_spin); |
| 1050 | sdp->sd_rindex_forward = rgd; |
| 1051 | spin_unlock(&sdp->sd_rindex_spin); |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * get_local_rgrp - Choose and lock a rgrp for allocation |
| 1056 | * @ip: the inode to reserve space for |
| 1057 | * @rgp: the chosen and locked rgrp |
| 1058 | * |
| 1059 | * Try to acquire rgrp in way which avoids contending with others. |
| 1060 | * |
| 1061 | * Returns: errno |
| 1062 | */ |
| 1063 | |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1064 | static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1065 | { |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1066 | struct inode *inode = NULL; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1067 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1068 | struct gfs2_rgrpd *rgd, *begin = NULL; |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1069 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1070 | int flags = LM_FLAG_TRY; |
| 1071 | int skipped = 0; |
| 1072 | int loops = 0; |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1073 | int error, rg_locked; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1074 | |
| 1075 | /* Try recently successful rgrps */ |
| 1076 | |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 1077 | rgd = recent_rgrp_first(sdp, ip->i_goal); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1078 | |
| 1079 | while (rgd) { |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1080 | rg_locked = 0; |
| 1081 | |
| 1082 | if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) { |
| 1083 | rg_locked = 1; |
| 1084 | error = 0; |
| 1085 | } else { |
| 1086 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, |
| 1087 | LM_FLAG_TRY, &al->al_rgd_gh); |
| 1088 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1089 | switch (error) { |
| 1090 | case 0: |
| 1091 | if (try_rgrp_fit(rgd, al)) |
| 1092 | goto out; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1093 | if (rgd->rd_flags & GFS2_RDF_CHECK) |
| 1094 | inode = try_rgrp_unlink(rgd, last_unlinked); |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1095 | if (!rg_locked) |
| 1096 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1097 | if (inode) |
| 1098 | return inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1099 | rgd = recent_rgrp_next(rgd, 1); |
| 1100 | break; |
| 1101 | |
| 1102 | case GLR_TRYFAILED: |
| 1103 | rgd = recent_rgrp_next(rgd, 0); |
| 1104 | break; |
| 1105 | |
| 1106 | default: |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1107 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* Go through full list of rgrps */ |
| 1112 | |
| 1113 | begin = rgd = forward_rgrp_get(sdp); |
| 1114 | |
| 1115 | for (;;) { |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1116 | rg_locked = 0; |
| 1117 | |
| 1118 | if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) { |
| 1119 | rg_locked = 1; |
| 1120 | error = 0; |
| 1121 | } else { |
| 1122 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags, |
| 1123 | &al->al_rgd_gh); |
| 1124 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1125 | switch (error) { |
| 1126 | case 0: |
| 1127 | if (try_rgrp_fit(rgd, al)) |
| 1128 | goto out; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1129 | if (rgd->rd_flags & GFS2_RDF_CHECK) |
| 1130 | inode = try_rgrp_unlink(rgd, last_unlinked); |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1131 | if (!rg_locked) |
| 1132 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1133 | if (inode) |
| 1134 | return inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1135 | break; |
| 1136 | |
| 1137 | case GLR_TRYFAILED: |
| 1138 | skipped++; |
| 1139 | break; |
| 1140 | |
| 1141 | default: |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1142 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | rgd = gfs2_rgrpd_get_next(rgd); |
| 1146 | if (!rgd) |
| 1147 | rgd = gfs2_rgrpd_get_first(sdp); |
| 1148 | |
| 1149 | if (rgd == begin) { |
Benjamin Marzinski | 172e045 | 2007-03-23 14:51:56 -0600 | [diff] [blame] | 1150 | if (++loops >= 3) |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1151 | return ERR_PTR(-ENOSPC); |
Benjamin Marzinski | 172e045 | 2007-03-23 14:51:56 -0600 | [diff] [blame] | 1152 | if (!skipped) |
| 1153 | loops++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1154 | flags = 0; |
Benjamin Marzinski | 172e045 | 2007-03-23 14:51:56 -0600 | [diff] [blame] | 1155 | if (loops == 2) |
| 1156 | gfs2_log_flush(sdp, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1160 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1161 | if (begin) { |
| 1162 | recent_rgrp_add(rgd); |
| 1163 | rgd = gfs2_rgrpd_get_next(rgd); |
| 1164 | if (!rgd) |
| 1165 | rgd = gfs2_rgrpd_get_first(sdp); |
| 1166 | forward_rgrp_set(sdp, rgd); |
| 1167 | } |
| 1168 | |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1169 | return NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * gfs2_inplace_reserve_i - Reserve space in the filesystem |
| 1174 | * @ip: the inode to reserve space for |
| 1175 | * |
| 1176 | * Returns: errno |
| 1177 | */ |
| 1178 | |
| 1179 | int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line) |
| 1180 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1181 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1182 | struct gfs2_alloc *al = ip->i_alloc; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1183 | struct inode *inode; |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 1184 | int error = 0; |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 1185 | u64 last_unlinked = NO_BLOCK; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (gfs2_assert_warn(sdp, al->al_requested)) |
| 1188 | return -EINVAL; |
| 1189 | |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1190 | try_again: |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 1191 | /* We need to hold the rindex unless the inode we're using is |
| 1192 | the rindex itself, in which case it's already held. */ |
| 1193 | if (ip != GFS2_I(sdp->sd_rindex)) |
| 1194 | error = gfs2_rindex_hold(sdp, &al->al_ri_gh); |
| 1195 | else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */ |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 1196 | error = gfs2_ri_update_special(ip); |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 1197 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1198 | if (error) |
| 1199 | return error; |
| 1200 | |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1201 | inode = get_local_rgrp(ip, &last_unlinked); |
| 1202 | if (inode) { |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 1203 | if (ip != GFS2_I(sdp->sd_rindex)) |
| 1204 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1205 | if (IS_ERR(inode)) |
| 1206 | return PTR_ERR(inode); |
| 1207 | iput(inode); |
| 1208 | gfs2_log_flush(sdp, NULL); |
| 1209 | goto try_again; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | al->al_file = file; |
| 1213 | al->al_line = line; |
| 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * gfs2_inplace_release - release an inplace reservation |
| 1220 | * @ip: the inode the reservation was taken out on |
| 1221 | * |
| 1222 | * Release a reservation made by gfs2_inplace_reserve(). |
| 1223 | */ |
| 1224 | |
| 1225 | void gfs2_inplace_release(struct gfs2_inode *ip) |
| 1226 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1227 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1228 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1229 | |
| 1230 | if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1) |
| 1231 | fs_warn(sdp, "al_alloced = %u, al_requested = %u " |
| 1232 | "al_file = %s, al_line = %u\n", |
| 1233 | al->al_alloced, al->al_requested, al->al_file, |
| 1234 | al->al_line); |
| 1235 | |
| 1236 | al->al_rgd = NULL; |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1237 | if (al->al_rgd_gh.gh_gl) |
| 1238 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 1239 | if (ip != GFS2_I(sdp->sd_rindex)) |
| 1240 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * gfs2_get_block_type - Check a block in a RG is of given type |
| 1245 | * @rgd: the resource group holding the block |
| 1246 | * @block: the block number |
| 1247 | * |
| 1248 | * Returns: The block type (GFS2_BLKST_*) |
| 1249 | */ |
| 1250 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1251 | unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1252 | { |
| 1253 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1254 | u32 length, rgrp_block, buf_block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1255 | unsigned int buf; |
| 1256 | unsigned char type; |
| 1257 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1258 | length = rgd->rd_length; |
| 1259 | rgrp_block = block - rgd->rd_data0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1260 | |
| 1261 | for (buf = 0; buf < length; buf++) { |
| 1262 | bi = rgd->rd_bits + buf; |
| 1263 | if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY) |
| 1264 | break; |
| 1265 | } |
| 1266 | |
| 1267 | gfs2_assert(rgd->rd_sbd, buf < length); |
| 1268 | buf_block = rgrp_block - bi->bi_start * GFS2_NBBY; |
| 1269 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1270 | type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1271 | bi->bi_len, buf_block); |
| 1272 | |
| 1273 | return type; |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * rgblk_search - find a block in @old_state, change allocation |
| 1278 | * state to @new_state |
| 1279 | * @rgd: the resource group descriptor |
| 1280 | * @goal: the goal block within the RG (start here to search for avail block) |
| 1281 | * @old_state: GFS2_BLKST_XXX the before-allocation state to find |
| 1282 | * @new_state: GFS2_BLKST_XXX the after-allocation block state |
| 1283 | * |
| 1284 | * Walk rgrp's bitmap to find bits that represent a block in @old_state. |
| 1285 | * Add the found bitmap buffer to the transaction. |
| 1286 | * Set the found bits to @new_state to change block's allocation state. |
| 1287 | * |
| 1288 | * This function never fails, because we wouldn't call it unless we |
| 1289 | * know (from reservation results, etc.) that a block is available. |
| 1290 | * |
| 1291 | * Scope of @goal and returned block is just within rgrp, not the whole |
| 1292 | * filesystem. |
| 1293 | * |
| 1294 | * Returns: the block number allocated |
| 1295 | */ |
| 1296 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1297 | static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1298 | unsigned char old_state, unsigned char new_state) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1299 | { |
| 1300 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1301 | u32 length = rgd->rd_length; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1302 | u32 blk = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1303 | unsigned int buf, x; |
| 1304 | |
| 1305 | /* Find bitmap block that contains bits for goal block */ |
| 1306 | for (buf = 0; buf < length; buf++) { |
| 1307 | bi = rgd->rd_bits + buf; |
| 1308 | if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | gfs2_assert(rgd->rd_sbd, buf < length); |
| 1313 | |
| 1314 | /* Convert scope of "goal" from rgrp-wide to within found bit block */ |
| 1315 | goal -= bi->bi_start * GFS2_NBBY; |
| 1316 | |
| 1317 | /* Search (up to entire) bitmap in this rgrp for allocatable block. |
| 1318 | "x <= length", instead of "x < length", because we typically start |
| 1319 | the search in the middle of a bit block, but if we can't find an |
| 1320 | allocatable block anywhere else, we want to be able wrap around and |
| 1321 | search in the first part of our first-searched bit block. */ |
| 1322 | for (x = 0; x <= length; x++) { |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 1323 | /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone |
| 1324 | bitmaps, so we must search the originals for that. */ |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 1325 | const u8 *buffer = bi->bi_bh->b_data + bi->bi_offset; |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 1326 | if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone) |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 1327 | buffer = bi->bi_clone + bi->bi_offset; |
| 1328 | |
| 1329 | blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1330 | if (blk != BFITNOENT) |
| 1331 | break; |
| 1332 | |
| 1333 | /* Try next bitmap block (wrap back to rgrp header if at end) */ |
| 1334 | buf = (buf + 1) % length; |
| 1335 | bi = rgd->rd_bits + buf; |
| 1336 | goal = 0; |
| 1337 | } |
| 1338 | |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 1339 | if (blk != BFITNOENT && old_state != new_state) { |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1340 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
| 1341 | gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1342 | bi->bi_len, blk, new_state); |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1343 | if (bi->bi_clone) |
| 1344 | gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset, |
| 1345 | bi->bi_len, blk, new_state); |
| 1346 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1347 | |
Steven Whitehouse | 6eefaf6 | 2007-07-17 10:26:56 +0100 | [diff] [blame] | 1348 | return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * rgblk_free - Change alloc state of given block(s) |
| 1353 | * @sdp: the filesystem |
| 1354 | * @bstart: the start of a run of blocks to free |
| 1355 | * @blen: the length of the block run (all must lie within ONE RG!) |
| 1356 | * @new_state: GFS2_BLKST_XXX the after-allocation block state |
| 1357 | * |
| 1358 | * Returns: Resource group containing the block(s) |
| 1359 | */ |
| 1360 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1361 | static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart, |
| 1362 | u32 blen, unsigned char new_state) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1363 | { |
| 1364 | struct gfs2_rgrpd *rgd; |
| 1365 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1366 | u32 length, rgrp_blk, buf_blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1367 | unsigned int buf; |
| 1368 | |
| 1369 | rgd = gfs2_blk2rgrpd(sdp, bstart); |
| 1370 | if (!rgd) { |
| 1371 | if (gfs2_consist(sdp)) |
Steven Whitehouse | 382066d | 2006-05-24 10:22:09 -0400 | [diff] [blame] | 1372 | fs_err(sdp, "block = %llu\n", (unsigned long long)bstart); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1373 | return NULL; |
| 1374 | } |
| 1375 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1376 | length = rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1377 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1378 | rgrp_blk = bstart - rgd->rd_data0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1379 | |
| 1380 | while (blen--) { |
| 1381 | for (buf = 0; buf < length; buf++) { |
| 1382 | bi = rgd->rd_bits + buf; |
| 1383 | if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY) |
| 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | gfs2_assert(rgd->rd_sbd, buf < length); |
| 1388 | |
| 1389 | buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY; |
| 1390 | rgrp_blk++; |
| 1391 | |
| 1392 | if (!bi->bi_clone) { |
| 1393 | bi->bi_clone = kmalloc(bi->bi_bh->b_size, |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 1394 | GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1395 | memcpy(bi->bi_clone + bi->bi_offset, |
| 1396 | bi->bi_bh->b_data + bi->bi_offset, |
| 1397 | bi->bi_len); |
| 1398 | } |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1399 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 1400 | gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1401 | bi->bi_len, buf_blk, new_state); |
| 1402 | } |
| 1403 | |
| 1404 | return rgd; |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * gfs2_alloc_data - Allocate a data block |
| 1409 | * @ip: the inode to allocate the data block for |
| 1410 | * |
| 1411 | * Returns: the allocated block |
| 1412 | */ |
| 1413 | |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 1414 | u64 gfs2_alloc_data(struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1415 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1416 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1417 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1418 | struct gfs2_rgrpd *rgd = al->al_rgd; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1419 | u32 goal, blk; |
| 1420 | u64 block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1421 | |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 1422 | if (rgrp_contains_block(rgd, ip->i_goal)) |
| 1423 | goal = ip->i_goal - rgd->rd_data0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1424 | else |
Steven Whitehouse | ac576cc | 2008-02-01 10:34:15 +0000 | [diff] [blame] | 1425 | goal = rgd->rd_last_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1426 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 1427 | blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED); |
Steven Whitehouse | 6eefaf6 | 2007-07-17 10:26:56 +0100 | [diff] [blame] | 1428 | BUG_ON(blk == BFITNOENT); |
Steven Whitehouse | ac576cc | 2008-02-01 10:34:15 +0000 | [diff] [blame] | 1429 | rgd->rd_last_alloc = blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1430 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1431 | block = rgd->rd_data0 + blk; |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 1432 | ip->i_goal = block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1433 | |
| 1434 | gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free); |
| 1435 | rgd->rd_rg.rg_free--; |
| 1436 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1437 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1438 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1439 | |
| 1440 | al->al_alloced++; |
| 1441 | |
| 1442 | gfs2_statfs_change(sdp, 0, -1, 0); |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 1443 | gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1444 | |
| 1445 | spin_lock(&sdp->sd_rindex_spin); |
| 1446 | rgd->rd_free_clone--; |
| 1447 | spin_unlock(&sdp->sd_rindex_spin); |
| 1448 | |
| 1449 | return block; |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * gfs2_alloc_meta - Allocate a metadata block |
| 1454 | * @ip: the inode to allocate the metadata block for |
| 1455 | * |
| 1456 | * Returns: the allocated block |
| 1457 | */ |
| 1458 | |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 1459 | u64 gfs2_alloc_meta(struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1460 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1461 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1462 | struct gfs2_alloc *al = ip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1463 | struct gfs2_rgrpd *rgd = al->al_rgd; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1464 | u32 goal, blk; |
| 1465 | u64 block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1466 | |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 1467 | if (rgrp_contains_block(rgd, ip->i_goal)) |
| 1468 | goal = ip->i_goal - rgd->rd_data0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1469 | else |
Steven Whitehouse | ac576cc | 2008-02-01 10:34:15 +0000 | [diff] [blame] | 1470 | goal = rgd->rd_last_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1471 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 1472 | blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED); |
Steven Whitehouse | 6eefaf6 | 2007-07-17 10:26:56 +0100 | [diff] [blame] | 1473 | BUG_ON(blk == BFITNOENT); |
Steven Whitehouse | ac576cc | 2008-02-01 10:34:15 +0000 | [diff] [blame] | 1474 | rgd->rd_last_alloc = blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1475 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1476 | block = rgd->rd_data0 + blk; |
Steven Whitehouse | ce276b0 | 2008-02-06 09:25:45 +0000 | [diff] [blame] | 1477 | ip->i_goal = block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1478 | |
| 1479 | gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free); |
| 1480 | rgd->rd_rg.rg_free--; |
| 1481 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1482 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1483 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1484 | |
| 1485 | al->al_alloced++; |
| 1486 | |
| 1487 | gfs2_statfs_change(sdp, 0, -1, 0); |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 1488 | gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1489 | |
| 1490 | spin_lock(&sdp->sd_rindex_spin); |
| 1491 | rgd->rd_free_clone--; |
| 1492 | spin_unlock(&sdp->sd_rindex_spin); |
| 1493 | |
| 1494 | return block; |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * gfs2_alloc_di - Allocate a dinode |
| 1499 | * @dip: the directory that the inode is going in |
| 1500 | * |
| 1501 | * Returns: the block allocated |
| 1502 | */ |
| 1503 | |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 1504 | u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1505 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1506 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1507 | struct gfs2_alloc *al = dip->i_alloc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1508 | struct gfs2_rgrpd *rgd = al->al_rgd; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 1509 | u32 blk; |
| 1510 | u64 block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1511 | |
Steven Whitehouse | ac576cc | 2008-02-01 10:34:15 +0000 | [diff] [blame] | 1512 | blk = rgblk_search(rgd, rgd->rd_last_alloc, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1513 | GFS2_BLKST_FREE, GFS2_BLKST_DINODE); |
Steven Whitehouse | 6eefaf6 | 2007-07-17 10:26:56 +0100 | [diff] [blame] | 1514 | BUG_ON(blk == BFITNOENT); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1515 | |
Steven Whitehouse | ac576cc | 2008-02-01 10:34:15 +0000 | [diff] [blame] | 1516 | rgd->rd_last_alloc = blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1517 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1518 | block = rgd->rd_data0 + blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1519 | |
| 1520 | gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free); |
| 1521 | rgd->rd_rg.rg_free--; |
| 1522 | rgd->rd_rg.rg_dinodes++; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 1523 | *generation = rgd->rd_rg.rg_igeneration++; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1524 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1525 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1526 | |
| 1527 | al->al_alloced++; |
| 1528 | |
| 1529 | gfs2_statfs_change(sdp, 0, -1, +1); |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame^] | 1530 | gfs2_trans_add_unrevoke(sdp, block, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1531 | |
| 1532 | spin_lock(&sdp->sd_rindex_spin); |
| 1533 | rgd->rd_free_clone--; |
| 1534 | spin_unlock(&sdp->sd_rindex_spin); |
| 1535 | |
| 1536 | return block; |
| 1537 | } |
| 1538 | |
| 1539 | /** |
| 1540 | * gfs2_free_data - free a contiguous run of data block(s) |
| 1541 | * @ip: the inode these blocks are being freed from |
| 1542 | * @bstart: first block of a run of contiguous blocks |
| 1543 | * @blen: the length of the block run |
| 1544 | * |
| 1545 | */ |
| 1546 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1547 | void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1548 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1549 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1550 | struct gfs2_rgrpd *rgd; |
| 1551 | |
| 1552 | rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); |
| 1553 | if (!rgd) |
| 1554 | return; |
| 1555 | |
| 1556 | rgd->rd_rg.rg_free += blen; |
| 1557 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1558 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1559 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1560 | |
| 1561 | gfs2_trans_add_rg(rgd); |
| 1562 | |
| 1563 | gfs2_statfs_change(sdp, 0, +blen, 0); |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 1564 | gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * gfs2_free_meta - free a contiguous run of data block(s) |
| 1569 | * @ip: the inode these blocks are being freed from |
| 1570 | * @bstart: first block of a run of contiguous blocks |
| 1571 | * @blen: the length of the block run |
| 1572 | * |
| 1573 | */ |
| 1574 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1575 | void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1576 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1577 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1578 | struct gfs2_rgrpd *rgd; |
| 1579 | |
| 1580 | rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); |
| 1581 | if (!rgd) |
| 1582 | return; |
| 1583 | |
| 1584 | rgd->rd_rg.rg_free += blen; |
| 1585 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1586 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1587 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1588 | |
| 1589 | gfs2_trans_add_rg(rgd); |
| 1590 | |
| 1591 | gfs2_statfs_change(sdp, 0, +blen, 0); |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 1592 | gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1593 | gfs2_meta_wipe(ip, bstart, blen); |
| 1594 | } |
| 1595 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1596 | void gfs2_unlink_di(struct inode *inode) |
| 1597 | { |
| 1598 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1599 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 1600 | struct gfs2_rgrpd *rgd; |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1601 | u64 blkno = ip->i_no_addr; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1602 | |
| 1603 | rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED); |
| 1604 | if (!rgd) |
| 1605 | return; |
| 1606 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1607 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1608 | gfs2_trans_add_rg(rgd); |
| 1609 | } |
| 1610 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1611 | static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1612 | { |
| 1613 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 1614 | struct gfs2_rgrpd *tmp_rgd; |
| 1615 | |
| 1616 | tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE); |
| 1617 | if (!tmp_rgd) |
| 1618 | return; |
| 1619 | gfs2_assert_withdraw(sdp, rgd == tmp_rgd); |
| 1620 | |
| 1621 | if (!rgd->rd_rg.rg_dinodes) |
| 1622 | gfs2_consist_rgrpd(rgd); |
| 1623 | rgd->rd_rg.rg_dinodes--; |
| 1624 | rgd->rd_rg.rg_free++; |
| 1625 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1626 | gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1627 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1628 | |
| 1629 | gfs2_statfs_change(sdp, 0, +1, -1); |
| 1630 | gfs2_trans_add_rg(rgd); |
| 1631 | } |
| 1632 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1633 | |
| 1634 | void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) |
| 1635 | { |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1636 | gfs2_free_uninit_di(rgd, ip->i_no_addr); |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 1637 | gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid); |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1638 | gfs2_meta_wipe(ip, ip->i_no_addr, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | /** |
| 1642 | * gfs2_rlist_add - add a RG to a list of RGs |
| 1643 | * @sdp: the filesystem |
| 1644 | * @rlist: the list of resource groups |
| 1645 | * @block: the block |
| 1646 | * |
| 1647 | * Figure out what RG a block belongs to and add that RG to the list |
| 1648 | * |
| 1649 | * FIXME: Don't use NOFAIL |
| 1650 | * |
| 1651 | */ |
| 1652 | |
| 1653 | void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1654 | u64 block) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1655 | { |
| 1656 | struct gfs2_rgrpd *rgd; |
| 1657 | struct gfs2_rgrpd **tmp; |
| 1658 | unsigned int new_space; |
| 1659 | unsigned int x; |
| 1660 | |
| 1661 | if (gfs2_assert_warn(sdp, !rlist->rl_ghs)) |
| 1662 | return; |
| 1663 | |
| 1664 | rgd = gfs2_blk2rgrpd(sdp, block); |
| 1665 | if (!rgd) { |
| 1666 | if (gfs2_consist(sdp)) |
Steven Whitehouse | 382066d | 2006-05-24 10:22:09 -0400 | [diff] [blame] | 1667 | fs_err(sdp, "block = %llu\n", (unsigned long long)block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1668 | return; |
| 1669 | } |
| 1670 | |
| 1671 | for (x = 0; x < rlist->rl_rgrps; x++) |
| 1672 | if (rlist->rl_rgd[x] == rgd) |
| 1673 | return; |
| 1674 | |
| 1675 | if (rlist->rl_rgrps == rlist->rl_space) { |
| 1676 | new_space = rlist->rl_space + 10; |
| 1677 | |
| 1678 | tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *), |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 1679 | GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1680 | |
| 1681 | if (rlist->rl_rgd) { |
| 1682 | memcpy(tmp, rlist->rl_rgd, |
| 1683 | rlist->rl_space * sizeof(struct gfs2_rgrpd *)); |
| 1684 | kfree(rlist->rl_rgd); |
| 1685 | } |
| 1686 | |
| 1687 | rlist->rl_space = new_space; |
| 1688 | rlist->rl_rgd = tmp; |
| 1689 | } |
| 1690 | |
| 1691 | rlist->rl_rgd[rlist->rl_rgrps++] = rgd; |
| 1692 | } |
| 1693 | |
| 1694 | /** |
| 1695 | * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate |
| 1696 | * and initialize an array of glock holders for them |
| 1697 | * @rlist: the list of resource groups |
| 1698 | * @state: the lock state to acquire the RG lock in |
| 1699 | * @flags: the modifier flags for the holder structures |
| 1700 | * |
| 1701 | * FIXME: Don't use NOFAIL |
| 1702 | * |
| 1703 | */ |
| 1704 | |
Bob Peterson | fe6c991 | 2008-01-28 11:13:02 -0600 | [diff] [blame] | 1705 | void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1706 | { |
| 1707 | unsigned int x; |
| 1708 | |
| 1709 | rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder), |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 1710 | GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1711 | for (x = 0; x < rlist->rl_rgrps; x++) |
| 1712 | gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, |
Bob Peterson | fe6c991 | 2008-01-28 11:13:02 -0600 | [diff] [blame] | 1713 | state, 0, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1714 | &rlist->rl_ghs[x]); |
| 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * gfs2_rlist_free - free a resource group list |
| 1719 | * @list: the list of resource groups |
| 1720 | * |
| 1721 | */ |
| 1722 | |
| 1723 | void gfs2_rlist_free(struct gfs2_rgrp_list *rlist) |
| 1724 | { |
| 1725 | unsigned int x; |
| 1726 | |
| 1727 | kfree(rlist->rl_rgd); |
| 1728 | |
| 1729 | if (rlist->rl_ghs) { |
| 1730 | for (x = 0; x < rlist->rl_rgrps; x++) |
| 1731 | gfs2_holder_uninit(&rlist->rl_ghs[x]); |
| 1732 | kfree(rlist->rl_ghs); |
| 1733 | } |
| 1734 | } |
| 1735 | |