Whamcloud - gitweb
LU-10467 ptlrpc: convert final users of LWI_TIMEOUT_INTERVAL
[fs/lustre-release.git] / 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  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, 2016, 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 <uapi/linux/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_migrate_offset;
50         __u32   lsm_md_migrate_hash;
51         __u32   lsm_md_default_count;
52         __u32   lsm_md_default_index;
53         char    lsm_md_pool_name[LOV_MAXPOOLNAME + 1];
54         struct lmv_oinfo lsm_md_oinfo[0];
55 };
56
57 static inline bool lmv_dir_striped(const struct lmv_stripe_md *lsm)
58 {
59         return lsm && lsm->lsm_md_magic == LMV_MAGIC;
60 }
61
62 static inline bool lmv_dir_foreign(const struct lmv_stripe_md *lsm)
63 {
64         return lsm && lsm->lsm_md_magic == LMV_MAGIC_FOREIGN;
65 }
66
67 static inline bool lmv_dir_migrating(const struct lmv_stripe_md *lsm)
68 {
69         return lmv_dir_striped(lsm) &&
70                lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION;
71 }
72
73 static inline bool lmv_dir_bad_hash(const struct lmv_stripe_md *lsm)
74 {
75         if (!lmv_dir_striped(lsm))
76                 return false;
77
78         if (lmv_dir_migrating(lsm) &&
79             lsm->lsm_md_stripe_count - lsm->lsm_md_migrate_offset <= 1)
80                 return false;
81
82         return !lmv_is_known_hash_type(lsm->lsm_md_hash_type);
83 }
84
85 static inline bool
86 lsm_md_eq(const struct lmv_stripe_md *lsm1, const struct lmv_stripe_md *lsm2)
87 {
88         __u32 idx;
89
90         if (lsm1->lsm_md_magic != lsm2->lsm_md_magic ||
91             lsm1->lsm_md_stripe_count != lsm2->lsm_md_stripe_count ||
92             lsm1->lsm_md_master_mdt_index !=
93                                 lsm2->lsm_md_master_mdt_index ||
94             lsm1->lsm_md_hash_type != lsm2->lsm_md_hash_type ||
95             lsm1->lsm_md_layout_version !=
96                                 lsm2->lsm_md_layout_version ||
97             lsm1->lsm_md_migrate_offset !=
98                                 lsm2->lsm_md_migrate_offset ||
99             lsm1->lsm_md_migrate_hash !=
100                                 lsm2->lsm_md_migrate_hash ||
101             strcmp(lsm1->lsm_md_pool_name,
102                       lsm2->lsm_md_pool_name) != 0)
103                 return false;
104
105         if (lmv_dir_striped(lsm1)) {
106                 for (idx = 0; idx < lsm1->lsm_md_stripe_count; idx++) {
107                         if (!lu_fid_eq(&lsm1->lsm_md_oinfo[idx].lmo_fid,
108                                        &lsm2->lsm_md_oinfo[idx].lmo_fid))
109                                 return false;
110                 }
111         }
112
113         return true;
114 }
115
116 static inline void lsm_md_dump(int mask, const struct lmv_stripe_md *lsm)
117 {
118         int i;
119
120         CDEBUG(mask, "magic %#x stripe count %d master mdt %d hash type %#x "
121                 "version %d migrate offset %d migrate hash %#x pool %s\n",
122                 lsm->lsm_md_magic, lsm->lsm_md_stripe_count,
123                 lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
124                 lsm->lsm_md_layout_version, lsm->lsm_md_migrate_offset,
125                 lsm->lsm_md_migrate_hash, lsm->lsm_md_pool_name);
126
127         if (!lmv_dir_striped(lsm))
128                 return;
129
130         for (i = 0; i < lsm->lsm_md_stripe_count; i++)
131                 CDEBUG(mask, "stripe[%d] "DFID"\n",
132                        i, PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
133 }
134
135 union lmv_mds_md;
136
137 void lmv_free_memmd(struct lmv_stripe_md *lsm);
138
139 static inline void lmv1_le_to_cpu(struct lmv_mds_md_v1 *lmv_dst,
140                                   const struct lmv_mds_md_v1 *lmv_src)
141 {
142         __u32 i;
143
144         lmv_dst->lmv_magic = le32_to_cpu(lmv_src->lmv_magic);
145         lmv_dst->lmv_stripe_count = le32_to_cpu(lmv_src->lmv_stripe_count);
146         lmv_dst->lmv_master_mdt_index =
147                                 le32_to_cpu(lmv_src->lmv_master_mdt_index);
148         lmv_dst->lmv_hash_type = le32_to_cpu(lmv_src->lmv_hash_type);
149         lmv_dst->lmv_layout_version = le32_to_cpu(lmv_src->lmv_layout_version);
150         for (i = 0; i < lmv_src->lmv_stripe_count; i++)
151                 fid_le_to_cpu(&lmv_dst->lmv_stripe_fids[i],
152                               &lmv_src->lmv_stripe_fids[i]);
153 }
154
155 static inline void lmv_le_to_cpu(union lmv_mds_md *lmv_dst,
156                                  const union lmv_mds_md *lmv_src)
157 {
158         switch (le32_to_cpu(lmv_src->lmv_magic)) {
159         case LMV_MAGIC_V1:
160                 lmv1_le_to_cpu(&lmv_dst->lmv_md_v1, &lmv_src->lmv_md_v1);
161                 break;
162         default:
163                 break;
164         }
165 }
166
167 /* This hash is only for testing purpose */
168 static inline unsigned int
169 lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
170 {
171         unsigned int c = 0;
172         const unsigned char *p = (const unsigned char *)name;
173
174         while (--namelen >= 0)
175                 c += p[namelen];
176
177         c = c % count;
178
179         return c;
180 }
181
182 static inline unsigned int
183 lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
184 {
185         __u64 hash;
186
187         hash = lustre_hash_fnv_1a_64(name, namelen);
188
189         return do_div(hash, count);
190 }
191
192 static inline int lmv_name_to_stripe_index(__u32 lmv_hash_type,
193                                            unsigned int stripe_count,
194                                            const char *name, int namelen)
195 {
196         int idx;
197
198         LASSERT(namelen > 0);
199
200         if (stripe_count <= 1)
201                 return 0;
202
203         switch (lmv_hash_type & LMV_HASH_TYPE_MASK) {
204         case LMV_HASH_TYPE_ALL_CHARS:
205                 idx = lmv_hash_all_chars(stripe_count, name, namelen);
206                 break;
207         case LMV_HASH_TYPE_FNV_1A_64:
208                 idx = lmv_hash_fnv1a(stripe_count, name, namelen);
209                 break;
210         default:
211                 idx = -EBADFD;
212                 break;
213         }
214
215         CDEBUG(D_INFO, "name %.*s hash_type %#x idx %d/%u\n", namelen, name,
216                lmv_hash_type, idx, stripe_count);
217
218         return idx;
219 }
220
221 static inline bool lmv_magic_supported(__u32 lum_magic)
222 {
223         return lum_magic == LMV_USER_MAGIC ||
224                lum_magic == LMV_USER_MAGIC_SPECIFIC ||
225                lum_magic == LMV_MAGIC_FOREIGN;
226 }
227
228 #endif