1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Inspired by ext3/resize.c.
8 * Copyright (C) 2007 Oracle. All rights reserved.
12 #include <linux/types.h>
14 #include <cluster/masklog.h>
25 #include "ocfs2_trace.h"
27 #include "buffer_head_io.h"
32 * Check whether there are new backup superblocks exist
33 * in the last group. If there are some, mark them or clear
36 * Return how many backups we find in the last group.
38 static u16
ocfs2_calc_new_backup_super(struct inode
*inode
,
39 struct ocfs2_group_desc
*gd
,
46 u32 cluster
, lgd_cluster
;
47 u64 blkno
, gd_blkno
, lgd_blkno
= le64_to_cpu(gd
->bg_blkno
);
49 for (i
= 0; i
< OCFS2_MAX_BACKUP_SUPERBLOCKS
; i
++) {
50 blkno
= ocfs2_backup_super_blkno(inode
->i_sb
, i
);
51 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, blkno
);
53 gd_blkno
= ocfs2_which_cluster_group(inode
, cluster
);
54 if (gd_blkno
< lgd_blkno
)
56 else if (gd_blkno
> lgd_blkno
)
59 /* check if already done backup super */
60 lgd_cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, lgd_blkno
);
61 lgd_cluster
+= old_bg_clusters
;
62 if (lgd_cluster
>= cluster
)
66 ocfs2_set_bit(cluster
% cl_cpg
,
67 (unsigned long *)gd
->bg_bitmap
);
69 ocfs2_clear_bit(cluster
% cl_cpg
,
70 (unsigned long *)gd
->bg_bitmap
);
77 static int ocfs2_update_last_group_and_inode(handle_t
*handle
,
78 struct inode
*bm_inode
,
79 struct buffer_head
*bm_bh
,
80 struct buffer_head
*group_bh
,
81 u32 first_new_cluster
,
85 struct ocfs2_super
*osb
= OCFS2_SB(bm_inode
->i_sb
);
86 struct ocfs2_dinode
*fe
= (struct ocfs2_dinode
*) bm_bh
->b_data
;
87 struct ocfs2_chain_list
*cl
= &fe
->id2
.i_chain
;
88 struct ocfs2_chain_rec
*cr
;
89 struct ocfs2_group_desc
*group
;
90 u16 chain
, num_bits
, backups
= 0;
91 u16 cl_bpc
= le16_to_cpu(cl
->cl_bpc
);
92 u16 cl_cpg
= le16_to_cpu(cl
->cl_cpg
);
95 __le16 old_bg_contig_free_bits
;
97 trace_ocfs2_update_last_group_and_inode(new_clusters
,
100 ret
= ocfs2_journal_access_gd(handle
, INODE_CACHE(bm_inode
),
101 group_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
107 group
= (struct ocfs2_group_desc
*)group_bh
->b_data
;
109 old_bg_clusters
= le16_to_cpu(group
->bg_bits
) / cl_bpc
;
110 /* update the group first. */
111 num_bits
= new_clusters
* cl_bpc
;
112 le16_add_cpu(&group
->bg_bits
, num_bits
);
113 le16_add_cpu(&group
->bg_free_bits_count
, num_bits
);
116 * check whether there are some new backup superblocks exist in
117 * this group and update the group bitmap accordingly.
119 if (OCFS2_HAS_COMPAT_FEATURE(osb
->sb
,
120 OCFS2_FEATURE_COMPAT_BACKUP_SB
)) {
121 backups
= ocfs2_calc_new_backup_super(bm_inode
,
123 cl_cpg
, old_bg_clusters
, 1);
124 le16_add_cpu(&group
->bg_free_bits_count
, -1 * backups
);
127 contig_bits
= ocfs2_find_max_contig_free_bits(group
->bg_bitmap
,
128 le16_to_cpu(group
->bg_bits
), 0);
129 old_bg_contig_free_bits
= group
->bg_contig_free_bits
;
130 group
->bg_contig_free_bits
= cpu_to_le16(contig_bits
);
132 ocfs2_journal_dirty(handle
, group_bh
);
134 /* update the inode accordingly. */
135 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(bm_inode
), bm_bh
,
136 OCFS2_JOURNAL_ACCESS_WRITE
);
142 chain
= le16_to_cpu(group
->bg_chain
);
143 cr
= (&cl
->cl_recs
[chain
]);
144 le32_add_cpu(&cr
->c_total
, num_bits
);
145 le32_add_cpu(&cr
->c_free
, num_bits
);
146 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, num_bits
);
147 le32_add_cpu(&fe
->i_clusters
, new_clusters
);
150 le32_add_cpu(&cr
->c_free
, -1 * backups
);
151 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, backups
);
154 spin_lock(&OCFS2_I(bm_inode
)->ip_lock
);
155 OCFS2_I(bm_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
156 le64_add_cpu(&fe
->i_size
, (u64
)new_clusters
<< osb
->s_clustersize_bits
);
157 spin_unlock(&OCFS2_I(bm_inode
)->ip_lock
);
158 i_size_write(bm_inode
, le64_to_cpu(fe
->i_size
));
160 ocfs2_journal_dirty(handle
, bm_bh
);
164 ocfs2_calc_new_backup_super(bm_inode
,
166 cl_cpg
, old_bg_clusters
, 0);
167 le16_add_cpu(&group
->bg_free_bits_count
, backups
);
168 le16_add_cpu(&group
->bg_bits
, -1 * num_bits
);
169 le16_add_cpu(&group
->bg_free_bits_count
, -1 * num_bits
);
170 group
->bg_contig_free_bits
= old_bg_contig_free_bits
;
178 static int update_backups(struct inode
* inode
, u32 clusters
, char *data
)
183 struct buffer_head
*backup
= NULL
;
184 struct ocfs2_dinode
*backup_di
= NULL
;
185 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
187 /* calculate the real backups we need to update. */
188 for (i
= 0; i
< OCFS2_MAX_BACKUP_SUPERBLOCKS
; i
++) {
189 blkno
= ocfs2_backup_super_blkno(inode
->i_sb
, i
);
190 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, blkno
);
191 if (cluster
>= clusters
)
194 ret
= ocfs2_read_blocks_sync(osb
, blkno
, 1, &backup
);
200 memcpy(backup
->b_data
, data
, inode
->i_sb
->s_blocksize
);
202 backup_di
= (struct ocfs2_dinode
*)backup
->b_data
;
203 backup_di
->i_blkno
= cpu_to_le64(blkno
);
205 ret
= ocfs2_write_super_or_backup(osb
, backup
);
217 static void ocfs2_update_super_and_backups(struct inode
*inode
,
222 struct buffer_head
*super_bh
= NULL
;
223 struct ocfs2_dinode
*super_di
= NULL
;
224 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
227 * update the superblock last.
228 * It doesn't matter if the write failed.
230 ret
= ocfs2_read_blocks_sync(osb
, OCFS2_SUPER_BLOCK_BLKNO
, 1,
237 super_di
= (struct ocfs2_dinode
*)super_bh
->b_data
;
238 le32_add_cpu(&super_di
->i_clusters
, new_clusters
);
239 clusters
= le32_to_cpu(super_di
->i_clusters
);
241 ret
= ocfs2_write_super_or_backup(osb
, super_bh
);
247 if (OCFS2_HAS_COMPAT_FEATURE(osb
->sb
, OCFS2_FEATURE_COMPAT_BACKUP_SB
))
248 ret
= update_backups(inode
, clusters
, super_bh
->b_data
);
253 printk(KERN_WARNING
"ocfs2: Failed to update super blocks on %s"
254 " during fs resize. This condition is not fatal,"
255 " but fsck.ocfs2 should be run to fix it\n",
261 * Extend the filesystem to the new number of clusters specified. This entry
262 * point is only used to extend the current filesystem to the end of the last
265 int ocfs2_group_extend(struct inode
* inode
, int new_clusters
)
269 struct buffer_head
*main_bm_bh
= NULL
;
270 struct buffer_head
*group_bh
= NULL
;
271 struct inode
*main_bm_inode
= NULL
;
272 struct ocfs2_dinode
*fe
= NULL
;
273 struct ocfs2_group_desc
*group
= NULL
;
274 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
276 u32 first_new_cluster
;
279 if (ocfs2_is_hard_readonly(osb
) || ocfs2_is_soft_readonly(osb
))
282 if (new_clusters
< 0)
284 else if (new_clusters
== 0)
287 main_bm_inode
= ocfs2_get_system_file_inode(osb
,
288 GLOBAL_BITMAP_SYSTEM_INODE
,
290 if (!main_bm_inode
) {
296 inode_lock(main_bm_inode
);
298 ret
= ocfs2_inode_lock(main_bm_inode
, &main_bm_bh
, 1);
304 fe
= (struct ocfs2_dinode
*)main_bm_bh
->b_data
;
306 /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(),
307 * so any corruption is a code bug. */
308 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
310 if (le16_to_cpu(fe
->id2
.i_chain
.cl_cpg
) !=
311 ocfs2_group_bitmap_size(osb
->sb
, 0,
312 osb
->s_feature_incompat
) * 8) {
313 mlog(ML_ERROR
, "The disk is too old and small. "
314 "Force to do offline resize.");
319 first_new_cluster
= le32_to_cpu(fe
->i_clusters
);
320 lgd_blkno
= ocfs2_which_cluster_group(main_bm_inode
,
321 first_new_cluster
- 1);
323 ret
= ocfs2_read_group_descriptor(main_bm_inode
, fe
, lgd_blkno
,
329 group
= (struct ocfs2_group_desc
*)group_bh
->b_data
;
331 cl_bpc
= le16_to_cpu(fe
->id2
.i_chain
.cl_bpc
);
332 if (le16_to_cpu(group
->bg_bits
) / cl_bpc
+ new_clusters
>
333 le16_to_cpu(fe
->id2
.i_chain
.cl_cpg
)) {
339 trace_ocfs2_group_extend(
340 (unsigned long long)le64_to_cpu(group
->bg_blkno
), new_clusters
);
342 handle
= ocfs2_start_trans(osb
, OCFS2_GROUP_EXTEND_CREDITS
);
343 if (IS_ERR(handle
)) {
344 mlog_errno(PTR_ERR(handle
));
349 /* update the last group descriptor and inode. */
350 ret
= ocfs2_update_last_group_and_inode(handle
, main_bm_inode
,
351 main_bm_bh
, group_bh
,
359 ocfs2_update_super_and_backups(main_bm_inode
, new_clusters
);
362 ocfs2_commit_trans(osb
, handle
);
367 ocfs2_inode_unlock(main_bm_inode
, 1);
370 inode_unlock(main_bm_inode
);
377 static int ocfs2_check_new_group(struct inode
*inode
,
378 struct ocfs2_dinode
*di
,
379 struct ocfs2_new_group_input
*input
,
380 struct buffer_head
*group_bh
)
383 struct ocfs2_group_desc
*gd
=
384 (struct ocfs2_group_desc
*)group_bh
->b_data
;
385 u16 cl_bpc
= le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
387 ret
= ocfs2_check_group_descriptor(inode
->i_sb
, di
, group_bh
);
392 if (le16_to_cpu(gd
->bg_chain
) != input
->chain
)
393 mlog(ML_ERROR
, "Group descriptor # %llu has bad chain %u "
394 "while input has %u set.\n",
395 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
396 le16_to_cpu(gd
->bg_chain
), input
->chain
);
397 else if (le16_to_cpu(gd
->bg_bits
) != input
->clusters
* cl_bpc
)
398 mlog(ML_ERROR
, "Group descriptor # %llu has bit count %u but "
399 "input has %u clusters set\n",
400 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
401 le16_to_cpu(gd
->bg_bits
), input
->clusters
);
402 else if (le16_to_cpu(gd
->bg_free_bits_count
) != input
->frees
* cl_bpc
)
403 mlog(ML_ERROR
, "Group descriptor # %llu has free bit count %u "
404 "but it should have %u set\n",
405 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
406 le16_to_cpu(gd
->bg_bits
),
407 input
->frees
* cl_bpc
);
415 static int ocfs2_verify_group_and_input(struct inode
*inode
,
416 struct ocfs2_dinode
*di
,
417 struct ocfs2_new_group_input
*input
,
418 struct buffer_head
*group_bh
)
420 u16 cl_count
= le16_to_cpu(di
->id2
.i_chain
.cl_count
);
421 u16 cl_cpg
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
);
422 u16 next_free
= le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
);
423 u32 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, input
->group
);
424 u32 total_clusters
= le32_to_cpu(di
->i_clusters
);
427 if (cluster
< total_clusters
)
428 mlog(ML_ERROR
, "add a group which is in the current volume.\n");
429 else if (input
->chain
>= cl_count
)
430 mlog(ML_ERROR
, "input chain exceeds the limit.\n");
431 else if (next_free
!= cl_count
&& next_free
!= input
->chain
)
433 "the add group should be in chain %u\n", next_free
);
434 else if (total_clusters
+ input
->clusters
< total_clusters
)
435 mlog(ML_ERROR
, "add group's clusters overflow.\n");
436 else if (input
->clusters
> cl_cpg
)
437 mlog(ML_ERROR
, "the cluster exceeds the maximum of a group\n");
438 else if (input
->frees
> input
->clusters
)
439 mlog(ML_ERROR
, "the free cluster exceeds the total clusters\n");
440 else if (total_clusters
% cl_cpg
!= 0)
442 "the last group isn't full. Use group extend first.\n");
443 else if (input
->group
!= ocfs2_which_cluster_group(inode
, cluster
))
444 mlog(ML_ERROR
, "group blkno is invalid\n");
445 else if ((ret
= ocfs2_check_new_group(inode
, di
, input
, group_bh
)))
446 mlog(ML_ERROR
, "group descriptor check failed.\n");
453 /* Add a new group descriptor to global_bitmap. */
454 int ocfs2_group_add(struct inode
*inode
, struct ocfs2_new_group_input
*input
)
458 struct buffer_head
*main_bm_bh
= NULL
;
459 struct inode
*main_bm_inode
= NULL
;
460 struct ocfs2_dinode
*fe
= NULL
;
461 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
462 struct buffer_head
*group_bh
= NULL
;
463 struct ocfs2_group_desc
*group
= NULL
;
464 struct ocfs2_chain_list
*cl
;
465 struct ocfs2_chain_rec
*cr
;
469 if (ocfs2_is_hard_readonly(osb
) || ocfs2_is_soft_readonly(osb
))
472 main_bm_inode
= ocfs2_get_system_file_inode(osb
,
473 GLOBAL_BITMAP_SYSTEM_INODE
,
475 if (!main_bm_inode
) {
481 inode_lock(main_bm_inode
);
483 ret
= ocfs2_inode_lock(main_bm_inode
, &main_bm_bh
, 1);
489 fe
= (struct ocfs2_dinode
*)main_bm_bh
->b_data
;
491 if (le16_to_cpu(fe
->id2
.i_chain
.cl_cpg
) !=
492 ocfs2_group_bitmap_size(osb
->sb
, 0,
493 osb
->s_feature_incompat
) * 8) {
494 mlog(ML_ERROR
, "The disk is too old and small."
495 " Force to do offline resize.");
500 ret
= ocfs2_read_blocks_sync(osb
, input
->group
, 1, &group_bh
);
502 mlog(ML_ERROR
, "Can't read the group descriptor # %llu "
503 "from the device.", (unsigned long long)input
->group
);
507 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), group_bh
);
509 ret
= ocfs2_verify_group_and_input(main_bm_inode
, fe
, input
, group_bh
);
512 goto out_free_group_bh
;
515 trace_ocfs2_group_add((unsigned long long)input
->group
,
516 input
->chain
, input
->clusters
, input
->frees
);
518 handle
= ocfs2_start_trans(osb
, OCFS2_GROUP_ADD_CREDITS
);
519 if (IS_ERR(handle
)) {
520 mlog_errno(PTR_ERR(handle
));
522 goto out_free_group_bh
;
525 cl_bpc
= le16_to_cpu(fe
->id2
.i_chain
.cl_bpc
);
526 cl
= &fe
->id2
.i_chain
;
527 cr
= &cl
->cl_recs
[input
->chain
];
529 ret
= ocfs2_journal_access_gd(handle
, INODE_CACHE(main_bm_inode
),
530 group_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
536 group
= (struct ocfs2_group_desc
*)group_bh
->b_data
;
537 bg_ptr
= le64_to_cpu(group
->bg_next_group
);
538 group
->bg_next_group
= cr
->c_blkno
;
539 ocfs2_journal_dirty(handle
, group_bh
);
541 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(main_bm_inode
),
542 main_bm_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
544 group
->bg_next_group
= cpu_to_le64(bg_ptr
);
549 if (input
->chain
== le16_to_cpu(cl
->cl_next_free_rec
)) {
550 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
551 memset(cr
, 0, sizeof(struct ocfs2_chain_rec
));
554 cr
->c_blkno
= cpu_to_le64(input
->group
);
555 le32_add_cpu(&cr
->c_total
, input
->clusters
* cl_bpc
);
556 le32_add_cpu(&cr
->c_free
, input
->frees
* cl_bpc
);
558 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, input
->clusters
*cl_bpc
);
559 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
,
560 (input
->clusters
- input
->frees
) * cl_bpc
);
561 le32_add_cpu(&fe
->i_clusters
, input
->clusters
);
563 ocfs2_journal_dirty(handle
, main_bm_bh
);
565 spin_lock(&OCFS2_I(main_bm_inode
)->ip_lock
);
566 OCFS2_I(main_bm_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
567 le64_add_cpu(&fe
->i_size
, (u64
)input
->clusters
<< osb
->s_clustersize_bits
);
568 spin_unlock(&OCFS2_I(main_bm_inode
)->ip_lock
);
569 i_size_write(main_bm_inode
, le64_to_cpu(fe
->i_size
));
571 ocfs2_update_super_and_backups(main_bm_inode
, input
->clusters
);
574 ocfs2_commit_trans(osb
, handle
);
578 ocfs2_remove_from_cache(INODE_CACHE(inode
), group_bh
);
584 ocfs2_inode_unlock(main_bm_inode
, 1);
587 inode_unlock(main_bm_inode
);