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