blob: a2b43bb834993c6099d1ee9725254e9191c1b5cb [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonfe6c9912008-01-28 11:13:02 -06003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
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 Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
Steven Whitehousef42faf42006-01-30 18:34:10 +000014#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Bob Peterson1f466a42008-03-10 18:17:47 -050016#include <linux/prefetch.h>
Steven Whitehousef15ab562009-02-09 09:25:01 +000017#include <linux/blkdev.h>
Bob Peterson7c9ca622011-08-31 09:53:19 +010018#include <linux/rbtree.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "glock.h"
23#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#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 Whitehouse5c676f62006-02-27 17:23:27 -050030#include "util.h"
Benjamin Marzinski172e0452007-03-23 14:51:56 -060031#include "log.h"
Steven Whitehousec8cdf472007-06-08 10:05:33 +010032#include "inode.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010033#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034
Steven Whitehouse2c1e52a2006-09-05 15:41:57 -040035#define BFITNOENT ((u32)~0)
Bob Peterson6760bdc2007-07-24 14:09:32 -050036#define NO_BLOCK ((u64)~0)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040037
Bob Peterson8e2e0042012-07-19 08:12:40 -040038#define RSRV_CONTENTION_FACTOR 4
39#define RGRP_RSRV_MAX_CONTENDERS 2
40
Bob Peterson1f466a42008-03-10 18:17:47 -050041#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 Whitehouse88c8ab1f2006-05-18 13:52:39 -040051/*
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 Whitehousefeaa7bb2006-06-14 15:32:57 -040054 * 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 Whitehouse88c8ab1f2006-05-18 13:52:39 -040060 */
61
62static const char valid_change[16] = {
63 /* current */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040064 /* n */ 0, 1, 1, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040065 /* e */ 1, 0, 0, 0,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040066 /* w */ 0, 0, 0, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040067 1, 0, 0, 0
68};
69
Steven Whitehousec8cdf472007-06-08 10:05:33 +010070static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
Steven Whitehouse6a8099e2011-11-22 12:18:51 +000071 unsigned char old_state,
Bob Petersonb3e47ca2011-11-21 11:47:08 -050072 struct gfs2_bitmap **rbi);
Steven Whitehousec8cdf472007-06-08 10:05:33 +010073
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040074/**
75 * gfs2_setbit - Set a bit in the bitmaps
Bob Peterson29c578f2012-04-11 13:01:07 -040076 * @rgd: the resource group descriptor
Bob Peterson29c578f2012-04-11 13:01:07 -040077 * @buf2: the clone buffer that holds the bitmaps
78 * @bi: the bitmap structure
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040079 * @block: the block to set
80 * @new_state: the new state of the block
81 *
82 */
83
Bob Peterson06344b92012-04-26 12:44:35 -040084static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf2,
85 struct gfs2_bitmap *bi, u32 block,
86 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040087{
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000088 unsigned char *byte1, *byte2, *end, cur_state;
Bob Peterson95c8e172011-03-22 10:49:12 -040089 unsigned int buflen = bi->bi_len;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000090 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040091
Bob Peterson06344b92012-04-26 12:44:35 -040092 byte1 = bi->bi_bh->b_data + bi->bi_offset + (block / GFS2_NBBY);
93 end = bi->bi_bh->b_data + bi->bi_offset + buflen;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040094
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000095 BUG_ON(byte1 >= end);
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040096
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000097 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040098
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000099 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
Bob Peterson95c8e172011-03-22 10:49:12 -0400100 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 Whitehouse88c8ab1f2006-05-18 13:52:39 -0400110 gfs2_consist_rgrpd(rgd);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000111 return;
112 }
113 *byte1 ^= (cur_state ^ new_state) << bit;
114
115 if (buf2) {
Bob Peterson29c578f2012-04-11 13:01:07 -0400116 byte2 = buf2 + bi->bi_offset + (block / GFS2_NBBY);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000117 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
118 *byte2 ^= (cur_state ^ new_state) << bit;
119 }
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400120}
121
122/**
123 * gfs2_testbit - test a bit in the bitmaps
Bob Peterson886b1412012-04-11 13:03:52 -0400124 * @rgd: the resource group descriptor
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400125 * @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 Whitehouseb45e41d2008-02-06 10:11:15 +0000131static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
132 const unsigned char *buffer,
133 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400134{
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000135 const unsigned char *byte, *end;
136 unsigned char cur_state;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400137 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 Whitehouse223b2b82009-02-17 14:13:35 +0000151 * 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
168static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
169{
170 u64 tmp;
171 static const u64 search[] = {
Hannes Eder075ac442009-02-21 02:11:42 +0100172 [0] = 0xffffffffffffffffULL,
173 [1] = 0xaaaaaaaaaaaaaaaaULL,
174 [2] = 0x5555555555555555ULL,
175 [3] = 0x0000000000000000ULL,
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000176 };
177 tmp = le64_to_cpu(*ptr) ^ search[state];
178 tmp &= (tmp >> 1);
179 tmp &= mask;
180 return tmp;
181}
182
183/**
Bob Peterson8e2e0042012-07-19 08:12:40 -0400184 * 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 */
193static 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 */
209static 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 Whitehouse88c8ab1f2006-05-18 13:52:39 -0400235 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
236 * a block in a given allocation state.
Bob Peterson886b1412012-04-11 13:03:52 -0400237 * @buf: the buffer that holds the bitmaps
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000238 * @len: the length (in bytes) of the buffer
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400239 * @goal: start search at this block's bit-pair (within @buffer)
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000240 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400241 *
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 Whitehouse223b2b82009-02-17 14:13:35 +0000244 * 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 Gadiyarfd589a82009-07-16 17:13:03 +0200249 * always ok to read a complete multiple of 64 bits at the end
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000250 * of the block in case the end is no aligned to a natural boundary.
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400251 *
252 * Return: the block number (bitmap buffer scope) that was found
253 */
254
Hannes Eder02ab1722009-02-21 02:12:05 +0100255static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
256 u32 goal, u8 state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400257{
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000258 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 Eder075ac442009-02-21 02:11:42 +0100262 u64 mask = 0x5555555555555555ULL;
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000263 u32 bit;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400264
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000265 BUG_ON(state > 3);
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400266
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000267 /* 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 Eder075ac442009-02-21 02:11:42 +0100272 tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000273 ptr++;
Bob Peterson1f466a42008-03-10 18:17:47 -0500274 }
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000275 /* 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 Whitehoused8bd5042009-04-23 08:54:02 +0100282 bit = __ffs64(tmp);
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000283 bit /= 2; /* two bits per entry in the bitmap */
284 return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400285}
286
287/**
288 * gfs2_bitcount - count the number of bits in a certain state
Bob Peterson886b1412012-04-11 13:03:52 -0400289 * @rgd: the resource group descriptor
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400290 * @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 Whitehouse110acf32008-01-29 13:30:20 +0000297static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
298 unsigned int buflen, u8 state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400299{
Steven Whitehouse110acf32008-01-29 13:30:20 +0000300 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 Whitehousecd915492006-09-04 12:49:07 -0400305 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400306
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 Teiglandb3b94fa2006-01-16 16:50:04 +0000321/**
322 * gfs2_rgrp_verify - Verify that a resource group is consistent
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 * @rgd: the rgrp
324 *
325 */
326
327void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
328{
329 struct gfs2_sbd *sdp = rgd->rd_sbd;
330 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100331 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -0400332 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 int buf, x;
334
Steven Whitehousecd915492006-09-04 12:49:07 -0400335 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336
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 Whitehousecfc8b542008-11-04 10:25:13 +0000347 if (count[0] != rgd->rd_free) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 if (gfs2_consist_rgrpd(rgd))
349 fs_err(sdp, "free data mismatch: %u != %u\n",
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000350 count[0], rgd->rd_free);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 return;
352 }
353
Steven Whitehouse73f74942008-11-04 10:32:57 +0000354 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
Benjamin Marzinski6b946172009-07-10 18:13:26 -0500355 if (count[1] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 if (gfs2_consist_rgrpd(rgd))
357 fs_err(sdp, "used data mismatch: %u != %u\n",
358 count[1], tmp);
359 return;
360 }
361
Benjamin Marzinski6b946172009-07-10 18:13:26 -0500362 if (count[2] + count[3] != rgd->rd_dinodes) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363 if (gfs2_consist_rgrpd(rgd))
364 fs_err(sdp, "used metadata mismatch: %u != %u\n",
Benjamin Marzinski6b946172009-07-10 18:13:26 -0500365 count[2] + count[3], rgd->rd_dinodes);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 return;
367 }
368}
369
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100370static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100372 u64 first = rgd->rd_data0;
373 u64 last = first + rgd->rd_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400374 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375}
376
377/**
378 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
379 * @sdp: The GFS2 superblock
Bob Peterson886b1412012-04-11 13:03:52 -0400380 * @blk: The data block number
381 * @exact: True if this needs to be an exact match
David Teiglandb3b94fa2006-01-16 16:50:04 +0000382 *
383 * Returns: The resource group, or NULL if not found
384 */
385
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000386struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387{
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000388 struct rb_node *n, *next;
Steven Whitehousef75bbfb2011-09-08 10:21:13 +0100389 struct gfs2_rgrpd *cur;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390
391 spin_lock(&sdp->sd_rindex_spin);
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000392 n = sdp->sd_rindex_tree.rb_node;
393 while (n) {
394 cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
395 next = NULL;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100396 if (blk < cur->rd_addr)
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000397 next = n->rb_left;
Steven Whitehousef75bbfb2011-09-08 10:21:13 +0100398 else if (blk >= cur->rd_data0 + cur->rd_data)
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000399 next = n->rb_right;
400 if (next == NULL) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000401 spin_unlock(&sdp->sd_rindex_spin);
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000402 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 Peterson7c9ca622011-08-31 09:53:19 +0100408 return cur;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000409 }
Steven Whitehouse66fc0612012-02-08 12:58:32 +0000410 n = next;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 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
424struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
425{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100426 const struct rb_node *n;
427 struct gfs2_rgrpd *rgd;
428
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100429 spin_lock(&sdp->sd_rindex_spin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100430 n = rb_first(&sdp->sd_rindex_tree);
431 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100432 spin_unlock(&sdp->sd_rindex_spin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100433
434 return rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435}
436
437/**
438 * gfs2_rgrpd_get_next - get the next RG
Bob Peterson886b1412012-04-11 13:03:52 -0400439 * @rgd: the resource group descriptor
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 *
441 * Returns: The next rgrp
442 */
443
444struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
445{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100446 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 Teiglandb3b94fa2006-01-16 16:50:04 +0000456 return NULL;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100457 }
458 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
459 spin_unlock(&sdp->sd_rindex_spin);
460 return rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461}
462
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100463void 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 Peterson0a305e42012-06-06 11:17:59 +0100474/**
475 * gfs2_rs_alloc - make sure we have a reservation assigned to the inode
476 * @ip: the inode for this reservation
477 */
478int gfs2_rs_alloc(struct gfs2_inode *ip)
479{
480 int error = 0;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400481 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 Peterson0a305e42012-06-06 11:17:59 +0100489
490 down_write(&ip->i_rw_mutex);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400491 if (ip->i_res)
492 kmem_cache_free(gfs2_rsrv_cachep, res);
493 else
494 ip->i_res = res;
Bob Peterson0a305e42012-06-06 11:17:59 +0100495 up_write(&ip->i_rw_mutex);
496 return error;
497}
498
Bob Peterson8e2e0042012-07-19 08:12:40 -0400499static 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 Peterson0a305e42012-06-06 11:17:59 +0100506/**
Bob Peterson8e2e0042012-07-19 08:12:40 -0400507 * __rs_deltree - remove a multi-block reservation from the rgd tree
508 * @rs: The reservation to remove
509 *
510 */
511static 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);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400542 Can't: rs->rs_rgd = NULL;*/
543 rs->rs_bi = NULL;
544 rs->rs_biblk = 0;
545}
546
547/**
548 * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
549 * @rs: The reservation to remove
550 *
551 */
552void gfs2_rs_deltree(struct gfs2_blkreserv *rs)
553{
554 struct gfs2_rgrpd *rgd;
555
556 if (!gfs2_rs_active(rs))
557 return;
558
559 rgd = rs->rs_rgd;
560 spin_lock(&rgd->rd_rsspin);
561 __rs_deltree(rs);
562 spin_unlock(&rgd->rd_rsspin);
563}
564
565/**
566 * gfs2_rs_delete - delete a multi-block reservation
Bob Peterson0a305e42012-06-06 11:17:59 +0100567 * @ip: The inode for this reservation
568 *
569 */
570void gfs2_rs_delete(struct gfs2_inode *ip)
571{
572 down_write(&ip->i_rw_mutex);
573 if (ip->i_res) {
Bob Peterson8e2e0042012-07-19 08:12:40 -0400574 gfs2_rs_deltree(ip->i_res);
575 trace_gfs2_rs(ip, ip->i_res, TRACE_RS_DELETE);
576 BUG_ON(ip->i_res->rs_free);
Bob Peterson0a305e42012-06-06 11:17:59 +0100577 kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
578 ip->i_res = NULL;
579 }
580 up_write(&ip->i_rw_mutex);
581}
582
Bob Peterson8e2e0042012-07-19 08:12:40 -0400583/**
584 * return_all_reservations - return all reserved blocks back to the rgrp.
585 * @rgd: the rgrp that needs its space back
586 *
587 * We previously reserved a bunch of blocks for allocation. Now we need to
588 * give them back. This leave the reservation structures in tact, but removes
589 * all of their corresponding "no-fly zones".
590 */
591static void return_all_reservations(struct gfs2_rgrpd *rgd)
592{
593 struct rb_node *n;
594 struct gfs2_blkreserv *rs;
595
596 spin_lock(&rgd->rd_rsspin);
597 while ((n = rb_first(&rgd->rd_rstree))) {
598 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
599 __rs_deltree(rs);
600 }
601 spin_unlock(&rgd->rd_rsspin);
602}
603
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100604void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100606 struct rb_node *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 struct gfs2_rgrpd *rgd;
608 struct gfs2_glock *gl;
609
Bob Peterson7c9ca622011-08-31 09:53:19 +0100610 while ((n = rb_first(&sdp->sd_rindex_tree))) {
611 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 gl = rgd->rd_gl;
613
Bob Peterson7c9ca622011-08-31 09:53:19 +0100614 rb_erase(n, &sdp->sd_rindex_tree);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615
616 if (gl) {
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100617 spin_lock(&gl->gl_spin);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500618 gl->gl_object = NULL;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100619 spin_unlock(&gl->gl_spin);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100620 gfs2_glock_add_to_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 gfs2_glock_put(gl);
622 }
623
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100624 gfs2_free_clones(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625 kfree(rgd->rd_bits);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400626 return_all_reservations(rgd);
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600627 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 }
629}
630
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100631static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
632{
633 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
634 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
635 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
636 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
637 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
638}
639
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640/**
641 * gfs2_compute_bitstructs - Compute the bitmap sizes
642 * @rgd: The resource group descriptor
643 *
644 * Calculates bitmap descriptors, one for each block that contains bitmap data
645 *
646 * Returns: errno
647 */
648
649static int compute_bitstructs(struct gfs2_rgrpd *rgd)
650{
651 struct gfs2_sbd *sdp = rgd->rd_sbd;
652 struct gfs2_bitmap *bi;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100653 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
Steven Whitehousecd915492006-09-04 12:49:07 -0400654 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000655 int x;
656
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400657 if (!length)
658 return -EINVAL;
659
Steven Whitehousedd894be2006-07-27 14:29:00 -0400660 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 if (!rgd->rd_bits)
662 return -ENOMEM;
663
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100664 bytes_left = rgd->rd_bitbytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665
666 for (x = 0; x < length; x++) {
667 bi = rgd->rd_bits + x;
668
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +0100669 bi->bi_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 /* small rgrp; bitmap stored completely in header block */
671 if (length == 1) {
672 bytes = bytes_left;
673 bi->bi_offset = sizeof(struct gfs2_rgrp);
674 bi->bi_start = 0;
675 bi->bi_len = bytes;
676 /* header block */
677 } else if (x == 0) {
678 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
679 bi->bi_offset = sizeof(struct gfs2_rgrp);
680 bi->bi_start = 0;
681 bi->bi_len = bytes;
682 /* last block */
683 } else if (x + 1 == length) {
684 bytes = bytes_left;
685 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100686 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 bi->bi_len = bytes;
688 /* other blocks */
689 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500690 bytes = sdp->sd_sb.sb_bsize -
691 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100693 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 bi->bi_len = bytes;
695 }
696
697 bytes_left -= bytes;
698 }
699
700 if (bytes_left) {
701 gfs2_consist_rgrpd(rgd);
702 return -EIO;
703 }
704 bi = rgd->rd_bits + (length - 1);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100705 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 if (gfs2_consist_rgrpd(rgd)) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100707 gfs2_rindex_print(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708 fs_err(sdp, "start=%u len=%u offset=%u\n",
709 bi->bi_start, bi->bi_len, bi->bi_offset);
710 }
711 return -EIO;
712 }
713
714 return 0;
715}
716
717/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500718 * gfs2_ri_total - Total up the file system space, according to the rindex.
Bob Peterson886b1412012-04-11 13:03:52 -0400719 * @sdp: the filesystem
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500720 *
721 */
722u64 gfs2_ri_total(struct gfs2_sbd *sdp)
723{
724 u64 total_data = 0;
725 struct inode *inode = sdp->sd_rindex;
726 struct gfs2_inode *ip = GFS2_I(inode);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500727 char buf[sizeof(struct gfs2_rindex)];
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500728 int error, rgrps;
729
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500730 for (rgrps = 0;; rgrps++) {
731 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
732
Bob Petersonbcd72782010-12-07 13:58:56 -0500733 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500734 break;
Andrew Price43066292012-04-16 16:40:55 +0100735 error = gfs2_internal_read(ip, buf, &pos,
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500736 sizeof(struct gfs2_rindex));
737 if (error != sizeof(struct gfs2_rindex))
738 break;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100739 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500740 }
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500741 return total_data;
742}
743
Bob Peterson6aad1c32012-03-05 09:20:59 -0500744static int rgd_insert(struct gfs2_rgrpd *rgd)
Bob Peterson7c9ca622011-08-31 09:53:19 +0100745{
746 struct gfs2_sbd *sdp = rgd->rd_sbd;
747 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
748
749 /* Figure out where to put new node */
750 while (*newn) {
751 struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
752 rd_node);
753
754 parent = *newn;
755 if (rgd->rd_addr < cur->rd_addr)
756 newn = &((*newn)->rb_left);
757 else if (rgd->rd_addr > cur->rd_addr)
758 newn = &((*newn)->rb_right);
759 else
Bob Peterson6aad1c32012-03-05 09:20:59 -0500760 return -EEXIST;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100761 }
762
763 rb_link_node(&rgd->rd_node, parent, newn);
764 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
Bob Peterson6aad1c32012-03-05 09:20:59 -0500765 sdp->sd_rgrps++;
766 return 0;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100767}
768
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500769/**
Robert Peterson6c532672007-05-10 16:54:38 -0500770 * read_rindex_entry - Pull in a new resource index entry from the disk
Andrew Price43066292012-04-16 16:40:55 +0100771 * @ip: Pointer to the rindex inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 *
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100773 * Returns: 0 on success, > 0 on EOF, error code otherwise
Robert Peterson6c532672007-05-10 16:54:38 -0500774 */
775
Andrew Price43066292012-04-16 16:40:55 +0100776static int read_rindex_entry(struct gfs2_inode *ip)
Robert Peterson6c532672007-05-10 16:54:38 -0500777{
778 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
779 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100780 struct gfs2_rindex buf;
Robert Peterson6c532672007-05-10 16:54:38 -0500781 int error;
782 struct gfs2_rgrpd *rgd;
783
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100784 if (pos >= i_size_read(&ip->i_inode))
785 return 1;
786
Andrew Price43066292012-04-16 16:40:55 +0100787 error = gfs2_internal_read(ip, (char *)&buf, &pos,
Robert Peterson6c532672007-05-10 16:54:38 -0500788 sizeof(struct gfs2_rindex));
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100789
790 if (error != sizeof(struct gfs2_rindex))
791 return (error == 0) ? 1 : error;
Robert Peterson6c532672007-05-10 16:54:38 -0500792
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600793 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
Robert Peterson6c532672007-05-10 16:54:38 -0500794 error = -ENOMEM;
795 if (!rgd)
796 return error;
797
Robert Peterson6c532672007-05-10 16:54:38 -0500798 rgd->rd_sbd = sdp;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100799 rgd->rd_addr = be64_to_cpu(buf.ri_addr);
800 rgd->rd_length = be32_to_cpu(buf.ri_length);
801 rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
802 rgd->rd_data = be32_to_cpu(buf.ri_data);
803 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400804 spin_lock_init(&rgd->rd_rsspin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100805
Robert Peterson6c532672007-05-10 16:54:38 -0500806 error = compute_bitstructs(rgd);
807 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100808 goto fail;
Robert Peterson6c532672007-05-10 16:54:38 -0500809
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100810 error = gfs2_glock_get(sdp, rgd->rd_addr,
Robert Peterson6c532672007-05-10 16:54:38 -0500811 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
812 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100813 goto fail;
Robert Peterson6c532672007-05-10 16:54:38 -0500814
815 rgd->rd_gl->gl_object = rgd;
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500816 rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lvb;
Bob Petersoncf45b752008-01-31 10:31:39 -0600817 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100818 if (rgd->rd_data > sdp->sd_max_rg_data)
819 sdp->sd_max_rg_data = rgd->rd_data;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100820 spin_lock(&sdp->sd_rindex_spin);
Bob Peterson6aad1c32012-03-05 09:20:59 -0500821 error = rgd_insert(rgd);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100822 spin_unlock(&sdp->sd_rindex_spin);
Bob Peterson6aad1c32012-03-05 09:20:59 -0500823 if (!error)
824 return 0;
825
826 error = 0; /* someone else read in the rgrp; free it and ignore it */
Bob Petersonc1ac5392012-03-22 08:58:30 -0400827 gfs2_glock_put(rgd->rd_gl);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100828
829fail:
830 kfree(rgd->rd_bits);
831 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
Robert Peterson6c532672007-05-10 16:54:38 -0500832 return error;
833}
834
835/**
836 * gfs2_ri_update - Pull in a new resource index from the disk
837 * @ip: pointer to the rindex inode
838 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 * Returns: 0 on successful update, error code otherwise
840 */
841
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100842static int gfs2_ri_update(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400844 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 int error;
846
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100847 do {
Andrew Price43066292012-04-16 16:40:55 +0100848 error = read_rindex_entry(ip);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100849 } while (error == 0);
850
851 if (error < 0)
852 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853
Bob Petersoncf45b752008-01-31 10:31:39 -0600854 sdp->sd_rindex_uptodate = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 return 0;
Robert Peterson6c532672007-05-10 16:54:38 -0500856}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857
Robert Peterson6c532672007-05-10 16:54:38 -0500858/**
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100859 * gfs2_rindex_update - Update the rindex if required
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860 * @sdp: The GFS2 superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 *
862 * We grab a lock on the rindex inode to make sure that it doesn't
863 * change whilst we are performing an operation. We keep this lock
864 * for quite long periods of time compared to other locks. This
865 * doesn't matter, since it is shared and it is very, very rarely
866 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
867 *
868 * This makes sure that we're using the latest copy of the resource index
869 * special file, which might have been updated if someone expanded the
870 * filesystem (via gfs2_grow utility), which adds new resource groups.
871 *
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100872 * Returns: 0 on succeess, error code otherwise
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 */
874
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100875int gfs2_rindex_update(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400877 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878 struct gfs2_glock *gl = ip->i_gl;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100879 struct gfs2_holder ri_gh;
880 int error = 0;
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000881 int unlock_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882
883 /* Read new copy from disk if we don't have the latest */
Bob Petersoncf45b752008-01-31 10:31:39 -0600884 if (!sdp->sd_rindex_uptodate) {
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000885 if (!gfs2_glock_is_locked_by_me(gl)) {
886 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
887 if (error)
Bob Peterson6aad1c32012-03-05 09:20:59 -0500888 return error;
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000889 unlock_required = 1;
890 }
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100891 if (!sdp->sd_rindex_uptodate)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892 error = gfs2_ri_update(ip);
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000893 if (unlock_required)
894 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 }
896
897 return error;
898}
899
Bob Peterson42d52e32008-01-28 18:38:07 -0600900static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100901{
902 const struct gfs2_rgrp *str = buf;
Bob Peterson42d52e32008-01-28 18:38:07 -0600903 u32 rg_flags;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100904
Bob Peterson42d52e32008-01-28 18:38:07 -0600905 rg_flags = be32_to_cpu(str->rg_flags);
Steven Whitehouse09010972009-05-20 10:48:47 +0100906 rg_flags &= ~GFS2_RDF_MASK;
Steven Whitehouse1ce97e52009-05-21 15:18:19 +0100907 rgd->rd_flags &= GFS2_RDF_MASK;
908 rgd->rd_flags |= rg_flags;
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000909 rgd->rd_free = be32_to_cpu(str->rg_free);
Steven Whitehouse73f74942008-11-04 10:32:57 +0000910 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
Steven Whitehoused8b71f72008-11-04 10:19:03 +0000911 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100912}
913
Bob Peterson42d52e32008-01-28 18:38:07 -0600914static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100915{
916 struct gfs2_rgrp *str = buf;
917
Steven Whitehouse09010972009-05-20 10:48:47 +0100918 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000919 str->rg_free = cpu_to_be32(rgd->rd_free);
Steven Whitehouse73f74942008-11-04 10:32:57 +0000920 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100921 str->__pad = cpu_to_be32(0);
Steven Whitehoused8b71f72008-11-04 10:19:03 +0000922 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100923 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
924}
925
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500926static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
927{
928 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
929 struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
930
931 if (rgl->rl_flags != str->rg_flags || rgl->rl_free != str->rg_free ||
932 rgl->rl_dinodes != str->rg_dinodes ||
933 rgl->rl_igeneration != str->rg_igeneration)
934 return 0;
935 return 1;
936}
937
938static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
939{
940 const struct gfs2_rgrp *str = buf;
941
942 rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
943 rgl->rl_flags = str->rg_flags;
944 rgl->rl_free = str->rg_free;
945 rgl->rl_dinodes = str->rg_dinodes;
946 rgl->rl_igeneration = str->rg_igeneration;
947 rgl->__pad = 0UL;
948}
949
950static void update_rgrp_lvb_unlinked(struct gfs2_rgrpd *rgd, u32 change)
951{
952 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
953 u32 unlinked = be32_to_cpu(rgl->rl_unlinked) + change;
954 rgl->rl_unlinked = cpu_to_be32(unlinked);
955}
956
957static u32 count_unlinked(struct gfs2_rgrpd *rgd)
958{
959 struct gfs2_bitmap *bi;
960 const u32 length = rgd->rd_length;
961 const u8 *buffer = NULL;
962 u32 i, goal, count = 0;
963
964 for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
965 goal = 0;
966 buffer = bi->bi_bh->b_data + bi->bi_offset;
967 WARN_ON(!buffer_uptodate(bi->bi_bh));
968 while (goal < bi->bi_len * GFS2_NBBY) {
969 goal = gfs2_bitfit(buffer, bi->bi_len, goal,
970 GFS2_BLKST_UNLINKED);
971 if (goal == BFITNOENT)
972 break;
973 count++;
974 goal++;
975 }
976 }
977
978 return count;
979}
980
981
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982/**
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500983 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
984 * @rgd: the struct gfs2_rgrpd describing the RG to read in
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 *
986 * Read in all of a Resource Group's header and bitmap blocks.
987 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
988 *
989 * Returns: errno
990 */
991
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500992int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993{
994 struct gfs2_sbd *sdp = rgd->rd_sbd;
995 struct gfs2_glock *gl = rgd->rd_gl;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100996 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 struct gfs2_bitmap *bi;
998 unsigned int x, y;
999 int error;
1000
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001001 if (rgd->rd_bits[0].bi_bh != NULL)
1002 return 0;
1003
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 for (x = 0; x < length; x++) {
1005 bi = rgd->rd_bits + x;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001006 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007 if (error)
1008 goto fail;
1009 }
1010
1011 for (y = length; y--;) {
1012 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001013 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001014 if (error)
1015 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001016 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 GFS2_METATYPE_RG)) {
1018 error = -EIO;
1019 goto fail;
1020 }
1021 }
1022
Bob Petersoncf45b752008-01-31 10:31:39 -06001023 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001024 for (x = 0; x < length; x++)
1025 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
Bob Peterson42d52e32008-01-28 18:38:07 -06001026 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
Steven Whitehouse1ce97e52009-05-21 15:18:19 +01001027 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
Bob Peterson7c9ca622011-08-31 09:53:19 +01001028 rgd->rd_free_clone = rgd->rd_free;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 }
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001030 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
1031 rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
1032 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
1033 rgd->rd_bits[0].bi_bh->b_data);
1034 }
1035 else if (sdp->sd_args.ar_rgrplvb) {
1036 if (!gfs2_rgrp_lvb_valid(rgd)){
1037 gfs2_consist_rgrpd(rgd);
1038 error = -EIO;
1039 goto fail;
1040 }
1041 if (rgd->rd_rgl->rl_unlinked == 0)
1042 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1043 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 return 0;
1045
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001046fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047 while (x--) {
1048 bi = rgd->rd_bits + x;
1049 brelse(bi->bi_bh);
1050 bi->bi_bh = NULL;
1051 gfs2_assert_warn(sdp, !bi->bi_clone);
1052 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053
1054 return error;
1055}
1056
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001057int update_rgrp_lvb(struct gfs2_rgrpd *rgd)
1058{
1059 u32 rl_flags;
1060
1061 if (rgd->rd_flags & GFS2_RDF_UPTODATE)
1062 return 0;
1063
1064 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
1065 return gfs2_rgrp_bh_get(rgd);
1066
1067 rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
1068 rl_flags &= ~GFS2_RDF_MASK;
1069 rgd->rd_flags &= GFS2_RDF_MASK;
1070 rgd->rd_flags |= (rl_flags | GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
1071 if (rgd->rd_rgl->rl_unlinked == 0)
1072 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1073 rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
1074 rgd->rd_free_clone = rgd->rd_free;
1075 rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
1076 rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
1077 return 0;
1078}
1079
1080int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
1081{
1082 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
1083 struct gfs2_sbd *sdp = rgd->rd_sbd;
1084
1085 if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb)
1086 return 0;
1087 return gfs2_rgrp_bh_get((struct gfs2_rgrpd *)gh->gh_gl->gl_object);
1088}
1089
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090/**
Bob Peterson7c9ca622011-08-31 09:53:19 +01001091 * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get()
Bob Peterson886b1412012-04-11 13:03:52 -04001092 * @gh: The glock holder for the resource group
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 *
1094 */
1095
Bob Peterson7c9ca622011-08-31 09:53:19 +01001096void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097{
Bob Peterson7c9ca622011-08-31 09:53:19 +01001098 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001099 int x, length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 for (x = 0; x < length; x++) {
1102 struct gfs2_bitmap *bi = rgd->rd_bits + x;
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001103 if (bi->bi_bh) {
1104 brelse(bi->bi_bh);
1105 bi->bi_bh = NULL;
1106 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107 }
1108
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109}
1110
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001111int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
Bob Peterson7c9ca622011-08-31 09:53:19 +01001112 struct buffer_head *bh,
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001113 const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
Steven Whitehousef15ab562009-02-09 09:25:01 +00001114{
1115 struct super_block *sb = sdp->sd_vfs;
1116 struct block_device *bdev = sb->s_bdev;
1117 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
Martin K. Petersene1defc42009-05-22 17:17:49 -04001118 bdev_logical_block_size(sb->s_bdev);
Steven Whitehousef15ab562009-02-09 09:25:01 +00001119 u64 blk;
Steven Whitehouse64d576b2009-02-12 13:31:58 +00001120 sector_t start = 0;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001121 sector_t nr_sects = 0;
1122 int rv;
1123 unsigned int x;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001124 u32 trimmed = 0;
1125 u8 diff;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001126
1127 for (x = 0; x < bi->bi_len; x++) {
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001128 const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
1129 clone += bi->bi_offset;
1130 clone += x;
1131 if (bh) {
1132 const u8 *orig = bh->b_data + bi->bi_offset + x;
1133 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
1134 } else {
1135 diff = ~(*clone | (*clone >> 1));
1136 }
Steven Whitehousef15ab562009-02-09 09:25:01 +00001137 diff &= 0x55;
1138 if (diff == 0)
1139 continue;
1140 blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
1141 blk *= sects_per_blk; /* convert to sectors */
1142 while(diff) {
1143 if (diff & 1) {
1144 if (nr_sects == 0)
1145 goto start_new_extent;
1146 if ((start + nr_sects) != blk) {
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001147 if (nr_sects >= minlen) {
1148 rv = blkdev_issue_discard(bdev,
1149 start, nr_sects,
1150 GFP_NOFS, 0);
1151 if (rv)
1152 goto fail;
1153 trimmed += nr_sects;
1154 }
Steven Whitehousef15ab562009-02-09 09:25:01 +00001155 nr_sects = 0;
1156start_new_extent:
1157 start = blk;
1158 }
1159 nr_sects += sects_per_blk;
1160 }
1161 diff >>= 2;
1162 blk += sects_per_blk;
1163 }
1164 }
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001165 if (nr_sects >= minlen) {
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02001166 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
Steven Whitehousef15ab562009-02-09 09:25:01 +00001167 if (rv)
1168 goto fail;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001169 trimmed += nr_sects;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001170 }
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001171 if (ptrimmed)
1172 *ptrimmed = trimmed;
1173 return 0;
1174
Steven Whitehousef15ab562009-02-09 09:25:01 +00001175fail:
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001176 if (sdp->sd_args.ar_discard)
1177 fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
Steven Whitehousef15ab562009-02-09 09:25:01 +00001178 sdp->sd_args.ar_discard = 0;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001179 return -EIO;
1180}
1181
1182/**
1183 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
1184 * @filp: Any file on the filesystem
1185 * @argp: Pointer to the arguments (also used to pass result)
1186 *
1187 * Returns: 0 on success, otherwise error code
1188 */
1189
1190int gfs2_fitrim(struct file *filp, void __user *argp)
1191{
1192 struct inode *inode = filp->f_dentry->d_inode;
1193 struct gfs2_sbd *sdp = GFS2_SB(inode);
1194 struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev);
1195 struct buffer_head *bh;
1196 struct gfs2_rgrpd *rgd;
1197 struct gfs2_rgrpd *rgd_end;
1198 struct gfs2_holder gh;
1199 struct fstrim_range r;
1200 int ret = 0;
1201 u64 amt;
1202 u64 trimmed = 0;
1203 unsigned int x;
1204
1205 if (!capable(CAP_SYS_ADMIN))
1206 return -EPERM;
1207
1208 if (!blk_queue_discard(q))
1209 return -EOPNOTSUPP;
1210
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001211 if (argp == NULL) {
1212 r.start = 0;
1213 r.len = ULLONG_MAX;
1214 r.minlen = 0;
1215 } else if (copy_from_user(&r, argp, sizeof(r)))
1216 return -EFAULT;
1217
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001218 ret = gfs2_rindex_update(sdp);
1219 if (ret)
1220 return ret;
1221
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001222 rgd = gfs2_blk2rgrpd(sdp, r.start, 0);
1223 rgd_end = gfs2_blk2rgrpd(sdp, r.start + r.len, 0);
1224
1225 while (1) {
1226
1227 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1228 if (ret)
1229 goto out;
1230
1231 if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
1232 /* Trim each bitmap in the rgrp */
1233 for (x = 0; x < rgd->rd_length; x++) {
1234 struct gfs2_bitmap *bi = rgd->rd_bits + x;
1235 ret = gfs2_rgrp_send_discards(sdp, rgd->rd_data0, NULL, bi, r.minlen, &amt);
1236 if (ret) {
1237 gfs2_glock_dq_uninit(&gh);
1238 goto out;
1239 }
1240 trimmed += amt;
1241 }
1242
1243 /* Mark rgrp as having been trimmed */
1244 ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
1245 if (ret == 0) {
1246 bh = rgd->rd_bits[0].bi_bh;
1247 rgd->rd_flags |= GFS2_RGF_TRIMMED;
1248 gfs2_trans_add_bh(rgd->rd_gl, bh, 1);
1249 gfs2_rgrp_out(rgd, bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001250 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, bh->b_data);
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001251 gfs2_trans_end(sdp);
1252 }
1253 }
1254 gfs2_glock_dq_uninit(&gh);
1255
1256 if (rgd == rgd_end)
1257 break;
1258
1259 rgd = gfs2_rgrpd_get_next(rgd);
1260 }
1261
1262out:
1263 r.len = trimmed << 9;
1264 if (argp && copy_to_user(argp, &r, sizeof(r)))
1265 return -EFAULT;
1266
1267 return ret;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001268}
1269
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270/**
Bob Peterson8e2e0042012-07-19 08:12:40 -04001271 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
1272 * @bi: the bitmap with the blocks
1273 * @ip: the inode structure
1274 * @biblk: the 32-bit block number relative to the start of the bitmap
1275 * @amount: the number of blocks to reserve
1276 *
1277 * Returns: NULL - reservation was already taken, so not inserted
1278 * pointer to the inserted reservation
1279 */
1280static struct gfs2_blkreserv *rs_insert(struct gfs2_bitmap *bi,
1281 struct gfs2_inode *ip, u32 biblk,
1282 int amount)
1283{
1284 struct rb_node **newn, *parent = NULL;
1285 int rc;
1286 struct gfs2_blkreserv *rs = ip->i_res;
1287 struct gfs2_rgrpd *rgd = rs->rs_rgd;
1288 u64 fsblock = gfs2_bi2rgd_blk(bi, biblk) + rgd->rd_data0;
1289
1290 spin_lock(&rgd->rd_rsspin);
1291 newn = &rgd->rd_rstree.rb_node;
1292 BUG_ON(!ip->i_res);
1293 BUG_ON(gfs2_rs_active(rs));
1294 /* Figure out where to put new node */
1295 /*BUG_ON(!gfs2_glock_is_locked_by_me(rgd->rd_gl));*/
1296 while (*newn) {
1297 struct gfs2_blkreserv *cur =
1298 rb_entry(*newn, struct gfs2_blkreserv, rs_node);
1299
1300 parent = *newn;
1301 rc = rs_cmp(fsblock, amount, cur);
1302 if (rc > 0)
1303 newn = &((*newn)->rb_right);
1304 else if (rc < 0)
1305 newn = &((*newn)->rb_left);
1306 else {
1307 spin_unlock(&rgd->rd_rsspin);
1308 return NULL; /* reservation already in use */
1309 }
1310 }
1311
1312 /* Do our reservation work */
1313 rs = ip->i_res;
1314 rs->rs_free = amount;
1315 rs->rs_biblk = biblk;
1316 rs->rs_bi = bi;
1317 rb_link_node(&rs->rs_node, parent, newn);
1318 rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
1319
1320 /* Do our inode accounting for the reservation */
1321 /*BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));*/
1322
1323 /* Do our rgrp accounting for the reservation */
1324 rgd->rd_reserved += amount; /* blocks reserved */
1325 rgd->rd_rs_cnt++; /* number of in-tree reservations */
1326 spin_unlock(&rgd->rd_rsspin);
1327 trace_gfs2_rs(ip, rs, TRACE_RS_INSERT);
1328 return rs;
1329}
1330
1331/**
1332 * unclaimed_blocks - return number of blocks that aren't spoken for
1333 */
1334static u32 unclaimed_blocks(struct gfs2_rgrpd *rgd)
1335{
1336 return rgd->rd_free_clone - rgd->rd_reserved;
1337}
1338
1339/**
1340 * rg_mblk_search - find a group of multiple free blocks
1341 * @rgd: the resource group descriptor
1342 * @rs: the block reservation
1343 * @ip: pointer to the inode for which we're reserving blocks
1344 *
1345 * This is very similar to rgblk_search, except we're looking for whole
1346 * 64-bit words that represent a chunk of 32 free blocks. I'm only focusing
1347 * on aligned dwords for speed's sake.
1348 *
1349 * Returns: 0 if successful or BFITNOENT if there isn't enough free space
1350 */
1351
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001352static int rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip, unsigned requested)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001353{
1354 struct gfs2_bitmap *bi = rgd->rd_bits;
1355 const u32 length = rgd->rd_length;
1356 u32 blk;
1357 unsigned int buf, x, search_bytes;
1358 u8 *buffer = NULL;
1359 u8 *ptr, *end, *nonzero;
1360 u32 goal, rsv_bytes;
1361 struct gfs2_blkreserv *rs;
1362 u32 best_rs_bytes, unclaimed;
1363 int best_rs_blocks;
1364
1365 /* Find bitmap block that contains bits for goal block */
1366 if (rgrp_contains_block(rgd, ip->i_goal))
1367 goal = ip->i_goal - rgd->rd_data0;
1368 else
1369 goal = rgd->rd_last_alloc;
1370 for (buf = 0; buf < length; buf++) {
1371 bi = rgd->rd_bits + buf;
1372 /* Convert scope of "goal" from rgrp-wide to within
1373 found bit block */
1374 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1375 goal -= bi->bi_start * GFS2_NBBY;
1376 goto do_search;
1377 }
1378 }
1379 buf = 0;
1380 goal = 0;
1381
1382do_search:
1383 best_rs_blocks = max_t(int, atomic_read(&ip->i_res->rs_sizehint),
1384 (RGRP_RSRV_MINBLKS * rgd->rd_length));
1385 best_rs_bytes = (best_rs_blocks *
1386 (1 + (RSRV_CONTENTION_FACTOR * rgd->rd_rs_cnt))) /
1387 GFS2_NBBY; /* 1 + is for our not-yet-created reservation */
1388 best_rs_bytes = ALIGN(best_rs_bytes, sizeof(u64));
1389 unclaimed = unclaimed_blocks(rgd);
1390 if (best_rs_bytes * GFS2_NBBY > unclaimed)
1391 best_rs_bytes = unclaimed >> GFS2_BIT_SIZE;
1392
1393 for (x = 0; x <= length; x++) {
1394 bi = rgd->rd_bits + buf;
1395
1396 if (test_bit(GBF_FULL, &bi->bi_flags))
1397 goto skip;
1398
1399 WARN_ON(!buffer_uptodate(bi->bi_bh));
1400 if (bi->bi_clone)
1401 buffer = bi->bi_clone + bi->bi_offset;
1402 else
1403 buffer = bi->bi_bh->b_data + bi->bi_offset;
1404
1405 /* We have to keep the reservations aligned on u64 boundaries
1406 otherwise we could get situations where a byte can't be
1407 used because it's after a reservation, but a free bit still
1408 is within the reservation's area. */
1409 ptr = buffer + ALIGN(goal >> GFS2_BIT_SIZE, sizeof(u64));
1410 end = (buffer + bi->bi_len);
1411 while (ptr < end) {
1412 rsv_bytes = 0;
1413 if ((ptr + best_rs_bytes) <= end)
1414 search_bytes = best_rs_bytes;
1415 else
1416 search_bytes = end - ptr;
1417 BUG_ON(!search_bytes);
1418 nonzero = memchr_inv(ptr, 0, search_bytes);
1419 /* If the lot is all zeroes, reserve the whole size. If
1420 there's enough zeroes to satisfy the request, use
1421 what we can. If there's not enough, keep looking. */
1422 if (nonzero == NULL)
1423 rsv_bytes = search_bytes;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001424 else if ((nonzero - ptr) * GFS2_NBBY >= requested)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001425 rsv_bytes = (nonzero - ptr);
1426
1427 if (rsv_bytes) {
1428 blk = ((ptr - buffer) * GFS2_NBBY);
1429 BUG_ON(blk >= bi->bi_len * GFS2_NBBY);
1430 rs = rs_insert(bi, ip, blk,
1431 rsv_bytes * GFS2_NBBY);
1432 if (IS_ERR(rs))
1433 return PTR_ERR(rs);
1434 if (rs)
1435 return 0;
1436 }
1437 ptr += ALIGN(search_bytes, sizeof(u64));
1438 }
1439skip:
1440 /* Try next bitmap block (wrap back to rgrp header
1441 if at end) */
1442 buf++;
1443 buf %= length;
1444 goal = 0;
1445 }
1446
1447 return BFITNOENT;
1448}
1449
1450/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001451 * try_rgrp_fit - See if a given reservation will fit in a given RG
1452 * @rgd: the RG data
Steven Whitehouse54335b12011-09-01 13:31:59 +01001453 * @ip: the inode
David Teiglandb3b94fa2006-01-16 16:50:04 +00001454 *
1455 * If there's room for the requested blocks to be allocated from the RG:
Bob Peterson8e2e0042012-07-19 08:12:40 -04001456 * This will try to get a multi-block reservation first, and if that doesn't
1457 * fit, it will take what it can.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001458 *
1459 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
1460 */
1461
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001462static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
1463 unsigned requested)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001464{
Steven Whitehouse09010972009-05-20 10:48:47 +01001465 if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
Steven Whitehousea43a4902007-04-02 10:48:17 +01001466 return 0;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001467 /* Look for a multi-block reservation. */
1468 if (unclaimed_blocks(rgd) >= RGRP_RSRV_MINBLKS &&
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001469 rg_mblk_search(rgd, ip, requested) != BFITNOENT)
Bob Peterson7c9ca622011-08-31 09:53:19 +01001470 return 1;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001471 if (unclaimed_blocks(rgd) >= requested)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001472 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001473
Bob Peterson8e2e0042012-07-19 08:12:40 -04001474 return 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001475}
1476
David Teiglandb3b94fa2006-01-16 16:50:04 +00001477/**
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001478 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
1479 * @rgd: The rgrp
Bob Peterson886b1412012-04-11 13:03:52 -04001480 * @last_unlinked: block address of the last dinode we unlinked
1481 * @skip: block address we should explicitly not unlink
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001482 *
Bob Peterson1a0eae82010-04-14 11:58:16 -04001483 * Returns: 0 if no error
1484 * The inode, if one has been found, in inode.
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001485 */
1486
Steven Whitehouse044b9412010-11-03 20:01:07 +00001487static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001488{
Bob Peterson6760bdc2007-07-24 14:09:32 -05001489 u32 goal = 0, block;
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001490 u64 no_addr;
Bob Peterson5f3eae72007-08-08 16:52:09 -05001491 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehouse044b9412010-11-03 20:01:07 +00001492 struct gfs2_glock *gl;
1493 struct gfs2_inode *ip;
1494 int error;
1495 int found = 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001496 struct gfs2_bitmap *bi;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001497
Steven Whitehouse044b9412010-11-03 20:01:07 +00001498 while (goal < rgd->rd_data) {
Bob Peterson5f3eae72007-08-08 16:52:09 -05001499 down_write(&sdp->sd_log_flush_lock);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001500 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, &bi);
Bob Peterson5f3eae72007-08-08 16:52:09 -05001501 up_write(&sdp->sd_log_flush_lock);
Bob Peterson6760bdc2007-07-24 14:09:32 -05001502 if (block == BFITNOENT)
Bob Peterson24c73872007-07-12 16:58:50 -05001503 break;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001504
1505 block = gfs2_bi2rgd_blk(bi, block);
Bob Peterson6760bdc2007-07-24 14:09:32 -05001506 /* rgblk_search can return a block < goal, so we need to
1507 keep it marching forward. */
1508 no_addr = block + rgd->rd_data0;
Bob Peterson44ad37d2011-03-17 16:19:58 -04001509 goal = max(block + 1, goal + 1);
Bob Peterson6760bdc2007-07-24 14:09:32 -05001510 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001511 continue;
Steven Whitehouse1e19a192009-07-10 21:13:38 +01001512 if (no_addr == skip)
1513 continue;
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001514 *last_unlinked = no_addr;
Steven Whitehouse044b9412010-11-03 20:01:07 +00001515
1516 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
1517 if (error)
1518 continue;
1519
1520 /* If the inode is already in cache, we can ignore it here
1521 * because the existing inode disposal code will deal with
1522 * it when all refs have gone away. Accessing gl_object like
1523 * this is not safe in general. Here it is ok because we do
1524 * not dereference the pointer, and we only need an approx
1525 * answer to whether it is NULL or not.
1526 */
1527 ip = gl->gl_object;
1528
1529 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
1530 gfs2_glock_put(gl);
1531 else
1532 found++;
1533
1534 /* Limit reclaim to sensible number of tasks */
Bob Peterson44ad37d2011-03-17 16:19:58 -04001535 if (found > NR_CPUS)
Steven Whitehouse044b9412010-11-03 20:01:07 +00001536 return;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001537 }
1538
1539 rgd->rd_flags &= ~GFS2_RDF_CHECK;
Steven Whitehouse044b9412010-11-03 20:01:07 +00001540 return;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001541}
1542
1543/**
Bob Peterson666d1d82012-06-13 23:03:56 -04001544 * gfs2_inplace_reserve - Reserve space in the filesystem
David Teiglandb3b94fa2006-01-16 16:50:04 +00001545 * @ip: the inode to reserve space for
Bob Peterson666d1d82012-06-13 23:03:56 -04001546 * @requested: the number of blocks to be reserved
David Teiglandb3b94fa2006-01-16 16:50:04 +00001547 *
1548 * Returns: errno
1549 */
1550
Bob Peterson666d1d82012-06-13 23:03:56 -04001551int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001553 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001554 struct gfs2_rgrpd *begin = NULL;
Bob Peterson564e12b2011-11-21 13:36:17 -05001555 struct gfs2_blkreserv *rs = ip->i_res;
Bob Peterson666d1d82012-06-13 23:03:56 -04001556 int error = 0, rg_locked, flags = LM_FLAG_TRY;
1557 u64 last_unlinked = NO_BLOCK;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001558 int loops = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001559
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001560 if (sdp->sd_args.ar_rgrplvb)
1561 flags |= GL_SKIP;
Bob Peterson666d1d82012-06-13 23:03:56 -04001562 if (gfs2_assert_warn(sdp, requested)) {
1563 error = -EINVAL;
1564 goto out;
1565 }
Bob Peterson8e2e0042012-07-19 08:12:40 -04001566 if (gfs2_rs_active(rs)) {
1567 begin = rs->rs_rgd;
1568 flags = 0; /* Yoda: Do or do not. There is no try */
1569 } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) {
1570 rs->rs_rgd = begin = ip->i_rgd;
1571 } else {
1572 rs->rs_rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
1573 }
1574 if (rs->rs_rgd == NULL)
Bob Peterson7c9ca622011-08-31 09:53:19 +01001575 return -EBADSLT;
1576
1577 while (loops < 3) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001578 rg_locked = 0;
1579
Bob Peterson8e2e0042012-07-19 08:12:40 -04001580 if (gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl)) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001581 rg_locked = 1;
1582 error = 0;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001583 } else if (!loops && !gfs2_rs_active(rs) &&
1584 rs->rs_rgd->rd_rs_cnt > RGRP_RSRV_MAX_CONTENDERS) {
1585 /* If the rgrp already is maxed out for contenders,
1586 we can eliminate it as a "first pass" without even
1587 requesting the rgrp glock. */
1588 error = GLR_TRYFAILED;
Abhijith Das292c8c12007-11-29 14:13:54 -06001589 } else {
Bob Peterson8e2e0042012-07-19 08:12:40 -04001590 error = gfs2_glock_nq_init(rs->rs_rgd->rd_gl,
1591 LM_ST_EXCLUSIVE, flags,
1592 &rs->rs_rgd_gh);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001593 if (!error && sdp->sd_args.ar_rgrplvb) {
Bob Peterson8e2e0042012-07-19 08:12:40 -04001594 error = update_rgrp_lvb(rs->rs_rgd);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001595 if (error) {
1596 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1597 return error;
1598 }
1599 }
Abhijith Das292c8c12007-11-29 14:13:54 -06001600 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001601 switch (error) {
1602 case 0:
Bob Peterson8e2e0042012-07-19 08:12:40 -04001603 if (gfs2_rs_active(rs)) {
1604 if (unclaimed_blocks(rs->rs_rgd) +
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001605 rs->rs_free >= requested) {
Bob Peterson8e2e0042012-07-19 08:12:40 -04001606 ip->i_rgd = rs->rs_rgd;
1607 return 0;
1608 }
1609 /* We have a multi-block reservation, but the
1610 rgrp doesn't have enough free blocks to
1611 satisfy the request. Free the reservation
1612 and look for a suitable rgrp. */
1613 gfs2_rs_deltree(rs);
1614 }
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001615 if (try_rgrp_fit(rs->rs_rgd, ip, requested)) {
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001616 if (sdp->sd_args.ar_rgrplvb)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001617 gfs2_rgrp_bh_get(rs->rs_rgd);
1618 ip->i_rgd = rs->rs_rgd;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001619 return 0;
Steven Whitehouse54335b12011-09-01 13:31:59 +01001620 }
Bob Peterson8e2e0042012-07-19 08:12:40 -04001621 if (rs->rs_rgd->rd_flags & GFS2_RDF_CHECK) {
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001622 if (sdp->sd_args.ar_rgrplvb)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001623 gfs2_rgrp_bh_get(rs->rs_rgd);
1624 try_rgrp_unlink(rs->rs_rgd, &last_unlinked,
Bob Peterson666d1d82012-06-13 23:03:56 -04001625 ip->i_no_addr);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001626 }
Abhijith Das292c8c12007-11-29 14:13:54 -06001627 if (!rg_locked)
Bob Peterson564e12b2011-11-21 13:36:17 -05001628 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
Steven Whitehouse9cabcdb2008-07-10 15:54:12 +01001629 /* fall through */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001630 case GLR_TRYFAILED:
Bob Peterson8e2e0042012-07-19 08:12:40 -04001631 rs->rs_rgd = gfs2_rgrpd_get_next(rs->rs_rgd);
1632 rs->rs_rgd = rs->rs_rgd ? : begin; /* if NULL, wrap */
1633 if (rs->rs_rgd != begin) /* If we didn't wrap */
Bob Peterson666d1d82012-06-13 23:03:56 -04001634 break;
1635
1636 flags &= ~LM_FLAG_TRY;
1637 loops++;
1638 /* Check that fs hasn't grown if writing to rindex */
1639 if (ip == GFS2_I(sdp->sd_rindex) &&
1640 !sdp->sd_rindex_uptodate) {
1641 error = gfs2_ri_update(ip);
1642 if (error)
1643 goto out;
1644 } else if (loops == 2)
1645 /* Flushing the log may release space */
1646 gfs2_log_flush(sdp, NULL);
Bob Peterson7c9ca622011-08-31 09:53:19 +01001647 break;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001648 default:
Bob Peterson666d1d82012-06-13 23:03:56 -04001649 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001650 }
1651 }
Bob Peterson666d1d82012-06-13 23:03:56 -04001652 error = -ENOSPC;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001653
Bob Peterson564e12b2011-11-21 13:36:17 -05001654out:
Steven Whitehouse9ae32422011-09-20 12:16:11 +01001655 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001656}
1657
1658/**
1659 * gfs2_inplace_release - release an inplace reservation
1660 * @ip: the inode the reservation was taken out on
1661 *
1662 * Release a reservation made by gfs2_inplace_reserve().
1663 */
1664
1665void gfs2_inplace_release(struct gfs2_inode *ip)
1666{
Bob Peterson564e12b2011-11-21 13:36:17 -05001667 struct gfs2_blkreserv *rs = ip->i_res;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001668
Bob Peterson564e12b2011-11-21 13:36:17 -05001669 if (rs->rs_rgd_gh.gh_gl)
1670 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001671}
1672
1673/**
1674 * gfs2_get_block_type - Check a block in a RG is of given type
1675 * @rgd: the resource group holding the block
1676 * @block: the block number
1677 *
1678 * Returns: The block type (GFS2_BLKST_*)
1679 */
1680
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001681static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001682{
1683 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001684 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001685 unsigned int buf;
1686 unsigned char type;
1687
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001688 length = rgd->rd_length;
1689 rgrp_block = block - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001690
1691 for (buf = 0; buf < length; buf++) {
1692 bi = rgd->rd_bits + buf;
1693 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1694 break;
1695 }
1696
1697 gfs2_assert(rgd->rd_sbd, buf < length);
1698 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1699
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001700 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001701 bi->bi_len, buf_block);
1702
1703 return type;
1704}
1705
1706/**
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001707 * rgblk_search - find a block in @state
David Teiglandb3b94fa2006-01-16 16:50:04 +00001708 * @rgd: the resource group descriptor
1709 * @goal: the goal block within the RG (start here to search for avail block)
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001710 * @state: GFS2_BLKST_XXX the before-allocation state to find
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001711 * @rbi: address of the pointer to the bitmap containing the block found
David Teiglandb3b94fa2006-01-16 16:50:04 +00001712 *
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001713 * Walk rgrp's bitmap to find bits that represent a block in @state.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001714 *
1715 * This function never fails, because we wouldn't call it unless we
1716 * know (from reservation results, etc.) that a block is available.
1717 *
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001718 * Scope of @goal is just within rgrp, not the whole filesystem.
1719 * Scope of @returned block is just within bitmap, not the whole filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001720 *
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001721 * Returns: the block number found relative to the bitmap rbi
David Teiglandb3b94fa2006-01-16 16:50:04 +00001722 */
1723
Bob Peterson886b1412012-04-11 13:03:52 -04001724static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, unsigned char state,
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001725 struct gfs2_bitmap **rbi)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001726{
1727 struct gfs2_bitmap *bi = NULL;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001728 const u32 length = rgd->rd_length;
Bob Peterson9598d252012-04-12 08:41:43 -04001729 u32 biblk = BFITNOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001730 unsigned int buf, x;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001731 const u8 *buffer = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001732
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001733 *rbi = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001734 /* Find bitmap block that contains bits for goal block */
1735 for (buf = 0; buf < length; buf++) {
1736 bi = rgd->rd_bits + buf;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001737 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1738 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1739 goal -= bi->bi_start * GFS2_NBBY;
1740 goto do_search;
1741 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001742 }
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001743 buf = 0;
1744 goal = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001745
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001746do_search:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001747 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1748 "x <= length", instead of "x < length", because we typically start
1749 the search in the middle of a bit block, but if we can't find an
1750 allocatable block anywhere else, we want to be able wrap around and
1751 search in the first part of our first-searched bit block. */
1752 for (x = 0; x <= length; x++) {
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001753 bi = rgd->rd_bits + buf;
1754
1755 if (test_bit(GBF_FULL, &bi->bi_flags) &&
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001756 (state == GFS2_BLKST_FREE))
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001757 goto skip;
1758
Bob Peterson5f3eae72007-08-08 16:52:09 -05001759 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1760 bitmaps, so we must search the originals for that. */
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001761 buffer = bi->bi_bh->b_data + bi->bi_offset;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001762 WARN_ON(!buffer_uptodate(bi->bi_bh));
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001763 if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
Steven Whitehouse110acf32008-01-29 13:30:20 +00001764 buffer = bi->bi_clone + bi->bi_offset;
1765
Bob Peterson8e2e0042012-07-19 08:12:40 -04001766 while (1) {
1767 struct gfs2_blkreserv *rs;
1768 u32 rgblk;
1769
1770 biblk = gfs2_bitfit(buffer, bi->bi_len, goal, state);
1771 if (biblk == BFITNOENT)
1772 break;
1773 /* Check if this block is reserved() */
1774 rgblk = gfs2_bi2rgd_blk(bi, biblk);
1775 rs = rs_find(rgd, rgblk);
1776 if (rs == NULL)
1777 break;
1778
1779 BUG_ON(rs->rs_bi != bi);
1780 biblk = BFITNOENT;
1781 /* This should jump to the first block after the
1782 reservation. */
1783 goal = rs->rs_biblk + rs->rs_free;
1784 if (goal >= bi->bi_len * GFS2_NBBY)
1785 break;
1786 }
Bob Peterson9598d252012-04-12 08:41:43 -04001787 if (biblk != BFITNOENT)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001788 break;
1789
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001790 if ((goal == 0) && (state == GFS2_BLKST_FREE))
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001791 set_bit(GBF_FULL, &bi->bi_flags);
1792
David Teiglandb3b94fa2006-01-16 16:50:04 +00001793 /* Try next bitmap block (wrap back to rgrp header if at end) */
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001794skip:
1795 buf++;
1796 buf %= length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001797 goal = 0;
1798 }
1799
Bob Peterson9598d252012-04-12 08:41:43 -04001800 if (biblk != BFITNOENT)
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001801 *rbi = bi;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001802
Bob Peterson9598d252012-04-12 08:41:43 -04001803 return biblk;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001804}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001805
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001806/**
1807 * gfs2_alloc_extent - allocate an extent from a given bitmap
1808 * @rgd: the resource group descriptor
1809 * @bi: the bitmap within the rgrp
1810 * @blk: the block within the bitmap
1811 * @dinode: TRUE if the first block we allocate is for a dinode
1812 * @n: The extent length
1813 *
1814 * Add the found bitmap buffer to the transaction.
1815 * Set the found bits to @new_state to change block's allocation state.
1816 * Returns: starting block number of the extent (fs scope)
1817 */
1818static u64 gfs2_alloc_extent(struct gfs2_rgrpd *rgd, struct gfs2_bitmap *bi,
1819 u32 blk, bool dinode, unsigned int *n)
1820{
1821 const unsigned int elen = *n;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001822 u32 goal, rgblk;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001823 const u8 *buffer = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001824 struct gfs2_blkreserv *rs;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001825
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001826 *n = 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001827 buffer = bi->bi_bh->b_data + bi->bi_offset;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001828 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Bob Peterson06344b92012-04-26 12:44:35 -04001829 gfs2_setbit(rgd, bi->bi_clone, bi, blk,
1830 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001831 (*n)++;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001832 goal = blk;
1833 while (*n < elen) {
1834 goal++;
1835 if (goal >= (bi->bi_len * GFS2_NBBY))
1836 break;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001837 rgblk = gfs2_bi2rgd_blk(bi, goal);
1838 rs = rs_find(rgd, rgblk);
1839 if (rs) /* Oops, we bumped into someone's reservation */
1840 break;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001841 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1842 GFS2_BLKST_FREE)
1843 break;
Bob Peterson06344b92012-04-26 12:44:35 -04001844 gfs2_setbit(rgd, bi->bi_clone, bi, goal, GFS2_BLKST_USED);
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001845 (*n)++;
1846 }
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001847 blk = gfs2_bi2rgd_blk(bi, blk);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001848 rgd->rd_last_alloc = blk + *n - 1;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001849 return rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001850}
1851
1852/**
1853 * rgblk_free - Change alloc state of given block(s)
1854 * @sdp: the filesystem
1855 * @bstart: the start of a run of blocks to free
1856 * @blen: the length of the block run (all must lie within ONE RG!)
1857 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1858 *
1859 * Returns: Resource group containing the block(s)
1860 */
1861
Steven Whitehousecd915492006-09-04 12:49:07 -04001862static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1863 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001864{
1865 struct gfs2_rgrpd *rgd;
1866 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001867 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001868 unsigned int buf;
1869
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001870 rgd = gfs2_blk2rgrpd(sdp, bstart, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001871 if (!rgd) {
1872 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001873 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001874 return NULL;
1875 }
1876
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001877 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001878
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001879 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001880
1881 while (blen--) {
1882 for (buf = 0; buf < length; buf++) {
1883 bi = rgd->rd_bits + buf;
1884 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1885 break;
1886 }
1887
1888 gfs2_assert(rgd->rd_sbd, buf < length);
1889
1890 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1891 rgrp_blk++;
1892
1893 if (!bi->bi_clone) {
1894 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001895 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001896 memcpy(bi->bi_clone + bi->bi_offset,
1897 bi->bi_bh->b_data + bi->bi_offset,
1898 bi->bi_len);
1899 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001900 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Bob Peterson06344b92012-04-26 12:44:35 -04001901 gfs2_setbit(rgd, NULL, bi, buf_blk, new_state);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001902 }
1903
1904 return rgd;
1905}
1906
1907/**
Steven Whitehouse09010972009-05-20 10:48:47 +01001908 * gfs2_rgrp_dump - print out an rgrp
1909 * @seq: The iterator
1910 * @gl: The glock in question
David Teiglandb3b94fa2006-01-16 16:50:04 +00001911 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001912 */
1913
Steven Whitehouse09010972009-05-20 10:48:47 +01001914int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
1915{
Bob Peterson8e2e0042012-07-19 08:12:40 -04001916 struct gfs2_rgrpd *rgd = gl->gl_object;
1917 struct gfs2_blkreserv *trs;
1918 const struct rb_node *n;
1919
Steven Whitehouse09010972009-05-20 10:48:47 +01001920 if (rgd == NULL)
1921 return 0;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001922 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u r:%u\n",
Steven Whitehouse09010972009-05-20 10:48:47 +01001923 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
Bob Peterson8e2e0042012-07-19 08:12:40 -04001924 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
1925 rgd->rd_reserved);
1926 spin_lock(&rgd->rd_rsspin);
1927 for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
1928 trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1929 dump_rs(seq, trs);
1930 }
1931 spin_unlock(&rgd->rd_rsspin);
Steven Whitehouse09010972009-05-20 10:48:47 +01001932 return 0;
1933}
1934
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01001935static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
1936{
1937 struct gfs2_sbd *sdp = rgd->rd_sbd;
1938 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
Steven Whitehouse86d00632009-09-14 09:50:57 +01001939 (unsigned long long)rgd->rd_addr);
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01001940 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
1941 gfs2_rgrp_dump(NULL, rgd->rd_gl);
1942 rgd->rd_flags |= GFS2_RDF_ERROR;
1943}
1944
Steven Whitehouse09010972009-05-20 10:48:47 +01001945/**
Bob Peterson8e2e0042012-07-19 08:12:40 -04001946 * claim_reserved_blks - Claim previously reserved blocks
1947 * @ip: the inode that's claiming the reservation
1948 * @dinode: 1 if this block is a dinode block, otherwise data block
1949 * @nblocks: desired extent length
1950 *
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001951 * Lay claim to previously reserved blocks.
Bob Peterson8e2e0042012-07-19 08:12:40 -04001952 * Returns: Starting block number of the blocks claimed.
1953 * Sets *nblocks to the actual extent length allocated.
1954 */
1955static u64 claim_reserved_blks(struct gfs2_inode *ip, bool dinode,
1956 unsigned int *nblocks)
1957{
1958 struct gfs2_blkreserv *rs = ip->i_res;
1959 struct gfs2_rgrpd *rgd = rs->rs_rgd;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001960 struct gfs2_bitmap *bi;
1961 u64 start_block = gfs2_rs_startblk(rs);
1962 const unsigned int elen = *nblocks;
1963
Bob Peterson8e2e0042012-07-19 08:12:40 -04001964 bi = rs->rs_bi;
1965 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1966
1967 for (*nblocks = 0; *nblocks < elen && rs->rs_free; (*nblocks)++) {
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001968 if (gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
1969 bi->bi_len, rs->rs_biblk) != GFS2_BLKST_FREE)
1970 break;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001971 gfs2_setbit(rgd, bi->bi_clone, bi, rs->rs_biblk,
1972 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
1973 rs->rs_biblk++;
1974 rs->rs_free--;
1975
1976 BUG_ON(!rgd->rd_reserved);
1977 rgd->rd_reserved--;
1978 dinode = false;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001979 }
1980
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001981 trace_gfs2_rs(ip, rs, TRACE_RS_CLAIM);
1982 if (!rs->rs_free || *nblocks != elen)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001983 gfs2_rs_deltree(rs);
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001984
Bob Peterson8e2e0042012-07-19 08:12:40 -04001985 return start_block;
1986}
1987
1988/**
Bob Peterson6e87ed02011-11-18 10:58:32 -05001989 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
Steven Whitehouse09010972009-05-20 10:48:47 +01001990 * @ip: the inode to allocate the block for
1991 * @bn: Used to return the starting block number
Bob Peterson8e2e0042012-07-19 08:12:40 -04001992 * @nblocks: requested number of blocks/extent length (value/result)
Bob Peterson6e87ed02011-11-18 10:58:32 -05001993 * @dinode: 1 if we're allocating a dinode block, else 0
Bob Peterson3c5d7852011-11-14 11:17:08 -05001994 * @generation: the generation number of the inode
Steven Whitehouse09010972009-05-20 10:48:47 +01001995 *
1996 * Returns: 0 or error
1997 */
1998
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001999int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
Bob Peterson6e87ed02011-11-18 10:58:32 -05002000 bool dinode, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002001{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002002 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehoused9ba7612009-04-23 08:59:41 +01002003 struct buffer_head *dibh;
Steven Whitehouse9a3f2362010-08-23 11:49:34 +01002004 struct gfs2_rgrpd *rgd;
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002005 unsigned int ndata;
2006 u32 goal, blk; /* block, within the rgrp scope */
Bob Peterson3c5d7852011-11-14 11:17:08 -05002007 u64 block; /* block, within the file system scope */
Steven Whitehoused9ba7612009-04-23 08:59:41 +01002008 int error;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05002009 struct gfs2_bitmap *bi;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002010
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002011 /* If we have a reservation, claim blocks from it. */
Bob Peterson8e2e0042012-07-19 08:12:40 -04002012 if (gfs2_rs_active(ip->i_res)) {
2013 BUG_ON(!ip->i_res->rs_free);
2014 rgd = ip->i_res->rs_rgd;
2015 block = claim_reserved_blks(ip, dinode, nblocks);
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002016 if (*nblocks)
2017 goto found_blocks;
Bob Peterson8e2e0042012-07-19 08:12:40 -04002018 }
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002019
2020 rgd = ip->i_rgd;
2021
2022 if (!dinode && rgrp_contains_block(rgd, ip->i_goal))
2023 goal = ip->i_goal - rgd->rd_data0;
2024 else
2025 goal = rgd->rd_last_alloc;
2026
2027 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, &bi);
2028
2029 /* Since all blocks are reserved in advance, this shouldn't happen */
2030 if (blk == BFITNOENT) {
2031 printk(KERN_WARNING "BFITNOENT, nblocks=%u\n", *nblocks);
2032 printk(KERN_WARNING "FULL=%d\n",
2033 test_bit(GBF_FULL, &rgd->rd_bits->bi_flags));
2034 goto rgrp_error;
2035 }
2036
2037 block = gfs2_alloc_extent(rgd, bi, blk, dinode, nblocks);
2038found_blocks:
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002039 ndata = *nblocks;
2040 if (dinode)
2041 ndata--;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05002042
Bob Peterson3c5d7852011-11-14 11:17:08 -05002043 if (!dinode) {
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002044 ip->i_goal = block + ndata - 1;
Bob Peterson3c5d7852011-11-14 11:17:08 -05002045 error = gfs2_meta_inode_buffer(ip, &dibh);
2046 if (error == 0) {
2047 struct gfs2_dinode *di =
2048 (struct gfs2_dinode *)dibh->b_data;
2049 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
2050 di->di_goal_meta = di->di_goal_data =
2051 cpu_to_be64(ip->i_goal);
2052 brelse(dibh);
2053 }
Steven Whitehoused9ba7612009-04-23 08:59:41 +01002054 }
Bob Peterson8e2e0042012-07-19 08:12:40 -04002055 if (rgd->rd_free < *nblocks) {
2056 printk(KERN_WARNING "nblocks=%u\n", *nblocks);
Steven Whitehouse09010972009-05-20 10:48:47 +01002057 goto rgrp_error;
Bob Peterson8e2e0042012-07-19 08:12:40 -04002058 }
Steven Whitehouse09010972009-05-20 10:48:47 +01002059
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002060 rgd->rd_free -= *nblocks;
Bob Peterson3c5d7852011-11-14 11:17:08 -05002061 if (dinode) {
2062 rgd->rd_dinodes++;
2063 *generation = rgd->rd_igeneration++;
2064 if (*generation == 0)
2065 *generation = rgd->rd_igeneration++;
2066 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002067
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00002068 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06002069 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05002070 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002071
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002072 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
Bob Peterson3c5d7852011-11-14 11:17:08 -05002073 if (dinode)
2074 gfs2_trans_add_unrevoke(sdp, block, 1);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002075
2076 /*
2077 * This needs reviewing to see why we cannot do the quota change
2078 * at this point in the dinode case.
2079 */
2080 if (ndata)
2081 gfs2_quota_change(ip, ndata, ip->i_inode.i_uid,
Bob Peterson3c5d7852011-11-14 11:17:08 -05002082 ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002083
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002084 rgd->rd_free_clone -= *nblocks;
Bob Peterson41db1ab2012-05-09 12:11:35 -04002085 trace_gfs2_block_alloc(ip, rgd, block, *nblocks,
Bob Peterson6e87ed02011-11-18 10:58:32 -05002086 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01002087 *bn = block;
2088 return 0;
2089
2090rgrp_error:
2091 gfs2_rgrp_error(rgd);
2092 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002093}
2094
2095/**
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002096 * __gfs2_free_blocks - free a contiguous run of block(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002097 * @ip: the inode these blocks are being freed from
2098 * @bstart: first block of a run of contiguous blocks
2099 * @blen: the length of the block run
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002100 * @meta: 1 if the blocks represent metadata
David Teiglandb3b94fa2006-01-16 16:50:04 +00002101 *
2102 */
2103
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002104void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002105{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002106 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002107 struct gfs2_rgrpd *rgd;
2108
2109 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
2110 if (!rgd)
2111 return;
Bob Peterson41db1ab2012-05-09 12:11:35 -04002112 trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
Steven Whitehousecfc8b542008-11-04 10:25:13 +00002113 rgd->rd_free += blen;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00002114 rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00002115 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06002116 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05002117 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002118
Steven Whitehouse6d3117b2011-05-21 14:05:58 +01002119 /* Directories keep their data in the metadata address space */
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002120 if (meta || ip->i_depth)
Steven Whitehouse6d3117b2011-05-21 14:05:58 +01002121 gfs2_meta_wipe(ip, bstart, blen);
Bob Peterson4c16c362011-02-23 16:11:33 -05002122}
David Teiglandb3b94fa2006-01-16 16:50:04 +00002123
Bob Peterson4c16c362011-02-23 16:11:33 -05002124/**
Bob Peterson4c16c362011-02-23 16:11:33 -05002125 * gfs2_free_meta - free a contiguous run of data block(s)
2126 * @ip: the inode these blocks are being freed from
2127 * @bstart: first block of a run of contiguous blocks
2128 * @blen: the length of the block run
2129 *
2130 */
2131
Steven Whitehousecd915492006-09-04 12:49:07 -04002132void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002133{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002134 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002135
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002136 __gfs2_free_blocks(ip, bstart, blen, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002137 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05002138 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002139}
2140
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002141void gfs2_unlink_di(struct inode *inode)
2142{
2143 struct gfs2_inode *ip = GFS2_I(inode);
2144 struct gfs2_sbd *sdp = GFS2_SB(inode);
2145 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01002146 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002147
2148 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
2149 if (!rgd)
2150 return;
Bob Peterson41db1ab2012-05-09 12:11:35 -04002151 trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002152 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06002153 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05002154 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2155 update_rgrp_lvb_unlinked(rgd, 1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002156}
2157
Steven Whitehousecd915492006-09-04 12:49:07 -04002158static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002159{
2160 struct gfs2_sbd *sdp = rgd->rd_sbd;
2161 struct gfs2_rgrpd *tmp_rgd;
2162
2163 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
2164 if (!tmp_rgd)
2165 return;
2166 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
2167
Steven Whitehouse73f74942008-11-04 10:32:57 +00002168 if (!rgd->rd_dinodes)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002169 gfs2_consist_rgrpd(rgd);
Steven Whitehouse73f74942008-11-04 10:32:57 +00002170 rgd->rd_dinodes--;
Steven Whitehousecfc8b542008-11-04 10:25:13 +00002171 rgd->rd_free++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002172
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00002173 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06002174 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05002175 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2176 update_rgrp_lvb_unlinked(rgd, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002177
2178 gfs2_statfs_change(sdp, 0, +1, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002179}
2180
David Teiglandb3b94fa2006-01-16 16:50:04 +00002181
2182void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
2183{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01002184 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Bob Peterson41db1ab2012-05-09 12:11:35 -04002185 trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
Steven Whitehouse2933f922006-11-01 13:23:29 -05002186 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01002187 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002188}
2189
2190/**
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002191 * gfs2_check_blk_type - Check the type of a block
2192 * @sdp: The superblock
2193 * @no_addr: The block number to check
2194 * @type: The block type we are looking for
2195 *
2196 * Returns: 0 if the block type matches the expected type
2197 * -ESTALE if it doesn't match
2198 * or -ve errno if something went wrong while checking
2199 */
2200
2201int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
2202{
2203 struct gfs2_rgrpd *rgd;
Steven Whitehouse8339ee52011-08-31 16:38:29 +01002204 struct gfs2_holder rgd_gh;
Bob Peterson58884c42012-03-05 10:19:35 -05002205 int error = -EINVAL;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002206
Steven Whitehouse66fc0612012-02-08 12:58:32 +00002207 rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002208 if (!rgd)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01002209 goto fail;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002210
2211 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
2212 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01002213 goto fail;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002214
2215 if (gfs2_get_block_type(rgd, no_addr) != type)
2216 error = -ESTALE;
2217
2218 gfs2_glock_dq_uninit(&rgd_gh);
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002219fail:
2220 return error;
2221}
2222
2223/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00002224 * gfs2_rlist_add - add a RG to a list of RGs
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002225 * @ip: the inode
David Teiglandb3b94fa2006-01-16 16:50:04 +00002226 * @rlist: the list of resource groups
2227 * @block: the block
2228 *
2229 * Figure out what RG a block belongs to and add that RG to the list
2230 *
2231 * FIXME: Don't use NOFAIL
2232 *
2233 */
2234
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002235void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04002236 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002237{
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002238 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002239 struct gfs2_rgrpd *rgd;
2240 struct gfs2_rgrpd **tmp;
2241 unsigned int new_space;
2242 unsigned int x;
2243
2244 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
2245 return;
2246
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002247 if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block))
2248 rgd = ip->i_rgd;
2249 else
Steven Whitehouse66fc0612012-02-08 12:58:32 +00002250 rgd = gfs2_blk2rgrpd(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002251 if (!rgd) {
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002252 fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002253 return;
2254 }
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002255 ip->i_rgd = rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002256
2257 for (x = 0; x < rlist->rl_rgrps; x++)
2258 if (rlist->rl_rgd[x] == rgd)
2259 return;
2260
2261 if (rlist->rl_rgrps == rlist->rl_space) {
2262 new_space = rlist->rl_space + 10;
2263
2264 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04002265 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002266
2267 if (rlist->rl_rgd) {
2268 memcpy(tmp, rlist->rl_rgd,
2269 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
2270 kfree(rlist->rl_rgd);
2271 }
2272
2273 rlist->rl_space = new_space;
2274 rlist->rl_rgd = tmp;
2275 }
2276
2277 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
2278}
2279
2280/**
2281 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
2282 * and initialize an array of glock holders for them
2283 * @rlist: the list of resource groups
2284 * @state: the lock state to acquire the RG lock in
David Teiglandb3b94fa2006-01-16 16:50:04 +00002285 *
2286 * FIXME: Don't use NOFAIL
2287 *
2288 */
2289
Bob Petersonfe6c9912008-01-28 11:13:02 -06002290void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002291{
2292 unsigned int x;
2293
2294 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04002295 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002296 for (x = 0; x < rlist->rl_rgrps; x++)
2297 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
Bob Petersonfe6c9912008-01-28 11:13:02 -06002298 state, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002299 &rlist->rl_ghs[x]);
2300}
2301
2302/**
2303 * gfs2_rlist_free - free a resource group list
2304 * @list: the list of resource groups
2305 *
2306 */
2307
2308void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
2309{
2310 unsigned int x;
2311
2312 kfree(rlist->rl_rgd);
2313
2314 if (rlist->rl_ghs) {
2315 for (x = 0; x < rlist->rl_rgrps; x++)
2316 gfs2_holder_uninit(&rlist->rl_ghs[x]);
2317 kfree(rlist->rl_ghs);
Bob Peterson8e2e0042012-07-19 08:12:40 -04002318 rlist->rl_ghs = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002319 }
2320}
2321