]>
Commit | Line | Data |
---|---|---|
d659072f TM |
1 | /* -*- 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 | ||
d659072f TM |
30 | #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" | |
a5438702 | 41 | #include "ocfs2_trace.h" |
d659072f TM |
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 | */ | |
54 | static u16 ocfs2_calc_new_backup_super(struct inode *inode, | |
55 | struct ocfs2_group_desc *gd, | |
d659072f | 56 | u16 cl_cpg, |
5c9ee4cb | 57 | u16 old_bg_clusters, |
d659072f TM |
58 | int set) |
59 | { | |
60 | int i; | |
61 | u16 backups = 0; | |
5c9ee4cb | 62 | u32 cluster, lgd_cluster; |
d659072f TM |
63 | 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 | ||
5c9ee4cb JQ |
75 | /* 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 | ||
d659072f TM |
81 | 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 | ||
d659072f TM |
90 | return backups; |
91 | } | |
92 | ||
93 | static 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); | |
5c9ee4cb | 109 | u16 old_bg_clusters; |
d659072f | 110 | |
a5438702 TM |
111 | trace_ocfs2_update_last_group_and_inode(new_clusters, |
112 | first_new_cluster); | |
d659072f | 113 | |
0cf2f763 JB |
114 | ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode), |
115 | group_bh, OCFS2_JOURNAL_ACCESS_WRITE); | |
d659072f TM |
116 | if (ret < 0) { |
117 | mlog_errno(ret); | |
118 | goto out; | |
119 | } | |
120 | ||
121 | group = (struct ocfs2_group_desc *)group_bh->b_data; | |
122 | ||
5c9ee4cb | 123 | old_bg_clusters = le16_to_cpu(group->bg_bits) / cl_bpc; |
d659072f TM |
124 | /* 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, | |
5c9ee4cb | 137 | cl_cpg, old_bg_clusters, 1); |
d659072f TM |
138 | le16_add_cpu(&group->bg_free_bits_count, -1 * backups); |
139 | } | |
140 | ||
ec20cec7 | 141 | ocfs2_journal_dirty(handle, group_bh); |
d659072f TM |
142 | |
143 | /* update the inode accordingly. */ | |
0cf2f763 | 144 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh, |
13723d00 | 145 | OCFS2_JOURNAL_ACCESS_WRITE); |
d659072f TM |
146 | 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); | |
17bf1418 | 165 | le64_add_cpu(&fe->i_size, (u64)new_clusters << osb->s_clustersize_bits); |
d659072f TM |
166 | 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 | ||
171 | out_rollback: | |
172 | if (ret < 0) { | |
173 | ocfs2_calc_new_backup_super(bm_inode, | |
174 | group, | |
5c9ee4cb | 175 | cl_cpg, old_bg_clusters, 0); |
d659072f TM |
176 | 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 | } | |
180 | out: | |
c1e8d35e TM |
181 | if (ret) |
182 | mlog_errno(ret); | |
d659072f TM |
183 | return ret; |
184 | } | |
185 | ||
186 | static 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); | |
584dca34 | 199 | if (cluster >= clusters) |
d659072f TM |
200 | break; |
201 | ||
da1e9098 | 202 | ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup); |
d659072f TM |
203 | 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 | ||
225 | static 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 | */ | |
da1e9098 JB |
238 | ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1, |
239 | &super_bh); | |
d659072f TM |
240 | 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 | ||
258 | out: | |
2fe5c1d7 | 259 | brelse(super_bh); |
d659072f TM |
260 | 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 | */ | |
273 | int 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 | ||
d659072f TM |
287 | 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 | ||
5955102c | 304 | inode_lock(main_bm_inode); |
d659072f TM |
305 | |
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 | ||
10995aa2 JB |
314 | /* 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 | ||
d659072f | 318 | if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != |
8571882c TM |
319 | ocfs2_group_bitmap_size(osb->sb, 0, |
320 | osb->s_feature_incompat) * 8) { | |
d659072f TM |
321 | mlog(ML_ERROR, "The disk is too old and small. " |
322 | "Force to do offline resize."); | |
323 | ret = -EINVAL; | |
324 | goto out_unlock; | |
d659072f TM |
325 | } |
326 | ||
327 | 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 | ||
68f64d47 JB |
331 | ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno, |
332 | &group_bh); | |
d659072f TM |
333 | if (ret < 0) { |
334 | mlog_errno(ret); | |
335 | goto out_unlock; | |
336 | } | |
d659072f TM |
337 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
338 | ||
d659072f TM |
339 | 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 | ||
a5438702 TM |
346 | |
347 | trace_ocfs2_group_extend( | |
7909f2bf | 348 | (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters); |
d659072f TM |
349 | |
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 | ||
369 | out_commit: | |
370 | ocfs2_commit_trans(osb, handle); | |
371 | out_unlock: | |
2fe5c1d7 MF |
372 | brelse(group_bh); |
373 | brelse(main_bm_bh); | |
d659072f TM |
374 | |
375 | ocfs2_inode_unlock(main_bm_inode, 1); | |
376 | ||
377 | out_mutex: | |
5955102c | 378 | inode_unlock(main_bm_inode); |
d659072f TM |
379 | iput(main_bm_inode); |
380 | ||
381 | out: | |
d659072f TM |
382 | return ret; |
383 | } | |
7909f2bf TM |
384 | |
385 | static 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; | |
57e3e797 JB |
391 | struct ocfs2_group_desc *gd = |
392 | (struct ocfs2_group_desc *)group_bh->b_data; | |
7909f2bf | 393 | u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc); |
7909f2bf | 394 | |
970e4936 | 395 | ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh); |
57e3e797 JB |
396 | if (ret) |
397 | goto out; | |
7909f2bf | 398 | |
57e3e797 JB |
399 | ret = -EINVAL; |
400 | if (le16_to_cpu(gd->bg_chain) != input->chain) | |
7909f2bf TM |
401 | 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 | ||
57e3e797 | 419 | out: |
7909f2bf TM |
420 | return ret; |
421 | } | |
422 | ||
423 | static 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. */ | |
462 | int 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; | |
eedd40e1 | 475 | u64 bg_ptr; |
7909f2bf | 476 | |
7909f2bf TM |
477 | 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 | ||
5955102c | 489 | inode_lock(main_bm_inode); |
7909f2bf TM |
490 | |
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) != | |
8571882c TM |
500 | ocfs2_group_bitmap_size(osb->sb, 0, |
501 | osb->s_feature_incompat) * 8) { | |
7909f2bf TM |
502 | 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 | ||
da1e9098 | 508 | ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh); |
7909f2bf TM |
509 | 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 | ||
8cb471e8 | 515 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh); |
7909f2bf TM |
516 | |
517 | ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh); | |
518 | if (ret) { | |
519 | mlog_errno(ret); | |
8abaae8d | 520 | goto out_free_group_bh; |
7909f2bf TM |
521 | } |
522 | ||
a5438702 TM |
523 | trace_ocfs2_group_add((unsigned long long)input->group, |
524 | input->chain, input->clusters, input->frees); | |
7909f2bf TM |
525 | |
526 | handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS); | |
527 | if (IS_ERR(handle)) { | |
528 | mlog_errno(PTR_ERR(handle)); | |
529 | ret = -EINVAL; | |
8abaae8d | 530 | goto out_free_group_bh; |
7909f2bf TM |
531 | } |
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 | ||
0cf2f763 JB |
537 | ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode), |
538 | group_bh, OCFS2_JOURNAL_ACCESS_WRITE); | |
7909f2bf TM |
539 | if (ret < 0) { |
540 | mlog_errno(ret); | |
541 | goto out_commit; | |
542 | } | |
543 | ||
544 | group = (struct ocfs2_group_desc *)group_bh->b_data; | |
eedd40e1 | 545 | bg_ptr = le64_to_cpu(group->bg_next_group); |
7909f2bf | 546 | group->bg_next_group = cr->c_blkno; |
ec20cec7 | 547 | ocfs2_journal_dirty(handle, group_bh); |
7909f2bf | 548 | |
0cf2f763 JB |
549 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode), |
550 | main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE); | |
7909f2bf | 551 | if (ret < 0) { |
eedd40e1 | 552 | group->bg_next_group = cpu_to_le64(bg_ptr); |
7909f2bf TM |
553 | 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 | ||
4338ab6a | 562 | cr->c_blkno = cpu_to_le64(input->group); |
7909f2bf TM |
563 | 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); | |
17bf1418 | 575 | le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits); |
7909f2bf TM |
576 | 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 | ||
581 | out_commit: | |
582 | ocfs2_commit_trans(osb, handle); | |
8abaae8d YL |
583 | |
584 | out_free_group_bh: | |
2fe5c1d7 | 585 | brelse(group_bh); |
8abaae8d YL |
586 | |
587 | out_unlock: | |
2fe5c1d7 | 588 | brelse(main_bm_bh); |
7909f2bf TM |
589 | |
590 | ocfs2_inode_unlock(main_bm_inode, 1); | |
591 | ||
592 | out_mutex: | |
5955102c | 593 | inode_unlock(main_bm_inode); |
7909f2bf TM |
594 | iput(main_bm_inode); |
595 | ||
596 | out: | |
7909f2bf TM |
597 | return ret; |
598 | } |