GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / staging / lustre / lustre / include / lustre_lmv.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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING 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  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, Intel Corporation.
24  */
25 /*
26  * lustre/include/lustre_lmv.h
27  *
28  * Lustre LMV structures and functions.
29  *
30  * Author: Di Wang <di.wang@intel.com>
31  */
32
33 #ifndef _LUSTRE_LMV_H
34 #define _LUSTRE_LMV_H
35 #include "lustre/lustre_idl.h"
36
37 struct lmv_oinfo {
38         struct lu_fid   lmo_fid;
39         u32             lmo_mds;
40         struct inode    *lmo_root;
41 };
42
43 struct lmv_stripe_md {
44         __u32   lsm_md_magic;
45         __u32   lsm_md_stripe_count;
46         __u32   lsm_md_master_mdt_index;
47         __u32   lsm_md_hash_type;
48         __u32   lsm_md_layout_version;
49         __u32   lsm_md_default_count;
50         __u32   lsm_md_default_index;
51         char    lsm_md_pool_name[LOV_MAXPOOLNAME + 1];
52         struct lmv_oinfo lsm_md_oinfo[0];
53 };
54
55 static inline bool
56 lsm_md_eq(const struct lmv_stripe_md *lsm1, const struct lmv_stripe_md *lsm2)
57 {
58         __u32 idx;
59
60         if (lsm1->lsm_md_magic != lsm2->lsm_md_magic ||
61             lsm1->lsm_md_stripe_count != lsm2->lsm_md_stripe_count ||
62             lsm1->lsm_md_master_mdt_index != lsm2->lsm_md_master_mdt_index ||
63             lsm1->lsm_md_hash_type != lsm2->lsm_md_hash_type ||
64             lsm1->lsm_md_layout_version != lsm2->lsm_md_layout_version ||
65             !strcmp(lsm1->lsm_md_pool_name, lsm2->lsm_md_pool_name))
66                 return false;
67
68         for (idx = 0; idx < lsm1->lsm_md_stripe_count; idx++) {
69                 if (!lu_fid_eq(&lsm1->lsm_md_oinfo[idx].lmo_fid,
70                                &lsm2->lsm_md_oinfo[idx].lmo_fid))
71                         return false;
72         }
73
74         return true;
75 }
76
77 union lmv_mds_md;
78
79 int lmv_unpack_md(struct obd_export *exp, struct lmv_stripe_md **lsmp,
80                   const union lmv_mds_md *lmm, int stripe_count);
81
82 static inline int lmv_alloc_memmd(struct lmv_stripe_md **lsmp, int stripe_count)
83 {
84         return lmv_unpack_md(NULL, lsmp, NULL, stripe_count);
85 }
86
87 static inline void lmv_free_memmd(struct lmv_stripe_md *lsm)
88 {
89         lmv_unpack_md(NULL, &lsm, NULL, 0);
90 }
91
92 static inline void lmv1_le_to_cpu(struct lmv_mds_md_v1 *lmv_dst,
93                                   const struct lmv_mds_md_v1 *lmv_src)
94 {
95         __u32 i;
96
97         lmv_dst->lmv_magic = le32_to_cpu(lmv_src->lmv_magic);
98         lmv_dst->lmv_stripe_count = le32_to_cpu(lmv_src->lmv_stripe_count);
99         lmv_dst->lmv_master_mdt_index =
100                 le32_to_cpu(lmv_src->lmv_master_mdt_index);
101         lmv_dst->lmv_hash_type = le32_to_cpu(lmv_src->lmv_hash_type);
102         lmv_dst->lmv_layout_version = le32_to_cpu(lmv_src->lmv_layout_version);
103
104         for (i = 0; i < lmv_src->lmv_stripe_count; i++)
105                 fid_le_to_cpu(&lmv_dst->lmv_stripe_fids[i],
106                               &lmv_src->lmv_stripe_fids[i]);
107 }
108
109 static inline void lmv_le_to_cpu(union lmv_mds_md *lmv_dst,
110                                  const union lmv_mds_md *lmv_src)
111 {
112         switch (le32_to_cpu(lmv_src->lmv_magic)) {
113         case LMV_MAGIC_V1:
114                 lmv1_le_to_cpu(&lmv_dst->lmv_md_v1, &lmv_src->lmv_md_v1);
115                 break;
116         default:
117                 break;
118         }
119 }
120
121 /* This hash is only for testing purpose */
122 static inline unsigned int
123 lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
124 {
125         const unsigned char *p = (const unsigned char *)name;
126         unsigned int c = 0;
127
128         while (--namelen >= 0)
129                 c += p[namelen];
130
131         c = c % count;
132
133         return c;
134 }
135
136 static inline unsigned int
137 lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
138 {
139         __u64 hash;
140
141         hash = lustre_hash_fnv_1a_64(name, namelen);
142
143         return do_div(hash, count);
144 }
145
146 static inline int lmv_name_to_stripe_index(__u32 lmv_hash_type,
147                                            unsigned int stripe_count,
148                                            const char *name, int namelen)
149 {
150         __u32 hash_type = lmv_hash_type & LMV_HASH_TYPE_MASK;
151         int idx;
152
153         LASSERT(namelen > 0);
154         if (stripe_count <= 1)
155                 return 0;
156
157         /* for migrating object, always start from 0 stripe */
158         if (lmv_hash_type & LMV_HASH_FLAG_MIGRATION)
159                 return 0;
160
161         switch (hash_type) {
162         case LMV_HASH_TYPE_ALL_CHARS:
163                 idx = lmv_hash_all_chars(stripe_count, name, namelen);
164                 break;
165         case LMV_HASH_TYPE_FNV_1A_64:
166                 idx = lmv_hash_fnv1a(stripe_count, name, namelen);
167                 break;
168         default:
169                 idx = -EBADFD;
170                 break;
171         }
172         CDEBUG(D_INFO, "name %.*s hash_type %d idx %d\n", namelen, name,
173                hash_type, idx);
174
175         return idx;
176 }
177
178 static inline bool lmv_is_known_hash_type(__u32 type)
179 {
180         return (type & LMV_HASH_TYPE_MASK) == LMV_HASH_TYPE_FNV_1A_64 ||
181                (type & LMV_HASH_TYPE_MASK) == LMV_HASH_TYPE_ALL_CHARS;
182 }
183
184 #endif