blob: 76b37909a4a1e61d5c1ab73339e04978f14f54d3 [file] [log] [blame]
Tao Mad6590722007-12-18 15:47:03 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * resize.c
5 *
6 * volume resize.
7 * Inspired by ext3/resize.c.
8 *
9 * Copyright (C) 2007 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
30#define MLOG_MASK_PREFIX ML_DISK_ALLOC
31#include <cluster/masklog.h>
32
33#include "ocfs2.h"
34
35#include "alloc.h"
36#include "dlmglue.h"
37#include "inode.h"
38#include "journal.h"
39#include "super.h"
40#include "sysfile.h"
41#include "uptodate.h"
42
43#include "buffer_head_io.h"
44#include "suballoc.h"
45#include "resize.h"
46
47/*
48 * Check whether there are new backup superblocks exist
49 * in the last group. If there are some, mark them or clear
50 * them in the bitmap.
51 *
52 * Return how many backups we find in the last group.
53 */
54static u16 ocfs2_calc_new_backup_super(struct inode *inode,
55 struct ocfs2_group_desc *gd,
56 int new_clusters,
57 u32 first_new_cluster,
58 u16 cl_cpg,
59 int set)
60{
61 int i;
62 u16 backups = 0;
63 u32 cluster;
64 u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno);
65
66 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
67 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
68 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
69
70 gd_blkno = ocfs2_which_cluster_group(inode, cluster);
71 if (gd_blkno < lgd_blkno)
72 continue;
73 else if (gd_blkno > lgd_blkno)
74 break;
75
76 if (set)
77 ocfs2_set_bit(cluster % cl_cpg,
78 (unsigned long *)gd->bg_bitmap);
79 else
80 ocfs2_clear_bit(cluster % cl_cpg,
81 (unsigned long *)gd->bg_bitmap);
82 backups++;
83 }
84
Tao Mad6590722007-12-18 15:47:03 +080085 return backups;
86}
87
88static int ocfs2_update_last_group_and_inode(handle_t *handle,
89 struct inode *bm_inode,
90 struct buffer_head *bm_bh,
91 struct buffer_head *group_bh,
92 u32 first_new_cluster,
93 int new_clusters)
94{
95 int ret = 0;
96 struct ocfs2_super *osb = OCFS2_SB(bm_inode->i_sb);
97 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bm_bh->b_data;
98 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
99 struct ocfs2_chain_rec *cr;
100 struct ocfs2_group_desc *group;
101 u16 chain, num_bits, backups = 0;
102 u16 cl_bpc = le16_to_cpu(cl->cl_bpc);
103 u16 cl_cpg = le16_to_cpu(cl->cl_cpg);
104
Tao Maef6b6892011-02-21 11:10:44 +0800105 mlog(0, "(new_clusters=%d, first_new_cluster = %u)\n",
106 new_clusters, first_new_cluster);
Tao Mad6590722007-12-18 15:47:03 +0800107
Joel Becker0cf2f762009-02-12 16:41:25 -0800108 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode),
109 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Mad6590722007-12-18 15:47:03 +0800110 if (ret < 0) {
111 mlog_errno(ret);
112 goto out;
113 }
114
115 group = (struct ocfs2_group_desc *)group_bh->b_data;
116
117 /* update the group first. */
118 num_bits = new_clusters * cl_bpc;
119 le16_add_cpu(&group->bg_bits, num_bits);
120 le16_add_cpu(&group->bg_free_bits_count, num_bits);
121
122 /*
123 * check whether there are some new backup superblocks exist in
124 * this group and update the group bitmap accordingly.
125 */
126 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb,
127 OCFS2_FEATURE_COMPAT_BACKUP_SB)) {
128 backups = ocfs2_calc_new_backup_super(bm_inode,
129 group,
130 new_clusters,
131 first_new_cluster,
132 cl_cpg, 1);
133 le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
134 }
135
Joel Beckerec20cec2010-03-19 14:13:52 -0700136 ocfs2_journal_dirty(handle, group_bh);
Tao Mad6590722007-12-18 15:47:03 +0800137
138 /* update the inode accordingly. */
Joel Becker0cf2f762009-02-12 16:41:25 -0800139 ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700140 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Mad6590722007-12-18 15:47:03 +0800141 if (ret < 0) {
142 mlog_errno(ret);
143 goto out_rollback;
144 }
145
146 chain = le16_to_cpu(group->bg_chain);
147 cr = (&cl->cl_recs[chain]);
148 le32_add_cpu(&cr->c_total, num_bits);
149 le32_add_cpu(&cr->c_free, num_bits);
150 le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits);
151 le32_add_cpu(&fe->i_clusters, new_clusters);
152
153 if (backups) {
154 le32_add_cpu(&cr->c_free, -1 * backups);
155 le32_add_cpu(&fe->id1.bitmap1.i_used, backups);
156 }
157
158 spin_lock(&OCFS2_I(bm_inode)->ip_lock);
159 OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
160 le64_add_cpu(&fe->i_size, new_clusters << osb->s_clustersize_bits);
161 spin_unlock(&OCFS2_I(bm_inode)->ip_lock);
162 i_size_write(bm_inode, le64_to_cpu(fe->i_size));
163
164 ocfs2_journal_dirty(handle, bm_bh);
165
166out_rollback:
167 if (ret < 0) {
168 ocfs2_calc_new_backup_super(bm_inode,
169 group,
170 new_clusters,
171 first_new_cluster,
172 cl_cpg, 0);
173 le16_add_cpu(&group->bg_free_bits_count, backups);
174 le16_add_cpu(&group->bg_bits, -1 * num_bits);
175 le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits);
176 }
177out:
Tao Mac1e8d352011-03-07 16:43:21 +0800178 if (ret)
179 mlog_errno(ret);
Tao Mad6590722007-12-18 15:47:03 +0800180 return ret;
181}
182
183static int update_backups(struct inode * inode, u32 clusters, char *data)
184{
185 int i, ret = 0;
186 u32 cluster;
187 u64 blkno;
188 struct buffer_head *backup = NULL;
189 struct ocfs2_dinode *backup_di = NULL;
190 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
191
192 /* calculate the real backups we need to update. */
193 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
194 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
195 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
196 if (cluster > clusters)
197 break;
198
Joel Beckerda1e9092008-10-09 17:20:29 -0700199 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup);
Tao Mad6590722007-12-18 15:47:03 +0800200 if (ret < 0) {
201 mlog_errno(ret);
202 break;
203 }
204
205 memcpy(backup->b_data, data, inode->i_sb->s_blocksize);
206
207 backup_di = (struct ocfs2_dinode *)backup->b_data;
208 backup_di->i_blkno = cpu_to_le64(blkno);
209
210 ret = ocfs2_write_super_or_backup(osb, backup);
211 brelse(backup);
212 backup = NULL;
213 if (ret < 0) {
214 mlog_errno(ret);
215 break;
216 }
217 }
218
219 return ret;
220}
221
222static void ocfs2_update_super_and_backups(struct inode *inode,
223 int new_clusters)
224{
225 int ret;
226 u32 clusters = 0;
227 struct buffer_head *super_bh = NULL;
228 struct ocfs2_dinode *super_di = NULL;
229 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
230
231 /*
232 * update the superblock last.
233 * It doesn't matter if the write failed.
234 */
Joel Beckerda1e9092008-10-09 17:20:29 -0700235 ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1,
236 &super_bh);
Tao Mad6590722007-12-18 15:47:03 +0800237 if (ret < 0) {
238 mlog_errno(ret);
239 goto out;
240 }
241
242 super_di = (struct ocfs2_dinode *)super_bh->b_data;
243 le32_add_cpu(&super_di->i_clusters, new_clusters);
244 clusters = le32_to_cpu(super_di->i_clusters);
245
246 ret = ocfs2_write_super_or_backup(osb, super_bh);
247 if (ret < 0) {
248 mlog_errno(ret);
249 goto out;
250 }
251
252 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB))
253 ret = update_backups(inode, clusters, super_bh->b_data);
254
255out:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800256 brelse(super_bh);
Tao Mad6590722007-12-18 15:47:03 +0800257 if (ret)
258 printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s"
259 " during fs resize. This condition is not fatal,"
260 " but fsck.ocfs2 should be run to fix it\n",
261 osb->dev_str);
262 return;
263}
264
265/*
266 * Extend the filesystem to the new number of clusters specified. This entry
267 * point is only used to extend the current filesystem to the end of the last
268 * existing group.
269 */
270int ocfs2_group_extend(struct inode * inode, int new_clusters)
271{
272 int ret;
273 handle_t *handle;
274 struct buffer_head *main_bm_bh = NULL;
275 struct buffer_head *group_bh = NULL;
276 struct inode *main_bm_inode = NULL;
277 struct ocfs2_dinode *fe = NULL;
278 struct ocfs2_group_desc *group = NULL;
279 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
280 u16 cl_bpc;
281 u32 first_new_cluster;
282 u64 lgd_blkno;
283
Tao Mad6590722007-12-18 15:47:03 +0800284 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
285 return -EROFS;
286
287 if (new_clusters < 0)
288 return -EINVAL;
289 else if (new_clusters == 0)
290 return 0;
291
292 main_bm_inode = ocfs2_get_system_file_inode(osb,
293 GLOBAL_BITMAP_SYSTEM_INODE,
294 OCFS2_INVALID_SLOT);
295 if (!main_bm_inode) {
296 ret = -EINVAL;
297 mlog_errno(ret);
298 goto out;
299 }
300
301 mutex_lock(&main_bm_inode->i_mutex);
302
303 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
304 if (ret < 0) {
305 mlog_errno(ret);
306 goto out_mutex;
307 }
308
309 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
310
Joel Becker10995aa2008-11-13 14:49:12 -0800311 /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(),
312 * so any corruption is a code bug. */
313 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
314
Tao Mad6590722007-12-18 15:47:03 +0800315 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
Tao Ma85718822010-04-13 14:38:06 +0800316 ocfs2_group_bitmap_size(osb->sb, 0,
317 osb->s_feature_incompat) * 8) {
Tao Mad6590722007-12-18 15:47:03 +0800318 mlog(ML_ERROR, "The disk is too old and small. "
319 "Force to do offline resize.");
320 ret = -EINVAL;
321 goto out_unlock;
322 }
323
Tao Mad6590722007-12-18 15:47:03 +0800324 first_new_cluster = le32_to_cpu(fe->i_clusters);
325 lgd_blkno = ocfs2_which_cluster_group(main_bm_inode,
326 first_new_cluster - 1);
327
Joel Becker68f64d42008-11-13 14:49:14 -0800328 ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno,
329 &group_bh);
Tao Mad6590722007-12-18 15:47:03 +0800330 if (ret < 0) {
331 mlog_errno(ret);
332 goto out_unlock;
333 }
Tao Mad6590722007-12-18 15:47:03 +0800334 group = (struct ocfs2_group_desc *)group_bh->b_data;
335
Tao Mad6590722007-12-18 15:47:03 +0800336 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
337 if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters >
338 le16_to_cpu(fe->id2.i_chain.cl_cpg)) {
339 ret = -EINVAL;
340 goto out_unlock;
341 }
342
343 mlog(0, "extend the last group at %llu, new clusters = %d\n",
Tao Ma7909f2b2007-12-18 15:47:25 +0800344 (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters);
Tao Mad6590722007-12-18 15:47:03 +0800345
346 handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS);
347 if (IS_ERR(handle)) {
348 mlog_errno(PTR_ERR(handle));
349 ret = -EINVAL;
350 goto out_unlock;
351 }
352
353 /* update the last group descriptor and inode. */
354 ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode,
355 main_bm_bh, group_bh,
356 first_new_cluster,
357 new_clusters);
358 if (ret) {
359 mlog_errno(ret);
360 goto out_commit;
361 }
362
363 ocfs2_update_super_and_backups(main_bm_inode, new_clusters);
364
365out_commit:
366 ocfs2_commit_trans(osb, handle);
367out_unlock:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800368 brelse(group_bh);
369 brelse(main_bm_bh);
Tao Mad6590722007-12-18 15:47:03 +0800370
371 ocfs2_inode_unlock(main_bm_inode, 1);
372
373out_mutex:
374 mutex_unlock(&main_bm_inode->i_mutex);
375 iput(main_bm_inode);
376
377out:
Tao Mad6590722007-12-18 15:47:03 +0800378 return ret;
379}
Tao Ma7909f2b2007-12-18 15:47:25 +0800380
381static int ocfs2_check_new_group(struct inode *inode,
382 struct ocfs2_dinode *di,
383 struct ocfs2_new_group_input *input,
384 struct buffer_head *group_bh)
385{
386 int ret;
Joel Becker57e3e792008-11-13 14:49:13 -0800387 struct ocfs2_group_desc *gd =
388 (struct ocfs2_group_desc *)group_bh->b_data;
Tao Ma7909f2b2007-12-18 15:47:25 +0800389 u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc);
Tao Ma7909f2b2007-12-18 15:47:25 +0800390
Joel Becker970e4932008-11-13 14:49:19 -0800391 ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh);
Joel Becker57e3e792008-11-13 14:49:13 -0800392 if (ret)
393 goto out;
Tao Ma7909f2b2007-12-18 15:47:25 +0800394
Joel Becker57e3e792008-11-13 14:49:13 -0800395 ret = -EINVAL;
396 if (le16_to_cpu(gd->bg_chain) != input->chain)
Tao Ma7909f2b2007-12-18 15:47:25 +0800397 mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u "
398 "while input has %u set.\n",
399 (unsigned long long)le64_to_cpu(gd->bg_blkno),
400 le16_to_cpu(gd->bg_chain), input->chain);
401 else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc)
402 mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but "
403 "input has %u clusters set\n",
404 (unsigned long long)le64_to_cpu(gd->bg_blkno),
405 le16_to_cpu(gd->bg_bits), input->clusters);
406 else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc)
407 mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u "
408 "but it should have %u set\n",
409 (unsigned long long)le64_to_cpu(gd->bg_blkno),
410 le16_to_cpu(gd->bg_bits),
411 input->frees * cl_bpc);
412 else
413 ret = 0;
414
Joel Becker57e3e792008-11-13 14:49:13 -0800415out:
Tao Ma7909f2b2007-12-18 15:47:25 +0800416 return ret;
417}
418
419static int ocfs2_verify_group_and_input(struct inode *inode,
420 struct ocfs2_dinode *di,
421 struct ocfs2_new_group_input *input,
422 struct buffer_head *group_bh)
423{
424 u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count);
425 u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
426 u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec);
427 u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group);
428 u32 total_clusters = le32_to_cpu(di->i_clusters);
429 int ret = -EINVAL;
430
431 if (cluster < total_clusters)
432 mlog(ML_ERROR, "add a group which is in the current volume.\n");
433 else if (input->chain >= cl_count)
434 mlog(ML_ERROR, "input chain exceeds the limit.\n");
435 else if (next_free != cl_count && next_free != input->chain)
436 mlog(ML_ERROR,
437 "the add group should be in chain %u\n", next_free);
438 else if (total_clusters + input->clusters < total_clusters)
439 mlog(ML_ERROR, "add group's clusters overflow.\n");
440 else if (input->clusters > cl_cpg)
441 mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n");
442 else if (input->frees > input->clusters)
443 mlog(ML_ERROR, "the free cluster exceeds the total clusters\n");
444 else if (total_clusters % cl_cpg != 0)
445 mlog(ML_ERROR,
446 "the last group isn't full. Use group extend first.\n");
447 else if (input->group != ocfs2_which_cluster_group(inode, cluster))
448 mlog(ML_ERROR, "group blkno is invalid\n");
449 else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh)))
450 mlog(ML_ERROR, "group descriptor check failed.\n");
451 else
452 ret = 0;
453
454 return ret;
455}
456
457/* Add a new group descriptor to global_bitmap. */
458int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
459{
460 int ret;
461 handle_t *handle;
462 struct buffer_head *main_bm_bh = NULL;
463 struct inode *main_bm_inode = NULL;
464 struct ocfs2_dinode *fe = NULL;
465 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
466 struct buffer_head *group_bh = NULL;
467 struct ocfs2_group_desc *group = NULL;
468 struct ocfs2_chain_list *cl;
469 struct ocfs2_chain_rec *cr;
470 u16 cl_bpc;
471
Tao Ma7909f2b2007-12-18 15:47:25 +0800472 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
473 return -EROFS;
474
475 main_bm_inode = ocfs2_get_system_file_inode(osb,
476 GLOBAL_BITMAP_SYSTEM_INODE,
477 OCFS2_INVALID_SLOT);
478 if (!main_bm_inode) {
479 ret = -EINVAL;
480 mlog_errno(ret);
481 goto out;
482 }
483
484 mutex_lock(&main_bm_inode->i_mutex);
485
486 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
487 if (ret < 0) {
488 mlog_errno(ret);
489 goto out_mutex;
490 }
491
492 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
493
494 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
Tao Ma85718822010-04-13 14:38:06 +0800495 ocfs2_group_bitmap_size(osb->sb, 0,
496 osb->s_feature_incompat) * 8) {
Tao Ma7909f2b2007-12-18 15:47:25 +0800497 mlog(ML_ERROR, "The disk is too old and small."
498 " Force to do offline resize.");
499 ret = -EINVAL;
500 goto out_unlock;
501 }
502
Joel Beckerda1e9092008-10-09 17:20:29 -0700503 ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800504 if (ret < 0) {
505 mlog(ML_ERROR, "Can't read the group descriptor # %llu "
506 "from the device.", (unsigned long long)input->group);
507 goto out_unlock;
508 }
509
Joel Becker8cb471e2009-02-10 20:00:41 -0800510 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800511
512 ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh);
513 if (ret) {
514 mlog_errno(ret);
515 goto out_unlock;
516 }
517
518 mlog(0, "Add a new group %llu in chain = %u, length = %u\n",
519 (unsigned long long)input->group, input->chain, input->clusters);
520
521 handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS);
522 if (IS_ERR(handle)) {
523 mlog_errno(PTR_ERR(handle));
524 ret = -EINVAL;
525 goto out_unlock;
526 }
527
528 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
529 cl = &fe->id2.i_chain;
530 cr = &cl->cl_recs[input->chain];
531
Joel Becker0cf2f762009-02-12 16:41:25 -0800532 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode),
533 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma7909f2b2007-12-18 15:47:25 +0800534 if (ret < 0) {
535 mlog_errno(ret);
536 goto out_commit;
537 }
538
539 group = (struct ocfs2_group_desc *)group_bh->b_data;
540 group->bg_next_group = cr->c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -0700541 ocfs2_journal_dirty(handle, group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800542
Joel Becker0cf2f762009-02-12 16:41:25 -0800543 ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode),
544 main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma7909f2b2007-12-18 15:47:25 +0800545 if (ret < 0) {
546 mlog_errno(ret);
547 goto out_commit;
548 }
549
550 if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) {
551 le16_add_cpu(&cl->cl_next_free_rec, 1);
552 memset(cr, 0, sizeof(struct ocfs2_chain_rec));
553 }
554
Tao Ma4338ab62008-03-03 10:53:02 +0800555 cr->c_blkno = cpu_to_le64(input->group);
Tao Ma7909f2b2007-12-18 15:47:25 +0800556 le32_add_cpu(&cr->c_total, input->clusters * cl_bpc);
557 le32_add_cpu(&cr->c_free, input->frees * cl_bpc);
558
559 le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc);
560 le32_add_cpu(&fe->id1.bitmap1.i_used,
561 (input->clusters - input->frees) * cl_bpc);
562 le32_add_cpu(&fe->i_clusters, input->clusters);
563
564 ocfs2_journal_dirty(handle, main_bm_bh);
565
566 spin_lock(&OCFS2_I(main_bm_inode)->ip_lock);
567 OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
568 le64_add_cpu(&fe->i_size, input->clusters << osb->s_clustersize_bits);
569 spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock);
570 i_size_write(main_bm_inode, le64_to_cpu(fe->i_size));
571
572 ocfs2_update_super_and_backups(main_bm_inode, input->clusters);
573
574out_commit:
575 ocfs2_commit_trans(osb, handle);
576out_unlock:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800577 brelse(group_bh);
578 brelse(main_bm_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800579
580 ocfs2_inode_unlock(main_bm_inode, 1);
581
582out_mutex:
583 mutex_unlock(&main_bm_inode->i_mutex);
584 iput(main_bm_inode);
585
586out:
Tao Ma7909f2b2007-12-18 15:47:25 +0800587 return ret;
588}