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