blob: eaa41885a00de679e6b0c05ef577b759c173483e [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{
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100195 u64 startblk = gfs2_rbm_to_block(&rs->rs_rbm);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400196
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
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100490 rb_init_node(&res->rs_node);
491
Bob Peterson0a305e42012-06-06 11:17:59 +0100492 down_write(&ip->i_rw_mutex);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400493 if (ip->i_res)
494 kmem_cache_free(gfs2_rsrv_cachep, res);
495 else
496 ip->i_res = res;
Bob Peterson0a305e42012-06-06 11:17:59 +0100497 up_write(&ip->i_rw_mutex);
498 return error;
499}
500
Bob Peterson8e2e0042012-07-19 08:12:40 -0400501static void dump_rs(struct seq_file *seq, struct gfs2_blkreserv *rs)
502{
503 gfs2_print_dbg(seq, " r: %llu s:%llu b:%u f:%u\n",
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100504 rs->rs_rbm.rgd->rd_addr, gfs2_rbm_to_block(&rs->rs_rbm),
505 rs->rs_rbm.offset, rs->rs_free);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400506}
507
Bob Peterson0a305e42012-06-06 11:17:59 +0100508/**
Bob Peterson8e2e0042012-07-19 08:12:40 -0400509 * __rs_deltree - remove a multi-block reservation from the rgd tree
510 * @rs: The reservation to remove
511 *
512 */
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100513static void __rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
Bob Peterson8e2e0042012-07-19 08:12:40 -0400514{
515 struct gfs2_rgrpd *rgd;
516
517 if (!gfs2_rs_active(rs))
518 return;
519
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100520 rgd = rs->rs_rbm.rgd;
521 trace_gfs2_rs(ip, rs, TRACE_RS_TREEDEL);
522 rb_erase(&rs->rs_node, &rgd->rd_rstree);
523 rb_init_node(&rs->rs_node);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400524 BUG_ON(!rgd->rd_rs_cnt);
525 rgd->rd_rs_cnt--;
526
527 if (rs->rs_free) {
528 /* return reserved blocks to the rgrp and the ip */
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100529 BUG_ON(rs->rs_rbm.rgd->rd_reserved < rs->rs_free);
530 rs->rs_rbm.rgd->rd_reserved -= rs->rs_free;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400531 rs->rs_free = 0;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100532 clear_bit(GBF_FULL, &rs->rs_rbm.bi->bi_flags);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400533 smp_mb__after_clear_bit();
534 }
Bob Peterson8e2e0042012-07-19 08:12:40 -0400535}
536
537/**
538 * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
539 * @rs: The reservation to remove
540 *
541 */
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100542void gfs2_rs_deltree(struct gfs2_inode *ip, struct gfs2_blkreserv *rs)
Bob Peterson8e2e0042012-07-19 08:12:40 -0400543{
544 struct gfs2_rgrpd *rgd;
545
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100546 rgd = rs->rs_rbm.rgd;
547 if (rgd) {
548 spin_lock(&rgd->rd_rsspin);
549 __rs_deltree(ip, rs);
550 spin_unlock(&rgd->rd_rsspin);
551 }
Bob Peterson8e2e0042012-07-19 08:12:40 -0400552}
553
554/**
555 * gfs2_rs_delete - delete a multi-block reservation
Bob Peterson0a305e42012-06-06 11:17:59 +0100556 * @ip: The inode for this reservation
557 *
558 */
559void gfs2_rs_delete(struct gfs2_inode *ip)
560{
561 down_write(&ip->i_rw_mutex);
562 if (ip->i_res) {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100563 gfs2_rs_deltree(ip, ip->i_res);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400564 trace_gfs2_rs(ip, ip->i_res, TRACE_RS_DELETE);
565 BUG_ON(ip->i_res->rs_free);
Bob Peterson0a305e42012-06-06 11:17:59 +0100566 kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
567 ip->i_res = NULL;
568 }
569 up_write(&ip->i_rw_mutex);
570}
571
Bob Peterson8e2e0042012-07-19 08:12:40 -0400572/**
573 * return_all_reservations - return all reserved blocks back to the rgrp.
574 * @rgd: the rgrp that needs its space back
575 *
576 * We previously reserved a bunch of blocks for allocation. Now we need to
577 * give them back. This leave the reservation structures in tact, but removes
578 * all of their corresponding "no-fly zones".
579 */
580static void return_all_reservations(struct gfs2_rgrpd *rgd)
581{
582 struct rb_node *n;
583 struct gfs2_blkreserv *rs;
584
585 spin_lock(&rgd->rd_rsspin);
586 while ((n = rb_first(&rgd->rd_rstree))) {
587 rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
Steven Whitehouse4a993fb2012-07-31 15:21:20 +0100588 __rs_deltree(NULL, rs);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400589 }
590 spin_unlock(&rgd->rd_rsspin);
591}
592
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100593void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100595 struct rb_node *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 struct gfs2_rgrpd *rgd;
597 struct gfs2_glock *gl;
598
Bob Peterson7c9ca622011-08-31 09:53:19 +0100599 while ((n = rb_first(&sdp->sd_rindex_tree))) {
600 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601 gl = rgd->rd_gl;
602
Bob Peterson7c9ca622011-08-31 09:53:19 +0100603 rb_erase(n, &sdp->sd_rindex_tree);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604
605 if (gl) {
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100606 spin_lock(&gl->gl_spin);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500607 gl->gl_object = NULL;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100608 spin_unlock(&gl->gl_spin);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100609 gfs2_glock_add_to_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 gfs2_glock_put(gl);
611 }
612
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100613 gfs2_free_clones(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614 kfree(rgd->rd_bits);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400615 return_all_reservations(rgd);
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600616 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 }
618}
619
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100620static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
621{
622 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
623 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
624 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
625 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
626 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
627}
628
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629/**
630 * gfs2_compute_bitstructs - Compute the bitmap sizes
631 * @rgd: The resource group descriptor
632 *
633 * Calculates bitmap descriptors, one for each block that contains bitmap data
634 *
635 * Returns: errno
636 */
637
638static int compute_bitstructs(struct gfs2_rgrpd *rgd)
639{
640 struct gfs2_sbd *sdp = rgd->rd_sbd;
641 struct gfs2_bitmap *bi;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100642 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
Steven Whitehousecd915492006-09-04 12:49:07 -0400643 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 int x;
645
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400646 if (!length)
647 return -EINVAL;
648
Steven Whitehousedd894be2006-07-27 14:29:00 -0400649 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 if (!rgd->rd_bits)
651 return -ENOMEM;
652
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100653 bytes_left = rgd->rd_bitbytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654
655 for (x = 0; x < length; x++) {
656 bi = rgd->rd_bits + x;
657
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +0100658 bi->bi_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659 /* small rgrp; bitmap stored completely in header block */
660 if (length == 1) {
661 bytes = bytes_left;
662 bi->bi_offset = sizeof(struct gfs2_rgrp);
663 bi->bi_start = 0;
664 bi->bi_len = bytes;
665 /* header block */
666 } else if (x == 0) {
667 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
668 bi->bi_offset = sizeof(struct gfs2_rgrp);
669 bi->bi_start = 0;
670 bi->bi_len = bytes;
671 /* last block */
672 } else if (x + 1 == length) {
673 bytes = bytes_left;
674 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100675 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676 bi->bi_len = bytes;
677 /* other blocks */
678 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500679 bytes = sdp->sd_sb.sb_bsize -
680 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100682 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 bi->bi_len = bytes;
684 }
685
686 bytes_left -= bytes;
687 }
688
689 if (bytes_left) {
690 gfs2_consist_rgrpd(rgd);
691 return -EIO;
692 }
693 bi = rgd->rd_bits + (length - 1);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100694 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695 if (gfs2_consist_rgrpd(rgd)) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100696 gfs2_rindex_print(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 fs_err(sdp, "start=%u len=%u offset=%u\n",
698 bi->bi_start, bi->bi_len, bi->bi_offset);
699 }
700 return -EIO;
701 }
702
703 return 0;
704}
705
706/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500707 * gfs2_ri_total - Total up the file system space, according to the rindex.
Bob Peterson886b1412012-04-11 13:03:52 -0400708 * @sdp: the filesystem
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500709 *
710 */
711u64 gfs2_ri_total(struct gfs2_sbd *sdp)
712{
713 u64 total_data = 0;
714 struct inode *inode = sdp->sd_rindex;
715 struct gfs2_inode *ip = GFS2_I(inode);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500716 char buf[sizeof(struct gfs2_rindex)];
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500717 int error, rgrps;
718
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500719 for (rgrps = 0;; rgrps++) {
720 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
721
Bob Petersonbcd72782010-12-07 13:58:56 -0500722 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500723 break;
Andrew Price43066292012-04-16 16:40:55 +0100724 error = gfs2_internal_read(ip, buf, &pos,
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500725 sizeof(struct gfs2_rindex));
726 if (error != sizeof(struct gfs2_rindex))
727 break;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100728 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500729 }
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500730 return total_data;
731}
732
Bob Peterson6aad1c32012-03-05 09:20:59 -0500733static int rgd_insert(struct gfs2_rgrpd *rgd)
Bob Peterson7c9ca622011-08-31 09:53:19 +0100734{
735 struct gfs2_sbd *sdp = rgd->rd_sbd;
736 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
737
738 /* Figure out where to put new node */
739 while (*newn) {
740 struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
741 rd_node);
742
743 parent = *newn;
744 if (rgd->rd_addr < cur->rd_addr)
745 newn = &((*newn)->rb_left);
746 else if (rgd->rd_addr > cur->rd_addr)
747 newn = &((*newn)->rb_right);
748 else
Bob Peterson6aad1c32012-03-05 09:20:59 -0500749 return -EEXIST;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100750 }
751
752 rb_link_node(&rgd->rd_node, parent, newn);
753 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
Bob Peterson6aad1c32012-03-05 09:20:59 -0500754 sdp->sd_rgrps++;
755 return 0;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100756}
757
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500758/**
Robert Peterson6c532672007-05-10 16:54:38 -0500759 * read_rindex_entry - Pull in a new resource index entry from the disk
Andrew Price43066292012-04-16 16:40:55 +0100760 * @ip: Pointer to the rindex inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 *
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100762 * Returns: 0 on success, > 0 on EOF, error code otherwise
Robert Peterson6c532672007-05-10 16:54:38 -0500763 */
764
Andrew Price43066292012-04-16 16:40:55 +0100765static int read_rindex_entry(struct gfs2_inode *ip)
Robert Peterson6c532672007-05-10 16:54:38 -0500766{
767 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
768 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100769 struct gfs2_rindex buf;
Robert Peterson6c532672007-05-10 16:54:38 -0500770 int error;
771 struct gfs2_rgrpd *rgd;
772
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100773 if (pos >= i_size_read(&ip->i_inode))
774 return 1;
775
Andrew Price43066292012-04-16 16:40:55 +0100776 error = gfs2_internal_read(ip, (char *)&buf, &pos,
Robert Peterson6c532672007-05-10 16:54:38 -0500777 sizeof(struct gfs2_rindex));
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100778
779 if (error != sizeof(struct gfs2_rindex))
780 return (error == 0) ? 1 : error;
Robert Peterson6c532672007-05-10 16:54:38 -0500781
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600782 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
Robert Peterson6c532672007-05-10 16:54:38 -0500783 error = -ENOMEM;
784 if (!rgd)
785 return error;
786
Robert Peterson6c532672007-05-10 16:54:38 -0500787 rgd->rd_sbd = sdp;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100788 rgd->rd_addr = be64_to_cpu(buf.ri_addr);
789 rgd->rd_length = be32_to_cpu(buf.ri_length);
790 rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
791 rgd->rd_data = be32_to_cpu(buf.ri_data);
792 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
Bob Peterson8e2e0042012-07-19 08:12:40 -0400793 spin_lock_init(&rgd->rd_rsspin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100794
Robert Peterson6c532672007-05-10 16:54:38 -0500795 error = compute_bitstructs(rgd);
796 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100797 goto fail;
Robert Peterson6c532672007-05-10 16:54:38 -0500798
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100799 error = gfs2_glock_get(sdp, rgd->rd_addr,
Robert Peterson6c532672007-05-10 16:54:38 -0500800 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
801 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100802 goto fail;
Robert Peterson6c532672007-05-10 16:54:38 -0500803
804 rgd->rd_gl->gl_object = rgd;
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500805 rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lvb;
Bob Petersoncf45b752008-01-31 10:31:39 -0600806 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100807 if (rgd->rd_data > sdp->sd_max_rg_data)
808 sdp->sd_max_rg_data = rgd->rd_data;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100809 spin_lock(&sdp->sd_rindex_spin);
Bob Peterson6aad1c32012-03-05 09:20:59 -0500810 error = rgd_insert(rgd);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100811 spin_unlock(&sdp->sd_rindex_spin);
Bob Peterson6aad1c32012-03-05 09:20:59 -0500812 if (!error)
813 return 0;
814
815 error = 0; /* someone else read in the rgrp; free it and ignore it */
Bob Petersonc1ac5392012-03-22 08:58:30 -0400816 gfs2_glock_put(rgd->rd_gl);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100817
818fail:
819 kfree(rgd->rd_bits);
820 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
Robert Peterson6c532672007-05-10 16:54:38 -0500821 return error;
822}
823
824/**
825 * gfs2_ri_update - Pull in a new resource index from the disk
826 * @ip: pointer to the rindex inode
827 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 * Returns: 0 on successful update, error code otherwise
829 */
830
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100831static int gfs2_ri_update(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400833 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 int error;
835
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100836 do {
Andrew Price43066292012-04-16 16:40:55 +0100837 error = read_rindex_entry(ip);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100838 } while (error == 0);
839
840 if (error < 0)
841 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842
Bob Petersoncf45b752008-01-31 10:31:39 -0600843 sdp->sd_rindex_uptodate = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 return 0;
Robert Peterson6c532672007-05-10 16:54:38 -0500845}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846
Robert Peterson6c532672007-05-10 16:54:38 -0500847/**
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100848 * gfs2_rindex_update - Update the rindex if required
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 * @sdp: The GFS2 superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 *
851 * We grab a lock on the rindex inode to make sure that it doesn't
852 * change whilst we are performing an operation. We keep this lock
853 * for quite long periods of time compared to other locks. This
854 * doesn't matter, since it is shared and it is very, very rarely
855 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
856 *
857 * This makes sure that we're using the latest copy of the resource index
858 * special file, which might have been updated if someone expanded the
859 * filesystem (via gfs2_grow utility), which adds new resource groups.
860 *
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100861 * Returns: 0 on succeess, error code otherwise
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 */
863
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100864int gfs2_rindex_update(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400866 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 struct gfs2_glock *gl = ip->i_gl;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100868 struct gfs2_holder ri_gh;
869 int error = 0;
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000870 int unlock_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871
872 /* Read new copy from disk if we don't have the latest */
Bob Petersoncf45b752008-01-31 10:31:39 -0600873 if (!sdp->sd_rindex_uptodate) {
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000874 if (!gfs2_glock_is_locked_by_me(gl)) {
875 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
876 if (error)
Bob Peterson6aad1c32012-03-05 09:20:59 -0500877 return error;
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000878 unlock_required = 1;
879 }
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100880 if (!sdp->sd_rindex_uptodate)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 error = gfs2_ri_update(ip);
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000882 if (unlock_required)
883 gfs2_glock_dq_uninit(&ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884 }
885
886 return error;
887}
888
Bob Peterson42d52e32008-01-28 18:38:07 -0600889static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100890{
891 const struct gfs2_rgrp *str = buf;
Bob Peterson42d52e32008-01-28 18:38:07 -0600892 u32 rg_flags;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100893
Bob Peterson42d52e32008-01-28 18:38:07 -0600894 rg_flags = be32_to_cpu(str->rg_flags);
Steven Whitehouse09010972009-05-20 10:48:47 +0100895 rg_flags &= ~GFS2_RDF_MASK;
Steven Whitehouse1ce97e52009-05-21 15:18:19 +0100896 rgd->rd_flags &= GFS2_RDF_MASK;
897 rgd->rd_flags |= rg_flags;
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000898 rgd->rd_free = be32_to_cpu(str->rg_free);
Steven Whitehouse73f74942008-11-04 10:32:57 +0000899 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
Steven Whitehoused8b71f72008-11-04 10:19:03 +0000900 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100901}
902
Bob Peterson42d52e32008-01-28 18:38:07 -0600903static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100904{
905 struct gfs2_rgrp *str = buf;
906
Steven Whitehouse09010972009-05-20 10:48:47 +0100907 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000908 str->rg_free = cpu_to_be32(rgd->rd_free);
Steven Whitehouse73f74942008-11-04 10:32:57 +0000909 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100910 str->__pad = cpu_to_be32(0);
Steven Whitehoused8b71f72008-11-04 10:19:03 +0000911 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100912 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
913}
914
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500915static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
916{
917 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
918 struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
919
920 if (rgl->rl_flags != str->rg_flags || rgl->rl_free != str->rg_free ||
921 rgl->rl_dinodes != str->rg_dinodes ||
922 rgl->rl_igeneration != str->rg_igeneration)
923 return 0;
924 return 1;
925}
926
927static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
928{
929 const struct gfs2_rgrp *str = buf;
930
931 rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
932 rgl->rl_flags = str->rg_flags;
933 rgl->rl_free = str->rg_free;
934 rgl->rl_dinodes = str->rg_dinodes;
935 rgl->rl_igeneration = str->rg_igeneration;
936 rgl->__pad = 0UL;
937}
938
939static void update_rgrp_lvb_unlinked(struct gfs2_rgrpd *rgd, u32 change)
940{
941 struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
942 u32 unlinked = be32_to_cpu(rgl->rl_unlinked) + change;
943 rgl->rl_unlinked = cpu_to_be32(unlinked);
944}
945
946static u32 count_unlinked(struct gfs2_rgrpd *rgd)
947{
948 struct gfs2_bitmap *bi;
949 const u32 length = rgd->rd_length;
950 const u8 *buffer = NULL;
951 u32 i, goal, count = 0;
952
953 for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
954 goal = 0;
955 buffer = bi->bi_bh->b_data + bi->bi_offset;
956 WARN_ON(!buffer_uptodate(bi->bi_bh));
957 while (goal < bi->bi_len * GFS2_NBBY) {
958 goal = gfs2_bitfit(buffer, bi->bi_len, goal,
959 GFS2_BLKST_UNLINKED);
960 if (goal == BFITNOENT)
961 break;
962 count++;
963 goal++;
964 }
965 }
966
967 return count;
968}
969
970
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971/**
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500972 * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
973 * @rgd: the struct gfs2_rgrpd describing the RG to read in
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 *
975 * Read in all of a Resource Group's header and bitmap blocks.
976 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
977 *
978 * Returns: errno
979 */
980
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500981int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982{
983 struct gfs2_sbd *sdp = rgd->rd_sbd;
984 struct gfs2_glock *gl = rgd->rd_gl;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100985 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 struct gfs2_bitmap *bi;
987 unsigned int x, y;
988 int error;
989
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500990 if (rgd->rd_bits[0].bi_bh != NULL)
991 return 0;
992
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 for (x = 0; x < length; x++) {
994 bi = rgd->rd_bits + x;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100995 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 if (error)
997 goto fail;
998 }
999
1000 for (y = length; y--;) {
1001 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001002 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 if (error)
1004 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001005 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006 GFS2_METATYPE_RG)) {
1007 error = -EIO;
1008 goto fail;
1009 }
1010 }
1011
Bob Petersoncf45b752008-01-31 10:31:39 -06001012 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001013 for (x = 0; x < length; x++)
1014 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
Bob Peterson42d52e32008-01-28 18:38:07 -06001015 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
Steven Whitehouse1ce97e52009-05-21 15:18:19 +01001016 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
Bob Peterson7c9ca622011-08-31 09:53:19 +01001017 rgd->rd_free_clone = rgd->rd_free;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018 }
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001019 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
1020 rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
1021 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
1022 rgd->rd_bits[0].bi_bh->b_data);
1023 }
1024 else if (sdp->sd_args.ar_rgrplvb) {
1025 if (!gfs2_rgrp_lvb_valid(rgd)){
1026 gfs2_consist_rgrpd(rgd);
1027 error = -EIO;
1028 goto fail;
1029 }
1030 if (rgd->rd_rgl->rl_unlinked == 0)
1031 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1032 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 return 0;
1034
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001035fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 while (x--) {
1037 bi = rgd->rd_bits + x;
1038 brelse(bi->bi_bh);
1039 bi->bi_bh = NULL;
1040 gfs2_assert_warn(sdp, !bi->bi_clone);
1041 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042
1043 return error;
1044}
1045
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001046int update_rgrp_lvb(struct gfs2_rgrpd *rgd)
1047{
1048 u32 rl_flags;
1049
1050 if (rgd->rd_flags & GFS2_RDF_UPTODATE)
1051 return 0;
1052
1053 if (be32_to_cpu(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
1054 return gfs2_rgrp_bh_get(rgd);
1055
1056 rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
1057 rl_flags &= ~GFS2_RDF_MASK;
1058 rgd->rd_flags &= GFS2_RDF_MASK;
1059 rgd->rd_flags |= (rl_flags | GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
1060 if (rgd->rd_rgl->rl_unlinked == 0)
1061 rgd->rd_flags &= ~GFS2_RDF_CHECK;
1062 rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
1063 rgd->rd_free_clone = rgd->rd_free;
1064 rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
1065 rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
1066 return 0;
1067}
1068
1069int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
1070{
1071 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
1072 struct gfs2_sbd *sdp = rgd->rd_sbd;
1073
1074 if (gh->gh_flags & GL_SKIP && sdp->sd_args.ar_rgrplvb)
1075 return 0;
1076 return gfs2_rgrp_bh_get((struct gfs2_rgrpd *)gh->gh_gl->gl_object);
1077}
1078
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079/**
Bob Peterson7c9ca622011-08-31 09:53:19 +01001080 * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get()
Bob Peterson886b1412012-04-11 13:03:52 -04001081 * @gh: The glock holder for the resource group
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 *
1083 */
1084
Bob Peterson7c9ca622011-08-31 09:53:19 +01001085void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001086{
Bob Peterson7c9ca622011-08-31 09:53:19 +01001087 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001088 int x, length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 for (x = 0; x < length; x++) {
1091 struct gfs2_bitmap *bi = rgd->rd_bits + x;
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001092 if (bi->bi_bh) {
1093 brelse(bi->bi_bh);
1094 bi->bi_bh = NULL;
1095 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 }
1097
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098}
1099
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001100int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
Bob Peterson7c9ca622011-08-31 09:53:19 +01001101 struct buffer_head *bh,
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001102 const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
Steven Whitehousef15ab562009-02-09 09:25:01 +00001103{
1104 struct super_block *sb = sdp->sd_vfs;
1105 struct block_device *bdev = sb->s_bdev;
1106 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
Martin K. Petersene1defc42009-05-22 17:17:49 -04001107 bdev_logical_block_size(sb->s_bdev);
Steven Whitehousef15ab562009-02-09 09:25:01 +00001108 u64 blk;
Steven Whitehouse64d576b2009-02-12 13:31:58 +00001109 sector_t start = 0;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001110 sector_t nr_sects = 0;
1111 int rv;
1112 unsigned int x;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001113 u32 trimmed = 0;
1114 u8 diff;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001115
1116 for (x = 0; x < bi->bi_len; x++) {
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001117 const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
1118 clone += bi->bi_offset;
1119 clone += x;
1120 if (bh) {
1121 const u8 *orig = bh->b_data + bi->bi_offset + x;
1122 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
1123 } else {
1124 diff = ~(*clone | (*clone >> 1));
1125 }
Steven Whitehousef15ab562009-02-09 09:25:01 +00001126 diff &= 0x55;
1127 if (diff == 0)
1128 continue;
1129 blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
1130 blk *= sects_per_blk; /* convert to sectors */
1131 while(diff) {
1132 if (diff & 1) {
1133 if (nr_sects == 0)
1134 goto start_new_extent;
1135 if ((start + nr_sects) != blk) {
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001136 if (nr_sects >= minlen) {
1137 rv = blkdev_issue_discard(bdev,
1138 start, nr_sects,
1139 GFP_NOFS, 0);
1140 if (rv)
1141 goto fail;
1142 trimmed += nr_sects;
1143 }
Steven Whitehousef15ab562009-02-09 09:25:01 +00001144 nr_sects = 0;
1145start_new_extent:
1146 start = blk;
1147 }
1148 nr_sects += sects_per_blk;
1149 }
1150 diff >>= 2;
1151 blk += sects_per_blk;
1152 }
1153 }
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001154 if (nr_sects >= minlen) {
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02001155 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
Steven Whitehousef15ab562009-02-09 09:25:01 +00001156 if (rv)
1157 goto fail;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001158 trimmed += nr_sects;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001159 }
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001160 if (ptrimmed)
1161 *ptrimmed = trimmed;
1162 return 0;
1163
Steven Whitehousef15ab562009-02-09 09:25:01 +00001164fail:
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001165 if (sdp->sd_args.ar_discard)
1166 fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
Steven Whitehousef15ab562009-02-09 09:25:01 +00001167 sdp->sd_args.ar_discard = 0;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001168 return -EIO;
1169}
1170
1171/**
1172 * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
1173 * @filp: Any file on the filesystem
1174 * @argp: Pointer to the arguments (also used to pass result)
1175 *
1176 * Returns: 0 on success, otherwise error code
1177 */
1178
1179int gfs2_fitrim(struct file *filp, void __user *argp)
1180{
1181 struct inode *inode = filp->f_dentry->d_inode;
1182 struct gfs2_sbd *sdp = GFS2_SB(inode);
1183 struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev);
1184 struct buffer_head *bh;
1185 struct gfs2_rgrpd *rgd;
1186 struct gfs2_rgrpd *rgd_end;
1187 struct gfs2_holder gh;
1188 struct fstrim_range r;
1189 int ret = 0;
1190 u64 amt;
1191 u64 trimmed = 0;
1192 unsigned int x;
1193
1194 if (!capable(CAP_SYS_ADMIN))
1195 return -EPERM;
1196
1197 if (!blk_queue_discard(q))
1198 return -EOPNOTSUPP;
1199
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001200 if (argp == NULL) {
1201 r.start = 0;
1202 r.len = ULLONG_MAX;
1203 r.minlen = 0;
1204 } else if (copy_from_user(&r, argp, sizeof(r)))
1205 return -EFAULT;
1206
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001207 ret = gfs2_rindex_update(sdp);
1208 if (ret)
1209 return ret;
1210
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001211 rgd = gfs2_blk2rgrpd(sdp, r.start, 0);
1212 rgd_end = gfs2_blk2rgrpd(sdp, r.start + r.len, 0);
1213
1214 while (1) {
1215
1216 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1217 if (ret)
1218 goto out;
1219
1220 if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
1221 /* Trim each bitmap in the rgrp */
1222 for (x = 0; x < rgd->rd_length; x++) {
1223 struct gfs2_bitmap *bi = rgd->rd_bits + x;
1224 ret = gfs2_rgrp_send_discards(sdp, rgd->rd_data0, NULL, bi, r.minlen, &amt);
1225 if (ret) {
1226 gfs2_glock_dq_uninit(&gh);
1227 goto out;
1228 }
1229 trimmed += amt;
1230 }
1231
1232 /* Mark rgrp as having been trimmed */
1233 ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
1234 if (ret == 0) {
1235 bh = rgd->rd_bits[0].bi_bh;
1236 rgd->rd_flags |= GFS2_RGF_TRIMMED;
1237 gfs2_trans_add_bh(rgd->rd_gl, bh, 1);
1238 gfs2_rgrp_out(rgd, bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001239 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, bh->b_data);
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001240 gfs2_trans_end(sdp);
1241 }
1242 }
1243 gfs2_glock_dq_uninit(&gh);
1244
1245 if (rgd == rgd_end)
1246 break;
1247
1248 rgd = gfs2_rgrpd_get_next(rgd);
1249 }
1250
1251out:
1252 r.len = trimmed << 9;
1253 if (argp && copy_to_user(argp, &r, sizeof(r)))
1254 return -EFAULT;
1255
1256 return ret;
Steven Whitehousef15ab562009-02-09 09:25:01 +00001257}
1258
David Teiglandb3b94fa2006-01-16 16:50:04 +00001259/**
Bob Peterson8e2e0042012-07-19 08:12:40 -04001260 * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
1261 * @bi: the bitmap with the blocks
1262 * @ip: the inode structure
1263 * @biblk: the 32-bit block number relative to the start of the bitmap
1264 * @amount: the number of blocks to reserve
1265 *
1266 * Returns: NULL - reservation was already taken, so not inserted
1267 * pointer to the inserted reservation
1268 */
1269static struct gfs2_blkreserv *rs_insert(struct gfs2_bitmap *bi,
1270 struct gfs2_inode *ip, u32 biblk,
1271 int amount)
1272{
1273 struct rb_node **newn, *parent = NULL;
1274 int rc;
1275 struct gfs2_blkreserv *rs = ip->i_res;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001276 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001277 u64 fsblock = gfs2_bi2rgd_blk(bi, biblk) + rgd->rd_data0;
1278
1279 spin_lock(&rgd->rd_rsspin);
1280 newn = &rgd->rd_rstree.rb_node;
1281 BUG_ON(!ip->i_res);
1282 BUG_ON(gfs2_rs_active(rs));
1283 /* Figure out where to put new node */
1284 /*BUG_ON(!gfs2_glock_is_locked_by_me(rgd->rd_gl));*/
1285 while (*newn) {
1286 struct gfs2_blkreserv *cur =
1287 rb_entry(*newn, struct gfs2_blkreserv, rs_node);
1288
1289 parent = *newn;
1290 rc = rs_cmp(fsblock, amount, cur);
1291 if (rc > 0)
1292 newn = &((*newn)->rb_right);
1293 else if (rc < 0)
1294 newn = &((*newn)->rb_left);
1295 else {
1296 spin_unlock(&rgd->rd_rsspin);
1297 return NULL; /* reservation already in use */
1298 }
1299 }
1300
1301 /* Do our reservation work */
1302 rs = ip->i_res;
1303 rs->rs_free = amount;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001304 rs->rs_rbm.offset = biblk;
1305 rs->rs_rbm.bi = bi;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001306 rb_link_node(&rs->rs_node, parent, newn);
1307 rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
1308
1309 /* Do our inode accounting for the reservation */
1310 /*BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));*/
1311
1312 /* Do our rgrp accounting for the reservation */
1313 rgd->rd_reserved += amount; /* blocks reserved */
1314 rgd->rd_rs_cnt++; /* number of in-tree reservations */
1315 spin_unlock(&rgd->rd_rsspin);
1316 trace_gfs2_rs(ip, rs, TRACE_RS_INSERT);
1317 return rs;
1318}
1319
1320/**
1321 * unclaimed_blocks - return number of blocks that aren't spoken for
1322 */
1323static u32 unclaimed_blocks(struct gfs2_rgrpd *rgd)
1324{
1325 return rgd->rd_free_clone - rgd->rd_reserved;
1326}
1327
1328/**
1329 * rg_mblk_search - find a group of multiple free blocks
1330 * @rgd: the resource group descriptor
1331 * @rs: the block reservation
1332 * @ip: pointer to the inode for which we're reserving blocks
1333 *
1334 * This is very similar to rgblk_search, except we're looking for whole
1335 * 64-bit words that represent a chunk of 32 free blocks. I'm only focusing
1336 * on aligned dwords for speed's sake.
1337 *
1338 * Returns: 0 if successful or BFITNOENT if there isn't enough free space
1339 */
1340
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001341static int rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip, unsigned requested)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001342{
1343 struct gfs2_bitmap *bi = rgd->rd_bits;
1344 const u32 length = rgd->rd_length;
1345 u32 blk;
1346 unsigned int buf, x, search_bytes;
1347 u8 *buffer = NULL;
1348 u8 *ptr, *end, *nonzero;
1349 u32 goal, rsv_bytes;
1350 struct gfs2_blkreserv *rs;
1351 u32 best_rs_bytes, unclaimed;
1352 int best_rs_blocks;
1353
1354 /* Find bitmap block that contains bits for goal block */
1355 if (rgrp_contains_block(rgd, ip->i_goal))
1356 goal = ip->i_goal - rgd->rd_data0;
1357 else
1358 goal = rgd->rd_last_alloc;
1359 for (buf = 0; buf < length; buf++) {
1360 bi = rgd->rd_bits + buf;
1361 /* Convert scope of "goal" from rgrp-wide to within
1362 found bit block */
1363 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1364 goal -= bi->bi_start * GFS2_NBBY;
1365 goto do_search;
1366 }
1367 }
1368 buf = 0;
1369 goal = 0;
1370
1371do_search:
1372 best_rs_blocks = max_t(int, atomic_read(&ip->i_res->rs_sizehint),
1373 (RGRP_RSRV_MINBLKS * rgd->rd_length));
1374 best_rs_bytes = (best_rs_blocks *
1375 (1 + (RSRV_CONTENTION_FACTOR * rgd->rd_rs_cnt))) /
1376 GFS2_NBBY; /* 1 + is for our not-yet-created reservation */
1377 best_rs_bytes = ALIGN(best_rs_bytes, sizeof(u64));
1378 unclaimed = unclaimed_blocks(rgd);
1379 if (best_rs_bytes * GFS2_NBBY > unclaimed)
1380 best_rs_bytes = unclaimed >> GFS2_BIT_SIZE;
1381
1382 for (x = 0; x <= length; x++) {
1383 bi = rgd->rd_bits + buf;
1384
1385 if (test_bit(GBF_FULL, &bi->bi_flags))
1386 goto skip;
1387
1388 WARN_ON(!buffer_uptodate(bi->bi_bh));
1389 if (bi->bi_clone)
1390 buffer = bi->bi_clone + bi->bi_offset;
1391 else
1392 buffer = bi->bi_bh->b_data + bi->bi_offset;
1393
1394 /* We have to keep the reservations aligned on u64 boundaries
1395 otherwise we could get situations where a byte can't be
1396 used because it's after a reservation, but a free bit still
1397 is within the reservation's area. */
1398 ptr = buffer + ALIGN(goal >> GFS2_BIT_SIZE, sizeof(u64));
1399 end = (buffer + bi->bi_len);
1400 while (ptr < end) {
1401 rsv_bytes = 0;
1402 if ((ptr + best_rs_bytes) <= end)
1403 search_bytes = best_rs_bytes;
1404 else
1405 search_bytes = end - ptr;
1406 BUG_ON(!search_bytes);
1407 nonzero = memchr_inv(ptr, 0, search_bytes);
1408 /* If the lot is all zeroes, reserve the whole size. If
1409 there's enough zeroes to satisfy the request, use
1410 what we can. If there's not enough, keep looking. */
1411 if (nonzero == NULL)
1412 rsv_bytes = search_bytes;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001413 else if ((nonzero - ptr) * GFS2_NBBY >= requested)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001414 rsv_bytes = (nonzero - ptr);
1415
1416 if (rsv_bytes) {
1417 blk = ((ptr - buffer) * GFS2_NBBY);
1418 BUG_ON(blk >= bi->bi_len * GFS2_NBBY);
1419 rs = rs_insert(bi, ip, blk,
1420 rsv_bytes * GFS2_NBBY);
1421 if (IS_ERR(rs))
1422 return PTR_ERR(rs);
1423 if (rs)
1424 return 0;
1425 }
1426 ptr += ALIGN(search_bytes, sizeof(u64));
1427 }
1428skip:
1429 /* Try next bitmap block (wrap back to rgrp header
1430 if at end) */
1431 buf++;
1432 buf %= length;
1433 goal = 0;
1434 }
1435
1436 return BFITNOENT;
1437}
1438
1439/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001440 * try_rgrp_fit - See if a given reservation will fit in a given RG
1441 * @rgd: the RG data
Steven Whitehouse54335b12011-09-01 13:31:59 +01001442 * @ip: the inode
David Teiglandb3b94fa2006-01-16 16:50:04 +00001443 *
1444 * If there's room for the requested blocks to be allocated from the RG:
Bob Peterson8e2e0042012-07-19 08:12:40 -04001445 * This will try to get a multi-block reservation first, and if that doesn't
1446 * fit, it will take what it can.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001447 *
1448 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
1449 */
1450
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001451static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
1452 unsigned requested)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001453{
Steven Whitehouse09010972009-05-20 10:48:47 +01001454 if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
Steven Whitehousea43a4902007-04-02 10:48:17 +01001455 return 0;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001456 /* Look for a multi-block reservation. */
1457 if (unclaimed_blocks(rgd) >= RGRP_RSRV_MINBLKS &&
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001458 rg_mblk_search(rgd, ip, requested) != BFITNOENT)
Bob Peterson7c9ca622011-08-31 09:53:19 +01001459 return 1;
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001460 if (unclaimed_blocks(rgd) >= requested)
Bob Peterson8e2e0042012-07-19 08:12:40 -04001461 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001462
Bob Peterson8e2e0042012-07-19 08:12:40 -04001463 return 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001464}
1465
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466/**
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001467 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
1468 * @rgd: The rgrp
Bob Peterson886b1412012-04-11 13:03:52 -04001469 * @last_unlinked: block address of the last dinode we unlinked
1470 * @skip: block address we should explicitly not unlink
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001471 *
Bob Peterson1a0eae82010-04-14 11:58:16 -04001472 * Returns: 0 if no error
1473 * The inode, if one has been found, in inode.
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001474 */
1475
Steven Whitehouse044b9412010-11-03 20:01:07 +00001476static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001477{
Bob Peterson6760bdc2007-07-24 14:09:32 -05001478 u32 goal = 0, block;
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001479 u64 no_addr;
Bob Peterson5f3eae72007-08-08 16:52:09 -05001480 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehouse044b9412010-11-03 20:01:07 +00001481 struct gfs2_glock *gl;
1482 struct gfs2_inode *ip;
1483 int error;
1484 int found = 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001485 struct gfs2_bitmap *bi;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001486
Steven Whitehouse044b9412010-11-03 20:01:07 +00001487 while (goal < rgd->rd_data) {
Bob Peterson5f3eae72007-08-08 16:52:09 -05001488 down_write(&sdp->sd_log_flush_lock);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001489 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, &bi);
Bob Peterson5f3eae72007-08-08 16:52:09 -05001490 up_write(&sdp->sd_log_flush_lock);
Bob Peterson6760bdc2007-07-24 14:09:32 -05001491 if (block == BFITNOENT)
Bob Peterson24c73872007-07-12 16:58:50 -05001492 break;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001493
1494 block = gfs2_bi2rgd_blk(bi, block);
Bob Peterson6760bdc2007-07-24 14:09:32 -05001495 /* rgblk_search can return a block < goal, so we need to
1496 keep it marching forward. */
1497 no_addr = block + rgd->rd_data0;
Bob Peterson44ad37d2011-03-17 16:19:58 -04001498 goal = max(block + 1, goal + 1);
Bob Peterson6760bdc2007-07-24 14:09:32 -05001499 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001500 continue;
Steven Whitehouse1e19a192009-07-10 21:13:38 +01001501 if (no_addr == skip)
1502 continue;
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001503 *last_unlinked = no_addr;
Steven Whitehouse044b9412010-11-03 20:01:07 +00001504
1505 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
1506 if (error)
1507 continue;
1508
1509 /* If the inode is already in cache, we can ignore it here
1510 * because the existing inode disposal code will deal with
1511 * it when all refs have gone away. Accessing gl_object like
1512 * this is not safe in general. Here it is ok because we do
1513 * not dereference the pointer, and we only need an approx
1514 * answer to whether it is NULL or not.
1515 */
1516 ip = gl->gl_object;
1517
1518 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
1519 gfs2_glock_put(gl);
1520 else
1521 found++;
1522
1523 /* Limit reclaim to sensible number of tasks */
Bob Peterson44ad37d2011-03-17 16:19:58 -04001524 if (found > NR_CPUS)
Steven Whitehouse044b9412010-11-03 20:01:07 +00001525 return;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001526 }
1527
1528 rgd->rd_flags &= ~GFS2_RDF_CHECK;
Steven Whitehouse044b9412010-11-03 20:01:07 +00001529 return;
Steven Whitehousec8cdf472007-06-08 10:05:33 +01001530}
1531
1532/**
Bob Peterson666d1d82012-06-13 23:03:56 -04001533 * gfs2_inplace_reserve - Reserve space in the filesystem
David Teiglandb3b94fa2006-01-16 16:50:04 +00001534 * @ip: the inode to reserve space for
Bob Peterson666d1d82012-06-13 23:03:56 -04001535 * @requested: the number of blocks to be reserved
David Teiglandb3b94fa2006-01-16 16:50:04 +00001536 *
1537 * Returns: errno
1538 */
1539
Bob Peterson666d1d82012-06-13 23:03:56 -04001540int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001542 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001543 struct gfs2_rgrpd *begin = NULL;
Bob Peterson564e12b2011-11-21 13:36:17 -05001544 struct gfs2_blkreserv *rs = ip->i_res;
Bob Peterson666d1d82012-06-13 23:03:56 -04001545 int error = 0, rg_locked, flags = LM_FLAG_TRY;
1546 u64 last_unlinked = NO_BLOCK;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001547 int loops = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001549 if (sdp->sd_args.ar_rgrplvb)
1550 flags |= GL_SKIP;
Bob Peterson666d1d82012-06-13 23:03:56 -04001551 if (gfs2_assert_warn(sdp, requested)) {
1552 error = -EINVAL;
1553 goto out;
1554 }
Bob Peterson8e2e0042012-07-19 08:12:40 -04001555 if (gfs2_rs_active(rs)) {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001556 begin = rs->rs_rbm.rgd;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001557 flags = 0; /* Yoda: Do or do not. There is no try */
1558 } else if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal)) {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001559 rs->rs_rbm.rgd = begin = ip->i_rgd;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001560 } else {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001561 rs->rs_rbm.rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001562 }
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001563 if (rs->rs_rbm.rgd == NULL)
Bob Peterson7c9ca622011-08-31 09:53:19 +01001564 return -EBADSLT;
1565
1566 while (loops < 3) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001567 rg_locked = 0;
1568
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001569 if (gfs2_glock_is_locked_by_me(rs->rs_rbm.rgd->rd_gl)) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001570 rg_locked = 1;
1571 error = 0;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001572 } else if (!loops && !gfs2_rs_active(rs) &&
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001573 rs->rs_rbm.rgd->rd_rs_cnt > RGRP_RSRV_MAX_CONTENDERS) {
Bob Peterson8e2e0042012-07-19 08:12:40 -04001574 /* If the rgrp already is maxed out for contenders,
1575 we can eliminate it as a "first pass" without even
1576 requesting the rgrp glock. */
1577 error = GLR_TRYFAILED;
Abhijith Das292c8c12007-11-29 14:13:54 -06001578 } else {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001579 error = gfs2_glock_nq_init(rs->rs_rbm.rgd->rd_gl,
Bob Peterson8e2e0042012-07-19 08:12:40 -04001580 LM_ST_EXCLUSIVE, flags,
1581 &rs->rs_rgd_gh);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001582 if (!error && sdp->sd_args.ar_rgrplvb) {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001583 error = update_rgrp_lvb(rs->rs_rbm.rgd);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001584 if (error) {
1585 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
1586 return error;
1587 }
1588 }
Abhijith Das292c8c12007-11-29 14:13:54 -06001589 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001590 switch (error) {
1591 case 0:
Bob Peterson8e2e0042012-07-19 08:12:40 -04001592 if (gfs2_rs_active(rs)) {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001593 if (unclaimed_blocks(rs->rs_rbm.rgd) +
Steven Whitehouse71f890f2012-07-30 14:53:19 +01001594 rs->rs_free >= requested) {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001595 ip->i_rgd = rs->rs_rbm.rgd;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001596 return 0;
1597 }
1598 /* We have a multi-block reservation, but the
1599 rgrp doesn't have enough free blocks to
1600 satisfy the request. Free the reservation
1601 and look for a suitable rgrp. */
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001602 gfs2_rs_deltree(ip, rs);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001603 }
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001604 if (try_rgrp_fit(rs->rs_rbm.rgd, ip, requested)) {
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001605 if (sdp->sd_args.ar_rgrplvb)
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001606 gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
1607 ip->i_rgd = rs->rs_rbm.rgd;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001608 return 0;
Steven Whitehouse54335b12011-09-01 13:31:59 +01001609 }
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001610 if (rs->rs_rbm.rgd->rd_flags & GFS2_RDF_CHECK) {
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001611 if (sdp->sd_args.ar_rgrplvb)
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001612 gfs2_rgrp_bh_get(rs->rs_rbm.rgd);
1613 try_rgrp_unlink(rs->rs_rbm.rgd, &last_unlinked,
Bob Peterson666d1d82012-06-13 23:03:56 -04001614 ip->i_no_addr);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001615 }
Abhijith Das292c8c12007-11-29 14:13:54 -06001616 if (!rg_locked)
Bob Peterson564e12b2011-11-21 13:36:17 -05001617 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
Steven Whitehouse9cabcdb2008-07-10 15:54:12 +01001618 /* fall through */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001619 case GLR_TRYFAILED:
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001620 rs->rs_rbm.rgd = gfs2_rgrpd_get_next(rs->rs_rbm.rgd);
1621 rs->rs_rbm.rgd = rs->rs_rbm.rgd ? : begin; /* if NULL, wrap */
1622 if (rs->rs_rbm.rgd != begin) /* If we didn't wrap */
Bob Peterson666d1d82012-06-13 23:03:56 -04001623 break;
1624
1625 flags &= ~LM_FLAG_TRY;
1626 loops++;
1627 /* Check that fs hasn't grown if writing to rindex */
1628 if (ip == GFS2_I(sdp->sd_rindex) &&
1629 !sdp->sd_rindex_uptodate) {
1630 error = gfs2_ri_update(ip);
1631 if (error)
1632 goto out;
1633 } else if (loops == 2)
1634 /* Flushing the log may release space */
1635 gfs2_log_flush(sdp, NULL);
Bob Peterson7c9ca622011-08-31 09:53:19 +01001636 break;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001637 default:
Bob Peterson666d1d82012-06-13 23:03:56 -04001638 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001639 }
1640 }
Bob Peterson666d1d82012-06-13 23:03:56 -04001641 error = -ENOSPC;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001642
Bob Peterson564e12b2011-11-21 13:36:17 -05001643out:
Steven Whitehouse9ae32422011-09-20 12:16:11 +01001644 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001645}
1646
1647/**
1648 * gfs2_inplace_release - release an inplace reservation
1649 * @ip: the inode the reservation was taken out on
1650 *
1651 * Release a reservation made by gfs2_inplace_reserve().
1652 */
1653
1654void gfs2_inplace_release(struct gfs2_inode *ip)
1655{
Bob Peterson564e12b2011-11-21 13:36:17 -05001656 struct gfs2_blkreserv *rs = ip->i_res;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001657
Bob Peterson564e12b2011-11-21 13:36:17 -05001658 if (rs->rs_rgd_gh.gh_gl)
1659 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001660}
1661
1662/**
1663 * gfs2_get_block_type - Check a block in a RG is of given type
1664 * @rgd: the resource group holding the block
1665 * @block: the block number
1666 *
1667 * Returns: The block type (GFS2_BLKST_*)
1668 */
1669
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001670static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001671{
1672 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001673 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001674 unsigned int buf;
1675 unsigned char type;
1676
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001677 length = rgd->rd_length;
1678 rgrp_block = block - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679
1680 for (buf = 0; buf < length; buf++) {
1681 bi = rgd->rd_bits + buf;
1682 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1683 break;
1684 }
1685
1686 gfs2_assert(rgd->rd_sbd, buf < length);
1687 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1688
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001689 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001690 bi->bi_len, buf_block);
1691
1692 return type;
1693}
1694
1695/**
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001696 * rgblk_search - find a block in @state
David Teiglandb3b94fa2006-01-16 16:50:04 +00001697 * @rgd: the resource group descriptor
1698 * @goal: the goal block within the RG (start here to search for avail block)
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001699 * @state: GFS2_BLKST_XXX the before-allocation state to find
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001700 * @rbi: address of the pointer to the bitmap containing the block found
David Teiglandb3b94fa2006-01-16 16:50:04 +00001701 *
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001702 * Walk rgrp's bitmap to find bits that represent a block in @state.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001703 *
1704 * This function never fails, because we wouldn't call it unless we
1705 * know (from reservation results, etc.) that a block is available.
1706 *
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001707 * Scope of @goal is just within rgrp, not the whole filesystem.
1708 * Scope of @returned block is just within bitmap, not the whole filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001709 *
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001710 * Returns: the block number found relative to the bitmap rbi
David Teiglandb3b94fa2006-01-16 16:50:04 +00001711 */
1712
Bob Peterson886b1412012-04-11 13:03:52 -04001713static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, unsigned char state,
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001714 struct gfs2_bitmap **rbi)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001715{
1716 struct gfs2_bitmap *bi = NULL;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001717 const u32 length = rgd->rd_length;
Bob Peterson9598d252012-04-12 08:41:43 -04001718 u32 biblk = BFITNOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001719 unsigned int buf, x;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001720 const u8 *buffer = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001721
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001722 *rbi = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001723 /* Find bitmap block that contains bits for goal block */
1724 for (buf = 0; buf < length; buf++) {
1725 bi = rgd->rd_bits + buf;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001726 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1727 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1728 goal -= bi->bi_start * GFS2_NBBY;
1729 goto do_search;
1730 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001731 }
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001732 buf = 0;
1733 goal = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001734
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001735do_search:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001736 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1737 "x <= length", instead of "x < length", because we typically start
1738 the search in the middle of a bit block, but if we can't find an
1739 allocatable block anywhere else, we want to be able wrap around and
1740 search in the first part of our first-searched bit block. */
1741 for (x = 0; x <= length; x++) {
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001742 bi = rgd->rd_bits + buf;
1743
1744 if (test_bit(GBF_FULL, &bi->bi_flags) &&
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001745 (state == GFS2_BLKST_FREE))
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001746 goto skip;
1747
Bob Peterson5f3eae72007-08-08 16:52:09 -05001748 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1749 bitmaps, so we must search the originals for that. */
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001750 buffer = bi->bi_bh->b_data + bi->bi_offset;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001751 WARN_ON(!buffer_uptodate(bi->bi_bh));
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001752 if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
Steven Whitehouse110acf32008-01-29 13:30:20 +00001753 buffer = bi->bi_clone + bi->bi_offset;
1754
Bob Peterson8e2e0042012-07-19 08:12:40 -04001755 while (1) {
1756 struct gfs2_blkreserv *rs;
1757 u32 rgblk;
1758
1759 biblk = gfs2_bitfit(buffer, bi->bi_len, goal, state);
1760 if (biblk == BFITNOENT)
1761 break;
1762 /* Check if this block is reserved() */
1763 rgblk = gfs2_bi2rgd_blk(bi, biblk);
1764 rs = rs_find(rgd, rgblk);
1765 if (rs == NULL)
1766 break;
1767
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001768 BUG_ON(rs->rs_rbm.bi != bi);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001769 biblk = BFITNOENT;
1770 /* This should jump to the first block after the
1771 reservation. */
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001772 goal = rs->rs_rbm.offset + rs->rs_free;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001773 if (goal >= bi->bi_len * GFS2_NBBY)
1774 break;
1775 }
Bob Peterson9598d252012-04-12 08:41:43 -04001776 if (biblk != BFITNOENT)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001777 break;
1778
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001779 if ((goal == 0) && (state == GFS2_BLKST_FREE))
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001780 set_bit(GBF_FULL, &bi->bi_flags);
1781
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782 /* Try next bitmap block (wrap back to rgrp header if at end) */
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001783skip:
1784 buf++;
1785 buf %= length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001786 goal = 0;
1787 }
1788
Bob Peterson9598d252012-04-12 08:41:43 -04001789 if (biblk != BFITNOENT)
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001790 *rbi = bi;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001791
Bob Peterson9598d252012-04-12 08:41:43 -04001792 return biblk;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001793}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001794
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001795/**
1796 * gfs2_alloc_extent - allocate an extent from a given bitmap
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001797 * @rbm: the resource group information
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001798 * @dinode: TRUE if the first block we allocate is for a dinode
1799 * @n: The extent length
1800 *
1801 * Add the found bitmap buffer to the transaction.
1802 * Set the found bits to @new_state to change block's allocation state.
1803 * Returns: starting block number of the extent (fs scope)
1804 */
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001805static u64 gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
1806 unsigned int *n)
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001807{
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001808 struct gfs2_rgrpd *rgd = rbm->rgd;
1809 struct gfs2_bitmap *bi = rbm->bi;
1810 u32 blk = rbm->offset;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001811 const unsigned int elen = *n;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001812 u32 goal, rgblk;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001813 const u8 *buffer = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001814 struct gfs2_blkreserv *rs;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001815
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001816 *n = 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001817 buffer = bi->bi_bh->b_data + bi->bi_offset;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001818 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Bob Peterson06344b92012-04-26 12:44:35 -04001819 gfs2_setbit(rgd, bi->bi_clone, bi, blk,
1820 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001821 (*n)++;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001822 goal = blk;
1823 while (*n < elen) {
1824 goal++;
1825 if (goal >= (bi->bi_len * GFS2_NBBY))
1826 break;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001827 rgblk = gfs2_bi2rgd_blk(bi, goal);
1828 rs = rs_find(rgd, rgblk);
1829 if (rs) /* Oops, we bumped into someone's reservation */
1830 break;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001831 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1832 GFS2_BLKST_FREE)
1833 break;
Bob Peterson06344b92012-04-26 12:44:35 -04001834 gfs2_setbit(rgd, bi->bi_clone, bi, goal, GFS2_BLKST_USED);
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001835 (*n)++;
1836 }
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001837 blk = gfs2_bi2rgd_blk(bi, blk);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001838 rgd->rd_last_alloc = blk + *n - 1;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001839 return rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001840}
1841
1842/**
1843 * rgblk_free - Change alloc state of given block(s)
1844 * @sdp: the filesystem
1845 * @bstart: the start of a run of blocks to free
1846 * @blen: the length of the block run (all must lie within ONE RG!)
1847 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1848 *
1849 * Returns: Resource group containing the block(s)
1850 */
1851
Steven Whitehousecd915492006-09-04 12:49:07 -04001852static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1853 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001854{
1855 struct gfs2_rgrpd *rgd;
1856 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001857 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001858 unsigned int buf;
1859
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001860 rgd = gfs2_blk2rgrpd(sdp, bstart, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001861 if (!rgd) {
1862 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001863 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001864 return NULL;
1865 }
1866
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001867 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001868
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001869 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001870
1871 while (blen--) {
1872 for (buf = 0; buf < length; buf++) {
1873 bi = rgd->rd_bits + buf;
1874 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1875 break;
1876 }
1877
1878 gfs2_assert(rgd->rd_sbd, buf < length);
1879
1880 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1881 rgrp_blk++;
1882
1883 if (!bi->bi_clone) {
1884 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001885 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001886 memcpy(bi->bi_clone + bi->bi_offset,
1887 bi->bi_bh->b_data + bi->bi_offset,
1888 bi->bi_len);
1889 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001890 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Bob Peterson06344b92012-04-26 12:44:35 -04001891 gfs2_setbit(rgd, NULL, bi, buf_blk, new_state);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001892 }
1893
1894 return rgd;
1895}
1896
1897/**
Steven Whitehouse09010972009-05-20 10:48:47 +01001898 * gfs2_rgrp_dump - print out an rgrp
1899 * @seq: The iterator
1900 * @gl: The glock in question
David Teiglandb3b94fa2006-01-16 16:50:04 +00001901 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001902 */
1903
Steven Whitehouse09010972009-05-20 10:48:47 +01001904int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
1905{
Bob Peterson8e2e0042012-07-19 08:12:40 -04001906 struct gfs2_rgrpd *rgd = gl->gl_object;
1907 struct gfs2_blkreserv *trs;
1908 const struct rb_node *n;
1909
Steven Whitehouse09010972009-05-20 10:48:47 +01001910 if (rgd == NULL)
1911 return 0;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001912 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u r:%u\n",
Steven Whitehouse09010972009-05-20 10:48:47 +01001913 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
Bob Peterson8e2e0042012-07-19 08:12:40 -04001914 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
1915 rgd->rd_reserved);
1916 spin_lock(&rgd->rd_rsspin);
1917 for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
1918 trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
1919 dump_rs(seq, trs);
1920 }
1921 spin_unlock(&rgd->rd_rsspin);
Steven Whitehouse09010972009-05-20 10:48:47 +01001922 return 0;
1923}
1924
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01001925static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
1926{
1927 struct gfs2_sbd *sdp = rgd->rd_sbd;
1928 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
Steven Whitehouse86d00632009-09-14 09:50:57 +01001929 (unsigned long long)rgd->rd_addr);
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01001930 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
1931 gfs2_rgrp_dump(NULL, rgd->rd_gl);
1932 rgd->rd_flags |= GFS2_RDF_ERROR;
1933}
1934
Steven Whitehouse09010972009-05-20 10:48:47 +01001935/**
Bob Peterson8e2e0042012-07-19 08:12:40 -04001936 * claim_reserved_blks - Claim previously reserved blocks
1937 * @ip: the inode that's claiming the reservation
1938 * @dinode: 1 if this block is a dinode block, otherwise data block
1939 * @nblocks: desired extent length
1940 *
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001941 * Lay claim to previously reserved blocks.
Bob Peterson8e2e0042012-07-19 08:12:40 -04001942 * Returns: Starting block number of the blocks claimed.
1943 * Sets *nblocks to the actual extent length allocated.
1944 */
1945static u64 claim_reserved_blks(struct gfs2_inode *ip, bool dinode,
1946 unsigned int *nblocks)
1947{
1948 struct gfs2_blkreserv *rs = ip->i_res;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001949 struct gfs2_rgrpd *rgd = rs->rs_rbm.rgd;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001950 struct gfs2_bitmap *bi;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001951 u64 start_block = gfs2_rbm_to_block(&rs->rs_rbm);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001952 const unsigned int elen = *nblocks;
1953
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001954 bi = rs->rs_rbm.bi;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001955 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1956
1957 for (*nblocks = 0; *nblocks < elen && rs->rs_free; (*nblocks)++) {
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001958 if (gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001959 bi->bi_len, rs->rs_rbm.offset) != GFS2_BLKST_FREE)
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001960 break;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001961 gfs2_setbit(rgd, bi->bi_clone, bi, rs->rs_rbm.offset,
Bob Peterson8e2e0042012-07-19 08:12:40 -04001962 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001963 rs->rs_rbm.offset++;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001964 rs->rs_free--;
1965
1966 BUG_ON(!rgd->rd_reserved);
1967 rgd->rd_reserved--;
1968 dinode = false;
Bob Peterson8e2e0042012-07-19 08:12:40 -04001969 }
1970
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001971 trace_gfs2_rs(ip, rs, TRACE_RS_CLAIM);
1972 if (!rs->rs_free || *nblocks != elen)
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001973 gfs2_rs_deltree(ip, rs);
Steven Whitehouse62e252e2012-07-30 11:06:08 +01001974
Bob Peterson8e2e0042012-07-19 08:12:40 -04001975 return start_block;
1976}
1977
1978/**
Bob Peterson6e87ed02011-11-18 10:58:32 -05001979 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
Steven Whitehouse09010972009-05-20 10:48:47 +01001980 * @ip: the inode to allocate the block for
1981 * @bn: Used to return the starting block number
Bob Peterson8e2e0042012-07-19 08:12:40 -04001982 * @nblocks: requested number of blocks/extent length (value/result)
Bob Peterson6e87ed02011-11-18 10:58:32 -05001983 * @dinode: 1 if we're allocating a dinode block, else 0
Bob Peterson3c5d7852011-11-14 11:17:08 -05001984 * @generation: the generation number of the inode
Steven Whitehouse09010972009-05-20 10:48:47 +01001985 *
1986 * Returns: 0 or error
1987 */
1988
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001989int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
Bob Peterson6e87ed02011-11-18 10:58:32 -05001990 bool dinode, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001991{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001992 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehoused9ba7612009-04-23 08:59:41 +01001993 struct buffer_head *dibh;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001994 struct gfs2_rbm rbm = { .rgd = ip->i_rgd, };
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001995 unsigned int ndata;
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01001996 u32 goal; /* block, within the rgrp scope */
Bob Peterson3c5d7852011-11-14 11:17:08 -05001997 u64 block; /* block, within the file system scope */
Steven Whitehoused9ba7612009-04-23 08:59:41 +01001998 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001999
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002000 /* If we have a reservation, claim blocks from it. */
Bob Peterson8e2e0042012-07-19 08:12:40 -04002001 if (gfs2_rs_active(ip->i_res)) {
2002 BUG_ON(!ip->i_res->rs_free);
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002003 rbm.rgd = ip->i_res->rs_rbm.rgd;
Bob Peterson8e2e0042012-07-19 08:12:40 -04002004 block = claim_reserved_blks(ip, dinode, nblocks);
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002005 if (*nblocks)
2006 goto found_blocks;
Bob Peterson8e2e0042012-07-19 08:12:40 -04002007 }
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002008
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002009 if (!dinode && rgrp_contains_block(rbm.rgd, ip->i_goal))
2010 goal = ip->i_goal - rbm.rgd->rd_data0;
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002011 else
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002012 goal = rbm.rgd->rd_last_alloc;
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002013
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002014 rbm.offset = rgblk_search(rbm.rgd, goal, GFS2_BLKST_FREE, &rbm.bi);
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002015
2016 /* Since all blocks are reserved in advance, this shouldn't happen */
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002017 if (rbm.offset == BFITNOENT) {
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002018 printk(KERN_WARNING "BFITNOENT, nblocks=%u\n", *nblocks);
2019 printk(KERN_WARNING "FULL=%d\n",
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002020 test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags));
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002021 goto rgrp_error;
2022 }
2023
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002024 block = gfs2_alloc_extent(&rbm, dinode, nblocks);
Steven Whitehouse62e252e2012-07-30 11:06:08 +01002025found_blocks:
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002026 ndata = *nblocks;
2027 if (dinode)
2028 ndata--;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05002029
Bob Peterson3c5d7852011-11-14 11:17:08 -05002030 if (!dinode) {
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002031 ip->i_goal = block + ndata - 1;
Bob Peterson3c5d7852011-11-14 11:17:08 -05002032 error = gfs2_meta_inode_buffer(ip, &dibh);
2033 if (error == 0) {
2034 struct gfs2_dinode *di =
2035 (struct gfs2_dinode *)dibh->b_data;
2036 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
2037 di->di_goal_meta = di->di_goal_data =
2038 cpu_to_be64(ip->i_goal);
2039 brelse(dibh);
2040 }
Steven Whitehoused9ba7612009-04-23 08:59:41 +01002041 }
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002042 if (rbm.rgd->rd_free < *nblocks) {
Bob Peterson8e2e0042012-07-19 08:12:40 -04002043 printk(KERN_WARNING "nblocks=%u\n", *nblocks);
Steven Whitehouse09010972009-05-20 10:48:47 +01002044 goto rgrp_error;
Bob Peterson8e2e0042012-07-19 08:12:40 -04002045 }
Steven Whitehouse09010972009-05-20 10:48:47 +01002046
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002047 rbm.rgd->rd_free -= *nblocks;
Bob Peterson3c5d7852011-11-14 11:17:08 -05002048 if (dinode) {
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002049 rbm.rgd->rd_dinodes++;
2050 *generation = rbm.rgd->rd_igeneration++;
Bob Peterson3c5d7852011-11-14 11:17:08 -05002051 if (*generation == 0)
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002052 *generation = rbm.rgd->rd_igeneration++;
Bob Peterson3c5d7852011-11-14 11:17:08 -05002053 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002054
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002055 gfs2_trans_add_bh(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh, 1);
2056 gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
2057 gfs2_rgrp_ondisk2lvb(rbm.rgd->rd_rgl, rbm.rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002058
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002059 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
Bob Peterson3c5d7852011-11-14 11:17:08 -05002060 if (dinode)
2061 gfs2_trans_add_unrevoke(sdp, block, 1);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00002062
2063 /*
2064 * This needs reviewing to see why we cannot do the quota change
2065 * at this point in the dinode case.
2066 */
2067 if (ndata)
2068 gfs2_quota_change(ip, ndata, ip->i_inode.i_uid,
Bob Peterson3c5d7852011-11-14 11:17:08 -05002069 ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002070
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002071 rbm.rgd->rd_free_clone -= *nblocks;
2072 trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
Bob Peterson6e87ed02011-11-18 10:58:32 -05002073 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01002074 *bn = block;
2075 return 0;
2076
2077rgrp_error:
Steven Whitehouse4a993fb2012-07-31 15:21:20 +01002078 gfs2_rgrp_error(rbm.rgd);
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01002079 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002080}
2081
2082/**
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002083 * __gfs2_free_blocks - free a contiguous run of block(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002084 * @ip: the inode these blocks are being freed from
2085 * @bstart: first block of a run of contiguous blocks
2086 * @blen: the length of the block run
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002087 * @meta: 1 if the blocks represent metadata
David Teiglandb3b94fa2006-01-16 16:50:04 +00002088 *
2089 */
2090
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002091void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002092{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002093 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002094 struct gfs2_rgrpd *rgd;
2095
2096 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
2097 if (!rgd)
2098 return;
Bob Peterson41db1ab2012-05-09 12:11:35 -04002099 trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
Steven Whitehousecfc8b542008-11-04 10:25:13 +00002100 rgd->rd_free += blen;
Steven Whitehouse66fc0612012-02-08 12:58:32 +00002101 rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00002102 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06002103 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05002104 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002105
Steven Whitehouse6d3117b2011-05-21 14:05:58 +01002106 /* Directories keep their data in the metadata address space */
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002107 if (meta || ip->i_depth)
Steven Whitehouse6d3117b2011-05-21 14:05:58 +01002108 gfs2_meta_wipe(ip, bstart, blen);
Bob Peterson4c16c362011-02-23 16:11:33 -05002109}
David Teiglandb3b94fa2006-01-16 16:50:04 +00002110
Bob Peterson4c16c362011-02-23 16:11:33 -05002111/**
Bob Peterson4c16c362011-02-23 16:11:33 -05002112 * gfs2_free_meta - free a contiguous run of data block(s)
2113 * @ip: the inode these blocks are being freed from
2114 * @bstart: first block of a run of contiguous blocks
2115 * @blen: the length of the block run
2116 *
2117 */
2118
Steven Whitehousecd915492006-09-04 12:49:07 -04002119void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002120{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002121 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002122
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05002123 __gfs2_free_blocks(ip, bstart, blen, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002124 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05002125 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002126}
2127
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002128void gfs2_unlink_di(struct inode *inode)
2129{
2130 struct gfs2_inode *ip = GFS2_I(inode);
2131 struct gfs2_sbd *sdp = GFS2_SB(inode);
2132 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01002133 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002134
2135 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
2136 if (!rgd)
2137 return;
Bob Peterson41db1ab2012-05-09 12:11:35 -04002138 trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002139 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06002140 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05002141 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2142 update_rgrp_lvb_unlinked(rgd, 1);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002143}
2144
Steven Whitehousecd915492006-09-04 12:49:07 -04002145static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002146{
2147 struct gfs2_sbd *sdp = rgd->rd_sbd;
2148 struct gfs2_rgrpd *tmp_rgd;
2149
2150 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
2151 if (!tmp_rgd)
2152 return;
2153 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
2154
Steven Whitehouse73f74942008-11-04 10:32:57 +00002155 if (!rgd->rd_dinodes)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002156 gfs2_consist_rgrpd(rgd);
Steven Whitehouse73f74942008-11-04 10:32:57 +00002157 rgd->rd_dinodes--;
Steven Whitehousecfc8b542008-11-04 10:25:13 +00002158 rgd->rd_free++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002159
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00002160 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06002161 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Benjamin Marzinski90306c42012-05-29 23:01:09 -05002162 gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, rgd->rd_bits[0].bi_bh->b_data);
2163 update_rgrp_lvb_unlinked(rgd, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002164
2165 gfs2_statfs_change(sdp, 0, +1, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002166}
2167
David Teiglandb3b94fa2006-01-16 16:50:04 +00002168
2169void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
2170{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01002171 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Bob Peterson41db1ab2012-05-09 12:11:35 -04002172 trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
Steven Whitehouse2933f922006-11-01 13:23:29 -05002173 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01002174 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002175}
2176
2177/**
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002178 * gfs2_check_blk_type - Check the type of a block
2179 * @sdp: The superblock
2180 * @no_addr: The block number to check
2181 * @type: The block type we are looking for
2182 *
2183 * Returns: 0 if the block type matches the expected type
2184 * -ESTALE if it doesn't match
2185 * or -ve errno if something went wrong while checking
2186 */
2187
2188int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
2189{
2190 struct gfs2_rgrpd *rgd;
Steven Whitehouse8339ee52011-08-31 16:38:29 +01002191 struct gfs2_holder rgd_gh;
Bob Peterson58884c42012-03-05 10:19:35 -05002192 int error = -EINVAL;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002193
Steven Whitehouse66fc0612012-02-08 12:58:32 +00002194 rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002195 if (!rgd)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01002196 goto fail;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002197
2198 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
2199 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01002200 goto fail;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002201
2202 if (gfs2_get_block_type(rgd, no_addr) != type)
2203 error = -ESTALE;
2204
2205 gfs2_glock_dq_uninit(&rgd_gh);
Steven Whitehouseacf7e242009-09-08 18:00:30 +01002206fail:
2207 return error;
2208}
2209
2210/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00002211 * gfs2_rlist_add - add a RG to a list of RGs
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002212 * @ip: the inode
David Teiglandb3b94fa2006-01-16 16:50:04 +00002213 * @rlist: the list of resource groups
2214 * @block: the block
2215 *
2216 * Figure out what RG a block belongs to and add that RG to the list
2217 *
2218 * FIXME: Don't use NOFAIL
2219 *
2220 */
2221
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002222void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04002223 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002224{
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002225 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002226 struct gfs2_rgrpd *rgd;
2227 struct gfs2_rgrpd **tmp;
2228 unsigned int new_space;
2229 unsigned int x;
2230
2231 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
2232 return;
2233
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002234 if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block))
2235 rgd = ip->i_rgd;
2236 else
Steven Whitehouse66fc0612012-02-08 12:58:32 +00002237 rgd = gfs2_blk2rgrpd(sdp, block, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002238 if (!rgd) {
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002239 fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002240 return;
2241 }
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002242 ip->i_rgd = rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002243
2244 for (x = 0; x < rlist->rl_rgrps; x++)
2245 if (rlist->rl_rgd[x] == rgd)
2246 return;
2247
2248 if (rlist->rl_rgrps == rlist->rl_space) {
2249 new_space = rlist->rl_space + 10;
2250
2251 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04002252 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002253
2254 if (rlist->rl_rgd) {
2255 memcpy(tmp, rlist->rl_rgd,
2256 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
2257 kfree(rlist->rl_rgd);
2258 }
2259
2260 rlist->rl_space = new_space;
2261 rlist->rl_rgd = tmp;
2262 }
2263
2264 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
2265}
2266
2267/**
2268 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
2269 * and initialize an array of glock holders for them
2270 * @rlist: the list of resource groups
2271 * @state: the lock state to acquire the RG lock in
David Teiglandb3b94fa2006-01-16 16:50:04 +00002272 *
2273 * FIXME: Don't use NOFAIL
2274 *
2275 */
2276
Bob Petersonfe6c9912008-01-28 11:13:02 -06002277void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002278{
2279 unsigned int x;
2280
2281 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04002282 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002283 for (x = 0; x < rlist->rl_rgrps; x++)
2284 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
Bob Petersonfe6c9912008-01-28 11:13:02 -06002285 state, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002286 &rlist->rl_ghs[x]);
2287}
2288
2289/**
2290 * gfs2_rlist_free - free a resource group list
2291 * @list: the list of resource groups
2292 *
2293 */
2294
2295void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
2296{
2297 unsigned int x;
2298
2299 kfree(rlist->rl_rgd);
2300
2301 if (rlist->rl_ghs) {
2302 for (x = 0; x < rlist->rl_rgrps; x++)
2303 gfs2_holder_uninit(&rlist->rl_ghs[x]);
2304 kfree(rlist->rl_ghs);
Bob Peterson8e2e0042012-07-19 08:12:40 -04002305 rlist->rl_ghs = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002306 }
2307}
2308