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