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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/lod/lod_qos.c
34 * Implementation of different allocation algorithm used
35 * to distribute objects and data among OSTs.
38 #define DEBUG_SUBSYSTEM S_LOV
40 #include <asm/div64.h>
41 #include <linux/random.h>
43 #include <libcfs/libcfs.h>
44 #include <uapi/linux/lustre/lustre_idl.h>
45 #include <lustre_swab.h>
46 #include <obd_class.h>
48 #include "lod_internal.h"
51 * force QoS policy (not RR) to be used for testing purposes
57 #define QOS_DEBUG(fmt, ...) CDEBUG(D_QOS, fmt, ## __VA_ARGS__)
58 #define QOS_CONSOLE(fmt, ...) LCONSOLE(D_QOS, fmt, ## __VA_ARGS__)
60 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
61 OST_TGT(lod,i)->ltd_statfs.os_bsize)
63 static inline int lod_statfs_check(struct lu_tgt_descs *ltd,
64 struct lu_tgt_desc *tgt)
66 struct obd_statfs *sfs = &tgt->ltd_statfs;
68 if (((sfs->os_state & OS_STATFS_ENOSPC) ||
69 (!ltd->ltd_is_mdt && sfs->os_state & OS_STATFS_ENOINO &&
70 sfs->os_fprecreated == 0)))
73 /* If the OST is readonly then we can't allocate objects there */
74 if (sfs->os_state & OS_STATFS_READONLY)
77 /* object precreation is skipped on the OST with max_create_count=0 */
78 if (!ltd->ltd_is_mdt && sfs->os_state & OS_STATFS_NOPRECREATE)
85 * Check whether the target is available for new objects.
87 * Request statfs data from the given target and verify it's active and not
88 * read-only. If so, then it can be used to place new objects. This
89 * function also maintains the number of active/inactive targets and sets
90 * dirty flags if those numbers change so others can run re-balance procedures.
91 * No external locking is required.
93 * \param[in] env execution environment for this thread
94 * \param[in] d LOD device
95 * \param[in] ltd target table
96 * \param[in] tgt target
98 * \retval 0 if the target is good
99 * \retval negative negated errno on error
101 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
102 struct lu_tgt_descs *ltd,
103 struct lu_tgt_desc *tgt, __u64 reserve)
105 struct obd_statfs_info info = { 0 };
106 struct lov_desc *desc = <d->ltd_lov_desc;
113 info.os_enable_pre = 1;
114 rc = dt_statfs_info(env, tgt->ltd_tgt, &tgt->ltd_statfs, &info);
115 if (rc && rc != -ENOTCONN)
116 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
119 rc = lod_statfs_check(ltd, tgt);
125 (reserve + (info.os_reserved_mb_low << 20) >
126 tgt->ltd_statfs.os_bavail * tgt->ltd_statfs.os_bsize))
129 /* check whether device has changed state (active, inactive) */
130 if (rc != 0 && tgt->ltd_active) {
131 /* turned inactive? */
132 spin_lock(&d->lod_lock);
133 if (tgt->ltd_active) {
136 tgt->ltd_connecting = 1;
138 LASSERT(desc->ld_active_tgt_count > 0);
139 desc->ld_active_tgt_count--;
140 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
141 set_bit(LQ_DIRTY, <d->ltd_qos.lq_rr.lqr_flags);
142 CDEBUG(D_CONFIG, "%s: turns inactive\n",
143 tgt->ltd_exp->exp_obd->obd_name);
145 spin_unlock(&d->lod_lock);
146 } else if (rc == 0 && tgt->ltd_active == 0) {
148 LASSERTF(desc->ld_active_tgt_count < desc->ld_tgt_count,
149 "active tgt count %d, tgt nr %d\n",
150 desc->ld_active_tgt_count, desc->ld_tgt_count);
151 spin_lock(&d->lod_lock);
152 if (tgt->ltd_active == 0) {
154 tgt->ltd_connecting = 0;
155 desc->ld_active_tgt_count++;
156 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
157 set_bit(LQ_DIRTY, <d->ltd_qos.lq_rr.lqr_flags);
158 CDEBUG(D_CONFIG, "%s: turns active\n",
159 tgt->ltd_exp->exp_obd->obd_name);
161 spin_unlock(&d->lod_lock);
163 if (rc == -ENOTCONN) {
164 /* In case that the ENOTCONN for inactive OST state is
165 * mistreated as MDT disconnection state by the client,
166 * this error should be changed to someone else.
174 static int lod_is_tgt_usable(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
178 rc = lod_statfs_check(ltd, tgt);
182 if (!tgt->ltd_active)
189 * Maintain per-target statfs data.
191 * The function refreshes statfs data for all the targets every N seconds.
192 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
195 * \param[in] env execution environment for this thread
196 * \param[in] lod LOD device
197 * \param[in] ltd tgt table
199 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod,
200 struct lu_tgt_descs *ltd)
202 struct obd_device *obd = lod2obd(lod);
203 struct lu_tgt_desc *tgt;
208 max_age = ktime_get_seconds() - 2 * ltd->ltd_lov_desc.ld_qos_maxage;
210 if (obd->obd_osfs_age > max_age)
211 /* statfs data are quite recent, don't need to refresh it */
214 down_write(<d->ltd_qos.lq_rw_sem);
216 if (obd->obd_osfs_age > max_age)
219 ltd_foreach_tgt(ltd, tgt) {
220 avail = tgt->ltd_statfs.os_bavail;
221 if (lod_statfs_and_check(env, lod, ltd, tgt, 0))
224 if (tgt->ltd_statfs.os_bavail != avail)
225 /* recalculate weigths */
226 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
228 obd->obd_osfs_age = ktime_get_seconds();
231 up_write(<d->ltd_qos.lq_rw_sem);
235 #define LOV_QOS_EMPTY ((__u32)-1)
238 * Calculate optimal round-robin order with regard to OSSes.
240 * Place all the OSTs from pool \a src_pool in a special array to be used for
241 * round-robin (RR) stripe allocation. The placement algorithm interleaves
242 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
243 * Resorts the targets when the number of active targets changes (because of
244 * a new target or activation/deactivation).
246 * \param[in] lod LOD device
247 * \param[in] ltd tgt table
248 * \param[in] src_pool tgt pool
249 * \param[in] lqr round-robin list
251 * \retval 0 on success
252 * \retval -ENOMEM fails to allocate the array
254 static int lod_qos_calc_rr(struct lod_device *lod, struct lu_tgt_descs *ltd,
255 const struct lu_tgt_pool *src_pool,
256 struct lu_qos_rr *lqr)
258 struct lu_svr_qos *svr;
259 struct lu_tgt_desc *tgt;
260 unsigned placed, real_count;
265 if (!test_bit(LQ_DIRTY, &lqr->lqr_flags)) {
266 LASSERT(lqr->lqr_pool.op_size);
270 /* Do actual allocation. */
271 down_write(<d->ltd_qos.lq_rw_sem);
274 * Check again. While we were sleeping on @lq_rw_sem something could
277 if (!test_bit(LQ_DIRTY, &lqr->lqr_flags)) {
278 LASSERT(lqr->lqr_pool.op_size);
279 up_write(<d->ltd_qos.lq_rw_sem);
283 real_count = src_pool->op_count;
285 /* Zero the pool array */
286 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
287 deleting from the pool. The lq_rw_sem insures that nobody else
289 lqr->lqr_pool.op_count = real_count;
290 rc = tgt_pool_extend(&lqr->lqr_pool, real_count);
292 up_write(<d->ltd_qos.lq_rw_sem);
295 for (i = 0; i < lqr->lqr_pool.op_count; i++)
296 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
298 /* Place all the tgts from 1 svr at the same time. */
300 list_for_each_entry(svr, <d->ltd_qos.lq_svr_list, lsq_svr_list) {
303 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
306 if (!test_bit(src_pool->op_array[i],
307 ltd->ltd_tgt_bitmap))
310 tgt = LTD_TGT(ltd, src_pool->op_array[i]);
311 LASSERT(tgt && tgt->ltd_tgt);
312 if (tgt->ltd_qos.ltq_svr != svr)
315 /* Evenly space these tgts across arrayspace */
316 next = j * lqr->lqr_pool.op_count / svr->lsq_tgt_count;
317 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
318 next = (next + 1) % lqr->lqr_pool.op_count;
320 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
326 clear_bit(LQ_DIRTY, &lqr->lqr_flags);
327 up_write(<d->ltd_qos.lq_rw_sem);
329 if (placed != real_count) {
330 /* This should never happen */
331 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all tgts in the "
332 "round-robin list (%d of %d).\n",
334 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
335 LCONSOLE(D_WARNING, "rr #%d tgt idx=%d\n", i,
336 lqr->lqr_pool.op_array[i]);
338 set_bit(LQ_DIRTY, &lqr->lqr_flags);
343 for (i = 0; i < lqr->lqr_pool.op_count; i++)
344 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
351 * Instantiate and declare creation of a new object.
353 * The function instantiates LU representation for a new object on the
354 * specified device. Also it declares an intention to create that
355 * object on the storage target.
357 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
358 * infrastructure - in the existing precreation framework we can't assign FID
359 * at this moment, we do this later once a transaction is started. So the
360 * special method instantiates FID-less object in the cache and later it
361 * will get a FID and proper placement in LU cache.
363 * \param[in] env execution environment for this thread
364 * \param[in] d LOD device
365 * \param[in] ost_idx OST target index where the object is being created
366 * \param[in] th transaction handle
368 * \retval object ptr on success, ERR_PTR() otherwise
370 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
371 struct lod_device *d,
376 struct dt_allocation_hint *ah = &lod_env_info(env)->lti_ah;
377 struct lod_tgt_desc *ost;
378 struct lu_object *o, *n;
379 struct lu_device *nd;
380 struct dt_object *dt;
385 LASSERT(ost_idx < d->lod_ost_descs.ltd_tgts_size);
386 ost = OST_TGT(d,ost_idx);
388 LASSERT(ost->ltd_tgt);
390 nd = &ost->ltd_tgt->dd_lu_dev;
393 * allocate anonymous object with zero fid, real fid
394 * will be assigned by OSP within transaction
395 * XXX: to be fixed with fully-functional OST fids
397 o = lu_object_anon(env, nd, NULL);
399 GOTO(out, dt = ERR_CAST(o));
401 n = lu_object_locate(o->lo_header, nd->ld_type);
402 if (unlikely(n == NULL)) {
403 CERROR("can't find slice\n");
404 lu_object_put(env, o);
405 GOTO(out, dt = ERR_PTR(-EINVAL));
408 dt = container_of(n, struct dt_object, do_lu);
410 ah->dah_can_block = can_block;
411 rc = lod_sub_declare_create(env, dt, NULL, ah, NULL, th);
413 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
415 lu_object_put(env, o);
424 * Calculate a minimum acceptable stripe count.
426 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
427 * all stripes or 3/4 of stripes.
429 * \param[in] stripe_count number of stripes requested
430 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
432 * \retval acceptable stripecount
434 static int min_stripe_count(__u32 stripe_count, int flags)
436 return (flags & LOV_USES_DEFAULT_STRIPE ?
437 stripe_count - (stripe_count / 4) : stripe_count);
440 #define LOV_CREATE_RESEED_MULT 30
441 #define LOV_CREATE_RESEED_MIN 2000
444 * Initialize temporary tgt-in-use array.
446 * Allocate or extend the array used to mark targets already assigned to a new
447 * striping so they are not used more than once.
449 * \param[in] env execution environment for this thread
450 * \param[in] stripes number of items needed in the array
452 * \retval 0 on success
453 * \retval -ENOMEM on error
455 static inline int lod_qos_tgt_in_use_clear(const struct lu_env *env,
458 struct lod_thread_info *info = lod_env_info(env);
460 if (info->lti_ea_store_size < sizeof(int) * stripes)
461 lod_ea_store_resize(info, stripes * sizeof(int));
462 if (info->lti_ea_store_size < sizeof(int) * stripes) {
463 CERROR("can't allocate memory for tgt-in-use array\n");
466 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
471 * Remember a target in the array of used targets.
473 * Mark the given target as used for a new striping being created. The status
474 * of an tgt in a striping can be checked with lod_qos_is_tgt_used().
476 * \param[in] env execution environment for this thread
477 * \param[in] idx index in the array
478 * \param[in] tgt_idx target index to mark as used
480 static inline void lod_qos_tgt_in_use(const struct lu_env *env,
481 int idx, int tgt_idx)
483 struct lod_thread_info *info = lod_env_info(env);
484 int *tgts = info->lti_ea_store;
486 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
491 * Check is tgt used in a striping.
493 * Checks whether tgt with the given index is marked as used in the temporary
494 * array (see lod_qos_tgt_in_use()).
496 * \param[in] env execution environment for this thread
497 * \param[in] tgt_idx target index to check
498 * \param[in] stripes the number of items used in the array already
503 static int lod_qos_is_tgt_used(const struct lu_env *env, int tgt_idx,
506 struct lod_thread_info *info = lod_env_info(env);
507 int *tgts = info->lti_ea_store;
510 for (j = 0; j < stripes; j++) {
511 if (tgts[j] == tgt_idx)
518 lod_obj_is_ost_use_skip_cb(const struct lu_env *env, struct lod_object *lo,
519 int comp_idx, struct lod_obj_stripe_cb_data *data)
521 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
523 return comp->llc_ost_indices == NULL;
527 lod_obj_is_ost_use_cb(const struct lu_env *env, struct lod_object *lo,
528 int comp_idx, struct lod_obj_stripe_cb_data *data)
530 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
533 for (i = 0; i < comp->llc_stripe_count; i++) {
534 if (comp->llc_ost_indices[i] == data->locd_ost_index) {
535 data->locd_ost_index = -1;
544 * Check is OST used in a composite layout
546 * \param[in] lo lod object
547 * \param[in] ost OST target index to check
549 * \retval false not used
552 static inline bool lod_comp_is_ost_used(const struct lu_env *env,
553 struct lod_object *lo, int ost)
555 struct lod_obj_stripe_cb_data data = { { 0 } };
557 data.locd_ost_index = ost;
558 data.locd_comp_skip_cb = lod_obj_is_ost_use_skip_cb;
559 data.locd_comp_cb = lod_obj_is_ost_use_cb;
561 (void)lod_obj_for_each_stripe(env, lo, NULL, &data);
563 return data.locd_ost_index == -1;
566 static inline void lod_avoid_update(struct lod_object *lo,
567 struct lod_avoid_guide *lag)
572 lag->lag_ost_avail--;
575 static inline bool lod_should_avoid_ost(struct lod_object *lo,
576 struct lod_avoid_guide *lag,
579 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
580 struct lod_tgt_desc *ost = OST_TGT(lod, index);
581 struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
585 if (!test_bit(index, lod->lod_ost_bitmap)) {
586 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
592 * we've tried our best, all available OSTs have been used in
593 * overlapped components in the other mirror
595 if (lag->lag_ost_avail == 0)
599 for (i = 0; i < lag->lag_oaa_count; i++) {
600 if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
606 * if the OSS which OST[index] resides has not been used, we'd like to
612 /* if the OSS has been used, check whether the OST has been used */
613 if (!test_bit(index, lag->lag_ost_avoid_bitmap))
616 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
621 static int lod_check_and_reserve_ost(const struct lu_env *env,
622 struct lod_object *lo,
623 struct lod_layout_component *lod_comp,
624 __u32 ost_idx, __u32 speed, __u32 *s_idx,
625 struct dt_object **stripe,
631 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
632 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
633 struct lu_tgt_desc *ost = OST_TGT(lod, ost_idx);
635 __u32 stripe_idx = *s_idx;
640 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs, ost, reserve);
645 * We expect number of precreated objects in f_ffree at
646 * the first iteration, skip OSPs with no objects ready
648 if (ost->ltd_statfs.os_fprecreated == 0 && speed == 0) {
649 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
654 * try to use another OSP if this one is degraded
656 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED && speed < 2) {
657 QOS_DEBUG("#%d: degraded\n", ost_idx);
662 * try not allocate on OST which has been used by other
665 if (speed == 0 && lod_comp_is_ost_used(env, lo, ost_idx)) {
666 QOS_DEBUG("iter %d: OST%d used by other component\n",
672 * try not allocate OSTs used by conflicting component of other mirrors
673 * for the first and second time.
675 if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
676 QOS_DEBUG("iter %d: OST%d used by conflicting mirror component\n",
681 /* do not put >1 objects on a single OST, except for overstriping */
682 if (lod_qos_is_tgt_used(env, ost_idx, stripe_idx)) {
683 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
689 o = lod_qos_declare_object_on(env, lod, ost_idx, true, th);
691 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
692 ost_idx, (int) PTR_ERR(o));
698 * We've successfully declared (reserved) an object
700 lod_avoid_update(lo, lag);
701 lod_qos_tgt_in_use(env, stripe_idx, ost_idx);
702 stripe[stripe_idx] = o;
703 ost_indices[stripe_idx] = ost_idx;
704 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
712 * Allocate a striping using round-robin algorithm.
714 * Allocates a new striping using round-robin algorithm. The function refreshes
715 * all the internal structures (statfs cache, array of available OSTs sorted
716 * with regard to OSS, etc). The number of stripes required is taken from the
717 * object (must be prepared by the caller), but can change if the flag
718 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
719 * is trying to create a striping on the object in parallel. All the internal
720 * structures (like pools, etc) are protected and no additional locking is
721 * required. The function succeeds even if a single stripe is allocated. To save
722 * time we give priority to targets which already have objects precreated.
723 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
725 * \param[in] env execution environment for this thread
726 * \param[in] lo LOD object
727 * \param[out] stripe striping created
728 * \param[out] ost_indices ost indices of striping created
729 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
730 * \param[in] th transaction handle
731 * \param[in] comp_idx index of ldo_comp_entries
733 * \retval 0 on success
734 * \retval -ENOSPC if not enough OSTs are found
735 * \retval negative negated errno for other failures
737 static int lod_ost_alloc_rr(const struct lu_env *env, struct lod_object *lo,
738 struct dt_object **stripe, __u32 *ost_indices,
739 int flags, struct thandle *th, int comp_idx,
742 struct lod_layout_component *lod_comp;
743 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
744 struct pool_desc *pool = NULL;
745 struct lu_tgt_pool *osts;
746 struct lu_qos_rr *lqr;
747 unsigned int i, array_idx;
748 __u32 ost_start_idx_temp;
749 __u32 stripe_idx = 0;
750 __u32 stripe_count, stripe_count_min, ost_idx;
751 int rc, speed = 0, ost_connecting = 0;
752 int stripes_per_ost = 1;
753 bool overstriped = false;
756 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
757 lod_comp = &lo->ldo_comp_entries[comp_idx];
758 stripe_count = lod_comp->llc_stripe_count;
759 stripe_count_min = min_stripe_count(stripe_count, flags);
761 if (lod_comp->llc_pool != NULL)
762 pool = lod_find_pool(m, lod_comp->llc_pool);
765 down_read(&pool_tgt_rw_sem(pool));
766 osts = &(pool->pool_obds);
767 lqr = &(pool->pool_rr);
769 osts = &m->lod_ost_descs.ltd_tgt_pool;
770 lqr = &(m->lod_ost_descs.ltd_qos.lq_rr);
773 rc = lod_qos_calc_rr(m, &m->lod_ost_descs, osts, lqr);
777 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
781 down_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
782 spin_lock(&lqr->lqr_alloc);
783 if (--lqr->lqr_start_count <= 0) {
784 lqr->lqr_start_idx = prandom_u32_max(osts->op_count);
785 lqr->lqr_start_count =
786 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
787 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
788 } else if (stripe_count_min >= osts->op_count ||
789 lqr->lqr_start_idx > osts->op_count) {
790 /* If we have allocated from all of the OSTs, slowly
791 * precess the next start if the OST/stripe count isn't
792 * already doing this for us. */
793 lqr->lqr_start_idx %= osts->op_count;
794 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
795 ++lqr->lqr_offset_idx;
797 ost_start_idx_temp = lqr->lqr_start_idx;
801 QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
802 "active %d count %d\n",
803 lod_comp->llc_pool ? lod_comp->llc_pool : "",
804 stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
805 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
807 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
809 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
811 for (i = 0; i < osts->op_count * stripes_per_ost
812 && stripe_idx < stripe_count; i++) {
813 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
815 ++lqr->lqr_start_idx;
816 ost_idx = lqr->lqr_pool.op_array[array_idx];
818 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
819 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
820 stripe_idx, array_idx, ost_idx);
822 if ((ost_idx == LOV_QOS_EMPTY) ||
823 !test_bit(ost_idx, m->lod_ost_bitmap))
826 /* Fail Check before osc_precreate() is called
827 so we can only 'fail' single OSC. */
828 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
831 spin_unlock(&lqr->lqr_alloc);
832 rc = lod_check_and_reserve_ost(env, lo, lod_comp, ost_idx,
833 speed, &stripe_idx, stripe,
834 ost_indices, th, &overstriped,
836 spin_lock(&lqr->lqr_alloc);
838 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
841 if ((speed < 2) && (stripe_idx < stripe_count_min)) {
842 /* Try again, allowing slower OSCs */
844 lqr->lqr_start_idx = ost_start_idx_temp;
850 spin_unlock(&lqr->lqr_alloc);
851 up_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
853 /* If there are enough OSTs, a component with overstriping requested
854 * will not actually end up overstriped. The comp should reflect this.
857 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
860 lod_comp->llc_stripe_count = stripe_idx;
861 /* at least one stripe is allocated */
864 /* nobody provided us with a single object */
873 up_read(&pool_tgt_rw_sem(pool));
874 /* put back ref got by lod_find_pool() */
875 lod_pool_putref(pool);
882 lod_qos_mdt_in_use_init(const struct lu_env *env,
883 const struct lu_tgt_descs *ltd,
884 u32 stripe_idx, u32 stripe_count,
885 const struct lu_tgt_pool *pool,
886 struct dt_object **stripes)
889 struct lu_tgt_desc *mdt;
893 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
897 /* if stripe_idx > 1, we are splitting directory, mark existing stripes
898 * in_use. Because for either split or creation, stripe 0 is local,
899 * don't mark it in use.
901 for (i = 1; i < stripe_idx; i++) {
903 for (j = 0; j < pool->op_count; j++) {
904 mdt_idx = pool->op_array[j];
906 if (!test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
909 mdt = LTD_TGT(ltd, mdt_idx);
910 if (&mdt->ltd_tgt->dd_lu_dev ==
911 stripes[i]->do_lu.lo_dev)
912 lod_qos_tgt_in_use(env, i, mdt_idx);
920 * Allocate a striping using round-robin algorithm.
922 * Allocates a new striping using round-robin algorithm. The function refreshes
923 * all the internal structures (statfs cache, array of available remote MDTs
924 * sorted with regard to MDS, etc). The number of stripes required is taken from
925 * the object (must be prepared by the caller). The caller should ensure nobody
926 * else is trying to create a striping on the object in parallel. All the
927 * internal structures (like pools, etc) are protected and no additional locking
928 * is required. The function succeeds even if a single stripe is allocated.
930 * \param[in] env execution environment for this thread
931 * \param[in] lo LOD object
932 * \param[out] stripes striping created
934 * \retval positive stripe objects allocated, including the first stripe
936 * \retval -ENOSPC if not enough MDTs are found
937 * \retval negative negated errno for other failures
939 int lod_mdt_alloc_rr(const struct lu_env *env, struct lod_object *lo,
940 struct dt_object **stripes, u32 stripe_idx,
943 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
944 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
945 struct lu_tgt_pool *pool;
946 struct lu_qos_rr *lqr;
947 struct lu_tgt_desc *mdt;
948 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
949 struct lu_fid fid = { 0 };
950 struct dt_object *dto;
951 unsigned int pool_idx;
953 u32 saved_idx = stripe_idx;
956 bool use_degraded = false;
957 int tgt_connecting = 0;
962 pool = <d->ltd_tgt_pool;
963 lqr = <d->ltd_qos.lq_rr;
964 rc = lod_qos_calc_rr(lod, ltd, pool, lqr);
968 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
973 down_read(<d->ltd_qos.lq_rw_sem);
974 spin_lock(&lqr->lqr_alloc);
975 if (--lqr->lqr_start_count <= 0) {
976 lqr->lqr_start_idx = prandom_u32_max(pool->op_count);
977 lqr->lqr_start_count =
978 (LOV_CREATE_RESEED_MIN / max(pool->op_count, 1U) +
979 LOV_CREATE_RESEED_MULT) * max(pool->op_count, 1U);
980 } else if (stripe_count - 1 >= pool->op_count ||
981 lqr->lqr_start_idx > pool->op_count) {
982 /* If we have allocated from all of the tgts, slowly
983 * precess the next start if the tgt/stripe count isn't
984 * already doing this for us. */
985 lqr->lqr_start_idx %= pool->op_count;
986 if (stripe_count - 1 > 1 &&
987 (pool->op_count % (stripe_count - 1)) != 1)
988 ++lqr->lqr_offset_idx;
990 start_mdt = lqr->lqr_start_idx;
993 QOS_DEBUG("want=%d start_idx=%d start_count=%d offset=%d active=%d count=%d\n",
994 stripe_count - 1, lqr->lqr_start_idx, lqr->lqr_start_count,
995 lqr->lqr_offset_idx, pool->op_count, pool->op_count);
997 for (i = 0; i < pool->op_count && stripe_idx < stripe_count; i++) {
998 pool_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
1000 ++lqr->lqr_start_idx;
1001 mdt_idx = lqr->lqr_pool.op_array[pool_idx];
1002 mdt = LTD_TGT(ltd, mdt_idx);
1004 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1005 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1006 stripe_idx, pool_idx, mdt_idx);
1008 if (mdt_idx == LOV_QOS_EMPTY ||
1009 !test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
1012 /* do not put >1 objects on one MDT */
1013 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1016 rc = lod_is_tgt_usable(ltd, mdt);
1018 if (mdt->ltd_connecting)
1023 /* try to use another OSP if this one is degraded */
1024 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED &&
1026 QOS_DEBUG("#%d: degraded\n", mdt_idx);
1029 spin_unlock(&lqr->lqr_alloc);
1031 rc = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1033 QOS_DEBUG("#%d: alloc FID failed: %dl\n", mdt_idx, rc);
1034 spin_lock(&lqr->lqr_alloc);
1038 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1039 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1042 spin_lock(&lqr->lqr_alloc);
1044 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1045 mdt->ltd_index, (int) PTR_ERR(dto));
1047 if (mdt->ltd_connecting)
1052 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1053 stripes[stripe_idx++] = dto;
1056 if (!use_degraded && stripe_idx < stripe_count) {
1057 /* Try again, allowing slower MDTs */
1058 use_degraded = true;
1059 lqr->lqr_start_idx = start_mdt;
1064 spin_unlock(&lqr->lqr_alloc);
1065 up_read(<d->ltd_qos.lq_rw_sem);
1067 if (stripe_idx > saved_idx)
1068 /* at least one stripe is allocated */
1071 /* nobody provided us with a single object */
1073 RETURN(-EINPROGRESS);
1079 * Allocate a specific striping layout on a user defined set of OSTs.
1081 * Allocates new striping using the OST index range provided by the data from
1082 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1083 * OSTs are not considered. The exact order of OSTs requested by the user
1084 * is respected as much as possible depending on OST status. The number of
1085 * stripes needed and stripe offset are taken from the object. If that number
1086 * can not be met, then the function returns a failure and then it's the
1087 * caller's responsibility to release the stripes allocated. All the internal
1088 * structures are protected, but no concurrent allocation is allowed on the
1091 * \param[in] env execution environment for this thread
1092 * \param[in] lo LOD object
1093 * \param[out] stripe striping created
1094 * \param[out] ost_indices ost indices of striping created
1095 * \param[in] th transaction handle
1096 * \param[in] comp_idx index of ldo_comp_entries
1098 * \retval 0 on success
1099 * \retval -ENODEV OST index does not exist on file system
1100 * \retval -EINVAL requested OST index is invalid
1101 * \retval negative negated errno on error
1103 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1104 struct dt_object **stripe, __u32 *ost_indices,
1105 struct thandle *th, int comp_idx, __u64 reserve)
1107 struct lod_layout_component *lod_comp;
1108 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1109 struct dt_object *o;
1110 unsigned int array_idx = 0;
1111 int stripe_count = 0;
1116 /* for specific OSTs layout */
1117 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1118 lod_comp = &lo->ldo_comp_entries[comp_idx];
1119 LASSERT(lod_comp->llc_ostlist.op_array);
1120 LASSERT(lod_comp->llc_ostlist.op_count);
1122 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1126 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
1127 lod_comp->llc_stripe_offset =
1128 lod_comp->llc_ostlist.op_array[0];
1130 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1131 if (lod_comp->llc_ostlist.op_array[i] ==
1132 lod_comp->llc_stripe_offset) {
1137 if (i == lod_comp->llc_stripe_count) {
1139 "%s: start index %d not in the specified list of OSTs\n",
1140 lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1144 for (i = 0; i < lod_comp->llc_stripe_count;
1145 i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1146 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1148 if (!test_bit(ost_idx, m->lod_ost_bitmap)) {
1153 /* do not put >1 objects on a single OST, except for
1156 if (lod_qos_is_tgt_used(env, ost_idx, stripe_count) &&
1157 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
1162 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1163 LTD_TGT(&m->lod_ost_descs, ost_idx),
1165 if (rc < 0) /* this OSP doesn't feel well */
1168 o = lod_qos_declare_object_on(env, m, ost_idx, true, th);
1172 "%s: can't declare new object on #%u: %d\n",
1173 lod2obd(m)->obd_name, ost_idx, rc);
1178 * We've successfully declared (reserved) an object
1180 lod_qos_tgt_in_use(env, stripe_count, ost_idx);
1181 stripe[stripe_count] = o;
1182 ost_indices[stripe_count] = ost_idx;
1190 * Allocate a striping on a predefined set of OSTs.
1192 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1193 * Full OSTs are not considered. The exact order of OSTs is not important and
1194 * varies depending on OST status. The allocation procedure prefers the targets
1195 * with precreated objects ready. The number of stripes needed and stripe
1196 * offset are taken from the object. If that number cannot be met, then the
1197 * function returns an error and then it's the caller's responsibility to
1198 * release the stripes allocated. All the internal structures are protected,
1199 * but no concurrent allocation is allowed on the same objects.
1201 * \param[in] env execution environment for this thread
1202 * \param[in] lo LOD object
1203 * \param[out] stripe striping created
1204 * \param[out] ost_indices ost indices of striping created
1205 * \param[in] flags not used
1206 * \param[in] th transaction handle
1207 * \param[in] comp_idx index of ldo_comp_entries
1209 * \retval 0 on success
1210 * \retval -ENOSPC if no OST objects are available at all
1211 * \retval -EFBIG if not enough OST objects are found
1212 * \retval -EINVAL requested offset is invalid
1213 * \retval negative errno on failure
1215 static int lod_ost_alloc_specific(const struct lu_env *env,
1216 struct lod_object *lo,
1217 struct dt_object **stripe, __u32 *ost_indices,
1218 int flags, struct thandle *th, int comp_idx,
1221 struct lod_layout_component *lod_comp;
1222 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1223 struct dt_object *o;
1224 struct lu_tgt_desc *tgt;
1226 unsigned int i, array_idx, ost_count;
1227 int rc, stripe_num = 0;
1229 struct pool_desc *pool = NULL;
1230 struct lu_tgt_pool *osts;
1231 int stripes_per_ost = 1;
1232 bool overstriped = false;
1235 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1236 lod_comp = &lo->ldo_comp_entries[comp_idx];
1238 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1242 if (lod_comp->llc_pool != NULL)
1243 pool = lod_find_pool(m, lod_comp->llc_pool);
1246 down_read(&pool_tgt_rw_sem(pool));
1247 osts = &(pool->pool_obds);
1249 osts = &m->lod_ost_descs.ltd_tgt_pool;
1252 ost_count = osts->op_count;
1255 /* search loi_ost_idx in ost array */
1257 for (i = 0; i < ost_count; i++) {
1258 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1263 if (i == ost_count) {
1264 CERROR("Start index %d not found in pool '%s'\n",
1265 lod_comp->llc_stripe_offset,
1266 lod_comp->llc_pool ? lod_comp->llc_pool : "");
1267 GOTO(out, rc = -EINVAL);
1270 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1272 (lod_comp->llc_stripe_count - 1)/ost_count + 1;
1274 for (i = 0; i < ost_count * stripes_per_ost;
1275 i++, array_idx = (array_idx + 1) % ost_count) {
1276 ost_idx = osts->op_array[array_idx];
1278 if (!test_bit(ost_idx, m->lod_ost_bitmap))
1281 /* Fail Check before osc_precreate() is called
1282 so we can only 'fail' single OSC. */
1283 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1287 * do not put >1 objects on a single OST, except for
1288 * overstriping, where it is intended
1290 if (lod_qos_is_tgt_used(env, ost_idx, stripe_num)) {
1291 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1298 * try not allocate on the OST used by other component
1300 if (speed == 0 && i != 0 &&
1301 lod_comp_is_ost_used(env, lo, ost_idx))
1304 tgt = LTD_TGT(&m->lod_ost_descs, ost_idx);
1306 /* Drop slow OSCs if we can, but not for requested start idx.
1308 * This means "if OSC is slow and it is not the requested
1309 * start OST, then it can be skipped, otherwise skip it only
1310 * if it is inactive/recovering/out-of-space." */
1312 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1315 /* this OSP doesn't feel well */
1320 * We expect number of precreated objects at the first
1321 * iteration. Skip OSPs with no objects ready. Don't apply
1322 * this logic to OST specified with stripe_offset.
1324 if (i && !tgt->ltd_statfs.os_fprecreated && !speed)
1327 o = lod_qos_declare_object_on(env, m, ost_idx, true, th);
1329 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1330 ost_idx, (int) PTR_ERR(o));
1335 * We've successfully declared (reserved) an object
1337 lod_qos_tgt_in_use(env, stripe_num, ost_idx);
1338 stripe[stripe_num] = o;
1339 ost_indices[stripe_num] = ost_idx;
1342 /* We have enough stripes */
1343 if (stripe_num == lod_comp->llc_stripe_count)
1347 /* Try again, allowing slower OSCs */
1352 /* If we were passed specific striping params, then a failure to
1353 * meet those requirements is an error, since we can't reallocate
1354 * that memory (it might be part of a larger array or something).
1356 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1357 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1358 lod_comp->llc_stripe_count);
1359 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1361 /* If there are enough OSTs, a component with overstriping requessted
1362 * will not actually end up overstriped. The comp should reflect this.
1364 if (rc == 0 && !overstriped)
1365 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1369 up_read(&pool_tgt_rw_sem(pool));
1370 /* put back ref got by lod_find_pool() */
1371 lod_pool_putref(pool);
1378 * Allocate a striping using an algorithm with weights.
1380 * The function allocates OST objects to create a striping. The algorithm
1381 * used is based on weights (currently only using the free space), and it's
1382 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1383 * configuration (# of stripes, offset, pool) is taken from the object and
1384 * is prepared by the caller.
1386 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1387 * be met due to too few OSTs, then allocation fails. If the flag is passed
1388 * fewer than 3/4 of the requested number of stripes can be allocated, then
1391 * No concurrent allocation is allowed on the object and this must be ensured
1392 * by the caller. All the internal structures are protected by the function.
1394 * The algorithm has two steps: find available OSTs and calculate their
1395 * weights, then select the OSTs with their weights used as the probability.
1396 * An OST with a higher weight is proportionately more likely to be selected
1397 * than one with a lower weight.
1399 * \param[in] env execution environment for this thread
1400 * \param[in] lo LOD object
1401 * \param[out] stripe striping created
1402 * \param[out] ost_indices ost indices of striping created
1403 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1404 * \param[in] th transaction handle
1405 * \param[in] comp_idx index of ldo_comp_entries
1407 * \retval 0 on success
1408 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1409 * \retval -EINVAL requested OST index is invalid
1410 * \retval negative errno on failure
1412 static int lod_ost_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1413 struct dt_object **stripe, __u32 *ost_indices,
1414 int flags, struct thandle *th, int comp_idx,
1417 struct lod_layout_component *lod_comp;
1418 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1419 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1420 struct lod_tgt_desc *ost;
1421 struct dt_object *o;
1422 __u64 total_weight = 0;
1423 struct pool_desc *pool = NULL;
1424 struct lu_tgt_pool *osts;
1426 __u32 nfound, good_osts, stripe_count, stripe_count_min;
1427 bool overstriped = false;
1428 int stripes_per_ost = 1;
1433 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1434 lod_comp = &lo->ldo_comp_entries[comp_idx];
1435 stripe_count = lod_comp->llc_stripe_count;
1436 stripe_count_min = min_stripe_count(stripe_count, flags);
1437 if (stripe_count_min < 1)
1440 if (lod_comp->llc_pool != NULL)
1441 pool = lod_find_pool(lod, lod_comp->llc_pool);
1444 down_read(&pool_tgt_rw_sem(pool));
1445 osts = &(pool->pool_obds);
1447 osts = &lod->lod_ost_descs.ltd_tgt_pool;
1450 /* Detect -EAGAIN early, before expensive lock is taken. */
1451 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1452 GOTO(out_nolock, rc = -EAGAIN);
1454 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1456 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1458 /* Do actual allocation, use write lock here. */
1459 down_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1462 * Check again, while we were sleeping on @lq_rw_sem things could
1465 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1466 GOTO(out, rc = -EAGAIN);
1468 rc = ltd_qos_penalties_calc(&lod->lod_ost_descs);
1472 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1477 /* Find all the OSTs that are valid stripe candidates */
1478 for (i = 0; i < osts->op_count; i++) {
1479 if (!test_bit(osts->op_array[i], lod->lod_ost_bitmap))
1482 ost = OST_TGT(lod, osts->op_array[i]);
1483 ost->ltd_qos.ltq_usable = 0;
1485 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs,
1488 /* this OSP doesn't feel well */
1492 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1495 /* Fail Check before osc_precreate() is called
1496 * so we can only 'fail' single OSC.
1498 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1499 osts->op_array[i] == 0)
1502 ost->ltd_qos.ltq_usable = 1;
1503 lu_tgt_qos_weight_calc(ost);
1504 total_weight += ost->ltd_qos.ltq_weight;
1509 QOS_DEBUG("found %d good osts\n", good_osts);
1511 if (good_osts < stripe_count_min)
1512 GOTO(out, rc = -EAGAIN);
1514 /* If we do not have enough OSTs for the requested stripe count, do not
1515 * put more stripes per OST than requested.
1517 if (stripe_count / stripes_per_ost > good_osts)
1518 stripe_count = good_osts * stripes_per_ost;
1520 /* Find enough OSTs with weighted random allocation. */
1522 while (nfound < stripe_count) {
1523 u64 rand, cur_weight;
1528 rand = lu_prandom_u64_max(total_weight);
1530 /* On average, this will hit larger-weighted OSTs more often.
1531 * 0-weight OSTs will always get used last (only when rand=0)
1533 for (i = 0; i < osts->op_count; i++) {
1534 __u32 idx = osts->op_array[i];
1535 struct lod_tgt_desc *ost = OST_TGT(lod, idx);
1537 if (lod_should_avoid_ost(lo, lag, idx))
1540 ost = OST_TGT(lod, idx);
1542 if (!ost->ltd_qos.ltq_usable)
1545 cur_weight += ost->ltd_qos.ltq_weight;
1546 QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1547 "rand=%llu total_weight=%llu\n",
1548 stripe_count, nfound, cur_weight, rand,
1551 if (cur_weight < rand)
1554 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1556 * do not put >1 objects on a single OST, except for
1559 if ((lod_comp_is_ost_used(env, lo, idx)) &&
1560 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
1563 if (lod_qos_is_tgt_used(env, idx, nfound)) {
1564 if (lod_comp->llc_pattern &
1565 LOV_PATTERN_OVERSTRIPING)
1571 o = lod_qos_declare_object_on(env, lod, idx, slow, th);
1573 QOS_DEBUG("can't declare object on #%u: %d\n",
1574 idx, (int) PTR_ERR(o));
1578 lod_avoid_update(lo, lag);
1579 lod_qos_tgt_in_use(env, nfound, idx);
1581 ost_indices[nfound] = idx;
1582 ltd_qos_update(&lod->lod_ost_descs, ost, &total_weight);
1588 if (rc && !slow && nfound < stripe_count) {
1589 /* couldn't allocate using precreated objects
1590 * so try to wait for new precreations */
1596 /* no OST found on this iteration, give up */
1601 if (unlikely(nfound != stripe_count)) {
1603 * when the decision to use weighted algorithm was made
1604 * we had enough appropriate OSPs, but this state can
1605 * change anytime (no space on OST, broken connection, etc)
1606 * so it's possible OSP won't be able to provide us with
1607 * an object due to just changed state
1609 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1610 lod2obd(lod)->obd_name, stripe_count, nfound);
1611 for (i = 0; i < nfound; i++) {
1612 LASSERT(stripe[i] != NULL);
1613 dt_object_put(env, stripe[i]);
1617 /* makes sense to rebalance next time */
1618 set_bit(LQ_DIRTY, &lod->lod_ost_descs.ltd_qos.lq_flags);
1619 clear_bit(LQ_SAME_SPACE, &lod->lod_ost_descs.ltd_qos.lq_flags);
1623 /* If there are enough OSTs, a component with overstriping requessted
1624 * will not actually end up overstriped. The comp should reflect this.
1626 if (rc == 0 && !overstriped)
1627 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1630 up_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1634 up_read(&pool_tgt_rw_sem(pool));
1635 /* put back ref got by lod_find_pool() */
1636 lod_pool_putref(pool);
1643 * Allocate a striping using an algorithm with weights.
1645 * The function allocates remote MDT objects to create a striping, the first
1646 * object was already allocated on current MDT to ensure master object and
1647 * the first object are on the same MDT. The algorithm used is based on weights
1648 * (both free space and inodes), and it's trying to ensure the space/inodes are
1649 * used evenly by MDTs and MDSs. The striping configuration (# of stripes,
1650 * offset, pool) is taken from the object and is prepared by the caller.
1652 * If prepared configuration can't be met due to too few MDTs, then allocation
1655 * No concurrent allocation is allowed on the object and this must be ensured
1656 * by the caller. All the internal structures are protected by the function.
1658 * The algorithm has two steps: find available MDTs and calculate their
1659 * weights, then select the MDTs with their weights used as the probability.
1660 * An MDT with a higher weight is proportionately more likely to be selected
1661 * than one with a lower weight.
1663 * \param[in] env execution environment for this thread
1664 * \param[in] lo LOD object
1665 * \param[in] stripe_idx starting stripe index to allocate, if it's not
1666 * 0, we are restriping directory
1667 * \param[in] stripe_count total stripe count
1668 * \param[out] stripes striping created
1670 * \retval positive stripes allocated, and it should be equal to
1671 * lo->ldo_dir_stripe_count
1672 * \retval -EAGAIN not enough tgts are found for specified stripe count
1673 * \retval -EINVAL requested MDT index is invalid
1674 * \retval negative errno on failure
1676 int lod_mdt_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1677 struct dt_object **stripes, u32 stripe_idx,
1680 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1681 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
1682 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1683 struct lu_fid fid = { 0 };
1684 const struct lu_tgt_pool *pool;
1685 struct lu_tgt_desc *mdt;
1686 struct dt_object *dto;
1687 u64 total_weight = 0;
1688 u32 saved_idx = stripe_idx;
1690 unsigned int good_mdts;
1696 LASSERT(stripe_idx <= stripe_count);
1697 if (stripe_idx == stripe_count)
1698 RETURN(stripe_count);
1700 /* use MDT pool in @ltd, once MDT pool is supported in the future, it
1701 * can be passed in as argument like OST object allocation.
1703 pool = <d->ltd_tgt_pool;
1705 /* Detect -EAGAIN early, before expensive lock is taken. */
1706 if (!ltd_qos_is_usable(ltd))
1709 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
1714 /* Do actual allocation, use write lock here. */
1715 down_write(<d->ltd_qos.lq_rw_sem);
1718 * Check again, while we were sleeping on @lq_rw_sem things could
1721 if (!ltd_qos_is_usable(ltd))
1722 GOTO(unlock, rc = -EAGAIN);
1724 rc = ltd_qos_penalties_calc(ltd);
1729 /* Find all the MDTs that are valid stripe candidates */
1730 for (i = 0; i < pool->op_count; i++) {
1731 if (!test_bit(pool->op_array[i], ltd->ltd_tgt_bitmap))
1734 mdt = LTD_TGT(ltd, pool->op_array[i]);
1735 mdt->ltd_qos.ltq_usable = 0;
1737 rc = lod_is_tgt_usable(ltd, mdt);
1741 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1744 mdt->ltd_qos.ltq_usable = 1;
1745 lu_tgt_qos_weight_calc(mdt);
1746 total_weight += mdt->ltd_qos.ltq_weight;
1751 QOS_DEBUG("found %d good MDTs\n", good_mdts);
1753 if (good_mdts < stripe_count - stripe_idx)
1754 GOTO(unlock, rc = -EAGAIN);
1756 /* Find enough MDTs with weighted random allocation. */
1757 while (stripe_idx < stripe_count) {
1758 u64 rand, cur_weight;
1763 rand = lu_prandom_u64_max(total_weight);
1765 /* On average, this will hit larger-weighted MDTs more often.
1766 * 0-weight MDT will always get used last (only when rand=0) */
1767 for (i = 0; i < pool->op_count; i++) {
1770 mdt_idx = pool->op_array[i];
1771 mdt = LTD_TGT(ltd, mdt_idx);
1773 if (!mdt->ltd_qos.ltq_usable)
1776 cur_weight += mdt->ltd_qos.ltq_weight;
1778 QOS_DEBUG("stripe_count=%d stripe_index=%d cur_weight=%llu rand=%llu total_weight=%llu\n",
1779 stripe_count, stripe_idx, cur_weight, rand,
1782 if (cur_weight < rand)
1785 QOS_DEBUG("stripe=%d to idx=%d\n",
1786 stripe_idx, mdt_idx);
1788 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1791 rc2 = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1793 QOS_DEBUG("can't alloc FID on #%u: %d\n",
1798 conf.loc_flags = LOC_F_NEW;
1799 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1800 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1803 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1804 mdt_idx, (int) PTR_ERR(dto));
1808 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1809 stripes[stripe_idx] = dto;
1810 ltd_qos_update(ltd, mdt, &total_weight);
1816 /* no MDT found on this iteration, give up */
1821 if (unlikely(stripe_idx != stripe_count)) {
1823 * when the decision to use weighted algorithm was made
1824 * we had enough appropriate OSPs, but this state can
1825 * change anytime (no space on MDT, broken connection, etc)
1826 * so it's possible OSP won't be able to provide us with
1827 * an object due to just changed state
1829 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1830 lod2obd(lod)->obd_name, stripe_count, stripe_idx);
1831 for (i = saved_idx; i < stripe_idx; i++) {
1832 LASSERT(stripes[i] != NULL);
1833 dt_object_put(env, stripes[i]);
1837 /* makes sense to rebalance next time */
1838 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
1839 clear_bit(LQ_SAME_SPACE, <d->ltd_qos.lq_flags);
1847 up_write(<d->ltd_qos.lq_rw_sem);
1853 * Check stripe count the caller can use.
1855 * For new layouts (no initialized components), check the total size of the
1856 * layout against the maximum EA size from the backing file system. This
1857 * stops us from creating a layout which will be too large once initialized.
1859 * For existing layouts (with initialized components):
1860 * Find the maximal possible stripe count not greater than \a stripe_count.
1861 * If the provided stripe count is 0, then the filesystem's default is used.
1863 * \param[in] lod LOD device
1864 * \param[in] lo The lod_object
1865 * \param[in] comp_idx The component id, which the amount of stripes is
1867 * \param[in] stripe_count count the caller would like to use
1869 * \retval the maximum usable stripe count
1871 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
1872 int comp_idx, __u16 stripe_count, bool overstriping)
1874 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1875 /* max stripe count is based on OSD ea size */
1876 unsigned int easize = lod->lod_osd_max_easize;
1881 lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_count;
1884 /* Overstriping allows more stripes than targets */
1886 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count &&
1889 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count;
1891 if (lo->ldo_is_composite) {
1892 struct lod_layout_component *lod_comp;
1893 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
1894 unsigned int init_comp_sz = 0;
1895 unsigned int total_comp_sz = 0;
1896 unsigned int comp_sz;
1898 header_sz += sizeof(struct lov_comp_md_entry_v1) *
1901 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1902 unsigned int stripes;
1907 lod_comp = &lo->ldo_comp_entries[i];
1908 /* Extension comp is never inited - 0 stripes on disk */
1909 stripes = lod_comp->llc_flags & LCME_FL_EXTENSION ? 0 :
1910 lod_comp->llc_stripe_count;
1912 comp_sz = lov_mds_md_size(stripes, LOV_MAGIC_V3);
1913 total_comp_sz += comp_sz;
1914 if (lod_comp->llc_flags & LCME_FL_INIT)
1915 init_comp_sz += comp_sz;
1918 if (init_comp_sz > 0)
1919 total_comp_sz = init_comp_sz;
1921 header_sz += total_comp_sz;
1923 if (easize > header_sz)
1924 easize -= header_sz;
1929 max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
1930 max_stripes = (max_stripes == 0) ? 0 : max_stripes - 1;
1932 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1936 * Create in-core respresentation for a fully-defined striping
1938 * When the caller passes a fully-defined striping (i.e. everything including
1939 * OST object FIDs are defined), then we still need to instantiate LU-cache
1940 * with the objects representing the stripes defined. This function completes
1943 * \param[in] env execution environment for this thread
1944 * \param[in] mo LOD object
1945 * \param[in] buf buffer containing the striping
1947 * \retval 0 on success
1948 * \retval negative negated errno on error
1950 int lod_use_defined_striping(const struct lu_env *env,
1951 struct lod_object *mo,
1952 const struct lu_buf *buf)
1954 struct lod_layout_component *lod_comp;
1955 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1956 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1957 struct lov_comp_md_v1 *comp_v1 = NULL;
1958 struct lov_ost_data_v1 *objs;
1965 mutex_lock(&mo->ldo_layout_mutex);
1966 lod_striping_free_nolock(env, mo);
1968 magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
1970 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
1971 magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
1972 GOTO(unlock, rc = -EINVAL);
1974 if (magic == LOV_MAGIC_COMP_V1) {
1975 comp_v1 = buf->lb_buf;
1976 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1978 GOTO(unlock, rc = -EINVAL);
1979 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1980 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
1982 mo->ldo_is_composite = 1;
1983 } else if (magic == LOV_MAGIC_FOREIGN) {
1984 struct lov_foreign_md *foreign;
1987 if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
1989 "buf len %zu < min lov_foreign_md size (%zu)\n",
1991 offsetof(typeof(*foreign), lfm_value));
1992 GOTO(out, rc = -EINVAL);
1994 foreign = (struct lov_foreign_md *)buf->lb_buf;
1995 length = foreign_size_le(foreign);
1996 if (buf->lb_len < length) {
1998 "buf len %zu < this lov_foreign_md size (%zu)\n",
1999 buf->lb_len, length);
2000 GOTO(out, rc = -EINVAL);
2003 /* just cache foreign LOV EA raw */
2004 rc = lod_alloc_foreign_lov(mo, length);
2007 memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
2010 mo->ldo_is_composite = 0;
2014 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
2016 rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
2020 for (i = 0; i < comp_cnt; i++) {
2021 struct lu_extent *ext;
2025 lod_comp = &mo->ldo_comp_entries[i];
2027 if (mo->ldo_is_composite) {
2028 offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
2029 v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
2030 v3 = (struct lov_mds_md_v3 *)v1;
2031 magic = le32_to_cpu(v1->lmm_magic);
2033 ext = &comp_v1->lcm_entries[i].lcme_extent;
2034 lod_comp->llc_extent.e_start =
2035 le64_to_cpu(ext->e_start);
2036 lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
2037 lod_comp->llc_flags =
2038 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
2039 if (lod_comp->llc_flags & LCME_FL_NOSYNC)
2040 lod_comp->llc_timestamp = le64_to_cpu(
2041 comp_v1->lcm_entries[i].lcme_timestamp);
2043 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
2044 if (lod_comp->llc_id == LCME_ID_INVAL)
2045 GOTO(out, rc = -EINVAL);
2049 if (magic == LOV_MAGIC_V1) {
2050 objs = &v1->lmm_objects[0];
2051 } else if (magic == LOV_MAGIC_V3) {
2052 objs = &v3->lmm_objects[0];
2053 if (v3->lmm_pool_name[0] != '\0')
2054 pool_name = v3->lmm_pool_name;
2056 CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
2057 GOTO(out, rc = -EINVAL);
2060 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
2061 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
2062 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
2063 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
2065 * The stripe_offset of an uninit-ed component is stored in
2066 * the lmm_layout_gen
2068 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
2069 lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
2070 lod_obj_set_pool(mo, i, pool_name);
2072 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
2073 !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
2074 !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
2075 rc = lod_initialize_objects(env, mo, objs, i);
2081 rc = lod_fill_mirrors(mo);
2085 lod_striping_free_nolock(env, mo);
2087 mutex_unlock(&mo->ldo_layout_mutex);
2093 * Parse suggested striping configuration.
2095 * The caller gets a suggested striping configuration from a number of sources
2096 * including per-directory default and applications. Then it needs to verify
2097 * the suggested striping is valid, apply missing bits and store the resulting
2098 * configuration in the object to be used by the allocator later. Must not be
2099 * called concurrently against the same object. It's OK to provide a
2100 * fully-defined striping.
2102 * \param[in] env execution environment for this thread
2103 * \param[in] lo LOD object
2104 * \param[in] buf buffer containing the striping
2106 * \retval 0 on success
2107 * \retval negative negated errno on error
2109 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
2110 const struct lu_buf *buf)
2112 struct lod_layout_component *lod_comp;
2113 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2114 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2115 struct lov_user_md_v1 *v1 = NULL;
2116 struct lov_user_md_v3 *v3 = NULL;
2117 struct lov_comp_md_v1 *comp_v1 = NULL;
2118 struct lov_foreign_md *lfm = NULL;
2119 char def_pool[LOV_MAXPOOLNAME + 1];
2126 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
2129 memset(def_pool, 0, sizeof(def_pool));
2130 if (lo->ldo_comp_entries != NULL)
2131 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
2132 def_pool, sizeof(def_pool));
2134 /* free default striping info */
2135 if (lo->ldo_is_foreign)
2136 lod_free_foreign_lov(lo);
2138 lod_free_comp_entries(lo);
2140 rc = lod_verify_striping(env, d, lo, buf, false);
2146 comp_v1 = buf->lb_buf;
2147 /* {lmm,lfm}_magic position/length work for all LOV formats */
2148 magic = v1->lmm_magic;
2150 if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
2151 /* try to use as fully defined striping */
2152 rc = lod_use_defined_striping(env, lo, buf);
2157 case __swab32(LOV_USER_MAGIC_V1):
2158 lustre_swab_lov_user_md_v1(v1);
2159 magic = v1->lmm_magic;
2161 case LOV_USER_MAGIC_V1:
2163 case __swab32(LOV_USER_MAGIC_V3):
2164 lustre_swab_lov_user_md_v3(v3);
2165 magic = v3->lmm_magic;
2167 case LOV_USER_MAGIC_V3:
2169 case __swab32(LOV_USER_MAGIC_SPECIFIC):
2170 lustre_swab_lov_user_md_v3(v3);
2171 lustre_swab_lov_user_md_objects(v3->lmm_objects,
2172 v3->lmm_stripe_count);
2173 magic = v3->lmm_magic;
2175 case LOV_USER_MAGIC_SPECIFIC:
2177 case __swab32(LOV_USER_MAGIC_COMP_V1):
2178 lustre_swab_lov_comp_md_v1(comp_v1);
2179 magic = comp_v1->lcm_magic;
2181 case LOV_USER_MAGIC_COMP_V1:
2183 case __swab32(LOV_USER_MAGIC_FOREIGN):
2185 __swab32s(&lfm->lfm_magic);
2186 __swab32s(&lfm->lfm_length);
2187 __swab32s(&lfm->lfm_type);
2188 __swab32s(&lfm->lfm_flags);
2189 magic = lfm->lfm_magic;
2191 case LOV_USER_MAGIC_FOREIGN:
2194 rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
2197 memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
2200 CERROR("%s: unrecognized magic %X\n",
2201 lod2obd(d)->obd_name, magic);
2205 lustre_print_user_md(D_OTHER, v1, "parse config");
2207 if (magic == LOV_USER_MAGIC_COMP_V1) {
2208 comp_cnt = comp_v1->lcm_entry_count;
2211 mirror_cnt = comp_v1->lcm_mirror_count + 1;
2213 lo->ldo_flr_state = LCM_FL_RDONLY;
2214 lo->ldo_is_composite = 1;
2218 lo->ldo_is_composite = 0;
2221 rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2225 LASSERT(lo->ldo_comp_entries);
2227 for (i = 0; i < comp_cnt; i++) {
2228 struct pool_desc *pool;
2229 struct lu_extent *ext;
2232 lod_comp = &lo->ldo_comp_entries[i];
2234 if (lo->ldo_is_composite) {
2235 v1 = (struct lov_user_md *)((char *)comp_v1 +
2236 comp_v1->lcm_entries[i].lcme_offset);
2237 ext = &comp_v1->lcm_entries[i].lcme_extent;
2238 lod_comp->llc_extent = *ext;
2239 lod_comp->llc_flags =
2240 comp_v1->lcm_entries[i].lcme_flags &
2245 if (def_pool[0] != '\0')
2246 pool_name = def_pool;
2248 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2249 v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2250 v3 = (struct lov_user_md_v3 *)v1;
2252 if (v3->lmm_pool_name[0] != '\0')
2253 pool_name = v3->lmm_pool_name;
2255 if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2256 rc = lod_comp_copy_ost_lists(lod_comp, v3);
2258 GOTO(free_comp, rc);
2264 if (v1->lmm_pattern == 0)
2265 v1->lmm_pattern = LOV_PATTERN_RAID0;
2266 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2267 lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
2268 lov_pattern(v1->lmm_pattern) !=
2269 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
2270 CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2271 lod2obd(d)->obd_name, v1->lmm_pattern);
2272 GOTO(free_comp, rc = -EINVAL);
2275 lod_comp->llc_pattern = v1->lmm_pattern;
2276 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2277 lod_adjust_stripe_size(lod_comp, desc->ld_default_stripe_size);
2279 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2280 if (v1->lmm_stripe_count ||
2281 lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2282 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2284 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT &&
2285 lod_comp->llc_stripe_count != 0) {
2286 CDEBUG(D_LAYOUT, "%s: invalid stripe count: %u\n",
2287 lod2obd(d)->obd_name,
2288 lod_comp->llc_stripe_count);
2289 GOTO(free_comp, rc = -EINVAL);
2292 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2293 lod_obj_set_pool(lo, i, pool_name);
2295 if (pool_name == NULL)
2298 /* In the function below, .hs_keycmp resolves to
2299 * pool_hashkey_keycmp() */
2300 /* coverity[overrun-buffer-val] */
2301 pool = lod_find_pool(d, pool_name);
2305 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2306 rc = lod_check_index_in_pool(
2307 lod_comp->llc_stripe_offset, pool);
2309 lod_pool_putref(pool);
2310 CDEBUG(D_LAYOUT, "%s: invalid offset, %u\n",
2311 lod2obd(d)->obd_name,
2312 lod_comp->llc_stripe_offset);
2313 GOTO(free_comp, rc = -EINVAL);
2317 if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
2318 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
2319 lod_comp->llc_stripe_count = pool_tgt_count(pool);
2321 lod_pool_putref(pool);
2327 lod_free_comp_entries(lo);
2332 * prepare enough OST avoidance bitmap space
2334 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2336 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2337 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2338 unsigned long *bitmap = NULL;
2339 __u32 *new_oss = NULL;
2341 lag->lag_ost_avail = lod->lod_ost_count;
2343 /* reset OSS avoid guide array */
2344 lag->lag_oaa_count = 0;
2345 if (lag->lag_oss_avoid_array &&
2346 lag->lag_oaa_size < lod->lod_ost_count) {
2347 OBD_FREE_PTR_ARRAY(lag->lag_oss_avoid_array, lag->lag_oaa_size);
2348 lag->lag_oss_avoid_array = NULL;
2349 lag->lag_oaa_size = 0;
2352 /* init OST avoid guide bitmap */
2353 if (lag->lag_ost_avoid_bitmap) {
2354 if (lod->lod_ost_count <= lag->lag_ost_avoid_size) {
2355 bitmap_zero(lag->lag_ost_avoid_bitmap,
2356 lag->lag_ost_avoid_size);
2358 bitmap_free(lag->lag_ost_avoid_bitmap);
2359 lag->lag_ost_avoid_bitmap = NULL;
2363 if (!lag->lag_ost_avoid_bitmap) {
2364 bitmap = bitmap_zalloc(lod->lod_ost_count, GFP_KERNEL);
2369 if (!lag->lag_oss_avoid_array) {
2371 * usually there are multiple OSTs in one OSS, but we don't
2372 * know the exact OSS number, so we choose a safe option,
2373 * using OST count to allocate the array to store the OSS
2376 OBD_ALLOC_PTR_ARRAY(new_oss, lod->lod_ost_count);
2378 bitmap_free(bitmap);
2384 lag->lag_oss_avoid_array = new_oss;
2385 lag->lag_oaa_size = lod->lod_ost_count;
2388 lag->lag_ost_avoid_bitmap = bitmap;
2389 lag->lag_ost_avoid_size = lod->lod_ost_count;
2396 * Collect information of used OSTs and OSSs in the overlapped components
2399 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2402 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2403 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2404 unsigned long *bitmap = lag->lag_ost_avoid_bitmap;
2407 /* iterate mirrors */
2408 for (i = 0; i < lo->ldo_mirror_count; i++) {
2409 struct lod_layout_component *comp;
2412 * skip mirror containing component[comp_idx], we only
2413 * collect OSTs info of conflicting component in other mirrors,
2414 * so that during read, if OSTs of a mirror's component are
2415 * not available, we still have other mirror with different
2416 * OSTs to read the data.
2418 comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
2419 if (comp->llc_id != LCME_ID_INVAL &&
2420 mirror_id_of(comp->llc_id) ==
2421 mirror_id_of(lod_comp->llc_id))
2424 /* iterate components of a mirror */
2425 lod_foreach_mirror_comp(comp, lo, i) {
2427 * skip non-overlapped or un-instantiated components,
2428 * NOTE: don't use lod_comp_inited(comp) to judge
2429 * whether @comp has been inited, since during
2430 * declare phase, comp->llc_stripe has been allocated
2431 * while it's init flag not been set until the exec
2434 if (!lu_extent_is_overlapped(&comp->llc_extent,
2435 &lod_comp->llc_extent) ||
2440 * collect used OSTs index and OSS info from a
2443 for (j = 0; j < comp->llc_stripe_count; j++) {
2444 struct lod_tgt_desc *ost;
2445 struct lu_svr_qos *lsq;
2448 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2449 lsq = ost->ltd_qos.ltq_svr;
2451 if (test_bit(ost->ltd_index, bitmap))
2454 QOS_DEBUG("OST%d used in conflicting mirror "
2455 "component\n", ost->ltd_index);
2456 set_bit(ost->ltd_index, bitmap);
2457 lag->lag_ost_avail--;
2459 for (k = 0; k < lag->lag_oaa_count; k++) {
2460 if (lag->lag_oss_avoid_array[k] ==
2464 if (k == lag->lag_oaa_count) {
2465 lag->lag_oss_avoid_array[k] =
2467 lag->lag_oaa_count++;
2475 * Create a striping for an obejct.
2477 * The function creates a new striping for the object. The function tries QoS
2478 * algorithm first unless free space is distributed evenly among OSTs, but
2479 * by default RR algorithm is preferred due to internal concurrency (QoS is
2480 * serialized). The caller must ensure no concurrent calls to the function
2481 * are made against the same object.
2483 * \param[in] env execution environment for this thread
2484 * \param[in] lo LOD object
2485 * \param[in] attr attributes OST objects will be declared with
2486 * \param[in] th transaction handle
2487 * \param[in] comp_idx index of ldo_comp_entries
2489 * \retval 0 on success
2490 * \retval negative negated errno on error
2492 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2493 struct lu_attr *attr, struct thandle *th,
2494 int comp_idx, __u64 reserve)
2496 struct lod_layout_component *lod_comp;
2497 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2499 int flag = LOV_USES_ASSIGNED_STRIPE;
2501 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2502 struct dt_object **stripe = NULL;
2503 __u32 *ost_indices = NULL;
2507 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2508 lod_comp = &lo->ldo_comp_entries[comp_idx];
2509 LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
2511 /* A released component is being created */
2512 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2515 /* A Data-on-MDT component is being created */
2516 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2519 if (likely(lod_comp->llc_stripe == NULL)) {
2521 * no striping has been created so far
2523 LASSERT(lod_comp->llc_stripe_count);
2525 * statfs and check OST targets now, since ld_active_tgt_count
2526 * could be changed if some OSTs are [de]activated manually.
2528 lod_qos_statfs_update(env, d, &d->lod_ost_descs);
2529 stripe_len = lod_get_stripe_count(d, lo, comp_idx,
2530 lod_comp->llc_stripe_count,
2531 lod_comp->llc_pattern &
2532 LOV_PATTERN_OVERSTRIPING);
2534 if (stripe_len == 0)
2535 GOTO(out, rc = -ERANGE);
2536 lod_comp->llc_stripe_count = stripe_len;
2537 OBD_ALLOC_PTR_ARRAY(stripe, stripe_len);
2539 GOTO(out, rc = -ENOMEM);
2540 OBD_ALLOC_PTR_ARRAY(ost_indices, stripe_len);
2542 GOTO(out, rc = -ENOMEM);
2545 lod_getref(&d->lod_ost_descs);
2546 /* XXX: support for non-0 files w/o objects */
2547 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2548 d->lod_ost_count, stripe_len);
2550 if (lod_comp->llc_ostlist.op_array &&
2551 lod_comp->llc_ostlist.op_count) {
2552 rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2553 th, comp_idx, reserve);
2554 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2556 * collect OSTs and OSSs used in other mirrors whose
2557 * components cross the ldo_comp_entries[comp_idx]
2559 rc = lod_prepare_avoidance(env, lo);
2563 QOS_DEBUG("collecting conflict osts for comp[%d]\n",
2565 lod_collect_avoidance(lo, lag, comp_idx);
2567 rc = lod_ost_alloc_qos(env, lo, stripe, ost_indices,
2568 flag, th, comp_idx, reserve);
2570 rc = lod_ost_alloc_rr(env, lo, stripe,
2571 ost_indices, flag, th,
2574 rc = lod_ost_alloc_specific(env, lo, stripe,
2575 ost_indices, flag, th,
2579 lod_putref(d, &d->lod_ost_descs);
2581 for (i = 0; i < stripe_len; i++)
2582 if (stripe[i] != NULL)
2583 dt_object_put(env, stripe[i]);
2585 /* In case there is no space on any OST, let's ignore
2586 * the @reserve space to avoid an error at the init
2587 * time, probably the actual IO will be less than the
2588 * given @reserve space (aka extension_size). */
2593 lod_comp->llc_stripe_count = 0;
2595 lod_comp->llc_stripe = stripe;
2596 lod_comp->llc_ost_indices = ost_indices;
2597 lod_comp->llc_stripes_allocated = stripe_len;
2601 * lod_qos_parse_config() found supplied buf as a predefined
2602 * striping (not a hint), so it allocated all the object
2603 * now we need to create them
2605 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2606 struct dt_object *o;
2608 o = lod_comp->llc_stripe[i];
2611 rc = lod_sub_declare_create(env, o, attr, NULL,
2614 CERROR("can't declare create: %d\n", rc);
2619 * Clear LCME_FL_INIT for the component so that
2620 * lod_striping_create() can create the striping objects
2623 lod_comp_unset_init(lod_comp);
2629 OBD_FREE_PTR_ARRAY(stripe, stripe_len);
2631 OBD_FREE_PTR_ARRAY(ost_indices, stripe_len);
2636 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2637 struct lu_attr *attr, const struct lu_buf *buf,
2641 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2649 /* no OST available */
2650 /* XXX: should we be waiting a bit to prevent failures during
2651 * cluster initialization? */
2652 if (!d->lod_ost_count)
2656 * by this time, the object's ldo_stripe_count and ldo_stripe_size
2657 * contain default value for striping: taken from the parent
2658 * or from filesystem defaults
2660 * in case the caller is passing lovea with new striping config,
2661 * we may need to parse lovea and apply new configuration
2663 rc = lod_qos_parse_config(env, lo, buf);
2667 if (attr->la_valid & LA_SIZE)
2668 size = attr->la_size;
2671 * prepare OST object creation for the component covering file's
2672 * size, the 1st component (including plain layout file) is always
2675 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2676 struct lod_layout_component *lod_comp;
2677 struct lu_extent *extent;
2679 lod_comp = &lo->ldo_comp_entries[i];
2680 extent = &lod_comp->llc_extent;
2681 QOS_DEBUG("comp[%d] %lld "DEXT"\n", i, size, PEXT(extent));
2682 if (!lo->ldo_is_composite || size >= extent->e_start) {
2683 rc = lod_qos_prep_create(env, lo, attr, th, i, 0);