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 <lustre_net.h>
34 #include <obd_class.h>
35 #include <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));
52 if (le32_to_cpu(lmm->lmm_stripe_count) <= LOV_V1_INSANE_STRIPE_COUNT) {
53 for (i = 0, lod = lmm->lmm_objects;
54 i < (int)le32_to_cpu(lmm->lmm_stripe_count); i++, lod++)
56 "stripe %u idx %u subobj "LPX64"/"LPX64"\n",
57 i, le32_to_cpu(lod->l_ost_idx),
58 le64_to_cpu(lod->l_object_gr),
59 le64_to_cpu(lod->l_object_id));
61 CDEBUG(level, "bad stripe_count %u > max_stripe_count %u\n",
62 le32_to_cpu(lmm->lmm_stripe_count),
63 LOV_V1_INSANE_STRIPE_COUNT);
67 void lov_dump_lmm_join(int level, struct lov_mds_md_join *lmmj)
70 CDEBUG(level, "objid "LPX64", magic 0x%08X, pattern %#X\n",
71 le64_to_cpu(lmmj->lmmj_md.lmm_object_id),
72 le32_to_cpu(lmmj->lmmj_md.lmm_magic),
73 le32_to_cpu(lmmj->lmmj_md.lmm_pattern));
74 CDEBUG(level,"stripe_size %u, stripe_count %u extent_count %u \n",
75 le32_to_cpu(lmmj->lmmj_md.lmm_stripe_size),
76 le32_to_cpu(lmmj->lmmj_md.lmm_stripe_count),
77 le32_to_cpu(lmmj->lmmj_extent_count));
80 #define LMM_ASSERT(test) \
82 if (!(test)) lov_dump_lmm(D_ERROR, lmm); \
83 LASSERT(test); /* so we know what assertion failed */ \
86 /* Pack LOV object metadata for disk storage. It is packed in LE byte
87 * order and is opaque to the networking layer.
89 * XXX In the future, this will be enhanced to get the EA size from the
90 * underlying OSC device(s) to get their EA sizes so we can stack
91 * LOVs properly. For now lov_mds_md_size() just assumes one obd_id
94 int lov_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
95 struct lov_stripe_md *lsm)
97 struct obd_device *obd = class_exp2obd(exp);
98 struct lov_obd *lov = &obd->u.lov;
99 struct lov_mds_md *lmm;
100 int stripe_count = lov->desc.ld_tgt_count;
106 if (lsm->lsm_magic != LOV_MAGIC) {
107 CERROR("bad mem LOV MAGIC: 0x%08X != 0x%08X\n",
108 lsm->lsm_magic, LOV_MAGIC);
111 /* If we are just sizing the EA, limit the stripe count
112 * to the actual number of OSTs in this filesystem. */
114 stripe_count = lov_get_stripecnt(lov, lsm->lsm_stripe_count);
115 lsm->lsm_stripe_count = stripe_count;
117 stripe_count = lsm->lsm_stripe_count;
121 /* XXX LOV STACKING call into osc for sizes */
122 lmm_size = lov_mds_md_size(stripe_count);
128 stripe_count = le32_to_cpu((*lmmp)->lmm_stripe_count);
129 OBD_FREE(*lmmp, lov_mds_md_size(stripe_count));
135 OBD_ALLOC(*lmmp, lmm_size);
141 lmm->lmm_magic = cpu_to_le32(LOV_MAGIC); /* only write new format */
146 lmm->lmm_object_id = cpu_to_le64(lsm->lsm_object_id);
147 lmm->lmm_object_gr = cpu_to_le64(lsm->lsm_object_gr);
148 lmm->lmm_stripe_size = cpu_to_le32(lsm->lsm_stripe_size);
149 lmm->lmm_stripe_count = cpu_to_le32(stripe_count);
150 lmm->lmm_pattern = cpu_to_le32(lsm->lsm_pattern);
152 for (i = 0; i < stripe_count; i++) {
153 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
155 /* XXX LOV STACKING call down to osc_packmd() to do packing */
156 LASSERTF(loi->loi_id, "lmm_oid "LPU64" stripe %u/%u idx %u\n",
157 lmm->lmm_object_id, i, stripe_count, loi->loi_ost_idx);
158 lmm->lmm_objects[i].l_object_id = cpu_to_le64(loi->loi_id);
159 lmm->lmm_objects[i].l_object_gr = cpu_to_le64(loi->loi_gr);
160 lmm->lmm_objects[i].l_ost_gen = cpu_to_le32(loi->loi_ost_gen);
161 lmm->lmm_objects[i].l_ost_idx = cpu_to_le32(loi->loi_ost_idx);
167 /* Find the max stripecount we should use */
168 int lov_get_stripecnt(struct lov_obd *lov, __u32 stripe_count)
171 stripe_count = lov->desc.ld_default_stripe_count;
172 if (stripe_count > lov->desc.ld_active_tgt_count)
173 stripe_count = lov->desc.ld_active_tgt_count;
176 /* for now, we limit the stripe count directly, when bug 4424 is
177 * fixed this needs to be somewhat dynamic based on whether ext3
178 * can handle larger EA sizes. */
179 if (stripe_count > LOV_MAX_STRIPE_COUNT)
180 stripe_count = LOV_MAX_STRIPE_COUNT;
186 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
190 if (lsm_op_find(le32_to_cpu(*(__u32 *)lmm)) == NULL) {
191 CERROR("bad disk LOV MAGIC: 0x%08X; dumping V1 LMM:\n",
192 le32_to_cpu(*(__u32 *)lmm));
193 lov_dump_lmm_v1(D_WARNING, lmm);
196 rc = lsm_op_find(le32_to_cpu(*(__u32 *)lmm))->lsm_lmm_verify(lmm,
197 lmm_bytes, stripe_count);
201 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count,
202 int pattern, int magic)
207 CDEBUG(D_INFO, "alloc lsm, stripe_count %d\n", stripe_count);
209 *lsmp = lsm_alloc_plain(stripe_count, &lsm_size);
211 CERROR("can't allocate lsmp stripe_count %d\n", stripe_count);
215 spin_lock_init(&(*lsmp)->lsm_lock);
216 (*lsmp)->lsm_magic = magic;
217 (*lsmp)->lsm_stripe_count = stripe_count;
218 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES * stripe_count;
219 (*lsmp)->lsm_pattern = pattern;
220 (*lsmp)->lsm_oinfo[0]->loi_ost_idx = ~0;
222 for (i = 0; i < stripe_count; i++)
223 loi_init((*lsmp)->lsm_oinfo[i]);
228 void lov_free_memmd(struct lov_stripe_md **lsmp)
230 struct lov_stripe_md *lsm = *lsmp;
232 LASSERT(lsm_op_find(lsm->lsm_magic) != NULL);
233 lsm_op_find(lsm->lsm_magic)->lsm_free(lsm);
239 /* Unpack LOV object metadata from disk storage. It is packed in LE byte
240 * order and is opaque to the networking layer.
242 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
243 struct lov_mds_md *lmm, int lmm_bytes)
245 struct obd_device *obd = class_exp2obd(exp);
246 struct lov_obd *lov = &obd->u.lov;
247 int rc = 0, stripe_count, lsm_size;
251 /* If passed an MDS struct use values from there, otherwise defaults */
253 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
256 magic = le32_to_cpu(lmm->lmm_magic);
258 stripe_count = lov_get_stripecnt(lov, 0);
262 /* If we aren't passed an lsmp struct, we just want the size */
264 /* XXX LOV STACKING call into osc for sizes */
266 RETURN(lov_stripe_md_size(stripe_count));
268 /* If we are passed an allocated struct but nothing to unpack, free */
270 lov_free_memmd(lsmp);
274 lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0,
279 /* If we are passed a pointer but nothing to unpack, we only alloc */
283 LASSERT(lsm_op_find(magic) != NULL);
284 rc = lsm_op_find(magic)->lsm_unpackmd(lov, *lsmp, lmm);
286 lov_free_memmd(lsmp);
293 static int __lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
294 struct lov_user_md *lump)
296 struct obd_device *obd = class_exp2obd(exp);
297 struct lov_obd *lov = &obd->u.lov;
298 struct lov_user_md lum;
303 rc = copy_from_user(&lum, lump, sizeof(lum));
307 if (lum.lmm_magic != LOV_USER_MAGIC) {
308 if (lum.lmm_magic == __swab32(LOV_USER_MAGIC)) {
309 lustre_swab_lov_user_md(&lum);
311 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
313 lum.lmm_magic, LOV_USER_MAGIC);
318 if (lum.lmm_pattern == 0) {
319 lum.lmm_pattern = lov->desc.ld_pattern ?
320 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
323 if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
324 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
329 /* 64kB is the largest common page size we see (ia64), and matches the
331 if (lum.lmm_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
332 CDEBUG(D_IOCTL, "stripe size %u not multiple of %u, fixing\n",
333 lum.lmm_stripe_size, LOV_MIN_STRIPE_SIZE);
334 lum.lmm_stripe_size = LOV_MIN_STRIPE_SIZE;
337 if ((lum.lmm_stripe_offset >= lov->desc.ld_tgt_count) &&
338 (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
339 CDEBUG(D_IOCTL, "stripe offset %u > number of OSTs %u\n",
340 lum.lmm_stripe_offset, lov->desc.ld_tgt_count);
343 stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
345 if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
346 CDEBUG(D_IOCTL, "stripe width %ux%i exeeds %lu bytes\n",
347 lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
351 rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern, LOV_MAGIC);
354 (*lsmp)->lsm_oinfo[0]->loi_ost_idx = lum.lmm_stripe_offset;
355 (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
362 /* Configure object striping information on a new file.
364 * @lmmu is a pointer to a user struct with one or more of the fields set to
365 * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
366 * lmm_stripe_offset, and lmm_stripe_pattern. lmm_magic must be LOV_MAGIC.
367 * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
369 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
370 struct lov_user_md *lump)
378 rc = __lov_setstripe(exp, lsmp, lump);
383 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
384 struct lov_user_md *lump)
388 struct obd_export *oexp;
389 struct lov_obd *lov = &exp->exp_obd->u.lov;
393 for (i = 0; i < lump->lmm_stripe_count; i++) {
394 __u32 len = sizeof(last_id);
395 oexp = lov->lov_tgts[lump->lmm_objects[i].l_ost_idx]->ltd_exp;
396 rc = obd_get_info(oexp, sizeof(KEY_LAST_ID), KEY_LAST_ID,
400 if (lump->lmm_objects[i].l_object_id > last_id) {
401 CERROR("Setting EA for object > than last id on "
402 "ost idx %d "LPD64" > "LPD64" \n",
403 lump->lmm_objects[i].l_ost_idx,
404 lump->lmm_objects[i].l_object_id, last_id);
409 rc = lov_setstripe(exp, lsmp, lump);
413 for (i = 0; i < lump->lmm_stripe_count; i++) {
414 (*lsmp)->lsm_oinfo[i]->loi_ost_idx =
415 lump->lmm_objects[i].l_ost_idx;
416 (*lsmp)->lsm_oinfo[i]->loi_id = lump->lmm_objects[i].l_object_id;
417 (*lsmp)->lsm_oinfo[i]->loi_gr = lump->lmm_objects[i].l_object_gr;
423 /* Retrieve object striping information.
425 * @lump is a pointer to an in-core struct with lmm_ost_count indicating
426 * the maximum number of OST indices which will fit in the user buffer.
427 * lmm_magic must be LOV_USER_MAGIC.
429 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
430 struct lov_user_md *lump)
433 * XXX huge struct allocated on stack.
435 struct lov_user_md lum;
436 struct lov_mds_md *lmmk = NULL;
445 * "Switch to kernel segment" to allow copying from kernel space by
446 * copy_{to,from}_user().
450 rc = copy_from_user(&lum, lump, sizeof(lum));
453 else if (lum.lmm_magic != LOV_USER_MAGIC)
456 rc = lov_packmd(exp, &lmmk, lsm);
462 /* FIXME: Bug 1185 - copy fields properly when structs change */
463 CLASSERT(sizeof lum == sizeof *lmmk);
464 CLASSERT(sizeof lum.lmm_objects[0] ==
465 sizeof lmmk->lmm_objects[0]);
467 /* User wasn't expecting this many OST entries */
468 if (lum.lmm_stripe_count == 0) {
469 if (copy_to_user(lump, lmmk, sizeof lum))
471 } else if (lum.lmm_stripe_count < lmmk->lmm_stripe_count) {
473 } else if (copy_to_user(lump, lmmk, sizeof lum))
476 obd_free_diskmd(exp, &lmmk);