Whamcloud - gitweb
LU-5560 obd: reserve connection flag OBD_CONNECT2_FILE_SECCTX
[fs/lustre-release.git] / libcfs / libcfs / prng.c
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) 2007, 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/libcfs/prng.c
35  *
36  * concatenation of following two 16-bit multiply with carry generators
37  * x(n)=a*x(n-1)+carry mod 2^16 and y(n)=b*y(n-1)+carry mod 2^16,
38  * number and carry packed within the same 32 bit integer.
39  * algorithm recommended by Marsaglia
40 */
41
42 #include <linux/random.h>
43 #include <libcfs/libcfs.h>
44
45 /*
46 From: George Marsaglia <geo@stat.fsu.edu>
47 Newsgroups: sci.math
48 Subject: Re: A RANDOM NUMBER GENERATOR FOR C
49 Date: Tue, 30 Sep 1997 05:29:35 -0700
50
51  * You may replace the two constants 36969 and 18000 by any
52  * pair of distinct constants from this list:
53  * 18000 18030 18273 18513 18879 19074 19098 19164 19215 19584
54  * 19599 19950 20088 20508 20544 20664 20814 20970 21153 21243
55  * 21423 21723 21954 22125 22188 22293 22860 22938 22965 22974
56  * 23109 23124 23163 23208 23508 23520 23553 23658 23865 24114
57  * 24219 24660 24699 24864 24948 25023 25308 25443 26004 26088
58  * 26154 26550 26679 26838 27183 27258 27753 27795 27810 27834
59  * 27960 28320 28380 28689 28710 28794 28854 28959 28980 29013
60  * 29379 29889 30135 30345 30459 30714 30903 30963 31059 31083
61  * (or any other 16-bit constants k for which both k*2^16-1
62  * and k*2^15-1 are prime) */
63
64 #define RANDOM_CONST_A 18030
65 #define RANDOM_CONST_B 29013
66
67 static unsigned int seed_x = 521288629;
68 static unsigned int seed_y = 362436069;
69
70 /**
71  * cfs_rand - creates new seeds
72  *
73  * First it creates new seeds from the previous seeds. Then it generates a
74  * new psuedo random number for use.
75  *
76  * Returns a pseudo-random 32-bit integer
77  */
78 unsigned int cfs_rand(void)
79 {
80         seed_x = RANDOM_CONST_A * (seed_x & 65535) + (seed_x >> 16);
81         seed_y = RANDOM_CONST_B * (seed_y & 65535) + (seed_y >> 16);
82
83         return ((seed_x << 16) + (seed_y & 65535));
84 }
85 EXPORT_SYMBOL(cfs_rand);
86
87 /**
88  * cfs_srand - sets the inital seed
89  * @seed1 : (seed_x) should have the most entropy in the low bits of the word
90  * @seed2 : (seed_y) should have the most entropy in the high bits of the word
91  *
92  * Replaces the original seeds with new values. Used to generate a new pseudo
93  * random numbers.
94  */
95 void cfs_srand(unsigned int seed1, unsigned int seed2)
96 {
97         if (seed1)
98                 seed_x = seed1; /* use default seeds if parameter is 0 */
99         if (seed2)
100                 seed_y = seed2;
101 }
102 EXPORT_SYMBOL(cfs_srand);
103
104 /**
105  * cfs_get_random_bytes - generate a bunch of random numbers
106  * @buf : buffer to fill with random numbers
107  * @size: size of passed in buffer
108  *
109  * Fills a buffer with random bytes
110  */
111 void cfs_get_random_bytes(void *buf, int size)
112 {
113         int *p = buf;
114         int rem, tmp;
115
116         LASSERT(size >= 0);
117
118         rem = min((int)((unsigned long)buf & (sizeof(int) - 1)), size);
119         if (rem) {
120                 get_random_bytes(&tmp, sizeof(tmp));
121                 tmp ^= cfs_rand();
122                 memcpy(buf, &tmp, rem);
123                 p = buf + rem;
124                 size -= rem;
125         }
126
127         while (size >= sizeof(int)) {
128                 get_random_bytes(&tmp, sizeof(tmp));
129                 *p = cfs_rand() ^ tmp;
130                 size -= sizeof(int);
131                 p++;
132         }
133         buf = p;
134         if (size) {
135                 get_random_bytes(&tmp, sizeof(tmp));
136                 tmp ^= cfs_rand();
137                 memcpy(buf, &tmp, size);
138         }
139 }
140 EXPORT_SYMBOL(cfs_get_random_bytes);