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