Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / portals_utils.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/winnt/portals_utils.h
35  *
36  * Basic library routines.
37  */
38
39 #ifndef __LIBCFS_WINNT_PORTALS_UTILS_H__
40 #define __LIBCFS_WINNT_PORTALS_UTILS_H__
41
42 #ifndef cfs_is_flag_set
43 #define cfs_is_flag_set(x,f) (((x)&(f))==(f))
44 #endif
45
46 #ifndef cfs_set_flag
47 #define cfs_set_flag(x,f)    ((x) |= (f))
48 #endif
49
50 #ifndef cfs_clear_flag
51 #define cfs_clear_flag(x,f)  ((x) &= ~(f))
52 #endif
53
54 static inline __u32 do_div64(__u64 * n, __u64 b) 
55 {
56     __u64   mod;
57
58     mod = *n % b;
59     *n  = *n / b;
60     return (__u32)mod;
61
62
63 #define do_div(n, b) do_div64(&(n), (__u64)b)
64 #ifdef __KERNEL__
65
66 #include <stdlib.h>
67 #include <libcfs/winnt/winnt-types.h>
68
69 char * strsep(char **s, const char *ct);
70 char * ul2dstr(ulong_ptr_t address, char *buf, int len);
71
72 #define simple_strtol(a1, a2, a3)               strtol(a1, a2, a3)
73 #define simple_strtoll(a1, a2, a3)              (__s64)strtoull(a1, a2, a3)
74 #define simple_strtoull(a1, a2, a3)             strtoull(a1, a2, a3)
75
76 unsigned long simple_strtoul(const char *cp,char **endp, unsigned int base);
77
78 static inline int cfs_set_bit(int nr, void * addr)
79 {
80     (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
81     return *((int *) addr);
82 }
83
84 static inline int cfs_test_bit(int nr, void * addr)
85 {
86     return (int)(((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0);
87 }
88
89 static inline int cfs_clear_bit(int nr, void * addr)
90 {
91     (((volatile ULONG *) addr)[nr >> 5]) &= (~(1UL << (nr & 31)));
92     return *((int *) addr);
93 }
94
95 static inline int cfs_test_and_set_bit(int nr, volatile void *addr)
96 {
97     int rc;
98     unsigned char  mask;
99     volatile unsigned char *ADDR = addr;
100
101     ADDR += nr >> 3;
102     mask = 1 << (nr & 0x07);
103     rc = ((mask & *ADDR) != 0);
104     *ADDR |= mask;
105
106     return rc;
107 }
108
109 #define ext2_set_bit(nr,addr)   (cfs_set_bit(nr, addr), 0)
110 #define ext2_clear_bit(nr,addr) (cfs_clear_bit(nr, addr), 0)
111 #define ext2_test_bit(nr,addr)  cfs_test_bit(nr, addr)
112
113 static inline int cfs_ffs(int x)
114 {
115         int r = 1;
116
117         if (!x)
118                 return 0;
119         if (!(x & 0xffff)) {
120                 x >>= 16;
121                 r += 16;
122         }
123         if (!(x & 0xff)) {
124                 x >>= 8;
125                 r += 8;
126         }
127         if (!(x & 0xf)) {
128                 x >>= 4;
129                 r += 4;
130         }
131         if (!(x & 3)) {
132                 x >>= 2;
133                 r += 2;
134         }
135         if (!(x & 1)) {
136                 x >>= 1;
137                 r += 1;
138         }
139         return r;
140 }
141
142 static inline unsigned long __cfs_ffs(unsigned long word)
143 {
144         int num = 0;
145
146 #if BITS_PER_LONG == 64
147         if ((word & 0xffffffff) == 0) {
148                 num += 32;
149                 word >>= 32;
150         }
151 #endif
152         if ((word & 0xffff) == 0) {
153                 num += 16;
154                 word >>= 16;
155         }
156         if ((word & 0xff) == 0) {
157                 num += 8;
158                 word >>= 8;
159         }
160         if ((word & 0xf) == 0) {
161                 num += 4;
162                 word >>= 4;
163         }
164         if ((word & 0x3) == 0) {
165                 num += 2;
166                 word >>= 2;
167         }
168         if ((word & 0x1) == 0)
169                 num += 1;
170         return num;
171 }
172
173 /**
174  * fls - find last (most-significant) bit set
175  * @x: the word to search
176  *
177  * This is defined the same way as ffs.
178  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
179  */
180 static inline
181 int cfs_fls(int x)
182 {
183         int r = 32;
184
185         if (!x)
186                 return 0;
187         if (!(x & 0xffff0000u)) {
188                 x <<= 16;
189                 r -= 16;
190         }
191         if (!(x & 0xff000000u)) {
192                 x <<= 8;
193                 r -= 8;
194         }
195         if (!(x & 0xf0000000u)) {
196                 x <<= 4;
197                 r -= 4;
198         }
199         if (!(x & 0xc0000000u)) {
200                 x <<= 2;
201                 r -= 2;
202         }
203         if (!(x & 0x80000000u)) {
204                 x <<= 1;
205                 r -= 1;
206         }
207         return r;
208 }
209
210 static inline unsigned cfs_find_first_bit(const unsigned long *addr,
211                                           unsigned size)
212 {
213         unsigned x = 0;
214
215         while (x < size) {
216                 unsigned long val = *addr++;
217                 if (val)
218                         return __cfs_ffs(val) + x;
219                 x += (sizeof(*addr)<<3);
220         }
221         return x;
222 }
223
224 static inline void read_random(char *buf, int len)
225 {
226     ULONG   Seed = (ULONG)(ULONG_PTR) buf;
227     Seed = RtlRandom(&Seed);
228     while (len >0) {
229         if (len > sizeof(ULONG)) {
230             memcpy(buf, &Seed, sizeof(ULONG));
231             len -= sizeof(ULONG);
232             buf += sizeof(ULONG);
233         } else {
234             memcpy(buf, &Seed, len);
235             len = 0;
236             break;
237         } 
238     }
239 }
240
241 #define cfs_get_random_bytes_prim(buf, len)  read_random(buf, len)
242
243 /* do NOT use function or expression as parameters ... */
244
245 #ifndef min_t
246 #define min_t(type,x,y) (type)(x) < (type)(y) ? (x): (y)
247 #endif
248
249 #ifndef max_t
250 #define max_t(type,x,y) (type)(x) < (type)(y) ? (y): (x)
251 #endif
252
253
254 #define NIPQUAD(addr)                       \
255         ((unsigned char *)&addr)[0],    \
256         ((unsigned char *)&addr)[1],    \
257         ((unsigned char *)&addr)[2],    \
258         ((unsigned char *)&addr)[3]
259
260 #define HIPQUAD(addr)                       \
261         ((unsigned char *)&addr)[3],    \
262         ((unsigned char *)&addr)[2],    \
263         ((unsigned char *)&addr)[1],    \
264         ((unsigned char *)&addr)[0]
265
266 static int cfs_copy_from_user(void *to, void *from, int c) 
267 {
268     memcpy(to, from, c);
269     return 0;
270 }
271
272 static int cfs_copy_to_user(void *to, const void *from, int c) 
273 {
274     memcpy(to, from, c);
275     return 0;
276 }
277
278 static unsigned long
279 clear_user(void __user *to, unsigned long n)
280 {
281     memset(to, 0, n);
282         return n;
283 }
284
285 #define put_user(x, ptr)        \
286 (                               \
287     *(ptr) = x,                 \
288     0                           \
289 )
290
291
292 #define get_user(x,ptr)         \
293 (                               \
294     x = *(ptr),                 \
295     0                           \
296 )
297
298 #define cfs_num_physpages               (64 * 1024)
299 #define CFS_NUM_CACHEPAGES              cfs_num_physpages
300
301 #else
302
303 #define unlink _unlink 
304 #define close  _close
305 #define open   _open
306 #define fdopen _fdopen
307 #define strdup _strdup
308 #define fileno _fileno
309 #define isattry _isattry
310 #define stat    _stat
311
312 #endif  /* !__KERNEL__ */
313
314 int cfs_error_code(NTSTATUS);
315
316 static inline int vsnprintf(char *buf, size_t cnt,
317                             const char *fmt, va_list va)
318 {
319     int rc;
320
321 #ifdef TRUE /* using msvcrt from windkk 3790 */
322     rc = _vsnprintf(buf, cnt, fmt, va);
323 #else
324     rc = _vsnprintf_s(buf, cnt, cnt, fmt, va);
325 #endif
326     if (rc == -1)
327         return cnt;
328     return rc;
329 }
330
331 static inline int snprintf(char *buf, size_t cnt, 
332                            const char *fmt, ...)
333 {
334     int         rc;
335     va_list     va;
336
337     va_start(va, fmt);
338     rc = vsnprintf(buf, cnt, fmt, va);
339     va_end(va);
340     return rc;
341 }
342
343 #endif