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