Whamcloud - gitweb
9f48a1520c01f76aafa3e3b8917416c2a86e0369
[fs/lustre-release.git] / lustre / lmv / lmv_internal.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef _LMV_INTERNAL_H_
34 #define _LMV_INTERNAL_H_
35
36 #include <obd.h>
37 #include <lustre_lmv.h>
38
39 #define LMV_MAX_TGT_COUNT 128
40
41 #define LL_IT2STR(it)                                   \
42         ((it) ? ldlm_it2str((it)->it_op) : "0")
43
44 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
45                     struct lookup_intent *it, struct ptlrpc_request **reqp,
46                     ldlm_blocking_callback cb_blocking,
47                     __u64 extra_lock_flags);
48
49 int lmv_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
50                      void *, int);
51 int lmv_fld_lookup(struct lmv_obd *lmv, const struct lu_fid *fid, u32 *mds);
52 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds);
53 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
54                   struct lu_fid *fid, struct md_op_data *op_data);
55
56 int lmv_revalidate_slaves(struct obd_export *exp,
57                           const struct lmv_stripe_md *lsm,
58                           ldlm_blocking_callback cb_blocking,
59                           int extra_lock_flags);
60
61 int lmv_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
62                      struct ptlrpc_request **preq);
63 void lmv_activate_target(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
64                          int activate);
65
66 int lmv_statfs_check_update(struct obd_device *obd, struct lmv_tgt_desc *tgt);
67
68 static inline struct obd_device *lmv2obd_dev(struct lmv_obd *lmv)
69 {
70         return container_of0(lmv, struct obd_device, u.lmv);
71 }
72
73 static inline struct lu_tgt_desc *
74 lmv_tgt(struct lmv_obd *lmv, __u32 index)
75 {
76         return index < lmv->lmv_mdt_descs.ltd_tgt_bitmap->size ?
77                 LTD_TGT(&lmv->lmv_mdt_descs, index) : NULL;
78 }
79
80 static inline bool
81 lmv_mdt0_inited(struct lmv_obd *lmv)
82 {
83         return lmv->lmv_mdt_descs.ltd_tgt_bitmap->size > 0 &&
84                cfs_bitmap_check(lmv->lmv_mdt_descs.ltd_tgt_bitmap, 0);
85 }
86
87 #define lmv_foreach_tgt(lmv, tgt) ltd_foreach_tgt(&(lmv)->lmv_mdt_descs, tgt)
88
89 #define lmv_foreach_tgt_safe(lmv, tgt, tmp) \
90         ltd_foreach_tgt_safe(&(lmv)->lmv_mdt_descs, tgt, tmp)
91
92 static inline
93 struct lu_tgt_desc *lmv_first_connected_tgt(struct lmv_obd *lmv)
94 {
95         struct lu_tgt_desc *tgt;
96
97         tgt = ltd_first_tgt(&lmv->lmv_mdt_descs);
98         while (tgt && !tgt->ltd_exp)
99                 tgt = ltd_next_tgt(&lmv->lmv_mdt_descs, tgt);
100
101         return tgt;
102 }
103
104 static inline
105 struct lu_tgt_desc *lmv_next_connected_tgt(struct lmv_obd *lmv,
106                                            struct lu_tgt_desc *tgt)
107 {
108         do {
109                 tgt = ltd_next_tgt(&lmv->lmv_mdt_descs, tgt);
110         } while (tgt && !tgt->ltd_exp);
111
112         return tgt;
113 }
114
115 #define lmv_foreach_connected_tgt(lmv, tgt) \
116         for (tgt = lmv_first_connected_tgt(lmv); tgt; \
117              tgt = lmv_next_connected_tgt(lmv, tgt))
118
119 static inline int
120 lmv_fid2tgt_index(struct lmv_obd *lmv, const struct lu_fid *fid)
121 {
122         u32 mdt_idx;
123         int rc;
124
125         if (lmv->desc.ld_tgt_count < 2)
126                 return 0;
127
128         rc = lmv_fld_lookup(lmv, fid, &mdt_idx);
129         if (rc < 0)
130                 return rc;
131
132         return mdt_idx;
133 }
134
135 static inline struct lmv_tgt_desc *
136 lmv_fid2tgt(struct lmv_obd *lmv, const struct lu_fid *fid)
137 {
138         struct lu_tgt_desc *tgt;
139         int index;
140
141         index = lmv_fid2tgt_index(lmv, fid);
142         if (index < 0)
143                 return ERR_PTR(index);
144
145         tgt = lmv_tgt(lmv, index);
146
147         return tgt ? tgt : ERR_PTR(-ENODEV);
148 }
149
150 static inline int lmv_stripe_md_size(int stripe_count)
151 {
152         struct lmv_stripe_md *lsm;
153
154         return sizeof(*lsm) + stripe_count * sizeof(lsm->lsm_md_oinfo[0]);
155 }
156
157 /* for file under migrating directory, return the target stripe info */
158 static inline const struct lmv_oinfo *
159 lsm_name_to_stripe_info(const struct lmv_stripe_md *lsm, const char *name,
160                         int namelen, bool post_migrate)
161 {
162         __u32 hash_type = lsm->lsm_md_hash_type;
163         __u32 stripe_count = lsm->lsm_md_stripe_count;
164         int stripe_index;
165
166         LASSERT(lmv_dir_striped(lsm));
167
168         if (hash_type & LMV_HASH_FLAG_MIGRATION) {
169                 if (post_migrate) {
170                         hash_type &= ~LMV_HASH_FLAG_MIGRATION;
171                         stripe_count = lsm->lsm_md_migrate_offset;
172                 } else {
173                         hash_type = lsm->lsm_md_migrate_hash;
174                         stripe_count -= lsm->lsm_md_migrate_offset;
175                 }
176         }
177
178         stripe_index = lmv_name_to_stripe_index(hash_type, stripe_count,
179                                                 name, namelen);
180         if (stripe_index < 0)
181                 return ERR_PTR(stripe_index);
182
183         if ((lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) && !post_migrate)
184                 stripe_index += lsm->lsm_md_migrate_offset;
185
186         if (stripe_index >= lsm->lsm_md_stripe_count) {
187                 CERROR("stripe_index %d stripe_count %d hash_type %#x "
188                         "migrate_offset %d migrate_hash %#x name %.*s\n",
189                         stripe_index, lsm->lsm_md_stripe_count,
190                         lsm->lsm_md_hash_type, lsm->lsm_md_migrate_offset,
191                         lsm->lsm_md_migrate_hash, namelen, name);
192                 return ERR_PTR(-EBADF);
193         }
194
195         return &lsm->lsm_md_oinfo[stripe_index];
196 }
197
198 static inline bool lmv_dir_retry_check_update(struct md_op_data *op_data)
199 {
200         const struct lmv_stripe_md *lsm = op_data->op_mea1;
201
202         if (!lsm)
203                 return false;
204
205         if (lmv_dir_migrating(lsm) && !op_data->op_post_migrate) {
206                 op_data->op_post_migrate = true;
207                 return true;
208         }
209
210         if (lmv_dir_bad_hash(lsm) &&
211             op_data->op_stripe_index < lsm->lsm_md_stripe_count - 1) {
212                 op_data->op_stripe_index++;
213                 return true;
214         }
215
216         return false;
217 }
218
219 struct lmv_tgt_desc *lmv_locate_tgt(struct lmv_obd *lmv,
220                                     struct md_op_data *op_data);
221
222 /* lproc_lmv.c */
223 int lmv_tunables_init(struct obd_device *obd);
224
225 #endif