1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
7 * Inspired by ext3/resize.c.
9 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
28 #include <linux/types.h>
30 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
31 #include <cluster/masklog.h>
43 #include "buffer_head_io.h"
48 * Check whether there are new backup superblocks exist
49 * in the last group. If there are some, mark them or clear
52 * Return how many backups we find in the last group.
54 static u16
ocfs2_calc_new_backup_super(struct inode
*inode
,
55 struct ocfs2_group_desc
*gd
,
57 u32 first_new_cluster
,
64 u64 blkno
, gd_blkno
, lgd_blkno
= le64_to_cpu(gd
->bg_blkno
);
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
);
70 gd_blkno
= ocfs2_which_cluster_group(inode
, cluster
);
71 if (gd_blkno
< lgd_blkno
)
73 else if (gd_blkno
> lgd_blkno
)
77 ocfs2_set_bit(cluster
% cl_cpg
,
78 (unsigned long *)gd
->bg_bitmap
);
80 ocfs2_clear_bit(cluster
% cl_cpg
,
81 (unsigned long *)gd
->bg_bitmap
);
89 static 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
,
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
);
106 mlog_entry("(new_clusters=%d, first_new_cluster = %u)\n",
107 new_clusters
, first_new_cluster
);
109 ret
= ocfs2_journal_access_gd(handle
, INODE_CACHE(bm_inode
),
110 group_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
116 group
= (struct ocfs2_group_desc
*)group_bh
->b_data
;
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
);
124 * check whether there are some new backup superblocks exist in
125 * this group and update the group bitmap accordingly.
127 if (OCFS2_HAS_COMPAT_FEATURE(osb
->sb
,
128 OCFS2_FEATURE_COMPAT_BACKUP_SB
)) {
129 backups
= ocfs2_calc_new_backup_super(bm_inode
,
134 le16_add_cpu(&group
->bg_free_bits_count
, -1 * backups
);
137 ret
= ocfs2_journal_dirty(handle
, group_bh
);
143 /* update the inode accordingly. */
144 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(bm_inode
), bm_bh
,
145 OCFS2_JOURNAL_ACCESS_WRITE
);
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
);
159 le32_add_cpu(&cr
->c_free
, -1 * backups
);
160 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
, backups
);
163 spin_lock(&OCFS2_I(bm_inode
)->ip_lock
);
164 OCFS2_I(bm_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
165 le64_add_cpu(&fe
->i_size
, new_clusters
<< osb
->s_clustersize_bits
);
166 spin_unlock(&OCFS2_I(bm_inode
)->ip_lock
);
167 i_size_write(bm_inode
, le64_to_cpu(fe
->i_size
));
169 ocfs2_journal_dirty(handle
, bm_bh
);
173 ocfs2_calc_new_backup_super(bm_inode
,
178 le16_add_cpu(&group
->bg_free_bits_count
, backups
);
179 le16_add_cpu(&group
->bg_bits
, -1 * num_bits
);
180 le16_add_cpu(&group
->bg_free_bits_count
, -1 * num_bits
);
187 static int update_backups(struct inode
* inode
, u32 clusters
, char *data
)
192 struct buffer_head
*backup
= NULL
;
193 struct ocfs2_dinode
*backup_di
= NULL
;
194 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
196 /* calculate the real backups we need to update. */
197 for (i
= 0; i
< OCFS2_MAX_BACKUP_SUPERBLOCKS
; i
++) {
198 blkno
= ocfs2_backup_super_blkno(inode
->i_sb
, i
);
199 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, blkno
);
200 if (cluster
> clusters
)
203 ret
= ocfs2_read_blocks_sync(osb
, blkno
, 1, &backup
);
209 memcpy(backup
->b_data
, data
, inode
->i_sb
->s_blocksize
);
211 backup_di
= (struct ocfs2_dinode
*)backup
->b_data
;
212 backup_di
->i_blkno
= cpu_to_le64(blkno
);
214 ret
= ocfs2_write_super_or_backup(osb
, backup
);
226 static void ocfs2_update_super_and_backups(struct inode
*inode
,
231 struct buffer_head
*super_bh
= NULL
;
232 struct ocfs2_dinode
*super_di
= NULL
;
233 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
236 * update the superblock last.
237 * It doesn't matter if the write failed.
239 ret
= ocfs2_read_blocks_sync(osb
, OCFS2_SUPER_BLOCK_BLKNO
, 1,
246 super_di
= (struct ocfs2_dinode
*)super_bh
->b_data
;
247 le32_add_cpu(&super_di
->i_clusters
, new_clusters
);
248 clusters
= le32_to_cpu(super_di
->i_clusters
);
250 ret
= ocfs2_write_super_or_backup(osb
, super_bh
);
256 if (OCFS2_HAS_COMPAT_FEATURE(osb
->sb
, OCFS2_FEATURE_COMPAT_BACKUP_SB
))
257 ret
= update_backups(inode
, clusters
, super_bh
->b_data
);
262 printk(KERN_WARNING
"ocfs2: Failed to update super blocks on %s"
263 " during fs resize. This condition is not fatal,"
264 " but fsck.ocfs2 should be run to fix it\n",
270 * Extend the filesystem to the new number of clusters specified. This entry
271 * point is only used to extend the current filesystem to the end of the last
274 int ocfs2_group_extend(struct inode
* inode
, int new_clusters
)
278 struct buffer_head
*main_bm_bh
= NULL
;
279 struct buffer_head
*group_bh
= NULL
;
280 struct inode
*main_bm_inode
= NULL
;
281 struct ocfs2_dinode
*fe
= NULL
;
282 struct ocfs2_group_desc
*group
= NULL
;
283 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
285 u32 first_new_cluster
;
290 if (ocfs2_is_hard_readonly(osb
) || ocfs2_is_soft_readonly(osb
))
293 if (new_clusters
< 0)
295 else if (new_clusters
== 0)
298 main_bm_inode
= ocfs2_get_system_file_inode(osb
,
299 GLOBAL_BITMAP_SYSTEM_INODE
,
301 if (!main_bm_inode
) {
307 mutex_lock(&main_bm_inode
->i_mutex
);
309 ret
= ocfs2_inode_lock(main_bm_inode
, &main_bm_bh
, 1);
315 fe
= (struct ocfs2_dinode
*)main_bm_bh
->b_data
;
317 /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(),
318 * so any corruption is a code bug. */
319 BUG_ON(!OCFS2_IS_VALID_DINODE(fe
));
321 if (le16_to_cpu(fe
->id2
.i_chain
.cl_cpg
) !=
322 ocfs2_group_bitmap_size(osb
->sb
) * 8) {
323 mlog(ML_ERROR
, "The disk is too old and small. "
324 "Force to do offline resize.");
329 first_new_cluster
= le32_to_cpu(fe
->i_clusters
);
330 lgd_blkno
= ocfs2_which_cluster_group(main_bm_inode
,
331 first_new_cluster
- 1);
333 ret
= ocfs2_read_group_descriptor(main_bm_inode
, fe
, lgd_blkno
,
339 group
= (struct ocfs2_group_desc
*)group_bh
->b_data
;
341 cl_bpc
= le16_to_cpu(fe
->id2
.i_chain
.cl_bpc
);
342 if (le16_to_cpu(group
->bg_bits
) / cl_bpc
+ new_clusters
>
343 le16_to_cpu(fe
->id2
.i_chain
.cl_cpg
)) {
348 mlog(0, "extend the last group at %llu, new clusters = %d\n",
349 (unsigned long long)le64_to_cpu(group
->bg_blkno
), new_clusters
);
351 handle
= ocfs2_start_trans(osb
, OCFS2_GROUP_EXTEND_CREDITS
);
352 if (IS_ERR(handle
)) {
353 mlog_errno(PTR_ERR(handle
));
358 /* update the last group descriptor and inode. */
359 ret
= ocfs2_update_last_group_and_inode(handle
, main_bm_inode
,
360 main_bm_bh
, group_bh
,
368 ocfs2_update_super_and_backups(main_bm_inode
, new_clusters
);
371 ocfs2_commit_trans(osb
, handle
);
376 ocfs2_inode_unlock(main_bm_inode
, 1);
379 mutex_unlock(&main_bm_inode
->i_mutex
);
387 static int ocfs2_check_new_group(struct inode
*inode
,
388 struct ocfs2_dinode
*di
,
389 struct ocfs2_new_group_input
*input
,
390 struct buffer_head
*group_bh
)
393 struct ocfs2_group_desc
*gd
=
394 (struct ocfs2_group_desc
*)group_bh
->b_data
;
395 u16 cl_bpc
= le16_to_cpu(di
->id2
.i_chain
.cl_bpc
);
397 ret
= ocfs2_check_group_descriptor(inode
->i_sb
, di
, group_bh
);
402 if (le16_to_cpu(gd
->bg_chain
) != input
->chain
)
403 mlog(ML_ERROR
, "Group descriptor # %llu has bad chain %u "
404 "while input has %u set.\n",
405 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
406 le16_to_cpu(gd
->bg_chain
), input
->chain
);
407 else if (le16_to_cpu(gd
->bg_bits
) != input
->clusters
* cl_bpc
)
408 mlog(ML_ERROR
, "Group descriptor # %llu has bit count %u but "
409 "input has %u clusters set\n",
410 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
411 le16_to_cpu(gd
->bg_bits
), input
->clusters
);
412 else if (le16_to_cpu(gd
->bg_free_bits_count
) != input
->frees
* cl_bpc
)
413 mlog(ML_ERROR
, "Group descriptor # %llu has free bit count %u "
414 "but it should have %u set\n",
415 (unsigned long long)le64_to_cpu(gd
->bg_blkno
),
416 le16_to_cpu(gd
->bg_bits
),
417 input
->frees
* cl_bpc
);
425 static int ocfs2_verify_group_and_input(struct inode
*inode
,
426 struct ocfs2_dinode
*di
,
427 struct ocfs2_new_group_input
*input
,
428 struct buffer_head
*group_bh
)
430 u16 cl_count
= le16_to_cpu(di
->id2
.i_chain
.cl_count
);
431 u16 cl_cpg
= le16_to_cpu(di
->id2
.i_chain
.cl_cpg
);
432 u16 next_free
= le16_to_cpu(di
->id2
.i_chain
.cl_next_free_rec
);
433 u32 cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
, input
->group
);
434 u32 total_clusters
= le32_to_cpu(di
->i_clusters
);
437 if (cluster
< total_clusters
)
438 mlog(ML_ERROR
, "add a group which is in the current volume.\n");
439 else if (input
->chain
>= cl_count
)
440 mlog(ML_ERROR
, "input chain exceeds the limit.\n");
441 else if (next_free
!= cl_count
&& next_free
!= input
->chain
)
443 "the add group should be in chain %u\n", next_free
);
444 else if (total_clusters
+ input
->clusters
< total_clusters
)
445 mlog(ML_ERROR
, "add group's clusters overflow.\n");
446 else if (input
->clusters
> cl_cpg
)
447 mlog(ML_ERROR
, "the cluster exceeds the maximum of a group\n");
448 else if (input
->frees
> input
->clusters
)
449 mlog(ML_ERROR
, "the free cluster exceeds the total clusters\n");
450 else if (total_clusters
% cl_cpg
!= 0)
452 "the last group isn't full. Use group extend first.\n");
453 else if (input
->group
!= ocfs2_which_cluster_group(inode
, cluster
))
454 mlog(ML_ERROR
, "group blkno is invalid\n");
455 else if ((ret
= ocfs2_check_new_group(inode
, di
, input
, group_bh
)))
456 mlog(ML_ERROR
, "group descriptor check failed.\n");
463 /* Add a new group descriptor to global_bitmap. */
464 int ocfs2_group_add(struct inode
*inode
, struct ocfs2_new_group_input
*input
)
468 struct buffer_head
*main_bm_bh
= NULL
;
469 struct inode
*main_bm_inode
= NULL
;
470 struct ocfs2_dinode
*fe
= NULL
;
471 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
472 struct buffer_head
*group_bh
= NULL
;
473 struct ocfs2_group_desc
*group
= NULL
;
474 struct ocfs2_chain_list
*cl
;
475 struct ocfs2_chain_rec
*cr
;
480 if (ocfs2_is_hard_readonly(osb
) || ocfs2_is_soft_readonly(osb
))
483 main_bm_inode
= ocfs2_get_system_file_inode(osb
,
484 GLOBAL_BITMAP_SYSTEM_INODE
,
486 if (!main_bm_inode
) {
492 mutex_lock(&main_bm_inode
->i_mutex
);
494 ret
= ocfs2_inode_lock(main_bm_inode
, &main_bm_bh
, 1);
500 fe
= (struct ocfs2_dinode
*)main_bm_bh
->b_data
;
502 if (le16_to_cpu(fe
->id2
.i_chain
.cl_cpg
) !=
503 ocfs2_group_bitmap_size(osb
->sb
) * 8) {
504 mlog(ML_ERROR
, "The disk is too old and small."
505 " Force to do offline resize.");
510 ret
= ocfs2_read_blocks_sync(osb
, input
->group
, 1, &group_bh
);
512 mlog(ML_ERROR
, "Can't read the group descriptor # %llu "
513 "from the device.", (unsigned long long)input
->group
);
517 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), group_bh
);
519 ret
= ocfs2_verify_group_and_input(main_bm_inode
, fe
, input
, group_bh
);
525 mlog(0, "Add a new group %llu in chain = %u, length = %u\n",
526 (unsigned long long)input
->group
, input
->chain
, input
->clusters
);
528 handle
= ocfs2_start_trans(osb
, OCFS2_GROUP_ADD_CREDITS
);
529 if (IS_ERR(handle
)) {
530 mlog_errno(PTR_ERR(handle
));
535 cl_bpc
= le16_to_cpu(fe
->id2
.i_chain
.cl_bpc
);
536 cl
= &fe
->id2
.i_chain
;
537 cr
= &cl
->cl_recs
[input
->chain
];
539 ret
= ocfs2_journal_access_gd(handle
, INODE_CACHE(main_bm_inode
),
540 group_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
546 group
= (struct ocfs2_group_desc
*)group_bh
->b_data
;
547 group
->bg_next_group
= cr
->c_blkno
;
549 ret
= ocfs2_journal_dirty(handle
, group_bh
);
555 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(main_bm_inode
),
556 main_bm_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
562 if (input
->chain
== le16_to_cpu(cl
->cl_next_free_rec
)) {
563 le16_add_cpu(&cl
->cl_next_free_rec
, 1);
564 memset(cr
, 0, sizeof(struct ocfs2_chain_rec
));
567 cr
->c_blkno
= cpu_to_le64(input
->group
);
568 le32_add_cpu(&cr
->c_total
, input
->clusters
* cl_bpc
);
569 le32_add_cpu(&cr
->c_free
, input
->frees
* cl_bpc
);
571 le32_add_cpu(&fe
->id1
.bitmap1
.i_total
, input
->clusters
*cl_bpc
);
572 le32_add_cpu(&fe
->id1
.bitmap1
.i_used
,
573 (input
->clusters
- input
->frees
) * cl_bpc
);
574 le32_add_cpu(&fe
->i_clusters
, input
->clusters
);
576 ocfs2_journal_dirty(handle
, main_bm_bh
);
578 spin_lock(&OCFS2_I(main_bm_inode
)->ip_lock
);
579 OCFS2_I(main_bm_inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
);
580 le64_add_cpu(&fe
->i_size
, input
->clusters
<< osb
->s_clustersize_bits
);
581 spin_unlock(&OCFS2_I(main_bm_inode
)->ip_lock
);
582 i_size_write(main_bm_inode
, le64_to_cpu(fe
->i_size
));
584 ocfs2_update_super_and_backups(main_bm_inode
, input
->clusters
);
587 ocfs2_commit_trans(osb
, handle
);
592 ocfs2_inode_unlock(main_bm_inode
, 1);
595 mutex_unlock(&main_bm_inode
->i_mutex
);