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