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>
37 #include <lustre_lfsck.h>
38 #include <lustre_lmv.h>
40 #include "lod_internal.h"
43 * Keep a refcount of lod->ltd_tgts usage to prevent racing with
44 * addition/deletion. Any function that expects lov_tgts to remain stationary
47 * \param lod - is the lod device from which we want to grab a reference
49 void lod_getref(struct lod_tgt_descs *ltd)
51 down_read(<d->ltd_rw_sem);
52 mutex_lock(<d->ltd_mutex);
54 mutex_unlock(<d->ltd_mutex);
58 * Companion of lod_getref() to release a reference on the lod table.
59 * If this is the last reference and the ost entry was scheduled for deletion,
60 * the descriptor is removed from the array.
62 * \param lod - is the lod device from which we release a reference
64 void lod_putref(struct lod_device *lod, struct lod_tgt_descs *ltd)
66 mutex_lock(<d->ltd_mutex);
68 if (ltd->ltd_refcount == 0 && ltd->ltd_death_row) {
69 struct lod_tgt_desc *tgt_desc, *tmp;
73 CDEBUG(D_CONFIG, "destroying %d ltd desc\n",
76 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx) {
77 tgt_desc = LTD_TGT(ltd, idx);
80 if (!tgt_desc->ltd_reap)
83 cfs_list_add(&tgt_desc->ltd_kill, &kill);
84 LTD_TGT(ltd, idx) = NULL;
85 /*FIXME: only support ost pool for now */
86 if (ltd == &lod->lod_ost_descs) {
87 lod_ost_pool_remove(&lod->lod_pool_info, idx);
88 if (tgt_desc->ltd_active)
89 lod->lod_desc.ld_active_tgt_count--;
92 cfs_bitmap_clear(ltd->ltd_tgt_bitmap, idx);
95 mutex_unlock(<d->ltd_mutex);
96 up_read(<d->ltd_rw_sem);
98 cfs_list_for_each_entry_safe(tgt_desc, tmp, &kill, ltd_kill) {
100 cfs_list_del(&tgt_desc->ltd_kill);
101 if (ltd == &lod->lod_ost_descs) {
102 /* remove from QoS structures */
103 rc = qos_del_tgt(lod, tgt_desc);
105 CERROR("%s: qos_del_tgt(%s) failed:"
107 lod2obd(lod)->obd_name,
108 obd_uuid2str(&tgt_desc->ltd_uuid),
111 rc = obd_disconnect(tgt_desc->ltd_exp);
113 CERROR("%s: failed to disconnect %s: rc = %d\n",
114 lod2obd(lod)->obd_name,
115 obd_uuid2str(&tgt_desc->ltd_uuid), rc);
116 OBD_FREE_PTR(tgt_desc);
119 mutex_unlock(<d->ltd_mutex);
120 up_read(<d->ltd_rw_sem);
124 static int ltd_bitmap_resize(struct lod_tgt_descs *ltd, __u32 newsize)
126 cfs_bitmap_t *new_bitmap, *old_bitmap = NULL;
130 /* grab write reference on the lod. Relocating the array requires
131 * exclusive access */
133 down_write(<d->ltd_rw_sem);
134 if (newsize <= ltd->ltd_tgts_size)
135 /* someone else has already resize the array */
138 /* allocate new bitmap */
139 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
141 GOTO(out, rc = -ENOMEM);
143 if (ltd->ltd_tgts_size > 0) {
144 /* the bitmap already exists, we need
145 * to copy data from old one */
146 cfs_bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap);
147 old_bitmap = ltd->ltd_tgt_bitmap;
150 ltd->ltd_tgts_size = newsize;
151 ltd->ltd_tgt_bitmap = new_bitmap;
154 CFS_FREE_BITMAP(old_bitmap);
156 CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
160 up_write(<d->ltd_rw_sem);
165 * Connect LOD to a new OSP and add it to the device table.
167 * \param env - is the environment passed by the caller
168 * \param lod - is the LOD device to be connected to the new OSP
169 * \param osp - is the name of OSP device name about to be added
170 * \param index - is the OSP index
171 * \param gen - is the generation number
172 * \param tgt_index - is the group of the OSP.
173 * \param type - is the type of device (mdc or osc)
175 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
176 char *osp, unsigned index, unsigned gen, int tgt_index,
177 char *type, int active)
179 struct obd_connect_data *data = NULL;
180 struct obd_export *exp = NULL;
181 struct obd_device *obd;
182 struct lu_device *ldev;
185 struct lod_tgt_desc *tgt_desc;
186 struct lod_tgt_descs *ltd;
187 struct obd_uuid obd_uuid;
191 CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, index, gen);
194 CERROR("request to add OBD %s with invalid generation: %d\n",
199 obd_str2uuid(&obd_uuid, osp);
201 obd = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
202 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
204 CERROR("can't find %s device\n", osp);
212 data->ocd_connect_flags = OBD_CONNECT_INDEX | OBD_CONNECT_VERSION;
213 data->ocd_version = LUSTRE_VERSION_CODE;
214 data->ocd_index = index;
216 if (strcmp(LUSTRE_OSC_NAME, type) == 0) {
218 data->ocd_connect_flags |= OBD_CONNECT_AT |
221 #ifdef HAVE_LRU_RESIZE_SUPPORT
222 OBD_CONNECT_LRU_RESIZE |
225 OBD_CONNECT_OSS_CAPA |
226 OBD_CONNECT_REQPORTAL |
227 OBD_CONNECT_SKIP_ORPHAN |
229 OBD_CONNECT_LVB_TYPE |
230 OBD_CONNECT_VERSION |
231 OBD_CONNECT_PINGLESS |
234 data->ocd_group = tgt_index;
235 ltd = &lod->lod_ost_descs;
237 struct obd_import *imp = obd->u.cli.cl_import;
240 data->ocd_ibits_known = MDS_INODELOCK_UPDATE;
241 data->ocd_connect_flags |= OBD_CONNECT_ACL |
242 OBD_CONNECT_MDS_CAPA |
243 OBD_CONNECT_OSS_CAPA |
245 OBD_CONNECT_MDS_MDS |
250 /* XXX set MDS-MDS flags, remove this when running this
252 data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS;
253 spin_lock(&imp->imp_lock);
254 imp->imp_server_timeout = 1;
255 spin_unlock(&imp->imp_lock);
256 imp->imp_client->cli_request_portal = OUT_PORTAL;
257 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
259 ltd = &lod->lod_mdt_descs;
262 rc = obd_connect(env, &exp, obd, &obd->obd_uuid, data, NULL);
265 CERROR("%s: cannot connect to next dev %s (%d)\n",
266 obd->obd_name, osp, rc);
270 LASSERT(obd->obd_lu_dev);
271 LASSERT(obd->obd_lu_dev->ld_site == lod->lod_dt_dev.dd_lu_dev.ld_site);
273 ldev = obd->obd_lu_dev;
276 /* Allocate ost descriptor and fill it */
277 OBD_ALLOC_PTR(tgt_desc);
279 GOTO(out_conn, rc = -ENOMEM);
281 tgt_desc->ltd_tgt = d;
282 tgt_desc->ltd_exp = exp;
283 tgt_desc->ltd_uuid = obd->u.cli.cl_target_uuid;
284 tgt_desc->ltd_gen = gen;
285 tgt_desc->ltd_index = index;
286 tgt_desc->ltd_active = active;
289 if (index >= ltd->ltd_tgts_size) {
290 /* we have to increase the size of the lod_osts array */
293 newsize = max(ltd->ltd_tgts_size, (__u32)2);
294 while (newsize < index + 1)
295 newsize = newsize << 1;
297 /* lod_bitmap_resize() needs lod_rw_sem
298 * which we hold with th reference */
299 lod_putref(lod, ltd);
301 rc = ltd_bitmap_resize(ltd, newsize);
308 mutex_lock(<d->ltd_mutex);
309 if (cfs_bitmap_check(ltd->ltd_tgt_bitmap, index)) {
310 CERROR("%s: device %d is registered already\n", obd->obd_name,
312 GOTO(out_mutex, rc = -EEXIST);
315 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
316 OBD_ALLOC_PTR(ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK]);
317 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
318 CERROR("can't allocate index to add %s\n",
320 GOTO(out_mutex, rc = -ENOMEM);
324 if (!strcmp(LUSTRE_OSC_NAME, type)) {
325 /* pool and qos are not supported for MDS stack yet */
326 rc = lod_ost_pool_add(&lod->lod_pool_info, index,
329 CERROR("%s: can't set up pool, failed with %d\n",
334 rc = qos_add_tgt(lod, tgt_desc);
336 CERROR("%s: qos_add_tgt failed with %d\n",
341 /* The new OST is now a full citizen */
342 if (index >= lod->lod_desc.ld_tgt_count)
343 lod->lod_desc.ld_tgt_count = index + 1;
345 lod->lod_desc.ld_active_tgt_count++;
348 LTD_TGT(ltd, index) = tgt_desc;
349 cfs_bitmap_set(ltd->ltd_tgt_bitmap, index);
351 mutex_unlock(<d->ltd_mutex);
352 lod_putref(lod, ltd);
353 if (lod->lod_recovery_completed)
354 ldev->ld_ops->ldo_recovery_complete(env, ldev);
356 rc = lfsck_add_target(env, lod->lod_child, d, exp, index, for_ost);
358 CERROR("Fail to add LFSCK target: name = %s, type = %s, "
359 "index = %u, rc = %d\n", osp, type, index, rc);
364 lod_ost_pool_remove(&lod->lod_pool_info, index);
366 mutex_unlock(<d->ltd_mutex);
367 lod_putref(lod, ltd);
369 OBD_FREE_PTR(tgt_desc);
377 * helper function to schedule OST removal from the device table
379 static void __lod_del_device(const struct lu_env *env, struct lod_device *lod,
380 struct lod_tgt_descs *ltd, unsigned idx,
383 LASSERT(LTD_TGT(ltd, idx));
385 lfsck_del_target(env, lod->lod_child, LTD_TGT(ltd, idx)->ltd_tgt,
388 if (LTD_TGT(ltd, idx)->ltd_reap == 0) {
389 LTD_TGT(ltd, idx)->ltd_reap = 1;
390 ltd->ltd_death_row++;
394 int lod_fini_tgt(const struct lu_env *env, struct lod_device *lod,
395 struct lod_tgt_descs *ltd, bool for_ost)
399 if (ltd->ltd_tgts_size <= 0)
402 mutex_lock(<d->ltd_mutex);
403 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx)
404 __lod_del_device(env, lod, ltd, idx, for_ost);
405 mutex_unlock(<d->ltd_mutex);
406 lod_putref(lod, ltd);
407 CFS_FREE_BITMAP(ltd->ltd_tgt_bitmap);
408 for (idx = 0; idx < TGT_PTRS; idx++) {
409 if (ltd->ltd_tgt_idx[idx])
410 OBD_FREE_PTR(ltd->ltd_tgt_idx[idx]);
412 ltd->ltd_tgts_size = 0;
417 * Add support for administratively disabled OST (through the MGS).
418 * Schedule a target for deletion. Disconnection and real removal from the
419 * table takes place in lod_putref() once the last table user release its
422 * \param env - is the environment passed by the caller
423 * \param lod - is the lod device currently connected to the OSP about to be
425 * \param osp - is the name of OSP device about to be removed
426 * \param idx - is the OSP index
427 * \param gen - is the generation number, not used currently
429 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
430 struct lod_tgt_descs *ltd, char *osp, unsigned idx,
431 unsigned gen, bool for_ost)
433 struct obd_device *obd;
435 struct obd_uuid uuid;
438 CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, idx, gen);
440 obd_str2uuid(&uuid, osp);
442 obd = class_find_client_obd(&uuid, LUSTRE_OSP_NAME,
443 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
445 CERROR("can't find %s device\n", osp);
450 CERROR("%s: request to remove OBD %s with invalid generation %d"
451 "\n", obd->obd_name, osp, gen);
455 obd_str2uuid(&uuid, osp);
458 mutex_lock(<d->ltd_mutex);
459 /* check that the index is allocated in the bitmap */
460 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) ||
461 !LTD_TGT(ltd, idx)) {
462 CERROR("%s: device %d is not set up\n", obd->obd_name, idx);
463 GOTO(out, rc = -EINVAL);
466 /* check that the UUID matches */
467 if (!obd_uuid_equals(&uuid, <D_TGT(ltd, idx)->ltd_uuid)) {
468 CERROR("%s: LOD target UUID %s at index %d does not match %s\n",
469 obd->obd_name, obd_uuid2str(<D_TGT(ltd,idx)->ltd_uuid),
471 GOTO(out, rc = -EINVAL);
474 __lod_del_device(env, lod, ltd, idx, for_ost);
477 mutex_unlock(<d->ltd_mutex);
478 lod_putref(lod, ltd);
482 int lod_ea_store_resize(struct lod_thread_info *info, size_t size)
484 __u32 round = size_roundup_power2(size);
487 lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3));
488 if (info->lti_ea_store) {
489 LASSERT(info->lti_ea_store_size);
490 LASSERT(info->lti_ea_store_size < round);
491 CDEBUG(D_INFO, "EA store size %d is not enough, need %d\n",
492 info->lti_ea_store_size, round);
493 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
494 info->lti_ea_store = NULL;
495 info->lti_ea_store_size = 0;
498 OBD_ALLOC_LARGE(info->lti_ea_store, round);
499 if (info->lti_ea_store == NULL)
501 info->lti_ea_store_size = round;
506 * generate and write LOV EA for given striped object
508 int lod_generate_and_set_lovea(const struct lu_env *env,
509 struct lod_object *lo, struct thandle *th)
511 struct lod_thread_info *info = lod_env_info(env);
512 struct dt_object *next = dt_object_child(&lo->ldo_obj);
513 const struct lu_fid *fid = lu_object_fid(&lo->ldo_obj.do_lu);
514 struct lov_mds_md_v1 *lmm;
515 struct lov_ost_data_v1 *objs;
523 magic = lo->ldo_pool != NULL ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
524 lmm_size = lov_mds_md_size(lo->ldo_stripenr, magic);
525 if (info->lti_ea_store_size < lmm_size) {
526 rc = lod_ea_store_resize(info, lmm_size);
531 if (lo->ldo_pattern == 0) /* default striping */
532 lo->ldo_pattern = LOV_PATTERN_RAID0;
534 lmm = info->lti_ea_store;
536 lmm->lmm_magic = cpu_to_le32(magic);
537 lmm->lmm_pattern = cpu_to_le32(lo->ldo_pattern);
538 fid_to_lmm_oi(fid, &lmm->lmm_oi);
539 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_LMMOI))
540 lmm->lmm_oi.oi.oi_id++;
541 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
542 lmm->lmm_stripe_size = cpu_to_le32(lo->ldo_stripe_size);
543 lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_stripenr);
544 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED)
545 lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_released_stripenr);
546 lmm->lmm_layout_gen = 0;
547 if (magic == LOV_MAGIC_V1) {
548 objs = &lmm->lmm_objects[0];
550 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
551 size_t cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
552 sizeof(v3->lmm_pool_name));
553 if (cplen >= sizeof(v3->lmm_pool_name))
555 objs = &v3->lmm_objects[0];
558 for (i = 0; i < lo->ldo_stripenr; i++) {
559 struct lu_fid *fid = &info->lti_fid;
560 struct lod_device *lod;
562 int type = LU_SEQ_RANGE_OST;
564 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
565 LASSERT(lo->ldo_stripe[i]);
567 *fid = *lu_object_fid(&lo->ldo_stripe[i]->do_lu);
568 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MULTIPLE_REF)) {
569 if (cfs_fail_val == 0)
570 cfs_fail_val = fid->f_oid;
572 fid->f_oid = cfs_fail_val;
575 rc = fid_to_ostid(fid, &info->lti_ostid);
578 ostid_cpu_to_le(&info->lti_ostid, &objs[i].l_ost_oi);
579 objs[i].l_ost_gen = cpu_to_le32(0);
580 rc = lod_fld_lookup(env, lod, fid, &index, &type);
582 CERROR("%s: Can not locate "DFID": rc = %d\n",
583 lod2obd(lod)->obd_name, PFID(fid), rc);
584 lod_object_free_striping(env, lo);
587 objs[i].l_ost_idx = cpu_to_le32(index);
590 info->lti_buf.lb_buf = lmm;
591 info->lti_buf.lb_len = lmm_size;
592 rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0,
595 lod_object_free_striping(env, lo);
600 int lod_get_ea(const struct lu_env *env, struct lod_object *lo,
603 struct lod_thread_info *info = lod_env_info(env);
604 struct dt_object *next = dt_object_child(&lo->ldo_obj);
610 if (unlikely(info->lti_ea_store == NULL)) {
611 /* just to enter in allocation block below */
615 info->lti_buf.lb_buf = info->lti_ea_store;
616 info->lti_buf.lb_len = info->lti_ea_store_size;
617 rc = dt_xattr_get(env, next, &info->lti_buf, name, BYPASS_CAPA);
620 /* if object is not striped or inaccessible */
621 if (rc == -ENODATA || rc == -ENOENT)
625 /* EA doesn't fit, reallocate new buffer */
626 rc = dt_xattr_get(env, next, &LU_BUF_NULL, name,
628 if (rc == -ENODATA || rc == -ENOENT)
634 rc = lod_ea_store_resize(info, rc);
643 int lod_store_def_striping(const struct lu_env *env, struct dt_object *dt,
646 struct lod_thread_info *info = lod_env_info(env);
647 struct lod_object *lo = lod_dt_obj(dt);
648 struct dt_object *next = dt_object_child(dt);
649 struct lov_user_md_v3 *v3;
653 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
656 * store striping defaults into new directory
657 * used to implement defaults inheritance
660 /* probably nothing to inherite */
661 if (lo->ldo_striping_cached == 0)
664 if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size, lo->ldo_def_stripenr,
665 lo->ldo_def_stripe_offset))
668 v3 = info->lti_ea_store;
669 if (info->lti_ea_store_size < sizeof(*v3)) {
670 rc = lod_ea_store_resize(info, sizeof(*v3));
673 v3 = info->lti_ea_store;
675 memset(v3, 0, sizeof(*v3));
676 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
677 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
678 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
679 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
680 if (lo->ldo_pool != NULL)
681 strlcpy(v3->lmm_pool_name, lo->ldo_pool,
682 sizeof(v3->lmm_pool_name));
683 info->lti_buf.lb_buf = v3;
684 info->lti_buf.lb_len = sizeof(*v3);
685 rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0, th,
691 static int validate_lod_and_idx(struct lod_device *md, __u32 idx)
693 if (unlikely(idx >= md->lod_ost_descs.ltd_tgts_size ||
694 !cfs_bitmap_check(md->lod_ost_bitmap, idx))) {
695 CERROR("%s: bad idx: %d of %d\n", lod2obd(md)->obd_name, idx,
696 md->lod_ost_descs.ltd_tgts_size);
700 if (unlikely(OST_TGT(md, idx) == NULL)) {
701 CERROR("%s: bad lod_tgt_desc for idx: %d\n",
702 lod2obd(md)->obd_name, idx);
706 if (unlikely(OST_TGT(md, idx)->ltd_ost == NULL)) {
707 CERROR("%s: invalid lod device, for idx: %d\n",
708 lod2obd(md)->obd_name , idx);
716 * allocate array of objects pointers, find/create objects
717 * stripenr and other fields should be initialized by this moment
719 int lod_initialize_objects(const struct lu_env *env, struct lod_object *lo,
720 struct lov_ost_data_v1 *objs)
722 struct lod_thread_info *info = lod_env_info(env);
723 struct lod_device *md;
724 struct lu_object *o, *n;
725 struct lu_device *nd;
726 struct dt_object **stripe;
733 md = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
734 LASSERT(lo->ldo_stripe == NULL);
735 LASSERT(lo->ldo_stripenr > 0);
736 LASSERT(lo->ldo_stripe_size > 0);
738 stripe_len = lo->ldo_stripenr;
739 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
743 for (i = 0; i < lo->ldo_stripenr; i++) {
744 if (unlikely(lovea_slot_is_dummy(&objs[i])))
747 ostid_le_to_cpu(&objs[i].l_ost_oi, &info->lti_ostid);
748 idx = le32_to_cpu(objs[i].l_ost_idx);
749 rc = ostid_to_fid(&info->lti_fid, &info->lti_ostid, idx);
752 LASSERTF(fid_is_sane(&info->lti_fid), ""DFID" insane!\n",
753 PFID(&info->lti_fid));
754 lod_getref(&md->lod_ost_descs);
756 rc = validate_lod_and_idx(md, idx);
757 if (unlikely(rc != 0)) {
758 lod_putref(md, &md->lod_ost_descs);
762 nd = &OST_TGT(md,idx)->ltd_ost->dd_lu_dev;
763 lod_putref(md, &md->lod_ost_descs);
765 /* In the function below, .hs_keycmp resolves to
766 * u_obj_hop_keycmp() */
767 /* coverity[overrun-buffer-val] */
768 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
770 GOTO(out, rc = PTR_ERR(o));
772 n = lu_object_locate(o->lo_header, nd->ld_type);
775 stripe[i] = container_of(n, struct dt_object, do_lu);
780 for (i = 0; i < stripe_len; i++)
781 if (stripe[i] != NULL)
782 lu_object_put(env, &stripe[i]->do_lu);
784 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
785 lo->ldo_stripenr = 0;
787 lo->ldo_stripe = stripe;
788 lo->ldo_stripes_allocated = stripe_len;
795 * Parse striping information stored in lti_ea_store
797 int lod_parse_striping(const struct lu_env *env, struct lod_object *lo,
798 const struct lu_buf *buf)
800 struct lov_mds_md_v1 *lmm;
801 struct lov_ost_data_v1 *objs;
808 LASSERT(buf->lb_buf);
809 LASSERT(buf->lb_len);
811 lmm = (struct lov_mds_md_v1 *) buf->lb_buf;
812 magic = le32_to_cpu(lmm->lmm_magic);
813 pattern = le32_to_cpu(lmm->lmm_pattern);
815 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
816 GOTO(out, rc = -EINVAL);
817 if (lov_pattern(pattern) != LOV_PATTERN_RAID0)
818 GOTO(out, rc = -EINVAL);
820 lo->ldo_pattern = pattern;
821 lo->ldo_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
822 lo->ldo_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
823 lo->ldo_stripenr = le16_to_cpu(lmm->lmm_stripe_count);
824 /* released file stripenr fixup. */
825 if (pattern & LOV_PATTERN_F_RELEASED)
826 lo->ldo_stripenr = 0;
828 LASSERT(buf->lb_len >= lov_mds_md_size(lo->ldo_stripenr, magic));
830 if (magic == LOV_MAGIC_V3) {
831 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
832 objs = &v3->lmm_objects[0];
833 lod_object_set_pool(lo, v3->lmm_pool_name);
835 objs = &lmm->lmm_objects[0];
838 if (lo->ldo_stripenr > 0)
839 rc = lod_initialize_objects(env, lo, objs);
845 int lod_load_striping_locked(const struct lu_env *env, struct lod_object *lo)
847 struct lod_thread_info *info = lod_env_info(env);
848 struct lu_buf *buf = &info->lti_buf;
849 struct dt_object *next = dt_object_child(&lo->ldo_obj);
853 /* already initialized? */
854 if (lo->ldo_stripe != NULL)
857 if (!dt_object_exists(next))
860 /* Do not load stripe for slaves of striped dir */
861 if (lo->ldo_dir_slave_stripe)
864 if (S_ISREG(lu_object_attr(lod2lu_obj(lo)))) {
865 rc = lod_get_lov_ea(env, lo);
869 * there is LOV EA (striping information) in this object
870 * let's parse it and create in-core objects for the stripes
872 buf->lb_buf = info->lti_ea_store;
873 buf->lb_len = info->lti_ea_store_size;
874 rc = lod_parse_striping(env, lo, buf);
875 } else if (S_ISDIR(lu_object_attr(lod2lu_obj(lo)))) {
876 rc = lod_get_lmv_ea(env, lo);
877 if (rc < (typeof(rc))sizeof(struct lmv_mds_md_v1))
878 GOTO(out, rc = rc > 0 ? -EINVAL : rc);
880 buf->lb_buf = info->lti_ea_store;
881 buf->lb_len = info->lti_ea_store_size;
882 if (rc == sizeof(struct lmv_mds_md_v1)) {
883 rc = lod_load_lmv_shards(env, lo, buf, true);
884 if (buf->lb_buf != info->lti_ea_store) {
885 OBD_FREE_LARGE(info->lti_ea_store,
886 info->lti_ea_store_size);
887 info->lti_ea_store = buf->lb_buf;
888 info->lti_ea_store_size = buf->lb_len;
896 * there is LOV EA (striping information) in this object
897 * let's parse it and create in-core objects for the stripes
899 rc = lod_parse_dir_striping(env, lo, buf);
906 * Load and parse striping information, create in-core representation for the
909 int lod_load_striping(const struct lu_env *env, struct lod_object *lo)
911 struct dt_object *next = dt_object_child(&lo->ldo_obj);
914 /* currently this code is supposed to be called from declaration
915 * phase only, thus the object is not expected to be locked by caller */
916 dt_write_lock(env, next, 0);
917 rc = lod_load_striping_locked(env, lo);
918 dt_write_unlock(env, next);
922 /* verify the striping information for directory */
923 int lod_verify_striping(struct lod_device *d, const struct lu_buf *buf,
926 struct lov_user_md_v1 *lum;
927 struct lov_user_md_v3 *lum3;
928 struct pool_desc *pool = NULL;
939 LASSERT(sizeof(*lum) < sizeof(*lum3));
941 if (buf->lb_len < sizeof(*lum)) {
942 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md\n",
944 GOTO(out, rc = -EINVAL);
947 magic = le32_to_cpu(lum->lmm_magic);
948 if (magic != LOV_USER_MAGIC_V1 &&
949 magic != LOV_USER_MAGIC_V3 &&
950 magic != LOV_MAGIC_V1_DEF &&
951 magic != LOV_MAGIC_V3_DEF) {
952 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#x\n", magic);
953 GOTO(out, rc = -EINVAL);
956 /* the user uses "0" for default stripe pattern normally. */
957 if (!is_from_disk && lum->lmm_pattern == 0)
958 lum->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
960 if (le32_to_cpu(lum->lmm_pattern) != LOV_PATTERN_RAID0) {
961 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
962 le32_to_cpu(lum->lmm_pattern));
963 GOTO(out, rc = -EINVAL);
966 /* 64kB is the largest common page size we see (ia64), and matches the
968 stripe_size = le32_to_cpu(lum->lmm_stripe_size);
969 if (stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
970 CDEBUG(D_IOCTL, "stripe size %u not a multiple of %u\n",
971 stripe_size, LOV_MIN_STRIPE_SIZE);
972 GOTO(out, rc = -EINVAL);
975 /* an offset of -1 is treated as a "special" valid offset */
976 stripe_offset = le16_to_cpu(lum->lmm_stripe_offset);
977 if (stripe_offset != (typeof(stripe_offset))-1) {
978 /* if offset is not within valid range [0, osts_size) */
979 if (stripe_offset >= d->lod_osts_size) {
980 CDEBUG(D_IOCTL, "stripe offset %u >= bitmap size %u\n",
981 stripe_offset, d->lod_osts_size);
982 GOTO(out, rc = -EINVAL);
985 /* if lmm_stripe_offset is *not* in bitmap */
986 if (!cfs_bitmap_check(d->lod_ost_bitmap, stripe_offset)) {
987 CDEBUG(D_IOCTL, "stripe offset %u not in bitmap\n",
989 GOTO(out, rc = -EINVAL);
993 if (magic == LOV_USER_MAGIC_V1 || magic == LOV_MAGIC_V1_DEF)
994 lum_size = offsetof(struct lov_user_md_v1,
996 else if (magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF)
997 lum_size = offsetof(struct lov_user_md_v3,
1000 GOTO(out, rc = -EINVAL);
1002 stripe_count = le16_to_cpu(lum->lmm_stripe_count);
1003 if (buf->lb_len != lum_size) {
1004 CDEBUG(D_IOCTL, "invalid buf len %zd for lov_user_md with "
1005 "magic %#x and stripe_count %u\n",
1006 buf->lb_len, magic, stripe_count);
1007 GOTO(out, rc = -EINVAL);
1010 if (!(magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF))
1014 if (buf->lb_len < sizeof(*lum3)) {
1015 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md_v3\n",
1017 GOTO(out, rc = -EINVAL);
1020 /* In the function below, .hs_keycmp resolves to
1021 * pool_hashkey_keycmp() */
1022 /* coverity[overrun-buffer-val] */
1023 pool = lod_find_pool(d, lum3->lmm_pool_name);
1027 if (stripe_offset != (typeof(stripe_offset))-1) {
1028 rc = lod_check_index_in_pool(stripe_offset, pool);
1030 GOTO(out, rc = -EINVAL);
1033 if (is_from_disk && stripe_count > pool_tgt_count(pool)) {
1035 "stripe count %u > # OSTs %u in the pool\n",
1036 stripe_count, pool_tgt_count(pool));
1037 GOTO(out, rc = -EINVAL);
1042 lod_pool_putref(pool);
1047 void lod_fix_desc_stripe_size(__u64 *val)
1049 if (*val < LOV_MIN_STRIPE_SIZE) {
1051 LCONSOLE_INFO("Increasing default stripe size to "
1052 "minimum value %u\n",
1053 LOV_DESC_STRIPE_SIZE_DEFAULT);
1054 *val = LOV_DESC_STRIPE_SIZE_DEFAULT;
1055 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
1056 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
1057 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
1058 "multiple of %u)\n",
1059 *val, LOV_MIN_STRIPE_SIZE);
1063 void lod_fix_desc_stripe_count(__u32 *val)
1069 void lod_fix_desc_pattern(__u32 *val)
1071 /* from lov_setstripe */
1072 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
1073 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
1078 void lod_fix_desc_qos_maxage(__u32 *val)
1080 /* fix qos_maxage */
1082 *val = LOV_DESC_QOS_MAXAGE_DEFAULT;
1085 void lod_fix_desc(struct lov_desc *desc)
1087 lod_fix_desc_stripe_size(&desc->ld_default_stripe_size);
1088 lod_fix_desc_stripe_count(&desc->ld_default_stripe_count);
1089 lod_fix_desc_pattern(&desc->ld_pattern);
1090 lod_fix_desc_qos_maxage(&desc->ld_qos_maxage);
1093 int lod_pools_init(struct lod_device *lod, struct lustre_cfg *lcfg)
1095 struct obd_device *obd;
1096 struct lov_desc *desc;
1100 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1101 LASSERT(obd != NULL);
1102 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1104 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1105 CERROR("LOD setup requires a descriptor\n");
1109 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
1111 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1112 CERROR("descriptor size wrong: %d > %d\n",
1113 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1117 if (desc->ld_magic != LOV_DESC_MAGIC) {
1118 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
1119 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
1120 obd->obd_name, desc);
1121 lustre_swab_lov_desc(desc);
1123 CERROR("%s: Bad lov desc magic: %#x\n",
1124 obd->obd_name, desc->ld_magic);
1131 desc->ld_active_tgt_count = 0;
1132 lod->lod_desc = *desc;
1134 lod->lod_sp_me = LUSTRE_SP_CLI;
1136 /* Set up allocation policy (QoS and RR) */
1137 CFS_INIT_LIST_HEAD(&lod->lod_qos.lq_oss_list);
1138 init_rwsem(&lod->lod_qos.lq_rw_sem);
1139 lod->lod_qos.lq_dirty = 1;
1140 lod->lod_qos.lq_rr.lqr_dirty = 1;
1141 lod->lod_qos.lq_reset = 1;
1142 /* Default priority is toward free space balance */
1143 lod->lod_qos.lq_prio_free = 232;
1144 /* Default threshold for rr (roughly 17%) */
1145 lod->lod_qos.lq_threshold_rr = 43;
1147 /* Set up OST pool environment */
1148 lod->lod_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
1149 HASH_POOLS_MAX_BITS,
1150 HASH_POOLS_BKT_BITS, 0,
1153 &pool_hash_operations,
1155 if (lod->lod_pools_hash_body == NULL)
1158 CFS_INIT_LIST_HEAD(&lod->lod_pool_list);
1159 lod->lod_pool_count = 0;
1160 rc = lod_ost_pool_init(&lod->lod_pool_info, 0);
1163 rc = lod_ost_pool_init(&lod->lod_qos.lq_rr.lqr_pool, 0);
1165 GOTO(out_pool_info, rc);
1170 lod_ost_pool_free(&lod->lod_pool_info);
1172 cfs_hash_putref(lod->lod_pools_hash_body);
1177 int lod_pools_fini(struct lod_device *lod)
1179 struct obd_device *obd = lod2obd(lod);
1180 cfs_list_t *pos, *tmp;
1181 struct pool_desc *pool;
1184 cfs_list_for_each_safe(pos, tmp, &lod->lod_pool_list) {
1185 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
1186 /* free pool structs */
1187 CDEBUG(D_INFO, "delete pool %p\n", pool);
1188 /* In the function below, .hs_keycmp resolves to
1189 * pool_hashkey_keycmp() */
1190 /* coverity[overrun-buffer-val] */
1191 lod_pool_del(obd, pool->pool_name);
1194 cfs_hash_putref(lod->lod_pools_hash_body);
1195 lod_ost_pool_free(&(lod->lod_qos.lq_rr.lqr_pool));
1196 lod_ost_pool_free(&lod->lod_pool_info);