Whamcloud - gitweb
b=16227
[fs/lustre-release.git] / lustre / lov / lov_pack.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 [sun.com URL with a
20  * copy of GPLv2].
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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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_pack.c
37  *
38  * (Un)packing of OST/MDS requests
39  *
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_LOV
44 #ifndef __KERNEL__
45 #include <liblustre.h>
46 #endif
47
48 #include <lustre_net.h>
49 #include <obd.h>
50 #include <obd_lov.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre/lustre_user.h>
54
55 #include "lov_internal.h"
56
57 void lov_dump_lmm_v1(int level, struct lov_mds_md_v1 *lmm)
58 {
59         struct lov_ost_data_v1 *lod;
60         int i;
61         
62         CDEBUG(level, "objid "LPX64", magic 0x%08x, pattern %#x\n",
63                le64_to_cpu(lmm->lmm_object_id), le32_to_cpu(lmm->lmm_magic),
64                le32_to_cpu(lmm->lmm_pattern));
65         CDEBUG(level,"stripe_size %u, stripe_count %u\n",
66                le32_to_cpu(lmm->lmm_stripe_size),
67                le32_to_cpu(lmm->lmm_stripe_count));
68
69         if (le32_to_cpu(lmm->lmm_stripe_count) <= LOV_V1_INSANE_STRIPE_COUNT) {
70                 for (i = 0, lod = lmm->lmm_objects;
71                      i < (int)le32_to_cpu(lmm->lmm_stripe_count); i++, lod++)
72                          CDEBUG(level,
73                                 "stripe %u idx %u subobj "LPX64"/"LPX64"\n",
74                                 i, le32_to_cpu(lod->l_ost_idx),
75                                 le64_to_cpu(lod->l_object_gr),
76                                 le64_to_cpu(lod->l_object_id));
77         } else {
78                 CDEBUG(level, "bad stripe_count %u > max_stripe_count %u\n",
79                        le32_to_cpu(lmm->lmm_stripe_count),
80                        LOV_V1_INSANE_STRIPE_COUNT);
81         }
82 }
83
84 void lov_dump_lmm_join(int level, struct lov_mds_md_join *lmmj)
85 {
86
87         CDEBUG(level, "objid "LPX64", magic 0x%08X, pattern %#X\n",
88                le64_to_cpu(lmmj->lmmj_md.lmm_object_id),
89                le32_to_cpu(lmmj->lmmj_md.lmm_magic),
90                le32_to_cpu(lmmj->lmmj_md.lmm_pattern));
91         CDEBUG(level,"stripe_size %u, stripe_count %u extent_count %u \n",
92                le32_to_cpu(lmmj->lmmj_md.lmm_stripe_size),
93                le32_to_cpu(lmmj->lmmj_md.lmm_stripe_count),
94                le32_to_cpu(lmmj->lmmj_extent_count));
95 }
96
97 #define LMM_ASSERT(test)                                                \
98 do {                                                                    \
99         if (!(test)) lov_dump_lmm(D_ERROR, lmm);                        \
100         LASSERT(test); /* so we know what assertion failed */           \
101 } while(0)
102
103 /* Pack LOV object metadata for disk storage.  It is packed in LE byte
104  * order and is opaque to the networking layer.
105  *
106  * XXX In the future, this will be enhanced to get the EA size from the
107  *     underlying OSC device(s) to get their EA sizes so we can stack
108  *     LOVs properly.  For now lov_mds_md_size() just assumes one obd_id
109  *     per stripe.
110  */
111 int lov_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
112                struct lov_stripe_md *lsm)
113 {
114         struct obd_device *obd = class_exp2obd(exp);
115         struct lov_obd *lov = &obd->u.lov;
116         struct lov_mds_md *lmm;
117         int stripe_count = lov->desc.ld_tgt_count;
118         int lmm_size;
119         int i;
120         ENTRY;
121
122         if (lsm) {
123                 if (lsm->lsm_magic != LOV_MAGIC) {
124                         CERROR("bad mem LOV MAGIC: 0x%08X != 0x%08X\n",
125                                lsm->lsm_magic, LOV_MAGIC);
126                         RETURN(-EINVAL);
127                 }
128                 /* If we are just sizing the EA, limit the stripe count
129                  * to the actual number of OSTs in this filesystem. */
130                 if (!lmmp) {
131                         stripe_count = lov_get_stripecnt(lov, lsm->lsm_stripe_count);
132                         lsm->lsm_stripe_count = stripe_count;
133                 } else {
134                         stripe_count = lsm->lsm_stripe_count;
135                 }
136         }
137
138         /* XXX LOV STACKING call into osc for sizes */
139         lmm_size = lov_mds_md_size(stripe_count);
140
141         if (!lmmp)
142                 RETURN(lmm_size);
143
144         if (*lmmp && !lsm) {
145                 stripe_count = le32_to_cpu((*lmmp)->lmm_stripe_count);
146                 OBD_FREE(*lmmp, lov_mds_md_size(stripe_count));
147                 *lmmp = NULL;
148                 RETURN(0);
149         }
150
151         if (!*lmmp) {
152                 OBD_ALLOC(*lmmp, lmm_size);
153                 if (!*lmmp)
154                         RETURN(-ENOMEM);
155         }
156
157         lmm = *lmmp;
158         lmm->lmm_magic = cpu_to_le32(LOV_MAGIC); /* only write new format */
159
160         if (!lsm)
161                 RETURN(lmm_size);
162
163         lmm->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
164         lmm->lmm_object_gr = cpu_to_le64(lsm->lsm_object_gr);
165         lmm->lmm_stripe_size = cpu_to_le32(lsm->lsm_stripe_size);
166         lmm->lmm_stripe_count = cpu_to_le32(stripe_count);
167         lmm->lmm_pattern = cpu_to_le32(lsm->lsm_pattern);
168
169         for (i = 0; i < stripe_count; i++) {
170                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
171
172                 /* XXX LOV STACKING call down to osc_packmd() to do packing */
173                 LASSERTF(loi->loi_id, "lmm_oid "LPU64" stripe %u/%u idx %u\n",
174                          lmm->lmm_object_id, i, stripe_count, loi->loi_ost_idx);
175                 lmm->lmm_objects[i].l_object_id = cpu_to_le64(loi->loi_id);
176                 lmm->lmm_objects[i].l_object_gr = cpu_to_le64(loi->loi_gr);
177                 lmm->lmm_objects[i].l_ost_gen = cpu_to_le32(loi->loi_ost_gen);
178                 lmm->lmm_objects[i].l_ost_idx = cpu_to_le32(loi->loi_ost_idx);
179         }
180
181         RETURN(lmm_size);
182 }
183
184 /* Find the max stripecount we should use */
185 int lov_get_stripecnt(struct lov_obd *lov, __u32 stripe_count)
186 {
187         if (!stripe_count)
188                 stripe_count = lov->desc.ld_default_stripe_count;
189         if (stripe_count > lov->desc.ld_active_tgt_count)
190                 stripe_count = lov->desc.ld_active_tgt_count;
191         if (!stripe_count)
192                 stripe_count = 1;
193         /* for now, we limit the stripe count directly, when bug 4424 is
194          * fixed this needs to be somewhat dynamic based on whether ext3
195          * can handle larger EA sizes. */
196         if (stripe_count > LOV_MAX_STRIPE_COUNT)
197                 stripe_count = LOV_MAX_STRIPE_COUNT;
198
199         return stripe_count;
200 }
201
202
203 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
204 {
205         int rc;
206
207         if (lsm_op_find(le32_to_cpu(*(__u32 *)lmm)) == NULL) {
208                 CERROR("bad disk LOV MAGIC: 0x%08X; dumping V1 LMM:\n",
209                        le32_to_cpu(*(__u32 *)lmm));
210                 lov_dump_lmm_v1(D_WARNING, lmm);
211                 return -EINVAL;
212         }
213         rc = lsm_op_find(le32_to_cpu(*(__u32 *)lmm))->lsm_lmm_verify(lmm,
214                                      lmm_bytes, stripe_count);
215         return rc;
216 }
217
218 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count,
219                       int pattern, int magic)
220 {
221         int i, lsm_size;
222         ENTRY;
223
224         CDEBUG(D_INFO, "alloc lsm, stripe_count %d\n", stripe_count);
225
226         *lsmp = lsm_alloc_plain(stripe_count, &lsm_size);
227         if (!*lsmp) {
228                 CERROR("can't allocate lsmp stripe_count %d\n", stripe_count);
229                 RETURN(-ENOMEM);
230         }
231
232         spin_lock_init(&(*lsmp)->lsm_lock);
233         (*lsmp)->lsm_magic = magic;
234         (*lsmp)->lsm_stripe_count = stripe_count;
235         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES * stripe_count;
236         (*lsmp)->lsm_pattern = pattern;
237         (*lsmp)->lsm_oinfo[0]->loi_ost_idx = ~0;
238
239         for (i = 0; i < stripe_count; i++)
240                 loi_init((*lsmp)->lsm_oinfo[i]);
241
242         RETURN(lsm_size);
243 }
244
245 void lov_free_memmd(struct lov_stripe_md **lsmp)
246 {
247         struct lov_stripe_md *lsm = *lsmp;
248
249         LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
250         lsm_op_find(lsm->lsm_magic)->lsm_free(lsm);
251
252         *lsmp = NULL;
253 }
254
255
256 /* Unpack LOV object metadata from disk storage.  It is packed in LE byte
257  * order and is opaque to the networking layer.
258  */
259 int lov_unpackmd(struct obd_export *exp,  struct lov_stripe_md **lsmp,
260                  struct lov_mds_md *lmm, int lmm_bytes)
261 {
262         struct obd_device *obd = class_exp2obd(exp);
263         struct lov_obd *lov = &obd->u.lov;
264         int rc = 0, stripe_count, lsm_size;
265         __u32 magic;
266         ENTRY;
267
268         /* If passed an MDS struct use values from there, otherwise defaults */
269         if (lmm) {
270                 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
271                 if (rc)
272                         RETURN(rc);
273                 magic = le32_to_cpu(lmm->lmm_magic);
274         } else {
275                 stripe_count = lov_get_stripecnt(lov, 0);
276                 magic = LOV_MAGIC;
277         }
278
279         /* If we aren't passed an lsmp struct, we just want the size */
280         if (!lsmp) {
281                 /* XXX LOV STACKING call into osc for sizes */
282                 LBUG();
283                 RETURN(lov_stripe_md_size(stripe_count));
284         }
285         /* If we are passed an allocated struct but nothing to unpack, free */
286         if (*lsmp && !lmm) {
287                 lov_free_memmd(lsmp);
288                 RETURN(0);
289         }
290
291         lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0,
292                                    magic);
293         if (lsm_size < 0)
294                 RETURN(lsm_size);
295
296         /* If we are passed a pointer but nothing to unpack, we only alloc */
297         if (!lmm)
298                 RETURN(lsm_size);
299
300         LASSERT(lsm_op_find(magic) != NULL);
301         rc = lsm_op_find(magic)->lsm_unpackmd(lov, *lsmp, lmm);
302         if (rc) {
303                 lov_free_memmd(lsmp);
304                 RETURN(rc);
305         }
306
307         RETURN(lsm_size);
308 }
309
310 static int __lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
311                            struct lov_user_md *lump)
312 {
313         struct obd_device *obd = class_exp2obd(exp);
314         struct lov_obd *lov = &obd->u.lov;
315         struct lov_user_md lum;
316         int stripe_count;
317         int rc;
318         ENTRY;
319
320         rc = copy_from_user(&lum, lump, sizeof(lum));
321         if (rc)
322                 RETURN(-EFAULT);
323
324         if (lum.lmm_magic != LOV_USER_MAGIC) {
325                 if (lum.lmm_magic == __swab32(LOV_USER_MAGIC)) {
326                         lustre_swab_lov_user_md(&lum);
327                 } else {
328                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
329                                " %#08x != %#08x\n",
330                                lum.lmm_magic, LOV_USER_MAGIC);
331                         RETURN(-EINVAL);
332                 }
333         }
334
335         if (lum.lmm_pattern == 0) {
336                 lum.lmm_pattern = lov->desc.ld_pattern ?
337                         lov->desc.ld_pattern : LOV_PATTERN_RAID0;
338         }
339
340         if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
341                 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
342                        lum.lmm_pattern);
343                 RETURN(-EINVAL);
344         }
345
346         /* 64kB is the largest common page size we see (ia64), and matches the
347          * check in lfs */
348         if (lum.lmm_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
349                 CDEBUG(D_IOCTL, "stripe size %u not multiple of %u, fixing\n",
350                        lum.lmm_stripe_size, LOV_MIN_STRIPE_SIZE);
351                 lum.lmm_stripe_size = LOV_MIN_STRIPE_SIZE;
352         }
353
354         if ((lum.lmm_stripe_offset >= lov->desc.ld_tgt_count) &&
355             (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
356                 CDEBUG(D_IOCTL, "stripe offset %u > number of OSTs %u\n",
357                        lum.lmm_stripe_offset, lov->desc.ld_tgt_count);
358                 RETURN(-EINVAL);
359         }
360         stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
361
362         if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
363                 CDEBUG(D_IOCTL, "stripe width %ux%i exeeds %lu bytes\n",
364                        lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
365                 RETURN(-EINVAL);
366         }
367
368         rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern, LOV_MAGIC);
369
370         if (rc >= 0) {
371                 (*lsmp)->lsm_oinfo[0]->loi_ost_idx = lum.lmm_stripe_offset;
372                 (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
373                 rc = 0;
374         }
375
376         RETURN(0);
377 }
378
379 /* Configure object striping information on a new file.
380  *
381  * @lmmu is a pointer to a user struct with one or more of the fields set to
382  * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
383  * lmm_stripe_offset, and lmm_stripe_pattern.  lmm_magic must be LOV_MAGIC.
384  * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
385  */
386 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
387                   struct lov_user_md *lump)
388 {
389         int rc;
390         mm_segment_t seg;
391
392         seg = get_fs();
393         set_fs(KERNEL_DS);
394
395         rc = __lov_setstripe(exp, lsmp, lump);
396         set_fs(seg);
397         RETURN(rc);
398 }
399
400 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
401               struct lov_user_md *lump)
402 {
403         int i;
404         int rc;
405         struct obd_export *oexp;
406         struct lov_obd *lov = &exp->exp_obd->u.lov;
407         obd_id last_id = 0;
408
409         ENTRY;
410         for (i = 0; i < lump->lmm_stripe_count; i++) {
411                 __u32 len = sizeof(last_id);
412                 oexp = lov->lov_tgts[lump->lmm_objects[i].l_ost_idx]->ltd_exp;
413                 rc = obd_get_info(oexp, sizeof(KEY_LAST_ID), KEY_LAST_ID,
414                                   &len, &last_id, NULL);
415                 if (rc)
416                         RETURN(rc);
417                 if (lump->lmm_objects[i].l_object_id > last_id) {
418                         CERROR("Setting EA for object > than last id on "
419                                "ost idx %d "LPD64" > "LPD64" \n",
420                                lump->lmm_objects[i].l_ost_idx,
421                                lump->lmm_objects[i].l_object_id, last_id);
422                         RETURN(-EINVAL);
423                 }
424         }
425
426         rc = lov_setstripe(exp, lsmp, lump);
427         if (rc)
428                 RETURN(rc);
429
430         for (i = 0; i < lump->lmm_stripe_count; i++) {
431                 (*lsmp)->lsm_oinfo[i]->loi_ost_idx =
432                         lump->lmm_objects[i].l_ost_idx;
433                 (*lsmp)->lsm_oinfo[i]->loi_id = lump->lmm_objects[i].l_object_id;
434                 (*lsmp)->lsm_oinfo[i]->loi_gr = lump->lmm_objects[i].l_object_gr;
435         }
436         RETURN(0);
437 }
438
439
440 /* Retrieve object striping information.
441  *
442  * @lump is a pointer to an in-core struct with lmm_ost_count indicating
443  * the maximum number of OST indices which will fit in the user buffer.
444  * lmm_magic must be LOV_USER_MAGIC.
445  */
446 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
447                   struct lov_user_md *lump)
448 {
449         /*
450          * XXX huge struct allocated on stack.
451          */
452         struct lov_user_md lum;
453         struct lov_mds_md *lmmk = NULL;
454         int rc, lmm_size;
455         mm_segment_t seg;
456         ENTRY;
457
458         if (!lsm)
459                 RETURN(-ENODATA);
460
461         /*
462          * "Switch to kernel segment" to allow copying from kernel space by
463          * copy_{to,from}_user().
464          */
465         seg = get_fs();
466         set_fs(KERNEL_DS);
467         rc = copy_from_user(&lum, lump, sizeof(lum));
468         if (rc)
469                 rc = -EFAULT;
470         else if (lum.lmm_magic != LOV_USER_MAGIC)
471                 rc = -EINVAL;
472         else {
473                 rc = lov_packmd(exp, &lmmk, lsm);
474                 if (rc < 0)
475                         RETURN(rc);
476                 lmm_size = rc;
477                 rc = 0;
478
479                 /* FIXME: Bug 1185 - copy fields properly when structs change */
480                 CLASSERT(sizeof lum == sizeof *lmmk);
481                 CLASSERT(sizeof lum.lmm_objects[0] ==
482                          sizeof lmmk->lmm_objects[0]);
483
484                 /* User wasn't expecting this many OST entries */
485                 if (lum.lmm_stripe_count == 0) {
486                         if (copy_to_user(lump, lmmk, sizeof lum))
487                                 rc = -EFAULT;
488                 } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
489                         rc = -EOVERFLOW;
490                 } else if (copy_to_user(lump, lmmk, sizeof lum))
491                         rc = -EFAULT;
492
493                 obd_free_diskmd(exp, &lmmk);
494         }
495         set_fs(seg);
496         RETURN(rc);
497 }