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