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 ltd->ltd_qos.lq_dirty = 1;
141 ltd->ltd_qos.lq_rr.lqr_dirty = 1;
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 ltd->ltd_qos.lq_dirty = 1;
157 ltd->ltd_qos.lq_rr.lqr_dirty = 1;
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 ltd->ltd_qos.lq_dirty = 1;
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 (!lqr->lqr_dirty) {
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 (!lqr->lqr_dirty) {
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];
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]);
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,
375 struct lod_tgt_desc *ost;
376 struct lu_object *o, *n;
377 struct lu_device *nd;
378 struct dt_object *dt;
383 LASSERT(ost_idx < d->lod_ost_descs.ltd_tgts_size);
384 ost = OST_TGT(d,ost_idx);
386 LASSERT(ost->ltd_tgt);
388 nd = &ost->ltd_tgt->dd_lu_dev;
391 * allocate anonymous object with zero fid, real fid
392 * will be assigned by OSP within transaction
393 * XXX: to be fixed with fully-functional OST fids
395 o = lu_object_anon(env, nd, NULL);
397 GOTO(out, dt = ERR_CAST(o));
399 n = lu_object_locate(o->lo_header, nd->ld_type);
400 if (unlikely(n == NULL)) {
401 CERROR("can't find slice\n");
402 lu_object_put(env, o);
403 GOTO(out, dt = ERR_PTR(-EINVAL));
406 dt = container_of(n, struct dt_object, do_lu);
408 rc = lod_sub_declare_create(env, dt, NULL, NULL, NULL, th);
410 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
412 lu_object_put(env, o);
421 * Calculate a minimum acceptable stripe count.
423 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
424 * all stripes or 3/4 of stripes.
426 * \param[in] stripe_count number of stripes requested
427 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
429 * \retval acceptable stripecount
431 static int min_stripe_count(__u32 stripe_count, int flags)
433 return (flags & LOV_USES_DEFAULT_STRIPE ?
434 stripe_count - (stripe_count / 4) : stripe_count);
437 #define LOV_CREATE_RESEED_MULT 30
438 #define LOV_CREATE_RESEED_MIN 2000
441 * Initialize temporary tgt-in-use array.
443 * Allocate or extend the array used to mark targets already assigned to a new
444 * striping so they are not used more than once.
446 * \param[in] env execution environment for this thread
447 * \param[in] stripes number of items needed in the array
449 * \retval 0 on success
450 * \retval -ENOMEM on error
452 static inline int lod_qos_tgt_in_use_clear(const struct lu_env *env,
455 struct lod_thread_info *info = lod_env_info(env);
457 if (info->lti_ea_store_size < sizeof(int) * stripes)
458 lod_ea_store_resize(info, stripes * sizeof(int));
459 if (info->lti_ea_store_size < sizeof(int) * stripes) {
460 CERROR("can't allocate memory for tgt-in-use array\n");
463 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
468 * Remember a target in the array of used targets.
470 * Mark the given target as used for a new striping being created. The status
471 * of an tgt in a striping can be checked with lod_qos_is_tgt_used().
473 * \param[in] env execution environment for this thread
474 * \param[in] idx index in the array
475 * \param[in] tgt_idx target index to mark as used
477 static inline void lod_qos_tgt_in_use(const struct lu_env *env,
478 int idx, int tgt_idx)
480 struct lod_thread_info *info = lod_env_info(env);
481 int *tgts = info->lti_ea_store;
483 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
488 * Check is tgt used in a striping.
490 * Checks whether tgt with the given index is marked as used in the temporary
491 * array (see lod_qos_tgt_in_use()).
493 * \param[in] env execution environment for this thread
494 * \param[in] tgt_idx target index to check
495 * \param[in] stripes the number of items used in the array already
500 static int lod_qos_is_tgt_used(const struct lu_env *env, int tgt_idx,
503 struct lod_thread_info *info = lod_env_info(env);
504 int *tgts = info->lti_ea_store;
507 for (j = 0; j < stripes; j++) {
508 if (tgts[j] == tgt_idx)
515 lod_obj_is_ost_use_skip_cb(const struct lu_env *env, struct lod_object *lo,
516 int comp_idx, struct lod_obj_stripe_cb_data *data)
518 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
520 return comp->llc_ost_indices == NULL;
524 lod_obj_is_ost_use_cb(const struct lu_env *env, struct lod_object *lo,
525 int comp_idx, struct lod_obj_stripe_cb_data *data)
527 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
530 for (i = 0; i < comp->llc_stripe_count; i++) {
531 if (comp->llc_ost_indices[i] == data->locd_ost_index) {
532 data->locd_ost_index = -1;
541 * Check is OST used in a composite layout
543 * \param[in] lo lod object
544 * \param[in] ost OST target index to check
546 * \retval false not used
549 static inline bool lod_comp_is_ost_used(const struct lu_env *env,
550 struct lod_object *lo, int ost)
552 struct lod_obj_stripe_cb_data data = { { 0 } };
554 data.locd_ost_index = ost;
555 data.locd_comp_skip_cb = lod_obj_is_ost_use_skip_cb;
556 data.locd_comp_cb = lod_obj_is_ost_use_cb;
558 (void)lod_obj_for_each_stripe(env, lo, NULL, &data);
560 return data.locd_ost_index == -1;
563 static inline void lod_avoid_update(struct lod_object *lo,
564 struct lod_avoid_guide *lag)
569 lag->lag_ost_avail--;
572 static inline bool lod_should_avoid_ost(struct lod_object *lo,
573 struct lod_avoid_guide *lag,
576 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
577 struct lod_tgt_desc *ost = OST_TGT(lod, index);
578 struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
582 if (!test_bit(index, lod->lod_ost_bitmap)) {
583 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
589 * we've tried our best, all available OSTs have been used in
590 * overlapped components in the other mirror
592 if (lag->lag_ost_avail == 0)
596 for (i = 0; i < lag->lag_oaa_count; i++) {
597 if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
603 * if the OSS which OST[index] resides has not been used, we'd like to
609 /* if the OSS has been used, check whether the OST has been used */
610 if (!cfs_bitmap_check(lag->lag_ost_avoid_bitmap, index))
613 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
618 static int lod_check_and_reserve_ost(const struct lu_env *env,
619 struct lod_object *lo,
620 struct lod_layout_component *lod_comp,
621 __u32 ost_idx, __u32 speed, __u32 *s_idx,
622 struct dt_object **stripe,
628 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
629 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
630 struct lu_tgt_desc *ost = OST_TGT(lod, ost_idx);
632 __u32 stripe_idx = *s_idx;
637 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs, ost, reserve);
642 * We expect number of precreated objects in f_ffree at
643 * the first iteration, skip OSPs with no objects ready
645 if (ost->ltd_statfs.os_fprecreated == 0 && speed == 0) {
646 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
651 * try to use another OSP if this one is degraded
653 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED && speed < 2) {
654 QOS_DEBUG("#%d: degraded\n", ost_idx);
659 * try not allocate on OST which has been used by other
662 if (speed == 0 && lod_comp_is_ost_used(env, lo, ost_idx)) {
663 QOS_DEBUG("iter %d: OST%d used by other component\n",
669 * try not allocate OSTs used by conflicting component of other mirrors
670 * for the first and second time.
672 if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
673 QOS_DEBUG("iter %d: OST%d used by conflicting mirror component\n",
678 /* do not put >1 objects on a single OST, except for overstriping */
679 if (lod_qos_is_tgt_used(env, ost_idx, stripe_idx)) {
680 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
686 o = lod_qos_declare_object_on(env, lod, ost_idx, th);
688 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
689 ost_idx, (int) PTR_ERR(o));
695 * We've successfully declared (reserved) an object
697 lod_avoid_update(lo, lag);
698 lod_qos_tgt_in_use(env, stripe_idx, ost_idx);
699 stripe[stripe_idx] = o;
700 ost_indices[stripe_idx] = ost_idx;
701 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
709 * Allocate a striping using round-robin algorithm.
711 * Allocates a new striping using round-robin algorithm. The function refreshes
712 * all the internal structures (statfs cache, array of available OSTs sorted
713 * with regard to OSS, etc). The number of stripes required is taken from the
714 * object (must be prepared by the caller), but can change if the flag
715 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
716 * is trying to create a striping on the object in parallel. All the internal
717 * structures (like pools, etc) are protected and no additional locking is
718 * required. The function succeeds even if a single stripe is allocated. To save
719 * time we give priority to targets which already have objects precreated.
720 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
722 * \param[in] env execution environment for this thread
723 * \param[in] lo LOD object
724 * \param[out] stripe striping created
725 * \param[out] ost_indices ost indices of striping created
726 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
727 * \param[in] th transaction handle
728 * \param[in] comp_idx index of ldo_comp_entries
730 * \retval 0 on success
731 * \retval -ENOSPC if not enough OSTs are found
732 * \retval negative negated errno for other failures
734 static int lod_ost_alloc_rr(const struct lu_env *env, struct lod_object *lo,
735 struct dt_object **stripe, __u32 *ost_indices,
736 int flags, struct thandle *th, int comp_idx,
739 struct lod_layout_component *lod_comp;
740 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
741 struct pool_desc *pool = NULL;
742 struct lu_tgt_pool *osts;
743 struct lu_qos_rr *lqr;
744 unsigned int i, array_idx;
745 __u32 ost_start_idx_temp;
746 __u32 stripe_idx = 0;
747 __u32 stripe_count, stripe_count_min, ost_idx;
748 int rc, speed = 0, ost_connecting = 0;
749 int stripes_per_ost = 1;
750 bool overstriped = false;
753 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
754 lod_comp = &lo->ldo_comp_entries[comp_idx];
755 stripe_count = lod_comp->llc_stripe_count;
756 stripe_count_min = min_stripe_count(stripe_count, flags);
758 if (lod_comp->llc_pool != NULL)
759 pool = lod_find_pool(m, lod_comp->llc_pool);
762 down_read(&pool_tgt_rw_sem(pool));
763 osts = &(pool->pool_obds);
764 lqr = &(pool->pool_rr);
766 osts = &m->lod_ost_descs.ltd_tgt_pool;
767 lqr = &(m->lod_ost_descs.ltd_qos.lq_rr);
770 rc = lod_qos_calc_rr(m, &m->lod_ost_descs, osts, lqr);
774 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
778 down_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
779 spin_lock(&lqr->lqr_alloc);
780 if (--lqr->lqr_start_count <= 0) {
781 lqr->lqr_start_idx = prandom_u32_max(osts->op_count);
782 lqr->lqr_start_count =
783 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
784 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
785 } else if (stripe_count_min >= osts->op_count ||
786 lqr->lqr_start_idx > osts->op_count) {
787 /* If we have allocated from all of the OSTs, slowly
788 * precess the next start if the OST/stripe count isn't
789 * already doing this for us. */
790 lqr->lqr_start_idx %= osts->op_count;
791 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
792 ++lqr->lqr_offset_idx;
794 ost_start_idx_temp = lqr->lqr_start_idx;
798 QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
799 "active %d count %d\n",
800 lod_comp->llc_pool ? lod_comp->llc_pool : "",
801 stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
802 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
804 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
806 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
808 for (i = 0; i < osts->op_count * stripes_per_ost
809 && stripe_idx < stripe_count; i++) {
810 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
812 ++lqr->lqr_start_idx;
813 ost_idx = lqr->lqr_pool.op_array[array_idx];
815 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
816 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
817 stripe_idx, array_idx, ost_idx);
819 if ((ost_idx == LOV_QOS_EMPTY) ||
820 !test_bit(ost_idx, m->lod_ost_bitmap))
823 /* Fail Check before osc_precreate() is called
824 so we can only 'fail' single OSC. */
825 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
828 spin_unlock(&lqr->lqr_alloc);
829 rc = lod_check_and_reserve_ost(env, lo, lod_comp, ost_idx,
830 speed, &stripe_idx, stripe,
831 ost_indices, th, &overstriped,
833 spin_lock(&lqr->lqr_alloc);
835 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
838 if ((speed < 2) && (stripe_idx < stripe_count_min)) {
839 /* Try again, allowing slower OSCs */
841 lqr->lqr_start_idx = ost_start_idx_temp;
847 spin_unlock(&lqr->lqr_alloc);
848 up_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
850 /* If there are enough OSTs, a component with overstriping requested
851 * will not actually end up overstriped. The comp should reflect this.
854 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
857 lod_comp->llc_stripe_count = stripe_idx;
858 /* at least one stripe is allocated */
861 /* nobody provided us with a single object */
870 up_read(&pool_tgt_rw_sem(pool));
871 /* put back ref got by lod_find_pool() */
872 lod_pool_putref(pool);
879 lod_qos_mdt_in_use_init(const struct lu_env *env,
880 const struct lu_tgt_descs *ltd,
881 u32 stripe_idx, u32 stripe_count,
882 const struct lu_tgt_pool *pool,
883 struct dt_object **stripes)
886 struct lu_tgt_desc *mdt;
890 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
894 /* if stripe_idx > 1, we are splitting directory, mark existing stripes
895 * in_use. Because for either split or creation, stripe 0 is local,
896 * don't mark it in use.
898 for (i = 1; i < stripe_idx; i++) {
900 for (j = 0; j < pool->op_count; j++) {
901 mdt_idx = pool->op_array[j];
903 if (!test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
906 mdt = LTD_TGT(ltd, mdt_idx);
907 if (&mdt->ltd_tgt->dd_lu_dev ==
908 stripes[i]->do_lu.lo_dev)
909 lod_qos_tgt_in_use(env, i, mdt_idx);
917 * Allocate a striping using round-robin algorithm.
919 * Allocates a new striping using round-robin algorithm. The function refreshes
920 * all the internal structures (statfs cache, array of available remote MDTs
921 * sorted with regard to MDS, etc). The number of stripes required is taken from
922 * the object (must be prepared by the caller). The caller should ensure nobody
923 * else is trying to create a striping on the object in parallel. All the
924 * internal structures (like pools, etc) are protected and no additional locking
925 * is required. The function succeeds even if a single stripe is allocated.
927 * \param[in] env execution environment for this thread
928 * \param[in] lo LOD object
929 * \param[out] stripes striping created
931 * \retval positive stripe objects allocated, including the first stripe
933 * \retval -ENOSPC if not enough MDTs are found
934 * \retval negative negated errno for other failures
936 int lod_mdt_alloc_rr(const struct lu_env *env, struct lod_object *lo,
937 struct dt_object **stripes, u32 stripe_idx,
940 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
941 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
942 struct lu_tgt_pool *pool;
943 struct lu_qos_rr *lqr;
944 struct lu_tgt_desc *mdt;
945 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
946 struct lu_fid fid = { 0 };
947 struct dt_object *dto;
948 unsigned int pool_idx;
950 u32 saved_idx = stripe_idx;
953 bool use_degraded = false;
954 int tgt_connecting = 0;
959 pool = <d->ltd_tgt_pool;
960 lqr = <d->ltd_qos.lq_rr;
961 rc = lod_qos_calc_rr(lod, ltd, pool, lqr);
965 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
970 down_read(<d->ltd_qos.lq_rw_sem);
971 spin_lock(&lqr->lqr_alloc);
972 if (--lqr->lqr_start_count <= 0) {
973 lqr->lqr_start_idx = prandom_u32_max(pool->op_count);
974 lqr->lqr_start_count =
975 (LOV_CREATE_RESEED_MIN / max(pool->op_count, 1U) +
976 LOV_CREATE_RESEED_MULT) * max(pool->op_count, 1U);
977 } else if (stripe_count - 1 >= pool->op_count ||
978 lqr->lqr_start_idx > pool->op_count) {
979 /* If we have allocated from all of the tgts, slowly
980 * precess the next start if the tgt/stripe count isn't
981 * already doing this for us. */
982 lqr->lqr_start_idx %= pool->op_count;
983 if (stripe_count - 1 > 1 &&
984 (pool->op_count % (stripe_count - 1)) != 1)
985 ++lqr->lqr_offset_idx;
987 start_mdt = lqr->lqr_start_idx;
990 QOS_DEBUG("want=%d start_idx=%d start_count=%d offset=%d active=%d count=%d\n",
991 stripe_count - 1, lqr->lqr_start_idx, lqr->lqr_start_count,
992 lqr->lqr_offset_idx, pool->op_count, pool->op_count);
994 for (i = 0; i < pool->op_count && stripe_idx < stripe_count; i++) {
995 pool_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
997 ++lqr->lqr_start_idx;
998 mdt_idx = lqr->lqr_pool.op_array[pool_idx];
999 mdt = LTD_TGT(ltd, mdt_idx);
1001 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1002 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1003 stripe_idx, pool_idx, mdt_idx);
1005 if (mdt_idx == LOV_QOS_EMPTY ||
1006 !test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
1009 /* do not put >1 objects on one MDT */
1010 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1013 rc = lod_is_tgt_usable(ltd, mdt);
1015 if (mdt->ltd_connecting)
1020 /* try to use another OSP if this one is degraded */
1021 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED &&
1023 QOS_DEBUG("#%d: degraded\n", mdt_idx);
1026 spin_unlock(&lqr->lqr_alloc);
1028 rc = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1030 QOS_DEBUG("#%d: alloc FID failed: %dl\n", mdt_idx, rc);
1031 spin_lock(&lqr->lqr_alloc);
1035 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1036 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1039 spin_lock(&lqr->lqr_alloc);
1041 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1042 mdt->ltd_index, (int) PTR_ERR(dto));
1044 if (mdt->ltd_connecting)
1049 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1050 stripes[stripe_idx++] = dto;
1053 if (!use_degraded && stripe_idx < stripe_count) {
1054 /* Try again, allowing slower MDTs */
1055 use_degraded = true;
1056 lqr->lqr_start_idx = start_mdt;
1061 spin_unlock(&lqr->lqr_alloc);
1062 up_read(<d->ltd_qos.lq_rw_sem);
1064 if (stripe_idx > saved_idx)
1065 /* at least one stripe is allocated */
1068 /* nobody provided us with a single object */
1070 RETURN(-EINPROGRESS);
1076 * Allocate a specific striping layout on a user defined set of OSTs.
1078 * Allocates new striping using the OST index range provided by the data from
1079 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1080 * OSTs are not considered. The exact order of OSTs requested by the user
1081 * is respected as much as possible depending on OST status. The number of
1082 * stripes needed and stripe offset are taken from the object. If that number
1083 * can not be met, then the function returns a failure and then it's the
1084 * caller's responsibility to release the stripes allocated. All the internal
1085 * structures are protected, but no concurrent allocation is allowed on the
1088 * \param[in] env execution environment for this thread
1089 * \param[in] lo LOD object
1090 * \param[out] stripe striping created
1091 * \param[out] ost_indices ost indices of striping created
1092 * \param[in] th transaction handle
1093 * \param[in] comp_idx index of ldo_comp_entries
1095 * \retval 0 on success
1096 * \retval -ENODEV OST index does not exist on file system
1097 * \retval -EINVAL requested OST index is invalid
1098 * \retval negative negated errno on error
1100 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1101 struct dt_object **stripe, __u32 *ost_indices,
1102 struct thandle *th, int comp_idx, __u64 reserve)
1104 struct lod_layout_component *lod_comp;
1105 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1106 struct dt_object *o;
1107 unsigned int array_idx = 0;
1108 int stripe_count = 0;
1113 /* for specific OSTs layout */
1114 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1115 lod_comp = &lo->ldo_comp_entries[comp_idx];
1116 LASSERT(lod_comp->llc_ostlist.op_array);
1117 LASSERT(lod_comp->llc_ostlist.op_count);
1119 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1123 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
1124 lod_comp->llc_stripe_offset =
1125 lod_comp->llc_ostlist.op_array[0];
1127 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1128 if (lod_comp->llc_ostlist.op_array[i] ==
1129 lod_comp->llc_stripe_offset) {
1134 if (i == lod_comp->llc_stripe_count) {
1136 "%s: start index %d not in the specified list of OSTs\n",
1137 lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1141 for (i = 0; i < lod_comp->llc_stripe_count;
1142 i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1143 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1145 if (!test_bit(ost_idx, m->lod_ost_bitmap)) {
1150 /* do not put >1 objects on a single OST, except for
1153 if (lod_qos_is_tgt_used(env, ost_idx, stripe_count) &&
1154 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
1159 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1160 LTD_TGT(&m->lod_ost_descs, ost_idx),
1162 if (rc < 0) /* this OSP doesn't feel well */
1165 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1169 "%s: can't declare new object on #%u: %d\n",
1170 lod2obd(m)->obd_name, ost_idx, rc);
1175 * We've successfully declared (reserved) an object
1177 lod_qos_tgt_in_use(env, stripe_count, ost_idx);
1178 stripe[stripe_count] = o;
1179 ost_indices[stripe_count] = ost_idx;
1187 * Allocate a striping on a predefined set of OSTs.
1189 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1190 * Full OSTs are not considered. The exact order of OSTs is not important and
1191 * varies depending on OST status. The allocation procedure prefers the targets
1192 * with precreated objects ready. The number of stripes needed and stripe
1193 * offset are taken from the object. If that number cannot be met, then the
1194 * function returns an error and then it's the caller's responsibility to
1195 * release the stripes allocated. All the internal structures are protected,
1196 * but no concurrent allocation is allowed on the same objects.
1198 * \param[in] env execution environment for this thread
1199 * \param[in] lo LOD object
1200 * \param[out] stripe striping created
1201 * \param[out] ost_indices ost indices of striping created
1202 * \param[in] flags not used
1203 * \param[in] th transaction handle
1204 * \param[in] comp_idx index of ldo_comp_entries
1206 * \retval 0 on success
1207 * \retval -ENOSPC if no OST objects are available at all
1208 * \retval -EFBIG if not enough OST objects are found
1209 * \retval -EINVAL requested offset is invalid
1210 * \retval negative errno on failure
1212 static int lod_ost_alloc_specific(const struct lu_env *env,
1213 struct lod_object *lo,
1214 struct dt_object **stripe, __u32 *ost_indices,
1215 int flags, struct thandle *th, int comp_idx,
1218 struct lod_layout_component *lod_comp;
1219 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1220 struct dt_object *o;
1221 struct lu_tgt_desc *tgt;
1223 unsigned int i, array_idx, ost_count;
1224 int rc, stripe_num = 0;
1226 struct pool_desc *pool = NULL;
1227 struct lu_tgt_pool *osts;
1228 int stripes_per_ost = 1;
1229 bool overstriped = false;
1232 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1233 lod_comp = &lo->ldo_comp_entries[comp_idx];
1235 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1239 if (lod_comp->llc_pool != NULL)
1240 pool = lod_find_pool(m, lod_comp->llc_pool);
1243 down_read(&pool_tgt_rw_sem(pool));
1244 osts = &(pool->pool_obds);
1246 osts = &m->lod_ost_descs.ltd_tgt_pool;
1249 ost_count = osts->op_count;
1252 /* search loi_ost_idx in ost array */
1254 for (i = 0; i < ost_count; i++) {
1255 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1260 if (i == ost_count) {
1261 CERROR("Start index %d not found in pool '%s'\n",
1262 lod_comp->llc_stripe_offset,
1263 lod_comp->llc_pool ? lod_comp->llc_pool : "");
1264 GOTO(out, rc = -EINVAL);
1267 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1269 (lod_comp->llc_stripe_count - 1)/ost_count + 1;
1271 for (i = 0; i < ost_count * stripes_per_ost;
1272 i++, array_idx = (array_idx + 1) % ost_count) {
1273 ost_idx = osts->op_array[array_idx];
1275 if (!test_bit(ost_idx, m->lod_ost_bitmap))
1278 /* Fail Check before osc_precreate() is called
1279 so we can only 'fail' single OSC. */
1280 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1284 * do not put >1 objects on a single OST, except for
1285 * overstriping, where it is intended
1287 if (lod_qos_is_tgt_used(env, ost_idx, stripe_num)) {
1288 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1295 * try not allocate on the OST used by other component
1297 if (speed == 0 && i != 0 &&
1298 lod_comp_is_ost_used(env, lo, ost_idx))
1301 tgt = LTD_TGT(&m->lod_ost_descs, ost_idx);
1303 /* Drop slow OSCs if we can, but not for requested start idx.
1305 * This means "if OSC is slow and it is not the requested
1306 * start OST, then it can be skipped, otherwise skip it only
1307 * if it is inactive/recovering/out-of-space." */
1309 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1312 /* this OSP doesn't feel well */
1317 * We expect number of precreated objects at the first
1318 * iteration. Skip OSPs with no objects ready. Don't apply
1319 * this logic to OST specified with stripe_offset.
1321 if (i && !tgt->ltd_statfs.os_fprecreated && !speed)
1324 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1326 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1327 ost_idx, (int) PTR_ERR(o));
1332 * We've successfully declared (reserved) an object
1334 lod_qos_tgt_in_use(env, stripe_num, ost_idx);
1335 stripe[stripe_num] = o;
1336 ost_indices[stripe_num] = ost_idx;
1339 /* We have enough stripes */
1340 if (stripe_num == lod_comp->llc_stripe_count)
1344 /* Try again, allowing slower OSCs */
1349 /* If we were passed specific striping params, then a failure to
1350 * meet those requirements is an error, since we can't reallocate
1351 * that memory (it might be part of a larger array or something).
1353 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1354 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1355 lod_comp->llc_stripe_count);
1356 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1358 /* If there are enough OSTs, a component with overstriping requessted
1359 * will not actually end up overstriped. The comp should reflect this.
1361 if (rc == 0 && !overstriped)
1362 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1366 up_read(&pool_tgt_rw_sem(pool));
1367 /* put back ref got by lod_find_pool() */
1368 lod_pool_putref(pool);
1375 * Allocate a striping using an algorithm with weights.
1377 * The function allocates OST objects to create a striping. The algorithm
1378 * used is based on weights (currently only using the free space), and it's
1379 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1380 * configuration (# of stripes, offset, pool) is taken from the object and
1381 * is prepared by the caller.
1383 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1384 * be met due to too few OSTs, then allocation fails. If the flag is passed
1385 * fewer than 3/4 of the requested number of stripes can be allocated, then
1388 * No concurrent allocation is allowed on the object and this must be ensured
1389 * by the caller. All the internal structures are protected by the function.
1391 * The algorithm has two steps: find available OSTs and calculate their
1392 * weights, then select the OSTs with their weights used as the probability.
1393 * An OST with a higher weight is proportionately more likely to be selected
1394 * than one with a lower weight.
1396 * \param[in] env execution environment for this thread
1397 * \param[in] lo LOD object
1398 * \param[out] stripe striping created
1399 * \param[out] ost_indices ost indices of striping created
1400 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1401 * \param[in] th transaction handle
1402 * \param[in] comp_idx index of ldo_comp_entries
1404 * \retval 0 on success
1405 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1406 * \retval -EINVAL requested OST index is invalid
1407 * \retval negative errno on failure
1409 static int lod_ost_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1410 struct dt_object **stripe, __u32 *ost_indices,
1411 int flags, struct thandle *th, int comp_idx,
1414 struct lod_layout_component *lod_comp;
1415 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1416 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1417 struct lod_tgt_desc *ost;
1418 struct dt_object *o;
1419 __u64 total_weight = 0;
1420 struct pool_desc *pool = NULL;
1421 struct lu_tgt_pool *osts;
1423 __u32 nfound, good_osts, stripe_count, stripe_count_min;
1424 bool overstriped = false;
1425 int stripes_per_ost = 1;
1429 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1430 lod_comp = &lo->ldo_comp_entries[comp_idx];
1431 stripe_count = lod_comp->llc_stripe_count;
1432 stripe_count_min = min_stripe_count(stripe_count, flags);
1433 if (stripe_count_min < 1)
1436 if (lod_comp->llc_pool != NULL)
1437 pool = lod_find_pool(lod, lod_comp->llc_pool);
1440 down_read(&pool_tgt_rw_sem(pool));
1441 osts = &(pool->pool_obds);
1443 osts = &lod->lod_ost_descs.ltd_tgt_pool;
1446 /* Detect -EAGAIN early, before expensive lock is taken. */
1447 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1448 GOTO(out_nolock, rc = -EAGAIN);
1450 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1452 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1454 /* Do actual allocation, use write lock here. */
1455 down_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1458 * Check again, while we were sleeping on @lq_rw_sem things could
1461 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1462 GOTO(out, rc = -EAGAIN);
1464 rc = ltd_qos_penalties_calc(&lod->lod_ost_descs);
1468 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1473 /* Find all the OSTs that are valid stripe candidates */
1474 for (i = 0; i < osts->op_count; i++) {
1475 if (!test_bit(osts->op_array[i], lod->lod_ost_bitmap))
1478 ost = OST_TGT(lod, osts->op_array[i]);
1479 ost->ltd_qos.ltq_usable = 0;
1481 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs,
1484 /* this OSP doesn't feel well */
1488 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1491 /* Fail Check before osc_precreate() is called
1492 * so we can only 'fail' single OSC.
1494 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1495 osts->op_array[i] == 0)
1498 ost->ltd_qos.ltq_usable = 1;
1499 lu_tgt_qos_weight_calc(ost);
1500 total_weight += ost->ltd_qos.ltq_weight;
1505 QOS_DEBUG("found %d good osts\n", good_osts);
1507 if (good_osts < stripe_count_min)
1508 GOTO(out, rc = -EAGAIN);
1510 /* If we do not have enough OSTs for the requested stripe count, do not
1511 * put more stripes per OST than requested.
1513 if (stripe_count / stripes_per_ost > good_osts)
1514 stripe_count = good_osts * stripes_per_ost;
1516 /* Find enough OSTs with weighted random allocation. */
1518 while (nfound < stripe_count) {
1519 u64 rand, cur_weight;
1524 rand = lu_prandom_u64_max(total_weight);
1526 /* On average, this will hit larger-weighted OSTs more often.
1527 * 0-weight OSTs will always get used last (only when rand=0)
1529 for (i = 0; i < osts->op_count; i++) {
1530 __u32 idx = osts->op_array[i];
1532 if (lod_should_avoid_ost(lo, lag, idx))
1535 ost = OST_TGT(lod, idx);
1537 if (!ost->ltd_qos.ltq_usable)
1540 cur_weight += ost->ltd_qos.ltq_weight;
1541 QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1542 "rand=%llu total_weight=%llu\n",
1543 stripe_count, nfound, cur_weight, rand,
1546 if (cur_weight < rand)
1549 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1551 * do not put >1 objects on a single OST, except for
1554 if ((lod_comp_is_ost_used(env, lo, idx)) &&
1555 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
1558 if (lod_qos_is_tgt_used(env, idx, nfound)) {
1559 if (lod_comp->llc_pattern &
1560 LOV_PATTERN_OVERSTRIPING)
1566 o = lod_qos_declare_object_on(env, lod, idx, th);
1568 QOS_DEBUG("can't declare object on #%u: %d\n",
1569 idx, (int) PTR_ERR(o));
1573 lod_avoid_update(lo, lag);
1574 lod_qos_tgt_in_use(env, nfound, idx);
1576 ost_indices[nfound] = idx;
1577 ltd_qos_update(&lod->lod_ost_descs, ost, &total_weight);
1584 /* no OST found on this iteration, give up */
1589 if (unlikely(nfound != stripe_count)) {
1591 * when the decision to use weighted algorithm was made
1592 * we had enough appropriate OSPs, but this state can
1593 * change anytime (no space on OST, broken connection, etc)
1594 * so it's possible OSP won't be able to provide us with
1595 * an object due to just changed state
1597 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1598 lod2obd(lod)->obd_name, stripe_count, nfound);
1599 for (i = 0; i < nfound; i++) {
1600 LASSERT(stripe[i] != NULL);
1601 dt_object_put(env, stripe[i]);
1605 /* makes sense to rebalance next time */
1606 lod->lod_ost_descs.ltd_qos.lq_dirty = 1;
1607 lod->lod_ost_descs.ltd_qos.lq_same_space = 0;
1612 /* If there are enough OSTs, a component with overstriping requessted
1613 * will not actually end up overstriped. The comp should reflect this.
1615 if (rc == 0 && !overstriped)
1616 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1619 up_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1623 up_read(&pool_tgt_rw_sem(pool));
1624 /* put back ref got by lod_find_pool() */
1625 lod_pool_putref(pool);
1632 * Allocate a striping using an algorithm with weights.
1634 * The function allocates remote MDT objects to create a striping, the first
1635 * object was already allocated on current MDT to ensure master object and
1636 * the first object are on the same MDT. The algorithm used is based on weights
1637 * (both free space and inodes), and it's trying to ensure the space/inodes are
1638 * used evenly by MDTs and MDSs. The striping configuration (# of stripes,
1639 * offset, pool) is taken from the object and is prepared by the caller.
1641 * If prepared configuration can't be met due to too few MDTs, then allocation
1644 * No concurrent allocation is allowed on the object and this must be ensured
1645 * by the caller. All the internal structures are protected by the function.
1647 * The algorithm has two steps: find available MDTs and calculate their
1648 * weights, then select the MDTs with their weights used as the probability.
1649 * An MDT with a higher weight is proportionately more likely to be selected
1650 * than one with a lower weight.
1652 * \param[in] env execution environment for this thread
1653 * \param[in] lo LOD object
1654 * \param[in] stripe_idx starting stripe index to allocate, if it's not
1655 * 0, we are restriping directory
1656 * \param[in] stripe_count total stripe count
1657 * \param[out] stripes striping created
1659 * \retval positive stripes allocated, and it should be equal to
1660 * lo->ldo_dir_stripe_count
1661 * \retval -EAGAIN not enough tgts are found for specified stripe count
1662 * \retval -EINVAL requested MDT index is invalid
1663 * \retval negative errno on failure
1665 int lod_mdt_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1666 struct dt_object **stripes, u32 stripe_idx,
1669 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1670 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
1671 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1672 struct lu_fid fid = { 0 };
1673 const struct lu_tgt_pool *pool;
1674 struct lu_tgt_desc *mdt;
1675 struct dt_object *dto;
1676 u64 total_weight = 0;
1677 u32 saved_idx = stripe_idx;
1679 unsigned int good_mdts;
1685 LASSERT(stripe_idx <= stripe_count);
1686 if (stripe_idx == stripe_count)
1687 RETURN(stripe_count);
1689 /* use MDT pool in @ltd, once MDT pool is supported in the future, it
1690 * can be passed in as argument like OST object allocation.
1692 pool = <d->ltd_tgt_pool;
1694 /* Detect -EAGAIN early, before expensive lock is taken. */
1695 if (!ltd_qos_is_usable(ltd))
1698 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
1703 /* Do actual allocation, use write lock here. */
1704 down_write(<d->ltd_qos.lq_rw_sem);
1707 * Check again, while we were sleeping on @lq_rw_sem things could
1710 if (!ltd_qos_is_usable(ltd))
1711 GOTO(unlock, rc = -EAGAIN);
1713 rc = ltd_qos_penalties_calc(ltd);
1718 /* Find all the MDTs that are valid stripe candidates */
1719 for (i = 0; i < pool->op_count; i++) {
1720 if (!test_bit(pool->op_array[i], ltd->ltd_tgt_bitmap))
1723 mdt = LTD_TGT(ltd, pool->op_array[i]);
1724 mdt->ltd_qos.ltq_usable = 0;
1726 rc = lod_is_tgt_usable(ltd, mdt);
1730 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1733 mdt->ltd_qos.ltq_usable = 1;
1734 lu_tgt_qos_weight_calc(mdt);
1735 total_weight += mdt->ltd_qos.ltq_weight;
1740 QOS_DEBUG("found %d good MDTs\n", good_mdts);
1742 if (good_mdts < stripe_count - stripe_idx)
1743 GOTO(unlock, rc = -EAGAIN);
1745 /* Find enough MDTs with weighted random allocation. */
1746 while (stripe_idx < stripe_count) {
1747 u64 rand, cur_weight;
1752 rand = lu_prandom_u64_max(total_weight);
1754 /* On average, this will hit larger-weighted MDTs more often.
1755 * 0-weight MDT will always get used last (only when rand=0) */
1756 for (i = 0; i < pool->op_count; i++) {
1759 mdt_idx = pool->op_array[i];
1760 mdt = LTD_TGT(ltd, mdt_idx);
1762 if (!mdt->ltd_qos.ltq_usable)
1765 cur_weight += mdt->ltd_qos.ltq_weight;
1767 QOS_DEBUG("stripe_count=%d stripe_index=%d cur_weight=%llu rand=%llu total_weight=%llu\n",
1768 stripe_count, stripe_idx, cur_weight, rand,
1771 if (cur_weight < rand)
1774 QOS_DEBUG("stripe=%d to idx=%d\n",
1775 stripe_idx, mdt_idx);
1777 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1780 rc2 = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1782 QOS_DEBUG("can't alloc FID on #%u: %d\n",
1787 conf.loc_flags = LOC_F_NEW;
1788 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1789 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1792 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1793 mdt_idx, (int) PTR_ERR(dto));
1797 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1798 stripes[stripe_idx] = dto;
1799 ltd_qos_update(ltd, mdt, &total_weight);
1805 /* no MDT found on this iteration, give up */
1810 if (unlikely(stripe_idx != stripe_count)) {
1812 * when the decision to use weighted algorithm was made
1813 * we had enough appropriate OSPs, but this state can
1814 * change anytime (no space on MDT, broken connection, etc)
1815 * so it's possible OSP won't be able to provide us with
1816 * an object due to just changed state
1818 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1819 lod2obd(lod)->obd_name, stripe_count, stripe_idx);
1820 for (i = saved_idx; i < stripe_idx; i++) {
1821 LASSERT(stripes[i] != NULL);
1822 dt_object_put(env, stripes[i]);
1826 /* makes sense to rebalance next time */
1827 ltd->ltd_qos.lq_dirty = 1;
1828 ltd->ltd_qos.lq_same_space = 0;
1836 up_write(<d->ltd_qos.lq_rw_sem);
1842 * Check stripe count the caller can use.
1844 * For new layouts (no initialized components), check the total size of the
1845 * layout against the maximum EA size from the backing file system. This
1846 * stops us from creating a layout which will be too large once initialized.
1848 * For existing layouts (with initialized components):
1849 * Find the maximal possible stripe count not greater than \a stripe_count.
1850 * If the provided stripe count is 0, then the filesystem's default is used.
1852 * \param[in] lod LOD device
1853 * \param[in] lo The lod_object
1854 * \param[in] stripe_count count the caller would like to use
1856 * \retval the maximum usable stripe count
1858 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
1859 __u16 stripe_count, bool overstriping)
1861 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1862 /* max stripe count is based on OSD ea size */
1863 unsigned int easize = lod->lod_osd_max_easize;
1869 lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_count;
1872 /* Overstriping allows more stripes than targets */
1874 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count &&
1877 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count;
1879 if (lo->ldo_is_composite) {
1880 struct lod_layout_component *lod_comp;
1881 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
1882 unsigned int init_comp_sz = 0;
1883 unsigned int total_comp_sz = 0;
1884 unsigned int comp_sz;
1886 header_sz += sizeof(struct lov_comp_md_entry_v1) *
1889 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1890 lod_comp = &lo->ldo_comp_entries[i];
1891 comp_sz = lov_mds_md_size(lod_comp->llc_stripe_count,
1893 total_comp_sz += comp_sz;
1894 if (lod_comp->llc_flags & LCME_FL_INIT)
1895 init_comp_sz += comp_sz;
1898 if (init_comp_sz > 0)
1899 total_comp_sz = init_comp_sz;
1901 header_sz += total_comp_sz;
1903 if (easize > header_sz)
1904 easize -= header_sz;
1909 max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
1910 max_stripes = (max_stripes == 0) ? 0 : max_stripes - 1;
1912 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1916 * Create in-core respresentation for a fully-defined striping
1918 * When the caller passes a fully-defined striping (i.e. everything including
1919 * OST object FIDs are defined), then we still need to instantiate LU-cache
1920 * with the objects representing the stripes defined. This function completes
1923 * \param[in] env execution environment for this thread
1924 * \param[in] mo LOD object
1925 * \param[in] buf buffer containing the striping
1927 * \retval 0 on success
1928 * \retval negative negated errno on error
1930 int lod_use_defined_striping(const struct lu_env *env,
1931 struct lod_object *mo,
1932 const struct lu_buf *buf)
1934 struct lod_layout_component *lod_comp;
1935 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1936 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1937 struct lov_comp_md_v1 *comp_v1 = NULL;
1938 struct lov_ost_data_v1 *objs;
1945 mutex_lock(&mo->ldo_layout_mutex);
1946 lod_striping_free_nolock(env, mo);
1948 magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
1950 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
1951 magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
1952 GOTO(unlock, rc = -EINVAL);
1954 if (magic == LOV_MAGIC_COMP_V1) {
1955 comp_v1 = buf->lb_buf;
1956 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1958 GOTO(unlock, rc = -EINVAL);
1959 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1960 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
1962 mo->ldo_is_composite = 1;
1963 } else if (magic == LOV_MAGIC_FOREIGN) {
1964 struct lov_foreign_md *foreign;
1967 if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
1969 "buf len %zu < min lov_foreign_md size (%zu)\n",
1971 offsetof(typeof(*foreign), lfm_value));
1972 GOTO(out, rc = -EINVAL);
1974 foreign = (struct lov_foreign_md *)buf->lb_buf;
1975 length = foreign_size_le(foreign);
1976 if (buf->lb_len < length) {
1978 "buf len %zu < this lov_foreign_md size (%zu)\n",
1979 buf->lb_len, length);
1980 GOTO(out, rc = -EINVAL);
1983 /* just cache foreign LOV EA raw */
1984 rc = lod_alloc_foreign_lov(mo, length);
1987 memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
1990 mo->ldo_is_composite = 0;
1994 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1996 rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
2000 for (i = 0; i < comp_cnt; i++) {
2001 struct lu_extent *ext;
2005 lod_comp = &mo->ldo_comp_entries[i];
2007 if (mo->ldo_is_composite) {
2008 offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
2009 v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
2010 v3 = (struct lov_mds_md_v3 *)v1;
2011 magic = le32_to_cpu(v1->lmm_magic);
2013 ext = &comp_v1->lcm_entries[i].lcme_extent;
2014 lod_comp->llc_extent.e_start =
2015 le64_to_cpu(ext->e_start);
2016 lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
2017 lod_comp->llc_flags =
2018 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
2019 if (lod_comp->llc_flags & LCME_FL_NOSYNC)
2020 lod_comp->llc_timestamp = le64_to_cpu(
2021 comp_v1->lcm_entries[i].lcme_timestamp);
2023 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
2024 if (lod_comp->llc_id == LCME_ID_INVAL)
2025 GOTO(out, rc = -EINVAL);
2029 if (magic == LOV_MAGIC_V1) {
2030 objs = &v1->lmm_objects[0];
2031 } else if (magic == LOV_MAGIC_V3) {
2032 objs = &v3->lmm_objects[0];
2033 if (v3->lmm_pool_name[0] != '\0')
2034 pool_name = v3->lmm_pool_name;
2036 CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
2037 GOTO(out, rc = -EINVAL);
2040 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
2041 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
2042 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
2043 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
2045 * The stripe_offset of an uninit-ed component is stored in
2046 * the lmm_layout_gen
2048 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
2049 lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
2050 lod_obj_set_pool(mo, i, pool_name);
2052 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
2053 !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
2054 !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
2055 rc = lod_initialize_objects(env, mo, objs, i);
2061 rc = lod_fill_mirrors(mo);
2065 lod_striping_free_nolock(env, mo);
2067 mutex_unlock(&mo->ldo_layout_mutex);
2073 * Parse suggested striping configuration.
2075 * The caller gets a suggested striping configuration from a number of sources
2076 * including per-directory default and applications. Then it needs to verify
2077 * the suggested striping is valid, apply missing bits and store the resulting
2078 * configuration in the object to be used by the allocator later. Must not be
2079 * called concurrently against the same object. It's OK to provide a
2080 * fully-defined striping.
2082 * \param[in] env execution environment for this thread
2083 * \param[in] lo LOD object
2084 * \param[in] buf buffer containing the striping
2086 * \retval 0 on success
2087 * \retval negative negated errno on error
2089 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
2090 const struct lu_buf *buf)
2092 struct lod_layout_component *lod_comp;
2093 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2094 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2095 struct lov_user_md_v1 *v1 = NULL;
2096 struct lov_user_md_v3 *v3 = NULL;
2097 struct lov_comp_md_v1 *comp_v1 = NULL;
2098 struct lov_foreign_md *lfm = NULL;
2099 char def_pool[LOV_MAXPOOLNAME + 1];
2106 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
2109 memset(def_pool, 0, sizeof(def_pool));
2110 if (lo->ldo_comp_entries != NULL)
2111 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
2112 def_pool, sizeof(def_pool));
2114 /* free default striping info */
2115 if (lo->ldo_is_foreign)
2116 lod_free_foreign_lov(lo);
2118 lod_free_comp_entries(lo);
2120 rc = lod_verify_striping(env, d, lo, buf, false);
2126 comp_v1 = buf->lb_buf;
2127 /* {lmm,lfm}_magic position/length work for all LOV formats */
2128 magic = v1->lmm_magic;
2130 if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
2131 /* try to use as fully defined striping */
2132 rc = lod_use_defined_striping(env, lo, buf);
2137 case __swab32(LOV_USER_MAGIC_V1):
2138 lustre_swab_lov_user_md_v1(v1);
2139 magic = v1->lmm_magic;
2141 case LOV_USER_MAGIC_V1:
2143 case __swab32(LOV_USER_MAGIC_V3):
2144 lustre_swab_lov_user_md_v3(v3);
2145 magic = v3->lmm_magic;
2147 case LOV_USER_MAGIC_V3:
2149 case __swab32(LOV_USER_MAGIC_SPECIFIC):
2150 lustre_swab_lov_user_md_v3(v3);
2151 lustre_swab_lov_user_md_objects(v3->lmm_objects,
2152 v3->lmm_stripe_count);
2153 magic = v3->lmm_magic;
2155 case LOV_USER_MAGIC_SPECIFIC:
2157 case __swab32(LOV_USER_MAGIC_COMP_V1):
2158 lustre_swab_lov_comp_md_v1(comp_v1);
2159 magic = comp_v1->lcm_magic;
2161 case LOV_USER_MAGIC_COMP_V1:
2163 case __swab32(LOV_USER_MAGIC_FOREIGN):
2165 __swab32s(&lfm->lfm_magic);
2166 __swab32s(&lfm->lfm_length);
2167 __swab32s(&lfm->lfm_type);
2168 __swab32s(&lfm->lfm_flags);
2169 magic = lfm->lfm_magic;
2171 case LOV_USER_MAGIC_FOREIGN:
2174 rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
2177 memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
2180 CERROR("%s: unrecognized magic %X\n",
2181 lod2obd(d)->obd_name, magic);
2185 lustre_print_user_md(D_OTHER, v1, "parse config");
2187 if (magic == LOV_USER_MAGIC_COMP_V1) {
2188 comp_cnt = comp_v1->lcm_entry_count;
2191 mirror_cnt = comp_v1->lcm_mirror_count + 1;
2193 lo->ldo_flr_state = LCM_FL_RDONLY;
2194 lo->ldo_is_composite = 1;
2198 lo->ldo_is_composite = 0;
2201 rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2205 LASSERT(lo->ldo_comp_entries);
2207 for (i = 0; i < comp_cnt; i++) {
2208 struct pool_desc *pool;
2209 struct lu_extent *ext;
2212 lod_comp = &lo->ldo_comp_entries[i];
2214 if (lo->ldo_is_composite) {
2215 v1 = (struct lov_user_md *)((char *)comp_v1 +
2216 comp_v1->lcm_entries[i].lcme_offset);
2217 ext = &comp_v1->lcm_entries[i].lcme_extent;
2218 lod_comp->llc_extent = *ext;
2219 lod_comp->llc_flags =
2220 comp_v1->lcm_entries[i].lcme_flags &
2225 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2226 v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2227 v3 = (struct lov_user_md_v3 *)v1;
2228 if (v3->lmm_pool_name[0] != '\0')
2229 pool_name = v3->lmm_pool_name;
2231 if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2232 rc = lod_comp_copy_ost_lists(lod_comp, v3);
2234 GOTO(free_comp, rc);
2238 if (pool_name == NULL && def_pool[0] != '\0')
2239 pool_name = def_pool;
2241 if (v1->lmm_pattern == 0)
2242 v1->lmm_pattern = LOV_PATTERN_RAID0;
2243 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2244 lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
2245 lov_pattern(v1->lmm_pattern) !=
2246 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
2247 CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2248 lod2obd(d)->obd_name, v1->lmm_pattern);
2249 GOTO(free_comp, rc = -EINVAL);
2252 lod_comp->llc_pattern = v1->lmm_pattern;
2253 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2254 lod_adjust_stripe_size(lod_comp, desc->ld_default_stripe_size);
2256 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2257 if (v1->lmm_stripe_count ||
2258 lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2259 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2261 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT &&
2262 lod_comp->llc_stripe_count != 0) {
2263 CDEBUG(D_LAYOUT, "%s: invalid stripe count: %u\n",
2264 lod2obd(d)->obd_name,
2265 lod_comp->llc_stripe_count);
2266 GOTO(free_comp, rc = -EINVAL);
2269 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2270 lod_obj_set_pool(lo, i, pool_name);
2272 if (pool_name == NULL)
2275 /* In the function below, .hs_keycmp resolves to
2276 * pool_hashkey_keycmp() */
2277 /* coverity[overrun-buffer-val] */
2278 pool = lod_find_pool(d, pool_name);
2282 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2283 rc = lod_check_index_in_pool(
2284 lod_comp->llc_stripe_offset, pool);
2286 lod_pool_putref(pool);
2287 CDEBUG(D_LAYOUT, "%s: invalid offset, %u\n",
2288 lod2obd(d)->obd_name,
2289 lod_comp->llc_stripe_offset);
2290 GOTO(free_comp, rc = -EINVAL);
2294 if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
2295 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
2296 lod_comp->llc_stripe_count = pool_tgt_count(pool);
2298 lod_pool_putref(pool);
2304 lod_free_comp_entries(lo);
2309 * prepare enough OST avoidance bitmap space
2311 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2313 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2314 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2315 struct cfs_bitmap *bitmap = NULL;
2316 __u32 *new_oss = NULL;
2318 lag->lag_ost_avail = lod->lod_ost_count;
2320 /* reset OSS avoid guide array */
2321 lag->lag_oaa_count = 0;
2322 if (lag->lag_oss_avoid_array &&
2323 lag->lag_oaa_size < lod->lod_ost_count) {
2324 OBD_FREE_PTR_ARRAY(lag->lag_oss_avoid_array, lag->lag_oaa_size);
2325 lag->lag_oss_avoid_array = NULL;
2326 lag->lag_oaa_size = 0;
2329 /* init OST avoid guide bitmap */
2330 if (lag->lag_ost_avoid_bitmap) {
2331 if (lod->lod_ost_count <= lag->lag_ost_avoid_bitmap->size) {
2332 CFS_RESET_BITMAP(lag->lag_ost_avoid_bitmap);
2334 CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
2335 lag->lag_ost_avoid_bitmap = NULL;
2339 if (!lag->lag_ost_avoid_bitmap) {
2340 bitmap = CFS_ALLOCATE_BITMAP(lod->lod_ost_count);
2345 if (!lag->lag_oss_avoid_array) {
2347 * usually there are multiple OSTs in one OSS, but we don't
2348 * know the exact OSS number, so we choose a safe option,
2349 * using OST count to allocate the array to store the OSS
2352 OBD_ALLOC_PTR_ARRAY(new_oss, lod->lod_ost_count);
2354 CFS_FREE_BITMAP(bitmap);
2360 lag->lag_oss_avoid_array = new_oss;
2361 lag->lag_oaa_size = lod->lod_ost_count;
2364 lag->lag_ost_avoid_bitmap = bitmap;
2370 * Collect information of used OSTs and OSSs in the overlapped components
2373 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2376 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2377 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2378 struct cfs_bitmap *bitmap = lag->lag_ost_avoid_bitmap;
2381 /* iterate mirrors */
2382 for (i = 0; i < lo->ldo_mirror_count; i++) {
2383 struct lod_layout_component *comp;
2386 * skip mirror containing component[comp_idx], we only
2387 * collect OSTs info of conflicting component in other mirrors,
2388 * so that during read, if OSTs of a mirror's component are
2389 * not available, we still have other mirror with different
2390 * OSTs to read the data.
2392 comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
2393 if (comp->llc_id != LCME_ID_INVAL &&
2394 mirror_id_of(comp->llc_id) ==
2395 mirror_id_of(lod_comp->llc_id))
2398 /* iterate components of a mirror */
2399 lod_foreach_mirror_comp(comp, lo, i) {
2401 * skip non-overlapped or un-instantiated components,
2402 * NOTE: don't use lod_comp_inited(comp) to judge
2403 * whether @comp has been inited, since during
2404 * declare phase, comp->llc_stripe has been allocated
2405 * while it's init flag not been set until the exec
2408 if (!lu_extent_is_overlapped(&comp->llc_extent,
2409 &lod_comp->llc_extent) ||
2414 * collect used OSTs index and OSS info from a
2417 for (j = 0; j < comp->llc_stripe_count; j++) {
2418 struct lod_tgt_desc *ost;
2419 struct lu_svr_qos *lsq;
2422 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2423 lsq = ost->ltd_qos.ltq_svr;
2425 if (cfs_bitmap_check(bitmap, ost->ltd_index))
2428 QOS_DEBUG("OST%d used in conflicting mirror "
2429 "component\n", ost->ltd_index);
2430 cfs_bitmap_set(bitmap, ost->ltd_index);
2431 lag->lag_ost_avail--;
2433 for (k = 0; k < lag->lag_oaa_count; k++) {
2434 if (lag->lag_oss_avoid_array[k] ==
2438 if (k == lag->lag_oaa_count) {
2439 lag->lag_oss_avoid_array[k] =
2441 lag->lag_oaa_count++;
2449 * Create a striping for an obejct.
2451 * The function creates a new striping for the object. The function tries QoS
2452 * algorithm first unless free space is distributed evenly among OSTs, but
2453 * by default RR algorithm is preferred due to internal concurrency (QoS is
2454 * serialized). The caller must ensure no concurrent calls to the function
2455 * are made against the same object.
2457 * \param[in] env execution environment for this thread
2458 * \param[in] lo LOD object
2459 * \param[in] attr attributes OST objects will be declared with
2460 * \param[in] th transaction handle
2461 * \param[in] comp_idx index of ldo_comp_entries
2463 * \retval 0 on success
2464 * \retval negative negated errno on error
2466 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2467 struct lu_attr *attr, struct thandle *th,
2468 int comp_idx, __u64 reserve)
2470 struct lod_layout_component *lod_comp;
2471 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2473 int flag = LOV_USES_ASSIGNED_STRIPE;
2475 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2476 struct dt_object **stripe = NULL;
2477 __u32 *ost_indices = NULL;
2481 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2482 lod_comp = &lo->ldo_comp_entries[comp_idx];
2483 LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
2485 /* A released component is being created */
2486 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2489 /* A Data-on-MDT component is being created */
2490 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2493 if (likely(lod_comp->llc_stripe == NULL)) {
2495 * no striping has been created so far
2497 LASSERT(lod_comp->llc_stripe_count);
2499 * statfs and check OST targets now, since ld_active_tgt_count
2500 * could be changed if some OSTs are [de]activated manually.
2502 lod_qos_statfs_update(env, d, &d->lod_ost_descs);
2503 stripe_len = lod_get_stripe_count(d, lo,
2504 lod_comp->llc_stripe_count,
2505 lod_comp->llc_pattern &
2506 LOV_PATTERN_OVERSTRIPING);
2508 if (stripe_len == 0)
2509 GOTO(out, rc = -ERANGE);
2510 lod_comp->llc_stripe_count = stripe_len;
2511 OBD_ALLOC_PTR_ARRAY(stripe, stripe_len);
2513 GOTO(out, rc = -ENOMEM);
2514 OBD_ALLOC_PTR_ARRAY(ost_indices, stripe_len);
2516 GOTO(out, rc = -ENOMEM);
2519 lod_getref(&d->lod_ost_descs);
2520 /* XXX: support for non-0 files w/o objects */
2521 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2522 d->lod_ost_count, stripe_len);
2524 if (lod_comp->llc_ostlist.op_array &&
2525 lod_comp->llc_ostlist.op_count) {
2526 rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2527 th, comp_idx, reserve);
2528 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2530 * collect OSTs and OSSs used in other mirrors whose
2531 * components cross the ldo_comp_entries[comp_idx]
2533 rc = lod_prepare_avoidance(env, lo);
2537 QOS_DEBUG("collecting conflict osts for comp[%d]\n",
2539 lod_collect_avoidance(lo, lag, comp_idx);
2541 rc = lod_ost_alloc_qos(env, lo, stripe, ost_indices,
2542 flag, th, comp_idx, reserve);
2544 rc = lod_ost_alloc_rr(env, lo, stripe,
2545 ost_indices, flag, th,
2548 rc = lod_ost_alloc_specific(env, lo, stripe,
2549 ost_indices, flag, th,
2553 lod_putref(d, &d->lod_ost_descs);
2555 for (i = 0; i < stripe_len; i++)
2556 if (stripe[i] != NULL)
2557 dt_object_put(env, stripe[i]);
2559 /* In case there is no space on any OST, let's ignore
2560 * the @reserve space to avoid an error at the init
2561 * time, probably the actual IO will be less than the
2562 * given @reserve space (aka extension_size). */
2567 lod_comp->llc_stripe_count = 0;
2569 lod_comp->llc_stripe = stripe;
2570 lod_comp->llc_ost_indices = ost_indices;
2571 lod_comp->llc_stripes_allocated = stripe_len;
2575 * lod_qos_parse_config() found supplied buf as a predefined
2576 * striping (not a hint), so it allocated all the object
2577 * now we need to create them
2579 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2580 struct dt_object *o;
2582 o = lod_comp->llc_stripe[i];
2585 rc = lod_sub_declare_create(env, o, attr, NULL,
2588 CERROR("can't declare create: %d\n", rc);
2593 * Clear LCME_FL_INIT for the component so that
2594 * lod_striping_create() can create the striping objects
2597 lod_comp_unset_init(lod_comp);
2603 OBD_FREE_PTR_ARRAY(stripe, stripe_len);
2605 OBD_FREE_PTR_ARRAY(ost_indices, stripe_len);
2610 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2611 struct lu_attr *attr, const struct lu_buf *buf,
2615 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2623 /* no OST available */
2624 /* XXX: should we be waiting a bit to prevent failures during
2625 * cluster initialization? */
2626 if (!d->lod_ost_count)
2630 * by this time, the object's ldo_stripe_count and ldo_stripe_size
2631 * contain default value for striping: taken from the parent
2632 * or from filesystem defaults
2634 * in case the caller is passing lovea with new striping config,
2635 * we may need to parse lovea and apply new configuration
2637 rc = lod_qos_parse_config(env, lo, buf);
2641 if (attr->la_valid & LA_SIZE)
2642 size = attr->la_size;
2645 * prepare OST object creation for the component covering file's
2646 * size, the 1st component (including plain layout file) is always
2649 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2650 struct lod_layout_component *lod_comp;
2651 struct lu_extent *extent;
2653 lod_comp = &lo->ldo_comp_entries[i];
2654 extent = &lod_comp->llc_extent;
2655 QOS_DEBUG("comp[%d] %lld "DEXT"\n", i, size, PEXT(extent));
2656 if (!lo->ldo_is_composite || size >= extent->e_start) {
2657 rc = lod_qos_prep_create(env, lo, attr, th, i, 0);