Whamcloud - gitweb
LU-11213 lmv: mkdir with balanced space usage
[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 /* NB: LMV_HASH_TYPE_SPACE is set in default LMV only */
58 static inline bool lmv_is_known_hash_type(__u32 type)
59 {
60         return (type & LMV_HASH_TYPE_MASK) == LMV_HASH_TYPE_FNV_1A_64 ||
61                (type & LMV_HASH_TYPE_MASK) == LMV_HASH_TYPE_ALL_CHARS;
62 }
63
64 static inline bool lmv_dir_striped(const struct lmv_stripe_md *lsm)
65 {
66         return lsm && lsm->lsm_md_magic == LMV_MAGIC;
67 }
68
69 static inline bool lmv_dir_foreign(const struct lmv_stripe_md *lsm)
70 {
71         return lsm && lsm->lsm_md_magic == LMV_MAGIC_FOREIGN;
72 }
73
74 static inline bool lmv_dir_migrating(const struct lmv_stripe_md *lsm)
75 {
76         return lmv_dir_striped(lsm) &&
77                lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION;
78 }
79
80 static inline bool lmv_dir_bad_hash(const struct lmv_stripe_md *lsm)
81 {
82         if (!lmv_dir_striped(lsm))
83                 return false;
84
85         if (lmv_dir_migrating(lsm) &&
86             lsm->lsm_md_stripe_count - lsm->lsm_md_migrate_offset <= 1)
87                 return false;
88
89         return !lmv_is_known_hash_type(lsm->lsm_md_hash_type);
90 }
91
92 /* NB, this is checking directory default LMV */
93 static inline bool lmv_dir_space_hashed(const struct lmv_stripe_md *lsm)
94 {
95         return lsm && lsm->lsm_md_hash_type == LMV_HASH_TYPE_SPACE;
96 }
97
98 static inline bool
99 lsm_md_eq(const struct lmv_stripe_md *lsm1, const struct lmv_stripe_md *lsm2)
100 {
101         __u32 idx;
102
103         if (lsm1->lsm_md_magic != lsm2->lsm_md_magic ||
104             lsm1->lsm_md_stripe_count != lsm2->lsm_md_stripe_count ||
105             lsm1->lsm_md_master_mdt_index !=
106                                 lsm2->lsm_md_master_mdt_index ||
107             lsm1->lsm_md_hash_type != lsm2->lsm_md_hash_type ||
108             lsm1->lsm_md_layout_version !=
109                                 lsm2->lsm_md_layout_version ||
110             lsm1->lsm_md_migrate_offset !=
111                                 lsm2->lsm_md_migrate_offset ||
112             lsm1->lsm_md_migrate_hash !=
113                                 lsm2->lsm_md_migrate_hash ||
114             strcmp(lsm1->lsm_md_pool_name,
115                       lsm2->lsm_md_pool_name) != 0)
116                 return false;
117
118         if (lmv_dir_striped(lsm1)) {
119                 for (idx = 0; idx < lsm1->lsm_md_stripe_count; idx++) {
120                         if (!lu_fid_eq(&lsm1->lsm_md_oinfo[idx].lmo_fid,
121                                        &lsm2->lsm_md_oinfo[idx].lmo_fid))
122                                 return false;
123                 }
124         }
125
126         return true;
127 }
128
129 static inline void lsm_md_dump(int mask, const struct lmv_stripe_md *lsm)
130 {
131         int i;
132
133         CDEBUG(mask, "magic %#x stripe count %d master mdt %d hash type %#x "
134                 "version %d migrate offset %d migrate hash %#x pool %s\n",
135                 lsm->lsm_md_magic, lsm->lsm_md_stripe_count,
136                 lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
137                 lsm->lsm_md_layout_version, lsm->lsm_md_migrate_offset,
138                 lsm->lsm_md_migrate_hash, lsm->lsm_md_pool_name);
139
140         if (!lmv_dir_striped(lsm))
141                 return;
142
143         for (i = 0; i < lsm->lsm_md_stripe_count; i++)
144                 CDEBUG(mask, "stripe[%d] "DFID"\n",
145                        i, PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
146 }
147
148 union lmv_mds_md;
149
150 void lmv_free_memmd(struct lmv_stripe_md *lsm);
151
152 static inline void lmv1_le_to_cpu(struct lmv_mds_md_v1 *lmv_dst,
153                                   const struct lmv_mds_md_v1 *lmv_src)
154 {
155         __u32 i;
156
157         lmv_dst->lmv_magic = le32_to_cpu(lmv_src->lmv_magic);
158         lmv_dst->lmv_stripe_count = le32_to_cpu(lmv_src->lmv_stripe_count);
159         lmv_dst->lmv_master_mdt_index =
160                                 le32_to_cpu(lmv_src->lmv_master_mdt_index);
161         lmv_dst->lmv_hash_type = le32_to_cpu(lmv_src->lmv_hash_type);
162         lmv_dst->lmv_layout_version = le32_to_cpu(lmv_src->lmv_layout_version);
163         for (i = 0; i < lmv_src->lmv_stripe_count; i++)
164                 fid_le_to_cpu(&lmv_dst->lmv_stripe_fids[i],
165                               &lmv_src->lmv_stripe_fids[i]);
166 }
167
168 static inline void lmv_le_to_cpu(union lmv_mds_md *lmv_dst,
169                                  const union lmv_mds_md *lmv_src)
170 {
171         switch (le32_to_cpu(lmv_src->lmv_magic)) {
172         case LMV_MAGIC_V1:
173                 lmv1_le_to_cpu(&lmv_dst->lmv_md_v1, &lmv_src->lmv_md_v1);
174                 break;
175         default:
176                 break;
177         }
178 }
179
180 /* This hash is only for testing purpose */
181 static inline unsigned int
182 lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
183 {
184         unsigned int c = 0;
185         const unsigned char *p = (const unsigned char *)name;
186
187         while (--namelen >= 0)
188                 c += p[namelen];
189
190         c = c % count;
191
192         return c;
193 }
194
195 static inline unsigned int
196 lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
197 {
198         __u64 hash;
199
200         hash = lustre_hash_fnv_1a_64(name, namelen);
201
202         return do_div(hash, count);
203 }
204
205 static inline int lmv_name_to_stripe_index(__u32 lmv_hash_type,
206                                            unsigned int stripe_count,
207                                            const char *name, int namelen)
208 {
209         int idx;
210
211         LASSERT(namelen > 0);
212
213         if (stripe_count <= 1)
214                 return 0;
215
216         switch (lmv_hash_type & LMV_HASH_TYPE_MASK) {
217         case LMV_HASH_TYPE_ALL_CHARS:
218                 idx = lmv_hash_all_chars(stripe_count, name, namelen);
219                 break;
220         case LMV_HASH_TYPE_FNV_1A_64:
221                 idx = lmv_hash_fnv1a(stripe_count, name, namelen);
222                 break;
223         default:
224                 idx = -EBADFD;
225                 break;
226         }
227
228         CDEBUG(D_INFO, "name %.*s hash_type %#x idx %d/%u\n", namelen, name,
229                lmv_hash_type, idx, stripe_count);
230
231         return idx;
232 }
233
234 static inline bool lmv_magic_supported(__u32 lum_magic)
235 {
236         return lum_magic == LMV_USER_MAGIC ||
237                lum_magic == LMV_USER_MAGIC_SPECIFIC ||
238                lum_magic == LMV_MAGIC_FOREIGN;
239 }
240
241 #endif