5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
16 #define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
17 #define howmany(x,y) (((x)+(y)-1)/(y))
18 #define min(x,y) ((x)<(y)?(x):(y))
19 #define max(x,y) ((x)>(y)?(x):(y))
24 * macros used for retyping
26 #define UCPI_UBH ((struct ufs_buffer_head *)ucpi)
27 #define USPI_UBH ((struct ufs_buffer_head *)uspi)
32 * macros used for accesing structures
34 #define ufs_get_fs_state(usb1,usb3) _ufs_get_fs_state_(usb1,usb3,flags,swab)
35 static inline __s32
_ufs_get_fs_state_(struct ufs_super_block_first
* usb1
,
36 struct ufs_super_block_third
* usb3
, unsigned flags
, unsigned swab
)
38 switch (flags
& UFS_ST_MASK
) {
40 return SWAB32((usb3
)->fs_u2
.fs_sun
.fs_state
);
42 return SWAB32((usb1
)->fs_u1
.fs_sunx86
.fs_state
);
45 return SWAB32((usb3
)->fs_u2
.fs_44
.fs_state
);
49 #define ufs_set_fs_state(usb1,usb3,value) _ufs_set_fs_state_(usb1,usb3,value,flags,swab)
50 static inline void _ufs_set_fs_state_(struct ufs_super_block_first
* usb1
,
51 struct ufs_super_block_third
* usb3
, __s32 value
, unsigned flags
, unsigned swab
)
53 switch (flags
& UFS_ST_MASK
) {
55 (usb3
)->fs_u2
.fs_sun
.fs_state
= SWAB32(value
);
58 (usb1
)->fs_u1
.fs_sunx86
.fs_state
= SWAB32(value
);
61 (usb3
)->fs_u2
.fs_44
.fs_state
= SWAB32(value
);
66 #define ufs_get_fs_npsect(usb1,usb3) _ufs_get_fs_npsect_(usb1,usb3,flags,swab)
67 static inline __u32
_ufs_get_fs_npsect_(struct ufs_super_block_first
* usb1
,
68 struct ufs_super_block_third
* usb3
, unsigned flags
, unsigned swab
)
70 if ((flags
& UFS_ST_MASK
) == UFS_ST_SUNx86
)
71 return SWAB32((usb3
)->fs_u2
.fs_sunx86
.fs_npsect
);
73 return SWAB32((usb1
)->fs_u1
.fs_sun
.fs_npsect
);
76 #define ufs_get_fs_qbmask(usb3) _ufs_get_fs_qbmask_(usb3,flags,swab)
77 static inline __u64
_ufs_get_fs_qbmask_(struct ufs_super_block_third
* usb3
,
78 unsigned flags
, unsigned swab
)
81 switch (flags
& UFS_ST_MASK
) {
83 ((u32
*)&tmp
)[0] = usb3
->fs_u2
.fs_sun
.fs_qbmask
[0];
84 ((u32
*)&tmp
)[1] = usb3
->fs_u2
.fs_sun
.fs_qbmask
[1];
87 ((u32
*)&tmp
)[0] = usb3
->fs_u2
.fs_sunx86
.fs_qbmask
[0];
88 ((u32
*)&tmp
)[1] = usb3
->fs_u2
.fs_sunx86
.fs_qbmask
[1];
91 ((u32
*)&tmp
)[0] = usb3
->fs_u2
.fs_44
.fs_qbmask
[0];
92 ((u32
*)&tmp
)[1] = usb3
->fs_u2
.fs_44
.fs_qbmask
[1];
98 #define ufs_get_fs_qfmask(usb3) _ufs_get_fs_qfmask_(usb3,flags,swab)
99 static inline __u64
_ufs_get_fs_qfmask_(struct ufs_super_block_third
* usb3
,
100 unsigned flags
, unsigned swab
)
103 switch (flags
& UFS_ST_MASK
) {
105 ((u32
*)&tmp
)[0] = usb3
->fs_u2
.fs_sun
.fs_qfmask
[0];
106 ((u32
*)&tmp
)[1] = usb3
->fs_u2
.fs_sun
.fs_qfmask
[1];
109 ((u32
*)&tmp
)[0] = usb3
->fs_u2
.fs_sunx86
.fs_qfmask
[0];
110 ((u32
*)&tmp
)[1] = usb3
->fs_u2
.fs_sunx86
.fs_qfmask
[1];
113 ((u32
*)&tmp
)[0] = usb3
->fs_u2
.fs_44
.fs_qfmask
[0];
114 ((u32
*)&tmp
)[1] = usb3
->fs_u2
.fs_44
.fs_qfmask
[1];
120 #define ufs_get_de_namlen(de) \
121 (((flags & UFS_DE_MASK) == UFS_DE_OLD) \
122 ? SWAB16(de->d_u.d_namlen) \
123 : de->d_u.d_44.d_namlen)
125 #define ufs_set_de_namlen(de,value) \
126 (((flags & UFS_DE_MASK) == UFS_DE_OLD) \
127 ? (de->d_u.d_namlen = SWAB16(value)) \
128 : (de->d_u.d_44.d_namlen = value))
130 #define ufs_set_de_type(de,mode) _ufs_set_de_type_(de,mode,flags,swab)
131 static inline void _ufs_set_de_type_(struct ufs_dir_entry
* de
, int mode
,
132 unsigned flags
, unsigned swab
)
134 if ((flags
& UFS_DE_MASK
) == UFS_DE_44BSD
) {
135 switch (mode
& S_IFMT
) {
136 case S_IFSOCK
: de
->d_u
.d_44
.d_type
= DT_SOCK
; break;
137 case S_IFLNK
: de
->d_u
.d_44
.d_type
= DT_LNK
; break;
138 case S_IFREG
: de
->d_u
.d_44
.d_type
= DT_REG
; break;
139 case S_IFBLK
: de
->d_u
.d_44
.d_type
= DT_BLK
; break;
140 case S_IFDIR
: de
->d_u
.d_44
.d_type
= DT_DIR
; break;
141 case S_IFCHR
: de
->d_u
.d_44
.d_type
= DT_CHR
; break;
142 case S_IFIFO
: de
->d_u
.d_44
.d_type
= DT_FIFO
; break;
143 default: de
->d_u
.d_44
.d_type
= DT_UNKNOWN
;
148 #define ufs_get_inode_uid(inode) _ufs_get_inode_uid_(inode,flags,swab)
149 static inline __u32
_ufs_get_inode_uid_(struct ufs_inode
* inode
,
150 unsigned flags
, unsigned swab
)
152 switch (flags
& UFS_UID_MASK
) {
154 return SWAB32(inode
->ui_u3
.ui_sun
.ui_uid
);
156 return SWAB32(inode
->ui_u3
.ui_44
.ui_uid
);
158 return SWAB16(inode
->ui_u1
.oldids
.ui_suid
);
162 #define ufs_set_inode_uid(inode,value) _ufs_set_inode_uid_(inode,value,flags,swab)
163 static inline void _ufs_set_inode_uid_(struct ufs_inode
* inode
, __u32 value
,
164 unsigned flags
, unsigned swab
)
166 inode
->ui_u1
.oldids
.ui_suid
= SWAB16(value
);
167 switch (flags
& UFS_UID_MASK
) {
169 inode
->ui_u3
.ui_sun
.ui_uid
= SWAB32(value
);
172 inode
->ui_u3
.ui_44
.ui_uid
= SWAB32(value
);
177 #define ufs_get_inode_gid(inode) _ufs_get_inode_gid_(inode,flags,swab)
178 static inline __u32
_ufs_get_inode_gid_(struct ufs_inode
* inode
,
179 unsigned flags
, unsigned swab
)
181 switch (flags
& UFS_UID_MASK
) {
183 return SWAB32(inode
->ui_u3
.ui_sun
.ui_gid
);
185 return SWAB32(inode
->ui_u3
.ui_44
.ui_gid
);
187 return SWAB16(inode
->ui_u1
.oldids
.ui_sgid
);
191 #define ufs_set_inode_gid(inode,value) _ufs_set_inode_gid_(inode,value,flags,swab)
192 static inline void _ufs_set_inode_gid_(struct ufs_inode
* inode
, __u32 value
,
193 unsigned flags
, unsigned swab
)
195 inode
->ui_u1
.oldids
.ui_sgid
= SWAB16(value
);
196 switch (flags
& UFS_UID_MASK
) {
198 inode
->ui_u3
.ui_sun
.ui_gid
= SWAB32(value
);
201 inode
->ui_u3
.ui_44
.ui_gid
= SWAB32(value
);
209 * These functions manipulate ufs buffers
211 #define ubh_bread(dev,fragment,size) _ubh_bread_(uspi,dev,fragment,size)
212 extern struct ufs_buffer_head
* _ubh_bread_(struct ufs_sb_private_info
*, kdev_t
, unsigned, unsigned);
213 extern struct ufs_buffer_head
* ubh_bread_uspi(struct ufs_sb_private_info
*, kdev_t
, unsigned, unsigned);
214 extern void ubh_brelse (struct ufs_buffer_head
*);
215 extern void ubh_brelse_uspi (struct ufs_sb_private_info
*);
216 extern void ubh_mark_buffer_dirty (struct ufs_buffer_head
*, int);
217 extern void ubh_mark_buffer_uptodate (struct ufs_buffer_head
*, int);
218 extern void ubh_ll_rw_block (int, unsigned, struct ufs_buffer_head
**);
219 extern void ubh_wait_on_buffer (struct ufs_buffer_head
*);
220 extern unsigned ubh_max_bcount (struct ufs_buffer_head
*);
221 extern void ubh_bforget (struct ufs_buffer_head
*);
222 extern int ubh_buffer_dirty (struct ufs_buffer_head
*);
223 #define ubh_ubhcpymem(mem,ubh,size) _ubh_ubhcpymem_(uspi,mem,ubh,size)
224 extern void _ubh_ubhcpymem_(struct ufs_sb_private_info
*, unsigned char *, struct ufs_buffer_head
*, unsigned);
225 #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
226 extern void _ubh_memcpyubh_(struct ufs_sb_private_info
*, struct ufs_buffer_head
*, unsigned char *, unsigned);
231 * macros to get important structures from ufs_buffer_head
233 #define ubh_get_usb_first(ubh) \
234 ((struct ufs_super_block_first *)((ubh)->bh[0]->b_data))
236 #define ubh_get_usb_second(ubh) \
237 ((struct ufs_super_block_second *)(ubh)-> \
238 bh[UFS_SECTOR_SIZE >> uspi->s_fshift]->b_data + (UFS_SECTOR_SIZE & ~uspi->s_fmask))
240 #define ubh_get_usb_third(ubh) \
241 ((struct ufs_super_block_third *)((ubh)-> \
242 bh[UFS_SECTOR_SIZE*2 >> uspi->s_fshift]->b_data + (UFS_SECTOR_SIZE*2 & ~uspi->s_fmask)))
244 #define ubh_get_ucg(ubh) \
245 ((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
249 * Extract byte from ufs_buffer_head
250 * Extract the bits for a block from a map inside ufs_buffer_head
252 #define ubh_get_addr8(ubh,begin) \
253 ((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
254 ((begin) & ~uspi->s_fmask))
256 #define ubh_get_addr16(ubh,begin) \
257 (((u16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
258 ((begin) & (uspi->fsize>>1) - 1)))
260 #define ubh_get_addr32(ubh,begin) \
261 (((u32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
262 ((begin) & ((uspi->s_fsize>>2) - 1)))
264 #define ubh_get_addr ubh_get_addr8
266 #define ubh_blkmap(ubh,begin,bit) \
267 ((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
271 * Macros for access to superblock array structures
273 #define ubh_postbl(ubh,cylno,i) \
274 ((uspi->s_postblformat != UFS_DYNAMICPOSTBLFMT) \
275 ? (*(__s16*)(ubh_get_addr(ubh, \
276 (unsigned)(&((struct ufs_super_block *)0)->fs_opostbl) \
277 + (((cylno) * 16 + (i)) << 1) ) )) \
278 : (*(__s16*)(ubh_get_addr(ubh, \
279 uspi->s_postbloff + (((cylno) * uspi->s_nrpos + (i)) << 1) ))))
281 #define ubh_rotbl(ubh,i) \
282 ((uspi->s_postblformat != UFS_DYNAMICPOSTBLFMT) \
283 ? (*(__u8*)(ubh_get_addr(ubh, \
284 (unsigned)(&((struct ufs_super_block *)0)->fs_space) + (i)))) \
285 : (*(__u8*)(ubh_get_addr(ubh, uspi->s_rotbloff + (i)))))
288 * Determine the number of available frags given a
289 * percentage to hold in reserve.
291 #define ufs_freespace(usb, percentreserved) \
292 (ufs_blkstofrags(SWAB32((usb)->fs_cstotal.cs_nbfree)) + \
293 SWAB32((usb)->fs_cstotal.cs_nffree) - (uspi->s_dsize * (percentreserved) / 100))
296 * Macros to access cylinder group array structures
298 #define ubh_cg_blktot(ucpi,cylno) \
299 (*((__u32*)ubh_get_addr(UCPI_UBH, (ucpi)->c_btotoff + ((cylno) << 2))))
301 #define ubh_cg_blks(ucpi,cylno,rpos) \
302 (*((__u16*)ubh_get_addr(UCPI_UBH, \
303 (ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
307 * These functions work like classical bitmap operations.
308 * The difference is that we don't have the whole bitmap
309 * in one contiguous chunk of memory, but in several buffers.
310 * The parameters of each function are super_block, ufs_buffer_head and
311 * position of the beginning of the bitmap.
313 #define ubh_setbit(ubh,begin,bit) \
314 (*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
316 #define ubh_clrbit(ubh,begin,bit) \
317 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
319 #define ubh_isset(ubh,begin,bit) \
320 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
322 #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
324 #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
326 #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
327 static inline unsigned _ubh_find_next_zero_bit_(
328 struct ufs_sb_private_info
* uspi
, struct ufs_buffer_head
* ubh
,
329 unsigned begin
, unsigned size
, unsigned offset
)
331 unsigned base
, count
, pos
;
336 base
= offset
>> uspi
->s_bpfshift
;
337 offset
&= uspi
->s_bpfmask
;
339 count
= min (size
+ offset
, uspi
->s_bpf
);
340 size
-= count
- offset
;
341 pos
= ext2_find_next_zero_bit (ubh
->bh
[base
]->b_data
, count
, offset
);
342 if (pos
< count
|| !size
)
347 return (base
<< uspi
->s_bpfshift
) + pos
- begin
;
350 static inline unsigned find_last_zero_bit (unsigned char * bitmap
,
351 unsigned size
, unsigned offset
)
354 unsigned char * mapp
;
357 mapp
= bitmap
+ (size
>> 3);
359 bit
= 1 << (size
& 7);
360 for (i
= size
; i
> offset
; i
--) {
361 if ((map
& bit
) == 0)
373 #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
374 static inline unsigned _ubh_find_last_zero_bit_(
375 struct ufs_sb_private_info
* uspi
, struct ufs_buffer_head
* ubh
,
376 unsigned begin
, unsigned start
, unsigned end
)
378 unsigned base
, count
, pos
, size
;
383 base
= start
>> uspi
->s_bpfshift
;
384 start
&= uspi
->s_bpfmask
;
386 count
= min (size
+ (uspi
->s_bpf
- start
), uspi
->s_bpf
)
387 - (uspi
->s_bpf
- start
);
389 pos
= find_last_zero_bit (ubh
->bh
[base
]->b_data
,
390 start
, start
- count
);
391 if (pos
> start
- count
|| !size
)
396 return (base
<< uspi
->s_bpfshift
) + pos
- begin
;
399 #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
401 #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
402 static inline int _ubh_isblockset_(struct ufs_sb_private_info
* uspi
,
403 struct ufs_buffer_head
* ubh
, unsigned begin
, unsigned block
)
405 switch (uspi
->s_fpb
) {
407 return (*ubh_get_addr (ubh
, begin
+ block
) == 0xff);
409 return (*ubh_get_addr (ubh
, begin
+ (block
>> 1)) == (0x0f << ((block
& 0x01) << 2)));
411 return (*ubh_get_addr (ubh
, begin
+ (block
>> 2)) == (0x03 << ((block
& 0x03) << 1)));
413 return (*ubh_get_addr (ubh
, begin
+ (block
>> 3)) == (0x01 << (block
& 0x07)));
418 #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
419 static inline void _ubh_clrblock_(struct ufs_sb_private_info
* uspi
,
420 struct ufs_buffer_head
* ubh
, unsigned begin
, unsigned block
)
422 switch (uspi
->s_fpb
) {
424 *ubh_get_addr (ubh
, begin
+ block
) = 0x00;
427 *ubh_get_addr (ubh
, begin
+ (block
>> 1)) &= ~(0x0f << ((block
& 0x01) << 2));
430 *ubh_get_addr (ubh
, begin
+ (block
>> 2)) &= ~(0x03 << ((block
& 0x03) << 1));
433 *ubh_get_addr (ubh
, begin
+ (block
>> 3)) &= ~(0x01 << ((block
& 0x07)));
438 #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
439 static inline void _ubh_setblock_(struct ufs_sb_private_info
* uspi
,
440 struct ufs_buffer_head
* ubh
, unsigned begin
, unsigned block
)
442 switch (uspi
->s_fpb
) {
444 *ubh_get_addr(ubh
, begin
+ block
) = 0xff;
447 *ubh_get_addr(ubh
, begin
+ (block
>> 1)) |= (0x0f << ((block
& 0x01) << 2));
450 *ubh_get_addr(ubh
, begin
+ (block
>> 2)) |= (0x03 << ((block
& 0x03) << 1));
453 *ubh_get_addr(ubh
, begin
+ (block
>> 3)) |= (0x01 << ((block
& 0x07)));
458 static inline void ufs_fragacct (struct super_block
* sb
, unsigned blockmap
,
459 unsigned * fraglist
, int cnt
)
461 struct ufs_sb_private_info
* uspi
;
462 unsigned fragsize
, pos
;
465 swab
= sb
->u
.ufs_sb
.s_swab
;
466 uspi
= sb
->u
.ufs_sb
.s_uspi
;
469 for (pos
= 0; pos
< uspi
->s_fpb
; pos
++) {
470 if (blockmap
& (1 << pos
)) {
473 else if (fragsize
> 0) {
474 ADD_SWAB32(fraglist
[fragsize
], cnt
);
478 if (fragsize
> 0 && fragsize
< uspi
->s_fpb
)
479 ADD_SWAB32(fraglist
[fragsize
], cnt
);
482 #define ubh_scanc(ubh,begin,size,table,mask) _ubh_scanc_(uspi,ubh,begin,size,table,mask)
483 static inline unsigned _ubh_scanc_(struct ufs_sb_private_info
* uspi
, struct ufs_buffer_head
* ubh
,
484 unsigned begin
, unsigned size
, unsigned char * table
, unsigned char mask
)
486 unsigned rest
, offset
;
490 offset
= begin
& ~uspi
->s_fmask
;
491 begin
>>= uspi
->s_fshift
;
493 if ((offset
+ size
) < uspi
->s_fsize
)
496 rest
= uspi
->s_fsize
- offset
;
498 cp
= ubh
->bh
[begin
]->b_data
+ offset
;
499 while ((table
[*cp
++] & mask
) == 0 && --rest
);
505 return (size
+ rest
);