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