Whamcloud - gitweb
LU-4684 lmv: support accessing migrating directory
[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
64 static inline struct obd_device *lmv2obd_dev(struct lmv_obd *lmv)
65 {
66         return container_of0(lmv, struct obd_device, u.lmv);
67 }
68
69 static inline struct lmv_tgt_desc *
70 lmv_get_target(struct lmv_obd *lmv, u32 mdt_idx, int *index)
71 {
72         int i;
73
74         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
75                 if (lmv->tgts[i] == NULL)
76                         continue;
77
78                 if (lmv->tgts[i]->ltd_idx == mdt_idx) {
79                         if (index != NULL)
80                                 *index = i;
81                         return lmv->tgts[i];
82                 }
83         }
84
85         return ERR_PTR(-ENODEV);
86 }
87
88 static inline int
89 lmv_find_target_index(struct lmv_obd *lmv, const struct lu_fid *fid)
90 {
91         struct lmv_tgt_desc     *ltd;
92         u32                     mdt_idx = 0;
93         int                     index = 0;
94
95         if (lmv->desc.ld_tgt_count > 1) {
96                 int rc;
97                 rc = lmv_fld_lookup(lmv, fid, &mdt_idx);
98                 if (rc < 0)
99                         return rc;
100         }
101
102         ltd = lmv_get_target(lmv, mdt_idx, &index);
103         if (IS_ERR(ltd))
104                 return PTR_ERR(ltd);
105
106         return index;
107 }
108
109 static inline struct lmv_tgt_desc *
110 lmv_find_target(struct lmv_obd *lmv, const struct lu_fid *fid)
111 {
112         int index;
113
114         index = lmv_find_target_index(lmv, fid);
115         if (index < 0)
116                 return ERR_PTR(index);
117
118         return lmv->tgts[index];
119 }
120
121 static inline int lmv_stripe_md_size(int stripe_count)
122 {
123         struct lmv_stripe_md *lsm;
124
125         return sizeof(*lsm) + stripe_count * sizeof(lsm->lsm_md_oinfo[0]);
126 }
127
128 /* for file under migrating directory, return the target stripe info */
129 static inline const struct lmv_oinfo *
130 lsm_name_to_stripe_info(const struct lmv_stripe_md *lsm, const char *name,
131                         int namelen, bool post_migrate)
132 {
133         __u32 hash_type = lsm->lsm_md_hash_type;
134         __u32 stripe_count = lsm->lsm_md_stripe_count;
135         int stripe_index;
136
137         if (hash_type & LMV_HASH_FLAG_MIGRATION) {
138                 if (post_migrate) {
139                         hash_type &= ~LMV_HASH_FLAG_MIGRATION;
140                         stripe_count = lsm->lsm_md_migrate_offset;
141                 } else {
142                         hash_type = lsm->lsm_md_migrate_hash;
143                         stripe_count -= lsm->lsm_md_migrate_offset;
144                 }
145         }
146
147         stripe_index = lmv_name_to_stripe_index(hash_type, stripe_count,
148                                                 name, namelen);
149         if (stripe_index < 0)
150                 return ERR_PTR(stripe_index);
151
152         if ((lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) && !post_migrate)
153                 stripe_index += lsm->lsm_md_migrate_offset;
154
155         if (stripe_index >= lsm->lsm_md_stripe_count) {
156                 CERROR("stripe_index %d stripe_count %d hash_type %#x "
157                         "migrate_offset %d migrate_hash %#x name %.*s\n",
158                         stripe_index, lsm->lsm_md_stripe_count,
159                         lsm->lsm_md_hash_type, lsm->lsm_md_migrate_offset,
160                         lsm->lsm_md_migrate_hash, namelen, name);
161                 return ERR_PTR(-EBADF);
162         }
163
164         return &lsm->lsm_md_oinfo[stripe_index];
165 }
166
167 static inline bool lmv_is_dir_migrating(const struct lmv_stripe_md *lsm)
168 {
169         return lsm ? lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION : false;
170 }
171
172 static inline bool lmv_is_dir_bad_hash(const struct lmv_stripe_md *lsm)
173 {
174         if (!lsm)
175                 return false;
176
177         if (lmv_is_dir_migrating(lsm)) {
178                 if (lsm->lsm_md_stripe_count - lsm->lsm_md_migrate_offset > 1)
179                         return !lmv_is_known_hash_type(
180                                         lsm->lsm_md_migrate_hash);
181                 return false;
182         }
183
184         return !lmv_is_known_hash_type(lsm->lsm_md_hash_type);
185 }
186
187 static inline bool lmv_dir_retry_check_update(struct md_op_data *op_data)
188 {
189         const struct lmv_stripe_md *lsm = op_data->op_mea1;
190
191         if (!lsm)
192                 return false;
193
194         if (lmv_is_dir_migrating(lsm) && !op_data->op_post_migrate) {
195                 op_data->op_post_migrate = true;
196                 return true;
197         }
198
199         if (lmv_is_dir_bad_hash(lsm) &&
200             op_data->op_stripe_index < lsm->lsm_md_stripe_count - 1) {
201                 op_data->op_stripe_index++;
202                 return true;
203         }
204
205         return false;
206 }
207
208 struct lmv_tgt_desc *lmv_locate_tgt(struct lmv_obd *lmv,
209                                     struct md_op_data *op_data,
210                                     struct lu_fid *fid);
211 /* lproc_lmv.c */
212 int lmv_tunables_init(struct obd_device *obd);
213
214 #endif