Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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