Thomas Gleixner | 328970d | 2019-05-24 12:04:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 2 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 3 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 4 | * |
| 5 | * resize.c |
| 6 | * |
| 7 | * volume resize. |
| 8 | * Inspired by ext3/resize.c. |
| 9 | * |
| 10 | * Copyright (C) 2007 Oracle. All rights reserved. |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/types.h> |
| 15 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 16 | #include <cluster/masklog.h> |
| 17 | |
| 18 | #include "ocfs2.h" |
| 19 | |
| 20 | #include "alloc.h" |
| 21 | #include "dlmglue.h" |
| 22 | #include "inode.h" |
| 23 | #include "journal.h" |
| 24 | #include "super.h" |
| 25 | #include "sysfile.h" |
| 26 | #include "uptodate.h" |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 27 | #include "ocfs2_trace.h" |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 28 | |
| 29 | #include "buffer_head_io.h" |
| 30 | #include "suballoc.h" |
| 31 | #include "resize.h" |
| 32 | |
| 33 | /* |
| 34 | * Check whether there are new backup superblocks exist |
| 35 | * in the last group. If there are some, mark them or clear |
| 36 | * them in the bitmap. |
| 37 | * |
| 38 | * Return how many backups we find in the last group. |
| 39 | */ |
| 40 | static u16 ocfs2_calc_new_backup_super(struct inode *inode, |
| 41 | struct ocfs2_group_desc *gd, |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 42 | u16 cl_cpg, |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 43 | u16 old_bg_clusters, |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 44 | int set) |
| 45 | { |
| 46 | int i; |
| 47 | u16 backups = 0; |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 48 | u32 cluster, lgd_cluster; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 49 | u64 blkno, gd_blkno, lgd_blkno = le64_to_cpu(gd->bg_blkno); |
| 50 | |
| 51 | for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) { |
| 52 | blkno = ocfs2_backup_super_blkno(inode->i_sb, i); |
| 53 | cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno); |
| 54 | |
| 55 | gd_blkno = ocfs2_which_cluster_group(inode, cluster); |
| 56 | if (gd_blkno < lgd_blkno) |
| 57 | continue; |
| 58 | else if (gd_blkno > lgd_blkno) |
| 59 | break; |
| 60 | |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 61 | /* check if already done backup super */ |
| 62 | lgd_cluster = ocfs2_blocks_to_clusters(inode->i_sb, lgd_blkno); |
| 63 | lgd_cluster += old_bg_clusters; |
| 64 | if (lgd_cluster >= cluster) |
| 65 | continue; |
| 66 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 67 | if (set) |
| 68 | ocfs2_set_bit(cluster % cl_cpg, |
| 69 | (unsigned long *)gd->bg_bitmap); |
| 70 | else |
| 71 | ocfs2_clear_bit(cluster % cl_cpg, |
| 72 | (unsigned long *)gd->bg_bitmap); |
| 73 | backups++; |
| 74 | } |
| 75 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 76 | return backups; |
| 77 | } |
| 78 | |
| 79 | static int ocfs2_update_last_group_and_inode(handle_t *handle, |
| 80 | struct inode *bm_inode, |
| 81 | struct buffer_head *bm_bh, |
| 82 | struct buffer_head *group_bh, |
| 83 | u32 first_new_cluster, |
| 84 | int new_clusters) |
| 85 | { |
| 86 | int ret = 0; |
| 87 | struct ocfs2_super *osb = OCFS2_SB(bm_inode->i_sb); |
| 88 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bm_bh->b_data; |
| 89 | struct ocfs2_chain_list *cl = &fe->id2.i_chain; |
| 90 | struct ocfs2_chain_rec *cr; |
| 91 | struct ocfs2_group_desc *group; |
| 92 | u16 chain, num_bits, backups = 0; |
| 93 | u16 cl_bpc = le16_to_cpu(cl->cl_bpc); |
| 94 | u16 cl_cpg = le16_to_cpu(cl->cl_cpg); |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 95 | u16 old_bg_clusters; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 96 | |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 97 | trace_ocfs2_update_last_group_and_inode(new_clusters, |
| 98 | first_new_cluster); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 99 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 100 | ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode), |
| 101 | group_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 102 | if (ret < 0) { |
| 103 | mlog_errno(ret); |
| 104 | goto out; |
| 105 | } |
| 106 | |
| 107 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 108 | |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 109 | old_bg_clusters = le16_to_cpu(group->bg_bits) / cl_bpc; |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 110 | /* update the group first. */ |
| 111 | num_bits = new_clusters * cl_bpc; |
| 112 | le16_add_cpu(&group->bg_bits, num_bits); |
| 113 | le16_add_cpu(&group->bg_free_bits_count, num_bits); |
| 114 | |
| 115 | /* |
| 116 | * check whether there are some new backup superblocks exist in |
| 117 | * this group and update the group bitmap accordingly. |
| 118 | */ |
| 119 | if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, |
| 120 | OCFS2_FEATURE_COMPAT_BACKUP_SB)) { |
| 121 | backups = ocfs2_calc_new_backup_super(bm_inode, |
| 122 | group, |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 123 | cl_cpg, old_bg_clusters, 1); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 124 | le16_add_cpu(&group->bg_free_bits_count, -1 * backups); |
| 125 | } |
| 126 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 127 | ocfs2_journal_dirty(handle, group_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 128 | |
| 129 | /* update the inode accordingly. */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 130 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 131 | OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 132 | if (ret < 0) { |
| 133 | mlog_errno(ret); |
| 134 | goto out_rollback; |
| 135 | } |
| 136 | |
| 137 | chain = le16_to_cpu(group->bg_chain); |
| 138 | cr = (&cl->cl_recs[chain]); |
| 139 | le32_add_cpu(&cr->c_total, num_bits); |
| 140 | le32_add_cpu(&cr->c_free, num_bits); |
| 141 | le32_add_cpu(&fe->id1.bitmap1.i_total, num_bits); |
| 142 | le32_add_cpu(&fe->i_clusters, new_clusters); |
| 143 | |
| 144 | if (backups) { |
| 145 | le32_add_cpu(&cr->c_free, -1 * backups); |
| 146 | le32_add_cpu(&fe->id1.bitmap1.i_used, backups); |
| 147 | } |
| 148 | |
| 149 | spin_lock(&OCFS2_I(bm_inode)->ip_lock); |
| 150 | OCFS2_I(bm_inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
Joseph Qi | 17bf141 | 2014-06-04 16:06:17 -0700 | [diff] [blame] | 151 | 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] | 152 | spin_unlock(&OCFS2_I(bm_inode)->ip_lock); |
| 153 | i_size_write(bm_inode, le64_to_cpu(fe->i_size)); |
| 154 | |
| 155 | ocfs2_journal_dirty(handle, bm_bh); |
| 156 | |
| 157 | out_rollback: |
| 158 | if (ret < 0) { |
| 159 | ocfs2_calc_new_backup_super(bm_inode, |
| 160 | group, |
Joseph Qi | 5c9ee4c | 2015-12-29 14:54:06 -0800 | [diff] [blame] | 161 | cl_cpg, old_bg_clusters, 0); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 162 | le16_add_cpu(&group->bg_free_bits_count, backups); |
| 163 | le16_add_cpu(&group->bg_bits, -1 * num_bits); |
| 164 | le16_add_cpu(&group->bg_free_bits_count, -1 * num_bits); |
| 165 | } |
| 166 | out: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 167 | if (ret) |
| 168 | mlog_errno(ret); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int update_backups(struct inode * inode, u32 clusters, char *data) |
| 173 | { |
| 174 | int i, ret = 0; |
| 175 | u32 cluster; |
| 176 | u64 blkno; |
| 177 | struct buffer_head *backup = NULL; |
| 178 | struct ocfs2_dinode *backup_di = NULL; |
| 179 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 180 | |
| 181 | /* calculate the real backups we need to update. */ |
| 182 | for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) { |
| 183 | blkno = ocfs2_backup_super_blkno(inode->i_sb, i); |
| 184 | cluster = ocfs2_blocks_to_clusters(inode->i_sb, blkno); |
jiangyiwen | 584dca3 | 2016-03-25 14:21:35 -0700 | [diff] [blame] | 185 | if (cluster >= clusters) |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 186 | break; |
| 187 | |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 188 | ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 189 | if (ret < 0) { |
| 190 | mlog_errno(ret); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | memcpy(backup->b_data, data, inode->i_sb->s_blocksize); |
| 195 | |
| 196 | backup_di = (struct ocfs2_dinode *)backup->b_data; |
| 197 | backup_di->i_blkno = cpu_to_le64(blkno); |
| 198 | |
| 199 | ret = ocfs2_write_super_or_backup(osb, backup); |
| 200 | brelse(backup); |
| 201 | backup = NULL; |
| 202 | if (ret < 0) { |
| 203 | mlog_errno(ret); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static void ocfs2_update_super_and_backups(struct inode *inode, |
| 212 | int new_clusters) |
| 213 | { |
| 214 | int ret; |
| 215 | u32 clusters = 0; |
| 216 | struct buffer_head *super_bh = NULL; |
| 217 | struct ocfs2_dinode *super_di = NULL; |
| 218 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 219 | |
| 220 | /* |
| 221 | * update the superblock last. |
| 222 | * It doesn't matter if the write failed. |
| 223 | */ |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 224 | ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1, |
| 225 | &super_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 226 | if (ret < 0) { |
| 227 | mlog_errno(ret); |
| 228 | goto out; |
| 229 | } |
| 230 | |
| 231 | super_di = (struct ocfs2_dinode *)super_bh->b_data; |
| 232 | le32_add_cpu(&super_di->i_clusters, new_clusters); |
| 233 | clusters = le32_to_cpu(super_di->i_clusters); |
| 234 | |
| 235 | ret = ocfs2_write_super_or_backup(osb, super_bh); |
| 236 | if (ret < 0) { |
| 237 | mlog_errno(ret); |
| 238 | goto out; |
| 239 | } |
| 240 | |
| 241 | if (OCFS2_HAS_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_COMPAT_BACKUP_SB)) |
| 242 | ret = update_backups(inode, clusters, super_bh->b_data); |
| 243 | |
| 244 | out: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 245 | brelse(super_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 246 | if (ret) |
| 247 | printk(KERN_WARNING "ocfs2: Failed to update super blocks on %s" |
| 248 | " during fs resize. This condition is not fatal," |
| 249 | " but fsck.ocfs2 should be run to fix it\n", |
| 250 | osb->dev_str); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Extend the filesystem to the new number of clusters specified. This entry |
| 256 | * point is only used to extend the current filesystem to the end of the last |
| 257 | * existing group. |
| 258 | */ |
| 259 | int ocfs2_group_extend(struct inode * inode, int new_clusters) |
| 260 | { |
| 261 | int ret; |
| 262 | handle_t *handle; |
| 263 | struct buffer_head *main_bm_bh = NULL; |
| 264 | struct buffer_head *group_bh = NULL; |
| 265 | struct inode *main_bm_inode = NULL; |
| 266 | struct ocfs2_dinode *fe = NULL; |
| 267 | struct ocfs2_group_desc *group = NULL; |
| 268 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 269 | u16 cl_bpc; |
| 270 | u32 first_new_cluster; |
| 271 | u64 lgd_blkno; |
| 272 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 273 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
| 274 | return -EROFS; |
| 275 | |
| 276 | if (new_clusters < 0) |
| 277 | return -EINVAL; |
| 278 | else if (new_clusters == 0) |
| 279 | return 0; |
| 280 | |
| 281 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 282 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 283 | OCFS2_INVALID_SLOT); |
| 284 | if (!main_bm_inode) { |
| 285 | ret = -EINVAL; |
| 286 | mlog_errno(ret); |
| 287 | goto out; |
| 288 | } |
| 289 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 290 | inode_lock(main_bm_inode); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 291 | |
| 292 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
| 293 | if (ret < 0) { |
| 294 | mlog_errno(ret); |
| 295 | goto out_mutex; |
| 296 | } |
| 297 | |
| 298 | fe = (struct ocfs2_dinode *)main_bm_bh->b_data; |
| 299 | |
Joel Becker | 10995aa | 2008-11-13 14:49:12 -0800 | [diff] [blame] | 300 | /* main_bm_bh is validated by inode read inside ocfs2_inode_lock(), |
| 301 | * so any corruption is a code bug. */ |
| 302 | BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); |
| 303 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 304 | if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != |
Tao Ma | 8571882 | 2010-04-13 14:38:06 +0800 | [diff] [blame] | 305 | ocfs2_group_bitmap_size(osb->sb, 0, |
| 306 | osb->s_feature_incompat) * 8) { |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 307 | mlog(ML_ERROR, "The disk is too old and small. " |
| 308 | "Force to do offline resize."); |
| 309 | ret = -EINVAL; |
| 310 | goto out_unlock; |
| 311 | } |
| 312 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 313 | first_new_cluster = le32_to_cpu(fe->i_clusters); |
| 314 | lgd_blkno = ocfs2_which_cluster_group(main_bm_inode, |
| 315 | first_new_cluster - 1); |
| 316 | |
Joel Becker | 68f64d4 | 2008-11-13 14:49:14 -0800 | [diff] [blame] | 317 | ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno, |
| 318 | &group_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 319 | if (ret < 0) { |
| 320 | mlog_errno(ret); |
| 321 | goto out_unlock; |
| 322 | } |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 323 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
| 324 | |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 325 | cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc); |
| 326 | if (le16_to_cpu(group->bg_bits) / cl_bpc + new_clusters > |
| 327 | le16_to_cpu(fe->id2.i_chain.cl_cpg)) { |
| 328 | ret = -EINVAL; |
| 329 | goto out_unlock; |
| 330 | } |
| 331 | |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 332 | |
| 333 | trace_ocfs2_group_extend( |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 334 | (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 335 | |
| 336 | handle = ocfs2_start_trans(osb, OCFS2_GROUP_EXTEND_CREDITS); |
| 337 | if (IS_ERR(handle)) { |
| 338 | mlog_errno(PTR_ERR(handle)); |
| 339 | ret = -EINVAL; |
| 340 | goto out_unlock; |
| 341 | } |
| 342 | |
| 343 | /* update the last group descriptor and inode. */ |
| 344 | ret = ocfs2_update_last_group_and_inode(handle, main_bm_inode, |
| 345 | main_bm_bh, group_bh, |
| 346 | first_new_cluster, |
| 347 | new_clusters); |
| 348 | if (ret) { |
| 349 | mlog_errno(ret); |
| 350 | goto out_commit; |
| 351 | } |
| 352 | |
| 353 | ocfs2_update_super_and_backups(main_bm_inode, new_clusters); |
| 354 | |
| 355 | out_commit: |
| 356 | ocfs2_commit_trans(osb, handle); |
| 357 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 358 | brelse(group_bh); |
| 359 | brelse(main_bm_bh); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 360 | |
| 361 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 362 | |
| 363 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 364 | inode_unlock(main_bm_inode); |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 365 | iput(main_bm_inode); |
| 366 | |
| 367 | out: |
Tao Ma | d659072 | 2007-12-18 15:47:03 +0800 | [diff] [blame] | 368 | return ret; |
| 369 | } |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 370 | |
| 371 | static int ocfs2_check_new_group(struct inode *inode, |
| 372 | struct ocfs2_dinode *di, |
| 373 | struct ocfs2_new_group_input *input, |
| 374 | struct buffer_head *group_bh) |
| 375 | { |
| 376 | int ret; |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 377 | struct ocfs2_group_desc *gd = |
| 378 | (struct ocfs2_group_desc *)group_bh->b_data; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 379 | u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 380 | |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 381 | ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh); |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 382 | if (ret) |
| 383 | goto out; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 384 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 385 | ret = -EINVAL; |
| 386 | if (le16_to_cpu(gd->bg_chain) != input->chain) |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 387 | mlog(ML_ERROR, "Group descriptor # %llu has bad chain %u " |
| 388 | "while input has %u set.\n", |
| 389 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 390 | le16_to_cpu(gd->bg_chain), input->chain); |
| 391 | else if (le16_to_cpu(gd->bg_bits) != input->clusters * cl_bpc) |
| 392 | mlog(ML_ERROR, "Group descriptor # %llu has bit count %u but " |
| 393 | "input has %u clusters set\n", |
| 394 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 395 | le16_to_cpu(gd->bg_bits), input->clusters); |
| 396 | else if (le16_to_cpu(gd->bg_free_bits_count) != input->frees * cl_bpc) |
| 397 | mlog(ML_ERROR, "Group descriptor # %llu has free bit count %u " |
| 398 | "but it should have %u set\n", |
| 399 | (unsigned long long)le64_to_cpu(gd->bg_blkno), |
| 400 | le16_to_cpu(gd->bg_bits), |
| 401 | input->frees * cl_bpc); |
| 402 | else |
| 403 | ret = 0; |
| 404 | |
Joel Becker | 57e3e79 | 2008-11-13 14:49:13 -0800 | [diff] [blame] | 405 | out: |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | static int ocfs2_verify_group_and_input(struct inode *inode, |
| 410 | struct ocfs2_dinode *di, |
| 411 | struct ocfs2_new_group_input *input, |
| 412 | struct buffer_head *group_bh) |
| 413 | { |
| 414 | u16 cl_count = le16_to_cpu(di->id2.i_chain.cl_count); |
| 415 | u16 cl_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg); |
| 416 | u16 next_free = le16_to_cpu(di->id2.i_chain.cl_next_free_rec); |
| 417 | u32 cluster = ocfs2_blocks_to_clusters(inode->i_sb, input->group); |
| 418 | u32 total_clusters = le32_to_cpu(di->i_clusters); |
| 419 | int ret = -EINVAL; |
| 420 | |
| 421 | if (cluster < total_clusters) |
| 422 | mlog(ML_ERROR, "add a group which is in the current volume.\n"); |
| 423 | else if (input->chain >= cl_count) |
| 424 | mlog(ML_ERROR, "input chain exceeds the limit.\n"); |
| 425 | else if (next_free != cl_count && next_free != input->chain) |
| 426 | mlog(ML_ERROR, |
| 427 | "the add group should be in chain %u\n", next_free); |
| 428 | else if (total_clusters + input->clusters < total_clusters) |
| 429 | mlog(ML_ERROR, "add group's clusters overflow.\n"); |
| 430 | else if (input->clusters > cl_cpg) |
| 431 | mlog(ML_ERROR, "the cluster exceeds the maximum of a group\n"); |
| 432 | else if (input->frees > input->clusters) |
| 433 | mlog(ML_ERROR, "the free cluster exceeds the total clusters\n"); |
| 434 | else if (total_clusters % cl_cpg != 0) |
| 435 | mlog(ML_ERROR, |
| 436 | "the last group isn't full. Use group extend first.\n"); |
| 437 | else if (input->group != ocfs2_which_cluster_group(inode, cluster)) |
| 438 | mlog(ML_ERROR, "group blkno is invalid\n"); |
| 439 | else if ((ret = ocfs2_check_new_group(inode, di, input, group_bh))) |
| 440 | mlog(ML_ERROR, "group descriptor check failed.\n"); |
| 441 | else |
| 442 | ret = 0; |
| 443 | |
| 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | /* Add a new group descriptor to global_bitmap. */ |
| 448 | int ocfs2_group_add(struct inode *inode, struct ocfs2_new_group_input *input) |
| 449 | { |
| 450 | int ret; |
| 451 | handle_t *handle; |
| 452 | struct buffer_head *main_bm_bh = NULL; |
| 453 | struct inode *main_bm_inode = NULL; |
| 454 | struct ocfs2_dinode *fe = NULL; |
| 455 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 456 | struct buffer_head *group_bh = NULL; |
| 457 | struct ocfs2_group_desc *group = NULL; |
| 458 | struct ocfs2_chain_list *cl; |
| 459 | struct ocfs2_chain_rec *cr; |
| 460 | u16 cl_bpc; |
Younger Liu | eedd40e | 2013-11-12 15:07:03 -0800 | [diff] [blame] | 461 | u64 bg_ptr; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 462 | |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 463 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
| 464 | return -EROFS; |
| 465 | |
| 466 | main_bm_inode = ocfs2_get_system_file_inode(osb, |
| 467 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 468 | OCFS2_INVALID_SLOT); |
| 469 | if (!main_bm_inode) { |
| 470 | ret = -EINVAL; |
| 471 | mlog_errno(ret); |
| 472 | goto out; |
| 473 | } |
| 474 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 475 | inode_lock(main_bm_inode); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 476 | |
| 477 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1); |
| 478 | if (ret < 0) { |
| 479 | mlog_errno(ret); |
| 480 | goto out_mutex; |
| 481 | } |
| 482 | |
| 483 | fe = (struct ocfs2_dinode *)main_bm_bh->b_data; |
| 484 | |
| 485 | if (le16_to_cpu(fe->id2.i_chain.cl_cpg) != |
Tao Ma | 8571882 | 2010-04-13 14:38:06 +0800 | [diff] [blame] | 486 | ocfs2_group_bitmap_size(osb->sb, 0, |
| 487 | osb->s_feature_incompat) * 8) { |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 488 | mlog(ML_ERROR, "The disk is too old and small." |
| 489 | " Force to do offline resize."); |
| 490 | ret = -EINVAL; |
| 491 | goto out_unlock; |
| 492 | } |
| 493 | |
Joel Becker | da1e909 | 2008-10-09 17:20:29 -0700 | [diff] [blame] | 494 | ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 495 | if (ret < 0) { |
| 496 | mlog(ML_ERROR, "Can't read the group descriptor # %llu " |
| 497 | "from the device.", (unsigned long long)input->group); |
| 498 | goto out_unlock; |
| 499 | } |
| 500 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 501 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 502 | |
| 503 | ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh); |
| 504 | if (ret) { |
| 505 | mlog_errno(ret); |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 506 | goto out_free_group_bh; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 507 | } |
| 508 | |
Tao Ma | a543870 | 2011-02-22 08:24:01 +0800 | [diff] [blame] | 509 | trace_ocfs2_group_add((unsigned long long)input->group, |
| 510 | input->chain, input->clusters, input->frees); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 511 | |
| 512 | handle = ocfs2_start_trans(osb, OCFS2_GROUP_ADD_CREDITS); |
| 513 | if (IS_ERR(handle)) { |
| 514 | mlog_errno(PTR_ERR(handle)); |
| 515 | ret = -EINVAL; |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 516 | goto out_free_group_bh; |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | cl_bpc = le16_to_cpu(fe->id2.i_chain.cl_bpc); |
| 520 | cl = &fe->id2.i_chain; |
| 521 | cr = &cl->cl_recs[input->chain]; |
| 522 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 523 | ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode), |
| 524 | group_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 525 | if (ret < 0) { |
| 526 | mlog_errno(ret); |
| 527 | goto out_commit; |
| 528 | } |
| 529 | |
| 530 | group = (struct ocfs2_group_desc *)group_bh->b_data; |
Younger Liu | eedd40e | 2013-11-12 15:07:03 -0800 | [diff] [blame] | 531 | bg_ptr = le64_to_cpu(group->bg_next_group); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 532 | group->bg_next_group = cr->c_blkno; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 533 | ocfs2_journal_dirty(handle, group_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 534 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 535 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode), |
| 536 | main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 537 | if (ret < 0) { |
Younger Liu | eedd40e | 2013-11-12 15:07:03 -0800 | [diff] [blame] | 538 | group->bg_next_group = cpu_to_le64(bg_ptr); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 539 | mlog_errno(ret); |
| 540 | goto out_commit; |
| 541 | } |
| 542 | |
| 543 | if (input->chain == le16_to_cpu(cl->cl_next_free_rec)) { |
| 544 | le16_add_cpu(&cl->cl_next_free_rec, 1); |
| 545 | memset(cr, 0, sizeof(struct ocfs2_chain_rec)); |
| 546 | } |
| 547 | |
Tao Ma | 4338ab6 | 2008-03-03 10:53:02 +0800 | [diff] [blame] | 548 | cr->c_blkno = cpu_to_le64(input->group); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 549 | le32_add_cpu(&cr->c_total, input->clusters * cl_bpc); |
| 550 | le32_add_cpu(&cr->c_free, input->frees * cl_bpc); |
| 551 | |
| 552 | le32_add_cpu(&fe->id1.bitmap1.i_total, input->clusters *cl_bpc); |
| 553 | le32_add_cpu(&fe->id1.bitmap1.i_used, |
| 554 | (input->clusters - input->frees) * cl_bpc); |
| 555 | le32_add_cpu(&fe->i_clusters, input->clusters); |
| 556 | |
| 557 | ocfs2_journal_dirty(handle, main_bm_bh); |
| 558 | |
| 559 | spin_lock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 560 | 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] | 561 | 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] | 562 | spin_unlock(&OCFS2_I(main_bm_inode)->ip_lock); |
| 563 | i_size_write(main_bm_inode, le64_to_cpu(fe->i_size)); |
| 564 | |
| 565 | ocfs2_update_super_and_backups(main_bm_inode, input->clusters); |
| 566 | |
| 567 | out_commit: |
| 568 | ocfs2_commit_trans(osb, handle); |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 569 | |
| 570 | out_free_group_bh: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 571 | brelse(group_bh); |
Younger Liu | 8abaae8 | 2013-11-12 15:07:04 -0800 | [diff] [blame] | 572 | |
| 573 | out_unlock: |
Mark Fasheh | 2fe5c1d | 2008-01-23 18:35:31 -0800 | [diff] [blame] | 574 | brelse(main_bm_bh); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 575 | |
| 576 | ocfs2_inode_unlock(main_bm_inode, 1); |
| 577 | |
| 578 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 579 | inode_unlock(main_bm_inode); |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 580 | iput(main_bm_inode); |
| 581 | |
| 582 | out: |
Tao Ma | 7909f2b | 2007-12-18 15:47:25 +0800 | [diff] [blame] | 583 | return ret; |
| 584 | } |