coverity appeasement
[minix.git] / servers / ext2 / utility.c
blob776bd152f171cf824aa5c4a6785988c237534af4
1 /* Created (MFS based):
2 * February 2010 (Evgeniy Ivanov)
3 */
5 #include "fs.h"
6 #include "buf.h"
7 #include "inode.h"
8 #include "super.h"
11 /*===========================================================================*
12 * no_sys *
13 *===========================================================================*/
14 int no_sys()
16 /* Somebody has used an illegal system call number */
17 printf("no_sys: invalid call %d\n", req_nr);
18 return(EINVAL);
22 /*===========================================================================*
23 * conv2 *
24 *===========================================================================*/
25 unsigned conv2(norm, w)
26 int norm; /* TRUE if no swap, FALSE for byte swap */
27 int w; /* promotion of 16-bit word to be swapped */
29 /* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
30 if (norm) return( (unsigned) w & 0xFFFF);
31 return( ((w&BYTE) << 8) | ( (w>>8) & BYTE));
35 /*===========================================================================*
36 * conv4 *
37 *===========================================================================*/
38 long conv4(norm, x)
39 int norm; /* TRUE if no swap, FALSE for byte swap */
40 long x; /* 32-bit long to be byte swapped */
42 /* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
43 unsigned lo, hi;
44 long l;
46 if (norm) return(x); /* byte order was already ok */
47 lo = conv2(FALSE, (int) x & 0xFFFF); /* low-order half, byte swapped */
48 hi = conv2(FALSE, (int) (x>>16) & 0xFFFF); /* high-order half, swapped */
49 l = ( (long) lo <<16) | hi;
50 return(l);
54 /*===========================================================================*
55 * clock_time *
56 *===========================================================================*/
57 time_t clock_time()
59 /* This routine returns the time in seconds since 1.1.1970. MINIX is an
60 * astrophysically naive system that assumes the earth rotates at a constant
61 * rate and that such things as leap seconds do not exist.
64 register int k;
65 clock_t uptime;
66 time_t boottime;
68 if ( (k=getuptime2(&uptime, &boottime)) != OK)
69 panic("clock_time: getuptme2 failed: %d", k);
71 return( (time_t) (boottime + (uptime/sys_hz())));
75 /*===========================================================================*
76 * mfs_min *
77 *===========================================================================*/
78 int min(unsigned int l, unsigned int r)
80 if(r >= l) return(l);
82 return(r);
86 /*===========================================================================*
87 * mfs_nul *
88 *===========================================================================*/
89 void mfs_nul_f(char *file, int line, char *str, unsigned int len,
90 unsigned int maxlen)
92 if(len < maxlen && str[len-1] != '\0') {
93 printf("ext2 %s:%d string (length %d, maxlen %d) not null-terminated\n",
94 file, line, len, maxlen);
98 #define MYASSERT(c) if(!(c)) { printf("ext2:%s:%d: sanity check: %s failed\n", \
99 file, line, #c); panic("sanity check " #c " failed: %d", __LINE__); }
102 /*===========================================================================*
103 * sanity_check *
104 *===========================================================================*/
105 void sanitycheck(char *file, int line)
107 MYASSERT(SELF_E > 0);
108 if(superblock->s_dev != NO_DEV) {
109 MYASSERT(superblock->s_dev == fs_dev);
110 MYASSERT(superblock->s_block_size == fs_block_size);
111 } else {
112 MYASSERT(_MIN_BLOCK_SIZE == fs_block_size);
116 /*===========================================================================*
117 * ansi_strcmp *
118 *===========================================================================*/
119 int ansi_strcmp(register const char* ansi_s, register const char *s2,
120 register size_t ansi_s_length)
122 /* Compare non null-terminated string ansi_s (length=ansi_s_length)
123 * with C-string s2.
124 * It returns 0 if strings are equal, otherwise -1 is returned.
126 if (ansi_s_length) {
127 do {
128 if (*s2 == '\0')
129 return -1;
130 if (*ansi_s++ != *s2++)
131 return -1;
132 } while (--ansi_s_length > 0);
134 if (*s2 == '\0')
135 return 0;
136 else
137 return -1;
139 return 0;
143 /*===========================================================================*
144 * setbit *
145 *===========================================================================*/
146 bit_t setbit(bitchunk_t *bitmap, bit_t max_bits, unsigned int word)
148 /* Find free bit in bitmap and set. Return number of the bit,
149 * if failed return -1.
151 bitchunk_t *wptr, *wlim;
152 bit_t b = -1;
154 /* TODO: do we need to add 1? I saw a situation, when it was
155 * required, and since we check bit number with max_bits it
156 * should be safe.
158 wlim = &bitmap[FS_BITMAP_CHUNKS(max_bits >> 3)];
160 /* Iterate over the words in block. */
161 for (wptr = &bitmap[word]; wptr < wlim; wptr++) {
162 bit_t i;
163 bitchunk_t k;
165 /* Does this word contain a free bit? */
166 if (*wptr == (bitchunk_t) ~0)
167 continue;
169 /* Find and allocate the free bit. */
170 k = (int) *wptr;
171 for (i = 0; (k & (1 << i)) != 0; ++i) {}
173 /* Bit number from the start of the bit map. */
174 b = (wptr - &bitmap[0]) * FS_BITCHUNK_BITS + i;
176 /* Don't allocate bits beyond the end of the map. */
177 if (b >= max_bits) {
178 b = -1;
179 continue;
182 /* Allocate bit number. */
183 k |= 1 << i;
184 *wptr = (int) k;
185 break;
188 return b;
192 /*===========================================================================*
193 * setbyte *
194 *===========================================================================*/
195 bit_t setbyte(bitchunk_t *bitmap, bit_t max_bits)
197 /* Find free byte in bitmap and set it. Return number of the starting bit,
198 * if failed return -1.
200 unsigned char *wptr, *wlim;
201 bit_t b = -1;
203 wptr = (unsigned char*) &bitmap[0];
204 /* TODO: do we need to add 1? I saw a situation, when it was
205 * required, and since we check bit number with max_bits it
206 * should be safe.
208 wlim = &wptr[(max_bits >> 3)];
210 /* Iterate over the words in block. */
211 for ( ; wptr < wlim; wptr++) {
212 /* Is it a free byte? */
213 if (*wptr | 0)
214 continue;
216 /* Bit number from the start of the bit map. */
217 b = (wptr - (unsigned char*) &bitmap[0]) * CHAR_BIT;
219 /* Don't allocate bits beyond the end of the map. */
220 if (b + CHAR_BIT >= max_bits) {
221 b = -1;
222 continue;
225 /* Allocate byte number. */
226 *wptr = (unsigned char) ~0;
227 break;
229 return b;
233 /*===========================================================================*
234 * unsetbit *
235 *===========================================================================*/
236 int unsetbit(bitchunk_t *bitmap, bit_t bit)
238 /* Unset specified bit. If requested bit is already free return -1,
239 * otherwise return 0.
241 unsigned int word; /* bit_returned word in bitmap */
242 bitchunk_t k, mask;
244 word = bit / FS_BITCHUNK_BITS;
245 bit = bit % FS_BITCHUNK_BITS; /* index in word */
246 mask = 1 << bit;
248 k = (int) bitmap[word];
249 if (!(k & mask))
250 return -1;
252 k &= ~mask;
253 bitmap[word] = (int) k;
254 return 0;