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