Linux-libre 4.14.145-gnu
[librecmc/linux-libre.git] / drivers / staging / lustre / 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, 2015, 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 <asm/div64.h>
40 #include <linux/libcfs/libcfs.h>
41
42 #include <obd_class.h>
43 #include <uapi/linux/lustre/lustre_idl.h>
44
45 #include "lov_internal.h"
46
47 static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
48                                  __u16 stripe_count)
49 {
50         if (stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
51                 CERROR("bad stripe count %d\n", stripe_count);
52                 lov_dump_lmm_common(D_WARNING, lmm);
53                 return -EINVAL;
54         }
55
56         if (lmm_oi_id(&lmm->lmm_oi) == 0) {
57                 CERROR("zero object id\n");
58                 lov_dump_lmm_common(D_WARNING, lmm);
59                 return -EINVAL;
60         }
61
62         if (lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_RAID0) {
63                 CERROR("bad striping pattern\n");
64                 lov_dump_lmm_common(D_WARNING, lmm);
65                 return -EINVAL;
66         }
67
68         if (lmm->lmm_stripe_size == 0 ||
69             (le32_to_cpu(lmm->lmm_stripe_size) &
70              (LOV_MIN_STRIPE_SIZE - 1)) != 0) {
71                 CERROR("bad stripe size %u\n",
72                        le32_to_cpu(lmm->lmm_stripe_size));
73                 lov_dump_lmm_common(D_WARNING, lmm);
74                 return -EINVAL;
75         }
76         return 0;
77 }
78
79 struct lov_stripe_md *lsm_alloc_plain(u16 stripe_count)
80 {
81         size_t oinfo_ptrs_size, lsm_size;
82         struct lov_stripe_md *lsm;
83         struct lov_oinfo     *loi;
84         int i;
85
86         LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
87
88         oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
89         lsm_size = sizeof(*lsm) + oinfo_ptrs_size;
90
91         lsm = libcfs_kvzalloc(lsm_size, GFP_NOFS);
92         if (!lsm)
93                 return NULL;
94
95         for (i = 0; i < stripe_count; i++) {
96                 loi = kmem_cache_zalloc(lov_oinfo_slab, GFP_NOFS);
97                 if (!loi)
98                         goto err;
99                 lsm->lsm_oinfo[i] = loi;
100         }
101         lsm->lsm_stripe_count = stripe_count;
102         return lsm;
103
104 err:
105         while (--i >= 0)
106                 kmem_cache_free(lov_oinfo_slab, lsm->lsm_oinfo[i]);
107         kvfree(lsm);
108         return NULL;
109 }
110
111 void lsm_free_plain(struct lov_stripe_md *lsm)
112 {
113         __u16 stripe_count = lsm->lsm_stripe_count;
114         int i;
115
116         for (i = 0; i < stripe_count; i++)
117                 kmem_cache_free(lov_oinfo_slab, lsm->lsm_oinfo[i]);
118         kvfree(lsm);
119 }
120
121 /*
122  * Find minimum stripe maxbytes value.  For inactive or
123  * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES.
124  */
125 static loff_t lov_tgt_maxbytes(struct lov_tgt_desc *tgt)
126 {
127         loff_t maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
128         struct obd_import *imp;
129
130         if (!tgt->ltd_active)
131                 return maxbytes;
132
133         imp = tgt->ltd_obd->u.cli.cl_import;
134         if (!imp)
135                 return maxbytes;
136
137         spin_lock(&imp->imp_lock);
138         if (imp->imp_state == LUSTRE_IMP_FULL &&
139             (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
140              imp->imp_connect_data.ocd_maxbytes > 0)
141                 maxbytes = imp->imp_connect_data.ocd_maxbytes;
142
143         spin_unlock(&imp->imp_lock);
144
145         return maxbytes;
146 }
147
148 static int lsm_unpackmd_common(struct lov_obd *lov,
149                                struct lov_stripe_md *lsm,
150                                struct lov_mds_md *lmm,
151                                struct lov_ost_data_v1 *objects)
152 {
153         loff_t min_stripe_maxbytes = 0;
154         unsigned int stripe_count;
155         struct lov_oinfo *loi;
156         loff_t lov_bytes;
157         unsigned int i;
158
159         /*
160          * This supposes lov_mds_md_v1/v3 first fields are
161          * are the same
162          */
163         lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
164         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
165         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
166         lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
167         lsm->lsm_pool_name[0] = '\0';
168
169         stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
170
171         for (i = 0; i < stripe_count; i++) {
172                 loi = lsm->lsm_oinfo[i];
173                 ostid_le_to_cpu(&objects[i].l_ost_oi, &loi->loi_oi);
174                 loi->loi_ost_idx = le32_to_cpu(objects[i].l_ost_idx);
175                 loi->loi_ost_gen = le32_to_cpu(objects[i].l_ost_gen);
176                 if (lov_oinfo_is_dummy(loi))
177                         continue;
178
179                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count &&
180                     !lov2obd(lov)->obd_process_conf) {
181                         CERROR("%s: OST index %d more than OST count %d\n",
182                                (char *)lov->desc.ld_uuid.uuid,
183                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
184                         lov_dump_lmm_v1(D_WARNING, lmm);
185                         return -EINVAL;
186                 }
187
188                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
189                         CERROR("%s: OST index %d missing\n",
190                                (char *)lov->desc.ld_uuid.uuid,
191                                loi->loi_ost_idx);
192                         lov_dump_lmm_v1(D_WARNING, lmm);
193                         continue;
194                 }
195
196                 lov_bytes = lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx]);
197                 if (min_stripe_maxbytes == 0 || lov_bytes < min_stripe_maxbytes)
198                         min_stripe_maxbytes = lov_bytes;
199         }
200
201         if (min_stripe_maxbytes == 0)
202                 min_stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
203
204         stripe_count = lsm->lsm_stripe_count ?: lov->desc.ld_tgt_count;
205         lov_bytes = min_stripe_maxbytes * stripe_count;
206
207         if (lov_bytes < min_stripe_maxbytes) /* handle overflow */
208                 lsm->lsm_maxbytes = MAX_LFS_FILESIZE;
209         else
210                 lsm->lsm_maxbytes = lov_bytes;
211
212         return 0;
213 }
214
215 static void
216 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
217                           loff_t *lov_off, loff_t *swidth)
218 {
219         if (swidth)
220                 *swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
221 }
222
223 static void
224 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
225                            loff_t *lov_off, loff_t *swidth)
226 {
227         if (swidth)
228                 *swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
229 }
230
231 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
232                              __u16 *stripe_count)
233 {
234         if (lmm_bytes < sizeof(*lmm)) {
235                 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
236                        lmm_bytes, (int)sizeof(*lmm));
237                 return -EINVAL;
238         }
239
240         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
241         if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
242                 *stripe_count = 0;
243
244         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
245                 CERROR("LOV EA V1 too small: %d, need %d\n",
246                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
247                 lov_dump_lmm_common(D_WARNING, lmm);
248                 return -EINVAL;
249         }
250
251         return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
252 }
253
254 static int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
255                            struct lov_mds_md_v1 *lmm)
256 {
257         return lsm_unpackmd_common(lov, lsm, lmm, lmm->lmm_objects);
258 }
259
260 const struct lsm_operations lsm_v1_ops = {
261         .lsm_free           = lsm_free_plain,
262         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
263         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
264         .lsm_lmm_verify  = lsm_lmm_verify_v1,
265         .lsm_unpackmd      = lsm_unpackmd_v1,
266 };
267
268 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
269                              __u16 *stripe_count)
270 {
271         struct lov_mds_md_v3 *lmm;
272
273         lmm = (struct lov_mds_md_v3 *)lmmv1;
274
275         if (lmm_bytes < sizeof(*lmm)) {
276                 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
277                        lmm_bytes, (int)sizeof(*lmm));
278                 return -EINVAL;
279         }
280
281         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
282         if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
283                 *stripe_count = 0;
284
285         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
286                 CERROR("LOV EA V3 too small: %d, need %d\n",
287                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
288                 lov_dump_lmm_common(D_WARNING, lmm);
289                 return -EINVAL;
290         }
291
292         return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
293                                      *stripe_count);
294 }
295
296 static int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
297                            struct lov_mds_md *lmm)
298 {
299         struct lov_mds_md_v3 *lmm_v3 = (struct lov_mds_md_v3 *)lmm;
300         size_t cplen = 0;
301         int rc;
302
303         rc = lsm_unpackmd_common(lov, lsm, lmm, lmm_v3->lmm_objects);
304         if (rc)
305                 return rc;
306
307         cplen = strlcpy(lsm->lsm_pool_name, lmm_v3->lmm_pool_name,
308                         sizeof(lsm->lsm_pool_name));
309         if (cplen >= sizeof(lsm->lsm_pool_name))
310                 return -E2BIG;
311
312         return 0;
313 }
314
315 const struct lsm_operations lsm_v3_ops = {
316         .lsm_free           = lsm_free_plain,
317         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
318         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
319         .lsm_lmm_verify  = lsm_lmm_verify_v3,
320         .lsm_unpackmd      = lsm_unpackmd_v3,
321 };
322
323 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
324 {
325         CDEBUG(level, "lsm %p, objid " DOSTID ", maxbytes %#llx, magic 0x%08X, stripe_size %u, stripe_count %u, refc: %d, layout_gen %u, pool [" LOV_POOLNAMEF "]\n",
326                lsm,
327                POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
328                lsm->lsm_stripe_size, lsm->lsm_stripe_count,
329                atomic_read(&lsm->lsm_refc), lsm->lsm_layout_gen,
330                lsm->lsm_pool_name);
331 }