blob: 2f753954a7a89d89eed10aecf4b5bbeb167983ac [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"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070038#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#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
Tao Maffda89a2008-03-03 17:12:09 +080050#define NOT_ALLOC_NEW_GROUP 0
Tao Ma60ca81e2009-02-25 00:53:24 +080051#define ALLOC_NEW_GROUP 0x1
52#define ALLOC_GROUPS_FROM_GLOBAL 0x2
Tao Maffda89a2008-03-03 17:12:09 +080053
Tiger Yangb89c5422010-01-25 14:11:06 +080054#define OCFS2_MAX_TO_STEAL 1024
Tao Ma4d0ddb22008-03-05 16:11:46 +080055
Mark Fashehccd979b2005-12-15 14:31:24 -080056static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
57static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
58static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070059static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080060 struct inode *alloc_inode,
61 struct buffer_head *bg_bh,
62 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080063 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080064 u16 my_chain,
65 struct ocfs2_chain_list *cl);
66static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
67 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070068 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080069 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080070 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080071 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080072
Mark Fashehccd979b2005-12-15 14:31:24 -080073static int ocfs2_cluster_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);
78static int ocfs2_block_group_search(struct inode *inode,
79 struct buffer_head *group_bh,
80 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070081 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080082 u16 *bit_off, u16 *bits_found);
Mark Fashehccd979b2005-12-15 14:31:24 -080083static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
84 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070085 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080086 u32 bits_wanted,
87 u32 min_bits,
88 u16 *bit_off,
89 unsigned int *num_bits,
90 u64 *bg_blkno);
91static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
92 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070093static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080094 struct inode *alloc_inode,
95 struct ocfs2_group_desc *bg,
96 struct buffer_head *group_bh,
97 unsigned int bit_off,
98 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -070099static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800100 struct inode *alloc_inode,
101 struct buffer_head *fe_bh,
102 struct buffer_head *bg_bh,
103 struct buffer_head *prev_bg_bh,
104 u16 chain);
105static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
106 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800107static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
108 u64 bg_blkno,
109 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800110static inline void ocfs2_block_to_cluster_group(struct inode *inode,
111 u64 data_blkno,
112 u64 *bg_blkno,
113 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700114static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
115 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800116 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700117 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800118
Mark Fasheh9c7af402008-07-28 18:02:53 -0700119void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800120{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700121 struct inode *inode = ac->ac_inode;
122
123 if (inode) {
124 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700125 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700126
127 mutex_unlock(&inode->i_mutex);
128
129 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800130 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700131 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700132 brelse(ac->ac_bh);
133 ac->ac_bh = NULL;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800134 ac->ac_resv = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800135}
136
137void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
138{
139 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800140 kfree(ac);
141}
142
143static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
144{
145 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
146}
147
Joel Becker57e3e792008-11-13 14:49:13 -0800148#define do_error(fmt, ...) \
149 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800150 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800151 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
152 else \
153 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
154 } while (0)
155
Joel Becker970e4932008-11-13 14:49:19 -0800156static int ocfs2_validate_gd_self(struct super_block *sb,
157 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800158 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800159{
160 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
161
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700162 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800163 do_error("Group descriptor #%llu has bad signature %.*s",
164 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800165 gd->bg_signature);
166 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700167 }
168
Joel Becker68f64d42008-11-13 14:49:14 -0800169 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
170 do_error("Group descriptor #%llu has an invalid bg_blkno "
171 "of %llu",
172 (unsigned long long)bh->b_blocknr,
173 (unsigned long long)le64_to_cpu(gd->bg_blkno));
174 return -EINVAL;
175 }
176
177 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
178 do_error("Group descriptor #%llu has an invalid "
179 "fs_generation of #%u",
180 (unsigned long long)bh->b_blocknr,
181 le32_to_cpu(gd->bg_generation));
182 return -EINVAL;
183 }
184
Joel Becker970e4932008-11-13 14:49:19 -0800185 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
186 do_error("Group descriptor #%llu has bit count %u but "
187 "claims that %u are free",
188 (unsigned long long)bh->b_blocknr,
189 le16_to_cpu(gd->bg_bits),
190 le16_to_cpu(gd->bg_free_bits_count));
191 return -EINVAL;
192 }
193
194 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
195 do_error("Group descriptor #%llu has bit count %u but "
196 "max bitmap bits of %u",
197 (unsigned long long)bh->b_blocknr,
198 le16_to_cpu(gd->bg_bits),
199 8 * le16_to_cpu(gd->bg_size));
200 return -EINVAL;
201 }
202
203 return 0;
204}
205
206static int ocfs2_validate_gd_parent(struct super_block *sb,
207 struct ocfs2_dinode *di,
208 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800209 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800210{
211 unsigned int max_bits;
212 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
213
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700214 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800215 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800216 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800217 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800218 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
219 (unsigned long long)le64_to_cpu(di->i_blkno));
220 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700221 }
222
223 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
224 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800225 do_error("Group descriptor #%llu has bit count of %u",
226 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800227 le16_to_cpu(gd->bg_bits));
228 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700229 }
230
Tao Ma78c37eb2010-03-03 11:26:27 +0800231 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
232 if ((le16_to_cpu(gd->bg_chain) >
233 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
234 ((le16_to_cpu(gd->bg_chain) ==
235 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800236 do_error("Group descriptor #%llu has bad chain %u",
237 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800238 le16_to_cpu(gd->bg_chain));
239 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700240 }
241
Joel Becker970e4932008-11-13 14:49:19 -0800242 return 0;
243}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700244
Joel Becker57e3e792008-11-13 14:49:13 -0800245#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700246
Joel Becker970e4932008-11-13 14:49:19 -0800247/*
248 * This version only prints errors. It does not fail the filesystem, and
249 * exists only for resize.
250 */
251int ocfs2_check_group_descriptor(struct super_block *sb,
252 struct ocfs2_dinode *di,
253 struct buffer_head *bh)
254{
255 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700256 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800257
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700258 BUG_ON(!buffer_uptodate(bh));
259
260 /*
261 * If the ecc fails, we return the error but otherwise
262 * leave the filesystem running. We know any error is
263 * local to this block.
264 */
265 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700266 if (rc) {
267 mlog(ML_ERROR,
268 "Checksum failed for group descriptor %llu\n",
269 (unsigned long long)bh->b_blocknr);
270 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700271 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800272 if (!rc)
273 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
274
275 return rc;
276}
277
278static int ocfs2_validate_group_descriptor(struct super_block *sb,
279 struct buffer_head *bh)
280{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700281 int rc;
282 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
283
Joel Becker970e4932008-11-13 14:49:19 -0800284 mlog(0, "Validating group descriptor %llu\n",
285 (unsigned long long)bh->b_blocknr);
286
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700287 BUG_ON(!buffer_uptodate(bh));
288
289 /*
290 * If the ecc fails, we return the error but otherwise
291 * leave the filesystem running. We know any error is
292 * local to this block.
293 */
294 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
295 if (rc)
296 return rc;
297
298 /*
299 * Errors after here are fatal.
300 */
301
Joel Becker970e4932008-11-13 14:49:19 -0800302 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700303}
304
Joel Becker68f64d42008-11-13 14:49:14 -0800305int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
306 u64 gd_blkno, struct buffer_head **bh)
307{
308 int rc;
309 struct buffer_head *tmp = *bh;
310
Joel Becker8cb471e2009-02-10 20:00:41 -0800311 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800312 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800313 if (rc)
314 goto out;
315
Joel Becker970e4932008-11-13 14:49:19 -0800316 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800317 if (rc) {
318 brelse(tmp);
319 goto out;
320 }
321
322 /* If ocfs2_read_block() got us a new bh, pass it up. */
323 if (!*bh)
324 *bh = tmp;
325
326out:
327 return rc;
328}
329
Joel Becker798db352010-04-13 14:26:32 +0800330static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
331 struct ocfs2_group_desc *bg,
332 struct ocfs2_chain_list *cl,
333 u64 p_blkno, u32 clusters)
334{
335 struct ocfs2_extent_list *el = &bg->bg_list;
336 struct ocfs2_extent_rec *rec;
337
338 BUG_ON(!ocfs2_supports_discontig_bh(osb));
339 if (!el->l_next_free_rec)
340 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
341 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
342 rec->e_blkno = p_blkno;
343 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
344 le16_to_cpu(cl->cl_bpc));
345 rec->e_leaf_clusters = cpu_to_le32(clusters);
346 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
347 le16_add_cpu(&el->l_next_free_rec, 1);
348}
349
Mark Fasheh1fabe142006-10-09 18:11:45 -0700350static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800351 struct inode *alloc_inode,
352 struct buffer_head *bg_bh,
353 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800354 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800355 u16 my_chain,
356 struct ocfs2_chain_list *cl)
357{
358 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800359 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800360 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
361 struct super_block * sb = alloc_inode->i_sb;
362
363 mlog_entry_void();
364
365 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800366 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
367 "b_blocknr (%llu)",
368 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800369 (unsigned long long) bg_bh->b_blocknr);
370 status = -EIO;
371 goto bail;
372 }
373
Joel Becker13723d02008-10-17 19:25:01 -0700374 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800375 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700376 bg_bh,
377 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800378 if (status < 0) {
379 mlog_errno(status);
380 goto bail;
381 }
382
383 memset(bg, 0, sb->s_blocksize);
384 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
385 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
Joel Becker4cbe4242010-04-13 14:26:12 +0800386 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -0800387 bg->bg_chain = cpu_to_le16(my_chain);
388 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
389 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
390 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800391 if (group_clusters == le16_to_cpu(cl->cl_cpg))
392 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
393 else
394 ocfs2_bg_discontig_add_extent(osb, bg, cl, bg->bg_blkno,
395 group_clusters);
396
Mark Fashehccd979b2005-12-15 14:31:24 -0800397 /* set the 1st bit in the bitmap to account for the descriptor block */
398 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
399 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
400
Joel Beckerec20cec2010-03-19 14:13:52 -0700401 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800402
403 /* There is no need to zero out or otherwise initialize the
404 * other blocks in a group - All valid FS metadata in a block
405 * group stores the superblock fs_generation value at
406 * allocation time. */
407
408bail:
409 mlog_exit(status);
410 return status;
411}
412
413static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
414{
415 u16 curr, best;
416
417 best = curr = 0;
418 while (curr < le16_to_cpu(cl->cl_count)) {
419 if (le32_to_cpu(cl->cl_recs[best].c_total) >
420 le32_to_cpu(cl->cl_recs[curr].c_total))
421 best = curr;
422 curr++;
423 }
424 return best;
425}
426
Joel Becker798db352010-04-13 14:26:32 +0800427static struct buffer_head *
428ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
429 struct inode *alloc_inode,
430 struct ocfs2_alloc_context *ac,
431 struct ocfs2_chain_list *cl)
432{
433 int status;
434 u32 bit_off, num_bits;
435 u64 bg_blkno;
436 struct buffer_head *bg_bh;
437 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
438
439 status = ocfs2_claim_clusters(osb, handle, ac,
440 le16_to_cpu(cl->cl_cpg), &bit_off,
441 &num_bits);
442 if (status < 0) {
443 if (status != -ENOSPC)
444 mlog_errno(status);
445 goto bail;
446 }
447
448 /* setup the group */
449 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
450 mlog(0, "new descriptor, record %u, at block %llu\n",
451 alloc_rec, (unsigned long long)bg_blkno);
452
453 bg_bh = sb_getblk(osb->sb, bg_blkno);
454 if (!bg_bh) {
455 status = -EIO;
456 mlog_errno(status);
457 goto bail;
458 }
459 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
460
461 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
462 bg_blkno, num_bits, alloc_rec, cl);
463 if (status < 0) {
464 brelse(bg_bh);
465 mlog_errno(status);
466 }
467
468bail:
469 return status ? ERR_PTR(status) : bg_bh;
470}
471
472static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
473 handle_t *handle,
474 struct ocfs2_alloc_context *ac,
475 unsigned int min_bits,
476 u32 *bit_off, u32 *num_bits)
477{
478 int status;
479
480 while (min_bits) {
481 status = ocfs2_claim_clusters(osb, handle, ac, min_bits,
482 bit_off, num_bits);
483 if (status != -ENOSPC)
484 break;
485
486 min_bits >>= 1;
487 }
488
489 return status;
490}
491
492static int ocfs2_block_group_grow_discontig(handle_t *handle,
493 struct inode *alloc_inode,
494 struct buffer_head *bg_bh,
495 struct ocfs2_alloc_context *ac,
496 struct ocfs2_chain_list *cl,
497 unsigned int min_bits)
498{
499 int status;
500 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
501 struct ocfs2_group_desc *bg =
502 (struct ocfs2_group_desc *)bg_bh->b_data;
503 unsigned int needed =
504 ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
505 u32 p_cpos, clusters;
506 u64 p_blkno;
507 struct ocfs2_extent_list *el = &bg->bg_list;
508
509 status = ocfs2_journal_access_gd(handle,
510 INODE_CACHE(alloc_inode),
511 bg_bh,
512 OCFS2_JOURNAL_ACCESS_CREATE);
513 if (status < 0) {
514 mlog_errno(status);
515 goto bail;
516 }
517
518 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
519 le16_to_cpu(el->l_count))) {
520 status = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_ALLOC);
521 if (status) {
522 mlog_errno(status);
523 goto bail;
524 }
525
526 if (min_bits > needed)
527 min_bits = needed;
528 status = ocfs2_block_group_claim_bits(osb, handle, ac,
529 min_bits, &p_cpos,
530 &clusters);
531 if (status < 0) {
532 if (status != -ENOSPC)
533 mlog_errno(status);
534 goto bail;
535 }
536 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
537 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
538 clusters);
539
540 min_bits = clusters;
541 needed = ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
542 }
543
544 if (needed > 0) {
545 }
546
547 ocfs2_journal_dirty(handle, bg_bh);
548
549bail:
550 return status;
551}
552
553static void ocfs2_bg_alloc_cleanup(struct inode *alloc_inode,
554 struct buffer_head *bg_bh,
555 struct ocfs2_cached_dealloc_ctxt *dealloc)
556{
557 int i;
558 struct ocfs2_group_desc *bg;
559 struct ocfs2_extent_list *el;
560 struct ocfs2_extent_rec *rec;
561
562 if (!bg_bh)
563 return;
564
565 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
566 el = &bg->bg_list;
567 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
568 rec = &el->l_recs[i];
569 ocfs2_cache_cluster_dealloc(dealloc,
570 le64_to_cpu(rec->e_blkno),
571 le32_to_cpu(rec->e_leaf_clusters));
572 }
573
574 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
575 brelse(bg_bh);
576}
577
578static struct buffer_head *
579ocfs2_block_group_alloc_discontig(handle_t *handle,
580 struct inode *alloc_inode,
581 struct ocfs2_alloc_context *ac,
582 struct ocfs2_chain_list *cl,
583 struct ocfs2_cached_dealloc_ctxt *dealloc)
584{
585 int status;
586 u32 bit_off, num_bits;
587 u64 bg_blkno;
588 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
589 struct buffer_head *bg_bh = NULL;
590 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
591 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
592
593 if (!ocfs2_supports_discontig_bh(osb)) {
594 status = -ENOSPC;
595 goto bail;
596 }
597
598 /* Claim the first region */
599 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
600 &bit_off, &num_bits);
601 if (status < 0) {
602 if (status != -ENOSPC)
603 mlog_errno(status);
604 goto bail;
605 }
606 min_bits = num_bits;
607
608 /* setup the group */
609 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
610 mlog(0, "new descriptor, record %u, at block %llu\n",
611 alloc_rec, (unsigned long long)bg_blkno);
612
613 bg_bh = sb_getblk(osb->sb, bg_blkno);
614 if (!bg_bh) {
615 status = -EIO;
616 mlog_errno(status);
617 goto bail;
618 }
619 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
620
621 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
622 bg_blkno, num_bits, alloc_rec, cl);
623 if (status < 0) {
624 mlog_errno(status);
625 goto bail;
626 }
627
628 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
629 bg_bh, ac, cl, min_bits);
630 if (status)
631 mlog_errno(status);
632
633bail:
634 if (status)
635 ocfs2_bg_alloc_cleanup(alloc_inode, bg_bh, dealloc);
636 return status ? ERR_PTR(status) : bg_bh;
637}
638
Mark Fashehccd979b2005-12-15 14:31:24 -0800639/*
640 * We expect the block group allocator to already be locked.
641 */
642static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
643 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700644 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800645 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800646 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800647 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800648{
649 int status, credits;
650 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
651 struct ocfs2_chain_list *cl;
652 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700653 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800654 u64 bg_blkno;
655 struct buffer_head *bg_bh = NULL;
656 struct ocfs2_group_desc *bg;
Joel Becker798db352010-04-13 14:26:32 +0800657 struct ocfs2_cached_dealloc_ctxt dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -0800658
659 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
660
661 mlog_entry_void();
662
Joel Becker798db352010-04-13 14:26:32 +0800663 ocfs2_init_dealloc_ctxt(&dealloc);
664
Mark Fashehccd979b2005-12-15 14:31:24 -0800665 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700666 status = ocfs2_reserve_clusters_with_limit(osb,
667 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800668 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800669 if (status < 0) {
670 if (status != -ENOSPC)
671 mlog_errno(status);
672 goto bail;
673 }
674
675 credits = ocfs2_calc_group_alloc_credits(osb->sb,
676 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700677 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800678 if (IS_ERR(handle)) {
679 status = PTR_ERR(handle);
680 handle = NULL;
681 mlog_errno(status);
682 goto bail;
683 }
684
Tao Mafeb473a2009-02-25 00:53:25 +0800685 if (last_alloc_group && *last_alloc_group != 0) {
686 mlog(0, "use old allocation group %llu for block group alloc\n",
687 (unsigned long long)*last_alloc_group);
688 ac->ac_last_group = *last_alloc_group;
689 }
Joel Becker798db352010-04-13 14:26:32 +0800690
691 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
692 ac, cl);
693 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
694 bg_bh = ocfs2_block_group_alloc_discontig(handle,
695 alloc_inode,
696 ac, cl,
697 &dealloc);
698 if (IS_ERR(bg_bh)) {
699 status = PTR_ERR(bg_bh);
700 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800701 if (status != -ENOSPC)
702 mlog_errno(status);
703 goto bail;
704 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800705 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
706
Joel Becker0cf2f762009-02-12 16:41:25 -0800707 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700708 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800709 if (status < 0) {
710 mlog_errno(status);
711 goto bail;
712 }
713
Joel Becker798db352010-04-13 14:26:32 +0800714 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800715 le16_to_cpu(bg->bg_free_bits_count));
Joel Becker798db352010-04-13 14:26:32 +0800716 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_total,
717 le16_to_cpu(bg->bg_bits));
718 cl->cl_recs[bg->bg_chain].c_blkno = cpu_to_le64(bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800719 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
720 le16_add_cpu(&cl->cl_next_free_rec, 1);
721
722 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
723 le16_to_cpu(bg->bg_free_bits_count));
724 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
725 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
726
Joel Beckerec20cec2010-03-19 14:13:52 -0700727 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800728
729 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
730 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
731 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
732 le32_to_cpu(fe->i_clusters)));
733 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
734 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700735 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800736
737 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800738
739 /* save the new last alloc group so that the caller can cache it. */
740 if (last_alloc_group)
741 *last_alloc_group = ac->ac_last_group;
742
Mark Fashehccd979b2005-12-15 14:31:24 -0800743bail:
744 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700745 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800746
Joel Becker798db352010-04-13 14:26:32 +0800747 if (ocfs2_dealloc_has_cluster(&dealloc)) {
748 ocfs2_schedule_truncate_log_flush(osb, 1);
749 ocfs2_run_deallocs(osb, &dealloc);
750 }
751
Mark Fashehccd979b2005-12-15 14:31:24 -0800752 if (ac)
753 ocfs2_free_alloc_context(ac);
754
Mark Fasheha81cb882008-10-07 14:25:16 -0700755 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800756
757 mlog_exit(status);
758 return status;
759}
760
761static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700762 struct ocfs2_alloc_context *ac,
763 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800764 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800765 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800766 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800767{
768 int status;
769 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700770 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800771 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800772 struct ocfs2_dinode *fe;
773 u32 free_bits;
774
775 mlog_entry_void();
776
Mark Fashehda5cbf22006-10-06 18:34:35 -0700777 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
778 if (!alloc_inode) {
779 mlog_errno(-EINVAL);
780 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800781 }
782
Mark Fashehda5cbf22006-10-06 18:34:35 -0700783 mutex_lock(&alloc_inode->i_mutex);
784
Mark Fashehe63aecb62007-10-18 15:30:42 -0700785 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700786 if (status < 0) {
787 mutex_unlock(&alloc_inode->i_mutex);
788 iput(alloc_inode);
789
790 mlog_errno(status);
791 return status;
792 }
793
794 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800795 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700796
Mark Fashehccd979b2005-12-15 14:31:24 -0800797 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800798
799 /* The bh was validated by the inode read inside
800 * ocfs2_inode_lock(). Any corruption is a code bug. */
801 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
802
Mark Fashehccd979b2005-12-15 14:31:24 -0800803 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800804 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
805 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 status = -EIO;
807 goto bail;
808 }
809
810 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
811 le32_to_cpu(fe->id1.bitmap1.i_used);
812
813 if (bits_wanted > free_bits) {
814 /* cluster bitmap never grows */
815 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
816 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
817 bits_wanted, free_bits);
818 status = -ENOSPC;
819 goto bail;
820 }
821
Tao Ma60ca81e2009-02-25 00:53:24 +0800822 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800823 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
824 "and we don't alloc a new group for it.\n",
825 slot, bits_wanted, free_bits);
826 status = -ENOSPC;
827 goto bail;
828 }
829
Joel Becker1187c962008-09-03 20:03:39 -0700830 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800831 ac->ac_max_block,
832 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800833 if (status < 0) {
834 if (status != -ENOSPC)
835 mlog_errno(status);
836 goto bail;
837 }
838 atomic_inc(&osb->alloc_stats.bg_extends);
839
840 /* You should never ask for this much metadata */
841 BUG_ON(bits_wanted >
842 (le32_to_cpu(fe->id1.bitmap1.i_total)
843 - le32_to_cpu(fe->id1.bitmap1.i_used)));
844 }
845
846 get_bh(bh);
847 ac->ac_bh = bh;
848bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700849 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800850
851 mlog_exit(status);
852 return status;
853}
854
Tiger Yangb89c5422010-01-25 14:11:06 +0800855static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
856{
857 spin_lock(&osb->osb_lock);
858 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
859 spin_unlock(&osb->osb_lock);
860 atomic_set(&osb->s_num_inodes_stolen, 0);
861}
862
863static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
864{
865 spin_lock(&osb->osb_lock);
866 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
867 spin_unlock(&osb->osb_lock);
868 atomic_set(&osb->s_num_meta_stolen, 0);
869}
870
871void ocfs2_init_steal_slots(struct ocfs2_super *osb)
872{
873 ocfs2_init_inode_steal_slot(osb);
874 ocfs2_init_meta_steal_slot(osb);
875}
876
877static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
878{
879 spin_lock(&osb->osb_lock);
880 if (type == INODE_ALLOC_SYSTEM_INODE)
881 osb->s_inode_steal_slot = slot;
882 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
883 osb->s_meta_steal_slot = slot;
884 spin_unlock(&osb->osb_lock);
885}
886
887static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
888{
889 int slot = OCFS2_INVALID_SLOT;
890
891 spin_lock(&osb->osb_lock);
892 if (type == INODE_ALLOC_SYSTEM_INODE)
893 slot = osb->s_inode_steal_slot;
894 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
895 slot = osb->s_meta_steal_slot;
896 spin_unlock(&osb->osb_lock);
897
898 return slot;
899}
900
901static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
902{
903 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
904}
905
906static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
907{
908 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
909}
910
911static int ocfs2_steal_resource(struct ocfs2_super *osb,
912 struct ocfs2_alloc_context *ac,
913 int type)
914{
915 int i, status = -ENOSPC;
916 int slot = __ocfs2_get_steal_slot(osb, type);
917
918 /* Start to steal resource from the first slot after ours. */
919 if (slot == OCFS2_INVALID_SLOT)
920 slot = osb->slot_num + 1;
921
922 for (i = 0; i < osb->max_slots; i++, slot++) {
923 if (slot == osb->max_slots)
924 slot = 0;
925
926 if (slot == osb->slot_num)
927 continue;
928
929 status = ocfs2_reserve_suballoc_bits(osb, ac,
930 type,
931 (u32)slot, NULL,
932 NOT_ALLOC_NEW_GROUP);
933 if (status >= 0) {
934 __ocfs2_set_steal_slot(osb, slot, type);
935 break;
936 }
937
938 ocfs2_free_ac_resource(ac);
939 }
940
941 return status;
942}
943
944static int ocfs2_steal_inode(struct ocfs2_super *osb,
945 struct ocfs2_alloc_context *ac)
946{
947 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
948}
949
950static int ocfs2_steal_meta(struct ocfs2_super *osb,
951 struct ocfs2_alloc_context *ac)
952{
953 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
954}
955
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800956int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
957 int blocks,
958 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800959{
960 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800961 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800962
Robert P. J. Daycd861282006-12-13 00:34:52 -0800963 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 if (!(*ac)) {
965 status = -ENOMEM;
966 mlog_errno(status);
967 goto bail;
968 }
969
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800970 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800971 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800972 (*ac)->ac_group_search = ocfs2_block_group_search;
973
Tiger Yangb89c5422010-01-25 14:11:06 +0800974 if (slot != OCFS2_INVALID_SLOT &&
975 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
976 goto extent_steal;
977
978 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700979 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800980 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800981 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -0800982 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +0800983
984
985 if (status >= 0) {
986 status = 0;
987 if (slot != OCFS2_INVALID_SLOT)
988 ocfs2_init_meta_steal_slot(osb);
989 goto bail;
990 } else if (status < 0 && status != -ENOSPC) {
991 mlog_errno(status);
992 goto bail;
993 }
994
995 ocfs2_free_ac_resource(*ac);
996
997extent_steal:
998 status = ocfs2_steal_meta(osb, *ac);
999 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001000 if (status < 0) {
1001 if (status != -ENOSPC)
1002 mlog_errno(status);
1003 goto bail;
1004 }
1005
1006 status = 0;
1007bail:
1008 if ((status < 0) && *ac) {
1009 ocfs2_free_alloc_context(*ac);
1010 *ac = NULL;
1011 }
1012
Mark Fashehccd979b2005-12-15 14:31:24 -08001013 mlog_exit(status);
1014 return status;
1015}
1016
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001017int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1018 struct ocfs2_extent_list *root_el,
1019 struct ocfs2_alloc_context **ac)
1020{
1021 return ocfs2_reserve_new_metadata_blocks(osb,
1022 ocfs2_extend_meta_needed(root_el),
1023 ac);
1024}
1025
Mark Fashehccd979b2005-12-15 14:31:24 -08001026int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001027 struct ocfs2_alloc_context **ac)
1028{
1029 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001030 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001031 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001032
Robert P. J. Daycd861282006-12-13 00:34:52 -08001033 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001034 if (!(*ac)) {
1035 status = -ENOMEM;
1036 mlog_errno(status);
1037 goto bail;
1038 }
1039
1040 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1042
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 (*ac)->ac_group_search = ocfs2_block_group_search;
1044
Tao Ma4d0ddb22008-03-05 16:11:46 +08001045 /*
Joel Becker1187c962008-09-03 20:03:39 -07001046 * stat(2) can't handle i_ino > 32bits, so we tell the
1047 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001048 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001049 */
Joel Becker12462f12008-09-03 20:03:40 -07001050 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1051 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001052
1053 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001054 * slot is set when we successfully steal inode from other nodes.
1055 * It is reset in 3 places:
1056 * 1. when we flush the truncate log
1057 * 2. when we complete local alloc recovery.
1058 * 3. when we successfully allocate from our own slot.
1059 * After it is set, we will go on stealing inodes until we find the
1060 * need to check our slots to see whether there is some space for us.
1061 */
1062 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001063 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001064 goto inode_steal;
1065
1066 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001067 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001068 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1069 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001070 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001071 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001072 ALLOC_NEW_GROUP |
1073 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001074 if (status >= 0) {
1075 status = 0;
1076
Tao Mafeb473a2009-02-25 00:53:25 +08001077 spin_lock(&osb->osb_lock);
1078 osb->osb_inode_alloc_group = alloc_group;
1079 spin_unlock(&osb->osb_lock);
1080 mlog(0, "after reservation, new allocation group is "
1081 "%llu\n", (unsigned long long)alloc_group);
1082
Tao Ma4d0ddb22008-03-05 16:11:46 +08001083 /*
1084 * Some inodes must be freed by us, so try to allocate
1085 * from our own next time.
1086 */
1087 if (slot != OCFS2_INVALID_SLOT)
1088 ocfs2_init_inode_steal_slot(osb);
1089 goto bail;
1090 } else if (status < 0 && status != -ENOSPC) {
1091 mlog_errno(status);
1092 goto bail;
1093 }
1094
1095 ocfs2_free_ac_resource(*ac);
1096
1097inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001098 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001099 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001100 if (status < 0) {
1101 if (status != -ENOSPC)
1102 mlog_errno(status);
1103 goto bail;
1104 }
1105
1106 status = 0;
1107bail:
1108 if ((status < 0) && *ac) {
1109 ocfs2_free_alloc_context(*ac);
1110 *ac = NULL;
1111 }
1112
Mark Fashehccd979b2005-12-15 14:31:24 -08001113 mlog_exit(status);
1114 return status;
1115}
1116
1117/* local alloc code has to do the same thing, so rather than do this
1118 * twice.. */
1119int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1120 struct ocfs2_alloc_context *ac)
1121{
1122 int status;
1123
Mark Fashehccd979b2005-12-15 14:31:24 -08001124 ac->ac_which = OCFS2_AC_USE_MAIN;
1125 ac->ac_group_search = ocfs2_cluster_group_search;
1126
Mark Fashehda5cbf22006-10-06 18:34:35 -07001127 status = ocfs2_reserve_suballoc_bits(osb, ac,
1128 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001129 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001130 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001131 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001133 goto bail;
1134 }
1135
Mark Fashehccd979b2005-12-15 14:31:24 -08001136bail:
1137 return status;
1138}
1139
1140/* Callers don't need to care which bitmap (local alloc or main) to
1141 * use so we figure it out for them, but unfortunately this clutters
1142 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001143static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1144 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001145 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001146 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001147{
1148 int status;
1149
1150 mlog_entry_void();
1151
Robert P. J. Daycd861282006-12-13 00:34:52 -08001152 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001153 if (!(*ac)) {
1154 status = -ENOMEM;
1155 mlog_errno(status);
1156 goto bail;
1157 }
1158
1159 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001160 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001161
1162 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001163 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1164 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001166 bits_wanted,
1167 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001168 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 mlog_errno(status);
1170 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001171 }
1172 }
1173
1174 if (status == -ENOSPC) {
1175 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1176 if (status < 0) {
1177 if (status != -ENOSPC)
1178 mlog_errno(status);
1179 goto bail;
1180 }
1181 }
1182
1183 status = 0;
1184bail:
1185 if ((status < 0) && *ac) {
1186 ocfs2_free_alloc_context(*ac);
1187 *ac = NULL;
1188 }
1189
1190 mlog_exit(status);
1191 return status;
1192}
1193
Joel Becker1187c962008-09-03 20:03:39 -07001194int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1195 u32 bits_wanted,
1196 struct ocfs2_alloc_context **ac)
1197{
Tao Ma60ca81e2009-02-25 00:53:24 +08001198 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1199 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001200}
1201
Mark Fashehccd979b2005-12-15 14:31:24 -08001202/*
1203 * More or less lifted from ext3. I'll leave their description below:
1204 *
1205 * "For ext3 allocations, we must not reuse any blocks which are
1206 * allocated in the bitmap buffer's "last committed data" copy. This
1207 * prevents deletes from freeing up the page for reuse until we have
1208 * committed the delete transaction.
1209 *
1210 * If we didn't do this, then deleting something and reallocating it as
1211 * data would allow the old block to be overwritten before the
1212 * transaction committed (because we force data to disk before commit).
1213 * This would lead to corruption if we crashed between overwriting the
1214 * data and committing the delete.
1215 *
1216 * @@@ We may want to make this allocation behaviour conditional on
1217 * data-writes at some point, and disable it for metadata allocations or
1218 * sync-data inodes."
1219 *
1220 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001221 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001222 * called on a metadata group descriptor.
1223 */
1224static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1225 int nr)
1226{
1227 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001228 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001229
1230 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1231 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001232
1233 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001234 return 1;
1235
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001236 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001237 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001238 if (bg)
1239 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1240 else
1241 ret = 1;
1242 jbd_unlock_bh_state(bg_bh);
1243
1244 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001245}
1246
1247static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1248 struct buffer_head *bg_bh,
1249 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001250 unsigned int total_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001251 u16 *bit_off,
1252 u16 *bits_found)
1253{
1254 void *bitmap;
1255 u16 best_offset, best_size;
1256 int offset, start, found, status = 0;
1257 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1258
Joel Becker42035302008-11-13 14:49:15 -08001259 /* Callers got this descriptor from
1260 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1261 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001262
1263 found = start = best_offset = best_size = 0;
1264 bitmap = bg->bg_bitmap;
1265
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001266 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1267 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 break;
1269
1270 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1271 /* We found a zero, but we can't use it as it
1272 * hasn't been put to disk yet! */
1273 found = 0;
1274 start = offset + 1;
1275 } else if (offset == start) {
1276 /* we found a zero */
1277 found++;
1278 /* move start to the next bit to test */
1279 start++;
1280 } else {
1281 /* got a zero after some ones */
1282 found = 1;
1283 start = offset + 1;
1284 }
1285 if (found > best_size) {
1286 best_size = found;
1287 best_offset = start - found;
1288 }
1289 /* we got everything we needed */
1290 if (found == bits_wanted) {
1291 /* mlog(0, "Found it all!\n"); */
1292 break;
1293 }
1294 }
1295
1296 /* XXX: I think the first clause is equivalent to the second
1297 * - jlbec */
1298 if (found == bits_wanted) {
1299 *bit_off = start - found;
1300 *bits_found = found;
1301 } else if (best_size) {
1302 *bit_off = best_offset;
1303 *bits_found = best_size;
1304 } else {
1305 status = -ENOSPC;
1306 /* No error log here -- see the comment above
1307 * ocfs2_test_bg_bit_allocatable */
1308 }
1309
1310 return status;
1311}
1312
Mark Fasheh1fabe142006-10-09 18:11:45 -07001313static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001314 struct inode *alloc_inode,
1315 struct ocfs2_group_desc *bg,
1316 struct buffer_head *group_bh,
1317 unsigned int bit_off,
1318 unsigned int num_bits)
1319{
1320 int status;
1321 void *bitmap = bg->bg_bitmap;
1322 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1323
1324 mlog_entry_void();
1325
Joel Becker42035302008-11-13 14:49:15 -08001326 /* All callers get the descriptor via
1327 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1328 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001329 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1330
1331 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1332 num_bits);
1333
1334 if (ocfs2_is_cluster_bitmap(alloc_inode))
1335 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1336
Joel Becker13723d02008-10-17 19:25:01 -07001337 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001338 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001339 group_bh,
1340 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001341 if (status < 0) {
1342 mlog_errno(status);
1343 goto bail;
1344 }
1345
1346 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 while(num_bits--)
1348 ocfs2_set_bit(bit_off++, bitmap);
1349
Joel Beckerec20cec2010-03-19 14:13:52 -07001350 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001351
1352bail:
1353 mlog_exit(status);
1354 return status;
1355}
1356
1357/* find the one with the most empty bits */
1358static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1359{
1360 u16 curr, best;
1361
1362 BUG_ON(!cl->cl_next_free_rec);
1363
1364 best = curr = 0;
1365 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1366 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1367 le32_to_cpu(cl->cl_recs[best].c_free))
1368 best = curr;
1369 curr++;
1370 }
1371
1372 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1373 return best;
1374}
1375
Mark Fasheh1fabe142006-10-09 18:11:45 -07001376static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001377 struct inode *alloc_inode,
1378 struct buffer_head *fe_bh,
1379 struct buffer_head *bg_bh,
1380 struct buffer_head *prev_bg_bh,
1381 u16 chain)
1382{
1383 int status;
1384 /* there is a really tiny chance the journal calls could fail,
1385 * but we wouldn't want inconsistent blocks in *any* case. */
1386 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1387 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1388 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1389 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1390
Joel Becker42035302008-11-13 14:49:15 -08001391 /* The caller got these descriptors from
1392 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1393 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1394 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001395
Mark Fashehb06970532006-03-03 10:24:33 -08001396 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001397 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1398 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1399 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001400
1401 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1402 bg_ptr = le64_to_cpu(bg->bg_next_group);
1403 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1404
Joel Becker0cf2f762009-02-12 16:41:25 -08001405 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1406 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001407 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001408 if (status < 0) {
1409 mlog_errno(status);
1410 goto out_rollback;
1411 }
1412
1413 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001414 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001415
Joel Becker0cf2f762009-02-12 16:41:25 -08001416 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1417 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001418 if (status < 0) {
1419 mlog_errno(status);
1420 goto out_rollback;
1421 }
1422
1423 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001424 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001425
Joel Becker0cf2f762009-02-12 16:41:25 -08001426 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1427 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001428 if (status < 0) {
1429 mlog_errno(status);
1430 goto out_rollback;
1431 }
1432
1433 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001434 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001435
Mark Fashehccd979b2005-12-15 14:31:24 -08001436out_rollback:
1437 if (status < 0) {
1438 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1439 bg->bg_next_group = cpu_to_le64(bg_ptr);
1440 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1441 }
Joel Becker42035302008-11-13 14:49:15 -08001442
Mark Fashehccd979b2005-12-15 14:31:24 -08001443 mlog_exit(status);
1444 return status;
1445}
1446
1447static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1448 u32 wanted)
1449{
1450 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1451}
1452
1453/* return 0 on success, -ENOSPC to keep searching and any other < 0
1454 * value on error. */
1455static int ocfs2_cluster_group_search(struct inode *inode,
1456 struct buffer_head *group_bh,
1457 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001458 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001459 u16 *bit_off, u16 *bits_found)
1460{
1461 int search = -ENOSPC;
1462 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001463 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001464 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001465 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001466 u16 tmp_off, tmp_found;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001467 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001468
1469 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1470
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001471 if (gd->bg_free_bits_count) {
1472 max_bits = le16_to_cpu(gd->bg_bits);
1473
1474 /* Tail groups in cluster bitmaps which aren't cpg
1475 * aligned are prone to partial extention by a failed
1476 * fs resize. If the file system resize never got to
1477 * update the dinode cluster count, then we don't want
1478 * to trust any clusters past it, regardless of what
1479 * the group descriptor says. */
1480 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1481 le64_to_cpu(gd->bg_blkno));
1482 if ((gd_cluster_off + max_bits) >
1483 OCFS2_I(inode)->ip_clusters) {
1484 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1485 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1486 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1487 le16_to_cpu(gd->bg_bits),
1488 OCFS2_I(inode)->ip_clusters, max_bits);
1489 }
1490
Mark Fashehccd979b2005-12-15 14:31:24 -08001491 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1492 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001493 max_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001494 &tmp_off, &tmp_found);
1495 if (ret)
1496 return ret;
1497
Joel Becker1187c962008-09-03 20:03:39 -07001498 if (max_block) {
1499 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1500 gd_cluster_off +
1501 tmp_off + tmp_found);
1502 mlog(0, "Checking %llu against %llu\n",
1503 (unsigned long long)blkoff,
1504 (unsigned long long)max_block);
1505 if (blkoff > max_block)
1506 return -ENOSPC;
1507 }
1508
Mark Fashehccd979b2005-12-15 14:31:24 -08001509 /* ocfs2_block_group_find_clear_bits() might
1510 * return success, but we still want to return
1511 * -ENOSPC unless it found the minimum number
1512 * of bits. */
1513 if (min_bits <= tmp_found) {
1514 *bit_off = tmp_off;
1515 *bits_found = tmp_found;
1516 search = 0; /* success */
Mark Fasheh9c7af402008-07-28 18:02:53 -07001517 } else if (tmp_found) {
1518 /*
1519 * Don't show bits which we'll be returning
1520 * for allocation to the local alloc bitmap.
1521 */
1522 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
Mark Fashehccd979b2005-12-15 14:31:24 -08001523 }
1524 }
1525
1526 return search;
1527}
1528
1529static int ocfs2_block_group_search(struct inode *inode,
1530 struct buffer_head *group_bh,
1531 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001532 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 u16 *bit_off, u16 *bits_found)
1534{
1535 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001536 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001537 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1538
1539 BUG_ON(min_bits != 1);
1540 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1541
Joel Becker1187c962008-09-03 20:03:39 -07001542 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001543 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1544 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001545 le16_to_cpu(bg->bg_bits),
Mark Fashehccd979b2005-12-15 14:31:24 -08001546 bit_off, bits_found);
Joel Becker1187c962008-09-03 20:03:39 -07001547 if (!ret && max_block) {
1548 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1549 *bits_found;
1550 mlog(0, "Checking %llu against %llu\n",
1551 (unsigned long long)blkoff,
1552 (unsigned long long)max_block);
1553 if (blkoff > max_block)
1554 ret = -ENOSPC;
1555 }
1556 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001557
1558 return ret;
1559}
1560
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001561static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001562 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001563 struct buffer_head *di_bh,
1564 u32 num_bits,
1565 u16 chain)
1566{
1567 int ret;
1568 u32 tmp_used;
1569 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1570 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1571
Joel Becker0cf2f762009-02-12 16:41:25 -08001572 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001573 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001574 if (ret < 0) {
1575 mlog_errno(ret);
1576 goto out;
1577 }
1578
1579 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1580 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1581 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001582 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001583
1584out:
1585 return ret;
1586}
1587
1588static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001589 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001590 u32 bits_wanted,
1591 u32 min_bits,
1592 u16 *bit_off,
1593 unsigned int *num_bits,
1594 u64 gd_blkno,
1595 u16 *bits_left)
1596{
1597 int ret;
1598 u16 found;
1599 struct buffer_head *group_bh = NULL;
1600 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001601 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001602 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001603
Joel Becker68f64d42008-11-13 14:49:14 -08001604 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1605 &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001606 if (ret < 0) {
1607 mlog_errno(ret);
1608 return ret;
1609 }
1610
1611 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001612 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001613 ac->ac_max_block, bit_off, &found);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001614 if (ret < 0) {
1615 if (ret != -ENOSPC)
1616 mlog_errno(ret);
1617 goto out;
1618 }
1619
1620 *num_bits = found;
1621
1622 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1623 *num_bits,
1624 le16_to_cpu(gd->bg_chain));
1625 if (ret < 0) {
1626 mlog_errno(ret);
1627 goto out;
1628 }
1629
1630 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1631 *bit_off, *num_bits);
1632 if (ret < 0)
1633 mlog_errno(ret);
1634
1635 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1636
1637out:
1638 brelse(group_bh);
1639
1640 return ret;
1641}
1642
Mark Fashehccd979b2005-12-15 14:31:24 -08001643static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001644 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001645 u32 bits_wanted,
1646 u32 min_bits,
1647 u16 *bit_off,
1648 unsigned int *num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001649 u64 *bg_blkno,
1650 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001651{
1652 int status;
1653 u16 chain, tmp_bits;
1654 u32 tmp_used;
1655 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001656 struct inode *alloc_inode = ac->ac_inode;
1657 struct buffer_head *group_bh = NULL;
1658 struct buffer_head *prev_group_bh = NULL;
1659 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1660 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1661 struct ocfs2_group_desc *bg;
1662
1663 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001664 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1665 bits_wanted, chain,
1666 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001667
Joel Becker68f64d42008-11-13 14:49:14 -08001668 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1669 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1670 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001671 if (status < 0) {
1672 mlog_errno(status);
1673 goto bail;
1674 }
1675 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001676
1677 status = -ENOSPC;
1678 /* for now, the chain search is a bit simplistic. We just use
1679 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001680 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1681 bits_wanted, min_bits,
1682 ac->ac_max_block, bit_off,
1683 &tmp_bits)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001684 if (!bg->bg_next_group)
1685 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001686
1687 brelse(prev_group_bh);
1688 prev_group_bh = NULL;
1689
Mark Fashehccd979b2005-12-15 14:31:24 -08001690 next_group = le64_to_cpu(bg->bg_next_group);
1691 prev_group_bh = group_bh;
1692 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001693 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1694 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001695 if (status < 0) {
1696 mlog_errno(status);
1697 goto bail;
1698 }
1699 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001700 }
1701 if (status < 0) {
1702 if (status != -ENOSPC)
1703 mlog_errno(status);
1704 goto bail;
1705 }
1706
Mark Fashehb06970532006-03-03 10:24:33 -08001707 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001708 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001709
1710 *num_bits = tmp_bits;
1711
1712 BUG_ON(*num_bits == 0);
1713
1714 /*
1715 * Keep track of previous block descriptor read. When
1716 * we find a target, if we have read more than X
1717 * number of descriptors, and the target is reasonably
1718 * empty, relink him to top of his chain.
1719 *
1720 * We've read 0 extra blocks and only send one more to
1721 * the transaction, yet the next guy to search has a
1722 * much easier time.
1723 *
1724 * Do this *after* figuring out how many bits we're taking out
1725 * of our target group.
1726 */
1727 if (ac->ac_allow_chain_relink &&
1728 (prev_group_bh) &&
1729 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1730 status = ocfs2_relink_block_group(handle, alloc_inode,
1731 ac->ac_bh, group_bh,
1732 prev_group_bh, chain);
1733 if (status < 0) {
1734 mlog_errno(status);
1735 goto bail;
1736 }
1737 }
1738
1739 /* Ok, claim our bits now: set the info on dinode, chainlist
1740 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001741 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001742 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001743 ac->ac_bh,
1744 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001745 if (status < 0) {
1746 mlog_errno(status);
1747 goto bail;
1748 }
1749
1750 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1751 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1752 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
Joel Beckerec20cec2010-03-19 14:13:52 -07001753 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001754
1755 status = ocfs2_block_group_set_bits(handle,
1756 alloc_inode,
1757 bg,
1758 group_bh,
1759 *bit_off,
1760 *num_bits);
1761 if (status < 0) {
1762 mlog_errno(status);
1763 goto bail;
1764 }
1765
Mark Fashehb06970532006-03-03 10:24:33 -08001766 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001767 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001768
1769 *bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001770 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001771bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001772 brelse(group_bh);
1773 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001774
1775 mlog_exit(status);
1776 return status;
1777}
1778
1779/* will give out up to bits_wanted contiguous bits. */
1780static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1781 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001782 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001783 u32 bits_wanted,
1784 u32 min_bits,
1785 u16 *bit_off,
1786 unsigned int *num_bits,
1787 u64 *bg_blkno)
1788{
1789 int status;
1790 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001791 u16 bits_left = 0;
1792 u64 hint_blkno = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001793 struct ocfs2_chain_list *cl;
1794 struct ocfs2_dinode *fe;
1795
1796 mlog_entry_void();
1797
1798 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1799 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1800 BUG_ON(!ac->ac_bh);
1801
1802 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001803
1804 /* The bh was validated by the inode read during
1805 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1806 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1807
Mark Fashehccd979b2005-12-15 14:31:24 -08001808 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1809 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001810 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1811 "bits but only %u total.",
1812 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001813 le32_to_cpu(fe->id1.bitmap1.i_used),
1814 le32_to_cpu(fe->id1.bitmap1.i_total));
1815 status = -EIO;
1816 goto bail;
1817 }
1818
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001819 if (hint_blkno) {
1820 /* Attempt to short-circuit the usual search mechanism
1821 * by jumping straight to the most recently used
1822 * allocation group. This helps us mantain some
1823 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001824 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1825 min_bits, bit_off, num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001826 hint_blkno, &bits_left);
1827 if (!status) {
1828 /* Be careful to update *bg_blkno here as the
1829 * caller is expecting it to be filled in, and
1830 * ocfs2_search_one_group() won't do that for
1831 * us. */
1832 *bg_blkno = hint_blkno;
1833 goto set_hint;
1834 }
1835 if (status < 0 && status != -ENOSPC) {
1836 mlog_errno(status);
1837 goto bail;
1838 }
1839 }
1840
Mark Fashehccd979b2005-12-15 14:31:24 -08001841 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1842
1843 victim = ocfs2_find_victim_chain(cl);
1844 ac->ac_chain = victim;
1845 ac->ac_allow_chain_relink = 1;
1846
Mark Fashehda5cbf22006-10-06 18:34:35 -07001847 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001848 num_bits, bg_blkno, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001849 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001850 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001851 if (status < 0 && status != -ENOSPC) {
1852 mlog_errno(status);
1853 goto bail;
1854 }
1855
1856 mlog(0, "Search of victim chain %u came up with nothing, "
1857 "trying all chains now.\n", victim);
1858
1859 /* If we didn't pick a good victim, then just default to
1860 * searching each chain in order. Don't allow chain relinking
1861 * because we only calculate enough journal credits for one
1862 * relink per alloc. */
1863 ac->ac_allow_chain_relink = 0;
1864 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1865 if (i == victim)
1866 continue;
1867 if (!cl->cl_recs[i].c_free)
1868 continue;
1869
1870 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001871 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001872 bit_off, num_bits, bg_blkno,
1873 &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001874 if (!status)
1875 break;
1876 if (status < 0 && status != -ENOSPC) {
1877 mlog_errno(status);
1878 goto bail;
1879 }
1880 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001881
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001882set_hint:
1883 if (status != -ENOSPC) {
1884 /* If the next search of this group is not likely to
1885 * yield a suitable extent, then we reset the last
1886 * group hint so as to not waste a disk read */
1887 if (bits_left < min_bits)
1888 ac->ac_last_group = 0;
1889 else
1890 ac->ac_last_group = *bg_blkno;
1891 }
1892
1893bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001894 mlog_exit(status);
1895 return status;
1896}
1897
1898int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001899 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 struct ocfs2_alloc_context *ac,
1901 u32 bits_wanted,
1902 u16 *suballoc_bit_start,
1903 unsigned int *num_bits,
1904 u64 *blkno_start)
1905{
1906 int status;
1907 u64 bg_blkno;
1908
1909 BUG_ON(!ac);
1910 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1911 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001912
1913 status = ocfs2_claim_suballoc_bits(osb,
1914 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001915 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001916 bits_wanted,
1917 1,
1918 suballoc_bit_start,
1919 num_bits,
1920 &bg_blkno);
1921 if (status < 0) {
1922 mlog_errno(status);
1923 goto bail;
1924 }
1925 atomic_inc(&osb->alloc_stats.bg_allocs);
1926
1927 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1928 ac->ac_bits_given += (*num_bits);
1929 status = 0;
1930bail:
1931 mlog_exit(status);
1932 return status;
1933}
1934
Tao Ma13821152009-02-25 00:53:23 +08001935static void ocfs2_init_inode_ac_group(struct inode *dir,
1936 struct buffer_head *parent_fe_bh,
1937 struct ocfs2_alloc_context *ac)
1938{
1939 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1940 /*
1941 * Try to allocate inodes from some specific group.
1942 *
1943 * If the parent dir has recorded the last group used in allocation,
1944 * cool, use it. Otherwise if we try to allocate new inode from the
1945 * same slot the parent dir belongs to, use the same chunk.
1946 *
1947 * We are very careful here to avoid the mistake of setting
1948 * ac_last_group to a group descriptor from a different (unlocked) slot.
1949 */
1950 if (OCFS2_I(dir)->ip_last_used_group &&
1951 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1952 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1953 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1954 ac->ac_last_group = ocfs2_which_suballoc_group(
1955 le64_to_cpu(fe->i_blkno),
1956 le16_to_cpu(fe->i_suballoc_bit));
1957}
1958
1959static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1960 struct ocfs2_alloc_context *ac)
1961{
1962 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1963 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1964}
1965
Mark Fashehccd979b2005-12-15 14:31:24 -08001966int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001967 handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08001968 struct inode *dir,
1969 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001970 struct ocfs2_alloc_context *ac,
1971 u16 *suballoc_bit,
1972 u64 *fe_blkno)
1973{
1974 int status;
1975 unsigned int num_bits;
1976 u64 bg_blkno;
1977
1978 mlog_entry_void();
1979
1980 BUG_ON(!ac);
1981 BUG_ON(ac->ac_bits_given != 0);
1982 BUG_ON(ac->ac_bits_wanted != 1);
1983 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001984
Tao Ma13821152009-02-25 00:53:23 +08001985 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1986
Mark Fashehccd979b2005-12-15 14:31:24 -08001987 status = ocfs2_claim_suballoc_bits(osb,
1988 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001989 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001990 1,
1991 1,
1992 suballoc_bit,
1993 &num_bits,
1994 &bg_blkno);
1995 if (status < 0) {
1996 mlog_errno(status);
1997 goto bail;
1998 }
1999 atomic_inc(&osb->alloc_stats.bg_allocs);
2000
2001 BUG_ON(num_bits != 1);
2002
2003 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
2004 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002005 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002006 status = 0;
2007bail:
2008 mlog_exit(status);
2009 return status;
2010}
2011
2012/* translate a group desc. blkno and it's bitmap offset into
2013 * disk cluster offset. */
2014static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2015 u64 bg_blkno,
2016 u16 bg_bit_off)
2017{
2018 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2019 u32 cluster = 0;
2020
2021 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2022
2023 if (bg_blkno != osb->first_cluster_group_blkno)
2024 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2025 cluster += (u32) bg_bit_off;
2026 return cluster;
2027}
2028
2029/* given a cluster offset, calculate which block group it belongs to
2030 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002031u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002032{
2033 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2034 u32 group_no;
2035
2036 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2037
2038 group_no = cluster / osb->bitmap_cpg;
2039 if (!group_no)
2040 return osb->first_cluster_group_blkno;
2041 return ocfs2_clusters_to_blocks(inode->i_sb,
2042 group_no * osb->bitmap_cpg);
2043}
2044
2045/* given the block number of a cluster start, calculate which cluster
2046 * group and descriptor bitmap offset that corresponds to. */
2047static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2048 u64 data_blkno,
2049 u64 *bg_blkno,
2050 u16 *bg_bit_off)
2051{
2052 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2053 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2054
2055 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2056
2057 *bg_blkno = ocfs2_which_cluster_group(inode,
2058 data_cluster);
2059
2060 if (*bg_blkno == osb->first_cluster_group_blkno)
2061 *bg_bit_off = (u16) data_cluster;
2062 else
2063 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2064 data_blkno - *bg_blkno);
2065}
2066
2067/*
2068 * min_bits - minimum contiguous chunk from this total allocation we
2069 * can handle. set to what we asked for originally for a full
2070 * contig. allocation, set to '1' to indicate we can deal with extents
2071 * of any size.
2072 */
Mark Fasheh415cb802007-09-16 20:10:16 -07002073int __ocfs2_claim_clusters(struct ocfs2_super *osb,
2074 handle_t *handle,
2075 struct ocfs2_alloc_context *ac,
2076 u32 min_clusters,
2077 u32 max_clusters,
2078 u32 *cluster_start,
2079 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002080{
2081 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002082 unsigned int bits_wanted = max_clusters;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04002083 u64 bg_blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002084 u16 bg_bit_off;
2085
2086 mlog_entry_void();
2087
Mark Fashehccd979b2005-12-15 14:31:24 -08002088 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2089
2090 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2091 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002092
2093 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002094 WARN_ON(min_clusters > 1);
2095
Mark Fashehccd979b2005-12-15 14:31:24 -08002096 status = ocfs2_claim_local_alloc_bits(osb,
2097 handle,
2098 ac,
2099 bits_wanted,
2100 cluster_start,
2101 num_clusters);
2102 if (!status)
2103 atomic_inc(&osb->alloc_stats.local_data);
2104 } else {
2105 if (min_clusters > (osb->bitmap_cpg - 1)) {
2106 /* The only paths asking for contiguousness
2107 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002108 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2109 "group bitmap size %u!\n", min_clusters,
2110 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002111 status = -ENOSPC;
2112 goto bail;
2113 }
2114 /* clamp the current request down to a realistic size. */
2115 if (bits_wanted > (osb->bitmap_cpg - 1))
2116 bits_wanted = osb->bitmap_cpg - 1;
2117
2118 status = ocfs2_claim_suballoc_bits(osb,
2119 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002120 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002121 bits_wanted,
2122 min_clusters,
2123 &bg_bit_off,
2124 num_clusters,
2125 &bg_blkno);
2126 if (!status) {
2127 *cluster_start =
2128 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
2129 bg_blkno,
2130 bg_bit_off);
2131 atomic_inc(&osb->alloc_stats.bitmap_data);
2132 }
2133 }
2134 if (status < 0) {
2135 if (status != -ENOSPC)
2136 mlog_errno(status);
2137 goto bail;
2138 }
2139
2140 ac->ac_bits_given += *num_clusters;
2141
2142bail:
2143 mlog_exit(status);
2144 return status;
2145}
2146
Mark Fasheh415cb802007-09-16 20:10:16 -07002147int ocfs2_claim_clusters(struct ocfs2_super *osb,
2148 handle_t *handle,
2149 struct ocfs2_alloc_context *ac,
2150 u32 min_clusters,
2151 u32 *cluster_start,
2152 u32 *num_clusters)
2153{
2154 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2155
2156 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
2157 bits_wanted, cluster_start, num_clusters);
2158}
2159
Mark Fashehb4414ee2010-03-11 18:31:09 -08002160static int ocfs2_block_group_clear_bits(handle_t *handle,
2161 struct inode *alloc_inode,
2162 struct ocfs2_group_desc *bg,
2163 struct buffer_head *group_bh,
2164 unsigned int bit_off,
2165 unsigned int num_bits,
2166 void (*undo_fn)(unsigned int bit,
2167 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002168{
2169 int status;
2170 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002171 struct ocfs2_group_desc *undo_bg = NULL;
2172
2173 mlog_entry_void();
2174
Joel Becker42035302008-11-13 14:49:15 -08002175 /* The caller got this descriptor from
2176 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2177 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002178
2179 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2180
Mark Fashehb4414ee2010-03-11 18:31:09 -08002181 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002182 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002183 group_bh,
2184 undo_fn ?
2185 OCFS2_JOURNAL_ACCESS_UNDO :
2186 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002187 if (status < 0) {
2188 mlog_errno(status);
2189 goto bail;
2190 }
2191
Mark Fashehb4414ee2010-03-11 18:31:09 -08002192 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002193 jbd_lock_bh_state(group_bh);
2194 undo_bg = (struct ocfs2_group_desc *)
2195 bh2jh(group_bh)->b_committed_data;
2196 BUG_ON(!undo_bg);
2197 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002198
2199 tmp = num_bits;
2200 while(tmp--) {
2201 ocfs2_clear_bit((bit_off + tmp),
2202 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002203 if (undo_fn)
2204 undo_fn(bit_off + tmp,
2205 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002206 }
2207 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2208
Mark Fashehb4414ee2010-03-11 18:31:09 -08002209 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002210 jbd_unlock_bh_state(group_bh);
2211
Joel Beckerec20cec2010-03-19 14:13:52 -07002212 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002213bail:
2214 return status;
2215}
2216
2217/*
2218 * expects the suballoc inode to already be locked.
2219 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002220static int _ocfs2_free_suballoc_bits(handle_t *handle,
2221 struct inode *alloc_inode,
2222 struct buffer_head *alloc_bh,
2223 unsigned int start_bit,
2224 u64 bg_blkno,
2225 unsigned int count,
2226 void (*undo_fn)(unsigned int bit,
2227 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002228{
2229 int status = 0;
2230 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2232 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2233 struct buffer_head *group_bh = NULL;
2234 struct ocfs2_group_desc *group;
2235
2236 mlog_entry_void();
2237
Joel Becker10995aa2008-11-13 14:49:12 -08002238 /* The alloc_bh comes from ocfs2_free_dinode() or
2239 * ocfs2_free_clusters(). The callers have all locked the
2240 * allocator and gotten alloc_bh from the lock call. This
2241 * validates the dinode buffer. Any corruption that has happended
2242 * is a code bug. */
2243 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002244 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2245
Mark Fashehb06970532006-03-03 10:24:33 -08002246 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2247 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2248 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002249
Joel Becker68f64d42008-11-13 14:49:14 -08002250 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2251 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002252 if (status < 0) {
2253 mlog_errno(status);
2254 goto bail;
2255 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002256 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002257
Mark Fashehccd979b2005-12-15 14:31:24 -08002258 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2259
2260 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2261 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002262 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002263 if (status < 0) {
2264 mlog_errno(status);
2265 goto bail;
2266 }
2267
Joel Becker0cf2f762009-02-12 16:41:25 -08002268 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2269 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002270 if (status < 0) {
2271 mlog_errno(status);
2272 goto bail;
2273 }
2274
2275 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2276 count);
2277 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2278 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002279 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002280
2281bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002282 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002283
2284 mlog_exit(status);
2285 return status;
2286}
2287
Mark Fashehb4414ee2010-03-11 18:31:09 -08002288int ocfs2_free_suballoc_bits(handle_t *handle,
2289 struct inode *alloc_inode,
2290 struct buffer_head *alloc_bh,
2291 unsigned int start_bit,
2292 u64 bg_blkno,
2293 unsigned int count)
2294{
2295 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2296 start_bit, bg_blkno, count, NULL);
2297}
2298
Mark Fasheh1fabe142006-10-09 18:11:45 -07002299int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002300 struct inode *inode_alloc_inode,
2301 struct buffer_head *inode_alloc_bh,
2302 struct ocfs2_dinode *di)
2303{
2304 u64 blk = le64_to_cpu(di->i_blkno);
2305 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2306 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2307
2308 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2309 inode_alloc_bh, bit, bg_blkno, 1);
2310}
2311
Mark Fashehb4414ee2010-03-11 18:31:09 -08002312static int _ocfs2_free_clusters(handle_t *handle,
2313 struct inode *bitmap_inode,
2314 struct buffer_head *bitmap_bh,
2315 u64 start_blk,
2316 unsigned int num_clusters,
2317 void (*undo_fn)(unsigned int bit,
2318 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002319{
2320 int status;
2321 u16 bg_start_bit;
2322 u64 bg_blkno;
2323 struct ocfs2_dinode *fe;
2324
2325 /* You can't ever have a contiguous set of clusters
2326 * bigger than a block group bitmap so we never have to worry
2327 * about looping on them. */
2328
2329 mlog_entry_void();
2330
2331 /* This is expensive. We can safely remove once this stuff has
2332 * gotten tested really well. */
2333 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2334
2335 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2336
2337 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2338 &bg_start_bit);
2339
Mark Fashehb06970532006-03-03 10:24:33 -08002340 mlog(0, "want to free %u clusters starting at block %llu\n",
2341 num_clusters, (unsigned long long)start_blk);
2342 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2343 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002344
Mark Fashehb4414ee2010-03-11 18:31:09 -08002345 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2346 bg_start_bit, bg_blkno,
2347 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002348 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002349 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002350 goto out;
2351 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002352
Mark Fasheh9c7af402008-07-28 18:02:53 -07002353 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2354 num_clusters);
2355
2356out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002357 mlog_exit(status);
2358 return status;
2359}
2360
Mark Fashehb4414ee2010-03-11 18:31:09 -08002361int ocfs2_free_clusters(handle_t *handle,
2362 struct inode *bitmap_inode,
2363 struct buffer_head *bitmap_bh,
2364 u64 start_blk,
2365 unsigned int num_clusters)
2366{
2367 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2368 start_blk, num_clusters,
2369 _ocfs2_set_bit);
2370}
2371
2372/*
2373 * Give never-used clusters back to the global bitmap. We don't need
2374 * to protect these bits in the undo buffer.
2375 */
2376int ocfs2_release_clusters(handle_t *handle,
2377 struct inode *bitmap_inode,
2378 struct buffer_head *bitmap_bh,
2379 u64 start_blk,
2380 unsigned int num_clusters)
2381{
2382 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2383 start_blk, num_clusters,
2384 _ocfs2_clear_bit);
2385}
2386
Mark Fashehccd979b2005-12-15 14:31:24 -08002387static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2388{
2389 printk("Block Group:\n");
2390 printk("bg_signature: %s\n", bg->bg_signature);
2391 printk("bg_size: %u\n", bg->bg_size);
2392 printk("bg_bits: %u\n", bg->bg_bits);
2393 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2394 printk("bg_chain: %u\n", bg->bg_chain);
2395 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002396 printk("bg_next_group: %llu\n",
2397 (unsigned long long)bg->bg_next_group);
2398 printk("bg_parent_dinode: %llu\n",
2399 (unsigned long long)bg->bg_parent_dinode);
2400 printk("bg_blkno: %llu\n",
2401 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002402}
2403
2404static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2405{
2406 int i;
2407
Mark Fashehb06970532006-03-03 10:24:33 -08002408 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002409 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002410 printk("i_size: %llu\n",
2411 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002412 printk("i_clusters: %u\n", fe->i_clusters);
2413 printk("i_generation: %u\n",
2414 le32_to_cpu(fe->i_generation));
2415 printk("id1.bitmap1.i_used: %u\n",
2416 le32_to_cpu(fe->id1.bitmap1.i_used));
2417 printk("id1.bitmap1.i_total: %u\n",
2418 le32_to_cpu(fe->id1.bitmap1.i_total));
2419 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2420 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2421 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2422 printk("id2.i_chain.cl_next_free_rec: %u\n",
2423 fe->id2.i_chain.cl_next_free_rec);
2424 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2425 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2426 fe->id2.i_chain.cl_recs[i].c_free);
2427 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2428 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002429 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2430 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002431 }
2432}
Tao Mae7d4cb62008-08-18 17:38:44 +08002433
2434/*
2435 * For a given allocation, determine which allocators will need to be
2436 * accessed, and lock them, reserving the appropriate number of bits.
2437 *
2438 * Sparse file systems call this from ocfs2_write_begin_nolock()
2439 * and ocfs2_allocate_unwritten_extents().
2440 *
2441 * File systems which don't support holes call this from
2442 * ocfs2_extend_allocation().
2443 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002444int ocfs2_lock_allocators(struct inode *inode,
2445 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002446 u32 clusters_to_add, u32 extents_to_split,
2447 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002448 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002449{
2450 int ret = 0, num_free_extents;
2451 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2452 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2453
2454 *meta_ac = NULL;
2455 if (data_ac)
2456 *data_ac = NULL;
2457
2458 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2459
Joel Becker3d03a302009-02-12 17:49:26 -08002460 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002461 if (num_free_extents < 0) {
2462 ret = num_free_extents;
2463 mlog_errno(ret);
2464 goto out;
2465 }
2466
2467 /*
2468 * Sparse allocation file systems need to be more conservative
2469 * with reserving room for expansion - the actual allocation
2470 * happens while we've got a journal handle open so re-taking
2471 * a cluster lock (because we ran out of room for another
2472 * extent) will violate ordering rules.
2473 *
2474 * Most of the time we'll only be seeing this 1 cluster at a time
2475 * anyway.
2476 *
2477 * Always lock for any unwritten extents - we might want to
2478 * add blocks during a split.
2479 */
2480 if (!num_free_extents ||
2481 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002482 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002483 if (ret < 0) {
2484 if (ret != -ENOSPC)
2485 mlog_errno(ret);
2486 goto out;
2487 }
2488 }
2489
2490 if (clusters_to_add == 0)
2491 goto out;
2492
2493 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2494 if (ret < 0) {
2495 if (ret != -ENOSPC)
2496 mlog_errno(ret);
2497 goto out;
2498 }
2499
2500out:
2501 if (ret) {
2502 if (*meta_ac) {
2503 ocfs2_free_alloc_context(*meta_ac);
2504 *meta_ac = NULL;
2505 }
2506
2507 /*
2508 * We cannot have an error and a non null *data_ac.
2509 */
2510 }
2511
2512 return ret;
2513}
wengang wang6ca497a2009-03-06 21:29:10 +08002514
2515/*
2516 * Read the inode specified by blkno to get suballoc_slot and
2517 * suballoc_bit.
2518 */
2519static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2520 u16 *suballoc_slot, u16 *suballoc_bit)
2521{
2522 int status;
2523 struct buffer_head *inode_bh = NULL;
2524 struct ocfs2_dinode *inode_fe;
2525
Joel Becker5b09b502009-04-21 16:31:20 -07002526 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002527
2528 /* dirty read disk */
2529 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2530 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002531 mlog(ML_ERROR, "read block %llu failed %d\n",
2532 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002533 goto bail;
2534 }
2535
2536 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2537 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002538 mlog(ML_ERROR, "invalid inode %llu requested\n",
2539 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002540 status = -EINVAL;
2541 goto bail;
2542 }
2543
Tao Ma0fba8132009-03-19 05:08:43 +08002544 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002545 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2546 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002547 (unsigned long long)blkno,
2548 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002549 status = -EINVAL;
2550 goto bail;
2551 }
2552
2553 if (suballoc_slot)
2554 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2555 if (suballoc_bit)
2556 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2557
2558bail:
2559 brelse(inode_bh);
2560
2561 mlog_exit(status);
2562 return status;
2563}
2564
2565/*
2566 * test whether bit is SET in allocator bitmap or not. on success, 0
2567 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2568 * is returned and *res is meaningless. Call this after you have
2569 * cluster locked against suballoc, or you may get a result based on
2570 * non-up2date contents
2571 */
2572static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2573 struct inode *suballoc,
2574 struct buffer_head *alloc_bh, u64 blkno,
2575 u16 bit, int *res)
2576{
2577 struct ocfs2_dinode *alloc_fe;
2578 struct ocfs2_group_desc *group;
2579 struct buffer_head *group_bh = NULL;
2580 u64 bg_blkno;
2581 int status;
2582
Joel Becker5b09b502009-04-21 16:31:20 -07002583 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2584 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002585
2586 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2587 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2588 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2589 (unsigned int)bit,
2590 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2591 status = -EINVAL;
2592 goto bail;
2593 }
2594
2595 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2596 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2597 &group_bh);
2598 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002599 mlog(ML_ERROR, "read group %llu failed %d\n",
2600 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002601 goto bail;
2602 }
2603
2604 group = (struct ocfs2_group_desc *) group_bh->b_data;
2605 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2606
2607bail:
2608 brelse(group_bh);
2609
2610 mlog_exit(status);
2611 return status;
2612}
2613
2614/*
2615 * Test if the bit representing this inode (blkno) is set in the
2616 * suballocator.
2617 *
2618 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2619 *
2620 * In the event of failure, a negative value is returned and *res is
2621 * meaningless.
2622 *
2623 * Callers must make sure to hold nfs_sync_lock to prevent
2624 * ocfs2_delete_inode() on another node from accessing the same
2625 * suballocator concurrently.
2626 */
2627int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2628{
2629 int status;
2630 u16 suballoc_bit = 0, suballoc_slot = 0;
2631 struct inode *inode_alloc_inode;
2632 struct buffer_head *alloc_bh = NULL;
2633
Joel Becker5b09b502009-04-21 16:31:20 -07002634 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002635
2636 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2637 &suballoc_bit);
2638 if (status < 0) {
2639 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2640 goto bail;
2641 }
2642
2643 inode_alloc_inode =
2644 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2645 suballoc_slot);
2646 if (!inode_alloc_inode) {
2647 /* the error code could be inaccurate, but we are not able to
2648 * get the correct one. */
2649 status = -EINVAL;
2650 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2651 (u32)suballoc_slot);
2652 goto bail;
2653 }
2654
2655 mutex_lock(&inode_alloc_inode->i_mutex);
2656 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2657 if (status < 0) {
2658 mutex_unlock(&inode_alloc_inode->i_mutex);
2659 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2660 (u32)suballoc_slot, status);
2661 goto bail;
2662 }
2663
2664 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2665 blkno, suballoc_bit, res);
2666 if (status < 0)
2667 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2668
2669 ocfs2_inode_unlock(inode_alloc_inode, 0);
2670 mutex_unlock(&inode_alloc_inode->i_mutex);
2671
2672 iput(inode_alloc_inode);
2673 brelse(alloc_bh);
2674bail:
2675 mlog_exit(status);
2676 return status;
2677}