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