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