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