RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / ufs / util.h
blob06d344839c42477f85141a9b6a6a3d95c4a33ca7
1 /*
2 * linux/fs/ufs/util.h
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 */
9 #include <linux/buffer_head.h>
10 #include <linux/fs.h>
11 #include "swab.h"
15 * some useful macros
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)
24 return &cpi->c_ubh;
26 static inline struct ufs_buffer_head *USPI_UBH(struct ufs_sb_private_info *spi)
28 return &spi->s_ubh;
34 * macros used for accessing structures
36 static inline s32
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) {
41 case UFS_ST_SUN:
42 return fs32_to_cpu(sb, usb3->fs_un2.fs_sun.fs_state);
43 case UFS_ST_SUNx86:
44 return fs32_to_cpu(sb, usb1->fs_u1.fs_sunx86.fs_state);
45 case UFS_ST_44BSD:
46 default:
47 return fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_state);
51 static inline void
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) {
56 case UFS_ST_SUN:
57 usb3->fs_un2.fs_sun.fs_state = cpu_to_fs32(sb, value);
58 break;
59 case UFS_ST_SUNx86:
60 usb1->fs_u1.fs_sunx86.fs_state = cpu_to_fs32(sb, value);
61 break;
62 case UFS_ST_44BSD:
63 usb3->fs_un2.fs_44.fs_state = cpu_to_fs32(sb, value);
64 break;
68 static inline u32
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);
74 else
75 return fs32_to_cpu(sb, usb1->fs_u1.fs_sun.fs_npsect);
78 static inline u64
79 ufs_get_fs_qbmask(struct super_block *sb, struct ufs_super_block_third *usb3)
81 __fs64 tmp;
83 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
84 case UFS_ST_SUN:
85 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qbmask[0];
86 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qbmask[1];
87 break;
88 case UFS_ST_SUNx86:
89 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qbmask[0];
90 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qbmask[1];
91 break;
92 case UFS_ST_44BSD:
93 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qbmask[0];
94 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qbmask[1];
95 break;
98 return fs64_to_cpu(sb, tmp);
101 static inline u64
102 ufs_get_fs_qfmask(struct super_block *sb, struct ufs_super_block_third *usb3)
104 __fs64 tmp;
106 switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
107 case UFS_ST_SUN:
108 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qfmask[0];
109 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qfmask[1];
110 break;
111 case UFS_ST_SUNx86:
112 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qfmask[0];
113 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qfmask[1];
114 break;
115 case UFS_ST_44BSD:
116 ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qfmask[0];
117 ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qfmask[1];
118 break;
121 return fs64_to_cpu(sb, tmp);
124 static inline u16
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);
129 else
130 return de->d_u.d_44.d_namlen; /* XXX this seems wrong */
133 static inline void
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);
138 else
139 de->d_u.d_44.d_namlen = value; /* XXX this seems wrong */
142 static inline void
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)
146 return;
149 * TODO turn this into a table lookup
151 switch (mode & S_IFMT) {
152 case S_IFSOCK:
153 de->d_u.d_44.d_type = DT_SOCK;
154 break;
155 case S_IFLNK:
156 de->d_u.d_44.d_type = DT_LNK;
157 break;
158 case S_IFREG:
159 de->d_u.d_44.d_type = DT_REG;
160 break;
161 case S_IFBLK:
162 de->d_u.d_44.d_type = DT_BLK;
163 break;
164 case S_IFDIR:
165 de->d_u.d_44.d_type = DT_DIR;
166 break;
167 case S_IFCHR:
168 de->d_u.d_44.d_type = DT_CHR;
169 break;
170 case S_IFIFO:
171 de->d_u.d_44.d_type = DT_FIFO;
172 break;
173 default:
174 de->d_u.d_44.d_type = DT_UNKNOWN;
178 static inline u32
179 ufs_get_inode_uid(struct super_block *sb, struct ufs_inode *inode)
181 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
182 case UFS_UID_EFT:
183 return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_uid);
184 case UFS_UID_44BSD:
185 return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_uid);
186 default:
187 return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_suid);
191 static inline void
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) {
195 case UFS_UID_EFT:
196 inode->ui_u3.ui_sun.ui_uid = cpu_to_fs32(sb, value);
197 break;
198 case UFS_UID_44BSD:
199 inode->ui_u3.ui_44.ui_uid = cpu_to_fs32(sb, value);
200 break;
202 inode->ui_u1.oldids.ui_suid = cpu_to_fs16(sb, value);
205 static inline u32
206 ufs_get_inode_gid(struct super_block *sb, struct ufs_inode *inode)
208 switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
209 case UFS_UID_EFT:
210 return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_gid);
211 case UFS_UID_44BSD:
212 return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_gid);
213 default:
214 return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_sgid);
218 static inline void
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) {
222 case UFS_UID_EFT:
223 inode->ui_u3.ui_sun.ui_gid = cpu_to_fs32(sb, value);
224 break;
225 case UFS_UID_44BSD:
226 inode->ui_u3.ui_44.ui_gid = cpu_to_fs32(sb, value);
227 break;
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);
254 /* This functions works with cache pages*/
255 extern struct page *ufs_get_locked_page(struct address_space *mapping,
256 pgoff_t index);
257 static inline void ufs_put_locked_page(struct page *page)
259 unlock_page(page);
260 page_cache_release(page);
265 * macros and inline function to get important structures from ufs_sb_private_info
268 static inline void *get_usb_offset(struct ufs_sb_private_info *uspi,
269 unsigned int offset)
271 unsigned int index;
273 index = offset >> uspi->s_fshift;
274 offset &= ~uspi->s_fmask;
275 return uspi->s_ubh.bh[index]->b_data + offset;
278 #define ubh_get_usb_first(uspi) \
279 ((struct ufs_super_block_first *)get_usb_offset((uspi), 0))
281 #define ubh_get_usb_second(uspi) \
282 ((struct ufs_super_block_second *)get_usb_offset((uspi), UFS_SECTOR_SIZE))
284 #define ubh_get_usb_third(uspi) \
285 ((struct ufs_super_block_third *)get_usb_offset((uspi), 2*UFS_SECTOR_SIZE))
288 #define ubh_get_ucg(ubh) \
289 ((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
293 * Extract byte from ufs_buffer_head
294 * Extract the bits for a block from a map inside ufs_buffer_head
296 #define ubh_get_addr8(ubh,begin) \
297 ((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
298 ((begin) & ~uspi->s_fmask))
300 #define ubh_get_addr16(ubh,begin) \
301 (((__fs16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
302 ((begin) & ((uspi->fsize>>1) - 1)))
304 #define ubh_get_addr32(ubh,begin) \
305 (((__fs32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
306 ((begin) & ((uspi->s_fsize>>2) - 1)))
308 #define ubh_get_addr64(ubh,begin) \
309 (((__fs64*)((ubh)->bh[(begin) >> (uspi->s_fshift-3)]->b_data)) + \
310 ((begin) & ((uspi->s_fsize>>3) - 1)))
312 #define ubh_get_addr ubh_get_addr8
314 static inline void *ubh_get_data_ptr(struct ufs_sb_private_info *uspi,
315 struct ufs_buffer_head *ubh,
316 u64 blk)
318 if (uspi->fs_magic == UFS2_MAGIC)
319 return ubh_get_addr64(ubh, blk);
320 else
321 return ubh_get_addr32(ubh, blk);
324 #define ubh_blkmap(ubh,begin,bit) \
325 ((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
328 * Determine the number of available frags given a
329 * percentage to hold in reserve.
331 static inline u64
332 ufs_freespace(struct ufs_sb_private_info *uspi, int percentreserved)
334 return ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
335 uspi->cs_total.cs_nffree -
336 (uspi->s_dsize * (percentreserved) / 100);
340 * Macros to access cylinder group array structures
342 #define ubh_cg_blktot(ucpi,cylno) \
343 (*((__fs32*)ubh_get_addr(UCPI_UBH(ucpi), (ucpi)->c_btotoff + ((cylno) << 2))))
345 #define ubh_cg_blks(ucpi,cylno,rpos) \
346 (*((__fs16*)ubh_get_addr(UCPI_UBH(ucpi), \
347 (ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
350 * Bitmap operations
351 * These functions work like classical bitmap operations.
352 * The difference is that we don't have the whole bitmap
353 * in one contiguous chunk of memory, but in several buffers.
354 * The parameters of each function are super_block, ufs_buffer_head and
355 * position of the beginning of the bitmap.
357 #define ubh_setbit(ubh,begin,bit) \
358 (*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
360 #define ubh_clrbit(ubh,begin,bit) \
361 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
363 #define ubh_isset(ubh,begin,bit) \
364 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
366 #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
368 #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
370 #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
371 static inline unsigned _ubh_find_next_zero_bit_(
372 struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
373 unsigned begin, unsigned size, unsigned offset)
375 unsigned base, count, pos;
377 size -= offset;
378 begin <<= 3;
379 offset += begin;
380 base = offset >> uspi->s_bpfshift;
381 offset &= uspi->s_bpfmask;
382 for (;;) {
383 count = min_t(unsigned int, size + offset, uspi->s_bpf);
384 size -= count - offset;
385 pos = ext2_find_next_zero_bit (ubh->bh[base]->b_data, count, offset);
386 if (pos < count || !size)
387 break;
388 base++;
389 offset = 0;
391 return (base << uspi->s_bpfshift) + pos - begin;
394 static inline unsigned find_last_zero_bit (unsigned char * bitmap,
395 unsigned size, unsigned offset)
397 unsigned bit, i;
398 unsigned char * mapp;
399 unsigned char map;
401 mapp = bitmap + (size >> 3);
402 map = *mapp--;
403 bit = 1 << (size & 7);
404 for (i = size; i > offset; i--) {
405 if ((map & bit) == 0)
406 break;
407 if ((i & 7) != 0) {
408 bit >>= 1;
409 } else {
410 map = *mapp--;
411 bit = 1 << 7;
414 return i;
417 #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
418 static inline unsigned _ubh_find_last_zero_bit_(
419 struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
420 unsigned begin, unsigned start, unsigned end)
422 unsigned base, count, pos, size;
424 size = start - end;
425 begin <<= 3;
426 start += begin;
427 base = start >> uspi->s_bpfshift;
428 start &= uspi->s_bpfmask;
429 for (;;) {
430 count = min_t(unsigned int,
431 size + (uspi->s_bpf - start), uspi->s_bpf)
432 - (uspi->s_bpf - start);
433 size -= count;
434 pos = find_last_zero_bit (ubh->bh[base]->b_data,
435 start, start - count);
436 if (pos > start - count || !size)
437 break;
438 base--;
439 start = uspi->s_bpf;
441 return (base << uspi->s_bpfshift) + pos - begin;
444 #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
446 #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
447 static inline int _ubh_isblockset_(struct ufs_sb_private_info * uspi,
448 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
450 switch (uspi->s_fpb) {
451 case 8:
452 return (*ubh_get_addr (ubh, begin + block) == 0xff);
453 case 4:
454 return (*ubh_get_addr (ubh, begin + (block >> 1)) == (0x0f << ((block & 0x01) << 2)));
455 case 2:
456 return (*ubh_get_addr (ubh, begin + (block >> 2)) == (0x03 << ((block & 0x03) << 1)));
457 case 1:
458 return (*ubh_get_addr (ubh, begin + (block >> 3)) == (0x01 << (block & 0x07)));
460 return 0;
463 #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
464 static inline void _ubh_clrblock_(struct ufs_sb_private_info * uspi,
465 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
467 switch (uspi->s_fpb) {
468 case 8:
469 *ubh_get_addr (ubh, begin + block) = 0x00;
470 return;
471 case 4:
472 *ubh_get_addr (ubh, begin + (block >> 1)) &= ~(0x0f << ((block & 0x01) << 2));
473 return;
474 case 2:
475 *ubh_get_addr (ubh, begin + (block >> 2)) &= ~(0x03 << ((block & 0x03) << 1));
476 return;
477 case 1:
478 *ubh_get_addr (ubh, begin + (block >> 3)) &= ~(0x01 << ((block & 0x07)));
479 return;
483 #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
484 static inline void _ubh_setblock_(struct ufs_sb_private_info * uspi,
485 struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
487 switch (uspi->s_fpb) {
488 case 8:
489 *ubh_get_addr(ubh, begin + block) = 0xff;
490 return;
491 case 4:
492 *ubh_get_addr(ubh, begin + (block >> 1)) |= (0x0f << ((block & 0x01) << 2));
493 return;
494 case 2:
495 *ubh_get_addr(ubh, begin + (block >> 2)) |= (0x03 << ((block & 0x03) << 1));
496 return;
497 case 1:
498 *ubh_get_addr(ubh, begin + (block >> 3)) |= (0x01 << ((block & 0x07)));
499 return;
503 static inline void ufs_fragacct (struct super_block * sb, unsigned blockmap,
504 __fs32 * fraglist, int cnt)
506 struct ufs_sb_private_info * uspi;
507 unsigned fragsize, pos;
509 uspi = UFS_SB(sb)->s_uspi;
511 fragsize = 0;
512 for (pos = 0; pos < uspi->s_fpb; pos++) {
513 if (blockmap & (1 << pos)) {
514 fragsize++;
516 else if (fragsize > 0) {
517 fs32_add(sb, &fraglist[fragsize], cnt);
518 fragsize = 0;
521 if (fragsize > 0 && fragsize < uspi->s_fpb)
522 fs32_add(sb, &fraglist[fragsize], cnt);
525 static inline void *ufs_get_direct_data_ptr(struct ufs_sb_private_info *uspi,
526 struct ufs_inode_info *ufsi,
527 unsigned blk)
529 BUG_ON(blk > UFS_TIND_BLOCK);
530 return uspi->fs_magic == UFS2_MAGIC ?
531 (void *)&ufsi->i_u1.u2_i_data[blk] :
532 (void *)&ufsi->i_u1.i_data[blk];
535 static inline u64 ufs_data_ptr_to_cpu(struct super_block *sb, void *p)
537 return UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC ?
538 fs64_to_cpu(sb, *(__fs64 *)p) :
539 fs32_to_cpu(sb, *(__fs32 *)p);
542 static inline void ufs_cpu_to_data_ptr(struct super_block *sb, void *p, u64 val)
544 if (UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC)
545 *(__fs64 *)p = cpu_to_fs64(sb, val);
546 else
547 *(__fs32 *)p = cpu_to_fs32(sb, val);
550 static inline void ufs_data_ptr_clear(struct ufs_sb_private_info *uspi,
551 void *p)
553 if (uspi->fs_magic == UFS2_MAGIC)
554 *(__fs64 *)p = 0;
555 else
556 *(__fs32 *)p = 0;
559 static inline int ufs_is_data_ptr_zero(struct ufs_sb_private_info *uspi,
560 void *p)
562 if (uspi->fs_magic == UFS2_MAGIC)
563 return *(__fs64 *)p == 0;
564 else
565 return *(__fs32 *)p == 0;