blob: 18451e0fab8192628412db781eae9e1fad07247b [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
Tao Mad6590722007-12-18 15:47:03 +080030#include <cluster/masklog.h>
31
32#include "ocfs2.h"
33
34#include "alloc.h"
35#include "dlmglue.h"
36#include "inode.h"
37#include "journal.h"
38#include "super.h"
39#include "sysfile.h"
40#include "uptodate.h"
Tao Maa5438702011-02-22 08:24:01 +080041#include "ocfs2_trace.h"
Tao Mad6590722007-12-18 15:47:03 +080042
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,
Tao Mad6590722007-12-18 15:47:03 +080056 u16 cl_cpg,
Joseph Qi5c9ee4c2015-12-29 14:54:06 -080057 u16 old_bg_clusters,
Tao Mad6590722007-12-18 15:47:03 +080058 int set)
59{
60 int i;
61 u16 backups = 0;
Joseph Qi5c9ee4c2015-12-29 14:54:06 -080062 u32 cluster, lgd_cluster;
Tao Mad6590722007-12-18 15:47:03 +080063 u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno);
64
65 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
66 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
67 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
68
69 gd_blkno = ocfs2_which_cluster_group(inode, cluster);
70 if (gd_blkno < lgd_blkno)
71 continue;
72 else if (gd_blkno > lgd_blkno)
73 break;
74
Joseph Qi5c9ee4c2015-12-29 14:54:06 -080075 /* check if already done backup super */
76 lgd_cluster = ocfs2_blocks_to_clusters(inode->i_sb, lgd_blkno);
77 lgd_cluster += old_bg_clusters;
78 if (lgd_cluster >= cluster)
79 continue;
80
Tao Mad6590722007-12-18 15:47:03 +080081 if (set)
82 ocfs2_set_bit(cluster % cl_cpg,
83 (unsigned long *)gd->bg_bitmap);
84 else
85 ocfs2_clear_bit(cluster % cl_cpg,
86 (unsigned long *)gd->bg_bitmap);
87 backups++;
88 }
89
Tao Mad6590722007-12-18 15:47:03 +080090 return backups;
91}
92
93static int ocfs2_update_last_group_and_inode(handle_t *handle,
94 struct inode *bm_inode,
95 struct buffer_head *bm_bh,
96 struct buffer_head *group_bh,
97 u32 first_new_cluster,
98 int new_clusters)
99{
100 int ret = 0;
101 struct ocfs2_super *osb = OCFS2_SB(bm_inode->i_sb);
102 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bm_bh->b_data;
103 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
104 struct ocfs2_chain_rec *cr;
105 struct ocfs2_group_desc *group;
106 u16 chain, num_bits, backups = 0;
107 u16 cl_bpc = le16_to_cpu(cl->cl_bpc);
108 u16 cl_cpg = le16_to_cpu(cl->cl_cpg);
Joseph Qi5c9ee4c2015-12-29 14:54:06 -0800109 u16 old_bg_clusters;
Tao Mad6590722007-12-18 15:47:03 +0800110
Tao Maa5438702011-02-22 08:24:01 +0800111 trace_ocfs2_update_last_group_and_inode(new_clusters,
112 first_new_cluster);
Tao Mad6590722007-12-18 15:47:03 +0800113
Joel Becker0cf2f762009-02-12 16:41:25 -0800114 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode),
115 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Mad6590722007-12-18 15:47:03 +0800116 if (ret < 0) {
117 mlog_errno(ret);
118 goto out;
119 }
120
121 group = (struct ocfs2_group_desc *)group_bh->b_data;
122
Joseph Qi5c9ee4c2015-12-29 14:54:06 -0800123 old_bg_clusters = le16_to_cpu(group->bg_bits) / cl_bpc;
Tao Mad6590722007-12-18 15:47:03 +0800124 /* update the group first. */
125 num_bits = new_clusters * cl_bpc;
126 le16_add_cpu(&group->bg_bits, num_bits);
127 le16_add_cpu(&group->bg_free_bits_count, num_bits);
128
129 /*
130 * check whether there are some new backup superblocks exist in
131 * this group and update the group bitmap accordingly.
132 */
133 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb,
134 OCFS2_FEATURE_COMPAT_BACKUP_SB)) {
135 backups = ocfs2_calc_new_backup_super(bm_inode,
136 group,
Joseph Qi5c9ee4c2015-12-29 14:54:06 -0800137 cl_cpg, old_bg_clusters, 1);
Tao Mad6590722007-12-18 15:47:03 +0800138 le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
139 }
140
Joel Beckerec20cec2010-03-19 14:13:52 -0700141 ocfs2_journal_dirty(handle, group_bh);
Tao Mad6590722007-12-18 15:47:03 +0800142
143 /* update the inode accordingly. */
Joel Becker0cf2f762009-02-12 16:41:25 -0800144 ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700145 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Mad6590722007-12-18 15:47:03 +0800146 if (ret < 0) {
147 mlog_errno(ret);
148 goto out_rollback;
149 }
150
151 chain = le16_to_cpu(group->bg_chain);
152 cr = (&cl->cl_recs[chain]);
153 le32_add_cpu(&cr->c_total, num_bits);
154 le32_add_cpu(&cr->c_free, num_bits);
155 le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits);
156 le32_add_cpu(&fe->i_clusters, new_clusters);
157
158 if (backups) {
159 le32_add_cpu(&cr->c_free, -1 * backups);
160 le32_add_cpu(&fe->id1.bitmap1.i_used, backups);
161 }
162
163 spin_lock(&OCFS2_I(bm_inode)->ip_lock);
164 OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
Joseph Qi17bf1412014-06-04 16:06:17 -0700165 le64_add_cpu(&fe->i_size, (u64)new_clusters << osb->s_clustersize_bits);
Tao Mad6590722007-12-18 15:47:03 +0800166 spin_unlock(&OCFS2_I(bm_inode)->ip_lock);
167 i_size_write(bm_inode, le64_to_cpu(fe->i_size));
168
169 ocfs2_journal_dirty(handle, bm_bh);
170
171out_rollback:
172 if (ret < 0) {
173 ocfs2_calc_new_backup_super(bm_inode,
174 group,
Joseph Qi5c9ee4c2015-12-29 14:54:06 -0800175 cl_cpg, old_bg_clusters, 0);
Tao Mad6590722007-12-18 15:47:03 +0800176 le16_add_cpu(&group->bg_free_bits_count, backups);
177 le16_add_cpu(&group->bg_bits, -1 * num_bits);
178 le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits);
179 }
180out:
Tao Mac1e8d352011-03-07 16:43:21 +0800181 if (ret)
182 mlog_errno(ret);
Tao Mad6590722007-12-18 15:47:03 +0800183 return ret;
184}
185
186static int update_backups(struct inode * inode, u32 clusters, char *data)
187{
188 int i, ret = 0;
189 u32 cluster;
190 u64 blkno;
191 struct buffer_head *backup = NULL;
192 struct ocfs2_dinode *backup_di = NULL;
193 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
194
195 /* calculate the real backups we need to update. */
196 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
197 blkno = ocfs2_backup_super_blkno(inode->i_sb, i);
198 cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
jiangyiwen584dca32016-03-25 14:21:35 -0700199 if (cluster >= clusters)
Tao Mad6590722007-12-18 15:47:03 +0800200 break;
201
Joel Beckerda1e9092008-10-09 17:20:29 -0700202 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup);
Tao Mad6590722007-12-18 15:47:03 +0800203 if (ret < 0) {
204 mlog_errno(ret);
205 break;
206 }
207
208 memcpy(backup->b_data, data, inode->i_sb->s_blocksize);
209
210 backup_di = (struct ocfs2_dinode *)backup->b_data;
211 backup_di->i_blkno = cpu_to_le64(blkno);
212
213 ret = ocfs2_write_super_or_backup(osb, backup);
214 brelse(backup);
215 backup = NULL;
216 if (ret < 0) {
217 mlog_errno(ret);
218 break;
219 }
220 }
221
222 return ret;
223}
224
225static void ocfs2_update_super_and_backups(struct inode *inode,
226 int new_clusters)
227{
228 int ret;
229 u32 clusters = 0;
230 struct buffer_head *super_bh = NULL;
231 struct ocfs2_dinode *super_di = NULL;
232 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
233
234 /*
235 * update the superblock last.
236 * It doesn't matter if the write failed.
237 */
Joel Beckerda1e9092008-10-09 17:20:29 -0700238 ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1,
239 &super_bh);
Tao Mad6590722007-12-18 15:47:03 +0800240 if (ret < 0) {
241 mlog_errno(ret);
242 goto out;
243 }
244
245 super_di = (struct ocfs2_dinode *)super_bh->b_data;
246 le32_add_cpu(&super_di->i_clusters, new_clusters);
247 clusters = le32_to_cpu(super_di->i_clusters);
248
249 ret = ocfs2_write_super_or_backup(osb, super_bh);
250 if (ret < 0) {
251 mlog_errno(ret);
252 goto out;
253 }
254
255 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB))
256 ret = update_backups(inode, clusters, super_bh->b_data);
257
258out:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800259 brelse(super_bh);
Tao Mad6590722007-12-18 15:47:03 +0800260 if (ret)
261 printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s"
262 " during fs resize. This condition is not fatal,"
263 " but fsck.ocfs2 should be run to fix it\n",
264 osb->dev_str);
265 return;
266}
267
268/*
269 * Extend the filesystem to the new number of clusters specified. This entry
270 * point is only used to extend the current filesystem to the end of the last
271 * existing group.
272 */
273int ocfs2_group_extend(struct inode * inode, int new_clusters)
274{
275 int ret;
276 handle_t *handle;
277 struct buffer_head *main_bm_bh = NULL;
278 struct buffer_head *group_bh = NULL;
279 struct inode *main_bm_inode = NULL;
280 struct ocfs2_dinode *fe = NULL;
281 struct ocfs2_group_desc *group = NULL;
282 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
283 u16 cl_bpc;
284 u32 first_new_cluster;
285 u64 lgd_blkno;
286
Tao Mad6590722007-12-18 15:47:03 +0800287 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
288 return -EROFS;
289
290 if (new_clusters < 0)
291 return -EINVAL;
292 else if (new_clusters == 0)
293 return 0;
294
295 main_bm_inode = ocfs2_get_system_file_inode(osb,
296 GLOBAL_BITMAP_SYSTEM_INODE,
297 OCFS2_INVALID_SLOT);
298 if (!main_bm_inode) {
299 ret = -EINVAL;
300 mlog_errno(ret);
301 goto out;
302 }
303
Al Viro59551022016-01-22 15:40:57 -0500304 inode_lock(main_bm_inode);
Tao Mad6590722007-12-18 15:47:03 +0800305
306 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
307 if (ret < 0) {
308 mlog_errno(ret);
309 goto out_mutex;
310 }
311
312 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
313
Joel Becker10995aa2008-11-13 14:49:12 -0800314 /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(),
315 * so any corruption is a code bug. */
316 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
317
Tao Mad6590722007-12-18 15:47:03 +0800318 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
Tao Ma85718822010-04-13 14:38:06 +0800319 ocfs2_group_bitmap_size(osb->sb, 0,
320 osb->s_feature_incompat) * 8) {
Tao Mad6590722007-12-18 15:47:03 +0800321 mlog(ML_ERROR, "The disk is too old and small. "
322 "Force to do offline resize.");
323 ret = -EINVAL;
324 goto out_unlock;
325 }
326
Tao Mad6590722007-12-18 15:47:03 +0800327 first_new_cluster = le32_to_cpu(fe->i_clusters);
328 lgd_blkno = ocfs2_which_cluster_group(main_bm_inode,
329 first_new_cluster - 1);
330
Joel Becker68f64d42008-11-13 14:49:14 -0800331 ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno,
332 &group_bh);
Tao Mad6590722007-12-18 15:47:03 +0800333 if (ret < 0) {
334 mlog_errno(ret);
335 goto out_unlock;
336 }
Tao Mad6590722007-12-18 15:47:03 +0800337 group = (struct ocfs2_group_desc *)group_bh->b_data;
338
Tao Mad6590722007-12-18 15:47:03 +0800339 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
340 if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters >
341 le16_to_cpu(fe->id2.i_chain.cl_cpg)) {
342 ret = -EINVAL;
343 goto out_unlock;
344 }
345
Tao Maa5438702011-02-22 08:24:01 +0800346
347 trace_ocfs2_group_extend(
Tao Ma7909f2b2007-12-18 15:47:25 +0800348 (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters);
Tao Mad6590722007-12-18 15:47:03 +0800349
350 handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS);
351 if (IS_ERR(handle)) {
352 mlog_errno(PTR_ERR(handle));
353 ret = -EINVAL;
354 goto out_unlock;
355 }
356
357 /* update the last group descriptor and inode. */
358 ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode,
359 main_bm_bh, group_bh,
360 first_new_cluster,
361 new_clusters);
362 if (ret) {
363 mlog_errno(ret);
364 goto out_commit;
365 }
366
367 ocfs2_update_super_and_backups(main_bm_inode, new_clusters);
368
369out_commit:
370 ocfs2_commit_trans(osb, handle);
371out_unlock:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800372 brelse(group_bh);
373 brelse(main_bm_bh);
Tao Mad6590722007-12-18 15:47:03 +0800374
375 ocfs2_inode_unlock(main_bm_inode, 1);
376
377out_mutex:
Al Viro59551022016-01-22 15:40:57 -0500378 inode_unlock(main_bm_inode);
Tao Mad6590722007-12-18 15:47:03 +0800379 iput(main_bm_inode);
380
381out:
Tao Mad6590722007-12-18 15:47:03 +0800382 return ret;
383}
Tao Ma7909f2b2007-12-18 15:47:25 +0800384
385static int ocfs2_check_new_group(struct inode *inode,
386 struct ocfs2_dinode *di,
387 struct ocfs2_new_group_input *input,
388 struct buffer_head *group_bh)
389{
390 int ret;
Joel Becker57e3e792008-11-13 14:49:13 -0800391 struct ocfs2_group_desc *gd =
392 (struct ocfs2_group_desc *)group_bh->b_data;
Tao Ma7909f2b2007-12-18 15:47:25 +0800393 u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc);
Tao Ma7909f2b2007-12-18 15:47:25 +0800394
Joel Becker970e4932008-11-13 14:49:19 -0800395 ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh);
Joel Becker57e3e792008-11-13 14:49:13 -0800396 if (ret)
397 goto out;
Tao Ma7909f2b2007-12-18 15:47:25 +0800398
Joel Becker57e3e792008-11-13 14:49:13 -0800399 ret = -EINVAL;
400 if (le16_to_cpu(gd->bg_chain) != input->chain)
Tao Ma7909f2b2007-12-18 15:47:25 +0800401 mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u "
402 "while input has %u set.\n",
403 (unsigned long long)le64_to_cpu(gd->bg_blkno),
404 le16_to_cpu(gd->bg_chain), input->chain);
405 else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc)
406 mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but "
407 "input has %u clusters set\n",
408 (unsigned long long)le64_to_cpu(gd->bg_blkno),
409 le16_to_cpu(gd->bg_bits), input->clusters);
410 else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc)
411 mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u "
412 "but it should have %u set\n",
413 (unsigned long long)le64_to_cpu(gd->bg_blkno),
414 le16_to_cpu(gd->bg_bits),
415 input->frees * cl_bpc);
416 else
417 ret = 0;
418
Joel Becker57e3e792008-11-13 14:49:13 -0800419out:
Tao Ma7909f2b2007-12-18 15:47:25 +0800420 return ret;
421}
422
423static int ocfs2_verify_group_and_input(struct inode *inode,
424 struct ocfs2_dinode *di,
425 struct ocfs2_new_group_input *input,
426 struct buffer_head *group_bh)
427{
428 u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count);
429 u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
430 u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec);
431 u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group);
432 u32 total_clusters = le32_to_cpu(di->i_clusters);
433 int ret = -EINVAL;
434
435 if (cluster < total_clusters)
436 mlog(ML_ERROR, "add a group which is in the current volume.\n");
437 else if (input->chain >= cl_count)
438 mlog(ML_ERROR, "input chain exceeds the limit.\n");
439 else if (next_free != cl_count && next_free != input->chain)
440 mlog(ML_ERROR,
441 "the add group should be in chain %u\n", next_free);
442 else if (total_clusters + input->clusters < total_clusters)
443 mlog(ML_ERROR, "add group's clusters overflow.\n");
444 else if (input->clusters > cl_cpg)
445 mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n");
446 else if (input->frees > input->clusters)
447 mlog(ML_ERROR, "the free cluster exceeds the total clusters\n");
448 else if (total_clusters % cl_cpg != 0)
449 mlog(ML_ERROR,
450 "the last group isn't full. Use group extend first.\n");
451 else if (input->group != ocfs2_which_cluster_group(inode, cluster))
452 mlog(ML_ERROR, "group blkno is invalid\n");
453 else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh)))
454 mlog(ML_ERROR, "group descriptor check failed.\n");
455 else
456 ret = 0;
457
458 return ret;
459}
460
461/* Add a new group descriptor to global_bitmap. */
462int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
463{
464 int ret;
465 handle_t *handle;
466 struct buffer_head *main_bm_bh = NULL;
467 struct inode *main_bm_inode = NULL;
468 struct ocfs2_dinode *fe = NULL;
469 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
470 struct buffer_head *group_bh = NULL;
471 struct ocfs2_group_desc *group = NULL;
472 struct ocfs2_chain_list *cl;
473 struct ocfs2_chain_rec *cr;
474 u16 cl_bpc;
Younger Liueedd40e2013-11-12 15:07:03 -0800475 u64 bg_ptr;
Tao Ma7909f2b2007-12-18 15:47:25 +0800476
Tao Ma7909f2b2007-12-18 15:47:25 +0800477 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
478 return -EROFS;
479
480 main_bm_inode = ocfs2_get_system_file_inode(osb,
481 GLOBAL_BITMAP_SYSTEM_INODE,
482 OCFS2_INVALID_SLOT);
483 if (!main_bm_inode) {
484 ret = -EINVAL;
485 mlog_errno(ret);
486 goto out;
487 }
488
Al Viro59551022016-01-22 15:40:57 -0500489 inode_lock(main_bm_inode);
Tao Ma7909f2b2007-12-18 15:47:25 +0800490
491 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
492 if (ret < 0) {
493 mlog_errno(ret);
494 goto out_mutex;
495 }
496
497 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
498
499 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
Tao Ma85718822010-04-13 14:38:06 +0800500 ocfs2_group_bitmap_size(osb->sb, 0,
501 osb->s_feature_incompat) * 8) {
Tao Ma7909f2b2007-12-18 15:47:25 +0800502 mlog(ML_ERROR, "The disk is too old and small."
503 " Force to do offline resize.");
504 ret = -EINVAL;
505 goto out_unlock;
506 }
507
Joel Beckerda1e9092008-10-09 17:20:29 -0700508 ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800509 if (ret < 0) {
510 mlog(ML_ERROR, "Can't read the group descriptor # %llu "
511 "from the device.", (unsigned long long)input->group);
512 goto out_unlock;
513 }
514
Joel Becker8cb471e2009-02-10 20:00:41 -0800515 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800516
517 ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh);
518 if (ret) {
519 mlog_errno(ret);
Younger Liu8abaae82013-11-12 15:07:04 -0800520 goto out_free_group_bh;
Tao Ma7909f2b2007-12-18 15:47:25 +0800521 }
522
Tao Maa5438702011-02-22 08:24:01 +0800523 trace_ocfs2_group_add((unsigned long long)input->group,
524 input->chain, input->clusters, input->frees);
Tao Ma7909f2b2007-12-18 15:47:25 +0800525
526 handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS);
527 if (IS_ERR(handle)) {
528 mlog_errno(PTR_ERR(handle));
529 ret = -EINVAL;
Younger Liu8abaae82013-11-12 15:07:04 -0800530 goto out_free_group_bh;
Tao Ma7909f2b2007-12-18 15:47:25 +0800531 }
532
533 cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc);
534 cl = &fe->id2.i_chain;
535 cr = &cl->cl_recs[input->chain];
536
Joel Becker0cf2f762009-02-12 16:41:25 -0800537 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode),
538 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma7909f2b2007-12-18 15:47:25 +0800539 if (ret < 0) {
540 mlog_errno(ret);
541 goto out_commit;
542 }
543
544 group = (struct ocfs2_group_desc *)group_bh->b_data;
Younger Liueedd40e2013-11-12 15:07:03 -0800545 bg_ptr = le64_to_cpu(group->bg_next_group);
Tao Ma7909f2b2007-12-18 15:47:25 +0800546 group->bg_next_group = cr->c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -0700547 ocfs2_journal_dirty(handle, group_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800548
Joel Becker0cf2f762009-02-12 16:41:25 -0800549 ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode),
550 main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma7909f2b2007-12-18 15:47:25 +0800551 if (ret < 0) {
Younger Liueedd40e2013-11-12 15:07:03 -0800552 group->bg_next_group = cpu_to_le64(bg_ptr);
Tao Ma7909f2b2007-12-18 15:47:25 +0800553 mlog_errno(ret);
554 goto out_commit;
555 }
556
557 if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) {
558 le16_add_cpu(&cl->cl_next_free_rec, 1);
559 memset(cr, 0, sizeof(struct ocfs2_chain_rec));
560 }
561
Tao Ma4338ab62008-03-03 10:53:02 +0800562 cr->c_blkno = cpu_to_le64(input->group);
Tao Ma7909f2b2007-12-18 15:47:25 +0800563 le32_add_cpu(&cr->c_total, input->clusters * cl_bpc);
564 le32_add_cpu(&cr->c_free, input->frees * cl_bpc);
565
566 le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc);
567 le32_add_cpu(&fe->id1.bitmap1.i_used,
568 (input->clusters - input->frees) * cl_bpc);
569 le32_add_cpu(&fe->i_clusters, input->clusters);
570
571 ocfs2_journal_dirty(handle, main_bm_bh);
572
573 spin_lock(&OCFS2_I(main_bm_inode)->ip_lock);
574 OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
Joseph Qi17bf1412014-06-04 16:06:17 -0700575 le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits);
Tao Ma7909f2b2007-12-18 15:47:25 +0800576 spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock);
577 i_size_write(main_bm_inode, le64_to_cpu(fe->i_size));
578
579 ocfs2_update_super_and_backups(main_bm_inode, input->clusters);
580
581out_commit:
582 ocfs2_commit_trans(osb, handle);
Younger Liu8abaae82013-11-12 15:07:04 -0800583
584out_free_group_bh:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800585 brelse(group_bh);
Younger Liu8abaae82013-11-12 15:07:04 -0800586
587out_unlock:
Mark Fasheh2fe5c1d2008-01-23 18:35:31 -0800588 brelse(main_bm_bh);
Tao Ma7909f2b2007-12-18 15:47:25 +0800589
590 ocfs2_inode_unlock(main_bm_inode, 1);
591
592out_mutex:
Al Viro59551022016-01-22 15:40:57 -0500593 inode_unlock(main_bm_inode);
Tao Ma7909f2b2007-12-18 15:47:25 +0800594 iput(main_bm_inode);
595
596out:
Tao Ma7909f2b2007-12-18 15:47:25 +0800597 return ret;
598}