Whamcloud - gitweb
b=14836
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 void lov_dump_lmm_v3(int level, struct lov_mds_md_v3 *lmm)
98 {
99         struct lov_ost_data_v1 *lod;
100         int i;
101
102         CDEBUG(level, "objid "LPX64", magic 0x%08x, pattern %#x\n",
103                le64_to_cpu(lmm->lmm_object_id), le32_to_cpu(lmm->lmm_magic),
104                le32_to_cpu(lmm->lmm_pattern));
105         CDEBUG(level,"stripe_size %u, stripe_count %u\n",
106                le32_to_cpu(lmm->lmm_stripe_size),
107                le32_to_cpu(lmm->lmm_stripe_count));
108         CDEBUG(level,"pool_name "POOLNAMEF"\n", lmm->lmm_pool_name);
109
110         if (le32_to_cpu(lmm->lmm_stripe_count) <= LOV_V1_INSANE_STRIPE_COUNT) {
111                 for (i = 0, lod = lmm->lmm_objects;
112                      i < (int)le32_to_cpu(lmm->lmm_stripe_count); i++, lod++)
113                          CDEBUG(level,
114                                 "stripe %u idx %u subobj "LPX64"/"LPX64"\n",
115                                 i, le32_to_cpu(lod->l_ost_idx),
116                                 le64_to_cpu(lod->l_object_gr),
117                                 le64_to_cpu(lod->l_object_id));
118         } else {
119                 CDEBUG(level, "bad stripe_count %u > max_stripe_count %u\n",
120                        le32_to_cpu(lmm->lmm_stripe_count),
121                        LOV_V1_INSANE_STRIPE_COUNT);
122         }
123 }
124
125 void lov_dump_lmm(int level, void *lmm)
126 {
127         int magic;
128
129         magic = ((struct lov_mds_md_v1 *)(lmm))->lmm_magic;
130         switch (magic) {
131         case LOV_MAGIC_V1:
132                 return lov_dump_lmm_v1(level, (struct lov_mds_md_v1 *)(lmm));
133         case LOV_MAGIC_JOIN:
134                 return lov_dump_lmm_join(level, (struct lov_mds_md_join *)(lmm));
135         case LOV_MAGIC_V3:
136                 return lov_dump_lmm_v3(level, (struct lov_mds_md_v3 *)(lmm));
137         default:
138                 CERROR("Cannot recognize lmm_magic %x", magic);
139         }
140         return;
141 }
142
143 #define LMM_ASSERT(test)                                                \
144 do {                                                                    \
145         if (!(test)) lov_dump_lmm(D_ERROR, lmm);                        \
146         LASSERT(test); /* so we know what assertion failed */           \
147 } while(0)
148
149 /* Pack LOV object metadata for disk storage.  It is packed in LE byte
150  * order and is opaque to the networking layer.
151  *
152  * XXX In the future, this will be enhanced to get the EA size from the
153  *     underlying OSC device(s) to get their EA sizes so we can stack
154  *     LOVs properly.  For now lov_mds_md_size() just assumes one obd_id
155  *     per stripe.
156  */
157 int lov_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
158                struct lov_stripe_md *lsm)
159 {
160         struct obd_device *obd = class_exp2obd(exp);
161         struct lov_obd *lov = &obd->u.lov;
162         struct lov_mds_md_v1 *lmmv1;
163         struct lov_mds_md_v3 *lmmv3;
164         int stripe_count = lov->desc.ld_tgt_count;
165         struct lov_ost_data_v1 *lmm_objects;
166         int lmm_size, lmm_magic;
167         int i;
168         ENTRY;
169
170         if (lsm) {
171                 lmm_magic = lsm->lsm_magic;
172
173                 /* If we are just sizing the EA, limit the stripe count
174                  * to the actual number of OSTs in this filesystem. */
175                 if (!lmmp) {
176                         stripe_count = lov_get_stripecnt(lov,
177                                                          lsm->lsm_stripe_count);
178                         lsm->lsm_stripe_count = stripe_count;
179                 } else {
180                         stripe_count = lsm->lsm_stripe_count;
181                 }
182         } else if (lmmp && *lmmp) {
183                 lmm_magic = le32_to_cpu((*lmmp)->lmm_magic);
184         } else {
185                 /* lsm == NULL and lmmp == NULL */
186                 lmm_magic = LOV_MAGIC;
187         }
188
189         if ((lmm_magic != LOV_MAGIC_V1) &&
190             (lmm_magic != LOV_MAGIC_V3)) {
191                 CERROR("bad mem LOV MAGIC: 0x%08X != 0x%08X nor 0x%08X\n",
192                         lmm_magic, LOV_MAGIC_V1, LOV_MAGIC_V3);
193                 RETURN(-EINVAL);
194
195         }
196
197         /* XXX LOV STACKING call into osc for sizes */
198         lmm_size = lov_mds_md_size(stripe_count, lmm_magic);
199
200         if (!lmmp)
201                 RETURN(lmm_size);
202
203         if (*lmmp && !lsm) {
204                 stripe_count = le32_to_cpu((*lmmp)->lmm_stripe_count);
205                 lmm_size = lov_mds_md_size(stripe_count, lmm_magic);
206                 OBD_FREE(*lmmp, lmm_size);
207                 *lmmp = NULL;
208                 RETURN(0);
209         }
210
211         if (!*lmmp) {
212                 OBD_ALLOC(*lmmp, lmm_size);
213                 if (!*lmmp)
214                         RETURN(-ENOMEM);
215         }
216
217         CDEBUG(D_INFO, "lov_packmd: LOV_MAGIC 0x%08X, lmm_size = %d \n",
218                lmm_magic, lmm_size);
219
220         lmmv1 = *lmmp;
221         lmmv3 = (struct lov_mds_md_v3 *)*lmmp;
222         if (lmm_magic == LOV_MAGIC_V3)
223                 lmmv3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
224         else
225                 lmmv1->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
226
227         if (!lsm)
228                 RETURN(lmm_size);
229
230         /* lmmv1 and lmmv3 point to the same struct and have the
231          * same first fields
232          */
233         lmmv1->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
234         lmmv1->lmm_object_gr = cpu_to_le64(lsm->lsm_object_gr);
235         lmmv1->lmm_stripe_size = cpu_to_le32(lsm->lsm_stripe_size);
236         lmmv1->lmm_stripe_count = cpu_to_le32(stripe_count);
237         lmmv1->lmm_pattern = cpu_to_le32(lsm->lsm_pattern);
238         if (lsm->lsm_magic == LOV_MAGIC_V3) {
239                 strncpy(lmmv3->lmm_pool_name, lsm->lsm_pool_name, MAXPOOLNAME);
240                 lmm_objects = lmmv3->lmm_objects;
241         } else {
242                 lmm_objects = lmmv1->lmm_objects;
243         }
244
245         for (i = 0; i < stripe_count; i++) {
246                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
247
248                 /* XXX LOV STACKING call down to osc_packmd() to do packing */
249                 LASSERTF(loi->loi_id, "lmm_oid "LPU64" stripe %u/%u idx %u\n",
250                          lmmv1->lmm_object_id, i, stripe_count, loi->loi_ost_idx);
251                 lmm_objects[i].l_object_id = cpu_to_le64(loi->loi_id);
252                 lmm_objects[i].l_object_gr = cpu_to_le64(loi->loi_gr);
253                 lmm_objects[i].l_ost_gen = cpu_to_le32(loi->loi_ost_gen);
254                 lmm_objects[i].l_ost_idx = cpu_to_le32(loi->loi_ost_idx);
255         }
256
257         RETURN(lmm_size);
258 }
259
260 /* Find the max stripecount we should use */
261 int lov_get_stripecnt(struct lov_obd *lov, __u32 stripe_count)
262 {
263         if (!stripe_count)
264                 stripe_count = lov->desc.ld_default_stripe_count;
265         if (stripe_count > lov->desc.ld_active_tgt_count)
266                 stripe_count = lov->desc.ld_active_tgt_count;
267         if (!stripe_count)
268                 stripe_count = 1;
269         /* for now, we limit the stripe count directly, when bug 4424 is
270          * fixed this needs to be somewhat dynamic based on whether ext3
271          * can handle larger EA sizes. */
272         if (stripe_count > LOV_MAX_STRIPE_COUNT)
273                 stripe_count = LOV_MAX_STRIPE_COUNT;
274
275         return stripe_count;
276 }
277
278
279 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
280 {
281         int rc;
282
283         if (lsm_op_find(le32_to_cpu(*(__u32 *)lmm)) == NULL) {
284                 char *buffer;
285                 int sz;
286
287                 CERROR("bad disk LOV MAGIC: 0x%08X; dumping LMM (size=%d):\n",
288                        le32_to_cpu(*(__u32 *)lmm), lmm_bytes);
289                 sz = lmm_bytes * 2 + 1;
290                 OBD_ALLOC(buffer, sz);
291                 if (buffer != NULL) {
292                         int i;
293
294                         for (i = 0; i < lmm_bytes; i++)
295                                 sprintf(buffer+2*i, "%.2X", ((char *)lmm)[i]);
296                         buffer[sz] = '\0';
297                         CERROR("%s\n", buffer);
298                         OBD_FREE(buffer, sz);
299                 }
300                 return -EINVAL;
301         }
302         rc = lsm_op_find(le32_to_cpu(*(__u32 *)lmm))->lsm_lmm_verify(lmm,
303                                      lmm_bytes, stripe_count);
304         return rc;
305 }
306
307 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count,
308                       int pattern, int magic)
309 {
310         int i, lsm_size;
311         ENTRY;
312
313         CDEBUG(D_INFO, "alloc lsm, stripe_count %d\n", stripe_count);
314
315         *lsmp = lsm_alloc_plain(stripe_count, &lsm_size);
316         if (!*lsmp) {
317                 CERROR("can't allocate lsmp stripe_count %d\n", stripe_count);
318                 RETURN(-ENOMEM);
319         }
320
321         spin_lock_init(&(*lsmp)->lsm_lock);
322         (*lsmp)->lsm_magic = magic;
323         (*lsmp)->lsm_stripe_count = stripe_count;
324         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES * stripe_count;
325         (*lsmp)->lsm_pattern = pattern;
326         (*lsmp)->lsm_pool_name[0] = '\0';
327         (*lsmp)->lsm_oinfo[0]->loi_ost_idx = ~0;
328
329         for (i = 0; i < stripe_count; i++)
330                 loi_init((*lsmp)->lsm_oinfo[i]);
331
332         RETURN(lsm_size);
333 }
334
335 void lov_free_memmd(struct lov_stripe_md **lsmp)
336 {
337         struct lov_stripe_md *lsm = *lsmp;
338
339         LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
340         lsm_op_find(lsm->lsm_magic)->lsm_free(lsm);
341
342         *lsmp = NULL;
343 }
344
345
346 /* Unpack LOV object metadata from disk storage.  It is packed in LE byte
347  * order and is opaque to the networking layer.
348  */
349 int lov_unpackmd(struct obd_export *exp,  struct lov_stripe_md **lsmp,
350                  struct lov_mds_md *lmm, int lmm_bytes)
351 {
352         struct obd_device *obd = class_exp2obd(exp);
353         struct lov_obd *lov = &obd->u.lov;
354         int rc = 0, stripe_count, lsm_size;
355         __u32 magic;
356         ENTRY;
357
358         /* If passed an MDS struct use values from there, otherwise defaults */
359         if (lmm) {
360                 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
361                 if (rc)
362                         RETURN(rc);
363                 magic = le32_to_cpu(lmm->lmm_magic);
364         } else {
365                 stripe_count = lov_get_stripecnt(lov, 0);
366                 magic = LOV_MAGIC;
367         }
368
369         /* If we aren't passed an lsmp struct, we just want the size */
370         if (!lsmp) {
371                 /* XXX LOV STACKING call into osc for sizes */
372                 LBUG();
373                 RETURN(lov_stripe_md_size(stripe_count));
374         }
375         /* If we are passed an allocated struct but nothing to unpack, free */
376         if (*lsmp && !lmm) {
377                 lov_free_memmd(lsmp);
378                 RETURN(0);
379         }
380
381         lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0,
382                                    magic);
383         if (lsm_size < 0)
384                 RETURN(lsm_size);
385
386         /* If we are passed a pointer but nothing to unpack, we only alloc */
387         if (!lmm)
388                 RETURN(lsm_size);
389
390         LASSERT(lsm_op_find(magic) != NULL);
391         rc = lsm_op_find(magic)->lsm_unpackmd(lov, *lsmp, lmm);
392         if (rc) {
393                 lov_free_memmd(lsmp);
394                 RETURN(rc);
395         }
396
397         RETURN(lsm_size);
398 }
399
400 static int __lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
401                            struct lov_user_md *lump)
402 {
403         struct obd_device *obd = class_exp2obd(exp);
404         struct lov_obd *lov = &obd->u.lov;
405         struct lov_user_md_v3 lumv3;
406         struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
407         int lmm_magic;
408         int stripe_count;
409         int rc;
410         ENTRY;
411
412         rc = copy_from_user(&lumv3, lump, sizeof(struct lov_user_md_v1));
413         if (rc)
414                 RETURN(-EFAULT);
415
416         lmm_magic = lumv1->lmm_magic;
417
418         if (lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
419                 lustre_swab_lov_user_md_v1(lumv1);
420                 lmm_magic = LOV_USER_MAGIC_V1;
421         } else if (lmm_magic == LOV_USER_MAGIC_V3) {
422                 rc = copy_from_user(&lumv3, lump, sizeof(lumv3));
423                 if (rc)
424                         RETURN(-EFAULT);
425         } else if (lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
426                 rc = copy_from_user(&lumv3, lump, sizeof(lumv3));
427                 if (rc)
428                         RETURN(-EFAULT);
429                 lustre_swab_lov_user_md_v3(&lumv3);
430                 lmm_magic = LOV_USER_MAGIC_V3;
431         } else if (lmm_magic != LOV_USER_MAGIC_V1) {
432                 CDEBUG(D_IOCTL,
433                        "bad userland LOV MAGIC: %#08x != %#08x nor %#08x\n",
434                        lmm_magic, LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
435                        RETURN(-EINVAL);
436         }
437
438         /* in the rest of the tests, as *lumv1 and lumv3 have the same
439          * fields, we use lumv1 to avoid code duplication */
440
441         if (lumv1->lmm_pattern == 0) {
442                 lumv1->lmm_pattern = lov->desc.ld_pattern ?
443                         lov->desc.ld_pattern : LOV_PATTERN_RAID0;
444         }
445
446         if (lumv1->lmm_pattern != LOV_PATTERN_RAID0) {
447                 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
448                        lumv1->lmm_pattern);
449                 RETURN(-EINVAL);
450         }
451
452         /* 64kB is the largest common page size we see (ia64), and matches the
453          * check in lfs */
454         if (lumv1->lmm_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
455                 CDEBUG(D_IOCTL, "stripe size %u not multiple of %u, fixing\n",
456                        lumv1->lmm_stripe_size, LOV_MIN_STRIPE_SIZE);
457                 lumv1->lmm_stripe_size = LOV_MIN_STRIPE_SIZE;
458         }
459
460         if ((lumv1->lmm_stripe_offset >= lov->desc.ld_tgt_count) &&
461             (lumv1->lmm_stripe_offset !=
462              (typeof(lumv1->lmm_stripe_offset))(-1))) {
463                 CDEBUG(D_IOCTL, "stripe offset %u > number of OSTs %u\n",
464                        lumv1->lmm_stripe_offset, lov->desc.ld_tgt_count);
465                 RETURN(-EINVAL);
466         }
467         stripe_count = lov_get_stripecnt(lov, lumv1->lmm_stripe_count);
468
469         if (lmm_magic == LOV_USER_MAGIC_V3) {
470                 struct pool_desc *pool;
471
472                 pool = lov_find_pool(lov, lumv3.lmm_pool_name);
473                 if (pool == NULL)
474                         RETURN(-EINVAL);
475
476                 if (lumv3.lmm_stripe_offset !=
477                     (typeof(lumv3.lmm_stripe_offset))(-1)) {
478                         rc = lov_check_index_in_pool(lumv3.lmm_stripe_offset,
479                                                      pool);
480                         if (rc < 0)
481                                 RETURN(-EINVAL);
482                 }
483
484                 if (stripe_count > pool_tgt_count(pool))
485                         stripe_count = pool_tgt_count(pool);
486         }
487
488         if ((__u64)lumv1->lmm_stripe_size * stripe_count > ~0UL) {
489                 CDEBUG(D_IOCTL, "stripe width %ux%i exeeds %lu bytes\n",
490                        lumv1->lmm_stripe_size, (int)lumv1->lmm_stripe_count,
491                        ~0UL);
492                 RETURN(-EINVAL);
493         }
494
495         rc = lov_alloc_memmd(lsmp, stripe_count, lumv1->lmm_pattern, lmm_magic);
496
497         if (rc >= 0) {
498                 (*lsmp)->lsm_oinfo[0]->loi_ost_idx = lumv1->lmm_stripe_offset;
499                 (*lsmp)->lsm_stripe_size = lumv1->lmm_stripe_size;
500                 if (lmm_magic == LOV_USER_MAGIC_V3)
501                         strncpy((*lsmp)->lsm_pool_name, lumv3.lmm_pool_name,
502                                 MAXPOOLNAME);
503                 rc = 0;
504         }
505
506         RETURN(rc);
507 }
508
509 /* Configure object striping information on a new file.
510  *
511  * @lmmu is a pointer to a user struct with one or more of the fields set to
512  * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
513  * lmm_stripe_offset, and lmm_stripe_pattern.  lmm_magic must be LOV_MAGIC.
514  * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
515  */
516 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
517                   struct lov_user_md *lump)
518 {
519         int rc;
520         mm_segment_t seg;
521
522         seg = get_fs();
523         set_fs(KERNEL_DS);
524
525         rc = __lov_setstripe(exp, lsmp, lump);
526         set_fs(seg);
527         RETURN(rc);
528 }
529
530 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
531               struct lov_user_md *lump)
532 {
533         int i;
534         int rc;
535         struct obd_export *oexp;
536         struct lov_obd *lov = &exp->exp_obd->u.lov;
537         obd_id last_id = 0;
538         struct lov_user_ost_data_v1 *lmm_objects;
539
540         ENTRY;
541
542         if (lump->lmm_magic == LOV_USER_MAGIC_V3)
543                 lmm_objects = ((struct lov_user_md_v3 *)lump)->lmm_objects;
544         else
545                 lmm_objects = lump->lmm_objects;
546
547         for (i = 0; i < lump->lmm_stripe_count; i++) {
548                 __u32 len = sizeof(last_id);
549                 oexp = lov->lov_tgts[lmm_objects[i].l_ost_idx]->ltd_exp;
550                 rc = obd_get_info(oexp, sizeof(KEY_LAST_ID), KEY_LAST_ID,
551                                   &len, &last_id, NULL);
552                 if (rc)
553                         RETURN(rc);
554                 if (lmm_objects[i].l_object_id > last_id) {
555                         CERROR("Setting EA for object > than last id on "
556                                "ost idx %d "LPD64" > "LPD64" \n",
557                                lmm_objects[i].l_ost_idx,
558                                lmm_objects[i].l_object_id, last_id);
559                         RETURN(-EINVAL);
560                 }
561         }
562
563         rc = lov_setstripe(exp, lsmp, lump);
564         if (rc)
565                 RETURN(rc);
566
567         for (i = 0; i < lump->lmm_stripe_count; i++) {
568                 (*lsmp)->lsm_oinfo[i]->loi_ost_idx =
569                         lmm_objects[i].l_ost_idx;
570                 (*lsmp)->lsm_oinfo[i]->loi_id = lmm_objects[i].l_object_id;
571                 (*lsmp)->lsm_oinfo[i]->loi_gr = lmm_objects[i].l_object_gr;
572         }
573         RETURN(0);
574 }
575
576
577 /* Retrieve object striping information.
578  *
579  * @lump is a pointer to an in-core struct with lmm_ost_count indicating
580  * the maximum number of OST indices which will fit in the user buffer.
581  * lmm_magic must be LOV_USER_MAGIC.
582  */
583 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
584                   struct lov_user_md *lump)
585 {
586         /*
587          * XXX huge struct allocated on stack.
588          */
589         /* we use lov_user_md_v3 because it is larger than lov_user_md_v1 */
590         struct lov_user_md_v3 lum;
591         struct lov_mds_md *lmmk = NULL;
592         int rc, lmm_size;
593         int lum_size;
594         mm_segment_t seg;
595         ENTRY;
596
597         if (!lsm)
598                 RETURN(-ENODATA);
599
600         /*
601          * "Switch to kernel segment" to allow copying from kernel space by
602          * copy_{to,from}_user().
603          */
604         seg = get_fs();
605         set_fs(KERNEL_DS);
606
607         /* we only need the header part from user space to get lmm_magic and
608          * lmm_stripe_count, (the header part is common to v1 and v3) */
609         lum_size = sizeof(struct lov_user_md_v1);
610         rc = copy_from_user(&lum, lump, lum_size);
611
612         if (rc)
613                 rc = -EFAULT;
614         else if ((lum.lmm_magic != LOV_USER_MAGIC) &&
615                  (lum.lmm_magic != LOV_USER_MAGIC_V3))
616                 rc = -EINVAL;
617         else {
618                 /* if v3 we just have to update the lum_size */
619                 if (lum.lmm_magic == LOV_USER_MAGIC_V3)
620                         lum_size = sizeof(struct lov_user_md_v3);
621
622                 rc = lov_packmd(exp, &lmmk, lsm);
623                 if (rc < 0)
624                         RETURN(rc);
625                 lmm_size = rc;
626                 rc = 0;
627
628                 /* FIXME: Bug 1185 - copy fields properly when structs change */
629                 /* struct lov_user_md_v3 and struct lov_mds_md_v3 must be the same */
630                 CLASSERT(sizeof(lum) == sizeof(struct lov_mds_md_v3));
631                 CLASSERT(sizeof lum.lmm_objects[0] ==
632                          sizeof lmmk->lmm_objects[0]);
633
634                 /* User wasn't expecting this many OST entries */
635                 if (lum.lmm_stripe_count == 0) {
636                         if (copy_to_user(lump, lmmk, lum_size))
637                                 rc = -EFAULT;
638                 } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
639                         rc = -EOVERFLOW;
640                 } else if (copy_to_user(lump, lmmk, lmm_size))
641                         rc = -EFAULT;
642
643                 obd_free_diskmd(exp, &lmmk);
644         }
645         set_fs(seg);
646         RETURN(rc);
647 }