ocfs2: Don't relink cluster groups when allocating discontig block groups
[deliverable/linux.git] / fs / ocfs2 / suballoc.c
CommitLineData
ccd979bd
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * suballoc.c
5 *
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
8 *
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
d6b32bbb 38#include "blockcheck.h"
ccd979bd
MF
39#include "dlmglue.h"
40#include "inode.h"
41#include "journal.h"
42#include "localalloc.h"
43#include "suballoc.h"
44#include "super.h"
45#include "sysfile.h"
46#include "uptodate.h"
47
48#include "buffer_head_io.h"
49
ffda89a3 50#define NOT_ALLOC_NEW_GROUP 0
60ca81e8
TM
51#define ALLOC_NEW_GROUP 0x1
52#define ALLOC_GROUPS_FROM_GLOBAL 0x2
ffda89a3 53
b89c5428 54#define OCFS2_MAX_TO_STEAL 1024
4d0ddb2c 55
7d1fe093 56struct ocfs2_suballoc_result {
2b6cb576
JB
57 u64 sr_bg_blkno; /* The bg we allocated from. Set
58 to 0 when a block group is
59 contiguous. */
ba206635 60 u64 sr_blkno; /* The first allocated block */
7d1fe093
JB
61 unsigned int sr_bit_offset; /* The bit in the bg */
62 unsigned int sr_bits; /* How many bits we claimed */
63};
64
ccd979bd
MF
65static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
66static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
67static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
1fabe148 68static int ocfs2_block_group_fill(handle_t *handle,
ccd979bd
MF
69 struct inode *alloc_inode,
70 struct buffer_head *bg_bh,
71 u64 group_blkno,
798db35f 72 unsigned int group_clusters,
ccd979bd
MF
73 u16 my_chain,
74 struct ocfs2_chain_list *cl);
75static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
76 struct inode *alloc_inode,
1187c968 77 struct buffer_head *bh,
60ca81e8 78 u64 max_block,
feb473a6 79 u64 *last_alloc_group,
60ca81e8 80 int flags);
ccd979bd 81
ccd979bd
MF
82static int ocfs2_cluster_group_search(struct inode *inode,
83 struct buffer_head *group_bh,
84 u32 bits_wanted, u32 min_bits,
1187c968 85 u64 max_block,
7d1fe093 86 struct ocfs2_suballoc_result *res);
ccd979bd
MF
87static int ocfs2_block_group_search(struct inode *inode,
88 struct buffer_head *group_bh,
89 u32 bits_wanted, u32 min_bits,
1187c968 90 u64 max_block,
7d1fe093 91 struct ocfs2_suballoc_result *res);
aa8f8e93 92static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1fabe148 93 handle_t *handle,
ccd979bd
MF
94 u32 bits_wanted,
95 u32 min_bits,
7d1fe093 96 struct ocfs2_suballoc_result *res);
ccd979bd
MF
97static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
98 int nr);
1fabe148 99static inline int ocfs2_block_group_set_bits(handle_t *handle,
ccd979bd
MF
100 struct inode *alloc_inode,
101 struct ocfs2_group_desc *bg,
102 struct buffer_head *group_bh,
103 unsigned int bit_off,
104 unsigned int num_bits);
1fabe148 105static int ocfs2_relink_block_group(handle_t *handle,
ccd979bd
MF
106 struct inode *alloc_inode,
107 struct buffer_head *fe_bh,
108 struct buffer_head *bg_bh,
109 struct buffer_head *prev_bg_bh,
110 u16 chain);
111static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
112 u32 wanted);
ccd979bd
MF
113static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
114 u64 bg_blkno,
115 u16 bg_bit_off);
ccd979bd
MF
116static inline void ocfs2_block_to_cluster_group(struct inode *inode,
117 u64 data_blkno,
118 u64 *bg_blkno,
119 u16 *bg_bit_off);
1187c968
JB
120static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
121 u32 bits_wanted, u64 max_block,
60ca81e8 122 int flags,
1187c968 123 struct ocfs2_alloc_context **ac);
ccd979bd 124
9c7af40b 125void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
ccd979bd 126{
da5cbf2f
MF
127 struct inode *inode = ac->ac_inode;
128
129 if (inode) {
130 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
e63aecb6 131 ocfs2_inode_unlock(inode, 1);
da5cbf2f
MF
132
133 mutex_unlock(&inode->i_mutex);
134
135 iput(inode);
4d0ddb2c 136 ac->ac_inode = NULL;
da5cbf2f 137 }
a81cb88b
MF
138 brelse(ac->ac_bh);
139 ac->ac_bh = NULL;
e3b4a97d 140 ac->ac_resv = NULL;
4d0ddb2c
TM
141}
142
143void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
144{
145 ocfs2_free_ac_resource(ac);
ccd979bd
MF
146 kfree(ac);
147}
148
149static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
150{
151 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
152}
153
57e3e797
JB
154#define do_error(fmt, ...) \
155 do{ \
78c37eb0 156 if (resize) \
57e3e797
JB
157 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
158 else \
159 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
160 } while (0)
161
970e4936
JB
162static int ocfs2_validate_gd_self(struct super_block *sb,
163 struct buffer_head *bh,
78c37eb0 164 int resize)
970e4936
JB
165{
166 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
167
7bf72ede 168 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
68f64d47
JB
169 do_error("Group descriptor #%llu has bad signature %.*s",
170 (unsigned long long)bh->b_blocknr, 7,
57e3e797
JB
171 gd->bg_signature);
172 return -EINVAL;
7bf72ede
MF
173 }
174
68f64d47
JB
175 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
176 do_error("Group descriptor #%llu has an invalid bg_blkno "
177 "of %llu",
178 (unsigned long long)bh->b_blocknr,
179 (unsigned long long)le64_to_cpu(gd->bg_blkno));
180 return -EINVAL;
181 }
182
183 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
184 do_error("Group descriptor #%llu has an invalid "
185 "fs_generation of #%u",
186 (unsigned long long)bh->b_blocknr,
187 le32_to_cpu(gd->bg_generation));
188 return -EINVAL;
189 }
190
970e4936
JB
191 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
192 do_error("Group descriptor #%llu has bit count %u but "
193 "claims that %u are free",
194 (unsigned long long)bh->b_blocknr,
195 le16_to_cpu(gd->bg_bits),
196 le16_to_cpu(gd->bg_free_bits_count));
197 return -EINVAL;
198 }
199
200 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
201 do_error("Group descriptor #%llu has bit count %u but "
202 "max bitmap bits of %u",
203 (unsigned long long)bh->b_blocknr,
204 le16_to_cpu(gd->bg_bits),
205 8 * le16_to_cpu(gd->bg_size));
206 return -EINVAL;
207 }
208
209 return 0;
210}
211
212static int ocfs2_validate_gd_parent(struct super_block *sb,
213 struct ocfs2_dinode *di,
214 struct buffer_head *bh,
78c37eb0 215 int resize)
970e4936
JB
216{
217 unsigned int max_bits;
218 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
219
7bf72ede 220 if (di->i_blkno != gd->bg_parent_dinode) {
68f64d47 221 do_error("Group descriptor #%llu has bad parent "
57e3e797 222 "pointer (%llu, expected %llu)",
68f64d47 223 (unsigned long long)bh->b_blocknr,
57e3e797
JB
224 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
225 (unsigned long long)le64_to_cpu(di->i_blkno));
226 return -EINVAL;
7bf72ede
MF
227 }
228
229 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
230 if (le16_to_cpu(gd->bg_bits) > max_bits) {
68f64d47
JB
231 do_error("Group descriptor #%llu has bit count of %u",
232 (unsigned long long)bh->b_blocknr,
57e3e797
JB
233 le16_to_cpu(gd->bg_bits));
234 return -EINVAL;
7bf72ede
MF
235 }
236
78c37eb0
TM
237 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
238 if ((le16_to_cpu(gd->bg_chain) >
239 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
240 ((le16_to_cpu(gd->bg_chain) ==
241 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
68f64d47
JB
242 do_error("Group descriptor #%llu has bad chain %u",
243 (unsigned long long)bh->b_blocknr,
57e3e797
JB
244 le16_to_cpu(gd->bg_chain));
245 return -EINVAL;
7bf72ede
MF
246 }
247
970e4936
JB
248 return 0;
249}
7bf72ede 250
57e3e797 251#undef do_error
7bf72ede 252
970e4936
JB
253/*
254 * This version only prints errors. It does not fail the filesystem, and
255 * exists only for resize.
256 */
257int ocfs2_check_group_descriptor(struct super_block *sb,
258 struct ocfs2_dinode *di,
259 struct buffer_head *bh)
260{
261 int rc;
d6b32bbb
JB
262 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
263
264 BUG_ON(!buffer_uptodate(bh));
970e4936 265
d6b32bbb
JB
266 /*
267 * If the ecc fails, we return the error but otherwise
268 * leave the filesystem running. We know any error is
269 * local to this block.
270 */
271 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
13723d00
JB
272 if (rc) {
273 mlog(ML_ERROR,
274 "Checksum failed for group descriptor %llu\n",
275 (unsigned long long)bh->b_blocknr);
276 } else
d6b32bbb 277 rc = ocfs2_validate_gd_self(sb, bh, 1);
970e4936
JB
278 if (!rc)
279 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
280
281 return rc;
282}
283
284static int ocfs2_validate_group_descriptor(struct super_block *sb,
285 struct buffer_head *bh)
286{
d6b32bbb
JB
287 int rc;
288 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
289
970e4936
JB
290 mlog(0, "Validating group descriptor %llu\n",
291 (unsigned long long)bh->b_blocknr);
292
d6b32bbb
JB
293 BUG_ON(!buffer_uptodate(bh));
294
295 /*
296 * If the ecc fails, we return the error but otherwise
297 * leave the filesystem running. We know any error is
298 * local to this block.
299 */
300 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
301 if (rc)
302 return rc;
303
304 /*
305 * Errors after here are fatal.
306 */
307
970e4936 308 return ocfs2_validate_gd_self(sb, bh, 0);
7bf72ede
MF
309}
310
68f64d47
JB
311int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
312 u64 gd_blkno, struct buffer_head **bh)
313{
314 int rc;
315 struct buffer_head *tmp = *bh;
316
8cb471e8 317 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
970e4936 318 ocfs2_validate_group_descriptor);
68f64d47
JB
319 if (rc)
320 goto out;
321
970e4936 322 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
68f64d47
JB
323 if (rc) {
324 brelse(tmp);
325 goto out;
326 }
327
328 /* If ocfs2_read_block() got us a new bh, pass it up. */
329 if (!*bh)
330 *bh = tmp;
331
332out:
333 return rc;
334}
335
798db35f
JB
336static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
337 struct ocfs2_group_desc *bg,
338 struct ocfs2_chain_list *cl,
339 u64 p_blkno, u32 clusters)
340{
341 struct ocfs2_extent_list *el = &bg->bg_list;
342 struct ocfs2_extent_rec *rec;
343
344 BUG_ON(!ocfs2_supports_discontig_bh(osb));
345 if (!el->l_next_free_rec)
346 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
347 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
348 rec->e_blkno = p_blkno;
349 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
350 le16_to_cpu(cl->cl_bpc));
351 rec->e_leaf_clusters = cpu_to_le32(clusters);
352 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
353 le16_add_cpu(&el->l_next_free_rec, 1);
354}
355
1fabe148 356static int ocfs2_block_group_fill(handle_t *handle,
ccd979bd
MF
357 struct inode *alloc_inode,
358 struct buffer_head *bg_bh,
359 u64 group_blkno,
798db35f 360 unsigned int group_clusters,
ccd979bd
MF
361 u16 my_chain,
362 struct ocfs2_chain_list *cl)
363{
364 int status = 0;
798db35f 365 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
ccd979bd
MF
366 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
367 struct super_block * sb = alloc_inode->i_sb;
368
369 mlog_entry_void();
370
371 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
b0697053
MF
372 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
373 "b_blocknr (%llu)",
374 (unsigned long long)group_blkno,
ccd979bd
MF
375 (unsigned long long) bg_bh->b_blocknr);
376 status = -EIO;
377 goto bail;
378 }
379
13723d00 380 status = ocfs2_journal_access_gd(handle,
0cf2f763 381 INODE_CACHE(alloc_inode),
13723d00
JB
382 bg_bh,
383 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bd
MF
384 if (status < 0) {
385 mlog_errno(status);
386 goto bail;
387 }
388
389 memset(bg, 0, sb->s_blocksize);
390 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
391 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
4cbe4249 392 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1));
ccd979bd
MF
393 bg->bg_chain = cpu_to_le16(my_chain);
394 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
395 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
396 bg->bg_blkno = cpu_to_le64(group_blkno);
798db35f
JB
397 if (group_clusters == le16_to_cpu(cl->cl_cpg))
398 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
399 else
400 ocfs2_bg_discontig_add_extent(osb, bg, cl, bg->bg_blkno,
401 group_clusters);
402
ccd979bd
MF
403 /* set the 1st bit in the bitmap to account for the descriptor block */
404 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
405 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
406
ec20cec7 407 ocfs2_journal_dirty(handle, bg_bh);
ccd979bd
MF
408
409 /* There is no need to zero out or otherwise initialize the
410 * other blocks in a group - All valid FS metadata in a block
411 * group stores the superblock fs_generation value at
412 * allocation time. */
413
414bail:
415 mlog_exit(status);
416 return status;
417}
418
419static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
420{
421 u16 curr, best;
422
423 best = curr = 0;
424 while (curr < le16_to_cpu(cl->cl_count)) {
425 if (le32_to_cpu(cl->cl_recs[best].c_total) >
426 le32_to_cpu(cl->cl_recs[curr].c_total))
427 best = curr;
428 curr++;
429 }
430 return best;
431}
432
798db35f
JB
433static struct buffer_head *
434ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
435 struct inode *alloc_inode,
436 struct ocfs2_alloc_context *ac,
437 struct ocfs2_chain_list *cl)
438{
439 int status;
440 u32 bit_off, num_bits;
441 u64 bg_blkno;
442 struct buffer_head *bg_bh;
443 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
444
1ed9b777 445 status = ocfs2_claim_clusters(handle, ac,
798db35f
JB
446 le16_to_cpu(cl->cl_cpg), &bit_off,
447 &num_bits);
448 if (status < 0) {
449 if (status != -ENOSPC)
450 mlog_errno(status);
451 goto bail;
452 }
453
454 /* setup the group */
455 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
456 mlog(0, "new descriptor, record %u, at block %llu\n",
457 alloc_rec, (unsigned long long)bg_blkno);
458
459 bg_bh = sb_getblk(osb->sb, bg_blkno);
460 if (!bg_bh) {
461 status = -EIO;
462 mlog_errno(status);
463 goto bail;
464 }
465 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
466
467 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
468 bg_blkno, num_bits, alloc_rec, cl);
469 if (status < 0) {
470 brelse(bg_bh);
471 mlog_errno(status);
472 }
473
474bail:
475 return status ? ERR_PTR(status) : bg_bh;
476}
477
478static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
479 handle_t *handle,
480 struct ocfs2_alloc_context *ac,
481 unsigned int min_bits,
482 u32 *bit_off, u32 *num_bits)
483{
484 int status;
485
486 while (min_bits) {
1ed9b777 487 status = ocfs2_claim_clusters(handle, ac, min_bits,
798db35f
JB
488 bit_off, num_bits);
489 if (status != -ENOSPC)
490 break;
491
492 min_bits >>= 1;
493 }
494
495 return status;
496}
497
498static int ocfs2_block_group_grow_discontig(handle_t *handle,
499 struct inode *alloc_inode,
500 struct buffer_head *bg_bh,
501 struct ocfs2_alloc_context *ac,
502 struct ocfs2_chain_list *cl,
503 unsigned int min_bits)
504{
505 int status;
506 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
507 struct ocfs2_group_desc *bg =
508 (struct ocfs2_group_desc *)bg_bh->b_data;
509 unsigned int needed =
510 ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
511 u32 p_cpos, clusters;
512 u64 p_blkno;
513 struct ocfs2_extent_list *el = &bg->bg_list;
514
515 status = ocfs2_journal_access_gd(handle,
516 INODE_CACHE(alloc_inode),
517 bg_bh,
518 OCFS2_JOURNAL_ACCESS_CREATE);
519 if (status < 0) {
520 mlog_errno(status);
521 goto bail;
522 }
523
524 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
525 le16_to_cpu(el->l_count))) {
798db35f
JB
526 if (min_bits > needed)
527 min_bits = needed;
528 status = ocfs2_block_group_claim_bits(osb, handle, ac,
529 min_bits, &p_cpos,
530 &clusters);
531 if (status < 0) {
532 if (status != -ENOSPC)
533 mlog_errno(status);
534 goto bail;
535 }
536 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
537 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
538 clusters);
539
540 min_bits = clusters;
541 needed = ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
542 }
543
544 if (needed > 0) {
545 }
546
547 ocfs2_journal_dirty(handle, bg_bh);
548
549bail:
550 return status;
551}
552
8b06bc59
JB
553static void ocfs2_bg_alloc_cleanup(handle_t *handle,
554 struct ocfs2_alloc_context *cluster_ac,
555 struct inode *alloc_inode,
556 struct buffer_head *bg_bh)
798db35f 557{
8b06bc59 558 int i, ret;
798db35f
JB
559 struct ocfs2_group_desc *bg;
560 struct ocfs2_extent_list *el;
561 struct ocfs2_extent_rec *rec;
562
563 if (!bg_bh)
564 return;
565
566 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
567 el = &bg->bg_list;
568 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
569 rec = &el->l_recs[i];
8b06bc59
JB
570 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
571 cluster_ac->ac_bh,
572 le64_to_cpu(rec->e_blkno),
573 le32_to_cpu(rec->e_leaf_clusters));
574 if (ret)
575 mlog_errno(ret);
576 /* Try all the clusters to free */
798db35f
JB
577 }
578
579 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
580 brelse(bg_bh);
581}
582
583static struct buffer_head *
584ocfs2_block_group_alloc_discontig(handle_t *handle,
585 struct inode *alloc_inode,
586 struct ocfs2_alloc_context *ac,
8b06bc59 587 struct ocfs2_chain_list *cl)
798db35f
JB
588{
589 int status;
590 u32 bit_off, num_bits;
591 u64 bg_blkno;
592 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
593 struct buffer_head *bg_bh = NULL;
594 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
595 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
596
597 if (!ocfs2_supports_discontig_bh(osb)) {
598 status = -ENOSPC;
599 goto bail;
600 }
601
8b06bc59
JB
602 status = ocfs2_extend_trans(handle,
603 ocfs2_calc_bg_discontig_credits(osb->sb));
604 if (status) {
605 mlog_errno(status);
606 goto bail;
607 }
608
95ec0adf
JB
609 /*
610 * We're going to be grabbing from multiple cluster groups.
611 * We don't have enough credits to relink them all, and the
612 * cluster groups will be staying in cache for the duration of
613 * this operation.
614 */
615 ac->ac_allow_chain_relink = 0;
616
798db35f
JB
617 /* Claim the first region */
618 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
619 &bit_off, &num_bits);
620 if (status < 0) {
621 if (status != -ENOSPC)
622 mlog_errno(status);
623 goto bail;
624 }
625 min_bits = num_bits;
626
627 /* setup the group */
628 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
629 mlog(0, "new descriptor, record %u, at block %llu\n",
630 alloc_rec, (unsigned long long)bg_blkno);
631
632 bg_bh = sb_getblk(osb->sb, bg_blkno);
633 if (!bg_bh) {
634 status = -EIO;
635 mlog_errno(status);
636 goto bail;
637 }
638 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
639
640 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
641 bg_blkno, num_bits, alloc_rec, cl);
642 if (status < 0) {
643 mlog_errno(status);
644 goto bail;
645 }
646
647 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
648 bg_bh, ac, cl, min_bits);
649 if (status)
650 mlog_errno(status);
651
652bail:
653 if (status)
8b06bc59 654 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
798db35f
JB
655 return status ? ERR_PTR(status) : bg_bh;
656}
657
ccd979bd
MF
658/*
659 * We expect the block group allocator to already be locked.
660 */
661static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
662 struct inode *alloc_inode,
1187c968 663 struct buffer_head *bh,
60ca81e8 664 u64 max_block,
feb473a6 665 u64 *last_alloc_group,
60ca81e8 666 int flags)
ccd979bd
MF
667{
668 int status, credits;
669 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
670 struct ocfs2_chain_list *cl;
671 struct ocfs2_alloc_context *ac = NULL;
1fabe148 672 handle_t *handle = NULL;
ccd979bd
MF
673 u64 bg_blkno;
674 struct buffer_head *bg_bh = NULL;
675 struct ocfs2_group_desc *bg;
676
677 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
678
679 mlog_entry_void();
680
ccd979bd 681 cl = &fe->id2.i_chain;
1187c968
JB
682 status = ocfs2_reserve_clusters_with_limit(osb,
683 le16_to_cpu(cl->cl_cpg),
60ca81e8 684 max_block, flags, &ac);
ccd979bd
MF
685 if (status < 0) {
686 if (status != -ENOSPC)
687 mlog_errno(status);
688 goto bail;
689 }
690
691 credits = ocfs2_calc_group_alloc_credits(osb->sb,
692 le16_to_cpu(cl->cl_cpg));
65eff9cc 693 handle = ocfs2_start_trans(osb, credits);
ccd979bd
MF
694 if (IS_ERR(handle)) {
695 status = PTR_ERR(handle);
696 handle = NULL;
697 mlog_errno(status);
698 goto bail;
699 }
700
feb473a6
TM
701 if (last_alloc_group && *last_alloc_group != 0) {
702 mlog(0, "use old allocation group %llu for block group alloc\n",
703 (unsigned long long)*last_alloc_group);
704 ac->ac_last_group = *last_alloc_group;
705 }
798db35f
JB
706
707 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
708 ac, cl);
709 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
710 bg_bh = ocfs2_block_group_alloc_discontig(handle,
711 alloc_inode,
8b06bc59 712 ac, cl);
798db35f
JB
713 if (IS_ERR(bg_bh)) {
714 status = PTR_ERR(bg_bh);
715 bg_bh = NULL;
ccd979bd
MF
716 if (status != -ENOSPC)
717 mlog_errno(status);
718 goto bail;
719 }
ccd979bd
MF
720 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
721
0cf2f763 722 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
13723d00 723 bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
724 if (status < 0) {
725 mlog_errno(status);
726 goto bail;
727 }
728
798db35f 729 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_free,
ccd979bd 730 le16_to_cpu(bg->bg_free_bits_count));
798db35f
JB
731 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_total,
732 le16_to_cpu(bg->bg_bits));
733 cl->cl_recs[bg->bg_chain].c_blkno = cpu_to_le64(bg_blkno);
ccd979bd
MF
734 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
735 le16_add_cpu(&cl->cl_next_free_rec, 1);
736
737 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
738 le16_to_cpu(bg->bg_free_bits_count));
739 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
740 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
741
ec20cec7 742 ocfs2_journal_dirty(handle, bh);
ccd979bd
MF
743
744 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
745 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
746 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
747 le32_to_cpu(fe->i_clusters)));
748 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
749 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
8110b073 750 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
ccd979bd
MF
751
752 status = 0;
feb473a6
TM
753
754 /* save the new last alloc group so that the caller can cache it. */
755 if (last_alloc_group)
756 *last_alloc_group = ac->ac_last_group;
757
ccd979bd
MF
758bail:
759 if (handle)
02dc1af4 760 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
761
762 if (ac)
763 ocfs2_free_alloc_context(ac);
764
a81cb88b 765 brelse(bg_bh);
ccd979bd
MF
766
767 mlog_exit(status);
768 return status;
769}
770
771static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
da5cbf2f
MF
772 struct ocfs2_alloc_context *ac,
773 int type,
ffda89a3 774 u32 slot,
feb473a6 775 u64 *last_alloc_group,
60ca81e8 776 int flags)
ccd979bd
MF
777{
778 int status;
779 u32 bits_wanted = ac->ac_bits_wanted;
da5cbf2f 780 struct inode *alloc_inode;
ccd979bd 781 struct buffer_head *bh = NULL;
ccd979bd
MF
782 struct ocfs2_dinode *fe;
783 u32 free_bits;
784
785 mlog_entry_void();
786
da5cbf2f
MF
787 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
788 if (!alloc_inode) {
789 mlog_errno(-EINVAL);
790 return -EINVAL;
791 }
ccd979bd 792
da5cbf2f
MF
793 mutex_lock(&alloc_inode->i_mutex);
794
e63aecb6 795 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
ccd979bd 796 if (status < 0) {
da5cbf2f
MF
797 mutex_unlock(&alloc_inode->i_mutex);
798 iput(alloc_inode);
799
ccd979bd 800 mlog_errno(status);
da5cbf2f 801 return status;
ccd979bd
MF
802 }
803
da5cbf2f 804 ac->ac_inode = alloc_inode;
a4a48911 805 ac->ac_alloc_slot = slot;
da5cbf2f 806
ccd979bd 807 fe = (struct ocfs2_dinode *) bh->b_data;
10995aa2
JB
808
809 /* The bh was validated by the inode read inside
810 * ocfs2_inode_lock(). Any corruption is a code bug. */
811 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
812
ccd979bd 813 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
b0697053
MF
814 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
815 (unsigned long long)le64_to_cpu(fe->i_blkno));
ccd979bd
MF
816 status = -EIO;
817 goto bail;
818 }
819
820 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
821 le32_to_cpu(fe->id1.bitmap1.i_used);
822
823 if (bits_wanted > free_bits) {
824 /* cluster bitmap never grows */
825 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
826 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
827 bits_wanted, free_bits);
828 status = -ENOSPC;
829 goto bail;
830 }
831
60ca81e8 832 if (!(flags & ALLOC_NEW_GROUP)) {
ffda89a3
TM
833 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
834 "and we don't alloc a new group for it.\n",
835 slot, bits_wanted, free_bits);
836 status = -ENOSPC;
837 goto bail;
838 }
839
1187c968 840 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
feb473a6
TM
841 ac->ac_max_block,
842 last_alloc_group, flags);
ccd979bd
MF
843 if (status < 0) {
844 if (status != -ENOSPC)
845 mlog_errno(status);
846 goto bail;
847 }
848 atomic_inc(&osb->alloc_stats.bg_extends);
849
850 /* You should never ask for this much metadata */
851 BUG_ON(bits_wanted >
852 (le32_to_cpu(fe->id1.bitmap1.i_total)
853 - le32_to_cpu(fe->id1.bitmap1.i_used)));
854 }
855
856 get_bh(bh);
857 ac->ac_bh = bh;
858bail:
a81cb88b 859 brelse(bh);
ccd979bd
MF
860
861 mlog_exit(status);
862 return status;
863}
864
b89c5428
TY
865static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
866{
867 spin_lock(&osb->osb_lock);
868 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
869 spin_unlock(&osb->osb_lock);
870 atomic_set(&osb->s_num_inodes_stolen, 0);
871}
872
873static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
874{
875 spin_lock(&osb->osb_lock);
876 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
877 spin_unlock(&osb->osb_lock);
878 atomic_set(&osb->s_num_meta_stolen, 0);
879}
880
881void ocfs2_init_steal_slots(struct ocfs2_super *osb)
882{
883 ocfs2_init_inode_steal_slot(osb);
884 ocfs2_init_meta_steal_slot(osb);
885}
886
887static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
888{
889 spin_lock(&osb->osb_lock);
890 if (type == INODE_ALLOC_SYSTEM_INODE)
891 osb->s_inode_steal_slot = slot;
892 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
893 osb->s_meta_steal_slot = slot;
894 spin_unlock(&osb->osb_lock);
895}
896
897static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
898{
899 int slot = OCFS2_INVALID_SLOT;
900
901 spin_lock(&osb->osb_lock);
902 if (type == INODE_ALLOC_SYSTEM_INODE)
903 slot = osb->s_inode_steal_slot;
904 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
905 slot = osb->s_meta_steal_slot;
906 spin_unlock(&osb->osb_lock);
907
908 return slot;
909}
910
911static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
912{
913 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
914}
915
916static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
917{
918 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
919}
920
921static int ocfs2_steal_resource(struct ocfs2_super *osb,
922 struct ocfs2_alloc_context *ac,
923 int type)
924{
925 int i, status = -ENOSPC;
926 int slot = __ocfs2_get_steal_slot(osb, type);
927
928 /* Start to steal resource from the first slot after ours. */
929 if (slot == OCFS2_INVALID_SLOT)
930 slot = osb->slot_num + 1;
931
932 for (i = 0; i < osb->max_slots; i++, slot++) {
933 if (slot == osb->max_slots)
934 slot = 0;
935
936 if (slot == osb->slot_num)
937 continue;
938
939 status = ocfs2_reserve_suballoc_bits(osb, ac,
940 type,
941 (u32)slot, NULL,
942 NOT_ALLOC_NEW_GROUP);
943 if (status >= 0) {
944 __ocfs2_set_steal_slot(osb, slot, type);
945 break;
946 }
947
948 ocfs2_free_ac_resource(ac);
949 }
950
951 return status;
952}
953
954static int ocfs2_steal_inode(struct ocfs2_super *osb,
955 struct ocfs2_alloc_context *ac)
956{
957 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
958}
959
960static int ocfs2_steal_meta(struct ocfs2_super *osb,
961 struct ocfs2_alloc_context *ac)
962{
963 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
964}
965
cf1d6c76
TY
966int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
967 int blocks,
968 struct ocfs2_alloc_context **ac)
ccd979bd
MF
969{
970 int status;
b89c5428 971 int slot = ocfs2_get_meta_steal_slot(osb);
ccd979bd 972
cd861280 973 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bd
MF
974 if (!(*ac)) {
975 status = -ENOMEM;
976 mlog_errno(status);
977 goto bail;
978 }
979
cf1d6c76 980 (*ac)->ac_bits_wanted = blocks;
ccd979bd 981 (*ac)->ac_which = OCFS2_AC_USE_META;
ccd979bd
MF
982 (*ac)->ac_group_search = ocfs2_block_group_search;
983
b89c5428
TY
984 if (slot != OCFS2_INVALID_SLOT &&
985 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
986 goto extent_steal;
987
988 atomic_set(&osb->s_num_meta_stolen, 0);
da5cbf2f 989 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
ffda89a3 990 EXTENT_ALLOC_SYSTEM_INODE,
b89c5428 991 (u32)osb->slot_num, NULL,
33d5d380 992 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
b89c5428
TY
993
994
995 if (status >= 0) {
996 status = 0;
997 if (slot != OCFS2_INVALID_SLOT)
998 ocfs2_init_meta_steal_slot(osb);
999 goto bail;
1000 } else if (status < 0 && status != -ENOSPC) {
1001 mlog_errno(status);
1002 goto bail;
1003 }
1004
1005 ocfs2_free_ac_resource(*ac);
1006
1007extent_steal:
1008 status = ocfs2_steal_meta(osb, *ac);
1009 atomic_inc(&osb->s_num_meta_stolen);
ccd979bd
MF
1010 if (status < 0) {
1011 if (status != -ENOSPC)
1012 mlog_errno(status);
1013 goto bail;
1014 }
1015
1016 status = 0;
1017bail:
1018 if ((status < 0) && *ac) {
1019 ocfs2_free_alloc_context(*ac);
1020 *ac = NULL;
1021 }
1022
ccd979bd
MF
1023 mlog_exit(status);
1024 return status;
1025}
1026
cf1d6c76
TY
1027int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1028 struct ocfs2_extent_list *root_el,
1029 struct ocfs2_alloc_context **ac)
1030{
1031 return ocfs2_reserve_new_metadata_blocks(osb,
1032 ocfs2_extend_meta_needed(root_el),
1033 ac);
1034}
1035
ccd979bd 1036int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
ccd979bd
MF
1037 struct ocfs2_alloc_context **ac)
1038{
1039 int status;
b89c5428 1040 int slot = ocfs2_get_inode_steal_slot(osb);
feb473a6 1041 u64 alloc_group;
ccd979bd 1042
cd861280 1043 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bd
MF
1044 if (!(*ac)) {
1045 status = -ENOMEM;
1046 mlog_errno(status);
1047 goto bail;
1048 }
1049
1050 (*ac)->ac_bits_wanted = 1;
ccd979bd
MF
1051 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1052
ccd979bd
MF
1053 (*ac)->ac_group_search = ocfs2_block_group_search;
1054
1187c968
JB
1055 /*
1056 * stat(2) can't handle i_ino > 32bits, so we tell the
1057 * lower levels not to allocate us a block group past that
12462f1d 1058 * limit. The 'inode64' mount option avoids this behavior.
1187c968 1059 */
12462f1d
JB
1060 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1061 (*ac)->ac_max_block = (u32)~0U;
1187c968 1062
4d0ddb2c
TM
1063 /*
1064 * slot is set when we successfully steal inode from other nodes.
1065 * It is reset in 3 places:
1066 * 1. when we flush the truncate log
1067 * 2. when we complete local alloc recovery.
1068 * 3. when we successfully allocate from our own slot.
1069 * After it is set, we will go on stealing inodes until we find the
1070 * need to check our slots to see whether there is some space for us.
1071 */
1072 if (slot != OCFS2_INVALID_SLOT &&
b89c5428 1073 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
4d0ddb2c
TM
1074 goto inode_steal;
1075
1076 atomic_set(&osb->s_num_inodes_stolen, 0);
feb473a6 1077 alloc_group = osb->osb_inode_alloc_group;
da5cbf2f
MF
1078 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1079 INODE_ALLOC_SYSTEM_INODE,
b89c5428 1080 (u32)osb->slot_num,
feb473a6 1081 &alloc_group,
60ca81e8
TM
1082 ALLOC_NEW_GROUP |
1083 ALLOC_GROUPS_FROM_GLOBAL);
4d0ddb2c
TM
1084 if (status >= 0) {
1085 status = 0;
1086
feb473a6
TM
1087 spin_lock(&osb->osb_lock);
1088 osb->osb_inode_alloc_group = alloc_group;
1089 spin_unlock(&osb->osb_lock);
1090 mlog(0, "after reservation, new allocation group is "
1091 "%llu\n", (unsigned long long)alloc_group);
1092
4d0ddb2c
TM
1093 /*
1094 * Some inodes must be freed by us, so try to allocate
1095 * from our own next time.
1096 */
1097 if (slot != OCFS2_INVALID_SLOT)
1098 ocfs2_init_inode_steal_slot(osb);
1099 goto bail;
1100 } else if (status < 0 && status != -ENOSPC) {
1101 mlog_errno(status);
1102 goto bail;
1103 }
1104
1105 ocfs2_free_ac_resource(*ac);
1106
1107inode_steal:
b89c5428 1108 status = ocfs2_steal_inode(osb, *ac);
4d0ddb2c 1109 atomic_inc(&osb->s_num_inodes_stolen);
ccd979bd
MF
1110 if (status < 0) {
1111 if (status != -ENOSPC)
1112 mlog_errno(status);
1113 goto bail;
1114 }
1115
1116 status = 0;
1117bail:
1118 if ((status < 0) && *ac) {
1119 ocfs2_free_alloc_context(*ac);
1120 *ac = NULL;
1121 }
1122
ccd979bd
MF
1123 mlog_exit(status);
1124 return status;
1125}
1126
1127/* local alloc code has to do the same thing, so rather than do this
1128 * twice.. */
1129int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1130 struct ocfs2_alloc_context *ac)
1131{
1132 int status;
1133
ccd979bd
MF
1134 ac->ac_which = OCFS2_AC_USE_MAIN;
1135 ac->ac_group_search = ocfs2_cluster_group_search;
1136
da5cbf2f
MF
1137 status = ocfs2_reserve_suballoc_bits(osb, ac,
1138 GLOBAL_BITMAP_SYSTEM_INODE,
feb473a6 1139 OCFS2_INVALID_SLOT, NULL,
ffda89a3 1140 ALLOC_NEW_GROUP);
da5cbf2f 1141 if (status < 0 && status != -ENOSPC) {
ccd979bd 1142 mlog_errno(status);
da5cbf2f
MF
1143 goto bail;
1144 }
1145
ccd979bd
MF
1146bail:
1147 return status;
1148}
1149
1150/* Callers don't need to care which bitmap (local alloc or main) to
1151 * use so we figure it out for them, but unfortunately this clutters
1152 * things a bit. */
1187c968
JB
1153static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1154 u32 bits_wanted, u64 max_block,
60ca81e8 1155 int flags,
1187c968 1156 struct ocfs2_alloc_context **ac)
ccd979bd
MF
1157{
1158 int status;
1159
1160 mlog_entry_void();
1161
cd861280 1162 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bd
MF
1163 if (!(*ac)) {
1164 status = -ENOMEM;
1165 mlog_errno(status);
1166 goto bail;
1167 }
1168
1169 (*ac)->ac_bits_wanted = bits_wanted;
1187c968 1170 (*ac)->ac_max_block = max_block;
ccd979bd
MF
1171
1172 status = -ENOSPC;
60ca81e8
TM
1173 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1174 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
ccd979bd 1175 status = ocfs2_reserve_local_alloc_bits(osb,
ccd979bd
MF
1176 bits_wanted,
1177 *ac);
a57c8fd2 1178 if ((status < 0) && (status != -ENOSPC)) {
ccd979bd
MF
1179 mlog_errno(status);
1180 goto bail;
ccd979bd
MF
1181 }
1182 }
1183
1184 if (status == -ENOSPC) {
1185 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1186 if (status < 0) {
1187 if (status != -ENOSPC)
1188 mlog_errno(status);
1189 goto bail;
1190 }
1191 }
1192
1193 status = 0;
1194bail:
1195 if ((status < 0) && *ac) {
1196 ocfs2_free_alloc_context(*ac);
1197 *ac = NULL;
1198 }
1199
1200 mlog_exit(status);
1201 return status;
1202}
1203
1187c968
JB
1204int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1205 u32 bits_wanted,
1206 struct ocfs2_alloc_context **ac)
1207{
60ca81e8
TM
1208 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1209 ALLOC_NEW_GROUP, ac);
1187c968
JB
1210}
1211
ccd979bd
MF
1212/*
1213 * More or less lifted from ext3. I'll leave their description below:
1214 *
1215 * "For ext3 allocations, we must not reuse any blocks which are
1216 * allocated in the bitmap buffer's "last committed data" copy. This
1217 * prevents deletes from freeing up the page for reuse until we have
1218 * committed the delete transaction.
1219 *
1220 * If we didn't do this, then deleting something and reallocating it as
1221 * data would allow the old block to be overwritten before the
1222 * transaction committed (because we force data to disk before commit).
1223 * This would lead to corruption if we crashed between overwriting the
1224 * data and committing the delete.
1225 *
1226 * @@@ We may want to make this allocation behaviour conditional on
1227 * data-writes at some point, and disable it for metadata allocations or
1228 * sync-data inodes."
1229 *
1230 * Note: OCFS2 already does this differently for metadata vs data
c78bad11 1231 * allocations, as those bitmaps are separate and undo access is never
ccd979bd
MF
1232 * called on a metadata group descriptor.
1233 */
1234static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1235 int nr)
1236{
1237 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
94e41ecf 1238 int ret;
ccd979bd
MF
1239
1240 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1241 return 0;
94e41ecf
SM
1242
1243 if (!buffer_jbd(bg_bh))
ccd979bd
MF
1244 return 1;
1245
94e41ecf 1246 jbd_lock_bh_state(bg_bh);
ccd979bd 1247 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
94e41ecf
SM
1248 if (bg)
1249 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1250 else
1251 ret = 1;
1252 jbd_unlock_bh_state(bg_bh);
1253
1254 return ret;
ccd979bd
MF
1255}
1256
1257static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1258 struct buffer_head *bg_bh,
1259 unsigned int bits_wanted,
7bf72ede 1260 unsigned int total_bits,
7d1fe093 1261 struct ocfs2_suballoc_result *res)
ccd979bd
MF
1262{
1263 void *bitmap;
1264 u16 best_offset, best_size;
1265 int offset, start, found, status = 0;
1266 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1267
42035306
JB
1268 /* Callers got this descriptor from
1269 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1270 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
ccd979bd
MF
1271
1272 found = start = best_offset = best_size = 0;
1273 bitmap = bg->bg_bitmap;
1274
7bf72ede
MF
1275 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1276 if (offset == total_bits)
ccd979bd
MF
1277 break;
1278
1279 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1280 /* We found a zero, but we can't use it as it
1281 * hasn't been put to disk yet! */
1282 found = 0;
1283 start = offset + 1;
1284 } else if (offset == start) {
1285 /* we found a zero */
1286 found++;
1287 /* move start to the next bit to test */
1288 start++;
1289 } else {
1290 /* got a zero after some ones */
1291 found = 1;
1292 start = offset + 1;
1293 }
1294 if (found > best_size) {
1295 best_size = found;
1296 best_offset = start - found;
1297 }
1298 /* we got everything we needed */
1299 if (found == bits_wanted) {
1300 /* mlog(0, "Found it all!\n"); */
1301 break;
1302 }
1303 }
1304
7d1fe093
JB
1305 if (best_size) {
1306 res->sr_bit_offset = best_offset;
1307 res->sr_bits = best_size;
ccd979bd
MF
1308 } else {
1309 status = -ENOSPC;
1310 /* No error log here -- see the comment above
1311 * ocfs2_test_bg_bit_allocatable */
1312 }
1313
1314 return status;
1315}
1316
1fabe148 1317static inline int ocfs2_block_group_set_bits(handle_t *handle,
ccd979bd
MF
1318 struct inode *alloc_inode,
1319 struct ocfs2_group_desc *bg,
1320 struct buffer_head *group_bh,
1321 unsigned int bit_off,
1322 unsigned int num_bits)
1323{
1324 int status;
1325 void *bitmap = bg->bg_bitmap;
1326 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1327
1328 mlog_entry_void();
1329
42035306
JB
1330 /* All callers get the descriptor via
1331 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1332 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
ccd979bd
MF
1333 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1334
1335 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1336 num_bits);
1337
1338 if (ocfs2_is_cluster_bitmap(alloc_inode))
1339 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1340
13723d00 1341 status = ocfs2_journal_access_gd(handle,
0cf2f763 1342 INODE_CACHE(alloc_inode),
13723d00
JB
1343 group_bh,
1344 journal_type);
ccd979bd
MF
1345 if (status < 0) {
1346 mlog_errno(status);
1347 goto bail;
1348 }
1349
1350 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
ccd979bd
MF
1351 while(num_bits--)
1352 ocfs2_set_bit(bit_off++, bitmap);
1353
ec20cec7 1354 ocfs2_journal_dirty(handle, group_bh);
ccd979bd
MF
1355
1356bail:
1357 mlog_exit(status);
1358 return status;
1359}
1360
1361/* find the one with the most empty bits */
1362static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1363{
1364 u16 curr, best;
1365
1366 BUG_ON(!cl->cl_next_free_rec);
1367
1368 best = curr = 0;
1369 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1370 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1371 le32_to_cpu(cl->cl_recs[best].c_free))
1372 best = curr;
1373 curr++;
1374 }
1375
1376 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1377 return best;
1378}
1379
1fabe148 1380static int ocfs2_relink_block_group(handle_t *handle,
ccd979bd
MF
1381 struct inode *alloc_inode,
1382 struct buffer_head *fe_bh,
1383 struct buffer_head *bg_bh,
1384 struct buffer_head *prev_bg_bh,
1385 u16 chain)
1386{
1387 int status;
1388 /* there is a really tiny chance the journal calls could fail,
1389 * but we wouldn't want inconsistent blocks in *any* case. */
1390 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1391 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1392 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1393 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1394
42035306
JB
1395 /* The caller got these descriptors from
1396 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1397 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1398 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
ccd979bd 1399
b0697053 1400 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
1ca1a111
MF
1401 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1402 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1403 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
ccd979bd
MF
1404
1405 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1406 bg_ptr = le64_to_cpu(bg->bg_next_group);
1407 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1408
0cf2f763
JB
1409 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1410 prev_bg_bh,
13723d00 1411 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1412 if (status < 0) {
1413 mlog_errno(status);
1414 goto out_rollback;
1415 }
1416
1417 prev_bg->bg_next_group = bg->bg_next_group;
ec20cec7 1418 ocfs2_journal_dirty(handle, prev_bg_bh);
ccd979bd 1419
0cf2f763
JB
1420 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1421 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1422 if (status < 0) {
1423 mlog_errno(status);
1424 goto out_rollback;
1425 }
1426
1427 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
ec20cec7 1428 ocfs2_journal_dirty(handle, bg_bh);
ccd979bd 1429
0cf2f763
JB
1430 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1431 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1432 if (status < 0) {
1433 mlog_errno(status);
1434 goto out_rollback;
1435 }
1436
1437 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
ec20cec7 1438 ocfs2_journal_dirty(handle, fe_bh);
ccd979bd 1439
ccd979bd
MF
1440out_rollback:
1441 if (status < 0) {
1442 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1443 bg->bg_next_group = cpu_to_le64(bg_ptr);
1444 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1445 }
42035306 1446
ccd979bd
MF
1447 mlog_exit(status);
1448 return status;
1449}
1450
1451static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1452 u32 wanted)
1453{
1454 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1455}
1456
1457/* return 0 on success, -ENOSPC to keep searching and any other < 0
1458 * value on error. */
1459static int ocfs2_cluster_group_search(struct inode *inode,
1460 struct buffer_head *group_bh,
1461 u32 bits_wanted, u32 min_bits,
1187c968 1462 u64 max_block,
7d1fe093 1463 struct ocfs2_suballoc_result *res)
ccd979bd
MF
1464{
1465 int search = -ENOSPC;
1466 int ret;
1187c968 1467 u64 blkoff;
7bf72ede 1468 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
9c7af40b 1469 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7bf72ede 1470 unsigned int max_bits, gd_cluster_off;
ccd979bd
MF
1471
1472 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1473
7bf72ede
MF
1474 if (gd->bg_free_bits_count) {
1475 max_bits = le16_to_cpu(gd->bg_bits);
1476
1477 /* Tail groups in cluster bitmaps which aren't cpg
1478 * aligned are prone to partial extention by a failed
1479 * fs resize. If the file system resize never got to
1480 * update the dinode cluster count, then we don't want
1481 * to trust any clusters past it, regardless of what
1482 * the group descriptor says. */
1483 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1484 le64_to_cpu(gd->bg_blkno));
1485 if ((gd_cluster_off + max_bits) >
1486 OCFS2_I(inode)->ip_clusters) {
1487 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1488 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1489 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1490 le16_to_cpu(gd->bg_bits),
1491 OCFS2_I(inode)->ip_clusters, max_bits);
1492 }
1493
ccd979bd
MF
1494 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1495 group_bh, bits_wanted,
7d1fe093 1496 max_bits, res);
ccd979bd
MF
1497 if (ret)
1498 return ret;
1499
1187c968
JB
1500 if (max_block) {
1501 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1502 gd_cluster_off +
7d1fe093
JB
1503 res->sr_bit_offset +
1504 res->sr_bits);
1187c968
JB
1505 mlog(0, "Checking %llu against %llu\n",
1506 (unsigned long long)blkoff,
1507 (unsigned long long)max_block);
1508 if (blkoff > max_block)
1509 return -ENOSPC;
1510 }
1511
ccd979bd
MF
1512 /* ocfs2_block_group_find_clear_bits() might
1513 * return success, but we still want to return
1514 * -ENOSPC unless it found the minimum number
1515 * of bits. */
7d1fe093 1516 if (min_bits <= res->sr_bits)
ccd979bd 1517 search = 0; /* success */
7d1fe093 1518 else if (res->sr_bits) {
9c7af40b
MF
1519 /*
1520 * Don't show bits which we'll be returning
1521 * for allocation to the local alloc bitmap.
1522 */
7d1fe093 1523 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
ccd979bd
MF
1524 }
1525 }
1526
1527 return search;
1528}
1529
1530static int ocfs2_block_group_search(struct inode *inode,
1531 struct buffer_head *group_bh,
1532 u32 bits_wanted, u32 min_bits,
1187c968 1533 u64 max_block,
7d1fe093 1534 struct ocfs2_suballoc_result *res)
ccd979bd
MF
1535{
1536 int ret = -ENOSPC;
1187c968 1537 u64 blkoff;
ccd979bd
MF
1538 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1539
1540 BUG_ON(min_bits != 1);
1541 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1542
1187c968 1543 if (bg->bg_free_bits_count) {
ccd979bd
MF
1544 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1545 group_bh, bits_wanted,
7bf72ede 1546 le16_to_cpu(bg->bg_bits),
7d1fe093 1547 res);
1187c968 1548 if (!ret && max_block) {
7d1fe093
JB
1549 blkoff = le64_to_cpu(bg->bg_blkno) +
1550 res->sr_bit_offset + res->sr_bits;
1187c968
JB
1551 mlog(0, "Checking %llu against %llu\n",
1552 (unsigned long long)blkoff,
1553 (unsigned long long)max_block);
1554 if (blkoff > max_block)
1555 ret = -ENOSPC;
1556 }
1557 }
ccd979bd
MF
1558
1559 return ret;
1560}
1561
883d4cae 1562static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1fabe148 1563 handle_t *handle,
883d4cae
MF
1564 struct buffer_head *di_bh,
1565 u32 num_bits,
1566 u16 chain)
1567{
1568 int ret;
1569 u32 tmp_used;
1570 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1571 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1572
0cf2f763 1573 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
13723d00 1574 OCFS2_JOURNAL_ACCESS_WRITE);
883d4cae
MF
1575 if (ret < 0) {
1576 mlog_errno(ret);
1577 goto out;
1578 }
1579
1580 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1581 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1582 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
ec20cec7 1583 ocfs2_journal_dirty(handle, di_bh);
883d4cae
MF
1584
1585out:
1586 return ret;
1587}
1588
ba206635
JB
1589static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1590 struct ocfs2_extent_rec *rec,
1591 struct ocfs2_chain_list *cl)
13e434cf
JB
1592{
1593 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1594 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1595 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1596
1597 if (res->sr_bit_offset < bitoff)
1598 return 0;
1599 if (res->sr_bit_offset >= (bitoff + bitcount))
1600 return 0;
ba206635
JB
1601 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1602 (res->sr_bit_offset - bitoff);
13e434cf
JB
1603 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1604 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1605 return 1;
1606}
1607
ba206635
JB
1608static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1609 struct ocfs2_group_desc *bg,
1610 struct ocfs2_suballoc_result *res)
13e434cf
JB
1611{
1612 int i;
2b6cb576 1613 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
13e434cf
JB
1614 struct ocfs2_extent_rec *rec;
1615 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1616 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1617
ba206635
JB
1618 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1619 res->sr_blkno = 0;
13e434cf 1620 return;
ba206635 1621 }
13e434cf 1622
ba206635 1623 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
2b6cb576 1624 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
ba206635
JB
1625 if (!ocfs2_supports_discontig_bh(OCFS2_SB(ac->ac_inode->i_sb)) ||
1626 !bg->bg_list.l_next_free_rec)
13e434cf
JB
1627 return;
1628
1629 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1630 rec = &bg->bg_list.l_recs[i];
2b6cb576
JB
1631 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1632 res->sr_bg_blkno = bg_blkno; /* Restore */
13e434cf 1633 break;
2b6cb576 1634 }
13e434cf
JB
1635 }
1636}
1637
883d4cae 1638static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1fabe148 1639 handle_t *handle,
883d4cae
MF
1640 u32 bits_wanted,
1641 u32 min_bits,
7d1fe093 1642 struct ocfs2_suballoc_result *res,
883d4cae
MF
1643 u16 *bits_left)
1644{
1645 int ret;
883d4cae
MF
1646 struct buffer_head *group_bh = NULL;
1647 struct ocfs2_group_desc *gd;
68f64d47 1648 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
883d4cae 1649 struct inode *alloc_inode = ac->ac_inode;
883d4cae 1650
7d1fe093
JB
1651 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1652 res->sr_bg_blkno, &group_bh);
883d4cae
MF
1653 if (ret < 0) {
1654 mlog_errno(ret);
1655 return ret;
1656 }
1657
1658 gd = (struct ocfs2_group_desc *) group_bh->b_data;
883d4cae 1659 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
7d1fe093 1660 ac->ac_max_block, res);
883d4cae
MF
1661 if (ret < 0) {
1662 if (ret != -ENOSPC)
1663 mlog_errno(ret);
1664 goto out;
1665 }
1666
13e434cf 1667 if (!ret)
ba206635 1668 ocfs2_bg_discontig_fix_result(ac, gd, res);
13e434cf 1669
883d4cae 1670 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
7d1fe093 1671 res->sr_bits,
883d4cae
MF
1672 le16_to_cpu(gd->bg_chain));
1673 if (ret < 0) {
1674 mlog_errno(ret);
1675 goto out;
1676 }
1677
1678 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
7d1fe093 1679 res->sr_bit_offset, res->sr_bits);
883d4cae
MF
1680 if (ret < 0)
1681 mlog_errno(ret);
1682
1683 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1684
1685out:
1686 brelse(group_bh);
1687
1688 return ret;
1689}
1690
ccd979bd 1691static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1fabe148 1692 handle_t *handle,
ccd979bd
MF
1693 u32 bits_wanted,
1694 u32 min_bits,
7d1fe093 1695 struct ocfs2_suballoc_result *res,
883d4cae 1696 u16 *bits_left)
ccd979bd
MF
1697{
1698 int status;
7d1fe093 1699 u16 chain;
ccd979bd
MF
1700 u32 tmp_used;
1701 u64 next_group;
ccd979bd
MF
1702 struct inode *alloc_inode = ac->ac_inode;
1703 struct buffer_head *group_bh = NULL;
1704 struct buffer_head *prev_group_bh = NULL;
1705 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1706 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1707 struct ocfs2_group_desc *bg;
1708
1709 chain = ac->ac_chain;
b0697053
MF
1710 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1711 bits_wanted, chain,
1712 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
ccd979bd 1713
68f64d47
JB
1714 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1715 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1716 &group_bh);
ccd979bd
MF
1717 if (status < 0) {
1718 mlog_errno(status);
1719 goto bail;
1720 }
1721 bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bd
MF
1722
1723 status = -ENOSPC;
1724 /* for now, the chain search is a bit simplistic. We just use
1725 * the 1st group with any empty bits. */
1187c968
JB
1726 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1727 bits_wanted, min_bits,
7d1fe093
JB
1728 ac->ac_max_block,
1729 res)) == -ENOSPC) {
ccd979bd
MF
1730 if (!bg->bg_next_group)
1731 break;
a81cb88b
MF
1732
1733 brelse(prev_group_bh);
1734 prev_group_bh = NULL;
1735
ccd979bd
MF
1736 next_group = le64_to_cpu(bg->bg_next_group);
1737 prev_group_bh = group_bh;
1738 group_bh = NULL;
68f64d47
JB
1739 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1740 next_group, &group_bh);
ccd979bd
MF
1741 if (status < 0) {
1742 mlog_errno(status);
1743 goto bail;
1744 }
1745 bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bd
MF
1746 }
1747 if (status < 0) {
1748 if (status != -ENOSPC)
1749 mlog_errno(status);
1750 goto bail;
1751 }
1752
b0697053 1753 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
7d1fe093 1754 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
ccd979bd 1755
7d1fe093 1756 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
ccd979bd 1757
7d1fe093 1758 BUG_ON(res->sr_bits == 0);
13e434cf 1759 if (!status)
ba206635 1760 ocfs2_bg_discontig_fix_result(ac, bg, res);
13e434cf 1761
ccd979bd
MF
1762
1763 /*
1764 * Keep track of previous block descriptor read. When
1765 * we find a target, if we have read more than X
1766 * number of descriptors, and the target is reasonably
1767 * empty, relink him to top of his chain.
1768 *
1769 * We've read 0 extra blocks and only send one more to
1770 * the transaction, yet the next guy to search has a
1771 * much easier time.
1772 *
1773 * Do this *after* figuring out how many bits we're taking out
1774 * of our target group.
1775 */
1776 if (ac->ac_allow_chain_relink &&
1777 (prev_group_bh) &&
7d1fe093 1778 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
ccd979bd
MF
1779 status = ocfs2_relink_block_group(handle, alloc_inode,
1780 ac->ac_bh, group_bh,
1781 prev_group_bh, chain);
1782 if (status < 0) {
1783 mlog_errno(status);
1784 goto bail;
1785 }
1786 }
1787
1788 /* Ok, claim our bits now: set the info on dinode, chainlist
1789 * and then the group */
13723d00 1790 status = ocfs2_journal_access_di(handle,
0cf2f763 1791 INODE_CACHE(alloc_inode),
13723d00
JB
1792 ac->ac_bh,
1793 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1794 if (status < 0) {
1795 mlog_errno(status);
1796 goto bail;
1797 }
1798
1799 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
7d1fe093
JB
1800 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1801 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
ec20cec7 1802 ocfs2_journal_dirty(handle, ac->ac_bh);
ccd979bd
MF
1803
1804 status = ocfs2_block_group_set_bits(handle,
1805 alloc_inode,
1806 bg,
1807 group_bh,
7d1fe093
JB
1808 res->sr_bit_offset,
1809 res->sr_bits);
ccd979bd
MF
1810 if (status < 0) {
1811 mlog_errno(status);
1812 goto bail;
1813 }
1814
7d1fe093 1815 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
1ca1a111 1816 (unsigned long long)le64_to_cpu(fe->i_blkno));
ccd979bd 1817
883d4cae 1818 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
ccd979bd 1819bail:
a81cb88b
MF
1820 brelse(group_bh);
1821 brelse(prev_group_bh);
ccd979bd
MF
1822
1823 mlog_exit(status);
1824 return status;
1825}
1826
1827/* will give out up to bits_wanted contiguous bits. */
aa8f8e93 1828static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1fabe148 1829 handle_t *handle,
ccd979bd
MF
1830 u32 bits_wanted,
1831 u32 min_bits,
7d1fe093 1832 struct ocfs2_suballoc_result *res)
ccd979bd
MF
1833{
1834 int status;
1835 u16 victim, i;
883d4cae 1836 u16 bits_left = 0;
ccd979bd
MF
1837 struct ocfs2_chain_list *cl;
1838 struct ocfs2_dinode *fe;
1839
1840 mlog_entry_void();
1841
1842 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1843 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1844 BUG_ON(!ac->ac_bh);
1845
1846 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
10995aa2
JB
1847
1848 /* The bh was validated by the inode read during
1849 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1850 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1851
ccd979bd
MF
1852 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1853 le32_to_cpu(fe->id1.bitmap1.i_total)) {
aa8f8e93
JB
1854 ocfs2_error(ac->ac_inode->i_sb,
1855 "Chain allocator dinode %llu has %u used "
b0697053
MF
1856 "bits but only %u total.",
1857 (unsigned long long)le64_to_cpu(fe->i_blkno),
ccd979bd
MF
1858 le32_to_cpu(fe->id1.bitmap1.i_used),
1859 le32_to_cpu(fe->id1.bitmap1.i_total));
1860 status = -EIO;
1861 goto bail;
1862 }
1863
7d1fe093
JB
1864 res->sr_bg_blkno = ac->ac_last_group;
1865 if (res->sr_bg_blkno) {
883d4cae
MF
1866 /* Attempt to short-circuit the usual search mechanism
1867 * by jumping straight to the most recently used
1868 * allocation group. This helps us mantain some
1869 * contiguousness across allocations. */
da5cbf2f 1870 status = ocfs2_search_one_group(ac, handle, bits_wanted,
7d1fe093
JB
1871 min_bits, res, &bits_left);
1872 if (!status)
883d4cae 1873 goto set_hint;
883d4cae
MF
1874 if (status < 0 && status != -ENOSPC) {
1875 mlog_errno(status);
1876 goto bail;
1877 }
1878 }
1879
ccd979bd
MF
1880 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1881
1882 victim = ocfs2_find_victim_chain(cl);
1883 ac->ac_chain = victim;
1884 ac->ac_allow_chain_relink = 1;
1885
7d1fe093
JB
1886 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1887 res, &bits_left);
ccd979bd 1888 if (!status)
883d4cae 1889 goto set_hint;
ccd979bd
MF
1890 if (status < 0 && status != -ENOSPC) {
1891 mlog_errno(status);
1892 goto bail;
1893 }
1894
1895 mlog(0, "Search of victim chain %u came up with nothing, "
1896 "trying all chains now.\n", victim);
1897
1898 /* If we didn't pick a good victim, then just default to
1899 * searching each chain in order. Don't allow chain relinking
1900 * because we only calculate enough journal credits for one
1901 * relink per alloc. */
1902 ac->ac_allow_chain_relink = 0;
1903 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1904 if (i == victim)
1905 continue;
1906 if (!cl->cl_recs[i].c_free)
1907 continue;
1908
1909 ac->ac_chain = i;
da5cbf2f 1910 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
7d1fe093 1911 res, &bits_left);
ccd979bd
MF
1912 if (!status)
1913 break;
1914 if (status < 0 && status != -ENOSPC) {
1915 mlog_errno(status);
1916 goto bail;
1917 }
1918 }
ccd979bd 1919
883d4cae
MF
1920set_hint:
1921 if (status != -ENOSPC) {
1922 /* If the next search of this group is not likely to
1923 * yield a suitable extent, then we reset the last
1924 * group hint so as to not waste a disk read */
1925 if (bits_left < min_bits)
1926 ac->ac_last_group = 0;
1927 else
7d1fe093 1928 ac->ac_last_group = res->sr_bg_blkno;
883d4cae
MF
1929 }
1930
1931bail:
ccd979bd
MF
1932 mlog_exit(status);
1933 return status;
1934}
1935
1ed9b777 1936int ocfs2_claim_metadata(handle_t *handle,
ccd979bd
MF
1937 struct ocfs2_alloc_context *ac,
1938 u32 bits_wanted,
2b6cb576 1939 u64 *suballoc_loc,
ccd979bd
MF
1940 u16 *suballoc_bit_start,
1941 unsigned int *num_bits,
1942 u64 *blkno_start)
1943{
1944 int status;
ba206635 1945 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
ccd979bd
MF
1946
1947 BUG_ON(!ac);
1948 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1949 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
ccd979bd 1950
aa8f8e93 1951 status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f 1952 handle,
ccd979bd
MF
1953 bits_wanted,
1954 1,
7d1fe093 1955 &res);
ccd979bd
MF
1956 if (status < 0) {
1957 mlog_errno(status);
1958 goto bail;
1959 }
1ed9b777 1960 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
ccd979bd 1961
2b6cb576 1962 *suballoc_loc = res.sr_bg_blkno;
7d1fe093 1963 *suballoc_bit_start = res.sr_bit_offset;
ba206635 1964 *blkno_start = res.sr_blkno;
7d1fe093
JB
1965 ac->ac_bits_given += res.sr_bits;
1966 *num_bits = res.sr_bits;
ccd979bd
MF
1967 status = 0;
1968bail:
1969 mlog_exit(status);
1970 return status;
1971}
1972
13821151
TM
1973static void ocfs2_init_inode_ac_group(struct inode *dir,
1974 struct buffer_head *parent_fe_bh,
1975 struct ocfs2_alloc_context *ac)
1976{
1977 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1978 /*
1979 * Try to allocate inodes from some specific group.
1980 *
1981 * If the parent dir has recorded the last group used in allocation,
1982 * cool, use it. Otherwise if we try to allocate new inode from the
1983 * same slot the parent dir belongs to, use the same chunk.
1984 *
1985 * We are very careful here to avoid the mistake of setting
1986 * ac_last_group to a group descriptor from a different (unlocked) slot.
1987 */
1988 if (OCFS2_I(dir)->ip_last_used_group &&
1989 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1990 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1991 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1992 ac->ac_last_group = ocfs2_which_suballoc_group(
1993 le64_to_cpu(fe->i_blkno),
1994 le16_to_cpu(fe->i_suballoc_bit));
1995}
1996
1997static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1998 struct ocfs2_alloc_context *ac)
1999{
2000 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2001 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2002}
2003
1ed9b777 2004int ocfs2_claim_new_inode(handle_t *handle,
13821151
TM
2005 struct inode *dir,
2006 struct buffer_head *parent_fe_bh,
ccd979bd 2007 struct ocfs2_alloc_context *ac,
2b6cb576 2008 u64 *suballoc_loc,
ccd979bd
MF
2009 u16 *suballoc_bit,
2010 u64 *fe_blkno)
2011{
2012 int status;
2b6cb576 2013 struct ocfs2_suballoc_result res;
ccd979bd
MF
2014
2015 mlog_entry_void();
2016
2017 BUG_ON(!ac);
2018 BUG_ON(ac->ac_bits_given != 0);
2019 BUG_ON(ac->ac_bits_wanted != 1);
2020 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
ccd979bd 2021
13821151
TM
2022 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2023
aa8f8e93 2024 status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f 2025 handle,
ccd979bd
MF
2026 1,
2027 1,
7d1fe093 2028 &res);
ccd979bd
MF
2029 if (status < 0) {
2030 mlog_errno(status);
2031 goto bail;
2032 }
1ed9b777 2033 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
ccd979bd 2034
7d1fe093 2035 BUG_ON(res.sr_bits != 1);
ccd979bd 2036
2b6cb576 2037 *suballoc_loc = res.sr_bg_blkno;
7d1fe093 2038 *suballoc_bit = res.sr_bit_offset;
ba206635 2039 *fe_blkno = res.sr_blkno;
ccd979bd 2040 ac->ac_bits_given++;
13821151 2041 ocfs2_save_inode_ac_group(dir, ac);
ccd979bd
MF
2042 status = 0;
2043bail:
2044 mlog_exit(status);
2045 return status;
2046}
2047
2048/* translate a group desc. blkno and it's bitmap offset into
2049 * disk cluster offset. */
2050static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2051 u64 bg_blkno,
2052 u16 bg_bit_off)
2053{
2054 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2055 u32 cluster = 0;
2056
2057 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2058
2059 if (bg_blkno != osb->first_cluster_group_blkno)
2060 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2061 cluster += (u32) bg_bit_off;
2062 return cluster;
2063}
2064
2065/* given a cluster offset, calculate which block group it belongs to
2066 * and return that block offset. */
d659072f 2067u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
ccd979bd
MF
2068{
2069 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2070 u32 group_no;
2071
2072 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2073
2074 group_no = cluster / osb->bitmap_cpg;
2075 if (!group_no)
2076 return osb->first_cluster_group_blkno;
2077 return ocfs2_clusters_to_blocks(inode->i_sb,
2078 group_no * osb->bitmap_cpg);
2079}
2080
2081/* given the block number of a cluster start, calculate which cluster
2082 * group and descriptor bitmap offset that corresponds to. */
2083static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2084 u64 data_blkno,
2085 u64 *bg_blkno,
2086 u16 *bg_bit_off)
2087{
2088 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2089 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2090
2091 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2092
2093 *bg_blkno = ocfs2_which_cluster_group(inode,
2094 data_cluster);
2095
2096 if (*bg_blkno == osb->first_cluster_group_blkno)
2097 *bg_bit_off = (u16) data_cluster;
2098 else
2099 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2100 data_blkno - *bg_blkno);
2101}
2102
2103/*
2104 * min_bits - minimum contiguous chunk from this total allocation we
2105 * can handle. set to what we asked for originally for a full
2106 * contig. allocation, set to '1' to indicate we can deal with extents
2107 * of any size.
2108 */
1ed9b777 2109int __ocfs2_claim_clusters(handle_t *handle,
415cb800
MF
2110 struct ocfs2_alloc_context *ac,
2111 u32 min_clusters,
2112 u32 max_clusters,
2113 u32 *cluster_start,
2114 u32 *num_clusters)
ccd979bd
MF
2115{
2116 int status;
415cb800 2117 unsigned int bits_wanted = max_clusters;
ba206635 2118 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
1ed9b777 2119 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
ccd979bd
MF
2120
2121 mlog_entry_void();
2122
ccd979bd
MF
2123 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2124
2125 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2126 && ac->ac_which != OCFS2_AC_USE_MAIN);
ccd979bd
MF
2127
2128 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
33d5d380
MF
2129 WARN_ON(min_clusters > 1);
2130
ccd979bd
MF
2131 status = ocfs2_claim_local_alloc_bits(osb,
2132 handle,
2133 ac,
2134 bits_wanted,
2135 cluster_start,
2136 num_clusters);
2137 if (!status)
2138 atomic_inc(&osb->alloc_stats.local_data);
2139 } else {
2140 if (min_clusters > (osb->bitmap_cpg - 1)) {
2141 /* The only paths asking for contiguousness
2142 * should know about this already. */
2fbe8d1e
SM
2143 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2144 "group bitmap size %u!\n", min_clusters,
2145 osb->bitmap_cpg);
ccd979bd
MF
2146 status = -ENOSPC;
2147 goto bail;
2148 }
2149 /* clamp the current request down to a realistic size. */
2150 if (bits_wanted > (osb->bitmap_cpg - 1))
2151 bits_wanted = osb->bitmap_cpg - 1;
2152
aa8f8e93 2153 status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f 2154 handle,
ccd979bd
MF
2155 bits_wanted,
2156 min_clusters,
7d1fe093 2157 &res);
ccd979bd 2158 if (!status) {
ba206635 2159 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
ccd979bd
MF
2160 *cluster_start =
2161 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
7d1fe093
JB
2162 res.sr_bg_blkno,
2163 res.sr_bit_offset);
ccd979bd
MF
2164 atomic_inc(&osb->alloc_stats.bitmap_data);
2165 }
2166 }
2167 if (status < 0) {
2168 if (status != -ENOSPC)
2169 mlog_errno(status);
2170 goto bail;
2171 }
2172
7d1fe093
JB
2173 ac->ac_bits_given += res.sr_bits;
2174 *num_clusters = res.sr_bits;
ccd979bd
MF
2175
2176bail:
2177 mlog_exit(status);
2178 return status;
2179}
2180
1ed9b777 2181int ocfs2_claim_clusters(handle_t *handle,
415cb800
MF
2182 struct ocfs2_alloc_context *ac,
2183 u32 min_clusters,
2184 u32 *cluster_start,
2185 u32 *num_clusters)
2186{
2187 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2188
1ed9b777 2189 return __ocfs2_claim_clusters(handle, ac, min_clusters,
415cb800
MF
2190 bits_wanted, cluster_start, num_clusters);
2191}
2192
b4414eea
MF
2193static int ocfs2_block_group_clear_bits(handle_t *handle,
2194 struct inode *alloc_inode,
2195 struct ocfs2_group_desc *bg,
2196 struct buffer_head *group_bh,
2197 unsigned int bit_off,
2198 unsigned int num_bits,
2199 void (*undo_fn)(unsigned int bit,
2200 unsigned long *bmap))
ccd979bd
MF
2201{
2202 int status;
2203 unsigned int tmp;
ccd979bd
MF
2204 struct ocfs2_group_desc *undo_bg = NULL;
2205
2206 mlog_entry_void();
2207
42035306
JB
2208 /* The caller got this descriptor from
2209 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2210 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
ccd979bd
MF
2211
2212 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2213
b4414eea 2214 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
0cf2f763 2215 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
b4414eea
MF
2216 group_bh,
2217 undo_fn ?
2218 OCFS2_JOURNAL_ACCESS_UNDO :
2219 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
2220 if (status < 0) {
2221 mlog_errno(status);
2222 goto bail;
2223 }
2224
b4414eea 2225 if (undo_fn) {
94e41ecf
SM
2226 jbd_lock_bh_state(group_bh);
2227 undo_bg = (struct ocfs2_group_desc *)
2228 bh2jh(group_bh)->b_committed_data;
2229 BUG_ON(!undo_bg);
2230 }
ccd979bd
MF
2231
2232 tmp = num_bits;
2233 while(tmp--) {
2234 ocfs2_clear_bit((bit_off + tmp),
2235 (unsigned long *) bg->bg_bitmap);
b4414eea
MF
2236 if (undo_fn)
2237 undo_fn(bit_off + tmp,
2238 (unsigned long *) undo_bg->bg_bitmap);
ccd979bd
MF
2239 }
2240 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2241
b4414eea 2242 if (undo_fn)
94e41ecf
SM
2243 jbd_unlock_bh_state(group_bh);
2244
ec20cec7 2245 ocfs2_journal_dirty(handle, group_bh);
ccd979bd
MF
2246bail:
2247 return status;
2248}
2249
2250/*
2251 * expects the suballoc inode to already be locked.
2252 */
b4414eea
MF
2253static int _ocfs2_free_suballoc_bits(handle_t *handle,
2254 struct inode *alloc_inode,
2255 struct buffer_head *alloc_bh,
2256 unsigned int start_bit,
2257 u64 bg_blkno,
2258 unsigned int count,
2259 void (*undo_fn)(unsigned int bit,
2260 unsigned long *bitmap))
ccd979bd
MF
2261{
2262 int status = 0;
2263 u32 tmp_used;
ccd979bd
MF
2264 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2265 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2266 struct buffer_head *group_bh = NULL;
2267 struct ocfs2_group_desc *group;
2268
2269 mlog_entry_void();
2270
10995aa2
JB
2271 /* The alloc_bh comes from ocfs2_free_dinode() or
2272 * ocfs2_free_clusters(). The callers have all locked the
2273 * allocator and gotten alloc_bh from the lock call. This
2274 * validates the dinode buffer. Any corruption that has happended
2275 * is a code bug. */
2276 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
ccd979bd
MF
2277 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2278
b0697053
MF
2279 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2280 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2281 (unsigned long long)bg_blkno, start_bit);
ccd979bd 2282
68f64d47
JB
2283 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2284 &group_bh);
ccd979bd
MF
2285 if (status < 0) {
2286 mlog_errno(status);
2287 goto bail;
2288 }
ccd979bd 2289 group = (struct ocfs2_group_desc *) group_bh->b_data;
68f64d47 2290
ccd979bd
MF
2291 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2292
2293 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2294 group, group_bh,
b4414eea 2295 start_bit, count, undo_fn);
ccd979bd
MF
2296 if (status < 0) {
2297 mlog_errno(status);
2298 goto bail;
2299 }
2300
0cf2f763
JB
2301 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2302 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
2303 if (status < 0) {
2304 mlog_errno(status);
2305 goto bail;
2306 }
2307
2308 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2309 count);
2310 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2311 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
ec20cec7 2312 ocfs2_journal_dirty(handle, alloc_bh);
ccd979bd
MF
2313
2314bail:
a81cb88b 2315 brelse(group_bh);
ccd979bd
MF
2316
2317 mlog_exit(status);
2318 return status;
2319}
2320
b4414eea
MF
2321int ocfs2_free_suballoc_bits(handle_t *handle,
2322 struct inode *alloc_inode,
2323 struct buffer_head *alloc_bh,
2324 unsigned int start_bit,
2325 u64 bg_blkno,
2326 unsigned int count)
2327{
2328 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2329 start_bit, bg_blkno, count, NULL);
2330}
2331
1fabe148 2332int ocfs2_free_dinode(handle_t *handle,
ccd979bd
MF
2333 struct inode *inode_alloc_inode,
2334 struct buffer_head *inode_alloc_bh,
2335 struct ocfs2_dinode *di)
2336{
2337 u64 blk = le64_to_cpu(di->i_blkno);
2338 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2339 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2340
2341 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2342 inode_alloc_bh, bit, bg_blkno, 1);
2343}
2344
b4414eea
MF
2345static int _ocfs2_free_clusters(handle_t *handle,
2346 struct inode *bitmap_inode,
2347 struct buffer_head *bitmap_bh,
2348 u64 start_blk,
2349 unsigned int num_clusters,
2350 void (*undo_fn)(unsigned int bit,
2351 unsigned long *bitmap))
ccd979bd
MF
2352{
2353 int status;
2354 u16 bg_start_bit;
2355 u64 bg_blkno;
2356 struct ocfs2_dinode *fe;
2357
2358 /* You can't ever have a contiguous set of clusters
2359 * bigger than a block group bitmap so we never have to worry
2360 * about looping on them. */
2361
2362 mlog_entry_void();
2363
2364 /* This is expensive. We can safely remove once this stuff has
2365 * gotten tested really well. */
2366 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2367
2368 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2369
2370 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2371 &bg_start_bit);
2372
b0697053
MF
2373 mlog(0, "want to free %u clusters starting at block %llu\n",
2374 num_clusters, (unsigned long long)start_blk);
2375 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2376 (unsigned long long)bg_blkno, bg_start_bit);
ccd979bd 2377
b4414eea
MF
2378 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2379 bg_start_bit, bg_blkno,
2380 num_clusters, undo_fn);
9c7af40b 2381 if (status < 0) {
ccd979bd 2382 mlog_errno(status);
9c7af40b
MF
2383 goto out;
2384 }
2385
2386 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2387 num_clusters);
ccd979bd 2388
9c7af40b 2389out:
ccd979bd
MF
2390 mlog_exit(status);
2391 return status;
2392}
2393
b4414eea
MF
2394int ocfs2_free_clusters(handle_t *handle,
2395 struct inode *bitmap_inode,
2396 struct buffer_head *bitmap_bh,
2397 u64 start_blk,
2398 unsigned int num_clusters)
2399{
2400 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2401 start_blk, num_clusters,
2402 _ocfs2_set_bit);
2403}
2404
2405/*
2406 * Give never-used clusters back to the global bitmap. We don't need
2407 * to protect these bits in the undo buffer.
2408 */
2409int ocfs2_release_clusters(handle_t *handle,
2410 struct inode *bitmap_inode,
2411 struct buffer_head *bitmap_bh,
2412 u64 start_blk,
2413 unsigned int num_clusters)
2414{
2415 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2416 start_blk, num_clusters,
2417 _ocfs2_clear_bit);
2418}
2419
ccd979bd
MF
2420static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2421{
2422 printk("Block Group:\n");
2423 printk("bg_signature: %s\n", bg->bg_signature);
2424 printk("bg_size: %u\n", bg->bg_size);
2425 printk("bg_bits: %u\n", bg->bg_bits);
2426 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2427 printk("bg_chain: %u\n", bg->bg_chain);
2428 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
b0697053
MF
2429 printk("bg_next_group: %llu\n",
2430 (unsigned long long)bg->bg_next_group);
2431 printk("bg_parent_dinode: %llu\n",
2432 (unsigned long long)bg->bg_parent_dinode);
2433 printk("bg_blkno: %llu\n",
2434 (unsigned long long)bg->bg_blkno);
ccd979bd
MF
2435}
2436
2437static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2438{
2439 int i;
2440
b0697053 2441 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
ccd979bd 2442 printk("i_signature: %s\n", fe->i_signature);
b0697053
MF
2443 printk("i_size: %llu\n",
2444 (unsigned long long)fe->i_size);
ccd979bd
MF
2445 printk("i_clusters: %u\n", fe->i_clusters);
2446 printk("i_generation: %u\n",
2447 le32_to_cpu(fe->i_generation));
2448 printk("id1.bitmap1.i_used: %u\n",
2449 le32_to_cpu(fe->id1.bitmap1.i_used));
2450 printk("id1.bitmap1.i_total: %u\n",
2451 le32_to_cpu(fe->id1.bitmap1.i_total));
2452 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2453 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2454 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2455 printk("id2.i_chain.cl_next_free_rec: %u\n",
2456 fe->id2.i_chain.cl_next_free_rec);
2457 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2458 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2459 fe->id2.i_chain.cl_recs[i].c_free);
2460 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2461 fe->id2.i_chain.cl_recs[i].c_total);
b0697053
MF
2462 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2463 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
ccd979bd
MF
2464 }
2465}
e7d4cb6b
TM
2466
2467/*
2468 * For a given allocation, determine which allocators will need to be
2469 * accessed, and lock them, reserving the appropriate number of bits.
2470 *
2471 * Sparse file systems call this from ocfs2_write_begin_nolock()
2472 * and ocfs2_allocate_unwritten_extents().
2473 *
2474 * File systems which don't support holes call this from
2475 * ocfs2_extend_allocation().
2476 */
f99b9b7c
JB
2477int ocfs2_lock_allocators(struct inode *inode,
2478 struct ocfs2_extent_tree *et,
e7d4cb6b
TM
2479 u32 clusters_to_add, u32 extents_to_split,
2480 struct ocfs2_alloc_context **data_ac,
f99b9b7c 2481 struct ocfs2_alloc_context **meta_ac)
e7d4cb6b
TM
2482{
2483 int ret = 0, num_free_extents;
2484 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2485 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2486
2487 *meta_ac = NULL;
2488 if (data_ac)
2489 *data_ac = NULL;
2490
2491 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2492
3d03a305 2493 num_free_extents = ocfs2_num_free_extents(osb, et);
e7d4cb6b
TM
2494 if (num_free_extents < 0) {
2495 ret = num_free_extents;
2496 mlog_errno(ret);
2497 goto out;
2498 }
2499
2500 /*
2501 * Sparse allocation file systems need to be more conservative
2502 * with reserving room for expansion - the actual allocation
2503 * happens while we've got a journal handle open so re-taking
2504 * a cluster lock (because we ran out of room for another
2505 * extent) will violate ordering rules.
2506 *
2507 * Most of the time we'll only be seeing this 1 cluster at a time
2508 * anyway.
2509 *
2510 * Always lock for any unwritten extents - we might want to
2511 * add blocks during a split.
2512 */
2513 if (!num_free_extents ||
2514 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
f99b9b7c 2515 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
e7d4cb6b
TM
2516 if (ret < 0) {
2517 if (ret != -ENOSPC)
2518 mlog_errno(ret);
2519 goto out;
2520 }
2521 }
2522
2523 if (clusters_to_add == 0)
2524 goto out;
2525
2526 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2527 if (ret < 0) {
2528 if (ret != -ENOSPC)
2529 mlog_errno(ret);
2530 goto out;
2531 }
2532
2533out:
2534 if (ret) {
2535 if (*meta_ac) {
2536 ocfs2_free_alloc_context(*meta_ac);
2537 *meta_ac = NULL;
2538 }
2539
2540 /*
2541 * We cannot have an error and a non null *data_ac.
2542 */
2543 }
2544
2545 return ret;
2546}
6ca497a8 2547
2548/*
2549 * Read the inode specified by blkno to get suballoc_slot and
2550 * suballoc_bit.
2551 */
2552static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2553 u16 *suballoc_slot, u16 *suballoc_bit)
2554{
2555 int status;
2556 struct buffer_head *inode_bh = NULL;
2557 struct ocfs2_dinode *inode_fe;
2558
5b09b507 2559 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
6ca497a8 2560
2561 /* dirty read disk */
2562 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2563 if (status < 0) {
5b09b507
JB
2564 mlog(ML_ERROR, "read block %llu failed %d\n",
2565 (unsigned long long)blkno, status);
6ca497a8 2566 goto bail;
2567 }
2568
2569 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2570 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
5b09b507
JB
2571 mlog(ML_ERROR, "invalid inode %llu requested\n",
2572 (unsigned long long)blkno);
6ca497a8 2573 status = -EINVAL;
2574 goto bail;
2575 }
2576
0fba8137 2577 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
6ca497a8 2578 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2579 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
5b09b507
JB
2580 (unsigned long long)blkno,
2581 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
6ca497a8 2582 status = -EINVAL;
2583 goto bail;
2584 }
2585
2586 if (suballoc_slot)
2587 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2588 if (suballoc_bit)
2589 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2590
2591bail:
2592 brelse(inode_bh);
2593
2594 mlog_exit(status);
2595 return status;
2596}
2597
2598/*
2599 * test whether bit is SET in allocator bitmap or not. on success, 0
2600 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2601 * is returned and *res is meaningless. Call this after you have
2602 * cluster locked against suballoc, or you may get a result based on
2603 * non-up2date contents
2604 */
2605static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2606 struct inode *suballoc,
2607 struct buffer_head *alloc_bh, u64 blkno,
2608 u16 bit, int *res)
2609{
2610 struct ocfs2_dinode *alloc_fe;
2611 struct ocfs2_group_desc *group;
2612 struct buffer_head *group_bh = NULL;
2613 u64 bg_blkno;
2614 int status;
2615
5b09b507
JB
2616 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2617 (unsigned int)bit);
6ca497a8 2618
2619 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2620 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2621 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2622 (unsigned int)bit,
2623 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2624 status = -EINVAL;
2625 goto bail;
2626 }
2627
2628 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2629 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2630 &group_bh);
2631 if (status < 0) {
5b09b507
JB
2632 mlog(ML_ERROR, "read group %llu failed %d\n",
2633 (unsigned long long)bg_blkno, status);
6ca497a8 2634 goto bail;
2635 }
2636
2637 group = (struct ocfs2_group_desc *) group_bh->b_data;
2638 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2639
2640bail:
2641 brelse(group_bh);
2642
2643 mlog_exit(status);
2644 return status;
2645}
2646
2647/*
2648 * Test if the bit representing this inode (blkno) is set in the
2649 * suballocator.
2650 *
2651 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2652 *
2653 * In the event of failure, a negative value is returned and *res is
2654 * meaningless.
2655 *
2656 * Callers must make sure to hold nfs_sync_lock to prevent
2657 * ocfs2_delete_inode() on another node from accessing the same
2658 * suballocator concurrently.
2659 */
2660int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2661{
2662 int status;
2663 u16 suballoc_bit = 0, suballoc_slot = 0;
2664 struct inode *inode_alloc_inode;
2665 struct buffer_head *alloc_bh = NULL;
2666
5b09b507 2667 mlog_entry("blkno: %llu", (unsigned long long)blkno);
6ca497a8 2668
2669 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2670 &suballoc_bit);
2671 if (status < 0) {
2672 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2673 goto bail;
2674 }
2675
2676 inode_alloc_inode =
2677 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2678 suballoc_slot);
2679 if (!inode_alloc_inode) {
2680 /* the error code could be inaccurate, but we are not able to
2681 * get the correct one. */
2682 status = -EINVAL;
2683 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2684 (u32)suballoc_slot);
2685 goto bail;
2686 }
2687
2688 mutex_lock(&inode_alloc_inode->i_mutex);
2689 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2690 if (status < 0) {
2691 mutex_unlock(&inode_alloc_inode->i_mutex);
2692 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2693 (u32)suballoc_slot, status);
2694 goto bail;
2695 }
2696
2697 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2698 blkno, suballoc_bit, res);
2699 if (status < 0)
2700 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2701
2702 ocfs2_inode_unlock(inode_alloc_inode, 0);
2703 mutex_unlock(&inode_alloc_inode->i_mutex);
2704
2705 iput(inode_alloc_inode);
2706 brelse(alloc_bh);
2707bail:
2708 mlog_exit(status);
2709 return status;
2710}
This page took 0.572629 seconds and 5 git commands to generate.