blob: d4babfba4f049326da4f8384e9bcc7d78273d1c8 [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,
63 u16 my_chain,
64 struct ocfs2_chain_list *cl);
65static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
66 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070067 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080068 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080069 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080070 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080071
Mark Fashehccd979b2005-12-15 14:31:24 -080072static int ocfs2_cluster_group_search(struct inode *inode,
73 struct buffer_head *group_bh,
74 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070075 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080076 u16 *bit_off, u16 *bits_found);
77static int ocfs2_block_group_search(struct inode *inode,
78 struct buffer_head *group_bh,
79 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070080 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080081 u16 *bit_off, u16 *bits_found);
Mark Fashehccd979b2005-12-15 14:31:24 -080082static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
83 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070084 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080085 u32 bits_wanted,
86 u32 min_bits,
87 u16 *bit_off,
88 unsigned int *num_bits,
89 u64 *bg_blkno);
90static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
91 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070092static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080093 struct inode *alloc_inode,
94 struct ocfs2_group_desc *bg,
95 struct buffer_head *group_bh,
96 unsigned int bit_off,
97 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -070098static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080099 struct inode *alloc_inode,
100 struct buffer_head *fe_bh,
101 struct buffer_head *bg_bh,
102 struct buffer_head *prev_bg_bh,
103 u16 chain);
104static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
105 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800106static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
107 u64 bg_blkno,
108 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800109static inline void ocfs2_block_to_cluster_group(struct inode *inode,
110 u64 data_blkno,
111 u64 *bg_blkno,
112 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700113static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
114 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800115 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700116 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800117
Mark Fasheh9c7af402008-07-28 18:02:53 -0700118void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800119{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700120 struct inode *inode = ac->ac_inode;
121
122 if (inode) {
123 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700124 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700125
126 mutex_unlock(&inode->i_mutex);
127
128 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800129 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700130 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700131 brelse(ac->ac_bh);
132 ac->ac_bh = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800133}
134
135void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
136{
137 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800138 kfree(ac);
139}
140
141static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
142{
143 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
144}
145
Joel Becker57e3e792008-11-13 14:49:13 -0800146#define do_error(fmt, ...) \
147 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800148 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800149 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
150 else \
151 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
152 } while (0)
153
Joel Becker970e4932008-11-13 14:49:19 -0800154static int ocfs2_validate_gd_self(struct super_block *sb,
155 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800156 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800157{
158 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
159
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700160 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800161 do_error("Group descriptor #%llu has bad signature %.*s",
162 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800163 gd->bg_signature);
164 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700165 }
166
Joel Becker68f64d42008-11-13 14:49:14 -0800167 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
168 do_error("Group descriptor #%llu has an invalid bg_blkno "
169 "of %llu",
170 (unsigned long long)bh->b_blocknr,
171 (unsigned long long)le64_to_cpu(gd->bg_blkno));
172 return -EINVAL;
173 }
174
175 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
176 do_error("Group descriptor #%llu has an invalid "
177 "fs_generation of #%u",
178 (unsigned long long)bh->b_blocknr,
179 le32_to_cpu(gd->bg_generation));
180 return -EINVAL;
181 }
182
Joel Becker970e4932008-11-13 14:49:19 -0800183 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
184 do_error("Group descriptor #%llu has bit count %u but "
185 "claims that %u are free",
186 (unsigned long long)bh->b_blocknr,
187 le16_to_cpu(gd->bg_bits),
188 le16_to_cpu(gd->bg_free_bits_count));
189 return -EINVAL;
190 }
191
192 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
193 do_error("Group descriptor #%llu has bit count %u but "
194 "max bitmap bits of %u",
195 (unsigned long long)bh->b_blocknr,
196 le16_to_cpu(gd->bg_bits),
197 8 * le16_to_cpu(gd->bg_size));
198 return -EINVAL;
199 }
200
201 return 0;
202}
203
204static int ocfs2_validate_gd_parent(struct super_block *sb,
205 struct ocfs2_dinode *di,
206 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800207 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800208{
209 unsigned int max_bits;
210 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
211
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700212 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800213 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800214 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800215 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800216 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
217 (unsigned long long)le64_to_cpu(di->i_blkno));
218 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700219 }
220
221 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
222 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800223 do_error("Group descriptor #%llu has bit count of %u",
224 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800225 le16_to_cpu(gd->bg_bits));
226 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700227 }
228
Tao Ma78c37eb2010-03-03 11:26:27 +0800229 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
230 if ((le16_to_cpu(gd->bg_chain) >
231 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
232 ((le16_to_cpu(gd->bg_chain) ==
233 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800234 do_error("Group descriptor #%llu has bad chain %u",
235 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800236 le16_to_cpu(gd->bg_chain));
237 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700238 }
239
Joel Becker970e4932008-11-13 14:49:19 -0800240 return 0;
241}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700242
Joel Becker57e3e792008-11-13 14:49:13 -0800243#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700244
Joel Becker970e4932008-11-13 14:49:19 -0800245/*
246 * This version only prints errors. It does not fail the filesystem, and
247 * exists only for resize.
248 */
249int ocfs2_check_group_descriptor(struct super_block *sb,
250 struct ocfs2_dinode *di,
251 struct buffer_head *bh)
252{
253 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700254 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800255
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700256 BUG_ON(!buffer_uptodate(bh));
257
258 /*
259 * If the ecc fails, we return the error but otherwise
260 * leave the filesystem running. We know any error is
261 * local to this block.
262 */
263 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700264 if (rc) {
265 mlog(ML_ERROR,
266 "Checksum failed for group descriptor %llu\n",
267 (unsigned long long)bh->b_blocknr);
268 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700269 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800270 if (!rc)
271 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
272
273 return rc;
274}
275
276static int ocfs2_validate_group_descriptor(struct super_block *sb,
277 struct buffer_head *bh)
278{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700279 int rc;
280 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
281
Joel Becker970e4932008-11-13 14:49:19 -0800282 mlog(0, "Validating group descriptor %llu\n",
283 (unsigned long long)bh->b_blocknr);
284
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700285 BUG_ON(!buffer_uptodate(bh));
286
287 /*
288 * If the ecc fails, we return the error but otherwise
289 * leave the filesystem running. We know any error is
290 * local to this block.
291 */
292 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
293 if (rc)
294 return rc;
295
296 /*
297 * Errors after here are fatal.
298 */
299
Joel Becker970e4932008-11-13 14:49:19 -0800300 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700301}
302
Joel Becker68f64d42008-11-13 14:49:14 -0800303int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
304 u64 gd_blkno, struct buffer_head **bh)
305{
306 int rc;
307 struct buffer_head *tmp = *bh;
308
Joel Becker8cb471e2009-02-10 20:00:41 -0800309 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800310 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800311 if (rc)
312 goto out;
313
Joel Becker970e4932008-11-13 14:49:19 -0800314 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800315 if (rc) {
316 brelse(tmp);
317 goto out;
318 }
319
320 /* If ocfs2_read_block() got us a new bh, pass it up. */
321 if (!*bh)
322 *bh = tmp;
323
324out:
325 return rc;
326}
327
Mark Fasheh1fabe142006-10-09 18:11:45 -0700328static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800329 struct inode *alloc_inode,
330 struct buffer_head *bg_bh,
331 u64 group_blkno,
332 u16 my_chain,
333 struct ocfs2_chain_list *cl)
334{
335 int status = 0;
336 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
337 struct super_block * sb = alloc_inode->i_sb;
338
339 mlog_entry_void();
340
341 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800342 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
343 "b_blocknr (%llu)",
344 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800345 (unsigned long long) bg_bh->b_blocknr);
346 status = -EIO;
347 goto bail;
348 }
349
Joel Becker13723d02008-10-17 19:25:01 -0700350 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800351 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700352 bg_bh,
353 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800354 if (status < 0) {
355 mlog_errno(status);
356 goto bail;
357 }
358
359 memset(bg, 0, sb->s_blocksize);
360 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
361 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
362 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
363 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
364 bg->bg_chain = cpu_to_le16(my_chain);
365 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
366 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
367 bg->bg_blkno = cpu_to_le64(group_blkno);
368 /* set the 1st bit in the bitmap to account for the descriptor block */
369 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
370 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
371
Joel Beckerec20cec2010-03-19 14:13:52 -0700372 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800373
374 /* There is no need to zero out or otherwise initialize the
375 * other blocks in a group - All valid FS metadata in a block
376 * group stores the superblock fs_generation value at
377 * allocation time. */
378
379bail:
380 mlog_exit(status);
381 return status;
382}
383
384static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
385{
386 u16 curr, best;
387
388 best = curr = 0;
389 while (curr < le16_to_cpu(cl->cl_count)) {
390 if (le32_to_cpu(cl->cl_recs[best].c_total) >
391 le32_to_cpu(cl->cl_recs[curr].c_total))
392 best = curr;
393 curr++;
394 }
395 return best;
396}
397
398/*
399 * We expect the block group allocator to already be locked.
400 */
401static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
402 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700403 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800404 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800405 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800406 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800407{
408 int status, credits;
409 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
410 struct ocfs2_chain_list *cl;
411 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700412 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800413 u32 bit_off, num_bits;
414 u16 alloc_rec;
415 u64 bg_blkno;
416 struct buffer_head *bg_bh = NULL;
417 struct ocfs2_group_desc *bg;
418
419 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
420
421 mlog_entry_void();
422
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700424 status = ocfs2_reserve_clusters_with_limit(osb,
425 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800426 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 if (status < 0) {
428 if (status != -ENOSPC)
429 mlog_errno(status);
430 goto bail;
431 }
432
433 credits = ocfs2_calc_group_alloc_credits(osb->sb,
434 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700435 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800436 if (IS_ERR(handle)) {
437 status = PTR_ERR(handle);
438 handle = NULL;
439 mlog_errno(status);
440 goto bail;
441 }
442
Tao Mafeb473a2009-02-25 00:53:25 +0800443 if (last_alloc_group && *last_alloc_group != 0) {
444 mlog(0, "use old allocation group %llu for block group alloc\n",
445 (unsigned long long)*last_alloc_group);
446 ac->ac_last_group = *last_alloc_group;
447 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800448 status = ocfs2_claim_clusters(osb,
449 handle,
450 ac,
451 le16_to_cpu(cl->cl_cpg),
452 &bit_off,
453 &num_bits);
454 if (status < 0) {
455 if (status != -ENOSPC)
456 mlog_errno(status);
457 goto bail;
458 }
459
460 alloc_rec = ocfs2_find_smallest_chain(cl);
461
462 /* setup the group */
463 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb06970532006-03-03 10:24:33 -0800464 mlog(0, "new descriptor, record %u, at block %llu\n",
465 alloc_rec, (unsigned long long)bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800466
467 bg_bh = sb_getblk(osb->sb, bg_blkno);
468 if (!bg_bh) {
469 status = -EIO;
470 mlog_errno(status);
471 goto bail;
472 }
Joel Becker8cb471e2009-02-10 20:00:41 -0800473 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800474
475 status = ocfs2_block_group_fill(handle,
476 alloc_inode,
477 bg_bh,
478 bg_blkno,
479 alloc_rec,
480 cl);
481 if (status < 0) {
482 mlog_errno(status);
483 goto bail;
484 }
485
486 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
487
Joel Becker0cf2f762009-02-12 16:41:25 -0800488 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700489 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800490 if (status < 0) {
491 mlog_errno(status);
492 goto bail;
493 }
494
495 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
496 le16_to_cpu(bg->bg_free_bits_count));
497 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
498 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
499 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
500 le16_add_cpu(&cl->cl_next_free_rec, 1);
501
502 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
503 le16_to_cpu(bg->bg_free_bits_count));
504 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
505 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
506
Joel Beckerec20cec2010-03-19 14:13:52 -0700507 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800508
509 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
510 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
511 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
512 le32_to_cpu(fe->i_clusters)));
513 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
514 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700515 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800516
517 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800518
519 /* save the new last alloc group so that the caller can cache it. */
520 if (last_alloc_group)
521 *last_alloc_group = ac->ac_last_group;
522
Mark Fashehccd979b2005-12-15 14:31:24 -0800523bail:
524 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700525 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800526
527 if (ac)
528 ocfs2_free_alloc_context(ac);
529
Mark Fasheha81cb882008-10-07 14:25:16 -0700530 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800531
532 mlog_exit(status);
533 return status;
534}
535
536static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700537 struct ocfs2_alloc_context *ac,
538 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800539 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800540 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800541 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800542{
543 int status;
544 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700545 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800546 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800547 struct ocfs2_dinode *fe;
548 u32 free_bits;
549
550 mlog_entry_void();
551
Mark Fashehda5cbf22006-10-06 18:34:35 -0700552 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
553 if (!alloc_inode) {
554 mlog_errno(-EINVAL);
555 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800556 }
557
Mark Fashehda5cbf22006-10-06 18:34:35 -0700558 mutex_lock(&alloc_inode->i_mutex);
559
Mark Fashehe63aecb62007-10-18 15:30:42 -0700560 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700561 if (status < 0) {
562 mutex_unlock(&alloc_inode->i_mutex);
563 iput(alloc_inode);
564
565 mlog_errno(status);
566 return status;
567 }
568
569 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800570 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700571
Mark Fashehccd979b2005-12-15 14:31:24 -0800572 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800573
574 /* The bh was validated by the inode read inside
575 * ocfs2_inode_lock(). Any corruption is a code bug. */
576 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
577
Mark Fashehccd979b2005-12-15 14:31:24 -0800578 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800579 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
580 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800581 status = -EIO;
582 goto bail;
583 }
584
585 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
586 le32_to_cpu(fe->id1.bitmap1.i_used);
587
588 if (bits_wanted > free_bits) {
589 /* cluster bitmap never grows */
590 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
591 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
592 bits_wanted, free_bits);
593 status = -ENOSPC;
594 goto bail;
595 }
596
Tao Ma60ca81e2009-02-25 00:53:24 +0800597 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800598 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
599 "and we don't alloc a new group for it.\n",
600 slot, bits_wanted, free_bits);
601 status = -ENOSPC;
602 goto bail;
603 }
604
Joel Becker1187c962008-09-03 20:03:39 -0700605 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800606 ac->ac_max_block,
607 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800608 if (status < 0) {
609 if (status != -ENOSPC)
610 mlog_errno(status);
611 goto bail;
612 }
613 atomic_inc(&osb->alloc_stats.bg_extends);
614
615 /* You should never ask for this much metadata */
616 BUG_ON(bits_wanted >
617 (le32_to_cpu(fe->id1.bitmap1.i_total)
618 - le32_to_cpu(fe->id1.bitmap1.i_used)));
619 }
620
621 get_bh(bh);
622 ac->ac_bh = bh;
623bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700624 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800625
626 mlog_exit(status);
627 return status;
628}
629
Tiger Yangb89c5422010-01-25 14:11:06 +0800630static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
631{
632 spin_lock(&osb->osb_lock);
633 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
634 spin_unlock(&osb->osb_lock);
635 atomic_set(&osb->s_num_inodes_stolen, 0);
636}
637
638static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
639{
640 spin_lock(&osb->osb_lock);
641 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
642 spin_unlock(&osb->osb_lock);
643 atomic_set(&osb->s_num_meta_stolen, 0);
644}
645
646void ocfs2_init_steal_slots(struct ocfs2_super *osb)
647{
648 ocfs2_init_inode_steal_slot(osb);
649 ocfs2_init_meta_steal_slot(osb);
650}
651
652static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
653{
654 spin_lock(&osb->osb_lock);
655 if (type == INODE_ALLOC_SYSTEM_INODE)
656 osb->s_inode_steal_slot = slot;
657 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
658 osb->s_meta_steal_slot = slot;
659 spin_unlock(&osb->osb_lock);
660}
661
662static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
663{
664 int slot = OCFS2_INVALID_SLOT;
665
666 spin_lock(&osb->osb_lock);
667 if (type == INODE_ALLOC_SYSTEM_INODE)
668 slot = osb->s_inode_steal_slot;
669 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
670 slot = osb->s_meta_steal_slot;
671 spin_unlock(&osb->osb_lock);
672
673 return slot;
674}
675
676static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
677{
678 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
679}
680
681static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
682{
683 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
684}
685
686static int ocfs2_steal_resource(struct ocfs2_super *osb,
687 struct ocfs2_alloc_context *ac,
688 int type)
689{
690 int i, status = -ENOSPC;
691 int slot = __ocfs2_get_steal_slot(osb, type);
692
693 /* Start to steal resource from the first slot after ours. */
694 if (slot == OCFS2_INVALID_SLOT)
695 slot = osb->slot_num + 1;
696
697 for (i = 0; i < osb->max_slots; i++, slot++) {
698 if (slot == osb->max_slots)
699 slot = 0;
700
701 if (slot == osb->slot_num)
702 continue;
703
704 status = ocfs2_reserve_suballoc_bits(osb, ac,
705 type,
706 (u32)slot, NULL,
707 NOT_ALLOC_NEW_GROUP);
708 if (status >= 0) {
709 __ocfs2_set_steal_slot(osb, slot, type);
710 break;
711 }
712
713 ocfs2_free_ac_resource(ac);
714 }
715
716 return status;
717}
718
719static int ocfs2_steal_inode(struct ocfs2_super *osb,
720 struct ocfs2_alloc_context *ac)
721{
722 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
723}
724
725static int ocfs2_steal_meta(struct ocfs2_super *osb,
726 struct ocfs2_alloc_context *ac)
727{
728 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
729}
730
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800731int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
732 int blocks,
733 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800734{
735 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800736 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800737
Robert P. J. Daycd861282006-12-13 00:34:52 -0800738 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800739 if (!(*ac)) {
740 status = -ENOMEM;
741 mlog_errno(status);
742 goto bail;
743 }
744
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800745 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800746 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800747 (*ac)->ac_group_search = ocfs2_block_group_search;
748
Tiger Yangb89c5422010-01-25 14:11:06 +0800749 if (slot != OCFS2_INVALID_SLOT &&
750 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
751 goto extent_steal;
752
753 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700754 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800755 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800756 (u32)osb->slot_num, NULL,
757 ALLOC_NEW_GROUP);
758
759
760 if (status >= 0) {
761 status = 0;
762 if (slot != OCFS2_INVALID_SLOT)
763 ocfs2_init_meta_steal_slot(osb);
764 goto bail;
765 } else if (status < 0 && status != -ENOSPC) {
766 mlog_errno(status);
767 goto bail;
768 }
769
770 ocfs2_free_ac_resource(*ac);
771
772extent_steal:
773 status = ocfs2_steal_meta(osb, *ac);
774 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -0800775 if (status < 0) {
776 if (status != -ENOSPC)
777 mlog_errno(status);
778 goto bail;
779 }
780
781 status = 0;
782bail:
783 if ((status < 0) && *ac) {
784 ocfs2_free_alloc_context(*ac);
785 *ac = NULL;
786 }
787
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 mlog_exit(status);
789 return status;
790}
791
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800792int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
793 struct ocfs2_extent_list *root_el,
794 struct ocfs2_alloc_context **ac)
795{
796 return ocfs2_reserve_new_metadata_blocks(osb,
797 ocfs2_extend_meta_needed(root_el),
798 ac);
799}
800
Mark Fashehccd979b2005-12-15 14:31:24 -0800801int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800802 struct ocfs2_alloc_context **ac)
803{
804 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800805 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +0800806 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -0800807
Robert P. J. Daycd861282006-12-13 00:34:52 -0800808 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 if (!(*ac)) {
810 status = -ENOMEM;
811 mlog_errno(status);
812 goto bail;
813 }
814
815 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800816 (*ac)->ac_which = OCFS2_AC_USE_INODE;
817
Mark Fashehccd979b2005-12-15 14:31:24 -0800818 (*ac)->ac_group_search = ocfs2_block_group_search;
819
Tao Ma4d0ddb22008-03-05 16:11:46 +0800820 /*
Joel Becker1187c962008-09-03 20:03:39 -0700821 * stat(2) can't handle i_ino > 32bits, so we tell the
822 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -0700823 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -0700824 */
Joel Becker12462f12008-09-03 20:03:40 -0700825 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
826 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -0700827
828 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +0800829 * slot is set when we successfully steal inode from other nodes.
830 * It is reset in 3 places:
831 * 1. when we flush the truncate log
832 * 2. when we complete local alloc recovery.
833 * 3. when we successfully allocate from our own slot.
834 * After it is set, we will go on stealing inodes until we find the
835 * need to check our slots to see whether there is some space for us.
836 */
837 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +0800838 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +0800839 goto inode_steal;
840
841 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +0800842 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700843 status = ocfs2_reserve_suballoc_bits(osb, *ac,
844 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800845 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +0800846 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800847 ALLOC_NEW_GROUP |
848 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800849 if (status >= 0) {
850 status = 0;
851
Tao Mafeb473a2009-02-25 00:53:25 +0800852 spin_lock(&osb->osb_lock);
853 osb->osb_inode_alloc_group = alloc_group;
854 spin_unlock(&osb->osb_lock);
855 mlog(0, "after reservation, new allocation group is "
856 "%llu\n", (unsigned long long)alloc_group);
857
Tao Ma4d0ddb22008-03-05 16:11:46 +0800858 /*
859 * Some inodes must be freed by us, so try to allocate
860 * from our own next time.
861 */
862 if (slot != OCFS2_INVALID_SLOT)
863 ocfs2_init_inode_steal_slot(osb);
864 goto bail;
865 } else if (status < 0 && status != -ENOSPC) {
866 mlog_errno(status);
867 goto bail;
868 }
869
870 ocfs2_free_ac_resource(*ac);
871
872inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +0800873 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800874 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -0800875 if (status < 0) {
876 if (status != -ENOSPC)
877 mlog_errno(status);
878 goto bail;
879 }
880
881 status = 0;
882bail:
883 if ((status < 0) && *ac) {
884 ocfs2_free_alloc_context(*ac);
885 *ac = NULL;
886 }
887
Mark Fashehccd979b2005-12-15 14:31:24 -0800888 mlog_exit(status);
889 return status;
890}
891
892/* local alloc code has to do the same thing, so rather than do this
893 * twice.. */
894int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
895 struct ocfs2_alloc_context *ac)
896{
897 int status;
898
Mark Fashehccd979b2005-12-15 14:31:24 -0800899 ac->ac_which = OCFS2_AC_USE_MAIN;
900 ac->ac_group_search = ocfs2_cluster_group_search;
901
Mark Fashehda5cbf22006-10-06 18:34:35 -0700902 status = ocfs2_reserve_suballoc_bits(osb, ac,
903 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +0800904 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +0800905 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700906 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800907 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700908 goto bail;
909 }
910
Mark Fashehccd979b2005-12-15 14:31:24 -0800911bail:
912 return status;
913}
914
915/* Callers don't need to care which bitmap (local alloc or main) to
916 * use so we figure it out for them, but unfortunately this clutters
917 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -0700918static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
919 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800920 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700921 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800922{
923 int status;
924
925 mlog_entry_void();
926
Robert P. J. Daycd861282006-12-13 00:34:52 -0800927 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800928 if (!(*ac)) {
929 status = -ENOMEM;
930 mlog_errno(status);
931 goto bail;
932 }
933
934 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -0700935 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -0800936
937 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +0800938 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
939 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800940 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800941 bits_wanted,
942 *ac);
Joel Becker1187c962008-09-03 20:03:39 -0700943 if (status == -EFBIG) {
944 /* The local alloc window is outside ac_max_block.
945 * use the main bitmap. */
946 status = -ENOSPC;
947 } else if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800948 mlog_errno(status);
949 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800950 }
951 }
952
953 if (status == -ENOSPC) {
954 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
955 if (status < 0) {
956 if (status != -ENOSPC)
957 mlog_errno(status);
958 goto bail;
959 }
960 }
961
962 status = 0;
963bail:
964 if ((status < 0) && *ac) {
965 ocfs2_free_alloc_context(*ac);
966 *ac = NULL;
967 }
968
969 mlog_exit(status);
970 return status;
971}
972
Joel Becker1187c962008-09-03 20:03:39 -0700973int ocfs2_reserve_clusters(struct ocfs2_super *osb,
974 u32 bits_wanted,
975 struct ocfs2_alloc_context **ac)
976{
Tao Ma60ca81e2009-02-25 00:53:24 +0800977 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
978 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -0700979}
980
Mark Fashehccd979b2005-12-15 14:31:24 -0800981/*
982 * More or less lifted from ext3. I'll leave their description below:
983 *
984 * "For ext3 allocations, we must not reuse any blocks which are
985 * allocated in the bitmap buffer's "last committed data" copy. This
986 * prevents deletes from freeing up the page for reuse until we have
987 * committed the delete transaction.
988 *
989 * If we didn't do this, then deleting something and reallocating it as
990 * data would allow the old block to be overwritten before the
991 * transaction committed (because we force data to disk before commit).
992 * This would lead to corruption if we crashed between overwriting the
993 * data and committing the delete.
994 *
995 * @@@ We may want to make this allocation behaviour conditional on
996 * data-writes at some point, and disable it for metadata allocations or
997 * sync-data inodes."
998 *
999 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001000 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001001 * called on a metadata group descriptor.
1002 */
1003static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1004 int nr)
1005{
1006 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001007 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001008
1009 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1010 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001011
1012 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001013 return 1;
1014
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001015 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001016 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001017 if (bg)
1018 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1019 else
1020 ret = 1;
1021 jbd_unlock_bh_state(bg_bh);
1022
1023 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001024}
1025
1026static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1027 struct buffer_head *bg_bh,
1028 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001029 unsigned int total_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001030 u16 *bit_off,
1031 u16 *bits_found)
1032{
1033 void *bitmap;
1034 u16 best_offset, best_size;
1035 int offset, start, found, status = 0;
1036 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1037
Joel Becker42035302008-11-13 14:49:15 -08001038 /* Callers got this descriptor from
1039 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1040 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001041
1042 found = start = best_offset = best_size = 0;
1043 bitmap = bg->bg_bitmap;
1044
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001045 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1046 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001047 break;
1048
1049 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1050 /* We found a zero, but we can't use it as it
1051 * hasn't been put to disk yet! */
1052 found = 0;
1053 start = offset + 1;
1054 } else if (offset == start) {
1055 /* we found a zero */
1056 found++;
1057 /* move start to the next bit to test */
1058 start++;
1059 } else {
1060 /* got a zero after some ones */
1061 found = 1;
1062 start = offset + 1;
1063 }
1064 if (found > best_size) {
1065 best_size = found;
1066 best_offset = start - found;
1067 }
1068 /* we got everything we needed */
1069 if (found == bits_wanted) {
1070 /* mlog(0, "Found it all!\n"); */
1071 break;
1072 }
1073 }
1074
1075 /* XXX: I think the first clause is equivalent to the second
1076 * - jlbec */
1077 if (found == bits_wanted) {
1078 *bit_off = start - found;
1079 *bits_found = found;
1080 } else if (best_size) {
1081 *bit_off = best_offset;
1082 *bits_found = best_size;
1083 } else {
1084 status = -ENOSPC;
1085 /* No error log here -- see the comment above
1086 * ocfs2_test_bg_bit_allocatable */
1087 }
1088
1089 return status;
1090}
1091
Mark Fasheh1fabe142006-10-09 18:11:45 -07001092static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001093 struct inode *alloc_inode,
1094 struct ocfs2_group_desc *bg,
1095 struct buffer_head *group_bh,
1096 unsigned int bit_off,
1097 unsigned int num_bits)
1098{
1099 int status;
1100 void *bitmap = bg->bg_bitmap;
1101 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1102
1103 mlog_entry_void();
1104
Joel Becker42035302008-11-13 14:49:15 -08001105 /* All callers get the descriptor via
1106 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1107 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001108 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1109
1110 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1111 num_bits);
1112
1113 if (ocfs2_is_cluster_bitmap(alloc_inode))
1114 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1115
Joel Becker13723d02008-10-17 19:25:01 -07001116 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001117 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001118 group_bh,
1119 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001120 if (status < 0) {
1121 mlog_errno(status);
1122 goto bail;
1123 }
1124
1125 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001126 while(num_bits--)
1127 ocfs2_set_bit(bit_off++, bitmap);
1128
Joel Beckerec20cec2010-03-19 14:13:52 -07001129 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001130
1131bail:
1132 mlog_exit(status);
1133 return status;
1134}
1135
1136/* find the one with the most empty bits */
1137static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1138{
1139 u16 curr, best;
1140
1141 BUG_ON(!cl->cl_next_free_rec);
1142
1143 best = curr = 0;
1144 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1145 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1146 le32_to_cpu(cl->cl_recs[best].c_free))
1147 best = curr;
1148 curr++;
1149 }
1150
1151 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1152 return best;
1153}
1154
Mark Fasheh1fabe142006-10-09 18:11:45 -07001155static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 struct inode *alloc_inode,
1157 struct buffer_head *fe_bh,
1158 struct buffer_head *bg_bh,
1159 struct buffer_head *prev_bg_bh,
1160 u16 chain)
1161{
1162 int status;
1163 /* there is a really tiny chance the journal calls could fail,
1164 * but we wouldn't want inconsistent blocks in *any* case. */
1165 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1166 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1167 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1168 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1169
Joel Becker42035302008-11-13 14:49:15 -08001170 /* The caller got these descriptors from
1171 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1172 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1173 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001174
Mark Fashehb06970532006-03-03 10:24:33 -08001175 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001176 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1177 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1178 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001179
1180 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1181 bg_ptr = le64_to_cpu(bg->bg_next_group);
1182 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1183
Joel Becker0cf2f762009-02-12 16:41:25 -08001184 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1185 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001186 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 if (status < 0) {
1188 mlog_errno(status);
1189 goto out_rollback;
1190 }
1191
1192 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001193 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001194
Joel Becker0cf2f762009-02-12 16:41:25 -08001195 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1196 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001197 if (status < 0) {
1198 mlog_errno(status);
1199 goto out_rollback;
1200 }
1201
1202 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001203 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001204
Joel Becker0cf2f762009-02-12 16:41:25 -08001205 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1206 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001207 if (status < 0) {
1208 mlog_errno(status);
1209 goto out_rollback;
1210 }
1211
1212 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001213 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001214
Mark Fashehccd979b2005-12-15 14:31:24 -08001215out_rollback:
1216 if (status < 0) {
1217 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1218 bg->bg_next_group = cpu_to_le64(bg_ptr);
1219 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1220 }
Joel Becker42035302008-11-13 14:49:15 -08001221
Mark Fashehccd979b2005-12-15 14:31:24 -08001222 mlog_exit(status);
1223 return status;
1224}
1225
1226static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1227 u32 wanted)
1228{
1229 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1230}
1231
1232/* return 0 on success, -ENOSPC to keep searching and any other < 0
1233 * value on error. */
1234static int ocfs2_cluster_group_search(struct inode *inode,
1235 struct buffer_head *group_bh,
1236 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001237 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001238 u16 *bit_off, u16 *bits_found)
1239{
1240 int search = -ENOSPC;
1241 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001242 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001243 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001244 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001245 u16 tmp_off, tmp_found;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001246 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001247
1248 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1249
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001250 if (gd->bg_free_bits_count) {
1251 max_bits = le16_to_cpu(gd->bg_bits);
1252
1253 /* Tail groups in cluster bitmaps which aren't cpg
1254 * aligned are prone to partial extention by a failed
1255 * fs resize. If the file system resize never got to
1256 * update the dinode cluster count, then we don't want
1257 * to trust any clusters past it, regardless of what
1258 * the group descriptor says. */
1259 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1260 le64_to_cpu(gd->bg_blkno));
1261 if ((gd_cluster_off + max_bits) >
1262 OCFS2_I(inode)->ip_clusters) {
1263 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1264 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1265 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1266 le16_to_cpu(gd->bg_bits),
1267 OCFS2_I(inode)->ip_clusters, max_bits);
1268 }
1269
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1271 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001272 max_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001273 &tmp_off, &tmp_found);
1274 if (ret)
1275 return ret;
1276
Joel Becker1187c962008-09-03 20:03:39 -07001277 if (max_block) {
1278 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1279 gd_cluster_off +
1280 tmp_off + tmp_found);
1281 mlog(0, "Checking %llu against %llu\n",
1282 (unsigned long long)blkoff,
1283 (unsigned long long)max_block);
1284 if (blkoff > max_block)
1285 return -ENOSPC;
1286 }
1287
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 /* ocfs2_block_group_find_clear_bits() might
1289 * return success, but we still want to return
1290 * -ENOSPC unless it found the minimum number
1291 * of bits. */
1292 if (min_bits <= tmp_found) {
1293 *bit_off = tmp_off;
1294 *bits_found = tmp_found;
1295 search = 0; /* success */
Mark Fasheh9c7af402008-07-28 18:02:53 -07001296 } else if (tmp_found) {
1297 /*
1298 * Don't show bits which we'll be returning
1299 * for allocation to the local alloc bitmap.
1300 */
1301 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 }
1303 }
1304
1305 return search;
1306}
1307
1308static int ocfs2_block_group_search(struct inode *inode,
1309 struct buffer_head *group_bh,
1310 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001311 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001312 u16 *bit_off, u16 *bits_found)
1313{
1314 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001315 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001316 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1317
1318 BUG_ON(min_bits != 1);
1319 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1320
Joel Becker1187c962008-09-03 20:03:39 -07001321 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001322 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1323 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001324 le16_to_cpu(bg->bg_bits),
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 bit_off, bits_found);
Joel Becker1187c962008-09-03 20:03:39 -07001326 if (!ret && max_block) {
1327 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1328 *bits_found;
1329 mlog(0, "Checking %llu against %llu\n",
1330 (unsigned long long)blkoff,
1331 (unsigned long long)max_block);
1332 if (blkoff > max_block)
1333 ret = -ENOSPC;
1334 }
1335 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001336
1337 return ret;
1338}
1339
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001340static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001341 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001342 struct buffer_head *di_bh,
1343 u32 num_bits,
1344 u16 chain)
1345{
1346 int ret;
1347 u32 tmp_used;
1348 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1349 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1350
Joel Becker0cf2f762009-02-12 16:41:25 -08001351 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001352 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001353 if (ret < 0) {
1354 mlog_errno(ret);
1355 goto out;
1356 }
1357
1358 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1359 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1360 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001361 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001362
1363out:
1364 return ret;
1365}
1366
1367static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001368 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001369 u32 bits_wanted,
1370 u32 min_bits,
1371 u16 *bit_off,
1372 unsigned int *num_bits,
1373 u64 gd_blkno,
1374 u16 *bits_left)
1375{
1376 int ret;
1377 u16 found;
1378 struct buffer_head *group_bh = NULL;
1379 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001380 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001381 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001382
Joel Becker68f64d42008-11-13 14:49:14 -08001383 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1384 &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001385 if (ret < 0) {
1386 mlog_errno(ret);
1387 return ret;
1388 }
1389
1390 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001391 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001392 ac->ac_max_block, bit_off, &found);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001393 if (ret < 0) {
1394 if (ret != -ENOSPC)
1395 mlog_errno(ret);
1396 goto out;
1397 }
1398
1399 *num_bits = found;
1400
1401 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1402 *num_bits,
1403 le16_to_cpu(gd->bg_chain));
1404 if (ret < 0) {
1405 mlog_errno(ret);
1406 goto out;
1407 }
1408
1409 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1410 *bit_off, *num_bits);
1411 if (ret < 0)
1412 mlog_errno(ret);
1413
1414 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1415
1416out:
1417 brelse(group_bh);
1418
1419 return ret;
1420}
1421
Mark Fashehccd979b2005-12-15 14:31:24 -08001422static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001423 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001424 u32 bits_wanted,
1425 u32 min_bits,
1426 u16 *bit_off,
1427 unsigned int *num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001428 u64 *bg_blkno,
1429 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001430{
1431 int status;
1432 u16 chain, tmp_bits;
1433 u32 tmp_used;
1434 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001435 struct inode *alloc_inode = ac->ac_inode;
1436 struct buffer_head *group_bh = NULL;
1437 struct buffer_head *prev_group_bh = NULL;
1438 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1439 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1440 struct ocfs2_group_desc *bg;
1441
1442 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001443 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1444 bits_wanted, chain,
1445 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001446
Joel Becker68f64d42008-11-13 14:49:14 -08001447 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1448 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1449 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001450 if (status < 0) {
1451 mlog_errno(status);
1452 goto bail;
1453 }
1454 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001455
1456 status = -ENOSPC;
1457 /* for now, the chain search is a bit simplistic. We just use
1458 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001459 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1460 bits_wanted, min_bits,
1461 ac->ac_max_block, bit_off,
1462 &tmp_bits)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001463 if (!bg->bg_next_group)
1464 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001465
1466 brelse(prev_group_bh);
1467 prev_group_bh = NULL;
1468
Mark Fashehccd979b2005-12-15 14:31:24 -08001469 next_group = le64_to_cpu(bg->bg_next_group);
1470 prev_group_bh = group_bh;
1471 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001472 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1473 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001474 if (status < 0) {
1475 mlog_errno(status);
1476 goto bail;
1477 }
1478 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001479 }
1480 if (status < 0) {
1481 if (status != -ENOSPC)
1482 mlog_errno(status);
1483 goto bail;
1484 }
1485
Mark Fashehb06970532006-03-03 10:24:33 -08001486 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001487 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001488
1489 *num_bits = tmp_bits;
1490
1491 BUG_ON(*num_bits == 0);
1492
1493 /*
1494 * Keep track of previous block descriptor read. When
1495 * we find a target, if we have read more than X
1496 * number of descriptors, and the target is reasonably
1497 * empty, relink him to top of his chain.
1498 *
1499 * We've read 0 extra blocks and only send one more to
1500 * the transaction, yet the next guy to search has a
1501 * much easier time.
1502 *
1503 * Do this *after* figuring out how many bits we're taking out
1504 * of our target group.
1505 */
1506 if (ac->ac_allow_chain_relink &&
1507 (prev_group_bh) &&
1508 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1509 status = ocfs2_relink_block_group(handle, alloc_inode,
1510 ac->ac_bh, group_bh,
1511 prev_group_bh, chain);
1512 if (status < 0) {
1513 mlog_errno(status);
1514 goto bail;
1515 }
1516 }
1517
1518 /* Ok, claim our bits now: set the info on dinode, chainlist
1519 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001520 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001521 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001522 ac->ac_bh,
1523 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001524 if (status < 0) {
1525 mlog_errno(status);
1526 goto bail;
1527 }
1528
1529 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1530 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1531 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
Joel Beckerec20cec2010-03-19 14:13:52 -07001532 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001533
1534 status = ocfs2_block_group_set_bits(handle,
1535 alloc_inode,
1536 bg,
1537 group_bh,
1538 *bit_off,
1539 *num_bits);
1540 if (status < 0) {
1541 mlog_errno(status);
1542 goto bail;
1543 }
1544
Mark Fashehb06970532006-03-03 10:24:33 -08001545 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001546 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001547
1548 *bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001549 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001550bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001551 brelse(group_bh);
1552 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001553
1554 mlog_exit(status);
1555 return status;
1556}
1557
1558/* will give out up to bits_wanted contiguous bits. */
1559static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1560 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001561 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001562 u32 bits_wanted,
1563 u32 min_bits,
1564 u16 *bit_off,
1565 unsigned int *num_bits,
1566 u64 *bg_blkno)
1567{
1568 int status;
1569 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001570 u16 bits_left = 0;
1571 u64 hint_blkno = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001572 struct ocfs2_chain_list *cl;
1573 struct ocfs2_dinode *fe;
1574
1575 mlog_entry_void();
1576
1577 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1578 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1579 BUG_ON(!ac->ac_bh);
1580
1581 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001582
1583 /* The bh was validated by the inode read during
1584 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1585 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1586
Mark Fashehccd979b2005-12-15 14:31:24 -08001587 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1588 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001589 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1590 "bits but only %u total.",
1591 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001592 le32_to_cpu(fe->id1.bitmap1.i_used),
1593 le32_to_cpu(fe->id1.bitmap1.i_total));
1594 status = -EIO;
1595 goto bail;
1596 }
1597
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001598 if (hint_blkno) {
1599 /* Attempt to short-circuit the usual search mechanism
1600 * by jumping straight to the most recently used
1601 * allocation group. This helps us mantain some
1602 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001603 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1604 min_bits, bit_off, num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001605 hint_blkno, &bits_left);
1606 if (!status) {
1607 /* Be careful to update *bg_blkno here as the
1608 * caller is expecting it to be filled in, and
1609 * ocfs2_search_one_group() won't do that for
1610 * us. */
1611 *bg_blkno = hint_blkno;
1612 goto set_hint;
1613 }
1614 if (status < 0 && status != -ENOSPC) {
1615 mlog_errno(status);
1616 goto bail;
1617 }
1618 }
1619
Mark Fashehccd979b2005-12-15 14:31:24 -08001620 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1621
1622 victim = ocfs2_find_victim_chain(cl);
1623 ac->ac_chain = victim;
1624 ac->ac_allow_chain_relink = 1;
1625
Mark Fashehda5cbf22006-10-06 18:34:35 -07001626 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001627 num_bits, bg_blkno, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001628 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001629 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001630 if (status < 0 && status != -ENOSPC) {
1631 mlog_errno(status);
1632 goto bail;
1633 }
1634
1635 mlog(0, "Search of victim chain %u came up with nothing, "
1636 "trying all chains now.\n", victim);
1637
1638 /* If we didn't pick a good victim, then just default to
1639 * searching each chain in order. Don't allow chain relinking
1640 * because we only calculate enough journal credits for one
1641 * relink per alloc. */
1642 ac->ac_allow_chain_relink = 0;
1643 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1644 if (i == victim)
1645 continue;
1646 if (!cl->cl_recs[i].c_free)
1647 continue;
1648
1649 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001650 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001651 bit_off, num_bits, bg_blkno,
1652 &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001653 if (!status)
1654 break;
1655 if (status < 0 && status != -ENOSPC) {
1656 mlog_errno(status);
1657 goto bail;
1658 }
1659 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001660
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001661set_hint:
1662 if (status != -ENOSPC) {
1663 /* If the next search of this group is not likely to
1664 * yield a suitable extent, then we reset the last
1665 * group hint so as to not waste a disk read */
1666 if (bits_left < min_bits)
1667 ac->ac_last_group = 0;
1668 else
1669 ac->ac_last_group = *bg_blkno;
1670 }
1671
1672bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001673 mlog_exit(status);
1674 return status;
1675}
1676
1677int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001678 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001679 struct ocfs2_alloc_context *ac,
1680 u32 bits_wanted,
1681 u16 *suballoc_bit_start,
1682 unsigned int *num_bits,
1683 u64 *blkno_start)
1684{
1685 int status;
1686 u64 bg_blkno;
1687
1688 BUG_ON(!ac);
1689 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1690 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001691
1692 status = ocfs2_claim_suballoc_bits(osb,
1693 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001694 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001695 bits_wanted,
1696 1,
1697 suballoc_bit_start,
1698 num_bits,
1699 &bg_blkno);
1700 if (status < 0) {
1701 mlog_errno(status);
1702 goto bail;
1703 }
1704 atomic_inc(&osb->alloc_stats.bg_allocs);
1705
1706 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1707 ac->ac_bits_given += (*num_bits);
1708 status = 0;
1709bail:
1710 mlog_exit(status);
1711 return status;
1712}
1713
Tao Ma13821152009-02-25 00:53:23 +08001714static void ocfs2_init_inode_ac_group(struct inode *dir,
1715 struct buffer_head *parent_fe_bh,
1716 struct ocfs2_alloc_context *ac)
1717{
1718 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1719 /*
1720 * Try to allocate inodes from some specific group.
1721 *
1722 * If the parent dir has recorded the last group used in allocation,
1723 * cool, use it. Otherwise if we try to allocate new inode from the
1724 * same slot the parent dir belongs to, use the same chunk.
1725 *
1726 * We are very careful here to avoid the mistake of setting
1727 * ac_last_group to a group descriptor from a different (unlocked) slot.
1728 */
1729 if (OCFS2_I(dir)->ip_last_used_group &&
1730 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1731 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1732 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1733 ac->ac_last_group = ocfs2_which_suballoc_group(
1734 le64_to_cpu(fe->i_blkno),
1735 le16_to_cpu(fe->i_suballoc_bit));
1736}
1737
1738static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1739 struct ocfs2_alloc_context *ac)
1740{
1741 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1742 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1743}
1744
Mark Fashehccd979b2005-12-15 14:31:24 -08001745int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001746 handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08001747 struct inode *dir,
1748 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001749 struct ocfs2_alloc_context *ac,
1750 u16 *suballoc_bit,
1751 u64 *fe_blkno)
1752{
1753 int status;
1754 unsigned int num_bits;
1755 u64 bg_blkno;
1756
1757 mlog_entry_void();
1758
1759 BUG_ON(!ac);
1760 BUG_ON(ac->ac_bits_given != 0);
1761 BUG_ON(ac->ac_bits_wanted != 1);
1762 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001763
Tao Ma13821152009-02-25 00:53:23 +08001764 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1765
Mark Fashehccd979b2005-12-15 14:31:24 -08001766 status = ocfs2_claim_suballoc_bits(osb,
1767 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001768 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001769 1,
1770 1,
1771 suballoc_bit,
1772 &num_bits,
1773 &bg_blkno);
1774 if (status < 0) {
1775 mlog_errno(status);
1776 goto bail;
1777 }
1778 atomic_inc(&osb->alloc_stats.bg_allocs);
1779
1780 BUG_ON(num_bits != 1);
1781
1782 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1783 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08001784 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001785 status = 0;
1786bail:
1787 mlog_exit(status);
1788 return status;
1789}
1790
1791/* translate a group desc. blkno and it's bitmap offset into
1792 * disk cluster offset. */
1793static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1794 u64 bg_blkno,
1795 u16 bg_bit_off)
1796{
1797 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1798 u32 cluster = 0;
1799
1800 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1801
1802 if (bg_blkno != osb->first_cluster_group_blkno)
1803 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1804 cluster += (u32) bg_bit_off;
1805 return cluster;
1806}
1807
1808/* given a cluster offset, calculate which block group it belongs to
1809 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08001810u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08001811{
1812 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1813 u32 group_no;
1814
1815 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1816
1817 group_no = cluster / osb->bitmap_cpg;
1818 if (!group_no)
1819 return osb->first_cluster_group_blkno;
1820 return ocfs2_clusters_to_blocks(inode->i_sb,
1821 group_no * osb->bitmap_cpg);
1822}
1823
1824/* given the block number of a cluster start, calculate which cluster
1825 * group and descriptor bitmap offset that corresponds to. */
1826static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1827 u64 data_blkno,
1828 u64 *bg_blkno,
1829 u16 *bg_bit_off)
1830{
1831 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1832 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1833
1834 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1835
1836 *bg_blkno = ocfs2_which_cluster_group(inode,
1837 data_cluster);
1838
1839 if (*bg_blkno == osb->first_cluster_group_blkno)
1840 *bg_bit_off = (u16) data_cluster;
1841 else
1842 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1843 data_blkno - *bg_blkno);
1844}
1845
1846/*
1847 * min_bits - minimum contiguous chunk from this total allocation we
1848 * can handle. set to what we asked for originally for a full
1849 * contig. allocation, set to '1' to indicate we can deal with extents
1850 * of any size.
1851 */
Mark Fasheh415cb802007-09-16 20:10:16 -07001852int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1853 handle_t *handle,
1854 struct ocfs2_alloc_context *ac,
1855 u32 min_clusters,
1856 u32 max_clusters,
1857 u32 *cluster_start,
1858 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08001859{
1860 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07001861 unsigned int bits_wanted = max_clusters;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001862 u64 bg_blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001863 u16 bg_bit_off;
1864
1865 mlog_entry_void();
1866
Mark Fashehccd979b2005-12-15 14:31:24 -08001867 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1868
1869 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1870 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001871
1872 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1873 status = ocfs2_claim_local_alloc_bits(osb,
1874 handle,
1875 ac,
1876 bits_wanted,
1877 cluster_start,
1878 num_clusters);
1879 if (!status)
1880 atomic_inc(&osb->alloc_stats.local_data);
1881 } else {
1882 if (min_clusters > (osb->bitmap_cpg - 1)) {
1883 /* The only paths asking for contiguousness
1884 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001885 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1886 "group bitmap size %u!\n", min_clusters,
1887 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08001888 status = -ENOSPC;
1889 goto bail;
1890 }
1891 /* clamp the current request down to a realistic size. */
1892 if (bits_wanted > (osb->bitmap_cpg - 1))
1893 bits_wanted = osb->bitmap_cpg - 1;
1894
1895 status = ocfs2_claim_suballoc_bits(osb,
1896 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001897 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001898 bits_wanted,
1899 min_clusters,
1900 &bg_bit_off,
1901 num_clusters,
1902 &bg_blkno);
1903 if (!status) {
1904 *cluster_start =
1905 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1906 bg_blkno,
1907 bg_bit_off);
1908 atomic_inc(&osb->alloc_stats.bitmap_data);
1909 }
1910 }
1911 if (status < 0) {
1912 if (status != -ENOSPC)
1913 mlog_errno(status);
1914 goto bail;
1915 }
1916
1917 ac->ac_bits_given += *num_clusters;
1918
1919bail:
1920 mlog_exit(status);
1921 return status;
1922}
1923
Mark Fasheh415cb802007-09-16 20:10:16 -07001924int ocfs2_claim_clusters(struct ocfs2_super *osb,
1925 handle_t *handle,
1926 struct ocfs2_alloc_context *ac,
1927 u32 min_clusters,
1928 u32 *cluster_start,
1929 u32 *num_clusters)
1930{
1931 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1932
1933 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1934 bits_wanted, cluster_start, num_clusters);
1935}
1936
Mark Fashehb4414ee2010-03-11 18:31:09 -08001937static int ocfs2_block_group_clear_bits(handle_t *handle,
1938 struct inode *alloc_inode,
1939 struct ocfs2_group_desc *bg,
1940 struct buffer_head *group_bh,
1941 unsigned int bit_off,
1942 unsigned int num_bits,
1943 void (*undo_fn)(unsigned int bit,
1944 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08001945{
1946 int status;
1947 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08001948 struct ocfs2_group_desc *undo_bg = NULL;
1949
1950 mlog_entry_void();
1951
Joel Becker42035302008-11-13 14:49:15 -08001952 /* The caller got this descriptor from
1953 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1954 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001955
1956 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1957
Mark Fashehb4414ee2010-03-11 18:31:09 -08001958 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08001959 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08001960 group_bh,
1961 undo_fn ?
1962 OCFS2_JOURNAL_ACCESS_UNDO :
1963 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001964 if (status < 0) {
1965 mlog_errno(status);
1966 goto bail;
1967 }
1968
Mark Fashehb4414ee2010-03-11 18:31:09 -08001969 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001970 jbd_lock_bh_state(group_bh);
1971 undo_bg = (struct ocfs2_group_desc *)
1972 bh2jh(group_bh)->b_committed_data;
1973 BUG_ON(!undo_bg);
1974 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001975
1976 tmp = num_bits;
1977 while(tmp--) {
1978 ocfs2_clear_bit((bit_off + tmp),
1979 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08001980 if (undo_fn)
1981 undo_fn(bit_off + tmp,
1982 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08001983 }
1984 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1985
Mark Fashehb4414ee2010-03-11 18:31:09 -08001986 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001987 jbd_unlock_bh_state(group_bh);
1988
Joel Beckerec20cec2010-03-19 14:13:52 -07001989 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001990bail:
1991 return status;
1992}
1993
1994/*
1995 * expects the suballoc inode to already be locked.
1996 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08001997static int _ocfs2_free_suballoc_bits(handle_t *handle,
1998 struct inode *alloc_inode,
1999 struct buffer_head *alloc_bh,
2000 unsigned int start_bit,
2001 u64 bg_blkno,
2002 unsigned int count,
2003 void (*undo_fn)(unsigned int bit,
2004 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002005{
2006 int status = 0;
2007 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002008 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2009 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2010 struct buffer_head *group_bh = NULL;
2011 struct ocfs2_group_desc *group;
2012
2013 mlog_entry_void();
2014
Joel Becker10995aa2008-11-13 14:49:12 -08002015 /* The alloc_bh comes from ocfs2_free_dinode() or
2016 * ocfs2_free_clusters(). The callers have all locked the
2017 * allocator and gotten alloc_bh from the lock call. This
2018 * validates the dinode buffer. Any corruption that has happended
2019 * is a code bug. */
2020 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002021 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2022
Mark Fashehb06970532006-03-03 10:24:33 -08002023 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2024 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2025 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002026
Joel Becker68f64d42008-11-13 14:49:14 -08002027 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2028 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002029 if (status < 0) {
2030 mlog_errno(status);
2031 goto bail;
2032 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002033 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002034
Mark Fashehccd979b2005-12-15 14:31:24 -08002035 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2036
2037 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2038 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002039 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002040 if (status < 0) {
2041 mlog_errno(status);
2042 goto bail;
2043 }
2044
Joel Becker0cf2f762009-02-12 16:41:25 -08002045 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2046 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002047 if (status < 0) {
2048 mlog_errno(status);
2049 goto bail;
2050 }
2051
2052 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2053 count);
2054 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2055 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002056 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002057
2058bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002059 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002060
2061 mlog_exit(status);
2062 return status;
2063}
2064
Mark Fashehb4414ee2010-03-11 18:31:09 -08002065int ocfs2_free_suballoc_bits(handle_t *handle,
2066 struct inode *alloc_inode,
2067 struct buffer_head *alloc_bh,
2068 unsigned int start_bit,
2069 u64 bg_blkno,
2070 unsigned int count)
2071{
2072 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2073 start_bit, bg_blkno, count, NULL);
2074}
2075
Mark Fasheh1fabe142006-10-09 18:11:45 -07002076int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002077 struct inode *inode_alloc_inode,
2078 struct buffer_head *inode_alloc_bh,
2079 struct ocfs2_dinode *di)
2080{
2081 u64 blk = le64_to_cpu(di->i_blkno);
2082 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2083 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2084
2085 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2086 inode_alloc_bh, bit, bg_blkno, 1);
2087}
2088
Mark Fashehb4414ee2010-03-11 18:31:09 -08002089static int _ocfs2_free_clusters(handle_t *handle,
2090 struct inode *bitmap_inode,
2091 struct buffer_head *bitmap_bh,
2092 u64 start_blk,
2093 unsigned int num_clusters,
2094 void (*undo_fn)(unsigned int bit,
2095 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002096{
2097 int status;
2098 u16 bg_start_bit;
2099 u64 bg_blkno;
2100 struct ocfs2_dinode *fe;
2101
2102 /* You can't ever have a contiguous set of clusters
2103 * bigger than a block group bitmap so we never have to worry
2104 * about looping on them. */
2105
2106 mlog_entry_void();
2107
2108 /* This is expensive. We can safely remove once this stuff has
2109 * gotten tested really well. */
2110 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2111
2112 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2113
2114 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2115 &bg_start_bit);
2116
Mark Fashehb06970532006-03-03 10:24:33 -08002117 mlog(0, "want to free %u clusters starting at block %llu\n",
2118 num_clusters, (unsigned long long)start_blk);
2119 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2120 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002121
Mark Fashehb4414ee2010-03-11 18:31:09 -08002122 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2123 bg_start_bit, bg_blkno,
2124 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002125 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002126 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002127 goto out;
2128 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002129
Mark Fasheh9c7af402008-07-28 18:02:53 -07002130 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2131 num_clusters);
2132
2133out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002134 mlog_exit(status);
2135 return status;
2136}
2137
Mark Fashehb4414ee2010-03-11 18:31:09 -08002138int ocfs2_free_clusters(handle_t *handle,
2139 struct inode *bitmap_inode,
2140 struct buffer_head *bitmap_bh,
2141 u64 start_blk,
2142 unsigned int num_clusters)
2143{
2144 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2145 start_blk, num_clusters,
2146 _ocfs2_set_bit);
2147}
2148
2149/*
2150 * Give never-used clusters back to the global bitmap. We don't need
2151 * to protect these bits in the undo buffer.
2152 */
2153int ocfs2_release_clusters(handle_t *handle,
2154 struct inode *bitmap_inode,
2155 struct buffer_head *bitmap_bh,
2156 u64 start_blk,
2157 unsigned int num_clusters)
2158{
2159 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2160 start_blk, num_clusters,
2161 _ocfs2_clear_bit);
2162}
2163
Mark Fashehccd979b2005-12-15 14:31:24 -08002164static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2165{
2166 printk("Block Group:\n");
2167 printk("bg_signature: %s\n", bg->bg_signature);
2168 printk("bg_size: %u\n", bg->bg_size);
2169 printk("bg_bits: %u\n", bg->bg_bits);
2170 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2171 printk("bg_chain: %u\n", bg->bg_chain);
2172 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002173 printk("bg_next_group: %llu\n",
2174 (unsigned long long)bg->bg_next_group);
2175 printk("bg_parent_dinode: %llu\n",
2176 (unsigned long long)bg->bg_parent_dinode);
2177 printk("bg_blkno: %llu\n",
2178 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002179}
2180
2181static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2182{
2183 int i;
2184
Mark Fashehb06970532006-03-03 10:24:33 -08002185 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002186 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002187 printk("i_size: %llu\n",
2188 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002189 printk("i_clusters: %u\n", fe->i_clusters);
2190 printk("i_generation: %u\n",
2191 le32_to_cpu(fe->i_generation));
2192 printk("id1.bitmap1.i_used: %u\n",
2193 le32_to_cpu(fe->id1.bitmap1.i_used));
2194 printk("id1.bitmap1.i_total: %u\n",
2195 le32_to_cpu(fe->id1.bitmap1.i_total));
2196 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2197 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2198 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2199 printk("id2.i_chain.cl_next_free_rec: %u\n",
2200 fe->id2.i_chain.cl_next_free_rec);
2201 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2202 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2203 fe->id2.i_chain.cl_recs[i].c_free);
2204 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2205 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002206 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2207 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002208 }
2209}
Tao Mae7d4cb62008-08-18 17:38:44 +08002210
2211/*
2212 * For a given allocation, determine which allocators will need to be
2213 * accessed, and lock them, reserving the appropriate number of bits.
2214 *
2215 * Sparse file systems call this from ocfs2_write_begin_nolock()
2216 * and ocfs2_allocate_unwritten_extents().
2217 *
2218 * File systems which don't support holes call this from
2219 * ocfs2_extend_allocation().
2220 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002221int ocfs2_lock_allocators(struct inode *inode,
2222 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002223 u32 clusters_to_add, u32 extents_to_split,
2224 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002225 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002226{
2227 int ret = 0, num_free_extents;
2228 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2229 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2230
2231 *meta_ac = NULL;
2232 if (data_ac)
2233 *data_ac = NULL;
2234
2235 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2236
Joel Becker3d03a302009-02-12 17:49:26 -08002237 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002238 if (num_free_extents < 0) {
2239 ret = num_free_extents;
2240 mlog_errno(ret);
2241 goto out;
2242 }
2243
2244 /*
2245 * Sparse allocation file systems need to be more conservative
2246 * with reserving room for expansion - the actual allocation
2247 * happens while we've got a journal handle open so re-taking
2248 * a cluster lock (because we ran out of room for another
2249 * extent) will violate ordering rules.
2250 *
2251 * Most of the time we'll only be seeing this 1 cluster at a time
2252 * anyway.
2253 *
2254 * Always lock for any unwritten extents - we might want to
2255 * add blocks during a split.
2256 */
2257 if (!num_free_extents ||
2258 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002259 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002260 if (ret < 0) {
2261 if (ret != -ENOSPC)
2262 mlog_errno(ret);
2263 goto out;
2264 }
2265 }
2266
2267 if (clusters_to_add == 0)
2268 goto out;
2269
2270 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2271 if (ret < 0) {
2272 if (ret != -ENOSPC)
2273 mlog_errno(ret);
2274 goto out;
2275 }
2276
2277out:
2278 if (ret) {
2279 if (*meta_ac) {
2280 ocfs2_free_alloc_context(*meta_ac);
2281 *meta_ac = NULL;
2282 }
2283
2284 /*
2285 * We cannot have an error and a non null *data_ac.
2286 */
2287 }
2288
2289 return ret;
2290}
wengang wang6ca497a2009-03-06 21:29:10 +08002291
2292/*
2293 * Read the inode specified by blkno to get suballoc_slot and
2294 * suballoc_bit.
2295 */
2296static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2297 u16 *suballoc_slot, u16 *suballoc_bit)
2298{
2299 int status;
2300 struct buffer_head *inode_bh = NULL;
2301 struct ocfs2_dinode *inode_fe;
2302
Joel Becker5b09b502009-04-21 16:31:20 -07002303 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002304
2305 /* dirty read disk */
2306 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2307 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002308 mlog(ML_ERROR, "read block %llu failed %d\n",
2309 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002310 goto bail;
2311 }
2312
2313 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2314 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002315 mlog(ML_ERROR, "invalid inode %llu requested\n",
2316 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002317 status = -EINVAL;
2318 goto bail;
2319 }
2320
Tao Ma0fba8132009-03-19 05:08:43 +08002321 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002322 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2323 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002324 (unsigned long long)blkno,
2325 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002326 status = -EINVAL;
2327 goto bail;
2328 }
2329
2330 if (suballoc_slot)
2331 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2332 if (suballoc_bit)
2333 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2334
2335bail:
2336 brelse(inode_bh);
2337
2338 mlog_exit(status);
2339 return status;
2340}
2341
2342/*
2343 * test whether bit is SET in allocator bitmap or not. on success, 0
2344 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2345 * is returned and *res is meaningless. Call this after you have
2346 * cluster locked against suballoc, or you may get a result based on
2347 * non-up2date contents
2348 */
2349static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2350 struct inode *suballoc,
2351 struct buffer_head *alloc_bh, u64 blkno,
2352 u16 bit, int *res)
2353{
2354 struct ocfs2_dinode *alloc_fe;
2355 struct ocfs2_group_desc *group;
2356 struct buffer_head *group_bh = NULL;
2357 u64 bg_blkno;
2358 int status;
2359
Joel Becker5b09b502009-04-21 16:31:20 -07002360 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2361 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002362
2363 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2364 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2365 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2366 (unsigned int)bit,
2367 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2368 status = -EINVAL;
2369 goto bail;
2370 }
2371
2372 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2373 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2374 &group_bh);
2375 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002376 mlog(ML_ERROR, "read group %llu failed %d\n",
2377 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002378 goto bail;
2379 }
2380
2381 group = (struct ocfs2_group_desc *) group_bh->b_data;
2382 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2383
2384bail:
2385 brelse(group_bh);
2386
2387 mlog_exit(status);
2388 return status;
2389}
2390
2391/*
2392 * Test if the bit representing this inode (blkno) is set in the
2393 * suballocator.
2394 *
2395 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2396 *
2397 * In the event of failure, a negative value is returned and *res is
2398 * meaningless.
2399 *
2400 * Callers must make sure to hold nfs_sync_lock to prevent
2401 * ocfs2_delete_inode() on another node from accessing the same
2402 * suballocator concurrently.
2403 */
2404int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2405{
2406 int status;
2407 u16 suballoc_bit = 0, suballoc_slot = 0;
2408 struct inode *inode_alloc_inode;
2409 struct buffer_head *alloc_bh = NULL;
2410
Joel Becker5b09b502009-04-21 16:31:20 -07002411 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002412
2413 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2414 &suballoc_bit);
2415 if (status < 0) {
2416 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2417 goto bail;
2418 }
2419
2420 inode_alloc_inode =
2421 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2422 suballoc_slot);
2423 if (!inode_alloc_inode) {
2424 /* the error code could be inaccurate, but we are not able to
2425 * get the correct one. */
2426 status = -EINVAL;
2427 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2428 (u32)suballoc_slot);
2429 goto bail;
2430 }
2431
2432 mutex_lock(&inode_alloc_inode->i_mutex);
2433 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2434 if (status < 0) {
2435 mutex_unlock(&inode_alloc_inode->i_mutex);
2436 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2437 (u32)suballoc_slot, status);
2438 goto bail;
2439 }
2440
2441 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2442 blkno, suballoc_bit, res);
2443 if (status < 0)
2444 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2445
2446 ocfs2_inode_unlock(inode_alloc_inode, 0);
2447 mutex_unlock(&inode_alloc_inode->i_mutex);
2448
2449 iput(inode_alloc_inode);
2450 brelse(alloc_bh);
2451bail:
2452 mlog_exit(status);
2453 return status;
2454}