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 * functions used for retyping
22 static inline struct ufs_buffer_head *UCPI_UBH(struct ufs_cg_private_info *cpi)
26 static inline struct ufs_buffer_head *USPI_UBH(struct ufs_sb_private_info *spi)
34 * macros used for accessing structures
37 ufs_get_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
38 struct ufs_super_block_third *usb3)
40 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
42 return fs32_to_cpu(sb, usb3->fs_un2.fs_sun.fs_state);
44 return fs32_to_cpu(sb, usb1->fs_u1.fs_sunx86.fs_state);
47 return fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_state);
52 ufs_set_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
53 struct ufs_super_block_third *usb3, s32 value)
55 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
57 usb3->fs_un2.fs_sun.fs_state = cpu_to_fs32(sb, value);
60 usb1->fs_u1.fs_sunx86.fs_state = cpu_to_fs32(sb, value);
63 usb3->fs_un2.fs_44.fs_state = cpu_to_fs32(sb, value);
69 ufs_get_fs_npsect(struct super_block *sb, struct ufs_super_block_first *usb1,
70 struct ufs_super_block_third *usb3)
72 if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
73 return fs32_to_cpu(sb, usb3->fs_un2.fs_sunx86.fs_npsect);
75 return fs32_to_cpu(sb, usb1->fs_u1.fs_sun.fs_npsect);
79 ufs_get_fs_qbmask(struct super_block *sb, struct ufs_super_block_third *usb3)
83 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
85 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qbmask[0];
86 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qbmask[1];
89 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qbmask[0];
90 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qbmask[1];
93 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qbmask[0];
94 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qbmask[1];
98 return fs64_to_cpu(sb, tmp);
102 ufs_get_fs_qfmask(struct super_block *sb, struct ufs_super_block_third *usb3)
106 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
108 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qfmask[0];
109 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qfmask[1];
112 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qfmask[0];
113 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qfmask[1];
116 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qfmask[0];
117 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qfmask[1];
121 return fs64_to_cpu(sb, tmp);
125 ufs_get_de_namlen(struct super_block *sb, struct ufs_dir_entry *de)
127 if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
128 return fs16_to_cpu(sb, de->d_u.d_namlen);
130 return de->d_u.d_44.d_namlen; /* XXX this seems wrong */
134 ufs_set_de_namlen(struct super_block *sb, struct ufs_dir_entry *de, u16 value)
136 if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
137 de->d_u.d_namlen = cpu_to_fs16(sb, value);
139 de->d_u.d_44.d_namlen = value; /* XXX this seems wrong */
143 ufs_set_de_type(struct super_block *sb, struct ufs_dir_entry *de, int mode)
145 if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) != UFS_DE_44BSD)
149 * TODO turn this into a table lookup
151 switch (mode & S_IFMT) {
153 de->d_u.d_44.d_type = DT_SOCK;
156 de->d_u.d_44.d_type = DT_LNK;
159 de->d_u.d_44.d_type = DT_REG;
162 de->d_u.d_44.d_type = DT_BLK;
165 de->d_u.d_44.d_type = DT_DIR;
168 de->d_u.d_44.d_type = DT_CHR;
171 de->d_u.d_44.d_type = DT_FIFO;
174 de->d_u.d_44.d_type = DT_UNKNOWN;
179 ufs_get_inode_uid(struct super_block *sb, struct ufs_inode *inode)
181 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
183 return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_uid);
185 return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_uid);
187 return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_suid);
192 ufs_set_inode_uid(struct super_block *sb, struct ufs_inode *inode, u32 value)
194 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
196 inode->ui_u3.ui_sun.ui_uid = cpu_to_fs32(sb, value);
199 inode->ui_u3.ui_44.ui_uid = cpu_to_fs32(sb, value);
202 inode->ui_u1.oldids.ui_suid = cpu_to_fs16(sb, value);
206 ufs_get_inode_gid(struct super_block *sb, struct ufs_inode *inode)
208 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
210 return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_gid);
212 return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_gid);
214 return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_sgid);
219 ufs_set_inode_gid(struct super_block *sb, struct ufs_inode *inode, u32 value)
221 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
223 inode->ui_u3.ui_sun.ui_gid = cpu_to_fs32(sb, value);
226 inode->ui_u3.ui_44.ui_gid = cpu_to_fs32(sb, value);
229 inode->ui_u1.oldids.ui_sgid = cpu_to_fs16(sb, value);
232 extern dev_t ufs_get_inode_dev(struct super_block *, struct ufs_inode_info *);
233 extern void ufs_set_inode_dev(struct super_block *, struct ufs_inode_info *, dev_t);
236 * These functions manipulate ufs buffers
238 #define ubh_bread(sb,fragment,size) _ubh_bread_(uspi,sb,fragment,size)
239 extern struct ufs_buffer_head * _ubh_bread_(struct ufs_sb_private_info *, struct super_block *, u64 , u64);
240 extern struct ufs_buffer_head * ubh_bread_uspi(struct ufs_sb_private_info *, struct super_block *, u64, u64);
241 extern void ubh_brelse (struct ufs_buffer_head *);
242 extern void ubh_brelse_uspi (struct ufs_sb_private_info *);
243 extern void ubh_mark_buffer_dirty (struct ufs_buffer_head *);
244 extern void ubh_mark_buffer_uptodate (struct ufs_buffer_head *, int);
245 extern void ubh_ll_rw_block(int, struct ufs_buffer_head *);
246 extern void ubh_wait_on_buffer (struct ufs_buffer_head *);
247 extern void ubh_bforget (struct ufs_buffer_head *);
248 extern int ubh_buffer_dirty (struct ufs_buffer_head *);
249 #define ubh_ubhcpymem(mem,ubh,size) _ubh_ubhcpymem_(uspi,mem,ubh,size)
250 extern void _ubh_ubhcpymem_(struct ufs_sb_private_info *, unsigned char *, struct ufs_buffer_head *, unsigned);
251 #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
252 extern void _ubh_memcpyubh_(struct ufs_sb_private_info *, struct ufs_buffer_head *, unsigned char *, unsigned);
257 * macros and inline function to get important structures from ufs_sb_private_info
260 static inline void *get_usb_offset(struct ufs_sb_private_info *uspi,
265 index = offset >> uspi->s_fshift;
266 offset &= ~uspi->s_fmask;
267 return uspi->s_ubh.bh[index]->b_data + offset;
270 #define ubh_get_usb_first(uspi) \
271 ((struct ufs_super_block_first *)get_usb_offset((uspi), 0))
273 #define ubh_get_usb_second(uspi) \
274 ((struct ufs_super_block_second *)get_usb_offset((uspi), UFS_SECTOR_SIZE))
276 #define ubh_get_usb_third(uspi) \
277 ((struct ufs_super_block_third *)get_usb_offset((uspi), 2*UFS_SECTOR_SIZE))
280 #define ubh_get_ucg(ubh) \
281 ((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
285 * Extract byte from ufs_buffer_head
286 * Extract the bits for a block from a map inside ufs_buffer_head
288 #define ubh_get_addr8(ubh,begin) \
289 ((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
290 ((begin) & ~uspi->s_fmask))
292 #define ubh_get_addr16(ubh,begin) \
293 (((__fs16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
294 ((begin) & (uspi->fsize>>1) - 1)))
296 #define ubh_get_addr32(ubh,begin) \
297 (((__fs32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
298 ((begin) & ((uspi->s_fsize>>2) - 1)))
300 #define ubh_get_addr ubh_get_addr8
302 #define ubh_blkmap(ubh,begin,bit) \
303 ((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
306 * Determine the number of available frags given a
307 * percentage to hold in reserve.
310 ufs_freespace(struct ufs_sb_private_info *uspi, int percentreserved)
312 return ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
313 uspi->cs_total.cs_nffree -
314 (uspi->s_dsize * (percentreserved) / 100);
318 * Macros to access cylinder group array structures
320 #define ubh_cg_blktot(ucpi,cylno) \
321 (*((__fs32*)ubh_get_addr(UCPI_UBH(ucpi), (ucpi)->c_btotoff + ((cylno) << 2))))
323 #define ubh_cg_blks(ucpi,cylno,rpos) \
324 (*((__fs16*)ubh_get_addr(UCPI_UBH(ucpi), \
325 (ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
329 * These functions work like classical bitmap operations.
330 * The difference is that we don't have the whole bitmap
331 * in one contiguous chunk of memory, but in several buffers.
332 * The parameters of each function are super_block, ufs_buffer_head and
333 * position of the beginning of the bitmap.
335 #define ubh_setbit(ubh,begin,bit) \
336 (*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
338 #define ubh_clrbit(ubh,begin,bit) \
339 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
341 #define ubh_isset(ubh,begin,bit) \
342 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
344 #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
346 #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
348 #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
349 static inline unsigned _ubh_find_next_zero_bit_(
350 struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
351 unsigned begin, unsigned size, unsigned offset)
353 unsigned base, count, pos;
358 base = offset >> uspi->s_bpfshift;
359 offset &= uspi->s_bpfmask;
361 count = min_t(unsigned int, size + offset, uspi->s_bpf);
362 size -= count - offset;
363 pos = ext2_find_next_zero_bit (ubh->bh[base]->b_data, count, offset);
364 if (pos < count || !size)
369 return (base << uspi->s_bpfshift) + pos - begin;
372 static inline unsigned find_last_zero_bit (unsigned char * bitmap,
373 unsigned size, unsigned offset)
376 unsigned char * mapp;
379 mapp = bitmap + (size >> 3);
381 bit = 1 << (size & 7);
382 for (i = size; i > offset; i--) {
383 if ((map & bit) == 0)
395 #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
396 static inline unsigned _ubh_find_last_zero_bit_(
397 struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
398 unsigned begin, unsigned start, unsigned end)
400 unsigned base, count, pos, size;
405 base = start >> uspi->s_bpfshift;
406 start &= uspi->s_bpfmask;
408 count = min_t(unsigned int,
409 size + (uspi->s_bpf - start), uspi->s_bpf)
410 - (uspi->s_bpf - start);
412 pos = find_last_zero_bit (ubh->bh[base]->b_data,
413 start, start - count);
414 if (pos > start - count || !size)
419 return (base << uspi->s_bpfshift) + pos - begin;
422 #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
424 #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
425 static inline int _ubh_isblockset_(struct ufs_sb_private_info * uspi,
426 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
428 switch (uspi->s_fpb) {
430 return (*ubh_get_addr (ubh, begin + block) == 0xff);
432 return (*ubh_get_addr (ubh, begin + (block >> 1)) == (0x0f << ((block & 0x01) << 2)));
434 return (*ubh_get_addr (ubh, begin + (block >> 2)) == (0x03 << ((block & 0x03) << 1)));
436 return (*ubh_get_addr (ubh, begin + (block >> 3)) == (0x01 << (block & 0x07)));
441 #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
442 static inline void _ubh_clrblock_(struct ufs_sb_private_info * uspi,
443 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
445 switch (uspi->s_fpb) {
447 *ubh_get_addr (ubh, begin + block) = 0x00;
450 *ubh_get_addr (ubh, begin + (block >> 1)) &= ~(0x0f << ((block & 0x01) << 2));
453 *ubh_get_addr (ubh, begin + (block >> 2)) &= ~(0x03 << ((block & 0x03) << 1));
456 *ubh_get_addr (ubh, begin + (block >> 3)) &= ~(0x01 << ((block & 0x07)));
461 #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
462 static inline void _ubh_setblock_(struct ufs_sb_private_info * uspi,
463 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
465 switch (uspi->s_fpb) {
467 *ubh_get_addr(ubh, begin + block) = 0xff;
470 *ubh_get_addr(ubh, begin + (block >> 1)) |= (0x0f << ((block & 0x01) << 2));
473 *ubh_get_addr(ubh, begin + (block >> 2)) |= (0x03 << ((block & 0x03) << 1));
476 *ubh_get_addr(ubh, begin + (block >> 3)) |= (0x01 << ((block & 0x07)));
481 static inline void ufs_fragacct (struct super_block * sb, unsigned blockmap,
482 __fs32 * fraglist, int cnt)
484 struct ufs_sb_private_info * uspi;
485 unsigned fragsize, pos;
487 uspi = UFS_SB(sb)->s_uspi;
490 for (pos = 0; pos < uspi->s_fpb; pos++) {
491 if (blockmap & (1 << pos)) {
494 else if (fragsize > 0) {
495 fs32_add(sb, &fraglist[fragsize], cnt);
499 if (fragsize > 0 && fragsize < uspi->s_fpb)
500 fs32_add(sb, &fraglist[fragsize], cnt);