Whamcloud - gitweb
land b_ost_amd onto HEAD.
[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                         CERROR("bad mem 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 void lov_free_memmd(struct lov_stripe_md **lsmp)
311 {
312         OBD_FREE(*lsmp, lov_stripe_md_size((*lsmp)->lsm_stripe_count));
313         *lsmp = NULL;
314 }
315 EXPORT_SYMBOL(lov_free_memmd);
316 int lov_unpackmd_v0(struct lov_obd *lov, struct lov_stripe_md *lsm,
317                     struct lov_mds_md_v0 *lmm)
318 {
319         struct lov_oinfo *loi;
320         int i, ost_offset, ost_count;
321
322         lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
323         /* lsm->lsm_object_gr = 0; implicit */
324         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
325         lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
326         lsm->lsm_pattern = LOV_PATTERN_RAID0;
327         ost_offset = le32_to_cpu(lmm->lmm_stripe_offset);
328         ost_count = le16_to_cpu(lmm->lmm_ost_count);
329
330         for (i = 0, loi = lsm->lsm_oinfo; i < ost_count; i++, ost_offset++) {
331                 ost_offset %= ost_count;
332
333                 if (!lmm->lmm_objects[ost_offset].l_object_id)
334                         continue;
335
336                 loi->loi_id =
337                         le64_to_cpu(lmm->lmm_objects[ost_offset].l_object_id);
338                 /* loi->loi_gr = 0; implicit */
339                 loi->loi_ost_idx = ost_offset;
340                 /* loi->loi_ost_gen = 0; implicit */
341                 loi++;
342         }
343
344         if (loi - lsm->lsm_oinfo != lsm->lsm_stripe_count) {
345                 CERROR("missing objects in lmm struct\n");
346                 lov_dump_lmm_v0(D_WARNING, lmm);
347                 return -EINVAL;
348         }
349
350         return 0;
351 }
352
353 int lov_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
354                     struct lov_mds_md_v1 *lmm)
355 {
356         struct lov_oinfo *loi;
357         int i;
358
359         lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
360         lsm->lsm_object_gr = le64_to_cpu(lmm->lmm_object_gr);
361         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
362         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
363         lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
364
365         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++) {
366                 /* XXX LOV STACKING call down to osc_unpackmd() */
367                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
368                 loi->loi_gr = le64_to_cpu(lmm->lmm_objects[i].l_object_gr);
369                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
370                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
371                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
372                         CERROR("OST index %d more than OST count %d\n",
373                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
374                         lov_dump_lmm_v1(D_WARNING, lmm);
375                         return -EINVAL;
376                 }
377                 loi++;
378         }
379
380         return 0;
381 }
382
383 /* Unpack LOV object metadata from disk storage.  It is packed in LE byte
384  * order and is opaque to the networking layer.
385  */
386 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
387                  struct lov_mds_md *lmm, int lmm_bytes)
388 {
389         struct obd_device *obd = class_exp2obd(exp);
390         struct lov_obd *lov = &obd->u.lov;
391         int rc = 0, stripe_count, lsm_size;
392         ENTRY;
393
394         /* If passed an MDS struct use values from there, otherwise defaults */
395         if (lmm) {
396                 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
397                 if (rc)
398                         RETURN(rc);
399         } else {
400                 stripe_count = lov_get_stripecnt(lov, 0);
401         }
402
403         /* If we aren't passed an lsmp struct, we just want the size */
404         if (!lsmp)
405                 /* XXX LOV STACKING call into osc for sizes */
406                 RETURN(lov_stripe_md_size(stripe_count));
407
408         /* If we are passed an allocated struct but nothing to unpack, free */
409         if (*lsmp && !lmm) {
410                 lov_free_memmd(lsmp);
411                 RETURN(0);
412         }
413
414         lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0);
415         if (lsm_size < 0)
416                 RETURN(lsm_size);
417
418         /* If we are passed a pointer but nothing to unpack, we only alloc */
419         if (!lmm)
420                 RETURN(lsm_size);
421
422         switch (le32_to_cpu(lmm->lmm_magic)) {
423         case LOV_MAGIC_V1:
424                 rc = lov_unpackmd_v1(lov, *lsmp, lmm);
425                 break;
426         case LOV_MAGIC_V0:
427                 rc = lov_unpackmd_v0(lov, *lsmp, (void *)lmm);
428                 break;
429         }
430
431         if (rc) {
432                 lov_free_memmd(lsmp);
433                 RETURN(rc);
434         }
435
436         RETURN(lsm_size);
437 }
438
439 /* Configure object striping information on a new file.
440  *
441  * @lmmu is a pointer to a user struct with one or more of the fields set to
442  * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
443  * lmm_stripe_offset, and lmm_stripe_pattern.  lmm_magic must be LOV_MAGIC.
444  * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
445  */
446 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
447                   struct lov_user_md *lump)
448 {
449         struct obd_device *obd = class_exp2obd(exp);
450         struct lov_obd *lov = &obd->u.lov;
451         struct lov_user_md lum;
452         int stripe_count;
453         int rc;
454         ENTRY;
455
456         rc = copy_from_user(&lum, lump, sizeof(lum));
457         if (rc)
458                 RETURN(-EFAULT);
459
460         if (lum.lmm_magic != LOV_USER_MAGIC) {
461                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x\n",
462                        lum.lmm_magic, LOV_USER_MAGIC);
463                 RETURN(-EINVAL);
464         }
465
466         if (lum.lmm_pattern == 0) {
467                 lum.lmm_pattern = lov->desc.ld_pattern ?
468                         lov->desc.ld_pattern : LOV_PATTERN_RAID0;
469         }
470
471         if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
472                 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
473                        lum.lmm_pattern);
474                 RETURN(-EINVAL);
475         }
476
477         if (lum.lmm_stripe_size & (PAGE_SIZE - 1)) {
478                 CDEBUG(D_IOCTL, "stripe size %u not multiple of %lu\n",
479                        lum.lmm_stripe_size, PAGE_SIZE);
480                 RETURN(-EINVAL);
481         }
482
483         if ((lum.lmm_stripe_offset >= lov->desc.ld_active_tgt_count) &&
484             (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
485                 CDEBUG(D_IOCTL, "stripe offset %u > number of active OSTs %u\n",
486                        lum.lmm_stripe_offset, lov->desc.ld_active_tgt_count);
487                 RETURN(-EINVAL);
488         }
489         stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
490
491         if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
492                 CDEBUG(D_IOCTL, "stripe width %ux%u > %lu on 32-bit system\n",
493                        lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
494                 RETURN(-EINVAL);
495         }
496
497         rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern);
498
499         if (rc < 0)
500                 RETURN(rc);
501
502         (*lsmp)->lsm_oinfo[0].loi_ost_idx = lum.lmm_stripe_offset;
503         (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
504         (*lsmp)->lsm_xfersize = lum.lmm_stripe_size * stripe_count;
505
506         RETURN(0);
507 }
508
509 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
510               struct lov_user_md *lump)
511 {
512         int i;
513         int rc;
514         struct obd_export *oexp;
515         struct lov_obd *lov = &exp->exp_obd->u.lov;
516         obd_id last_id = 0;
517
518         for (i = 0; i < lump->lmm_stripe_count; i++) {
519                 __u32 len = sizeof(last_id);
520                 oexp = lov->tgts[lump->lmm_objects[i].l_ost_idx].ltd_exp;
521                 rc = obd_get_info(oexp, strlen("last_id"), "last_id",
522                                   &len, &last_id);
523                 if (rc)
524                         RETURN(rc);
525                 if (lump->lmm_objects[i].l_object_id > last_id) {
526                         CERROR("Setting EA for object > than last id on "
527                                "ost idx %d "LPD64" > "LPD64" \n",
528                                lump->lmm_objects[i].l_ost_idx,
529                                lump->lmm_objects[i].l_object_id, last_id);
530                         RETURN(-EINVAL);
531                 }
532         }
533
534         rc = lov_setstripe(exp, lsmp, lump);
535         if (rc)
536                 RETURN(rc);
537
538         for (i = 0; i < lump->lmm_stripe_count; i++) {
539                 (*lsmp)->lsm_oinfo[i].loi_ost_idx =
540                         lump->lmm_objects[i].l_ost_idx;
541                 (*lsmp)->lsm_oinfo[i].loi_id = lump->lmm_objects[i].l_object_id;
542                 (*lsmp)->lsm_oinfo[i].loi_gr = lump->lmm_objects[i].l_object_gr;
543         }
544         RETURN(0);
545 }
546
547
548 /* Retrieve object striping information.
549  *
550  * @lump is a pointer to an in-core struct with lmm_ost_count indicating
551  * the maximum number of OST indices which will fit in the user buffer.
552  * lmm_magic must be LOV_USER_MAGIC.
553  */
554 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
555                   struct lov_user_md *lump)
556 {
557         struct lov_user_md lum;
558         struct lov_mds_md *lmmk = NULL;
559         int rc, lmm_size;
560         ENTRY;
561
562         if (!lsm)
563                 RETURN(-ENODATA);
564
565         rc = copy_from_user(&lum, lump, sizeof(lum));
566         if (rc)
567                 RETURN(-EFAULT);
568
569         if (lum.lmm_magic != LOV_USER_MAGIC)
570                 RETURN(-EINVAL);
571
572         rc = lov_packmd(exp, &lmmk, lsm);
573         if (rc < 0)
574                 RETURN(rc);
575         lmm_size = rc;
576         rc = 0;
577
578         /* FIXME: Bug 1185 - copy fields properly when structs change */
579         LASSERT(sizeof(lum) == sizeof(*lmmk));
580         LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lmmk->lmm_objects[0]));
581
582         /* User wasn't expecting this many OST entries */
583         if (lum.lmm_stripe_count == 0) {
584                 if (copy_to_user(lump, lmmk, sizeof(lum)))
585                         rc = -EFAULT;
586         } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
587                 rc = -EOVERFLOW;
588         } else if (copy_to_user(lump, lmmk, lmm_size)) {
589                 rc = -EFAULT;
590         }
591
592         obd_free_diskmd(exp, &lmmk);
593
594         RETURN(rc);
595 }