1 /* Created (MFS based):
2 * February 2010 (Evgeniy Ivanov)
11 /*===========================================================================*
13 *===========================================================================*/
14 struct buf
*get_block(dev_t dev
, block_t block
, int how
)
16 /* Wrapper routine for lmfs_get_block(). This ext2 implementation does not deal
17 * well with block read errors pretty much anywhere. To prevent corruption due
18 * to unchecked error conditions, we panic upon an I/O failure here.
23 if ((r
= lmfs_get_block(&bp
, dev
, block
, how
)) != OK
&& r
!= ENOENT
)
24 panic("ext2: error getting block (%llu,%u): %d", dev
, block
, r
);
26 assert(r
== OK
|| how
== PEEK
);
28 return (r
== OK
) ? bp
: NULL
;
33 /*===========================================================================*
35 *===========================================================================*/
36 unsigned conv2(norm
, w
)
37 int norm
; /* TRUE if no swap, FALSE for byte swap */
38 int w
; /* promotion of 16-bit word to be swapped */
40 /* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
41 if (norm
) return( (unsigned) w
& 0xFFFF);
42 return( ((w
&BYTE
) << 8) | ( (w
>>8) & BYTE
));
46 /*===========================================================================*
48 *===========================================================================*/
50 int norm
; /* TRUE if no swap, FALSE for byte swap */
51 long x
; /* 32-bit long to be byte swapped */
53 /* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
57 if (norm
) return(x
); /* byte order was already ok */
58 lo
= conv2(FALSE
, (int) x
& 0xFFFF); /* low-order half, byte swapped */
59 hi
= conv2(FALSE
, (int) (x
>>16) & 0xFFFF); /* high-order half, swapped */
60 l
= ( (long) lo
<<16) | hi
;
65 /*===========================================================================*
67 *===========================================================================*/
68 int ansi_strcmp(register const char* ansi_s
, register const char *s2
,
69 register size_t ansi_s_length
)
71 /* Compare non null-terminated string ansi_s (length=ansi_s_length)
73 * It returns 0 if strings are equal, otherwise -1 is returned.
79 if (*ansi_s
++ != *s2
++)
81 } while (--ansi_s_length
> 0);
92 /*===========================================================================*
94 *===========================================================================*/
95 bit_t
setbit(bitchunk_t
*bitmap
, bit_t max_bits
, unsigned int word
)
97 /* Find free bit in bitmap and set. Return number of the bit,
98 * if failed return -1.
100 bitchunk_t
*wptr
, *wlim
;
103 /* TODO: do we need to add 1? I saw a situation, when it was
104 * required, and since we check bit number with max_bits it
107 wlim
= &bitmap
[FS_BITMAP_CHUNKS(max_bits
>> 3)];
109 /* Iterate over the words in block. */
110 for (wptr
= &bitmap
[word
]; wptr
< wlim
; wptr
++) {
114 /* Does this word contain a free bit? */
115 if (*wptr
== (bitchunk_t
) ~0)
118 /* Find and allocate the free bit. */
120 for (i
= 0; (k
& (1 << i
)) != 0; ++i
) {}
122 /* Bit number from the start of the bit map. */
123 b
= (wptr
- &bitmap
[0]) * FS_BITCHUNK_BITS
+ i
;
125 /* Don't allocate bits beyond the end of the map. */
131 /* Allocate bit number. */
141 /*===========================================================================*
143 *===========================================================================*/
144 bit_t
setbyte(bitchunk_t
*bitmap
, bit_t max_bits
)
146 /* Find free byte in bitmap and set it. Return number of the starting bit,
147 * if failed return -1.
149 unsigned char *wptr
, *wlim
;
152 wptr
= (unsigned char*) &bitmap
[0];
153 /* TODO: do we need to add 1? I saw a situation, when it was
154 * required, and since we check bit number with max_bits it
157 wlim
= &wptr
[(max_bits
>> 3)];
159 /* Iterate over the words in block. */
160 for ( ; wptr
< wlim
; wptr
++) {
161 /* Is it a free byte? */
165 /* Bit number from the start of the bit map. */
166 b
= (wptr
- (unsigned char*) &bitmap
[0]) * CHAR_BIT
;
168 /* Don't allocate bits beyond the end of the map. */
169 if (b
+ CHAR_BIT
>= max_bits
) {
174 /* Allocate byte number. */
175 *wptr
= (unsigned char) ~0;
182 /*===========================================================================*
184 *===========================================================================*/
185 int unsetbit(bitchunk_t
*bitmap
, bit_t bit
)
187 /* Unset specified bit. If requested bit is already free return -1,
188 * otherwise return 0.
190 unsigned int word
; /* bit_returned word in bitmap */
193 word
= bit
/ FS_BITCHUNK_BITS
;
194 bit
= bit
% FS_BITCHUNK_BITS
; /* index in word */
197 k
= (int) bitmap
[word
];
202 bitmap
[word
] = (int) k
;