1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 6 * Copyright (c) 2014-2015 François Tigeot 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef _LINUXKPI_LINUX_KERNEL_H_ 31 #define _LINUXKPI_LINUX_KERNEL_H_ 32 33 #include <sys/types.h> 34 #include <sys/systm.h> 35 #include <sys/param.h> 36 #include <sys/libkern.h> 37 #include <sys/stat.h> 38 #include <sys/smp.h> 39 #include <sys/stddef.h> 40 #include <sys/syslog.h> 41 #include <sys/time.h> 42 43 #include <linux/bitops.h> 44 #include <linux/compiler.h> 45 #include <linux/stringify.h> 46 #include <linux/errno.h> 47 #include <linux/sched.h> 48 #include <linux/types.h> 49 #include <linux/typecheck.h> 50 #include <linux/jiffies.h> 51 #include <linux/log2.h> 52 #include <linux/kconfig.h> 53 54 #include <asm/byteorder.h> 55 #include <asm/cpufeature.h> 56 #include <asm/processor.h> 57 #include <asm/uaccess.h> 58 59 #include <linux/stdarg.h> 60 61 #define KERN_CONT "" 62 #define KERN_EMERG "<0>" 63 #define KERN_ALERT "<1>" 64 #define KERN_CRIT "<2>" 65 #define KERN_ERR "<3>" 66 #define KERN_WARNING "<4>" 67 #define KERN_NOTICE "<5>" 68 #define KERN_INFO "<6>" 69 #define KERN_DEBUG "<7>" 70 71 #define U8_MAX ((u8)~0U) 72 #define S8_MAX ((s8)(U8_MAX >> 1)) 73 #define S8_MIN ((s8)(-S8_MAX - 1)) 74 #define U16_MAX ((u16)~0U) 75 #define S16_MAX ((s16)(U16_MAX >> 1)) 76 #define S16_MIN ((s16)(-S16_MAX - 1)) 77 #define U32_MAX ((u32)~0U) 78 #define S32_MAX ((s32)(U32_MAX >> 1)) 79 #define S32_MIN ((s32)(-S32_MAX - 1)) 80 #define U64_MAX ((u64)~0ULL) 81 #define S64_MAX ((s64)(U64_MAX >> 1)) 82 #define S64_MIN ((s64)(-S64_MAX - 1)) 83 84 #define S8_C(x) x 85 #define U8_C(x) x ## U 86 #define S16_C(x) x 87 #define U16_C(x) x ## U 88 #define S32_C(x) x 89 #define U32_C(x) x ## U 90 #define S64_C(x) x ## LL 91 #define U64_C(x) x ## ULL 92 93 /* 94 * BUILD_BUG_ON() can happen inside functions where _Static_assert() does not 95 * seem to work. Use old-schoold-ish CTASSERT from before commit 96 * a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not 97 * end up with an unused typedef or variable. The compiler should optimise 98 * it away entirely. 99 */ 100 #define _O_CTASSERT(x) _O__CTASSERT(x, __LINE__) 101 #define _O__CTASSERT(x, y) _O___CTASSERT(x, y) 102 #define _O___CTASSERT(x, y) while (0) { \ 103 typedef char __unused __assert_line_ ## y[(x) ? 1 : -1]; \ 104 __assert_line_ ## y _x; \ 105 _x[0] = '\0'; \ 106 } 107 108 #define BUILD_BUG() do { CTASSERT(0); } while (0) 109 #define BUILD_BUG_ON(x) do { _O_CTASSERT(!(x)) } while (0) 110 #define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x) 111 #define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x)) 112 #define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); } 113 #define BUILD_BUG_ON_ZERO(x) ((int)sizeof(struct { int:-((x) != 0); })) 114 115 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 116 #define BUG_ON(cond) do { \ 117 if (cond) { \ 118 panic("BUG ON %s failed at %s:%d", \ 119 __stringify(cond), __FILE__, __LINE__); \ 120 } \ 121 } while (0) 122 123 extern int linuxkpi_warn_dump_stack; 124 #define WARN_ON(cond) ({ \ 125 bool __ret = (cond); \ 126 if (__ret) { \ 127 printf("WARNING %s failed at %s:%d\n", \ 128 __stringify(cond), __FILE__, __LINE__); \ 129 if (linuxkpi_warn_dump_stack) \ 130 linux_dump_stack(); \ 131 } \ 132 unlikely(__ret); \ 133 }) 134 135 #define WARN_ON_SMP(cond) WARN_ON(cond) 136 137 #define WARN_ON_ONCE(cond) ({ \ 138 static bool __warn_on_once; \ 139 bool __ret = (cond); \ 140 if (__ret && !__warn_on_once) { \ 141 __warn_on_once = 1; \ 142 printf("WARNING %s failed at %s:%d\n", \ 143 __stringify(cond), __FILE__, __LINE__); \ 144 if (linuxkpi_warn_dump_stack) \ 145 linux_dump_stack(); \ 146 } \ 147 unlikely(__ret); \ 148 }) 149 150 #define oops_in_progress SCHEDULER_STOPPED() 151 152 #undef ALIGN 153 #define ALIGN(x, y) roundup2((x), (y)) 154 #define ALIGN_DOWN(x, y) rounddown2(x, y) 155 #undef PTR_ALIGN 156 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 157 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 158 #define DIV_ROUND_UP(x, n) howmany(x, n) 159 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 160 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 161 #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) 162 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 163 164 #define printk(...) printf(__VA_ARGS__) 165 #define vprintk(f, a) vprintf(f, a) 166 167 #define asm __asm 168 169 extern void linux_dump_stack(void); 170 #define dump_stack() linux_dump_stack() 171 172 struct va_format { 173 const char *fmt; 174 va_list *va; 175 }; 176 177 static inline int 178 vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 179 { 180 ssize_t ssize = size; 181 int i; 182 183 i = vsnprintf(buf, size, fmt, args); 184 185 return ((i >= ssize) ? (ssize - 1) : i); 186 } 187 188 static inline int 189 scnprintf(char *buf, size_t size, const char *fmt, ...) 190 { 191 va_list args; 192 int i; 193 194 va_start(args, fmt); 195 i = vscnprintf(buf, size, fmt, args); 196 va_end(args); 197 198 return (i); 199 } 200 201 /* 202 * The "pr_debug()" and "pr_devel()" macros should produce zero code 203 * unless DEBUG is defined: 204 */ 205 #ifdef DEBUG 206 extern int linuxkpi_debug; 207 #define pr_debug(fmt, ...) \ 208 do { \ 209 if (linuxkpi_debug) \ 210 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 211 } while (0) 212 #define pr_devel(fmt, ...) \ 213 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 214 #else 215 #define pr_debug(fmt, ...) \ 216 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 217 #define pr_devel(fmt, ...) \ 218 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 219 #endif 220 221 #ifndef pr_fmt 222 #define pr_fmt(fmt) fmt 223 #endif 224 225 /* 226 * Print a one-time message (analogous to WARN_ONCE() et al): 227 */ 228 #define printk_once(...) do { \ 229 static bool __print_once; \ 230 \ 231 if (!__print_once) { \ 232 __print_once = true; \ 233 printk(__VA_ARGS__); \ 234 } \ 235 } while (0) 236 237 /* 238 * Log a one-time message (analogous to WARN_ONCE() et al): 239 */ 240 #define log_once(level,...) do { \ 241 static bool __log_once; \ 242 \ 243 if (unlikely(!__log_once)) { \ 244 __log_once = true; \ 245 log(level, __VA_ARGS__); \ 246 } \ 247 } while (0) 248 249 #define pr_emerg(fmt, ...) \ 250 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 251 #define pr_alert(fmt, ...) \ 252 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 253 #define pr_crit(fmt, ...) \ 254 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 255 #define pr_err(fmt, ...) \ 256 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 257 #define pr_err_once(fmt, ...) \ 258 log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 259 #define pr_warning(fmt, ...) \ 260 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 261 #define pr_warn(...) \ 262 pr_warning(__VA_ARGS__) 263 #define pr_warn_once(fmt, ...) \ 264 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 265 #define pr_notice(fmt, ...) \ 266 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 267 #define pr_info(fmt, ...) \ 268 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 269 #define pr_info_once(fmt, ...) \ 270 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 271 #define pr_cont(fmt, ...) \ 272 printk(KERN_CONT fmt, ##__VA_ARGS__) 273 #define pr_warn_ratelimited(...) do { \ 274 static linux_ratelimit_t __ratelimited; \ 275 if (linux_ratelimited(&__ratelimited)) \ 276 pr_warning(__VA_ARGS__); \ 277 } while (0) 278 279 #ifndef WARN 280 #define WARN(condition, ...) ({ \ 281 bool __ret_warn_on = (condition); \ 282 if (unlikely(__ret_warn_on)) \ 283 pr_warning(__VA_ARGS__); \ 284 unlikely(__ret_warn_on); \ 285 }) 286 #endif 287 288 #ifndef WARN_ONCE 289 #define WARN_ONCE(condition, ...) ({ \ 290 bool __ret_warn_on = (condition); \ 291 if (unlikely(__ret_warn_on)) \ 292 pr_warn_once(__VA_ARGS__); \ 293 unlikely(__ret_warn_on); \ 294 }) 295 #endif 296 297 #define container_of(ptr, type, member) \ 298 ({ \ 299 const __typeof(((type *)0)->member) *__p = (ptr); \ 300 (type *)((uintptr_t)__p - offsetof(type, member)); \ 301 }) 302 303 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 304 305 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 306 307 #define _RET_IP_ __builtin_return_address(0) 308 309 static inline unsigned long long 310 simple_strtoull(const char *cp, char **endp, unsigned int base) 311 { 312 return (strtouq(cp, endp, base)); 313 } 314 315 static inline long long 316 simple_strtoll(const char *cp, char **endp, unsigned int base) 317 { 318 return (strtoq(cp, endp, base)); 319 } 320 321 static inline unsigned long 322 simple_strtoul(const char *cp, char **endp, unsigned int base) 323 { 324 return (strtoul(cp, endp, base)); 325 } 326 327 static inline long 328 simple_strtol(const char *cp, char **endp, unsigned int base) 329 { 330 return (strtol(cp, endp, base)); 331 } 332 333 static inline int 334 kstrtoul(const char *cp, unsigned int base, unsigned long *res) 335 { 336 char *end; 337 338 *res = strtoul(cp, &end, base); 339 340 /* skip newline character, if any */ 341 if (*end == '\n') 342 end++; 343 if (*cp == 0 || *end != 0) 344 return (-EINVAL); 345 return (0); 346 } 347 348 static inline int 349 kstrtol(const char *cp, unsigned int base, long *res) 350 { 351 char *end; 352 353 *res = strtol(cp, &end, base); 354 355 /* skip newline character, if any */ 356 if (*end == '\n') 357 end++; 358 if (*cp == 0 || *end != 0) 359 return (-EINVAL); 360 return (0); 361 } 362 363 static inline int 364 kstrtoint(const char *cp, unsigned int base, int *res) 365 { 366 char *end; 367 long temp; 368 369 *res = temp = strtol(cp, &end, base); 370 371 /* skip newline character, if any */ 372 if (*end == '\n') 373 end++; 374 if (*cp == 0 || *end != 0) 375 return (-EINVAL); 376 if (temp != (int)temp) 377 return (-ERANGE); 378 return (0); 379 } 380 381 static inline int 382 kstrtouint(const char *cp, unsigned int base, unsigned int *res) 383 { 384 char *end; 385 unsigned long temp; 386 387 *res = temp = strtoul(cp, &end, base); 388 389 /* skip newline character, if any */ 390 if (*end == '\n') 391 end++; 392 if (*cp == 0 || *end != 0) 393 return (-EINVAL); 394 if (temp != (unsigned int)temp) 395 return (-ERANGE); 396 return (0); 397 } 398 399 static inline int 400 kstrtou8(const char *cp, unsigned int base, u8 *res) 401 { 402 char *end; 403 unsigned long temp; 404 405 *res = temp = strtoul(cp, &end, base); 406 407 /* skip newline character, if any */ 408 if (*end == '\n') 409 end++; 410 if (*cp == 0 || *end != 0) 411 return (-EINVAL); 412 if (temp != (u8)temp) 413 return (-ERANGE); 414 return (0); 415 } 416 417 static inline int 418 kstrtou16(const char *cp, unsigned int base, u16 *res) 419 { 420 char *end; 421 unsigned long temp; 422 423 *res = temp = strtoul(cp, &end, base); 424 425 /* skip newline character, if any */ 426 if (*end == '\n') 427 end++; 428 if (*cp == 0 || *end != 0) 429 return (-EINVAL); 430 if (temp != (u16)temp) 431 return (-ERANGE); 432 return (0); 433 } 434 435 static inline int 436 kstrtou32(const char *cp, unsigned int base, u32 *res) 437 { 438 439 return (kstrtouint(cp, base, res)); 440 } 441 442 static inline int 443 kstrtou64(const char *cp, unsigned int base, u64 *res) 444 { 445 char *end; 446 447 *res = strtouq(cp, &end, base); 448 449 /* skip newline character, if any */ 450 if (*end == '\n') 451 end++; 452 if (*cp == 0 || *end != 0) 453 return (-EINVAL); 454 return (0); 455 } 456 457 static inline int 458 kstrtoull(const char *cp, unsigned int base, unsigned long long *res) 459 { 460 return (kstrtou64(cp, base, (u64 *)res)); 461 } 462 463 static inline int 464 kstrtobool(const char *s, bool *res) 465 { 466 int len; 467 468 if (s == NULL || (len = strlen(s)) == 0 || res == NULL) 469 return (-EINVAL); 470 471 /* skip newline character, if any */ 472 if (s[len - 1] == '\n') 473 len--; 474 475 if (len == 1 && strchr("yY1", s[0]) != NULL) 476 *res = true; 477 else if (len == 1 && strchr("nN0", s[0]) != NULL) 478 *res = false; 479 else if (strncasecmp("on", s, len) == 0) 480 *res = true; 481 else if (strncasecmp("off", s, len) == 0) 482 *res = false; 483 else 484 return (-EINVAL); 485 486 return (0); 487 } 488 489 static inline int 490 kstrtobool_from_user(const char __user *s, size_t count, bool *res) 491 { 492 char buf[8] = {}; 493 494 if (count > (sizeof(buf) - 1)) 495 count = (sizeof(buf) - 1); 496 497 if (copy_from_user(buf, s, count)) 498 return (-EFAULT); 499 500 return (kstrtobool(buf, res)); 501 } 502 503 static inline int 504 kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, 505 int *p) 506 { 507 char buf[36] = {}; 508 509 if (count > (sizeof(buf) - 1)) 510 count = (sizeof(buf) - 1); 511 512 if (copy_from_user(buf, s, count)) 513 return (-EFAULT); 514 515 return (kstrtoint(buf, base, p)); 516 } 517 518 static inline int 519 kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, 520 unsigned int *p) 521 { 522 char buf[36] = {}; 523 524 if (count > (sizeof(buf) - 1)) 525 count = (sizeof(buf) - 1); 526 527 if (copy_from_user(buf, s, count)) 528 return (-EFAULT); 529 530 return (kstrtouint(buf, base, p)); 531 } 532 533 static inline int 534 kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, 535 unsigned int *p) 536 { 537 538 return (kstrtouint_from_user(s, count, base, p)); 539 } 540 541 static inline int 542 kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, 543 u8 *p) 544 { 545 char buf[8] = {}; 546 547 if (count > (sizeof(buf) - 1)) 548 count = (sizeof(buf) - 1); 549 550 if (copy_from_user(buf, s, count)) 551 return (-EFAULT); 552 553 return (kstrtou8(buf, base, p)); 554 } 555 556 #define min(x, y) ((x) < (y) ? (x) : (y)) 557 #define max(x, y) ((x) > (y) ? (x) : (y)) 558 559 #define min3(a, b, c) min(a, min(b,c)) 560 #define max3(a, b, c) max(a, max(b,c)) 561 562 #define min_t(type, x, y) ({ \ 563 type __min1 = (x); \ 564 type __min2 = (y); \ 565 __min1 < __min2 ? __min1 : __min2; }) 566 567 #define max_t(type, x, y) ({ \ 568 type __max1 = (x); \ 569 type __max2 = (y); \ 570 __max1 > __max2 ? __max1 : __max2; }) 571 572 #define offsetofend(t, m) \ 573 (offsetof(t, m) + sizeof((((t *)0)->m))) 574 575 #define typeof_member(s, e) typeof(((s *)0)->e) 576 577 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 578 #define clamp(x, lo, hi) min( max(x,lo), hi) 579 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 580 581 /* 582 * This looks more complex than it should be. But we need to 583 * get the type for the ~ right in round_down (it needs to be 584 * as wide as the result!), and we want to evaluate the macro 585 * arguments just once each. 586 */ 587 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 588 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 589 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 590 591 #define smp_processor_id() PCPU_GET(cpuid) 592 #define num_possible_cpus() mp_ncpus 593 #define num_online_cpus() mp_ncpus 594 595 #if defined(__i386__) || defined(__amd64__) 596 extern bool linux_cpu_has_clflush; 597 #define cpu_has_clflush linux_cpu_has_clflush 598 #endif 599 600 /* Swap values of a and b */ 601 #define swap(a, b) do { \ 602 typeof(a) _swap_tmp = a; \ 603 a = b; \ 604 b = _swap_tmp; \ 605 } while (0) 606 607 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 608 609 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 610 __typeof(divisor) __d = (divisor); \ 611 unsigned long long __ret = (x) + (__d) / 2; \ 612 __ret /= __d; \ 613 __ret; \ 614 }) 615 616 static inline uintmax_t 617 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 618 { 619 uintmax_t q = (x / divisor); 620 uintmax_t r = (x % divisor); 621 622 return ((q * multiplier) + ((r * multiplier) / divisor)); 623 } 624 625 typedef struct linux_ratelimit { 626 struct timeval lasttime; 627 int counter; 628 } linux_ratelimit_t; 629 630 static inline bool 631 linux_ratelimited(linux_ratelimit_t *rl) 632 { 633 return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 634 } 635 636 #define struct_size(ptr, field, num) ({ \ 637 const size_t __size = offsetof(__typeof(*(ptr)), field); \ 638 const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ 639 ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ 640 }) 641 642 #define __is_constexpr(x) \ 643 __builtin_constant_p(x) 644 645 /* 646 * The is_signed() macro below returns true if the passed data type is 647 * signed. Else false is returned. 648 */ 649 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 650 651 /* 652 * The type_max() macro below returns the maxium positive value the 653 * passed data type can hold. 654 */ 655 #define type_max(datatype) ( \ 656 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ 657 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ 658 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ 659 (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ 660 ) 661 662 /* 663 * The type_min() macro below returns the minimum value the passed 664 * data type can hold. For unsigned types the minimum value is always 665 * zero. For signed types it may vary. 666 */ 667 #define type_min(datatype) ( \ 668 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ 669 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ 670 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ 671 (is_signed(datatype) ? INT8_MIN : 0) \ 672 ) 673 674 #define TAINT_WARN 0 675 #define test_taint(x) (0) 676 #define add_taint(x,y) do { \ 677 } while (0) 678 679 static inline int 680 _h2b(const char c) 681 { 682 683 if (c >= '0' && c <= '9') 684 return (c - '0'); 685 if (c >= 'a' && c <= 'f') 686 return (10 + c - 'a'); 687 if (c >= 'A' && c <= 'F') 688 return (10 + c - 'A'); 689 return (-EINVAL); 690 } 691 692 static inline int 693 hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) 694 { 695 int hi4, lo4; 696 697 while (binlen > 0) { 698 hi4 = _h2b(*hexsrc++); 699 lo4 = _h2b(*hexsrc++); 700 if (hi4 < 0 || lo4 < 0) 701 return (-EINVAL); 702 703 *bindst++ = (hi4 << 4) | lo4; 704 binlen--; 705 } 706 707 return (0); 708 } 709 710 static inline bool 711 mac_pton(const char *macin, uint8_t *macout) 712 { 713 const char *s, *d; 714 uint8_t mac[6], hx, lx;; 715 int i; 716 717 if (strlen(macin) < (3 * 6 - 1)) 718 return (false); 719 720 i = 0; 721 s = macin; 722 do { 723 /* Should we also support '-'-delimiters? */ 724 d = strchrnul(s, ':'); 725 hx = lx = 0; 726 while (s < d) { 727 /* Fail on abc:123:xxx:... */ 728 if ((d - s) > 2) 729 return (false); 730 /* We do support non-well-formed strings: 3:45:6:... */ 731 if ((d - s) > 1) { 732 hx = _h2b(*s); 733 if (hx < 0) 734 return (false); 735 s++; 736 } 737 lx = _h2b(*s); 738 if (lx < 0) 739 return (false); 740 s++; 741 } 742 mac[i] = (hx << 4) | lx; 743 i++; 744 if (i >= 6) 745 return (false); 746 } while (d != NULL && *d != '\0'); 747 748 memcpy(macout, mac, 6); 749 return (true); 750 } 751 752 #define DECLARE_FLEX_ARRAY(_t, _n) \ 753 struct { struct { } __dummy_ ## _n; _t _n[0]; } 754 755 #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ 756