Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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 | |
| 30 | #define MLOG_MASK_PREFIX ML_DISK_ALLOC |
| 31 | #include <cluster/masklog.h> |
| 32 | |
| 33 | #include "ocfs2.h" |
| 34 | |
| 35 | #include "alloc.h" |
| 36 | #include "dlmglue.h" |
| 37 | #include "inode.h" |
| 38 | #include "journal.h" |
| 39 | #include "super.h" |
| 40 | #include "sysfile.h" |
| 41 | #include "uptodate.h" |
| 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, |
| 56 | int new_clusters, |
| 57 | u32 first_new_cluster, |
| 58 | u16 cl_cpg, |
| 59 | int set) |
| 60 | { |
| 61 | int i; |
| 62 | u16 backups = 0; |
| 63 | u32 cluster; |
| 64 | u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno); |
| 65 | |
| 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); |
| 69 | |
| 70 | gd_blkno = ocfs2_which_cluster_group(inode, cluster); |
| 71 | if (gd_blkno < lgd_blkno) |
| 72 | continue; |
| 73 | else if (gd_blkno > lgd_blkno) |
| 74 | break; |
| 75 | |
| 76 | if (set) |
| 77 | ocfs2_set_bit(cluster % cl_cpg, |
| 78 | (unsigned long *)gd->bg_bitmap); |
| 79 | else |
| 80 | ocfs2_clear_bit(cluster % cl_cpg, |
| 81 | (unsigned long *)gd->bg_bitmap); |
| 82 | backups++; |
| 83 | } |
| 84 | |
| 85 | mlog_exit_void(); |
| 86 | return backups; |
| 87 | } |
| 88 | |
| 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, |
| 94 | int new_clusters) |
| 95 | { |
| 96 | int ret = 0; |
| 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); |
| 105 | |
| 106 | mlog_entry("(new_clusters=%d, first_new_cluster = %u)\n", |
| 107 | new_clusters, first_new_cluster); |
| 108 | |
| 109 | ret = ocfs2_journal_access(handle, bm_inode, group_bh, |
| 110 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 111 | if (ret < 0) { |
| 112 | mlog_errno(ret); |
| 113 | goto out; |
| 114 | } |
| 115 | |
| 116 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 117 | |
| 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); |
| 122 | |
| 123 | /* |
| 124 | * check whether there are some new backup superblocks exist in |
| 125 | * this group and update the group bitmap accordingly. |
| 126 | */ |
| 127 | if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, |
| 128 | OCFS2_FEATURE_COMPAT_BACKUP_SB)) { |
| 129 | backups = ocfs2_calc_new_backup_super(bm_inode, |
| 130 | group, |
| 131 | new_clusters, |
| 132 | first_new_cluster, |
| 133 | cl_cpg, 1); |
| 134 | le16_add_cpu(&group->bg_free_bits_count, -1 * backups); |
| 135 | } |
| 136 | |
| 137 | ret = ocfs2_journal_dirty(handle, group_bh); |
| 138 | if (ret < 0) { |
| 139 | mlog_errno(ret); |
| 140 | goto out_rollback; |
| 141 | } |
| 142 | |
| 143 | /* update the inode accordingly. */ |
| 144 | ret = ocfs2_journal_access(handle, bm_inode, bm_bh, |
| 145 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 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); |
| 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)); |
| 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, |
| 175 | new_clusters, |
| 176 | first_new_cluster, |
| 177 | cl_cpg, 0); |
| 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); |
| 181 | } |
| 182 | out: |
| 183 | mlog_exit(ret); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | static int update_backups(struct inode * inode, u32 clusters, char *data) |
| 188 | { |
| 189 | int i, ret = 0; |
| 190 | u32 cluster; |
| 191 | u64 blkno; |
| 192 | struct buffer_head *backup = NULL; |
| 193 | struct ocfs2_dinode *backup_di = NULL; |
| 194 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 195 | |
| 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) |
| 201 | break; |
| 202 | |
| 203 | ret = ocfs2_read_block(osb, blkno, &backup, 0, NULL); |
| 204 | if (ret < 0) { |
| 205 | mlog_errno(ret); |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | memcpy(backup->b_data, data, inode->i_sb->s_blocksize); |
| 210 | |
| 211 | backup_di = (struct ocfs2_dinode *)backup->b_data; |
| 212 | backup_di->i_blkno = cpu_to_le64(blkno); |
| 213 | |
| 214 | ret = ocfs2_write_super_or_backup(osb, backup); |
| 215 | brelse(backup); |
| 216 | backup = NULL; |
| 217 | if (ret < 0) { |
| 218 | mlog_errno(ret); |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | static void ocfs2_update_super_and_backups(struct inode *inode, |
| 227 | int new_clusters) |
| 228 | { |
| 229 | int ret; |
| 230 | u32 clusters = 0; |
| 231 | struct buffer_head *super_bh = NULL; |
| 232 | struct ocfs2_dinode *super_di = NULL; |
| 233 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 234 | |
| 235 | /* |
| 236 | * update the superblock last. |
| 237 | * It doesn't matter if the write failed. |
| 238 | */ |
| 239 | ret = ocfs2_read_block(osb, OCFS2_SUPER_BLOCK_BLKNO, |
| 240 | &super_bh, 0, NULL); |
| 241 | if (ret < 0) { |
| 242 | mlog_errno(ret); |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 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); |
| 249 | |
| 250 | ret = ocfs2_write_super_or_backup(osb, super_bh); |
| 251 | if (ret < 0) { |
| 252 | mlog_errno(ret); |
| 253 | goto out; |
| 254 | } |
| 255 | |
| 256 | if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB)) |
| 257 | ret = update_backups(inode, clusters, super_bh->b_data); |
| 258 | |
| 259 | out: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 260 | brelse(super_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 261 | if (ret) |
| 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", |
| 265 | osb->dev_str); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | /* |
| 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 |
| 272 | * existing group. |
| 273 | */ |
| 274 | int ocfs2_group_extend(struct inode * inode, int new_clusters) |
| 275 | { |
| 276 | int ret; |
| 277 | handle_t *handle; |
| 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); |
| 284 | u16 cl_bpc; |
| 285 | u32 first_new_cluster; |
| 286 | u64 lgd_blkno; |
| 287 | |
| 288 | mlog_entry_void(); |
| 289 | |
| 290 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
| 291 | return -EROFS; |
| 292 | |
| 293 | if (new_clusters < 0) |
| 294 | return -EINVAL; |
| 295 | else if (new_clusters == 0) |
| 296 | return 0; |
| 297 | |
| 298 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 299 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 300 | OCFS2_INVALID_SLOT); |
| 301 | if (!main_bm_inode) { |
| 302 | ret = -EINVAL; |
| 303 | mlog_errno(ret); |
| 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | mutex_lock(&main_bm_inode->i_mutex); |
| 308 | |
| 309 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
| 310 | if (ret < 0) { |
| 311 | mlog_errno(ret); |
| 312 | goto out_mutex; |
| 313 | } |
| 314 | |
| 315 | fe = (struct ocfs2_dinode *)main_bm_bh->b_data; |
| 316 | |
| 317 | if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != |
| 318 | ocfs2_group_bitmap_size(osb->sb) * 8) { |
| 319 | mlog(ML_ERROR, "The disk is too old and small. " |
| 320 | "Force to do offline resize."); |
| 321 | ret = -EINVAL; |
| 322 | goto out_unlock; |
| 323 | } |
| 324 | |
| 325 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 326 | OCFS2_RO_ON_INVALID_DINODE(main_bm_inode->i_sb, fe); |
| 327 | ret = -EIO; |
| 328 | goto out_unlock; |
| 329 | } |
| 330 | |
| 331 | first_new_cluster = le32_to_cpu(fe->i_clusters); |
| 332 | lgd_blkno = ocfs2_which_cluster_group(main_bm_inode, |
| 333 | first_new_cluster - 1); |
| 334 | |
| 335 | ret = ocfs2_read_block(osb, lgd_blkno, &group_bh, OCFS2_BH_CACHED, |
| 336 | main_bm_inode); |
| 337 | if (ret < 0) { |
| 338 | mlog_errno(ret); |
| 339 | goto out_unlock; |
| 340 | } |
| 341 | |
| 342 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 343 | |
| 344 | ret = ocfs2_check_group_descriptor(inode->i_sb, fe, group); |
| 345 | if (ret) { |
| 346 | mlog_errno(ret); |
| 347 | goto out_unlock; |
| 348 | } |
| 349 | |
| 350 | cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc); |
| 351 | if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters > |
| 352 | le16_to_cpu(fe->id2.i_chain.cl_cpg)) { |
| 353 | ret = -EINVAL; |
| 354 | goto out_unlock; |
| 355 | } |
| 356 | |
| 357 | mlog(0, "extend the last group at %llu, new clusters = %d\n", |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 358 | (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 359 | |
| 360 | handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS); |
| 361 | if (IS_ERR(handle)) { |
| 362 | mlog_errno(PTR_ERR(handle)); |
| 363 | ret = -EINVAL; |
| 364 | goto out_unlock; |
| 365 | } |
| 366 | |
| 367 | /* update the last group descriptor and inode. */ |
| 368 | ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode, |
| 369 | main_bm_bh, group_bh, |
| 370 | first_new_cluster, |
| 371 | new_clusters); |
| 372 | if (ret) { |
| 373 | mlog_errno(ret); |
| 374 | goto out_commit; |
| 375 | } |
| 376 | |
| 377 | ocfs2_update_super_and_backups(main_bm_inode, new_clusters); |
| 378 | |
| 379 | out_commit: |
| 380 | ocfs2_commit_trans(osb, handle); |
| 381 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 382 | brelse(group_bh); |
| 383 | brelse(main_bm_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 384 | |
| 385 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 386 | |
| 387 | out_mutex: |
| 388 | mutex_unlock(&main_bm_inode->i_mutex); |
| 389 | iput(main_bm_inode); |
| 390 | |
| 391 | out: |
| 392 | mlog_exit_void(); |
| 393 | return ret; |
| 394 | } |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 395 | |
| 396 | static int ocfs2_check_new_group(struct inode *inode, |
| 397 | struct ocfs2_dinode *di, |
| 398 | struct ocfs2_new_group_input *input, |
| 399 | struct buffer_head *group_bh) |
| 400 | { |
| 401 | int ret; |
| 402 | struct ocfs2_group_desc *gd; |
| 403 | u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc); |
| 404 | unsigned int max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * |
| 405 | le16_to_cpu(di->id2.i_chain.cl_bpc); |
| 406 | |
| 407 | |
| 408 | gd = (struct ocfs2_group_desc *)group_bh->b_data; |
| 409 | |
| 410 | ret = -EIO; |
| 411 | if (!OCFS2_IS_VALID_GROUP_DESC(gd)) |
| 412 | mlog(ML_ERROR, "Group descriptor # %llu isn't valid.\n", |
| 413 | (unsigned long long)le64_to_cpu(gd->bg_blkno)); |
| 414 | else if (di->i_blkno != gd->bg_parent_dinode) |
| 415 | mlog(ML_ERROR, "Group descriptor # %llu has bad parent " |
| 416 | "pointer (%llu, expected %llu)\n", |
| 417 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 418 | (unsigned long long)le64_to_cpu(gd->bg_parent_dinode), |
| 419 | (unsigned long long)le64_to_cpu(di->i_blkno)); |
| 420 | else if (le16_to_cpu(gd->bg_bits) > max_bits) |
| 421 | mlog(ML_ERROR, "Group descriptor # %llu has bit count of %u\n", |
| 422 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 423 | le16_to_cpu(gd->bg_bits)); |
| 424 | else if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) |
| 425 | mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but " |
| 426 | "claims that %u are free\n", |
| 427 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 428 | le16_to_cpu(gd->bg_bits), |
| 429 | le16_to_cpu(gd->bg_free_bits_count)); |
| 430 | else if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) |
| 431 | mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but " |
| 432 | "max bitmap bits of %u\n", |
| 433 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 434 | le16_to_cpu(gd->bg_bits), |
| 435 | 8 * le16_to_cpu(gd->bg_size)); |
| 436 | else if (le16_to_cpu(gd->bg_chain) != input->chain) |
| 437 | mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u " |
| 438 | "while input has %u set.\n", |
| 439 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 440 | le16_to_cpu(gd->bg_chain), input->chain); |
| 441 | else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc) |
| 442 | mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but " |
| 443 | "input has %u clusters set\n", |
| 444 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 445 | le16_to_cpu(gd->bg_bits), input->clusters); |
| 446 | else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc) |
| 447 | mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u " |
| 448 | "but it should have %u set\n", |
| 449 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 450 | le16_to_cpu(gd->bg_bits), |
| 451 | input->frees * cl_bpc); |
| 452 | else |
| 453 | ret = 0; |
| 454 | |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | static int ocfs2_verify_group_and_input(struct inode *inode, |
| 459 | struct ocfs2_dinode *di, |
| 460 | struct ocfs2_new_group_input *input, |
| 461 | struct buffer_head *group_bh) |
| 462 | { |
| 463 | u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count); |
| 464 | u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg); |
| 465 | u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec); |
| 466 | u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group); |
| 467 | u32 total_clusters = le32_to_cpu(di->i_clusters); |
| 468 | int ret = -EINVAL; |
| 469 | |
| 470 | if (cluster < total_clusters) |
| 471 | mlog(ML_ERROR, "add a group which is in the current volume.\n"); |
| 472 | else if (input->chain >= cl_count) |
| 473 | mlog(ML_ERROR, "input chain exceeds the limit.\n"); |
| 474 | else if (next_free != cl_count && next_free != input->chain) |
| 475 | mlog(ML_ERROR, |
| 476 | "the add group should be in chain %u\n", next_free); |
| 477 | else if (total_clusters + input->clusters < total_clusters) |
| 478 | mlog(ML_ERROR, "add group's clusters overflow.\n"); |
| 479 | else if (input->clusters > cl_cpg) |
| 480 | mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n"); |
| 481 | else if (input->frees > input->clusters) |
| 482 | mlog(ML_ERROR, "the free cluster exceeds the total clusters\n"); |
| 483 | else if (total_clusters % cl_cpg != 0) |
| 484 | mlog(ML_ERROR, |
| 485 | "the last group isn't full. Use group extend first.\n"); |
| 486 | else if (input->group != ocfs2_which_cluster_group(inode, cluster)) |
| 487 | mlog(ML_ERROR, "group blkno is invalid\n"); |
| 488 | else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh))) |
| 489 | mlog(ML_ERROR, "group descriptor check failed.\n"); |
| 490 | else |
| 491 | ret = 0; |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | /* Add a new group descriptor to global_bitmap. */ |
| 497 | int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input) |
| 498 | { |
| 499 | int ret; |
| 500 | handle_t *handle; |
| 501 | struct buffer_head *main_bm_bh = NULL; |
| 502 | struct inode *main_bm_inode = NULL; |
| 503 | struct ocfs2_dinode *fe = NULL; |
| 504 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 505 | struct buffer_head *group_bh = NULL; |
| 506 | struct ocfs2_group_desc *group = NULL; |
| 507 | struct ocfs2_chain_list *cl; |
| 508 | struct ocfs2_chain_rec *cr; |
| 509 | u16 cl_bpc; |
| 510 | |
| 511 | mlog_entry_void(); |
| 512 | |
| 513 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
| 514 | return -EROFS; |
| 515 | |
| 516 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 517 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 518 | OCFS2_INVALID_SLOT); |
| 519 | if (!main_bm_inode) { |
| 520 | ret = -EINVAL; |
| 521 | mlog_errno(ret); |
| 522 | goto out; |
| 523 | } |
| 524 | |
| 525 | mutex_lock(&main_bm_inode->i_mutex); |
| 526 | |
| 527 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
| 528 | if (ret < 0) { |
| 529 | mlog_errno(ret); |
| 530 | goto out_mutex; |
| 531 | } |
| 532 | |
| 533 | fe = (struct ocfs2_dinode *)main_bm_bh->b_data; |
| 534 | |
| 535 | if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != |
| 536 | ocfs2_group_bitmap_size(osb->sb) * 8) { |
| 537 | mlog(ML_ERROR, "The disk is too old and small." |
| 538 | " Force to do offline resize."); |
| 539 | ret = -EINVAL; |
| 540 | goto out_unlock; |
| 541 | } |
| 542 | |
| 543 | ret = ocfs2_read_block(osb, input->group, &group_bh, 0, NULL); |
| 544 | if (ret < 0) { |
| 545 | mlog(ML_ERROR, "Can't read the group descriptor # %llu " |
| 546 | "from the device.", (unsigned long long)input->group); |
| 547 | goto out_unlock; |
| 548 | } |
| 549 | |
| 550 | ocfs2_set_new_buffer_uptodate(inode, group_bh); |
| 551 | |
| 552 | ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh); |
| 553 | if (ret) { |
| 554 | mlog_errno(ret); |
| 555 | goto out_unlock; |
| 556 | } |
| 557 | |
| 558 | mlog(0, "Add a new group %llu in chain = %u, length = %u\n", |
| 559 | (unsigned long long)input->group, input->chain, input->clusters); |
| 560 | |
| 561 | handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS); |
| 562 | if (IS_ERR(handle)) { |
| 563 | mlog_errno(PTR_ERR(handle)); |
| 564 | ret = -EINVAL; |
| 565 | goto out_unlock; |
| 566 | } |
| 567 | |
| 568 | cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc); |
| 569 | cl = &fe->id2.i_chain; |
| 570 | cr = &cl->cl_recs[input->chain]; |
| 571 | |
| 572 | ret = ocfs2_journal_access(handle, main_bm_inode, group_bh, |
| 573 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 574 | if (ret < 0) { |
| 575 | mlog_errno(ret); |
| 576 | goto out_commit; |
| 577 | } |
| 578 | |
| 579 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 580 | group->bg_next_group = cr->c_blkno; |
| 581 | |
| 582 | ret = ocfs2_journal_dirty(handle, group_bh); |
| 583 | if (ret < 0) { |
| 584 | mlog_errno(ret); |
| 585 | goto out_commit; |
| 586 | } |
| 587 | |
| 588 | ret = ocfs2_journal_access(handle, main_bm_inode, main_bm_bh, |
| 589 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 590 | if (ret < 0) { |
| 591 | mlog_errno(ret); |
| 592 | goto out_commit; |
| 593 | } |
| 594 | |
| 595 | if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) { |
| 596 | le16_add_cpu(&cl->cl_next_free_rec, 1); |
| 597 | memset(cr, 0, sizeof(struct ocfs2_chain_rec)); |
| 598 | } |
| 599 | |
| 600 | cr->c_blkno = le64_to_cpu(input->group); |
| 601 | le32_add_cpu(&cr->c_total, input->clusters * cl_bpc); |
| 602 | le32_add_cpu(&cr->c_free, input->frees * cl_bpc); |
| 603 | |
| 604 | le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc); |
| 605 | le32_add_cpu(&fe->id1.bitmap1.i_used, |
| 606 | (input->clusters - input->frees) * cl_bpc); |
| 607 | le32_add_cpu(&fe->i_clusters, input->clusters); |
| 608 | |
| 609 | ocfs2_journal_dirty(handle, main_bm_bh); |
| 610 | |
| 611 | spin_lock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 612 | OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
| 613 | le64_add_cpu(&fe->i_size, input->clusters << osb->s_clustersize_bits); |
| 614 | spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 615 | i_size_write(main_bm_inode, le64_to_cpu(fe->i_size)); |
| 616 | |
| 617 | ocfs2_update_super_and_backups(main_bm_inode, input->clusters); |
| 618 | |
| 619 | out_commit: |
| 620 | ocfs2_commit_trans(osb, handle); |
| 621 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 622 | brelse(group_bh); |
| 623 | brelse(main_bm_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 624 | |
| 625 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 626 | |
| 627 | out_mutex: |
| 628 | mutex_unlock(&main_bm_inode->i_mutex); |
| 629 | iput(main_bm_inode); |
| 630 | |
| 631 | out: |
| 632 | mlog_exit_void(); |
| 633 | return ret; |
| 634 | } |