Merge branch 'pm-sleep'
[cascardo/linux.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 "../../include/linux/libcfs/libcfs.h"
41
42 #include "../include/obd_class.h"
43 #include "../include/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)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
70                 CERROR("bad stripe size %u\n",
71                        le32_to_cpu(lmm->lmm_stripe_size));
72                 lov_dump_lmm_common(D_WARNING, lmm);
73                 return -EINVAL;
74         }
75         return 0;
76 }
77
78 struct lov_stripe_md *lsm_alloc_plain(__u16 stripe_count, int *size)
79 {
80         struct lov_stripe_md *lsm;
81         struct lov_oinfo     *loi;
82         int                i, oinfo_ptrs_size;
83
84         LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
85
86         oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
87         *size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;
88
89         lsm = libcfs_kvzalloc(*size, GFP_NOFS);
90         if (!lsm)
91                 return NULL;
92
93         for (i = 0; i < stripe_count; i++) {
94                 loi = kmem_cache_zalloc(lov_oinfo_slab, GFP_NOFS);
95                 if (!loi)
96                         goto err;
97                 lsm->lsm_oinfo[i] = loi;
98         }
99         lsm->lsm_stripe_count = stripe_count;
100         return lsm;
101
102 err:
103         while (--i >= 0)
104                 kmem_cache_free(lov_oinfo_slab, lsm->lsm_oinfo[i]);
105         kvfree(lsm);
106         return NULL;
107 }
108
109 void lsm_free_plain(struct lov_stripe_md *lsm)
110 {
111         __u16 stripe_count = lsm->lsm_stripe_count;
112         int i;
113
114         for (i = 0; i < stripe_count; i++)
115                 kmem_cache_free(lov_oinfo_slab, lsm->lsm_oinfo[i]);
116         kvfree(lsm);
117 }
118
119 static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
120                                 struct lov_mds_md *lmm)
121 {
122         /*
123          * This supposes lov_mds_md_v1/v3 first fields are
124          * are the same
125          */
126         lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
127         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
128         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
129         lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
130         lsm->lsm_pool_name[0] = '\0';
131 }
132
133 static void
134 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
135                           u64 *lov_off, u64 *swidth)
136 {
137         if (swidth)
138                 *swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
139 }
140
141 static void
142 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
143                            u64 *lov_off, u64 *swidth)
144 {
145         if (swidth)
146                 *swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
147 }
148
149 static int lsm_destroy_plain(struct lov_stripe_md *lsm, struct obdo *oa,
150                              struct obd_export *md_exp)
151 {
152         return 0;
153 }
154
155 /* Find minimum stripe maxbytes value.  For inactive or
156  * reconnecting targets use LUSTRE_STRIPE_MAXBYTES.
157  */
158 static void lov_tgt_maxbytes(struct lov_tgt_desc *tgt, __u64 *stripe_maxbytes)
159 {
160         struct obd_import *imp = tgt->ltd_obd->u.cli.cl_import;
161
162         if (!imp || !tgt->ltd_active) {
163                 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
164                 return;
165         }
166
167         spin_lock(&imp->imp_lock);
168         if (imp->imp_state == LUSTRE_IMP_FULL &&
169             (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
170             imp->imp_connect_data.ocd_maxbytes > 0) {
171                 if (*stripe_maxbytes > imp->imp_connect_data.ocd_maxbytes)
172                         *stripe_maxbytes = imp->imp_connect_data.ocd_maxbytes;
173         } else {
174                 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
175         }
176         spin_unlock(&imp->imp_lock);
177 }
178
179 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
180                              __u16 *stripe_count)
181 {
182         if (lmm_bytes < sizeof(*lmm)) {
183                 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
184                        lmm_bytes, (int)sizeof(*lmm));
185                 return -EINVAL;
186         }
187
188         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
189         if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
190                 *stripe_count = 0;
191
192         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
193                 CERROR("LOV EA V1 too small: %d, need %d\n",
194                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
195                 lov_dump_lmm_common(D_WARNING, lmm);
196                 return -EINVAL;
197         }
198
199         return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
200 }
201
202 static int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
203                            struct lov_mds_md_v1 *lmm)
204 {
205         struct lov_oinfo *loi;
206         int i;
207         int stripe_count;
208         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
209
210         lsm_unpackmd_common(lsm, lmm);
211
212         stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
213
214         for (i = 0; i < stripe_count; i++) {
215                 /* XXX LOV STACKING call down to osc_unpackmd() */
216                 loi = lsm->lsm_oinfo[i];
217                 ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
218                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
219                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_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                         CERROR("OST index %d more than OST count %d\n",
225                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
226                         lov_dump_lmm_v1(D_WARNING, lmm);
227                         return -EINVAL;
228                 }
229                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
230                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
231                         lov_dump_lmm_v1(D_WARNING, lmm);
232                         return -EINVAL;
233                 }
234                 /* calculate the minimum stripe max bytes */
235                 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
236                                  &stripe_maxbytes);
237         }
238
239         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
240         if (lsm->lsm_stripe_count == 0)
241                 lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;
242
243         return 0;
244 }
245
246 const struct lsm_operations lsm_v1_ops = {
247         .lsm_free           = lsm_free_plain,
248         .lsm_destroy     = lsm_destroy_plain,
249         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
250         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
251         .lsm_lmm_verify  = lsm_lmm_verify_v1,
252         .lsm_unpackmd      = lsm_unpackmd_v1,
253 };
254
255 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
256                              __u16 *stripe_count)
257 {
258         struct lov_mds_md_v3 *lmm;
259
260         lmm = (struct lov_mds_md_v3 *)lmmv1;
261
262         if (lmm_bytes < sizeof(*lmm)) {
263                 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
264                        lmm_bytes, (int)sizeof(*lmm));
265                 return -EINVAL;
266         }
267
268         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
269         if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
270                 *stripe_count = 0;
271
272         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
273                 CERROR("LOV EA V3 too small: %d, need %d\n",
274                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
275                 lov_dump_lmm_common(D_WARNING, lmm);
276                 return -EINVAL;
277         }
278
279         return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
280                                      *stripe_count);
281 }
282
283 static int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
284                            struct lov_mds_md *lmmv1)
285 {
286         struct lov_mds_md_v3 *lmm;
287         struct lov_oinfo *loi;
288         int i;
289         int stripe_count;
290         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
291         int cplen = 0;
292
293         lmm = (struct lov_mds_md_v3 *)lmmv1;
294
295         lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);
296
297         stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
298
299         cplen = strlcpy(lsm->lsm_pool_name, lmm->lmm_pool_name,
300                         sizeof(lsm->lsm_pool_name));
301         if (cplen >= sizeof(lsm->lsm_pool_name))
302                 return -E2BIG;
303
304         for (i = 0; i < stripe_count; i++) {
305                 /* XXX LOV STACKING call down to osc_unpackmd() */
306                 loi = lsm->lsm_oinfo[i];
307                 ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
308                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
309                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
310                 if (lov_oinfo_is_dummy(loi))
311                         continue;
312
313                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
314                         CERROR("OST index %d more than OST count %d\n",
315                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
316                         lov_dump_lmm_v3(D_WARNING, lmm);
317                         return -EINVAL;
318                 }
319                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
320                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
321                         lov_dump_lmm_v3(D_WARNING, lmm);
322                         return -EINVAL;
323                 }
324                 /* calculate the minimum stripe max bytes */
325                 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
326                                  &stripe_maxbytes);
327         }
328
329         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
330         if (lsm->lsm_stripe_count == 0)
331                 lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;
332
333         return 0;
334 }
335
336 const struct lsm_operations lsm_v3_ops = {
337         .lsm_free           = lsm_free_plain,
338         .lsm_destroy     = lsm_destroy_plain,
339         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
340         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
341         .lsm_lmm_verify  = lsm_lmm_verify_v3,
342         .lsm_unpackmd      = lsm_unpackmd_v3,
343 };
344
345 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
346 {
347         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",
348                lsm,
349                POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
350                lsm->lsm_stripe_size, lsm->lsm_stripe_count,
351                atomic_read(&lsm->lsm_refc), lsm->lsm_layout_gen,
352                lsm->lsm_pool_name);
353 }