Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / lov / lov_ea.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/lov/lov_ea.c
39  *
40  * Author: Wang Di <wangdi@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_LOV
47
48 #ifdef __KERNEL__
49 #include <asm/div64.h>
50 #include <libcfs/libcfs.h>
51 #else
52 #include <liblustre.h>
53 #endif
54
55 #include <obd_class.h>
56 #include <obd_lov.h>
57 #include <lustre/lustre_idl.h>
58 #include <lustre_log.h>
59
60 #include "lov_internal.h"
61
62 struct lovea_unpack_args {
63         struct lov_stripe_md *lsm;
64         int                   cursor;
65 };
66
67 static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
68                                  __u16 stripe_count)
69 {
70
71         if (stripe_count == 0 || stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
72                 CERROR("bad stripe count %d\n", stripe_count);
73                 lov_dump_lmm(D_WARNING, lmm);
74                 return -EINVAL;
75         }
76
77         if (lmm->lmm_object_id == 0) {
78                 CERROR("zero object id\n");
79                 lov_dump_lmm(D_WARNING, lmm);
80                 return -EINVAL;
81         }
82
83         if (lmm->lmm_pattern != cpu_to_le32(LOV_PATTERN_RAID0)) {
84                 CERROR("bad striping pattern\n");
85                 lov_dump_lmm(D_WARNING, lmm);
86                 return -EINVAL;
87         }
88
89         if (lmm->lmm_stripe_size == 0 ||
90              (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
91                 CERROR("bad stripe size %u\n",
92                        le32_to_cpu(lmm->lmm_stripe_size));
93                 lov_dump_lmm(D_WARNING, lmm);
94                 return -EINVAL;
95         }
96         return 0;
97 }
98
99 struct lov_stripe_md *lsm_alloc_plain(__u16 stripe_count, int *size)
100 {
101         struct lov_stripe_md *lsm;
102         struct lov_oinfo     *loi;
103         int                   i, oinfo_ptrs_size;
104
105         LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
106
107         oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
108         *size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;
109
110         OBD_ALLOC_LARGE(lsm, *size);
111         if (!lsm)
112                 return NULL;;
113
114         for (i = 0; i < stripe_count; i++) {
115                 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, CFS_ALLOC_IO);
116                 if (loi == NULL)
117                         goto err;
118                 lsm->lsm_oinfo[i] = loi;
119         }
120         lsm->lsm_stripe_count = stripe_count;
121         return lsm;
122
123 err:
124         while (--i >= 0)
125                 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
126         OBD_FREE_LARGE(lsm, *size);
127         return NULL;
128 }
129
130 void lsm_free_plain(struct lov_stripe_md *lsm)
131 {
132         __u16 stripe_count = lsm->lsm_stripe_count;
133         int i;
134
135         for (i = 0; i < stripe_count; i++)
136                 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
137                               sizeof(struct lov_oinfo));
138         OBD_FREE_LARGE(lsm, sizeof(struct lov_stripe_md) +
139                        stripe_count * sizeof(struct lov_oinfo *));
140 }
141
142 static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
143                                 struct lov_mds_md *lmm)
144 {
145         /*
146          * This supposes lov_mds_md_v1/v3 first fields are
147          * are the same
148          */
149         lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
150         lsm->lsm_object_seq = le64_to_cpu(lmm->lmm_object_seq);
151         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
152         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
153         lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
154         lsm->lsm_pool_name[0] = '\0';
155 }
156
157 static void
158 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
159                            obd_off *lov_off, obd_off *swidth)
160 {
161         if (swidth)
162                 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
163 }
164
165 static void
166 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
167                            obd_off *lov_off, obd_off *swidth)
168 {
169         if (swidth)
170                 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
171 }
172
173 static int lsm_destroy_plain(struct lov_stripe_md *lsm, struct obdo *oa,
174                              struct obd_export *md_exp)
175 {
176         return 0;
177 }
178
179 /* Find minimum stripe maxbytes value.  For inactive or
180  * reconnecting targets use LUSTRE_STRIPE_MAXBYTES. */
181 static void lov_tgt_maxbytes(struct lov_tgt_desc *tgt, __u64 *stripe_maxbytes)
182 {
183         struct obd_import *imp = tgt->ltd_obd->u.cli.cl_import;
184
185         if (imp == NULL || !tgt->ltd_active) {
186                 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
187                 return;
188         }
189
190         cfs_spin_lock(&imp->imp_lock);
191         if (imp->imp_state == LUSTRE_IMP_FULL &&
192            (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
193            imp->imp_connect_data.ocd_maxbytes > 0) {
194                 if (*stripe_maxbytes > imp->imp_connect_data.ocd_maxbytes)
195                         *stripe_maxbytes = imp->imp_connect_data.ocd_maxbytes;
196         } else {
197                 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
198         }
199         cfs_spin_unlock(&imp->imp_lock);
200 }
201
202 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
203                              __u16 *stripe_count)
204 {
205         if (lmm_bytes < sizeof(*lmm)) {
206                 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
207                        lmm_bytes, (int)sizeof(*lmm));
208                 return -EINVAL;
209         }
210
211         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
212
213         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
214                 CERROR("LOV EA V1 too small: %d, need %d\n",
215                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
216                 lov_dump_lmm_v1(D_WARNING, lmm);
217                 return -EINVAL;
218         }
219
220         return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
221 }
222
223 int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
224                     struct lov_mds_md_v1 *lmm)
225 {
226         struct lov_oinfo *loi;
227         int i;
228         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
229
230         lsm_unpackmd_common(lsm, lmm);
231
232         for (i = 0; i < lsm->lsm_stripe_count; i++) {
233                 /* XXX LOV STACKING call down to osc_unpackmd() */
234                 loi = lsm->lsm_oinfo[i];
235                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
236                 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
237                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
238                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
239                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
240                         CERROR("OST index %d more than OST count %d\n",
241                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
242                         lov_dump_lmm_v1(D_WARNING, lmm);
243                         return -EINVAL;
244                 }
245                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
246                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
247                         lov_dump_lmm_v1(D_WARNING, lmm);
248                         return -EINVAL;
249                 }
250                 /* calculate the minimum stripe max bytes */
251                 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
252                                  &stripe_maxbytes);
253         }
254
255         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
256
257         return 0;
258 }
259
260 const struct lsm_operations lsm_v1_ops = {
261         .lsm_free            = lsm_free_plain,
262         .lsm_destroy         = lsm_destroy_plain,
263         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
264         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
265         .lsm_lmm_verify         = lsm_lmm_verify_v1,
266         .lsm_unpackmd           = lsm_unpackmd_v1,
267 };
268
269 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
270                              __u16 *stripe_count)
271 {
272         struct lov_mds_md_v3 *lmm;
273
274         lmm = (struct lov_mds_md_v3 *)lmmv1;
275
276         if (lmm_bytes < sizeof(*lmm)) {
277                 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
278                        lmm_bytes, (int)sizeof(*lmm));
279                 return -EINVAL;
280         }
281
282         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
283
284         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
285                 CERROR("LOV EA V3 too small: %d, need %d\n",
286                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
287                 lov_dump_lmm_v3(D_WARNING, lmm);
288                 return -EINVAL;
289         }
290
291         return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
292                                      *stripe_count);
293 }
294
295 int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
296                     struct lov_mds_md *lmmv1)
297 {
298         struct lov_mds_md_v3 *lmm;
299         struct lov_oinfo *loi;
300         int i;
301         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
302
303         lmm = (struct lov_mds_md_v3 *)lmmv1;
304
305         lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);
306         strncpy(lsm->lsm_pool_name, lmm->lmm_pool_name, LOV_MAXPOOLNAME);
307
308         for (i = 0; i < lsm->lsm_stripe_count; i++) {
309                 /* XXX LOV STACKING call down to osc_unpackmd() */
310                 loi = lsm->lsm_oinfo[i];
311                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
312                 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
313                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
314                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
315                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
316                         CERROR("OST index %d more than OST count %d\n",
317                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
318                         lov_dump_lmm_v3(D_WARNING, lmm);
319                         return -EINVAL;
320                 }
321                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
322                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
323                         lov_dump_lmm_v3(D_WARNING, lmm);
324                         return -EINVAL;
325                 }
326                 /* calculate the minimum stripe max bytes */
327                 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
328                                  &stripe_maxbytes);
329         }
330
331         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_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