blob: 49ada95209d0108e333ddd164b44534edc8fa86b [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 Peterson1f466a42008-03-10 18:17:47 -050038#if BITS_PER_LONG == 32
39#define LBITMASK (0x55555555UL)
40#define LBITSKIP55 (0x55555555UL)
41#define LBITSKIP00 (0x00000000UL)
42#else
43#define LBITMASK (0x5555555555555555UL)
44#define LBITSKIP55 (0x5555555555555555UL)
45#define LBITSKIP00 (0x0000000000000000UL)
46#endif
47
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040048/*
49 * These routines are used by the resource group routines (rgrp.c)
50 * to keep track of block allocation. Each block is represented by two
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040051 * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
52 *
53 * 0 = Free
54 * 1 = Used (not metadata)
55 * 2 = Unlinked (still in use) inode
56 * 3 = Used (metadata)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040057 */
58
59static const char valid_change[16] = {
60 /* current */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040061 /* n */ 0, 1, 1, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040062 /* e */ 1, 0, 0, 0,
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040063 /* w */ 0, 0, 0, 1,
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040064 1, 0, 0, 0
65};
66
Steven Whitehousec8cdf472007-06-08 10:05:33 +010067static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
Steven Whitehouse6a8099e2011-11-22 12:18:51 +000068 unsigned char old_state,
Bob Petersonb3e47ca2011-11-21 11:47:08 -050069 struct gfs2_bitmap **rbi);
Steven Whitehousec8cdf472007-06-08 10:05:33 +010070
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040071/**
72 * gfs2_setbit - Set a bit in the bitmaps
73 * @buffer: the buffer that holds the bitmaps
74 * @buflen: the length (in bytes) of the buffer
75 * @block: the block to set
76 * @new_state: the new state of the block
77 *
78 */
79
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000080static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
81 unsigned char *buf2, unsigned int offset,
Bob Peterson95c8e172011-03-22 10:49:12 -040082 struct gfs2_bitmap *bi, u32 block,
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000083 unsigned char new_state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040084{
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000085 unsigned char *byte1, *byte2, *end, cur_state;
Bob Peterson95c8e172011-03-22 10:49:12 -040086 unsigned int buflen = bi->bi_len;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000087 const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040088
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000089 byte1 = buf1 + offset + (block / GFS2_NBBY);
90 end = buf1 + offset + buflen;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040091
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000092 BUG_ON(byte1 >= end);
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040093
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000094 cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -040095
Steven Whitehouseb45e41d2008-02-06 10:11:15 +000096 if (unlikely(!valid_change[new_state * 4 + cur_state])) {
Bob Peterson95c8e172011-03-22 10:49:12 -040097 printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, "
98 "new_state=%d\n",
99 (unsigned long long)block, cur_state, new_state);
100 printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n",
101 (unsigned long long)rgd->rd_addr,
102 (unsigned long)bi->bi_start);
103 printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n",
104 (unsigned long)bi->bi_offset,
105 (unsigned long)bi->bi_len);
106 dump_stack();
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400107 gfs2_consist_rgrpd(rgd);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000108 return;
109 }
110 *byte1 ^= (cur_state ^ new_state) << bit;
111
112 if (buf2) {
113 byte2 = buf2 + offset + (block / GFS2_NBBY);
114 cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
115 *byte2 ^= (cur_state ^ new_state) << bit;
116 }
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400117}
118
119/**
120 * gfs2_testbit - test a bit in the bitmaps
121 * @buffer: the buffer that holds the bitmaps
122 * @buflen: the length (in bytes) of the buffer
123 * @block: the block to read
124 *
125 */
126
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000127static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
128 const unsigned char *buffer,
129 unsigned int buflen, u32 block)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400130{
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000131 const unsigned char *byte, *end;
132 unsigned char cur_state;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400133 unsigned int bit;
134
135 byte = buffer + (block / GFS2_NBBY);
136 bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
137 end = buffer + buflen;
138
139 gfs2_assert(rgd->rd_sbd, byte < end);
140
141 cur_state = (*byte >> bit) & GFS2_BIT_MASK;
142
143 return cur_state;
144}
145
146/**
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000147 * gfs2_bit_search
148 * @ptr: Pointer to bitmap data
149 * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
150 * @state: The state we are searching for
151 *
152 * We xor the bitmap data with a patter which is the bitwise opposite
153 * of what we are looking for, this gives rise to a pattern of ones
154 * wherever there is a match. Since we have two bits per entry, we
155 * take this pattern, shift it down by one place and then and it with
156 * the original. All the even bit positions (0,2,4, etc) then represent
157 * successful matches, so we mask with 0x55555..... to remove the unwanted
158 * odd bit positions.
159 *
160 * This allows searching of a whole u64 at once (32 blocks) with a
161 * single test (on 64 bit arches).
162 */
163
164static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
165{
166 u64 tmp;
167 static const u64 search[] = {
Hannes Eder075ac442009-02-21 02:11:42 +0100168 [0] = 0xffffffffffffffffULL,
169 [1] = 0xaaaaaaaaaaaaaaaaULL,
170 [2] = 0x5555555555555555ULL,
171 [3] = 0x0000000000000000ULL,
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000172 };
173 tmp = le64_to_cpu(*ptr) ^ search[state];
174 tmp &= (tmp >> 1);
175 tmp &= mask;
176 return tmp;
177}
178
179/**
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400180 * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
181 * a block in a given allocation state.
182 * @buffer: the buffer that holds the bitmaps
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000183 * @len: the length (in bytes) of the buffer
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400184 * @goal: start search at this block's bit-pair (within @buffer)
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000185 * @state: GFS2_BLKST_XXX the state of the block we're looking for.
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400186 *
187 * Scope of @goal and returned block number is only within this bitmap buffer,
188 * not entire rgrp or filesystem. @buffer will be offset from the actual
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000189 * beginning of a bitmap block buffer, skipping any header structures, but
190 * headers are always a multiple of 64 bits long so that the buffer is
191 * always aligned to a 64 bit boundary.
192 *
193 * The size of the buffer is in bytes, but is it assumed that it is
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200194 * always ok to read a complete multiple of 64 bits at the end
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000195 * of the block in case the end is no aligned to a natural boundary.
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400196 *
197 * Return: the block number (bitmap buffer scope) that was found
198 */
199
Hannes Eder02ab1722009-02-21 02:12:05 +0100200static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
201 u32 goal, u8 state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400202{
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000203 u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
204 const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
205 const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
206 u64 tmp;
Hannes Eder075ac442009-02-21 02:11:42 +0100207 u64 mask = 0x5555555555555555ULL;
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000208 u32 bit;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400209
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000210 BUG_ON(state > 3);
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400211
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000212 /* Mask off bits we don't care about at the start of the search */
213 mask <<= spoint;
214 tmp = gfs2_bit_search(ptr, mask, state);
215 ptr++;
216 while(tmp == 0 && ptr < end) {
Hannes Eder075ac442009-02-21 02:11:42 +0100217 tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000218 ptr++;
Bob Peterson1f466a42008-03-10 18:17:47 -0500219 }
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000220 /* Mask off any bits which are more than len bytes from the start */
221 if (ptr == end && (len & (sizeof(u64) - 1)))
222 tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
223 /* Didn't find anything, so return */
224 if (tmp == 0)
225 return BFITNOENT;
226 ptr--;
Steven Whitehoused8bd5042009-04-23 08:54:02 +0100227 bit = __ffs64(tmp);
Steven Whitehouse223b2b82009-02-17 14:13:35 +0000228 bit /= 2; /* two bits per entry in the bitmap */
229 return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400230}
231
232/**
233 * gfs2_bitcount - count the number of bits in a certain state
234 * @buffer: the buffer that holds the bitmaps
235 * @buflen: the length (in bytes) of the buffer
236 * @state: the state of the block we're looking for
237 *
238 * Returns: The number of bits
239 */
240
Steven Whitehouse110acf32008-01-29 13:30:20 +0000241static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
242 unsigned int buflen, u8 state)
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400243{
Steven Whitehouse110acf32008-01-29 13:30:20 +0000244 const u8 *byte = buffer;
245 const u8 *end = buffer + buflen;
246 const u8 state1 = state << 2;
247 const u8 state2 = state << 4;
248 const u8 state3 = state << 6;
Steven Whitehousecd915492006-09-04 12:49:07 -0400249 u32 count = 0;
Steven Whitehouse88c8ab1f2006-05-18 13:52:39 -0400250
251 for (; byte < end; byte++) {
252 if (((*byte) & 0x03) == state)
253 count++;
254 if (((*byte) & 0x0C) == state1)
255 count++;
256 if (((*byte) & 0x30) == state2)
257 count++;
258 if (((*byte) & 0xC0) == state3)
259 count++;
260 }
261
262 return count;
263}
264
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265/**
266 * gfs2_rgrp_verify - Verify that a resource group is consistent
267 * @sdp: the filesystem
268 * @rgd: the rgrp
269 *
270 */
271
272void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
273{
274 struct gfs2_sbd *sdp = rgd->rd_sbd;
275 struct gfs2_bitmap *bi = NULL;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100276 u32 length = rgd->rd_length;
Steven Whitehousecd915492006-09-04 12:49:07 -0400277 u32 count[4], tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 int buf, x;
279
Steven Whitehousecd915492006-09-04 12:49:07 -0400280 memset(count, 0, 4 * sizeof(u32));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281
282 /* Count # blocks in each of 4 possible allocation states */
283 for (buf = 0; buf < length; buf++) {
284 bi = rgd->rd_bits + buf;
285 for (x = 0; x < 4; x++)
286 count[x] += gfs2_bitcount(rgd,
287 bi->bi_bh->b_data +
288 bi->bi_offset,
289 bi->bi_len, x);
290 }
291
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000292 if (count[0] != rgd->rd_free) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 if (gfs2_consist_rgrpd(rgd))
294 fs_err(sdp, "free data mismatch: %u != %u\n",
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000295 count[0], rgd->rd_free);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296 return;
297 }
298
Steven Whitehouse73f74942008-11-04 10:32:57 +0000299 tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
Benjamin Marzinski6b946172009-07-10 18:13:26 -0500300 if (count[1] != tmp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 if (gfs2_consist_rgrpd(rgd))
302 fs_err(sdp, "used data mismatch: %u != %u\n",
303 count[1], tmp);
304 return;
305 }
306
Benjamin Marzinski6b946172009-07-10 18:13:26 -0500307 if (count[2] + count[3] != rgd->rd_dinodes) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 if (gfs2_consist_rgrpd(rgd))
309 fs_err(sdp, "used metadata mismatch: %u != %u\n",
Benjamin Marzinski6b946172009-07-10 18:13:26 -0500310 count[2] + count[3], rgd->rd_dinodes);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 return;
312 }
313}
314
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100315static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316{
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100317 u64 first = rgd->rd_data0;
318 u64 last = first + rgd->rd_data;
Steven Whitehouse16910422006-09-05 11:15:45 -0400319 return first <= block && block < last;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320}
321
322/**
323 * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
324 * @sdp: The GFS2 superblock
325 * @n: The data block number
326 *
327 * Returns: The resource group, or NULL if not found
328 */
329
Steven Whitehousecd915492006-09-04 12:49:07 -0400330struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331{
Steven Whitehousef75bbfb2011-09-08 10:21:13 +0100332 struct rb_node **newn;
333 struct gfs2_rgrpd *cur;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
335 spin_lock(&sdp->sd_rindex_spin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100336 newn = &sdp->sd_rindex_tree.rb_node;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100337 while (*newn) {
Steven Whitehousef75bbfb2011-09-08 10:21:13 +0100338 cur = rb_entry(*newn, struct gfs2_rgrpd, rd_node);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100339 if (blk < cur->rd_addr)
340 newn = &((*newn)->rb_left);
Steven Whitehousef75bbfb2011-09-08 10:21:13 +0100341 else if (blk >= cur->rd_data0 + cur->rd_data)
Bob Peterson7c9ca622011-08-31 09:53:19 +0100342 newn = &((*newn)->rb_right);
343 else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 spin_unlock(&sdp->sd_rindex_spin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100345 return cur;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 }
347 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 spin_unlock(&sdp->sd_rindex_spin);
349
350 return NULL;
351}
352
353/**
354 * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
355 * @sdp: The GFS2 superblock
356 *
357 * Returns: The first rgrp in the filesystem
358 */
359
360struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
361{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100362 const struct rb_node *n;
363 struct gfs2_rgrpd *rgd;
364
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100365 spin_lock(&sdp->sd_rindex_spin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100366 n = rb_first(&sdp->sd_rindex_tree);
367 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100368 spin_unlock(&sdp->sd_rindex_spin);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100369
370 return rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371}
372
373/**
374 * gfs2_rgrpd_get_next - get the next RG
375 * @rgd: A RG
376 *
377 * Returns: The next rgrp
378 */
379
380struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
381{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100382 struct gfs2_sbd *sdp = rgd->rd_sbd;
383 const struct rb_node *n;
384
385 spin_lock(&sdp->sd_rindex_spin);
386 n = rb_next(&rgd->rd_node);
387 if (n == NULL)
388 n = rb_first(&sdp->sd_rindex_tree);
389
390 if (unlikely(&rgd->rd_node == n)) {
391 spin_unlock(&sdp->sd_rindex_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 return NULL;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100393 }
394 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
395 spin_unlock(&sdp->sd_rindex_spin);
396 return rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000397}
398
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100399void gfs2_free_clones(struct gfs2_rgrpd *rgd)
400{
401 int x;
402
403 for (x = 0; x < rgd->rd_length; x++) {
404 struct gfs2_bitmap *bi = rgd->rd_bits + x;
405 kfree(bi->bi_clone);
406 bi->bi_clone = NULL;
407 }
408}
409
410void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100412 struct rb_node *n;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000413 struct gfs2_rgrpd *rgd;
414 struct gfs2_glock *gl;
415
Bob Peterson7c9ca622011-08-31 09:53:19 +0100416 while ((n = rb_first(&sdp->sd_rindex_tree))) {
417 rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418 gl = rgd->rd_gl;
419
Bob Peterson7c9ca622011-08-31 09:53:19 +0100420 rb_erase(n, &sdp->sd_rindex_tree);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421
422 if (gl) {
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100423 spin_lock(&gl->gl_spin);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500424 gl->gl_object = NULL;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100425 spin_unlock(&gl->gl_spin);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100426 gfs2_glock_add_to_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 gfs2_glock_put(gl);
428 }
429
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100430 gfs2_free_clones(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 kfree(rgd->rd_bits);
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600432 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433 }
434}
435
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100436static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
437{
438 printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
439 printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
440 printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
441 printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
442 printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
443}
444
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445/**
446 * gfs2_compute_bitstructs - Compute the bitmap sizes
447 * @rgd: The resource group descriptor
448 *
449 * Calculates bitmap descriptors, one for each block that contains bitmap data
450 *
451 * Returns: errno
452 */
453
454static int compute_bitstructs(struct gfs2_rgrpd *rgd)
455{
456 struct gfs2_sbd *sdp = rgd->rd_sbd;
457 struct gfs2_bitmap *bi;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100458 u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
Steven Whitehousecd915492006-09-04 12:49:07 -0400459 u32 bytes_left, bytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 int x;
461
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400462 if (!length)
463 return -EINVAL;
464
Steven Whitehousedd894be2006-07-27 14:29:00 -0400465 rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466 if (!rgd->rd_bits)
467 return -ENOMEM;
468
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100469 bytes_left = rgd->rd_bitbytes;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000470
471 for (x = 0; x < length; x++) {
472 bi = rgd->rd_bits + x;
473
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +0100474 bi->bi_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 /* small rgrp; bitmap stored completely in header block */
476 if (length == 1) {
477 bytes = bytes_left;
478 bi->bi_offset = sizeof(struct gfs2_rgrp);
479 bi->bi_start = 0;
480 bi->bi_len = bytes;
481 /* header block */
482 } else if (x == 0) {
483 bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
484 bi->bi_offset = sizeof(struct gfs2_rgrp);
485 bi->bi_start = 0;
486 bi->bi_len = bytes;
487 /* last block */
488 } else if (x + 1 == length) {
489 bytes = bytes_left;
490 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100491 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 bi->bi_len = bytes;
493 /* other blocks */
494 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500495 bytes = sdp->sd_sb.sb_bsize -
496 sizeof(struct gfs2_meta_header);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 bi->bi_offset = sizeof(struct gfs2_meta_header);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100498 bi->bi_start = rgd->rd_bitbytes - bytes_left;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 bi->bi_len = bytes;
500 }
501
502 bytes_left -= bytes;
503 }
504
505 if (bytes_left) {
506 gfs2_consist_rgrpd(rgd);
507 return -EIO;
508 }
509 bi = rgd->rd_bits + (length - 1);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100510 if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 if (gfs2_consist_rgrpd(rgd)) {
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100512 gfs2_rindex_print(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 fs_err(sdp, "start=%u len=%u offset=%u\n",
514 bi->bi_start, bi->bi_len, bi->bi_offset);
515 }
516 return -EIO;
517 }
518
519 return 0;
520}
521
522/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500523 * gfs2_ri_total - Total up the file system space, according to the rindex.
524 *
525 */
526u64 gfs2_ri_total(struct gfs2_sbd *sdp)
527{
528 u64 total_data = 0;
529 struct inode *inode = sdp->sd_rindex;
530 struct gfs2_inode *ip = GFS2_I(inode);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500531 char buf[sizeof(struct gfs2_rindex)];
532 struct file_ra_state ra_state;
533 int error, rgrps;
534
535 mutex_lock(&sdp->sd_rindex_mutex);
536 file_ra_state_init(&ra_state, inode->i_mapping);
537 for (rgrps = 0;; rgrps++) {
538 loff_t pos = rgrps * sizeof(struct gfs2_rindex);
539
Bob Petersonbcd72782010-12-07 13:58:56 -0500540 if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500541 break;
542 error = gfs2_internal_read(ip, &ra_state, buf, &pos,
543 sizeof(struct gfs2_rindex));
544 if (error != sizeof(struct gfs2_rindex))
545 break;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100546 total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500547 }
548 mutex_unlock(&sdp->sd_rindex_mutex);
549 return total_data;
550}
551
Bob Peterson7c9ca622011-08-31 09:53:19 +0100552static void rgd_insert(struct gfs2_rgrpd *rgd)
553{
554 struct gfs2_sbd *sdp = rgd->rd_sbd;
555 struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
556
557 /* Figure out where to put new node */
558 while (*newn) {
559 struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
560 rd_node);
561
562 parent = *newn;
563 if (rgd->rd_addr < cur->rd_addr)
564 newn = &((*newn)->rb_left);
565 else if (rgd->rd_addr > cur->rd_addr)
566 newn = &((*newn)->rb_right);
567 else
568 return;
569 }
570
571 rb_link_node(&rgd->rd_node, parent, newn);
572 rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
573}
574
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500575/**
Robert Peterson6c532672007-05-10 16:54:38 -0500576 * read_rindex_entry - Pull in a new resource index entry from the disk
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577 * @gl: The glock covering the rindex inode
578 *
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100579 * Returns: 0 on success, > 0 on EOF, error code otherwise
Robert Peterson6c532672007-05-10 16:54:38 -0500580 */
581
582static int read_rindex_entry(struct gfs2_inode *ip,
583 struct file_ra_state *ra_state)
584{
585 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
586 loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100587 struct gfs2_rindex buf;
Robert Peterson6c532672007-05-10 16:54:38 -0500588 int error;
589 struct gfs2_rgrpd *rgd;
590
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100591 if (pos >= i_size_read(&ip->i_inode))
592 return 1;
593
594 error = gfs2_internal_read(ip, ra_state, (char *)&buf, &pos,
Robert Peterson6c532672007-05-10 16:54:38 -0500595 sizeof(struct gfs2_rindex));
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100596
597 if (error != sizeof(struct gfs2_rindex))
598 return (error == 0) ? 1 : error;
Robert Peterson6c532672007-05-10 16:54:38 -0500599
Bob Peterson6bdd9be2008-01-28 17:20:26 -0600600 rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
Robert Peterson6c532672007-05-10 16:54:38 -0500601 error = -ENOMEM;
602 if (!rgd)
603 return error;
604
Robert Peterson6c532672007-05-10 16:54:38 -0500605 rgd->rd_sbd = sdp;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100606 rgd->rd_addr = be64_to_cpu(buf.ri_addr);
607 rgd->rd_length = be32_to_cpu(buf.ri_length);
608 rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
609 rgd->rd_data = be32_to_cpu(buf.ri_data);
610 rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100611
Robert Peterson6c532672007-05-10 16:54:38 -0500612 error = compute_bitstructs(rgd);
613 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100614 goto fail;
Robert Peterson6c532672007-05-10 16:54:38 -0500615
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100616 error = gfs2_glock_get(sdp, rgd->rd_addr,
Robert Peterson6c532672007-05-10 16:54:38 -0500617 &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
618 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100619 goto fail;
Robert Peterson6c532672007-05-10 16:54:38 -0500620
621 rgd->rd_gl->gl_object = rgd;
Bob Petersoncf45b752008-01-31 10:31:39 -0600622 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100623 if (rgd->rd_data > sdp->sd_max_rg_data)
624 sdp->sd_max_rg_data = rgd->rd_data;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100625 spin_lock(&sdp->sd_rindex_spin);
626 rgd_insert(rgd);
627 sdp->sd_rgrps++;
628 spin_unlock(&sdp->sd_rindex_spin);
629 return error;
630
631fail:
632 kfree(rgd->rd_bits);
633 kmem_cache_free(gfs2_rgrpd_cachep, rgd);
Robert Peterson6c532672007-05-10 16:54:38 -0500634 return error;
635}
636
637/**
638 * gfs2_ri_update - Pull in a new resource index from the disk
639 * @ip: pointer to the rindex inode
640 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 * Returns: 0 on successful update, error code otherwise
642 */
643
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100644static int gfs2_ri_update(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000645{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400646 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
647 struct inode *inode = &ip->i_inode;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000648 struct file_ra_state ra_state;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649 int error;
650
Steven Whitehousef42faf42006-01-30 18:34:10 +0000651 file_ra_state_init(&ra_state, inode->i_mapping);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100652 do {
Robert Peterson6c532672007-05-10 16:54:38 -0500653 error = read_rindex_entry(ip, &ra_state);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100654 } while (error == 0);
655
656 if (error < 0)
657 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658
Bob Petersoncf45b752008-01-31 10:31:39 -0600659 sdp->sd_rindex_uptodate = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 return 0;
Robert Peterson6c532672007-05-10 16:54:38 -0500661}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662
Robert Peterson6c532672007-05-10 16:54:38 -0500663/**
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100664 * gfs2_rindex_update - Update the rindex if required
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 * @sdp: The GFS2 superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 *
667 * We grab a lock on the rindex inode to make sure that it doesn't
668 * change whilst we are performing an operation. We keep this lock
669 * for quite long periods of time compared to other locks. This
670 * doesn't matter, since it is shared and it is very, very rarely
671 * accessed in the exclusive mode (i.e. only when expanding the filesystem).
672 *
673 * This makes sure that we're using the latest copy of the resource index
674 * special file, which might have been updated if someone expanded the
675 * filesystem (via gfs2_grow utility), which adds new resource groups.
676 *
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100677 * Returns: 0 on succeess, error code otherwise
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 */
679
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100680int gfs2_rindex_update(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400682 struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 struct gfs2_glock *gl = ip->i_gl;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100684 struct gfs2_holder ri_gh;
685 int error = 0;
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000686 int unlock_required = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687
688 /* Read new copy from disk if we don't have the latest */
Bob Petersoncf45b752008-01-31 10:31:39 -0600689 if (!sdp->sd_rindex_uptodate) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000690 mutex_lock(&sdp->sd_rindex_mutex);
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000691 if (!gfs2_glock_is_locked_by_me(gl)) {
692 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
693 if (error)
694 return error;
695 unlock_required = 1;
696 }
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100697 if (!sdp->sd_rindex_uptodate)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698 error = gfs2_ri_update(ip);
Steven Whitehousea365fbf2012-02-24 15:09:14 +0000699 if (unlock_required)
700 gfs2_glock_dq_uninit(&ri_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000701 mutex_unlock(&sdp->sd_rindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702 }
703
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100704
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 return error;
706}
707
Bob Peterson42d52e32008-01-28 18:38:07 -0600708static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100709{
710 const struct gfs2_rgrp *str = buf;
Bob Peterson42d52e32008-01-28 18:38:07 -0600711 u32 rg_flags;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100712
Bob Peterson42d52e32008-01-28 18:38:07 -0600713 rg_flags = be32_to_cpu(str->rg_flags);
Steven Whitehouse09010972009-05-20 10:48:47 +0100714 rg_flags &= ~GFS2_RDF_MASK;
Steven Whitehouse1ce97e52009-05-21 15:18:19 +0100715 rgd->rd_flags &= GFS2_RDF_MASK;
716 rgd->rd_flags |= rg_flags;
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000717 rgd->rd_free = be32_to_cpu(str->rg_free);
Steven Whitehouse73f74942008-11-04 10:32:57 +0000718 rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
Steven Whitehoused8b71f72008-11-04 10:19:03 +0000719 rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100720}
721
Bob Peterson42d52e32008-01-28 18:38:07 -0600722static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100723{
724 struct gfs2_rgrp *str = buf;
725
Steven Whitehouse09010972009-05-20 10:48:47 +0100726 str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
Steven Whitehousecfc8b542008-11-04 10:25:13 +0000727 str->rg_free = cpu_to_be32(rgd->rd_free);
Steven Whitehouse73f74942008-11-04 10:32:57 +0000728 str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100729 str->__pad = cpu_to_be32(0);
Steven Whitehoused8b71f72008-11-04 10:19:03 +0000730 str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100731 memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
732}
733
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734/**
Bob Peterson7c9ca622011-08-31 09:53:19 +0100735 * gfs2_rgrp_go_lock - Read in a RG's header and bitmaps
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 * @rgd: the struct gfs2_rgrpd describing the RG to read in
737 *
738 * Read in all of a Resource Group's header and bitmap blocks.
739 * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
740 *
741 * Returns: errno
742 */
743
Bob Peterson7c9ca622011-08-31 09:53:19 +0100744int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100746 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 struct gfs2_sbd *sdp = rgd->rd_sbd;
748 struct gfs2_glock *gl = rgd->rd_gl;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100749 unsigned int length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750 struct gfs2_bitmap *bi;
751 unsigned int x, y;
752 int error;
753
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754 for (x = 0; x < length; x++) {
755 bi = rgd->rd_bits + x;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100756 error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 if (error)
758 goto fail;
759 }
760
761 for (y = length; y--;) {
762 bi = rgd->rd_bits + y;
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400763 error = gfs2_meta_wait(sdp, bi->bi_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 if (error)
765 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400766 if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 GFS2_METATYPE_RG)) {
768 error = -EIO;
769 goto fail;
770 }
771 }
772
Bob Petersoncf45b752008-01-31 10:31:39 -0600773 if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +0100774 for (x = 0; x < length; x++)
775 clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
Bob Peterson42d52e32008-01-28 18:38:07 -0600776 gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
Steven Whitehouse1ce97e52009-05-21 15:18:19 +0100777 rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100778 rgd->rd_free_clone = rgd->rd_free;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 }
780
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 return 0;
782
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400783fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 while (x--) {
785 bi = rgd->rd_bits + x;
786 brelse(bi->bi_bh);
787 bi->bi_bh = NULL;
788 gfs2_assert_warn(sdp, !bi->bi_clone);
789 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790
791 return error;
792}
793
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794/**
Bob Peterson7c9ca622011-08-31 09:53:19 +0100795 * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get()
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 * @rgd: the struct gfs2_rgrpd describing the RG to read in
797 *
798 */
799
Bob Peterson7c9ca622011-08-31 09:53:19 +0100800void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801{
Bob Peterson7c9ca622011-08-31 09:53:19 +0100802 struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100803 int x, length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 for (x = 0; x < length; x++) {
806 struct gfs2_bitmap *bi = rgd->rd_bits + x;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807 brelse(bi->bi_bh);
808 bi->bi_bh = NULL;
809 }
810
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811}
812
Bob Peterson7c9ca622011-08-31 09:53:19 +0100813void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
814 struct buffer_head *bh,
815 const struct gfs2_bitmap *bi)
Steven Whitehousef15ab562009-02-09 09:25:01 +0000816{
817 struct super_block *sb = sdp->sd_vfs;
818 struct block_device *bdev = sb->s_bdev;
819 const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
Martin K. Petersene1defc42009-05-22 17:17:49 -0400820 bdev_logical_block_size(sb->s_bdev);
Steven Whitehousef15ab562009-02-09 09:25:01 +0000821 u64 blk;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000822 sector_t start = 0;
Steven Whitehousef15ab562009-02-09 09:25:01 +0000823 sector_t nr_sects = 0;
824 int rv;
825 unsigned int x;
826
827 for (x = 0; x < bi->bi_len; x++) {
Bob Peterson7c9ca622011-08-31 09:53:19 +0100828 const u8 *orig = bh->b_data + bi->bi_offset + x;
Steven Whitehousef15ab562009-02-09 09:25:01 +0000829 const u8 *clone = bi->bi_clone + bi->bi_offset + x;
830 u8 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
831 diff &= 0x55;
832 if (diff == 0)
833 continue;
834 blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
835 blk *= sects_per_blk; /* convert to sectors */
836 while(diff) {
837 if (diff & 1) {
838 if (nr_sects == 0)
839 goto start_new_extent;
840 if ((start + nr_sects) != blk) {
841 rv = blkdev_issue_discard(bdev, start,
Christoph Hellwig746cd1e2009-09-12 07:35:43 +0200842 nr_sects, GFP_NOFS,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200843 0);
Steven Whitehousef15ab562009-02-09 09:25:01 +0000844 if (rv)
845 goto fail;
846 nr_sects = 0;
847start_new_extent:
848 start = blk;
849 }
850 nr_sects += sects_per_blk;
851 }
852 diff >>= 2;
853 blk += sects_per_blk;
854 }
855 }
856 if (nr_sects) {
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200857 rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
Steven Whitehousef15ab562009-02-09 09:25:01 +0000858 if (rv)
859 goto fail;
860 }
861 return;
862fail:
863 fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
864 sdp->sd_args.ar_discard = 0;
865}
866
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867/**
Bob Peterson564e12b2011-11-21 13:36:17 -0500868 * gfs2_qadata_get - get the struct gfs2_qadata structure for an inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 * @ip: the incore GFS2 inode structure
870 *
Bob Peterson564e12b2011-11-21 13:36:17 -0500871 * Returns: the struct gfs2_qadata
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 */
873
Bob Peterson564e12b2011-11-21 13:36:17 -0500874struct gfs2_qadata *gfs2_qadata_get(struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875{
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100876 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
877 int error;
Bob Peterson564e12b2011-11-21 13:36:17 -0500878 BUG_ON(ip->i_qadata != NULL);
879 ip->i_qadata = kzalloc(sizeof(struct gfs2_qadata), GFP_NOFS);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100880 error = gfs2_rindex_update(sdp);
881 if (error)
882 fs_warn(sdp, "rindex update returns %d\n", error);
Bob Peterson564e12b2011-11-21 13:36:17 -0500883 return ip->i_qadata;
884}
885
886/**
887 * gfs2_blkrsv_get - get the struct gfs2_blkreserv structure for an inode
888 * @ip: the incore GFS2 inode structure
889 *
890 * Returns: the struct gfs2_qadata
891 */
892
893static struct gfs2_blkreserv *gfs2_blkrsv_get(struct gfs2_inode *ip)
894{
895 BUG_ON(ip->i_res != NULL);
896 ip->i_res = kzalloc(sizeof(struct gfs2_blkreserv), GFP_NOFS);
897 return ip->i_res;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898}
899
900/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 * try_rgrp_fit - See if a given reservation will fit in a given RG
902 * @rgd: the RG data
Steven Whitehouse54335b12011-09-01 13:31:59 +0100903 * @ip: the inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904 *
905 * If there's room for the requested blocks to be allocated from the RG:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 *
907 * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
908 */
909
Steven Whitehouse54335b12011-09-01 13:31:59 +0100910static int try_rgrp_fit(const struct gfs2_rgrpd *rgd, const struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911{
Bob Peterson564e12b2011-11-21 13:36:17 -0500912 const struct gfs2_blkreserv *rs = ip->i_res;
Steven Whitehouse54335b12011-09-01 13:31:59 +0100913
Steven Whitehouse09010972009-05-20 10:48:47 +0100914 if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
Steven Whitehousea43a4902007-04-02 10:48:17 +0100915 return 0;
Bob Peterson564e12b2011-11-21 13:36:17 -0500916 if (rgd->rd_free_clone >= rs->rs_requested)
Bob Peterson7c9ca622011-08-31 09:53:19 +0100917 return 1;
Bob Peterson7c9ca622011-08-31 09:53:19 +0100918 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000919}
920
Bob Petersonb3e47ca2011-11-21 11:47:08 -0500921static inline u32 gfs2_bi2rgd_blk(struct gfs2_bitmap *bi, u32 blk)
922{
923 return (bi->bi_start * GFS2_NBBY) + blk;
924}
925
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926/**
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100927 * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
928 * @rgd: The rgrp
929 *
Bob Peterson1a0eae82010-04-14 11:58:16 -0400930 * Returns: 0 if no error
931 * The inode, if one has been found, in inode.
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100932 */
933
Steven Whitehouse044b9412010-11-03 20:01:07 +0000934static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100935{
Bob Peterson6760bdc2007-07-24 14:09:32 -0500936 u32 goal = 0, block;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400937 u64 no_addr;
Bob Peterson5f3eae72007-08-08 16:52:09 -0500938 struct gfs2_sbd *sdp = rgd->rd_sbd;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000939 struct gfs2_glock *gl;
940 struct gfs2_inode *ip;
941 int error;
942 int found = 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -0500943 struct gfs2_bitmap *bi;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100944
Steven Whitehouse044b9412010-11-03 20:01:07 +0000945 while (goal < rgd->rd_data) {
Bob Peterson5f3eae72007-08-08 16:52:09 -0500946 down_write(&sdp->sd_log_flush_lock);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +0000947 block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, &bi);
Bob Peterson5f3eae72007-08-08 16:52:09 -0500948 up_write(&sdp->sd_log_flush_lock);
Bob Peterson6760bdc2007-07-24 14:09:32 -0500949 if (block == BFITNOENT)
Bob Peterson24c73872007-07-12 16:58:50 -0500950 break;
Bob Petersonb3e47ca2011-11-21 11:47:08 -0500951
952 block = gfs2_bi2rgd_blk(bi, block);
Bob Peterson6760bdc2007-07-24 14:09:32 -0500953 /* rgblk_search can return a block < goal, so we need to
954 keep it marching forward. */
955 no_addr = block + rgd->rd_data0;
Bob Peterson44ad37d2011-03-17 16:19:58 -0400956 goal = max(block + 1, goal + 1);
Bob Peterson6760bdc2007-07-24 14:09:32 -0500957 if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100958 continue;
Steven Whitehouse1e19a192009-07-10 21:13:38 +0100959 if (no_addr == skip)
960 continue;
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400961 *last_unlinked = no_addr;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000962
963 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
964 if (error)
965 continue;
966
967 /* If the inode is already in cache, we can ignore it here
968 * because the existing inode disposal code will deal with
969 * it when all refs have gone away. Accessing gl_object like
970 * this is not safe in general. Here it is ok because we do
971 * not dereference the pointer, and we only need an approx
972 * answer to whether it is NULL or not.
973 */
974 ip = gl->gl_object;
975
976 if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
977 gfs2_glock_put(gl);
978 else
979 found++;
980
981 /* Limit reclaim to sensible number of tasks */
Bob Peterson44ad37d2011-03-17 16:19:58 -0400982 if (found > NR_CPUS)
Steven Whitehouse044b9412010-11-03 20:01:07 +0000983 return;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100984 }
985
986 rgd->rd_flags &= ~GFS2_RDF_CHECK;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000987 return;
Steven Whitehousec8cdf472007-06-08 10:05:33 +0100988}
989
990/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 * get_local_rgrp - Choose and lock a rgrp for allocation
992 * @ip: the inode to reserve space for
993 * @rgp: the chosen and locked rgrp
994 *
995 * Try to acquire rgrp in way which avoids contending with others.
996 *
997 * Returns: errno
998 */
999
Steven Whitehouse044b9412010-11-03 20:01:07 +00001000static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001002 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 struct gfs2_rgrpd *rgd, *begin = NULL;
Bob Peterson564e12b2011-11-21 13:36:17 -05001004 struct gfs2_blkreserv *rs = ip->i_res;
Bob Petersonc688b8b2011-11-14 10:45:40 -05001005 int error, rg_locked, flags = LM_FLAG_TRY;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001006 int loops = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007
Steven Whitehouse54335b12011-09-01 13:31:59 +01001008 if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal))
1009 rgd = begin = ip->i_rgd;
1010 else
1011 rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012
Bob Peterson7c9ca622011-08-31 09:53:19 +01001013 if (rgd == NULL)
1014 return -EBADSLT;
1015
1016 while (loops < 3) {
Abhijith Das292c8c12007-11-29 14:13:54 -06001017 rg_locked = 0;
1018
1019 if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
1020 rg_locked = 1;
1021 error = 0;
1022 } else {
1023 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
Bob Peterson564e12b2011-11-21 13:36:17 -05001024 flags, &rs->rs_rgd_gh);
Abhijith Das292c8c12007-11-29 14:13:54 -06001025 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 switch (error) {
1027 case 0:
Steven Whitehouse54335b12011-09-01 13:31:59 +01001028 if (try_rgrp_fit(rgd, ip)) {
1029 ip->i_rgd = rgd;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001030 return 0;
Steven Whitehouse54335b12011-09-01 13:31:59 +01001031 }
Steven Whitehouse044b9412010-11-03 20:01:07 +00001032 if (rgd->rd_flags & GFS2_RDF_CHECK)
1033 try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
Abhijith Das292c8c12007-11-29 14:13:54 -06001034 if (!rg_locked)
Bob Peterson564e12b2011-11-21 13:36:17 -05001035 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
Steven Whitehouse9cabcdb2008-07-10 15:54:12 +01001036 /* fall through */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 case GLR_TRYFAILED:
Bob Peterson7c9ca622011-08-31 09:53:19 +01001038 rgd = gfs2_rgrpd_get_next(rgd);
Bob Petersonc688b8b2011-11-14 10:45:40 -05001039 if (rgd == begin) {
1040 flags = 0;
Benjamin Marzinski172e0452007-03-23 14:51:56 -06001041 loops++;
Bob Petersonc688b8b2011-11-14 10:45:40 -05001042 }
Bob Peterson7c9ca622011-08-31 09:53:19 +01001043 break;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001044 default:
1045 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 }
1047 }
1048
Bob Peterson7c9ca622011-08-31 09:53:19 +01001049 return -ENOSPC;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050}
1051
Bob Peterson564e12b2011-11-21 13:36:17 -05001052static void gfs2_blkrsv_put(struct gfs2_inode *ip)
1053{
1054 BUG_ON(ip->i_res == NULL);
1055 kfree(ip->i_res);
1056 ip->i_res = NULL;
1057}
1058
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059/**
Steven Whitehouse9ae32422011-09-20 12:16:11 +01001060 * gfs2_inplace_reserve - Reserve space in the filesystem
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 * @ip: the inode to reserve space for
1062 *
1063 * Returns: errno
1064 */
1065
Bob Peterson564e12b2011-11-21 13:36:17 -05001066int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001068 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Bob Peterson564e12b2011-11-21 13:36:17 -05001069 struct gfs2_blkreserv *rs;
Robert Peterson7ae8fa82007-05-09 09:37:57 -05001070 int error = 0;
Steven Whitehouse044b9412010-11-03 20:01:07 +00001071 u64 last_unlinked = NO_BLOCK;
1072 int tries = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073
Bob Peterson564e12b2011-11-21 13:36:17 -05001074 rs = gfs2_blkrsv_get(ip);
1075 if (!rs)
1076 return -ENOMEM;
1077
1078 rs->rs_requested = requested;
1079 if (gfs2_assert_warn(sdp, requested)) {
1080 error = -EINVAL;
1081 goto out;
1082 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083
Steven Whitehouse044b9412010-11-03 20:01:07 +00001084 do {
1085 error = get_local_rgrp(ip, &last_unlinked);
Steven Whitehouse54335b12011-09-01 13:31:59 +01001086 if (error != -ENOSPC)
1087 break;
1088 /* Check that fs hasn't grown if writing to rindex */
1089 if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
1090 error = gfs2_ri_update(ip);
1091 if (error)
1092 break;
1093 continue;
Benjamin Marzinski0489b3f2010-11-30 09:38:35 -06001094 }
Steven Whitehouse54335b12011-09-01 13:31:59 +01001095 /* Flushing the log may release space */
1096 gfs2_log_flush(sdp, NULL);
1097 } while (tries++ < 3);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098
Bob Peterson564e12b2011-11-21 13:36:17 -05001099out:
1100 if (error)
1101 gfs2_blkrsv_put(ip);
Steven Whitehouse9ae32422011-09-20 12:16:11 +01001102 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103}
1104
1105/**
1106 * gfs2_inplace_release - release an inplace reservation
1107 * @ip: the inode the reservation was taken out on
1108 *
1109 * Release a reservation made by gfs2_inplace_reserve().
1110 */
1111
1112void gfs2_inplace_release(struct gfs2_inode *ip)
1113{
Bob Peterson564e12b2011-11-21 13:36:17 -05001114 struct gfs2_blkreserv *rs = ip->i_res;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115
Bob Peterson564e12b2011-11-21 13:36:17 -05001116 if (rs->rs_rgd_gh.gh_gl)
1117 gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
Bob Peterson49528b42012-01-06 16:48:50 -05001118 gfs2_blkrsv_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119}
1120
1121/**
1122 * gfs2_get_block_type - Check a block in a RG is of given type
1123 * @rgd: the resource group holding the block
1124 * @block: the block number
1125 *
1126 * Returns: The block type (GFS2_BLKST_*)
1127 */
1128
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001129static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130{
1131 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001132 u32 length, rgrp_block, buf_block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133 unsigned int buf;
1134 unsigned char type;
1135
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001136 length = rgd->rd_length;
1137 rgrp_block = block - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138
1139 for (buf = 0; buf < length; buf++) {
1140 bi = rgd->rd_bits + buf;
1141 if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1142 break;
1143 }
1144
1145 gfs2_assert(rgd->rd_sbd, buf < length);
1146 buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
1147
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001148 type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 bi->bi_len, buf_block);
1150
1151 return type;
1152}
1153
1154/**
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001155 * rgblk_search - find a block in @state
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156 * @rgd: the resource group descriptor
1157 * @goal: the goal block within the RG (start here to search for avail block)
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001158 * @state: GFS2_BLKST_XXX the before-allocation state to find
Bob Peterson6e87ed02011-11-18 10:58:32 -05001159 * @dinode: TRUE if the first block we allocate is for a dinode
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001160 * @rbi: address of the pointer to the bitmap containing the block found
David Teiglandb3b94fa2006-01-16 16:50:04 +00001161 *
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001162 * Walk rgrp's bitmap to find bits that represent a block in @state.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163 *
1164 * This function never fails, because we wouldn't call it unless we
1165 * know (from reservation results, etc.) that a block is available.
1166 *
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001167 * Scope of @goal is just within rgrp, not the whole filesystem.
1168 * Scope of @returned block is just within bitmap, not the whole filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169 *
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001170 * Returns: the block number found relative to the bitmap rbi
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171 */
1172
Steven Whitehousecd915492006-09-04 12:49:07 -04001173static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001174 unsigned char state,
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001175 struct gfs2_bitmap **rbi)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001176{
1177 struct gfs2_bitmap *bi = NULL;
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001178 const u32 length = rgd->rd_length;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001179 u32 blk = BFITNOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 unsigned int buf, x;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001181 const u8 *buffer = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001183 *rbi = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184 /* Find bitmap block that contains bits for goal block */
1185 for (buf = 0; buf < length; buf++) {
1186 bi = rgd->rd_bits + buf;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001187 /* Convert scope of "goal" from rgrp-wide to within found bit block */
1188 if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
1189 goal -= bi->bi_start * GFS2_NBBY;
1190 goto do_search;
1191 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001192 }
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001193 buf = 0;
1194 goal = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001196do_search:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197 /* Search (up to entire) bitmap in this rgrp for allocatable block.
1198 "x <= length", instead of "x < length", because we typically start
1199 the search in the middle of a bit block, but if we can't find an
1200 allocatable block anywhere else, we want to be able wrap around and
1201 search in the first part of our first-searched bit block. */
1202 for (x = 0; x <= length; x++) {
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001203 bi = rgd->rd_bits + buf;
1204
1205 if (test_bit(GBF_FULL, &bi->bi_flags) &&
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001206 (state == GFS2_BLKST_FREE))
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001207 goto skip;
1208
Bob Peterson5f3eae72007-08-08 16:52:09 -05001209 /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
1210 bitmaps, so we must search the originals for that. */
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001211 buffer = bi->bi_bh->b_data + bi->bi_offset;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001212 WARN_ON(!buffer_uptodate(bi->bi_bh));
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001213 if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
Steven Whitehouse110acf32008-01-29 13:30:20 +00001214 buffer = bi->bi_clone + bi->bi_offset;
1215
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001216 blk = gfs2_bitfit(buffer, bi->bi_len, goal, state);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 if (blk != BFITNOENT)
1218 break;
1219
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001220 if ((goal == 0) && (state == GFS2_BLKST_FREE))
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001221 set_bit(GBF_FULL, &bi->bi_flags);
1222
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223 /* Try next bitmap block (wrap back to rgrp header if at end) */
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001224skip:
1225 buf++;
1226 buf %= length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 goal = 0;
1228 }
1229
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001230 if (blk != BFITNOENT)
1231 *rbi = bi;
Bob Peterson7c9ca622011-08-31 09:53:19 +01001232
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001233 return blk;
1234}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001236/**
1237 * gfs2_alloc_extent - allocate an extent from a given bitmap
1238 * @rgd: the resource group descriptor
1239 * @bi: the bitmap within the rgrp
1240 * @blk: the block within the bitmap
1241 * @dinode: TRUE if the first block we allocate is for a dinode
1242 * @n: The extent length
1243 *
1244 * Add the found bitmap buffer to the transaction.
1245 * Set the found bits to @new_state to change block's allocation state.
1246 * Returns: starting block number of the extent (fs scope)
1247 */
1248static u64 gfs2_alloc_extent(struct gfs2_rgrpd *rgd, struct gfs2_bitmap *bi,
1249 u32 blk, bool dinode, unsigned int *n)
1250{
1251 const unsigned int elen = *n;
1252 u32 goal;
1253 const u8 *buffer = NULL;
1254
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001255 *n = 0;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001256 buffer = bi->bi_bh->b_data + bi->bi_offset;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001257 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
1258 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
Bob Peterson6e87ed02011-11-18 10:58:32 -05001259 bi, blk, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001260 (*n)++;
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001261 goal = blk;
1262 while (*n < elen) {
1263 goal++;
1264 if (goal >= (bi->bi_len * GFS2_NBBY))
1265 break;
1266 if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
1267 GFS2_BLKST_FREE)
1268 break;
1269 gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
Bob Peterson6e87ed02011-11-18 10:58:32 -05001270 bi, goal, GFS2_BLKST_USED);
Steven Whitehouse60a0b8f2009-05-21 12:23:12 +01001271 (*n)++;
1272 }
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001273 blk = gfs2_bi2rgd_blk(bi, blk);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001274 rgd->rd_last_alloc = blk + *n - 1;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001275 return rgd->rd_data0 + blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276}
1277
1278/**
1279 * rgblk_free - Change alloc state of given block(s)
1280 * @sdp: the filesystem
1281 * @bstart: the start of a run of blocks to free
1282 * @blen: the length of the block run (all must lie within ONE RG!)
1283 * @new_state: GFS2_BLKST_XXX the after-allocation block state
1284 *
1285 * Returns: Resource group containing the block(s)
1286 */
1287
Steven Whitehousecd915492006-09-04 12:49:07 -04001288static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
1289 u32 blen, unsigned char new_state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290{
1291 struct gfs2_rgrpd *rgd;
1292 struct gfs2_bitmap *bi = NULL;
Steven Whitehousecd915492006-09-04 12:49:07 -04001293 u32 length, rgrp_blk, buf_blk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001294 unsigned int buf;
1295
1296 rgd = gfs2_blk2rgrpd(sdp, bstart);
1297 if (!rgd) {
1298 if (gfs2_consist(sdp))
Steven Whitehouse382066d2006-05-24 10:22:09 -04001299 fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300 return NULL;
1301 }
1302
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001303 length = rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001305 rgrp_blk = bstart - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001306
1307 while (blen--) {
1308 for (buf = 0; buf < length; buf++) {
1309 bi = rgd->rd_bits + buf;
1310 if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
1311 break;
1312 }
1313
1314 gfs2_assert(rgd->rd_sbd, buf < length);
1315
1316 buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
1317 rgrp_blk++;
1318
1319 if (!bi->bi_clone) {
1320 bi->bi_clone = kmalloc(bi->bi_bh->b_size,
Steven Whitehousedd894be2006-07-27 14:29:00 -04001321 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322 memcpy(bi->bi_clone + bi->bi_offset,
1323 bi->bi_bh->b_data + bi->bi_offset,
1324 bi->bi_len);
1325 }
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001326 gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +00001327 gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
Bob Peterson95c8e172011-03-22 10:49:12 -04001328 bi, buf_blk, new_state);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329 }
1330
1331 return rgd;
1332}
1333
1334/**
Steven Whitehouse09010972009-05-20 10:48:47 +01001335 * gfs2_rgrp_dump - print out an rgrp
1336 * @seq: The iterator
1337 * @gl: The glock in question
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339 */
1340
Steven Whitehouse09010972009-05-20 10:48:47 +01001341int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
1342{
1343 const struct gfs2_rgrpd *rgd = gl->gl_object;
1344 if (rgd == NULL)
1345 return 0;
1346 gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
1347 (unsigned long long)rgd->rd_addr, rgd->rd_flags,
1348 rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
1349 return 0;
1350}
1351
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01001352static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
1353{
1354 struct gfs2_sbd *sdp = rgd->rd_sbd;
1355 fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
Steven Whitehouse86d00632009-09-14 09:50:57 +01001356 (unsigned long long)rgd->rd_addr);
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01001357 fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
1358 gfs2_rgrp_dump(NULL, rgd->rd_gl);
1359 rgd->rd_flags |= GFS2_RDF_ERROR;
1360}
1361
Steven Whitehouse09010972009-05-20 10:48:47 +01001362/**
Bob Peterson6e87ed02011-11-18 10:58:32 -05001363 * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
Steven Whitehouse09010972009-05-20 10:48:47 +01001364 * @ip: the inode to allocate the block for
1365 * @bn: Used to return the starting block number
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001366 * @ndata: requested number of blocks/extent length (value/result)
Bob Peterson6e87ed02011-11-18 10:58:32 -05001367 * @dinode: 1 if we're allocating a dinode block, else 0
Bob Peterson3c5d7852011-11-14 11:17:08 -05001368 * @generation: the generation number of the inode
Steven Whitehouse09010972009-05-20 10:48:47 +01001369 *
1370 * Returns: 0 or error
1371 */
1372
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001373int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
Bob Peterson6e87ed02011-11-18 10:58:32 -05001374 bool dinode, u64 *generation)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001375{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001376 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehoused9ba7612009-04-23 08:59:41 +01001377 struct buffer_head *dibh;
Steven Whitehouse9a3f2362010-08-23 11:49:34 +01001378 struct gfs2_rgrpd *rgd;
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001379 unsigned int ndata;
1380 u32 goal, blk; /* block, within the rgrp scope */
Bob Peterson3c5d7852011-11-14 11:17:08 -05001381 u64 block; /* block, within the file system scope */
Steven Whitehoused9ba7612009-04-23 08:59:41 +01001382 int error;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001383 struct gfs2_bitmap *bi;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384
Steven Whitehouse9a3f2362010-08-23 11:49:34 +01001385 /* Only happens if there is a bug in gfs2, return something distinctive
1386 * to ensure that it is noticed.
1387 */
Bob Peterson564e12b2011-11-21 13:36:17 -05001388 if (ip->i_res == NULL)
Steven Whitehouse9a3f2362010-08-23 11:49:34 +01001389 return -ECANCELED;
1390
Steven Whitehouse54335b12011-09-01 13:31:59 +01001391 rgd = ip->i_rgd;
Steven Whitehouse9a3f2362010-08-23 11:49:34 +01001392
Bob Peterson3c5d7852011-11-14 11:17:08 -05001393 if (!dinode && rgrp_contains_block(rgd, ip->i_goal))
Steven Whitehousece276b02008-02-06 09:25:45 +00001394 goal = ip->i_goal - rgd->rd_data0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395 else
Steven Whitehouseac576cc2008-02-01 10:34:15 +00001396 goal = rgd->rd_last_alloc;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001398 blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, &bi);
Steven Whitehouse09010972009-05-20 10:48:47 +01001399
1400 /* Since all blocks are reserved in advance, this shouldn't happen */
1401 if (blk == BFITNOENT)
1402 goto rgrp_error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001404 block = gfs2_alloc_extent(rgd, bi, blk, dinode, nblocks);
1405 ndata = *nblocks;
1406 if (dinode)
1407 ndata--;
Bob Petersonb3e47ca2011-11-21 11:47:08 -05001408
Bob Peterson3c5d7852011-11-14 11:17:08 -05001409 if (!dinode) {
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001410 ip->i_goal = block + ndata - 1;
Bob Peterson3c5d7852011-11-14 11:17:08 -05001411 error = gfs2_meta_inode_buffer(ip, &dibh);
1412 if (error == 0) {
1413 struct gfs2_dinode *di =
1414 (struct gfs2_dinode *)dibh->b_data;
1415 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1416 di->di_goal_meta = di->di_goal_data =
1417 cpu_to_be64(ip->i_goal);
1418 brelse(dibh);
1419 }
Steven Whitehoused9ba7612009-04-23 08:59:41 +01001420 }
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001421 if (rgd->rd_free < *nblocks)
Steven Whitehouse09010972009-05-20 10:48:47 +01001422 goto rgrp_error;
1423
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001424 rgd->rd_free -= *nblocks;
Bob Peterson3c5d7852011-11-14 11:17:08 -05001425 if (dinode) {
1426 rgd->rd_dinodes++;
1427 *generation = rgd->rd_igeneration++;
1428 if (*generation == 0)
1429 *generation = rgd->rd_igeneration++;
1430 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001431
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001432 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001433 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001434
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001435 gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
Bob Peterson3c5d7852011-11-14 11:17:08 -05001436 if (dinode)
1437 gfs2_trans_add_unrevoke(sdp, block, 1);
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001438
1439 /*
1440 * This needs reviewing to see why we cannot do the quota change
1441 * at this point in the dinode case.
1442 */
1443 if (ndata)
1444 gfs2_quota_change(ip, ndata, ip->i_inode.i_uid,
Bob Peterson3c5d7852011-11-14 11:17:08 -05001445 ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001446
Steven Whitehouse6a8099e2011-11-22 12:18:51 +00001447 rgd->rd_free_clone -= *nblocks;
1448 trace_gfs2_block_alloc(ip, block, *nblocks,
Bob Peterson6e87ed02011-11-18 10:58:32 -05001449 dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
Steven Whitehouse6050b9c2009-07-31 16:19:40 +01001450 *bn = block;
1451 return 0;
1452
1453rgrp_error:
1454 gfs2_rgrp_error(rgd);
1455 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456}
1457
1458/**
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05001459 * __gfs2_free_blocks - free a contiguous run of block(s)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001460 * @ip: the inode these blocks are being freed from
1461 * @bstart: first block of a run of contiguous blocks
1462 * @blen: the length of the block run
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05001463 * @meta: 1 if the blocks represent metadata
David Teiglandb3b94fa2006-01-16 16:50:04 +00001464 *
1465 */
1466
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05001467void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001468{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001469 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470 struct gfs2_rgrpd *rgd;
1471
1472 rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
1473 if (!rgd)
1474 return;
Steven Whitehouse63997772009-06-12 08:49:20 +01001475 trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
Steven Whitehousecfc8b542008-11-04 10:25:13 +00001476 rgd->rd_free += blen;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001477
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001478 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001479 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001480
Steven Whitehouse6d3117b2011-05-21 14:05:58 +01001481 /* Directories keep their data in the metadata address space */
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05001482 if (meta || ip->i_depth)
Steven Whitehouse6d3117b2011-05-21 14:05:58 +01001483 gfs2_meta_wipe(ip, bstart, blen);
Bob Peterson4c16c362011-02-23 16:11:33 -05001484}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485
Bob Peterson4c16c362011-02-23 16:11:33 -05001486/**
Bob Peterson4c16c362011-02-23 16:11:33 -05001487 * gfs2_free_meta - free a contiguous run of data block(s)
1488 * @ip: the inode these blocks are being freed from
1489 * @bstart: first block of a run of contiguous blocks
1490 * @blen: the length of the block run
1491 *
1492 */
1493
Steven Whitehousecd915492006-09-04 12:49:07 -04001494void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001496 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001497
Eric Sandeen46fcb2e2011-06-23 10:39:34 -05001498 __gfs2_free_blocks(ip, bstart, blen, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001499 gfs2_statfs_change(sdp, 0, +blen, 0);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001500 gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001501}
1502
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001503void gfs2_unlink_di(struct inode *inode)
1504{
1505 struct gfs2_inode *ip = GFS2_I(inode);
1506 struct gfs2_sbd *sdp = GFS2_SB(inode);
1507 struct gfs2_rgrpd *rgd;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001508 u64 blkno = ip->i_no_addr;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001509
1510 rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
1511 if (!rgd)
1512 return;
Steven Whitehouse63997772009-06-12 08:49:20 +01001513 trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001514 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001515 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001516}
1517
Steven Whitehousecd915492006-09-04 12:49:07 -04001518static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001519{
1520 struct gfs2_sbd *sdp = rgd->rd_sbd;
1521 struct gfs2_rgrpd *tmp_rgd;
1522
1523 tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
1524 if (!tmp_rgd)
1525 return;
1526 gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
1527
Steven Whitehouse73f74942008-11-04 10:32:57 +00001528 if (!rgd->rd_dinodes)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001529 gfs2_consist_rgrpd(rgd);
Steven Whitehouse73f74942008-11-04 10:32:57 +00001530 rgd->rd_dinodes--;
Steven Whitehousecfc8b542008-11-04 10:25:13 +00001531 rgd->rd_free++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001532
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001533 gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
Bob Peterson42d52e32008-01-28 18:38:07 -06001534 gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001535
1536 gfs2_statfs_change(sdp, 0, +1, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001537}
1538
David Teiglandb3b94fa2006-01-16 16:50:04 +00001539
1540void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
1541{
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001542 gfs2_free_uninit_di(rgd, ip->i_no_addr);
Steven Whitehouse63997772009-06-12 08:49:20 +01001543 trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
Steven Whitehouse2933f922006-11-01 13:23:29 -05001544 gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001545 gfs2_meta_wipe(ip, ip->i_no_addr, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546}
1547
1548/**
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001549 * gfs2_check_blk_type - Check the type of a block
1550 * @sdp: The superblock
1551 * @no_addr: The block number to check
1552 * @type: The block type we are looking for
1553 *
1554 * Returns: 0 if the block type matches the expected type
1555 * -ESTALE if it doesn't match
1556 * or -ve errno if something went wrong while checking
1557 */
1558
1559int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
1560{
1561 struct gfs2_rgrpd *rgd;
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001562 struct gfs2_holder rgd_gh;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001563 int error;
1564
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001565 error = gfs2_rindex_update(sdp);
1566 if (error)
1567 return error;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001568
1569 error = -EINVAL;
1570 rgd = gfs2_blk2rgrpd(sdp, no_addr);
1571 if (!rgd)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001572 goto fail;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001573
1574 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
1575 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001576 goto fail;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001577
1578 if (gfs2_get_block_type(rgd, no_addr) != type)
1579 error = -ESTALE;
1580
1581 gfs2_glock_dq_uninit(&rgd_gh);
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001582fail:
1583 return error;
1584}
1585
1586/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001587 * gfs2_rlist_add - add a RG to a list of RGs
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001588 * @ip: the inode
David Teiglandb3b94fa2006-01-16 16:50:04 +00001589 * @rlist: the list of resource groups
1590 * @block: the block
1591 *
1592 * Figure out what RG a block belongs to and add that RG to the list
1593 *
1594 * FIXME: Don't use NOFAIL
1595 *
1596 */
1597
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001598void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
Steven Whitehousecd915492006-09-04 12:49:07 -04001599 u64 block)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001600{
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001601 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001602 struct gfs2_rgrpd *rgd;
1603 struct gfs2_rgrpd **tmp;
1604 unsigned int new_space;
1605 unsigned int x;
1606
1607 if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
1608 return;
1609
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001610 if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block))
1611 rgd = ip->i_rgd;
1612 else
1613 rgd = gfs2_blk2rgrpd(sdp, block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001614 if (!rgd) {
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001615 fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001616 return;
1617 }
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001618 ip->i_rgd = rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001619
1620 for (x = 0; x < rlist->rl_rgrps; x++)
1621 if (rlist->rl_rgd[x] == rgd)
1622 return;
1623
1624 if (rlist->rl_rgrps == rlist->rl_space) {
1625 new_space = rlist->rl_space + 10;
1626
1627 tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001628 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001629
1630 if (rlist->rl_rgd) {
1631 memcpy(tmp, rlist->rl_rgd,
1632 rlist->rl_space * sizeof(struct gfs2_rgrpd *));
1633 kfree(rlist->rl_rgd);
1634 }
1635
1636 rlist->rl_space = new_space;
1637 rlist->rl_rgd = tmp;
1638 }
1639
1640 rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
1641}
1642
1643/**
1644 * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
1645 * and initialize an array of glock holders for them
1646 * @rlist: the list of resource groups
1647 * @state: the lock state to acquire the RG lock in
1648 * @flags: the modifier flags for the holder structures
1649 *
1650 * FIXME: Don't use NOFAIL
1651 *
1652 */
1653
Bob Petersonfe6c9912008-01-28 11:13:02 -06001654void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001655{
1656 unsigned int x;
1657
1658 rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
Steven Whitehousedd894be2006-07-27 14:29:00 -04001659 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001660 for (x = 0; x < rlist->rl_rgrps; x++)
1661 gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
Bob Petersonfe6c9912008-01-28 11:13:02 -06001662 state, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001663 &rlist->rl_ghs[x]);
1664}
1665
1666/**
1667 * gfs2_rlist_free - free a resource group list
1668 * @list: the list of resource groups
1669 *
1670 */
1671
1672void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
1673{
1674 unsigned int x;
1675
1676 kfree(rlist->rl_rgd);
1677
1678 if (rlist->rl_ghs) {
1679 for (x = 0; x < rlist->rl_rgrps; x++)
1680 gfs2_holder_uninit(&rlist->rl_ghs[x]);
1681 kfree(rlist->rl_ghs);
1682 }
1683}
1684