Whamcloud - gitweb
LU-12580 lov: fix typo in lov_comp_md_size
[fs/lustre-release.git] / lustre / lov / lov_ea.c
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) 2007, 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  * lustre/lov/lov_ea.c
33  *
34  * Author: Wang Di <wangdi@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #include <linux/math64.h>
40 #include <linux/sort.h>
41 #include <libcfs/libcfs.h>
42
43 #include <obd_class.h>
44 #include "lov_internal.h"
45
46 static inline void
47 lu_extent_le_to_cpu(struct lu_extent *dst, const struct lu_extent *src)
48 {
49         dst->e_start = le64_to_cpu(src->e_start);
50         dst->e_end = le64_to_cpu(src->e_end);
51 }
52
53 /*
54  * Find minimum stripe maxbytes value.  For inactive or
55  * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES.
56  */
57 static loff_t lov_tgt_maxbytes(struct lov_tgt_desc *tgt)
58 {
59         struct obd_import *imp;
60         loff_t maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
61
62         if (!tgt->ltd_active)
63                 return maxbytes;
64
65         imp = tgt->ltd_obd->u.cli.cl_import;
66         if (!imp)
67                 return maxbytes;
68
69         spin_lock(&imp->imp_lock);
70         if ((imp->imp_state == LUSTRE_IMP_FULL ||
71             imp->imp_state == LUSTRE_IMP_IDLE) &&
72             (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
73             imp->imp_connect_data.ocd_maxbytes > 0)
74                 maxbytes = imp->imp_connect_data.ocd_maxbytes;
75
76         spin_unlock(&imp->imp_lock);
77
78         return maxbytes;
79 }
80
81 static int lsm_lmm_verify_v1v3(struct lov_mds_md *lmm, size_t lmm_size,
82                                u16 stripe_count)
83 {
84         if (stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
85                 CERROR("bad stripe count %d\n", stripe_count);
86                 lov_dump_lmm_common(D_WARNING, lmm);
87                 return -EINVAL;
88         }
89
90         if (lmm_oi_id(&lmm->lmm_oi) == 0) {
91                 CERROR("zero object id\n");
92                 lov_dump_lmm_common(D_WARNING, lmm);
93                 return -EINVAL;
94         }
95
96         if (lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_MDT &&
97             lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_RAID0) {
98                 CERROR("bad striping pattern\n");
99                 lov_dump_lmm_common(D_WARNING, lmm);
100                 return -EINVAL;
101         }
102
103         if (lmm->lmm_stripe_size == 0 ||
104             (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
105                 CERROR("bad stripe size %u\n",
106                        le32_to_cpu(lmm->lmm_stripe_size));
107                 lov_dump_lmm_common(D_WARNING, lmm);
108                 return -EINVAL;
109         }
110         return 0;
111 }
112
113 static void lsme_free(struct lov_stripe_md_entry *lsme)
114 {
115         unsigned int stripe_count = lsme->lsme_stripe_count;
116         unsigned int i;
117         size_t lsme_size;
118
119         if (!lsme_inited(lsme) ||
120             lsme->lsme_pattern & LOV_PATTERN_F_RELEASED)
121                 stripe_count = 0;
122         for (i = 0; i < stripe_count; i++)
123                 OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
124
125         lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
126         OBD_FREE_LARGE(lsme, lsme_size);
127 }
128
129 void lsm_free(struct lov_stripe_md *lsm)
130 {
131         unsigned int entry_count = lsm->lsm_entry_count;
132         unsigned int i;
133         size_t lsm_size;
134
135         for (i = 0; i < entry_count; i++)
136                 lsme_free(lsm->lsm_entries[i]);
137
138         lsm_size = offsetof(typeof(*lsm), lsm_entries[entry_count]);
139         OBD_FREE(lsm, lsm_size);
140 }
141
142 /**
143  * Unpack a struct lov_mds_md into a struct lov_stripe_md_entry.
144  *
145  * The caller should set id and extent.
146  */
147 static struct lov_stripe_md_entry *
148 lsme_unpack(struct lov_obd *lov, struct lov_mds_md *lmm, size_t buf_size,
149             const char *pool_name, bool inited, struct lov_ost_data_v1 *objects,
150             loff_t *maxbytes)
151 {
152         struct lov_stripe_md_entry *lsme;
153         size_t lsme_size;
154         loff_t min_stripe_maxbytes = 0;
155         loff_t lov_bytes;
156         u32 magic;
157         u32 pattern;
158         unsigned int stripe_count;
159         unsigned int i;
160         int rc;
161
162         magic = le32_to_cpu(lmm->lmm_magic);
163         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
164                 RETURN(ERR_PTR(-EINVAL));
165
166         pattern = le32_to_cpu(lmm->lmm_pattern);
167         if (pattern & LOV_PATTERN_F_RELEASED || !inited)
168                 stripe_count = 0;
169         else
170                 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
171
172         if (buf_size < (magic == LOV_MAGIC_V1 ? sizeof(struct lov_mds_md_v1) :
173                                                 sizeof(struct lov_mds_md_v3))) {
174                 CERROR("LOV EA %s too small: %zu, need %u\n",
175                        magic == LOV_MAGIC_V1 ? "V1" : "V3", buf_size,
176                        lov_mds_md_size(stripe_count, magic == LOV_MAGIC_V1 ?
177                                        LOV_MAGIC_V1 : LOV_MAGIC_V3));
178                 lov_dump_lmm_common(D_WARNING, lmm);
179                 return ERR_PTR(-EINVAL);
180         }
181
182         rc = lsm_lmm_verify_v1v3(lmm, buf_size, stripe_count);
183         if (rc < 0)
184                 return ERR_PTR(rc);
185
186         lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
187         OBD_ALLOC_LARGE(lsme, lsme_size);
188         if (!lsme)
189                 RETURN(ERR_PTR(-ENOMEM));
190
191         lsme->lsme_magic = magic;
192         lsme->lsme_pattern = pattern;
193         lsme->lsme_flags = 0;
194         lsme->lsme_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
195         /* preserve the possible -1 stripe count for uninstantiated component */
196         lsme->lsme_stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
197         lsme->lsme_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
198
199         if (pool_name) {
200                 size_t pool_name_len;
201
202                 pool_name_len = strlcpy(lsme->lsme_pool_name, pool_name,
203                                         sizeof(lsme->lsme_pool_name));
204                 if (pool_name_len >= sizeof(lsme->lsme_pool_name))
205                         GOTO(out_lsme, rc = -E2BIG);
206         }
207
208         /* with Data-on-MDT set maxbytes to stripe size */
209         if (lsme_is_dom(lsme)) {
210                 if (maxbytes) {
211                         lov_bytes = lsme->lsme_stripe_size;
212                         goto out_dom1;
213                 } else {
214                         goto out_dom2;
215                 }
216         }
217
218         for (i = 0; i < stripe_count; i++) {
219                 struct lov_oinfo *loi;
220                 struct lov_tgt_desc *ltd;
221
222                 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
223                 if (!loi)
224                         GOTO(out_lsme, rc = -ENOMEM);
225
226                 lsme->lsme_oinfo[i] = loi;
227
228                 ostid_le_to_cpu(&objects[i].l_ost_oi, &loi->loi_oi);
229                 loi->loi_ost_idx = le32_to_cpu(objects[i].l_ost_idx);
230                 loi->loi_ost_gen = le32_to_cpu(objects[i].l_ost_gen);
231                 if (lov_oinfo_is_dummy(loi))
232                         continue;
233
234                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count &&
235                     !lov2obd(lov)->obd_process_conf) {
236                         CERROR("%s: OST index %d more than OST count %d\n",
237                                (char*)lov->desc.ld_uuid.uuid,
238                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
239                         lov_dump_lmm_v1(D_WARNING, lmm);
240                         GOTO(out_lsme, rc = -EINVAL);
241                 }
242
243                 ltd = lov->lov_tgts[loi->loi_ost_idx];
244                 if (!ltd) {
245                         CERROR("%s: OST index %d missing\n",
246                                (char*)lov->desc.ld_uuid.uuid, loi->loi_ost_idx);
247                         lov_dump_lmm_v1(D_WARNING, lmm);
248                         continue;
249                 }
250
251                 lov_bytes = lov_tgt_maxbytes(ltd);
252                 if (min_stripe_maxbytes == 0 || lov_bytes < min_stripe_maxbytes)
253                         min_stripe_maxbytes = lov_bytes;
254         }
255
256         if (maxbytes) {
257                 if (min_stripe_maxbytes == 0)
258                         min_stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
259
260                 if (stripe_count == 0)
261                         stripe_count = lov->desc.ld_tgt_count;
262
263                 if (min_stripe_maxbytes <= LLONG_MAX / stripe_count)
264                         lov_bytes = min_stripe_maxbytes * stripe_count;
265                 else
266                         lov_bytes = MAX_LFS_FILESIZE;
267 out_dom1:
268                 *maxbytes = min_t(loff_t, lov_bytes, MAX_LFS_FILESIZE);
269         }
270 out_dom2:
271
272         return lsme;
273
274 out_lsme:
275         for (i = 0; i < stripe_count; i++) {
276                 struct lov_oinfo *loi = lsme->lsme_oinfo[i];
277
278                 if (loi)
279                         OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
280         }
281         OBD_FREE_LARGE(lsme, lsme_size);
282
283         return ERR_PTR(rc);
284 }
285
286 static struct
287 lov_stripe_md *lsm_unpackmd_v1v3(struct lov_obd *lov, struct lov_mds_md *lmm,
288                                  size_t buf_size, const char *pool_name,
289                                  struct lov_ost_data_v1 *objects)
290 {
291         struct lov_stripe_md *lsm;
292         struct lov_stripe_md_entry *lsme;
293         size_t lsm_size;
294         loff_t maxbytes;
295         u32 pattern;
296         int rc;
297
298         pattern = le32_to_cpu(lmm->lmm_pattern);
299
300         lsme = lsme_unpack(lov, lmm, buf_size, pool_name, true, objects,
301                            &maxbytes);
302         if (IS_ERR(lsme))
303                 RETURN(ERR_CAST(lsme));
304
305         lsme->lsme_flags = LCME_FL_INIT;
306         lsme->lsme_extent.e_start = 0;
307         lsme->lsme_extent.e_end = LUSTRE_EOF;
308
309         lsm_size = offsetof(typeof(*lsm), lsm_entries[1]);
310         OBD_ALLOC(lsm, lsm_size);
311         if (!lsm)
312                 GOTO(out_lsme, rc = -ENOMEM);
313
314         atomic_set(&lsm->lsm_refc, 1);
315         spin_lock_init(&lsm->lsm_lock);
316         lsm->lsm_maxbytes = maxbytes;
317         lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
318         lsm->lsm_magic = le32_to_cpu(lmm->lmm_magic);
319         lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
320         lsm->lsm_entry_count = 1;
321         lsm->lsm_is_released = pattern & LOV_PATTERN_F_RELEASED;
322         lsm->lsm_entries[0] = lsme;
323
324         return lsm;
325
326 out_lsme:
327         lsme_free(lsme);
328
329         return ERR_PTR(rc);
330 }
331
332 static inline struct lov_stripe_md *
333 lsm_unpackmd_v1(struct lov_obd *lov, void *buf, size_t buf_size)
334 {
335         struct lov_mds_md_v1 *lmm = buf;
336
337         return lsm_unpackmd_v1v3(lov, buf, buf_size, NULL, lmm->lmm_objects);
338 }
339
340 const struct lsm_operations lsm_v1_ops = {
341         .lsm_unpackmd           = lsm_unpackmd_v1,
342 };
343
344 static inline
345 struct lov_stripe_md *lsm_unpackmd_v3(struct lov_obd *lov, void *buf,
346                                       size_t buf_size)
347 {
348         struct lov_mds_md_v3 *lmm = buf;
349
350         return lsm_unpackmd_v1v3(lov, buf, buf_size, lmm->lmm_pool_name,
351                                  lmm->lmm_objects);
352 }
353
354 const struct lsm_operations lsm_v3_ops = {
355         .lsm_unpackmd           = lsm_unpackmd_v3,
356 };
357
358 static int lsm_verify_comp_md_v1(struct lov_comp_md_v1 *lcm,
359                                  size_t lcm_buf_size)
360 {
361         unsigned int entry_count;
362         unsigned int i;
363         size_t lcm_size;
364
365         lcm_size = le32_to_cpu(lcm->lcm_size);
366         if (lcm_buf_size < lcm_size) {
367                 CERROR("bad LCM buffer size %zu, expected %zu\n",
368                        lcm_buf_size, lcm_size);
369                 RETURN(-EINVAL);
370         }
371
372         entry_count = le16_to_cpu(lcm->lcm_entry_count);
373         for (i = 0; i < entry_count; i++) {
374                 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
375                 size_t blob_offset;
376                 size_t blob_size;
377
378                 blob_offset = le32_to_cpu(lcme->lcme_offset);
379                 blob_size = le32_to_cpu(lcme->lcme_size);
380
381                 if (lcm_size < blob_offset || lcm_size < blob_size ||
382                     lcm_size < blob_offset + blob_size) {
383                         CERROR("LCM entry %u has invalid blob: "
384                                "LCM size = %zu, offset = %zu, size = %zu\n",
385                                le32_to_cpu(lcme->lcme_id),
386                                lcm_size, blob_offset, blob_size);
387                         RETURN(-EINVAL);
388                 }
389         }
390
391         return 0;
392 }
393
394 static struct lov_stripe_md_entry *
395 lsme_unpack_comp(struct lov_obd *lov, struct lov_mds_md *lmm,
396                  size_t lmm_buf_size, bool inited, loff_t *maxbytes)
397 {
398         unsigned int magic;
399         unsigned int stripe_count;
400
401         stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
402         if (stripe_count == 0 &&
403             lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_MDT)
404                 RETURN(ERR_PTR(-EINVAL));
405         /* un-instantiated lmm contains no ost id info, i.e. lov_ost_data_v1 */
406         if (!inited)
407                 stripe_count = 0;
408
409         magic = le32_to_cpu(lmm->lmm_magic);
410         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
411                 RETURN(ERR_PTR(-EINVAL));
412
413         if (lmm_buf_size < lov_mds_md_size(stripe_count, magic))
414                 RETURN(ERR_PTR(-EINVAL));
415
416         if (magic == LOV_MAGIC_V1) {
417                 return lsme_unpack(lov, lmm, lmm_buf_size, NULL,
418                                    inited, lmm->lmm_objects, maxbytes);
419         } else {
420                 struct lov_mds_md_v3 *lmm3 = (struct lov_mds_md_v3 *)lmm;
421
422                 return lsme_unpack(lov, lmm, lmm_buf_size, lmm3->lmm_pool_name,
423                                    inited, lmm3->lmm_objects, maxbytes);
424         }
425 }
426
427 static struct lov_stripe_md *
428 lsm_unpackmd_comp_md_v1(struct lov_obd *lov, void *buf, size_t buf_size)
429 {
430         struct lov_comp_md_v1 *lcm = buf;
431         struct lov_stripe_md *lsm;
432         size_t lsm_size;
433         unsigned int entry_count = 0;
434         unsigned int i;
435         loff_t maxbytes;
436         int rc;
437
438         rc = lsm_verify_comp_md_v1(buf, buf_size);
439         if (rc < 0)
440                 return ERR_PTR(rc);
441
442         entry_count = le16_to_cpu(lcm->lcm_entry_count);
443
444         lsm_size = offsetof(typeof(*lsm), lsm_entries[entry_count]);
445         OBD_ALLOC(lsm, lsm_size);
446         if (!lsm)
447                 return ERR_PTR(-ENOMEM);
448
449         atomic_set(&lsm->lsm_refc, 1);
450         spin_lock_init(&lsm->lsm_lock);
451         lsm->lsm_magic = le32_to_cpu(lcm->lcm_magic);
452         lsm->lsm_layout_gen = le32_to_cpu(lcm->lcm_layout_gen);
453         lsm->lsm_entry_count = entry_count;
454         lsm->lsm_mirror_count = le16_to_cpu(lcm->lcm_mirror_count);
455         lsm->lsm_flags = le16_to_cpu(lcm->lcm_flags);
456         lsm->lsm_is_released = true;
457         lsm->lsm_maxbytes = LLONG_MIN;
458
459         for (i = 0; i < entry_count; i++) {
460                 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
461                 struct lov_stripe_md_entry *lsme;
462                 size_t blob_offset;
463                 size_t blob_size;
464                 void *blob;
465
466                 blob_offset = le32_to_cpu(lcme->lcme_offset);
467                 blob_size = le32_to_cpu(lcme->lcme_size);
468                 blob = (char *)lcm + blob_offset;
469
470                 lsme = lsme_unpack_comp(lov, blob, blob_size,
471                                         le32_to_cpu(lcme->lcme_flags) &
472                                         LCME_FL_INIT,
473                                         (i == entry_count - 1) ? &maxbytes :
474                                                                  NULL);
475                 if (IS_ERR(lsme))
476                         GOTO(out_lsm, rc = PTR_ERR(lsme));
477
478                 if (!(lsme->lsme_pattern & LOV_PATTERN_F_RELEASED))
479                         lsm->lsm_is_released = false;
480
481                 lsm->lsm_entries[i] = lsme;
482                 lsme->lsme_id = le32_to_cpu(lcme->lcme_id);
483                 lsme->lsme_flags = le32_to_cpu(lcme->lcme_flags);
484                 if (lsme->lsme_flags & LCME_FL_NOSYNC)
485                         lsme->lsme_timestamp =
486                                 le64_to_cpu(lcme->lcme_timestamp);
487                 lu_extent_le_to_cpu(&lsme->lsme_extent, &lcme->lcme_extent);
488
489                 if (i == entry_count - 1) {
490                         lsm->lsm_maxbytes = (loff_t)lsme->lsme_extent.e_start +
491                                             maxbytes;
492                         /*
493                          * the last component hasn't been defined, or
494                          * lsm_maxbytes overflowed.
495                          */
496                         if (!lsme_is_dom(lsme) &&
497                             (lsme->lsme_extent.e_end != LUSTRE_EOF ||
498                              lsm->lsm_maxbytes <
499                              (loff_t)lsme->lsme_extent.e_start))
500                                 lsm->lsm_maxbytes = MAX_LFS_FILESIZE;
501                 }
502         }
503
504         RETURN(lsm);
505
506 out_lsm:
507         for (i = 0; i < entry_count; i++)
508                 if (lsm->lsm_entries[i])
509                         lsme_free(lsm->lsm_entries[i]);
510
511         OBD_FREE(lsm, lsm_size);
512
513         RETURN(ERR_PTR(rc));
514 }
515
516 const struct lsm_operations lsm_comp_md_v1_ops = {
517         .lsm_unpackmd         = lsm_unpackmd_comp_md_v1,
518 };
519
520 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
521 {
522         int i, j;
523
524         CDEBUG(level, "lsm %p, objid "DOSTID", maxbytes %#llx, magic 0x%08X, "
525                "refc: %d, entry: %u, layout_gen %u\n",
526                lsm, POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
527                atomic_read(&lsm->lsm_refc), lsm->lsm_entry_count,
528                lsm->lsm_layout_gen);
529
530         for (i = 0; i < lsm->lsm_entry_count; i++) {
531                 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
532
533                 CDEBUG(level, DEXT ": id: %u, flags: %x, "
534                        "magic 0x%08X, layout_gen %u, "
535                        "stripe count %u, sstripe size %u, "
536                        "pool: ["LOV_POOLNAMEF"]\n",
537                        PEXT(&lse->lsme_extent), lse->lsme_id, lse->lsme_flags,
538                        lse->lsme_magic, lse->lsme_layout_gen,
539                        lse->lsme_stripe_count, lse->lsme_stripe_size,
540                        lse->lsme_pool_name);
541                 if (!lsme_inited(lse) ||
542                     lse->lsme_pattern & LOV_PATTERN_F_RELEASED)
543                         continue;
544                 for (j = 0; j < lse->lsme_stripe_count; j++) {
545                         CDEBUG(level, "   oinfo:%p: ostid: "DOSTID
546                                " ost idx: %d gen: %d\n",
547                                lse->lsme_oinfo[j],
548                                POSTID(&lse->lsme_oinfo[j]->loi_oi),
549                                lse->lsme_oinfo[j]->loi_ost_idx,
550                                lse->lsme_oinfo[j]->loi_ost_gen);
551                 }
552         }
553 }
554
555 int lov_lsm_entry(const struct lov_stripe_md *lsm, __u64 offset)
556 {
557         int i;
558
559         for (i = 0; i < lsm->lsm_entry_count; i++) {
560                 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
561
562                 if ((offset >= lse->lsme_extent.e_start &&
563                      offset < lse->lsme_extent.e_end) ||
564                     (offset == OBD_OBJECT_EOF &&
565                      lse->lsme_extent.e_end == OBD_OBJECT_EOF))
566                         return i;
567         }
568
569         return -1;
570 }