aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/lustre/lustre/lov/lov_ea.c
blob: 2bcfaeaff6fa586efe924ed1edf182d0c980177b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * GPL HEADER START
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License version 2 for more details (a copy is included
 * in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; If not, see
 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 * GPL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright (c) 2011, 2012, Intel Corporation.
 */
/*
 * This file is part of Lustre, http://www.lustre.org/
 * Lustre is a trademark of Sun Microsystems, Inc.
 *
 * lustre/lov/lov_ea.c
 *
 * Author: Wang Di <wangdi@clusterfs.com>
 */

#define DEBUG_SUBSYSTEM S_LOV

#include <asm/div64.h>
#include "../../include/linux/libcfs/libcfs.h"

#include "../include/obd_class.h"
#include "../include/lustre/lustre_idl.h"

#include "lov_internal.h"

struct lovea_unpack_args {
	struct lov_stripe_md *lsm;
	int		   cursor;
};

static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
				 __u16 stripe_count)
{
	if (stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
		CERROR("bad stripe count %d\n", stripe_count);
		lov_dump_lmm_common(D_WARNING, lmm);
		return -EINVAL;
	}

	if (lmm_oi_id(&lmm->lmm_oi) == 0) {
		CERROR("zero object id\n");
		lov_dump_lmm_common(D_WARNING, lmm);
		return -EINVAL;
	}

	if (lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_RAID0) {
		CERROR("bad striping pattern\n");
		lov_dump_lmm_common(D_WARNING, lmm);
		return -EINVAL;
	}

	if (lmm->lmm_stripe_size == 0 ||
	    (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
		CERROR("bad stripe size %u\n",
		       le32_to_cpu(lmm->lmm_stripe_size));
		lov_dump_lmm_common(D_WARNING, lmm);
		return -EINVAL;
	}
	return 0;
}

struct lov_stripe_md *lsm_alloc_plain(__u16 stripe_count, int *size)
{
	struct lov_stripe_md *lsm;
	struct lov_oinfo     *loi;
	int		   i, oinfo_ptrs_size;

	LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);

	oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
	*size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;

	OBD_ALLOC_LARGE(lsm, *size);
	if (!lsm)
		return NULL;

	for (i = 0; i < stripe_count; i++) {
		OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
		if (loi == NULL)
			goto err;
		lsm->lsm_oinfo[i] = loi;
	}
	lsm->lsm_stripe_count = stripe_count;
	return lsm;

err:
	while (--i >= 0)
		OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
	OBD_FREE_LARGE(lsm, *size);
	return NULL;
}

void lsm_free_plain(struct lov_stripe_md *lsm)
{
	__u16 stripe_count = lsm->lsm_stripe_count;
	int i;

	for (i = 0; i < stripe_count; i++)
		OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
			      sizeof(struct lov_oinfo));
	OBD_FREE_LARGE(lsm, sizeof(struct lov_stripe_md) +
		       stripe_count * sizeof(struct lov_oinfo *));
}

static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
				struct lov_mds_md *lmm)
{
	/*
	 * This supposes lov_mds_md_v1/v3 first fields are
	 * are the same
	 */
	lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
	lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
	lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
	lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
	lsm->lsm_pool_name[0] = '\0';
}

static void
lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
			   u64 *lov_off, u64 *swidth)
{
	if (swidth)
		*swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
}

static void
lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
			   u64 *lov_off, u64 *swidth)
{
	if (swidth)
		*swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
}

static int lsm_destroy_plain(struct lov_stripe_md *lsm, struct obdo *oa,
			     struct obd_export *md_exp)
{
	return 0;
}

/* Find minimum stripe maxbytes value.  For inactive or
 * reconnecting targets use LUSTRE_STRIPE_MAXBYTES. */
static void lov_tgt_maxbytes(struct lov_tgt_desc *tgt, __u64 *stripe_maxbytes)
{
	struct obd_import *imp = tgt->ltd_obd->u.cli.cl_import;

	if (imp == NULL || !tgt->ltd_active) {
		*stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
		return;
	}

	spin_lock(&imp->imp_lock);
	if (imp->imp_state == LUSTRE_IMP_FULL &&
	    (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
	    imp->imp_connect_data.ocd_maxbytes > 0) {
		if (*stripe_maxbytes > imp->imp_connect_data.ocd_maxbytes)
			*stripe_maxbytes = imp->imp_connect_data.ocd_maxbytes;
	} else {
		*stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
	}
	spin_unlock(&imp->imp_lock);
}

static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
			     __u16 *stripe_count)
{
	if (lmm_bytes < sizeof(*lmm)) {
		CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
		       lmm_bytes, (int)sizeof(*lmm));
		return -EINVAL;
	}

	*stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
	if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
		*stripe_count = 0;

	if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
		CERROR("LOV EA V1 too small: %d, need %d\n",
		       lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
		lov_dump_lmm_common(D_WARNING, lmm);
		return -EINVAL;
	}

	return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
}

