Whamcloud - gitweb
b=21259 "lfs check" is only allowed for root.
[fs/lustre-release.git] / lustre / obdclass / mea.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38 #include <obd_class.h>
39 #ifdef __KERNEL__
40 #include <linux/kmod.h>   /* for request_module() */
41 #include <linux/module.h>
42 #include <linux/random.h>
43 #include <linux/slab.h>
44 #include <linux/pagemap.h>
45 #else
46 #include <liblustre.h>
47 #include <obd.h>
48 #endif
49 #include <lprocfs_status.h>
50 #include <lustre/lustre_idl.h>
51
52 #ifdef __KERNEL__
53
54 #ifdef HAVE_SERVER_SUPPORT
55
56 #ifdef HAVE_EXT4_LDISKFS
57 #include <ldiskfs/ldiskfs_jbd2.h>
58 #include <ldiskfs/ldiskfs.h>
59 #else
60 #include <linux/jbd.h>
61 #include <linux/ldiskfs_fs.h>
62 #endif
63
64 #endif
65 #endif
66 static int mea_last_char_hash(int count, char *name, int namelen)
67 {
68         unsigned int c;
69
70         c = name[namelen - 1];
71         if (c == 0)
72                 CWARN("looks like wrong len is passed\n");
73         c = c % count;
74         return c;
75 }
76
77 static int mea_all_chars_hash(int count, char *name, int namelen)
78 {
79         unsigned int c = 0;
80
81         while (--namelen >= 0)
82                 c += name[namelen];
83         c = c % count;
84         return c;
85 }
86
87 #ifdef __KERNEL__
88 /* This hash calculate method must be same as the lvar hash method */
89
90 #define LVAR_HASH_SANDWICH  (0)
91 #define LVAR_HASH_PREFIX    (0)
92
93 static __u32 hash_build0(const char *name, int namelen)
94 {
95         __u32 result;
96
97         if (namelen == 0)
98                 return 0;
99         if (strncmp(name, ".", 1) == 0 && namelen == 1)
100                 return 1;
101         if (strncmp(name, "..", 2) == 0 && namelen == 2)
102                 return 2;
103
104         if (LVAR_HASH_PREFIX) {
105                 result = 0;
106                 strncpy((void *)&result,
107                         name, min(namelen, (int)sizeof result));
108         } else {
109                 struct ldiskfs_dx_hash_info hinfo;
110
111                 hinfo.hash_version = LDISKFS_DX_HASH_TEA;
112                 hinfo.seed = 0;
113                 ldiskfsfs_dirhash(name, namelen, &hinfo);
114                 result = hinfo.hash;
115                 if (LVAR_HASH_SANDWICH) {
116                         __u32 result2;
117
118                         hinfo.hash_version = LDISKFS_DX_HASH_TEA;
119                         hinfo.seed = 0;
120                         ldiskfsfs_dirhash(name, namelen, &hinfo);
121                         result2 = hinfo.hash;
122                         result = (0xfc000000 & result2) | (0x03ffffff & result);
123                 }
124         }
125
126         return result;
127 }
128
129 enum {
130         HASH_GRAY_AREA = 1024
131 };
132
133 static __u32 hash_build(const char *name, int namelen)
134 {
135         __u32 hash;
136
137         hash = (hash_build0(name, namelen) << 1) & MAX_HASH_SIZE_32;
138         if (hash > MAX_HASH_SIZE_32 - HASH_GRAY_AREA)
139                 hash &= HASH_GRAY_AREA - 1;
140         return hash;
141 }
142
143 static int mea_hash_segment(int count, const char *name, int namelen)
144 {
145         __u32 hash;
146
147         LASSERT(IS_PO2(MAX_HASH_SIZE_32 + 1));
148
149         hash = hash_build(name, namelen) / (MAX_HASH_SIZE_32 / count);
150         LASSERTF(hash < count, "hash %x count %d \n", hash, count);
151
152         return hash;
153 }
154 #else
155 static int mea_hash_segment(int count, char *name, int namelen)
156 {
157         return 0;
158 }
159 #endif
160 int raw_name2idx(int hashtype, int count, const char *name, int namelen)
161 {
162         unsigned int c = 0;
163
164         LASSERT(namelen > 0);
165         if (count <= 1)
166                 return 0;
167
168         switch (hashtype) {
169                 case MEA_MAGIC_LAST_CHAR:
170                         c = mea_last_char_hash(count, (char *)name, namelen);
171                         break;
172                 case MEA_MAGIC_ALL_CHARS:
173                         c = mea_all_chars_hash(count, (char *)name, namelen);
174                         break;
175                 case MEA_MAGIC_HASH_SEGMENT:
176                         c = mea_hash_segment(count, (char *)name, namelen);
177                         break;
178                 default:
179                         CERROR("Unknown hash type 0x%x\n", hashtype);
180         }
181         
182         LASSERT(c < count);
183         return c;
184 }
185
186 int mea_name2idx(struct lmv_stripe_md *mea, const char *name, int namelen)
187 {
188         unsigned int c;
189
190         LASSERT(mea && mea->mea_count);
191
192         c = raw_name2idx(mea->mea_magic, mea->mea_count, name, namelen);
193
194         LASSERT(c < mea->mea_count);
195         return c;
196 }