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 DEBUG_SUBSYSTEM S_LOV
28 #include <liblustre.h>
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>
38 #include "lov_internal.h"
40 void lov_dump_lmm_v1(int level, struct lov_mds_md_v1 *lmm)
42 struct lov_ost_data_v1 *lod;
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));
59 #define LMM_ASSERT(test) \
61 if (!(test)) lov_dump_lmm(D_ERROR, lmm); \
62 LASSERT(test); /* so we know what assertion failed */ \
65 /* Pack LOV object metadata for disk storage. It is packed in LE byte
66 * order and is opaque to the networking layer.
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
73 int lov_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
74 struct lov_stripe_md *lsm)
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;
86 if (lsm->lsm_magic != LOV_MAGIC) {
87 CERROR("bad mem LOV MAGIC: 0x%08X != 0x%08X\n",
88 lsm->lsm_magic, LOV_MAGIC);
91 stripe_count = lsm->lsm_stripe_count;
94 /* XXX LOV STACKING call into osc for sizes */
95 lmm_size = lov_mds_md_size(stripe_count);
101 stripe_count = le32_to_cpu((*lmmp)->lmm_stripe_count);
102 OBD_FREE(*lmmp, lov_mds_md_size(stripe_count));
108 OBD_ALLOC(*lmmp, lmm_size);
114 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC); /* only write new format */
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);
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);
138 int lov_get_stripecnt(struct lov_obd *lov, int stripe_count)
141 stripe_count = lov->desc.ld_default_stripe_count;
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;
155 static int lov_verify_lmm_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
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));
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));
170 *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
172 if (*stripe_count == 0) {
173 CERROR("bad stripe count %d\n", *stripe_count);
174 lov_dump_lmm_v1(D_WARNING, lmm);
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);
185 if (lmm->lmm_object_id == 0) {
186 CERROR("zero object id\n");
187 lov_dump_lmm_v1(D_WARNING, lmm);
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);
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);
208 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
210 switch (le32_to_cpu(*(__u32 *)lmm)) {
212 return lov_verify_lmm_v1(lmm, lmm_bytes, stripe_count);
214 CERROR("bad disk LOV MAGIC: 0x%08X\n",
215 le32_to_cpu(*(__u32 *)lmm));
220 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count, int pattern)
222 int lsm_size = lov_stripe_md_size(stripe_count);
223 struct lov_oinfo *loi;
226 OBD_ALLOC(*lsmp, lsm_size);
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;
237 for (i = 0, loi = (*lsmp)->lsm_oinfo; i < stripe_count; i++, loi++)
243 void lov_free_memmd(struct lov_stripe_md **lsmp)
245 OBD_FREE(*lsmp, lov_stripe_md_size((*lsmp)->lsm_stripe_count));
249 int lov_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
250 struct lov_mds_md_v1 *lmm)
252 struct lov_oinfo *loi;
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;
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);
279 /* Unpack LOV object metadata from disk storage. It is packed in LE byte
280 * order and is opaque to the networking layer.
282 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
283 struct lov_mds_md *lmm, int lmm_bytes)
285 struct obd_device *obd = class_exp2obd(exp);
286 struct lov_obd *lov = &obd->u.lov;
287 int rc = 0, stripe_count, lsm_size;
290 /* If passed an MDS struct use values from there, otherwise defaults */
292 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
296 stripe_count = lov_get_stripecnt(lov, 0);
299 /* If we aren't passed an lsmp struct, we just want the size */
301 /* XXX LOV STACKING call into osc for sizes */
302 RETURN(lov_stripe_md_size(stripe_count));
304 /* If we are passed an allocated struct but nothing to unpack, free */
306 lov_free_memmd(lsmp);
310 lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0);
314 /* If we are passed a pointer but nothing to unpack, we only alloc */
318 switch (le32_to_cpu(lmm->lmm_magic)) {
320 rc = lov_unpackmd_v1(lov, *lsmp, lmm);
325 lov_free_memmd(lsmp);
332 /* Configure object striping information on a new file.
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.
339 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
340 struct lov_user_md *lump)
342 struct obd_device *obd = class_exp2obd(exp);
343 struct lov_obd *lov = &obd->u.lov;
344 struct lov_user_md lum;
349 rc = copy_from_user(&lum, lump, sizeof(lum));
353 if (lum.lmm_magic != LOV_USER_MAGIC) {
354 if (lum.lmm_magic == __swab32(LOV_USER_MAGIC)) {
355 lustre_swab_lov_user_md(&lum);
357 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
359 lum.lmm_magic, LOV_USER_MAGIC);
364 if (lum.lmm_pattern == 0) {
365 lum.lmm_pattern = lov->desc.ld_pattern ?
366 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
369 if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
370 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
375 /* 64kB is the largest common page size we see (ia64), and matches the
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;
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);
389 stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
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);
397 rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern);
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;
409 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
410 struct lov_user_md *lump)
414 struct obd_export *oexp;
415 struct lov_obd *lov = &exp->exp_obd->u.lov;
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",
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);
434 rc = lov_setstripe(exp, lsmp, lump);
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;
448 /* Retrieve object striping information.
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.
454 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
455 struct lov_user_md *lump)
457 struct lov_user_md lum;
458 struct lov_mds_md *lmmk = NULL;
465 rc = copy_from_user(&lum, lump, sizeof(lum));
469 if (lum.lmm_magic != LOV_USER_MAGIC)
472 rc = lov_packmd(exp, &lmmk, lsm);
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]));
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)))
486 } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
488 } else if (copy_to_user(lump, lmmk, lmm_size)) {
492 obd_free_diskmd(exp, &lmmk);