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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/fs/ufs/balloc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 1998
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Daniel Pirkl <daniel.pirkl@email.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Charles University, Faculty of Mathematics and Physics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * UFS2 write support Evgeniy Dushistov <dushistov@mail.ru>, 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "ufs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "ufs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "swab.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define INVBLOCK ((u64)-1L)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static u64 ufs_add_fragments(struct inode *, u64, unsigned, unsigned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static u64 ufs_alloc_fragments(struct inode *, unsigned, u64, unsigned, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static u64 ufs_alloccg_block(struct inode *, struct ufs_cg_private_info *, u64, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static u64 ufs_bitmap_search (struct super_block *, struct ufs_cg_private_info *, u64, unsigned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static unsigned char ufs_fragtable_8fpb[], ufs_fragtable_other[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static void ufs_clusteracct(struct super_block *, struct ufs_cg_private_info *, unsigned, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * Free 'count' fragments from fragment number 'fragment'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) void ufs_free_fragments(struct inode *inode, u64 fragment, unsigned count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct super_block * sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct ufs_cg_private_info * ucpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct ufs_cylinder_group * ucg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	unsigned cgno, bit, end_bit, bbase, blkmap, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	u64 blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	UFSD("ENTER, fragment %llu, count %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	     (unsigned long long)fragment, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (ufs_fragnum(fragment) + count > uspi->s_fpg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		ufs_error (sb, "ufs_free_fragments", "internal error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	mutex_lock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	cgno = ufs_dtog(uspi, fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	bit = ufs_dtogd(uspi, fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (cgno >= uspi->s_ncg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		ufs_panic (sb, "ufs_free_fragments", "freeing blocks are outside device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	ucpi = ufs_load_cylinder (sb, cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (!ucpi) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	ucg = ubh_get_ucg (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!ufs_cg_chkmagic(sb, ucg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		ufs_panic (sb, "ufs_free_fragments", "internal error, bad magic number on cg %u", cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	end_bit = bit + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	bbase = ufs_blknum (bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	ufs_fragacct (sb, blkmap, ucg->cg_frsum, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	for (i = bit; i < end_bit; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		else 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			ufs_error (sb, "ufs_free_fragments",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				   "bit already cleared for fragment %u", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	inode_sub_bytes(inode, count << uspi->s_fshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	fs32_add(sb, &ucg->cg_cs.cs_nffree, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	uspi->cs_total.cs_nffree += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	blkmap = ubh_blkmap (UCPI_UBH(ucpi), ucpi->c_freeoff, bbase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	ufs_fragacct(sb, blkmap, ucg->cg_frsum, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * Trying to reassemble free fragments into block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	blkno = ufs_fragstoblks (bbase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		fs32_sub(sb, &ucg->cg_cs.cs_nffree, uspi->s_fpb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		uspi->cs_total.cs_nffree -= uspi->s_fpb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, uspi->s_fpb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			ufs_clusteracct (sb, ucpi, blkno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		uspi->cs_total.cs_nbfree++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		if (uspi->fs_magic != UFS2_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			unsigned cylno = ufs_cbtocylno (bbase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			fs16_add(sb, &ubh_cg_blks(ucpi, cylno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 						  ufs_cbtorpos(bbase)), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		}
^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) 	ubh_mark_buffer_dirty (USPI_UBH(uspi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (sb->s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		ubh_sync_block(UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	ufs_mark_sb_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	UFSD("EXIT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	UFSD("EXIT (FAILED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  * Free 'count' fragments from fragment number 'fragment' (free whole blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) void ufs_free_blocks(struct inode *inode, u64 fragment, unsigned count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct super_block * sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct ufs_cg_private_info * ucpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct ufs_cylinder_group * ucg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	unsigned overflow, cgno, bit, end_bit, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	u64 blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	UFSD("ENTER, fragment %llu, count %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	     (unsigned long long)fragment, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if ((fragment & uspi->s_fpbmask) || (count & uspi->s_fpbmask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		ufs_error (sb, "ufs_free_blocks", "internal error, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			   "fragment %llu, count %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			   (unsigned long long)fragment, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	mutex_lock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) do_more:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	overflow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	cgno = ufs_dtog(uspi, fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	bit = ufs_dtogd(uspi, fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (cgno >= uspi->s_ncg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		ufs_panic (sb, "ufs_free_blocks", "freeing blocks are outside device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		goto failed_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	end_bit = bit + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (end_bit > uspi->s_fpg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		overflow = bit + count - uspi->s_fpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		count -= overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		end_bit -= overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ucpi = ufs_load_cylinder (sb, cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (!ucpi) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		goto failed_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	ucg = ubh_get_ucg (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (!ufs_cg_chkmagic(sb, ucg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		ufs_panic (sb, "ufs_free_blocks", "internal error, bad magic number on cg %u", cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		goto failed_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	for (i = bit; i < end_bit; i += uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		blkno = ufs_fragstoblks(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			ufs_error(sb, "ufs_free_blocks", "freeing free fragment");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		ubh_setblock(UCPI_UBH(ucpi), ucpi->c_freeoff, blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		inode_sub_bytes(inode, uspi->s_fpb << uspi->s_fshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			ufs_clusteracct (sb, ucpi, blkno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		fs32_add(sb, &ucg->cg_cs.cs_nbfree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		uspi->cs_total.cs_nbfree++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nbfree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (uspi->fs_magic != UFS2_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			unsigned cylno = ufs_cbtocylno(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			fs16_add(sb, &ubh_cg_blks(ucpi, cylno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 						  ufs_cbtorpos(i)), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	ubh_mark_buffer_dirty (USPI_UBH(uspi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (sb->s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		ubh_sync_block(UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (overflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		fragment += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		count = overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		goto do_more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	ufs_mark_sb_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	UFSD("EXIT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) failed_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	UFSD("EXIT (FAILED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * Modify inode page cache in such way:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * have - blocks with b_blocknr equal to oldb...oldb+count-1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * get - blocks with b_blocknr equal to newb...newb+count-1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * also we suppose that oldb...oldb+count-1 blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * situated at the end of file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * We can come here from ufs_writepage or ufs_prepare_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * locked_page is argument of these functions, so we already lock it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static void ufs_change_blocknr(struct inode *inode, sector_t beg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			       unsigned int count, sector_t oldb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			       sector_t newb, struct page *locked_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	const unsigned blks_per_page =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		1 << (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	const unsigned mask = blks_per_page - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct address_space * const mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	pgoff_t index, cur_index, last_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	unsigned pos, j, lblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	sector_t end, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct buffer_head *head, *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	UFSD("ENTER, ino %lu, count %u, oldb %llu, newb %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	      inode->i_ino, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	     (unsigned long long)oldb, (unsigned long long)newb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	BUG_ON(!locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	BUG_ON(!PageLocked(locked_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	cur_index = locked_page->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	end = count + beg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	last_index = end >> (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	for (i = beg; i < end; i = (i | mask) + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		index = i >> (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (likely(cur_index != index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			page = ufs_get_locked_page(mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			if (!page)/* it was truncated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			if (IS_ERR(page)) {/* or EIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 				ufs_error(inode->i_sb, __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 					  "read of page %llu failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 					  (unsigned long long)index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			page = locked_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		bh = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		pos = i & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		for (j = 0; j < pos; ++j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			bh = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		if (unlikely(index == last_index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			lblock = end & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			lblock = blks_per_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			if (j >= lblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			pos = (i - beg) + j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			if (!buffer_mapped(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 					map_bh(bh, inode->i_sb, oldb + pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				ll_rw_block(REQ_OP_READ, 0, 1, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 					ufs_error(inode->i_sb, __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 						  "read of block failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			UFSD(" change from %llu to %llu, pos %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			     (unsigned long long)(pos + oldb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			     (unsigned long long)(pos + newb), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			bh->b_blocknr = newb + pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			clean_bdev_bh_alias(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			++j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			bh = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		} while (bh != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		if (likely(cur_index != index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			ufs_put_locked_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	UFSD("EXIT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static void ufs_clear_frags(struct inode *inode, sector_t beg, unsigned int n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			    int sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	sector_t end = beg + n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	for (; beg < end; ++beg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		bh = sb_getblk(inode->i_sb, beg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		memset(bh->b_data, 0, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (IS_SYNC(inode) || sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		brelse(bh);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) u64 ufs_new_fragments(struct inode *inode, void *p, u64 fragment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			   u64 goal, unsigned count, int *err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			   struct page *locked_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	struct super_block * sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct ufs_super_block_first * usb1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	unsigned cgno, oldcount, newcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	u64 tmp, request, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	UFSD("ENTER, ino %lu, fragment %llu, goal %llu, count %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	     inode->i_ino, (unsigned long long)fragment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	     (unsigned long long)goal, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	usb1 = ubh_get_usb_first(uspi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	*err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	mutex_lock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	tmp = ufs_data_ptr_to_cpu(sb, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (count + ufs_fragnum(fragment) > uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		ufs_warning(sb, "ufs_new_fragments", "internal warning"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			    " fragment %llu, count %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			    (unsigned long long)fragment, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		count = uspi->s_fpb - ufs_fragnum(fragment); 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	oldcount = ufs_fragnum (fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	newcount = oldcount + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	 * Somebody else has just allocated our fragments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (oldcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		if (!tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			ufs_error(sb, "ufs_new_fragments", "internal error, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 				  "fragment %llu, tmp %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 				  (unsigned long long)fragment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				  (unsigned long long)tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			return INVBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		if (fragment < UFS_I(inode)->i_lastfrag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			UFSD("EXIT (ALREADY ALLOCATED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			UFSD("EXIT (ALREADY ALLOCATED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	 * There is not enough space for user on the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (unlikely(ufs_freefrags(uspi) <= uspi->s_root_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if (!capable(CAP_SYS_RESOURCE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			UFSD("EXIT (FAILED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (goal >= uspi->s_size) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		goal = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (goal == 0) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		cgno = ufs_inotocg (inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		cgno = ufs_dtog(uspi, goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	 * allocate new fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (oldcount == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		result = ufs_alloc_fragments (inode, cgno, goal, count, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			ufs_clear_frags(inode, result + oldcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 					newcount - oldcount, locked_page != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			*err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			write_seqlock(&UFS_I(inode)->meta_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			ufs_cpu_to_data_ptr(sb, p, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			UFS_I(inode)->i_lastfrag =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 				max(UFS_I(inode)->i_lastfrag, fragment + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			write_sequnlock(&UFS_I(inode)->meta_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		UFSD("EXIT, result %llu\n", (unsigned long long)result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	 * resize block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	result = ufs_add_fragments(inode, tmp, oldcount, newcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		*err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		read_seqlock_excl(&UFS_I(inode)->meta_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		UFS_I(inode)->i_lastfrag = max(UFS_I(inode)->i_lastfrag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 						fragment + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		read_sequnlock_excl(&UFS_I(inode)->meta_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 				locked_page != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		UFSD("EXIT, result %llu\n", (unsigned long long)result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	 * allocate new block and move data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (fs32_to_cpu(sb, usb1->fs_optim) == UFS_OPTSPACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		request = newcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (uspi->cs_total.cs_nffree < uspi->s_space_to_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		request = uspi->s_fpb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		if (uspi->cs_total.cs_nffree > uspi->s_time_to_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTSPACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	result = ufs_alloc_fragments (inode, cgno, goal, request, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		ufs_clear_frags(inode, result + oldcount, newcount - oldcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 				locked_page != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		ufs_change_blocknr(inode, fragment - oldcount, oldcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 				   uspi->s_sbbase + tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				   uspi->s_sbbase + result, locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		*err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		write_seqlock(&UFS_I(inode)->meta_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		ufs_cpu_to_data_ptr(sb, p, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		UFS_I(inode)->i_lastfrag = max(UFS_I(inode)->i_lastfrag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 						fragment + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		write_sequnlock(&UFS_I(inode)->meta_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		if (newcount < request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			ufs_free_fragments (inode, result + newcount, request - newcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		ufs_free_fragments (inode, tmp, oldcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		UFSD("EXIT, result %llu\n", (unsigned long long)result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	mutex_unlock(&UFS_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	UFSD("EXIT (FAILED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	return 0;
^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) static bool try_add_frags(struct inode *inode, unsigned frags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	unsigned size = frags * i_blocksize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	__inode_add_bytes(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (unlikely((u32)inode->i_blocks != inode->i_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		__inode_sub_bytes(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static u64 ufs_add_fragments(struct inode *inode, u64 fragment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			     unsigned oldcount, unsigned newcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	struct super_block * sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	struct ufs_cg_private_info * ucpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	struct ufs_cylinder_group * ucg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	unsigned cgno, fragno, fragoff, count, fragsize, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	UFSD("ENTER, fragment %llu, oldcount %u, newcount %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	     (unsigned long long)fragment, oldcount, newcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	count = newcount - oldcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	cgno = ufs_dtog(uspi, fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if (fs32_to_cpu(sb, UFS_SB(sb)->fs_cs(cgno).cs_nffree) < count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if ((ufs_fragnum (fragment) + newcount) > uspi->s_fpb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	ucpi = ufs_load_cylinder (sb, cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (!ucpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	ucg = ubh_get_ucg (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (!ufs_cg_chkmagic(sb, ucg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		ufs_panic (sb, "ufs_add_fragments",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			"internal error, bad magic number on cg %u", cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	fragno = ufs_dtogd(uspi, fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	fragoff = ufs_fragnum (fragno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	for (i = oldcount; i < newcount; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (!try_add_frags(inode, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	 * Block can be extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	ucg->cg_time = ufs_get_seconds(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	for (i = newcount; i < (uspi->s_fpb - fragoff); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		if (ubh_isclr (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	fragsize = i - oldcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	if (!fs32_to_cpu(sb, ucg->cg_frsum[fragsize]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		ufs_panic (sb, "ufs_add_fragments",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			"internal error or corrupted bitmap on cg %u", cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	fs32_sub(sb, &ucg->cg_frsum[fragsize], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (fragsize != count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		fs32_add(sb, &ucg->cg_frsum[fragsize - count], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	for (i = oldcount; i < newcount; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, fragno + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	uspi->cs_total.cs_nffree -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	ubh_mark_buffer_dirty (USPI_UBH(uspi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (sb->s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		ubh_sync_block(UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	ufs_mark_sb_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	UFSD("EXIT, fragment %llu\n", (unsigned long long)fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	return fragment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) #define UFS_TEST_FREE_SPACE_CG \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	ucg = (struct ufs_cylinder_group *) UFS_SB(sb)->s_ucg[cgno]->b_data; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	if (fs32_to_cpu(sb, ucg->cg_cs.cs_nbfree)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		goto cg_found; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	for (k = count; k < uspi->s_fpb; k++) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		if (fs32_to_cpu(sb, ucg->cg_frsum[k])) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 			goto cg_found; 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static u64 ufs_alloc_fragments(struct inode *inode, unsigned cgno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			       u64 goal, unsigned count, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	struct super_block * sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	struct ufs_cg_private_info * ucpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	struct ufs_cylinder_group * ucg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	unsigned oldcg, i, j, k, allocsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	u64 result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	UFSD("ENTER, ino %lu, cgno %u, goal %llu, count %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	     inode->i_ino, cgno, (unsigned long long)goal, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	oldcg = cgno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	 * 1. searching on preferred cylinder group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	UFS_TEST_FREE_SPACE_CG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	 * 2. quadratic rehash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	for (j = 1; j < uspi->s_ncg; j *= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		cgno += j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		if (cgno >= uspi->s_ncg) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			cgno -= uspi->s_ncg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		UFS_TEST_FREE_SPACE_CG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	 * 3. brute force search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	 * We start at i = 2 ( 0 is checked at 1.step, 1 at 2.step )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	cgno = (oldcg + 1) % uspi->s_ncg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	for (j = 2; j < uspi->s_ncg; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		cgno++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		if (cgno >= uspi->s_ncg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 			cgno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		UFS_TEST_FREE_SPACE_CG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	UFSD("EXIT (FAILED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) cg_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	ucpi = ufs_load_cylinder (sb, cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	if (!ucpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	ucg = ubh_get_ucg (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (!ufs_cg_chkmagic(sb, ucg)) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		ufs_panic (sb, "ufs_alloc_fragments",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			"internal error, bad magic number on cg %u", cgno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	ucg->cg_time = ufs_get_seconds(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	if (count == uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		result = ufs_alloccg_block (inode, ucpi, goal, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		if (result == INVBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		goto succed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	for (allocsize = count; allocsize < uspi->s_fpb; allocsize++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		if (fs32_to_cpu(sb, ucg->cg_frsum[allocsize]) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	if (allocsize == uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		result = ufs_alloccg_block (inode, ucpi, goal, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		if (result == INVBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		goal = ufs_dtogd(uspi, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		for (i = count; i < uspi->s_fpb; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 			ubh_setbit (UCPI_UBH(ucpi), ucpi->c_freeoff, goal + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		i = uspi->s_fpb - count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		inode_sub_bytes(inode, i << uspi->s_fshift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		fs32_add(sb, &ucg->cg_cs.cs_nffree, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		uspi->cs_total.cs_nffree += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		fs32_add(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		fs32_add(sb, &ucg->cg_frsum[i], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		goto succed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	result = ufs_bitmap_search (sb, ucpi, goal, allocsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (result == INVBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	if (!try_add_frags(inode, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		ubh_clrbit (UCPI_UBH(ucpi), ucpi->c_freeoff, result + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	fs32_sub(sb, &ucg->cg_cs.cs_nffree, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	uspi->cs_total.cs_nffree -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	fs32_sub(sb, &UFS_SB(sb)->fs_cs(cgno).cs_nffree, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	fs32_sub(sb, &ucg->cg_frsum[allocsize], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	if (count != allocsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		fs32_add(sb, &ucg->cg_frsum[allocsize - count], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) succed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	ubh_mark_buffer_dirty (USPI_UBH(uspi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	if (sb->s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		ubh_sync_block(UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	ufs_mark_sb_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	result += cgno * uspi->s_fpg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	UFSD("EXIT3, result %llu\n", (unsigned long long)result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) static u64 ufs_alloccg_block(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 			     struct ufs_cg_private_info *ucpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			     u64 goal, int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	struct super_block * sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	struct ufs_cylinder_group * ucg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	u64 result, blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	UFSD("ENTER, goal %llu\n", (unsigned long long)goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	ucg = ubh_get_ucg(UCPI_UBH(ucpi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	if (goal == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		goal = ucpi->c_rotor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		goto norot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	goal = ufs_blknum (goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	goal = ufs_dtogd(uspi, goal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	 * If the requested block is available, use it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (ubh_isblockset(UCPI_UBH(ucpi), ucpi->c_freeoff, ufs_fragstoblks(goal))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		result = goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		goto gotit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) norot:	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	result = ufs_bitmap_search (sb, ucpi, goal, uspi->s_fpb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	if (result == INVBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		return INVBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	ucpi->c_rotor = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) gotit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	if (!try_add_frags(inode, uspi->s_fpb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	blkno = ufs_fragstoblks(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	ubh_clrblock (UCPI_UBH(ucpi), ucpi->c_freeoff, blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	if ((UFS_SB(sb)->s_flags & UFS_CG_MASK) == UFS_CG_44BSD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		ufs_clusteracct (sb, ucpi, blkno, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	fs32_sub(sb, &ucg->cg_cs.cs_nbfree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	uspi->cs_total.cs_nbfree--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	fs32_sub(sb, &UFS_SB(sb)->fs_cs(ucpi->c_cgx).cs_nbfree, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	if (uspi->fs_magic != UFS2_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		unsigned cylno = ufs_cbtocylno((unsigned)result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		fs16_sub(sb, &ubh_cg_blks(ucpi, cylno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 					  ufs_cbtorpos((unsigned)result)), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		fs32_sub(sb, &ubh_cg_blktot(ucpi, cylno), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	UFSD("EXIT, result %llu\n", (unsigned long long)result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) static unsigned ubh_scanc(struct ufs_sb_private_info *uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 			  struct ufs_buffer_head *ubh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 			  unsigned begin, unsigned size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 			  unsigned char *table, unsigned char mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	unsigned rest, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	unsigned char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	offset = begin & ~uspi->s_fmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	begin >>= uspi->s_fshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 		if ((offset + size) < uspi->s_fsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 			rest = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 			rest = uspi->s_fsize - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		size -= rest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		cp = ubh->bh[begin]->b_data + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 		while ((table[*cp++] & mask) == 0 && --rest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 		if (rest || !size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		begin++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	return (size + rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)  * Find a block of the specified size in the specified cylinder group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)  * @sp: pointer to super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)  * @ucpi: pointer to cylinder group info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)  * @goal: near which block we want find new one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)  * @count: specified size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) static u64 ufs_bitmap_search(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 			     struct ufs_cg_private_info *ucpi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 			     u64 goal, unsigned count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	 * Bit patterns for identifying fragments in the block map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	 * used as ((map & mask_arr) == want_arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	static const int mask_arr[9] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 		0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	static const int want_arr[9] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		0x0, 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe, 0x1fe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	unsigned start, length, loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	unsigned pos, want, blockmap, mask, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	u64 result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	UFSD("ENTER, cg %u, goal %llu, count %u\n", ucpi->c_cgx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	     (unsigned long long)goal, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	if (goal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 		start = ufs_dtogd(uspi, goal) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 		start = ucpi->c_frotor >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	length = ((uspi->s_fpg + 7) >> 3) - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff + start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 		(uspi->s_fpb == 8) ? ufs_fragtable_8fpb : ufs_fragtable_other,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		1 << (count - 1 + (uspi->s_fpb & 7))); 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	if (loc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 		length = start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 		loc = ubh_scanc(uspi, UCPI_UBH(ucpi), ucpi->c_freeoff, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 				(uspi->s_fpb == 8) ? ufs_fragtable_8fpb :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 				ufs_fragtable_other,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 				1 << (count - 1 + (uspi->s_fpb & 7)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 		if (loc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 			ufs_error(sb, "ufs_bitmap_search",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 				  "bitmap corrupted on cg %u, start %u,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 				  " length %u, count %u, freeoff %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 				  ucpi->c_cgx, start, length, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 				  ucpi->c_freeoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 			return INVBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 		start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	result = (start + length - loc) << 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	ucpi->c_frotor = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	 * found the byte in the map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	for (end = result + 8; result < end; result += uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		blockmap = ubh_blkmap(UCPI_UBH(ucpi), ucpi->c_freeoff, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 		blockmap <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 		mask = mask_arr[count];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 		want = want_arr[count];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 		for (pos = 0; pos <= uspi->s_fpb - count; pos++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 			if ((blockmap & mask) == want) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 				UFSD("EXIT, result %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 				     (unsigned long long)result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 				return result + pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)  			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 			mask <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 			want <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)  		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)  	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	ufs_error(sb, "ufs_bitmap_search", "block not in map on cg %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 		  ucpi->c_cgx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	UFSD("EXIT (FAILED)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	return INVBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) static void ufs_clusteracct(struct super_block * sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 	struct ufs_cg_private_info * ucpi, unsigned blkno, int cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 	int i, start, end, forw, back;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	if (uspi->s_contigsumsize <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	if (cnt > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 		ubh_setbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 		ubh_clrbit(UCPI_UBH(ucpi), ucpi->c_clusteroff, blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 	 * Find the size of the cluster going forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	start = blkno + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	end = start + uspi->s_contigsumsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	if ( end >= ucpi->c_nclusterblks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 		end = ucpi->c_nclusterblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	i = ubh_find_next_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, end, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	if (i > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 		i = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	forw = i - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	 * Find the size of the cluster going backward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	start = blkno - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	end = start - uspi->s_contigsumsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	if (end < 0 ) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 		end = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	i = ubh_find_last_zero_bit (UCPI_UBH(ucpi), ucpi->c_clusteroff, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	if ( i < end) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 		i = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	back = start - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	 * Account for old cluster and the possibly new forward and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	 * back clusters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	i = back + forw + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	if (i > uspi->s_contigsumsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 		i = uspi->s_contigsumsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	fs32_add(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (i << 2)), cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	if (back > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 		fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (back << 2)), cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	if (forw > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 		fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (forw << 2)), cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) static unsigned char ufs_fragtable_8fpb[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x04, 0x01, 0x01, 0x01, 0x03, 0x02, 0x03, 0x04, 0x08,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x02, 0x03, 0x03, 0x02, 0x04, 0x05, 0x08, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x04, 0x05, 0x05, 0x06, 0x08, 0x09, 0x10, 0x20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 	0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x08, 0x09, 0x09, 0x0A, 0x10, 0x11, 0x20, 0x40,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 	0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x03, 0x03, 0x03, 0x03, 0x05, 0x05, 0x09, 0x11,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 	0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x03, 0x05, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x05, 0x09,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x05, 0x05, 0x05, 0x07, 0x09, 0x09, 0x11, 0x21,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 	0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0x06, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x06, 0x0A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x02, 0x03, 0x03, 0x02, 0x06, 0x07, 0x0A, 0x12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	0x04, 0x05, 0x05, 0x06, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x05, 0x07, 0x06, 0x07, 0x04, 0x0C,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	0x08, 0x09, 0x09, 0x0A, 0x09, 0x09, 0x0A, 0x0C, 0x10, 0x11, 0x11, 0x12, 0x20, 0x21, 0x40, 0x80,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) static unsigned char ufs_fragtable_other[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 	0x00, 0x16, 0x16, 0x2A, 0x16, 0x16, 0x26, 0x4E, 0x16, 0x16, 0x16, 0x3E, 0x2A, 0x3E, 0x4E, 0x8A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 	0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 	0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 	0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 	0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 	0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 	0x26, 0x36, 0x36, 0x2E, 0x36, 0x36, 0x26, 0x6E, 0x36, 0x36, 0x36, 0x3E, 0x2E, 0x3E, 0x6E, 0xAE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) 	0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 	0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) 	0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) 	0x16, 0x16, 0x16, 0x3E, 0x16, 0x16, 0x36, 0x5E, 0x16, 0x16, 0x16, 0x3E, 0x3E, 0x3E, 0x5E, 0x9E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) 	0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) 	0x2A, 0x3E, 0x3E, 0x2A, 0x3E, 0x3E, 0x2E, 0x6E, 0x3E, 0x3E, 0x3E, 0x3E, 0x2A, 0x3E, 0x6E, 0xAA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) 	0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E,	0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x7E, 0xBE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) 	0x4E, 0x5E, 0x5E, 0x6E, 0x5E, 0x5E, 0x6E, 0x4E, 0x5E, 0x5E, 0x5E, 0x7E, 0x6E, 0x7E, 0x4E, 0xCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) 	0x8A, 0x9E, 0x9E, 0xAA, 0x9E, 0x9E, 0xAE, 0xCE, 0x9E, 0x9E, 0x9E, 0xBE, 0xAA, 0xBE, 0xCE, 0x8A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) };