ocfs2: Return allocated metadata blknos on the ocfs2_suballoc_result.
[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
JB
56struct ocfs2_suballoc_result {
57 u64 sr_bg_blkno; /* The bg we allocated from */
ba206635 58 u64 sr_blkno; /* The first allocated block */
7d1fe093
JB
59 unsigned int sr_bit_offset; /* The bit in the bg */
60 unsigned int sr_bits; /* How many bits we claimed */
61};
62
ccd979bd
MF
63static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
64static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
65static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
1fabe148 66static int ocfs2_block_group_fill(handle_t *handle,
ccd979bd
MF
67 struct inode *alloc_inode,
68 struct buffer_head *bg_bh,
69 u64 group_blkno,
798db35f 70 unsigned int group_clusters,
ccd979bd
MF
71 u16 my_chain,
72 struct ocfs2_chain_list *cl);
73static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
74 struct inode *alloc_inode,
1187c968 75 struct buffer_head *bh,
60ca81e8 76 u64 max_block,
feb473a6 77 u64 *last_alloc_group,
60ca81e8 78 int flags);
ccd979bd 79
ccd979bd
MF
80static int ocfs2_cluster_group_search(struct inode *inode,
81 struct buffer_head *group_bh,
82 u32 bits_wanted, u32 min_bits,
1187c968 83 u64 max_block,
7d1fe093 84 struct ocfs2_suballoc_result *res);
ccd979bd
MF
85static int ocfs2_block_group_search(struct inode *inode,
86 struct buffer_head *group_bh,
87 u32 bits_wanted, u32 min_bits,
1187c968 88 u64 max_block,
7d1fe093 89 struct ocfs2_suballoc_result *res);
aa8f8e93 90static int ocfs2_claim_suballoc_bits(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
1ed9b777 443 status = ocfs2_claim_clusters(handle, ac,
798db35f
JB
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) {
1ed9b777 485 status = ocfs2_claim_clusters(handle, ac, min_bits,
798db35f
JB
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
ba206635
JB
1583static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1584 struct ocfs2_extent_rec *rec,
1585 struct ocfs2_chain_list *cl)
13e434cf
JB
1586{
1587 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1588 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1589 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1590
1591 if (res->sr_bit_offset < bitoff)
1592 return 0;
1593 if (res->sr_bit_offset >= (bitoff + bitcount))
1594 return 0;
ba206635
JB
1595 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1596 (res->sr_bit_offset - bitoff);
13e434cf
JB
1597 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1598 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1599 return 1;
1600}
1601
ba206635
JB
1602static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1603 struct ocfs2_group_desc *bg,
1604 struct ocfs2_suballoc_result *res)
13e434cf
JB
1605{
1606 int i;
1607 struct ocfs2_extent_rec *rec;
1608 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1609 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1610
ba206635
JB
1611 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1612 res->sr_blkno = 0;
13e434cf 1613 return;
ba206635 1614 }
13e434cf 1615
ba206635
JB
1616 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
1617 if (!ocfs2_supports_discontig_bh(OCFS2_SB(ac->ac_inode->i_sb)) ||
1618 !bg->bg_list.l_next_free_rec)
13e434cf
JB
1619 return;
1620
1621 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1622 rec = &bg->bg_list.l_recs[i];
ba206635 1623 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl))
13e434cf
JB
1624 break;
1625 }
1626}
1627
883d4cae 1628static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1fabe148 1629 handle_t *handle,
883d4cae
MF
1630 u32 bits_wanted,
1631 u32 min_bits,
7d1fe093 1632 struct ocfs2_suballoc_result *res,
883d4cae
MF
1633 u16 *bits_left)
1634{
1635 int ret;
883d4cae
MF
1636 struct buffer_head *group_bh = NULL;
1637 struct ocfs2_group_desc *gd;
68f64d47 1638 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
883d4cae 1639 struct inode *alloc_inode = ac->ac_inode;
883d4cae 1640
7d1fe093
JB
1641 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1642 res->sr_bg_blkno, &group_bh);
883d4cae
MF
1643 if (ret < 0) {
1644 mlog_errno(ret);
1645 return ret;
1646 }
1647
1648 gd = (struct ocfs2_group_desc *) group_bh->b_data;
883d4cae 1649 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
7d1fe093 1650 ac->ac_max_block, res);
883d4cae
MF
1651 if (ret < 0) {
1652 if (ret != -ENOSPC)
1653 mlog_errno(ret);
1654 goto out;
1655 }
1656
13e434cf 1657 if (!ret)
ba206635 1658 ocfs2_bg_discontig_fix_result(ac, gd, res);
13e434cf 1659
883d4cae 1660 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
7d1fe093 1661 res->sr_bits,
883d4cae
MF
1662 le16_to_cpu(gd->bg_chain));
1663 if (ret < 0) {
1664 mlog_errno(ret);
1665 goto out;
1666 }
1667
1668 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
7d1fe093 1669 res->sr_bit_offset, res->sr_bits);
883d4cae
MF
1670 if (ret < 0)
1671 mlog_errno(ret);
1672
1673 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1674
1675out:
1676 brelse(group_bh);
1677
1678 return ret;
1679}
1680
ccd979bd 1681static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1fabe148 1682 handle_t *handle,
ccd979bd
MF
1683 u32 bits_wanted,
1684 u32 min_bits,
7d1fe093 1685 struct ocfs2_suballoc_result *res,
883d4cae 1686 u16 *bits_left)
ccd979bd
MF
1687{
1688 int status;
7d1fe093 1689 u16 chain;
ccd979bd
MF
1690 u32 tmp_used;
1691 u64 next_group;
ccd979bd
MF
1692 struct inode *alloc_inode = ac->ac_inode;
1693 struct buffer_head *group_bh = NULL;
1694 struct buffer_head *prev_group_bh = NULL;
1695 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1696 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1697 struct ocfs2_group_desc *bg;
1698
1699 chain = ac->ac_chain;
b0697053
MF
1700 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1701 bits_wanted, chain,
1702 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
ccd979bd 1703
68f64d47
JB
1704 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1705 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1706 &group_bh);
ccd979bd
MF
1707 if (status < 0) {
1708 mlog_errno(status);
1709 goto bail;
1710 }
1711 bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bd
MF
1712
1713 status = -ENOSPC;
1714 /* for now, the chain search is a bit simplistic. We just use
1715 * the 1st group with any empty bits. */
1187c968
JB
1716 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1717 bits_wanted, min_bits,
7d1fe093
JB
1718 ac->ac_max_block,
1719 res)) == -ENOSPC) {
ccd979bd
MF
1720 if (!bg->bg_next_group)
1721 break;
a81cb88b
MF
1722
1723 brelse(prev_group_bh);
1724 prev_group_bh = NULL;
1725
ccd979bd
MF
1726 next_group = le64_to_cpu(bg->bg_next_group);
1727 prev_group_bh = group_bh;
1728 group_bh = NULL;
68f64d47
JB
1729 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1730 next_group, &group_bh);
ccd979bd
MF
1731 if (status < 0) {
1732 mlog_errno(status);
1733 goto bail;
1734 }
1735 bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bd
MF
1736 }
1737 if (status < 0) {
1738 if (status != -ENOSPC)
1739 mlog_errno(status);
1740 goto bail;
1741 }
1742
b0697053 1743 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
7d1fe093 1744 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
ccd979bd 1745
7d1fe093 1746 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
ccd979bd 1747
7d1fe093 1748 BUG_ON(res->sr_bits == 0);
13e434cf 1749 if (!status)
ba206635 1750 ocfs2_bg_discontig_fix_result(ac, bg, res);
13e434cf 1751
ccd979bd
MF
1752
1753 /*
1754 * Keep track of previous block descriptor read. When
1755 * we find a target, if we have read more than X
1756 * number of descriptors, and the target is reasonably
1757 * empty, relink him to top of his chain.
1758 *
1759 * We've read 0 extra blocks and only send one more to
1760 * the transaction, yet the next guy to search has a
1761 * much easier time.
1762 *
1763 * Do this *after* figuring out how many bits we're taking out
1764 * of our target group.
1765 */
1766 if (ac->ac_allow_chain_relink &&
1767 (prev_group_bh) &&
7d1fe093 1768 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
ccd979bd
MF
1769 status = ocfs2_relink_block_group(handle, alloc_inode,
1770 ac->ac_bh, group_bh,
1771 prev_group_bh, chain);
1772 if (status < 0) {
1773 mlog_errno(status);
1774 goto bail;
1775 }
1776 }
1777
1778 /* Ok, claim our bits now: set the info on dinode, chainlist
1779 * and then the group */
13723d00 1780 status = ocfs2_journal_access_di(handle,
0cf2f763 1781 INODE_CACHE(alloc_inode),
13723d00
JB
1782 ac->ac_bh,
1783 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
1784 if (status < 0) {
1785 mlog_errno(status);
1786 goto bail;
1787 }
1788
1789 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
7d1fe093
JB
1790 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1791 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
ec20cec7 1792 ocfs2_journal_dirty(handle, ac->ac_bh);
ccd979bd
MF
1793
1794 status = ocfs2_block_group_set_bits(handle,
1795 alloc_inode,
1796 bg,
1797 group_bh,
7d1fe093
JB
1798 res->sr_bit_offset,
1799 res->sr_bits);
ccd979bd
MF
1800 if (status < 0) {
1801 mlog_errno(status);
1802 goto bail;
1803 }
1804
7d1fe093 1805 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
1ca1a111 1806 (unsigned long long)le64_to_cpu(fe->i_blkno));
ccd979bd 1807
883d4cae 1808 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
ccd979bd 1809bail:
a81cb88b
MF
1810 brelse(group_bh);
1811 brelse(prev_group_bh);
ccd979bd
MF
1812
1813 mlog_exit(status);
1814 return status;
1815}
1816
1817/* will give out up to bits_wanted contiguous bits. */
aa8f8e93 1818static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1fabe148 1819 handle_t *handle,
ccd979bd
MF
1820 u32 bits_wanted,
1821 u32 min_bits,
7d1fe093 1822 struct ocfs2_suballoc_result *res)
ccd979bd
MF
1823{
1824 int status;
1825 u16 victim, i;
883d4cae 1826 u16 bits_left = 0;
ccd979bd
MF
1827 struct ocfs2_chain_list *cl;
1828 struct ocfs2_dinode *fe;
1829
1830 mlog_entry_void();
1831
1832 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1833 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1834 BUG_ON(!ac->ac_bh);
1835
1836 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
10995aa2
JB
1837
1838 /* The bh was validated by the inode read during
1839 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1840 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1841
ccd979bd
MF
1842 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1843 le32_to_cpu(fe->id1.bitmap1.i_total)) {
aa8f8e93
JB
1844 ocfs2_error(ac->ac_inode->i_sb,
1845 "Chain allocator dinode %llu has %u used "
b0697053
MF
1846 "bits but only %u total.",
1847 (unsigned long long)le64_to_cpu(fe->i_blkno),
ccd979bd
MF
1848 le32_to_cpu(fe->id1.bitmap1.i_used),
1849 le32_to_cpu(fe->id1.bitmap1.i_total));
1850 status = -EIO;
1851 goto bail;
1852 }
1853
7d1fe093
JB
1854 res->sr_bg_blkno = ac->ac_last_group;
1855 if (res->sr_bg_blkno) {
883d4cae
MF
1856 /* Attempt to short-circuit the usual search mechanism
1857 * by jumping straight to the most recently used
1858 * allocation group. This helps us mantain some
1859 * contiguousness across allocations. */
da5cbf2f 1860 status = ocfs2_search_one_group(ac, handle, bits_wanted,
7d1fe093
JB
1861 min_bits, res, &bits_left);
1862 if (!status)
883d4cae 1863 goto set_hint;
883d4cae
MF
1864 if (status < 0 && status != -ENOSPC) {
1865 mlog_errno(status);
1866 goto bail;
1867 }
1868 }
1869
ccd979bd
MF
1870 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1871
1872 victim = ocfs2_find_victim_chain(cl);
1873 ac->ac_chain = victim;
1874 ac->ac_allow_chain_relink = 1;
1875
7d1fe093
JB
1876 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1877 res, &bits_left);
ccd979bd 1878 if (!status)
883d4cae 1879 goto set_hint;
ccd979bd
MF
1880 if (status < 0 && status != -ENOSPC) {
1881 mlog_errno(status);
1882 goto bail;
1883 }
1884
1885 mlog(0, "Search of victim chain %u came up with nothing, "
1886 "trying all chains now.\n", victim);
1887
1888 /* If we didn't pick a good victim, then just default to
1889 * searching each chain in order. Don't allow chain relinking
1890 * because we only calculate enough journal credits for one
1891 * relink per alloc. */
1892 ac->ac_allow_chain_relink = 0;
1893 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1894 if (i == victim)
1895 continue;
1896 if (!cl->cl_recs[i].c_free)
1897 continue;
1898
1899 ac->ac_chain = i;
da5cbf2f 1900 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
7d1fe093 1901 res, &bits_left);
ccd979bd
MF
1902 if (!status)
1903 break;
1904 if (status < 0 && status != -ENOSPC) {
1905 mlog_errno(status);
1906 goto bail;
1907 }
1908 }
ccd979bd 1909
883d4cae
MF
1910set_hint:
1911 if (status != -ENOSPC) {
1912 /* If the next search of this group is not likely to
1913 * yield a suitable extent, then we reset the last
1914 * group hint so as to not waste a disk read */
1915 if (bits_left < min_bits)
1916 ac->ac_last_group = 0;
1917 else
7d1fe093 1918 ac->ac_last_group = res->sr_bg_blkno;
883d4cae
MF
1919 }
1920
1921bail:
ccd979bd
MF
1922 mlog_exit(status);
1923 return status;
1924}
1925
1ed9b777 1926int ocfs2_claim_metadata(handle_t *handle,
ccd979bd
MF
1927 struct ocfs2_alloc_context *ac,
1928 u32 bits_wanted,
1929 u16 *suballoc_bit_start,
1930 unsigned int *num_bits,
1931 u64 *blkno_start)
1932{
1933 int status;
ba206635 1934 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
ccd979bd
MF
1935
1936 BUG_ON(!ac);
1937 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1938 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
ccd979bd 1939
aa8f8e93 1940 status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f 1941 handle,
ccd979bd
MF
1942 bits_wanted,
1943 1,
7d1fe093 1944 &res);
ccd979bd
MF
1945 if (status < 0) {
1946 mlog_errno(status);
1947 goto bail;
1948 }
1ed9b777 1949 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
ccd979bd 1950
7d1fe093 1951 *suballoc_bit_start = res.sr_bit_offset;
ba206635 1952 *blkno_start = res.sr_blkno;
7d1fe093
JB
1953 ac->ac_bits_given += res.sr_bits;
1954 *num_bits = res.sr_bits;
ccd979bd
MF
1955 status = 0;
1956bail:
1957 mlog_exit(status);
1958 return status;
1959}
1960
13821151
TM
1961static void ocfs2_init_inode_ac_group(struct inode *dir,
1962 struct buffer_head *parent_fe_bh,
1963 struct ocfs2_alloc_context *ac)
1964{
1965 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1966 /*
1967 * Try to allocate inodes from some specific group.
1968 *
1969 * If the parent dir has recorded the last group used in allocation,
1970 * cool, use it. Otherwise if we try to allocate new inode from the
1971 * same slot the parent dir belongs to, use the same chunk.
1972 *
1973 * We are very careful here to avoid the mistake of setting
1974 * ac_last_group to a group descriptor from a different (unlocked) slot.
1975 */
1976 if (OCFS2_I(dir)->ip_last_used_group &&
1977 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1978 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1979 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1980 ac->ac_last_group = ocfs2_which_suballoc_group(
1981 le64_to_cpu(fe->i_blkno),
1982 le16_to_cpu(fe->i_suballoc_bit));
1983}
1984
1985static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1986 struct ocfs2_alloc_context *ac)
1987{
1988 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1989 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1990}
1991
1ed9b777 1992int ocfs2_claim_new_inode(handle_t *handle,
13821151
TM
1993 struct inode *dir,
1994 struct buffer_head *parent_fe_bh,
ccd979bd
MF
1995 struct ocfs2_alloc_context *ac,
1996 u16 *suballoc_bit,
1997 u64 *fe_blkno)
1998{
1999 int status;
ba206635 2000 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
ccd979bd
MF
2001
2002 mlog_entry_void();
2003
2004 BUG_ON(!ac);
2005 BUG_ON(ac->ac_bits_given != 0);
2006 BUG_ON(ac->ac_bits_wanted != 1);
2007 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
ccd979bd 2008
13821151
TM
2009 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2010
aa8f8e93 2011 status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f 2012 handle,
ccd979bd
MF
2013 1,
2014 1,
7d1fe093 2015 &res);
ccd979bd
MF
2016 if (status < 0) {
2017 mlog_errno(status);
2018 goto bail;
2019 }
1ed9b777 2020 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
ccd979bd 2021
7d1fe093 2022 BUG_ON(res.sr_bits != 1);
ccd979bd 2023
7d1fe093 2024 *suballoc_bit = res.sr_bit_offset;
ba206635 2025 *fe_blkno = res.sr_blkno;
ccd979bd 2026 ac->ac_bits_given++;
13821151 2027 ocfs2_save_inode_ac_group(dir, ac);
ccd979bd
MF
2028 status = 0;
2029bail:
2030 mlog_exit(status);
2031 return status;
2032}
2033
2034/* translate a group desc. blkno and it's bitmap offset into
2035 * disk cluster offset. */
2036static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2037 u64 bg_blkno,
2038 u16 bg_bit_off)
2039{
2040 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2041 u32 cluster = 0;
2042
2043 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2044
2045 if (bg_blkno != osb->first_cluster_group_blkno)
2046 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2047 cluster += (u32) bg_bit_off;
2048 return cluster;
2049}
2050
2051/* given a cluster offset, calculate which block group it belongs to
2052 * and return that block offset. */
d659072f 2053u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
ccd979bd
MF
2054{
2055 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2056 u32 group_no;
2057
2058 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2059
2060 group_no = cluster / osb->bitmap_cpg;
2061 if (!group_no)
2062 return osb->first_cluster_group_blkno;
2063 return ocfs2_clusters_to_blocks(inode->i_sb,
2064 group_no * osb->bitmap_cpg);
2065}
2066
2067/* given the block number of a cluster start, calculate which cluster
2068 * group and descriptor bitmap offset that corresponds to. */
2069static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2070 u64 data_blkno,
2071 u64 *bg_blkno,
2072 u16 *bg_bit_off)
2073{
2074 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2075 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2076
2077 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2078
2079 *bg_blkno = ocfs2_which_cluster_group(inode,
2080 data_cluster);
2081
2082 if (*bg_blkno == osb->first_cluster_group_blkno)
2083 *bg_bit_off = (u16) data_cluster;
2084 else
2085 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2086 data_blkno - *bg_blkno);
2087}
2088
2089/*
2090 * min_bits - minimum contiguous chunk from this total allocation we
2091 * can handle. set to what we asked for originally for a full
2092 * contig. allocation, set to '1' to indicate we can deal with extents
2093 * of any size.
2094 */
1ed9b777 2095int __ocfs2_claim_clusters(handle_t *handle,
415cb800
MF
2096 struct ocfs2_alloc_context *ac,
2097 u32 min_clusters,
2098 u32 max_clusters,
2099 u32 *cluster_start,
2100 u32 *num_clusters)
ccd979bd
MF
2101{
2102 int status;
415cb800 2103 unsigned int bits_wanted = max_clusters;
ba206635 2104 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
1ed9b777 2105 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
ccd979bd
MF
2106
2107 mlog_entry_void();
2108
ccd979bd
MF
2109 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2110
2111 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2112 && ac->ac_which != OCFS2_AC_USE_MAIN);
ccd979bd
MF
2113
2114 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
33d5d380
MF
2115 WARN_ON(min_clusters > 1);
2116
ccd979bd
MF
2117 status = ocfs2_claim_local_alloc_bits(osb,
2118 handle,
2119 ac,
2120 bits_wanted,
2121 cluster_start,
2122 num_clusters);
2123 if (!status)
2124 atomic_inc(&osb->alloc_stats.local_data);
2125 } else {
2126 if (min_clusters > (osb->bitmap_cpg - 1)) {
2127 /* The only paths asking for contiguousness
2128 * should know about this already. */
2fbe8d1e
SM
2129 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2130 "group bitmap size %u!\n", min_clusters,
2131 osb->bitmap_cpg);
ccd979bd
MF
2132 status = -ENOSPC;
2133 goto bail;
2134 }
2135 /* clamp the current request down to a realistic size. */
2136 if (bits_wanted > (osb->bitmap_cpg - 1))
2137 bits_wanted = osb->bitmap_cpg - 1;
2138
aa8f8e93 2139 status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f 2140 handle,
ccd979bd
MF
2141 bits_wanted,
2142 min_clusters,
7d1fe093 2143 &res);
ccd979bd 2144 if (!status) {
ba206635 2145 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
ccd979bd
MF
2146 *cluster_start =
2147 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
7d1fe093
JB
2148 res.sr_bg_blkno,
2149 res.sr_bit_offset);
ccd979bd
MF
2150 atomic_inc(&osb->alloc_stats.bitmap_data);
2151 }
2152 }
2153 if (status < 0) {
2154 if (status != -ENOSPC)
2155 mlog_errno(status);
2156 goto bail;
2157 }
2158
7d1fe093
JB
2159 ac->ac_bits_given += res.sr_bits;
2160 *num_clusters = res.sr_bits;
ccd979bd
MF
2161
2162bail:
2163 mlog_exit(status);
2164 return status;
2165}
2166
1ed9b777 2167int ocfs2_claim_clusters(handle_t *handle,
415cb800
MF
2168 struct ocfs2_alloc_context *ac,
2169 u32 min_clusters,
2170 u32 *cluster_start,
2171 u32 *num_clusters)
2172{
2173 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2174
1ed9b777 2175 return __ocfs2_claim_clusters(handle, ac, min_clusters,
415cb800
MF
2176 bits_wanted, cluster_start, num_clusters);
2177}
2178
b4414eea
MF
2179static int ocfs2_block_group_clear_bits(handle_t *handle,
2180 struct inode *alloc_inode,
2181 struct ocfs2_group_desc *bg,
2182 struct buffer_head *group_bh,
2183 unsigned int bit_off,
2184 unsigned int num_bits,
2185 void (*undo_fn)(unsigned int bit,
2186 unsigned long *bmap))
ccd979bd
MF
2187{
2188 int status;
2189 unsigned int tmp;
ccd979bd
MF
2190 struct ocfs2_group_desc *undo_bg = NULL;
2191
2192 mlog_entry_void();
2193
42035306
JB
2194 /* The caller got this descriptor from
2195 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2196 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
ccd979bd
MF
2197
2198 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2199
b4414eea 2200 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
0cf2f763 2201 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
b4414eea
MF
2202 group_bh,
2203 undo_fn ?
2204 OCFS2_JOURNAL_ACCESS_UNDO :
2205 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
2206 if (status < 0) {
2207 mlog_errno(status);
2208 goto bail;
2209 }
2210
b4414eea 2211 if (undo_fn) {
94e41ecf
SM
2212 jbd_lock_bh_state(group_bh);
2213 undo_bg = (struct ocfs2_group_desc *)
2214 bh2jh(group_bh)->b_committed_data;
2215 BUG_ON(!undo_bg);
2216 }
ccd979bd
MF
2217
2218 tmp = num_bits;
2219 while(tmp--) {
2220 ocfs2_clear_bit((bit_off + tmp),
2221 (unsigned long *) bg->bg_bitmap);
b4414eea
MF
2222 if (undo_fn)
2223 undo_fn(bit_off + tmp,
2224 (unsigned long *) undo_bg->bg_bitmap);
ccd979bd
MF
2225 }
2226 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2227
b4414eea 2228 if (undo_fn)
94e41ecf
SM
2229 jbd_unlock_bh_state(group_bh);
2230
ec20cec7 2231 ocfs2_journal_dirty(handle, group_bh);
ccd979bd
MF
2232bail:
2233 return status;
2234}
2235
2236/*
2237 * expects the suballoc inode to already be locked.
2238 */
b4414eea
MF
2239static int _ocfs2_free_suballoc_bits(handle_t *handle,
2240 struct inode *alloc_inode,
2241 struct buffer_head *alloc_bh,
2242 unsigned int start_bit,
2243 u64 bg_blkno,
2244 unsigned int count,
2245 void (*undo_fn)(unsigned int bit,
2246 unsigned long *bitmap))
ccd979bd
MF
2247{
2248 int status = 0;
2249 u32 tmp_used;
ccd979bd
MF
2250 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2251 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2252 struct buffer_head *group_bh = NULL;
2253 struct ocfs2_group_desc *group;
2254
2255 mlog_entry_void();
2256
10995aa2
JB
2257 /* The alloc_bh comes from ocfs2_free_dinode() or
2258 * ocfs2_free_clusters(). The callers have all locked the
2259 * allocator and gotten alloc_bh from the lock call. This
2260 * validates the dinode buffer. Any corruption that has happended
2261 * is a code bug. */
2262 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
ccd979bd
MF
2263 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2264
b0697053
MF
2265 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2266 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2267 (unsigned long long)bg_blkno, start_bit);
ccd979bd 2268
68f64d47
JB
2269 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2270 &group_bh);
ccd979bd
MF
2271 if (status < 0) {
2272 mlog_errno(status);
2273 goto bail;
2274 }
ccd979bd 2275 group = (struct ocfs2_group_desc *) group_bh->b_data;
68f64d47 2276
ccd979bd
MF
2277 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2278
2279 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2280 group, group_bh,
b4414eea 2281 start_bit, count, undo_fn);
ccd979bd
MF
2282 if (status < 0) {
2283 mlog_errno(status);
2284 goto bail;
2285 }
2286
0cf2f763
JB
2287 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2288 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bd
MF
2289 if (status < 0) {
2290 mlog_errno(status);
2291 goto bail;
2292 }
2293
2294 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2295 count);
2296 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2297 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
ec20cec7 2298 ocfs2_journal_dirty(handle, alloc_bh);
ccd979bd
MF
2299
2300bail:
a81cb88b 2301 brelse(group_bh);
ccd979bd
MF
2302
2303 mlog_exit(status);
2304 return status;
2305}
2306
b4414eea
MF
2307int ocfs2_free_suballoc_bits(handle_t *handle,
2308 struct inode *alloc_inode,
2309 struct buffer_head *alloc_bh,
2310 unsigned int start_bit,
2311 u64 bg_blkno,
2312 unsigned int count)
2313{
2314 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2315 start_bit, bg_blkno, count, NULL);
2316}
2317
1fabe148 2318int ocfs2_free_dinode(handle_t *handle,
ccd979bd
MF
2319 struct inode *inode_alloc_inode,
2320 struct buffer_head *inode_alloc_bh,
2321 struct ocfs2_dinode *di)
2322{
2323 u64 blk = le64_to_cpu(di->i_blkno);
2324 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2325 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2326
2327 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2328 inode_alloc_bh, bit, bg_blkno, 1);
2329}
2330
b4414eea
MF
2331static int _ocfs2_free_clusters(handle_t *handle,
2332 struct inode *bitmap_inode,
2333 struct buffer_head *bitmap_bh,
2334 u64 start_blk,
2335 unsigned int num_clusters,
2336 void (*undo_fn)(unsigned int bit,
2337 unsigned long *bitmap))
ccd979bd
MF
2338{
2339 int status;
2340 u16 bg_start_bit;
2341 u64 bg_blkno;
2342 struct ocfs2_dinode *fe;
2343
2344 /* You can't ever have a contiguous set of clusters
2345 * bigger than a block group bitmap so we never have to worry
2346 * about looping on them. */
2347
2348 mlog_entry_void();
2349
2350 /* This is expensive. We can safely remove once this stuff has
2351 * gotten tested really well. */
2352 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2353
2354 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2355
2356 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2357 &bg_start_bit);
2358
b0697053
MF
2359 mlog(0, "want to free %u clusters starting at block %llu\n",
2360 num_clusters, (unsigned long long)start_blk);
2361 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2362 (unsigned long long)bg_blkno, bg_start_bit);
ccd979bd 2363
b4414eea
MF
2364 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2365 bg_start_bit, bg_blkno,
2366 num_clusters, undo_fn);
9c7af40b 2367 if (status < 0) {
ccd979bd 2368 mlog_errno(status);
9c7af40b
MF
2369 goto out;
2370 }
2371
2372 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2373 num_clusters);
ccd979bd 2374
9c7af40b 2375out:
ccd979bd
MF
2376 mlog_exit(status);
2377 return status;
2378}
2379
b4414eea
MF
2380int ocfs2_free_clusters(handle_t *handle,
2381 struct inode *bitmap_inode,
2382 struct buffer_head *bitmap_bh,
2383 u64 start_blk,
2384 unsigned int num_clusters)
2385{
2386 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2387 start_blk, num_clusters,
2388 _ocfs2_set_bit);
2389}
2390
2391/*
2392 * Give never-used clusters back to the global bitmap. We don't need
2393 * to protect these bits in the undo buffer.
2394 */
2395int ocfs2_release_clusters(handle_t *handle,
2396 struct inode *bitmap_inode,
2397 struct buffer_head *bitmap_bh,
2398 u64 start_blk,
2399 unsigned int num_clusters)
2400{
2401 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2402 start_blk, num_clusters,
2403 _ocfs2_clear_bit);
2404}
2405
ccd979bd
MF
2406static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2407{
2408 printk("Block Group:\n");
2409 printk("bg_signature: %s\n", bg->bg_signature);
2410 printk("bg_size: %u\n", bg->bg_size);
2411 printk("bg_bits: %u\n", bg->bg_bits);
2412 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2413 printk("bg_chain: %u\n", bg->bg_chain);
2414 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
b0697053
MF
2415 printk("bg_next_group: %llu\n",
2416 (unsigned long long)bg->bg_next_group);
2417 printk("bg_parent_dinode: %llu\n",
2418 (unsigned long long)bg->bg_parent_dinode);
2419 printk("bg_blkno: %llu\n",
2420 (unsigned long long)bg->bg_blkno);
ccd979bd
MF
2421}
2422
2423static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2424{
2425 int i;
2426
b0697053 2427 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
ccd979bd 2428 printk("i_signature: %s\n", fe->i_signature);
b0697053
MF
2429 printk("i_size: %llu\n",
2430 (unsigned long long)fe->i_size);
ccd979bd
MF
2431 printk("i_clusters: %u\n", fe->i_clusters);
2432 printk("i_generation: %u\n",
2433 le32_to_cpu(fe->i_generation));
2434 printk("id1.bitmap1.i_used: %u\n",
2435 le32_to_cpu(fe->id1.bitmap1.i_used));
2436 printk("id1.bitmap1.i_total: %u\n",
2437 le32_to_cpu(fe->id1.bitmap1.i_total));
2438 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2439 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2440 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2441 printk("id2.i_chain.cl_next_free_rec: %u\n",
2442 fe->id2.i_chain.cl_next_free_rec);
2443 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2444 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2445 fe->id2.i_chain.cl_recs[i].c_free);
2446 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2447 fe->id2.i_chain.cl_recs[i].c_total);
b0697053
MF
2448 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2449 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
ccd979bd
MF
2450 }
2451}
e7d4cb6b
TM
2452
2453/*
2454 * For a given allocation, determine which allocators will need to be
2455 * accessed, and lock them, reserving the appropriate number of bits.
2456 *
2457 * Sparse file systems call this from ocfs2_write_begin_nolock()
2458 * and ocfs2_allocate_unwritten_extents().
2459 *
2460 * File systems which don't support holes call this from
2461 * ocfs2_extend_allocation().
2462 */
f99b9b7c
JB
2463int ocfs2_lock_allocators(struct inode *inode,
2464 struct ocfs2_extent_tree *et,
e7d4cb6b
TM
2465 u32 clusters_to_add, u32 extents_to_split,
2466 struct ocfs2_alloc_context **data_ac,
f99b9b7c 2467 struct ocfs2_alloc_context **meta_ac)
e7d4cb6b
TM
2468{
2469 int ret = 0, num_free_extents;
2470 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2471 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2472
2473 *meta_ac = NULL;
2474 if (data_ac)
2475 *data_ac = NULL;
2476
2477 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2478
3d03a305 2479 num_free_extents = ocfs2_num_free_extents(osb, et);
e7d4cb6b
TM
2480 if (num_free_extents < 0) {
2481 ret = num_free_extents;
2482 mlog_errno(ret);
2483 goto out;
2484 }
2485
2486 /*
2487 * Sparse allocation file systems need to be more conservative
2488 * with reserving room for expansion - the actual allocation
2489 * happens while we've got a journal handle open so re-taking
2490 * a cluster lock (because we ran out of room for another
2491 * extent) will violate ordering rules.
2492 *
2493 * Most of the time we'll only be seeing this 1 cluster at a time
2494 * anyway.
2495 *
2496 * Always lock for any unwritten extents - we might want to
2497 * add blocks during a split.
2498 */
2499 if (!num_free_extents ||
2500 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
f99b9b7c 2501 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
e7d4cb6b
TM
2502 if (ret < 0) {
2503 if (ret != -ENOSPC)
2504 mlog_errno(ret);
2505 goto out;
2506 }
2507 }
2508
2509 if (clusters_to_add == 0)
2510 goto out;
2511
2512 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2513 if (ret < 0) {
2514 if (ret != -ENOSPC)
2515 mlog_errno(ret);
2516 goto out;
2517 }
2518
2519out:
2520 if (ret) {
2521 if (*meta_ac) {
2522 ocfs2_free_alloc_context(*meta_ac);
2523 *meta_ac = NULL;
2524 }
2525
2526 /*
2527 * We cannot have an error and a non null *data_ac.
2528 */
2529 }
2530
2531 return ret;
2532}
6ca497a8 2533
2534/*
2535 * Read the inode specified by blkno to get suballoc_slot and
2536 * suballoc_bit.
2537 */
2538static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2539 u16 *suballoc_slot, u16 *suballoc_bit)
2540{
2541 int status;
2542 struct buffer_head *inode_bh = NULL;
2543 struct ocfs2_dinode *inode_fe;
2544
5b09b507 2545 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
6ca497a8 2546
2547 /* dirty read disk */
2548 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2549 if (status < 0) {
5b09b507
JB
2550 mlog(ML_ERROR, "read block %llu failed %d\n",
2551 (unsigned long long)blkno, status);
6ca497a8 2552 goto bail;
2553 }
2554
2555 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2556 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
5b09b507
JB
2557 mlog(ML_ERROR, "invalid inode %llu requested\n",
2558 (unsigned long long)blkno);
6ca497a8 2559 status = -EINVAL;
2560 goto bail;
2561 }
2562
0fba8137 2563 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
6ca497a8 2564 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2565 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
5b09b507
JB
2566 (unsigned long long)blkno,
2567 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
6ca497a8 2568 status = -EINVAL;
2569 goto bail;
2570 }
2571
2572 if (suballoc_slot)
2573 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2574 if (suballoc_bit)
2575 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2576
2577bail:
2578 brelse(inode_bh);
2579
2580 mlog_exit(status);
2581 return status;
2582}
2583
2584/*
2585 * test whether bit is SET in allocator bitmap or not. on success, 0
2586 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2587 * is returned and *res is meaningless. Call this after you have
2588 * cluster locked against suballoc, or you may get a result based on
2589 * non-up2date contents
2590 */
2591static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2592 struct inode *suballoc,
2593 struct buffer_head *alloc_bh, u64 blkno,
2594 u16 bit, int *res)
2595{
2596 struct ocfs2_dinode *alloc_fe;
2597 struct ocfs2_group_desc *group;
2598 struct buffer_head *group_bh = NULL;
2599 u64 bg_blkno;
2600 int status;
2601
5b09b507
JB
2602 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2603 (unsigned int)bit);
6ca497a8 2604
2605 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2606 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2607 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2608 (unsigned int)bit,
2609 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2610 status = -EINVAL;
2611 goto bail;
2612 }
2613
2614 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2615 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2616 &group_bh);
2617 if (status < 0) {
5b09b507
JB
2618 mlog(ML_ERROR, "read group %llu failed %d\n",
2619 (unsigned long long)bg_blkno, status);
6ca497a8 2620 goto bail;
2621 }
2622
2623 group = (struct ocfs2_group_desc *) group_bh->b_data;
2624 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2625
2626bail:
2627 brelse(group_bh);
2628
2629 mlog_exit(status);
2630 return status;
2631}
2632
2633/*
2634 * Test if the bit representing this inode (blkno) is set in the
2635 * suballocator.
2636 *
2637 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2638 *
2639 * In the event of failure, a negative value is returned and *res is
2640 * meaningless.
2641 *
2642 * Callers must make sure to hold nfs_sync_lock to prevent
2643 * ocfs2_delete_inode() on another node from accessing the same
2644 * suballocator concurrently.
2645 */
2646int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2647{
2648 int status;
2649 u16 suballoc_bit = 0, suballoc_slot = 0;
2650 struct inode *inode_alloc_inode;
2651 struct buffer_head *alloc_bh = NULL;
2652
5b09b507 2653 mlog_entry("blkno: %llu", (unsigned long long)blkno);
6ca497a8 2654
2655 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2656 &suballoc_bit);
2657 if (status < 0) {
2658 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2659 goto bail;
2660 }
2661
2662 inode_alloc_inode =
2663 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2664 suballoc_slot);
2665 if (!inode_alloc_inode) {
2666 /* the error code could be inaccurate, but we are not able to
2667 * get the correct one. */
2668 status = -EINVAL;
2669 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2670 (u32)suballoc_slot);
2671 goto bail;
2672 }
2673
2674 mutex_lock(&inode_alloc_inode->i_mutex);
2675 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2676 if (status < 0) {
2677 mutex_unlock(&inode_alloc_inode->i_mutex);
2678 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2679 (u32)suballoc_slot, status);
2680 goto bail;
2681 }
2682
2683 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2684 blkno, suballoc_bit, res);
2685 if (status < 0)
2686 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2687
2688 ocfs2_inode_unlock(inode_alloc_inode, 0);
2689 mutex_unlock(&inode_alloc_inode->i_mutex);
2690
2691 iput(inode_alloc_inode);
2692 brelse(alloc_bh);
2693bail:
2694 mlog_exit(status);
2695 return status;
2696}
This page took 0.893023 seconds and 5 git commands to generate.