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