]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/ufs/util.h
0466036912f1adb41ccf85eb2d3dbd81b669ef4e
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 if (fs32_to_cpu(sb
, usb3
->fs_postblformat
) == UFS_42POSTBLFMT
)
43 return fs32_to_cpu(sb
, usb1
->fs_u0
.fs_sun
.fs_state
);
44 /* Fall Through to UFS_ST_SUN */
46 return fs32_to_cpu(sb
, usb3
->fs_un2
.fs_sun
.fs_state
);
48 return fs32_to_cpu(sb
, usb1
->fs_u1
.fs_sunx86
.fs_state
);
51 return fs32_to_cpu(sb
, usb3
->fs_un2
.fs_44
.fs_state
);
56 ufs_set_fs_state(struct super_block
*sb
, struct ufs_super_block_first
*usb1
,
57 struct ufs_super_block_third
*usb3
, s32 value
)
59 switch (UFS_SB(sb
)->s_flags
& UFS_ST_MASK
) {
61 if (fs32_to_cpu(sb
, usb3
->fs_postblformat
) == UFS_42POSTBLFMT
) {
62 usb1
->fs_u0
.fs_sun
.fs_state
= cpu_to_fs32(sb
, value
);
65 /* Fall Through to UFS_ST_SUN */
67 usb3
->fs_un2
.fs_sun
.fs_state
= cpu_to_fs32(sb
, value
);
70 usb1
->fs_u1
.fs_sunx86
.fs_state
= cpu_to_fs32(sb
, value
);
73 usb3
->fs_un2
.fs_44
.fs_state
= cpu_to_fs32(sb
, value
);
79 ufs_get_fs_npsect(struct super_block
*sb
, struct ufs_super_block_first
*usb1
,
80 struct ufs_super_block_third
*usb3
)
82 if ((UFS_SB(sb
)->s_flags
& UFS_ST_MASK
) == UFS_ST_SUNx86
)
83 return fs32_to_cpu(sb
, usb3
->fs_un2
.fs_sunx86
.fs_npsect
);
85 return fs32_to_cpu(sb
, usb1
->fs_u1
.fs_sun
.fs_npsect
);
89 ufs_get_fs_qbmask(struct super_block
*sb
, struct ufs_super_block_third
*usb3
)
93 switch (UFS_SB(sb
)->s_flags
& UFS_ST_MASK
) {
96 ((__fs32
*)&tmp
)[0] = usb3
->fs_un2
.fs_sun
.fs_qbmask
[0];
97 ((__fs32
*)&tmp
)[1] = usb3
->fs_un2
.fs_sun
.fs_qbmask
[1];
100 ((__fs32
*)&tmp
)[0] = usb3
->fs_un2
.fs_sunx86
.fs_qbmask
[0];
101 ((__fs32
*)&tmp
)[1] = usb3
->fs_un2
.fs_sunx86
.fs_qbmask
[1];
104 ((__fs32
*)&tmp
)[0] = usb3
->fs_un2
.fs_44
.fs_qbmask
[0];
105 ((__fs32
*)&tmp
)[1] = usb3
->fs_un2
.fs_44
.fs_qbmask
[1];
109 return fs64_to_cpu(sb
, tmp
);
113 ufs_get_fs_qfmask(struct super_block
*sb
, struct ufs_super_block_third
*usb3
)
117 switch (UFS_SB(sb
)->s_flags
& UFS_ST_MASK
) {
120 ((__fs32
*)&tmp
)[0] = usb3
->fs_un2
.fs_sun
.fs_qfmask
[0];
121 ((__fs32
*)&tmp
)[1] = usb3
->fs_un2
.fs_sun
.fs_qfmask
[1];
124 ((__fs32
*)&tmp
)[0] = usb3
->fs_un2
.fs_sunx86
.fs_qfmask
[0];
125 ((__fs32
*)&tmp
)[1] = usb3
->fs_un2
.fs_sunx86
.fs_qfmask
[1];
128 ((__fs32
*)&tmp
)[0] = usb3
->fs_un2
.fs_44
.fs_qfmask
[0];
129 ((__fs32
*)&tmp
)[1] = usb3
->fs_un2
.fs_44
.fs_qfmask
[1];
133 return fs64_to_cpu(sb
, tmp
);
137 ufs_get_de_namlen(struct super_block
*sb
, struct ufs_dir_entry
*de
)
139 if ((UFS_SB(sb
)->s_flags
& UFS_DE_MASK
) == UFS_DE_OLD
)
140 return fs16_to_cpu(sb
, de
->d_u
.d_namlen
);
142 return de
->d_u
.d_44
.d_namlen
; /* XXX this seems wrong */
146 ufs_set_de_namlen(struct super_block
*sb
, struct ufs_dir_entry
*de
, u16 value
)
148 if ((UFS_SB(sb
)->s_flags
& UFS_DE_MASK
) == UFS_DE_OLD
)
149 de
->d_u
.d_namlen
= cpu_to_fs16(sb
, value
);
151 de
->d_u
.d_44
.d_namlen
= value
; /* XXX this seems wrong */
155 ufs_set_de_type(struct super_block
*sb
, struct ufs_dir_entry
*de
, int mode
)
157 if ((UFS_SB(sb
)->s_flags
& UFS_DE_MASK
) != UFS_DE_44BSD
)
161 * TODO turn this into a table lookup
163 switch (mode
& S_IFMT
) {
165 de
->d_u
.d_44
.d_type
= DT_SOCK
;
168 de
->d_u
.d_44
.d_type
= DT_LNK
;
171 de
->d_u
.d_44
.d_type
= DT_REG
;
174 de
->d_u
.d_44
.d_type
= DT_BLK
;
177 de
->d_u
.d_44
.d_type
= DT_DIR
;
180 de
->d_u
.d_44
.d_type
= DT_CHR
;
183 de
->d_u
.d_44
.d_type
= DT_FIFO
;
186 de
->d_u
.d_44
.d_type
= DT_UNKNOWN
;
191 ufs_get_inode_uid(struct super_block
*sb
, struct ufs_inode
*inode
)
193 switch (UFS_SB(sb
)->s_flags
& UFS_UID_MASK
) {
195 return fs32_to_cpu(sb
, inode
->ui_u3
.ui_44
.ui_uid
);
197 if (inode
->ui_u1
.oldids
.ui_suid
== 0xFFFF)
198 return fs32_to_cpu(sb
, inode
->ui_u3
.ui_sun
.ui_uid
);
201 return fs16_to_cpu(sb
, inode
->ui_u1
.oldids
.ui_suid
);
206 ufs_set_inode_uid(struct super_block
*sb
, struct ufs_inode
*inode
, u32 value
)
208 switch (UFS_SB(sb
)->s_flags
& UFS_UID_MASK
) {
210 inode
->ui_u3
.ui_44
.ui_uid
= cpu_to_fs32(sb
, value
);
211 inode
->ui_u1
.oldids
.ui_suid
= cpu_to_fs16(sb
, value
);
214 inode
->ui_u3
.ui_sun
.ui_uid
= cpu_to_fs32(sb
, value
);
219 inode
->ui_u1
.oldids
.ui_suid
= cpu_to_fs16(sb
, value
);
225 ufs_get_inode_gid(struct super_block
*sb
, struct ufs_inode
*inode
)
227 switch (UFS_SB(sb
)->s_flags
& UFS_UID_MASK
) {
229 return fs32_to_cpu(sb
, inode
->ui_u3
.ui_44
.ui_gid
);
231 if (inode
->ui_u1
.oldids
.ui_suid
== 0xFFFF)
232 return fs32_to_cpu(sb
, inode
->ui_u3
.ui_sun
.ui_gid
);
235 return fs16_to_cpu(sb
, inode
->ui_u1
.oldids
.ui_sgid
);
240 ufs_set_inode_gid(struct super_block
*sb
, struct ufs_inode
*inode
, u32 value
)
242 switch (UFS_SB(sb
)->s_flags
& UFS_UID_MASK
) {
244 inode
->ui_u3
.ui_44
.ui_gid
= cpu_to_fs32(sb
, value
);
245 inode
->ui_u1
.oldids
.ui_sgid
= cpu_to_fs16(sb
, value
);
248 inode
->ui_u3
.ui_sun
.ui_gid
= cpu_to_fs32(sb
, value
);
253 inode
->ui_u1
.oldids
.ui_sgid
= cpu_to_fs16(sb
, value
);
258 extern dev_t
ufs_get_inode_dev(struct super_block
*, struct ufs_inode_info
*);
259 extern void ufs_set_inode_dev(struct super_block
*, struct ufs_inode_info
*, dev_t
);
260 extern int ufs_prepare_chunk(struct page
*page
, loff_t pos
, unsigned len
);
263 * These functions manipulate ufs buffers
265 #define ubh_bread(sb,fragment,size) _ubh_bread_(uspi,sb,fragment,size)
266 extern struct ufs_buffer_head
* _ubh_bread_(struct ufs_sb_private_info
*, struct super_block
*, u64
, u64
);
267 extern struct ufs_buffer_head
* ubh_bread_uspi(struct ufs_sb_private_info
*, struct super_block
*, u64
, u64
);
268 extern void ubh_brelse (struct ufs_buffer_head
*);
269 extern void ubh_brelse_uspi (struct ufs_sb_private_info
*);
270 extern void ubh_mark_buffer_dirty (struct ufs_buffer_head
*);
271 extern void ubh_mark_buffer_uptodate (struct ufs_buffer_head
*, int);
272 extern void ubh_ll_rw_block(int, struct ufs_buffer_head
*);
273 extern void ubh_wait_on_buffer (struct ufs_buffer_head
*);
274 extern void ubh_bforget (struct ufs_buffer_head
*);
275 extern int ubh_buffer_dirty (struct ufs_buffer_head
*);
276 #define ubh_ubhcpymem(mem,ubh,size) _ubh_ubhcpymem_(uspi,mem,ubh,size)
277 extern void _ubh_ubhcpymem_(struct ufs_sb_private_info
*, unsigned char *, struct ufs_buffer_head
*, unsigned);
278 #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
279 extern void _ubh_memcpyubh_(struct ufs_sb_private_info
*, struct ufs_buffer_head
*, unsigned char *, unsigned);
281 /* This functions works with cache pages*/
282 extern struct page
*ufs_get_locked_page(struct address_space
*mapping
,
284 static inline void ufs_put_locked_page(struct page
*page
)
287 page_cache_release(page
);
292 * macros and inline function to get important structures from ufs_sb_private_info
295 static inline void *get_usb_offset(struct ufs_sb_private_info
*uspi
,
300 index
= offset
>> uspi
->s_fshift
;
301 offset
&= ~uspi
->s_fmask
;
302 return uspi
->s_ubh
.bh
[index
]->b_data
+ offset
;
305 #define ubh_get_usb_first(uspi) \
306 ((struct ufs_super_block_first *)get_usb_offset((uspi), 0))
308 #define ubh_get_usb_second(uspi) \
309 ((struct ufs_super_block_second *)get_usb_offset((uspi), UFS_SECTOR_SIZE))
311 #define ubh_get_usb_third(uspi) \
312 ((struct ufs_super_block_third *)get_usb_offset((uspi), 2*UFS_SECTOR_SIZE))
315 #define ubh_get_ucg(ubh) \
316 ((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
320 * Extract byte from ufs_buffer_head
321 * Extract the bits for a block from a map inside ufs_buffer_head
323 #define ubh_get_addr8(ubh,begin) \
324 ((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
325 ((begin) & ~uspi->s_fmask))
327 #define ubh_get_addr16(ubh,begin) \
328 (((__fs16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
329 ((begin) & ((uspi->fsize>>1) - 1)))
331 #define ubh_get_addr32(ubh,begin) \
332 (((__fs32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
333 ((begin) & ((uspi->s_fsize>>2) - 1)))
335 #define ubh_get_addr64(ubh,begin) \
336 (((__fs64*)((ubh)->bh[(begin) >> (uspi->s_fshift-3)]->b_data)) + \
337 ((begin) & ((uspi->s_fsize>>3) - 1)))
339 #define ubh_get_addr ubh_get_addr8
341 static inline void *ubh_get_data_ptr(struct ufs_sb_private_info
*uspi
,
342 struct ufs_buffer_head
*ubh
,
345 if (uspi
->fs_magic
== UFS2_MAGIC
)
346 return ubh_get_addr64(ubh
, blk
);
348 return ubh_get_addr32(ubh
, blk
);
351 #define ubh_blkmap(ubh,begin,bit) \
352 ((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
355 * Determine the number of available frags given a
356 * percentage to hold in reserve.
359 ufs_freespace(struct ufs_sb_private_info
*uspi
, int percentreserved
)
361 return ufs_blkstofrags(uspi
->cs_total
.cs_nbfree
) +
362 uspi
->cs_total
.cs_nffree
-
363 (uspi
->s_dsize
* (percentreserved
) / 100);
367 * Macros to access cylinder group array structures
369 #define ubh_cg_blktot(ucpi,cylno) \
370 (*((__fs32*)ubh_get_addr(UCPI_UBH(ucpi), (ucpi)->c_btotoff + ((cylno) << 2))))
372 #define ubh_cg_blks(ucpi,cylno,rpos) \
373 (*((__fs16*)ubh_get_addr(UCPI_UBH(ucpi), \
374 (ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
378 * These functions work like classical bitmap operations.
379 * The difference is that we don't have the whole bitmap
380 * in one contiguous chunk of memory, but in several buffers.
381 * The parameters of each function are super_block, ufs_buffer_head and
382 * position of the beginning of the bitmap.
384 #define ubh_setbit(ubh,begin,bit) \
385 (*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
387 #define ubh_clrbit(ubh,begin,bit) \
388 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
390 #define ubh_isset(ubh,begin,bit) \
391 (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
393 #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
395 #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
397 #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
398 static inline unsigned _ubh_find_next_zero_bit_(
399 struct ufs_sb_private_info
* uspi
, struct ufs_buffer_head
* ubh
,
400 unsigned begin
, unsigned size
, unsigned offset
)
402 unsigned base
, count
, pos
;
407 base
= offset
>> uspi
->s_bpfshift
;
408 offset
&= uspi
->s_bpfmask
;
410 count
= min_t(unsigned int, size
+ offset
, uspi
->s_bpf
);
411 size
-= count
- offset
;
412 pos
= ext2_find_next_zero_bit (ubh
->bh
[base
]->b_data
, count
, offset
);
413 if (pos
< count
|| !size
)
418 return (base
<< uspi
->s_bpfshift
) + pos
- begin
;
421 static inline unsigned find_last_zero_bit (unsigned char * bitmap
,
422 unsigned size
, unsigned offset
)
425 unsigned char * mapp
;
428 mapp
= bitmap
+ (size
>> 3);
430 bit
= 1 << (size
& 7);
431 for (i
= size
; i
> offset
; i
--) {
432 if ((map
& bit
) == 0)
444 #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
445 static inline unsigned _ubh_find_last_zero_bit_(
446 struct ufs_sb_private_info
* uspi
, struct ufs_buffer_head
* ubh
,
447 unsigned begin
, unsigned start
, unsigned end
)
449 unsigned base
, count
, pos
, size
;
454 base
= start
>> uspi
->s_bpfshift
;
455 start
&= uspi
->s_bpfmask
;
457 count
= min_t(unsigned int,
458 size
+ (uspi
->s_bpf
- start
), uspi
->s_bpf
)
459 - (uspi
->s_bpf
- start
);
461 pos
= find_last_zero_bit (ubh
->bh
[base
]->b_data
,
462 start
, start
- count
);
463 if (pos
> start
- count
|| !size
)
468 return (base
<< uspi
->s_bpfshift
) + pos
- begin
;
471 #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
473 #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
474 static inline int _ubh_isblockset_(struct ufs_sb_private_info
* uspi
,
475 struct ufs_buffer_head
* ubh
, unsigned begin
, unsigned block
)
477 switch (uspi
->s_fpb
) {
479 return (*ubh_get_addr (ubh
, begin
+ block
) == 0xff);
481 return (*ubh_get_addr (ubh
, begin
+ (block
>> 1)) == (0x0f << ((block
& 0x01) << 2)));
483 return (*ubh_get_addr (ubh
, begin
+ (block
>> 2)) == (0x03 << ((block
& 0x03) << 1)));
485 return (*ubh_get_addr (ubh
, begin
+ (block
>> 3)) == (0x01 << (block
& 0x07)));
490 #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
491 static inline void _ubh_clrblock_(struct ufs_sb_private_info
* uspi
,
492 struct ufs_buffer_head
* ubh
, unsigned begin
, unsigned block
)
494 switch (uspi
->s_fpb
) {
496 *ubh_get_addr (ubh
, begin
+ block
) = 0x00;
499 *ubh_get_addr (ubh
, begin
+ (block
>> 1)) &= ~(0x0f << ((block
& 0x01) << 2));
502 *ubh_get_addr (ubh
, begin
+ (block
>> 2)) &= ~(0x03 << ((block
& 0x03) << 1));
505 *ubh_get_addr (ubh
, begin
+ (block
>> 3)) &= ~(0x01 << ((block
& 0x07)));
510 #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
511 static inline void _ubh_setblock_(struct ufs_sb_private_info
* uspi
,
512 struct ufs_buffer_head
* ubh
, unsigned begin
, unsigned block
)
514 switch (uspi
->s_fpb
) {
516 *ubh_get_addr(ubh
, begin
+ block
) = 0xff;
519 *ubh_get_addr(ubh
, begin
+ (block
>> 1)) |= (0x0f << ((block
& 0x01) << 2));
522 *ubh_get_addr(ubh
, begin
+ (block
>> 2)) |= (0x03 << ((block
& 0x03) << 1));
525 *ubh_get_addr(ubh
, begin
+ (block
>> 3)) |= (0x01 << ((block
& 0x07)));
530 static inline void ufs_fragacct (struct super_block
* sb
, unsigned blockmap
,
531 __fs32
* fraglist
, int cnt
)
533 struct ufs_sb_private_info
* uspi
;
534 unsigned fragsize
, pos
;
536 uspi
= UFS_SB(sb
)->s_uspi
;
539 for (pos
= 0; pos
< uspi
->s_fpb
; pos
++) {
540 if (blockmap
& (1 << pos
)) {
543 else if (fragsize
> 0) {
544 fs32_add(sb
, &fraglist
[fragsize
], cnt
);
548 if (fragsize
> 0 && fragsize
< uspi
->s_fpb
)
549 fs32_add(sb
, &fraglist
[fragsize
], cnt
);
552 static inline void *ufs_get_direct_data_ptr(struct ufs_sb_private_info
*uspi
,
553 struct ufs_inode_info
*ufsi
,
556 BUG_ON(blk
> UFS_TIND_BLOCK
);
557 return uspi
->fs_magic
== UFS2_MAGIC
?
558 (void *)&ufsi
->i_u1
.u2_i_data
[blk
] :
559 (void *)&ufsi
->i_u1
.i_data
[blk
];
562 static inline u64
ufs_data_ptr_to_cpu(struct super_block
*sb
, void *p
)
564 return UFS_SB(sb
)->s_uspi
->fs_magic
== UFS2_MAGIC
?
565 fs64_to_cpu(sb
, *(__fs64
*)p
) :
566 fs32_to_cpu(sb
, *(__fs32
*)p
);
569 static inline void ufs_cpu_to_data_ptr(struct super_block
*sb
, void *p
, u64 val
)
571 if (UFS_SB(sb
)->s_uspi
->fs_magic
== UFS2_MAGIC
)
572 *(__fs64
*)p
= cpu_to_fs64(sb
, val
);
574 *(__fs32
*)p
= cpu_to_fs32(sb
, val
);
577 static inline void ufs_data_ptr_clear(struct ufs_sb_private_info
*uspi
,
580 if (uspi
->fs_magic
== UFS2_MAGIC
)
586 static inline int ufs_is_data_ptr_zero(struct ufs_sb_private_info
*uspi
,
589 if (uspi
->fs_magic
== UFS2_MAGIC
)
590 return *(__fs64
*)p
== 0;
592 return *(__fs32
*)p
== 0;