blob: 213bdca16fe48c7f814348abed5c42cb5d365071 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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"
38#include "dlmglue.h"
39#include "inode.h"
40#include "journal.h"
41#include "localalloc.h"
42#include "suballoc.h"
43#include "super.h"
44#include "sysfile.h"
45#include "uptodate.h"
46
47#include "buffer_head_io.h"
48
Tao Maffda89a2008-03-03 17:12:09 +080049#define NOT_ALLOC_NEW_GROUP 0
50#define ALLOC_NEW_GROUP 1
51
Tao Ma4d0ddb22008-03-05 16:11:46 +080052#define OCFS2_MAX_INODES_TO_STEAL 1024
53
Mark Fashehccd979b2005-12-15 14:31:24 -080054static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070057static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080058 struct inode *alloc_inode,
59 struct buffer_head *bg_bh,
60 u64 group_blkno,
61 u16 my_chain,
62 struct ocfs2_chain_list *cl);
63static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070065 struct buffer_head *bh,
66 u64 max_block);
Mark Fashehccd979b2005-12-15 14:31:24 -080067
Mark Fashehccd979b2005-12-15 14:31:24 -080068static int ocfs2_cluster_group_search(struct inode *inode,
69 struct buffer_head *group_bh,
70 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070071 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080072 u16 *bit_off, u16 *bits_found);
73static int ocfs2_block_group_search(struct inode *inode,
74 struct buffer_head *group_bh,
75 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070076 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080077 u16 *bit_off, u16 *bits_found);
Mark Fashehccd979b2005-12-15 14:31:24 -080078static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
79 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070080 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080081 u32 bits_wanted,
82 u32 min_bits,
83 u16 *bit_off,
84 unsigned int *num_bits,
85 u64 *bg_blkno);
86static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
87 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070088static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080089 struct inode *alloc_inode,
90 struct ocfs2_group_desc *bg,
91 struct buffer_head *group_bh,
92 unsigned int bit_off,
93 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -070094static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080095 struct inode *alloc_inode,
96 struct ocfs2_group_desc *bg,
97 struct buffer_head *group_bh,
98 unsigned int bit_off,
99 unsigned int num_bits);
100
Mark Fasheh1fabe142006-10-09 18:11:45 -0700101static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800102 struct inode *alloc_inode,
103 struct buffer_head *fe_bh,
104 struct buffer_head *bg_bh,
105 struct buffer_head *prev_bg_bh,
106 u16 chain);
107static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
108 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800109static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
110 u64 bg_blkno,
111 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800112static inline void ocfs2_block_to_cluster_group(struct inode *inode,
113 u64 data_blkno,
114 u64 *bg_blkno,
115 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700116static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
117 u32 bits_wanted, u64 max_block,
118 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800119
Mark Fasheh9c7af402008-07-28 18:02:53 -0700120void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800121{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700122 struct inode *inode = ac->ac_inode;
123
124 if (inode) {
125 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700126 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700127
128 mutex_unlock(&inode->i_mutex);
129
130 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800131 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700132 }
Tao Ma4d0ddb22008-03-05 16:11:46 +0800133 if (ac->ac_bh) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800134 brelse(ac->ac_bh);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800135 ac->ac_bh = NULL;
136 }
137}
138
139void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
140{
141 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800142 kfree(ac);
143}
144
145static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
146{
147 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
148}
149
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700150/* somewhat more expensive than our other checks, so use sparingly. */
Tao Mad6590722007-12-18 15:47:03 +0800151int ocfs2_check_group_descriptor(struct super_block *sb,
152 struct ocfs2_dinode *di,
153 struct ocfs2_group_desc *gd)
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700154{
155 unsigned int max_bits;
156
157 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
158 OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
159 return -EIO;
160 }
161
162 if (di->i_blkno != gd->bg_parent_dinode) {
163 ocfs2_error(sb, "Group descriptor # %llu has bad parent "
164 "pointer (%llu, expected %llu)",
165 (unsigned long long)le64_to_cpu(gd->bg_blkno),
166 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
167 (unsigned long long)le64_to_cpu(di->i_blkno));
168 return -EIO;
169 }
170
171 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
172 if (le16_to_cpu(gd->bg_bits) > max_bits) {
173 ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
174 (unsigned long long)le64_to_cpu(gd->bg_blkno),
175 le16_to_cpu(gd->bg_bits));
176 return -EIO;
177 }
178
179 if (le16_to_cpu(gd->bg_chain) >=
180 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
181 ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
182 (unsigned long long)le64_to_cpu(gd->bg_blkno),
183 le16_to_cpu(gd->bg_chain));
184 return -EIO;
185 }
186
187 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
188 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
189 "claims that %u are free",
190 (unsigned long long)le64_to_cpu(gd->bg_blkno),
191 le16_to_cpu(gd->bg_bits),
192 le16_to_cpu(gd->bg_free_bits_count));
193 return -EIO;
194 }
195
196 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
197 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
198 "max bitmap bits of %u",
199 (unsigned long long)le64_to_cpu(gd->bg_blkno),
200 le16_to_cpu(gd->bg_bits),
201 8 * le16_to_cpu(gd->bg_size));
202 return -EIO;
203 }
204
205 return 0;
206}
207
Mark Fasheh1fabe142006-10-09 18:11:45 -0700208static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800209 struct inode *alloc_inode,
210 struct buffer_head *bg_bh,
211 u64 group_blkno,
212 u16 my_chain,
213 struct ocfs2_chain_list *cl)
214{
215 int status = 0;
216 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
217 struct super_block * sb = alloc_inode->i_sb;
218
219 mlog_entry_void();
220
221 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800222 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
223 "b_blocknr (%llu)",
224 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800225 (unsigned long long) bg_bh->b_blocknr);
226 status = -EIO;
227 goto bail;
228 }
229
230 status = ocfs2_journal_access(handle,
231 alloc_inode,
232 bg_bh,
233 OCFS2_JOURNAL_ACCESS_CREATE);
234 if (status < 0) {
235 mlog_errno(status);
236 goto bail;
237 }
238
239 memset(bg, 0, sb->s_blocksize);
240 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
241 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
242 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
243 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
244 bg->bg_chain = cpu_to_le16(my_chain);
245 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
246 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
247 bg->bg_blkno = cpu_to_le64(group_blkno);
248 /* set the 1st bit in the bitmap to account for the descriptor block */
249 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
250 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
251
252 status = ocfs2_journal_dirty(handle, bg_bh);
253 if (status < 0)
254 mlog_errno(status);
255
256 /* There is no need to zero out or otherwise initialize the
257 * other blocks in a group - All valid FS metadata in a block
258 * group stores the superblock fs_generation value at
259 * allocation time. */
260
261bail:
262 mlog_exit(status);
263 return status;
264}
265
266static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
267{
268 u16 curr, best;
269
270 best = curr = 0;
271 while (curr < le16_to_cpu(cl->cl_count)) {
272 if (le32_to_cpu(cl->cl_recs[best].c_total) >
273 le32_to_cpu(cl->cl_recs[curr].c_total))
274 best = curr;
275 curr++;
276 }
277 return best;
278}
279
280/*
281 * We expect the block group allocator to already be locked.
282 */
283static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
284 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700285 struct buffer_head *bh,
286 u64 max_block)
Mark Fashehccd979b2005-12-15 14:31:24 -0800287{
288 int status, credits;
289 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
290 struct ocfs2_chain_list *cl;
291 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700292 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800293 u32 bit_off, num_bits;
294 u16 alloc_rec;
295 u64 bg_blkno;
296 struct buffer_head *bg_bh = NULL;
297 struct ocfs2_group_desc *bg;
298
299 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
300
301 mlog_entry_void();
302
Mark Fashehccd979b2005-12-15 14:31:24 -0800303 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700304 status = ocfs2_reserve_clusters_with_limit(osb,
305 le16_to_cpu(cl->cl_cpg),
306 max_block, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800307 if (status < 0) {
308 if (status != -ENOSPC)
309 mlog_errno(status);
310 goto bail;
311 }
312
313 credits = ocfs2_calc_group_alloc_credits(osb->sb,
314 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700315 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800316 if (IS_ERR(handle)) {
317 status = PTR_ERR(handle);
318 handle = NULL;
319 mlog_errno(status);
320 goto bail;
321 }
322
323 status = ocfs2_claim_clusters(osb,
324 handle,
325 ac,
326 le16_to_cpu(cl->cl_cpg),
327 &bit_off,
328 &num_bits);
329 if (status < 0) {
330 if (status != -ENOSPC)
331 mlog_errno(status);
332 goto bail;
333 }
334
335 alloc_rec = ocfs2_find_smallest_chain(cl);
336
337 /* setup the group */
338 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb06970532006-03-03 10:24:33 -0800339 mlog(0, "new descriptor, record %u, at block %llu\n",
340 alloc_rec, (unsigned long long)bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800341
342 bg_bh = sb_getblk(osb->sb, bg_blkno);
343 if (!bg_bh) {
344 status = -EIO;
345 mlog_errno(status);
346 goto bail;
347 }
348 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
349
350 status = ocfs2_block_group_fill(handle,
351 alloc_inode,
352 bg_bh,
353 bg_blkno,
354 alloc_rec,
355 cl);
356 if (status < 0) {
357 mlog_errno(status);
358 goto bail;
359 }
360
361 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
362
363 status = ocfs2_journal_access(handle, alloc_inode,
364 bh, OCFS2_JOURNAL_ACCESS_WRITE);
365 if (status < 0) {
366 mlog_errno(status);
367 goto bail;
368 }
369
370 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
371 le16_to_cpu(bg->bg_free_bits_count));
372 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
373 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
374 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
375 le16_add_cpu(&cl->cl_next_free_rec, 1);
376
377 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
378 le16_to_cpu(bg->bg_free_bits_count));
379 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
380 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
381
382 status = ocfs2_journal_dirty(handle, bh);
383 if (status < 0) {
384 mlog_errno(status);
385 goto bail;
386 }
387
388 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
389 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
390 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
391 le32_to_cpu(fe->i_clusters)));
392 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
393 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700394 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800395
396 status = 0;
397bail:
398 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700399 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800400
401 if (ac)
402 ocfs2_free_alloc_context(ac);
403
404 if (bg_bh)
405 brelse(bg_bh);
406
407 mlog_exit(status);
408 return status;
409}
410
411static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700412 struct ocfs2_alloc_context *ac,
413 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800414 u32 slot,
415 int alloc_new_group)
Mark Fashehccd979b2005-12-15 14:31:24 -0800416{
417 int status;
418 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700419 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800420 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800421 struct ocfs2_dinode *fe;
422 u32 free_bits;
423
424 mlog_entry_void();
425
Mark Fashehda5cbf22006-10-06 18:34:35 -0700426 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
427 if (!alloc_inode) {
428 mlog_errno(-EINVAL);
429 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800430 }
431
Mark Fashehda5cbf22006-10-06 18:34:35 -0700432 mutex_lock(&alloc_inode->i_mutex);
433
Mark Fashehe63aecb62007-10-18 15:30:42 -0700434 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700435 if (status < 0) {
436 mutex_unlock(&alloc_inode->i_mutex);
437 iput(alloc_inode);
438
439 mlog_errno(status);
440 return status;
441 }
442
443 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800444 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700445
Mark Fashehccd979b2005-12-15 14:31:24 -0800446 fe = (struct ocfs2_dinode *) bh->b_data;
447 if (!OCFS2_IS_VALID_DINODE(fe)) {
448 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
449 status = -EIO;
450 goto bail;
451 }
452 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800453 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
454 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800455 status = -EIO;
456 goto bail;
457 }
458
459 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
460 le32_to_cpu(fe->id1.bitmap1.i_used);
461
462 if (bits_wanted > free_bits) {
463 /* cluster bitmap never grows */
464 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
465 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
466 bits_wanted, free_bits);
467 status = -ENOSPC;
468 goto bail;
469 }
470
Tao Maffda89a2008-03-03 17:12:09 +0800471 if (alloc_new_group != ALLOC_NEW_GROUP) {
472 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
473 "and we don't alloc a new group for it.\n",
474 slot, bits_wanted, free_bits);
475 status = -ENOSPC;
476 goto bail;
477 }
478
Joel Becker1187c962008-09-03 20:03:39 -0700479 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
480 ac->ac_max_block);
Mark Fashehccd979b2005-12-15 14:31:24 -0800481 if (status < 0) {
482 if (status != -ENOSPC)
483 mlog_errno(status);
484 goto bail;
485 }
486 atomic_inc(&osb->alloc_stats.bg_extends);
487
488 /* You should never ask for this much metadata */
489 BUG_ON(bits_wanted >
490 (le32_to_cpu(fe->id1.bitmap1.i_total)
491 - le32_to_cpu(fe->id1.bitmap1.i_used)));
492 }
493
494 get_bh(bh);
495 ac->ac_bh = bh;
496bail:
497 if (bh)
498 brelse(bh);
499
500 mlog_exit(status);
501 return status;
502}
503
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800504int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
505 int blocks,
506 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800507{
508 int status;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700509 u32 slot;
Mark Fashehccd979b2005-12-15 14:31:24 -0800510
Robert P. J. Daycd861282006-12-13 00:34:52 -0800511 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800512 if (!(*ac)) {
513 status = -ENOMEM;
514 mlog_errno(status);
515 goto bail;
516 }
517
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800518 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700520 slot = osb->slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -0800521 (*ac)->ac_group_search = ocfs2_block_group_search;
522
Mark Fashehda5cbf22006-10-06 18:34:35 -0700523 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800524 EXTENT_ALLOC_SYSTEM_INODE,
525 slot, ALLOC_NEW_GROUP);
Mark Fashehccd979b2005-12-15 14:31:24 -0800526 if (status < 0) {
527 if (status != -ENOSPC)
528 mlog_errno(status);
529 goto bail;
530 }
531
532 status = 0;
533bail:
534 if ((status < 0) && *ac) {
535 ocfs2_free_alloc_context(*ac);
536 *ac = NULL;
537 }
538
Mark Fashehccd979b2005-12-15 14:31:24 -0800539 mlog_exit(status);
540 return status;
541}
542
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800543int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
544 struct ocfs2_extent_list *root_el,
545 struct ocfs2_alloc_context **ac)
546{
547 return ocfs2_reserve_new_metadata_blocks(osb,
548 ocfs2_extend_meta_needed(root_el),
549 ac);
550}
551
Tao Ma4d0ddb22008-03-05 16:11:46 +0800552static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
553 struct ocfs2_alloc_context *ac)
554{
555 int i, status = -ENOSPC;
556 s16 slot = ocfs2_get_inode_steal_slot(osb);
557
558 /* Start to steal inodes from the first slot after ours. */
559 if (slot == OCFS2_INVALID_SLOT)
560 slot = osb->slot_num + 1;
561
562 for (i = 0; i < osb->max_slots; i++, slot++) {
563 if (slot == osb->max_slots)
564 slot = 0;
565
566 if (slot == osb->slot_num)
567 continue;
568
569 status = ocfs2_reserve_suballoc_bits(osb, ac,
570 INODE_ALLOC_SYSTEM_INODE,
571 slot, NOT_ALLOC_NEW_GROUP);
572 if (status >= 0) {
573 ocfs2_set_inode_steal_slot(osb, slot);
574 break;
575 }
576
577 ocfs2_free_ac_resource(ac);
578 }
579
580 return status;
581}
582
Mark Fashehccd979b2005-12-15 14:31:24 -0800583int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800584 struct ocfs2_alloc_context **ac)
585{
586 int status;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800587 s16 slot = ocfs2_get_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800588
Robert P. J. Daycd861282006-12-13 00:34:52 -0800589 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800590 if (!(*ac)) {
591 status = -ENOMEM;
592 mlog_errno(status);
593 goto bail;
594 }
595
596 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800597 (*ac)->ac_which = OCFS2_AC_USE_INODE;
598
Mark Fashehccd979b2005-12-15 14:31:24 -0800599 (*ac)->ac_group_search = ocfs2_block_group_search;
600
Tao Ma4d0ddb22008-03-05 16:11:46 +0800601 /*
Joel Becker1187c962008-09-03 20:03:39 -0700602 * stat(2) can't handle i_ino > 32bits, so we tell the
603 * lower levels not to allocate us a block group past that
604 * limit.
605 */
606 (*ac)->ac_max_block = (u32)~0U;
607
608 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +0800609 * slot is set when we successfully steal inode from other nodes.
610 * It is reset in 3 places:
611 * 1. when we flush the truncate log
612 * 2. when we complete local alloc recovery.
613 * 3. when we successfully allocate from our own slot.
614 * After it is set, we will go on stealing inodes until we find the
615 * need to check our slots to see whether there is some space for us.
616 */
617 if (slot != OCFS2_INVALID_SLOT &&
618 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
619 goto inode_steal;
620
621 atomic_set(&osb->s_num_inodes_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700622 status = ocfs2_reserve_suballoc_bits(osb, *ac,
623 INODE_ALLOC_SYSTEM_INODE,
Tao Maffda89a2008-03-03 17:12:09 +0800624 osb->slot_num, ALLOC_NEW_GROUP);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800625 if (status >= 0) {
626 status = 0;
627
628 /*
629 * Some inodes must be freed by us, so try to allocate
630 * from our own next time.
631 */
632 if (slot != OCFS2_INVALID_SLOT)
633 ocfs2_init_inode_steal_slot(osb);
634 goto bail;
635 } else if (status < 0 && status != -ENOSPC) {
636 mlog_errno(status);
637 goto bail;
638 }
639
640 ocfs2_free_ac_resource(*ac);
641
642inode_steal:
643 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
644 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -0800645 if (status < 0) {
646 if (status != -ENOSPC)
647 mlog_errno(status);
648 goto bail;
649 }
650
651 status = 0;
652bail:
653 if ((status < 0) && *ac) {
654 ocfs2_free_alloc_context(*ac);
655 *ac = NULL;
656 }
657
Mark Fashehccd979b2005-12-15 14:31:24 -0800658 mlog_exit(status);
659 return status;
660}
661
662/* local alloc code has to do the same thing, so rather than do this
663 * twice.. */
664int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
665 struct ocfs2_alloc_context *ac)
666{
667 int status;
668
Mark Fashehccd979b2005-12-15 14:31:24 -0800669 ac->ac_which = OCFS2_AC_USE_MAIN;
670 ac->ac_group_search = ocfs2_cluster_group_search;
671
Mark Fashehda5cbf22006-10-06 18:34:35 -0700672 status = ocfs2_reserve_suballoc_bits(osb, ac,
673 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Maffda89a2008-03-03 17:12:09 +0800674 OCFS2_INVALID_SLOT,
675 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700676 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800677 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700678 goto bail;
679 }
680
Mark Fashehccd979b2005-12-15 14:31:24 -0800681bail:
682 return status;
683}
684
685/* Callers don't need to care which bitmap (local alloc or main) to
686 * use so we figure it out for them, but unfortunately this clutters
687 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -0700688static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
689 u32 bits_wanted, u64 max_block,
690 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800691{
692 int status;
693
694 mlog_entry_void();
695
Robert P. J. Daycd861282006-12-13 00:34:52 -0800696 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800697 if (!(*ac)) {
698 status = -ENOMEM;
699 mlog_errno(status);
700 goto bail;
701 }
702
703 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -0700704 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -0800705
706 status = -ENOSPC;
707 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
708 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800709 bits_wanted,
710 *ac);
Joel Becker1187c962008-09-03 20:03:39 -0700711 if (status == -EFBIG) {
712 /* The local alloc window is outside ac_max_block.
713 * use the main bitmap. */
714 status = -ENOSPC;
715 } else if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800716 mlog_errno(status);
717 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800718 }
719 }
720
721 if (status == -ENOSPC) {
722 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
723 if (status < 0) {
724 if (status != -ENOSPC)
725 mlog_errno(status);
726 goto bail;
727 }
728 }
729
730 status = 0;
731bail:
732 if ((status < 0) && *ac) {
733 ocfs2_free_alloc_context(*ac);
734 *ac = NULL;
735 }
736
737 mlog_exit(status);
738 return status;
739}
740
Joel Becker1187c962008-09-03 20:03:39 -0700741int ocfs2_reserve_clusters(struct ocfs2_super *osb,
742 u32 bits_wanted,
743 struct ocfs2_alloc_context **ac)
744{
745 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
746}
747
Mark Fashehccd979b2005-12-15 14:31:24 -0800748/*
749 * More or less lifted from ext3. I'll leave their description below:
750 *
751 * "For ext3 allocations, we must not reuse any blocks which are
752 * allocated in the bitmap buffer's "last committed data" copy. This
753 * prevents deletes from freeing up the page for reuse until we have
754 * committed the delete transaction.
755 *
756 * If we didn't do this, then deleting something and reallocating it as
757 * data would allow the old block to be overwritten before the
758 * transaction committed (because we force data to disk before commit).
759 * This would lead to corruption if we crashed between overwriting the
760 * data and committing the delete.
761 *
762 * @@@ We may want to make this allocation behaviour conditional on
763 * data-writes at some point, and disable it for metadata allocations or
764 * sync-data inodes."
765 *
766 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +0200767 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -0800768 * called on a metadata group descriptor.
769 */
770static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
771 int nr)
772{
773 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
774
775 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
776 return 0;
777 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
778 return 1;
779
780 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
781 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
782}
783
784static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
785 struct buffer_head *bg_bh,
786 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700787 unsigned int total_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 u16 *bit_off,
789 u16 *bits_found)
790{
791 void *bitmap;
792 u16 best_offset, best_size;
793 int offset, start, found, status = 0;
794 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
795
796 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
797 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
798 return -EIO;
799 }
800
801 found = start = best_offset = best_size = 0;
802 bitmap = bg->bg_bitmap;
803
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700804 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
805 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 break;
807
808 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
809 /* We found a zero, but we can't use it as it
810 * hasn't been put to disk yet! */
811 found = 0;
812 start = offset + 1;
813 } else if (offset == start) {
814 /* we found a zero */
815 found++;
816 /* move start to the next bit to test */
817 start++;
818 } else {
819 /* got a zero after some ones */
820 found = 1;
821 start = offset + 1;
822 }
823 if (found > best_size) {
824 best_size = found;
825 best_offset = start - found;
826 }
827 /* we got everything we needed */
828 if (found == bits_wanted) {
829 /* mlog(0, "Found it all!\n"); */
830 break;
831 }
832 }
833
834 /* XXX: I think the first clause is equivalent to the second
835 * - jlbec */
836 if (found == bits_wanted) {
837 *bit_off = start - found;
838 *bits_found = found;
839 } else if (best_size) {
840 *bit_off = best_offset;
841 *bits_found = best_size;
842 } else {
843 status = -ENOSPC;
844 /* No error log here -- see the comment above
845 * ocfs2_test_bg_bit_allocatable */
846 }
847
848 return status;
849}
850
Mark Fasheh1fabe142006-10-09 18:11:45 -0700851static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800852 struct inode *alloc_inode,
853 struct ocfs2_group_desc *bg,
854 struct buffer_head *group_bh,
855 unsigned int bit_off,
856 unsigned int num_bits)
857{
858 int status;
859 void *bitmap = bg->bg_bitmap;
860 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
861
862 mlog_entry_void();
863
864 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
865 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
866 status = -EIO;
867 goto bail;
868 }
869 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
870
871 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
872 num_bits);
873
874 if (ocfs2_is_cluster_bitmap(alloc_inode))
875 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
876
877 status = ocfs2_journal_access(handle,
878 alloc_inode,
879 group_bh,
880 journal_type);
881 if (status < 0) {
882 mlog_errno(status);
883 goto bail;
884 }
885
886 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
887
888 while(num_bits--)
889 ocfs2_set_bit(bit_off++, bitmap);
890
891 status = ocfs2_journal_dirty(handle,
892 group_bh);
893 if (status < 0) {
894 mlog_errno(status);
895 goto bail;
896 }
897
898bail:
899 mlog_exit(status);
900 return status;
901}
902
903/* find the one with the most empty bits */
904static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
905{
906 u16 curr, best;
907
908 BUG_ON(!cl->cl_next_free_rec);
909
910 best = curr = 0;
911 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
912 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
913 le32_to_cpu(cl->cl_recs[best].c_free))
914 best = curr;
915 curr++;
916 }
917
918 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
919 return best;
920}
921
Mark Fasheh1fabe142006-10-09 18:11:45 -0700922static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800923 struct inode *alloc_inode,
924 struct buffer_head *fe_bh,
925 struct buffer_head *bg_bh,
926 struct buffer_head *prev_bg_bh,
927 u16 chain)
928{
929 int status;
930 /* there is a really tiny chance the journal calls could fail,
931 * but we wouldn't want inconsistent blocks in *any* case. */
932 u64 fe_ptr, bg_ptr, prev_bg_ptr;
933 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
934 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
935 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
936
937 if (!OCFS2_IS_VALID_DINODE(fe)) {
938 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
939 status = -EIO;
940 goto out;
941 }
942 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
943 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
944 status = -EIO;
945 goto out;
946 }
947 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
948 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
949 status = -EIO;
950 goto out;
951 }
952
Mark Fashehb06970532006-03-03 10:24:33 -0800953 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700954 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
955 (unsigned long long)le64_to_cpu(bg->bg_blkno),
956 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800957
958 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
959 bg_ptr = le64_to_cpu(bg->bg_next_group);
960 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
961
962 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
963 OCFS2_JOURNAL_ACCESS_WRITE);
964 if (status < 0) {
965 mlog_errno(status);
966 goto out_rollback;
967 }
968
969 prev_bg->bg_next_group = bg->bg_next_group;
970
971 status = ocfs2_journal_dirty(handle, prev_bg_bh);
972 if (status < 0) {
973 mlog_errno(status);
974 goto out_rollback;
975 }
976
977 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
978 OCFS2_JOURNAL_ACCESS_WRITE);
979 if (status < 0) {
980 mlog_errno(status);
981 goto out_rollback;
982 }
983
984 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
985
986 status = ocfs2_journal_dirty(handle, bg_bh);
987 if (status < 0) {
988 mlog_errno(status);
989 goto out_rollback;
990 }
991
992 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
993 OCFS2_JOURNAL_ACCESS_WRITE);
994 if (status < 0) {
995 mlog_errno(status);
996 goto out_rollback;
997 }
998
999 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1000
1001 status = ocfs2_journal_dirty(handle, fe_bh);
1002 if (status < 0) {
1003 mlog_errno(status);
1004 goto out_rollback;
1005 }
1006
1007 status = 0;
1008out_rollback:
1009 if (status < 0) {
1010 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1011 bg->bg_next_group = cpu_to_le64(bg_ptr);
1012 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1013 }
1014out:
1015 mlog_exit(status);
1016 return status;
1017}
1018
1019static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1020 u32 wanted)
1021{
1022 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1023}
1024
1025/* return 0 on success, -ENOSPC to keep searching and any other < 0
1026 * value on error. */
1027static int ocfs2_cluster_group_search(struct inode *inode,
1028 struct buffer_head *group_bh,
1029 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001030 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001031 u16 *bit_off, u16 *bits_found)
1032{
1033 int search = -ENOSPC;
1034 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001035 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001036 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001037 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001038 u16 tmp_off, tmp_found;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001039 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001040
1041 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1042
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001043 if (gd->bg_free_bits_count) {
1044 max_bits = le16_to_cpu(gd->bg_bits);
1045
1046 /* Tail groups in cluster bitmaps which aren't cpg
1047 * aligned are prone to partial extention by a failed
1048 * fs resize. If the file system resize never got to
1049 * update the dinode cluster count, then we don't want
1050 * to trust any clusters past it, regardless of what
1051 * the group descriptor says. */
1052 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1053 le64_to_cpu(gd->bg_blkno));
1054 if ((gd_cluster_off + max_bits) >
1055 OCFS2_I(inode)->ip_clusters) {
1056 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1057 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1058 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1059 le16_to_cpu(gd->bg_bits),
1060 OCFS2_I(inode)->ip_clusters, max_bits);
1061 }
1062
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1064 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001065 max_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001066 &tmp_off, &tmp_found);
1067 if (ret)
1068 return ret;
1069
Joel Becker1187c962008-09-03 20:03:39 -07001070 if (max_block) {
1071 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1072 gd_cluster_off +
1073 tmp_off + tmp_found);
1074 mlog(0, "Checking %llu against %llu\n",
1075 (unsigned long long)blkoff,
1076 (unsigned long long)max_block);
1077 if (blkoff > max_block)
1078 return -ENOSPC;
1079 }
1080
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 /* ocfs2_block_group_find_clear_bits() might
1082 * return success, but we still want to return
1083 * -ENOSPC unless it found the minimum number
1084 * of bits. */
1085 if (min_bits <= tmp_found) {
1086 *bit_off = tmp_off;
1087 *bits_found = tmp_found;
1088 search = 0; /* success */
Mark Fasheh9c7af402008-07-28 18:02:53 -07001089 } else if (tmp_found) {
1090 /*
1091 * Don't show bits which we'll be returning
1092 * for allocation to the local alloc bitmap.
1093 */
1094 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
Mark Fashehccd979b2005-12-15 14:31:24 -08001095 }
1096 }
1097
1098 return search;
1099}
1100
1101static int ocfs2_block_group_search(struct inode *inode,
1102 struct buffer_head *group_bh,
1103 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001104 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001105 u16 *bit_off, u16 *bits_found)
1106{
1107 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001108 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001109 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1110
1111 BUG_ON(min_bits != 1);
1112 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1113
Joel Becker1187c962008-09-03 20:03:39 -07001114 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001115 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1116 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001117 le16_to_cpu(bg->bg_bits),
Mark Fashehccd979b2005-12-15 14:31:24 -08001118 bit_off, bits_found);
Joel Becker1187c962008-09-03 20:03:39 -07001119 if (!ret && max_block) {
1120 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1121 *bits_found;
1122 mlog(0, "Checking %llu against %llu\n",
1123 (unsigned long long)blkoff,
1124 (unsigned long long)max_block);
1125 if (blkoff > max_block)
1126 ret = -ENOSPC;
1127 }
1128 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001129
1130 return ret;
1131}
1132
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001133static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001134 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001135 struct buffer_head *di_bh,
1136 u32 num_bits,
1137 u16 chain)
1138{
1139 int ret;
1140 u32 tmp_used;
1141 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1142 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1143
1144 ret = ocfs2_journal_access(handle, inode, di_bh,
1145 OCFS2_JOURNAL_ACCESS_WRITE);
1146 if (ret < 0) {
1147 mlog_errno(ret);
1148 goto out;
1149 }
1150
1151 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1152 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1153 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1154
1155 ret = ocfs2_journal_dirty(handle, di_bh);
1156 if (ret < 0)
1157 mlog_errno(ret);
1158
1159out:
1160 return ret;
1161}
1162
1163static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001164 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001165 u32 bits_wanted,
1166 u32 min_bits,
1167 u16 *bit_off,
1168 unsigned int *num_bits,
1169 u64 gd_blkno,
1170 u16 *bits_left)
1171{
1172 int ret;
1173 u16 found;
1174 struct buffer_head *group_bh = NULL;
1175 struct ocfs2_group_desc *gd;
1176 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001177
1178 ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1179 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1180 if (ret < 0) {
1181 mlog_errno(ret);
1182 return ret;
1183 }
1184
1185 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1186 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1187 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1188 ret = -EIO;
1189 goto out;
1190 }
1191
1192 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001193 ac->ac_max_block, bit_off, &found);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001194 if (ret < 0) {
1195 if (ret != -ENOSPC)
1196 mlog_errno(ret);
1197 goto out;
1198 }
1199
1200 *num_bits = found;
1201
1202 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1203 *num_bits,
1204 le16_to_cpu(gd->bg_chain));
1205 if (ret < 0) {
1206 mlog_errno(ret);
1207 goto out;
1208 }
1209
1210 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1211 *bit_off, *num_bits);
1212 if (ret < 0)
1213 mlog_errno(ret);
1214
1215 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1216
1217out:
1218 brelse(group_bh);
1219
1220 return ret;
1221}
1222
Mark Fashehccd979b2005-12-15 14:31:24 -08001223static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001224 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 u32 bits_wanted,
1226 u32 min_bits,
1227 u16 *bit_off,
1228 unsigned int *num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001229 u64 *bg_blkno,
1230 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001231{
1232 int status;
1233 u16 chain, tmp_bits;
1234 u32 tmp_used;
1235 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001236 struct inode *alloc_inode = ac->ac_inode;
1237 struct buffer_head *group_bh = NULL;
1238 struct buffer_head *prev_group_bh = NULL;
1239 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1240 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1241 struct ocfs2_group_desc *bg;
1242
1243 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001244 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1245 bits_wanted, chain,
1246 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001247
1248 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1249 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1250 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1251 if (status < 0) {
1252 mlog_errno(status);
1253 goto bail;
1254 }
1255 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001256 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1257 if (status) {
1258 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 goto bail;
1260 }
1261
1262 status = -ENOSPC;
1263 /* for now, the chain search is a bit simplistic. We just use
1264 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001265 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1266 bits_wanted, min_bits,
1267 ac->ac_max_block, bit_off,
1268 &tmp_bits)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001269 if (!bg->bg_next_group)
1270 break;
Mark Fashehccd979b2005-12-15 14:31:24 -08001271 if (prev_group_bh) {
1272 brelse(prev_group_bh);
1273 prev_group_bh = NULL;
1274 }
1275 next_group = le64_to_cpu(bg->bg_next_group);
1276 prev_group_bh = group_bh;
1277 group_bh = NULL;
1278 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1279 next_group, &group_bh,
1280 OCFS2_BH_CACHED, alloc_inode);
1281 if (status < 0) {
1282 mlog_errno(status);
1283 goto bail;
1284 }
1285 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001286 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1287 if (status) {
1288 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 goto bail;
1290 }
1291 }
1292 if (status < 0) {
1293 if (status != -ENOSPC)
1294 mlog_errno(status);
1295 goto bail;
1296 }
1297
Mark Fashehb06970532006-03-03 10:24:33 -08001298 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001299 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001300
1301 *num_bits = tmp_bits;
1302
1303 BUG_ON(*num_bits == 0);
1304
1305 /*
1306 * Keep track of previous block descriptor read. When
1307 * we find a target, if we have read more than X
1308 * number of descriptors, and the target is reasonably
1309 * empty, relink him to top of his chain.
1310 *
1311 * We've read 0 extra blocks and only send one more to
1312 * the transaction, yet the next guy to search has a
1313 * much easier time.
1314 *
1315 * Do this *after* figuring out how many bits we're taking out
1316 * of our target group.
1317 */
1318 if (ac->ac_allow_chain_relink &&
1319 (prev_group_bh) &&
1320 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1321 status = ocfs2_relink_block_group(handle, alloc_inode,
1322 ac->ac_bh, group_bh,
1323 prev_group_bh, chain);
1324 if (status < 0) {
1325 mlog_errno(status);
1326 goto bail;
1327 }
1328 }
1329
1330 /* Ok, claim our bits now: set the info on dinode, chainlist
1331 * and then the group */
1332 status = ocfs2_journal_access(handle,
1333 alloc_inode,
1334 ac->ac_bh,
1335 OCFS2_JOURNAL_ACCESS_WRITE);
1336 if (status < 0) {
1337 mlog_errno(status);
1338 goto bail;
1339 }
1340
1341 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1342 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1343 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1344
1345 status = ocfs2_journal_dirty(handle,
1346 ac->ac_bh);
1347 if (status < 0) {
1348 mlog_errno(status);
1349 goto bail;
1350 }
1351
1352 status = ocfs2_block_group_set_bits(handle,
1353 alloc_inode,
1354 bg,
1355 group_bh,
1356 *bit_off,
1357 *num_bits);
1358 if (status < 0) {
1359 mlog_errno(status);
1360 goto bail;
1361 }
1362
Mark Fashehb06970532006-03-03 10:24:33 -08001363 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001364 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001365
1366 *bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001367 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001368bail:
1369 if (group_bh)
1370 brelse(group_bh);
1371 if (prev_group_bh)
1372 brelse(prev_group_bh);
1373
1374 mlog_exit(status);
1375 return status;
1376}
1377
1378/* will give out up to bits_wanted contiguous bits. */
1379static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1380 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001381 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001382 u32 bits_wanted,
1383 u32 min_bits,
1384 u16 *bit_off,
1385 unsigned int *num_bits,
1386 u64 *bg_blkno)
1387{
1388 int status;
1389 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001390 u16 bits_left = 0;
1391 u64 hint_blkno = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001392 struct ocfs2_chain_list *cl;
1393 struct ocfs2_dinode *fe;
1394
1395 mlog_entry_void();
1396
1397 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1398 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1399 BUG_ON(!ac->ac_bh);
1400
1401 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1402 if (!OCFS2_IS_VALID_DINODE(fe)) {
1403 OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1404 status = -EIO;
1405 goto bail;
1406 }
1407 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1408 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001409 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1410 "bits but only %u total.",
1411 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001412 le32_to_cpu(fe->id1.bitmap1.i_used),
1413 le32_to_cpu(fe->id1.bitmap1.i_total));
1414 status = -EIO;
1415 goto bail;
1416 }
1417
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001418 if (hint_blkno) {
1419 /* Attempt to short-circuit the usual search mechanism
1420 * by jumping straight to the most recently used
1421 * allocation group. This helps us mantain some
1422 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001423 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1424 min_bits, bit_off, num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001425 hint_blkno, &bits_left);
1426 if (!status) {
1427 /* Be careful to update *bg_blkno here as the
1428 * caller is expecting it to be filled in, and
1429 * ocfs2_search_one_group() won't do that for
1430 * us. */
1431 *bg_blkno = hint_blkno;
1432 goto set_hint;
1433 }
1434 if (status < 0 && status != -ENOSPC) {
1435 mlog_errno(status);
1436 goto bail;
1437 }
1438 }
1439
Mark Fashehccd979b2005-12-15 14:31:24 -08001440 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1441
1442 victim = ocfs2_find_victim_chain(cl);
1443 ac->ac_chain = victim;
1444 ac->ac_allow_chain_relink = 1;
1445
Mark Fashehda5cbf22006-10-06 18:34:35 -07001446 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001447 num_bits, bg_blkno, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001448 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001449 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001450 if (status < 0 && status != -ENOSPC) {
1451 mlog_errno(status);
1452 goto bail;
1453 }
1454
1455 mlog(0, "Search of victim chain %u came up with nothing, "
1456 "trying all chains now.\n", victim);
1457
1458 /* If we didn't pick a good victim, then just default to
1459 * searching each chain in order. Don't allow chain relinking
1460 * because we only calculate enough journal credits for one
1461 * relink per alloc. */
1462 ac->ac_allow_chain_relink = 0;
1463 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1464 if (i == victim)
1465 continue;
1466 if (!cl->cl_recs[i].c_free)
1467 continue;
1468
1469 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001470 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001471 bit_off, num_bits, bg_blkno,
1472 &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001473 if (!status)
1474 break;
1475 if (status < 0 && status != -ENOSPC) {
1476 mlog_errno(status);
1477 goto bail;
1478 }
1479 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001480
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001481set_hint:
1482 if (status != -ENOSPC) {
1483 /* If the next search of this group is not likely to
1484 * yield a suitable extent, then we reset the last
1485 * group hint so as to not waste a disk read */
1486 if (bits_left < min_bits)
1487 ac->ac_last_group = 0;
1488 else
1489 ac->ac_last_group = *bg_blkno;
1490 }
1491
1492bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001493 mlog_exit(status);
1494 return status;
1495}
1496
1497int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001498 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 struct ocfs2_alloc_context *ac,
1500 u32 bits_wanted,
1501 u16 *suballoc_bit_start,
1502 unsigned int *num_bits,
1503 u64 *blkno_start)
1504{
1505 int status;
1506 u64 bg_blkno;
1507
1508 BUG_ON(!ac);
1509 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1510 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001511
1512 status = ocfs2_claim_suballoc_bits(osb,
1513 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001514 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001515 bits_wanted,
1516 1,
1517 suballoc_bit_start,
1518 num_bits,
1519 &bg_blkno);
1520 if (status < 0) {
1521 mlog_errno(status);
1522 goto bail;
1523 }
1524 atomic_inc(&osb->alloc_stats.bg_allocs);
1525
1526 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1527 ac->ac_bits_given += (*num_bits);
1528 status = 0;
1529bail:
1530 mlog_exit(status);
1531 return status;
1532}
1533
1534int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001535 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001536 struct ocfs2_alloc_context *ac,
1537 u16 *suballoc_bit,
1538 u64 *fe_blkno)
1539{
1540 int status;
1541 unsigned int num_bits;
1542 u64 bg_blkno;
1543
1544 mlog_entry_void();
1545
1546 BUG_ON(!ac);
1547 BUG_ON(ac->ac_bits_given != 0);
1548 BUG_ON(ac->ac_bits_wanted != 1);
1549 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001550
1551 status = ocfs2_claim_suballoc_bits(osb,
1552 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001553 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001554 1,
1555 1,
1556 suballoc_bit,
1557 &num_bits,
1558 &bg_blkno);
1559 if (status < 0) {
1560 mlog_errno(status);
1561 goto bail;
1562 }
1563 atomic_inc(&osb->alloc_stats.bg_allocs);
1564
1565 BUG_ON(num_bits != 1);
1566
1567 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1568 ac->ac_bits_given++;
1569 status = 0;
1570bail:
1571 mlog_exit(status);
1572 return status;
1573}
1574
1575/* translate a group desc. blkno and it's bitmap offset into
1576 * disk cluster offset. */
1577static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1578 u64 bg_blkno,
1579 u16 bg_bit_off)
1580{
1581 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1582 u32 cluster = 0;
1583
1584 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1585
1586 if (bg_blkno != osb->first_cluster_group_blkno)
1587 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1588 cluster += (u32) bg_bit_off;
1589 return cluster;
1590}
1591
1592/* given a cluster offset, calculate which block group it belongs to
1593 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08001594u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08001595{
1596 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1597 u32 group_no;
1598
1599 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1600
1601 group_no = cluster / osb->bitmap_cpg;
1602 if (!group_no)
1603 return osb->first_cluster_group_blkno;
1604 return ocfs2_clusters_to_blocks(inode->i_sb,
1605 group_no * osb->bitmap_cpg);
1606}
1607
1608/* given the block number of a cluster start, calculate which cluster
1609 * group and descriptor bitmap offset that corresponds to. */
1610static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1611 u64 data_blkno,
1612 u64 *bg_blkno,
1613 u16 *bg_bit_off)
1614{
1615 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1616 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1617
1618 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1619
1620 *bg_blkno = ocfs2_which_cluster_group(inode,
1621 data_cluster);
1622
1623 if (*bg_blkno == osb->first_cluster_group_blkno)
1624 *bg_bit_off = (u16) data_cluster;
1625 else
1626 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1627 data_blkno - *bg_blkno);
1628}
1629
1630/*
1631 * min_bits - minimum contiguous chunk from this total allocation we
1632 * can handle. set to what we asked for originally for a full
1633 * contig. allocation, set to '1' to indicate we can deal with extents
1634 * of any size.
1635 */
Mark Fasheh415cb802007-09-16 20:10:16 -07001636int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1637 handle_t *handle,
1638 struct ocfs2_alloc_context *ac,
1639 u32 min_clusters,
1640 u32 max_clusters,
1641 u32 *cluster_start,
1642 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08001643{
1644 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07001645 unsigned int bits_wanted = max_clusters;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001646 u64 bg_blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001647 u16 bg_bit_off;
1648
1649 mlog_entry_void();
1650
Mark Fashehccd979b2005-12-15 14:31:24 -08001651 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1652
1653 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1654 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001655
1656 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1657 status = ocfs2_claim_local_alloc_bits(osb,
1658 handle,
1659 ac,
1660 bits_wanted,
1661 cluster_start,
1662 num_clusters);
1663 if (!status)
1664 atomic_inc(&osb->alloc_stats.local_data);
1665 } else {
1666 if (min_clusters > (osb->bitmap_cpg - 1)) {
1667 /* The only paths asking for contiguousness
1668 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001669 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1670 "group bitmap size %u!\n", min_clusters,
1671 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08001672 status = -ENOSPC;
1673 goto bail;
1674 }
1675 /* clamp the current request down to a realistic size. */
1676 if (bits_wanted > (osb->bitmap_cpg - 1))
1677 bits_wanted = osb->bitmap_cpg - 1;
1678
1679 status = ocfs2_claim_suballoc_bits(osb,
1680 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001681 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001682 bits_wanted,
1683 min_clusters,
1684 &bg_bit_off,
1685 num_clusters,
1686 &bg_blkno);
1687 if (!status) {
1688 *cluster_start =
1689 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1690 bg_blkno,
1691 bg_bit_off);
1692 atomic_inc(&osb->alloc_stats.bitmap_data);
1693 }
1694 }
1695 if (status < 0) {
1696 if (status != -ENOSPC)
1697 mlog_errno(status);
1698 goto bail;
1699 }
1700
1701 ac->ac_bits_given += *num_clusters;
1702
1703bail:
1704 mlog_exit(status);
1705 return status;
1706}
1707
Mark Fasheh415cb802007-09-16 20:10:16 -07001708int ocfs2_claim_clusters(struct ocfs2_super *osb,
1709 handle_t *handle,
1710 struct ocfs2_alloc_context *ac,
1711 u32 min_clusters,
1712 u32 *cluster_start,
1713 u32 *num_clusters)
1714{
1715 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1716
1717 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1718 bits_wanted, cluster_start, num_clusters);
1719}
1720
Mark Fasheh1fabe142006-10-09 18:11:45 -07001721static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001722 struct inode *alloc_inode,
1723 struct ocfs2_group_desc *bg,
1724 struct buffer_head *group_bh,
1725 unsigned int bit_off,
1726 unsigned int num_bits)
1727{
1728 int status;
1729 unsigned int tmp;
1730 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1731 struct ocfs2_group_desc *undo_bg = NULL;
1732
1733 mlog_entry_void();
1734
1735 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1736 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1737 status = -EIO;
1738 goto bail;
1739 }
1740
1741 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1742
1743 if (ocfs2_is_cluster_bitmap(alloc_inode))
1744 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1745
1746 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1747 journal_type);
1748 if (status < 0) {
1749 mlog_errno(status);
1750 goto bail;
1751 }
1752
1753 if (ocfs2_is_cluster_bitmap(alloc_inode))
1754 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1755
1756 tmp = num_bits;
1757 while(tmp--) {
1758 ocfs2_clear_bit((bit_off + tmp),
1759 (unsigned long *) bg->bg_bitmap);
1760 if (ocfs2_is_cluster_bitmap(alloc_inode))
1761 ocfs2_set_bit(bit_off + tmp,
1762 (unsigned long *) undo_bg->bg_bitmap);
1763 }
1764 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1765
1766 status = ocfs2_journal_dirty(handle, group_bh);
1767 if (status < 0)
1768 mlog_errno(status);
1769bail:
1770 return status;
1771}
1772
1773/*
1774 * expects the suballoc inode to already be locked.
1775 */
Mark Fasheh2b604352007-06-22 15:45:27 -07001776int ocfs2_free_suballoc_bits(handle_t *handle,
1777 struct inode *alloc_inode,
1778 struct buffer_head *alloc_bh,
1779 unsigned int start_bit,
1780 u64 bg_blkno,
1781 unsigned int count)
Mark Fashehccd979b2005-12-15 14:31:24 -08001782{
1783 int status = 0;
1784 u32 tmp_used;
1785 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1786 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1787 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1788 struct buffer_head *group_bh = NULL;
1789 struct ocfs2_group_desc *group;
1790
1791 mlog_entry_void();
1792
1793 if (!OCFS2_IS_VALID_DINODE(fe)) {
1794 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1795 status = -EIO;
1796 goto bail;
1797 }
1798 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1799
Mark Fashehb06970532006-03-03 10:24:33 -08001800 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1801 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1802 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08001803
1804 status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1805 alloc_inode);
1806 if (status < 0) {
1807 mlog_errno(status);
1808 goto bail;
1809 }
1810
1811 group = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001812 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
1813 if (status) {
1814 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001815 goto bail;
1816 }
1817 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1818
1819 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1820 group, group_bh,
1821 start_bit, count);
1822 if (status < 0) {
1823 mlog_errno(status);
1824 goto bail;
1825 }
1826
1827 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1828 OCFS2_JOURNAL_ACCESS_WRITE);
1829 if (status < 0) {
1830 mlog_errno(status);
1831 goto bail;
1832 }
1833
1834 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1835 count);
1836 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1837 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1838
1839 status = ocfs2_journal_dirty(handle, alloc_bh);
1840 if (status < 0) {
1841 mlog_errno(status);
1842 goto bail;
1843 }
1844
1845bail:
1846 if (group_bh)
1847 brelse(group_bh);
1848
1849 mlog_exit(status);
1850 return status;
1851}
1852
Mark Fasheh1fabe142006-10-09 18:11:45 -07001853int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001854 struct inode *inode_alloc_inode,
1855 struct buffer_head *inode_alloc_bh,
1856 struct ocfs2_dinode *di)
1857{
1858 u64 blk = le64_to_cpu(di->i_blkno);
1859 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1860 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1861
1862 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1863 inode_alloc_bh, bit, bg_blkno, 1);
1864}
1865
Mark Fasheh1fabe142006-10-09 18:11:45 -07001866int ocfs2_free_clusters(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001867 struct inode *bitmap_inode,
1868 struct buffer_head *bitmap_bh,
1869 u64 start_blk,
1870 unsigned int num_clusters)
1871{
1872 int status;
1873 u16 bg_start_bit;
1874 u64 bg_blkno;
1875 struct ocfs2_dinode *fe;
1876
1877 /* You can't ever have a contiguous set of clusters
1878 * bigger than a block group bitmap so we never have to worry
1879 * about looping on them. */
1880
1881 mlog_entry_void();
1882
1883 /* This is expensive. We can safely remove once this stuff has
1884 * gotten tested really well. */
1885 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1886
1887 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1888
1889 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1890 &bg_start_bit);
1891
Mark Fashehb06970532006-03-03 10:24:33 -08001892 mlog(0, "want to free %u clusters starting at block %llu\n",
1893 num_clusters, (unsigned long long)start_blk);
1894 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1895 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08001896
1897 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1898 bg_start_bit, bg_blkno,
1899 num_clusters);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001900 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001901 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07001902 goto out;
1903 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001904
Mark Fasheh9c7af402008-07-28 18:02:53 -07001905 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
1906 num_clusters);
1907
1908out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001909 mlog_exit(status);
1910 return status;
1911}
1912
1913static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1914{
1915 printk("Block Group:\n");
1916 printk("bg_signature: %s\n", bg->bg_signature);
1917 printk("bg_size: %u\n", bg->bg_size);
1918 printk("bg_bits: %u\n", bg->bg_bits);
1919 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1920 printk("bg_chain: %u\n", bg->bg_chain);
1921 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08001922 printk("bg_next_group: %llu\n",
1923 (unsigned long long)bg->bg_next_group);
1924 printk("bg_parent_dinode: %llu\n",
1925 (unsigned long long)bg->bg_parent_dinode);
1926 printk("bg_blkno: %llu\n",
1927 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001928}
1929
1930static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1931{
1932 int i;
1933
Mark Fashehb06970532006-03-03 10:24:33 -08001934 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001935 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08001936 printk("i_size: %llu\n",
1937 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001938 printk("i_clusters: %u\n", fe->i_clusters);
1939 printk("i_generation: %u\n",
1940 le32_to_cpu(fe->i_generation));
1941 printk("id1.bitmap1.i_used: %u\n",
1942 le32_to_cpu(fe->id1.bitmap1.i_used));
1943 printk("id1.bitmap1.i_total: %u\n",
1944 le32_to_cpu(fe->id1.bitmap1.i_total));
1945 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
1946 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
1947 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
1948 printk("id2.i_chain.cl_next_free_rec: %u\n",
1949 fe->id2.i_chain.cl_next_free_rec);
1950 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1951 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
1952 fe->id2.i_chain.cl_recs[i].c_free);
1953 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1954 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08001955 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1956 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001957 }
1958}
Tao Mae7d4cb62008-08-18 17:38:44 +08001959
1960/*
1961 * For a given allocation, determine which allocators will need to be
1962 * accessed, and lock them, reserving the appropriate number of bits.
1963 *
1964 * Sparse file systems call this from ocfs2_write_begin_nolock()
1965 * and ocfs2_allocate_unwritten_extents().
1966 *
1967 * File systems which don't support holes call this from
1968 * ocfs2_extend_allocation().
1969 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07001970int ocfs2_lock_allocators(struct inode *inode,
1971 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08001972 u32 clusters_to_add, u32 extents_to_split,
1973 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07001974 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08001975{
1976 int ret = 0, num_free_extents;
1977 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
1978 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1979
1980 *meta_ac = NULL;
1981 if (data_ac)
1982 *data_ac = NULL;
1983
1984 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
1985
Joel Beckerf99b9b72008-08-20 19:36:33 -07001986 num_free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08001987 if (num_free_extents < 0) {
1988 ret = num_free_extents;
1989 mlog_errno(ret);
1990 goto out;
1991 }
1992
1993 /*
1994 * Sparse allocation file systems need to be more conservative
1995 * with reserving room for expansion - the actual allocation
1996 * happens while we've got a journal handle open so re-taking
1997 * a cluster lock (because we ran out of room for another
1998 * extent) will violate ordering rules.
1999 *
2000 * Most of the time we'll only be seeing this 1 cluster at a time
2001 * anyway.
2002 *
2003 * Always lock for any unwritten extents - we might want to
2004 * add blocks during a split.
2005 */
2006 if (!num_free_extents ||
2007 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002008 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002009 if (ret < 0) {
2010 if (ret != -ENOSPC)
2011 mlog_errno(ret);
2012 goto out;
2013 }
2014 }
2015
2016 if (clusters_to_add == 0)
2017 goto out;
2018
2019 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2020 if (ret < 0) {
2021 if (ret != -ENOSPC)
2022 mlog_errno(ret);
2023 goto out;
2024 }
2025
2026out:
2027 if (ret) {
2028 if (*meta_ac) {
2029 ocfs2_free_alloc_context(*meta_ac);
2030 *meta_ac = NULL;
2031 }
2032
2033 /*
2034 * We cannot have an error and a non null *data_ac.
2035 */
2036 }
2037
2038 return ret;
2039}