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 #include <cluster/masklog.h>
41 #include "ocfs2_trace.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,
62 u32 cluster, lgd_cluster;
63 u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno);
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);
69 gd_blkno = ocfs2_which_cluster_group(inode, cluster);
70 if (gd_blkno < lgd_blkno)
72 else if (gd_blkno > lgd_blkno)
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)
82 ocfs2_set_bit(cluster % cl_cpg,
83 (unsigned long *)gd->bg_bitmap);
85 ocfs2_clear_bit(cluster % cl_cpg,
86 (unsigned long *)gd->bg_bitmap);
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,
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);
111 trace_ocfs2_update_last_group_and_inode(new_clusters,
114 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode),
115 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
121 group = (struct ocfs2_group_desc *)group_bh->b_data;
123 old_bg_clusters = le16_to_cpu(group->bg_bits) / cl_bpc;
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);
130 * check whether there are some new backup superblocks exist in
131 * this group and update the group bitmap accordingly.
133 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb,
134 OCFS2_FEATURE_COMPAT_BACKUP_SB)) {
135 backups = ocfs2_calc_new_backup_super(bm_inode,
137 cl_cpg, old_bg_clusters, 1);
138 le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
141 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, (u64)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,
175 cl_cpg, old_bg_clusters, 0);
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);
186 static int update_backups(struct inode * inode, u32 clusters, char *data)
191 struct buffer_head *backup = NULL;
192 struct ocfs2_dinode *backup_di = NULL;
193 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
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);
199 if (cluster >= clusters)
202 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup);
208 memcpy(backup->b_data, data, inode->i_sb->s_blocksize);
210 backup_di = (struct ocfs2_dinode *)backup->b_data;
211 backup_di->i_blkno = cpu_to_le64(blkno);
213 ret = ocfs2_write_super_or_backup(osb, backup);
225 static void ocfs2_update_super_and_backups(struct inode *inode,
230 struct buffer_head *super_bh = NULL;
231 struct ocfs2_dinode *super_di = NULL;
232 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
235 * update the superblock last.
236 * It doesn't matter if the write failed.
238 ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1,
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);
249 ret = ocfs2_write_super_or_backup(osb, super_bh);
255 if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB))
256 ret = update_backups(inode, clusters, super_bh->b_data);
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",
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
273 int ocfs2_group_extend(struct inode * inode, int new_clusters)
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);
284 u32 first_new_cluster;
287 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
290 if (new_clusters < 0)
292 else if (new_clusters == 0)
295 main_bm_inode = ocfs2_get_system_file_inode(osb,
296 GLOBAL_BITMAP_SYSTEM_INODE,
298 if (!main_bm_inode) {
304 inode_lock(main_bm_inode);
306 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
312 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
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));
318 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
319 ocfs2_group_bitmap_size(osb->sb, 0,
320 osb->s_feature_incompat) * 8) {
321 mlog(ML_ERROR, "The disk is too old and small. "
322 "Force to do offline resize.");
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);
331 ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno,
337 group = (struct ocfs2_group_desc *)group_bh->b_data;
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)) {
347 trace_ocfs2_group_extend(
348 (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters);
350 handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS);
351 if (IS_ERR(handle)) {
352 mlog_errno(PTR_ERR(handle));
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,
367 ocfs2_update_super_and_backups(main_bm_inode, new_clusters);
370 ocfs2_commit_trans(osb, handle);
375 ocfs2_inode_unlock(main_bm_inode, 1);
378 inode_unlock(main_bm_inode);
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)
391 struct ocfs2_group_desc *gd =
392 (struct ocfs2_group_desc *)group_bh->b_data;
393 u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc);
395 ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh);
400 if (le16_to_cpu(gd->bg_chain) != input->chain)
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);
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)
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);
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)
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)
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");
461 /* Add a new group descriptor to global_bitmap. */
462 int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input)
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;
477 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
480 main_bm_inode = ocfs2_get_system_file_inode(osb,
481 GLOBAL_BITMAP_SYSTEM_INODE,
483 if (!main_bm_inode) {
489 inode_lock(main_bm_inode);
491 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
497 fe = (struct ocfs2_dinode *)main_bm_bh->b_data;
499 if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
500 ocfs2_group_bitmap_size(osb->sb, 0,
501 osb->s_feature_incompat) * 8) {
502 mlog(ML_ERROR, "The disk is too old and small."
503 " Force to do offline resize.");
508 ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh);
510 mlog(ML_ERROR, "Can't read the group descriptor # %llu "
511 "from the device.", (unsigned long long)input->group);
515 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh);
517 ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh);
520 goto out_free_group_bh;
523 trace_ocfs2_group_add((unsigned long long)input->group,
524 input->chain, input->clusters, input->frees);
526 handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS);
527 if (IS_ERR(handle)) {
528 mlog_errno(PTR_ERR(handle));
530 goto out_free_group_bh;
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];
537 ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode),
538 group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
544 group = (struct ocfs2_group_desc *)group_bh->b_data;
545 bg_ptr = le64_to_cpu(group->bg_next_group);
546 group->bg_next_group = cr->c_blkno;
547 ocfs2_journal_dirty(handle, group_bh);
549 ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode),
550 main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE);
552 group->bg_next_group = cpu_to_le64(bg_ptr);
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));
562 cr->c_blkno = cpu_to_le64(input->group);
563 le32_add_cpu(&cr->c_total, input->clusters * cl_bpc);
564 le32_add_cpu(&cr->c_free, input->frees * cl_bpc);
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);
571 ocfs2_journal_dirty(handle, main_bm_bh);
573 spin_lock(&OCFS2_I(main_bm_inode)->ip_lock);
574 OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
575 le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits);
576 spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock);
577 i_size_write(main_bm_inode, le64_to_cpu(fe->i_size));
579 ocfs2_update_super_and_backups(main_bm_inode, input->clusters);
582 ocfs2_commit_trans(osb, handle);
590 ocfs2_inode_unlock(main_bm_inode, 1);
593 inode_unlock(main_bm_inode);