Whamcloud - gitweb
LU-10264 mdc: fix possible NULL pointer dereference
[fs/lustre-release.git] / lustre / lov / lov_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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, 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 LOV_INTERNAL_H
34 #define LOV_INTERNAL_H
35
36 #include <obd_class.h>
37 #include <uapi/linux/lustre/lustre_user.h>
38
39 /* If we are unable to get the maximum object size from the OST in
40  * ocd_maxbytes using OBD_CONNECT_MAXBYTES, then we fall back to using
41  * the old maximum object size from ext3. */
42 #define LUSTRE_EXT3_STRIPE_MAXBYTES 0x1fffffff000ULL
43
44 struct lov_stripe_md_entry {
45         struct lu_extent        lsme_extent;
46         u32                     lsme_id;
47         u32                     lsme_magic;
48         u32                     lsme_flags;
49         u32                     lsme_pattern;
50         u32                     lsme_stripe_size;
51         u16                     lsme_stripe_count;
52         u16                     lsme_layout_gen;
53         char                    lsme_pool_name[LOV_MAXPOOLNAME + 1];
54         struct lov_oinfo       *lsme_oinfo[];
55 };
56
57 static inline bool lsme_is_dom(struct lov_stripe_md_entry *lsme)
58 {
59         return (lov_pattern(lsme->lsme_pattern) == LOV_PATTERN_MDT);
60 }
61
62 static inline void copy_lsm_entry(struct lov_stripe_md_entry *dst,
63                                   struct lov_stripe_md_entry *src)
64 {
65         unsigned i;
66
67         for (i = 0; i < src->lsme_stripe_count; i++)
68                 *dst->lsme_oinfo[i] = *src->lsme_oinfo[i];
69         memcpy(dst, src, offsetof(typeof(*src), lsme_oinfo));
70 }
71
72 struct lov_stripe_md {
73         atomic_t        lsm_refc;
74         spinlock_t      lsm_lock;
75         pid_t           lsm_lock_owner; /* debugging */
76
77         /* maximum possible file size, might change as OSTs status changes,
78          * e.g. disconnected, deactivated */
79         loff_t          lsm_maxbytes;
80         struct ost_id   lsm_oi;
81         u32             lsm_magic;
82         u32             lsm_layout_gen;
83         u16             lsm_flags;
84         bool            lsm_is_released;
85         u16             lsm_mirror_count;
86         u16             lsm_entry_count;
87         struct lov_stripe_md_entry *lsm_entries[];
88 };
89
90 static inline bool lsme_inited(const struct lov_stripe_md_entry *lsme)
91 {
92         return lsme->lsme_flags & LCME_FL_INIT;
93 }
94
95 static inline bool lsm_entry_inited(const struct lov_stripe_md *lsm, int index)
96 {
97         return lsme_inited(lsm->lsm_entries[index]);
98 }
99
100 static inline bool lsm_is_composite(__u32 magic)
101 {
102         return magic == LOV_MAGIC_COMP_V1;
103 }
104
105 static inline size_t lov_comp_md_size(const struct lov_stripe_md *lsm)
106 {
107         struct lov_stripe_md_entry *lsme;
108         size_t size;
109         int entry;
110
111         if (lsm->lsm_magic == LOV_MAGIC_V1 || lsm->lsm_magic == LOV_MAGIC_V3)
112                 return lov_mds_md_size(lsm->lsm_entries[0]->lsme_stripe_count,
113                                        lsm->lsm_entries[0]->lsme_magic);
114
115         LASSERT(lsm->lsm_magic == LOV_MAGIC_COMP_V1);
116
117         size = sizeof(struct lov_comp_md_v1);
118         for (entry = 0; entry < lsm->lsm_entry_count; entry++) {
119                 u16 stripe_count;
120
121                 lsme = lsm->lsm_entries[entry];
122
123                 if (lsme_inited(lsme))
124                         stripe_count = lsme->lsme_stripe_count;
125                 else
126                         stripe_count = 0;
127
128                 size += sizeof(*lsme);
129                 size += lov_mds_md_size(lsme->lsme_stripe_count,
130                                         lsme->lsme_magic);
131         }
132
133         return size;
134 }
135
136 static inline bool lsm_has_objects(struct lov_stripe_md *lsm)
137 {
138         return lsm != NULL && !lsm->lsm_is_released;
139 }
140
141 static inline unsigned int lov_comp_index(int entry, int stripe)
142 {
143         LASSERT(entry >= 0 && entry <= SHRT_MAX);
144         LASSERT(stripe >= 0 && stripe < USHRT_MAX);
145
146         return entry << 16 | stripe;
147 }
148
149 static inline int lov_comp_stripe(int index)
150 {
151         return index & 0xffff;
152 }
153
154 static inline int lov_comp_entry(int index)
155 {
156         return index >> 16;
157 }
158
159 struct lsm_operations {
160         struct lov_stripe_md *(*lsm_unpackmd)(struct lov_obd *, void *, size_t);
161 };
162
163 extern const struct lsm_operations lsm_v1_ops;
164 extern const struct lsm_operations lsm_v3_ops;
165 extern const struct lsm_operations lsm_comp_md_v1_ops;
166 static inline const struct lsm_operations *lsm_op_find(int magic)
167 {
168         switch (magic) {
169         case LOV_MAGIC_V1:
170                 return &lsm_v1_ops;
171         case LOV_MAGIC_V3:
172                 return &lsm_v3_ops;
173         case LOV_MAGIC_COMP_V1:
174                 return &lsm_comp_md_v1_ops;
175         default:
176                 CERROR("unrecognized lsm_magic %08x\n", magic);
177                 return NULL;
178         }
179 }
180
181 void lsm_free(struct lov_stripe_md *lsm);
182
183 /* lov_do_div64(a, b) returns a % b, and a = a / b.
184  * The 32-bit code is LOV-specific due to knowing about stripe limits in
185  * order to reduce the divisor to a 32-bit number.  If the divisor is
186  * already a 32-bit value the compiler handles this directly. */
187 #if BITS_PER_LONG == 64
188 # define lov_do_div64(n, base) ({                                       \
189         uint64_t __base = (base);                                       \
190         uint64_t __rem;                                                 \
191         __rem = ((uint64_t)(n)) % __base;                               \
192         (n) = ((uint64_t)(n)) / __base;                                 \
193         __rem;                                                          \
194 })
195 #elif BITS_PER_LONG == 32
196 # define lov_do_div64(n, base) ({                                       \
197         uint64_t __rem;                                                 \
198         if ((sizeof(base) > 4) && (((base) & 0xffffffff00000000ULL) != 0)) {  \
199                 int __remainder;                                              \
200                 LASSERTF(!((base) & (LOV_MIN_STRIPE_SIZE - 1)), "64 bit lov " \
201                          "division %llu / %llu\n", (n), (uint64_t)(base));    \
202                 __remainder = (n) & (LOV_MIN_STRIPE_SIZE - 1);          \
203                 (n) >>= LOV_MIN_STRIPE_BITS;                            \
204                 __rem = do_div(n, (base) >> LOV_MIN_STRIPE_BITS);       \
205                 __rem <<= LOV_MIN_STRIPE_BITS;                          \
206                 __rem += __remainder;                                   \
207         } else {                                                        \
208                 __rem = do_div(n, base);                                \
209         }                                                               \
210         __rem;                                                          \
211 })
212 #endif
213
214 #define pool_tgt_count(p) ((p)->pool_obds.op_count)
215 #define pool_tgt_array(p) ((p)->pool_obds.op_array)
216 #define pool_tgt_rw_sem(p) ((p)->pool_obds.op_rw_sem)
217
218 struct pool_desc {
219         char                     pool_name[LOV_MAXPOOLNAME + 1];
220         struct ost_pool          pool_obds;
221         atomic_t                 pool_refcount;
222         struct hlist_node        pool_hash;     /* access by poolname */
223         struct list_head         pool_list;     /* serial access */
224         struct proc_dir_entry   *pool_proc_entry;
225         struct obd_device       *pool_lobd;     /* owner */
226 };
227
228 struct lov_request {
229         struct obd_info          rq_oi;
230         struct lov_request_set  *rq_rqset;
231         struct list_head         rq_link;
232         int                      rq_idx;        /* index in lov->tgts array */
233 };
234
235 struct lov_request_set {
236         struct obd_info         *set_oi;
237         struct obd_device       *set_obd;
238         int                      set_count;
239         atomic_t                 set_completes;
240         atomic_t                 set_success;
241         struct list_head         set_list;
242 };
243
244 extern struct kmem_cache *lov_oinfo_slab;
245
246 extern struct lu_kmem_descr lov_caches[];
247
248 #define lov_uuid2str(lv, index) \
249         (char *)((lv)->lov_tgts[index]->ltd_uuid.uuid)
250
251 /* lov_merge.c */
252 int lov_merge_lvb_kms(struct lov_stripe_md *lsm, int index,
253                       struct ost_lvb *lvb, __u64 *kms_place);
254
255 /* lov_offset.c */
256 u64 lov_stripe_size(struct lov_stripe_md *lsm, int index,
257                     u64 ost_size, int stripeno);
258 int lov_stripe_offset(struct lov_stripe_md *lsm, int index, loff_t lov_off,
259                       int stripeno, loff_t *obd_off);
260 loff_t lov_size_to_stripe(struct lov_stripe_md *lsm, int index, u64 file_size,
261                           int stripeno);
262 int lov_stripe_intersects(struct lov_stripe_md *lsm, int index, int stripeno,
263                           struct lu_extent *ext, u64 *obd_start, u64 *obd_end);
264 int lov_stripe_number(struct lov_stripe_md *lsm, int index, loff_t lov_off);
265 pgoff_t lov_stripe_pgoff(struct lov_stripe_md *lsm, int index,
266                          pgoff_t stripe_index, int stripe);
267
268 /* lov_request.c */
269 int lov_prep_statfs_set(struct obd_device *obd, struct obd_info *oinfo,
270                         struct lov_request_set **reqset);
271 int lov_fini_statfs_set(struct lov_request_set *set);
272
273 /* lov_obd.c */
274 void lov_stripe_lock(struct lov_stripe_md *md);
275 void lov_stripe_unlock(struct lov_stripe_md *md);
276 void lov_fix_desc(struct lov_desc *desc);
277 void lov_fix_desc_stripe_size(__u64 *val);
278 void lov_fix_desc_stripe_count(__u32 *val);
279 void lov_fix_desc_pattern(__u32 *val);
280 void lov_fix_desc_qos_maxage(__u32 *val);
281 __u16 lov_get_stripe_count(struct lov_obd *lov, __u32 magic,
282                            __u16 stripe_count);
283 int lov_connect_obd(struct obd_device *obd, u32 index, int activate,
284                     struct obd_connect_data *data);
285 int lov_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
286 int lov_process_config_base(struct obd_device *obd, struct lustre_cfg *lcfg,
287                             u32 *indexp, int *genp);
288 int lov_del_target(struct obd_device *obd, u32 index,
289                    struct obd_uuid *uuidp, int gen);
290
291 /* lov_pack.c */
292 ssize_t lov_lsm_pack(const struct lov_stripe_md *lsm, void *buf,
293                      size_t buf_size);
294 struct lov_stripe_md *lov_unpackmd(struct lov_obd *lov, void *buf,
295                                    size_t buf_size);
296 int lov_free_memmd(struct lov_stripe_md **lsmp);
297
298 void lov_dump_lmm_v1(int level, struct lov_mds_md_v1 *lmm);
299 void lov_dump_lmm_v3(int level, struct lov_mds_md_v3 *lmm);
300 void lov_dump_lmm_common(int level, void *lmmp);
301 void lov_dump_lmm(int level, void *lmm);
302
303 /* lov_ea.c */
304 void lsm_free_plain(struct lov_stripe_md *lsm);
305 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm);
306
307 /* lproc_lov.c */
308 extern const struct file_operations lov_proc_target_fops;
309 #ifdef CONFIG_PROC_FS
310 extern struct lprocfs_vars lprocfs_lov_obd_vars[];
311 #endif
312
313 /* lov_cl.c */
314 extern struct lu_device_type lov_device_type;
315
316 #define LOV_MDC_TGT_MAX 256
317
318 /* pools */
319 extern struct cfs_hash_ops pool_hash_operations;
320 /* ost_pool methods */
321 int lov_ost_pool_init(struct ost_pool *op, unsigned int count);
322 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count);
323 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count);
324 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx);
325 int lov_ost_pool_free(struct ost_pool *op);
326
327 /* high level pool methods */
328 int lov_pool_new(struct obd_device *obd, char *poolname);
329 int lov_pool_del(struct obd_device *obd, char *poolname);
330 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname);
331 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname);
332 void lov_dump_pool(int level, struct pool_desc *pool);
333
334 static inline struct lov_stripe_md *lsm_addref(struct lov_stripe_md *lsm)
335 {
336         LASSERT(atomic_read(&lsm->lsm_refc) > 0);
337         atomic_inc(&lsm->lsm_refc);
338         return lsm;
339 }
340
341 static inline bool lov_oinfo_is_dummy(const struct lov_oinfo *loi)
342 {
343         if (unlikely(loi->loi_oi.oi.oi_id == 0 &&
344                      loi->loi_oi.oi.oi_seq == 0 &&
345                      loi->loi_ost_idx == 0 &&
346                      loi->loi_ost_gen == 0))
347                 return true;
348
349         return false;
350 }
351
352 static inline struct obd_device *lov2obd(const struct lov_obd *lov)
353 {
354         return container_of0(lov, struct obd_device, u.lov);
355 }
356
357 static inline void lov_lsm2layout(struct lov_stripe_md *lsm,
358                                   struct lov_stripe_md_entry *lsme,
359                                   struct ost_layout *ol)
360 {
361         ol->ol_stripe_size = lsme->lsme_stripe_size;
362         ol->ol_stripe_count = lsme->lsme_stripe_count;
363         if (lsm->lsm_magic == LOV_MAGIC_COMP_V1) {
364                 ol->ol_comp_start = lsme->lsme_extent.e_start;
365                 ol->ol_comp_end = lsme->lsme_extent.e_end;
366                 ol->ol_comp_id = lsme->lsme_id;
367         } else {
368                 ol->ol_comp_start = 0;
369                 ol->ol_comp_end = 0;
370                 ol->ol_comp_id = 0;
371         }
372 }
373 #endif