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