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 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 109 | ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode), |
| 110 | group_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 137 | ocfs2_journal_dirty(handle, group_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 138 | |
| 139 | /* update the inode accordingly. */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 140 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 141 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 142 | if (ret < 0) { |
| 143 | mlog_errno(ret); |
| 144 | goto out_rollback; |
| 145 | } |
| 146 | |
| 147 | chain = le16_to_cpu(group->bg_chain); |
| 148 | cr = (&cl->cl_recs[chain]); |
| 149 | le32_add_cpu(&cr->c_total, num_bits); |
| 150 | le32_add_cpu(&cr->c_free, num_bits); |
| 151 | le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits); |
| 152 | le32_add_cpu(&fe->i_clusters, new_clusters); |
| 153 | |
| 154 | if (backups) { |
| 155 | le32_add_cpu(&cr->c_free, -1 * backups); |
| 156 | le32_add_cpu(&fe->id1.bitmap1.i_used, backups); |
| 157 | } |
| 158 | |
| 159 | spin_lock(&OCFS2_I(bm_inode)->ip_lock); |
| 160 | OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
| 161 | le64_add_cpu(&fe->i_size, new_clusters << osb->s_clustersize_bits); |
| 162 | spin_unlock(&OCFS2_I(bm_inode)->ip_lock); |
| 163 | i_size_write(bm_inode, le64_to_cpu(fe->i_size)); |
| 164 | |
| 165 | ocfs2_journal_dirty(handle, bm_bh); |
| 166 | |
| 167 | out_rollback: |
| 168 | if (ret < 0) { |
| 169 | ocfs2_calc_new_backup_super(bm_inode, |
| 170 | group, |
| 171 | new_clusters, |
| 172 | first_new_cluster, |
| 173 | cl_cpg, 0); |
| 174 | le16_add_cpu(&group->bg_free_bits_count, backups); |
| 175 | le16_add_cpu(&group->bg_bits, -1 * num_bits); |
| 176 | le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits); |
| 177 | } |
| 178 | out: |
| 179 | mlog_exit(ret); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int update_backups(struct inode * inode, u32 clusters, char *data) |
| 184 | { |
| 185 | int i, ret = 0; |
| 186 | u32 cluster; |
| 187 | u64 blkno; |
| 188 | struct buffer_head *backup = NULL; |
| 189 | struct ocfs2_dinode *backup_di = NULL; |
| 190 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 191 | |
| 192 | /* calculate the real backups we need to update. */ |
| 193 | for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) { |
| 194 | blkno = ocfs2_backup_super_blkno(inode->i_sb, i); |
| 195 | cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno); |
| 196 | if (cluster > clusters) |
| 197 | break; |
| 198 | |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 199 | ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 200 | if (ret < 0) { |
| 201 | mlog_errno(ret); |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | memcpy(backup->b_data, data, inode->i_sb->s_blocksize); |
| 206 | |
| 207 | backup_di = (struct ocfs2_dinode *)backup->b_data; |
| 208 | backup_di->i_blkno = cpu_to_le64(blkno); |
| 209 | |
| 210 | ret = ocfs2_write_super_or_backup(osb, backup); |
| 211 | brelse(backup); |
| 212 | backup = NULL; |
| 213 | if (ret < 0) { |
| 214 | mlog_errno(ret); |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static void ocfs2_update_super_and_backups(struct inode *inode, |
| 223 | int new_clusters) |
| 224 | { |
| 225 | int ret; |
| 226 | u32 clusters = 0; |
| 227 | struct buffer_head *super_bh = NULL; |
| 228 | struct ocfs2_dinode *super_di = NULL; |
| 229 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 230 | |
| 231 | /* |
| 232 | * update the superblock last. |
| 233 | * It doesn't matter if the write failed. |
| 234 | */ |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 235 | ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1, |
| 236 | &super_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 237 | if (ret < 0) { |
| 238 | mlog_errno(ret); |
| 239 | goto out; |
| 240 | } |
| 241 | |
| 242 | super_di = (struct ocfs2_dinode *)super_bh->b_data; |
| 243 | le32_add_cpu(&super_di->i_clusters, new_clusters); |
| 244 | clusters = le32_to_cpu(super_di->i_clusters); |
| 245 | |
| 246 | ret = ocfs2_write_super_or_backup(osb, super_bh); |
| 247 | if (ret < 0) { |
| 248 | mlog_errno(ret); |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB)) |
| 253 | ret = update_backups(inode, clusters, super_bh->b_data); |
| 254 | |
| 255 | out: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 256 | brelse(super_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 257 | if (ret) |
| 258 | printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s" |
| 259 | " during fs resize. This condition is not fatal," |
| 260 | " but fsck.ocfs2 should be run to fix it\n", |
| 261 | osb->dev_str); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Extend the filesystem to the new number of clusters specified. This entry |
| 267 | * point is only used to extend the current filesystem to the end of the last |
| 268 | * existing group. |
| 269 | */ |
| 270 | int ocfs2_group_extend(struct inode * inode, int new_clusters) |
| 271 | { |
| 272 | int ret; |
| 273 | handle_t *handle; |
| 274 | struct buffer_head *main_bm_bh = NULL; |
| 275 | struct buffer_head *group_bh = NULL; |
| 276 | struct inode *main_bm_inode = NULL; |
| 277 | struct ocfs2_dinode *fe = NULL; |
| 278 | struct ocfs2_group_desc *group = NULL; |
| 279 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 280 | u16 cl_bpc; |
| 281 | u32 first_new_cluster; |
| 282 | u64 lgd_blkno; |
| 283 | |
| 284 | mlog_entry_void(); |
| 285 | |
| 286 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
| 287 | return -EROFS; |
| 288 | |
| 289 | if (new_clusters < 0) |
| 290 | return -EINVAL; |
| 291 | else if (new_clusters == 0) |
| 292 | return 0; |
| 293 | |
| 294 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 295 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 296 | OCFS2_INVALID_SLOT); |
| 297 | if (!main_bm_inode) { |
| 298 | ret = -EINVAL; |
| 299 | mlog_errno(ret); |
| 300 | goto out; |
| 301 | } |
| 302 | |
| 303 | mutex_lock(&main_bm_inode->i_mutex); |
| 304 | |
| 305 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
| 306 | if (ret < 0) { |
| 307 | mlog_errno(ret); |
| 308 | goto out_mutex; |
| 309 | } |
| 310 | |
| 311 | fe = (struct ocfs2_dinode *)main_bm_bh->b_data; |
| 312 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 313 | /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(), |
| 314 | * so any corruption is a code bug. */ |
| 315 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); |
| 316 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 317 | if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != |
Tao Ma | 8571882 | 2010-04-13 14:38:06 +0800 | [diff] [blame^] | 318 | ocfs2_group_bitmap_size(osb->sb, 0, |
| 319 | osb->s_feature_incompat) * 8) { |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 320 | mlog(ML_ERROR, "The disk is too old and small. " |
| 321 | "Force to do offline resize."); |
| 322 | ret = -EINVAL; |
| 323 | goto out_unlock; |
| 324 | } |
| 325 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 326 | first_new_cluster = le32_to_cpu(fe->i_clusters); |
| 327 | lgd_blkno = ocfs2_which_cluster_group(main_bm_inode, |
| 328 | first_new_cluster - 1); |
| 329 | |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 330 | ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno, |
| 331 | &group_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 332 | if (ret < 0) { |
| 333 | mlog_errno(ret); |
| 334 | goto out_unlock; |
| 335 | } |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 336 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 337 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 338 | cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc); |
| 339 | if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters > |
| 340 | le16_to_cpu(fe->id2.i_chain.cl_cpg)) { |
| 341 | ret = -EINVAL; |
| 342 | goto out_unlock; |
| 343 | } |
| 344 | |
| 345 | mlog(0, "extend the last group at %llu, new clusters = %d\n", |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 346 | (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 347 | |
| 348 | handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS); |
| 349 | if (IS_ERR(handle)) { |
| 350 | mlog_errno(PTR_ERR(handle)); |
| 351 | ret = -EINVAL; |
| 352 | goto out_unlock; |
| 353 | } |
| 354 | |
| 355 | /* update the last group descriptor and inode. */ |
| 356 | ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode, |
| 357 | main_bm_bh, group_bh, |
| 358 | first_new_cluster, |
| 359 | new_clusters); |
| 360 | if (ret) { |
| 361 | mlog_errno(ret); |
| 362 | goto out_commit; |
| 363 | } |
| 364 | |
| 365 | ocfs2_update_super_and_backups(main_bm_inode, new_clusters); |
| 366 | |
| 367 | out_commit: |
| 368 | ocfs2_commit_trans(osb, handle); |
| 369 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 370 | brelse(group_bh); |
| 371 | brelse(main_bm_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 372 | |
| 373 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 374 | |
| 375 | out_mutex: |
| 376 | mutex_unlock(&main_bm_inode->i_mutex); |
| 377 | iput(main_bm_inode); |
| 378 | |
| 379 | out: |
| 380 | mlog_exit_void(); |
| 381 | return ret; |
| 382 | } |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 383 | |
| 384 | static int ocfs2_check_new_group(struct inode *inode, |
| 385 | struct ocfs2_dinode *di, |
| 386 | struct ocfs2_new_group_input *input, |
| 387 | struct buffer_head *group_bh) |
| 388 | { |
| 389 | int ret; |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 390 | struct ocfs2_group_desc *gd = |
| 391 | (struct ocfs2_group_desc *)group_bh->b_data; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 392 | u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 393 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 394 | ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh); |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 395 | if (ret) |
| 396 | goto out; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 397 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 398 | ret = -EINVAL; |
| 399 | if (le16_to_cpu(gd->bg_chain) != input->chain) |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 400 | mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u " |
| 401 | "while input has %u set.\n", |
| 402 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 403 | le16_to_cpu(gd->bg_chain), input->chain); |
| 404 | else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc) |
| 405 | mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but " |
| 406 | "input has %u clusters set\n", |
| 407 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 408 | le16_to_cpu(gd->bg_bits), input->clusters); |
| 409 | else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc) |
| 410 | mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u " |
| 411 | "but it should have %u set\n", |
| 412 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 413 | le16_to_cpu(gd->bg_bits), |
| 414 | input->frees * cl_bpc); |
| 415 | else |
| 416 | ret = 0; |
| 417 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 418 | out: |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static int ocfs2_verify_group_and_input(struct inode *inode, |
| 423 | struct ocfs2_dinode *di, |
| 424 | struct ocfs2_new_group_input *input, |
| 425 | struct buffer_head *group_bh) |
| 426 | { |
| 427 | u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count); |
| 428 | u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg); |
| 429 | u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec); |
| 430 | u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group); |
| 431 | u32 total_clusters = le32_to_cpu(di->i_clusters); |
| 432 | int ret = -EINVAL; |
| 433 | |
| 434 | if (cluster < total_clusters) |
| 435 | mlog(ML_ERROR, "add a group which is in the current volume.\n"); |
| 436 | else if (input->chain >= cl_count) |
| 437 | mlog(ML_ERROR, "input chain exceeds the limit.\n"); |
| 438 | else if (next_free != cl_count && next_free != input->chain) |
| 439 | mlog(ML_ERROR, |
| 440 | "the add group should be in chain %u\n", next_free); |
| 441 | else if (total_clusters + input->clusters < total_clusters) |
| 442 | mlog(ML_ERROR, "add group's clusters overflow.\n"); |
| 443 | else if (input->clusters > cl_cpg) |
| 444 | mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n"); |
| 445 | else if (input->frees > input->clusters) |
| 446 | mlog(ML_ERROR, "the free cluster exceeds the total clusters\n"); |
| 447 | else if (total_clusters % cl_cpg != 0) |
| 448 | mlog(ML_ERROR, |
| 449 | "the last group isn't full. Use group extend first.\n"); |
| 450 | else if (input->group != ocfs2_which_cluster_group(inode, cluster)) |
| 451 | mlog(ML_ERROR, "group blkno is invalid\n"); |
| 452 | else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh))) |
| 453 | mlog(ML_ERROR, "group descriptor check failed.\n"); |
| 454 | else |
| 455 | ret = 0; |
| 456 | |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | /* Add a new group descriptor to global_bitmap. */ |
| 461 | int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input) |
| 462 | { |
| 463 | int ret; |
| 464 | handle_t *handle; |
| 465 | struct buffer_head *main_bm_bh = NULL; |
| 466 | struct inode *main_bm_inode = NULL; |
| 467 | struct ocfs2_dinode *fe = NULL; |
| 468 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 469 | struct buffer_head *group_bh = NULL; |
| 470 | struct ocfs2_group_desc *group = NULL; |
| 471 | struct ocfs2_chain_list *cl; |
| 472 | struct ocfs2_chain_rec *cr; |
| 473 | u16 cl_bpc; |
| 474 | |
| 475 | mlog_entry_void(); |
| 476 | |
| 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 | |
| 489 | mutex_lock(&main_bm_inode->i_mutex); |
| 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) != |
Tao Ma | 8571882 | 2010-04-13 14:38:06 +0800 | [diff] [blame^] | 500 | ocfs2_group_bitmap_size(osb->sb, 0, |
| 501 | osb->s_feature_incompat) * 8) { |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 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 | |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 508 | ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 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 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 515 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 516 | |
| 517 | ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh); |
| 518 | if (ret) { |
| 519 | mlog_errno(ret); |
| 520 | goto out_unlock; |
| 521 | } |
| 522 | |
| 523 | mlog(0, "Add a new group %llu in chain = %u, length = %u\n", |
| 524 | (unsigned long long)input->group, input->chain, input->clusters); |
| 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; |
| 530 | goto out_unlock; |
| 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 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 537 | ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode), |
| 538 | group_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 539 | if (ret < 0) { |
| 540 | mlog_errno(ret); |
| 541 | goto out_commit; |
| 542 | } |
| 543 | |
| 544 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 545 | group->bg_next_group = cr->c_blkno; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 546 | ocfs2_journal_dirty(handle, group_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 547 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 548 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode), |
| 549 | main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 550 | if (ret < 0) { |
| 551 | mlog_errno(ret); |
| 552 | goto out_commit; |
| 553 | } |
| 554 | |
| 555 | if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) { |
| 556 | le16_add_cpu(&cl->cl_next_free_rec, 1); |
| 557 | memset(cr, 0, sizeof(struct ocfs2_chain_rec)); |
| 558 | } |
| 559 | |
Tao Ma | 4338ab6 | 2008-03-03 10:53:02 +0800 | [diff] [blame] | 560 | cr->c_blkno = cpu_to_le64(input->group); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 561 | le32_add_cpu(&cr->c_total, input->clusters * cl_bpc); |
| 562 | le32_add_cpu(&cr->c_free, input->frees * cl_bpc); |
| 563 | |
| 564 | le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc); |
| 565 | le32_add_cpu(&fe->id1.bitmap1.i_used, |
| 566 | (input->clusters - input->frees) * cl_bpc); |
| 567 | le32_add_cpu(&fe->i_clusters, input->clusters); |
| 568 | |
| 569 | ocfs2_journal_dirty(handle, main_bm_bh); |
| 570 | |
| 571 | spin_lock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 572 | OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
| 573 | le64_add_cpu(&fe->i_size, input->clusters << osb->s_clustersize_bits); |
| 574 | spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 575 | i_size_write(main_bm_inode, le64_to_cpu(fe->i_size)); |
| 576 | |
| 577 | ocfs2_update_super_and_backups(main_bm_inode, input->clusters); |
| 578 | |
| 579 | out_commit: |
| 580 | ocfs2_commit_trans(osb, handle); |
| 581 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 582 | brelse(group_bh); |
| 583 | brelse(main_bm_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 584 | |
| 585 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 586 | |
| 587 | out_mutex: |
| 588 | mutex_unlock(&main_bm_inode->i_mutex); |
| 589 | iput(main_bm_inode); |
| 590 | |
| 591 | out: |
| 592 | mlog_exit_void(); |
| 593 | return ret; |
| 594 | } |