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>
35 # define EXPORT_SYMTAB
37 #define DEBUG_SUBSYSTEM S_MDS
39 #include <obd_class.h>
42 #include "lod_internal.h"
45 * Keep a refcount of lod->ltd_tgts usage to prevent racing with
46 * addition/deletion. Any function that expects lov_tgts to remain stationary
49 * \param lod - is the lod device from which we want to grab a reference
51 void lod_getref(struct lod_tgt_descs *ltd)
53 down_read(<d->ltd_rw_sem);
54 mutex_lock(<d->ltd_mutex);
56 mutex_unlock(<d->ltd_mutex);
60 * Companion of lod_getref() to release a reference on the lod table.
61 * If this is the last reference and the ost entry was scheduled for deletion,
62 * the descriptor is removed from the array.
64 * \param lod - is the lod device from which we release a reference
66 void lod_putref(struct lod_device *lod, struct lod_tgt_descs *ltd)
68 mutex_lock(<d->ltd_mutex);
70 if (ltd->ltd_refcount == 0 && ltd->ltd_death_row) {
71 struct lod_tgt_desc *tgt_desc, *tmp;
75 CDEBUG(D_CONFIG, "destroying %d ltd desc\n",
78 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx) {
79 tgt_desc = LTD_TGT(ltd, idx);
82 if (!tgt_desc->ltd_reap)
85 cfs_list_add(&tgt_desc->ltd_kill, &kill);
86 LTD_TGT(ltd, idx) = NULL;
87 /*FIXME: only support ost pool for now */
88 if (ltd == &lod->lod_ost_descs) {
89 lod_ost_pool_remove(&lod->lod_pool_info, idx);
90 if (tgt_desc->ltd_active)
91 lod->lod_desc.ld_active_tgt_count--;
94 cfs_bitmap_clear(ltd->ltd_tgt_bitmap, idx);
97 mutex_unlock(<d->ltd_mutex);
98 up_read(<d->ltd_rw_sem);
100 cfs_list_for_each_entry_safe(tgt_desc, tmp, &kill, ltd_kill) {
102 cfs_list_del(&tgt_desc->ltd_kill);
103 if (ltd == &lod->lod_ost_descs) {
104 /* remove from QoS structures */
105 rc = qos_del_tgt(lod, tgt_desc);
107 CERROR("%s: qos_del_tgt(%s) failed:"
109 lod2obd(lod)->obd_name,
110 obd_uuid2str(&tgt_desc->ltd_uuid),
113 rc = obd_disconnect(tgt_desc->ltd_exp);
115 CERROR("%s: failed to disconnect %s: rc = %d\n",
116 lod2obd(lod)->obd_name,
117 obd_uuid2str(&tgt_desc->ltd_uuid), rc);
118 OBD_FREE_PTR(tgt_desc);
121 mutex_unlock(<d->ltd_mutex);
122 up_read(<d->ltd_rw_sem);
126 static int ltd_bitmap_resize(struct lod_tgt_descs *ltd, __u32 newsize)
128 cfs_bitmap_t *new_bitmap, *old_bitmap = NULL;
132 /* grab write reference on the lod. Relocating the array requires
133 * exclusive access */
135 down_write(<d->ltd_rw_sem);
136 if (newsize <= ltd->ltd_tgts_size)
137 /* someone else has already resize the array */
140 /* allocate new bitmap */
141 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
143 GOTO(out, rc = -ENOMEM);
145 if (ltd->ltd_tgts_size > 0) {
146 /* the bitmap already exists, we need
147 * to copy data from old one */
148 cfs_bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap);
149 old_bitmap = ltd->ltd_tgt_bitmap;
152 ltd->ltd_tgts_size = newsize;
153 ltd->ltd_tgt_bitmap = new_bitmap;
156 CFS_FREE_BITMAP(old_bitmap);
158 CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
162 up_write(<d->ltd_rw_sem);
167 * Connect LOD to a new OSP and add it to the device table.
169 * \param env - is the environment passed by the caller
170 * \param lod - is the LOD device to be connected to the new OSP
171 * \param osp - is the name of OSP device name about to be added
172 * \param index - is the OSP index
173 * \param gen - is the generation number
174 * \param tgt_index - is the group of the OSP.
175 * \param type - is the type of device (mdc or osc)
177 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
178 char *osp, unsigned index, unsigned gen, int tgt_index,
179 char *type, int active)
181 struct obd_connect_data *data = NULL;
182 struct obd_export *exp = NULL;
183 struct obd_device *obd;
184 struct lu_device *ldev;
187 struct lod_tgt_desc *tgt_desc;
188 struct lod_tgt_descs *ltd;
189 struct obd_uuid obd_uuid;
192 CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, index, gen);
195 CERROR("request to add OBD %s with invalid generation: %d\n",
200 obd_str2uuid(&obd_uuid, osp);
202 obd = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
203 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
205 CERROR("can't find %s device\n", osp);
213 data->ocd_connect_flags = OBD_CONNECT_INDEX | OBD_CONNECT_VERSION;
214 data->ocd_version = LUSTRE_VERSION_CODE;
215 data->ocd_index = index;
217 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;
233 data->ocd_group = tgt_index;
234 ltd = &lod->lod_ost_descs;
236 struct obd_import *imp = obd->u.cli.cl_import;
238 data->ocd_ibits_known = MDS_INODELOCK_UPDATE;
239 data->ocd_connect_flags |= OBD_CONNECT_ACL |
240 OBD_CONNECT_MDS_CAPA |
241 OBD_CONNECT_OSS_CAPA |
243 OBD_CONNECT_MDS_MDS |
247 /* XXX set MDS-MDS flags, remove this when running this
249 data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS;
250 spin_lock(&imp->imp_lock);
251 imp->imp_server_timeout = 1;
252 spin_unlock(&imp->imp_lock);
253 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
254 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
256 ltd = &lod->lod_mdt_descs;
259 rc = obd_connect(env, &exp, obd, &obd->obd_uuid, data, NULL);
262 CERROR("%s: cannot connect to next dev %s (%d)\n",
263 obd->obd_name, osp, rc);
267 LASSERT(obd->obd_lu_dev);
268 LASSERT(obd->obd_lu_dev->ld_site == lod->lod_dt_dev.dd_lu_dev.ld_site);
270 ldev = obd->obd_lu_dev;
273 /* Allocate ost descriptor and fill it */
274 OBD_ALLOC_PTR(tgt_desc);
276 GOTO(out_conn, rc = -ENOMEM);
278 tgt_desc->ltd_tgt = d;
279 tgt_desc->ltd_exp = exp;
280 tgt_desc->ltd_uuid = obd->u.cli.cl_target_uuid;
281 tgt_desc->ltd_gen = gen;
282 tgt_desc->ltd_index = index;
283 tgt_desc->ltd_active = active;
286 if (index >= ltd->ltd_tgts_size) {
287 /* we have to increase the size of the lod_osts array */
290 newsize = max(ltd->ltd_tgts_size, (__u32)2);
291 while (newsize < index + 1)
292 newsize = newsize << 1;
294 /* lod_bitmap_resize() needs lod_rw_sem
295 * which we hold with th reference */
296 lod_putref(lod, ltd);
298 rc = ltd_bitmap_resize(ltd, newsize);
305 mutex_lock(<d->ltd_mutex);
306 if (cfs_bitmap_check(ltd->ltd_tgt_bitmap, index)) {
307 CERROR("%s: device %d is registered already\n", obd->obd_name,
309 GOTO(out_mutex, rc = -EEXIST);
312 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
313 OBD_ALLOC_PTR(ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK]);
314 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
315 CERROR("can't allocate index to add %s\n",
317 GOTO(out_mutex, rc = -ENOMEM);
321 if (!strcmp(LUSTRE_OSC_NAME, type)) {
322 /* pool and qos are not supported for MDS stack yet */
323 rc = lod_ost_pool_add(&lod->lod_pool_info, index,
326 CERROR("%s: can't set up pool, failed with %d\n",
331 rc = qos_add_tgt(lod, tgt_desc);
333 CERROR("%s: qos_add_tgt failed with %d\n",
338 /* The new OST is now a full citizen */
339 if (index >= lod->lod_desc.ld_tgt_count)
340 lod->lod_desc.ld_tgt_count = index + 1;
342 lod->lod_desc.ld_active_tgt_count++;
345 LTD_TGT(ltd, index) = tgt_desc;
346 cfs_bitmap_set(ltd->ltd_tgt_bitmap, index);
348 mutex_unlock(<d->ltd_mutex);
349 lod_putref(lod, ltd);
350 if (lod->lod_recovery_completed)
351 ldev->ld_ops->ldo_recovery_complete(env, ldev);
356 lod_ost_pool_remove(&lod->lod_pool_info, index);
358 mutex_unlock(<d->ltd_mutex);
359 lod_putref(lod, ltd);
361 OBD_FREE_PTR(tgt_desc);
369 * helper function to schedule OST removal from the device table
371 static void __lod_del_device(struct lod_tgt_descs *ltd,
374 LASSERT(LTD_TGT(ltd, idx));
375 if (LTD_TGT(ltd, idx)->ltd_reap == 0) {
376 LTD_TGT(ltd, idx)->ltd_reap = 1;
377 ltd->ltd_death_row++;
381 int lod_fini_tgt(struct lod_device *lod, struct lod_tgt_descs *ltd)
385 if (ltd->ltd_tgts_size <= 0)
388 mutex_lock(<d->ltd_mutex);
389 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx)
390 __lod_del_device(ltd, idx);
391 mutex_unlock(<d->ltd_mutex);
392 lod_putref(lod, ltd);
393 CFS_FREE_BITMAP(ltd->ltd_tgt_bitmap);
394 for (idx = 0; idx < TGT_PTRS; idx++) {
395 if (ltd->ltd_tgt_idx[idx])
396 OBD_FREE_PTR(ltd->ltd_tgt_idx[idx]);
398 ltd->ltd_tgts_size = 0;
403 * Add support for administratively disabled OST (through the MGS).
404 * Schedule a target for deletion. Disconnection and real removal from the
405 * table takes place in lod_putref() once the last table user release its
408 * \param env - is the environment passed by the caller
409 * \param lod - is the lod device currently connected to the OSP about to be
411 * \param osp - is the name of OSP device about to be removed
412 * \param idx - is the OSP index
413 * \param gen - is the generation number, not used currently
415 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
416 struct lod_tgt_descs *ltd, char *osp, unsigned idx,
419 struct obd_device *obd;
421 struct obd_uuid uuid;
424 CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, idx, gen);
426 obd_str2uuid(&uuid, osp);
428 obd = class_find_client_obd(&uuid, LUSTRE_OSP_NAME,
429 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
431 CERROR("can't find %s device\n", osp);
436 CERROR("%s: request to remove OBD %s with invalid generation %d"
437 "\n", obd->obd_name, osp, gen);
441 obd_str2uuid(&uuid, osp);
444 mutex_lock(<d->ltd_mutex);
445 /* check that the index is allocated in the bitmap */
446 if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) ||
447 !LTD_TGT(ltd, idx)) {
448 CERROR("%s: device %d is not set up\n", obd->obd_name, idx);
449 GOTO(out, rc = -EINVAL);
452 /* check that the UUID matches */
453 if (!obd_uuid_equals(&uuid, <D_TGT(ltd, idx)->ltd_uuid)) {
454 CERROR("%s: LOD target UUID %s at index %d does not match %s\n",
455 obd->obd_name, obd_uuid2str(<D_TGT(ltd,idx)->ltd_uuid),
457 GOTO(out, rc = -EINVAL);
460 __lod_del_device(ltd, idx);
463 mutex_unlock(<d->ltd_mutex);
464 lod_putref(lod, ltd);
468 int lod_ea_store_resize(struct lod_thread_info *info, int size)
470 int round = size_roundup_power2(size);
472 LASSERT(round <= lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3));
473 if (info->lti_ea_store) {
474 LASSERT(info->lti_ea_store_size);
475 LASSERT(info->lti_ea_store_size < round);
476 CDEBUG(D_INFO, "EA store size %d is not enough, need %d\n",
477 info->lti_ea_store_size, round);
478 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
479 info->lti_ea_store = NULL;
480 info->lti_ea_store_size = 0;
483 OBD_ALLOC_LARGE(info->lti_ea_store, round);
484 if (info->lti_ea_store == NULL)
486 info->lti_ea_store_size = round;
491 * generate and write LOV EA for given striped object
493 int lod_generate_and_set_lovea(const struct lu_env *env,
494 struct lod_object *lo, struct thandle *th)
496 struct lod_thread_info *info = lod_env_info(env);
497 struct dt_object *next = dt_object_child(&lo->ldo_obj);
498 const struct lu_fid *fid = lu_object_fid(&lo->ldo_obj.do_lu);
499 struct lov_mds_md_v1 *lmm;
500 struct lov_ost_data_v1 *objs;
508 magic = lo->ldo_pool != NULL ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
509 lmm_size = lov_mds_md_size(lo->ldo_stripenr, magic);
510 if (info->lti_ea_store_size < lmm_size) {
511 rc = lod_ea_store_resize(info, lmm_size);
516 if (lo->ldo_pattern == 0) /* default striping */
517 lo->ldo_pattern = LOV_PATTERN_RAID0;
519 lmm = info->lti_ea_store;
521 lmm->lmm_magic = cpu_to_le32(magic);
522 lmm->lmm_pattern = cpu_to_le32(lo->ldo_pattern);
523 fid_to_lmm_oi(fid, &lmm->lmm_oi);
524 lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
525 lmm->lmm_stripe_size = cpu_to_le32(lo->ldo_stripe_size);
526 lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_stripenr);
527 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED)
528 lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_released_stripenr);
529 lmm->lmm_layout_gen = 0;
530 if (magic == LOV_MAGIC_V1) {
531 objs = &lmm->lmm_objects[0];
533 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
534 cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
535 sizeof(v3->lmm_pool_name));
536 if (cplen >= sizeof(v3->lmm_pool_name))
538 objs = &v3->lmm_objects[0];
541 for (i = 0; i < lo->ldo_stripenr; i++) {
542 const struct lu_fid *fid;
543 struct lod_device *lod;
546 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
547 LASSERT(lo->ldo_stripe[i]);
548 fid = lu_object_fid(&lo->ldo_stripe[i]->do_lu);
550 rc = fid_to_ostid(fid, &info->lti_ostid);
553 ostid_cpu_to_le(&info->lti_ostid, &objs[i].l_ost_oi);
554 objs[i].l_ost_gen = cpu_to_le32(0);
555 rc = lod_fld_lookup(env, lod, fid, &index, LU_SEQ_RANGE_OST);
557 CERROR("%s: Can not locate "DFID": rc = %d\n",
558 lod2obd(lod)->obd_name, PFID(fid), rc);
561 objs[i].l_ost_idx = cpu_to_le32(index);
564 info->lti_buf.lb_buf = lmm;
565 info->lti_buf.lb_len = lmm_size;
566 rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0,
572 int lod_get_lov_ea(const struct lu_env *env, struct lod_object *lo)
574 struct lod_thread_info *info = lod_env_info(env);
575 struct dt_object *next = dt_object_child(&lo->ldo_obj);
581 if (unlikely(info->lti_ea_store_size == 0)) {
582 /* just to enter in allocation block below */
586 info->lti_buf.lb_buf = info->lti_ea_store;
587 info->lti_buf.lb_len = info->lti_ea_store_size;
588 rc = dt_xattr_get(env, next, &info->lti_buf, XATTR_NAME_LOV,
591 /* if object is not striped or inaccessible */
596 /* EA doesn't fit, reallocate new buffer */
597 rc = dt_xattr_get(env, next, &LU_BUF_NULL, XATTR_NAME_LOV,
605 rc = lod_ea_store_resize(info, rc);
614 int lod_store_def_striping(const struct lu_env *env, struct dt_object *dt,
617 struct lod_thread_info *info = lod_env_info(env);
618 struct lod_object *lo = lod_dt_obj(dt);
619 struct dt_object *next = dt_object_child(dt);
620 struct lov_user_md_v3 *v3;
625 LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
628 * store striping defaults into new directory
629 * used to implement defaults inheritance
632 /* probably nothing to inherite */
633 if (lo->ldo_striping_cached == 0)
636 if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size, lo->ldo_def_stripenr,
637 lo->ldo_def_stripe_offset))
640 /* XXX: use thread info */
645 v3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
646 v3->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
647 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
648 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
649 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
651 cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
652 sizeof(v3->lmm_pool_name));
653 if (cplen >= sizeof(v3->lmm_pool_name)) {
659 info->lti_buf.lb_buf = v3;
660 info->lti_buf.lb_len = sizeof(*v3);
661 rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0, th,
670 * allocate array of objects pointers, find/create objects
671 * stripenr and other fields should be initialized by this moment
673 int lod_initialize_objects(const struct lu_env *env, struct lod_object *lo,
674 struct lov_ost_data_v1 *objs)
676 struct lod_thread_info *info = lod_env_info(env);
677 struct lod_device *md = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
678 struct lu_object *o, *n;
679 struct lu_device *nd;
680 struct dt_object **stripe;
686 LASSERT(lo->ldo_stripe == NULL);
687 LASSERT(lo->ldo_stripenr > 0);
688 LASSERT(lo->ldo_stripe_size > 0);
690 stripe_len = lo->ldo_stripenr;
691 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
695 for (i = 0; i < lo->ldo_stripenr; i++) {
696 ostid_le_to_cpu(&objs[i].l_ost_oi, &info->lti_ostid);
697 idx = le64_to_cpu(objs[i].l_ost_idx);
698 rc = ostid_to_fid(&info->lti_fid, &info->lti_ostid, idx);
701 LASSERTF(fid_is_sane(&info->lti_fid), ""DFID" insane!\n",
702 PFID(&info->lti_fid));
704 * XXX: assertion is left for testing, to make
705 * sure we never process requests till configuration
706 * is completed. to be changed to -EINVAL
709 lod_getref(&md->lod_ost_descs);
710 LASSERT(cfs_bitmap_check(md->lod_ost_bitmap, idx));
711 LASSERT(OST_TGT(md,idx));
712 LASSERTF(OST_TGT(md,idx)->ltd_ost, "idx %d\n", idx);
713 nd = &OST_TGT(md,idx)->ltd_ost->dd_lu_dev;
714 lod_putref(md, &md->lod_ost_descs);
716 /* In the function below, .hs_keycmp resolves to
717 * u_obj_hop_keycmp() */
718 /* coverity[overrun-buffer-val] */
719 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
721 GOTO(out, rc = PTR_ERR(o));
723 n = lu_object_locate(o->lo_header, nd->ld_type);
726 stripe[i] = container_of(n, struct dt_object, do_lu);
731 for (i = 0; i < stripe_len; i++)
732 if (stripe[i] != NULL)
733 lu_object_put(env, &stripe[i]->do_lu);
735 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
737 lo->ldo_stripe = stripe;
738 lo->ldo_stripes_allocated = stripe_len;
745 * Parse striping information stored in lti_ea_store
747 int lod_parse_striping(const struct lu_env *env, struct lod_object *lo,
748 const struct lu_buf *buf)
750 struct lov_mds_md_v1 *lmm;
751 struct lov_ost_data_v1 *objs;
758 LASSERT(buf->lb_buf);
759 LASSERT(buf->lb_len);
761 lmm = (struct lov_mds_md_v1 *) buf->lb_buf;
762 magic = le32_to_cpu(lmm->lmm_magic);
763 pattern = le32_to_cpu(lmm->lmm_pattern);
765 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
766 GOTO(out, rc = -EINVAL);
767 if (lov_pattern(pattern) != LOV_PATTERN_RAID0)
768 GOTO(out, rc = -EINVAL);
770 lo->ldo_pattern = pattern;
771 lo->ldo_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
772 lo->ldo_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
773 lo->ldo_stripenr = le16_to_cpu(lmm->lmm_stripe_count);
774 /* released file stripenr fixup. */
775 if (pattern & LOV_PATTERN_F_RELEASED)
776 lo->ldo_stripenr = 0;
778 LASSERT(buf->lb_len >= lov_mds_md_size(lo->ldo_stripenr, magic));
780 if (magic == LOV_MAGIC_V3) {
781 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
782 objs = &v3->lmm_objects[0];
783 lod_object_set_pool(lo, v3->lmm_pool_name);
785 objs = &lmm->lmm_objects[0];
788 if (lo->ldo_stripenr > 0)
789 rc = lod_initialize_objects(env, lo, objs);
796 * Load and parse striping information, create in-core representation for the
799 int lod_load_striping(const struct lu_env *env, struct lod_object *lo)
801 struct lod_thread_info *info = lod_env_info(env);
802 struct dt_object *next = dt_object_child(&lo->ldo_obj);
807 * currently this code is supposed to be called from declaration
808 * phase only, thus the object is not expected to be locked by caller
810 dt_write_lock(env, next, 0);
811 /* already initialized? */
812 if (lo->ldo_stripe) {
815 for (i = 0; i < lo->ldo_stripenr; i++)
816 LASSERTF(lo->ldo_stripe[i], "stripe %d is NULL\n", i);
820 if (!dt_object_exists(next))
823 /* only regular files can be striped */
824 if (!(lu_object_attr(lod2lu_obj(lo)) & S_IFREG))
827 LASSERT(lo->ldo_stripenr == 0);
829 rc = lod_get_lov_ea(env, lo);
834 * there is LOV EA (striping information) in this object
835 * let's parse it and create in-core objects for the stripes
837 info->lti_buf.lb_buf = info->lti_ea_store;
838 info->lti_buf.lb_len = info->lti_ea_store_size;
839 rc = lod_parse_striping(env, lo, &info->lti_buf);
841 dt_write_unlock(env, next);
845 int lod_verify_striping(struct lod_device *d, const struct lu_buf *buf,
848 struct lov_user_md_v1 *lum;
849 struct lov_user_md_v3 *lum3;
850 struct pool_desc *pool = NULL;
861 LASSERT(sizeof(*lum) < sizeof(*lum3));
863 if (buf->lb_len < sizeof(*lum)) {
864 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md\n",
866 GOTO(out, rc = -EINVAL);
869 magic = le32_to_cpu(lum->lmm_magic);
870 if (magic != LOV_USER_MAGIC_V1 &&
871 magic != LOV_USER_MAGIC_V3 &&
872 magic != LOV_MAGIC_V1_DEF &&
873 magic != LOV_MAGIC_V3_DEF) {
874 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#x\n", magic);
875 GOTO(out, rc = -EINVAL);
878 if ((specific && le32_to_cpu(lum->lmm_pattern) != LOV_PATTERN_RAID0) ||
879 (!specific && lum->lmm_pattern != 0)) {
880 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
881 le32_to_cpu(lum->lmm_pattern));
882 GOTO(out, rc = -EINVAL);
885 /* 64kB is the largest common page size we see (ia64), and matches the
887 stripe_size = le32_to_cpu(lum->lmm_stripe_size);
888 if (stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
889 CDEBUG(D_IOCTL, "stripe size %u not a multiple of %u\n",
890 stripe_size, LOV_MIN_STRIPE_SIZE);
891 GOTO(out, rc = -EINVAL);
894 /* an offset of -1 is treated as a "special" valid offset */
895 stripe_offset = le16_to_cpu(lum->lmm_stripe_offset);
896 if (stripe_offset != (typeof(stripe_offset))-1) {
897 /* if offset is not within valid range [0, osts_size) */
898 if (stripe_offset >= d->lod_osts_size) {
899 CDEBUG(D_IOCTL, "stripe offset %u >= bitmap size %u\n",
900 stripe_offset, d->lod_osts_size);
901 GOTO(out, rc = -EINVAL);
904 /* if lmm_stripe_offset is *not* in bitmap */
905 if (!cfs_bitmap_check(d->lod_ost_bitmap, stripe_offset)) {
906 CDEBUG(D_IOCTL, "stripe offset %u not in bitmap\n",
908 GOTO(out, rc = -EINVAL);
912 stripe_count = le16_to_cpu(lum->lmm_stripe_count);
913 if (magic == LOV_USER_MAGIC_V1 || magic == LOV_MAGIC_V1_DEF)
914 lum_size = offsetof(struct lov_user_md_v1,
915 lmm_objects[stripe_count]);
916 else if (magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF)
917 lum_size = offsetof(struct lov_user_md_v3,
918 lmm_objects[stripe_count]);
922 if (specific && buf->lb_len != lum_size) {
923 CDEBUG(D_IOCTL, "invalid buf len %zd for lov_user_md with "
924 "magic %#x and stripe_count %u\n",
925 buf->lb_len, magic, stripe_count);
926 GOTO(out, rc = -EINVAL);
929 if (!(magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF))
933 if (buf->lb_len < sizeof(*lum3)) {
934 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md_v3\n",
936 GOTO(out, rc = -EINVAL);
939 /* In the function below, .hs_keycmp resolves to
940 * pool_hashkey_keycmp() */
941 /* coverity[overrun-buffer-val] */
942 pool = lod_find_pool(d, lum3->lmm_pool_name);
946 if (stripe_offset != (typeof(stripe_offset))-1) {
947 rc = lod_check_index_in_pool(stripe_offset, pool);
949 GOTO(out, rc = -EINVAL);
952 if (specific && stripe_count > pool_tgt_count(pool)) {
954 "stripe count %u > # OSTs %u in the pool\n",
955 stripe_count, pool_tgt_count(pool));
956 GOTO(out, rc = -EINVAL);
961 lod_pool_putref(pool);
966 void lod_fix_desc_stripe_size(__u64 *val)
968 if (*val < LOV_MIN_STRIPE_SIZE) {
970 LCONSOLE_INFO("Increasing default stripe size to "
971 "minimum value %u\n",
972 LOV_DEFAULT_STRIPE_SIZE);
973 *val = LOV_DEFAULT_STRIPE_SIZE;
974 } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
975 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
976 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
978 *val, LOV_MIN_STRIPE_SIZE);
982 void lod_fix_desc_stripe_count(__u32 *val)
988 void lod_fix_desc_pattern(__u32 *val)
990 /* from lov_setstripe */
991 if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
992 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
997 void lod_fix_desc_qos_maxage(__u32 *val)
1001 *val = QOS_DEFAULT_MAXAGE;
1004 void lod_fix_desc(struct lov_desc *desc)
1006 lod_fix_desc_stripe_size(&desc->ld_default_stripe_size);
1007 lod_fix_desc_stripe_count(&desc->ld_default_stripe_count);
1008 lod_fix_desc_pattern(&desc->ld_pattern);
1009 lod_fix_desc_qos_maxage(&desc->ld_qos_maxage);
1012 int lod_pools_init(struct lod_device *lod, struct lustre_cfg *lcfg)
1014 struct obd_device *obd;
1015 struct lov_desc *desc;
1019 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1020 LASSERT(obd != NULL);
1021 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1023 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1024 CERROR("LOD setup requires a descriptor\n");
1028 desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
1030 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1031 CERROR("descriptor size wrong: %d > %d\n",
1032 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1036 if (desc->ld_magic != LOV_DESC_MAGIC) {
1037 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
1038 CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
1039 obd->obd_name, desc);
1040 lustre_swab_lov_desc(desc);
1042 CERROR("%s: Bad lov desc magic: %#x\n",
1043 obd->obd_name, desc->ld_magic);
1050 desc->ld_active_tgt_count = 0;
1051 lod->lod_desc = *desc;
1053 lod->lod_sp_me = LUSTRE_SP_CLI;
1055 /* Set up allocation policy (QoS and RR) */
1056 CFS_INIT_LIST_HEAD(&lod->lod_qos.lq_oss_list);
1057 init_rwsem(&lod->lod_qos.lq_rw_sem);
1058 lod->lod_qos.lq_dirty = 1;
1059 lod->lod_qos.lq_rr.lqr_dirty = 1;
1060 lod->lod_qos.lq_reset = 1;
1061 /* Default priority is toward free space balance */
1062 lod->lod_qos.lq_prio_free = 232;
1063 /* Default threshold for rr (roughly 17%) */
1064 lod->lod_qos.lq_threshold_rr = 43;
1065 /* Init statfs fields */
1066 OBD_ALLOC_PTR(lod->lod_qos.lq_statfs_data);
1067 if (NULL == lod->lod_qos.lq_statfs_data)
1069 cfs_waitq_init(&lod->lod_qos.lq_statfs_waitq);
1071 /* Set up OST pool environment */
1072 lod->lod_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
1073 HASH_POOLS_MAX_BITS,
1074 HASH_POOLS_BKT_BITS, 0,
1077 &pool_hash_operations,
1079 if (!lod->lod_pools_hash_body)
1080 GOTO(out_statfs, rc = -ENOMEM);
1081 CFS_INIT_LIST_HEAD(&lod->lod_pool_list);
1082 lod->lod_pool_count = 0;
1083 rc = lod_ost_pool_init(&lod->lod_pool_info, 0);
1086 rc = lod_ost_pool_init(&lod->lod_qos.lq_rr.lqr_pool, 0);
1088 GOTO(out_pool_info, rc);
1093 lod_ost_pool_free(&lod->lod_pool_info);
1095 cfs_hash_putref(lod->lod_pools_hash_body);
1097 OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
1101 int lod_pools_fini(struct lod_device *lod)
1103 struct obd_device *obd = lod2obd(lod);
1104 cfs_list_t *pos, *tmp;
1105 struct pool_desc *pool;
1108 cfs_list_for_each_safe(pos, tmp, &lod->lod_pool_list) {
1109 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
1110 /* free pool structs */
1111 CDEBUG(D_INFO, "delete pool %p\n", pool);
1112 /* In the function below, .hs_keycmp resolves to
1113 * pool_hashkey_keycmp() */
1114 /* coverity[overrun-buffer-val] */
1115 lod_pool_del(obd, pool->pool_name);
1118 cfs_hash_putref(lod->lod_pools_hash_body);
1119 lod_ost_pool_free(&(lod->lod_qos.lq_rr.lqr_pool));
1120 lod_ost_pool_free(&lod->lod_pool_info);
1121 OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);