1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lov/lov_pack.c
38 * (Un)packing of OST/MDS requests
40 * Author: Andreas Dilger <adilger@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_LOV
45 #include <liblustre.h>
48 #include <lustre_net.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre/lustre_user.h>
55 #include "lov_internal.h"
57 void lov_dump_lmm_v1(int level, struct lov_mds_md_v1 *lmm)
59 struct lov_ost_data_v1 *lod;
62 CDEBUG(level, "objid "LPX64", magic 0x%08x, pattern %#x\n",
63 le64_to_cpu(lmm->lmm_object_id), le32_to_cpu(lmm->lmm_magic),
64 le32_to_cpu(lmm->lmm_pattern));
65 CDEBUG(level,"stripe_size %u, stripe_count %u\n",
66 le32_to_cpu(lmm->lmm_stripe_size),
67 le32_to_cpu(lmm->lmm_stripe_count));
69 if (le32_to_cpu(lmm->lmm_stripe_count) <= LOV_V1_INSANE_STRIPE_COUNT) {
70 for (i = 0, lod = lmm->lmm_objects;
71 i < (int)le32_to_cpu(lmm->lmm_stripe_count); i++, lod++)
73 "stripe %u idx %u subobj "LPX64"/"LPX64"\n",
74 i, le32_to_cpu(lod->l_ost_idx),
75 le64_to_cpu(lod->l_object_gr),
76 le64_to_cpu(lod->l_object_id));
78 CDEBUG(level, "bad stripe_count %u > max_stripe_count %u\n",
79 le32_to_cpu(lmm->lmm_stripe_count),
80 LOV_V1_INSANE_STRIPE_COUNT);
84 void lov_dump_lmm_join(int level, struct lov_mds_md_join *lmmj)
87 CDEBUG(level, "objid "LPX64", magic 0x%08X, pattern %#X\n",
88 le64_to_cpu(lmmj->lmmj_md.lmm_object_id),
89 le32_to_cpu(lmmj->lmmj_md.lmm_magic),
90 le32_to_cpu(lmmj->lmmj_md.lmm_pattern));
91 CDEBUG(level,"stripe_size %u, stripe_count %u extent_count %u \n",
92 le32_to_cpu(lmmj->lmmj_md.lmm_stripe_size),
93 le32_to_cpu(lmmj->lmmj_md.lmm_stripe_count),
94 le32_to_cpu(lmmj->lmmj_extent_count));
97 #define LMM_ASSERT(test) \
99 if (!(test)) lov_dump_lmm(D_ERROR, lmm); \
100 LASSERT(test); /* so we know what assertion failed */ \
103 /* Pack LOV object metadata for disk storage. It is packed in LE byte
104 * order and is opaque to the networking layer.
106 * XXX In the future, this will be enhanced to get the EA size from the
107 * underlying OSC device(s) to get their EA sizes so we can stack
108 * LOVs properly. For now lov_mds_md_size() just assumes one obd_id
111 int lov_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
112 struct lov_stripe_md *lsm)
114 struct obd_device *obd = class_exp2obd(exp);
115 struct lov_obd *lov = &obd->u.lov;
116 struct lov_mds_md *lmm;
117 int stripe_count = lov->desc.ld_tgt_count;
123 if (lsm->lsm_magic != LOV_MAGIC) {
124 CERROR("bad mem LOV MAGIC: 0x%08X != 0x%08X\n",
125 lsm->lsm_magic, LOV_MAGIC);
128 /* If we are just sizing the EA, limit the stripe count
129 * to the actual number of OSTs in this filesystem. */
131 stripe_count = lov_get_stripecnt(lov, lsm->lsm_stripe_count);
132 lsm->lsm_stripe_count = stripe_count;
134 stripe_count = lsm->lsm_stripe_count;
138 /* XXX LOV STACKING call into osc for sizes */
139 lmm_size = lov_mds_md_size(stripe_count);
145 stripe_count = le32_to_cpu((*lmmp)->lmm_stripe_count);
146 OBD_FREE(*lmmp, lov_mds_md_size(stripe_count));
152 OBD_ALLOC(*lmmp, lmm_size);
158 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC); /* only write new format */
163 lmm->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
164 lmm->lmm_object_gr = cpu_to_le64(lsm->lsm_object_gr);
165 lmm->lmm_stripe_size = cpu_to_le32(lsm->lsm_stripe_size);
166 lmm->lmm_stripe_count = cpu_to_le32(stripe_count);
167 lmm->lmm_pattern = cpu_to_le32(lsm->lsm_pattern);
169 for (i = 0; i < stripe_count; i++) {
170 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
172 /* XXX LOV STACKING call down to osc_packmd() to do packing */
173 LASSERTF(loi->loi_id, "lmm_oid "LPU64" stripe %u/%u idx %u\n",
174 lmm->lmm_object_id, i, stripe_count, loi->loi_ost_idx);
175 lmm->lmm_objects[i].l_object_id = cpu_to_le64(loi->loi_id);
176 lmm->lmm_objects[i].l_object_gr = cpu_to_le64(loi->loi_gr);
177 lmm->lmm_objects[i].l_ost_gen = cpu_to_le32(loi->loi_ost_gen);
178 lmm->lmm_objects[i].l_ost_idx = cpu_to_le32(loi->loi_ost_idx);
184 /* Find the max stripecount we should use */
185 int lov_get_stripecnt(struct lov_obd *lov, __u32 stripe_count)
188 stripe_count = lov->desc.ld_default_stripe_count;
189 if (stripe_count > lov->desc.ld_active_tgt_count)
190 stripe_count = lov->desc.ld_active_tgt_count;
193 /* for now, we limit the stripe count directly, when bug 4424 is
194 * fixed this needs to be somewhat dynamic based on whether ext3
195 * can handle larger EA sizes. */
196 if (stripe_count > LOV_MAX_STRIPE_COUNT)
197 stripe_count = LOV_MAX_STRIPE_COUNT;
203 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
207 if (lsm_op_find(le32_to_cpu(*(__u32 *)lmm)) == NULL) {
208 CERROR("bad disk LOV MAGIC: 0x%08X; dumping V1 LMM:\n",
209 le32_to_cpu(*(__u32 *)lmm));
210 lov_dump_lmm_v1(D_WARNING, lmm);
213 rc = lsm_op_find(le32_to_cpu(*(__u32 *)lmm))->lsm_lmm_verify(lmm,
214 lmm_bytes, stripe_count);
218 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count,
219 int pattern, int magic)
224 CDEBUG(D_INFO, "alloc lsm, stripe_count %d\n", stripe_count);
226 *lsmp = lsm_alloc_plain(stripe_count, &lsm_size);
228 CERROR("can't allocate lsmp stripe_count %d\n", stripe_count);
232 spin_lock_init(&(*lsmp)->lsm_lock);
233 (*lsmp)->lsm_magic = magic;
234 (*lsmp)->lsm_stripe_count = stripe_count;
235 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES * stripe_count;
236 (*lsmp)->lsm_pattern = pattern;
237 (*lsmp)->lsm_oinfo[0]->loi_ost_idx = ~0;
239 for (i = 0; i < stripe_count; i++)
240 loi_init((*lsmp)->lsm_oinfo[i]);
245 void lov_free_memmd(struct lov_stripe_md **lsmp)
247 struct lov_stripe_md *lsm = *lsmp;
249 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
250 lsm_op_find(lsm->lsm_magic)->lsm_free(lsm);
256 /* Unpack LOV object metadata from disk storage. It is packed in LE byte
257 * order and is opaque to the networking layer.
259 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
260 struct lov_mds_md *lmm, int lmm_bytes)
262 struct obd_device *obd = class_exp2obd(exp);
263 struct lov_obd *lov = &obd->u.lov;
264 int rc = 0, stripe_count, lsm_size;
268 /* If passed an MDS struct use values from there, otherwise defaults */
270 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
273 magic = le32_to_cpu(lmm->lmm_magic);
275 stripe_count = lov_get_stripecnt(lov, 0);
279 /* If we aren't passed an lsmp struct, we just want the size */
281 /* XXX LOV STACKING call into osc for sizes */
283 RETURN(lov_stripe_md_size(stripe_count));
285 /* If we are passed an allocated struct but nothing to unpack, free */
287 lov_free_memmd(lsmp);
291 lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0,
296 /* If we are passed a pointer but nothing to unpack, we only alloc */
300 LASSERT(lsm_op_find(magic) != NULL);
301 rc = lsm_op_find(magic)->lsm_unpackmd(lov, *lsmp, lmm);
303 lov_free_memmd(lsmp);
310 static int __lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
311 struct lov_user_md *lump)
313 struct obd_device *obd = class_exp2obd(exp);
314 struct lov_obd *lov = &obd->u.lov;
315 struct lov_user_md lum;
320 rc = copy_from_user(&lum, lump, sizeof(lum));
324 if (lum.lmm_magic != LOV_USER_MAGIC) {
325 if (lum.lmm_magic == __swab32(LOV_USER_MAGIC)) {
326 lustre_swab_lov_user_md(&lum);
328 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
330 lum.lmm_magic, LOV_USER_MAGIC);
335 if (lum.lmm_pattern == 0) {
336 lum.lmm_pattern = lov->desc.ld_pattern ?
337 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
340 if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
341 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
346 /* 64kB is the largest common page size we see (ia64), and matches the
348 if (lum.lmm_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
349 CDEBUG(D_IOCTL, "stripe size %u not multiple of %u, fixing\n",
350 lum.lmm_stripe_size, LOV_MIN_STRIPE_SIZE);
351 lum.lmm_stripe_size = LOV_MIN_STRIPE_SIZE;
354 if ((lum.lmm_stripe_offset >= lov->desc.ld_tgt_count) &&
355 (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
356 CDEBUG(D_IOCTL, "stripe offset %u > number of OSTs %u\n",
357 lum.lmm_stripe_offset, lov->desc.ld_tgt_count);
360 stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
362 if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
363 CDEBUG(D_IOCTL, "stripe width %ux%i exeeds %lu bytes\n",
364 lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
368 rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern, LOV_MAGIC);
371 (*lsmp)->lsm_oinfo[0]->loi_ost_idx = lum.lmm_stripe_offset;
372 (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
379 /* Configure object striping information on a new file.
381 * @lmmu is a pointer to a user struct with one or more of the fields set to
382 * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
383 * lmm_stripe_offset, and lmm_stripe_pattern. lmm_magic must be LOV_MAGIC.
384 * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
386 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
387 struct lov_user_md *lump)
395 rc = __lov_setstripe(exp, lsmp, lump);
400 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
401 struct lov_user_md *lump)
405 struct obd_export *oexp;
406 struct lov_obd *lov = &exp->exp_obd->u.lov;
410 for (i = 0; i < lump->lmm_stripe_count; i++) {
411 __u32 len = sizeof(last_id);
412 oexp = lov->lov_tgts[lump->lmm_objects[i].l_ost_idx]->ltd_exp;
413 rc = obd_get_info(oexp, sizeof(KEY_LAST_ID), KEY_LAST_ID,
414 &len, &last_id, NULL);
417 if (lump->lmm_objects[i].l_object_id > last_id) {
418 CERROR("Setting EA for object > than last id on "
419 "ost idx %d "LPD64" > "LPD64" \n",
420 lump->lmm_objects[i].l_ost_idx,
421 lump->lmm_objects[i].l_object_id, last_id);
426 rc = lov_setstripe(exp, lsmp, lump);
430 for (i = 0; i < lump->lmm_stripe_count; i++) {
431 (*lsmp)->lsm_oinfo[i]->loi_ost_idx =
432 lump->lmm_objects[i].l_ost_idx;
433 (*lsmp)->lsm_oinfo[i]->loi_id = lump->lmm_objects[i].l_object_id;
434 (*lsmp)->lsm_oinfo[i]->loi_gr = lump->lmm_objects[i].l_object_gr;
440 /* Retrieve object striping information.
442 * @lump is a pointer to an in-core struct with lmm_ost_count indicating
443 * the maximum number of OST indices which will fit in the user buffer.
444 * lmm_magic must be LOV_USER_MAGIC.
446 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
447 struct lov_user_md *lump)
450 * XXX huge struct allocated on stack.
452 struct lov_user_md lum;
453 struct lov_mds_md *lmmk = NULL;
462 * "Switch to kernel segment" to allow copying from kernel space by
463 * copy_{to,from}_user().
467 rc = copy_from_user(&lum, lump, sizeof(lum));
470 else if (lum.lmm_magic != LOV_USER_MAGIC)
473 rc = lov_packmd(exp, &lmmk, lsm);
479 /* FIXME: Bug 1185 - copy fields properly when structs change */
480 CLASSERT(sizeof lum == sizeof *lmmk);
481 CLASSERT(sizeof lum.lmm_objects[0] ==
482 sizeof lmmk->lmm_objects[0]);
484 /* User wasn't expecting this many OST entries */
485 if (lum.lmm_stripe_count == 0) {
486 if (copy_to_user(lump, lmmk, sizeof lum))
488 } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
490 } else if (copy_to_user(lump, lmmk, sizeof lum))
493 obd_free_diskmd(exp, &lmmk);