5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
9 #include <linux/buffer_head.h>
17 #define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
20 * macros used for retyping
22 #define UCPI_UBH ((struct ufs_buffer_head *)ucpi)
23 #define USPI_UBH ((struct ufs_buffer_head *)uspi)
28 * macros used for accessing structures
31 ufs_get_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
32 struct ufs_super_block_third *usb3)
34 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
36 return fs32_to_cpu(sb, usb3->fs_u2.fs_sun.fs_state);
38 return fs32_to_cpu(sb, usb1->fs_u1.fs_sunx86.fs_state);
41 return fs32_to_cpu(sb, usb3->fs_u2.fs_44.fs_state);
46 ufs_set_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
47 struct ufs_super_block_third *usb3, s32 value)
49 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
51 usb3->fs_u2.fs_sun.fs_state = cpu_to_fs32(sb, value);
54 usb1->fs_u1.fs_sunx86.fs_state = cpu_to_fs32(sb, value);
57 usb3->fs_u2.fs_44.fs_state = cpu_to_fs32(sb, value);
63 ufs_get_fs_npsect(struct super_block *sb, struct ufs_super_block_first *usb1,
64 struct ufs_super_block_third *usb3)
66 if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
67 return fs32_to_cpu(sb, usb3->fs_u2.fs_sunx86.fs_npsect);
69 return fs32_to_cpu(sb, usb1->fs_u1.fs_sun.fs_npsect);
73 ufs_get_fs_qbmask(struct super_block *sb, struct ufs_super_block_third *usb3)
77 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
79 ((__fs32 *)&tmp)[0] = usb3->fs_u2.fs_sun.fs_qbmask[0];
80 ((__fs32 *)&tmp)[1] = usb3->fs_u2.fs_sun.fs_qbmask[1];
83 ((__fs32 *)&tmp)[0] = usb3->fs_u2.fs_sunx86.fs_qbmask[0];
84 ((__fs32 *)&tmp)[1] = usb3->fs_u2.fs_sunx86.fs_qbmask[1];
87 ((__fs32 *)&tmp)[0] = usb3->fs_u2.fs_44.fs_qbmask[0];
88 ((__fs32 *)&tmp)[1] = usb3->fs_u2.fs_44.fs_qbmask[1];
92 return fs64_to_cpu(sb, tmp);
96 ufs_get_fs_qfmask(struct super_block *sb, struct ufs_super_block_third *usb3)
100 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
102 ((__fs32 *)&tmp)[0] = usb3->fs_u2.fs_sun.fs_qfmask[0];
103 ((__fs32 *)&tmp)[1] = usb3->fs_u2.fs_sun.fs_qfmask[1];
106 ((__fs32 *)&tmp)[0] = usb3->fs_u2.fs_sunx86.fs_qfmask[0];
107 ((__fs32 *)&tmp)[1] = usb3->fs_u2.fs_sunx86.fs_qfmask[1];
110 ((__fs32 *)&tmp)[0] = usb3->fs_u2.fs_44.fs_qfmask[0];
111 ((__fs32 *)&tmp)[1] = usb3->fs_u2.fs_44.fs_qfmask[1];
115 return fs64_to_cpu(sb, tmp);
119 ufs_get_de_namlen(struct super_block *sb, struct ufs_dir_entry *de)
121 if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
122 return fs16_to_cpu(sb, de->d_u.d_namlen);
124 return de->d_u.d_44.d_namlen; /* XXX this seems wrong */
128 ufs_set_de_namlen(struct super_block *sb, struct ufs_dir_entry *de, u16 value)
130 if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
131 de->d_u.d_namlen = cpu_to_fs16(sb, value);
133 de->d_u.d_44.d_namlen = value; /* XXX this seems wrong */
137 ufs_set_de_type(struct super_block *sb, struct ufs_dir_entry *de, int mode)
139 if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) != UFS_DE_44BSD)
143 * TODO turn this into a table lookup
145 switch (mode & S_IFMT) {
147 de->d_u.d_44.d_type = DT_SOCK;
150 de->d_u.d_44.d_type = DT_LNK;
153 de->d_u.d_44.d_type = DT_REG;
156 de->d_u.d_44.d_type = DT_BLK;
159 de->d_u.d_44.d_type = DT_DIR;
162 de->d_u.d_44.d_type = DT_CHR;
165 de->d_u.d_44.d_type = DT_FIFO;
168 de->d_u.d_44.d_type = DT_UNKNOWN;
173 ufs_get_inode_uid(struct super_block *sb, struct ufs_inode *inode)
175 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
177 return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_uid);
179 return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_uid);
181 return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_suid);
186 ufs_set_inode_uid(struct super_block *sb, struct ufs_inode *inode, u32 value)
188 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
190 inode->ui_u3.ui_sun.ui_uid = cpu_to_fs32(sb, value);
193 inode->ui_u3.ui_44.ui_uid = cpu_to_fs32(sb, value);
196 inode->ui_u1.oldids.ui_suid = cpu_to_fs16(sb, value);
200 ufs_get_inode_gid(struct super_block *sb, struct ufs_inode *inode)
202 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
204 return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_gid);
206 return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_gid);
208 return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_sgid);
213 ufs_set_inode_gid(struct super_block *sb, struct ufs_inode *inode, u32 value)
215 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
217 inode->ui_u3.ui_sun.ui_gid = cpu_to_fs32(sb, value);
220 inode->ui_u3.ui_44.ui_gid = cpu_to_fs32(sb, value);
223 inode->ui_u1.oldids.ui_sgid = cpu_to_fs16(sb, value);
226 extern dev_t ufs_get_inode_dev(struct super_block *, struct ufs_inode_info *);
227 extern void ufs_set_inode_dev(struct super_block *, struct ufs_inode_info *, dev_t);
230 * These functions manipulate ufs buffers
232 #define ubh_bread(sb,fragment,size) _ubh_bread_(uspi,sb,fragment,size)
233 extern struct ufs_buffer_head * _ubh_bread_(struct ufs_sb_private_info *, struct super_block *, u64 , u64);
234 extern struct ufs_buffer_head * ubh_bread_uspi(struct ufs_sb_private_info *, struct super_block *, u64, u64);
235 extern void ubh_brelse (struct ufs_buffer_head *);
236 extern void ubh_brelse_uspi (struct ufs_sb_private_info *);
237 extern void ubh_mark_buffer_dirty (struct ufs_buffer_head *);
238 extern void ubh_mark_buffer_uptodate (struct ufs_buffer_head *, int);
239 extern void ubh_ll_rw_block (int, unsigned, struct ufs_buffer_head **);
240 extern void ubh_wait_on_buffer (struct ufs_buffer_head *);
241 extern unsigned ubh_max_bcount (struct ufs_buffer_head *);
242 extern void ubh_bforget (struct ufs_buffer_head *);
243 extern int ubh_buffer_dirty (struct ufs_buffer_head *);
244 #define ubh_ubhcpymem(mem,ubh,size) _ubh_ubhcpymem_(uspi,mem,ubh,size)
245 extern void _ubh_ubhcpymem_(struct ufs_sb_private_info *, unsigned char *, struct ufs_buffer_head *, unsigned);
246 #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
247 extern void _ubh_memcpyubh_(struct ufs_sb_private_info *, struct ufs_buffer_head *, unsigned char *, unsigned);
252 * macros and inline function to get important structures from ufs_sb_private_info
255 static inline void *get_usb_offset(struct ufs_sb_private_info *uspi,
260 index = offset >> uspi->s_fshift;
261 offset &= ~uspi->s_fmask;
262 return uspi->s_ubh.bh[index]->b_data + offset;
265 #define ubh_get_usb_first(uspi) \
266 ((struct ufs_super_block_first *)get_usb_offset((uspi), 0))
268 #define ubh_get_usb_second(uspi) \
269 ((struct ufs_super_block_second *)get_usb_offset((uspi), UFS_SECTOR_SIZE))
271 #define ubh_get_usb_third(uspi) \
272 ((struct ufs_super_block_third *)get_usb_offset((uspi), 2*UFS_SECTOR_SIZE))
275 #define ubh_get_ucg(ubh) \
276 ((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
280 * Extract byte from ufs_buffer_head
281 * Extract the bits for a block from a map inside ufs_buffer_head
283 #define ubh_get_addr8(ubh,begin) \
284 ((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
285 ((begin) & ~uspi->s_fmask))
287 #define ubh_get_addr16(ubh,begin) \
288 (((__fs16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
289 ((begin) & (uspi->fsize>>1) - 1)))
291 #define ubh_get_addr32(ubh,begin) \
292 (((__fs32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
293 ((begin) & ((uspi->s_fsize>>2) - 1)))
295 #define ubh_get_addr ubh_get_addr8
297 #define ubh_blkmap(ubh,begin,bit) \
298 ((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
302 * Macros for access to superblock array structures
304 #define ubh_postbl(ubh,cylno,i) \
305 ((uspi->s_postblformat != UFS_DYNAMICPOSTBLFMT) \
306 ? (*(__s16*)(ubh_get_addr(ubh, \
307 (unsigned)(&((struct ufs_super_block *)0)->fs_opostbl) \
308 + (((cylno) * 16 + (i)) << 1) ) )) \
309 : (*(__s16*)(ubh_get_addr(ubh, \
310 uspi->s_postbloff + (((cylno) * uspi->s_nrpos + (i)) << 1) ))))
312 #define ubh_rotbl(ubh,i) \
313 ((uspi->s_postblformat != UFS_DYNAMICPOSTBLFMT) \
314 ? (*(__u8*)(ubh_get_addr(ubh, \
315 (unsigned)(&((struct ufs_super_block *)0)->fs_space) + (i)))) \
316 : (*(__u8*)(ubh_get_addr(ubh, uspi->s_rotbloff + (i)))))
319 * Determine the number of available frags given a
320 * percentage to hold in reserve.
322 #define ufs_freespace(usb, percentreserved) \
323 (ufs_blkstofrags(fs32_to_cpu(sb, (usb)->fs_cstotal.cs_nbfree)) + \
324 fs32_to_cpu(sb, (usb)->fs_cstotal.cs_nffree) - (uspi->s_dsize * (percentreserved) / 100))
327 * Macros to access cylinder group array structures
329 #define ubh_cg_blktot(ucpi,cylno) \
330 (*((__fs32*)ubh_get_addr(UCPI_UBH, (ucpi)->c_btotoff + ((cylno) << 2))))
332 #define ubh_cg_blks(ucpi,cylno,rpos) \
333 (*((__fs16*)ubh_get_addr(UCPI_UBH, \
334 (ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
338 * These functions work like classical bitmap operations.
339 * The difference is that we don't have the whole bitmap
340 * in one contiguous chunk of memory, but in several buffers.
341 * The parameters of each function are super_block, ufs_buffer_head and
342 * position of the beginning of the bitmap.
344 #define ubh_setbit(ubh,begin,bit) \
345 (*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
347 #define ubh_clrbit(ubh,begin,bit) \
348 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
350 #define ubh_isset(ubh,begin,bit) \
351 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
353 #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
355 #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
357 #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
358 static inline unsigned _ubh_find_next_zero_bit_(
359 struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
360 unsigned begin, unsigned size, unsigned offset)
362 unsigned base, count, pos;
367 base = offset >> uspi->s_bpfshift;
368 offset &= uspi->s_bpfmask;
370 count = min_t(unsigned int, size + offset, uspi->s_bpf);
371 size -= count - offset;
372 pos = ext2_find_next_zero_bit (ubh->bh[base]->b_data, count, offset);
373 if (pos < count || !size)
378 return (base << uspi->s_bpfshift) + pos - begin;
381 static inline unsigned find_last_zero_bit (unsigned char * bitmap,
382 unsigned size, unsigned offset)
385 unsigned char * mapp;
388 mapp = bitmap + (size >> 3);
390 bit = 1 << (size & 7);
391 for (i = size; i > offset; i--) {
392 if ((map & bit) == 0)
404 #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
405 static inline unsigned _ubh_find_last_zero_bit_(
406 struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
407 unsigned begin, unsigned start, unsigned end)
409 unsigned base, count, pos, size;
414 base = start >> uspi->s_bpfshift;
415 start &= uspi->s_bpfmask;
417 count = min_t(unsigned int,
418 size + (uspi->s_bpf - start), uspi->s_bpf)
419 - (uspi->s_bpf - start);
421 pos = find_last_zero_bit (ubh->bh[base]->b_data,
422 start, start - count);
423 if (pos > start - count || !size)
428 return (base << uspi->s_bpfshift) + pos - begin;
431 #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
433 #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
434 static inline int _ubh_isblockset_(struct ufs_sb_private_info * uspi,
435 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
437 switch (uspi->s_fpb) {
439 return (*ubh_get_addr (ubh, begin + block) == 0xff);
441 return (*ubh_get_addr (ubh, begin + (block >> 1)) == (0x0f << ((block & 0x01) << 2)));
443 return (*ubh_get_addr (ubh, begin + (block >> 2)) == (0x03 << ((block & 0x03) << 1)));
445 return (*ubh_get_addr (ubh, begin + (block >> 3)) == (0x01 << (block & 0x07)));
450 #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
451 static inline void _ubh_clrblock_(struct ufs_sb_private_info * uspi,
452 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
454 switch (uspi->s_fpb) {
456 *ubh_get_addr (ubh, begin + block) = 0x00;
459 *ubh_get_addr (ubh, begin + (block >> 1)) &= ~(0x0f << ((block & 0x01) << 2));
462 *ubh_get_addr (ubh, begin + (block >> 2)) &= ~(0x03 << ((block & 0x03) << 1));
465 *ubh_get_addr (ubh, begin + (block >> 3)) &= ~(0x01 << ((block & 0x07)));
470 #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
471 static inline void _ubh_setblock_(struct ufs_sb_private_info * uspi,
472 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
474 switch (uspi->s_fpb) {
476 *ubh_get_addr(ubh, begin + block) = 0xff;
479 *ubh_get_addr(ubh, begin + (block >> 1)) |= (0x0f << ((block & 0x01) << 2));
482 *ubh_get_addr(ubh, begin + (block >> 2)) |= (0x03 << ((block & 0x03) << 1));
485 *ubh_get_addr(ubh, begin + (block >> 3)) |= (0x01 << ((block & 0x07)));
490 static inline void ufs_fragacct (struct super_block * sb, unsigned blockmap,
491 __fs32 * fraglist, int cnt)
493 struct ufs_sb_private_info * uspi;
494 unsigned fragsize, pos;
496 uspi = UFS_SB(sb)->s_uspi;
499 for (pos = 0; pos < uspi->s_fpb; pos++) {
500 if (blockmap & (1 << pos)) {
503 else if (fragsize > 0) {
504 fs32_add(sb, &fraglist[fragsize], cnt);
508 if (fragsize > 0 && fragsize < uspi->s_fpb)
509 fs32_add(sb, &fraglist[fragsize], cnt);
512 #define ubh_scanc(ubh,begin,size,table,mask) _ubh_scanc_(uspi,ubh,begin,size,table,mask)
513 static inline unsigned _ubh_scanc_(struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
514 unsigned begin, unsigned size, unsigned char * table, unsigned char mask)
516 unsigned rest, offset;
520 offset = begin & ~uspi->s_fmask;
521 begin >>= uspi->s_fshift;
523 if ((offset + size) < uspi->s_fsize)
526 rest = uspi->s_fsize - offset;
528 cp = ubh->bh[begin]->b_data + offset;
529 while ((table[*cp++] & mask) == 0 && --rest);
535 return (size + rest);