blob: 761d20da9dcd1c8f9cd8c00875b4740433ce4a30 [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"
Tao Maa5438702011-02-22 08:24:01 +080042#include "ocfs2_trace.h"
Tao Mad6590722007-12-18 15:47:03 +080043
44#include "buffer_head_io.h"
45#include "suballoc.h"
46#include "resize.h"
47
48/*
49 * Check whether there are new backup superblocks exist
50 * in the last group. If there are some, mark them or clear
51 * them in the bitmap.
52 *
53 * Return how many backups we find in the last group.
54 */
55static u16 ocfs2_calc_new_backup_super(struct inode *inode,
56 struct ocfs2_group_desc *gd,
57 int new_clusters,
58 u32 first_new_cluster,
59 u16 cl_cpg,
60 int set)
61{
62 int i;
63 u16 backups = 0;
64 u32 cluster;
65 u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno);
66
67 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
68 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
69 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
70
71 gd_blkno = ocfs2_which_cluster_group(inode, cluster);
72 if (gd_blkno < lgd_blkno)
73 continue;
74 else if (gd_blkno > lgd_blkno)
75 break;
76
77 if (set)
78 ocfs2_set_bit(cluster % cl_cpg,
79 (unsigned long *)gd->bg_bitmap);
80 else
81 ocfs2_clear_bit(cluster % cl_cpg,
82 (unsigned long *)gd->bg_bitmap);
83 backups++;
84 }
85
Tao Mad6590722007-12-18 15:47:03 +080086 return backups;
87}
88
89static int ocfs2_update_last_group_and_inode(handle_t *handle,
90 struct inode *bm_inode,
91 struct buffer_head *bm_bh,
92 struct buffer_head *group_bh,
93 u32 first_new_cluster,
94 int new_clusters)
95{
96 int ret = 0;
97 struct ocfs2_super *osb = OCFS2_SB(bm_inode->i_sb);
98 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bm_bh->b_data;
99 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
100 struct ocfs2_chain_rec *cr;
101 struct ocfs2_group_desc *group;
102 u16 chain, num_bits, backups = 0;
103 u16 cl_bpc = le16_to_cpu(cl->cl_bpc);
104 u16 cl_cpg = le16_to_cpu(cl->cl_cpg);
105
Tao Maa5438702011-02-22 08:24:01 +0800106 trace_ocfs2_update_last_group_and_inode(new_clusters,
107 first_new_cluster);
Tao Mad6590722007-12-18 15:47:03 +0800108
Joel Becker0cf2f762009-02-12 16:41:25 -0800109 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode),
110 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Mad6590722007-12-18 15:47:03 +0800111 if (ret < 0) {
112 mlog_errno(ret);
113 goto out;
114 }
115
116 group = (struct ocfs2_group_desc *)group_bh->b_data;
117
118 /* update the group first. */
119 num_bits = new_clusters * cl_bpc;
120 le16_add_cpu(&group->bg_bits, num_bits);
121 le16_add_cpu(&group->bg_free_bits_count, num_bits);
122
123 /*
124 * check whether there are some new backup superblocks exist in
125 * this group and update the group bitmap accordingly.
126 */
127 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb,
128 OCFS2_FEATURE_COMPAT_BACKUP_SB)) {
129 backups = ocfs2_calc_new_backup_super(bm_inode,
130 group,
131 new_clusters,
132 first_new_cluster,
133 cl_cpg, 1);
134 le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
135 }
136
Joel Beckerec20cec2010-03-19 14:13:52 -0700137 ocfs2_journal_dirty(handle, group_bh);
Tao Mad6590722007-12-18 15:47:03 +0800138
139 /* update the inode accordingly. */
Joel Becker0cf2f762009-02-12 16:41:25 -0800140 ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700141 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Mad6590722007-12-18 15:47:03 +0800142 if (ret < 0) {
143 mlog_errno(ret);
144 goto out_rollback;
145 }
146
147 chain = le16_to_cpu(group->bg_chain);
148 cr = (&cl->cl_recs[chain]);
149 le32_add_cpu(&cr->c_total, num_bits);
150 le32_add_cpu(&cr->c_free, num_bits);
151 le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits);
152 le32_add_cpu(&fe->i_clusters, new_clusters);
153
154 if (backups) {
155 le32_add_cpu(&cr->c_free, -1 * backups);
156 le32_add_cpu(&fe->id1.bitmap1.i_used, backups);
157 }
158
159 spin_lock(&OCFS2_I(bm_inode)->ip_lock);
160 OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
161 le64_add_cpu(&fe->i_size, new_clusters << osb->s_clustersize_bits);
162 spin_unlock(&OCFS2_I(bm_inode)->ip_lock);
163 i_size_write(bm_inode, le64_to_cpu(fe->i_size));
164
165 ocfs2_journal_dirty(handle, bm_bh);
166
167out_rollback:
168 if (ret < 0) {
169 ocfs2_calc_new_backup_super(bm_inode,
170 group,
171 new_clusters,
172 first_new_cluster,
173 cl_cpg, 0);
174 le16_add_cpu(&group->bg_free_bits_count, backups);
175 le16_add_cpu(&group->bg_bits, -1 * num_bits);
176 le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits);
177 }
178out:
Tao Mac1e8d352011-03-07 16:43:21 +0800179 if (ret)
180 mlog_errno(ret);
Tao Mad6590722007-12-18 15:47:03 +0800181 return ret;
182}
183
184static int update_backups(struct inode * inode, u32 clusters, char *data)
185{
186 int i, ret = 0;
187 u32 cluster;
188 u64 blkno;
189 struct buffer_head *backup = NULL;
190 struct ocfs2_dinode *backup_di = NULL;
191 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
192
193 /* calculate the real backups we need to update. */
194 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
195 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
196 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
197 if (cluster > clusters)
198 break;
199
Joel Beckerda1e9092008-10-09 17:20:29 -0700200 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup);
Tao Mad6590722007-12-18 15:47:03 +0800201 if (ret < 0) {
202 mlog_errno(ret);
203 break;
204 }
205
206 memcpy(backup->b_data, data, inode->i_sb->s_blocksize);
207
208 backup_di = (struct ocfs2_dinode *)backup->b_data;
209 backup_di->i_blkno = cpu_to_le64(blkno);
210
211 ret = ocfs2_write_super_or_backup(osb, backup);
212 brelse(backup);
213 backup = NULL;
214 if (ret < 0) {
215 mlog_errno(ret);
216 break;
217 }
218 }
219
220 return ret;
221}
222
223static void ocfs2_update_super_and_backups(struct inode *inode,
224 int new_clusters)
225{
226 int ret;
227 u32 clusters = 0;
228 struct buffer_head *super_bh = NULL;
229 struct ocfs2_dinode *super_di = NULL;
230 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
231
232 /*
233 * update the superblock last.
234 * It doesn't matter if the write failed.
235 */
Joel Beckerda1e9092008-10-09 17:20:29 -0700236 ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1,
237 &super_bh);
Tao Mad6590722007-12-18 15:47:03 +0800238 if (ret < 0) {
239 mlog_errno(ret);
240 goto out;
241 }
242
243 super_di = (struct ocfs2_dinode *)super_bh->b_data;
244 le32_add_cpu(&super_di->i_clusters, new_clusters);
245 clusters = le32_to_cpu(super_di->i_clusters);
246
247 ret = ocfs2_write_super_or_backup(osb, super_bh);
248 if (ret < 0) {
249 mlog_errno(ret);
250 goto out;
251 }
252
253 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB))
254 ret = update_backups(inode, clusters, super_bh->b_data);
255
256out:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800257 brelse(super_bh);
Tao Mad6590722007-12-18 15:47:03 +0800258 if (ret)
259 printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s"
260 " during fs resize. This condition is not fatal,"
261 " but fsck.ocfs2 should be run to fix it\n",
262 osb->dev_str);
263 return;
264}
265
266/*
267 * Extend the filesystem to the new number of clusters specified. This entry
268 * point is only used to extend the current filesystem to the end of the last
269 * existing group.
270 */
271int ocfs2_group_extend(struct inode * inode, int new_clusters)
272{
273 int ret;
274 handle_t *handle;
275 struct buffer_head *main_bm_bh = NULL;
276 struct buffer_head *group_bh = NULL;
277 struct inode *main_bm_inode = NULL;
278 struct ocfs2_dinode *fe = NULL;
279 struct ocfs2_group_desc *group = NULL;
280 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
281 u16 cl_bpc;
282 u32 first_new_cluster;
283 u64 lgd_blkno;
284
Tao Mad6590722007-12-18 15:47:03 +0800285 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
286 return -EROFS;
287
288 if (new_clusters < 0)
289 return -EINVAL;
290 else if (new_clusters == 0)
291 return 0;
292
293 main_bm_inode = ocfs2_get_system_file_inode(osb,
294 GLOBAL_BITMAP_SYSTEM_INODE,
295 OCFS2_INVALID_SLOT);
296 if (!main_bm_inode) {
297 ret = -EINVAL;
298 mlog_errno(ret);
299 goto out;
300 }
301
302 mutex_lock(&main_bm_inode->i_mutex);
303
304 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
305 if (ret < 0) {
306 mlog_errno(ret);
307 goto out_mutex;
308 }
309
310 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
311
Joel Becker10995aa2008-11-13 14:49:12 -0800312 /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(),
313 * so any corruption is a code bug. */
314 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
315
Tao Mad6590722007-12-18 15:47:03 +0800316 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
Tao Ma85718822010-04-13 14:38:06 +0800317 ocfs2_group_bitmap_size(osb->sb, 0,
318 osb->s_feature_incompat) * 8) {
Tao Mad6590722007-12-18 15:47:03 +0800319 mlog(ML_ERROR, "The disk is too old and small. "
320 "Force to do offline resize.");
321 ret = -EINVAL;
322 goto out_unlock;
323 }
324
Tao Mad6590722007-12-18 15:47:03 +0800325 first_new_cluster = le32_to_cpu(fe->i_clusters);
326 lgd_blkno = ocfs2_which_cluster_group(main_bm_inode,
327 first_new_cluster - 1);
328
Joel Becker68f64d42008-11-13 14:49:14 -0800329 ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno,
330 &group_bh);
Tao Mad6590722007-12-18 15:47:03 +0800331 if (ret < 0) {
332 mlog_errno(ret);
333 goto out_unlock;
334 }
Tao Mad6590722007-12-18 15:47:03 +0800335 group = (struct ocfs2_group_desc *)group_bh->b_data;
336
Tao Mad6590722007-12-18 15:47:03 +0800337 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
338 if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters >
339 le16_to_cpu(fe->id2.i_chain.cl_cpg)) {
340 ret = -EINVAL;
341 goto out_unlock;
342 }
343
Tao Maa5438702011-02-22 08:24:01 +0800344
345 trace_ocfs2_group_extend(
Tao Ma7909f2b2007-12-18 15:47:25 +0800346 (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters);
Tao Mad6590722007-12-18 15:47:03 +0800347
348 handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS);
349 if (IS_ERR(handle)) {
350 mlog_errno(PTR_ERR(handle));
351 ret = -EINVAL;
352 goto out_unlock;
353 }
354
355 /* update the last group descriptor and inode. */
356 ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode,
357 main_bm_bh, group_bh,
358 first_new_cluster,
359 new_clusters);
360 if (ret) {
361 mlog_errno(ret);
362 goto out_commit;
363 }
364
365 ocfs2_update_super_and_backups(main_bm_inode, new_clusters);
366
367out_commit:
368 ocfs2_commit_trans(osb, handle);
369out_unlock:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800370 brelse(group_bh);
371 brelse(main_bm_bh);
Tao Mad6590722007-12-18 15:47:03 +0800372
373 ocfs2_inode_unlock(main_bm_inode, 1);
374
375out_mutex:
376 mutex_unlock(&main_bm_inode->i_mutex);
377 iput(main_bm_inode);
378
379out:
Tao Mad6590722007-12-18 15:47:03 +0800380 return ret;
381}
Tao Ma7909f2b2007-12-18 15:47:25 +0800382
383static int ocfs2_check_new_group(struct inode *inode,
384 struct ocfs2_dinode *di,
385 struct ocfs2_new_group_input *input,
386 struct buffer_head *group_bh)
387{
388 int ret;
Joel Becker57e3e792008-11-13 14:49:13 -0800389 struct ocfs2_group_desc *gd =
390 (struct ocfs2_group_desc *)group_bh->b_data;
Tao Ma7909f2b2007-12-18 15:47:25 +0800391 u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc);
Tao Ma7909f2b2007-12-18 15:47:25 +0800392
Joel Becker970e4932008-11-13 14:49:19 -0800393 ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh);
Joel Becker57e3e792008-11-13 14:49:13 -0800394 if (ret)
395 goto out;
Tao Ma7909f2b2007-12-18 15:47:25 +0800396
Joel Becker57e3e792008-11-13 14:49:13 -0800397 ret = -EINVAL;
398 if (le16_to_cpu(gd->bg_chain) != input->chain)
Tao Ma7909f2b2007-12-18 15:47:25 +0800399 mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u "
400 "while input has %u set.\n",
401 (unsigned long long)le64_to_cpu(gd->bg_blkno),
402 le16_to_cpu(gd->bg_chain), input->chain);
403 else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc)
404 mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but "
405 "input has %u clusters set\n",
406 (unsigned long long)le64_to_cpu(gd->bg_blkno),
407 le16_to_cpu(gd->bg_bits), input->clusters);
408 else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc)
409 mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u "
410 "but it should have %u set\n",
411 (unsigned long long)le64_to_cpu(gd->bg_blkno),
412 le16_to_cpu(gd->bg_bits),
413 input->frees * cl_bpc);
414 else
415 ret = 0;
416
Joel Becker57e3e792008-11-13 14:49:13 -0800417out:
Tao Ma7909f2b2007-12-18 15:47:25 +0800418 return ret;
419}
420
421static int ocfs2_verify_group_and_input(struct inode *inode,
422 struct ocfs2_dinode *di,
423 struct ocfs2_new_group_input *input,
424 struct buffer_head *group_bh)
425{
426 u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count);
427 u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
428 u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec);
429 u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group);
430 u32 total_clusters = le32_to_cpu(di->i_clusters);
431 int ret = -EINVAL;
432
433 if (cluster < total_clusters)
434 mlog(ML_ERROR, "add a group which is in the current volume.\n");
435 else if (input->chain >= cl_count)
436 mlog(ML_ERROR, "input chain exceeds the limit.\n");
437 else if (next_free != cl_count && next_free != input->chain)
438 mlog(ML_ERROR,
439 "the add group should be in chain %u\n", next_free);
440 else if (total_clusters + input->clusters < total_clusters)
441 mlog(ML_ERROR, "add group's clusters overflow.\n");
442 else if (input->clusters > cl_cpg)
443 mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n");
444 else if (input->frees > input->clusters)
445 mlog(ML_ERROR, "the free cluster exceeds the total clusters\n");
446 else if (total_clusters % cl_cpg != 0)
447 mlog(ML_ERROR,
448 "the last group isn't full. Use group extend first.\n");
449 else if (input->group != ocfs2_which_cluster_group(inode, cluster))
450 mlog(ML_ERROR, "group blkno is invalid\n");
451 else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh)))
452 mlog(ML_ERROR, "group descriptor check failed.\n");
453 else
454 ret = 0;
455
456 return ret;
457}
458
459/* Add a new group descriptor to global_bitmap. */
460int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
461{
462 int ret;
463 handle_t *handle;
464 struct buffer_head *main_bm_bh = NULL;
465 struct inode *main_bm_inode = NULL;
466 struct ocfs2_dinode *fe = NULL;
467 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
468 struct buffer_head *group_bh = NULL;
469 struct ocfs2_group_desc *group = NULL;
470 struct ocfs2_chain_list *cl;
471 struct ocfs2_chain_rec *cr;
472 u16 cl_bpc;
473
Tao Ma7909f2b2007-12-18 15:47:25 +0800474 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
475 return -EROFS;
476
477 main_bm_inode = ocfs2_get_system_file_inode(osb,
478 GLOBAL_BITMAP_SYSTEM_INODE,
479 OCFS2_INVALID_SLOT);
480 if (!main_bm_inode) {
481 ret = -EINVAL;
482 mlog_errno(ret);
483 goto out;
484 }
485
486 mutex_lock(&main_bm_inode->i_mutex);
487
488 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
489 if (ret < 0) {
490 mlog_errno(ret);
491 goto out_mutex;
492 }
493
494 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
495
496 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
Tao Ma85718822010-04-13 14:38:06 +0800497 ocfs2_group_bitmap_size(osb->sb, 0,
498 osb->s_feature_incompat) * 8) {
Tao Ma7909f2b2007-12-18 15:47:25 +0800499 mlog(ML_ERROR, "The disk is too old and small."
500 " Force to do offline resize.");
501 ret = -EINVAL;
502 goto out_unlock;
503 }
504
Joel Beckerda1e9092008-10-09 17:20:29 -0700505 ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800506 if (ret < 0) {
507 mlog(ML_ERROR, "Can't read the group descriptor # %llu "
508 "from the device.", (unsigned long long)input->group);
509 goto out_unlock;
510 }
511
Joel Becker8cb471e2009-02-10 20:00:41 -0800512 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800513
514 ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh);
515 if (ret) {
516 mlog_errno(ret);
517 goto out_unlock;
518 }
519
Tao Maa5438702011-02-22 08:24:01 +0800520 trace_ocfs2_group_add((unsigned long long)input->group,
521 input->chain, input->clusters, input->frees);
Tao Ma7909f2b2007-12-18 15:47:25 +0800522
523 handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS);
524 if (IS_ERR(handle)) {
525 mlog_errno(PTR_ERR(handle));
526 ret = -EINVAL;
527 goto out_unlock;
528 }
529
530 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
531 cl = &fe->id2.i_chain;
532 cr = &cl->cl_recs[input->chain];
533
Joel Becker0cf2f762009-02-12 16:41:25 -0800534 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode),
535 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma7909f2b2007-12-18 15:47:25 +0800536 if (ret < 0) {
537 mlog_errno(ret);
538 goto out_commit;
539 }
540
541 group = (struct ocfs2_group_desc *)group_bh->b_data;
542 group->bg_next_group = cr->c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -0700543 ocfs2_journal_dirty(handle, group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800544
Joel Becker0cf2f762009-02-12 16:41:25 -0800545 ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode),
546 main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma7909f2b2007-12-18 15:47:25 +0800547 if (ret < 0) {
548 mlog_errno(ret);
549 goto out_commit;
550 }
551
552 if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) {
553 le16_add_cpu(&cl->cl_next_free_rec, 1);
554 memset(cr, 0, sizeof(struct ocfs2_chain_rec));
555 }
556
Tao Ma4338ab62008-03-03 10:53:02 +0800557 cr->c_blkno = cpu_to_le64(input->group);
Tao Ma7909f2b2007-12-18 15:47:25 +0800558 le32_add_cpu(&cr->c_total, input->clusters * cl_bpc);
559 le32_add_cpu(&cr->c_free, input->frees * cl_bpc);
560
561 le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc);
562 le32_add_cpu(&fe->id1.bitmap1.i_used,
563 (input->clusters - input->frees) * cl_bpc);
564 le32_add_cpu(&fe->i_clusters, input->clusters);
565
566 ocfs2_journal_dirty(handle, main_bm_bh);
567
568 spin_lock(&OCFS2_I(main_bm_inode)->ip_lock);
569 OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
570 le64_add_cpu(&fe->i_size, input->clusters << osb->s_clustersize_bits);
571 spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock);
572 i_size_write(main_bm_inode, le64_to_cpu(fe->i_size));
573
574 ocfs2_update_super_and_backups(main_bm_inode, input->clusters);
575
576out_commit:
577 ocfs2_commit_trans(osb, handle);
578out_unlock:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800579 brelse(group_bh);
580 brelse(main_bm_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800581
582 ocfs2_inode_unlock(main_bm_inode, 1);
583
584out_mutex:
585 mutex_unlock(&main_bm_inode->i_mutex);
586 iput(main_bm_inode);
587
588out:
Tao Ma7909f2b2007-12-18 15:47:25 +0800589 return ret;
590}