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