Whamcloud - gitweb
LU-5814 lov: move LSM to LOV layer
[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, 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 !=
63                                 lsm2->lsm_md_master_mdt_index ||
64             lsm1->lsm_md_hash_type != lsm2->lsm_md_hash_type ||
65             lsm1->lsm_md_layout_version !=
66                                 lsm2->lsm_md_layout_version ||
67             strcmp(lsm1->lsm_md_pool_name,
68                       lsm2->lsm_md_pool_name) != 0)
69                 return false;
70
71         for (idx = 0; idx < lsm1->lsm_md_stripe_count; idx++) {
72                 if (!lu_fid_eq(&lsm1->lsm_md_oinfo[idx].lmo_fid,
73                                &lsm2->lsm_md_oinfo[idx].lmo_fid))
74                         return false;
75         }
76
77         return true;
78 }
79 union lmv_mds_md;
80
81 int lmv_alloc_md(union lmv_mds_md **lmmp, int stripe_count);
82 void lmv_free_md(union lmv_mds_md *lmm);
83 int lmv_alloc_memmd(struct lmv_stripe_md **lsmp, int stripe_count);
84 void lmv_free_memmd(struct lmv_stripe_md *lsm);
85
86 int lmvea_load_shards(const struct lu_env *env, struct dt_object *obj,
87                       struct lu_dirent *ent, struct lu_buf *buf,
88                       bool resize);
89
90 static inline void lmv1_le_to_cpu(struct lmv_mds_md_v1 *lmv_dst,
91                                   const struct lmv_mds_md_v1 *lmv_src)
92 {
93         __u32 i;
94
95         lmv_dst->lmv_magic = le32_to_cpu(lmv_src->lmv_magic);
96         lmv_dst->lmv_stripe_count = le32_to_cpu(lmv_src->lmv_stripe_count);
97         lmv_dst->lmv_master_mdt_index =
98                                 le32_to_cpu(lmv_src->lmv_master_mdt_index);
99         lmv_dst->lmv_hash_type = le32_to_cpu(lmv_src->lmv_hash_type);
100         lmv_dst->lmv_layout_version = le32_to_cpu(lmv_src->lmv_layout_version);
101         for (i = 0; i < lmv_src->lmv_stripe_count; i++)
102                 fid_le_to_cpu(&lmv_dst->lmv_stripe_fids[i],
103                               &lmv_src->lmv_stripe_fids[i]);
104 }
105
106 static inline void lmv_le_to_cpu(union lmv_mds_md *lmv_dst,
107                                  const union lmv_mds_md *lmv_src)
108 {
109         switch (le32_to_cpu(lmv_src->lmv_magic)) {
110         case LMV_MAGIC_V1:
111                 lmv1_le_to_cpu(&lmv_dst->lmv_md_v1, &lmv_src->lmv_md_v1);
112                 break;
113         default:
114                 break;
115         }
116 }
117
118 /* This hash is only for testing purpose */
119 static inline unsigned int
120 lmv_hash_all_chars(unsigned int count, const char *name, int namelen)
121 {
122         unsigned int c = 0;
123         const unsigned char *p = (const unsigned char *)name;
124
125         while (--namelen >= 0)
126                 c += p[namelen];
127
128         c = c % count;
129
130         return c;
131 }
132
133 static inline unsigned int
134 lmv_hash_fnv1a(unsigned int count, const char *name, int namelen)
135 {
136         __u64   hash;
137
138         hash = lustre_hash_fnv_1a_64(name, namelen);
139
140         hash = hash % count;
141
142         return hash;
143 }
144
145 static inline int lmv_name_to_stripe_index(__u32 lmv_hash_type,
146                                            unsigned int stripe_count,
147                                            const char *name, int namelen)
148 {
149         int     idx;
150         __u32   hash_type = lmv_hash_type & LMV_HASH_TYPE_MASK;
151
152         LASSERT(namelen > 0);
153         if (stripe_count <= 1)
154                 return 0;
155
156         /* for migrating object, always start from 0 stripe */
157         if (lmv_hash_type & LMV_HASH_FLAG_MIGRATION)
158                 return 0;
159
160         switch (hash_type) {
161         case LMV_HASH_TYPE_ALL_CHARS:
162                 idx = lmv_hash_all_chars(stripe_count, name, namelen);
163                 break;
164         case LMV_HASH_TYPE_FNV_1A_64:
165                 idx = lmv_hash_fnv1a(stripe_count, name, namelen);
166                 break;
167         default:
168                 idx = -EBADFD;
169                 break;
170         }
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