1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
22 * (Un)packing of OST/MDS requests
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_LOV
30 #include <liblustre.h>
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>
40 #include "lov_internal.h"
42 void lov_dump_lmm_v0(int level, struct lov_mds_md_v0 *lmm)
44 int i, num_ost, stripe, idx;
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),
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++) {
56 if (lmm->lmm_objects[idx].l_object_id == 0)
58 CDEBUG(level, "stripe %u idx %u subobj "LPX64"\n", stripe, idx,
59 le64_to_cpu(lmm->lmm_objects[idx].l_object_id));
64 void lov_dump_lmm_v1(int level, struct lov_mds_md_v1 *lmm)
66 struct lov_ost_data_v1 *lod;
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));
83 #define LMM_ASSERT(test) \
85 if (!(test)) lov_dump_lmm(D_ERROR, lmm); \
86 LASSERT(test); /* so we know what assertion failed */ \
89 /* Pack LOV object metadata for disk storage. It is packed in LE byte
90 * order and is opaque to the networking layer.
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
97 int lov_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
98 struct lov_stripe_md *lsm)
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;
110 if (lsm->lsm_magic != LOV_MAGIC) {
111 CWARN("bad LOV MAGIC: 0x%08X != 0x%08X\n",
112 lsm->lsm_magic, LOV_MAGIC);
115 stripe_count = lsm->lsm_stripe_count;
118 /* XXX LOV STACKING call into osc for sizes */
119 lmm_size = lov_mds_md_size(stripe_count);
125 stripe_count = le32_to_cpu((*lmmp)->lmm_stripe_count);
126 OBD_FREE(*lmmp, lov_mds_md_size(stripe_count));
132 OBD_ALLOC(*lmmp, lmm_size);
138 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC); /* only write new format */
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);
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);
161 int lov_get_stripecnt(struct lov_obd *lov, int 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;
171 static int lov_verify_lmm_v0(struct lov_mds_md_v0 *lmm, int lmm_bytes,
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));
180 *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
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);
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);
196 if (lmm->lmm_object_id == 0) {
197 CERROR("zero object id\n");
198 lov_dump_lmm_v0(D_WARNING, lmm);
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);
211 if (lmm->lmm_stripe_size == 0) {
212 CERROR("zero stripe size\n");
213 lov_dump_lmm_v0(D_WARNING, lmm);
220 static int lov_verify_lmm_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
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));
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));
235 *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
237 if (*stripe_count == 0) {
238 CERROR("bad stripe count %d\n", *stripe_count);
239 lov_dump_lmm_v1(D_WARNING, lmm);
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);
250 if (lmm->lmm_object_id == 0) {
251 CERROR("zero object id\n");
252 lov_dump_lmm_v1(D_WARNING, lmm);
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);
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);
273 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
275 switch (le32_to_cpu(*(__u32 *)lmm)) {
277 return lov_verify_lmm_v1(lmm, lmm_bytes, stripe_count);
279 return lov_verify_lmm_v0(lmm, lmm_bytes, stripe_count);
281 CERROR("bad disk LOV MAGIC: 0x%08X\n",
282 le32_to_cpu(*(__u32 *)lmm));
287 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count, int pattern)
289 int lsm_size = lov_stripe_md_size(stripe_count);
290 struct lov_oinfo *loi;
293 OBD_ALLOC(*lsmp, lsm_size);
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 (*lsmp)->lsm_oinfo[0].loi_ost_gen = ~0;
305 for (i = 0, loi = (*lsmp)->lsm_oinfo; i < stripe_count; i++, loi++)
310 EXPORT_SYMBOL(lov_alloc_memmd);
312 void lov_free_memmd(struct lov_stripe_md **lsmp)
314 OBD_FREE(*lsmp, lov_stripe_md_size((*lsmp)->lsm_stripe_count));
317 EXPORT_SYMBOL(lov_free_memmd);
319 int lov_unpackmd_v0(struct lov_obd *lov, struct lov_stripe_md *lsm,
320 struct lov_mds_md_v0 *lmm)
322 struct lov_oinfo *loi;
323 int i, ost_offset, ost_count;
325 lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
326 /* lsm->lsm_object_gr = 0; implicit */
327 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
328 lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
329 lsm->lsm_pattern = LOV_PATTERN_RAID0;
330 ost_offset = le32_to_cpu(lmm->lmm_stripe_offset);
331 ost_count = le16_to_cpu(lmm->lmm_ost_count);
333 for (i = 0, loi = lsm->lsm_oinfo; i < ost_count; i++, ost_offset++) {
334 ost_offset %= ost_count;
336 if (!lmm->lmm_objects[ost_offset].l_object_id)
340 le64_to_cpu(lmm->lmm_objects[ost_offset].l_object_id);
341 /* loi->loi_gr = 0; implicit */
342 loi->loi_ost_idx = ost_offset;
343 loi->loi_ost_gen = 1;
347 if (loi - lsm->lsm_oinfo != lsm->lsm_stripe_count) {
348 CERROR("missing objects in lmm struct\n");
349 lov_dump_lmm_v0(D_WARNING, lmm);
356 int lov_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
357 struct lov_mds_md_v1 *lmm)
359 struct lov_oinfo *loi;
362 lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
363 lsm->lsm_object_gr = le64_to_cpu(lmm->lmm_object_gr);
364 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
365 lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
366 lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
368 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++) {
369 /* XXX LOV STACKING call down to osc_unpackmd() */
370 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
371 loi->loi_gr = le64_to_cpu(lmm->lmm_objects[i].l_object_gr);
372 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
373 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
374 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
375 CERROR("OST index %d more than OST count %d. "
376 "MDS and client use different OST sets?\n",
377 loi->loi_ost_idx, lov->desc.ld_tgt_count);
378 lov_dump_lmm_v1(D_WARNING, lmm);
387 /* Unpack LOV object metadata from disk storage. It is packed in LE byte
388 * order and is opaque to the networking layer.
390 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
391 struct lov_mds_md *lmm, int lmm_bytes)
393 struct obd_device *obd = class_exp2obd(exp);
394 struct lov_obd *lov = &obd->u.lov;
395 int rc = 0, stripe_count, lsm_size;
398 /* If passed an MDS struct use values from there, otherwise defaults */
400 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
404 stripe_count = lov_get_stripecnt(lov, 0);
407 /* If we aren't passed an lsmp struct, we just want the size */
409 /* XXX LOV STACKING call into osc for sizes */
410 RETURN(lov_stripe_md_size(stripe_count));
412 /* If we are passed an allocated struct but nothing to unpack, free */
414 lov_free_memmd(lsmp);
418 lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0);
422 /* If we are passed a pointer but nothing to unpack, we only alloc */
426 switch (le32_to_cpu(lmm->lmm_magic)) {
428 rc = lov_unpackmd_v1(lov, *lsmp, lmm);
431 rc = lov_unpackmd_v0(lov, *lsmp, (void *)lmm);
436 lov_free_memmd(lsmp);
443 /* Configure object striping information on a new file.
445 * @lmmu is a pointer to a user struct with one or more of the fields set to
446 * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
447 * lmm_stripe_offset, and lmm_stripe_pattern. lmm_magic must be LOV_MAGIC.
448 * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
450 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
451 struct lov_user_md *lump)
453 struct obd_device *obd = class_exp2obd(exp);
454 struct lov_obd *lov = &obd->u.lov;
455 struct lov_user_md lum;
460 rc = copy_from_user(&lum, lump, sizeof(lum));
464 if (lum.lmm_magic != LOV_USER_MAGIC) {
465 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x\n",
466 lum.lmm_magic, LOV_USER_MAGIC);
470 if (lum.lmm_pattern == 0) {
471 lum.lmm_pattern = lov->desc.ld_pattern ?
472 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
475 if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
476 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
481 if (lum.lmm_stripe_size & (PAGE_SIZE - 1)) {
482 CDEBUG(D_IOCTL, "stripe size %u not multiple of %lu\n",
483 lum.lmm_stripe_size, PAGE_SIZE);
487 if ((lum.lmm_stripe_offset >= lov->desc.ld_active_tgt_count) &&
488 (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
489 CDEBUG(D_IOCTL, "stripe offset %u > number of active OSTs %u\n",
490 lum.lmm_stripe_offset, lov->desc.ld_active_tgt_count);
493 stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
495 if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
496 CDEBUG(D_IOCTL, "stripe width %ux%u > %lu on 32-bit system\n",
497 lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
501 rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern);
506 (*lsmp)->lsm_oinfo[0].loi_ost_idx = lum.lmm_stripe_offset;
507 /* XXX - what about loi_ost_gen ? */
508 (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
509 (*lsmp)->lsm_xfersize = lum.lmm_stripe_size * stripe_count;
514 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
515 struct lov_user_md *lump)
519 struct obd_export *oexp;
520 struct lov_obd *lov = &exp->exp_obd->u.lov;
523 for (i = 0; i < lump->lmm_stripe_count; i++) {
524 struct lov_tgt_desc *tgt;
525 __u32 len = sizeof(last_id);
527 tgt = lov->tgts + lump->lmm_objects[i].l_ost_idx;
528 if (!lov_tgt_active(lov, tgt, lump->lmm_objects[i].l_ost_gen)) {
529 CERROR("Object on ost idx %d: osc inactive.\n",
530 lump->lmm_objects[i].l_ost_idx);
535 rc = obd_get_info(oexp, strlen("last_id"), "last_id",
537 lov_tgt_decref(lov, tgt);
540 if (lump->lmm_objects[i].l_object_id > last_id) {
541 CERROR("Setting EA for object > than last id on "
542 "ost idx %d "LPD64" > "LPD64" \n",
543 lump->lmm_objects[i].l_ost_idx,
544 lump->lmm_objects[i].l_object_id, last_id);
549 rc = lov_setstripe(exp, lsmp, lump);
553 for (i = 0; i < lump->lmm_stripe_count; i++) {
554 (*lsmp)->lsm_oinfo[i].loi_ost_idx =
555 lump->lmm_objects[i].l_ost_idx;
556 (*lsmp)->lsm_oinfo[i].loi_ost_gen =
557 lump->lmm_objects[i].l_ost_gen;
558 (*lsmp)->lsm_oinfo[i].loi_id = lump->lmm_objects[i].l_object_id;
559 (*lsmp)->lsm_oinfo[i].loi_gr = lump->lmm_objects[i].l_object_gr;
565 /* Retrieve object striping information.
567 * @lump is a pointer to an in-core struct with lmm_ost_count indicating
568 * the maximum number of OST indices which will fit in the user buffer.
569 * lmm_magic must be LOV_USER_MAGIC.
571 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
572 struct lov_user_md *lump)
574 struct lov_user_md lum;
575 struct lov_mds_md *lmmk = NULL;
582 rc = copy_from_user(&lum, lump, sizeof(lum));
586 if (lum.lmm_magic != LOV_USER_MAGIC)
589 rc = lov_packmd(exp, &lmmk, lsm);
595 /* FIXME: Bug 1185 - copy fields properly when structs change */
596 LASSERT(sizeof(lum) == sizeof(*lmmk));
597 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lmmk->lmm_objects[0]));
599 /* User wasn't expecting this many OST entries */
600 if (lum.lmm_stripe_count == 0) {
601 rc = copy_to_user(lump, lmmk, sizeof(lum));
604 } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
606 } else if (copy_to_user(lump, lmmk, lmm_size)) {
610 obd_free_diskmd(exp, &lmmk);