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/util.h
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "swab.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * some useful macros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define in_range(b,first,len)	((b)>=(first)&&(b)<(first)+(len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * functions used for retyping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static inline struct ufs_buffer_head *UCPI_UBH(struct ufs_cg_private_info *cpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	return &cpi->c_ubh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static inline struct ufs_buffer_head *USPI_UBH(struct ufs_sb_private_info *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	return &spi->s_ubh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * macros used for accessing structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static inline s32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) ufs_get_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		 struct ufs_super_block_third *usb3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	case UFS_ST_SUNOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		if (fs32_to_cpu(sb, usb3->fs_postblformat) == UFS_42POSTBLFMT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			return fs32_to_cpu(sb, usb1->fs_u0.fs_sun.fs_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		fallthrough;	/* to UFS_ST_SUN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	case UFS_ST_SUN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return fs32_to_cpu(sb, usb3->fs_un2.fs_sun.fs_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	case UFS_ST_SUNx86:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return fs32_to_cpu(sb, usb1->fs_u1.fs_sunx86.fs_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	case UFS_ST_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) ufs_set_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		 struct ufs_super_block_third *usb3, s32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	case UFS_ST_SUNOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (fs32_to_cpu(sb, usb3->fs_postblformat) == UFS_42POSTBLFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			usb1->fs_u0.fs_sun.fs_state = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		fallthrough;	/* to UFS_ST_SUN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	case UFS_ST_SUN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		usb3->fs_un2.fs_sun.fs_state = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	case UFS_ST_SUNx86:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		usb1->fs_u1.fs_sunx86.fs_state = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	case UFS_ST_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		usb3->fs_un2.fs_44.fs_state = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^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 inline u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) ufs_get_fs_npsect(struct super_block *sb, struct ufs_super_block_first *usb1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		  struct ufs_super_block_third *usb3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return fs32_to_cpu(sb, usb3->fs_un2.fs_sunx86.fs_npsect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return fs32_to_cpu(sb, usb1->fs_u1.fs_sun.fs_npsect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static inline u64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) ufs_get_fs_qbmask(struct super_block *sb, struct ufs_super_block_third *usb3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	__fs64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	case UFS_ST_SUNOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	case UFS_ST_SUN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qbmask[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qbmask[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	case UFS_ST_SUNx86:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qbmask[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qbmask[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	case UFS_ST_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qbmask[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qbmask[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return fs64_to_cpu(sb, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static inline u64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ufs_get_fs_qfmask(struct super_block *sb, struct ufs_super_block_third *usb3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	__fs64 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	case UFS_ST_SUNOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	case UFS_ST_SUN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qfmask[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qfmask[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	case UFS_ST_SUNx86:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qfmask[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qfmask[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	case UFS_ST_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qfmask[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qfmask[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return fs64_to_cpu(sb, tmp);
^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) static inline u16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ufs_get_de_namlen(struct super_block *sb, struct ufs_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return fs16_to_cpu(sb, de->d_u.d_namlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return de->d_u.d_44.d_namlen; /* XXX this seems wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ufs_set_de_namlen(struct super_block *sb, struct ufs_dir_entry *de, u16 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		de->d_u.d_namlen = cpu_to_fs16(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		de->d_u.d_44.d_namlen = value; /* XXX this seems wrong */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ufs_set_de_type(struct super_block *sb, struct ufs_dir_entry *de, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) != UFS_DE_44BSD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	 * TODO turn this into a table lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	switch (mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	case S_IFSOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		de->d_u.d_44.d_type = DT_SOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		de->d_u.d_44.d_type = DT_LNK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		de->d_u.d_44.d_type = DT_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	case S_IFBLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		de->d_u.d_44.d_type = DT_BLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		de->d_u.d_44.d_type = DT_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	case S_IFCHR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		de->d_u.d_44.d_type = DT_CHR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	case S_IFIFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		de->d_u.d_44.d_type = DT_FIFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		de->d_u.d_44.d_type = DT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static inline u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ufs_get_inode_uid(struct super_block *sb, struct ufs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	case UFS_UID_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	case UFS_UID_EFT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (inode->ui_u1.oldids.ui_suid == 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_suid);
^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) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ufs_set_inode_uid(struct super_block *sb, struct ufs_inode *inode, u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	case UFS_UID_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		inode->ui_u3.ui_44.ui_uid = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		inode->ui_u1.oldids.ui_suid = cpu_to_fs16(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	case UFS_UID_EFT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		inode->ui_u3.ui_sun.ui_uid = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		if (value > 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			value = 0xFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		inode->ui_u1.oldids.ui_suid = cpu_to_fs16(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static inline u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) ufs_get_inode_gid(struct super_block *sb, struct ufs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	case UFS_UID_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	case UFS_UID_EFT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		if (inode->ui_u1.oldids.ui_sgid == 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_sgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ufs_set_inode_gid(struct super_block *sb, struct ufs_inode *inode, u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	case UFS_UID_44BSD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		inode->ui_u3.ui_44.ui_gid = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		inode->ui_u1.oldids.ui_sgid =  cpu_to_fs16(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	case UFS_UID_EFT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		inode->ui_u3.ui_sun.ui_gid = cpu_to_fs32(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (value > 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			value = 0xFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		inode->ui_u1.oldids.ui_sgid =  cpu_to_fs16(sb, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) extern dev_t ufs_get_inode_dev(struct super_block *, struct ufs_inode_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) extern void ufs_set_inode_dev(struct super_block *, struct ufs_inode_info *, dev_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) extern int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  * These functions manipulate ufs buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #define ubh_bread(sb,fragment,size) _ubh_bread_(uspi,sb,fragment,size)  
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) extern struct ufs_buffer_head * _ubh_bread_(struct ufs_sb_private_info *, struct super_block *, u64 , u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) extern struct ufs_buffer_head * ubh_bread_uspi(struct ufs_sb_private_info *, struct super_block *, u64, u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) extern void ubh_brelse (struct ufs_buffer_head *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) extern void ubh_brelse_uspi (struct ufs_sb_private_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) extern void ubh_mark_buffer_dirty (struct ufs_buffer_head *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) extern void ubh_mark_buffer_uptodate (struct ufs_buffer_head *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) extern void ubh_sync_block(struct ufs_buffer_head *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) extern void ubh_bforget (struct ufs_buffer_head *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) extern int  ubh_buffer_dirty (struct ufs_buffer_head *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) #define ubh_ubhcpymem(mem,ubh,size) _ubh_ubhcpymem_(uspi,mem,ubh,size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) extern void _ubh_ubhcpymem_(struct ufs_sb_private_info *, unsigned char *, struct ufs_buffer_head *, unsigned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) extern void _ubh_memcpyubh_(struct ufs_sb_private_info *, struct ufs_buffer_head *, unsigned char *, unsigned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* This functions works with cache pages*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) extern struct page *ufs_get_locked_page(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 					pgoff_t index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static inline void ufs_put_locked_page(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)        unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)        put_page(page);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * macros and inline function to get important structures from ufs_sb_private_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static inline void *get_usb_offset(struct ufs_sb_private_info *uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				   unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	index = offset >> uspi->s_fshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	offset &= ~uspi->s_fmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return uspi->s_ubh.bh[index]->b_data + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #define ubh_get_usb_first(uspi) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	((struct ufs_super_block_first *)get_usb_offset((uspi), 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) #define ubh_get_usb_second(uspi) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	((struct ufs_super_block_second *)get_usb_offset((uspi), UFS_SECTOR_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #define ubh_get_usb_third(uspi)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	((struct ufs_super_block_third *)get_usb_offset((uspi), 2*UFS_SECTOR_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #define ubh_get_ucg(ubh) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  * Extract byte from ufs_buffer_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  * Extract the bits for a block from a map inside ufs_buffer_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) #define ubh_get_addr8(ubh,begin) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	((begin) & ~uspi->s_fmask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) #define ubh_get_addr16(ubh,begin) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	(((__fs16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	((begin) & ((uspi->fsize>>1) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) #define ubh_get_addr32(ubh,begin) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	(((__fs32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	((begin) & ((uspi->s_fsize>>2) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) #define ubh_get_addr64(ubh,begin) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	(((__fs64*)((ubh)->bh[(begin) >> (uspi->s_fshift-3)]->b_data)) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	((begin) & ((uspi->s_fsize>>3) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #define ubh_get_addr ubh_get_addr8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static inline void *ubh_get_data_ptr(struct ufs_sb_private_info *uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				     struct ufs_buffer_head *ubh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				     u64 blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (uspi->fs_magic == UFS2_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return ubh_get_addr64(ubh, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		return ubh_get_addr32(ubh, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) #define ubh_blkmap(ubh,begin,bit) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static inline u64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) ufs_freefrags(struct ufs_sb_private_info *uspi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	return ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		uspi->cs_total.cs_nffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  * Macros to access cylinder group array structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) #define ubh_cg_blktot(ucpi,cylno) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	(*((__fs32*)ubh_get_addr(UCPI_UBH(ucpi), (ucpi)->c_btotoff + ((cylno) << 2))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) #define ubh_cg_blks(ucpi,cylno,rpos) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	(*((__fs16*)ubh_get_addr(UCPI_UBH(ucpi), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	(ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * Bitmap operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  * These functions work like classical bitmap operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * The difference is that we don't have the whole bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * in one contiguous chunk of memory, but in several buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * The parameters of each function are super_block, ufs_buffer_head and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  * position of the beginning of the bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) #define ubh_setbit(ubh,begin,bit) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	(*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) #define ubh_clrbit(ubh,begin,bit) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	(*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) #define ubh_isset(ubh,begin,bit) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	(*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static inline unsigned _ubh_find_next_zero_bit_(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	unsigned begin, unsigned size, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	unsigned base, count, pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	size -= offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	begin <<= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	offset += begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	base = offset >> uspi->s_bpfshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	offset &= uspi->s_bpfmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		count = min_t(unsigned int, size + offset, uspi->s_bpf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		size -= count - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		pos = find_next_zero_bit_le(ubh->bh[base]->b_data, count, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		if (pos < count || !size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		base++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	return (base << uspi->s_bpfshift) + pos - begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) } 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static inline unsigned find_last_zero_bit (unsigned char * bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	unsigned size, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	unsigned bit, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	unsigned char * mapp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	unsigned char map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	mapp = bitmap + (size >> 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	map = *mapp--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	bit = 1 << (size & 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	for (i = size; i > offset; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		if ((map & bit) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		if ((i & 7) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			bit >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			map = *mapp--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			bit = 1 << 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	return i;
^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) #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static inline unsigned _ubh_find_last_zero_bit_(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	unsigned begin, unsigned start, unsigned end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	unsigned base, count, pos, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	size = start - end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	begin <<= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	start += begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	base = start >> uspi->s_bpfshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	start &= uspi->s_bpfmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		count = min_t(unsigned int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			    size + (uspi->s_bpf - start), uspi->s_bpf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			- (uspi->s_bpf - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		size -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		pos = find_last_zero_bit (ubh->bh[base]->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			start, start - count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		if (pos > start - count || !size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		base--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		start = uspi->s_bpf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	return (base << uspi->s_bpfshift) + pos - begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) } 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static inline int _ubh_isblockset_(struct ufs_sb_private_info * uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	u8 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	switch (uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	    	return (*ubh_get_addr (ubh, begin + block) == 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		mask = 0x0f << ((block & 0x01) << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		return (*ubh_get_addr (ubh, begin + (block >> 1)) & mask) == mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		mask = 0x03 << ((block & 0x03) << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		return (*ubh_get_addr (ubh, begin + (block >> 2)) & mask) == mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		mask = 0x01 << (block & 0x07);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		return (*ubh_get_addr (ubh, begin + (block >> 3)) & mask) == mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	return 0;	
^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) #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static inline void _ubh_clrblock_(struct ufs_sb_private_info * uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	switch (uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	    	*ubh_get_addr (ubh, begin + block) = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	    	return; 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		*ubh_get_addr (ubh, begin + (block >> 1)) &= ~(0x0f << ((block & 0x01) << 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		*ubh_get_addr (ubh, begin + (block >> 2)) &= ~(0x03 << ((block & 0x03) << 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		*ubh_get_addr (ubh, begin + (block >> 3)) &= ~(0x01 << ((block & 0x07)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		return;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static inline void _ubh_setblock_(struct ufs_sb_private_info * uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	switch (uspi->s_fpb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	    	*ubh_get_addr(ubh, begin + block) = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	    	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		*ubh_get_addr(ubh, begin + (block >> 1)) |= (0x0f << ((block & 0x01) << 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		*ubh_get_addr(ubh, begin + (block >> 2)) |= (0x03 << ((block & 0x03) << 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		*ubh_get_addr(ubh, begin + (block >> 3)) |= (0x01 << ((block & 0x07)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) static inline void ufs_fragacct (struct super_block * sb, unsigned blockmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	__fs32 * fraglist, int cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	struct ufs_sb_private_info * uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	unsigned fragsize, pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	uspi = UFS_SB(sb)->s_uspi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	fragsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	for (pos = 0; pos < uspi->s_fpb; pos++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		if (blockmap & (1 << pos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			fragsize++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		else if (fragsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			fs32_add(sb, &fraglist[fragsize], cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			fragsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (fragsize > 0 && fragsize < uspi->s_fpb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		fs32_add(sb, &fraglist[fragsize], cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static inline void *ufs_get_direct_data_ptr(struct ufs_sb_private_info *uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 					    struct ufs_inode_info *ufsi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 					    unsigned blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	BUG_ON(blk > UFS_TIND_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	return uspi->fs_magic == UFS2_MAGIC ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		(void *)&ufsi->i_u1.u2_i_data[blk] :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		(void *)&ufsi->i_u1.i_data[blk];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) static inline u64 ufs_data_ptr_to_cpu(struct super_block *sb, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	return UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		fs64_to_cpu(sb, *(__fs64 *)p) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		fs32_to_cpu(sb, *(__fs32 *)p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static inline void ufs_cpu_to_data_ptr(struct super_block *sb, void *p, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		*(__fs64 *)p = cpu_to_fs64(sb, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		*(__fs32 *)p = cpu_to_fs32(sb, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static inline void ufs_data_ptr_clear(struct ufs_sb_private_info *uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 				      void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (uspi->fs_magic == UFS2_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		*(__fs64 *)p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		*(__fs32 *)p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) static inline int ufs_is_data_ptr_zero(struct ufs_sb_private_info *uspi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 				       void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if (uspi->fs_magic == UFS2_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		return *(__fs64 *)p == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		return *(__fs32 *)p == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) static inline __fs32 ufs_get_seconds(struct super_block *sbp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	time64_t now = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	/* Signed 32-bit interpretation wraps around in 2038, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	 * happens in ufs1 inode stamps but not ufs2 using 64-bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	 * stamps. For superblock and blockgroup, let's assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	 * unsigned 32-bit stamps, which are good until y2106.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	 * Wrap around rather than clamp here to make the dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	 * file system detection work in the superblock stamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	return cpu_to_fs32(sbp, lower_32_bits(now));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }