Whamcloud - gitweb
- landed b_hd_cray_merge3
[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 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_LOV
29 #ifndef __KERNEL__
30 #include <liblustre.h>
31 #endif
32
33 #include <linux/lustre_net.h>
34 #include <linux/obd.h>
35 #include <linux/obd_lov.h>
36 #include <linux/obd_class.h>
37 #include <linux/obd_support.h>
38 #include <lustre/lustre_user.h>
39
40 #include "lov_internal.h"
41
42 void lov_dump_lmm_v0(int level, struct lov_mds_md_v0 *lmm)
43 {
44         int i, num_ost, stripe, idx;
45
46         num_ost = le32_to_cpu(lmm->lmm_ost_count);
47         idx = le32_to_cpu(lmm->lmm_stripe_offset);
48         CDEBUG(level, "objid "LPX64", magic 0x%08X, ost_count %u\n",
49                le64_to_cpu(lmm->lmm_object_id), le32_to_cpu(lmm->lmm_magic),
50                num_ost);
51         CDEBUG(level,"stripe_size %u, stripe_count %u, stripe_offset %u\n",
52                le32_to_cpu(lmm->lmm_stripe_size),
53                le32_to_cpu(lmm->lmm_stripe_count), idx);
54         for (i = stripe = 0; i < le32_to_cpu(lmm->lmm_ost_count); i++, idx++) {
55                 idx %= num_ost;
56                 if (lmm->lmm_objects[idx].l_object_id == 0)
57                         continue;
58                 CDEBUG(level, "stripe %u idx %u subobj "LPX64"\n", stripe, idx,
59                        le64_to_cpu(lmm->lmm_objects[idx].l_object_id));
60                 stripe++;
61         }
62 }
63
64 void lov_dump_lmm_v1(int level, struct lov_mds_md_v1 *lmm)
65 {
66         struct lov_ost_data_v1 *lod;
67         int i;
68
69         CDEBUG(level, "objid "LPX64", magic 0x%08X, pattern %#X\n",
70                le64_to_cpu(lmm->lmm_object_id), le32_to_cpu(lmm->lmm_magic),
71                le32_to_cpu(lmm->lmm_pattern));
72         CDEBUG(level,"stripe_size %u, stripe_count %u\n",
73                le32_to_cpu(lmm->lmm_stripe_size),
74                le32_to_cpu(lmm->lmm_stripe_count));
75         for (i = 0, lod = lmm->lmm_objects;
76              i < le32_to_cpu(lmm->lmm_stripe_count); i++, lod++)
77                 CDEBUG(level, "stripe %u idx %u subobj "LPX64"/"LPX64"\n",
78                        i, le32_to_cpu(lod->l_ost_idx),
79                        le64_to_cpu(lod->l_object_gr),
80                        le64_to_cpu(lod->l_object_id));
81 }
82
83 #define LMM_ASSERT(test)                                                \
84 do {                                                                    \
85         if (!(test)) lov_dump_lmm(D_ERROR, lmm);                        \
86         LASSERT(test); /* so we know what assertion failed */           \
87 } while(0)
88
89 /* Pack LOV object metadata for disk storage.  It is packed in LE byte
90  * order and is opaque to the networking layer.
91  *
92  * XXX In the future, this will be enhanced to get the EA size from the
93  *     underlying OSC device(s) to get their EA sizes so we can stack
94  *     LOVs properly.  For now lov_mds_md_size() just assumes one obd_id
95  *     per stripe.
96  */
97 int lov_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
98                struct lov_stripe_md *lsm)
99 {
100         struct obd_device *obd = class_exp2obd(exp);
101         struct lov_obd *lov = &obd->u.lov;
102         struct lov_oinfo *loi;
103         struct lov_mds_md *lmm;
104         int stripe_count = lov->desc.ld_tgt_count;
105         int lmm_size;
106         int i;
107         ENTRY;
108
109         if (lsm) {
110                 if (lsm->lsm_magic != LOV_MAGIC) {
111                         CWARN("bad LOV MAGIC: 0x%08X != 0x%08X\n",
112                                lsm->lsm_magic, LOV_MAGIC);
113                         RETURN(-EINVAL);
114                 }
115                 stripe_count = lsm->lsm_stripe_count;
116         }
117
118         /* XXX LOV STACKING call into osc for sizes */
119         lmm_size = lov_mds_md_size(stripe_count);
120
121         if (!lmmp)
122                 RETURN(lmm_size);
123
124         if (*lmmp && !lsm) {
125                 stripe_count = le32_to_cpu((*lmmp)->lmm_stripe_count);
126                 OBD_FREE(*lmmp, lov_mds_md_size(stripe_count));
127                 *lmmp = NULL;
128                 RETURN(0);
129         }
130
131         if (!*lmmp) {
132                 OBD_ALLOC(*lmmp, lmm_size);
133                 if (!*lmmp)
134                         RETURN(-ENOMEM);
135         }
136
137         lmm = *lmmp;
138         lmm->lmm_magic = cpu_to_le32(LOV_MAGIC); /* only write new format */
139
140         if (!lsm)
141                 RETURN(lmm_size);
142
143         lmm->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
144         lmm->lmm_object_gr = cpu_to_le64(lsm->lsm_object_gr);
145         lmm->lmm_stripe_size = cpu_to_le32(lsm->lsm_stripe_size);
146         lmm->lmm_stripe_count = cpu_to_le32(stripe_count);
147         lmm->lmm_pattern = cpu_to_le32(lsm->lsm_pattern);
148
149         for (i = 0, loi = lsm->lsm_oinfo; i < stripe_count; i++, loi++) {
150                 /* XXX LOV STACKING call down to osc_packmd() to do packing */
151                 LASSERT(loi->loi_id);
152                 lmm->lmm_objects[i].l_object_id = cpu_to_le64(loi->loi_id);
153                 lmm->lmm_objects[i].l_object_gr = cpu_to_le64(loi->loi_gr);
154                 lmm->lmm_objects[i].l_ost_gen = cpu_to_le32(loi->loi_ost_gen);
155                 lmm->lmm_objects[i].l_ost_idx = cpu_to_le32(loi->loi_ost_idx);
156         }
157
158         RETURN(lmm_size);
159 }
160
161 int lov_get_stripecnt(struct lov_obd *lov, int stripe_count)
162 {
163         if (!stripe_count)
164                 stripe_count = lov->desc.ld_default_stripe_count;
165         if (!stripe_count || stripe_count > lov->desc.ld_active_tgt_count)
166                 stripe_count = lov->desc.ld_active_tgt_count;
167
168         return stripe_count;
169 }
170
171 static int lov_verify_lmm_v0(struct lov_mds_md_v0 *lmm, int lmm_bytes,
172                              int *stripe_count)
173 {
174         if (lmm_bytes < sizeof(*lmm)) {
175                 CERROR("lov_mds_md too small: %d, need at least %d\n",
176                        lmm_bytes, (int)sizeof(*lmm));
177                 return -EINVAL;
178         }
179
180         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
181
182         if (*stripe_count == 0 ||
183             *stripe_count > le32_to_cpu(lmm->lmm_ost_count)) {
184                 CERROR("bad stripe count %d\n", *stripe_count);
185                 lov_dump_lmm_v0(D_WARNING, lmm);
186                 return -EINVAL;
187         }
188
189         if (lmm_bytes < lov_mds_md_v0_size(*stripe_count)) {
190                 CERROR("LOV EA too small: %d, need %d\n",
191                        lmm_bytes, lov_mds_md_v0_size(*stripe_count));
192                 lov_dump_lmm_v0(D_WARNING, lmm);
193                 return -EINVAL;
194         }
195
196         if (lmm->lmm_object_id == 0) {
197                 CERROR("zero object id\n");
198                 lov_dump_lmm_v0(D_WARNING, lmm);
199                 return -EINVAL;
200         }
201
202         if (le32_to_cpu(lmm->lmm_stripe_offset) >
203             le32_to_cpu(lmm->lmm_ost_count)) {
204                 CERROR("stripe offset %d more than number of OSTs %d\n",
205                        le32_to_cpu(lmm->lmm_stripe_offset),
206                        le32_to_cpu(lmm->lmm_ost_count));
207                 lov_dump_lmm_v0(D_WARNING, lmm);
208                 return -EINVAL;
209         }
210
211         if (lmm->lmm_stripe_size == 0) {
212                 CERROR("zero stripe size\n");
213                 lov_dump_lmm_v0(D_WARNING, lmm);
214                 return -EINVAL;
215         }
216
217         return 0;
218 }
219
220 static int lov_verify_lmm_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
221                              int *stripe_count)
222 {
223         if (lmm_bytes < sizeof(*lmm)) {
224                 CERROR("lov_mds_md too small: %d, need at least %d\n",
225                        lmm_bytes, (int)sizeof(*lmm));
226                 return -EINVAL;
227         }
228
229         if (lmm->lmm_magic != le32_to_cpu(LOV_MAGIC_V1)) {
230                 CERROR("bad disk LOV MAGIC: 0x%08X\n",
231                        le32_to_cpu(*(__u32 *)lmm));
232                 return -EINVAL;
233         }
234
235         *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
236
237         if (*stripe_count == 0) {
238                 CERROR("bad stripe count %d\n", *stripe_count);
239                 lov_dump_lmm_v1(D_WARNING, lmm);
240                 return -EINVAL;
241         }
242
243         if (lmm_bytes < lov_mds_md_v1_size(*stripe_count)) {
244                 CERROR("LOV EA too small: %d, need %d\n",
245                        lmm_bytes, lov_mds_md_v1_size(*stripe_count));
246                 lov_dump_lmm_v1(D_WARNING, lmm);
247                 return -EINVAL;
248         }
249
250         if (lmm->lmm_object_id == 0) {
251                 CERROR("zero object id\n");
252                 lov_dump_lmm_v1(D_WARNING, lmm);
253                 return -EINVAL;
254         }
255
256         if (lmm->lmm_pattern != cpu_to_le32(LOV_PATTERN_RAID0)) {
257                 CERROR("bad striping pattern\n");
258                 lov_dump_lmm_v1(D_WARNING, lmm);
259                 return -EINVAL;
260         }
261
262         if (lmm->lmm_stripe_size == 0 ||
263             (__u64)le32_to_cpu(lmm->lmm_stripe_size) * *stripe_count > ~0UL) {
264                 CERROR("bad stripe size %u\n",
265                        le32_to_cpu(lmm->lmm_stripe_size));
266                 lov_dump_lmm_v1(D_WARNING, lmm);
267                 return -EINVAL;
268         }
269
270         return 0;
271 }
272
273 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
274 {
275         switch (le32_to_cpu(*(__u32 *)lmm)) {
276         case LOV_MAGIC_V1:
277                 return lov_verify_lmm_v1(lmm, lmm_bytes, stripe_count);
278         case LOV_MAGIC_V0:
279                 return lov_verify_lmm_v0(lmm, lmm_bytes, stripe_count);
280         default:
281                 CERROR("bad disk LOV MAGIC: 0x%08X\n",
282                        le32_to_cpu(*(__u32 *)lmm));
283                 return -EINVAL;
284         }
285 }
286
287 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count, int pattern)
288 {
289         int lsm_size = lov_stripe_md_size(stripe_count);
290         struct lov_oinfo *loi;
291         int i;
292
293         OBD_ALLOC(*lsmp, lsm_size);
294         if (!*lsmp)
295                 return -ENOMEM;
296
297         (*lsmp)->lsm_magic = LOV_MAGIC;
298         (*lsmp)->lsm_stripe_count = stripe_count;
299         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES * stripe_count;
300         (*lsmp)->lsm_xfersize = PTLRPC_MAX_BRW_SIZE * stripe_count;
301         (*lsmp)->lsm_pattern = pattern;
302         (*lsmp)->lsm_oinfo[0].loi_ost_idx = ~0;
303
304         for (i = 0, loi = (*lsmp)->lsm_oinfo; i < stripe_count; i++, loi++)
305                 loi_init(loi);
306
307         return lsm_size;
308 }
309 EXPORT_SYMBOL(lov_alloc_memmd);
310
311 void lov_free_memmd(struct lov_stripe_md **lsmp)
312 {
313         OBD_FREE(*lsmp, lov_stripe_md_size((*lsmp)->lsm_stripe_count));
314         *lsmp = NULL;
315 }
316 EXPORT_SYMBOL(lov_free_memmd);
317
318 int lov_unpackmd_v0(struct lov_obd *lov, struct lov_stripe_md *lsm,
319                     struct lov_mds_md_v0 *lmm)
320 {
321         struct lov_oinfo *loi;
322         int i, ost_offset, ost_count;
323
324         lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
325         /* lsm->lsm_object_gr = 0; implicit */
326         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
327         lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
328         lsm->lsm_pattern = LOV_PATTERN_RAID0;
329         ost_offset = le32_to_cpu(lmm->lmm_stripe_offset);
330         ost_count = le16_to_cpu(lmm->lmm_ost_count);
331
332         for (i = 0, loi = lsm->lsm_oinfo; i < ost_count; i++, ost_offset++) {
333                 ost_offset %= ost_count;
334
335                 if (!lmm->lmm_objects[ost_offset].l_object_id)
336                         continue;
337
338                 loi->loi_id =
339                         le64_to_cpu(lmm->lmm_objects[ost_offset].l_object_id);
340                 /* loi->loi_gr = 0; implicit */
341                 loi->loi_ost_idx = ost_offset;
342                 /* loi->loi_ost_gen = 0; implicit */
343                 loi++;
344         }
345
346         if (loi - lsm->lsm_oinfo != lsm->lsm_stripe_count) {
347                 CERROR("missing objects in lmm struct\n");
348                 lov_dump_lmm_v0(D_WARNING, lmm);
349                 return -EINVAL;
350         }
351
352         return 0;
353 }
354
355 int lov_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
356                     struct lov_mds_md_v1 *lmm)
357 {
358         struct lov_oinfo *loi;
359         int i;
360
361         lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
362         lsm->lsm_object_gr = le64_to_cpu(lmm->lmm_object_gr);
363         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
364         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
365         lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
366
367         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++) {
368                 /* XXX LOV STACKING call down to osc_unpackmd() */
369                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
370                 loi->loi_gr = le64_to_cpu(lmm->lmm_objects[i].l_object_gr);
371                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
372                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
373                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
374                         CERROR("OST index %d more than OST count %d\n",
375                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
376                         lov_dump_lmm_v1(D_WARNING, lmm);
377                         return -EINVAL;
378                 }
379                 loi++;
380         }
381
382         return 0;
383 }
384
385 /* Unpack LOV object metadata from disk storage.  It is packed in LE byte
386  * order and is opaque to the networking layer.
387  */
388 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
389                  struct lov_mds_md *lmm, int lmm_bytes)
390 {
391         struct obd_device *obd = class_exp2obd(exp);
392         struct lov_obd *lov = &obd->u.lov;
393         int rc = 0, stripe_count, lsm_size;
394         ENTRY;
395
396         /* If passed an MDS struct use values from there, otherwise defaults */
397         if (lmm) {
398                 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
399                 if (rc)
400                         RETURN(rc);
401         } else {
402                 stripe_count = lov_get_stripecnt(lov, 0);
403         }
404
405         /* If we aren't passed an lsmp struct, we just want the size */
406         if (!lsmp)
407                 /* XXX LOV STACKING call into osc for sizes */
408                 RETURN(lov_stripe_md_size(stripe_count));
409
410         /* If we are passed an allocated struct but nothing to unpack, free */
411         if (*lsmp && !lmm) {
412                 lov_free_memmd(lsmp);
413                 RETURN(0);
414         }
415
416         lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0);
417         if (lsm_size < 0)
418                 RETURN(lsm_size);
419
420         /* If we are passed a pointer but nothing to unpack, we only alloc */
421         if (!lmm)
422                 RETURN(lsm_size);
423
424         switch (le32_to_cpu(lmm->lmm_magic)) {
425         case LOV_MAGIC_V1:
426                 rc = lov_unpackmd_v1(lov, *lsmp, lmm);
427                 break;
428         case LOV_MAGIC_V0:
429                 rc = lov_unpackmd_v0(lov, *lsmp, (void *)lmm);
430                 break;
431         }
432
433         if (rc) {
434                 lov_free_memmd(lsmp);
435                 RETURN(rc);
436         }
437
438         RETURN(lsm_size);
439 }
440
441 /* Configure object striping information on a new file.
442  *
443  * @lmmu is a pointer to a user struct with one or more of the fields set to
444  * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
445  * lmm_stripe_offset, and lmm_stripe_pattern.  lmm_magic must be LOV_MAGIC.
446  * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
447  */
448 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
449                   struct lov_user_md *lump)
450 {
451         struct obd_device *obd = class_exp2obd(exp);
452         struct lov_obd *lov = &obd->u.lov;
453         struct lov_user_md lum;
454         int stripe_count;
455         int rc;
456         ENTRY;
457
458         rc = copy_from_user(&lum, lump, sizeof(lum));
459         if (rc)
460                 RETURN(-EFAULT);
461
462         if (lum.lmm_magic != LOV_USER_MAGIC) {
463                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x\n",
464                        lum.lmm_magic, LOV_USER_MAGIC);
465                 RETURN(-EINVAL);
466         }
467
468         if (lum.lmm_pattern == 0) {
469                 lum.lmm_pattern = lov->desc.ld_pattern ?
470                         lov->desc.ld_pattern : LOV_PATTERN_RAID0;
471         }
472
473         if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
474                 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
475                        lum.lmm_pattern);
476                 RETURN(-EINVAL);
477         }
478
479         if (lum.lmm_stripe_size & (PAGE_SIZE - 1)) {
480                 CDEBUG(D_IOCTL, "stripe size %u not multiple of %lu\n",
481                        lum.lmm_stripe_size, PAGE_SIZE);
482                 RETURN(-EINVAL);
483         }
484
485         if ((lum.lmm_stripe_offset >= lov->desc.ld_active_tgt_count) &&
486             (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
487                 CDEBUG(D_IOCTL, "stripe offset %u > number of active OSTs %u\n",
488                        lum.lmm_stripe_offset, lov->desc.ld_active_tgt_count);
489                 RETURN(-EINVAL);
490         }
491         stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
492
493         if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
494                 CDEBUG(D_IOCTL, "stripe width %ux%u > %lu on 32-bit system\n",
495                        lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
496                 RETURN(-EINVAL);
497         }
498
499         rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern);
500
501         if (rc < 0)
502                 RETURN(rc);
503
504         (*lsmp)->lsm_oinfo[0].loi_ost_idx = lum.lmm_stripe_offset;
505         (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
506         (*lsmp)->lsm_xfersize = lum.lmm_stripe_size * stripe_count;
507
508         RETURN(0);
509 }
510
511 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
512               struct lov_user_md *lump)
513 {
514         int i;
515         int rc;
516         struct obd_export *oexp;
517         struct lov_obd *lov = &exp->exp_obd->u.lov;
518         obd_id last_id = 0;
519
520         for (i = 0; i < lump->lmm_stripe_count; i++) {
521                 __u32 len = sizeof(last_id);
522                 oexp = lov->tgts[lump->lmm_objects[i].l_ost_idx].ltd_exp;
523                 rc = obd_get_info(oexp, strlen("last_id"), "last_id",
524                                   &len, &last_id);
525                 if (rc)
526                         RETURN(rc);
527                 if (lump->lmm_objects[i].l_object_id > last_id) {
528                         CERROR("Setting EA for object > than last id on "
529                                "ost idx %d "LPD64" > "LPD64" \n",
530                                lump->lmm_objects[i].l_ost_idx,
531                                lump->lmm_objects[i].l_object_id, last_id);
532                         RETURN(-EINVAL);
533                 }
534         }
535
536         rc = lov_setstripe(exp, lsmp, lump);
537         if (rc)
538                 RETURN(rc);
539
540         for (i = 0; i < lump->lmm_stripe_count; i++) {
541                 (*lsmp)->lsm_oinfo[i].loi_ost_idx =
542                         lump->lmm_objects[i].l_ost_idx;
543                 (*lsmp)->lsm_oinfo[i].loi_id = lump->lmm_objects[i].l_object_id;
544                 (*lsmp)->lsm_oinfo[i].loi_gr = lump->lmm_objects[i].l_object_gr;
545         }
546         RETURN(0);
547 }
548
549
550 /* Retrieve object striping information.
551  *
552  * @lump is a pointer to an in-core struct with lmm_ost_count indicating
553  * the maximum number of OST indices which will fit in the user buffer.
554  * lmm_magic must be LOV_USER_MAGIC.
555  */
556 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
557                   struct lov_user_md *lump)
558 {
559         struct lov_user_md lum;
560         struct lov_mds_md *lmmk = NULL;
561         int rc, lmm_size;
562         ENTRY;
563
564         if (!lsm)
565                 RETURN(-ENODATA);
566
567         rc = copy_from_user(&lum, lump, sizeof(lum));
568         if (rc)
569                 RETURN(-EFAULT);
570
571         if (lum.lmm_magic != LOV_USER_MAGIC)
572                 RETURN(-EINVAL);
573
574         rc = lov_packmd(exp, &lmmk, lsm);
575         if (rc < 0)
576                 RETURN(rc);
577         lmm_size = rc;
578         rc = 0;
579
580         /* FIXME: Bug 1185 - copy fields properly when structs change */
581         LASSERT(sizeof(lum) == sizeof(*lmmk));
582         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lmmk->lmm_objects[0]));
583
584         /* User wasn't expecting this many OST entries */
585         if (lum.lmm_stripe_count == 0) {
586                 rc = copy_to_user(lump, lmmk, sizeof(lum));
587                 if (rc)
588                         rc = -EFAULT;
589         } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
590                 rc = -EOVERFLOW;
591         } else if (copy_to_user(lump, lmmk, lmm_size)) {
592                 rc = -EFAULT;
593         }
594
595         obd_free_diskmd(exp, &lmmk);
596
597         RETURN(rc);
598 }