Whamcloud - gitweb
LU-16 Allow objects larger than 2TB in size
[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 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/lov/lov_ea.c
40  *
41  * Author: Wang Di <wangdi@clusterfs.com>
42  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_LOV
48
49 #ifdef __KERNEL__
50 #include <asm/div64.h>
51 #include <libcfs/libcfs.h>
52 #else
53 #include <liblustre.h>
54 #endif
55
56 #include <obd_class.h>
57 #include <obd_lov.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre_log.h>
60
61 #include "lov_internal.h"
62
63 struct lovea_unpack_args {
64         struct lov_stripe_md *lsm;
65         int                   cursor;
66 };
67
68 static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
69                                  int stripe_count)
70 {
71
72         if (stripe_count == 0 || stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
73                 CERROR("bad stripe count %d\n", stripe_count);
74                 lov_dump_lmm(D_WARNING, lmm);
75                 return -EINVAL;
76         }
77
78         if (lmm->lmm_object_id == 0) {
79                 CERROR("zero object id\n");
80                 lov_dump_lmm(D_WARNING, lmm);
81                 return -EINVAL;
82         }
83
84         if (lmm->lmm_pattern != cpu_to_le32(LOV_PATTERN_RAID0)) {
85                 CERROR("bad striping pattern\n");
86                 lov_dump_lmm(D_WARNING, lmm);
87                 return -EINVAL;
88         }
89
90         if (lmm->lmm_stripe_size == 0 ||
91              (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
92                 CERROR("bad stripe size %u\n",
93                        le32_to_cpu(lmm->lmm_stripe_size));
94                 lov_dump_lmm(D_WARNING, lmm);
95                 return -EINVAL;
96         }
97         return 0;
98 }
99
100 struct lov_stripe_md *lsm_alloc_plain(int stripe_count, int *size)
101 {
102         struct lov_stripe_md *lsm;
103         int i, oinfo_ptrs_size;
104         struct lov_oinfo *loi;
105
106         LASSERT(stripe_count > 0);
107
108         oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
109         *size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;
110
111         OBD_ALLOC_LARGE(lsm, *size);
112         if (!lsm)
113                 return NULL;;
114
115         for (i = 0; i < stripe_count; i++) {
116                 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, CFS_ALLOC_IO);
117                 if (loi == NULL)
118                         goto err;
119                 lsm->lsm_oinfo[i] = loi;
120         }
121         lsm->lsm_stripe_count = stripe_count;
122         lsm->lsm_pool_name[0] = '\0';
123         return lsm;
124
125 err:
126         while (--i >= 0)
127                 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
128         OBD_FREE_LARGE(lsm, *size);
129         return NULL;
130 }
131
132 void lsm_free_plain(struct lov_stripe_md *lsm)
133 {
134         int stripe_count = lsm->lsm_stripe_count;
135         int i;
136
137         for (i = 0; i < stripe_count; i++)
138                 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
139                               sizeof(struct lov_oinfo));
140         OBD_FREE_LARGE(lsm, sizeof(struct lov_stripe_md) +
141                        stripe_count * sizeof(struct lov_oinfo *));
142 }
143
144 static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
145                                 struct lov_mds_md *lmm)
146 {
147         /*
148          * This supposes lov_mds_md_v1/v3 first fields are
149          * are the same
150          */
151         lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
152         lsm->lsm_object_seq = le64_to_cpu(lmm->lmm_object_seq);
153         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
154         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
155         lsm->lsm_pool_name[0] = '\0';
156 }
157
158 static void
159 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
160                            obd_off *lov_off, obd_off *swidth)
161 {
162         if (swidth)
163                 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
164 }
165
166 static void
167 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
168                            obd_off *lov_off, obd_off *swidth)
169 {
170         if (swidth)
171                 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
172 }
173
174 static int lsm_destroy_plain(struct lov_stripe_md *lsm, struct obdo *oa,
175                              struct obd_export *md_exp)
176 {
177         return 0;
178 }
179
180 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
181                              int *stripe_count)
182 {
183         if (lmm_bytes < sizeof(*lmm)) {
184                 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
185                        lmm_bytes, (int)sizeof(*lmm));
186                 return -EINVAL;
187         }
188
189         *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
190
191         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
192                 CERROR("LOV EA V1 too small: %d, need %d\n",
193                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
194                 lov_dump_lmm_v1(D_WARNING, lmm);
195                 return -EINVAL;
196         }
197
198         return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
199 }
200
201 int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
202                     struct lov_mds_md_v1 *lmm)
203 {
204         struct lov_oinfo *loi;
205         int i;
206         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
207
208         lsm_unpackmd_common(lsm, lmm);
209
210         for (i = 0; i < lsm->lsm_stripe_count; i++) {
211                 struct obd_import *imp;
212
213                 /* XXX LOV STACKING call down to osc_unpackmd() */
214                 loi = lsm->lsm_oinfo[i];
215                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
216                 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
217                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
218                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
219                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
220                         CERROR("OST index %d more than OST count %d\n",
221                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
222                         lov_dump_lmm_v1(D_WARNING, lmm);
223                         return -EINVAL;
224                 }
225                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
226                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
227                         lov_dump_lmm_v1(D_WARNING, lmm);
228                         return -EINVAL;
229                 }
230                 /* calculate the minimum stripe max bytes */
231                 imp = lov->lov_tgts[loi->loi_ost_idx]->ltd_obd->u.cli.cl_import;
232                 if (imp != NULL) {
233                         if (!(imp->imp_connect_data.ocd_connect_flags &
234                               OBD_CONNECT_MAXBYTES)) {
235                                 imp->imp_connect_data.ocd_maxbytes =
236                                                          LUSTRE_STRIPE_MAXBYTES;
237                         }
238                         if (stripe_maxbytes>imp->imp_connect_data.ocd_maxbytes){
239                                 stripe_maxbytes =
240                                              imp->imp_connect_data.ocd_maxbytes;
241                         }
242                 }
243         }
244
245         /* no ost connected yet */
246         if (stripe_maxbytes == OBD_OBJECT_EOF)
247                 stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
248         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
249
250         return 0;
251 }
252
253 const struct lsm_operations lsm_v1_ops = {
254         .lsm_free            = lsm_free_plain,
255         .lsm_destroy         = lsm_destroy_plain,
256         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
257         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
258         .lsm_lmm_verify         = lsm_lmm_verify_v1,
259         .lsm_unpackmd           = lsm_unpackmd_v1,
260 };
261
262 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
263                              int *stripe_count)
264 {
265         struct lov_mds_md_v3 *lmm;
266
267         lmm = (struct lov_mds_md_v3 *)lmmv1;
268
269         if (lmm_bytes < sizeof(*lmm)) {
270                 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
271                        lmm_bytes, (int)sizeof(*lmm));
272                 return -EINVAL;
273         }
274
275         *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
276
277         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
278                 CERROR("LOV EA V3 too small: %d, need %d\n",
279                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
280                 lov_dump_lmm_v3(D_WARNING, lmm);
281                 return -EINVAL;
282         }
283
284         return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
285                                      *stripe_count);
286 }
287
288 int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
289                     struct lov_mds_md *lmmv1)
290 {
291         struct lov_mds_md_v3 *lmm;
292         struct lov_oinfo *loi;
293         int i;
294         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
295
296         lmm = (struct lov_mds_md_v3 *)lmmv1;
297
298         lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);
299         strncpy(lsm->lsm_pool_name, lmm->lmm_pool_name, LOV_MAXPOOLNAME);
300
301         for (i = 0; i < lsm->lsm_stripe_count; i++) {
302                 struct obd_import *imp;
303
304                 /* XXX LOV STACKING call down to osc_unpackmd() */
305                 loi = lsm->lsm_oinfo[i];
306                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
307                 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
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 (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
311                         CERROR("OST index %d more than OST count %d\n",
312                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
313                         lov_dump_lmm_v3(D_WARNING, lmm);
314                         return -EINVAL;
315                 }
316                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
317                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
318                         lov_dump_lmm_v3(D_WARNING, lmm);
319                         return -EINVAL;
320                 }
321                 /* calculate the minimum stripe max bytes */
322                 imp = lov->lov_tgts[loi->loi_ost_idx]->ltd_obd->u.cli.cl_import;
323                 if (imp != NULL) {
324                         if (!(imp->imp_connect_data.ocd_connect_flags &
325                               OBD_CONNECT_MAXBYTES)) {
326                                 imp->imp_connect_data.ocd_maxbytes =
327                                                          LUSTRE_STRIPE_MAXBYTES;
328                         }
329                         if (stripe_maxbytes>imp->imp_connect_data.ocd_maxbytes){
330                                 stripe_maxbytes =
331                                              imp->imp_connect_data.ocd_maxbytes;
332                         }
333                 }
334         }
335
336         /* no ost connected yet */
337         if (stripe_maxbytes == OBD_OBJECT_EOF)
338                 stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
339         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
340
341         return 0;
342 }
343
344 const struct lsm_operations lsm_v3_ops = {
345         .lsm_free            = lsm_free_plain,
346         .lsm_destroy         = lsm_destroy_plain,
347         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
348         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
349         .lsm_lmm_verify         = lsm_lmm_verify_v3,
350         .lsm_unpackmd           = lsm_unpackmd_v3,
351 };
352