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/
31 * lustre/lod/lod_qos.c
33 * Implementation of different allocation algorithm used
34 * to distribute objects and data among OSTs.
37 #define DEBUG_SUBSYSTEM S_LOV
39 #include <asm/div64.h>
40 #include <linux/random.h>
42 #include <libcfs/libcfs.h>
43 #include <uapi/linux/lustre/lustre_idl.h>
44 #include <lustre_swab.h>
45 #include <obd_class.h>
47 #include "lod_internal.h"
50 * force QoS policy (not RR) to be used for testing purposes
56 #define QOS_DEBUG(fmt, ...) CDEBUG(D_QOS, fmt, ## __VA_ARGS__)
57 #define QOS_CONSOLE(fmt, ...) LCONSOLE(D_QOS, fmt, ## __VA_ARGS__)
59 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
60 OST_TGT(lod,i)->ltd_statfs.os_bsize)
62 static inline int lod_statfs_check(struct lu_tgt_descs *ltd,
63 struct lu_tgt_desc *tgt)
65 struct obd_statfs *sfs = &tgt->ltd_statfs;
67 if (((sfs->os_state & OS_STATFS_ENOSPC) ||
68 (!ltd->ltd_is_mdt && sfs->os_state & OS_STATFS_ENOINO &&
69 sfs->os_fprecreated == 0)))
72 /* If the OST is readonly then we can't allocate objects there */
73 if (sfs->os_state & OS_STATFS_READONLY)
76 /* object precreation is skipped on the OST with max_create_count=0 */
77 if (!ltd->ltd_is_mdt && sfs->os_state & OS_STATFS_NOPRECREATE)
84 * Check whether the target is available for new objects.
86 * Request statfs data from the given target and verify it's active and not
87 * read-only. If so, then it can be used to place new objects. This
88 * function also maintains the number of active/inactive targets and sets
89 * dirty flags if those numbers change so others can run re-balance procedures.
90 * No external locking is required.
92 * \param[in] env execution environment for this thread
93 * \param[in] d LOD device
94 * \param[in] ltd target table
95 * \param[in] tgt target
97 * \retval 0 if the target is good
98 * \retval negative negated errno on error
100 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
101 struct lu_tgt_descs *ltd,
102 struct lu_tgt_desc *tgt, __u64 reserve)
104 struct obd_statfs_info info = { 0 };
105 struct lov_desc *desc = <d->ltd_lov_desc;
112 info.os_enable_pre = 1;
113 rc = dt_statfs_info(env, tgt->ltd_tgt, &tgt->ltd_statfs, &info);
114 if (rc && rc != -ENOTCONN)
115 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
118 rc = lod_statfs_check(ltd, tgt);
124 (reserve + (info.os_reserved_mb_low << 20) >
125 tgt->ltd_statfs.os_bavail * tgt->ltd_statfs.os_bsize))
128 /* check whether device has changed state (active, inactive) */
129 if (rc != 0 && tgt->ltd_active) {
130 /* turned inactive? */
131 spin_lock(&d->lod_lock);
132 if (tgt->ltd_active) {
135 tgt->ltd_connecting = 1;
137 LASSERT(desc->ld_active_tgt_count > 0);
138 desc->ld_active_tgt_count--;
139 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
140 set_bit(LQ_DIRTY, <d->ltd_qos.lq_rr.lqr_flags);
141 CDEBUG(D_CONFIG, "%s: turns inactive\n",
142 tgt->ltd_exp->exp_obd->obd_name);
144 spin_unlock(&d->lod_lock);
145 } else if (rc == 0 && tgt->ltd_active == 0) {
147 LASSERTF(desc->ld_active_tgt_count < desc->ld_tgt_count,
148 "active tgt count %d, tgt nr %d\n",
149 desc->ld_active_tgt_count, desc->ld_tgt_count);
150 spin_lock(&d->lod_lock);
151 if (tgt->ltd_active == 0) {
153 tgt->ltd_connecting = 0;
154 desc->ld_active_tgt_count++;
155 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
156 set_bit(LQ_DIRTY, <d->ltd_qos.lq_rr.lqr_flags);
157 CDEBUG(D_CONFIG, "%s: turns active\n",
158 tgt->ltd_exp->exp_obd->obd_name);
160 spin_unlock(&d->lod_lock);
162 if (rc == -ENOTCONN) {
163 /* In case that the ENOTCONN for inactive OST state is
164 * mistreated as MDT disconnection state by the client,
165 * this error should be changed to someone else.
173 static int lod_is_tgt_usable(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
177 rc = lod_statfs_check(ltd, tgt);
181 if (!tgt->ltd_active)
188 * Maintain per-target statfs data.
190 * The function refreshes statfs data for all the targets every N seconds.
191 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
194 * \param[in] env execution environment for this thread
195 * \param[in] lod LOD device
196 * \param[in] ltd tgt table
198 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod,
199 struct lu_tgt_descs *ltd)
201 struct obd_device *obd = lod2obd(lod);
202 struct lu_tgt_desc *tgt;
207 max_age = ktime_get_seconds() - 2 * ltd->ltd_lov_desc.ld_qos_maxage;
209 if (obd->obd_osfs_age > max_age)
210 /* statfs data are quite recent, don't need to refresh it */
213 down_write(<d->ltd_qos.lq_rw_sem);
215 if (obd->obd_osfs_age > max_age)
218 ltd_foreach_tgt(ltd, tgt) {
219 avail = tgt->ltd_statfs.os_bavail;
220 if (lod_statfs_and_check(env, lod, ltd, tgt, 0))
223 if (tgt->ltd_statfs.os_bavail != avail)
224 /* recalculate weigths */
225 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
227 obd->obd_osfs_age = ktime_get_seconds();
230 up_write(<d->ltd_qos.lq_rw_sem);
234 #define LOV_QOS_EMPTY ((__u32)-1)
237 * Calculate optimal round-robin order with regard to OSSes.
239 * Place all the OSTs from pool \a src_pool in a special array to be used for
240 * round-robin (RR) stripe allocation. The placement algorithm interleaves
241 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
242 * Resorts the targets when the number of active targets changes (because of
243 * a new target or activation/deactivation).
245 * \param[in] lod LOD device
246 * \param[in] ltd tgt table
247 * \param[in] src_pool tgt pool
248 * \param[in] lqr round-robin list
250 * \retval 0 on success
251 * \retval -ENOMEM fails to allocate the array
253 static int lod_qos_calc_rr(struct lod_device *lod, struct lu_tgt_descs *ltd,
254 const struct lu_tgt_pool *src_pool,
255 struct lu_qos_rr *lqr)
257 struct lu_svr_qos *svr;
258 struct lu_tgt_desc *tgt;
259 unsigned placed, real_count;
264 if (!test_bit(LQ_DIRTY, &lqr->lqr_flags)) {
265 LASSERT(lqr->lqr_pool.op_size);
269 /* Do actual allocation. */
270 down_write(<d->ltd_qos.lq_rw_sem);
273 * Check again. While we were sleeping on @lq_rw_sem something could
276 if (!test_bit(LQ_DIRTY, &lqr->lqr_flags)) {
277 LASSERT(lqr->lqr_pool.op_size);
278 up_write(<d->ltd_qos.lq_rw_sem);
282 real_count = src_pool->op_count;
284 /* Zero the pool array */
285 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
286 deleting from the pool. The lq_rw_sem insures that nobody else
288 lqr->lqr_pool.op_count = real_count;
289 rc = lu_tgt_pool_extend(&lqr->lqr_pool, real_count);
291 up_write(<d->ltd_qos.lq_rw_sem);
294 for (i = 0; i < lqr->lqr_pool.op_count; i++)
295 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
297 /* Place all the tgts from 1 svr at the same time. */
299 list_for_each_entry(svr, <d->ltd_qos.lq_svr_list, lsq_svr_list) {
302 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
305 if (!test_bit(src_pool->op_array[i],
306 ltd->ltd_tgt_bitmap))
309 tgt = LTD_TGT(ltd, src_pool->op_array[i]);
310 LASSERT(tgt && tgt->ltd_tgt);
311 if (tgt->ltd_qos.ltq_svr != svr)
314 /* Evenly space these tgts across arrayspace */
315 next = j * lqr->lqr_pool.op_count / svr->lsq_tgt_count;
316 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
317 next = (next + 1) % lqr->lqr_pool.op_count;
319 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
325 clear_bit(LQ_DIRTY, &lqr->lqr_flags);
326 up_write(<d->ltd_qos.lq_rw_sem);
328 if (placed != real_count) {
329 /* This should never happen */
330 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all tgts in the "
331 "round-robin list (%d of %d).\n",
333 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
334 LCONSOLE(D_WARNING, "rr #%d tgt idx=%d\n", i,
335 lqr->lqr_pool.op_array[i]);
337 set_bit(LQ_DIRTY, &lqr->lqr_flags);
342 for (i = 0; i < lqr->lqr_pool.op_count; i++)
343 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
350 * Instantiate and declare creation of a new object.
352 * The function instantiates LU representation for a new object on the
353 * specified device. Also it declares an intention to create that
354 * object on the storage target.
356 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
357 * infrastructure - in the existing precreation framework we can't assign FID
358 * at this moment, we do this later once a transaction is started. So the
359 * special method instantiates FID-less object in the cache and later it
360 * will get a FID and proper placement in LU cache.
362 * \param[in] env execution environment for this thread
363 * \param[in] d LOD device
364 * \param[in] ost_idx OST target index where the object is being created
365 * \param[in] th transaction handle
367 * \retval object ptr on success, ERR_PTR() otherwise
369 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
370 struct lod_device *d,
375 struct dt_allocation_hint *ah = &lod_env_info(env)->lti_ah;
376 struct lod_tgt_desc *ost;
377 struct lu_object *o, *n;
378 struct lu_device *nd;
379 struct dt_object *dt;
384 LASSERT(ost_idx < d->lod_ost_descs.ltd_tgts_size);
385 ost = OST_TGT(d,ost_idx);
387 LASSERT(ost->ltd_tgt);
389 nd = &ost->ltd_tgt->dd_lu_dev;
392 * allocate anonymous object with zero fid, real fid
393 * will be assigned by OSP within transaction
394 * XXX: to be fixed with fully-functional OST fids
396 o = lu_object_anon(env, nd, NULL);
398 GOTO(out, dt = ERR_CAST(o));
400 n = lu_object_locate(o->lo_header, nd->ld_type);
401 if (unlikely(n == NULL)) {
402 CERROR("can't find slice\n");
403 lu_object_put(env, o);
404 GOTO(out, dt = ERR_PTR(-EINVAL));
407 dt = container_of(n, struct dt_object, do_lu);
409 ah->dah_can_block = can_block;
410 rc = lod_sub_declare_create(env, dt, NULL, ah, NULL, th);
412 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
414 lu_object_put(env, o);
423 * Calculate a minimum acceptable stripe count.
425 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
426 * all stripes or 3/4 of stripes.
428 * \param[in] stripe_count number of stripes requested
429 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
431 * \retval acceptable stripecount
433 static int min_stripe_count(__u32 stripe_count, int flags)
435 return (flags & LOV_USES_DEFAULT_STRIPE ?
436 stripe_count - (stripe_count / 4) : stripe_count);
439 #define LOV_CREATE_RESEED_MULT 30
440 #define LOV_CREATE_RESEED_MIN 2000
443 * Initialize temporary tgt-in-use array.
445 * Allocate or extend the array used to mark targets already assigned to a new
446 * striping so they are not used more than once.
448 * \param[in] env execution environment for this thread
449 * \param[in] stripes number of items needed in the array
451 * \retval 0 on success
452 * \retval -ENOMEM on error
454 static inline int lod_qos_tgt_in_use_clear(const struct lu_env *env,
457 struct lod_thread_info *info = lod_env_info(env);
459 if (info->lti_ea_store_size < sizeof(int) * stripes)
460 lod_ea_store_resize(info, stripes * sizeof(int));
461 if (info->lti_ea_store_size < sizeof(int) * stripes) {
462 CERROR("can't allocate memory for tgt-in-use array\n");
465 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
470 * Remember a target in the array of used targets.
472 * Mark the given target as used for a new striping being created. The status
473 * of an tgt in a striping can be checked with lod_qos_is_tgt_used().
475 * \param[in] env execution environment for this thread
476 * \param[in] idx index in the array
477 * \param[in] tgt_idx target index to mark as used
479 static inline void lod_qos_tgt_in_use(const struct lu_env *env,
480 int idx, int tgt_idx)
482 struct lod_thread_info *info = lod_env_info(env);
483 int *tgts = info->lti_ea_store;
485 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
490 * Check is tgt used in a striping.
492 * Checks whether tgt with the given index is marked as used in the temporary
493 * array (see lod_qos_tgt_in_use()).
495 * \param[in] env execution environment for this thread
496 * \param[in] tgt_idx target index to check
497 * \param[in] stripes the number of items used in the array already
502 static int lod_qos_is_tgt_used(const struct lu_env *env, int tgt_idx,
505 struct lod_thread_info *info = lod_env_info(env);
506 int *tgts = info->lti_ea_store;
509 for (j = 0; j < stripes; j++) {
510 if (tgts[j] == tgt_idx)
517 lod_obj_is_ost_use_skip_cb(const struct lu_env *env, struct lod_object *lo,
518 int comp_idx, struct lod_obj_stripe_cb_data *data)
520 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
522 return comp->llc_ost_indices == NULL;
526 lod_obj_is_ost_use_cb(const struct lu_env *env, struct lod_object *lo,
527 int comp_idx, struct lod_obj_stripe_cb_data *data)
529 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
532 for (i = 0; i < comp->llc_stripe_count; i++) {
533 if (comp->llc_ost_indices[i] == data->locd_ost_index) {
534 data->locd_ost_index = -1;
543 * Check is OST used in a composite layout
545 * \param[in] lo lod object
546 * \param[in] ost OST target index to check
548 * \retval false not used
551 static inline bool lod_comp_is_ost_used(const struct lu_env *env,
552 struct lod_object *lo, int ost)
554 struct lod_obj_stripe_cb_data data = { { 0 } };
556 data.locd_ost_index = ost;
557 data.locd_comp_skip_cb = lod_obj_is_ost_use_skip_cb;
558 data.locd_comp_cb = lod_obj_is_ost_use_cb;
560 (void)lod_obj_for_each_stripe(env, lo, NULL, &data);
562 return data.locd_ost_index == -1;
565 static inline void lod_avoid_update(struct lod_object *lo,
566 struct lod_avoid_guide *lag)
571 lag->lag_ost_avail--;
574 static inline bool lod_should_avoid_ost(struct lod_object *lo,
575 struct lod_avoid_guide *lag,
578 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
579 struct lod_tgt_desc *ost = OST_TGT(lod, index);
580 struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
584 if (!test_bit(index, lod->lod_ost_bitmap)) {
585 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
591 * we've tried our best, all available OSTs have been used in
592 * overlapped components in the other mirror
594 if (lag->lag_ost_avail == 0)
598 for (i = 0; i < lag->lag_oaa_count; i++) {
599 if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
605 * if the OSS which OST[index] resides has not been used, we'd like to
611 /* if the OSS has been used, check whether the OST has been used */
612 if (!test_bit(index, lag->lag_ost_avoid_bitmap))
615 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
620 static int lod_check_and_reserve_ost(const struct lu_env *env,
621 struct lod_object *lo,
622 struct lod_layout_component *lod_comp,
623 __u32 ost_idx, __u32 speed, __u32 *s_idx,
624 struct dt_object **stripe,
630 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
631 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
632 struct lu_tgt_desc *ost = OST_TGT(lod, ost_idx);
634 __u32 stripe_idx = *s_idx;
639 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs, ost, reserve);
644 * We expect number of precreated objects in f_ffree at
645 * the first iteration, skip OSPs with no objects ready
647 if (ost->ltd_statfs.os_fprecreated == 0 && speed == 0) {
648 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
653 * try to use another OSP if this one is degraded
655 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED && speed < 2) {
656 QOS_DEBUG("#%d: degraded\n", ost_idx);
661 * try not allocate on OST which has been used by other
664 if (speed == 0 && lod_comp_is_ost_used(env, lo, ost_idx)) {
665 QOS_DEBUG("iter %d: OST%d used by other component\n",
671 * try not allocate OSTs used by conflicting component of other mirrors
672 * for the first and second time.
674 if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
675 QOS_DEBUG("iter %d: OST%d used by conflicting mirror component\n",
680 /* do not put >1 objects on a single OST, except for overstriping */
681 if (lod_qos_is_tgt_used(env, ost_idx, stripe_idx)) {
682 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
688 o = lod_qos_declare_object_on(env, lod, ost_idx, true, th);
690 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
691 ost_idx, (int) PTR_ERR(o));
697 * We've successfully declared (reserved) an object
699 lod_avoid_update(lo, lag);
700 lod_qos_tgt_in_use(env, stripe_idx, ost_idx);
701 stripe[stripe_idx] = o;
702 ost_indices[stripe_idx] = ost_idx;
703 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
711 * Allocate a striping using round-robin algorithm.
713 * Allocates a new striping using round-robin algorithm. The function refreshes
714 * all the internal structures (statfs cache, array of available OSTs sorted
715 * with regard to OSS, etc). The number of stripes required is taken from the
716 * object (must be prepared by the caller), but can change if the flag
717 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
718 * is trying to create a striping on the object in parallel. All the internal
719 * structures (like pools, etc) are protected and no additional locking is
720 * required. The function succeeds even if a single stripe is allocated. To save
721 * time we give priority to targets which already have objects precreated.
722 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
724 * \param[in] env execution environment for this thread
725 * \param[in] lo LOD object
726 * \param[out] stripe striping created
727 * \param[out] ost_indices ost indices of striping created
728 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
729 * \param[in] th transaction handle
730 * \param[in] comp_idx index of ldo_comp_entries
732 * \retval 0 on success
733 * \retval -ENOSPC if not enough OSTs are found
734 * \retval negative negated errno for other failures
736 static int lod_ost_alloc_rr(const struct lu_env *env, struct lod_object *lo,
737 struct dt_object **stripe, __u32 *ost_indices,
738 int flags, struct thandle *th, int comp_idx,
741 struct lod_layout_component *lod_comp;
742 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
743 struct pool_desc *pool = NULL;
744 struct lu_tgt_pool *osts;
745 struct lu_qos_rr *lqr;
746 unsigned int i, array_idx;
747 __u32 ost_start_idx_temp;
748 __u32 stripe_idx = 0;
749 __u32 stripe_count, stripe_count_min, ost_idx;
750 int rc, speed = 0, ost_connecting = 0;
751 int stripes_per_ost = 1;
752 bool overstriped = false;
755 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
756 lod_comp = &lo->ldo_comp_entries[comp_idx];
757 stripe_count = lod_comp->llc_stripe_count;
758 stripe_count_min = min_stripe_count(stripe_count, flags);
760 if (lod_comp->llc_pool != NULL)
761 pool = lod_find_pool(m, lod_comp->llc_pool);
764 down_read(&pool_tgt_rw_sem(pool));
765 osts = &(pool->pool_obds);
766 lqr = &(pool->pool_rr);
768 osts = &m->lod_ost_descs.ltd_tgt_pool;
769 lqr = &(m->lod_ost_descs.ltd_qos.lq_rr);
772 rc = lod_qos_calc_rr(m, &m->lod_ost_descs, osts, lqr);
776 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
780 down_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
781 spin_lock(&lqr->lqr_alloc);
782 if (--lqr->lqr_start_count <= 0) {
783 lqr->lqr_start_idx = prandom_u32_max(osts->op_count);
784 lqr->lqr_start_count =
785 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
786 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
787 } else if (stripe_count_min >= osts->op_count ||
788 lqr->lqr_start_idx > osts->op_count) {
789 /* If we have allocated from all of the OSTs, slowly
790 * precess the next start if the OST/stripe count isn't
791 * already doing this for us. */
792 lqr->lqr_start_idx %= osts->op_count;
793 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
794 ++lqr->lqr_offset_idx;
796 ost_start_idx_temp = lqr->lqr_start_idx;
800 QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
801 "active %d count %d\n",
802 lod_comp->llc_pool ? lod_comp->llc_pool : "",
803 stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
804 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
806 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
808 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
810 for (i = 0; i < osts->op_count * stripes_per_ost
811 && stripe_idx < stripe_count; i++) {
812 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
814 ++lqr->lqr_start_idx;
815 ost_idx = lqr->lqr_pool.op_array[array_idx];
817 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
818 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
819 stripe_idx, array_idx, ost_idx);
821 if ((ost_idx == LOV_QOS_EMPTY) ||
822 !test_bit(ost_idx, m->lod_ost_bitmap))
825 /* Fail Check before osc_precreate() is called
826 so we can only 'fail' single OSC. */
827 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
830 spin_unlock(&lqr->lqr_alloc);
831 rc = lod_check_and_reserve_ost(env, lo, lod_comp, ost_idx,
832 speed, &stripe_idx, stripe,
833 ost_indices, th, &overstriped,
835 spin_lock(&lqr->lqr_alloc);
837 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
840 if ((speed < 2) && (stripe_idx < stripe_count_min)) {
841 /* Try again, allowing slower OSCs */
843 lqr->lqr_start_idx = ost_start_idx_temp;
849 spin_unlock(&lqr->lqr_alloc);
850 up_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
852 /* If there are enough OSTs, a component with overstriping requested
853 * will not actually end up overstriped. The comp should reflect this.
856 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
859 lod_comp->llc_stripe_count = stripe_idx;
860 /* at least one stripe is allocated */
863 /* nobody provided us with a single object */
872 up_read(&pool_tgt_rw_sem(pool));
873 /* put back ref got by lod_find_pool() */
874 lod_pool_putref(pool);
881 lod_qos_mdt_in_use_init(const struct lu_env *env,
882 const struct lu_tgt_descs *ltd,
883 u32 stripe_idx, u32 stripe_count,
884 const struct lu_tgt_pool *pool,
885 struct dt_object **stripes)
888 struct lu_tgt_desc *mdt;
892 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
896 /* if stripe_idx > 1, we are splitting directory, mark existing stripes
897 * in_use. Because for either split or creation, stripe 0 is local,
898 * don't mark it in use.
900 for (i = 1; i < stripe_idx; i++) {
902 for (j = 0; j < pool->op_count; j++) {
903 mdt_idx = pool->op_array[j];
905 if (!test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
908 mdt = LTD_TGT(ltd, mdt_idx);
909 if (&mdt->ltd_tgt->dd_lu_dev ==
910 stripes[i]->do_lu.lo_dev)
911 lod_qos_tgt_in_use(env, i, mdt_idx);
919 * Allocate a striping using round-robin algorithm.
921 * Allocates a new striping using round-robin algorithm. The function refreshes
922 * all the internal structures (statfs cache, array of available remote MDTs
923 * sorted with regard to MDS, etc). The number of stripes required is taken from
924 * the object (must be prepared by the caller). The caller should ensure nobody
925 * else is trying to create a striping on the object in parallel. All the
926 * internal structures (like pools, etc) are protected and no additional locking
927 * is required. The function succeeds even if a single stripe is allocated.
929 * \param[in] env execution environment for this thread
930 * \param[in] lo LOD object
931 * \param[out] stripes striping created
933 * \retval positive stripe objects allocated, including the first stripe
935 * \retval -ENOSPC if not enough MDTs are found
936 * \retval negative negated errno for other failures
938 int lod_mdt_alloc_rr(const struct lu_env *env, struct lod_object *lo,
939 struct dt_object **stripes, u32 stripe_idx,
942 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
943 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
944 struct lu_tgt_pool *pool;
945 struct lu_qos_rr *lqr;
946 struct lu_tgt_desc *mdt;
947 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
948 struct lu_fid fid = { 0 };
949 struct dt_object *dto;
950 unsigned int pool_idx;
952 u32 saved_idx = stripe_idx;
955 bool use_degraded = false;
956 int tgt_connecting = 0;
961 pool = <d->ltd_tgt_pool;
962 lqr = <d->ltd_qos.lq_rr;
963 rc = lod_qos_calc_rr(lod, ltd, pool, lqr);
967 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
972 down_read(<d->ltd_qos.lq_rw_sem);
973 spin_lock(&lqr->lqr_alloc);
974 if (--lqr->lqr_start_count <= 0) {
975 lqr->lqr_start_idx = prandom_u32_max(pool->op_count);
976 lqr->lqr_start_count =
977 (LOV_CREATE_RESEED_MIN / max(pool->op_count, 1U) +
978 LOV_CREATE_RESEED_MULT) * max(pool->op_count, 1U);
979 } else if (stripe_count - 1 >= pool->op_count ||
980 lqr->lqr_start_idx > pool->op_count) {
981 /* If we have allocated from all of the tgts, slowly
982 * precess the next start if the tgt/stripe count isn't
983 * already doing this for us. */
984 lqr->lqr_start_idx %= pool->op_count;
985 if (stripe_count - 1 > 1 &&
986 (pool->op_count % (stripe_count - 1)) != 1)
987 ++lqr->lqr_offset_idx;
989 start_mdt = lqr->lqr_start_idx;
992 QOS_DEBUG("want=%d start_idx=%d start_count=%d offset=%d active=%d count=%d\n",
993 stripe_count - 1, lqr->lqr_start_idx, lqr->lqr_start_count,
994 lqr->lqr_offset_idx, pool->op_count, pool->op_count);
996 for (i = 0; i < pool->op_count && stripe_idx < stripe_count; i++) {
997 pool_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
999 ++lqr->lqr_start_idx;
1000 mdt_idx = lqr->lqr_pool.op_array[pool_idx];
1001 mdt = LTD_TGT(ltd, mdt_idx);
1003 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1004 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1005 stripe_idx, pool_idx, mdt_idx);
1007 if (mdt_idx == LOV_QOS_EMPTY ||
1008 !test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
1011 /* do not put >1 objects on one MDT */
1012 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1015 rc = lod_is_tgt_usable(ltd, mdt);
1017 if (mdt->ltd_connecting)
1022 /* try to use another OSP if this one is degraded */
1023 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED &&
1025 QOS_DEBUG("#%d: degraded\n", mdt_idx);
1028 spin_unlock(&lqr->lqr_alloc);
1030 rc = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1032 QOS_DEBUG("#%d: alloc FID failed: %dl\n", mdt_idx, rc);
1033 spin_lock(&lqr->lqr_alloc);
1037 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1038 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1041 spin_lock(&lqr->lqr_alloc);
1043 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1044 mdt->ltd_index, (int) PTR_ERR(dto));
1046 if (mdt->ltd_connecting)
1051 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1052 stripes[stripe_idx++] = dto;
1055 if (!use_degraded && stripe_idx < stripe_count) {
1056 /* Try again, allowing slower MDTs */
1057 use_degraded = true;
1058 lqr->lqr_start_idx = start_mdt;
1063 spin_unlock(&lqr->lqr_alloc);
1064 up_read(<d->ltd_qos.lq_rw_sem);
1066 if (stripe_idx > saved_idx)
1067 /* at least one stripe is allocated */
1070 /* nobody provided us with a single object */
1072 RETURN(-EINPROGRESS);
1078 * Allocate a specific striping layout on a user defined set of OSTs.
1080 * Allocates new striping using the OST index range provided by the data from
1081 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1082 * OSTs are not considered. The exact order of OSTs requested by the user
1083 * is respected as much as possible depending on OST status. The number of
1084 * stripes needed and stripe offset are taken from the object. If that number
1085 * can not be met, then the function returns a failure and then it's the
1086 * caller's responsibility to release the stripes allocated. All the internal
1087 * structures are protected, but no concurrent allocation is allowed on the
1090 * \param[in] env execution environment for this thread
1091 * \param[in] lo LOD object
1092 * \param[out] stripe striping created
1093 * \param[out] ost_indices ost indices of striping created
1094 * \param[in] th transaction handle
1095 * \param[in] comp_idx index of ldo_comp_entries
1097 * \retval 0 on success
1098 * \retval -ENODEV OST index does not exist on file system
1099 * \retval -EINVAL requested OST index is invalid
1100 * \retval negative negated errno on error
1102 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1103 struct dt_object **stripe, __u32 *ost_indices,
1104 struct thandle *th, int comp_idx, __u64 reserve)
1106 struct lod_layout_component *lod_comp;
1107 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1108 struct dt_object *o;
1109 unsigned int array_idx = 0;
1110 int stripe_count = 0;
1115 /* for specific OSTs layout */
1116 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1117 lod_comp = &lo->ldo_comp_entries[comp_idx];
1118 LASSERT(lod_comp->llc_ostlist.op_array);
1119 LASSERT(lod_comp->llc_ostlist.op_count);
1121 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1125 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
1126 lod_comp->llc_stripe_offset =
1127 lod_comp->llc_ostlist.op_array[0];
1129 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1130 if (lod_comp->llc_ostlist.op_array[i] ==
1131 lod_comp->llc_stripe_offset) {
1136 if (i == lod_comp->llc_stripe_count) {
1138 "%s: start index %d not in the specified list of OSTs\n",
1139 lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1143 for (i = 0; i < lod_comp->llc_stripe_count;
1144 i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1145 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1147 if (!test_bit(ost_idx, m->lod_ost_bitmap)) {
1152 /* do not put >1 objects on a single OST, except for
1155 if (lod_qos_is_tgt_used(env, ost_idx, stripe_count) &&
1156 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
1161 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1162 LTD_TGT(&m->lod_ost_descs, ost_idx),
1164 if (rc < 0) /* this OSP doesn't feel well */
1167 o = lod_qos_declare_object_on(env, m, ost_idx, true, th);
1171 "%s: can't declare new object on #%u: %d\n",
1172 lod2obd(m)->obd_name, ost_idx, rc);
1177 * We've successfully declared (reserved) an object
1179 lod_qos_tgt_in_use(env, stripe_count, ost_idx);
1180 stripe[stripe_count] = o;
1181 ost_indices[stripe_count] = ost_idx;
1189 * Allocate a striping on a predefined set of OSTs.
1191 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1192 * Full OSTs are not considered. The exact order of OSTs is not important and
1193 * varies depending on OST status. The allocation procedure prefers the targets
1194 * with precreated objects ready. The number of stripes needed and stripe
1195 * offset are taken from the object. If that number cannot be met, then the
1196 * function returns an error and then it's the caller's responsibility to
1197 * release the stripes allocated. All the internal structures are protected,
1198 * but no concurrent allocation is allowed on the same objects.
1200 * \param[in] env execution environment for this thread
1201 * \param[in] lo LOD object
1202 * \param[out] stripe striping created
1203 * \param[out] ost_indices ost indices of striping created
1204 * \param[in] flags not used
1205 * \param[in] th transaction handle
1206 * \param[in] comp_idx index of ldo_comp_entries
1208 * \retval 0 on success
1209 * \retval -ENOSPC if no OST objects are available at all
1210 * \retval -EFBIG if not enough OST objects are found
1211 * \retval -EINVAL requested offset is invalid
1212 * \retval negative errno on failure
1214 static int lod_ost_alloc_specific(const struct lu_env *env,
1215 struct lod_object *lo,
1216 struct dt_object **stripe, __u32 *ost_indices,
1217 int flags, struct thandle *th, int comp_idx,
1220 struct lod_layout_component *lod_comp;
1221 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1222 struct dt_object *o;
1223 struct lu_tgt_desc *tgt;
1225 unsigned int i, array_idx, ost_count;
1226 int rc, stripe_num = 0;
1228 struct pool_desc *pool = NULL;
1229 struct lu_tgt_pool *osts;
1230 int stripes_per_ost = 1;
1231 bool overstriped = false;
1234 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1235 lod_comp = &lo->ldo_comp_entries[comp_idx];
1237 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1241 if (lod_comp->llc_pool != NULL)
1242 pool = lod_find_pool(m, lod_comp->llc_pool);
1245 down_read(&pool_tgt_rw_sem(pool));
1246 osts = &(pool->pool_obds);
1248 osts = &m->lod_ost_descs.ltd_tgt_pool;
1251 ost_count = osts->op_count;
1254 /* search loi_ost_idx in ost array */
1256 for (i = 0; i < ost_count; i++) {
1257 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1262 if (i == ost_count) {
1263 CERROR("Start index %d not found in pool '%s'\n",
1264 lod_comp->llc_stripe_offset,
1265 lod_comp->llc_pool ? lod_comp->llc_pool : "");
1266 GOTO(out, rc = -EINVAL);
1269 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1271 (lod_comp->llc_stripe_count - 1)/ost_count + 1;
1273 /* user specifies bigger stripe count than available ost count */
1274 if (lod_comp->llc_stripe_count > ost_count * stripes_per_ost)
1275 lod_comp->llc_stripe_count = ost_count * stripes_per_ost;
1277 for (i = 0; i < ost_count * stripes_per_ost;
1278 i++, array_idx = (array_idx + 1) % ost_count) {
1279 ost_idx = osts->op_array[array_idx];
1281 if (!test_bit(ost_idx, m->lod_ost_bitmap))
1284 /* Fail Check before osc_precreate() is called
1285 so we can only 'fail' single OSC. */
1286 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1290 * do not put >1 objects on a single OST, except for
1291 * overstriping, where it is intended
1293 if (lod_qos_is_tgt_used(env, ost_idx, stripe_num)) {
1294 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1301 * try not allocate on the OST used by other component
1303 if (speed == 0 && i != 0 &&
1304 lod_comp_is_ost_used(env, lo, ost_idx))
1307 tgt = LTD_TGT(&m->lod_ost_descs, ost_idx);
1309 /* Drop slow OSCs if we can, but not for requested start idx.
1311 * This means "if OSC is slow and it is not the requested
1312 * start OST, then it can be skipped, otherwise skip it only
1313 * if it is inactive/recovering/out-of-space." */
1315 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1318 /* this OSP doesn't feel well */
1323 * We expect number of precreated objects at the first
1324 * iteration. Skip OSPs with no objects ready. Don't apply
1325 * this logic to OST specified with stripe_offset.
1327 if (i && !tgt->ltd_statfs.os_fprecreated && !speed)
1330 o = lod_qos_declare_object_on(env, m, ost_idx, true, th);
1332 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1333 ost_idx, (int) PTR_ERR(o));
1338 * We've successfully declared (reserved) an object
1340 lod_qos_tgt_in_use(env, stripe_num, ost_idx);
1341 stripe[stripe_num] = o;
1342 ost_indices[stripe_num] = ost_idx;
1345 /* We have enough stripes */
1346 if (stripe_num == lod_comp->llc_stripe_count)
1350 /* Try again, allowing slower OSCs */
1355 /* If we were passed specific striping params, then a failure to
1356 * meet those requirements is an error, since we can't reallocate
1357 * that memory (it might be part of a larger array or something).
1359 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1360 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1361 lod_comp->llc_stripe_count);
1362 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1364 /* If there are enough OSTs, a component with overstriping requessted
1365 * will not actually end up overstriped. The comp should reflect this.
1367 if (rc == 0 && !overstriped)
1368 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1372 up_read(&pool_tgt_rw_sem(pool));
1373 /* put back ref got by lod_find_pool() */
1374 lod_pool_putref(pool);
1381 * Allocate a striping using an algorithm with weights.
1383 * The function allocates OST objects to create a striping. The algorithm
1384 * used is based on weights (currently only using the free space), and it's
1385 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1386 * configuration (# of stripes, offset, pool) is taken from the object and
1387 * is prepared by the caller.
1389 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1390 * be met due to too few OSTs, then allocation fails. If the flag is passed
1391 * fewer than 3/4 of the requested number of stripes can be allocated, then
1394 * No concurrent allocation is allowed on the object and this must be ensured
1395 * by the caller. All the internal structures are protected by the function.
1397 * The algorithm has two steps: find available OSTs and calculate their
1398 * weights, then select the OSTs with their weights used as the probability.
1399 * An OST with a higher weight is proportionately more likely to be selected
1400 * than one with a lower weight.
1402 * \param[in] env execution environment for this thread
1403 * \param[in] lo LOD object
1404 * \param[out] stripe striping created
1405 * \param[out] ost_indices ost indices of striping created
1406 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1407 * \param[in] th transaction handle
1408 * \param[in] comp_idx index of ldo_comp_entries
1410 * \retval 0 on success
1411 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1412 * \retval -EINVAL requested OST index is invalid
1413 * \retval negative errno on failure
1415 static int lod_ost_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1416 struct dt_object **stripe, __u32 *ost_indices,
1417 int flags, struct thandle *th, int comp_idx,
1420 struct lod_layout_component *lod_comp;
1421 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1422 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1423 struct lod_tgt_desc *ost;
1424 struct dt_object *o;
1425 __u64 total_weight = 0;
1426 struct pool_desc *pool = NULL;
1427 struct lu_tgt_pool *osts;
1429 __u32 nfound, good_osts, stripe_count, stripe_count_min;
1430 bool overstriped = false;
1431 int stripes_per_ost = 1;
1436 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1437 lod_comp = &lo->ldo_comp_entries[comp_idx];
1438 stripe_count = lod_comp->llc_stripe_count;
1439 stripe_count_min = min_stripe_count(stripe_count, flags);
1440 if (stripe_count_min < 1)
1443 if (lod_comp->llc_pool != NULL)
1444 pool = lod_find_pool(lod, lod_comp->llc_pool);
1447 down_read(&pool_tgt_rw_sem(pool));
1448 osts = &(pool->pool_obds);
1450 osts = &lod->lod_ost_descs.ltd_tgt_pool;
1453 /* Detect -EAGAIN early, before expensive lock is taken. */
1454 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1455 GOTO(out_nolock, rc = -EAGAIN);
1457 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1459 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1461 /* Do actual allocation, use write lock here. */
1462 down_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1465 * Check again, while we were sleeping on @lq_rw_sem things could
1468 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1469 GOTO(out, rc = -EAGAIN);
1471 rc = ltd_qos_penalties_calc(&lod->lod_ost_descs);
1475 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1480 /* Find all the OSTs that are valid stripe candidates */
1481 for (i = 0; i < osts->op_count; i++) {
1482 if (!test_bit(osts->op_array[i], lod->lod_ost_bitmap))
1485 ost = OST_TGT(lod, osts->op_array[i]);
1486 ost->ltd_qos.ltq_usable = 0;
1488 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs,
1491 /* this OSP doesn't feel well */
1495 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1498 /* Fail Check before osc_precreate() is called
1499 * so we can only 'fail' single OSC.
1501 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1502 osts->op_array[i] == 0)
1505 ost->ltd_qos.ltq_usable = 1;
1506 lu_tgt_qos_weight_calc(ost);
1507 total_weight += ost->ltd_qos.ltq_weight;
1512 QOS_DEBUG("found %d good osts\n", good_osts);
1514 if (good_osts < stripe_count_min)
1515 GOTO(out, rc = -EAGAIN);
1517 /* If we do not have enough OSTs for the requested stripe count, do not
1518 * put more stripes per OST than requested.
1520 if (stripe_count / stripes_per_ost > good_osts)
1521 stripe_count = good_osts * stripes_per_ost;
1523 /* Find enough OSTs with weighted random allocation. */
1525 while (nfound < stripe_count) {
1526 u64 rand, cur_weight;
1531 rand = lu_prandom_u64_max(total_weight);
1533 /* On average, this will hit larger-weighted OSTs more often.
1534 * 0-weight OSTs will always get used last (only when rand=0)
1536 for (i = 0; i < osts->op_count; i++) {
1537 __u32 idx = osts->op_array[i];
1538 struct lod_tgt_desc *ost;
1540 if (lod_should_avoid_ost(lo, lag, idx))
1543 ost = OST_TGT(lod, idx);
1545 if (!ost->ltd_qos.ltq_usable)
1548 cur_weight += ost->ltd_qos.ltq_weight;
1549 QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1550 "rand=%llu total_weight=%llu\n",
1551 stripe_count, nfound, cur_weight, rand,
1554 if (cur_weight < rand)
1557 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1559 * do not put >1 objects on a single OST, except for
1562 if ((lod_comp_is_ost_used(env, lo, idx)) &&
1563 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
1566 if (lod_qos_is_tgt_used(env, idx, nfound)) {
1567 if (lod_comp->llc_pattern &
1568 LOV_PATTERN_OVERSTRIPING)
1574 o = lod_qos_declare_object_on(env, lod, idx, slow, th);
1576 QOS_DEBUG("can't declare object on #%u: %d\n",
1577 idx, (int) PTR_ERR(o));
1581 lod_avoid_update(lo, lag);
1582 lod_qos_tgt_in_use(env, nfound, idx);
1584 ost_indices[nfound] = idx;
1585 ltd_qos_update(&lod->lod_ost_descs, ost, &total_weight);
1591 if (rc && !slow && nfound < stripe_count) {
1592 /* couldn't allocate using precreated objects
1593 * so try to wait for new precreations */
1599 /* no OST found on this iteration, give up */
1604 if (unlikely(nfound != stripe_count)) {
1606 * when the decision to use weighted algorithm was made
1607 * we had enough appropriate OSPs, but this state can
1608 * change anytime (no space on OST, broken connection, etc)
1609 * so it's possible OSP won't be able to provide us with
1610 * an object due to just changed state
1612 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1613 lod2obd(lod)->obd_name, stripe_count, nfound);
1614 for (i = 0; i < nfound; i++) {
1615 LASSERT(stripe[i] != NULL);
1616 dt_object_put(env, stripe[i]);
1620 /* makes sense to rebalance next time */
1621 set_bit(LQ_DIRTY, &lod->lod_ost_descs.ltd_qos.lq_flags);
1622 clear_bit(LQ_SAME_SPACE, &lod->lod_ost_descs.ltd_qos.lq_flags);
1626 /* If there are enough OSTs, a component with overstriping requessted
1627 * will not actually end up overstriped. The comp should reflect this.
1629 if (rc == 0 && !overstriped)
1630 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1633 up_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1637 up_read(&pool_tgt_rw_sem(pool));
1638 /* put back ref got by lod_find_pool() */
1639 lod_pool_putref(pool);
1646 * Allocate a striping using an algorithm with weights.
1648 * The function allocates remote MDT objects to create a striping, the first
1649 * object was already allocated on current MDT to ensure master object and
1650 * the first object are on the same MDT. The algorithm used is based on weights
1651 * (both free space and inodes), and it's trying to ensure the space/inodes are
1652 * used evenly by MDTs and MDSs. The striping configuration (# of stripes,
1653 * offset, pool) is taken from the object and is prepared by the caller.
1655 * If prepared configuration can't be met due to too few MDTs, then allocation
1658 * No concurrent allocation is allowed on the object and this must be ensured
1659 * by the caller. All the internal structures are protected by the function.
1661 * The algorithm has two steps: find available MDTs and calculate their
1662 * weights, then select the MDTs with their weights used as the probability.
1663 * An MDT with a higher weight is proportionately more likely to be selected
1664 * than one with a lower weight.
1666 * \param[in] env execution environment for this thread
1667 * \param[in] lo LOD object
1668 * \param[in] stripe_idx starting stripe index to allocate, if it's not
1669 * 0, we are restriping directory
1670 * \param[in] stripe_count total stripe count
1671 * \param[out] stripes striping created
1673 * \retval positive stripes allocated, and it should be equal to
1674 * lo->ldo_dir_stripe_count
1675 * \retval -EAGAIN not enough tgts are found for specified stripe count
1676 * \retval -EINVAL requested MDT index is invalid
1677 * \retval negative errno on failure
1679 int lod_mdt_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1680 struct dt_object **stripes, u32 stripe_idx,
1683 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1684 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
1685 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1686 struct lu_fid fid = { 0 };
1687 const struct lu_tgt_pool *pool;
1688 struct lu_tgt_desc *mdt;
1689 struct dt_object *dto;
1690 u64 total_weight = 0;
1691 u32 saved_idx = stripe_idx;
1693 unsigned int good_mdts;
1699 LASSERT(stripe_idx <= stripe_count);
1700 if (stripe_idx == stripe_count)
1701 RETURN(stripe_count);
1703 /* use MDT pool in @ltd, once MDT pool is supported in the future, it
1704 * can be passed in as argument like OST object allocation.
1706 pool = <d->ltd_tgt_pool;
1708 /* Detect -EAGAIN early, before expensive lock is taken. */
1709 if (!ltd_qos_is_usable(ltd))
1712 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
1717 /* Do actual allocation, use write lock here. */
1718 down_write(<d->ltd_qos.lq_rw_sem);
1721 * Check again, while we were sleeping on @lq_rw_sem things could
1724 if (!ltd_qos_is_usable(ltd))
1725 GOTO(unlock, rc = -EAGAIN);
1727 rc = ltd_qos_penalties_calc(ltd);
1732 /* Find all the MDTs that are valid stripe candidates */
1733 for (i = 0; i < pool->op_count; i++) {
1734 if (!test_bit(pool->op_array[i], ltd->ltd_tgt_bitmap))
1737 mdt = LTD_TGT(ltd, pool->op_array[i]);
1738 mdt->ltd_qos.ltq_usable = 0;
1740 rc = lod_is_tgt_usable(ltd, mdt);
1744 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1747 mdt->ltd_qos.ltq_usable = 1;
1748 lu_tgt_qos_weight_calc(mdt);
1749 total_weight += mdt->ltd_qos.ltq_weight;
1754 QOS_DEBUG("found %d good MDTs\n", good_mdts);
1756 if (good_mdts < stripe_count - stripe_idx)
1757 GOTO(unlock, rc = -EAGAIN);
1759 /* Find enough MDTs with weighted random allocation. */
1760 while (stripe_idx < stripe_count) {
1761 u64 rand, cur_weight;
1766 rand = lu_prandom_u64_max(total_weight);
1768 /* On average, this will hit larger-weighted MDTs more often.
1769 * 0-weight MDT will always get used last (only when rand=0) */
1770 for (i = 0; i < pool->op_count; i++) {
1773 mdt_idx = pool->op_array[i];
1774 mdt = LTD_TGT(ltd, mdt_idx);
1776 if (!mdt->ltd_qos.ltq_usable)
1779 cur_weight += mdt->ltd_qos.ltq_weight;
1781 QOS_DEBUG("stripe_count=%d stripe_index=%d cur_weight=%llu rand=%llu total_weight=%llu\n",
1782 stripe_count, stripe_idx, cur_weight, rand,
1785 if (cur_weight < rand)
1788 QOS_DEBUG("stripe=%d to idx=%d\n",
1789 stripe_idx, mdt_idx);
1791 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1794 rc2 = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1796 QOS_DEBUG("can't alloc FID on #%u: %d\n",
1801 conf.loc_flags = LOC_F_NEW;
1802 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1803 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1806 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1807 mdt_idx, (int) PTR_ERR(dto));
1811 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1812 stripes[stripe_idx] = dto;
1813 ltd_qos_update(ltd, mdt, &total_weight);
1819 /* no MDT found on this iteration, give up */
1824 if (unlikely(stripe_idx != stripe_count)) {
1826 * when the decision to use weighted algorithm was made
1827 * we had enough appropriate OSPs, but this state can
1828 * change anytime (no space on MDT, broken connection, etc)
1829 * so it's possible OSP won't be able to provide us with
1830 * an object due to just changed state
1832 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1833 lod2obd(lod)->obd_name, stripe_count, stripe_idx);
1834 for (i = saved_idx; i < stripe_idx; i++) {
1835 LASSERT(stripes[i] != NULL);
1836 dt_object_put(env, stripes[i]);
1840 /* makes sense to rebalance next time */
1841 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
1842 clear_bit(LQ_SAME_SPACE, <d->ltd_qos.lq_flags);
1850 up_write(<d->ltd_qos.lq_rw_sem);
1856 * Check stripe count the caller can use.
1858 * For new layouts (no initialized components), check the total size of the
1859 * layout against the maximum EA size from the backing file system. This
1860 * stops us from creating a layout which will be too large once initialized.
1862 * For existing layouts (with initialized components):
1863 * Find the maximal possible stripe count not greater than \a stripe_count.
1864 * If the provided stripe count is 0, then the filesystem's default is used.
1866 * \param[in] lod LOD device
1867 * \param[in] lo The lod_object
1868 * \param[in] comp_idx The component id, which the amount of stripes is
1870 * \param[in] stripe_count count the caller would like to use
1872 * \retval the maximum usable stripe count
1874 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
1875 int comp_idx, __u16 stripe_count, bool overstriping)
1877 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1878 /* max stripe count is based on OSD ea size */
1879 unsigned int easize = lod->lod_osd_max_easize;
1883 if (stripe_count == (__u16)(-1) && lod->lod_max_stripecount)
1884 stripe_count = lod->lod_max_stripecount;
1887 lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_count;
1890 /* Overstriping allows more stripes than targets */
1892 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count &&
1895 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count;
1897 if (lo->ldo_is_composite) {
1898 struct lod_layout_component *lod_comp;
1899 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
1900 unsigned int init_comp_sz = 0;
1901 unsigned int total_comp_sz = 0;
1902 unsigned int comp_sz;
1904 header_sz += sizeof(struct lov_comp_md_entry_v1) *
1907 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1908 unsigned int stripes;
1913 lod_comp = &lo->ldo_comp_entries[i];
1914 /* Extension comp is never inited - 0 stripes on disk */
1915 stripes = lod_comp->llc_flags & LCME_FL_EXTENSION ? 0 :
1916 lod_comp->llc_stripe_count;
1918 comp_sz = lov_mds_md_size(stripes, LOV_MAGIC_V3);
1919 total_comp_sz += comp_sz;
1920 if (lod_comp->llc_flags & LCME_FL_INIT)
1921 init_comp_sz += comp_sz;
1924 if (init_comp_sz > 0)
1925 total_comp_sz = init_comp_sz;
1927 header_sz += total_comp_sz;
1929 if (easize > header_sz)
1930 easize -= header_sz;
1935 max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
1936 max_stripes = (max_stripes == 0) ? 0 : max_stripes - 1;
1938 stripe_count = min_t(__u16, stripe_count, max_stripes);
1939 RETURN(stripe_count);
1943 * Create in-core respresentation for a fully-defined striping
1945 * When the caller passes a fully-defined striping (i.e. everything including
1946 * OST object FIDs are defined), then we still need to instantiate LU-cache
1947 * with the objects representing the stripes defined. This function completes
1950 * \param[in] env execution environment for this thread
1951 * \param[in] mo LOD object
1952 * \param[in] buf buffer containing the striping
1954 * \retval 0 on success
1955 * \retval negative negated errno on error
1957 int lod_use_defined_striping(const struct lu_env *env,
1958 struct lod_object *mo,
1959 const struct lu_buf *buf)
1961 struct lod_layout_component *lod_comp;
1962 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1963 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1964 struct lov_comp_md_v1 *comp_v1 = NULL;
1965 struct lov_ost_data_v1 *objs;
1972 mutex_lock(&mo->ldo_layout_mutex);
1973 lod_striping_free_nolock(env, mo);
1975 magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
1977 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
1978 magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
1979 GOTO(unlock, rc = -EINVAL);
1981 if (magic == LOV_MAGIC_COMP_V1) {
1982 comp_v1 = buf->lb_buf;
1983 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1985 GOTO(unlock, rc = -EINVAL);
1986 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1987 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
1989 mo->ldo_is_composite = 1;
1990 } else if (magic == LOV_MAGIC_FOREIGN) {
1991 struct lov_foreign_md *foreign;
1994 if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
1996 "buf len %zu < min lov_foreign_md size (%zu)\n",
1998 offsetof(typeof(*foreign), lfm_value));
1999 GOTO(out, rc = -EINVAL);
2001 foreign = (struct lov_foreign_md *)buf->lb_buf;
2002 length = foreign_size_le(foreign);
2003 if (buf->lb_len < length) {
2005 "buf len %zu < this lov_foreign_md size (%zu)\n",
2006 buf->lb_len, length);
2007 GOTO(out, rc = -EINVAL);
2010 /* just cache foreign LOV EA raw */
2011 rc = lod_alloc_foreign_lov(mo, length);
2014 memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
2017 mo->ldo_is_composite = 0;
2021 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
2023 rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
2027 for (i = 0; i < comp_cnt; i++) {
2028 struct lu_extent *ext;
2032 lod_comp = &mo->ldo_comp_entries[i];
2034 if (mo->ldo_is_composite) {
2035 offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
2036 v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
2037 v3 = (struct lov_mds_md_v3 *)v1;
2038 magic = le32_to_cpu(v1->lmm_magic);
2040 ext = &comp_v1->lcm_entries[i].lcme_extent;
2041 lod_comp->llc_extent.e_start =
2042 le64_to_cpu(ext->e_start);
2043 lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
2044 lod_comp->llc_flags =
2045 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
2046 if (lod_comp->llc_flags & LCME_FL_NOSYNC)
2047 lod_comp->llc_timestamp = le64_to_cpu(
2048 comp_v1->lcm_entries[i].lcme_timestamp);
2050 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
2051 if (lod_comp->llc_id == LCME_ID_INVAL)
2052 GOTO(out, rc = -EINVAL);
2056 if (magic == LOV_MAGIC_V1) {
2057 objs = &v1->lmm_objects[0];
2058 } else if (magic == LOV_MAGIC_V3) {
2059 objs = &v3->lmm_objects[0];
2060 if (v3->lmm_pool_name[0] != '\0')
2061 pool_name = v3->lmm_pool_name;
2063 CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
2064 GOTO(out, rc = -EINVAL);
2067 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
2068 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
2069 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
2070 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
2072 * The stripe_offset of an uninit-ed component is stored in
2073 * the lmm_layout_gen
2075 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
2076 lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
2077 lod_obj_set_pool(mo, i, pool_name);
2079 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
2080 !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
2081 !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
2082 rc = lod_initialize_objects(env, mo, objs, i);
2088 rc = lod_fill_mirrors(mo);
2092 lod_striping_free_nolock(env, mo);
2094 mutex_unlock(&mo->ldo_layout_mutex);
2100 * Parse suggested striping configuration.
2102 * The caller gets a suggested striping configuration from a number of sources
2103 * including per-directory default and applications. Then it needs to verify
2104 * the suggested striping is valid, apply missing bits and store the resulting
2105 * configuration in the object to be used by the allocator later. Must not be
2106 * called concurrently against the same object. It's OK to provide a
2107 * fully-defined striping.
2109 * \param[in] env execution environment for this thread
2110 * \param[in] lo LOD object
2111 * \param[in] buf buffer containing the striping
2113 * \retval 0 on success
2114 * \retval negative negated errno on error
2116 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
2117 const struct lu_buf *buf)
2119 struct lod_layout_component *lod_comp;
2120 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2121 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2122 struct lov_user_md_v1 *v1 = NULL;
2123 struct lov_user_md_v3 *v3 = NULL;
2124 struct lov_comp_md_v1 *comp_v1 = NULL;
2125 struct lov_foreign_md *lfm = NULL;
2126 char def_pool[LOV_MAXPOOLNAME + 1];
2133 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
2136 memset(def_pool, 0, sizeof(def_pool));
2137 if (lo->ldo_comp_entries != NULL)
2138 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
2139 def_pool, sizeof(def_pool));
2141 /* free default striping info */
2142 if (lo->ldo_is_foreign)
2143 lod_free_foreign_lov(lo);
2145 lod_free_comp_entries(lo);
2147 rc = lod_verify_striping(env, d, lo, buf, false);
2153 comp_v1 = buf->lb_buf;
2154 /* {lmm,lfm}_magic position/length work for all LOV formats */
2155 magic = v1->lmm_magic;
2157 if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
2158 /* try to use as fully defined striping */
2159 rc = lod_use_defined_striping(env, lo, buf);
2164 case __swab32(LOV_USER_MAGIC_V1):
2165 lustre_swab_lov_user_md_v1(v1);
2166 magic = v1->lmm_magic;
2168 case LOV_USER_MAGIC_V1:
2170 case __swab32(LOV_USER_MAGIC_V3):
2171 lustre_swab_lov_user_md_v3(v3);
2172 magic = v3->lmm_magic;
2174 case LOV_USER_MAGIC_V3:
2176 case __swab32(LOV_USER_MAGIC_SPECIFIC):
2177 lustre_swab_lov_user_md_v3(v3);
2178 lustre_swab_lov_user_md_objects(v3->lmm_objects,
2179 v3->lmm_stripe_count);
2180 magic = v3->lmm_magic;
2182 case LOV_USER_MAGIC_SPECIFIC:
2184 case __swab32(LOV_USER_MAGIC_COMP_V1):
2185 lustre_swab_lov_comp_md_v1(comp_v1);
2186 magic = comp_v1->lcm_magic;
2188 case LOV_USER_MAGIC_COMP_V1:
2190 case __swab32(LOV_USER_MAGIC_FOREIGN):
2192 __swab32s(&lfm->lfm_magic);
2193 __swab32s(&lfm->lfm_length);
2194 __swab32s(&lfm->lfm_type);
2195 __swab32s(&lfm->lfm_flags);
2196 magic = lfm->lfm_magic;
2198 case LOV_USER_MAGIC_FOREIGN:
2201 rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
2204 memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
2207 CERROR("%s: unrecognized magic %X\n",
2208 lod2obd(d)->obd_name, magic);
2212 lustre_print_user_md(D_OTHER, v1, "parse config");
2214 if (magic == LOV_USER_MAGIC_COMP_V1) {
2215 comp_cnt = comp_v1->lcm_entry_count;
2218 mirror_cnt = comp_v1->lcm_mirror_count + 1;
2220 lo->ldo_flr_state = LCM_FL_RDONLY;
2221 lo->ldo_is_composite = 1;
2225 lo->ldo_is_composite = 0;
2228 rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2232 LASSERT(lo->ldo_comp_entries);
2234 for (i = 0; i < comp_cnt; i++) {
2235 struct pool_desc *pool;
2236 struct lu_extent *ext;
2239 lod_comp = &lo->ldo_comp_entries[i];
2241 if (lo->ldo_is_composite) {
2242 v1 = (struct lov_user_md *)((char *)comp_v1 +
2243 comp_v1->lcm_entries[i].lcme_offset);
2244 ext = &comp_v1->lcm_entries[i].lcme_extent;
2245 lod_comp->llc_extent = *ext;
2246 lod_comp->llc_flags =
2247 comp_v1->lcm_entries[i].lcme_flags &
2252 if (def_pool[0] != '\0')
2253 pool_name = def_pool;
2255 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2256 v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2257 v3 = (struct lov_user_md_v3 *)v1;
2259 if (v3->lmm_pool_name[0] != '\0')
2260 pool_name = v3->lmm_pool_name;
2262 if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2263 rc = lod_comp_copy_ost_lists(lod_comp, v3);
2265 GOTO(free_comp, rc);
2271 if (v1->lmm_pattern == 0)
2272 v1->lmm_pattern = LOV_PATTERN_RAID0;
2273 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2274 lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
2275 lov_pattern(v1->lmm_pattern) !=
2276 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
2277 CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2278 lod2obd(d)->obd_name, v1->lmm_pattern);
2279 GOTO(free_comp, rc = -EINVAL);
2282 lod_comp->llc_pattern = v1->lmm_pattern;
2283 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2284 lod_adjust_stripe_size(lod_comp, desc->ld_default_stripe_size);
2286 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2287 if (v1->lmm_stripe_count ||
2288 lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2289 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2291 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT &&
2292 lod_comp->llc_stripe_count != 0) {
2293 CDEBUG(D_LAYOUT, "%s: invalid stripe count: %u\n",
2294 lod2obd(d)->obd_name,
2295 lod_comp->llc_stripe_count);
2296 GOTO(free_comp, rc = -EINVAL);
2299 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2300 lod_obj_set_pool(lo, i, pool_name);
2302 if (pool_name == NULL)
2305 /* In the function below, .hs_keycmp resolves to
2306 * pool_hashkey_keycmp() */
2307 /* coverity[overrun-buffer-val] */
2308 pool = lod_find_pool(d, pool_name);
2312 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2313 rc = lod_check_index_in_pool(
2314 lod_comp->llc_stripe_offset, pool);
2316 lod_pool_putref(pool);
2317 CDEBUG(D_LAYOUT, "%s: invalid offset, %u\n",
2318 lod2obd(d)->obd_name,
2319 lod_comp->llc_stripe_offset);
2320 GOTO(free_comp, rc = -EINVAL);
2324 if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
2325 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
2326 lod_comp->llc_stripe_count = pool_tgt_count(pool);
2328 lod_pool_putref(pool);
2334 lod_free_comp_entries(lo);
2339 * prepare enough OST avoidance bitmap space
2341 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2343 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2344 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2345 unsigned long *bitmap = NULL;
2346 __u32 *new_oss = NULL;
2348 lag->lag_ost_avail = lod->lod_ost_count;
2350 /* reset OSS avoid guide array */
2351 lag->lag_oaa_count = 0;
2352 if (lag->lag_oss_avoid_array &&
2353 lag->lag_oaa_size < lod->lod_ost_count) {
2354 OBD_FREE_PTR_ARRAY(lag->lag_oss_avoid_array, lag->lag_oaa_size);
2355 lag->lag_oss_avoid_array = NULL;
2356 lag->lag_oaa_size = 0;
2359 /* init OST avoid guide bitmap */
2360 if (lag->lag_ost_avoid_bitmap) {
2361 if (lod->lod_ost_count <= lag->lag_ost_avoid_size) {
2362 bitmap_zero(lag->lag_ost_avoid_bitmap,
2363 lag->lag_ost_avoid_size);
2365 bitmap_free(lag->lag_ost_avoid_bitmap);
2366 lag->lag_ost_avoid_bitmap = NULL;
2370 if (!lag->lag_ost_avoid_bitmap) {
2371 bitmap = bitmap_zalloc(lod->lod_ost_count, GFP_KERNEL);
2376 if (!lag->lag_oss_avoid_array) {
2378 * usually there are multiple OSTs in one OSS, but we don't
2379 * know the exact OSS number, so we choose a safe option,
2380 * using OST count to allocate the array to store the OSS
2383 OBD_ALLOC_PTR_ARRAY(new_oss, lod->lod_ost_count);
2385 bitmap_free(bitmap);
2391 lag->lag_oss_avoid_array = new_oss;
2392 lag->lag_oaa_size = lod->lod_ost_count;
2395 lag->lag_ost_avoid_bitmap = bitmap;
2396 lag->lag_ost_avoid_size = lod->lod_ost_count;
2403 * Collect information of used OSTs and OSSs in the overlapped components
2406 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2409 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2410 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2411 unsigned long *bitmap = lag->lag_ost_avoid_bitmap;
2414 /* iterate mirrors */
2415 for (i = 0; i < lo->ldo_mirror_count; i++) {
2416 struct lod_layout_component *comp;
2419 * skip mirror containing component[comp_idx], we only
2420 * collect OSTs info of conflicting component in other mirrors,
2421 * so that during read, if OSTs of a mirror's component are
2422 * not available, we still have other mirror with different
2423 * OSTs to read the data.
2425 comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
2426 if (comp->llc_id != LCME_ID_INVAL &&
2427 mirror_id_of(comp->llc_id) ==
2428 mirror_id_of(lod_comp->llc_id))
2431 /* iterate components of a mirror */
2432 lod_foreach_mirror_comp(comp, lo, i) {
2434 * skip non-overlapped or un-instantiated components,
2435 * NOTE: don't use lod_comp_inited(comp) to judge
2436 * whether @comp has been inited, since during
2437 * declare phase, comp->llc_stripe has been allocated
2438 * while it's init flag not been set until the exec
2441 if (!lu_extent_is_overlapped(&comp->llc_extent,
2442 &lod_comp->llc_extent) ||
2447 * collect used OSTs index and OSS info from a
2450 for (j = 0; j < comp->llc_stripe_count; j++) {
2451 struct lod_tgt_desc *ost;
2452 struct lu_svr_qos *lsq;
2455 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2456 lsq = ost->ltd_qos.ltq_svr;
2458 if (test_bit(ost->ltd_index, bitmap))
2461 QOS_DEBUG("OST%d used in conflicting mirror "
2462 "component\n", ost->ltd_index);
2463 set_bit(ost->ltd_index, bitmap);
2464 lag->lag_ost_avail--;
2466 for (k = 0; k < lag->lag_oaa_count; k++) {
2467 if (lag->lag_oss_avoid_array[k] ==
2471 if (k == lag->lag_oaa_count) {
2472 lag->lag_oss_avoid_array[k] =
2474 lag->lag_oaa_count++;
2482 * Create a striping for an obejct.
2484 * The function creates a new striping for the object. The function tries QoS
2485 * algorithm first unless free space is distributed evenly among OSTs, but
2486 * by default RR algorithm is preferred due to internal concurrency (QoS is
2487 * serialized). The caller must ensure no concurrent calls to the function
2488 * are made against the same object.
2490 * \param[in] env execution environment for this thread
2491 * \param[in] lo LOD object
2492 * \param[in] attr attributes OST objects will be declared with
2493 * \param[in] th transaction handle
2494 * \param[in] comp_idx index of ldo_comp_entries
2496 * \retval 0 on success
2497 * \retval negative negated errno on error
2499 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2500 struct lu_attr *attr, struct thandle *th,
2501 int comp_idx, __u64 reserve)
2503 struct lod_layout_component *lod_comp;
2504 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2506 int flag = LOV_USES_ASSIGNED_STRIPE;
2508 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2509 struct dt_object **stripe = NULL;
2510 __u32 *ost_indices = NULL;
2514 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2515 lod_comp = &lo->ldo_comp_entries[comp_idx];
2516 LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
2518 /* A released component is being created */
2519 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2522 /* A Data-on-MDT component is being created */
2523 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2526 if (lod_comp->llc_pool)
2527 lod_check_and_spill_pool(env, d, &lod_comp->llc_pool);
2529 if (likely(lod_comp->llc_stripe == NULL)) {
2531 * no striping has been created so far
2533 LASSERT(lod_comp->llc_stripe_count);
2535 * statfs and check OST targets now, since ld_active_tgt_count
2536 * could be changed if some OSTs are [de]activated manually.
2538 lod_qos_statfs_update(env, d, &d->lod_ost_descs);
2539 stripe_len = lod_get_stripe_count(d, lo, comp_idx,
2540 lod_comp->llc_stripe_count,
2541 lod_comp->llc_pattern &
2542 LOV_PATTERN_OVERSTRIPING);
2544 if (stripe_len == 0)
2545 GOTO(out, rc = -ERANGE);
2546 lod_comp->llc_stripe_count = stripe_len;
2547 OBD_ALLOC_PTR_ARRAY(stripe, stripe_len);
2549 GOTO(out, rc = -ENOMEM);
2550 OBD_ALLOC_PTR_ARRAY(ost_indices, stripe_len);
2552 GOTO(out, rc = -ENOMEM);
2555 lod_getref(&d->lod_ost_descs);
2556 /* XXX: support for non-0 files w/o objects */
2557 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2558 d->lod_ost_count, stripe_len);
2560 if (lod_comp->llc_ostlist.op_array &&
2561 lod_comp->llc_ostlist.op_count) {
2562 rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2563 th, comp_idx, reserve);
2564 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2566 * collect OSTs and OSSs used in other mirrors whose
2567 * components cross the ldo_comp_entries[comp_idx]
2569 rc = lod_prepare_avoidance(env, lo);
2573 QOS_DEBUG("collecting conflict osts for comp[%d]\n",
2575 lod_collect_avoidance(lo, lag, comp_idx);
2577 rc = lod_ost_alloc_qos(env, lo, stripe, ost_indices,
2578 flag, th, comp_idx, reserve);
2580 rc = lod_ost_alloc_rr(env, lo, stripe,
2581 ost_indices, flag, th,
2584 rc = lod_ost_alloc_specific(env, lo, stripe,
2585 ost_indices, flag, th,
2589 lod_putref(d, &d->lod_ost_descs);
2591 for (i = 0; i < stripe_len; i++)
2592 if (stripe[i] != NULL)
2593 dt_object_put(env, stripe[i]);
2595 /* In case there is no space on any OST, let's ignore
2596 * the @reserve space to avoid an error at the init
2597 * time, probably the actual IO will be less than the
2598 * given @reserve space (aka extension_size). */
2603 lod_comp->llc_stripe_count = 0;
2605 lod_comp->llc_stripe = stripe;
2606 lod_comp->llc_ost_indices = ost_indices;
2607 lod_comp->llc_stripes_allocated = stripe_len;
2611 * lod_qos_parse_config() found supplied buf as a predefined
2612 * striping (not a hint), so it allocated all the object
2613 * now we need to create them
2615 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2616 struct dt_object *o;
2618 o = lod_comp->llc_stripe[i];
2621 rc = lod_sub_declare_create(env, o, attr, NULL,
2624 CERROR("can't declare create: %d\n", rc);
2629 * Clear LCME_FL_INIT for the component so that
2630 * lod_striping_create() can create the striping objects
2633 lod_comp_unset_init(lod_comp);
2639 OBD_FREE_PTR_ARRAY(stripe, stripe_len);
2641 OBD_FREE_PTR_ARRAY(ost_indices, stripe_len);
2646 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2647 struct lu_attr *attr, const struct lu_buf *buf,
2651 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2659 /* no OST available */
2660 /* XXX: should we be waiting a bit to prevent failures during
2661 * cluster initialization? */
2662 if (!d->lod_ost_count)
2666 * by this time, the object's ldo_stripe_count and ldo_stripe_size
2667 * contain default value for striping: taken from the parent
2668 * or from filesystem defaults
2670 * in case the caller is passing lovea with new striping config,
2671 * we may need to parse lovea and apply new configuration
2673 rc = lod_qos_parse_config(env, lo, buf);
2677 if (attr->la_valid & LA_SIZE)
2678 size = attr->la_size;
2681 * prepare OST object creation for the component covering file's
2682 * size, the 1st component (including plain layout file) is always
2685 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2686 struct lod_layout_component *lod_comp;
2687 struct lu_extent *extent;
2689 lod_comp = &lo->ldo_comp_entries[i];
2690 extent = &lod_comp->llc_extent;
2691 QOS_DEBUG("comp[%d] %lld "DEXT"\n", i, size, PEXT(extent));
2692 if (!lo->ldo_is_composite || size >= extent->e_start) {
2693 rc = lod_qos_prep_create(env, lo, attr, th, i, 0);