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