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