xref: /linux/fs/ocfs2/resize.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
11  */
12 
13 #include <linux/fs.h>
14 #include <linux/types.h>
15 
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"
27 #include "ocfs2_trace.h"
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,
42 				       u16 cl_cpg,
43 				       u16 old_bg_clusters,
44 				       int set)
45 {
46 	int i;
47 	u16 backups = 0;
48 	u32 cluster, lgd_cluster;
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 
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 
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 
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);
95 	u16 old_bg_clusters;
96 
97 	trace_ocfs2_update_last_group_and_inode(new_clusters,
98 						first_new_cluster);
99 
100 	ret = ocfs2_journal_access_gd(handle, INODE_CACHE(bm_inode),
101 				      group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
102 	if (ret < 0) {
103 		mlog_errno(ret);
104 		goto out;
105 	}
106 
107 	group = (struct ocfs2_group_desc *)group_bh->b_data;
108 
109 	old_bg_clusters = le16_to_cpu(group->bg_bits) / cl_bpc;
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,
123 						     cl_cpg, old_bg_clusters, 1);
124 		le16_add_cpu(&group->bg_free_bits_count, -1 * backups);
125 	}
126 
127 	ocfs2_journal_dirty(handle, group_bh);
128 
129 	/* update the inode accordingly. */
130 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(bm_inode), bm_bh,
131 				      OCFS2_JOURNAL_ACCESS_WRITE);
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);
151 	le64_add_cpu(&fe->i_size, (u64)new_clusters << osb->s_clustersize_bits);
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,
161 					    cl_cpg, old_bg_clusters, 0);
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:
167 	if (ret)
168 		mlog_errno(ret);
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);
185 		if (cluster >= clusters)
186 			break;
187 
188 		ret = ocfs2_read_blocks_sync(osb, blkno, 1, &backup);
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 	 */
224 	ret = ocfs2_read_blocks_sync(osb, OCFS2_SUPER_BLOCK_BLKNO, 1,
225 				     &super_bh);
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:
245 	brelse(super_bh);
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 
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 
290 	inode_lock(main_bm_inode);
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 
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 
304 	if (le16_to_cpu(fe->id2.i_chain.cl_cpg) !=
305 		ocfs2_group_bitmap_size(osb->sb, 0,
306 					osb->s_feature_incompat) * 8) {
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 
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 
317 	ret = ocfs2_read_group_descriptor(main_bm_inode, fe, lgd_blkno,
318 					  &group_bh);
319 	if (ret < 0) {
320 		mlog_errno(ret);
321 		goto out_unlock;
322 	}
323 	group = (struct ocfs2_group_desc *)group_bh->b_data;
324 
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 
332 
333 	trace_ocfs2_group_extend(
334 	     (unsigned long long)le64_to_cpu(group->bg_blkno), new_clusters);
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:
358 	brelse(group_bh);
359 	brelse(main_bm_bh);
360 
361 	ocfs2_inode_unlock(main_bm_inode, 1);
362 
363 out_mutex:
364 	inode_unlock(main_bm_inode);
365 	iput(main_bm_inode);
366 
367 out:
368 	return ret;
369 }
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;
377 	struct ocfs2_group_desc *gd =
378 		(struct ocfs2_group_desc *)group_bh->b_data;
379 	u16 cl_bpc = le16_to_cpu(di->id2.i_chain.cl_bpc);
380 
381 	ret = ocfs2_check_group_descriptor(inode->i_sb, di, group_bh);
382 	if (ret)
383 		goto out;
384 
385 	ret = -EINVAL;
386 	if (le16_to_cpu(gd->bg_chain) != input->chain)
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 
405 out:
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;
461 	u64 bg_ptr;
462 
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 
475 	inode_lock(main_bm_inode);
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) !=
486 		ocfs2_group_bitmap_size(osb->sb, 0,
487 					osb->s_feature_incompat) * 8) {
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 
494 	ret = ocfs2_read_blocks_sync(osb, input->group, 1, &group_bh);
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 
501 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), group_bh);
502 
503 	ret = ocfs2_verify_group_and_input(main_bm_inode, fe, input, group_bh);
504 	if (ret) {
505 		mlog_errno(ret);
506 		goto out_free_group_bh;
507 	}
508 
509 	trace_ocfs2_group_add((unsigned long long)input->group,
510 			       input->chain, input->clusters, input->frees);
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;
516 		goto out_free_group_bh;
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 
523 	ret = ocfs2_journal_access_gd(handle, INODE_CACHE(main_bm_inode),
524 				      group_bh, OCFS2_JOURNAL_ACCESS_WRITE);
525 	if (ret < 0) {
526 		mlog_errno(ret);
527 		goto out_commit;
528 	}
529 
530 	group = (struct ocfs2_group_desc *)group_bh->b_data;
531 	bg_ptr = le64_to_cpu(group->bg_next_group);
532 	group->bg_next_group = cr->c_blkno;
533 	ocfs2_journal_dirty(handle, group_bh);
534 
535 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(main_bm_inode),
536 				      main_bm_bh, OCFS2_JOURNAL_ACCESS_WRITE);
537 	if (ret < 0) {
538 		group->bg_next_group = cpu_to_le64(bg_ptr);
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 
548 	cr->c_blkno = cpu_to_le64(input->group);
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);
561 	le64_add_cpu(&fe->i_size, (u64)input->clusters << osb->s_clustersize_bits);
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);
569 
570 out_free_group_bh:
571 	brelse(group_bh);
572 
573 out_unlock:
574 	brelse(main_bm_bh);
575 
576 	ocfs2_inode_unlock(main_bm_inode, 1);
577 
578 out_mutex:
579 	inode_unlock(main_bm_inode);
580 	iput(main_bm_inode);
581 
582 out:
583 	return ret;
584 }
585