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