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