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