4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2013, Intel Corporation.
29 * lustre/lod/lod_lov.c
31 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
34 #define DEBUG_SUBSYSTEM S_MDS
36 #include <obd_class.h>
39 #include "lod_internal.h"
42 * Keep a refcount of lod->ltd_tgts usage to prevent racing with
43 * addition/deletion. Any function that expects lov_tgts to remain stationary
46 * \param lod - is the lod device from which we want to grab a reference
48 void lod_getref(struct lod_tgt_descs *ltd)
50 down_read(<d->ltd_rw_sem);
51 mutex_lock(<d->ltd_mutex);
53 mutex_unlock(<d->ltd_mutex);
57 * Companion of lod_getref() to release a reference on the lod table.
58 * If this is the last reference and the ost entry was scheduled for deletion,
59 * the descriptor is removed from the array.
61 * \param lod - is the lod device from which we release a reference
63 void lod_putref(struct lod_device *lod, struct lod_tgt_descs *ltd)
65 mutex_lock(<d->ltd_mutex);
67 if (ltd->ltd_refcount == 0 && ltd->ltd_death_row) {
68 struct lod_tgt_desc *tgt_desc, *tmp;
72 CDEBUG(D_CONFIG, "destroying %d ltd desc\n",
75 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx) {
76 tgt_desc = LTD_TGT(ltd, idx);
79 if (!tgt_desc->ltd_reap)
82 cfs_list_add(&tgt_desc->ltd_kill, &kill);
83 LTD_TGT(ltd, idx) = NULL;
84 /*FIXME: only support ost pool for now */
85 if (ltd == &lod->lod_ost_descs) {
86 lod_ost_pool_remove(&lod->lod_pool_info, idx);
87 if (tgt_desc->ltd_active)
88 lod->lod_desc.ld_active_tgt_count--;
91 cfs_bitmap_clear(ltd->ltd_tgt_bitmap, idx);
94 mutex_unlock(<d->ltd_mutex);
95 up_read(<d->ltd_rw_sem);
97 cfs_list_for_each_entry_safe(tgt_desc, tmp, &kill, ltd_kill) {
99 cfs_list_del(&tgt_desc->ltd_kill);
100 if (ltd == &lod->lod_ost_descs) {
101 /* remove from QoS structures */
102 rc = qos_del_tgt(lod, tgt_desc);
104 CERROR("%s: qos_del_tgt(%s) failed:"
106 lod2obd(lod)->obd_name,
107 obd_uuid2str(&tgt_desc->ltd_uuid),
110 rc = obd_disconnect(tgt_desc->ltd_exp);
112 CERROR("%s: failed to disconnect %s: rc = %d\n",
113 lod2obd(lod)->obd_name,
114 obd_uuid2str(&tgt_desc->ltd_uuid), rc);
115 OBD_FREE_PTR(tgt_desc);
118 mutex_unlock(<d->ltd_mutex);
119 up_read(<d->ltd_rw_sem);
123 static int ltd_bitmap_resize(struct lod_tgt_descs *ltd, __u32 newsize)
125 cfs_bitmap_t *new_bitmap, *old_bitmap = NULL;
129 /* grab write reference on the lod. Relocating the array requires
130 * exclusive access */
132 down_write(<d->ltd_rw_sem);
133 if (newsize <= ltd->ltd_tgts_size)
134 /* someone else has already resize the array */
137 /* allocate new bitmap */
138 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
140 GOTO(out, rc = -ENOMEM);
142 if (ltd->ltd_tgts_size > 0) {
143 /* the bitmap already exists, we need
144 * to copy data from old one */
145 cfs_bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap);
146 old_bitmap = ltd->ltd_tgt_bitmap;
149 ltd->ltd_tgts_size = newsize;
150 ltd->ltd_tgt_bitmap = new_bitmap;
153 CFS_FREE_BITMAP(old_bitmap);
155 CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
159 up_write(<d->ltd_rw_sem);
164 * Connect LOD to a new OSP and add it to the device table.
166 * \param env - is the environment passed by the caller
167 * \param lod - is the LOD device to be connected to the new OSP
168 * \param osp - is the name of OSP device name about to be added
169 * \param index - is the OSP index
170 * \param gen - is the generation number
171 * \param tgt_index - is the group of the OSP.
172 * \param type - is the type of device (mdc or osc)
174 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
175 char *osp, unsigned index, unsigned gen, int tgt_index,
176 char *type, int active)
178 struct obd_connect_data *data = NULL;
179 struct obd_export *exp = NULL;
180 struct obd_device *obd;
181 struct lu_device *ldev;
184 struct lod_tgt_desc *tgt_desc;
185 struct lod_tgt_descs *ltd;
186 struct obd_uuid obd_uuid;
189 CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, index, gen);
192 CERROR("request to add OBD %s with invalid generation: %d\n",
197 obd_str2uuid(&obd_uuid, osp);
199 obd = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
200 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
202 CERROR("can't find %s device\n", osp);
210 data->ocd_connect_flags = OBD_CONNECT_INDEX | OBD_CONNECT_VERSION;
211 data->ocd_version = LUSTRE_VERSION_CODE;
212 data->ocd_index = index;
214 if (strcmp(LUSTRE_OSC_NAME, type) == 0) {
215 data->ocd_connect_flags |= OBD_CONNECT_AT |
218 #ifdef HAVE_LRU_RESIZE_SUPPORT
219 OBD_CONNECT_LRU_RESIZE |
222 OBD_CONNECT_OSS_CAPA |
223 OBD_CONNECT_REQPORTAL |
224 OBD_CONNECT_SKIP_ORPHAN |
226 OBD_CONNECT_LVB_TYPE |
227 OBD_CONNECT_VERSION |
228 OBD_CONNECT_PINGLESS;
230 data->ocd_group = tgt_index;
231 ltd = &lod->lod_ost_descs;
233 struct obd_import *imp = obd->u.cli.cl_import;
235 data->ocd_ibits_known = MDS_INODELOCK_UPDATE;
236 data->ocd_connect_flags |= OBD_CONNECT_ACL |
237 OBD_CONNECT_MDS_CAPA |
238 OBD_CONNECT_OSS_CAPA |
240 OBD_CONNECT_MDS_MDS |
244 /* XXX set MDS-MDS flags, remove this when running this
246 data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS;
247 spin_lock(&imp->imp_lock);
248 imp->imp_server_timeout = 1;
249 spin_unlock(&imp->imp_lock);
250 imp->imp_client->cli_request_portal = OUT_PORTAL;
251 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
253 ltd = &lod->lod_mdt_descs;
256 rc = obd_connect(env, &exp, obd, &obd->obd_uuid, data, NULL);
259 CERROR("%s: cannot connect to next dev %s (%d)\n",
260 obd->obd_name, osp, rc);
264 LASSERT(obd->obd_lu_dev);
265 LASSERT(obd->obd_lu_dev->ld_site == lod->lod_dt_dev.dd_lu_dev.ld_site);
267 ldev = obd->obd_lu_dev;
270 /* Allocate ost descriptor and fill it */
271 OBD_ALLOC_PTR(tgt_desc);
273 GOTO(out_conn, rc = -ENOMEM);
275 tgt_desc->ltd_tgt = d;
276 tgt_desc->ltd_exp = exp;
277 tgt_desc->ltd_uuid = obd->u.cli.cl_target_uuid;
278 tgt_desc->ltd_gen = gen;
279 tgt_desc->ltd_index = index;
280 tgt_desc->ltd_active = active;
283 if (index >= ltd->ltd_tgts_size) {
284 /* we have to increase the size of the lod_osts array */
287 newsize = max(ltd->ltd_tgts_size, (__u32)2);
288 while (newsize < index + 1)
289 newsize = newsize << 1;
291 /* lod_bitmap_resize() needs lod_rw_sem
292 * which we hold with th reference */
293 lod_putref(lod, ltd);
295 rc = ltd_bitmap_resize(ltd, newsize);
302 mutex_lock(<d->ltd_mutex);
303 if (cfs_bitmap_check(ltd->ltd_tgt_bitmap, index)) {
304 CERROR("%s: device %d is registered already\n", obd->obd_name,
306 GOTO(out_mutex, rc = -EEXIST);
309 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
310 OBD_ALLOC_PTR(ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK]);
311 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
312 CERROR("can't allocate index to add %s\n",
314 GOTO(out_mutex, rc = -ENOMEM);
318 if (!strcmp(LUSTRE_OSC_NAME, type)) {
319 /* pool and qos are not supported for MDS stack yet */
320 rc = lod_ost_pool_add(&lod->lod_pool_info, index,
323 CERROR("%s: can't set up pool, failed with %d\n",
328 rc = qos_add_tgt(lod, tgt_desc);
330 CERROR("%s: qos_add_tgt failed with %d\n",
335 /* The new OST is now a full citizen */
336 if (index >= lod->lod_desc.ld_tgt_count)
337 lod->lod_desc.ld_tgt_count = index + 1;
339 lod->lod_desc.ld_active_tgt_count++;
342 LTD_TGT(ltd, index) = tgt_desc;
343 cfs_bitmap_set(ltd->ltd_tgt_bitmap, index);
345 mutex_unlock(<d->ltd_mutex);
346 lod_putref(lod, ltd);
347 if (lod->lod_recovery_completed)
348 ldev->ld_ops->ldo_recovery_complete(env, ldev);
353 lod_ost_pool_remove(&lod->lod_pool_info, index);
355 mutex_unlock(<d->ltd_mutex);
356 lod_putref(lod, ltd);
358 OBD_FREE_PTR(tgt_desc);
366 * helper function to schedule OST removal from the device table
368 static void __lod_del_device(struct lod_tgt_descs *ltd,
371 LASSERT(LTD_TGT(ltd, idx));
372 if (LTD_TGT(ltd, idx)->ltd_reap == 0) {
373 LTD_TGT(ltd, idx)->ltd_reap = 1;
374 ltd->ltd_death_row++;
378 int lod_fini_tgt(struct lod_device *lod, struct lod_tgt_descs *ltd)
382 if (ltd->ltd_tgts_size <= 0)
385 mutex_lock(<d->ltd_mutex);
386 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx)
387 __lod_del_device(ltd, idx);
388 mutex_unlock(<d->ltd_mutex);
389 lod_putref(lod, ltd);
390 CFS_FREE_BITMAP(ltd->ltd_tgt_bitmap);
391 for (idx = 0; idx < TGT_PTRS; idx++) {
392 if (ltd->ltd_tgt_idx[idx])
393 OBD_FREE_PTR(ltd->ltd_tgt_idx[idx]);
395 ltd->ltd_tgts_size = 0;
400 * Add support for administratively disabled OST (through the MGS).
401 * Schedule a target for deletion. Disconnection and real removal from the
402 * table takes place in lod_putref() once the last table user release its
405 * \param env - is the environment passed by the caller
406 * \param lod - is the lod device currently connected to the OSP about to be
408 * \param osp - is the name of OSP device about to be removed
409 * \param idx - is the OSP index
410 * \param gen - is the generation number, not used currently
412 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
413 struct lod_tgt_descs *ltd, char *osp, unsigned idx,
416 struct obd_device *obd;
418 struct obd_uuid uuid;
421 CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, idx, gen);
423 obd_str2uuid(&uuid, osp);
425 obd = class_find_client_obd(&uuid, LUSTRE_OSP_NAME,
426 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
428 CERROR("can't find %s device\n", osp);
433 CERROR("%s: request to remove OBD %s with invalid generation %d"
434 "\n", obd->obd_name, osp, gen);
438 obd_str2uuid(&uuid, osp);
441 mutex_lock(<d->ltd_mutex);
442 /* check that the index is allocated in the bitmap */
443 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) ||
444 !LTD_TGT(ltd, idx)) {
445 CERROR("%s: device %d is not set up\n", obd->obd_name, idx);
446 GOTO(out, rc = -EINVAL);
449 /* check that the UUID matches */
450 if (!obd_uuid_equals(&uuid, <D_TGT(ltd, idx)->ltd_uuid)) {
451 CERROR("%s: LOD target UUID %s at index %d does not match %s\n",
452 obd->obd_name, obd_uuid2str(<D_TGT(ltd,idx)->ltd_uuid),
454 GOTO(out, rc = -EINVAL);
457 __lod_del_device(ltd, idx);
460 mutex_unlock(<d->ltd_mutex);
461 lod_putref(lod, ltd);
465 int lod_ea_store_resize(struct lod_thread_info *info, int size)
467 int round = size_roundup_power2(size);
469 LASSERT(round <= lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3));
470 if (info->lti_ea_store) {
471 LASSERT(info->lti_ea_store_size);
472 LASSERT(info->lti_ea_store_size < round);
473 CDEBUG(D_INFO, "EA store size %d is not enough, need %d\n",
474 info->lti_ea_store_size, round);
475 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
476 info->lti_ea_store = NULL;
477 info->lti_ea_store_size = 0;
480 OBD_ALLOC_LARGE(info->lti_ea_store, round);
481 if (info->lti_ea_store == NULL)
483 info->lti_ea_store_size = round;
488 * generate and write LOV EA for given striped object
490 int lod_generate_and_set_lovea(const struct lu_env *env,
491 struct lod_object *lo, struct thandle *th)
493 struct lod_thread_info *info = lod_env_info(env);
494 struct dt_object *next = dt_object_child(&lo->ldo_obj);
495 const struct lu_fid *fid = lu_object_fid(&lo->ldo_obj.do_lu);
496 struct lov_mds_md_v1 *lmm;
497 struct lov_ost_data_v1 *objs;
505 magic = lo->ldo_pool != NULL ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
506 lmm_size = lov_mds_md_size(lo->ldo_stripenr, magic);
507 if (info->lti_ea_store_size < lmm_size) {
508 rc = lod_ea_store_resize(info, lmm_size);
513 if (lo->ldo_pattern == 0) /* default striping */
514 lo->ldo_pattern = LOV_PATTERN_RAID0;
516 lmm = info->lti_ea_store;
518 lmm->lmm_magic = cpu_to_le32(magic);
519 lmm->lmm_pattern = cpu_to_le32(lo->ldo_pattern);
520 fid_to_lmm_oi(fid, &lmm->lmm_oi);
521 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
522 lmm->lmm_stripe_size = cpu_to_le32(lo->ldo_stripe_size);
523 lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_stripenr);
524 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED)
525 lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_released_stripenr);
526 lmm->lmm_layout_gen = 0;
527 if (magic == LOV_MAGIC_V1) {
528 objs = &lmm->lmm_objects[0];
530 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
531 cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
532 sizeof(v3->lmm_pool_name));
533 if (cplen >= sizeof(v3->lmm_pool_name))
535 objs = &v3->lmm_objects[0];
538 for (i = 0; i < lo->ldo_stripenr; i++) {
539 const struct lu_fid *fid;
540 struct lod_device *lod;
543 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
544 LASSERT(lo->ldo_stripe[i]);
545 fid = lu_object_fid(&lo->ldo_stripe[i]->do_lu);
547 rc = fid_to_ostid(fid, &info->lti_ostid);
550 ostid_cpu_to_le(&info->lti_ostid, &objs[i].l_ost_oi);
551 objs[i].l_ost_gen = cpu_to_le32(0);
552 rc = lod_fld_lookup(env, lod, fid, &index, LU_SEQ_RANGE_OST);
554 CERROR("%s: Can not locate "DFID": rc = %d\n",
555 lod2obd(lod)->obd_name, PFID(fid), rc);
558 objs[i].l_ost_idx = cpu_to_le32(index);
561 info->lti_buf.lb_buf = lmm;
562 info->lti_buf.lb_len = lmm_size;
563 rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0,
569 int lod_get_lov_ea(const struct lu_env *env, struct lod_object *lo)
571 struct lod_thread_info *info = lod_env_info(env);
572 struct dt_object *next = dt_object_child(&lo->ldo_obj);
578 if (unlikely(info->lti_ea_store_size == 0)) {
579 /* just to enter in allocation block below */
583 info->lti_buf.lb_buf = info->lti_ea_store;
584 info->lti_buf.lb_len = info->lti_ea_store_size;
585 rc = dt_xattr_get(env, next, &info->lti_buf, XATTR_NAME_LOV,
588 /* if object is not striped or inaccessible */
593 /* EA doesn't fit, reallocate new buffer */
594 rc = dt_xattr_get(env, next, &LU_BUF_NULL, XATTR_NAME_LOV,
602 rc = lod_ea_store_resize(info, rc);
611 int lod_store_def_striping(const struct lu_env *env, struct dt_object *dt,
614 struct lod_thread_info *info = lod_env_info(env);
615 struct lod_object *lo = lod_dt_obj(dt);
616 struct dt_object *next = dt_object_child(dt);
617 struct lov_user_md_v3 *v3;
622 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
625 * store striping defaults into new directory
626 * used to implement defaults inheritance
629 /* probably nothing to inherite */
630 if (lo->ldo_striping_cached == 0)
633 if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size, lo->ldo_def_stripenr,
634 lo->ldo_def_stripe_offset))
637 /* XXX: use thread info */
642 v3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
643 v3->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
644 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
645 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
646 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
648 cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
649 sizeof(v3->lmm_pool_name));
650 if (cplen >= sizeof(v3->lmm_pool_name)) {
656 info->lti_buf.lb_buf = v3;
657 info->lti_buf.lb_len = sizeof(*v3);
658 rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0, th,
667 * allocate array of objects pointers, find/create objects
668 * stripenr and other fields should be initialized by this moment
670 int lod_initialize_objects(const struct lu_env *env, struct lod_object *lo,
671 struct lov_ost_data_v1 *objs)
673 struct lod_thread_info *info = lod_env_info(env);
674 struct lod_device *md = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
675 struct lu_object *o, *n;
676 struct lu_device *nd;
677 struct dt_object **stripe;
683 LASSERT(lo->ldo_stripe == NULL);
684 LASSERT(lo->ldo_stripenr > 0);
685 LASSERT(lo->ldo_stripe_size > 0);
687 stripe_len = lo->ldo_stripenr;
688 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
692 for (i = 0; i < lo->ldo_stripenr; i++) {
693 ostid_le_to_cpu(&objs[i].l_ost_oi, &info->lti_ostid);
694 idx = le64_to_cpu(objs[i].l_ost_idx);
695 rc = ostid_to_fid(&info->lti_fid, &info->lti_ostid, idx);
698 LASSERTF(fid_is_sane(&info->lti_fid), ""DFID" insane!\n",
699 PFID(&info->lti_fid));
701 * XXX: assertion is left for testing, to make
702 * sure we never process requests till configuration
703 * is completed. to be changed to -EINVAL
706 lod_getref(&md->lod_ost_descs);
707 LASSERT(cfs_bitmap_check(md->lod_ost_bitmap, idx));
708 LASSERT(OST_TGT(md,idx));
709 LASSERTF(OST_TGT(md,idx)->ltd_ost, "idx %d\n", idx);
710 nd = &OST_TGT(md,idx)->ltd_ost->dd_lu_dev;
711 lod_putref(md, &md->lod_ost_descs);
713 /* In the function below, .hs_keycmp resolves to
714 * u_obj_hop_keycmp() */
715 /* coverity[overrun-buffer-val] */
716 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
718 GOTO(out, rc = PTR_ERR(o));
720 n = lu_object_locate(o->lo_header, nd->ld_type);
723 stripe[i] = container_of(n, struct dt_object, do_lu);
728 for (i = 0; i < stripe_len; i++)
729 if (stripe[i] != NULL)
730 lu_object_put(env, &stripe[i]->do_lu);
732 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
734 lo->ldo_stripe = stripe;
735 lo->ldo_stripes_allocated = stripe_len;
742 * Parse striping information stored in lti_ea_store
744 int lod_parse_striping(const struct lu_env *env, struct lod_object *lo,
745 const struct lu_buf *buf)
747 struct lov_mds_md_v1 *lmm;
748 struct lov_ost_data_v1 *objs;
755 LASSERT(buf->lb_buf);
756 LASSERT(buf->lb_len);
758 lmm = (struct lov_mds_md_v1 *) buf->lb_buf;
759 magic = le32_to_cpu(lmm->lmm_magic);
760 pattern = le32_to_cpu(lmm->lmm_pattern);
762 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
763 GOTO(out, rc = -EINVAL);
764 if (lov_pattern(pattern) != LOV_PATTERN_RAID0)
765 GOTO(out, rc = -EINVAL);
767 lo->ldo_pattern = pattern;
768 lo->ldo_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
769 lo->ldo_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
770 lo->ldo_stripenr = le16_to_cpu(lmm->lmm_stripe_count);
771 /* released file stripenr fixup. */
772 if (pattern & LOV_PATTERN_F_RELEASED)
773 lo->ldo_stripenr = 0;
775 LASSERT(buf->lb_len >= lov_mds_md_size(lo->ldo_stripenr, magic));
777 if (magic == LOV_MAGIC_V3) {
778 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
779 objs = &v3->lmm_objects[0];
780 lod_object_set_pool(lo, v3->lmm_pool_name);
782 objs = &lmm->lmm_objects[0];
785 if (lo->ldo_stripenr > 0)
786 rc = lod_initialize_objects(env, lo, objs);
793 * Load and parse striping information, create in-core representation for the
796 int lod_load_striping(const struct lu_env *env, struct lod_object *lo)
798 struct lod_thread_info *info = lod_env_info(env);
799 struct dt_object *next = dt_object_child(&lo->ldo_obj);
804 * currently this code is supposed to be called from declaration
805 * phase only, thus the object is not expected to be locked by caller
807 dt_write_lock(env, next, 0);
808 /* already initialized? */
809 if (lo->ldo_stripe) {
812 for (i = 0; i < lo->ldo_stripenr; i++)
813 LASSERTF(lo->ldo_stripe[i], "stripe %d is NULL\n", i);
817 if (!dt_object_exists(next))
820 /* only regular files can be striped */
821 if (!(lu_object_attr(lod2lu_obj(lo)) & S_IFREG))
824 LASSERT(lo->ldo_stripenr == 0);
826 rc = lod_get_lov_ea(env, lo);
831 * there is LOV EA (striping information) in this object
832 * let's parse it and create in-core objects for the stripes
834 info->lti_buf.lb_buf = info->lti_ea_store;
835 info->lti_buf.lb_len = info->lti_ea_store_size;
836 rc = lod_parse_striping(env, lo, &info->lti_buf);
838 dt_write_unlock(env, next);
842 int lod_verify_striping(struct lod_device *d, const struct lu_buf *buf,
845 struct lov_user_md_v1 *lum;
846 struct lov_user_md_v3 *lum3;
847 struct pool_desc *pool = NULL;
858 LASSERT(sizeof(*lum) < sizeof(*lum3));
860 if (buf->lb_len < sizeof(*lum)) {
861 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md\n",
863 GOTO(out, rc = -EINVAL);
866 magic = le32_to_cpu(lum->lmm_magic);
867 if (magic != LOV_USER_MAGIC_V1 &&
868 magic != LOV_USER_MAGIC_V3 &&
869 magic != LOV_MAGIC_V1_DEF &&
870 magic != LOV_MAGIC_V3_DEF) {
871 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#x\n", magic);
872 GOTO(out, rc = -EINVAL);
875 if ((specific && le32_to_cpu(lum->lmm_pattern) != LOV_PATTERN_RAID0) ||
876 (!specific && lum->lmm_pattern != 0)) {
877 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
878 le32_to_cpu(lum->lmm_pattern));
879 GOTO(out, rc = -EINVAL);
882 /* 64kB is the largest common page size we see (ia64), and matches the
884 stripe_size = le32_to_cpu(lum->lmm_stripe_size);
885 if (stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
886 CDEBUG(D_IOCTL, "stripe size %u not a multiple of %u\n",
887 stripe_size, LOV_MIN_STRIPE_SIZE);
888 GOTO(out, rc = -EINVAL);
891 /* an offset of -1 is treated as a "special" valid offset */
892 stripe_offset = le16_to_cpu(lum->lmm_stripe_offset);
893 if (stripe_offset != (typeof(stripe_offset))-1) {
894 /* if offset is not within valid range [0, osts_size) */
895 if (stripe_offset >= d->lod_osts_size) {
896 CDEBUG(D_IOCTL, "stripe offset %u >= bitmap size %u\n",
897 stripe_offset, d->lod_osts_size);
898 GOTO(out, rc = -EINVAL);
901 /* if lmm_stripe_offset is *not* in bitmap */
902 if (!cfs_bitmap_check(d->lod_ost_bitmap, stripe_offset)) {
903 CDEBUG(D_IOCTL, "stripe offset %u not in bitmap\n",
905 GOTO(out, rc = -EINVAL);
909 stripe_count = le16_to_cpu(lum->lmm_stripe_count);
910 if (magic == LOV_USER_MAGIC_V1 || magic == LOV_MAGIC_V1_DEF)
911 lum_size = offsetof(struct lov_user_md_v1,
912 lmm_objects[stripe_count]);
913 else if (magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF)
914 lum_size = offsetof(struct lov_user_md_v3,
915 lmm_objects[stripe_count]);
919 if (specific && buf->lb_len != lum_size) {
920 CDEBUG(D_IOCTL, "invalid buf len %zd for lov_user_md with "
921 "magic %#x and stripe_count %u\n",
922 buf->lb_len, magic, stripe_count);
923 GOTO(out, rc = -EINVAL);
926 if (!(magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF))
930 if (buf->lb_len < sizeof(*lum3)) {
931 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md_v3\n",
933 GOTO(out, rc = -EINVAL);
936 /* In the function below, .hs_keycmp resolves to
937 * pool_hashkey_keycmp() */
938 /* coverity[overrun-buffer-val] */
939 pool = lod_find_pool(d, lum3->lmm_pool_name);
943 if (stripe_offset != (typeof(stripe_offset))-1) {
944 rc = lod_check_index_in_pool(stripe_offset, pool);
946 GOTO(out, rc = -EINVAL);
949 if (specific && stripe_count > pool_tgt_count(pool)) {
951 "stripe count %u > # OSTs %u in the pool\n",
952 stripe_count, pool_tgt_count(pool));
953 GOTO(out, rc = -EINVAL);
958 lod_pool_putref(pool);
963 void lod_fix_desc_stripe_size(__u64 *val)
965 if (*val < LOV_MIN_STRIPE_SIZE) {
967 LCONSOLE_INFO("Increasing default stripe size to "
968 "minimum value %u\n",
969 LOV_DEFAULT_STRIPE_SIZE);
970 *val = LOV_DEFAULT_STRIPE_SIZE;
971 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
972 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
973 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
975 *val, LOV_MIN_STRIPE_SIZE);
979 void lod_fix_desc_stripe_count(__u32 *val)
985 void lod_fix_desc_pattern(__u32 *val)
987 /* from lov_setstripe */
988 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
989 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
994 void lod_fix_desc_qos_maxage(__u32 *val)
998 *val = QOS_DEFAULT_MAXAGE;
1001 void lod_fix_desc(struct lov_desc *desc)
1003 lod_fix_desc_stripe_size(&desc->ld_default_stripe_size);
1004 lod_fix_desc_stripe_count(&desc->ld_default_stripe_count);
1005 lod_fix_desc_pattern(&desc->ld_pattern);
1006 lod_fix_desc_qos_maxage(&desc->ld_qos_maxage);
1009 int lod_pools_init(struct lod_device *lod, struct lustre_cfg *lcfg)
1011 struct obd_device *obd;
1012 struct lov_desc *desc;
1016 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1017 LASSERT(obd != NULL);
1018 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1020 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1021 CERROR("LOD setup requires a descriptor\n");
1025 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
1027 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1028 CERROR("descriptor size wrong: %d > %d\n",
1029 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1033 if (desc->ld_magic != LOV_DESC_MAGIC) {
1034 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
1035 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
1036 obd->obd_name, desc);
1037 lustre_swab_lov_desc(desc);
1039 CERROR("%s: Bad lov desc magic: %#x\n",
1040 obd->obd_name, desc->ld_magic);
1047 desc->ld_active_tgt_count = 0;
1048 lod->lod_desc = *desc;
1050 lod->lod_sp_me = LUSTRE_SP_CLI;
1052 /* Set up allocation policy (QoS and RR) */
1053 CFS_INIT_LIST_HEAD(&lod->lod_qos.lq_oss_list);
1054 init_rwsem(&lod->lod_qos.lq_rw_sem);
1055 lod->lod_qos.lq_dirty = 1;
1056 lod->lod_qos.lq_rr.lqr_dirty = 1;
1057 lod->lod_qos.lq_reset = 1;
1058 /* Default priority is toward free space balance */
1059 lod->lod_qos.lq_prio_free = 232;
1060 /* Default threshold for rr (roughly 17%) */
1061 lod->lod_qos.lq_threshold_rr = 43;
1062 /* Init statfs fields */
1063 OBD_ALLOC_PTR(lod->lod_qos.lq_statfs_data);
1064 if (NULL == lod->lod_qos.lq_statfs_data)
1066 cfs_waitq_init(&lod->lod_qos.lq_statfs_waitq);
1068 /* Set up OST pool environment */
1069 lod->lod_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
1070 HASH_POOLS_MAX_BITS,
1071 HASH_POOLS_BKT_BITS, 0,
1074 &pool_hash_operations,
1076 if (!lod->lod_pools_hash_body)
1077 GOTO(out_statfs, rc = -ENOMEM);
1078 CFS_INIT_LIST_HEAD(&lod->lod_pool_list);
1079 lod->lod_pool_count = 0;
1080 rc = lod_ost_pool_init(&lod->lod_pool_info, 0);
1083 rc = lod_ost_pool_init(&lod->lod_qos.lq_rr.lqr_pool, 0);
1085 GOTO(out_pool_info, rc);
1090 lod_ost_pool_free(&lod->lod_pool_info);
1092 cfs_hash_putref(lod->lod_pools_hash_body);
1094 OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
1098 int lod_pools_fini(struct lod_device *lod)
1100 struct obd_device *obd = lod2obd(lod);
1101 cfs_list_t *pos, *tmp;
1102 struct pool_desc *pool;
1105 cfs_list_for_each_safe(pos, tmp, &lod->lod_pool_list) {
1106 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
1107 /* free pool structs */
1108 CDEBUG(D_INFO, "delete pool %p\n", pool);
1109 /* In the function below, .hs_keycmp resolves to
1110 * pool_hashkey_keycmp() */
1111 /* coverity[overrun-buffer-val] */
1112 lod_pool_del(obd, pool->pool_name);
1115 cfs_hash_putref(lod->lod_pools_hash_body);
1116 lod_ost_pool_free(&(lod->lod_qos.lq_rr.lqr_pool));
1117 lod_ost_pool_free(&lod->lod_pool_info);
1118 OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);