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