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 LASSERT(loi->loi_id);
128 lmm->lmm_objects[i].l_object_id = cpu_to_le64(loi->loi_id);
129 lmm->lmm_objects[i].l_object_gr = cpu_to_le64(loi->loi_gr);
130 lmm->lmm_objects[i].l_ost_gen = cpu_to_le32(loi->loi_ost_gen);
131 lmm->lmm_objects[i].l_ost_idx = cpu_to_le32(loi->loi_ost_idx);
137 int lov_get_stripecnt(struct lov_obd *lov, int stripe_count)
140 stripe_count = lov->desc.ld_default_stripe_count;
141 if (!stripe_count || stripe_count > lov->desc.ld_active_tgt_count)
142 stripe_count = lov->desc.ld_active_tgt_count;
147 static int lov_verify_lmm_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
150 if (lmm_bytes < sizeof(*lmm)) {
151 CERROR("lov_mds_md too small: %d, need at least %d\n",
152 lmm_bytes, (int)sizeof(*lmm));
156 if (lmm->lmm_magic != le32_to_cpu(LOV_MAGIC_V1)) {
157 CERROR("bad disk LOV MAGIC: 0x%08X\n",
158 le32_to_cpu(*(__u32 *)lmm));
162 *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
164 if (*stripe_count == 0) {
165 CERROR("bad stripe count %d\n", *stripe_count);
166 lov_dump_lmm_v1(D_WARNING, lmm);
170 if (lmm_bytes < lov_mds_md_v1_size(*stripe_count)) {
171 CERROR("LOV EA too small: %d, need %d\n",
172 lmm_bytes, lov_mds_md_v1_size(*stripe_count));
173 lov_dump_lmm_v1(D_WARNING, lmm);
177 if (lmm->lmm_object_id == 0) {
178 CERROR("zero object id\n");
179 lov_dump_lmm_v1(D_WARNING, lmm);
183 if (lmm->lmm_pattern != cpu_to_le32(LOV_PATTERN_RAID0)) {
184 CERROR("bad striping pattern\n");
185 lov_dump_lmm_v1(D_WARNING, lmm);
189 if (lmm->lmm_stripe_size == 0 ||
190 (__u64)le32_to_cpu(lmm->lmm_stripe_size) * *stripe_count > ~0UL) {
191 CERROR("bad stripe size %u\n",
192 le32_to_cpu(lmm->lmm_stripe_size));
193 lov_dump_lmm_v1(D_WARNING, lmm);
200 static int lov_verify_lmm(void *lmm, int lmm_bytes, int *stripe_count)
202 switch (le32_to_cpu(*(__u32 *)lmm)) {
204 return lov_verify_lmm_v1(lmm, lmm_bytes, stripe_count);
206 CERROR("bad disk LOV MAGIC: 0x%08X\n",
207 le32_to_cpu(*(__u32 *)lmm));
212 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count, int pattern)
214 int lsm_size = lov_stripe_md_size(stripe_count);
215 struct lov_oinfo *loi;
218 OBD_ALLOC(*lsmp, lsm_size);
222 (*lsmp)->lsm_magic = LOV_MAGIC;
223 (*lsmp)->lsm_stripe_count = stripe_count;
224 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES * stripe_count;
225 (*lsmp)->lsm_xfersize = PTLRPC_MAX_BRW_SIZE * stripe_count;
226 (*lsmp)->lsm_pattern = pattern;
227 (*lsmp)->lsm_oinfo[0].loi_ost_idx = ~0;
229 for (i = 0, loi = (*lsmp)->lsm_oinfo; i < stripe_count; i++, loi++)
235 void lov_free_memmd(struct lov_stripe_md **lsmp)
237 OBD_FREE(*lsmp, lov_stripe_md_size((*lsmp)->lsm_stripe_count));
241 int lov_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
242 struct lov_mds_md_v1 *lmm)
244 struct lov_oinfo *loi;
247 lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
248 lsm->lsm_object_gr = le64_to_cpu(lmm->lmm_object_gr);
249 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
250 lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
251 lsm->lsm_xfersize = lsm->lsm_stripe_size * lsm->lsm_stripe_count;
253 for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++) {
254 /* XXX LOV STACKING call down to osc_unpackmd() */
255 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
256 loi->loi_gr = le64_to_cpu(lmm->lmm_objects[i].l_object_gr);
257 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
258 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
259 if (loi->loi_ost_idx > lov->desc.ld_tgt_count) {
260 CERROR("OST index %d more than OST count %d\n",
261 loi->loi_ost_idx, lov->desc.ld_tgt_count);
262 lov_dump_lmm_v1(D_WARNING, lmm);
271 /* Unpack LOV object metadata from disk storage. It is packed in LE byte
272 * order and is opaque to the networking layer.
274 int lov_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
275 struct lov_mds_md *lmm, int lmm_bytes)
277 struct obd_device *obd = class_exp2obd(exp);
278 struct lov_obd *lov = &obd->u.lov;
279 int rc = 0, stripe_count, lsm_size;
282 /* If passed an MDS struct use values from there, otherwise defaults */
284 rc = lov_verify_lmm(lmm, lmm_bytes, &stripe_count);
288 stripe_count = lov_get_stripecnt(lov, 0);
291 /* If we aren't passed an lsmp struct, we just want the size */
293 /* XXX LOV STACKING call into osc for sizes */
294 RETURN(lov_stripe_md_size(stripe_count));
296 /* If we are passed an allocated struct but nothing to unpack, free */
298 lov_free_memmd(lsmp);
302 lsm_size = lov_alloc_memmd(lsmp, stripe_count, LOV_PATTERN_RAID0);
306 /* If we are passed a pointer but nothing to unpack, we only alloc */
310 switch (le32_to_cpu(lmm->lmm_magic)) {
312 rc = lov_unpackmd_v1(lov, *lsmp, lmm);
317 lov_free_memmd(lsmp);
324 /* Configure object striping information on a new file.
326 * @lmmu is a pointer to a user struct with one or more of the fields set to
327 * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
328 * lmm_stripe_offset, and lmm_stripe_pattern. lmm_magic must be LOV_MAGIC.
329 * @lsmp is a pointer to an in-core stripe MD that needs to be filled in.
331 int lov_setstripe(struct obd_export *exp, struct lov_stripe_md **lsmp,
332 struct lov_user_md *lump)
334 struct obd_device *obd = class_exp2obd(exp);
335 struct lov_obd *lov = &obd->u.lov;
336 struct lov_user_md lum;
341 rc = copy_from_user(&lum, lump, sizeof(lum));
345 if (lum.lmm_magic != LOV_USER_MAGIC) {
346 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#08x != %#08x\n",
347 lum.lmm_magic, LOV_USER_MAGIC);
351 if (lum.lmm_pattern == 0) {
352 lum.lmm_pattern = lov->desc.ld_pattern ?
353 lov->desc.ld_pattern : LOV_PATTERN_RAID0;
356 if (lum.lmm_pattern != LOV_PATTERN_RAID0) {
357 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
362 if (lum.lmm_stripe_size & (PAGE_SIZE - 1)) {
363 CDEBUG(D_IOCTL, "stripe size %u not multiple of %lu\n",
364 lum.lmm_stripe_size, PAGE_SIZE);
368 if ((lum.lmm_stripe_offset >= lov->desc.ld_active_tgt_count) &&
369 (lum.lmm_stripe_offset != (typeof(lum.lmm_stripe_offset))(-1))) {
370 CDEBUG(D_IOCTL, "stripe offset %u > number of active OSTs %u\n",
371 lum.lmm_stripe_offset, lov->desc.ld_active_tgt_count);
374 stripe_count = lov_get_stripecnt(lov, lum.lmm_stripe_count);
376 if ((__u64)lum.lmm_stripe_size * stripe_count > ~0UL) {
377 CDEBUG(D_IOCTL, "stripe width %ux%u > %lu on 32-bit system\n",
378 lum.lmm_stripe_size, (int)lum.lmm_stripe_count, ~0UL);
382 rc = lov_alloc_memmd(lsmp, stripe_count, lum.lmm_pattern);
387 (*lsmp)->lsm_oinfo[0].loi_ost_idx = lum.lmm_stripe_offset;
388 (*lsmp)->lsm_stripe_size = lum.lmm_stripe_size;
389 (*lsmp)->lsm_xfersize = lum.lmm_stripe_size * stripe_count;
394 int lov_setea(struct obd_export *exp, struct lov_stripe_md **lsmp,
395 struct lov_user_md *lump)
399 struct obd_export *oexp;
400 struct lov_obd *lov = &exp->exp_obd->u.lov;
403 for (i = 0; i < lump->lmm_stripe_count; i++) {
404 __u32 len = sizeof(last_id);
405 oexp = lov->tgts[lump->lmm_objects[i].l_ost_idx].ltd_exp;
406 rc = obd_get_info(oexp, strlen("last_id"), "last_id",
410 if (lump->lmm_objects[i].l_object_id > last_id) {
411 CERROR("Setting EA for object > than last id on "
412 "ost idx %d "LPD64" > "LPD64" \n",
413 lump->lmm_objects[i].l_ost_idx,
414 lump->lmm_objects[i].l_object_id, last_id);
419 rc = lov_setstripe(exp, lsmp, lump);
423 for (i = 0; i < lump->lmm_stripe_count; i++) {
424 (*lsmp)->lsm_oinfo[i].loi_ost_idx =
425 lump->lmm_objects[i].l_ost_idx;
426 (*lsmp)->lsm_oinfo[i].loi_id = lump->lmm_objects[i].l_object_id;
427 (*lsmp)->lsm_oinfo[i].loi_gr = lump->lmm_objects[i].l_object_gr;
433 /* Retrieve object striping information.
435 * @lump is a pointer to an in-core struct with lmm_ost_count indicating
436 * the maximum number of OST indices which will fit in the user buffer.
437 * lmm_magic must be LOV_USER_MAGIC.
439 int lov_getstripe(struct obd_export *exp, struct lov_stripe_md *lsm,
440 struct lov_user_md *lump)
442 struct lov_user_md lum;
443 struct lov_mds_md *lmmk = NULL;
450 rc = copy_from_user(&lum, lump, sizeof(lum));
454 if (lum.lmm_magic != LOV_USER_MAGIC)
457 rc = lov_packmd(exp, &lmmk, lsm);
463 /* FIXME: Bug 1185 - copy fields properly when structs change */
464 LASSERT(sizeof(lum) == sizeof(*lmmk));
465 LASSERT(sizeof(lum.lmm_objects[0]) == 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, lmm_size)) {
477 obd_free_diskmd(exp, &lmmk);