Whamcloud - gitweb
LU-17334 lmv: handle object created on newly added MDT
[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  */
31
32 #ifndef _LMV_INTERNAL_H_
33 #define _LMV_INTERNAL_H_
34
35 #include <obd.h>
36 #include <lustre_lmv.h>
37
38 #define LMV_MAX_TGT_COUNT 128
39
40 #define LL_IT2STR(it)                                   \
41         ((it) ? ldlm_it2str((it)->it_op) : "0")
42
43 struct lmvsub_batch {
44         struct lu_batch         *sbh_sub;
45         struct lmv_tgt_desc     *sbh_tgt;
46         struct list_head         sbh_sub_item;
47 };
48
49 struct lmv_batch {
50         struct lu_batch                  lbh_super;
51         struct ptlrpc_request_set       *lbh_rqset;
52         struct list_head                 lbh_sub_batch_list;
53 };
54
55 int lmv_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
56                     struct lookup_intent *it, struct ptlrpc_request **reqp,
57                     ldlm_blocking_callback cb_blocking,
58                     __u64 extra_lock_flags);
59
60 int lmv_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
61                      void *, int);
62 int lmv_fld_lookup(struct lmv_obd *lmv, const struct lu_fid *fid, u32 *mds);
63 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
64                   struct lu_fid *fid, struct md_op_data *op_data);
65
66 int lmv_revalidate_slaves(struct obd_export *exp,
67                           const struct lmv_stripe_md *lsm,
68                           ldlm_blocking_callback cb_blocking,
69                           int extra_lock_flags);
70
71 int lmv_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
72                      struct ptlrpc_request **preq);
73 void lmv_activate_target(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
74                          int activate);
75
76 int lmv_statfs_check_update(struct obd_device *obd, struct lmv_tgt_desc *tgt);
77
78 static inline struct obd_device *lmv2obd_dev(struct lmv_obd *lmv)
79 {
80         return container_of_safe(lmv, struct obd_device, u.lmv);
81 }
82
83 static inline struct lu_tgt_desc *lmv_tgt(struct lmv_obd *lmv, __u32 index)
84 {
85         return index < lmv->lmv_mdt_descs.ltd_tgts_size ?
86                 LTD_TGT(&lmv->lmv_mdt_descs, index) : NULL;
87 }
88 struct lu_tgt_desc *lmv_tgt_retry(struct lmv_obd *lmv, __u32 index);
89
90 static inline bool lmv_mdt0_inited(struct lmv_obd *lmv)
91 {
92         return lmv->lmv_mdt_descs.ltd_tgts_size > 0 &&
93                test_bit(0, lmv->lmv_mdt_descs.ltd_tgt_bitmap);
94 }
95
96 #define lmv_foreach_tgt(lmv, tgt) ltd_foreach_tgt(&(lmv)->lmv_mdt_descs, tgt)
97
98 #define lmv_foreach_tgt_safe(lmv, tgt, tmp) \
99         ltd_foreach_tgt_safe(&(lmv)->lmv_mdt_descs, tgt, tmp)
100
101 static inline
102 struct lu_tgt_desc *lmv_first_connected_tgt(struct lmv_obd *lmv)
103 {
104         struct lu_tgt_desc *tgt;
105
106         tgt = ltd_first_tgt(&lmv->lmv_mdt_descs);
107         while (tgt && !tgt->ltd_exp)
108                 tgt = ltd_next_tgt(&lmv->lmv_mdt_descs, tgt);
109
110         return tgt;
111 }
112
113 static inline
114 struct lu_tgt_desc *lmv_next_connected_tgt(struct lmv_obd *lmv,
115                                            struct lu_tgt_desc *tgt)
116 {
117         do {
118                 tgt = ltd_next_tgt(&lmv->lmv_mdt_descs, tgt);
119         } while (tgt && !tgt->ltd_exp);
120
121         return tgt;
122 }
123
124 #define lmv_foreach_connected_tgt(lmv, tgt) \
125         for (tgt = lmv_first_connected_tgt(lmv); tgt; \
126              tgt = lmv_next_connected_tgt(lmv, tgt))
127
128 static inline int
129 lmv_fid2tgt_index(struct lmv_obd *lmv, const struct lu_fid *fid)
130 {
131         u32 mdt_idx;
132         int rc;
133
134         if (lmv->lmv_mdt_count < 2)
135                 return 0;
136
137         rc = lmv_fld_lookup(lmv, fid, &mdt_idx);
138         if (rc < 0)
139                 return rc;
140
141         return mdt_idx;
142 }
143
144 static inline struct lmv_tgt_desc *
145 lmv_fid2tgt(struct lmv_obd *lmv, const struct lu_fid *fid)
146 {
147         struct lu_tgt_desc *tgt;
148         int index;
149
150         if (!fid_is_sane(fid))
151                 return ERR_PTR(-EINVAL);
152
153         index = lmv_fid2tgt_index(lmv, fid);
154         if (index < 0)
155                 return ERR_PTR(index);
156
157         tgt = lmv_tgt_retry(lmv, index);
158
159         return tgt ? tgt : ERR_PTR(-ENODEV);
160 }
161
162 static inline int lmv_stripe_md_size(int stripe_count)
163 {
164         struct lmv_stripe_md *lsm;
165
166         return sizeof(*lsm) + stripe_count * sizeof(lsm->lsm_md_oinfo[0]);
167 }
168
169 /* for file under migrating directory, return the target stripe info */
170 static inline const struct lmv_oinfo *
171 lsm_name_to_stripe_info(const struct lmv_stripe_object *lso, const char *name,
172                         int namelen, bool new_layout)
173 {
174         const struct lmv_stripe_md *lsm;
175         int stripe_index;
176
177         LASSERT(lmv_dir_striped(lso));
178
179         lsm = &lso->lso_lsm;
180         stripe_index = __lmv_name_to_stripe_index(lsm->lsm_md_hash_type,
181                                                   lsm->lsm_md_stripe_count,
182                                                   lsm->lsm_md_migrate_hash,
183                                                   lsm->lsm_md_migrate_offset,
184                                                   name, namelen, new_layout);
185         if (stripe_index < 0)
186                 return ERR_PTR(stripe_index);
187
188         return &lsm->lsm_md_oinfo[stripe_index];
189 }
190
191 static inline bool lmv_dir_retry_check_update(struct md_op_data *op_data)
192 {
193         const struct lmv_stripe_object *lso = op_data->op_lso1;
194
195         if (!lso)
196                 return false;
197
198         if (lmv_dir_layout_changing(lso) && !op_data->op_new_layout) {
199                 op_data->op_new_layout = true;
200                 return true;
201         }
202
203         if (lmv_dir_bad_hash(lso) &&
204             op_data->op_stripe_index < lso->lso_lsm.lsm_md_stripe_count - 1) {
205                 op_data->op_stripe_index++;
206                 return true;
207         }
208
209         return false;
210 }
211
212 struct lmv_tgt_desc *lmv_locate_tgt(struct lmv_obd *lmv,
213                                     struct md_op_data *op_data);
214 int lmv_old_layout_lookup(struct lmv_obd *lmv, struct md_op_data *op_data);
215
216 /* lproc_lmv.c */
217 int lmv_tunables_init(struct obd_device *obd);
218 #endif