static int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
			   struct lov_mds_md_v1 *lmm)
{
	struct lov_oinfo *loi;
	int i;
	int stripe_count;
	__u64 stripe_maxbytes = OBD_OBJECT_EOF;

	lsm_unpackmd_common(lsm, lmm);

	stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;

	for (i = 0; i < stripe_count; i++) {
		/* XXX LOV STACKING call down to osc_unpackmd() */
		loi = lsm->lsm_oinfo[i];
		ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
		loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
		loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
		if (lov_oinfo_is_dummy(loi))
			continue;

		if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
			CERROR("OST index %d more than OST count %d\n",
			       loi->loi_ost_idx, lov->desc.ld_tgt_count);
			lov_dump_lmm_v1(D_WARNING, lmm);
			return -EINVAL;
		}
		if (!lov->lov_tgts[loi->loi_ost_idx]) {
			CERROR("OST index %d missing\n", loi->loi_ost_idx);
			lov_dump_lmm_v1(D_WARNING, lmm);
			return -EINVAL;
		}
		/* calculate the minimum stripe max bytes */
		lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
				 &stripe_maxbytes);
	}

	lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
	if (lsm->lsm_stripe_count == 0)
		lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;

	return 0;
}

const struct lsm_operations lsm_v1_ops = {
	.lsm_free	    = lsm_free_plain,
	.lsm_destroy	 = lsm_destroy_plain,
	.lsm_stripe_by_index    = lsm_stripe_by_index_plain,
	.lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
	.lsm_lmm_verify	 = lsm_lmm_verify_v1,
	.lsm_unpackmd	   = lsm_unpackmd_v1,
};

static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
			     __u16 *stripe_count)
{
	struct lov_mds_md_v3 *lmm;

	lmm = (struct lov_mds_md_v3 *)lmmv1;

	if (lmm_bytes < sizeof(*lmm)) {
		CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
		       lmm_bytes, (int)sizeof(*lmm));
		return -EINVAL;
	}

	*stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
	if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
		*stripe_count = 0;

	if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
		CERROR("LOV EA V3 too small: %d, need %d\n",
		       lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
		lov_dump_lmm_common(D_WARNING, lmm);
		return -EINVAL;
	}

	return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
				     *stripe_count);
}

static int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
			   struct lov_mds_md *lmmv1)
{
	struct lov_mds_md_v3 *lmm;
	struct lov_oinfo *loi;
	int i;
	int stripe_count;
	__u64 stripe_maxbytes = OBD_OBJECT_EOF;
	int cplen = 0;

	lmm = (struct lov_mds_md_v3 *)lmmv1;

	lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);

	stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;

	cplen = strlcpy(lsm->lsm_pool_name, lmm->lmm_pool_name,
			sizeof(lsm->lsm_pool_name));
	if (cplen >= sizeof(lsm->lsm_pool_name))
		return -E2BIG;

	for (i = 0; i < stripe_count; i++) {
		/* XXX LOV STACKING call down to osc_unpackmd() */
		loi = lsm->lsm_oinfo[i];
		ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
		loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
		loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
		if (lov_oinfo_is_dummy(loi))
			continue;

		if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
			CERROR("OST index %d more than OST count %d\n",
			       loi->loi_ost_idx, lov->desc.ld_tgt_count);
			lov_dump_lmm_v3(D_WARNING, lmm);
			return -EINVAL;
		}
		if (!lov->lov_tgts[loi->loi_ost_idx]) {
			CERROR("OST index %d missing\n", loi->loi_ost_idx);
			lov_dump_lmm_v3(D_WARNING, lmm);
			return -EINVAL;
		}
		/* calculate the minimum stripe max bytes */
		lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
				 &stripe_maxbytes);
	}

	lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
	if (lsm->lsm_stripe_count == 0)
		lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;

	return 0;
}

const struct lsm_operations lsm_v3_ops = {
	.lsm_free	    = lsm_free_plain,
	.lsm_destroy	 = lsm_destroy_plain,
	.lsm_stripe_by_index    = lsm_stripe_by_index_plain,
	.lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
	.lsm_lmm_verify	 = lsm_lmm_verify_v3,
	.lsm_unpackmd	   = lsm_unpackmd_v3,
};

void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
{
	CDEBUG(level, "lsm %p, objid " DOSTID ", maxbytes %#llx, magic 0x%08X, stripe_size %u, stripe_count %u, refc: %d, layout_gen %u, pool [" LOV_POOLNAMEF "]\n",
	       lsm,
	       POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
	       lsm->lsm_stripe_size, lsm->lsm_stripe_count,
	       atomic_read(&lsm->lsm_refc), lsm->lsm_layout_gen,
	       lsm->lsm_pool_name);
}