4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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
23 * Copyright (c) 2014, 2016, Intel Corporation.
26 * lustre/include/lustre_lmv.h
28 * Lustre LMV structures and functions.
30 * Author: Di Wang <di.wang@intel.com>
35 #include <uapi/linux/lustre/lustre_idl.h>
38 struct lu_fid lmo_fid;
40 struct inode *lmo_root;
43 struct lmv_stripe_md {
45 __u32 lsm_md_stripe_count;
46 __u32 lsm_md_master_mdt_index;
47 __u32 lsm_md_hash_type;
48 __u8 lsm_md_max_inherit;
49 __u8 lsm_md_max_inherit_rr;
50 __u32 lsm_md_layout_version;
51 __u32 lsm_md_migrate_offset;
52 __u32 lsm_md_migrate_hash;
53 __u32 lsm_md_default_count;
54 __u32 lsm_md_default_index;
55 char lsm_md_pool_name[LOV_MAXPOOLNAME + 1];
56 struct lmv_oinfo lsm_md_oinfo[0];
59 static inline bool lmv_dir_striped(const struct lmv_stripe_md *lsm)
61 return lsm && lsm->lsm_md_magic == LMV_MAGIC;
64 static inline bool lmv_dir_foreign(const struct lmv_stripe_md *lsm)
66 return lsm && lsm->lsm_md_magic == LMV_MAGIC_FOREIGN;
69 static inline bool lmv_dir_layout_changing(const struct lmv_stripe_md *lsm)
71 return lmv_dir_striped(lsm) &&
72 lmv_hash_is_layout_changing(lsm->lsm_md_hash_type);
75 static inline bool lmv_dir_bad_hash(const struct lmv_stripe_md *lsm)
77 if (!lmv_dir_striped(lsm))
80 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_BAD_TYPE)
83 return !lmv_is_known_hash_type(lsm->lsm_md_hash_type);
87 lsm_md_eq(const struct lmv_stripe_md *lsm1, const struct lmv_stripe_md *lsm2)
91 if (lsm1->lsm_md_magic != lsm2->lsm_md_magic ||
92 lsm1->lsm_md_stripe_count != lsm2->lsm_md_stripe_count ||
93 lsm1->lsm_md_master_mdt_index !=
94 lsm2->lsm_md_master_mdt_index ||
95 lsm1->lsm_md_hash_type != lsm2->lsm_md_hash_type ||
96 lsm1->lsm_md_max_inherit != lsm2->lsm_md_max_inherit ||
97 lsm1->lsm_md_max_inherit_rr != lsm2->lsm_md_max_inherit_rr ||
98 lsm1->lsm_md_layout_version !=
99 lsm2->lsm_md_layout_version ||
100 lsm1->lsm_md_migrate_offset !=
101 lsm2->lsm_md_migrate_offset ||
102 lsm1->lsm_md_migrate_hash !=
103 lsm2->lsm_md_migrate_hash ||
104 strncmp(lsm1->lsm_md_pool_name, lsm2->lsm_md_pool_name,
105 sizeof(lsm1->lsm_md_pool_name)) != 0)
108 if (lmv_dir_striped(lsm1)) {
109 for (idx = 0; idx < lsm1->lsm_md_stripe_count; idx++) {
110 if (!lu_fid_eq(&lsm1->lsm_md_oinfo[idx].lmo_fid,
111 &lsm2->lsm_md_oinfo[idx].lmo_fid))
114 } else if (lsm1->lsm_md_magic == LMV_USER_MAGIC_SPECIFIC) {
115 for (idx = 0; idx < lsm1->lsm_md_stripe_count; idx++) {
116 if (lsm1->lsm_md_oinfo[idx].lmo_mds !=
117 lsm2->lsm_md_oinfo[idx].lmo_mds)
125 static inline void lsm_md_dump(int mask, const struct lmv_stripe_md *lsm)
130 "dump LMV: magic=%#x count=%u index=%u hash=%s:%#x max_inherit=%hhu max_inherit_rr=%hhu version=%u migrate_offset=%u migrate_hash=%s:%x pool=%.*s\n",
131 lsm->lsm_md_magic, lsm->lsm_md_stripe_count,
132 lsm->lsm_md_master_mdt_index,
133 lmv_is_known_hash_type(lsm->lsm_md_hash_type) ?
134 mdt_hash_name[lsm->lsm_md_hash_type & LMV_HASH_TYPE_MASK] :
135 "invalid", lsm->lsm_md_hash_type,
136 lsm->lsm_md_max_inherit, lsm->lsm_md_max_inherit_rr,
137 lsm->lsm_md_layout_version, lsm->lsm_md_migrate_offset,
138 lmv_is_known_hash_type(lsm->lsm_md_migrate_hash) ?
139 mdt_hash_name[lsm->lsm_md_migrate_hash & LMV_HASH_TYPE_MASK] :
140 "invalid", lsm->lsm_md_migrate_hash,
141 LOV_MAXPOOLNAME, lsm->lsm_md_pool_name);
143 if (!lmv_dir_striped(lsm))
146 for (i = 0; i < lsm->lsm_md_stripe_count; i++)
147 CDEBUG(mask, "stripe[%d] "DFID"\n",
148 i, PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
153 void lmv_free_memmd(struct lmv_stripe_md *lsm);
155 static inline void lmv1_le_to_cpu(struct lmv_mds_md_v1 *lmv_dst,
156 const struct lmv_mds_md_v1 *lmv_src)
160 lmv_dst->lmv_magic = le32_to_cpu(lmv_src->lmv_magic);
161 lmv_dst->lmv_stripe_count = le32_to_cpu(lmv_src->lmv_stripe_count);
162 lmv_dst->lmv_master_mdt_index =
163 le32_to_cpu(lmv_src->lmv_master_mdt_index);
164 lmv_dst->lmv_hash_type = le32_to_cpu(lmv_src->lmv_hash_type);
165 lmv_dst->lmv_layout_version = le32_to_cpu(lmv_src->lmv_layout_version);
166 if (lmv_src->lmv_stripe_count > LMV_MAX_STRIPE_COUNT)
168 for (i = 0; i < lmv_src->lmv_stripe_count; i++)
169 fid_le_to_cpu(&lmv_dst->lmv_stripe_fids[i],
170 &lmv_src->lmv_stripe_fids[i]);
173 static inline void lmv_le_to_cpu(union lmv_mds_md *lmv_dst,
174 const union lmv_mds_md *lmv_src)
176 switch (le32_to_cpu(lmv_src->lmv_magic)) {
178 lmv1_le_to_cpu(&lmv_dst->lmv_md_v1, &lmv_src->lmv_md_v1);
185 /* This hash is only for testing purpose */
186 static inline unsigned int
187 lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
190 const unsigned char *p = (const unsigned char *)name;
192 while (--namelen >= 0)
200 static inline unsigned int
201 lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
205 hash = lustre_hash_fnv_1a_64(name, namelen);
207 return do_div(hash, count);
211 * Robert Jenkins' function for mixing 32-bit values
212 * http://burtleburtle.net/bob/hash/evahash.html
213 * a, b = random bits, c = input and output
215 * Mixing inputs to generate an evenly distributed hash.
217 #define crush_hashmix(a, b, c) \
219 a = a - b; a = a - c; a = a ^ (c >> 13); \
220 b = b - c; b = b - a; b = b ^ (a << 8); \
221 c = c - a; c = c - b; c = c ^ (b >> 13); \
222 a = a - b; a = a - c; a = a ^ (c >> 12); \
223 b = b - c; b = b - a; b = b ^ (a << 16); \
224 c = c - a; c = c - b; c = c ^ (b >> 5); \
225 a = a - b; a = a - c; a = a ^ (c >> 3); \
226 b = b - c; b = b - a; b = b ^ (a << 10); \
227 c = c - a; c = c - b; c = c ^ (b >> 15); \
230 #define crush_hash_seed 1315423911
232 static inline __u32 crush_hash(__u32 a, __u32 b)
234 __u32 hash = crush_hash_seed ^ a ^ b;
238 crush_hashmix(a, b, hash);
239 crush_hashmix(x, a, hash);
240 crush_hashmix(b, y, hash);
245 /* refer to https://github.com/ceph/ceph/blob/master/src/crush/hash.c and
246 * https://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf for details of CRUSH
249 static inline unsigned int
250 lmv_hash_crush(unsigned int count, const char *name, int namelen, bool crush2)
252 unsigned long long straw;
253 unsigned long long highest_straw = 0;
255 unsigned int idx = 0;
258 /* put temp and backup file on the same MDT where target is located.
259 * temporary file naming rule:
260 * 1. rsync: .<target>.XXXXXX
261 * 2. dstripe: <target>.XXXXXXXX
263 if (lu_name_is_temp_file(name, namelen, true, 6, crush2)) {
266 } else if (lu_name_is_temp_file(name, namelen, false, 8, crush2)) {
268 } else if (lu_name_is_backup_file(name, namelen, &i)) {
269 LASSERT(i < namelen);
273 pg_id = lmv_hash_fnv1a(LMV_CRUSH_PG_COUNT, name, namelen);
275 /* distribute PG among all stripes pseudo-randomly, so they are almost
276 * evenly distributed, and when stripe count changes, only (delta /
277 * total) sub files need to be moved, herein 'delta' is added or removed
278 * stripe count, 'total' is total stripe count before change for
279 * removal, or count after change for addition.
281 for (i = 0; i < count; i++) {
282 straw = crush_hash(pg_id, i);
283 if (straw > highest_straw) {
284 highest_straw = straw;
288 LASSERT(idx < count);
293 /* directory layout may change in three ways:
294 * 1. directory migration, in its LMV source stripes are appended after
295 * target stripes, \a migrate_hash is source hash type, \a migrate_offset is
296 * target stripe count,
297 * 2. directory split, \a migrate_hash is hash type before split,
298 * \a migrate_offset is stripe count before split.
299 * 3. directory merge, \a migrate_hash is hash type after merge,
300 * \a migrate_offset is stripe count after merge.
303 __lmv_name_to_stripe_index(__u32 hash_type, __u32 stripe_count,
304 __u32 migrate_hash, __u32 migrate_offset,
305 const char *name, int namelen, bool new_layout)
307 __u32 saved_hash = hash_type;
308 __u32 saved_count = stripe_count;
309 int stripe_index = 0;
311 LASSERT(namelen > 0);
312 LASSERT(stripe_count > 0);
314 if (lmv_hash_is_splitting(hash_type)) {
316 hash_type = migrate_hash;
317 stripe_count = migrate_offset;
319 } else if (lmv_hash_is_merging(hash_type)) {
321 hash_type = migrate_hash;
322 stripe_count = migrate_offset;
324 } else if (lmv_hash_is_migrating(hash_type)) {
326 stripe_count = migrate_offset;
328 hash_type = migrate_hash;
329 stripe_count -= migrate_offset;
333 if (stripe_count > 1) {
334 switch (hash_type & LMV_HASH_TYPE_MASK) {
335 case LMV_HASH_TYPE_ALL_CHARS:
336 stripe_index = lmv_hash_all_chars(stripe_count, name,
339 case LMV_HASH_TYPE_FNV_1A_64:
340 stripe_index = lmv_hash_fnv1a(stripe_count, name,
343 case LMV_HASH_TYPE_CRUSH:
344 stripe_index = lmv_hash_crush(stripe_count, name,
347 case LMV_HASH_TYPE_CRUSH2:
348 stripe_index = lmv_hash_crush(stripe_count, name,
356 LASSERT(stripe_index < stripe_count);
358 if (!new_layout && lmv_hash_is_migrating(saved_hash))
359 stripe_index += migrate_offset;
361 LASSERT(stripe_index < saved_count);
363 CDEBUG(D_INFO, "name %.*s hash=%#x/%#x idx=%d/%u/%u under %s layout\n",
364 namelen, name, saved_hash, migrate_hash, stripe_index,
365 saved_count, migrate_offset, new_layout ? "new" : "old");
370 static inline int lmv_name_to_stripe_index(struct lmv_mds_md_v1 *lmv,
371 const char *name, int namelen)
373 if (lmv->lmv_magic == LMV_MAGIC_V1 ||
374 lmv->lmv_magic == LMV_MAGIC_STRIPE)
375 return __lmv_name_to_stripe_index(lmv->lmv_hash_type,
376 lmv->lmv_stripe_count,
377 lmv->lmv_migrate_hash,
378 lmv->lmv_migrate_offset,
379 name, namelen, true);
381 if (lmv->lmv_magic == cpu_to_le32(LMV_MAGIC_V1) ||
382 lmv->lmv_magic == cpu_to_le32(LMV_MAGIC_STRIPE))
383 return __lmv_name_to_stripe_index(
384 le32_to_cpu(lmv->lmv_hash_type),
385 le32_to_cpu(lmv->lmv_stripe_count),
386 le32_to_cpu(lmv->lmv_migrate_hash),
387 le32_to_cpu(lmv->lmv_migrate_offset),
388 name, namelen, true);
393 static inline int lmv_name_to_stripe_index_old(struct lmv_mds_md_v1 *lmv,
394 const char *name, int namelen)
396 if (lmv->lmv_magic == LMV_MAGIC_V1 ||
397 lmv->lmv_magic == LMV_MAGIC_STRIPE)
398 return __lmv_name_to_stripe_index(lmv->lmv_hash_type,
399 lmv->lmv_stripe_count,
400 lmv->lmv_migrate_hash,
401 lmv->lmv_migrate_offset,
402 name, namelen, false);
404 if (lmv->lmv_magic == cpu_to_le32(LMV_MAGIC_V1) ||
405 lmv->lmv_magic == cpu_to_le32(LMV_MAGIC_STRIPE))
406 return __lmv_name_to_stripe_index(
407 le32_to_cpu(lmv->lmv_hash_type),
408 le32_to_cpu(lmv->lmv_stripe_count),
409 le32_to_cpu(lmv->lmv_migrate_hash),
410 le32_to_cpu(lmv->lmv_migrate_offset),
411 name, namelen, false);
416 static inline bool lmv_user_magic_supported(__u32 lum_magic)
418 return lum_magic == LMV_USER_MAGIC ||
419 lum_magic == LMV_USER_MAGIC_SPECIFIC ||
420 lum_magic == LMV_MAGIC_FOREIGN;
423 #define LMV_DEBUG(mask, lmv, msg) \
425 "%s LMV: magic=%#x count=%u index=%u hash=%s:%#x version=%u migrate_offset=%u migrate_hash=%s:%x pool=%.*s\n",\
426 msg, (lmv)->lmv_magic, (lmv)->lmv_stripe_count, \
427 (lmv)->lmv_master_mdt_index, \
428 lmv_is_known_hash_type((lmv)->lmv_hash_type) ? \
429 mdt_hash_name[(lmv)->lmv_hash_type & LMV_HASH_TYPE_MASK] : \
430 "invalid", (lmv)->lmv_hash_type, \
431 (lmv)->lmv_layout_version, (lmv)->lmv_migrate_offset, \
432 lmv_is_known_hash_type((lmv)->lmv_migrate_hash) ? \
433 mdt_hash_name[(lmv)->lmv_migrate_hash & LMV_HASH_TYPE_MASK] : \
434 "invalid", (lmv)->lmv_migrate_hash, \
435 LOV_MAXPOOLNAME, lmv->lmv_pool_name)
437 /* master LMV is sane */
438 static inline bool lmv_is_sane(const struct lmv_mds_md_v1 *lmv)
443 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
446 if (le32_to_cpu(lmv->lmv_stripe_count) == 0)
449 if (!lmv_is_known_hash_type(le32_to_cpu(lmv->lmv_hash_type)))
454 LMV_DEBUG(D_ERROR, lmv, "unknown layout");
458 /* LMV can be either master or stripe LMV */
459 static inline bool lmv_is_sane2(const struct lmv_mds_md_v1 *lmv)
464 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1 &&
465 le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_STRIPE)
468 if (le32_to_cpu(lmv->lmv_stripe_count) == 0)
471 if (!lmv_is_known_hash_type(le32_to_cpu(lmv->lmv_hash_type)))
476 LMV_DEBUG(D_ERROR, lmv, "unknown layout");
480 static inline bool lmv_is_splitting(const struct lmv_mds_md_v1 *lmv)
482 if (!lmv_is_sane2(lmv))
485 return lmv_hash_is_splitting(cpu_to_le32(lmv->lmv_hash_type));
488 static inline bool lmv_is_merging(const struct lmv_mds_md_v1 *lmv)
490 if (!lmv_is_sane2(lmv))
493 return lmv_hash_is_merging(cpu_to_le32(lmv->lmv_hash_type));
496 static inline bool lmv_is_migrating(const struct lmv_mds_md_v1 *lmv)
498 if (!lmv_is_sane(lmv))
501 return lmv_hash_is_migrating(cpu_to_le32(lmv->lmv_hash_type));
504 static inline bool lmv_is_restriping(const struct lmv_mds_md_v1 *lmv)
506 if (!lmv_is_sane2(lmv))
509 return lmv_hash_is_splitting(cpu_to_le32(lmv->lmv_hash_type)) ||
510 lmv_hash_is_merging(cpu_to_le32(lmv->lmv_hash_type));
513 static inline bool lmv_is_layout_changing(const struct lmv_mds_md_v1 *lmv)
515 if (!lmv_is_sane2(lmv))
518 return lmv_hash_is_splitting(cpu_to_le32(lmv->lmv_hash_type)) ||
519 lmv_hash_is_merging(cpu_to_le32(lmv->lmv_hash_type)) ||
520 lmv_hash_is_migrating(cpu_to_le32(lmv->lmv_hash_type));
523 static inline bool lmv_is_fixed(const struct lmv_mds_md_v1 *lmv)
525 return cpu_to_le32(lmv->lmv_hash_type) & LMV_HASH_FLAG_FIXED;