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> |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 18 | #include <linux/rbtree.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 19 | |
| 20 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 21 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include "glock.h" |
| 23 | #include "glops.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "lops.h" |
| 25 | #include "meta_io.h" |
| 26 | #include "quota.h" |
| 27 | #include "rgrp.h" |
| 28 | #include "super.h" |
| 29 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 30 | #include "util.h" |
Benjamin Marzinski | 172e045 | 2007-03-23 14:51:56 -0600 | [diff] [blame] | 31 | #include "log.h" |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 32 | #include "inode.h" |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 33 | #include "trace_gfs2.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 34 | |
Steven Whitehouse | 2c1e52a | 2006-09-05 15:41:57 -0400 | [diff] [blame] | 35 | #define BFITNOENT ((u32)~0) |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 36 | #define NO_BLOCK ((u64)~0) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 37 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 38 | #define RSRV_CONTENTION_FACTOR 4 |
| 39 | #define RGRP_RSRV_MAX_CONTENDERS 2 |
| 40 | |
Bob Peterson | 1f466a4 | 2008-03-10 18:17:47 -0500 | [diff] [blame] | 41 | #if BITS_PER_LONG == 32 |
| 42 | #define LBITMASK (0x55555555UL) |
| 43 | #define LBITSKIP55 (0x55555555UL) |
| 44 | #define LBITSKIP00 (0x00000000UL) |
| 45 | #else |
| 46 | #define LBITMASK (0x5555555555555555UL) |
| 47 | #define LBITSKIP55 (0x5555555555555555UL) |
| 48 | #define LBITSKIP00 (0x0000000000000000UL) |
| 49 | #endif |
| 50 | |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 51 | /* |
| 52 | * These routines are used by the resource group routines (rgrp.c) |
| 53 | * to keep track of block allocation. Each block is represented by two |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 54 | * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks. |
| 55 | * |
| 56 | * 0 = Free |
| 57 | * 1 = Used (not metadata) |
| 58 | * 2 = Unlinked (still in use) inode |
| 59 | * 3 = Used (metadata) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | static const char valid_change[16] = { |
| 63 | /* current */ |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 64 | /* n */ 0, 1, 1, 1, |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 65 | /* e */ 1, 0, 0, 0, |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 66 | /* w */ 0, 0, 0, 1, |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 67 | 1, 0, 0, 0 |
| 68 | }; |
| 69 | |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 70 | static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 71 | unsigned char old_state, |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 72 | struct gfs2_bitmap **rbi); |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 73 | |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 74 | /** |
| 75 | * gfs2_setbit - Set a bit in the bitmaps |
Bob Peterson | 29c578f | 2012-04-11 13:01:07 -0400 | [diff] [blame] | 76 | * @rgd: the resource group descriptor |
Bob Peterson | 29c578f | 2012-04-11 13:01:07 -0400 | [diff] [blame] | 77 | * @buf2: the clone buffer that holds the bitmaps |
| 78 | * @bi: the bitmap structure |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 79 | * @block: the block to set |
| 80 | * @new_state: the new state of the block |
| 81 | * |
| 82 | */ |
| 83 | |
Bob Peterson | 06344b9 | 2012-04-26 12:44:35 -0400 | [diff] [blame] | 84 | static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf2, |
| 85 | struct gfs2_bitmap *bi, u32 block, |
| 86 | unsigned char new_state) |
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 | unsigned char *byte1, *byte2, *end, cur_state; |
Bob Peterson | 95c8e17 | 2011-03-22 10:49:12 -0400 | [diff] [blame] | 89 | unsigned int buflen = bi->bi_len; |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 90 | const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 91 | |
Bob Peterson | 06344b9 | 2012-04-26 12:44:35 -0400 | [diff] [blame] | 92 | byte1 = bi->bi_bh->b_data + bi->bi_offset + (block / GFS2_NBBY); |
| 93 | end = bi->bi_bh->b_data + bi->bi_offset + buflen; |
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 | BUG_ON(byte1 >= end); |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 96 | |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 97 | cur_state = (*byte1 >> bit) & GFS2_BIT_MASK; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 98 | |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 99 | if (unlikely(!valid_change[new_state * 4 + cur_state])) { |
Bob Peterson | 95c8e17 | 2011-03-22 10:49:12 -0400 | [diff] [blame] | 100 | printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, " |
| 101 | "new_state=%d\n", |
| 102 | (unsigned long long)block, cur_state, new_state); |
| 103 | printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n", |
| 104 | (unsigned long long)rgd->rd_addr, |
| 105 | (unsigned long)bi->bi_start); |
| 106 | printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n", |
| 107 | (unsigned long)bi->bi_offset, |
| 108 | (unsigned long)bi->bi_len); |
| 109 | dump_stack(); |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 110 | gfs2_consist_rgrpd(rgd); |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | *byte1 ^= (cur_state ^ new_state) << bit; |
| 114 | |
| 115 | if (buf2) { |
Bob Peterson | 29c578f | 2012-04-11 13:01:07 -0400 | [diff] [blame] | 116 | byte2 = buf2 + bi->bi_offset + (block / GFS2_NBBY); |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 117 | cur_state = (*byte2 >> bit) & GFS2_BIT_MASK; |
| 118 | *byte2 ^= (cur_state ^ new_state) << bit; |
| 119 | } |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * gfs2_testbit - test a bit in the bitmaps |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 124 | * @rgd: the resource group descriptor |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 125 | * @buffer: the buffer that holds the bitmaps |
| 126 | * @buflen: the length (in bytes) of the buffer |
| 127 | * @block: the block to read |
| 128 | * |
| 129 | */ |
| 130 | |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 131 | static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd, |
| 132 | const unsigned char *buffer, |
| 133 | unsigned int buflen, u32 block) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 134 | { |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 135 | const unsigned char *byte, *end; |
| 136 | unsigned char cur_state; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 137 | unsigned int bit; |
| 138 | |
| 139 | byte = buffer + (block / GFS2_NBBY); |
| 140 | bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE; |
| 141 | end = buffer + buflen; |
| 142 | |
| 143 | gfs2_assert(rgd->rd_sbd, byte < end); |
| 144 | |
| 145 | cur_state = (*byte >> bit) & GFS2_BIT_MASK; |
| 146 | |
| 147 | return cur_state; |
| 148 | } |
| 149 | |
| 150 | /** |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 151 | * gfs2_bit_search |
| 152 | * @ptr: Pointer to bitmap data |
| 153 | * @mask: Mask to use (normally 0x55555.... but adjusted for search start) |
| 154 | * @state: The state we are searching for |
| 155 | * |
| 156 | * We xor the bitmap data with a patter which is the bitwise opposite |
| 157 | * of what we are looking for, this gives rise to a pattern of ones |
| 158 | * wherever there is a match. Since we have two bits per entry, we |
| 159 | * take this pattern, shift it down by one place and then and it with |
| 160 | * the original. All the even bit positions (0,2,4, etc) then represent |
| 161 | * successful matches, so we mask with 0x55555..... to remove the unwanted |
| 162 | * odd bit positions. |
| 163 | * |
| 164 | * This allows searching of a whole u64 at once (32 blocks) with a |
| 165 | * single test (on 64 bit arches). |
| 166 | */ |
| 167 | |
| 168 | static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state) |
| 169 | { |
| 170 | u64 tmp; |
| 171 | static const u64 search[] = { |
Hannes Eder | 075ac44 | 2009-02-21 02:11:42 +0100 | [diff] [blame] | 172 | [0] = 0xffffffffffffffffULL, |
| 173 | [1] = 0xaaaaaaaaaaaaaaaaULL, |
| 174 | [2] = 0x5555555555555555ULL, |
| 175 | [3] = 0x0000000000000000ULL, |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 176 | }; |
| 177 | tmp = le64_to_cpu(*ptr) ^ search[state]; |
| 178 | tmp &= (tmp >> 1); |
| 179 | tmp &= mask; |
| 180 | return tmp; |
| 181 | } |
| 182 | |
| 183 | /** |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 184 | * rs_cmp - multi-block reservation range compare |
| 185 | * @blk: absolute file system block number of the new reservation |
| 186 | * @len: number of blocks in the new reservation |
| 187 | * @rs: existing reservation to compare against |
| 188 | * |
| 189 | * returns: 1 if the block range is beyond the reach of the reservation |
| 190 | * -1 if the block range is before the start of the reservation |
| 191 | * 0 if the block range overlaps with the reservation |
| 192 | */ |
| 193 | static inline int rs_cmp(u64 blk, u32 len, struct gfs2_blkreserv *rs) |
| 194 | { |
| 195 | u64 startblk = gfs2_rs_startblk(rs); |
| 196 | |
| 197 | if (blk >= startblk + rs->rs_free) |
| 198 | return 1; |
| 199 | if (blk + len - 1 < startblk) |
| 200 | return -1; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * rs_find - Find a rgrp multi-block reservation that contains a given block |
| 206 | * @rgd: The rgrp |
| 207 | * @rgblk: The block we're looking for, relative to the rgrp |
| 208 | */ |
| 209 | static struct gfs2_blkreserv *rs_find(struct gfs2_rgrpd *rgd, u32 rgblk) |
| 210 | { |
| 211 | struct rb_node **newn; |
| 212 | int rc; |
| 213 | u64 fsblk = rgblk + rgd->rd_data0; |
| 214 | |
| 215 | spin_lock(&rgd->rd_rsspin); |
| 216 | newn = &rgd->rd_rstree.rb_node; |
| 217 | while (*newn) { |
| 218 | struct gfs2_blkreserv *cur = |
| 219 | rb_entry(*newn, struct gfs2_blkreserv, rs_node); |
| 220 | rc = rs_cmp(fsblk, 1, cur); |
| 221 | if (rc < 0) |
| 222 | newn = &((*newn)->rb_left); |
| 223 | else if (rc > 0) |
| 224 | newn = &((*newn)->rb_right); |
| 225 | else { |
| 226 | spin_unlock(&rgd->rd_rsspin); |
| 227 | return cur; |
| 228 | } |
| 229 | } |
| 230 | spin_unlock(&rgd->rd_rsspin); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | /** |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 235 | * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing |
| 236 | * a block in a given allocation state. |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 237 | * @buf: the buffer that holds the bitmaps |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 238 | * @len: the length (in bytes) of the buffer |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 239 | * @goal: start search at this block's bit-pair (within @buffer) |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 240 | * @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] | 241 | * |
| 242 | * Scope of @goal and returned block number is only within this bitmap buffer, |
| 243 | * not entire rgrp or filesystem. @buffer will be offset from the actual |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 244 | * beginning of a bitmap block buffer, skipping any header structures, but |
| 245 | * headers are always a multiple of 64 bits long so that the buffer is |
| 246 | * always aligned to a 64 bit boundary. |
| 247 | * |
| 248 | * 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] | 249 | * 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] | 250 | * 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] | 251 | * |
| 252 | * Return: the block number (bitmap buffer scope) that was found |
| 253 | */ |
| 254 | |
Hannes Eder | 02ab172 | 2009-02-21 02:12:05 +0100 | [diff] [blame] | 255 | static u32 gfs2_bitfit(const u8 *buf, const unsigned int len, |
| 256 | u32 goal, u8 state) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 257 | { |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 258 | u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1); |
| 259 | const __le64 *ptr = ((__le64 *)buf) + (goal >> 5); |
| 260 | const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64))); |
| 261 | u64 tmp; |
Hannes Eder | 075ac44 | 2009-02-21 02:11:42 +0100 | [diff] [blame] | 262 | u64 mask = 0x5555555555555555ULL; |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 263 | u32 bit; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 264 | |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 265 | BUG_ON(state > 3); |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 266 | |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 267 | /* Mask off bits we don't care about at the start of the search */ |
| 268 | mask <<= spoint; |
| 269 | tmp = gfs2_bit_search(ptr, mask, state); |
| 270 | ptr++; |
| 271 | while(tmp == 0 && ptr < end) { |
Hannes Eder | 075ac44 | 2009-02-21 02:11:42 +0100 | [diff] [blame] | 272 | tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state); |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 273 | ptr++; |
Bob Peterson | 1f466a4 | 2008-03-10 18:17:47 -0500 | [diff] [blame] | 274 | } |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 275 | /* Mask off any bits which are more than len bytes from the start */ |
| 276 | if (ptr == end && (len & (sizeof(u64) - 1))) |
| 277 | tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1)))); |
| 278 | /* Didn't find anything, so return */ |
| 279 | if (tmp == 0) |
| 280 | return BFITNOENT; |
| 281 | ptr--; |
Steven Whitehouse | d8bd504 | 2009-04-23 08:54:02 +0100 | [diff] [blame] | 282 | bit = __ffs64(tmp); |
Steven Whitehouse | 223b2b8 | 2009-02-17 14:13:35 +0000 | [diff] [blame] | 283 | bit /= 2; /* two bits per entry in the bitmap */ |
| 284 | return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
| 288 | * gfs2_bitcount - count the number of bits in a certain state |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 289 | * @rgd: the resource group descriptor |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 290 | * @buffer: the buffer that holds the bitmaps |
| 291 | * @buflen: the length (in bytes) of the buffer |
| 292 | * @state: the state of the block we're looking for |
| 293 | * |
| 294 | * Returns: The number of bits |
| 295 | */ |
| 296 | |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 297 | static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer, |
| 298 | unsigned int buflen, u8 state) |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 299 | { |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 300 | const u8 *byte = buffer; |
| 301 | const u8 *end = buffer + buflen; |
| 302 | const u8 state1 = state << 2; |
| 303 | const u8 state2 = state << 4; |
| 304 | const u8 state3 = state << 6; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 305 | u32 count = 0; |
Steven Whitehouse | 88c8ab1f | 2006-05-18 13:52:39 -0400 | [diff] [blame] | 306 | |
| 307 | for (; byte < end; byte++) { |
| 308 | if (((*byte) & 0x03) == state) |
| 309 | count++; |
| 310 | if (((*byte) & 0x0C) == state1) |
| 311 | count++; |
| 312 | if (((*byte) & 0x30) == state2) |
| 313 | count++; |
| 314 | if (((*byte) & 0xC0) == state3) |
| 315 | count++; |
| 316 | } |
| 317 | |
| 318 | return count; |
| 319 | } |
| 320 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 321 | /** |
| 322 | * gfs2_rgrp_verify - Verify that a resource group is consistent |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 323 | * @rgd: the rgrp |
| 324 | * |
| 325 | */ |
| 326 | |
| 327 | void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd) |
| 328 | { |
| 329 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 330 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 331 | u32 length = rgd->rd_length; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 332 | u32 count[4], tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 333 | int buf, x; |
| 334 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 335 | memset(count, 0, 4 * sizeof(u32)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 336 | |
| 337 | /* Count # blocks in each of 4 possible allocation states */ |
| 338 | for (buf = 0; buf < length; buf++) { |
| 339 | bi = rgd->rd_bits + buf; |
| 340 | for (x = 0; x < 4; x++) |
| 341 | count[x] += gfs2_bitcount(rgd, |
| 342 | bi->bi_bh->b_data + |
| 343 | bi->bi_offset, |
| 344 | bi->bi_len, x); |
| 345 | } |
| 346 | |
Steven Whitehouse | cfc8b54 | 2008-11-04 10:25:13 +0000 | [diff] [blame] | 347 | if (count[0] != rgd->rd_free) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 348 | if (gfs2_consist_rgrpd(rgd)) |
| 349 | fs_err(sdp, "free data mismatch: %u != %u\n", |
Steven Whitehouse | cfc8b54 | 2008-11-04 10:25:13 +0000 | [diff] [blame] | 350 | count[0], rgd->rd_free); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
Steven Whitehouse | 73f7494 | 2008-11-04 10:32:57 +0000 | [diff] [blame] | 354 | tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes; |
Benjamin Marzinski | 6b94617 | 2009-07-10 18:13:26 -0500 | [diff] [blame] | 355 | if (count[1] != tmp) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 356 | if (gfs2_consist_rgrpd(rgd)) |
| 357 | fs_err(sdp, "used data mismatch: %u != %u\n", |
| 358 | count[1], tmp); |
| 359 | return; |
| 360 | } |
| 361 | |
Benjamin Marzinski | 6b94617 | 2009-07-10 18:13:26 -0500 | [diff] [blame] | 362 | if (count[2] + count[3] != rgd->rd_dinodes) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 363 | if (gfs2_consist_rgrpd(rgd)) |
| 364 | fs_err(sdp, "used metadata mismatch: %u != %u\n", |
Benjamin Marzinski | 6b94617 | 2009-07-10 18:13:26 -0500 | [diff] [blame] | 365 | count[2] + count[3], rgd->rd_dinodes); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | } |
| 369 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 370 | static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 371 | { |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 372 | u64 first = rgd->rd_data0; |
| 373 | u64 last = first + rgd->rd_data; |
Steven Whitehouse | 1691042 | 2006-09-05 11:15:45 -0400 | [diff] [blame] | 374 | return first <= block && block < last; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * gfs2_blk2rgrpd - Find resource group for a given data/meta block number |
| 379 | * @sdp: The GFS2 superblock |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 380 | * @blk: The data block number |
| 381 | * @exact: True if this needs to be an exact match |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 382 | * |
| 383 | * Returns: The resource group, or NULL if not found |
| 384 | */ |
| 385 | |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 386 | struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 387 | { |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 388 | struct rb_node *n, *next; |
Steven Whitehouse | f75bbfb | 2011-09-08 10:21:13 +0100 | [diff] [blame] | 389 | struct gfs2_rgrpd *cur; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 390 | |
| 391 | spin_lock(&sdp->sd_rindex_spin); |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 392 | n = sdp->sd_rindex_tree.rb_node; |
| 393 | while (n) { |
| 394 | cur = rb_entry(n, struct gfs2_rgrpd, rd_node); |
| 395 | next = NULL; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 396 | if (blk < cur->rd_addr) |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 397 | next = n->rb_left; |
Steven Whitehouse | f75bbfb | 2011-09-08 10:21:13 +0100 | [diff] [blame] | 398 | else if (blk >= cur->rd_data0 + cur->rd_data) |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 399 | next = n->rb_right; |
| 400 | if (next == NULL) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 401 | spin_unlock(&sdp->sd_rindex_spin); |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 402 | if (exact) { |
| 403 | if (blk < cur->rd_addr) |
| 404 | return NULL; |
| 405 | if (blk >= cur->rd_data0 + cur->rd_data) |
| 406 | return NULL; |
| 407 | } |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 408 | return cur; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 409 | } |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 410 | n = next; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 411 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 412 | spin_unlock(&sdp->sd_rindex_spin); |
| 413 | |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem |
| 419 | * @sdp: The GFS2 superblock |
| 420 | * |
| 421 | * Returns: The first rgrp in the filesystem |
| 422 | */ |
| 423 | |
| 424 | struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp) |
| 425 | { |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 426 | const struct rb_node *n; |
| 427 | struct gfs2_rgrpd *rgd; |
| 428 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 429 | spin_lock(&sdp->sd_rindex_spin); |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 430 | n = rb_first(&sdp->sd_rindex_tree); |
| 431 | rgd = rb_entry(n, struct gfs2_rgrpd, rd_node); |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 432 | spin_unlock(&sdp->sd_rindex_spin); |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 433 | |
| 434 | return rgd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * gfs2_rgrpd_get_next - get the next RG |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 439 | * @rgd: the resource group descriptor |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 440 | * |
| 441 | * Returns: The next rgrp |
| 442 | */ |
| 443 | |
| 444 | struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd) |
| 445 | { |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 446 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 447 | const struct rb_node *n; |
| 448 | |
| 449 | spin_lock(&sdp->sd_rindex_spin); |
| 450 | n = rb_next(&rgd->rd_node); |
| 451 | if (n == NULL) |
| 452 | n = rb_first(&sdp->sd_rindex_tree); |
| 453 | |
| 454 | if (unlikely(&rgd->rd_node == n)) { |
| 455 | spin_unlock(&sdp->sd_rindex_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 456 | return NULL; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 457 | } |
| 458 | rgd = rb_entry(n, struct gfs2_rgrpd, rd_node); |
| 459 | spin_unlock(&sdp->sd_rindex_spin); |
| 460 | return rgd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 463 | void gfs2_free_clones(struct gfs2_rgrpd *rgd) |
| 464 | { |
| 465 | int x; |
| 466 | |
| 467 | for (x = 0; x < rgd->rd_length; x++) { |
| 468 | struct gfs2_bitmap *bi = rgd->rd_bits + x; |
| 469 | kfree(bi->bi_clone); |
| 470 | bi->bi_clone = NULL; |
| 471 | } |
| 472 | } |
| 473 | |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 474 | /** |
| 475 | * gfs2_rs_alloc - make sure we have a reservation assigned to the inode |
| 476 | * @ip: the inode for this reservation |
| 477 | */ |
| 478 | int gfs2_rs_alloc(struct gfs2_inode *ip) |
| 479 | { |
| 480 | int error = 0; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 481 | struct gfs2_blkreserv *res; |
| 482 | |
| 483 | if (ip->i_res) |
| 484 | return 0; |
| 485 | |
| 486 | res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS); |
| 487 | if (!res) |
| 488 | error = -ENOMEM; |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 489 | |
| 490 | down_write(&ip->i_rw_mutex); |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 491 | if (ip->i_res) |
| 492 | kmem_cache_free(gfs2_rsrv_cachep, res); |
| 493 | else |
| 494 | ip->i_res = res; |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 495 | up_write(&ip->i_rw_mutex); |
| 496 | return error; |
| 497 | } |
| 498 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 499 | static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs) |
| 500 | { |
| 501 | gfs2_print_dbg(seq, " r: %llu s:%llu b:%u f:%u\n", |
| 502 | rs->rs_rgd->rd_addr, gfs2_rs_startblk(rs), rs->rs_biblk, |
| 503 | rs->rs_free); |
| 504 | } |
| 505 | |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 506 | /** |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 507 | * __rs_deltree - remove a multi-block reservation from the rgd tree |
| 508 | * @rs: The reservation to remove |
| 509 | * |
| 510 | */ |
| 511 | static void __rs_deltree(struct gfs2_blkreserv *rs) |
| 512 | { |
| 513 | struct gfs2_rgrpd *rgd; |
| 514 | |
| 515 | if (!gfs2_rs_active(rs)) |
| 516 | return; |
| 517 | |
| 518 | rgd = rs->rs_rgd; |
| 519 | /* We can't do this: The reason is that when the rgrp is invalidated, |
| 520 | it's in the "middle" of acquiring the glock, but the HOLDER bit |
| 521 | isn't set yet: |
| 522 | BUG_ON(!gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl));*/ |
| 523 | trace_gfs2_rs(NULL, rs, TRACE_RS_TREEDEL); |
| 524 | |
| 525 | if (!RB_EMPTY_ROOT(&rgd->rd_rstree)) |
| 526 | rb_erase(&rs->rs_node, &rgd->rd_rstree); |
| 527 | BUG_ON(!rgd->rd_rs_cnt); |
| 528 | rgd->rd_rs_cnt--; |
| 529 | |
| 530 | if (rs->rs_free) { |
| 531 | /* return reserved blocks to the rgrp and the ip */ |
| 532 | BUG_ON(rs->rs_rgd->rd_reserved < rs->rs_free); |
| 533 | rs->rs_rgd->rd_reserved -= rs->rs_free; |
| 534 | rs->rs_free = 0; |
| 535 | clear_bit(GBF_FULL, &rs->rs_bi->bi_flags); |
| 536 | smp_mb__after_clear_bit(); |
| 537 | } |
| 538 | /* We can't change any of the step 1 or step 2 components of the rs. |
| 539 | E.g. We can't set rs_rgd to NULL because the rgd glock is held and |
| 540 | dequeued through this pointer. |
| 541 | Can't: atomic_set(&rs->rs_sizehint, 0); |
| 542 | Can't: rs->rs_requested = 0; |
| 543 | Can't: rs->rs_rgd = NULL;*/ |
| 544 | rs->rs_bi = NULL; |
| 545 | rs->rs_biblk = 0; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree |
| 550 | * @rs: The reservation to remove |
| 551 | * |
| 552 | */ |
| 553 | void gfs2_rs_deltree(struct gfs2_blkreserv *rs) |
| 554 | { |
| 555 | struct gfs2_rgrpd *rgd; |
| 556 | |
| 557 | if (!gfs2_rs_active(rs)) |
| 558 | return; |
| 559 | |
| 560 | rgd = rs->rs_rgd; |
| 561 | spin_lock(&rgd->rd_rsspin); |
| 562 | __rs_deltree(rs); |
| 563 | spin_unlock(&rgd->rd_rsspin); |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * gfs2_rs_delete - delete a multi-block reservation |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 568 | * @ip: The inode for this reservation |
| 569 | * |
| 570 | */ |
| 571 | void gfs2_rs_delete(struct gfs2_inode *ip) |
| 572 | { |
| 573 | down_write(&ip->i_rw_mutex); |
| 574 | if (ip->i_res) { |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 575 | gfs2_rs_deltree(ip->i_res); |
| 576 | trace_gfs2_rs(ip, ip->i_res, TRACE_RS_DELETE); |
| 577 | BUG_ON(ip->i_res->rs_free); |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 578 | kmem_cache_free(gfs2_rsrv_cachep, ip->i_res); |
| 579 | ip->i_res = NULL; |
| 580 | } |
| 581 | up_write(&ip->i_rw_mutex); |
| 582 | } |
| 583 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 584 | /** |
| 585 | * return_all_reservations - return all reserved blocks back to the rgrp. |
| 586 | * @rgd: the rgrp that needs its space back |
| 587 | * |
| 588 | * We previously reserved a bunch of blocks for allocation. Now we need to |
| 589 | * give them back. This leave the reservation structures in tact, but removes |
| 590 | * all of their corresponding "no-fly zones". |
| 591 | */ |
| 592 | static void return_all_reservations(struct gfs2_rgrpd *rgd) |
| 593 | { |
| 594 | struct rb_node *n; |
| 595 | struct gfs2_blkreserv *rs; |
| 596 | |
| 597 | spin_lock(&rgd->rd_rsspin); |
| 598 | while ((n = rb_first(&rgd->rd_rstree))) { |
| 599 | rs = rb_entry(n, struct gfs2_blkreserv, rs_node); |
| 600 | __rs_deltree(rs); |
| 601 | } |
| 602 | spin_unlock(&rgd->rd_rsspin); |
| 603 | } |
| 604 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 605 | void gfs2_clear_rgrpd(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 606 | { |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 607 | struct rb_node *n; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 608 | struct gfs2_rgrpd *rgd; |
| 609 | struct gfs2_glock *gl; |
| 610 | |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 611 | while ((n = rb_first(&sdp->sd_rindex_tree))) { |
| 612 | rgd = rb_entry(n, struct gfs2_rgrpd, rd_node); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 613 | gl = rgd->rd_gl; |
| 614 | |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 615 | rb_erase(n, &sdp->sd_rindex_tree); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 616 | |
| 617 | if (gl) { |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 618 | spin_lock(&gl->gl_spin); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 619 | gl->gl_object = NULL; |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 620 | spin_unlock(&gl->gl_spin); |
Steven Whitehouse | 29687a2 | 2011-03-30 16:33:25 +0100 | [diff] [blame] | 621 | gfs2_glock_add_to_lru(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 622 | gfs2_glock_put(gl); |
| 623 | } |
| 624 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 625 | gfs2_free_clones(rgd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 626 | kfree(rgd->rd_bits); |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 627 | return_all_reservations(rgd); |
Bob Peterson | 6bdd9be | 2008-01-28 17:20:26 -0600 | [diff] [blame] | 628 | kmem_cache_free(gfs2_rgrpd_cachep, rgd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 632 | static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd) |
| 633 | { |
| 634 | printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr); |
| 635 | printk(KERN_INFO " ri_length = %u\n", rgd->rd_length); |
| 636 | printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0); |
| 637 | printk(KERN_INFO " ri_data = %u\n", rgd->rd_data); |
| 638 | printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes); |
| 639 | } |
| 640 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 641 | /** |
| 642 | * gfs2_compute_bitstructs - Compute the bitmap sizes |
| 643 | * @rgd: The resource group descriptor |
| 644 | * |
| 645 | * Calculates bitmap descriptors, one for each block that contains bitmap data |
| 646 | * |
| 647 | * Returns: errno |
| 648 | */ |
| 649 | |
| 650 | static int compute_bitstructs(struct gfs2_rgrpd *rgd) |
| 651 | { |
| 652 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 653 | struct gfs2_bitmap *bi; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 654 | u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */ |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 655 | u32 bytes_left, bytes; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 656 | int x; |
| 657 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 658 | if (!length) |
| 659 | return -EINVAL; |
| 660 | |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 661 | rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 662 | if (!rgd->rd_bits) |
| 663 | return -ENOMEM; |
| 664 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 665 | bytes_left = rgd->rd_bitbytes; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 666 | |
| 667 | for (x = 0; x < length; x++) { |
| 668 | bi = rgd->rd_bits + x; |
| 669 | |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 670 | bi->bi_flags = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 671 | /* small rgrp; bitmap stored completely in header block */ |
| 672 | if (length == 1) { |
| 673 | bytes = bytes_left; |
| 674 | bi->bi_offset = sizeof(struct gfs2_rgrp); |
| 675 | bi->bi_start = 0; |
| 676 | bi->bi_len = bytes; |
| 677 | /* header block */ |
| 678 | } else if (x == 0) { |
| 679 | bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp); |
| 680 | bi->bi_offset = sizeof(struct gfs2_rgrp); |
| 681 | bi->bi_start = 0; |
| 682 | bi->bi_len = bytes; |
| 683 | /* last block */ |
| 684 | } else if (x + 1 == length) { |
| 685 | bytes = bytes_left; |
| 686 | bi->bi_offset = sizeof(struct gfs2_meta_header); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 687 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 688 | bi->bi_len = bytes; |
| 689 | /* other blocks */ |
| 690 | } else { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 691 | bytes = sdp->sd_sb.sb_bsize - |
| 692 | sizeof(struct gfs2_meta_header); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 693 | bi->bi_offset = sizeof(struct gfs2_meta_header); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 694 | bi->bi_start = rgd->rd_bitbytes - bytes_left; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 695 | bi->bi_len = bytes; |
| 696 | } |
| 697 | |
| 698 | bytes_left -= bytes; |
| 699 | } |
| 700 | |
| 701 | if (bytes_left) { |
| 702 | gfs2_consist_rgrpd(rgd); |
| 703 | return -EIO; |
| 704 | } |
| 705 | bi = rgd->rd_bits + (length - 1); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 706 | if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 707 | if (gfs2_consist_rgrpd(rgd)) { |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 708 | gfs2_rindex_print(rgd); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 709 | fs_err(sdp, "start=%u len=%u offset=%u\n", |
| 710 | bi->bi_start, bi->bi_len, bi->bi_offset); |
| 711 | } |
| 712 | return -EIO; |
| 713 | } |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | /** |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 719 | * gfs2_ri_total - Total up the file system space, according to the rindex. |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 720 | * @sdp: the filesystem |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 721 | * |
| 722 | */ |
| 723 | u64 gfs2_ri_total(struct gfs2_sbd *sdp) |
| 724 | { |
| 725 | u64 total_data = 0; |
| 726 | struct inode *inode = sdp->sd_rindex; |
| 727 | struct gfs2_inode *ip = GFS2_I(inode); |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 728 | char buf[sizeof(struct gfs2_rindex)]; |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 729 | int error, rgrps; |
| 730 | |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 731 | for (rgrps = 0;; rgrps++) { |
| 732 | loff_t pos = rgrps * sizeof(struct gfs2_rindex); |
| 733 | |
Bob Peterson | bcd7278 | 2010-12-07 13:58:56 -0500 | [diff] [blame] | 734 | if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode)) |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 735 | break; |
Andrew Price | 4306629 | 2012-04-16 16:40:55 +0100 | [diff] [blame] | 736 | error = gfs2_internal_read(ip, buf, &pos, |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 737 | sizeof(struct gfs2_rindex)); |
| 738 | if (error != sizeof(struct gfs2_rindex)) |
| 739 | break; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 740 | total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data); |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 741 | } |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 742 | return total_data; |
| 743 | } |
| 744 | |
Bob Peterson | 6aad1c3 | 2012-03-05 09:20:59 -0500 | [diff] [blame] | 745 | static int rgd_insert(struct gfs2_rgrpd *rgd) |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 746 | { |
| 747 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 748 | struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL; |
| 749 | |
| 750 | /* Figure out where to put new node */ |
| 751 | while (*newn) { |
| 752 | struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd, |
| 753 | rd_node); |
| 754 | |
| 755 | parent = *newn; |
| 756 | if (rgd->rd_addr < cur->rd_addr) |
| 757 | newn = &((*newn)->rb_left); |
| 758 | else if (rgd->rd_addr > cur->rd_addr) |
| 759 | newn = &((*newn)->rb_right); |
| 760 | else |
Bob Peterson | 6aad1c3 | 2012-03-05 09:20:59 -0500 | [diff] [blame] | 761 | return -EEXIST; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | rb_link_node(&rgd->rd_node, parent, newn); |
| 765 | rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree); |
Bob Peterson | 6aad1c3 | 2012-03-05 09:20:59 -0500 | [diff] [blame] | 766 | sdp->sd_rgrps++; |
| 767 | return 0; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Robert Peterson | 7ae8fa8 | 2007-05-09 09:37:57 -0500 | [diff] [blame] | 770 | /** |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 771 | * read_rindex_entry - Pull in a new resource index entry from the disk |
Andrew Price | 4306629 | 2012-04-16 16:40:55 +0100 | [diff] [blame] | 772 | * @ip: Pointer to the rindex inode |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 773 | * |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 774 | * Returns: 0 on success, > 0 on EOF, error code otherwise |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 775 | */ |
| 776 | |
Andrew Price | 4306629 | 2012-04-16 16:40:55 +0100 | [diff] [blame] | 777 | static int read_rindex_entry(struct gfs2_inode *ip) |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 778 | { |
| 779 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 780 | loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex); |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 781 | struct gfs2_rindex buf; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 782 | int error; |
| 783 | struct gfs2_rgrpd *rgd; |
| 784 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 785 | if (pos >= i_size_read(&ip->i_inode)) |
| 786 | return 1; |
| 787 | |
Andrew Price | 4306629 | 2012-04-16 16:40:55 +0100 | [diff] [blame] | 788 | error = gfs2_internal_read(ip, (char *)&buf, &pos, |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 789 | sizeof(struct gfs2_rindex)); |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 790 | |
| 791 | if (error != sizeof(struct gfs2_rindex)) |
| 792 | return (error == 0) ? 1 : error; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 793 | |
Bob Peterson | 6bdd9be | 2008-01-28 17:20:26 -0600 | [diff] [blame] | 794 | rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS); |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 795 | error = -ENOMEM; |
| 796 | if (!rgd) |
| 797 | return error; |
| 798 | |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 799 | rgd->rd_sbd = sdp; |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 800 | rgd->rd_addr = be64_to_cpu(buf.ri_addr); |
| 801 | rgd->rd_length = be32_to_cpu(buf.ri_length); |
| 802 | rgd->rd_data0 = be64_to_cpu(buf.ri_data0); |
| 803 | rgd->rd_data = be32_to_cpu(buf.ri_data); |
| 804 | rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes); |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 805 | spin_lock_init(&rgd->rd_rsspin); |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 806 | |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 807 | error = compute_bitstructs(rgd); |
| 808 | if (error) |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 809 | goto fail; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 810 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 811 | error = gfs2_glock_get(sdp, rgd->rd_addr, |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 812 | &gfs2_rgrp_glops, CREATE, &rgd->rd_gl); |
| 813 | if (error) |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 814 | goto fail; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 815 | |
| 816 | rgd->rd_gl->gl_object = rgd; |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 817 | rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lvb; |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 818 | rgd->rd_flags &= ~GFS2_RDF_UPTODATE; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 819 | if (rgd->rd_data > sdp->sd_max_rg_data) |
| 820 | sdp->sd_max_rg_data = rgd->rd_data; |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 821 | spin_lock(&sdp->sd_rindex_spin); |
Bob Peterson | 6aad1c3 | 2012-03-05 09:20:59 -0500 | [diff] [blame] | 822 | error = rgd_insert(rgd); |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 823 | spin_unlock(&sdp->sd_rindex_spin); |
Bob Peterson | 6aad1c3 | 2012-03-05 09:20:59 -0500 | [diff] [blame] | 824 | if (!error) |
| 825 | return 0; |
| 826 | |
| 827 | error = 0; /* someone else read in the rgrp; free it and ignore it */ |
Bob Peterson | c1ac539 | 2012-03-22 08:58:30 -0400 | [diff] [blame] | 828 | gfs2_glock_put(rgd->rd_gl); |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 829 | |
| 830 | fail: |
| 831 | kfree(rgd->rd_bits); |
| 832 | kmem_cache_free(gfs2_rgrpd_cachep, rgd); |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 833 | return error; |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * gfs2_ri_update - Pull in a new resource index from the disk |
| 838 | * @ip: pointer to the rindex inode |
| 839 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 840 | * Returns: 0 on successful update, error code otherwise |
| 841 | */ |
| 842 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 843 | static int gfs2_ri_update(struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 844 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 845 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 846 | int error; |
| 847 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 848 | do { |
Andrew Price | 4306629 | 2012-04-16 16:40:55 +0100 | [diff] [blame] | 849 | error = read_rindex_entry(ip); |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 850 | } while (error == 0); |
| 851 | |
| 852 | if (error < 0) |
| 853 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 854 | |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 855 | sdp->sd_rindex_uptodate = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 856 | return 0; |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 857 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 858 | |
Robert Peterson | 6c53267 | 2007-05-10 16:54:38 -0500 | [diff] [blame] | 859 | /** |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 860 | * gfs2_rindex_update - Update the rindex if required |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 861 | * @sdp: The GFS2 superblock |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 862 | * |
| 863 | * We grab a lock on the rindex inode to make sure that it doesn't |
| 864 | * change whilst we are performing an operation. We keep this lock |
| 865 | * for quite long periods of time compared to other locks. This |
| 866 | * doesn't matter, since it is shared and it is very, very rarely |
| 867 | * accessed in the exclusive mode (i.e. only when expanding the filesystem). |
| 868 | * |
| 869 | * This makes sure that we're using the latest copy of the resource index |
| 870 | * special file, which might have been updated if someone expanded the |
| 871 | * filesystem (via gfs2_grow utility), which adds new resource groups. |
| 872 | * |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 873 | * Returns: 0 on succeess, error code otherwise |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 874 | */ |
| 875 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 876 | int gfs2_rindex_update(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 877 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 878 | struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 879 | struct gfs2_glock *gl = ip->i_gl; |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 880 | struct gfs2_holder ri_gh; |
| 881 | int error = 0; |
Steven Whitehouse | a365fbf | 2012-02-24 15:09:14 +0000 | [diff] [blame] | 882 | int unlock_required = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 883 | |
| 884 | /* Read new copy from disk if we don't have the latest */ |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 885 | if (!sdp->sd_rindex_uptodate) { |
Steven Whitehouse | a365fbf | 2012-02-24 15:09:14 +0000 | [diff] [blame] | 886 | if (!gfs2_glock_is_locked_by_me(gl)) { |
| 887 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh); |
| 888 | if (error) |
Bob Peterson | 6aad1c3 | 2012-03-05 09:20:59 -0500 | [diff] [blame] | 889 | return error; |
Steven Whitehouse | a365fbf | 2012-02-24 15:09:14 +0000 | [diff] [blame] | 890 | unlock_required = 1; |
| 891 | } |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 892 | if (!sdp->sd_rindex_uptodate) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 893 | error = gfs2_ri_update(ip); |
Steven Whitehouse | a365fbf | 2012-02-24 15:09:14 +0000 | [diff] [blame] | 894 | if (unlock_required) |
| 895 | gfs2_glock_dq_uninit(&ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | return error; |
| 899 | } |
| 900 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 901 | static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf) |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 902 | { |
| 903 | const struct gfs2_rgrp *str = buf; |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 904 | u32 rg_flags; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 905 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 906 | rg_flags = be32_to_cpu(str->rg_flags); |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 907 | rg_flags &= ~GFS2_RDF_MASK; |
Steven Whitehouse | 1ce97e5 | 2009-05-21 15:18:19 +0100 | [diff] [blame] | 908 | rgd->rd_flags &= GFS2_RDF_MASK; |
| 909 | rgd->rd_flags |= rg_flags; |
Steven Whitehouse | cfc8b54 | 2008-11-04 10:25:13 +0000 | [diff] [blame] | 910 | rgd->rd_free = be32_to_cpu(str->rg_free); |
Steven Whitehouse | 73f7494 | 2008-11-04 10:32:57 +0000 | [diff] [blame] | 911 | rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes); |
Steven Whitehouse | d8b71f7 | 2008-11-04 10:19:03 +0000 | [diff] [blame] | 912 | rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 913 | } |
| 914 | |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 915 | static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf) |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 916 | { |
| 917 | struct gfs2_rgrp *str = buf; |
| 918 | |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 919 | str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK); |
Steven Whitehouse | cfc8b54 | 2008-11-04 10:25:13 +0000 | [diff] [blame] | 920 | str->rg_free = cpu_to_be32(rgd->rd_free); |
Steven Whitehouse | 73f7494 | 2008-11-04 10:32:57 +0000 | [diff] [blame] | 921 | str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 922 | str->__pad = cpu_to_be32(0); |
Steven Whitehouse | d8b71f7 | 2008-11-04 10:19:03 +0000 | [diff] [blame] | 923 | str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration); |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 924 | memset(&str->rg_reserved, 0, sizeof(str->rg_reserved)); |
| 925 | } |
| 926 | |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 927 | static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd) |
| 928 | { |
| 929 | struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; |
| 930 | struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data; |
| 931 | |
| 932 | if (rgl->rl_flags != str->rg_flags || rgl->rl_free != str->rg_free || |
| 933 | rgl->rl_dinodes != str->rg_dinodes || |
| 934 | rgl->rl_igeneration != str->rg_igeneration) |
| 935 | return 0; |
| 936 | return 1; |
| 937 | } |
| 938 | |
| 939 | static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf) |
| 940 | { |
| 941 | const struct gfs2_rgrp *str = buf; |
| 942 | |
| 943 | rgl->rl_magic = cpu_to_be32(GFS2_MAGIC); |
| 944 | rgl->rl_flags = str->rg_flags; |
| 945 | rgl->rl_free = str->rg_free; |
| 946 | rgl->rl_dinodes = str->rg_dinodes; |
| 947 | rgl->rl_igeneration = str->rg_igeneration; |
| 948 | rgl->__pad = 0UL; |
| 949 | } |
| 950 | |
| 951 | static void update_rgrp_lvb_unlinked(struct gfs2_rgrpd *rgd, u32 change) |
| 952 | { |
| 953 | struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl; |
| 954 | u32 unlinked = be32_to_cpu(rgl->rl_unlinked) + change; |
| 955 | rgl->rl_unlinked = cpu_to_be32(unlinked); |
| 956 | } |
| 957 | |
| 958 | static u32 count_unlinked(struct gfs2_rgrpd *rgd) |
| 959 | { |
| 960 | struct gfs2_bitmap *bi; |
| 961 | const u32 length = rgd->rd_length; |
| 962 | const u8 *buffer = NULL; |
| 963 | u32 i, goal, count = 0; |
| 964 | |
| 965 | for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) { |
| 966 | goal = 0; |
| 967 | buffer = bi->bi_bh->b_data + bi->bi_offset; |
| 968 | WARN_ON(!buffer_uptodate(bi->bi_bh)); |
| 969 | while (goal < bi->bi_len * GFS2_NBBY) { |
| 970 | goal = gfs2_bitfit(buffer, bi->bi_len, goal, |
| 971 | GFS2_BLKST_UNLINKED); |
| 972 | if (goal == BFITNOENT) |
| 973 | break; |
| 974 | count++; |
| 975 | goal++; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | return count; |
| 980 | } |
| 981 | |
| 982 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 983 | /** |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 984 | * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps |
| 985 | * @rgd: the struct gfs2_rgrpd describing the RG to read in |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 986 | * |
| 987 | * Read in all of a Resource Group's header and bitmap blocks. |
| 988 | * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps. |
| 989 | * |
| 990 | * Returns: errno |
| 991 | */ |
| 992 | |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 993 | int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 994 | { |
| 995 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 996 | struct gfs2_glock *gl = rgd->rd_gl; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 997 | unsigned int length = rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 998 | struct gfs2_bitmap *bi; |
| 999 | unsigned int x, y; |
| 1000 | int error; |
| 1001 | |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1002 | if (rgd->rd_bits[0].bi_bh != NULL) |
| 1003 | return 0; |
| 1004 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1005 | for (x = 0; x < length; x++) { |
| 1006 | bi = rgd->rd_bits + x; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1007 | 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] | 1008 | if (error) |
| 1009 | goto fail; |
| 1010 | } |
| 1011 | |
| 1012 | for (y = length; y--;) { |
| 1013 | bi = rgd->rd_bits + y; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1014 | error = gfs2_meta_wait(sdp, bi->bi_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1015 | if (error) |
| 1016 | goto fail; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1017 | if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB : |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1018 | GFS2_METATYPE_RG)) { |
| 1019 | error = -EIO; |
| 1020 | goto fail; |
| 1021 | } |
| 1022 | } |
| 1023 | |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 1024 | if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) { |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1025 | for (x = 0; x < length; x++) |
| 1026 | clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags); |
Bob Peterson | 42d52e3 | 2008-01-28 18:38:07 -0600 | [diff] [blame] | 1027 | gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data); |
Steven Whitehouse | 1ce97e5 | 2009-05-21 15:18:19 +0100 | [diff] [blame] | 1028 | rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK); |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1029 | rgd->rd_free_clone = rgd->rd_free; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1030 | } |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1031 | if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) { |
| 1032 | rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd)); |
| 1033 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, |
| 1034 | rgd->rd_bits[0].bi_bh->b_data); |
| 1035 | } |
| 1036 | else if (sdp->sd_args.ar_rgrplvb) { |
| 1037 | if (!gfs2_rgrp_lvb_valid(rgd)){ |
| 1038 | gfs2_consist_rgrpd(rgd); |
| 1039 | error = -EIO; |
| 1040 | goto fail; |
| 1041 | } |
| 1042 | if (rgd->rd_rgl->rl_unlinked == 0) |
| 1043 | rgd->rd_flags &= ~GFS2_RDF_CHECK; |
| 1044 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1045 | return 0; |
| 1046 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1047 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1048 | while (x--) { |
| 1049 | bi = rgd->rd_bits + x; |
| 1050 | brelse(bi->bi_bh); |
| 1051 | bi->bi_bh = NULL; |
| 1052 | gfs2_assert_warn(sdp, !bi->bi_clone); |
| 1053 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1054 | |
| 1055 | return error; |
| 1056 | } |
| 1057 | |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1058 | int update_rgrp_lvb(struct gfs2_rgrpd *rgd) |
| 1059 | { |
| 1060 | u32 rl_flags; |
| 1061 | |
| 1062 | if (rgd->rd_flags & GFS2_RDF_UPTODATE) |
| 1063 | return 0; |
| 1064 | |
| 1065 | if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) |
| 1066 | return gfs2_rgrp_bh_get(rgd); |
| 1067 | |
| 1068 | rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags); |
| 1069 | rl_flags &= ~GFS2_RDF_MASK; |
| 1070 | rgd->rd_flags &= GFS2_RDF_MASK; |
| 1071 | rgd->rd_flags |= (rl_flags | GFS2_RDF_UPTODATE | GFS2_RDF_CHECK); |
| 1072 | if (rgd->rd_rgl->rl_unlinked == 0) |
| 1073 | rgd->rd_flags &= ~GFS2_RDF_CHECK; |
| 1074 | rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free); |
| 1075 | rgd->rd_free_clone = rgd->rd_free; |
| 1076 | rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes); |
| 1077 | rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration); |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | int gfs2_rgrp_go_lock(struct gfs2_holder *gh) |
| 1082 | { |
| 1083 | struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object; |
| 1084 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 1085 | |
| 1086 | if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb) |
| 1087 | return 0; |
| 1088 | return gfs2_rgrp_bh_get((struct gfs2_rgrpd *)gh->gh_gl->gl_object); |
| 1089 | } |
| 1090 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1091 | /** |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1092 | * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get() |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 1093 | * @gh: The glock holder for the resource group |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1094 | * |
| 1095 | */ |
| 1096 | |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1097 | void gfs2_rgrp_go_unlock(struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1098 | { |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1099 | struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1100 | int x, length = rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1101 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1102 | for (x = 0; x < length; x++) { |
| 1103 | struct gfs2_bitmap *bi = rgd->rd_bits + x; |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1104 | if (bi->bi_bh) { |
| 1105 | brelse(bi->bi_bh); |
| 1106 | bi->bi_bh = NULL; |
| 1107 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1112 | int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset, |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1113 | struct buffer_head *bh, |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1114 | const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed) |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1115 | { |
| 1116 | struct super_block *sb = sdp->sd_vfs; |
| 1117 | struct block_device *bdev = sb->s_bdev; |
| 1118 | const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize / |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1119 | bdev_logical_block_size(sb->s_bdev); |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1120 | u64 blk; |
Steven Whitehouse | 64d576b | 2009-02-12 13:31:58 +0000 | [diff] [blame] | 1121 | sector_t start = 0; |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1122 | sector_t nr_sects = 0; |
| 1123 | int rv; |
| 1124 | unsigned int x; |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1125 | u32 trimmed = 0; |
| 1126 | u8 diff; |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1127 | |
| 1128 | for (x = 0; x < bi->bi_len; x++) { |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1129 | const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data; |
| 1130 | clone += bi->bi_offset; |
| 1131 | clone += x; |
| 1132 | if (bh) { |
| 1133 | const u8 *orig = bh->b_data + bi->bi_offset + x; |
| 1134 | diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1)); |
| 1135 | } else { |
| 1136 | diff = ~(*clone | (*clone >> 1)); |
| 1137 | } |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1138 | diff &= 0x55; |
| 1139 | if (diff == 0) |
| 1140 | continue; |
| 1141 | blk = offset + ((bi->bi_start + x) * GFS2_NBBY); |
| 1142 | blk *= sects_per_blk; /* convert to sectors */ |
| 1143 | while(diff) { |
| 1144 | if (diff & 1) { |
| 1145 | if (nr_sects == 0) |
| 1146 | goto start_new_extent; |
| 1147 | if ((start + nr_sects) != blk) { |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1148 | if (nr_sects >= minlen) { |
| 1149 | rv = blkdev_issue_discard(bdev, |
| 1150 | start, nr_sects, |
| 1151 | GFP_NOFS, 0); |
| 1152 | if (rv) |
| 1153 | goto fail; |
| 1154 | trimmed += nr_sects; |
| 1155 | } |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1156 | nr_sects = 0; |
| 1157 | start_new_extent: |
| 1158 | start = blk; |
| 1159 | } |
| 1160 | nr_sects += sects_per_blk; |
| 1161 | } |
| 1162 | diff >>= 2; |
| 1163 | blk += sects_per_blk; |
| 1164 | } |
| 1165 | } |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1166 | if (nr_sects >= minlen) { |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 1167 | rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0); |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1168 | if (rv) |
| 1169 | goto fail; |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1170 | trimmed += nr_sects; |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1171 | } |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1172 | if (ptrimmed) |
| 1173 | *ptrimmed = trimmed; |
| 1174 | return 0; |
| 1175 | |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1176 | fail: |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1177 | if (sdp->sd_args.ar_discard) |
| 1178 | fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv); |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1179 | sdp->sd_args.ar_discard = 0; |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1180 | return -EIO; |
| 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * gfs2_fitrim - Generate discard requests for unused bits of the filesystem |
| 1185 | * @filp: Any file on the filesystem |
| 1186 | * @argp: Pointer to the arguments (also used to pass result) |
| 1187 | * |
| 1188 | * Returns: 0 on success, otherwise error code |
| 1189 | */ |
| 1190 | |
| 1191 | int gfs2_fitrim(struct file *filp, void __user *argp) |
| 1192 | { |
| 1193 | struct inode *inode = filp->f_dentry->d_inode; |
| 1194 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 1195 | struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev); |
| 1196 | struct buffer_head *bh; |
| 1197 | struct gfs2_rgrpd *rgd; |
| 1198 | struct gfs2_rgrpd *rgd_end; |
| 1199 | struct gfs2_holder gh; |
| 1200 | struct fstrim_range r; |
| 1201 | int ret = 0; |
| 1202 | u64 amt; |
| 1203 | u64 trimmed = 0; |
| 1204 | unsigned int x; |
| 1205 | |
| 1206 | if (!capable(CAP_SYS_ADMIN)) |
| 1207 | return -EPERM; |
| 1208 | |
| 1209 | if (!blk_queue_discard(q)) |
| 1210 | return -EOPNOTSUPP; |
| 1211 | |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1212 | if (argp == NULL) { |
| 1213 | r.start = 0; |
| 1214 | r.len = ULLONG_MAX; |
| 1215 | r.minlen = 0; |
| 1216 | } else if (copy_from_user(&r, argp, sizeof(r))) |
| 1217 | return -EFAULT; |
| 1218 | |
Bob Peterson | 5e2f7d6 | 2012-04-04 22:11:16 -0400 | [diff] [blame] | 1219 | ret = gfs2_rindex_update(sdp); |
| 1220 | if (ret) |
| 1221 | return ret; |
| 1222 | |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1223 | rgd = gfs2_blk2rgrpd(sdp, r.start, 0); |
| 1224 | rgd_end = gfs2_blk2rgrpd(sdp, r.start + r.len, 0); |
| 1225 | |
| 1226 | while (1) { |
| 1227 | |
| 1228 | ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 1229 | if (ret) |
| 1230 | goto out; |
| 1231 | |
| 1232 | if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) { |
| 1233 | /* Trim each bitmap in the rgrp */ |
| 1234 | for (x = 0; x < rgd->rd_length; x++) { |
| 1235 | struct gfs2_bitmap *bi = rgd->rd_bits + x; |
| 1236 | ret = gfs2_rgrp_send_discards(sdp, rgd->rd_data0, NULL, bi, r.minlen, &amt); |
| 1237 | if (ret) { |
| 1238 | gfs2_glock_dq_uninit(&gh); |
| 1239 | goto out; |
| 1240 | } |
| 1241 | trimmed += amt; |
| 1242 | } |
| 1243 | |
| 1244 | /* Mark rgrp as having been trimmed */ |
| 1245 | ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0); |
| 1246 | if (ret == 0) { |
| 1247 | bh = rgd->rd_bits[0].bi_bh; |
| 1248 | rgd->rd_flags |= GFS2_RGF_TRIMMED; |
| 1249 | gfs2_trans_add_bh(rgd->rd_gl, bh, 1); |
| 1250 | gfs2_rgrp_out(rgd, bh->b_data); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1251 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, bh->b_data); |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1252 | gfs2_trans_end(sdp); |
| 1253 | } |
| 1254 | } |
| 1255 | gfs2_glock_dq_uninit(&gh); |
| 1256 | |
| 1257 | if (rgd == rgd_end) |
| 1258 | break; |
| 1259 | |
| 1260 | rgd = gfs2_rgrpd_get_next(rgd); |
| 1261 | } |
| 1262 | |
| 1263 | out: |
| 1264 | r.len = trimmed << 9; |
| 1265 | if (argp && copy_to_user(argp, &r, sizeof(r))) |
| 1266 | return -EFAULT; |
| 1267 | |
| 1268 | return ret; |
Steven Whitehouse | f15ab56 | 2009-02-09 09:25:01 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1271 | /** |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1272 | * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree |
| 1273 | * @bi: the bitmap with the blocks |
| 1274 | * @ip: the inode structure |
| 1275 | * @biblk: the 32-bit block number relative to the start of the bitmap |
| 1276 | * @amount: the number of blocks to reserve |
| 1277 | * |
| 1278 | * Returns: NULL - reservation was already taken, so not inserted |
| 1279 | * pointer to the inserted reservation |
| 1280 | */ |
| 1281 | static struct gfs2_blkreserv *rs_insert(struct gfs2_bitmap *bi, |
| 1282 | struct gfs2_inode *ip, u32 biblk, |
| 1283 | int amount) |
| 1284 | { |
| 1285 | struct rb_node **newn, *parent = NULL; |
| 1286 | int rc; |
| 1287 | struct gfs2_blkreserv *rs = ip->i_res; |
| 1288 | struct gfs2_rgrpd *rgd = rs->rs_rgd; |
| 1289 | u64 fsblock = gfs2_bi2rgd_blk(bi, biblk) + rgd->rd_data0; |
| 1290 | |
| 1291 | spin_lock(&rgd->rd_rsspin); |
| 1292 | newn = &rgd->rd_rstree.rb_node; |
| 1293 | BUG_ON(!ip->i_res); |
| 1294 | BUG_ON(gfs2_rs_active(rs)); |
| 1295 | /* Figure out where to put new node */ |
| 1296 | /*BUG_ON(!gfs2_glock_is_locked_by_me(rgd->rd_gl));*/ |
| 1297 | while (*newn) { |
| 1298 | struct gfs2_blkreserv *cur = |
| 1299 | rb_entry(*newn, struct gfs2_blkreserv, rs_node); |
| 1300 | |
| 1301 | parent = *newn; |
| 1302 | rc = rs_cmp(fsblock, amount, cur); |
| 1303 | if (rc > 0) |
| 1304 | newn = &((*newn)->rb_right); |
| 1305 | else if (rc < 0) |
| 1306 | newn = &((*newn)->rb_left); |
| 1307 | else { |
| 1308 | spin_unlock(&rgd->rd_rsspin); |
| 1309 | return NULL; /* reservation already in use */ |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | /* Do our reservation work */ |
| 1314 | rs = ip->i_res; |
| 1315 | rs->rs_free = amount; |
| 1316 | rs->rs_biblk = biblk; |
| 1317 | rs->rs_bi = bi; |
| 1318 | rb_link_node(&rs->rs_node, parent, newn); |
| 1319 | rb_insert_color(&rs->rs_node, &rgd->rd_rstree); |
| 1320 | |
| 1321 | /* Do our inode accounting for the reservation */ |
| 1322 | /*BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));*/ |
| 1323 | |
| 1324 | /* Do our rgrp accounting for the reservation */ |
| 1325 | rgd->rd_reserved += amount; /* blocks reserved */ |
| 1326 | rgd->rd_rs_cnt++; /* number of in-tree reservations */ |
| 1327 | spin_unlock(&rgd->rd_rsspin); |
| 1328 | trace_gfs2_rs(ip, rs, TRACE_RS_INSERT); |
| 1329 | return rs; |
| 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * unclaimed_blocks - return number of blocks that aren't spoken for |
| 1334 | */ |
| 1335 | static u32 unclaimed_blocks(struct gfs2_rgrpd *rgd) |
| 1336 | { |
| 1337 | return rgd->rd_free_clone - rgd->rd_reserved; |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * rg_mblk_search - find a group of multiple free blocks |
| 1342 | * @rgd: the resource group descriptor |
| 1343 | * @rs: the block reservation |
| 1344 | * @ip: pointer to the inode for which we're reserving blocks |
| 1345 | * |
| 1346 | * This is very similar to rgblk_search, except we're looking for whole |
| 1347 | * 64-bit words that represent a chunk of 32 free blocks. I'm only focusing |
| 1348 | * on aligned dwords for speed's sake. |
| 1349 | * |
| 1350 | * Returns: 0 if successful or BFITNOENT if there isn't enough free space |
| 1351 | */ |
| 1352 | |
| 1353 | static int rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) |
| 1354 | { |
| 1355 | struct gfs2_bitmap *bi = rgd->rd_bits; |
| 1356 | const u32 length = rgd->rd_length; |
| 1357 | u32 blk; |
| 1358 | unsigned int buf, x, search_bytes; |
| 1359 | u8 *buffer = NULL; |
| 1360 | u8 *ptr, *end, *nonzero; |
| 1361 | u32 goal, rsv_bytes; |
| 1362 | struct gfs2_blkreserv *rs; |
| 1363 | u32 best_rs_bytes, unclaimed; |
| 1364 | int best_rs_blocks; |
| 1365 | |
| 1366 | /* Find bitmap block that contains bits for goal block */ |
| 1367 | if (rgrp_contains_block(rgd, ip->i_goal)) |
| 1368 | goal = ip->i_goal - rgd->rd_data0; |
| 1369 | else |
| 1370 | goal = rgd->rd_last_alloc; |
| 1371 | for (buf = 0; buf < length; buf++) { |
| 1372 | bi = rgd->rd_bits + buf; |
| 1373 | /* Convert scope of "goal" from rgrp-wide to within |
| 1374 | found bit block */ |
| 1375 | if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) { |
| 1376 | goal -= bi->bi_start * GFS2_NBBY; |
| 1377 | goto do_search; |
| 1378 | } |
| 1379 | } |
| 1380 | buf = 0; |
| 1381 | goal = 0; |
| 1382 | |
| 1383 | do_search: |
| 1384 | best_rs_blocks = max_t(int, atomic_read(&ip->i_res->rs_sizehint), |
| 1385 | (RGRP_RSRV_MINBLKS * rgd->rd_length)); |
| 1386 | best_rs_bytes = (best_rs_blocks * |
| 1387 | (1 + (RSRV_CONTENTION_FACTOR * rgd->rd_rs_cnt))) / |
| 1388 | GFS2_NBBY; /* 1 + is for our not-yet-created reservation */ |
| 1389 | best_rs_bytes = ALIGN(best_rs_bytes, sizeof(u64)); |
| 1390 | unclaimed = unclaimed_blocks(rgd); |
| 1391 | if (best_rs_bytes * GFS2_NBBY > unclaimed) |
| 1392 | best_rs_bytes = unclaimed >> GFS2_BIT_SIZE; |
| 1393 | |
| 1394 | for (x = 0; x <= length; x++) { |
| 1395 | bi = rgd->rd_bits + buf; |
| 1396 | |
| 1397 | if (test_bit(GBF_FULL, &bi->bi_flags)) |
| 1398 | goto skip; |
| 1399 | |
| 1400 | WARN_ON(!buffer_uptodate(bi->bi_bh)); |
| 1401 | if (bi->bi_clone) |
| 1402 | buffer = bi->bi_clone + bi->bi_offset; |
| 1403 | else |
| 1404 | buffer = bi->bi_bh->b_data + bi->bi_offset; |
| 1405 | |
| 1406 | /* We have to keep the reservations aligned on u64 boundaries |
| 1407 | otherwise we could get situations where a byte can't be |
| 1408 | used because it's after a reservation, but a free bit still |
| 1409 | is within the reservation's area. */ |
| 1410 | ptr = buffer + ALIGN(goal >> GFS2_BIT_SIZE, sizeof(u64)); |
| 1411 | end = (buffer + bi->bi_len); |
| 1412 | while (ptr < end) { |
| 1413 | rsv_bytes = 0; |
| 1414 | if ((ptr + best_rs_bytes) <= end) |
| 1415 | search_bytes = best_rs_bytes; |
| 1416 | else |
| 1417 | search_bytes = end - ptr; |
| 1418 | BUG_ON(!search_bytes); |
| 1419 | nonzero = memchr_inv(ptr, 0, search_bytes); |
| 1420 | /* If the lot is all zeroes, reserve the whole size. If |
| 1421 | there's enough zeroes to satisfy the request, use |
| 1422 | what we can. If there's not enough, keep looking. */ |
| 1423 | if (nonzero == NULL) |
| 1424 | rsv_bytes = search_bytes; |
| 1425 | else if ((nonzero - ptr) * GFS2_NBBY >= |
| 1426 | ip->i_res->rs_requested) |
| 1427 | rsv_bytes = (nonzero - ptr); |
| 1428 | |
| 1429 | if (rsv_bytes) { |
| 1430 | blk = ((ptr - buffer) * GFS2_NBBY); |
| 1431 | BUG_ON(blk >= bi->bi_len * GFS2_NBBY); |
| 1432 | rs = rs_insert(bi, ip, blk, |
| 1433 | rsv_bytes * GFS2_NBBY); |
| 1434 | if (IS_ERR(rs)) |
| 1435 | return PTR_ERR(rs); |
| 1436 | if (rs) |
| 1437 | return 0; |
| 1438 | } |
| 1439 | ptr += ALIGN(search_bytes, sizeof(u64)); |
| 1440 | } |
| 1441 | skip: |
| 1442 | /* Try next bitmap block (wrap back to rgrp header |
| 1443 | if at end) */ |
| 1444 | buf++; |
| 1445 | buf %= length; |
| 1446 | goal = 0; |
| 1447 | } |
| 1448 | |
| 1449 | return BFITNOENT; |
| 1450 | } |
| 1451 | |
| 1452 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1453 | * try_rgrp_fit - See if a given reservation will fit in a given RG |
| 1454 | * @rgd: the RG data |
Steven Whitehouse | 54335b1 | 2011-09-01 13:31:59 +0100 | [diff] [blame] | 1455 | * @ip: the inode |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1456 | * |
| 1457 | * If there's room for the requested blocks to be allocated from the RG: |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1458 | * This will try to get a multi-block reservation first, and if that doesn't |
| 1459 | * fit, it will take what it can. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1460 | * |
| 1461 | * Returns: 1 on success (it fits), 0 on failure (it doesn't fit) |
| 1462 | */ |
| 1463 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1464 | static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1465 | { |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1466 | struct gfs2_blkreserv *rs = ip->i_res; |
Steven Whitehouse | 54335b1 | 2011-09-01 13:31:59 +0100 | [diff] [blame] | 1467 | |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 1468 | if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR)) |
Steven Whitehouse | a43a490 | 2007-04-02 10:48:17 +0100 | [diff] [blame] | 1469 | return 0; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1470 | /* Look for a multi-block reservation. */ |
| 1471 | if (unclaimed_blocks(rgd) >= RGRP_RSRV_MINBLKS && |
| 1472 | rg_mblk_search(rgd, ip) != BFITNOENT) |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1473 | return 1; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1474 | if (unclaimed_blocks(rgd) >= rs->rs_requested) |
| 1475 | return 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1476 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1477 | return 0; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1478 | } |
| 1479 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1480 | /** |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1481 | * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes |
| 1482 | * @rgd: The rgrp |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 1483 | * @last_unlinked: block address of the last dinode we unlinked |
| 1484 | * @skip: block address we should explicitly not unlink |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1485 | * |
Bob Peterson | 1a0eae8 | 2010-04-14 11:58:16 -0400 | [diff] [blame] | 1486 | * Returns: 0 if no error |
| 1487 | * The inode, if one has been found, in inode. |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1488 | */ |
| 1489 | |
Steven Whitehouse | 044b941 | 2010-11-03 20:01:07 +0000 | [diff] [blame] | 1490 | 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] | 1491 | { |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 1492 | u32 goal = 0, block; |
Wendy Cheng | bb9bcf0 | 2007-06-27 17:07:08 -0400 | [diff] [blame] | 1493 | u64 no_addr; |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 1494 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
Steven Whitehouse | 044b941 | 2010-11-03 20:01:07 +0000 | [diff] [blame] | 1495 | struct gfs2_glock *gl; |
| 1496 | struct gfs2_inode *ip; |
| 1497 | int error; |
| 1498 | int found = 0; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1499 | struct gfs2_bitmap *bi; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1500 | |
Steven Whitehouse | 044b941 | 2010-11-03 20:01:07 +0000 | [diff] [blame] | 1501 | while (goal < rgd->rd_data) { |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 1502 | down_write(&sdp->sd_log_flush_lock); |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1503 | block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, &bi); |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 1504 | up_write(&sdp->sd_log_flush_lock); |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 1505 | if (block == BFITNOENT) |
Bob Peterson | 24c7387 | 2007-07-12 16:58:50 -0500 | [diff] [blame] | 1506 | break; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1507 | |
| 1508 | block = gfs2_bi2rgd_blk(bi, block); |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 1509 | /* rgblk_search can return a block < goal, so we need to |
| 1510 | keep it marching forward. */ |
| 1511 | no_addr = block + rgd->rd_data0; |
Bob Peterson | 44ad37d | 2011-03-17 16:19:58 -0400 | [diff] [blame] | 1512 | goal = max(block + 1, goal + 1); |
Bob Peterson | 6760bdc | 2007-07-24 14:09:32 -0500 | [diff] [blame] | 1513 | if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked) |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1514 | continue; |
Steven Whitehouse | 1e19a19 | 2009-07-10 21:13:38 +0100 | [diff] [blame] | 1515 | if (no_addr == skip) |
| 1516 | continue; |
Wendy Cheng | bb9bcf0 | 2007-06-27 17:07:08 -0400 | [diff] [blame] | 1517 | *last_unlinked = no_addr; |
Steven Whitehouse | 044b941 | 2010-11-03 20:01:07 +0000 | [diff] [blame] | 1518 | |
| 1519 | error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl); |
| 1520 | if (error) |
| 1521 | continue; |
| 1522 | |
| 1523 | /* If the inode is already in cache, we can ignore it here |
| 1524 | * because the existing inode disposal code will deal with |
| 1525 | * it when all refs have gone away. Accessing gl_object like |
| 1526 | * this is not safe in general. Here it is ok because we do |
| 1527 | * not dereference the pointer, and we only need an approx |
| 1528 | * answer to whether it is NULL or not. |
| 1529 | */ |
| 1530 | ip = gl->gl_object; |
| 1531 | |
| 1532 | if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0) |
| 1533 | gfs2_glock_put(gl); |
| 1534 | else |
| 1535 | found++; |
| 1536 | |
| 1537 | /* Limit reclaim to sensible number of tasks */ |
Bob Peterson | 44ad37d | 2011-03-17 16:19:58 -0400 | [diff] [blame] | 1538 | if (found > NR_CPUS) |
Steven Whitehouse | 044b941 | 2010-11-03 20:01:07 +0000 | [diff] [blame] | 1539 | return; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | rgd->rd_flags &= ~GFS2_RDF_CHECK; |
Steven Whitehouse | 044b941 | 2010-11-03 20:01:07 +0000 | [diff] [blame] | 1543 | return; |
Steven Whitehouse | c8cdf47 | 2007-06-08 10:05:33 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /** |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1547 | * gfs2_inplace_reserve - Reserve space in the filesystem |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1548 | * @ip: the inode to reserve space for |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1549 | * @requested: the number of blocks to be reserved |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1550 | * |
| 1551 | * Returns: errno |
| 1552 | */ |
| 1553 | |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1554 | int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1555 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1556 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1557 | struct gfs2_rgrpd *begin = NULL; |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1558 | struct gfs2_blkreserv *rs = ip->i_res; |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1559 | int error = 0, rg_locked, flags = LM_FLAG_TRY; |
| 1560 | u64 last_unlinked = NO_BLOCK; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1561 | int loops = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1562 | |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1563 | if (sdp->sd_args.ar_rgrplvb) |
| 1564 | flags |= GL_SKIP; |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1565 | rs->rs_requested = requested; |
| 1566 | if (gfs2_assert_warn(sdp, requested)) { |
| 1567 | error = -EINVAL; |
| 1568 | goto out; |
| 1569 | } |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1570 | if (gfs2_rs_active(rs)) { |
| 1571 | begin = rs->rs_rgd; |
| 1572 | flags = 0; /* Yoda: Do or do not. There is no try */ |
| 1573 | } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) { |
| 1574 | rs->rs_rgd = begin = ip->i_rgd; |
| 1575 | } else { |
| 1576 | rs->rs_rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1); |
| 1577 | } |
| 1578 | if (rs->rs_rgd == NULL) |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1579 | return -EBADSLT; |
| 1580 | |
| 1581 | while (loops < 3) { |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1582 | rg_locked = 0; |
| 1583 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1584 | if (gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl)) { |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1585 | rg_locked = 1; |
| 1586 | error = 0; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1587 | } else if (!loops && !gfs2_rs_active(rs) && |
| 1588 | rs->rs_rgd->rd_rs_cnt > RGRP_RSRV_MAX_CONTENDERS) { |
| 1589 | /* If the rgrp already is maxed out for contenders, |
| 1590 | we can eliminate it as a "first pass" without even |
| 1591 | requesting the rgrp glock. */ |
| 1592 | error = GLR_TRYFAILED; |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1593 | } else { |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1594 | error = gfs2_glock_nq_init(rs->rs_rgd->rd_gl, |
| 1595 | LM_ST_EXCLUSIVE, flags, |
| 1596 | &rs->rs_rgd_gh); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1597 | if (!error && sdp->sd_args.ar_rgrplvb) { |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1598 | error = update_rgrp_lvb(rs->rs_rgd); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1599 | if (error) { |
| 1600 | gfs2_glock_dq_uninit(&rs->rs_rgd_gh); |
| 1601 | return error; |
| 1602 | } |
| 1603 | } |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1604 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1605 | switch (error) { |
| 1606 | case 0: |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1607 | if (gfs2_rs_active(rs)) { |
| 1608 | if (unclaimed_blocks(rs->rs_rgd) + |
| 1609 | rs->rs_free >= rs->rs_requested) { |
| 1610 | ip->i_rgd = rs->rs_rgd; |
| 1611 | return 0; |
| 1612 | } |
| 1613 | /* We have a multi-block reservation, but the |
| 1614 | rgrp doesn't have enough free blocks to |
| 1615 | satisfy the request. Free the reservation |
| 1616 | and look for a suitable rgrp. */ |
| 1617 | gfs2_rs_deltree(rs); |
| 1618 | } |
| 1619 | if (try_rgrp_fit(rs->rs_rgd, ip)) { |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1620 | if (sdp->sd_args.ar_rgrplvb) |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1621 | gfs2_rgrp_bh_get(rs->rs_rgd); |
| 1622 | ip->i_rgd = rs->rs_rgd; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1623 | return 0; |
Steven Whitehouse | 54335b1 | 2011-09-01 13:31:59 +0100 | [diff] [blame] | 1624 | } |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1625 | if (rs->rs_rgd->rd_flags & GFS2_RDF_CHECK) { |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1626 | if (sdp->sd_args.ar_rgrplvb) |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1627 | gfs2_rgrp_bh_get(rs->rs_rgd); |
| 1628 | try_rgrp_unlink(rs->rs_rgd, &last_unlinked, |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1629 | ip->i_no_addr); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1630 | } |
Abhijith Das | 292c8c1 | 2007-11-29 14:13:54 -0600 | [diff] [blame] | 1631 | if (!rg_locked) |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1632 | gfs2_glock_dq_uninit(&rs->rs_rgd_gh); |
Steven Whitehouse | 9cabcdb | 2008-07-10 15:54:12 +0100 | [diff] [blame] | 1633 | /* fall through */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1634 | case GLR_TRYFAILED: |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1635 | rs->rs_rgd = gfs2_rgrpd_get_next(rs->rs_rgd); |
| 1636 | rs->rs_rgd = rs->rs_rgd ? : begin; /* if NULL, wrap */ |
| 1637 | if (rs->rs_rgd != begin) /* If we didn't wrap */ |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1638 | break; |
| 1639 | |
| 1640 | flags &= ~LM_FLAG_TRY; |
| 1641 | loops++; |
| 1642 | /* Check that fs hasn't grown if writing to rindex */ |
| 1643 | if (ip == GFS2_I(sdp->sd_rindex) && |
| 1644 | !sdp->sd_rindex_uptodate) { |
| 1645 | error = gfs2_ri_update(ip); |
| 1646 | if (error) |
| 1647 | goto out; |
| 1648 | } else if (loops == 2) |
| 1649 | /* Flushing the log may release space */ |
| 1650 | gfs2_log_flush(sdp, NULL); |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1651 | break; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1652 | default: |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1653 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1654 | } |
| 1655 | } |
Bob Peterson | 666d1d8 | 2012-06-13 23:03:56 -0400 | [diff] [blame] | 1656 | error = -ENOSPC; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1657 | |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1658 | out: |
| 1659 | if (error) |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 1660 | rs->rs_requested = 0; |
Steven Whitehouse | 9ae3242 | 2011-09-20 12:16:11 +0100 | [diff] [blame] | 1661 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * gfs2_inplace_release - release an inplace reservation |
| 1666 | * @ip: the inode the reservation was taken out on |
| 1667 | * |
| 1668 | * Release a reservation made by gfs2_inplace_reserve(). |
| 1669 | */ |
| 1670 | |
| 1671 | void gfs2_inplace_release(struct gfs2_inode *ip) |
| 1672 | { |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1673 | struct gfs2_blkreserv *rs = ip->i_res; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1674 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1675 | if (!rs) |
| 1676 | return; |
| 1677 | |
| 1678 | if (!rs->rs_free) |
| 1679 | gfs2_rs_deltree(rs); |
| 1680 | |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1681 | if (rs->rs_rgd_gh.gh_gl) |
| 1682 | gfs2_glock_dq_uninit(&rs->rs_rgd_gh); |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 1683 | rs->rs_requested = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /** |
| 1687 | * gfs2_get_block_type - Check a block in a RG is of given type |
| 1688 | * @rgd: the resource group holding the block |
| 1689 | * @block: the block number |
| 1690 | * |
| 1691 | * Returns: The block type (GFS2_BLKST_*) |
| 1692 | */ |
| 1693 | |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 1694 | 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] | 1695 | { |
| 1696 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1697 | u32 length, rgrp_block, buf_block; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1698 | unsigned int buf; |
| 1699 | unsigned char type; |
| 1700 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1701 | length = rgd->rd_length; |
| 1702 | rgrp_block = block - rgd->rd_data0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1703 | |
| 1704 | for (buf = 0; buf < length; buf++) { |
| 1705 | bi = rgd->rd_bits + buf; |
| 1706 | if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY) |
| 1707 | break; |
| 1708 | } |
| 1709 | |
| 1710 | gfs2_assert(rgd->rd_sbd, buf < length); |
| 1711 | buf_block = rgrp_block - bi->bi_start * GFS2_NBBY; |
| 1712 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1713 | type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1714 | bi->bi_len, buf_block); |
| 1715 | |
| 1716 | return type; |
| 1717 | } |
| 1718 | |
| 1719 | /** |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1720 | * rgblk_search - find a block in @state |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1721 | * @rgd: the resource group descriptor |
| 1722 | * @goal: the goal block within the RG (start here to search for avail block) |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1723 | * @state: GFS2_BLKST_XXX the before-allocation state to find |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1724 | * @rbi: address of the pointer to the bitmap containing the block found |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1725 | * |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1726 | * Walk rgrp's bitmap to find bits that represent a block in @state. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1727 | * |
| 1728 | * This function never fails, because we wouldn't call it unless we |
| 1729 | * know (from reservation results, etc.) that a block is available. |
| 1730 | * |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1731 | * Scope of @goal is just within rgrp, not the whole filesystem. |
| 1732 | * Scope of @returned block is just within bitmap, not the whole filesystem. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1733 | * |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1734 | * Returns: the block number found relative to the bitmap rbi |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1735 | */ |
| 1736 | |
Bob Peterson | 886b141 | 2012-04-11 13:03:52 -0400 | [diff] [blame] | 1737 | static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, unsigned char state, |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1738 | struct gfs2_bitmap **rbi) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1739 | { |
| 1740 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 1741 | const u32 length = rgd->rd_length; |
Bob Peterson | 9598d25 | 2012-04-12 08:41:43 -0400 | [diff] [blame] | 1742 | u32 biblk = BFITNOENT; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1743 | unsigned int buf, x; |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1744 | const u8 *buffer = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1745 | |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1746 | *rbi = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1747 | /* Find bitmap block that contains bits for goal block */ |
| 1748 | for (buf = 0; buf < length; buf++) { |
| 1749 | bi = rgd->rd_bits + buf; |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1750 | /* Convert scope of "goal" from rgrp-wide to within found bit block */ |
| 1751 | if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) { |
| 1752 | goal -= bi->bi_start * GFS2_NBBY; |
| 1753 | goto do_search; |
| 1754 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1755 | } |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1756 | buf = 0; |
| 1757 | goal = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1758 | |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1759 | do_search: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1760 | /* Search (up to entire) bitmap in this rgrp for allocatable block. |
| 1761 | "x <= length", instead of "x < length", because we typically start |
| 1762 | the search in the middle of a bit block, but if we can't find an |
| 1763 | allocatable block anywhere else, we want to be able wrap around and |
| 1764 | search in the first part of our first-searched bit block. */ |
| 1765 | for (x = 0; x <= length; x++) { |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1766 | bi = rgd->rd_bits + buf; |
| 1767 | |
| 1768 | if (test_bit(GBF_FULL, &bi->bi_flags) && |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1769 | (state == GFS2_BLKST_FREE)) |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1770 | goto skip; |
| 1771 | |
Bob Peterson | 5f3eae7 | 2007-08-08 16:52:09 -0500 | [diff] [blame] | 1772 | /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone |
| 1773 | bitmaps, so we must search the originals for that. */ |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 1774 | buffer = bi->bi_bh->b_data + bi->bi_offset; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1775 | WARN_ON(!buffer_uptodate(bi->bi_bh)); |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1776 | if (state != GFS2_BLKST_UNLINKED && bi->bi_clone) |
Steven Whitehouse | 110acf3 | 2008-01-29 13:30:20 +0000 | [diff] [blame] | 1777 | buffer = bi->bi_clone + bi->bi_offset; |
| 1778 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1779 | while (1) { |
| 1780 | struct gfs2_blkreserv *rs; |
| 1781 | u32 rgblk; |
| 1782 | |
| 1783 | biblk = gfs2_bitfit(buffer, bi->bi_len, goal, state); |
| 1784 | if (biblk == BFITNOENT) |
| 1785 | break; |
| 1786 | /* Check if this block is reserved() */ |
| 1787 | rgblk = gfs2_bi2rgd_blk(bi, biblk); |
| 1788 | rs = rs_find(rgd, rgblk); |
| 1789 | if (rs == NULL) |
| 1790 | break; |
| 1791 | |
| 1792 | BUG_ON(rs->rs_bi != bi); |
| 1793 | biblk = BFITNOENT; |
| 1794 | /* This should jump to the first block after the |
| 1795 | reservation. */ |
| 1796 | goal = rs->rs_biblk + rs->rs_free; |
| 1797 | if (goal >= bi->bi_len * GFS2_NBBY) |
| 1798 | break; |
| 1799 | } |
Bob Peterson | 9598d25 | 2012-04-12 08:41:43 -0400 | [diff] [blame] | 1800 | if (biblk != BFITNOENT) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1803 | if ((goal == 0) && (state == GFS2_BLKST_FREE)) |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1804 | set_bit(GBF_FULL, &bi->bi_flags); |
| 1805 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1806 | /* Try next bitmap block (wrap back to rgrp header if at end) */ |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1807 | skip: |
| 1808 | buf++; |
| 1809 | buf %= length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1810 | goal = 0; |
| 1811 | } |
| 1812 | |
Bob Peterson | 9598d25 | 2012-04-12 08:41:43 -0400 | [diff] [blame] | 1813 | if (biblk != BFITNOENT) |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1814 | *rbi = bi; |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 1815 | |
Bob Peterson | 9598d25 | 2012-04-12 08:41:43 -0400 | [diff] [blame] | 1816 | return biblk; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1817 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1818 | |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1819 | /** |
| 1820 | * gfs2_alloc_extent - allocate an extent from a given bitmap |
| 1821 | * @rgd: the resource group descriptor |
| 1822 | * @bi: the bitmap within the rgrp |
| 1823 | * @blk: the block within the bitmap |
| 1824 | * @dinode: TRUE if the first block we allocate is for a dinode |
| 1825 | * @n: The extent length |
| 1826 | * |
| 1827 | * Add the found bitmap buffer to the transaction. |
| 1828 | * Set the found bits to @new_state to change block's allocation state. |
| 1829 | * Returns: starting block number of the extent (fs scope) |
| 1830 | */ |
| 1831 | static u64 gfs2_alloc_extent(struct gfs2_rgrpd *rgd, struct gfs2_bitmap *bi, |
| 1832 | u32 blk, bool dinode, unsigned int *n) |
| 1833 | { |
| 1834 | const unsigned int elen = *n; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1835 | u32 goal, rgblk; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1836 | const u8 *buffer = NULL; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1837 | struct gfs2_blkreserv *rs; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1838 | |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1839 | *n = 0; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1840 | buffer = bi->bi_bh->b_data + bi->bi_offset; |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1841 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
Bob Peterson | 06344b9 | 2012-04-26 12:44:35 -0400 | [diff] [blame] | 1842 | gfs2_setbit(rgd, bi->bi_clone, bi, blk, |
| 1843 | dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1844 | (*n)++; |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1845 | goal = blk; |
| 1846 | while (*n < elen) { |
| 1847 | goal++; |
| 1848 | if (goal >= (bi->bi_len * GFS2_NBBY)) |
| 1849 | break; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1850 | rgblk = gfs2_bi2rgd_blk(bi, goal); |
| 1851 | rs = rs_find(rgd, rgblk); |
| 1852 | if (rs) /* Oops, we bumped into someone's reservation */ |
| 1853 | break; |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1854 | if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) != |
| 1855 | GFS2_BLKST_FREE) |
| 1856 | break; |
Bob Peterson | 06344b9 | 2012-04-26 12:44:35 -0400 | [diff] [blame] | 1857 | gfs2_setbit(rgd, bi->bi_clone, bi, goal, GFS2_BLKST_USED); |
Steven Whitehouse | 60a0b8f | 2009-05-21 12:23:12 +0100 | [diff] [blame] | 1858 | (*n)++; |
| 1859 | } |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1860 | blk = gfs2_bi2rgd_blk(bi, blk); |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 1861 | rgd->rd_last_alloc = blk + *n - 1; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 1862 | return rgd->rd_data0 + blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /** |
| 1866 | * rgblk_free - Change alloc state of given block(s) |
| 1867 | * @sdp: the filesystem |
| 1868 | * @bstart: the start of a run of blocks to free |
| 1869 | * @blen: the length of the block run (all must lie within ONE RG!) |
| 1870 | * @new_state: GFS2_BLKST_XXX the after-allocation block state |
| 1871 | * |
| 1872 | * Returns: Resource group containing the block(s) |
| 1873 | */ |
| 1874 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1875 | static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart, |
| 1876 | u32 blen, unsigned char new_state) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1877 | { |
| 1878 | struct gfs2_rgrpd *rgd; |
| 1879 | struct gfs2_bitmap *bi = NULL; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1880 | u32 length, rgrp_blk, buf_blk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1881 | unsigned int buf; |
| 1882 | |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1883 | rgd = gfs2_blk2rgrpd(sdp, bstart, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1884 | if (!rgd) { |
| 1885 | if (gfs2_consist(sdp)) |
Steven Whitehouse | 382066d | 2006-05-24 10:22:09 -0400 | [diff] [blame] | 1886 | fs_err(sdp, "block = %llu\n", (unsigned long long)bstart); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1887 | return NULL; |
| 1888 | } |
| 1889 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1890 | length = rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1891 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1892 | rgrp_blk = bstart - rgd->rd_data0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1893 | |
| 1894 | while (blen--) { |
| 1895 | for (buf = 0; buf < length; buf++) { |
| 1896 | bi = rgd->rd_bits + buf; |
| 1897 | if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY) |
| 1898 | break; |
| 1899 | } |
| 1900 | |
| 1901 | gfs2_assert(rgd->rd_sbd, buf < length); |
| 1902 | |
| 1903 | buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY; |
| 1904 | rgrp_blk++; |
| 1905 | |
| 1906 | if (!bi->bi_clone) { |
| 1907 | bi->bi_clone = kmalloc(bi->bi_bh->b_size, |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 1908 | GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1909 | memcpy(bi->bi_clone + bi->bi_offset, |
| 1910 | bi->bi_bh->b_data + bi->bi_offset, |
| 1911 | bi->bi_len); |
| 1912 | } |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1913 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
Bob Peterson | 06344b9 | 2012-04-26 12:44:35 -0400 | [diff] [blame] | 1914 | gfs2_setbit(rgd, NULL, bi, buf_blk, new_state); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | return rgd; |
| 1918 | } |
| 1919 | |
| 1920 | /** |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 1921 | * gfs2_rgrp_dump - print out an rgrp |
| 1922 | * @seq: The iterator |
| 1923 | * @gl: The glock in question |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1924 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1925 | */ |
| 1926 | |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 1927 | int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl) |
| 1928 | { |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1929 | struct gfs2_rgrpd *rgd = gl->gl_object; |
| 1930 | struct gfs2_blkreserv *trs; |
| 1931 | const struct rb_node *n; |
| 1932 | |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 1933 | if (rgd == NULL) |
| 1934 | return 0; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1935 | gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u r:%u\n", |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 1936 | (unsigned long long)rgd->rd_addr, rgd->rd_flags, |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1937 | rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes, |
| 1938 | rgd->rd_reserved); |
| 1939 | spin_lock(&rgd->rd_rsspin); |
| 1940 | for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) { |
| 1941 | trs = rb_entry(n, struct gfs2_blkreserv, rs_node); |
| 1942 | dump_rs(seq, trs); |
| 1943 | } |
| 1944 | spin_unlock(&rgd->rd_rsspin); |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 1945 | return 0; |
| 1946 | } |
| 1947 | |
Steven Whitehouse | 6050b9c | 2009-07-31 16:19:40 +0100 | [diff] [blame] | 1948 | static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd) |
| 1949 | { |
| 1950 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 1951 | 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] | 1952 | (unsigned long long)rgd->rd_addr); |
Steven Whitehouse | 6050b9c | 2009-07-31 16:19:40 +0100 | [diff] [blame] | 1953 | fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n"); |
| 1954 | gfs2_rgrp_dump(NULL, rgd->rd_gl); |
| 1955 | rgd->rd_flags |= GFS2_RDF_ERROR; |
| 1956 | } |
| 1957 | |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 1958 | /** |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1959 | * claim_reserved_blks - Claim previously reserved blocks |
| 1960 | * @ip: the inode that's claiming the reservation |
| 1961 | * @dinode: 1 if this block is a dinode block, otherwise data block |
| 1962 | * @nblocks: desired extent length |
| 1963 | * |
Steven Whitehouse | 62e252e | 2012-07-30 11:06:08 +0100 | [diff] [blame] | 1964 | * Lay claim to previously reserved blocks. |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1965 | * Returns: Starting block number of the blocks claimed. |
| 1966 | * Sets *nblocks to the actual extent length allocated. |
| 1967 | */ |
| 1968 | static u64 claim_reserved_blks(struct gfs2_inode *ip, bool dinode, |
| 1969 | unsigned int *nblocks) |
| 1970 | { |
| 1971 | struct gfs2_blkreserv *rs = ip->i_res; |
| 1972 | struct gfs2_rgrpd *rgd = rs->rs_rgd; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1973 | struct gfs2_bitmap *bi; |
| 1974 | u64 start_block = gfs2_rs_startblk(rs); |
| 1975 | const unsigned int elen = *nblocks; |
| 1976 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1977 | bi = rs->rs_bi; |
| 1978 | gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1); |
| 1979 | |
| 1980 | for (*nblocks = 0; *nblocks < elen && rs->rs_free; (*nblocks)++) { |
Steven Whitehouse | 62e252e | 2012-07-30 11:06:08 +0100 | [diff] [blame] | 1981 | if (gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset, |
| 1982 | bi->bi_len, rs->rs_biblk) != GFS2_BLKST_FREE) |
| 1983 | break; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1984 | gfs2_setbit(rgd, bi->bi_clone, bi, rs->rs_biblk, |
| 1985 | dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); |
| 1986 | rs->rs_biblk++; |
| 1987 | rs->rs_free--; |
| 1988 | |
| 1989 | BUG_ON(!rgd->rd_reserved); |
| 1990 | rgd->rd_reserved--; |
| 1991 | dinode = false; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1992 | } |
| 1993 | |
Steven Whitehouse | 62e252e | 2012-07-30 11:06:08 +0100 | [diff] [blame] | 1994 | trace_gfs2_rs(ip, rs, TRACE_RS_CLAIM); |
| 1995 | if (!rs->rs_free || *nblocks != elen) |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1996 | gfs2_rs_deltree(rs); |
Steven Whitehouse | 62e252e | 2012-07-30 11:06:08 +0100 | [diff] [blame] | 1997 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1998 | return start_block; |
| 1999 | } |
| 2000 | |
| 2001 | /** |
Bob Peterson | 6e87ed0 | 2011-11-18 10:58:32 -0500 | [diff] [blame] | 2002 | * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 2003 | * @ip: the inode to allocate the block for |
| 2004 | * @bn: Used to return the starting block number |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 2005 | * @nblocks: requested number of blocks/extent length (value/result) |
Bob Peterson | 6e87ed0 | 2011-11-18 10:58:32 -0500 | [diff] [blame] | 2006 | * @dinode: 1 if we're allocating a dinode block, else 0 |
Bob Peterson | 3c5d785 | 2011-11-14 11:17:08 -0500 | [diff] [blame] | 2007 | * @generation: the generation number of the inode |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 2008 | * |
| 2009 | * Returns: 0 or error |
| 2010 | */ |
| 2011 | |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2012 | int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks, |
Bob Peterson | 6e87ed0 | 2011-11-18 10:58:32 -0500 | [diff] [blame] | 2013 | bool dinode, u64 *generation) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2014 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 2015 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | d9ba761 | 2009-04-23 08:59:41 +0100 | [diff] [blame] | 2016 | struct buffer_head *dibh; |
Steven Whitehouse | 9a3f236 | 2010-08-23 11:49:34 +0100 | [diff] [blame] | 2017 | struct gfs2_rgrpd *rgd; |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2018 | unsigned int ndata; |
| 2019 | u32 goal, blk; /* block, within the rgrp scope */ |
Bob Peterson | 3c5d785 | 2011-11-14 11:17:08 -0500 | [diff] [blame] | 2020 | u64 block; /* block, within the file system scope */ |
Steven Whitehouse | d9ba761 | 2009-04-23 08:59:41 +0100 | [diff] [blame] | 2021 | int error; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 2022 | struct gfs2_bitmap *bi; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2023 | |
Steven Whitehouse | 9a3f236 | 2010-08-23 11:49:34 +0100 | [diff] [blame] | 2024 | /* Only happens if there is a bug in gfs2, return something distinctive |
| 2025 | * to ensure that it is noticed. |
| 2026 | */ |
Bob Peterson | 0a305e4 | 2012-06-06 11:17:59 +0100 | [diff] [blame] | 2027 | if (ip->i_res->rs_requested == 0) |
Steven Whitehouse | 9a3f236 | 2010-08-23 11:49:34 +0100 | [diff] [blame] | 2028 | return -ECANCELED; |
| 2029 | |
Steven Whitehouse | 62e252e | 2012-07-30 11:06:08 +0100 | [diff] [blame] | 2030 | /* If we have a reservation, claim blocks from it. */ |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 2031 | if (gfs2_rs_active(ip->i_res)) { |
| 2032 | BUG_ON(!ip->i_res->rs_free); |
| 2033 | rgd = ip->i_res->rs_rgd; |
| 2034 | block = claim_reserved_blks(ip, dinode, nblocks); |
Steven Whitehouse | 62e252e | 2012-07-30 11:06:08 +0100 | [diff] [blame] | 2035 | if (*nblocks) |
| 2036 | goto found_blocks; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 2037 | } |
Steven Whitehouse | 62e252e | 2012-07-30 11:06:08 +0100 | [diff] [blame] | 2038 | |
| 2039 | rgd = ip->i_rgd; |
| 2040 | |
| 2041 | if (!dinode && rgrp_contains_block(rgd, ip->i_goal)) |
| 2042 | goal = ip->i_goal - rgd->rd_data0; |
| 2043 | else |
| 2044 | goal = rgd->rd_last_alloc; |
| 2045 | |
| 2046 | blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, &bi); |
| 2047 | |
| 2048 | /* Since all blocks are reserved in advance, this shouldn't happen */ |
| 2049 | if (blk == BFITNOENT) { |
| 2050 | printk(KERN_WARNING "BFITNOENT, nblocks=%u\n", *nblocks); |
| 2051 | printk(KERN_WARNING "FULL=%d\n", |
| 2052 | test_bit(GBF_FULL, &rgd->rd_bits->bi_flags)); |
| 2053 | goto rgrp_error; |
| 2054 | } |
| 2055 | |
| 2056 | block = gfs2_alloc_extent(rgd, bi, blk, dinode, nblocks); |
| 2057 | found_blocks: |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2058 | ndata = *nblocks; |
| 2059 | if (dinode) |
| 2060 | ndata--; |
Bob Peterson | b3e47ca | 2011-11-21 11:47:08 -0500 | [diff] [blame] | 2061 | |
Bob Peterson | 3c5d785 | 2011-11-14 11:17:08 -0500 | [diff] [blame] | 2062 | if (!dinode) { |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2063 | ip->i_goal = block + ndata - 1; |
Bob Peterson | 3c5d785 | 2011-11-14 11:17:08 -0500 | [diff] [blame] | 2064 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 2065 | if (error == 0) { |
| 2066 | struct gfs2_dinode *di = |
| 2067 | (struct gfs2_dinode *)dibh->b_data; |
| 2068 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 2069 | di->di_goal_meta = di->di_goal_data = |
| 2070 | cpu_to_be64(ip->i_goal); |
| 2071 | brelse(dibh); |
| 2072 | } |
Steven Whitehouse | d9ba761 | 2009-04-23 08:59:41 +0100 | [diff] [blame] | 2073 | } |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 2074 | if (rgd->rd_free < *nblocks) { |
| 2075 | printk(KERN_WARNING "nblocks=%u\n", *nblocks); |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 2076 | goto rgrp_error; |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 2077 | } |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 2078 | |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2079 | rgd->rd_free -= *nblocks; |
Bob Peterson | 3c5d785 | 2011-11-14 11:17:08 -0500 | [diff] [blame] | 2080 | if (dinode) { |
| 2081 | rgd->rd_dinodes++; |
| 2082 | *generation = rgd->rd_igeneration++; |
| 2083 | if (*generation == 0) |
| 2084 | *generation = rgd->rd_igeneration++; |
| 2085 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2086 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 2087 | 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] | 2088 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 2089 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2090 | |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2091 | gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0); |
Bob Peterson | 3c5d785 | 2011-11-14 11:17:08 -0500 | [diff] [blame] | 2092 | if (dinode) |
| 2093 | gfs2_trans_add_unrevoke(sdp, block, 1); |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2094 | |
| 2095 | /* |
| 2096 | * This needs reviewing to see why we cannot do the quota change |
| 2097 | * at this point in the dinode case. |
| 2098 | */ |
| 2099 | if (ndata) |
| 2100 | gfs2_quota_change(ip, ndata, ip->i_inode.i_uid, |
Bob Peterson | 3c5d785 | 2011-11-14 11:17:08 -0500 | [diff] [blame] | 2101 | ip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2102 | |
Steven Whitehouse | 6a8099e | 2011-11-22 12:18:51 +0000 | [diff] [blame] | 2103 | rgd->rd_free_clone -= *nblocks; |
Bob Peterson | 41db1ab | 2012-05-09 12:11:35 -0400 | [diff] [blame] | 2104 | trace_gfs2_block_alloc(ip, rgd, block, *nblocks, |
Bob Peterson | 6e87ed0 | 2011-11-18 10:58:32 -0500 | [diff] [blame] | 2105 | dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED); |
Steven Whitehouse | 6050b9c | 2009-07-31 16:19:40 +0100 | [diff] [blame] | 2106 | *bn = block; |
| 2107 | return 0; |
| 2108 | |
| 2109 | rgrp_error: |
| 2110 | gfs2_rgrp_error(rgd); |
| 2111 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /** |
Eric Sandeen | 46fcb2e | 2011-06-23 10:39:34 -0500 | [diff] [blame] | 2115 | * __gfs2_free_blocks - free a contiguous run of block(s) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2116 | * @ip: the inode these blocks are being freed from |
| 2117 | * @bstart: first block of a run of contiguous blocks |
| 2118 | * @blen: the length of the block run |
Eric Sandeen | 46fcb2e | 2011-06-23 10:39:34 -0500 | [diff] [blame] | 2119 | * @meta: 1 if the blocks represent metadata |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2120 | * |
| 2121 | */ |
| 2122 | |
Eric Sandeen | 46fcb2e | 2011-06-23 10:39:34 -0500 | [diff] [blame] | 2123 | void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2124 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 2125 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2126 | struct gfs2_rgrpd *rgd; |
| 2127 | |
| 2128 | rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE); |
| 2129 | if (!rgd) |
| 2130 | return; |
Bob Peterson | 41db1ab | 2012-05-09 12:11:35 -0400 | [diff] [blame] | 2131 | trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE); |
Steven Whitehouse | cfc8b54 | 2008-11-04 10:25:13 +0000 | [diff] [blame] | 2132 | rgd->rd_free += blen; |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 2133 | rgd->rd_flags &= ~GFS2_RGF_TRIMMED; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 2134 | 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] | 2135 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 2136 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2137 | |
Steven Whitehouse | 6d3117b | 2011-05-21 14:05:58 +0100 | [diff] [blame] | 2138 | /* Directories keep their data in the metadata address space */ |
Eric Sandeen | 46fcb2e | 2011-06-23 10:39:34 -0500 | [diff] [blame] | 2139 | if (meta || ip->i_depth) |
Steven Whitehouse | 6d3117b | 2011-05-21 14:05:58 +0100 | [diff] [blame] | 2140 | gfs2_meta_wipe(ip, bstart, blen); |
Bob Peterson | 4c16c36 | 2011-02-23 16:11:33 -0500 | [diff] [blame] | 2141 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2142 | |
Bob Peterson | 4c16c36 | 2011-02-23 16:11:33 -0500 | [diff] [blame] | 2143 | /** |
Bob Peterson | 4c16c36 | 2011-02-23 16:11:33 -0500 | [diff] [blame] | 2144 | * gfs2_free_meta - free a contiguous run of data block(s) |
| 2145 | * @ip: the inode these blocks are being freed from |
| 2146 | * @bstart: first block of a run of contiguous blocks |
| 2147 | * @blen: the length of the block run |
| 2148 | * |
| 2149 | */ |
| 2150 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 2151 | void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2152 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 2153 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2154 | |
Eric Sandeen | 46fcb2e | 2011-06-23 10:39:34 -0500 | [diff] [blame] | 2155 | __gfs2_free_blocks(ip, bstart, blen, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2156 | gfs2_statfs_change(sdp, 0, +blen, 0); |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 2157 | 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] | 2158 | } |
| 2159 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 2160 | void gfs2_unlink_di(struct inode *inode) |
| 2161 | { |
| 2162 | struct gfs2_inode *ip = GFS2_I(inode); |
| 2163 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 2164 | struct gfs2_rgrpd *rgd; |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 2165 | u64 blkno = ip->i_no_addr; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 2166 | |
| 2167 | rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED); |
| 2168 | if (!rgd) |
| 2169 | return; |
Bob Peterson | 41db1ab | 2012-05-09 12:11:35 -0400 | [diff] [blame] | 2170 | trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 2171 | 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] | 2172 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 2173 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); |
| 2174 | update_rgrp_lvb_unlinked(rgd, 1); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 2175 | } |
| 2176 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 2177 | static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2178 | { |
| 2179 | struct gfs2_sbd *sdp = rgd->rd_sbd; |
| 2180 | struct gfs2_rgrpd *tmp_rgd; |
| 2181 | |
| 2182 | tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE); |
| 2183 | if (!tmp_rgd) |
| 2184 | return; |
| 2185 | gfs2_assert_withdraw(sdp, rgd == tmp_rgd); |
| 2186 | |
Steven Whitehouse | 73f7494 | 2008-11-04 10:32:57 +0000 | [diff] [blame] | 2187 | if (!rgd->rd_dinodes) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2188 | gfs2_consist_rgrpd(rgd); |
Steven Whitehouse | 73f7494 | 2008-11-04 10:32:57 +0000 | [diff] [blame] | 2189 | rgd->rd_dinodes--; |
Steven Whitehouse | cfc8b54 | 2008-11-04 10:25:13 +0000 | [diff] [blame] | 2190 | rgd->rd_free++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2191 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 2192 | 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] | 2193 | gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 2194 | gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data); |
| 2195 | update_rgrp_lvb_unlinked(rgd, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2196 | |
| 2197 | gfs2_statfs_change(sdp, 0, +1, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2200 | |
| 2201 | void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip) |
| 2202 | { |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 2203 | gfs2_free_uninit_di(rgd, ip->i_no_addr); |
Bob Peterson | 41db1ab | 2012-05-09 12:11:35 -0400 | [diff] [blame] | 2204 | trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE); |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 2205 | 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] | 2206 | gfs2_meta_wipe(ip, ip->i_no_addr, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /** |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 2210 | * gfs2_check_blk_type - Check the type of a block |
| 2211 | * @sdp: The superblock |
| 2212 | * @no_addr: The block number to check |
| 2213 | * @type: The block type we are looking for |
| 2214 | * |
| 2215 | * Returns: 0 if the block type matches the expected type |
| 2216 | * -ESTALE if it doesn't match |
| 2217 | * or -ve errno if something went wrong while checking |
| 2218 | */ |
| 2219 | |
| 2220 | int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type) |
| 2221 | { |
| 2222 | struct gfs2_rgrpd *rgd; |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 2223 | struct gfs2_holder rgd_gh; |
Bob Peterson | 58884c4 | 2012-03-05 10:19:35 -0500 | [diff] [blame] | 2224 | int error = -EINVAL; |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 2225 | |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 2226 | rgd = gfs2_blk2rgrpd(sdp, no_addr, 1); |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 2227 | if (!rgd) |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 2228 | goto fail; |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 2229 | |
| 2230 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh); |
| 2231 | if (error) |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 2232 | goto fail; |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 2233 | |
| 2234 | if (gfs2_get_block_type(rgd, no_addr) != type) |
| 2235 | error = -ESTALE; |
| 2236 | |
| 2237 | gfs2_glock_dq_uninit(&rgd_gh); |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 2238 | fail: |
| 2239 | return error; |
| 2240 | } |
| 2241 | |
| 2242 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2243 | * gfs2_rlist_add - add a RG to a list of RGs |
Steven Whitehouse | 70b0c36 | 2011-09-02 16:08:09 +0100 | [diff] [blame] | 2244 | * @ip: the inode |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2245 | * @rlist: the list of resource groups |
| 2246 | * @block: the block |
| 2247 | * |
| 2248 | * Figure out what RG a block belongs to and add that RG to the list |
| 2249 | * |
| 2250 | * FIXME: Don't use NOFAIL |
| 2251 | * |
| 2252 | */ |
| 2253 | |
Steven Whitehouse | 70b0c36 | 2011-09-02 16:08:09 +0100 | [diff] [blame] | 2254 | void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 2255 | u64 block) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2256 | { |
Steven Whitehouse | 70b0c36 | 2011-09-02 16:08:09 +0100 | [diff] [blame] | 2257 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2258 | struct gfs2_rgrpd *rgd; |
| 2259 | struct gfs2_rgrpd **tmp; |
| 2260 | unsigned int new_space; |
| 2261 | unsigned int x; |
| 2262 | |
| 2263 | if (gfs2_assert_warn(sdp, !rlist->rl_ghs)) |
| 2264 | return; |
| 2265 | |
Steven Whitehouse | 70b0c36 | 2011-09-02 16:08:09 +0100 | [diff] [blame] | 2266 | if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block)) |
| 2267 | rgd = ip->i_rgd; |
| 2268 | else |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 2269 | rgd = gfs2_blk2rgrpd(sdp, block, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2270 | if (!rgd) { |
Steven Whitehouse | 70b0c36 | 2011-09-02 16:08:09 +0100 | [diff] [blame] | 2271 | fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2272 | return; |
| 2273 | } |
Steven Whitehouse | 70b0c36 | 2011-09-02 16:08:09 +0100 | [diff] [blame] | 2274 | ip->i_rgd = rgd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2275 | |
| 2276 | for (x = 0; x < rlist->rl_rgrps; x++) |
| 2277 | if (rlist->rl_rgd[x] == rgd) |
| 2278 | return; |
| 2279 | |
| 2280 | if (rlist->rl_rgrps == rlist->rl_space) { |
| 2281 | new_space = rlist->rl_space + 10; |
| 2282 | |
| 2283 | tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *), |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 2284 | GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2285 | |
| 2286 | if (rlist->rl_rgd) { |
| 2287 | memcpy(tmp, rlist->rl_rgd, |
| 2288 | rlist->rl_space * sizeof(struct gfs2_rgrpd *)); |
| 2289 | kfree(rlist->rl_rgd); |
| 2290 | } |
| 2291 | |
| 2292 | rlist->rl_space = new_space; |
| 2293 | rlist->rl_rgd = tmp; |
| 2294 | } |
| 2295 | |
| 2296 | rlist->rl_rgd[rlist->rl_rgrps++] = rgd; |
| 2297 | } |
| 2298 | |
| 2299 | /** |
| 2300 | * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate |
| 2301 | * and initialize an array of glock holders for them |
| 2302 | * @rlist: the list of resource groups |
| 2303 | * @state: the lock state to acquire the RG lock in |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2304 | * |
| 2305 | * FIXME: Don't use NOFAIL |
| 2306 | * |
| 2307 | */ |
| 2308 | |
Bob Peterson | fe6c991 | 2008-01-28 11:13:02 -0600 | [diff] [blame] | 2309 | void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2310 | { |
| 2311 | unsigned int x; |
| 2312 | |
| 2313 | rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder), |
Steven Whitehouse | dd894be | 2006-07-27 14:29:00 -0400 | [diff] [blame] | 2314 | GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2315 | for (x = 0; x < rlist->rl_rgrps; x++) |
| 2316 | gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, |
Bob Peterson | fe6c991 | 2008-01-28 11:13:02 -0600 | [diff] [blame] | 2317 | state, 0, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2318 | &rlist->rl_ghs[x]); |
| 2319 | } |
| 2320 | |
| 2321 | /** |
| 2322 | * gfs2_rlist_free - free a resource group list |
| 2323 | * @list: the list of resource groups |
| 2324 | * |
| 2325 | */ |
| 2326 | |
| 2327 | void gfs2_rlist_free(struct gfs2_rgrp_list *rlist) |
| 2328 | { |
| 2329 | unsigned int x; |
| 2330 | |
| 2331 | kfree(rlist->rl_rgd); |
| 2332 | |
| 2333 | if (rlist->rl_ghs) { |
| 2334 | for (x = 0; x < rlist->rl_rgrps; x++) |
| 2335 | gfs2_holder_uninit(&rlist->rl_ghs[x]); |
| 2336 | kfree(rlist->rl_ghs); |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 2337 | rlist->rl_ghs = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |