Whamcloud - gitweb
acf6c559d0ef36f94868ddc56bd49592d9eaf089
[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  *
31  * lustre/lov/lov_ea.c
32  *
33  * Author: Wang Di <wangdi@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_LOV
37
38 #include <linux/math64.h>
39 #include <linux/sort.h>
40 #include <libcfs/libcfs.h>
41
42 #include <obd_class.h>
43 #include "lov_internal.h"
44
45 static inline void
46 lu_extent_le_to_cpu(struct lu_extent *dst, const struct lu_extent *src)
47 {
48         dst->e_start = le64_to_cpu(src->e_start);
49         dst->e_end = le64_to_cpu(src->e_end);
50 }
51
52 /*
53  * Find minimum stripe maxbytes value.  For inactive or
54  * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES.
55  */
56 static loff_t lov_tgt_maxbytes(struct lov_tgt_desc *tgt)
57 {
58         struct obd_import *imp;
59         loff_t maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
60
61         if (!tgt->ltd_active)
62                 return maxbytes;
63
64         imp = tgt->ltd_obd->u.cli.cl_import;
65         if (!imp)
66                 return maxbytes;
67
68         spin_lock(&imp->imp_lock);
69         if ((imp->imp_state == LUSTRE_IMP_FULL ||
70             imp->imp_state == LUSTRE_IMP_IDLE) &&
71             (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
72             imp->imp_connect_data.ocd_maxbytes > 0)
73                 maxbytes = imp->imp_connect_data.ocd_maxbytes;
74
75         spin_unlock(&imp->imp_lock);
76
77         return maxbytes;
78 }
79
80 static int lsm_lmm_verify_v1v3(struct lov_mds_md *lmm, size_t lmm_size,
81                                u16 stripe_count)
82 {
83         u32 pattern = le32_to_cpu(lmm->lmm_pattern);
84         int rc = 0;
85
86         if (stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
87                 rc = -EINVAL;
88                 CERROR("lov: bad stripe count %d: rc = %d\n",
89                        stripe_count, rc);
90                 lov_dump_lmm_common(D_WARNING, lmm);
91                 goto out;
92         }
93
94         if (lmm_oi_id(&lmm->lmm_oi) == 0) {
95                 rc = -EINVAL;
96                 CERROR("lov: zero object id: rc = %d\n", rc);
97                 lov_dump_lmm_common(D_WARNING, lmm);
98                 goto out;
99         }
100
101         if (!lov_pattern_supported(lov_pattern(pattern))) {
102                 static int nr;
103                 static ktime_t time2_clear_nr;
104                 ktime_t now = ktime_get();
105
106                 /* limit this message 20 times within 24h */
107                 if (ktime_after(now, time2_clear_nr)) {
108                         nr = 0;
109                         time2_clear_nr = ktime_add_ms(now,
110                                                       24 * 3600 * MSEC_PER_SEC);
111                 }
112                 if (nr++ < 20) {
113                         CWARN("lov: unrecognized striping pattern: rc = %d\n",
114                               rc);
115                         lov_dump_lmm_common(D_WARNING, lmm);
116                 }
117                 goto out;
118         }
119
120         if (lmm->lmm_stripe_size == 0 ||
121             (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
122                 rc = -EINVAL;
123                 CERROR("lov: bad stripe size %u: rc = %d\n",
124                        le32_to_cpu(lmm->lmm_stripe_size), rc);
125                 lov_dump_lmm_common(D_WARNING, lmm);
126                 goto out;
127         }
128
129 out:
130         return rc;
131 }
132
133 static void lsme_free(struct lov_stripe_md_entry *lsme)
134 {
135         unsigned int stripe_count;
136         unsigned int i;
137         size_t lsme_size;
138
139         if (lsme->lsme_magic == LOV_MAGIC_FOREIGN) {
140                 /*
141                  * TODO: In addition to HSM foreign layout, It needs to add
142                  * support for other kinds of foreign layout types such as
143                  * DAOS, S3. When add these supports, it will use non-inline
144                  * @lov_hsm_base to store layout information, and need to
145                  * free extra allocated buffer.
146                  */
147                 OBD_FREE_LARGE(lsme, sizeof(*lsme));
148                 return;
149         }
150
151         stripe_count = lsme->lsme_stripe_count;
152         if (!lsme_inited(lsme) ||
153             lsme->lsme_pattern & LOV_PATTERN_F_RELEASED ||
154             !lov_supported_comp_magic(lsme->lsme_magic) ||
155             !lov_pattern_supported(lov_pattern(lsme->lsme_pattern)))
156                 stripe_count = 0;
157         for (i = 0; i < stripe_count; i++)
158                 OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
159
160         lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
161         OBD_FREE_LARGE(lsme, lsme_size);
162 }
163
164 void lsm_free(struct lov_stripe_md *lsm)
165 {
166         unsigned int entry_count = lsm->lsm_entry_count;
167         unsigned int i;
168         size_t lsm_size;
169
170         if (lsm->lsm_magic == LOV_MAGIC_FOREIGN) {
171                 OBD_FREE_LARGE(lsm_foreign(lsm), lsm->lsm_foreign_size);
172         } else {
173                 for (i = 0; i < entry_count; i++)
174                         lsme_free(lsm->lsm_entries[i]);
175         }
176
177         lsm_size = lsm->lsm_magic == LOV_MAGIC_FOREIGN ?
178                    offsetof(typeof(*lsm), lsm_entries[1]) :
179                    offsetof(typeof(*lsm), lsm_entries[entry_count]);
180         OBD_FREE(lsm, lsm_size);
181 }
182
183 /**
184  * Unpack a struct lov_mds_md into a struct lov_stripe_md_entry.
185  *
186  * The caller should set id and extent.
187  */
188 static struct lov_stripe_md_entry *
189 lsme_unpack(struct lov_obd *lov, struct lov_mds_md *lmm, size_t buf_size,
190             const char *pool_name, bool inited, struct lov_ost_data_v1 *objects,
191             loff_t *maxbytes)
192 {
193         struct lov_stripe_md_entry *lsme;
194         size_t lsme_size;
195         loff_t min_stripe_maxbytes = 0;
196         loff_t lov_bytes;
197         u32 magic;
198         u32 pattern;
199         unsigned int stripe_count;
200         unsigned int i;
201         int rc;
202
203         magic = le32_to_cpu(lmm->lmm_magic);
204         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
205                 RETURN(ERR_PTR(-EINVAL));
206
207         pattern = le32_to_cpu(lmm->lmm_pattern);
208         if (pattern & LOV_PATTERN_F_RELEASED || !inited ||
209             !lov_pattern_supported(lov_pattern(pattern)))
210                 stripe_count = 0;
211         else
212                 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
213
214         if (buf_size < lov_mds_md_size(stripe_count, magic)) {
215                 CERROR("LOV EA %s too small: %zu, need %u\n",
216                        magic == LOV_MAGIC_V1 ? "V1" : "V3", buf_size,
217                        lov_mds_md_size(stripe_count, magic == LOV_MAGIC_V1 ?
218                                        LOV_MAGIC_V1 : LOV_MAGIC_V3));
219                 lov_dump_lmm_common(D_WARNING, lmm);
220                 return ERR_PTR(-EINVAL);
221         }
222
223         rc = lsm_lmm_verify_v1v3(lmm, buf_size, stripe_count);
224         if (rc < 0)
225                 return ERR_PTR(rc);
226
227         lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
228         OBD_ALLOC_LARGE(lsme, lsme_size);
229         if (!lsme)
230                 RETURN(ERR_PTR(-ENOMEM));
231
232         lsme->lsme_magic = magic;
233         lsme->lsme_pattern = pattern;
234         lsme->lsme_flags = 0;
235         lsme->lsme_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
236         /* preserve the possible -1 stripe count for uninstantiated component */
237         lsme->lsme_stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
238         lsme->lsme_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
239
240         if (pool_name) {
241                 size_t pool_name_len;
242
243                 pool_name_len = strlcpy(lsme->lsme_pool_name, pool_name,
244                                         sizeof(lsme->lsme_pool_name));
245                 if (pool_name_len >= sizeof(lsme->lsme_pool_name))
246                         GOTO(out_lsme, rc = -E2BIG);
247         }
248
249         /* with Data-on-MDT set maxbytes to stripe size */
250         if (lsme_is_dom(lsme)) {
251                 if (maxbytes) {
252                         lov_bytes = lsme->lsme_stripe_size;
253                         goto out_dom1;
254                 } else {
255                         goto out_dom2;
256                 }
257         }
258
259         for (i = 0; i < stripe_count; i++) {
260                 struct lov_oinfo *loi;
261                 struct lov_tgt_desc *ltd;
262
263                 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
264                 if (!loi)
265                         GOTO(out_lsme, rc = -ENOMEM);
266
267                 lsme->lsme_oinfo[i] = loi;
268
269                 ostid_le_to_cpu(&objects[i].l_ost_oi, &loi->loi_oi);
270                 loi->loi_ost_idx = le32_to_cpu(objects[i].l_ost_idx);
271                 loi->loi_ost_gen = le32_to_cpu(objects[i].l_ost_gen);
272                 if (lov_oinfo_is_dummy(loi))
273                         continue;
274
275                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count &&
276                     !lov2obd(lov)->obd_process_conf) {
277                         CERROR("%s: OST index %d more than OST count %d\n",
278                                (char*)lov->desc.ld_uuid.uuid,
279                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
280                         lov_dump_lmm_v1(D_WARNING, lmm);
281                         GOTO(out_lsme, rc = -EINVAL);
282                 }
283
284                 ltd = lov->lov_tgts[loi->loi_ost_idx];
285                 if (!ltd) {
286                         CERROR("%s: OST index %d missing\n",
287                                (char*)lov->desc.ld_uuid.uuid, loi->loi_ost_idx);
288                         lov_dump_lmm_v1(D_WARNING, lmm);
289                         continue;
290                 }
291
292                 lov_bytes = lov_tgt_maxbytes(ltd);
293                 if (min_stripe_maxbytes == 0 || lov_bytes < min_stripe_maxbytes)
294                         min_stripe_maxbytes = lov_bytes;
295         }
296
297         if (maxbytes) {
298                 if (min_stripe_maxbytes == 0)
299                         min_stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
300
301                 if (stripe_count == 0)
302                         stripe_count = lov->desc.ld_tgt_count;
303
304                 if (min_stripe_maxbytes <= LLONG_MAX / stripe_count)
305                         lov_bytes = min_stripe_maxbytes * stripe_count;
306                 else
307                         lov_bytes = MAX_LFS_FILESIZE;
308 out_dom1:
309                 *maxbytes = min_t(loff_t, lov_bytes, MAX_LFS_FILESIZE);
310         }
311 out_dom2:
312
313         return lsme;
314
315 out_lsme:
316         for (i = 0; i < stripe_count; i++) {
317                 struct lov_oinfo *loi = lsme->lsme_oinfo[i];
318
319                 if (loi)
320                         OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
321         }
322         OBD_FREE_LARGE(lsme, lsme_size);
323
324         return ERR_PTR(rc);
325 }
326
327 static struct
328 lov_stripe_md *lsm_unpackmd_v1v3(struct lov_obd *lov, struct lov_mds_md *lmm,
329                                  size_t buf_size, const char *pool_name,
330                                  struct lov_ost_data_v1 *objects)
331 {
332         struct lov_stripe_md *lsm;
333         struct lov_stripe_md_entry *lsme;
334         size_t lsm_size;
335         loff_t maxbytes;
336         u32 pattern;
337         int rc;
338
339         pattern = le32_to_cpu(lmm->lmm_pattern);
340
341         lsme = lsme_unpack(lov, lmm, buf_size, pool_name, true, objects,
342                            &maxbytes);
343         if (IS_ERR(lsme))
344                 RETURN(ERR_CAST(lsme));
345
346         lsme->lsme_flags = LCME_FL_INIT;
347         lsme->lsme_extent.e_start = 0;
348         lsme->lsme_extent.e_end = LUSTRE_EOF;
349
350         lsm_size = offsetof(typeof(*lsm), lsm_entries[1]);
351         OBD_ALLOC(lsm, lsm_size);
352         if (!lsm)
353                 GOTO(out_lsme, rc = -ENOMEM);
354
355         atomic_set(&lsm->lsm_refc, 1);
356         spin_lock_init(&lsm->lsm_lock);
357         lsm->lsm_maxbytes = maxbytes;
358         lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
359         lsm->lsm_magic = le32_to_cpu(lmm->lmm_magic);
360         lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
361         lsm->lsm_entry_count = 1;
362         lsm->lsm_is_released = pattern & LOV_PATTERN_F_RELEASED;
363         lsm->lsm_entries[0] = lsme;
364
365         return lsm;
366
367 out_lsme:
368         lsme_free(lsme);
369
370         return ERR_PTR(rc);
371 }
372
373 static struct lov_stripe_md *
374 lsm_unpackmd_v1(struct lov_obd *lov, void *buf, size_t buf_size)
375 {
376         struct lov_mds_md_v1 *lmm = buf;
377
378         return lsm_unpackmd_v1v3(lov, buf, buf_size, NULL, lmm->lmm_objects);
379 }
380
381 static const struct lsm_operations lsm_v1_ops = {
382         .lsm_unpackmd           = lsm_unpackmd_v1,
383 };
384
385 static struct lov_stripe_md *
386 lsm_unpackmd_v3(struct lov_obd *lov, void *buf, size_t buf_size)
387 {
388         struct lov_mds_md_v3 *lmm = buf;
389
390         return lsm_unpackmd_v1v3(lov, buf, buf_size, lmm->lmm_pool_name,
391                                  lmm->lmm_objects);
392 }
393
394 static const struct lsm_operations lsm_v3_ops = {
395         .lsm_unpackmd           = lsm_unpackmd_v3,
396 };
397
398 static int lsm_verify_comp_md_v1(struct lov_comp_md_v1 *lcm,
399                                  size_t lcm_buf_size)
400 {
401         unsigned int entry_count;
402         unsigned int i;
403         size_t lcm_size;
404
405         lcm_size = le32_to_cpu(lcm->lcm_size);
406         if (lcm_buf_size < lcm_size) {
407                 CERROR("bad LCM buffer size %zu, expected %zu\n",
408                        lcm_buf_size, lcm_size);
409                 RETURN(-EINVAL);
410         }
411
412         entry_count = le16_to_cpu(lcm->lcm_entry_count);
413         for (i = 0; i < entry_count; i++) {
414                 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
415                 size_t blob_offset;
416                 size_t blob_size;
417
418                 blob_offset = le32_to_cpu(lcme->lcme_offset);
419                 blob_size = le32_to_cpu(lcme->lcme_size);
420
421                 if (lcm_size < blob_offset || lcm_size < blob_size ||
422                     lcm_size < blob_offset + blob_size) {
423                         CERROR("LCM entry %u has invalid blob: "
424                                "LCM size = %zu, offset = %zu, size = %zu\n",
425                                le32_to_cpu(lcme->lcme_id),
426                                lcm_size, blob_offset, blob_size);
427                         RETURN(-EINVAL);
428                 }
429         }
430
431         return 0;
432 }
433
434 static struct lov_stripe_md_entry *
435 lsme_unpack_foreign(struct lov_obd *lov, void *buf, size_t buf_size,
436                     bool inited, loff_t *maxbytes)
437 {
438         struct lov_stripe_md_entry *lsme;
439         struct lov_foreign_md *lfm = buf;
440         __u32 magic;
441
442         ENTRY;
443
444         magic = le32_to_cpu(lfm->lfm_magic);
445         if (magic != LOV_MAGIC_FOREIGN)
446                 RETURN(ERR_PTR(-EINVAL));
447
448         OBD_ALLOC_LARGE(lsme, sizeof(*lsme));
449         if (!lsme)
450                 RETURN(ERR_PTR(-ENOMEM));
451
452         lsme->lsme_magic = magic;
453         lsme->lsme_pattern = LOV_PATTERN_FOREIGN;
454         lsme->lsme_flags = 0;
455
456         if (maxbytes)
457                 *maxbytes = MAX_LFS_FILESIZE;
458
459         RETURN(lsme);
460 }
461
462 static struct lov_stripe_md_entry *
463 lsme_unpack_comp(struct lov_obd *lov, struct lov_mds_md *lmm,
464                  size_t lmm_buf_size, bool inited, loff_t *maxbytes)
465 {
466         unsigned int magic;
467
468         magic = le32_to_cpu(lmm->lmm_magic);
469         if (!lov_supported_comp_magic(magic)) {
470                 struct lov_stripe_md_entry *lsme;
471
472                 /* allocate a lsme holder for invalid magic lmm */
473                 OBD_ALLOC_LARGE(lsme, offsetof(typeof(*lsme), lsme_oinfo[0]));
474                 lsme->lsme_magic = magic;
475                 lsme->lsme_pattern = le32_to_cpu(lmm->lmm_pattern);
476
477                 return lsme;
478         }
479
480         if (magic != LOV_MAGIC_FOREIGN &&
481             le16_to_cpu(lmm->lmm_stripe_count) == 0 &&
482             !(lov_pattern(le32_to_cpu(lmm->lmm_pattern)) & LOV_PATTERN_MDT))
483                 RETURN(ERR_PTR(-EINVAL));
484
485         if (magic == LOV_MAGIC_V1) {
486                 return lsme_unpack(lov, lmm, lmm_buf_size, NULL,
487                                    inited, lmm->lmm_objects, maxbytes);
488         } else if (magic == LOV_MAGIC_V3) {
489                 struct lov_mds_md_v3 *lmm3 = (struct lov_mds_md_v3 *)lmm;
490
491                 return lsme_unpack(lov, lmm, lmm_buf_size, lmm3->lmm_pool_name,
492                                    inited, lmm3->lmm_objects, maxbytes);
493         } else { /* LOV_MAGIC_FOREIGN */
494                 return lsme_unpack_foreign(lov, lmm, lmm_buf_size,
495                                            inited, maxbytes);
496         }
497 }
498
499 static struct lov_stripe_md *
500 lsm_unpackmd_comp_md_v1(struct lov_obd *lov, void *buf, size_t buf_size)
501 {
502         struct lov_comp_md_v1 *lcm = buf;
503         struct lov_stripe_md *lsm;
504         size_t lsm_size;
505         unsigned int entry_count = 0;
506         unsigned int i;
507         loff_t maxbytes;
508         int rc;
509
510         rc = lsm_verify_comp_md_v1(buf, buf_size);
511         if (rc < 0)
512                 return ERR_PTR(rc);
513
514         entry_count = le16_to_cpu(lcm->lcm_entry_count);
515
516         lsm_size = offsetof(typeof(*lsm), lsm_entries[entry_count]);
517         OBD_ALLOC(lsm, lsm_size);
518         if (!lsm)
519                 return ERR_PTR(-ENOMEM);
520
521         atomic_set(&lsm->lsm_refc, 1);
522         spin_lock_init(&lsm->lsm_lock);
523         lsm->lsm_magic = le32_to_cpu(lcm->lcm_magic);
524         lsm->lsm_layout_gen = le32_to_cpu(lcm->lcm_layout_gen);
525         lsm->lsm_entry_count = entry_count;
526         lsm->lsm_mirror_count = le16_to_cpu(lcm->lcm_mirror_count);
527         lsm->lsm_flags = le16_to_cpu(lcm->lcm_flags);
528         lsm->lsm_is_released = true;
529         lsm->lsm_maxbytes = LLONG_MIN;
530
531         for (i = 0; i < entry_count; i++) {
532                 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
533                 struct lov_stripe_md_entry *lsme;
534                 size_t blob_offset;
535                 size_t blob_size;
536                 void *blob;
537
538                 blob_offset = le32_to_cpu(lcme->lcme_offset);
539                 blob_size = le32_to_cpu(lcme->lcme_size);
540                 blob = (char *)lcm + blob_offset;
541
542                 if (unlikely(CFS_FAIL_CHECK(OBD_FAIL_LOV_COMP_MAGIC) &&
543                              (cfs_fail_val == i + 1)))
544                         ((struct lov_mds_md *)blob)->lmm_magic = LOV_MAGIC_BAD;
545
546                 if (unlikely(CFS_FAIL_CHECK(OBD_FAIL_LOV_COMP_PATTERN) &&
547                              (cfs_fail_val == i + 1))) {
548                         ((struct lov_mds_md *)blob)->lmm_pattern =
549                                                                 LOV_PATTERN_BAD;
550                 }
551
552                 lsme = lsme_unpack_comp(lov, blob, blob_size,
553                                         le32_to_cpu(lcme->lcme_flags) &
554                                         LCME_FL_INIT,
555                                         (i == entry_count - 1) ? &maxbytes :
556                                                                  NULL);
557                 if (IS_ERR(lsme))
558                         GOTO(out_lsm, rc = PTR_ERR(lsme));
559
560                 /**
561                  * pressume that unrecognized magic component also has valid
562                  * lsme_id/lsme_flags/lsme_extent
563                  */
564                 if (!(lsme->lsme_pattern & LOV_PATTERN_F_RELEASED))
565                         lsm->lsm_is_released = false;
566
567                 lsm->lsm_entries[i] = lsme;
568                 lsme->lsme_id = le32_to_cpu(lcme->lcme_id);
569                 lsme->lsme_flags = le32_to_cpu(lcme->lcme_flags);
570                 if (lsme->lsme_flags & LCME_FL_NOSYNC)
571                         lsme->lsme_timestamp =
572                                 le64_to_cpu(lcme->lcme_timestamp);
573                 lu_extent_le_to_cpu(&lsme->lsme_extent, &lcme->lcme_extent);
574
575                 if (i == entry_count - 1) {
576                         lsm->lsm_maxbytes = (loff_t)lsme->lsme_extent.e_start +
577                                             maxbytes;
578                         /*
579                          * the last component hasn't been defined, or
580                          * lsm_maxbytes overflowed.
581                          */
582                         if (!lsme_is_dom(lsme) &&
583                             (lsme->lsme_extent.e_end != LUSTRE_EOF ||
584                              lsm->lsm_maxbytes <
585                              (loff_t)lsme->lsme_extent.e_start))
586                                 lsm->lsm_maxbytes = MAX_LFS_FILESIZE;
587                 }
588         }
589
590         RETURN(lsm);
591
592 out_lsm:
593         for (i = 0; i < entry_count; i++)
594                 if (lsm->lsm_entries[i])
595                         lsme_free(lsm->lsm_entries[i]);
596
597         OBD_FREE(lsm, lsm_size);
598
599         RETURN(ERR_PTR(rc));
600 }
601
602 static const struct lsm_operations lsm_comp_md_v1_ops = {
603         .lsm_unpackmd           = lsm_unpackmd_comp_md_v1,
604 };
605
606 static struct
607 lov_stripe_md *lsm_unpackmd_foreign(struct lov_obd *lov, void *buf,
608                                     size_t buf_size)
609 {
610         struct lov_foreign_md *lfm = buf;
611         struct lov_stripe_md *lsm;
612         size_t lsm_size;
613         struct lov_stripe_md_entry *lsme;
614
615         lsm_size = offsetof(typeof(*lsm), lsm_entries[1]);
616         OBD_ALLOC(lsm, lsm_size);
617         if (lsm == NULL)
618                 RETURN(ERR_PTR(-ENOMEM));
619
620         atomic_set(&lsm->lsm_refc, 1);
621         spin_lock_init(&lsm->lsm_lock);
622         lsm->lsm_magic = le32_to_cpu(lfm->lfm_magic);
623         lsm->lsm_foreign_size = foreign_size_le(lfm);
624
625         /* alloc for full foreign EA including format fields */
626         OBD_ALLOC_LARGE(lsme, lsm->lsm_foreign_size);
627         if (lsme == NULL) {
628                 OBD_FREE(lsm, lsm_size);
629                 RETURN(ERR_PTR(-ENOMEM));
630         }
631
632         /* copy full foreign EA including format fields */
633         memcpy(lsme, buf, lsm->lsm_foreign_size);
634
635         lsm_foreign(lsm) = lsme;
636
637         return lsm;
638 }
639
640 static const struct lsm_operations lsm_foreign_ops = {
641         .lsm_unpackmd           = lsm_unpackmd_foreign,
642 };
643
644 const struct lsm_operations *lsm_op_find(int magic)
645 {
646         switch (magic) {
647         case LOV_MAGIC_V1:
648                 return &lsm_v1_ops;
649         case LOV_MAGIC_V3:
650                 return &lsm_v3_ops;
651         case LOV_MAGIC_COMP_V1:
652                 return &lsm_comp_md_v1_ops;
653         case LOV_MAGIC_FOREIGN:
654                 return &lsm_foreign_ops;
655         default:
656                 CERROR("unrecognized lsm_magic %08x\n", magic);
657                 return NULL;
658         }
659 }
660
661 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
662 {
663         int i, j;
664
665         CDEBUG_LIMIT(level,
666                      "lsm %p, objid "DOSTID", maxbytes %#llx, magic 0x%08X, refc: %d, entry: %u, mirror: %u, flags: %u,layout_gen %u\n",
667                lsm, POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
668                atomic_read(&lsm->lsm_refc), lsm->lsm_entry_count,
669                lsm->lsm_mirror_count, lsm->lsm_flags, lsm->lsm_layout_gen);
670
671         if (lsm->lsm_magic == LOV_MAGIC_FOREIGN) {
672                 struct lov_foreign_md *lfm = (void *)lsm_foreign(lsm);
673
674                 CDEBUG_LIMIT(level,
675                              "foreign LOV EA, magic %x, length %u, type %x, flags %x, value '%.*s'\n",
676                        lfm->lfm_magic, lfm->lfm_length, lfm->lfm_type,
677                        lfm->lfm_flags, lfm->lfm_length, lfm->lfm_value);
678                 return;
679         }
680
681         for (i = 0; i < lsm->lsm_entry_count; i++) {
682                 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
683
684                 CDEBUG(level, DEXT ": id: %u, flags: %x, "
685                        "magic 0x%08X, layout_gen %u, "
686                        "stripe count %u, sstripe size %u, "
687                        "pool: ["LOV_POOLNAMEF"]\n",
688                        PEXT(&lse->lsme_extent), lse->lsme_id, lse->lsme_flags,
689                        lse->lsme_magic, lse->lsme_layout_gen,
690                        lse->lsme_stripe_count, lse->lsme_stripe_size,
691                        lse->lsme_pool_name);
692                 if (!lsme_inited(lse) ||
693                     lse->lsme_pattern & LOV_PATTERN_F_RELEASED ||
694                     !lov_supported_comp_magic(lse->lsme_magic) ||
695                     !lov_pattern_supported(lov_pattern(lse->lsme_pattern)))
696                         continue;
697                 for (j = 0; j < lse->lsme_stripe_count; j++) {
698                         CDEBUG(level, "   oinfo:%p: ostid: "DOSTID
699                                " ost idx: %d gen: %d\n",
700                                lse->lsme_oinfo[j],
701                                POSTID(&lse->lsme_oinfo[j]->loi_oi),
702                                lse->lsme_oinfo[j]->loi_ost_idx,
703                                lse->lsme_oinfo[j]->loi_ost_gen);
704                 }
705         }
706 }
707
708 int lov_lsm_entry(const struct lov_stripe_md *lsm, __u64 offset)
709 {
710         int i;
711
712         for (i = 0; i < lsm->lsm_entry_count; i++) {
713                 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
714
715                 if ((offset >= lse->lsme_extent.e_start &&
716                      offset < lse->lsme_extent.e_end) ||
717                     (offset == OBD_OBJECT_EOF &&
718                      lse->lsme_extent.e_end == OBD_OBJECT_EOF))
719                         return i;
720         }
721
722         return -1;
723 }
724
725 /**
726  * lmm_layout_gen overlaps stripe_offset field, it needs to be reset back when
727  * sending to MDT for passing striping checks
728  */
729 void lov_fix_ea_for_replay(void *lovea)
730 {
731         struct lov_user_md *lmm = lovea;
732         struct lov_comp_md_v1 *c1;
733         int i;
734
735         switch (le32_to_cpu(lmm->lmm_magic)) {
736         case LOV_USER_MAGIC_V1:
737         case LOV_USER_MAGIC_V3:
738                 lmm->lmm_stripe_offset = LOV_OFFSET_DEFAULT;
739                 break;
740
741         case LOV_USER_MAGIC_COMP_V1:
742                 c1 = (void *)lmm;
743                 for (i = 0; i < le16_to_cpu(c1->lcm_entry_count); i++) {
744                         struct lov_comp_md_entry_v1 *ent = &c1->lcm_entries[i];
745
746                         if (le32_to_cpu(ent->lcme_flags) & LCME_FL_INIT) {
747                                 lmm = (void *)((char *)c1 +
748                                       le32_to_cpu(ent->lcme_offset));
749                                 lmm->lmm_stripe_offset = LOV_OFFSET_DEFAULT;
750                         }
751                 }
752         }
753 }
754 EXPORT_SYMBOL(lov_fix_ea_for_replay);