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