4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/winnt/portals_utils.h
38 * Basic library routines.
41 #ifndef __LIBCFS_WINNT_PORTALS_UTILS_H__
42 #define __LIBCFS_WINNT_PORTALS_UTILS_H__
44 #ifndef cfs_is_flag_set
45 #define cfs_is_flag_set(x,f) (((x)&(f))==(f))
49 #define cfs_set_flag(x,f) ((x) |= (f))
52 #ifndef cfs_clear_flag
53 #define cfs_clear_flag(x,f) ((x) &= ~(f))
56 static inline __u32 do_div64(__u64 * n, __u64 b)
65 #define do_div(n, b) do_div64(&(n), (__u64)b)
69 #include <libcfs/winnt/winnt-types.h>
71 char * strsep(char **s, const char *ct);
72 char * ul2dstr(ulong_ptr_t address, char *buf, int len);
74 #define simple_strtol(a1, a2, a3) strtol(a1, a2, a3)
75 #define simple_strtoll(a1, a2, a3) (__s64)strtoull(a1, a2, a3)
76 #define simple_strtoull(a1, a2, a3) strtoull(a1, a2, a3)
78 unsigned long simple_strtoul(const char *cp,char **endp, unsigned int base);
80 static inline int set_bit(int nr, void * addr)
82 (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
83 return *((int *) addr);
86 static inline int test_bit(int nr, void * addr)
88 return (int)(((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0);
91 static inline int clear_bit(int nr, void * addr)
93 (((volatile ULONG *) addr)[nr >> 5]) &= (~(1UL << (nr & 31)));
94 return *((int *) addr);
97 static inline int test_and_set_bit(int nr, volatile void *addr)
101 volatile unsigned char *ADDR = addr;
104 mask = 1 << (nr & 0x07);
105 rc = ((mask & *ADDR) != 0);
111 #define ext2_set_bit(nr, addr) (set_bit(nr, addr), 0)
112 #define ext2_clear_bit(nr, addr) (clear_bit(nr, addr), 0)
113 #define ext2_test_bit(nr, addr) test_bit(nr, addr)
115 static inline int ffs(int x)
144 static inline unsigned long __cfs_ffs(unsigned long word)
148 #if BITS_PER_LONG == 64
149 if ((word & 0xffffffff) == 0) {
154 if ((word & 0xffff) == 0) {
158 if ((word & 0xff) == 0) {
162 if ((word & 0xf) == 0) {
166 if ((word & 0x3) == 0) {
170 if ((word & 0x1) == 0)
176 * fls - find last (most-significant) bit set
177 * @x: the word to search
179 * This is defined the same way as ffs.
180 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
189 if (!(x & 0xffff0000u)) {
193 if (!(x & 0xff000000u)) {
197 if (!(x & 0xf0000000u)) {
201 if (!(x & 0xc0000000u)) {
205 if (!(x & 0x80000000u)) {
212 static inline unsigned find_first_bit(const unsigned long *addr,
218 unsigned long val = *addr++;
220 return __cfs_ffs(val) + x;
221 x += (sizeof(*addr)<<3);
226 static inline void read_random(char *buf, int len)
228 ULONG Seed = (ULONG)(ULONG_PTR) buf;
229 Seed = RtlRandom(&Seed);
231 if (len > sizeof(ULONG)) {
232 memcpy(buf, &Seed, sizeof(ULONG));
233 len -= sizeof(ULONG);
234 buf += sizeof(ULONG);
236 memcpy(buf, &Seed, len);
243 #define cfs_get_random_bytes_prim(buf, len) read_random(buf, len)
245 /* do NOT use function or expression as parameters ... */
248 #define min_t(type,x,y) (type)(x) < (type)(y) ? (x): (y)
252 #define max_t(type,x,y) (type)(x) < (type)(y) ? (y): (x)
256 #define NIPQUAD(addr) \
257 ((unsigned char *)&addr)[0], \
258 ((unsigned char *)&addr)[1], \
259 ((unsigned char *)&addr)[2], \
260 ((unsigned char *)&addr)[3]
262 #define HIPQUAD(addr) \
263 ((unsigned char *)&addr)[3], \
264 ((unsigned char *)&addr)[2], \
265 ((unsigned char *)&addr)[1], \
266 ((unsigned char *)&addr)[0]
268 static int cfs_copy_from_user(void *to, void *from, int c)
274 static int cfs_copy_to_user(void *to, const void *from, int c)
281 clear_user(void __user *to, unsigned long n)
287 #define put_user(x, ptr) \
294 #define get_user(x,ptr) \
300 #define cfs_num_physpages (64 * 1024)
301 #define CFS_NUM_CACHEPAGES cfs_num_physpages
305 #define unlink _unlink
308 #define fdopen _fdopen
309 #define strdup _strdup
310 #define fileno _fileno
311 #define isattry _isattry
314 #endif /* !__KERNEL__ */
316 int cfs_error_code(NTSTATUS);
318 static inline int vsnprintf(char *buf, size_t cnt,
319 const char *fmt, va_list va)
323 #ifdef TRUE /* using msvcrt from windkk 3790 */
324 rc = _vsnprintf(buf, cnt, fmt, va);
326 rc = _vsnprintf_s(buf, cnt, cnt, fmt, va);
333 static inline int snprintf(char *buf, size_t cnt,
334 const char *fmt, ...)
340 rc = vsnprintf(buf, cnt, fmt, va);