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 CERROR("bad mem 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;
304 for (i = 0, loi = (*lsmp)->lsm_oinfo; i < stripe_count; i++, loi++)
309 EXPORT_SYMBOL(lov_alloc_memmd);
310 void lov_free_memmd(struct lov_stripe_md **lsmp)
312 OBD_FREE(*lsmp, lov_stripe_md_size((*lsmp)->lsm_stripe_count));
315 EXPORT_SYMBOL(lov_free_memmd);
316 int lov_unpackmd_v0(struct lov_obd *lov, struct lov_stripe_md *lsm,
317 struct lov_mds_md_v0 *lmm)
319 struct lov_oinfo *loi;
320 int i, ost_offset, ost_count;
322 lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
323 /* lsm->lsm_object_gr = 0; implicit */
324 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
325 lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
326 lsm->lsm_pattern = LOV_PATTERN_RAID0;
327 ost_offset = le32_to_cpu(lmm->lmm_stripe_offset);
328 ost_count = le16_to_cpu(lmm->lmm_ost_count);
330 for (i = 0, loi = lsm->lsm_oinfo; i < ost_count; i++, ost_offset++) {
331 ost_offset %= ost_count;
333 if (!lmm->lmm_objects[ost_offset].l_object_id)
337 le64_to_cpu(lmm->lmm_objects[ost_offset].l_object_id);
338 /* loi->loi_gr = 0; implicit */
339 loi->loi_ost_idx = ost_offset;
340 /* loi->loi_ost_gen = 0; implicit */
344 if (loi - lsm->lsm_oinfo != lsm->lsm_stripe_count) {
345 CERROR("missing objects in lmm struct\n");
346 lov_dump_lmm_v0(D_WARNING, lmm);
353 int lov_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
354 struct lov_mds_md_v1 *lmm)
356 struct lov_oinfo *loi;
359 lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
360 lsm->lsm_object_gr = le64_to_cpu(lmm->lmm_object_gr);
361 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
362 lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
363 lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
365 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++) {
366 /* XXX LOV STACKING call down to osc_unpackmd() */
367 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
368 loi->loi_gr = le64_to_cpu(lmm->lmm_objects[i].l_object_gr);
369 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
370 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
371 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
372 CERROR("OST index %d more than OST count %d\n",
373 loi->loi_ost_idx, lov->desc.ld_tgt_count);
374 lov_dump_lmm_v1(D_WARNING, lmm);
383 /* Unpack LOV object metadata from disk storage. It is packed in LE byte
384 * order and is opaque to the networking layer.
386 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
387 struct lov_mds_md *lmm, int lmm_bytes)
389 struct obd_device *obd = class_exp2obd(exp);
390 struct lov_obd *lov = &obd->u.lov;
391 int rc = 0, stripe_count, lsm_size;
394 /* If passed an MDS struct use values from there, otherwise defaults */
396 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
400 stripe_count = lov_get_stripecnt(lov, 0);
403 /* If we aren't passed an lsmp struct, we just want the size */
405 /* XXX LOV STACKING call into osc for sizes */
406 RETURN(lov_stripe_md_size(stripe_count));
408 /* If we are passed an allocated struct but nothing to unpack, free */
410 lov_free_memmd(lsmp);
414 lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0);
418 /* If we are passed a pointer but nothing to unpack, we only alloc */
422 switch (le32_to_cpu(lmm->lmm_magic)) {
424 rc = lov_unpackmd_v1(lov, *lsmp, lmm);
427 rc = lov_unpackmd_v0(lov, *lsmp, (void *)lmm);
432 lov_free_memmd(lsmp);
439 /* Configure object striping information on a new file.
441 * @lmmu is a pointer to a user struct with one or more of the fields set to
442 * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
443 * lmm_stripe_offset, and lmm_stripe_pattern. lmm_magic must be LOV_MAGIC.
444 * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
446 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
447 struct lov_user_md *lump)
449 struct obd_device *obd = class_exp2obd(exp);
450 struct lov_obd *lov = &obd->u.lov;
451 struct lov_user_md lum;
456 rc = copy_from_user(&lum, lump, sizeof(lum));
460 if (lum.lmm_magic != LOV_USER_MAGIC) {
461 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x\n",
462 lum.lmm_magic, LOV_USER_MAGIC);
466 if (lum.lmm_pattern == 0) {
467 lum.lmm_pattern = lov->desc.ld_pattern ?
468 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
471 if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
472 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
477 if (lum.lmm_stripe_size & (PAGE_SIZE - 1)) {
478 CDEBUG(D_IOCTL, "stripe size %u not multiple of %lu\n",
479 lum.lmm_stripe_size, PAGE_SIZE);
483 if ((lum.lmm_stripe_offset >= lov->desc.ld_active_tgt_count) &&
484 (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
485 CDEBUG(D_IOCTL, "stripe offset %u > number of active OSTs %u\n",
486 lum.lmm_stripe_offset, lov->desc.ld_active_tgt_count);
489 stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
491 if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
492 CDEBUG(D_IOCTL, "stripe width %ux%u > %lu on 32-bit system\n",
493 lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
497 rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern);
502 (*lsmp)->lsm_oinfo[0].loi_ost_idx = lum.lmm_stripe_offset;
503 (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
504 (*lsmp)->lsm_xfersize = lum.lmm_stripe_size * stripe_count;
509 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
510 struct lov_user_md *lump)
514 struct obd_export *oexp;
515 struct lov_obd *lov = &exp->exp_obd->u.lov;
518 for (i = 0; i < lump->lmm_stripe_count; i++) {
519 __u32 len = sizeof(last_id);
520 oexp = lov->tgts[lump->lmm_objects[i].l_ost_idx].ltd_exp;
521 rc = obd_get_info(oexp, strlen("last_id"), "last_id",
525 if (lump->lmm_objects[i].l_object_id > last_id) {
526 CERROR("Setting EA for object > than last id on "
527 "ost idx %d "LPD64" > "LPD64" \n",
528 lump->lmm_objects[i].l_ost_idx,
529 lump->lmm_objects[i].l_object_id, last_id);
534 rc = lov_setstripe(exp, lsmp, lump);
538 for (i = 0; i < lump->lmm_stripe_count; i++) {
539 (*lsmp)->lsm_oinfo[i].loi_ost_idx =
540 lump->lmm_objects[i].l_ost_idx;
541 (*lsmp)->lsm_oinfo[i].loi_id = lump->lmm_objects[i].l_object_id;
542 (*lsmp)->lsm_oinfo[i].loi_gr = lump->lmm_objects[i].l_object_gr;
548 /* Retrieve object striping information.
550 * @lump is a pointer to an in-core struct with lmm_ost_count indicating
551 * the maximum number of OST indices which will fit in the user buffer.
552 * lmm_magic must be LOV_USER_MAGIC.
554 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
555 struct lov_user_md *lump)
557 struct lov_user_md lum;
558 struct lov_mds_md *lmmk = NULL;
565 rc = copy_from_user(&lum, lump, sizeof(lum));
569 if (lum.lmm_magic != LOV_USER_MAGIC)
572 rc = lov_packmd(exp, &lmmk, lsm);
578 /* FIXME: Bug 1185 - copy fields properly when structs change */
579 LASSERT(sizeof(lum) == sizeof(*lmmk));
580 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lmmk->lmm_objects[0]));
582 /* User wasn't expecting this many OST entries */
583 if (lum.lmm_stripe_count == 0) {
584 rc = copy_to_user(lump, lmmk, sizeof(lum));
587 } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
589 } else if (copy_to_user(lump, lmmk, lmm_size)) {
593 obd_free_diskmd(exp, &lmmk);