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