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 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 30 | #include <cluster/masklog.h> |
| 31 | |
| 32 | #include "ocfs2.h" |
| 33 | |
| 34 | #include "alloc.h" |
| 35 | #include "dlmglue.h" |
| 36 | #include "inode.h" |
| 37 | #include "journal.h" |
| 38 | #include "super.h" |
| 39 | #include "sysfile.h" |
| 40 | #include "uptodate.h" |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 41 | #include "ocfs2_trace.h" |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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, |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 56 | u16 cl_cpg, |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 57 | u16 old_bg_clusters, |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 58 | int set) |
| 59 | { |
| 60 | int i; |
| 61 | u16 backups = 0; |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 62 | u32 cluster, lgd_cluster; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 63 | u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno); |
| 64 | |
| 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); |
| 68 | |
| 69 | gd_blkno = ocfs2_which_cluster_group(inode, cluster); |
| 70 | if (gd_blkno < lgd_blkno) |
| 71 | continue; |
| 72 | else if (gd_blkno > lgd_blkno) |
| 73 | break; |
| 74 | |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 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) |
| 79 | continue; |
| 80 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 81 | if (set) |
| 82 | ocfs2_set_bit(cluster % cl_cpg, |
| 83 | (unsigned long *)gd->bg_bitmap); |
| 84 | else |
| 85 | ocfs2_clear_bit(cluster % cl_cpg, |
| 86 | (unsigned long *)gd->bg_bitmap); |
| 87 | backups++; |
| 88 | } |
| 89 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 90 | return backups; |
| 91 | } |
| 92 | |
| 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, |
| 98 | int new_clusters) |
| 99 | { |
| 100 | int ret = 0; |
| 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); |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 109 | u16 old_bg_clusters; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 110 | |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 111 | trace_ocfs2_update_last_group_and_inode(new_clusters, |
| 112 | first_new_cluster); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 113 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 114 | ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode), |
| 115 | group_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 116 | if (ret < 0) { |
| 117 | mlog_errno(ret); |
| 118 | goto out; |
| 119 | } |
| 120 | |
| 121 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 122 | |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 123 | old_bg_clusters = le16_to_cpu(group->bg_bits) / cl_bpc; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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); |
| 128 | |
| 129 | /* |
| 130 | * check whether there are some new backup superblocks exist in |
| 131 | * this group and update the group bitmap accordingly. |
| 132 | */ |
| 133 | if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, |
| 134 | OCFS2_FEATURE_COMPAT_BACKUP_SB)) { |
| 135 | backups = ocfs2_calc_new_backup_super(bm_inode, |
| 136 | group, |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 137 | cl_cpg, old_bg_clusters, 1); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 138 | le16_add_cpu(&group->bg_free_bits_count, -1 * backups); |
| 139 | } |
| 140 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 141 | ocfs2_journal_dirty(handle, group_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 142 | |
| 143 | /* update the inode accordingly. */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 144 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 145 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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); |
Joseph Qi | 17bf141 | 2014-06-04 16:06:17 -0700 | [diff] [blame] | 165 | le64_add_cpu(&fe->i_size, (u64)new_clusters << osb->s_clustersize_bits); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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, |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 175 | cl_cpg, old_bg_clusters, 0); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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); |
| 179 | } |
| 180 | out: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 181 | if (ret) |
| 182 | mlog_errno(ret); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | static int update_backups(struct inode * inode, u32 clusters, char *data) |
| 187 | { |
| 188 | int i, ret = 0; |
| 189 | u32 cluster; |
| 190 | u64 blkno; |
| 191 | struct buffer_head *backup = NULL; |
| 192 | struct ocfs2_dinode *backup_di = NULL; |
| 193 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 194 | |
| 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); |
jiangyiwen | 584dca3 | 2016-03-25 14:21:35 -0700 | [diff] [blame] | 199 | if (cluster >= clusters) |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 200 | break; |
| 201 | |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 202 | ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 203 | if (ret < 0) { |
| 204 | mlog_errno(ret); |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | memcpy(backup->b_data, data, inode->i_sb->s_blocksize); |
| 209 | |
| 210 | backup_di = (struct ocfs2_dinode *)backup->b_data; |
| 211 | backup_di->i_blkno = cpu_to_le64(blkno); |
| 212 | |
| 213 | ret = ocfs2_write_super_or_backup(osb, backup); |
| 214 | brelse(backup); |
| 215 | backup = NULL; |
| 216 | if (ret < 0) { |
| 217 | mlog_errno(ret); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | static void ocfs2_update_super_and_backups(struct inode *inode, |
| 226 | int new_clusters) |
| 227 | { |
| 228 | int ret; |
| 229 | u32 clusters = 0; |
| 230 | struct buffer_head *super_bh = NULL; |
| 231 | struct ocfs2_dinode *super_di = NULL; |
| 232 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 233 | |
| 234 | /* |
| 235 | * update the superblock last. |
| 236 | * It doesn't matter if the write failed. |
| 237 | */ |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 238 | ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1, |
| 239 | &super_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 240 | if (ret < 0) { |
| 241 | mlog_errno(ret); |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 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); |
| 248 | |
| 249 | ret = ocfs2_write_super_or_backup(osb, super_bh); |
| 250 | if (ret < 0) { |
| 251 | mlog_errno(ret); |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB)) |
| 256 | ret = update_backups(inode, clusters, super_bh->b_data); |
| 257 | |
| 258 | out: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 259 | brelse(super_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 260 | if (ret) |
| 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", |
| 264 | osb->dev_str); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | /* |
| 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 |
| 271 | * existing group. |
| 272 | */ |
| 273 | int ocfs2_group_extend(struct inode * inode, int new_clusters) |
| 274 | { |
| 275 | int ret; |
| 276 | handle_t *handle; |
| 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); |
| 283 | u16 cl_bpc; |
| 284 | u32 first_new_cluster; |
| 285 | u64 lgd_blkno; |
| 286 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 287 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
| 288 | return -EROFS; |
| 289 | |
| 290 | if (new_clusters < 0) |
| 291 | return -EINVAL; |
| 292 | else if (new_clusters == 0) |
| 293 | return 0; |
| 294 | |
| 295 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 296 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 297 | OCFS2_INVALID_SLOT); |
| 298 | if (!main_bm_inode) { |
| 299 | ret = -EINVAL; |
| 300 | mlog_errno(ret); |
| 301 | goto out; |
| 302 | } |
| 303 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 304 | inode_lock(main_bm_inode); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 305 | |
| 306 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
| 307 | if (ret < 0) { |
| 308 | mlog_errno(ret); |
| 309 | goto out_mutex; |
| 310 | } |
| 311 | |
| 312 | fe = (struct ocfs2_dinode *)main_bm_bh->b_data; |
| 313 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 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)); |
| 317 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 318 | if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != |
Tao Ma | 8571882 | 2010-04-13 14:38:06 +0800 | [diff] [blame] | 319 | ocfs2_group_bitmap_size(osb->sb, 0, |
| 320 | osb->s_feature_incompat) * 8) { |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 321 | mlog(ML_ERROR, "The disk is too old and small. " |
| 322 | "Force to do offline resize."); |
| 323 | ret = -EINVAL; |
| 324 | goto out_unlock; |
| 325 | } |
| 326 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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); |
| 330 | |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 331 | ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno, |
| 332 | &group_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 333 | if (ret < 0) { |
| 334 | mlog_errno(ret); |
| 335 | goto out_unlock; |
| 336 | } |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 337 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 338 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 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)) { |
| 342 | ret = -EINVAL; |
| 343 | goto out_unlock; |
| 344 | } |
| 345 | |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 346 | |
| 347 | trace_ocfs2_group_extend( |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 348 | (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 349 | |
| 350 | handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS); |
| 351 | if (IS_ERR(handle)) { |
| 352 | mlog_errno(PTR_ERR(handle)); |
| 353 | ret = -EINVAL; |
| 354 | goto out_unlock; |
| 355 | } |
| 356 | |
| 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, |
| 360 | first_new_cluster, |
| 361 | new_clusters); |
| 362 | if (ret) { |
| 363 | mlog_errno(ret); |
| 364 | goto out_commit; |
| 365 | } |
| 366 | |
| 367 | ocfs2_update_super_and_backups(main_bm_inode, new_clusters); |
| 368 | |
| 369 | out_commit: |
| 370 | ocfs2_commit_trans(osb, handle); |
| 371 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 372 | brelse(group_bh); |
| 373 | brelse(main_bm_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 374 | |
| 375 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 376 | |
| 377 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 378 | inode_unlock(main_bm_inode); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 379 | iput(main_bm_inode); |
| 380 | |
| 381 | out: |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 382 | return ret; |
| 383 | } |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 384 | |
| 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) |
| 389 | { |
| 390 | int ret; |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 391 | struct ocfs2_group_desc *gd = |
| 392 | (struct ocfs2_group_desc *)group_bh->b_data; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 393 | u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 394 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 395 | ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh); |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 396 | if (ret) |
| 397 | goto out; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 398 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 399 | ret = -EINVAL; |
| 400 | if (le16_to_cpu(gd->bg_chain) != input->chain) |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 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); |
| 416 | else |
| 417 | ret = 0; |
| 418 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 419 | out: |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 420 | return ret; |
| 421 | } |
| 422 | |
| 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) |
| 427 | { |
| 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); |
| 433 | int ret = -EINVAL; |
| 434 | |
| 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) |
| 440 | mlog(ML_ERROR, |
| 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) |
| 449 | mlog(ML_ERROR, |
| 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"); |
| 455 | else |
| 456 | ret = 0; |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /* Add a new group descriptor to global_bitmap. */ |
| 462 | int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input) |
| 463 | { |
| 464 | int ret; |
| 465 | handle_t *handle; |
| 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; |
| 474 | u16 cl_bpc; |
Younger Liu | eedd40e | 2013-11-12 15:07:03 -0800 | [diff] [blame] | 475 | u64 bg_ptr; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 476 | |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 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 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 489 | inode_lock(main_bm_inode); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 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); |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 520 | goto out_free_group_bh; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 521 | } |
| 522 | |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 523 | trace_ocfs2_group_add((unsigned long long)input->group, |
| 524 | input->chain, input->clusters, input->frees); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 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; |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 530 | goto out_free_group_bh; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 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; |
Younger Liu | eedd40e | 2013-11-12 15:07:03 -0800 | [diff] [blame] | 545 | bg_ptr = le64_to_cpu(group->bg_next_group); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 546 | group->bg_next_group = cr->c_blkno; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 547 | ocfs2_journal_dirty(handle, group_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 548 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 549 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode), |
| 550 | main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 551 | if (ret < 0) { |
Younger Liu | eedd40e | 2013-11-12 15:07:03 -0800 | [diff] [blame] | 552 | group->bg_next_group = cpu_to_le64(bg_ptr); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 553 | mlog_errno(ret); |
| 554 | goto out_commit; |
| 555 | } |
| 556 | |
| 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)); |
| 560 | } |
| 561 | |
Tao Ma | 4338ab6 | 2008-03-03 10:53:02 +0800 | [diff] [blame] | 562 | cr->c_blkno = cpu_to_le64(input->group); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 563 | le32_add_cpu(&cr->c_total, input->clusters * cl_bpc); |
| 564 | le32_add_cpu(&cr->c_free, input->frees * cl_bpc); |
| 565 | |
| 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); |
| 570 | |
| 571 | ocfs2_journal_dirty(handle, main_bm_bh); |
| 572 | |
| 573 | spin_lock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 574 | OCFS2_I(main_bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
Joseph Qi | 17bf141 | 2014-06-04 16:06:17 -0700 | [diff] [blame] | 575 | le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 576 | spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 577 | i_size_write(main_bm_inode, le64_to_cpu(fe->i_size)); |
| 578 | |
| 579 | ocfs2_update_super_and_backups(main_bm_inode, input->clusters); |
| 580 | |
| 581 | out_commit: |
| 582 | ocfs2_commit_trans(osb, handle); |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 583 | |
| 584 | out_free_group_bh: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 585 | brelse(group_bh); |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 586 | |
| 587 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 588 | brelse(main_bm_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 589 | |
| 590 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 591 | |
| 592 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 593 | inode_unlock(main_bm_inode); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 594 | iput(main_bm_inode); |
| 595 | |
| 596 | out: |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 597 | return ret; |
| 598 | } |