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 CDEBUG(D_CONFIG, "%s: turns inactive\n",
141 tgt->ltd_exp->exp_obd->obd_name);
143 spin_unlock(&d->lod_lock);
144 } else if (rc == 0 && tgt->ltd_active == 0) {
146 LASSERTF(desc->ld_active_tgt_count < desc->ld_tgt_count,
147 "active tgt count %d, tgt nr %d\n",
148 desc->ld_active_tgt_count, desc->ld_tgt_count);
149 spin_lock(&d->lod_lock);
150 if (tgt->ltd_active == 0) {
152 tgt->ltd_connecting = 0;
153 desc->ld_active_tgt_count++;
154 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
155 CDEBUG(D_CONFIG, "%s: turns active\n",
156 tgt->ltd_exp->exp_obd->obd_name);
158 spin_unlock(&d->lod_lock);
160 if (rc == -ENOTCONN) {
161 /* In case that the ENOTCONN for inactive OST state is
162 * mistreated as MDT disconnection state by the client,
163 * this error should be changed to someone else.
171 static int lod_is_tgt_usable(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
175 rc = lod_statfs_check(ltd, tgt);
179 if (!tgt->ltd_active)
186 * Maintain per-target statfs data.
188 * The function refreshes statfs data for all the targets every N seconds.
189 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
192 * \param[in] env execution environment for this thread
193 * \param[in] lod LOD device
194 * \param[in] ltd tgt table
196 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod,
197 struct lu_tgt_descs *ltd)
199 struct obd_device *obd = lod2obd(lod);
200 struct lu_tgt_desc *tgt;
205 max_age = ktime_get_seconds() - 2 * ltd->ltd_lov_desc.ld_qos_maxage;
207 if (obd->obd_osfs_age > max_age)
208 /* statfs data are quite recent, don't need to refresh it */
211 if (test_and_set_bit(LQ_SF_PROGRESS, <d->ltd_qos.lq_flags))
214 if (obd->obd_osfs_age > max_age) {
215 /* statfs data are quite recent, don't need to refresh it */
216 clear_bit(LQ_SF_PROGRESS, <d->ltd_qos.lq_flags);
220 ltd_foreach_tgt(ltd, tgt) {
221 avail = tgt->ltd_statfs.os_bavail;
222 if (lod_statfs_and_check(env, lod, ltd, tgt, 0))
225 if (tgt->ltd_statfs.os_bavail != avail)
226 /* recalculate weigths */
227 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
229 lod_putref(lod, ltd);
230 obd->obd_osfs_age = ktime_get_seconds();
232 clear_bit(LQ_SF_PROGRESS, <d->ltd_qos.lq_flags);
236 #define LOV_QOS_EMPTY ((__u32)-1)
239 * Calculate optimal round-robin order with regard to OSSes.
241 * Place all the OSTs from pool \a src_pool in a special array to be used for
242 * round-robin (RR) stripe allocation. The placement algorithm interleaves
243 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
244 * Resorts the targets when the number of active targets changes (because of
245 * a new target or activation/deactivation).
247 * \param[in] lod LOD device
248 * \param[in] ltd tgt table
249 * \param[in] src_pool tgt pool
250 * \param[in] lqr round-robin list
252 * \retval 0 on success
253 * \retval -ENOMEM fails to allocate the array
255 static int lod_qos_calc_rr(struct lod_device *lod, struct lu_tgt_descs *ltd,
256 const struct lu_tgt_pool *src_pool,
257 struct lu_qos_rr *lqr)
259 struct lu_svr_qos *svr;
260 struct lu_tgt_desc *tgt;
261 unsigned placed, real_count;
266 if (!test_bit(LQ_DIRTY, &lqr->lqr_flags)) {
267 LASSERT(lqr->lqr_pool.op_size);
271 /* Do actual allocation. */
272 down_write(<d->ltd_qos.lq_rw_sem);
275 * Check again. While we were sleeping on @lq_rw_sem something could
278 if (!test_bit(LQ_DIRTY, &lqr->lqr_flags)) {
279 LASSERT(lqr->lqr_pool.op_size);
280 up_write(<d->ltd_qos.lq_rw_sem);
284 real_count = src_pool->op_count;
286 /* Zero the pool array */
287 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
288 deleting from the pool. The lq_rw_sem insures that nobody else
290 lqr->lqr_pool.op_count = real_count;
291 rc = lu_tgt_pool_extend(&lqr->lqr_pool, real_count);
293 up_write(<d->ltd_qos.lq_rw_sem);
296 for (i = 0; i < lqr->lqr_pool.op_count; i++)
297 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
299 /* Place all the tgts from 1 svr at the same time. */
301 list_for_each_entry(svr, <d->ltd_qos.lq_svr_list, lsq_svr_list) {
304 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
307 if (!test_bit(src_pool->op_array[i],
308 ltd->ltd_tgt_bitmap))
311 tgt = LTD_TGT(ltd, src_pool->op_array[i]);
312 LASSERT(tgt && tgt->ltd_tgt);
313 if (tgt->ltd_qos.ltq_svr != svr)
316 /* Evenly space these tgts across arrayspace */
317 next = j * lqr->lqr_pool.op_count / svr->lsq_tgt_count;
318 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
319 next = (next + 1) % lqr->lqr_pool.op_count;
321 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
327 clear_bit(LQ_DIRTY, &lqr->lqr_flags);
328 up_write(<d->ltd_qos.lq_rw_sem);
330 if (placed != real_count) {
331 /* This should never happen */
332 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all tgts in the "
333 "round-robin list (%d of %d).\n",
335 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
336 LCONSOLE(D_WARNING, "rr #%d tgt idx=%d\n", i,
337 lqr->lqr_pool.op_array[i]);
339 set_bit(LQ_DIRTY, &lqr->lqr_flags);
344 for (i = 0; i < lqr->lqr_pool.op_count; i++)
345 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
352 * Instantiate and declare creation of a new object.
354 * The function instantiates LU representation for a new object on the
355 * specified device. Also it declares an intention to create that
356 * object on the storage target.
358 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
359 * infrastructure - in the existing precreation framework we can't assign FID
360 * at this moment, we do this later once a transaction is started. So the
361 * special method instantiates FID-less object in the cache and later it
362 * will get a FID and proper placement in LU cache.
364 * \param[in] env execution environment for this thread
365 * \param[in] d LOD device
366 * \param[in] ost_idx OST target index where the object is being created
367 * \param[in] th transaction handle
369 * \retval object ptr on success, ERR_PTR() otherwise
371 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
372 struct lod_device *d,
377 struct dt_allocation_hint *ah = &lod_env_info(env)->lti_ah;
378 struct lod_tgt_desc *ost;
379 struct lu_object *o, *n;
380 struct lu_device *nd;
381 struct dt_object *dt;
386 LASSERT(ost_idx < d->lod_ost_descs.ltd_tgts_size);
387 ost = OST_TGT(d,ost_idx);
389 LASSERT(ost->ltd_tgt);
391 nd = &ost->ltd_tgt->dd_lu_dev;
394 * allocate anonymous object with zero fid, real fid
395 * will be assigned by OSP within transaction
396 * XXX: to be fixed with fully-functional OST fids
398 o = lu_object_anon(env, nd, NULL);
400 GOTO(out, dt = ERR_CAST(o));
402 n = lu_object_locate(o->lo_header, nd->ld_type);
403 if (unlikely(n == NULL)) {
404 CERROR("can't find slice\n");
405 lu_object_put(env, o);
406 GOTO(out, dt = ERR_PTR(-EINVAL));
409 dt = container_of(n, struct dt_object, do_lu);
411 ah->dah_can_block = can_block;
412 rc = lod_sub_declare_create(env, dt, NULL, ah, NULL, th);
414 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
416 lu_object_put(env, o);
425 * Calculate a minimum acceptable stripe count.
427 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
428 * all stripes or 3/4 of stripes.
430 * \param[in] stripe_count number of stripes requested
431 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
433 * \retval acceptable stripecount
435 static int min_stripe_count(__u32 stripe_count, int flags)
437 return (flags & LOV_USES_DEFAULT_STRIPE ?
438 stripe_count - (stripe_count / 4) : stripe_count);
441 #define LOV_CREATE_RESEED_MULT 30
442 #define LOV_CREATE_RESEED_MIN 2000
445 * Initialize temporary tgt-in-use array.
447 * Allocate or extend the array used to mark targets already assigned to a new
448 * striping so they are not used more than once.
450 * \param[in] env execution environment for this thread
451 * \param[in] stripes number of items needed in the array
453 * \retval 0 on success
454 * \retval -ENOMEM on error
456 static inline int lod_qos_tgt_in_use_clear(const struct lu_env *env,
459 struct lod_thread_info *info = lod_env_info(env);
461 if (info->lti_ea_store_size < sizeof(int) * stripes)
462 lod_ea_store_resize(info, stripes * sizeof(int));
463 if (info->lti_ea_store_size < sizeof(int) * stripes) {
464 CERROR("can't allocate memory for tgt-in-use array\n");
467 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
472 * Remember a target in the array of used targets.
474 * Mark the given target as used for a new striping being created. The status
475 * of an tgt in a striping can be checked with lod_qos_is_tgt_used().
477 * \param[in] env execution environment for this thread
478 * \param[in] idx index in the array
479 * \param[in] tgt_idx target index to mark as used
481 static inline void lod_qos_tgt_in_use(const struct lu_env *env,
482 int idx, int tgt_idx)
484 struct lod_thread_info *info = lod_env_info(env);
485 int *tgts = info->lti_ea_store;
487 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
492 * Check is tgt used in a striping.
494 * Checks whether tgt with the given index is marked as used in the temporary
495 * array (see lod_qos_tgt_in_use()).
497 * \param[in] env execution environment for this thread
498 * \param[in] tgt_idx target index to check
499 * \param[in] stripes the number of items used in the array already
504 static int lod_qos_is_tgt_used(const struct lu_env *env, int tgt_idx,
507 struct lod_thread_info *info = lod_env_info(env);
508 int *tgts = info->lti_ea_store;
511 for (j = 0; j < stripes; j++) {
512 if (tgts[j] == tgt_idx)
519 lod_obj_is_ost_use_skip_cb(const struct lu_env *env, struct lod_object *lo,
520 int comp_idx, struct lod_obj_stripe_cb_data *data)
522 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
524 return comp->llc_ost_indices == NULL;
528 lod_obj_is_ost_use_cb(const struct lu_env *env, struct lod_object *lo,
529 int comp_idx, struct lod_obj_stripe_cb_data *data)
531 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
534 for (i = 0; i < comp->llc_stripe_count; i++) {
535 if (comp->llc_ost_indices[i] == data->locd_ost_index) {
536 data->locd_ost_index = -1;
545 * Check is OST used in a composite layout
547 * \param[in] lo lod object
548 * \param[in] ost OST target index to check
550 * \retval false not used
553 static inline bool lod_comp_is_ost_used(const struct lu_env *env,
554 struct lod_object *lo, int ost)
556 struct lod_obj_stripe_cb_data data = { { 0 } };
558 data.locd_ost_index = ost;
559 data.locd_comp_skip_cb = lod_obj_is_ost_use_skip_cb;
560 data.locd_comp_cb = lod_obj_is_ost_use_cb;
562 (void)lod_obj_for_each_stripe(env, lo, NULL, &data);
564 return data.locd_ost_index == -1;
567 static inline void lod_avoid_update(struct lod_object *lo,
568 struct lod_avoid_guide *lag)
573 lag->lag_ost_avail--;
576 static inline bool lod_should_avoid_ost(struct lod_object *lo,
577 struct lod_avoid_guide *lag,
580 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
581 struct lod_tgt_desc *ost = OST_TGT(lod, index);
582 struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
586 if (!test_bit(index, lod->lod_ost_bitmap)) {
587 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
593 * we've tried our best, all available OSTs have been used in
594 * overlapped components in the other mirror
596 if (lag->lag_ost_avail == 0)
600 for (i = 0; i < lag->lag_oaa_count; i++) {
601 if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
607 * if the OSS which OST[index] resides has not been used, we'd like to
613 /* if the OSS has been used, check whether the OST has been used */
614 if (!test_bit(index, lag->lag_ost_avoid_bitmap))
617 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
622 static int lod_check_and_reserve_ost(const struct lu_env *env,
623 struct lod_object *lo,
624 struct lod_layout_component *lod_comp,
625 __u32 ost_idx, __u32 speed, __u32 *s_idx,
626 struct dt_object **stripe,
632 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
633 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
634 struct lu_tgt_desc *ost = OST_TGT(lod, ost_idx);
636 __u32 stripe_idx = *s_idx;
641 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs, ost, reserve);
646 * We expect number of precreated objects in f_ffree at
647 * the first iteration, skip OSPs with no objects ready
649 if (ost->ltd_statfs.os_fprecreated == 0 && speed == 0) {
650 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
655 * try to use another OSP if this one is degraded
657 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED && speed < 2) {
658 QOS_DEBUG("#%d: degraded\n", ost_idx);
663 * try not allocate on OST which has been used by other
666 if (speed == 0 && lod_comp_is_ost_used(env, lo, ost_idx)) {
667 QOS_DEBUG("iter %d: OST%d used by other component\n",
673 * try not allocate OSTs used by conflicting component of other mirrors
674 * for the first and second time.
676 if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
677 QOS_DEBUG("iter %d: OST%d used by conflicting mirror component\n",
682 /* do not put >1 objects on a single OST, except for overstriping */
683 if (lod_qos_is_tgt_used(env, ost_idx, stripe_idx)) {
684 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
690 o = lod_qos_declare_object_on(env, lod, ost_idx, (speed > 1), th);
692 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
693 ost_idx, (int) PTR_ERR(o));
699 * We've successfully declared (reserved) an object
701 lod_avoid_update(lo, lag);
702 lod_qos_tgt_in_use(env, stripe_idx, ost_idx);
703 stripe[stripe_idx] = o;
704 ost_indices[stripe_idx] = ost_idx;
705 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
713 * Allocate a striping using round-robin algorithm.
715 * Allocates a new striping using round-robin algorithm. The function refreshes
716 * all the internal structures (statfs cache, array of available OSTs sorted
717 * with regard to OSS, etc). The number of stripes required is taken from the
718 * object (must be prepared by the caller), but can change if the flag
719 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
720 * is trying to create a striping on the object in parallel. All the internal
721 * structures (like pools, etc) are protected and no additional locking is
722 * required. The function succeeds even if a single stripe is allocated. To save
723 * time we give priority to targets which already have objects precreated.
724 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
726 * \param[in] env execution environment for this thread
727 * \param[in] lo LOD object
728 * \param[out] stripe striping created
729 * \param[out] ost_indices ost indices of striping created
730 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
731 * \param[in] th transaction handle
732 * \param[in] comp_idx index of ldo_comp_entries
734 * \retval 0 on success
735 * \retval -ENOSPC if not enough OSTs are found
736 * \retval negative negated errno for other failures
738 static int lod_ost_alloc_rr(const struct lu_env *env, struct lod_object *lo,
739 struct dt_object **stripe, __u32 *ost_indices,
740 int flags, struct thandle *th, int comp_idx,
743 struct lod_layout_component *lod_comp;
744 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
745 struct pool_desc *pool = NULL;
746 struct lu_tgt_pool *osts;
747 struct lu_qos_rr *lqr;
748 unsigned int i, array_idx;
749 __u32 stripe_idx = 0;
750 __u32 stripe_count, stripe_count_min, ost_idx;
751 int rc, speed = 0, ost_connecting = 0;
752 int stripes_per_ost = 1;
753 bool overstriped = false;
756 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
757 lod_comp = &lo->ldo_comp_entries[comp_idx];
758 stripe_count = lod_comp->llc_stripe_count;
759 stripe_count_min = min_stripe_count(stripe_count, flags);
761 if (lod_comp->llc_pool != NULL)
762 pool = lod_find_pool(m, lod_comp->llc_pool);
765 down_read(&pool_tgt_rw_sem(pool));
766 osts = &(pool->pool_obds);
767 lqr = &(pool->pool_rr);
769 osts = &m->lod_ost_descs.ltd_tgt_pool;
770 lqr = &(m->lod_ost_descs.ltd_qos.lq_rr);
773 rc = lod_qos_calc_rr(m, &m->lod_ost_descs, osts, lqr);
777 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
781 down_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
782 spin_lock(&lqr->lqr_alloc);
783 if (--lqr->lqr_start_count <= 0) {
784 atomic_set(&lqr->lqr_start_idx,
785 prandom_u32_max(osts->op_count));
786 lqr->lqr_start_count =
787 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
788 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
789 } else if (atomic_read(&lqr->lqr_start_idx) >= osts->op_count) {
790 /* If we have allocated from all of the tgts, slowly
791 * precess the next start OST if the tgt/stripe count
792 * difference isn't already doing this for us.
794 atomic_sub(osts->op_count, &lqr->lqr_start_idx);
795 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
796 ++lqr->lqr_offset_idx;
798 spin_unlock(&lqr->lqr_alloc);
799 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
801 (lod_comp->llc_stripe_count - 1) / osts->op_count + 1;
804 QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
805 "active %d count %d\n",
806 lod_comp->llc_pool ? lod_comp->llc_pool : "",
807 stripe_count, atomic_read(&lqr->lqr_start_idx),
808 lqr->lqr_start_count, lqr->lqr_offset_idx, osts->op_count,
811 for (i = 0; i < osts->op_count * stripes_per_ost &&
812 stripe_idx < stripe_count; i++) {
815 idx = atomic_inc_return(&lqr->lqr_start_idx);
816 array_idx = (idx + lqr->lqr_offset_idx) %
818 ost_idx = lqr->lqr_pool.op_array[array_idx];
820 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
821 i, idx, /* XXX: active*/ 0,
822 stripe_idx, array_idx, ost_idx);
824 if ((ost_idx == LOV_QOS_EMPTY) ||
825 !test_bit(ost_idx, m->lod_ost_bitmap))
828 /* Fail Check before osc_precreate() is called
829 so we can only 'fail' single OSC. */
830 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
833 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LOD_CREATE_PAUSE)) {
834 clear_bit(LQ_SAME_SPACE,
835 &m->lod_ost_descs.ltd_qos.lq_flags);
836 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOD_CREATE_PAUSE,
839 rc = lod_check_and_reserve_ost(env, lo, lod_comp, ost_idx,
840 speed, &stripe_idx, stripe,
841 ost_indices, th, &overstriped,
844 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
847 if ((speed < 2) && (stripe_idx < stripe_count_min)) {
848 /* Try again, allowing slower OSCs */
854 up_read(&m->lod_ost_descs.ltd_qos.lq_rw_sem);
856 /* If there are enough OSTs, a component with overstriping requested
857 * will not actually end up overstriped. The comp should reflect this.
860 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
863 lod_comp->llc_stripe_count = stripe_idx;
864 /* at least one stripe is allocated */
867 /* nobody provided us with a single object */
876 up_read(&pool_tgt_rw_sem(pool));
877 /* put back ref got by lod_find_pool() */
878 lod_pool_putref(pool);
885 lod_qos_mdt_in_use_init(const struct lu_env *env,
886 const struct lu_tgt_descs *ltd,
887 u32 stripe_idx, u32 stripe_count,
888 const struct lu_tgt_pool *pool,
889 struct dt_object **stripes)
892 struct lu_tgt_desc *mdt;
896 rc = lod_qos_tgt_in_use_clear(env, stripe_count);
900 /* if stripe_idx > 1, we are splitting directory, mark existing stripes
901 * in_use. Because for either split or creation, stripe 0 is local,
902 * don't mark it in use.
904 for (i = 1; i < stripe_idx; i++) {
906 for (j = 0; j < pool->op_count; j++) {
907 mdt_idx = pool->op_array[j];
909 if (!test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
912 mdt = LTD_TGT(ltd, mdt_idx);
913 if (&mdt->ltd_tgt->dd_lu_dev ==
914 stripes[i]->do_lu.lo_dev)
915 lod_qos_tgt_in_use(env, i, mdt_idx);
923 * Allocate a striping using round-robin algorithm.
925 * Allocates a new striping using round-robin algorithm. The function refreshes
926 * all the internal structures (statfs cache, array of available remote MDTs
927 * sorted with regard to MDS, etc). The number of stripes required is taken from
928 * the object (must be prepared by the caller). The caller should ensure nobody
929 * else is trying to create a striping on the object in parallel. All the
930 * internal structures (like pools, etc) are protected and no additional locking
931 * is required. The function succeeds even if a single stripe is allocated.
933 * \param[in] env execution environment for this thread
934 * \param[in] lo LOD object
935 * \param[out] stripes striping created
937 * \retval positive stripe objects allocated, including the first stripe
939 * \retval -ENOSPC if not enough MDTs are found
940 * \retval negative negated errno for other failures
942 int lod_mdt_alloc_rr(const struct lu_env *env, struct lod_object *lo,
943 struct dt_object **stripes, u32 stripe_idx,
946 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
947 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
948 struct lu_tgt_pool *pool;
949 struct lu_qos_rr *lqr;
950 struct lu_tgt_desc *mdt;
951 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
952 struct lu_fid fid = { 0 };
953 struct dt_object *dto;
954 unsigned int pool_idx;
956 u32 saved_idx = stripe_idx;
958 bool use_degraded = false;
959 int tgt_connecting = 0;
964 pool = <d->ltd_tgt_pool;
965 lqr = <d->ltd_qos.lq_rr;
966 rc = lod_qos_calc_rr(lod, ltd, pool, lqr);
970 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
975 down_read(<d->ltd_qos.lq_rw_sem);
976 spin_lock(&lqr->lqr_alloc);
977 if (--lqr->lqr_start_count <= 0) {
978 atomic_set(&lqr->lqr_start_idx,
979 prandom_u32_max(pool->op_count));
980 lqr->lqr_start_count =
981 (LOV_CREATE_RESEED_MIN / max(pool->op_count, 1U) +
982 LOV_CREATE_RESEED_MULT) * max(pool->op_count, 1U);
983 } else if (atomic_read(&lqr->lqr_start_idx) >= pool->op_count) {
984 /* If we have allocated from all of the tgts, slowly
985 * precess the next start if the tgt/stripe count isn't
986 * already doing this for us. */
987 atomic_sub(pool->op_count, &lqr->lqr_start_idx);
988 if (stripe_count - 1 > 1 &&
989 (pool->op_count % (stripe_count - 1)) != 1)
990 ++lqr->lqr_offset_idx;
992 spin_unlock(&lqr->lqr_alloc);
995 QOS_DEBUG("want=%d start_idx=%d start_count=%d offset=%d active=%d count=%d\n",
996 stripe_count - 1, atomic_read(&lqr->lqr_start_idx),
997 lqr->lqr_start_count, lqr->lqr_offset_idx, pool->op_count,
1000 for (i = 0; i < pool->op_count && stripe_idx < stripe_count; i++) {
1003 idx = atomic_inc_return(&lqr->lqr_start_idx);
1004 pool_idx = (idx + lqr->lqr_offset_idx) %
1006 mdt_idx = lqr->lqr_pool.op_array[pool_idx];
1007 mdt = LTD_TGT(ltd, mdt_idx);
1009 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1010 i, idx, /* XXX: active*/ 0,
1011 stripe_idx, pool_idx, mdt_idx);
1013 if (mdt_idx == LOV_QOS_EMPTY ||
1014 !test_bit(mdt_idx, ltd->ltd_tgt_bitmap))
1017 /* do not put >1 objects on one MDT */
1018 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1021 rc = lod_is_tgt_usable(ltd, mdt);
1023 if (mdt->ltd_connecting)
1028 /* try to use another OSP if this one is degraded */
1029 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED &&
1031 QOS_DEBUG("#%d: degraded\n", mdt_idx);
1035 rc = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1037 QOS_DEBUG("#%d: alloc FID failed: %dl\n", mdt_idx, rc);
1041 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1042 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1046 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1047 mdt->ltd_index, (int) PTR_ERR(dto));
1049 if (mdt->ltd_connecting)
1054 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1055 stripes[stripe_idx++] = dto;
1058 if (!use_degraded && stripe_idx < stripe_count) {
1059 /* Try again, allowing slower MDTs */
1060 use_degraded = true;
1065 up_read(<d->ltd_qos.lq_rw_sem);
1067 if (stripe_idx > saved_idx)
1068 /* at least one stripe is allocated */
1071 /* nobody provided us with a single object */
1073 RETURN(-EINPROGRESS);
1079 * Allocate a specific striping layout on a user defined set of OSTs.
1081 * Allocates new striping using the OST index range provided by the data from
1082 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1083 * OSTs are not considered. The exact order of OSTs requested by the user
1084 * is respected as much as possible depending on OST status. The number of
1085 * stripes needed and stripe offset are taken from the object. If that number
1086 * can not be met, then the function returns a failure and then it's the
1087 * caller's responsibility to release the stripes allocated. All the internal
1088 * structures are protected, but no concurrent allocation is allowed on the
1091 * \param[in] env execution environment for this thread
1092 * \param[in] lo LOD object
1093 * \param[out] stripe striping created
1094 * \param[out] ost_indices ost indices of striping created
1095 * \param[in] th transaction handle
1096 * \param[in] comp_idx index of ldo_comp_entries
1098 * \retval 0 on success
1099 * \retval -ENODEV OST index does not exist on file system
1100 * \retval -EINVAL requested OST index is invalid
1101 * \retval negative negated errno on error
1103 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1104 struct dt_object **stripe, __u32 *ost_indices,
1105 struct thandle *th, int comp_idx, __u64 reserve)
1107 struct lod_layout_component *lod_comp;
1108 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1109 struct dt_object *o;
1110 unsigned int array_idx = 0;
1111 int stripe_count = 0;
1116 /* for specific OSTs layout */
1117 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1118 lod_comp = &lo->ldo_comp_entries[comp_idx];
1119 LASSERT(lod_comp->llc_ostlist.op_array);
1120 LASSERT(lod_comp->llc_ostlist.op_count);
1122 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1126 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
1127 lod_comp->llc_stripe_offset =
1128 lod_comp->llc_ostlist.op_array[0];
1130 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1131 if (lod_comp->llc_ostlist.op_array[i] ==
1132 lod_comp->llc_stripe_offset) {
1137 if (i == lod_comp->llc_stripe_count) {
1139 "%s: start index %d not in the specified list of OSTs\n",
1140 lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1144 for (i = 0; i < lod_comp->llc_stripe_count;
1145 i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1146 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1148 if (!test_bit(ost_idx, m->lod_ost_bitmap)) {
1153 /* do not put >1 objects on a single OST, except for
1156 if (lod_qos_is_tgt_used(env, ost_idx, stripe_count) &&
1157 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
1162 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1163 LTD_TGT(&m->lod_ost_descs, ost_idx),
1165 if (rc < 0) /* this OSP doesn't feel well */
1168 o = lod_qos_declare_object_on(env, m, ost_idx, true, th);
1172 "%s: can't declare new object on #%u: %d\n",
1173 lod2obd(m)->obd_name, ost_idx, rc);
1178 * We've successfully declared (reserved) an object
1180 lod_qos_tgt_in_use(env, stripe_count, ost_idx);
1181 stripe[stripe_count] = o;
1182 ost_indices[stripe_count] = ost_idx;
1190 * Allocate a striping on a predefined set of OSTs.
1192 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1193 * Full OSTs are not considered. The exact order of OSTs is not important and
1194 * varies depending on OST status. The allocation procedure prefers the targets
1195 * with precreated objects ready. The number of stripes needed and stripe
1196 * offset are taken from the object. If that number cannot be met, then the
1197 * function returns an error and then it's the caller's responsibility to
1198 * release the stripes allocated. All the internal structures are protected,
1199 * but no concurrent allocation is allowed on the same objects.
1201 * \param[in] env execution environment for this thread
1202 * \param[in] lo LOD object
1203 * \param[out] stripe striping created
1204 * \param[out] ost_indices ost indices of striping created
1205 * \param[in] flags not used
1206 * \param[in] th transaction handle
1207 * \param[in] comp_idx index of ldo_comp_entries
1209 * \retval 0 on success
1210 * \retval -ENOSPC if no OST objects are available at all
1211 * \retval -EFBIG if not enough OST objects are found
1212 * \retval -EINVAL requested offset is invalid
1213 * \retval negative errno on failure
1215 static int lod_ost_alloc_specific(const struct lu_env *env,
1216 struct lod_object *lo,
1217 struct dt_object **stripe, __u32 *ost_indices,
1218 int flags, struct thandle *th, int comp_idx,
1221 struct lod_layout_component *lod_comp;
1222 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1223 struct dt_object *o;
1224 struct lu_tgt_desc *tgt;
1226 unsigned int i, array_idx, ost_count;
1227 int rc, stripe_num = 0;
1229 struct pool_desc *pool = NULL;
1230 struct lu_tgt_pool *osts;
1231 int stripes_per_ost = 1;
1232 bool overstriped = false;
1235 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1236 lod_comp = &lo->ldo_comp_entries[comp_idx];
1238 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1242 if (lod_comp->llc_pool != NULL)
1243 pool = lod_find_pool(m, lod_comp->llc_pool);
1246 down_read(&pool_tgt_rw_sem(pool));
1247 osts = &(pool->pool_obds);
1249 osts = &m->lod_ost_descs.ltd_tgt_pool;
1252 ost_count = osts->op_count;
1255 /* search loi_ost_idx in ost array */
1257 for (i = 0; i < ost_count; i++) {
1258 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1263 if (i == ost_count) {
1264 CERROR("Start index %d not found in pool '%s'\n",
1265 lod_comp->llc_stripe_offset,
1266 lod_comp->llc_pool ? lod_comp->llc_pool : "");
1267 GOTO(out, rc = -EINVAL);
1270 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1272 (lod_comp->llc_stripe_count - 1)/ost_count + 1;
1274 /* user specifies bigger stripe count than available ost count */
1275 if (lod_comp->llc_stripe_count > ost_count * stripes_per_ost)
1276 lod_comp->llc_stripe_count = ost_count * stripes_per_ost;
1278 for (i = 0; i < ost_count * stripes_per_ost;
1279 i++, array_idx = (array_idx + 1) % ost_count) {
1280 ost_idx = osts->op_array[array_idx];
1282 if (!test_bit(ost_idx, m->lod_ost_bitmap))
1285 /* Fail Check before osc_precreate() is called
1286 so we can only 'fail' single OSC. */
1287 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1291 * do not put >1 objects on a single OST, except for
1292 * overstriping, where it is intended
1294 if (lod_qos_is_tgt_used(env, ost_idx, stripe_num)) {
1295 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1302 * try not allocate on the OST used by other component
1304 if (speed == 0 && i != 0 &&
1305 lod_comp_is_ost_used(env, lo, ost_idx))
1308 tgt = LTD_TGT(&m->lod_ost_descs, ost_idx);
1310 /* Drop slow OSCs if we can, but not for requested start idx.
1312 * This means "if OSC is slow and it is not the requested
1313 * start OST, then it can be skipped, otherwise skip it only
1314 * if it is inactive/recovering/out-of-space." */
1316 rc = lod_statfs_and_check(env, m, &m->lod_ost_descs,
1319 /* this OSP doesn't feel well */
1324 * We expect number of precreated objects at the first
1325 * iteration. Skip OSPs with no objects ready. Don't apply
1326 * this logic to OST specified with stripe_offset.
1328 if (i && !tgt->ltd_statfs.os_fprecreated && !speed)
1331 o = lod_qos_declare_object_on(env, m, ost_idx, true, th);
1333 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1334 ost_idx, (int) PTR_ERR(o));
1339 * We've successfully declared (reserved) an object
1341 lod_qos_tgt_in_use(env, stripe_num, ost_idx);
1342 stripe[stripe_num] = o;
1343 ost_indices[stripe_num] = ost_idx;
1346 /* We have enough stripes */
1347 if (stripe_num == lod_comp->llc_stripe_count)
1351 /* Try again, allowing slower OSCs */
1356 /* If we were passed specific striping params, then a failure to
1357 * meet those requirements is an error, since we can't reallocate
1358 * that memory (it might be part of a larger array or something).
1360 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1361 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1362 lod_comp->llc_stripe_count);
1363 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1365 /* If there are enough OSTs, a component with overstriping requessted
1366 * will not actually end up overstriped. The comp should reflect this.
1368 if (rc == 0 && !overstriped)
1369 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1373 up_read(&pool_tgt_rw_sem(pool));
1374 /* put back ref got by lod_find_pool() */
1375 lod_pool_putref(pool);
1381 #ifdef HAVE_DOWN_WRITE_KILLABLE
1382 struct semaphore_timer {
1383 struct timer_list timer;
1384 struct task_struct *task;
1387 static void process_semaphore_timer(struct timer_list *t)
1389 struct semaphore_timer *timeout = cfs_from_timer(timeout, t, timer);
1391 send_sig(SIGKILL, timeout->task, 1);
1396 * Calculate penalties per-ost in a pool
1398 * The algorithm is similar to ltd_qos_penalties_calc(), but much simpler,
1399 * just considering the space of each OST in this pool.
1401 * \param[in] lod lod_device
1402 * \param[in] pool pool_desc
1404 * \retval 0 on success
1405 * \retval -EAGAIN the number of OSTs isn't enough or all tgt spaces are
1408 static int lod_pool_qos_penalties_calc(struct lod_device *lod,
1409 struct pool_desc *pool)
1411 struct lu_tgt_descs *ltd = &lod->lod_ost_descs;
1412 struct lu_qos *qos = <d->ltd_qos;
1413 struct lov_desc *desc = <d->ltd_lov_desc;
1414 struct lu_tgt_pool *osts = &pool->pool_obds;
1415 struct lod_tgt_desc *ost;
1416 __u64 ba_max, ba_min, ba;
1424 now = ktime_get_real_seconds();
1426 if (pool->pool_same_space && now < pool->pool_same_space_expire)
1429 num_active = osts->op_count - 1;
1431 GOTO(out, rc = -EAGAIN);
1433 prio_wide = 256 - qos->lq_prio_free;
1435 ba_min = (__u64)(-1);
1438 /* Calculate penalty per OST */
1439 for (i = 0; i < osts->op_count; i++) {
1440 if (!test_bit(osts->op_array[i], lod->lod_ost_bitmap))
1443 ost = OST_TGT(lod, osts->op_array[i]);
1444 if (!ost->ltd_active)
1447 ba = tgt_statfs_bavail(ost) >> 8;
1451 ba_min = min(ba, ba_min);
1452 ba_max = max(ba, ba_max);
1453 ost->ltd_qos.ltq_svr->lsq_bavail += ba;
1456 * per-ost penalty is
1457 * prio * bavail / (num_tgt - 1) / prio_max / 2
1459 ost->ltd_qos.ltq_penalty_per_obj = prio_wide * ba >> 9;
1460 do_div(ost->ltd_qos.ltq_penalty_per_obj, num_active);
1462 age = (now - ost->ltd_qos.ltq_used) >> 3;
1463 if (age > 32 * desc->ld_qos_maxage)
1464 ost->ltd_qos.ltq_penalty = 0;
1465 else if (age > desc->ld_qos_maxage)
1466 /* Decay ost penalty. */
1467 ost->ltd_qos.ltq_penalty >>= age / desc->ld_qos_maxage;
1471 * If each ost has almost same free space, do rr allocation for better
1472 * creation performance
1474 if ((ba_max * (256 - qos->lq_threshold_rr)) >> 8 < ba_min) {
1475 pool->pool_same_space = true;
1476 pool->pool_same_space_expire = now + desc->ld_qos_maxage;
1478 pool->pool_same_space = false;
1483 if (!rc && pool->pool_same_space)
1490 * Allocate a striping using an algorithm with weights.
1492 * The function allocates OST objects to create a striping. The algorithm
1493 * used is based on weights (currently only using the free space), and it's
1494 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1495 * configuration (# of stripes, offset, pool) is taken from the object and
1496 * is prepared by the caller.
1498 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1499 * be met due to too few OSTs, then allocation fails. If the flag is passed
1500 * fewer than 3/4 of the requested number of stripes can be allocated, then
1503 * No concurrent allocation is allowed on the object and this must be ensured
1504 * by the caller. All the internal structures are protected by the function.
1506 * The algorithm has two steps: find available OSTs and calculate their
1507 * weights, then select the OSTs with their weights used as the probability.
1508 * An OST with a higher weight is proportionately more likely to be selected
1509 * than one with a lower weight.
1511 * \param[in] env execution environment for this thread
1512 * \param[in] lo LOD object
1513 * \param[out] stripe striping created
1514 * \param[out] ost_indices ost indices of striping created
1515 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1516 * \param[in] th transaction handle
1517 * \param[in] comp_idx index of ldo_comp_entries
1519 * \retval 0 on success
1520 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1521 * \retval -EINVAL requested OST index is invalid
1522 * \retval negative errno on failure
1524 static int lod_ost_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1525 struct dt_object **stripe, __u32 *ost_indices,
1526 int flags, struct thandle *th, int comp_idx,
1529 struct lod_layout_component *lod_comp;
1530 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1531 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1532 struct lod_tgt_desc *ost;
1533 struct dt_object *o;
1534 __u64 total_weight = 0;
1535 struct pool_desc *pool = NULL;
1536 struct lu_tgt_pool *osts;
1538 __u32 nfound, good_osts, stripe_count, stripe_count_min;
1539 bool overstriped = false;
1540 int stripes_per_ost = 1;
1545 /* Totally skip qos part when qos_threshold_rr=100% */
1546 if (lod->lod_ost_descs.ltd_qos.lq_threshold_rr == QOS_THRESHOLD_MAX)
1549 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1550 lod_comp = &lo->ldo_comp_entries[comp_idx];
1551 stripe_count = lod_comp->llc_stripe_count;
1552 stripe_count_min = min_stripe_count(stripe_count, flags);
1553 if (stripe_count_min < 1)
1556 if (lod_comp->llc_pool != NULL)
1557 pool = lod_find_pool(lod, lod_comp->llc_pool);
1560 down_read(&pool_tgt_rw_sem(pool));
1561 osts = &(pool->pool_obds);
1563 osts = &lod->lod_ost_descs.ltd_tgt_pool;
1566 /* Detect -EAGAIN early, before expensive lock is taken. */
1567 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1568 GOTO(out_nolock, rc = -EAGAIN);
1570 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1572 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1574 #ifdef HAVE_DOWN_WRITE_KILLABLE
1575 if (!down_write_trylock(&lod->lod_ost_descs.ltd_qos.lq_rw_sem)) {
1576 struct semaphore_timer timer;
1578 kernel_sigaction(SIGKILL, SIG_DFL);
1579 timer.task = current;
1580 cfs_timer_setup(&timer.timer, process_semaphore_timer, 0, 0);
1581 mod_timer(&timer.timer, jiffies + cfs_time_seconds(2));
1582 /* Do actual allocation, use write lock here. */
1583 rc = down_write_killable(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1585 timer_delete_sync(&timer.timer);
1586 kernel_sigaction(SIGKILL, SIG_IGN);
1588 flush_signals(current);
1589 QOS_DEBUG("%s: wakeup semaphore on timeout rc = %d\n",
1590 lod2obd(lod)->obd_name, rc);
1591 GOTO(out_nolock, rc = -EAGAIN);
1595 /* Do actual allocation, use write lock here. */
1596 down_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1599 * Check again, while we were sleeping on @lq_rw_sem things could
1602 if (!ltd_qos_is_usable(&lod->lod_ost_descs))
1603 GOTO(out, rc = -EAGAIN);
1606 rc = lod_pool_qos_penalties_calc(lod, pool);
1608 rc = ltd_qos_penalties_calc(&lod->lod_ost_descs);
1612 rc = lod_qos_tgt_in_use_clear(env, lod_comp->llc_stripe_count);
1617 /* Find all the OSTs that are valid stripe candidates */
1618 for (i = 0; i < osts->op_count; i++) {
1619 if (!test_bit(osts->op_array[i], lod->lod_ost_bitmap))
1622 ost = OST_TGT(lod, osts->op_array[i]);
1623 ost->ltd_qos.ltq_usable = 0;
1625 rc = lod_statfs_and_check(env, lod, &lod->lod_ost_descs,
1628 /* this OSP doesn't feel well */
1632 if (ost->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1635 /* Fail Check before osc_precreate() is called
1636 * so we can only 'fail' single OSC.
1638 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1639 osts->op_array[i] == 0)
1642 ost->ltd_qos.ltq_usable = 1;
1643 lu_tgt_qos_weight_calc(ost, false);
1644 total_weight += ost->ltd_qos.ltq_weight;
1649 QOS_DEBUG("found %d good osts\n", good_osts);
1651 if (good_osts < stripe_count_min)
1652 GOTO(out, rc = -EAGAIN);
1654 /* If we do not have enough OSTs for the requested stripe count, do not
1655 * put more stripes per OST than requested.
1657 if (stripe_count / stripes_per_ost > good_osts)
1658 stripe_count = good_osts * stripes_per_ost;
1660 /* Find enough OSTs with weighted random allocation. */
1662 while (nfound < stripe_count) {
1663 u64 rand, cur_weight;
1668 rand = lu_prandom_u64_max(total_weight);
1670 /* On average, this will hit larger-weighted OSTs more often.
1671 * 0-weight OSTs will always get used last (only when rand=0)
1673 for (i = 0; i < osts->op_count; i++) {
1674 __u32 idx = osts->op_array[i];
1675 struct lod_tgt_desc *ost;
1677 if (lod_should_avoid_ost(lo, lag, idx))
1680 ost = OST_TGT(lod, idx);
1682 if (!ost->ltd_qos.ltq_usable)
1685 cur_weight += ost->ltd_qos.ltq_weight;
1686 QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1687 "rand=%llu total_weight=%llu\n",
1688 stripe_count, nfound, cur_weight, rand,
1691 if (cur_weight < rand)
1694 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1696 * do not put >1 objects on a single OST, except for
1699 if ((lod_comp_is_ost_used(env, lo, idx)) &&
1700 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
1703 if (lod_qos_is_tgt_used(env, idx, nfound)) {
1704 if (lod_comp->llc_pattern &
1705 LOV_PATTERN_OVERSTRIPING)
1711 o = lod_qos_declare_object_on(env, lod, idx, slow, th);
1713 QOS_DEBUG("can't declare object on #%u: %d\n",
1714 idx, (int) PTR_ERR(o));
1718 lod_avoid_update(lo, lag);
1719 lod_qos_tgt_in_use(env, nfound, idx);
1721 ost_indices[nfound] = idx;
1722 ltd_qos_update(&lod->lod_ost_descs, ost, &total_weight);
1728 if (rc && !slow && nfound < stripe_count) {
1729 /* couldn't allocate using precreated objects
1730 * so try to wait for new precreations */
1736 /* no OST found on this iteration, give up */
1741 if (unlikely(nfound != stripe_count)) {
1743 * when the decision to use weighted algorithm was made
1744 * we had enough appropriate OSPs, but this state can
1745 * change anytime (no space on OST, broken connection, etc)
1746 * so it's possible OSP won't be able to provide us with
1747 * an object due to just changed state
1749 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1750 lod2obd(lod)->obd_name, stripe_count, nfound);
1751 for (i = 0; i < nfound; i++) {
1752 LASSERT(stripe[i] != NULL);
1753 dt_object_put(env, stripe[i]);
1757 /* makes sense to rebalance next time */
1758 set_bit(LQ_DIRTY, &lod->lod_ost_descs.ltd_qos.lq_flags);
1759 clear_bit(LQ_SAME_SPACE, &lod->lod_ost_descs.ltd_qos.lq_flags);
1763 /* If there are enough OSTs, a component with overstriping requessted
1764 * will not actually end up overstriped. The comp should reflect this.
1766 if (rc == 0 && !overstriped)
1767 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1770 up_write(&lod->lod_ost_descs.ltd_qos.lq_rw_sem);
1774 up_read(&pool_tgt_rw_sem(pool));
1775 /* put back ref got by lod_find_pool() */
1776 lod_pool_putref(pool);
1783 * Allocate a striping using an algorithm with weights.
1785 * The function allocates remote MDT objects to create a striping, the first
1786 * object was already allocated on current MDT to ensure master object and
1787 * the first object are on the same MDT. The algorithm used is based on weights
1788 * (both free space and inodes), and it's trying to ensure the space/inodes are
1789 * used evenly by MDTs and MDSs. The striping configuration (# of stripes,
1790 * offset, pool) is taken from the object and is prepared by the caller.
1792 * If prepared configuration can't be met due to too few MDTs, then allocation
1795 * No concurrent allocation is allowed on the object and this must be ensured
1796 * by the caller. All the internal structures are protected by the function.
1798 * The algorithm has two steps: find available MDTs and calculate their
1799 * weights, then select the MDTs with their weights used as the probability.
1800 * An MDT with a higher weight is proportionately more likely to be selected
1801 * than one with a lower weight.
1803 * \param[in] env execution environment for this thread
1804 * \param[in] lo LOD object
1805 * \param[in] stripe_idx starting stripe index to allocate, if it's not
1806 * 0, we are restriping directory
1807 * \param[in] stripe_count total stripe count
1808 * \param[out] stripes striping created
1810 * \retval positive stripes allocated, and it should be equal to
1811 * lo->ldo_dir_stripe_count
1812 * \retval -EAGAIN not enough tgts are found for specified stripe count
1813 * \retval -EINVAL requested MDT index is invalid
1814 * \retval negative errno on failure
1816 int lod_mdt_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1817 struct dt_object **stripes, u32 stripe_idx,
1820 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1821 struct lu_tgt_descs *ltd = &lod->lod_mdt_descs;
1822 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
1823 struct lu_fid fid = { 0 };
1824 const struct lu_tgt_pool *pool;
1825 struct lu_tgt_desc *mdt;
1826 struct dt_object *dto;
1827 u64 total_weight = 0;
1828 u32 saved_idx = stripe_idx;
1830 unsigned int good_mdts;
1836 /* Totally skip qos part when qos_threshold_rr=100% */
1837 if (ltd->ltd_qos.lq_threshold_rr == QOS_THRESHOLD_MAX)
1840 LASSERT(stripe_idx <= stripe_count);
1841 if (stripe_idx == stripe_count)
1842 RETURN(stripe_count);
1844 /* use MDT pool in @ltd, once MDT pool is supported in the future, it
1845 * can be passed in as argument like OST object allocation.
1847 pool = <d->ltd_tgt_pool;
1849 /* Detect -EAGAIN early, before expensive lock is taken. */
1850 if (!ltd_qos_is_usable(ltd))
1853 rc = lod_qos_mdt_in_use_init(env, ltd, stripe_idx, stripe_count, pool,
1858 /* Do actual allocation, use write lock here. */
1859 down_write(<d->ltd_qos.lq_rw_sem);
1862 * Check again, while we were sleeping on @lq_rw_sem things could
1865 if (!ltd_qos_is_usable(ltd))
1866 GOTO(unlock, rc = -EAGAIN);
1868 rc = ltd_qos_penalties_calc(ltd);
1873 /* Find all the MDTs that are valid stripe candidates */
1874 for (i = 0; i < pool->op_count; i++) {
1875 if (!test_bit(pool->op_array[i], ltd->ltd_tgt_bitmap))
1878 mdt = LTD_TGT(ltd, pool->op_array[i]);
1879 mdt->ltd_qos.ltq_usable = 0;
1881 rc = lod_is_tgt_usable(ltd, mdt);
1885 if (mdt->ltd_statfs.os_state & OS_STATFS_DEGRADED)
1888 mdt->ltd_qos.ltq_usable = 1;
1889 lu_tgt_qos_weight_calc(mdt, true);
1890 total_weight += mdt->ltd_qos.ltq_weight;
1895 QOS_DEBUG("found %d good MDTs\n", good_mdts);
1897 if (good_mdts < stripe_count - stripe_idx)
1898 GOTO(unlock, rc = -EAGAIN);
1900 /* Find enough MDTs with weighted random allocation. */
1901 while (stripe_idx < stripe_count) {
1902 u64 rand, cur_weight;
1907 rand = lu_prandom_u64_max(total_weight);
1909 /* On average, this will hit larger-weighted MDTs more often.
1910 * 0-weight MDT will always get used last (only when rand=0) */
1911 for (i = 0; i < pool->op_count; i++) {
1914 mdt_idx = pool->op_array[i];
1915 mdt = LTD_TGT(ltd, mdt_idx);
1917 if (!mdt->ltd_qos.ltq_usable)
1920 cur_weight += mdt->ltd_qos.ltq_weight;
1922 QOS_DEBUG("stripe_count=%d stripe_index=%d cur_weight=%llu rand=%llu total_weight=%llu\n",
1923 stripe_count, stripe_idx, cur_weight, rand,
1926 if (cur_weight < rand)
1929 QOS_DEBUG("stripe=%d to idx=%d\n",
1930 stripe_idx, mdt_idx);
1932 if (lod_qos_is_tgt_used(env, mdt_idx, stripe_idx))
1935 rc2 = dt_fid_alloc(env, mdt->ltd_tgt, &fid, NULL, NULL);
1937 QOS_DEBUG("can't alloc FID on #%u: %d\n",
1942 conf.loc_flags = LOC_F_NEW;
1943 dto = dt_locate_at(env, mdt->ltd_tgt, &fid,
1944 lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1947 QOS_DEBUG("can't alloc stripe on #%u: %d\n",
1948 mdt_idx, (int) PTR_ERR(dto));
1952 lod_qos_tgt_in_use(env, stripe_idx, mdt_idx);
1953 stripes[stripe_idx] = dto;
1954 ltd_qos_update(ltd, mdt, &total_weight);
1960 /* no MDT found on this iteration, give up */
1965 if (unlikely(stripe_idx != stripe_count)) {
1967 * when the decision to use weighted algorithm was made
1968 * we had enough appropriate OSPs, but this state can
1969 * change anytime (no space on MDT, broken connection, etc)
1970 * so it's possible OSP won't be able to provide us with
1971 * an object due to just changed state
1973 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1974 lod2obd(lod)->obd_name, stripe_count, stripe_idx);
1975 for (i = saved_idx; i < stripe_idx; i++) {
1976 LASSERT(stripes[i] != NULL);
1977 dt_object_put(env, stripes[i]);
1981 /* makes sense to rebalance next time */
1982 set_bit(LQ_DIRTY, <d->ltd_qos.lq_flags);
1983 clear_bit(LQ_SAME_SPACE, <d->ltd_qos.lq_flags);
1991 up_write(<d->ltd_qos.lq_rw_sem);
1997 * Check stripe count the caller can use.
1999 * For new layouts (no initialized components), check the total size of the
2000 * layout against the maximum EA size from the backing file system. This
2001 * stops us from creating a layout which will be too large once initialized.
2003 * For existing layouts (with initialized components):
2004 * Find the maximal possible stripe count not greater than \a stripe_count.
2005 * If the provided stripe count is 0, then the filesystem's default is used.
2007 * \param[in] lod LOD device
2008 * \param[in] lo The lod_object
2009 * \param[in] comp_idx The component id, which the amount of stripes is
2011 * \param[in] stripe_count count the caller would like to use
2013 * \retval the maximum usable stripe count
2015 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
2016 int comp_idx, __u16 stripe_count, bool overstriping)
2018 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
2019 /* max stripe count is based on OSD ea size */
2020 unsigned int easize = lod->lod_osd_max_easize;
2024 if (stripe_count == (__u16)(-1) && lod->lod_max_stripecount)
2025 stripe_count = lod->lod_max_stripecount;
2028 lod->lod_ost_descs.ltd_lov_desc.ld_default_stripe_count;
2031 /* Overstriping allows more stripes than targets */
2033 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count &&
2036 lod->lod_ost_descs.ltd_lov_desc.ld_active_tgt_count;
2038 if (lo->ldo_is_composite) {
2039 struct lod_layout_component *lod_comp;
2040 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
2041 unsigned int init_comp_sz = 0;
2042 unsigned int total_comp_sz = 0;
2043 unsigned int comp_sz;
2045 header_sz += sizeof(struct lov_comp_md_entry_v1) *
2048 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2049 unsigned int stripes;
2054 lod_comp = &lo->ldo_comp_entries[i];
2055 /* Extension comp is never inited - 0 stripes on disk */
2056 stripes = lod_comp->llc_flags & LCME_FL_EXTENSION ? 0 :
2057 lod_comp->llc_stripe_count;
2059 comp_sz = lov_mds_md_size(stripes, LOV_MAGIC_V3);
2060 total_comp_sz += comp_sz;
2061 if (lod_comp->llc_flags & LCME_FL_INIT)
2062 init_comp_sz += comp_sz;
2065 if (init_comp_sz > 0)
2066 total_comp_sz = init_comp_sz;
2068 header_sz += total_comp_sz;
2070 if (easize > header_sz)
2071 easize -= header_sz;
2076 max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
2077 max_stripes = (max_stripes == 0) ? 0 : max_stripes - 1;
2079 stripe_count = min_t(__u16, stripe_count, max_stripes);
2080 RETURN(stripe_count);
2084 * Create in-core respresentation for a fully-defined striping
2086 * When the caller passes a fully-defined striping (i.e. everything including
2087 * OST object FIDs are defined), then we still need to instantiate LU-cache
2088 * with the objects representing the stripes defined. This function completes
2091 * \param[in] env execution environment for this thread
2092 * \param[in] mo LOD object
2093 * \param[in] buf buffer containing the striping
2095 * \retval 0 on success
2096 * \retval negative negated errno on error
2098 int lod_use_defined_striping(const struct lu_env *env,
2099 struct lod_object *mo,
2100 const struct lu_buf *buf)
2102 struct lod_layout_component *lod_comp;
2103 struct lov_mds_md_v1 *v1 = buf->lb_buf;
2104 struct lov_mds_md_v3 *v3 = buf->lb_buf;
2105 struct lov_comp_md_v1 *comp_v1 = NULL;
2106 struct lov_ost_data_v1 *objs;
2113 mutex_lock(&mo->ldo_layout_mutex);
2114 lod_striping_free_nolock(env, mo);
2116 magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
2118 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
2119 magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
2120 GOTO(unlock, rc = -EINVAL);
2122 if (magic == LOV_MAGIC_COMP_V1) {
2123 comp_v1 = buf->lb_buf;
2124 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
2126 GOTO(unlock, rc = -EINVAL);
2127 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
2128 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
2130 mo->ldo_is_composite = 1;
2131 } else if (magic == LOV_MAGIC_FOREIGN) {
2132 struct lov_foreign_md *foreign;
2135 if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
2137 "buf len %zu < min lov_foreign_md size (%zu)\n",
2139 offsetof(typeof(*foreign), lfm_value));
2140 GOTO(out, rc = -EINVAL);
2142 foreign = (struct lov_foreign_md *)buf->lb_buf;
2143 length = foreign_size_le(foreign);
2144 if (buf->lb_len < length) {
2146 "buf len %zu < this lov_foreign_md size (%zu)\n",
2147 buf->lb_len, length);
2148 GOTO(out, rc = -EINVAL);
2151 /* just cache foreign LOV EA raw */
2152 rc = lod_alloc_foreign_lov(mo, length);
2155 memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
2158 mo->ldo_is_composite = 0;
2162 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
2164 rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
2168 for (i = 0; i < comp_cnt; i++) {
2169 struct lu_extent *ext;
2173 lod_comp = &mo->ldo_comp_entries[i];
2175 if (mo->ldo_is_composite) {
2176 offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
2177 v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
2178 v3 = (struct lov_mds_md_v3 *)v1;
2179 magic = le32_to_cpu(v1->lmm_magic);
2181 ext = &comp_v1->lcm_entries[i].lcme_extent;
2182 lod_comp->llc_extent.e_start =
2183 le64_to_cpu(ext->e_start);
2184 lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
2185 lod_comp->llc_flags =
2186 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
2187 if (lod_comp->llc_flags & LCME_FL_NOSYNC)
2188 lod_comp->llc_timestamp = le64_to_cpu(
2189 comp_v1->lcm_entries[i].lcme_timestamp);
2191 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
2192 if (lod_comp->llc_id == LCME_ID_INVAL)
2193 GOTO(out, rc = -EINVAL);
2197 if (magic == LOV_MAGIC_V1) {
2198 objs = &v1->lmm_objects[0];
2199 } else if (magic == LOV_MAGIC_V3) {
2200 objs = &v3->lmm_objects[0];
2201 if (v3->lmm_pool_name[0] != '\0')
2202 pool_name = v3->lmm_pool_name;
2204 CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
2205 GOTO(out, rc = -EINVAL);
2208 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
2209 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
2210 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
2211 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
2213 * The stripe_offset of an uninit-ed component is stored in
2214 * the lmm_layout_gen
2216 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
2217 lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
2218 lod_obj_set_pool(mo, i, pool_name);
2220 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
2221 !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
2222 !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
2223 rc = lod_initialize_objects(env, mo, objs, i);
2229 rc = lod_fill_mirrors(mo);
2233 lod_striping_free_nolock(env, mo);
2235 mutex_unlock(&mo->ldo_layout_mutex);
2240 static void lod_qos_set_pool(struct lod_object *lo, int pos, char *pool_name,
2241 struct lov_user_md_v1 *v1)
2243 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2244 struct lod_layout_component *lod_comp;
2245 struct pool_desc *pool = NULL;
2248 /* In the function below, .hs_keycmp resolves to
2249 * pool_hashkey_keycmp() */
2250 /* coverity[overrun-buffer-val] */
2252 pool = lod_find_pool(d, pool_name);
2255 lod_comp = &lo->ldo_comp_entries[pos];
2256 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2257 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2258 struct lov_user_md_v3 *v3;
2260 v3 = (struct lov_user_md_v3 *)v1;
2261 for (j = 0; j < v3->lmm_stripe_count; j++) {
2264 num = lod_comp->llc_ostlist.op_array[j];
2265 rc = lod_check_index_in_pool(num, pool);
2270 rc = lod_check_index_in_pool(
2271 lod_comp->llc_stripe_offset, pool);
2274 CDEBUG(D_LAYOUT, "%s: index %u is not in the "
2275 "pool %s, dropping the pool\n",
2276 lod2obd(d)->obd_name,
2277 lod_comp->llc_stripe_offset,
2283 if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
2284 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
2285 lod_comp->llc_stripe_count = pool_tgt_count(pool);
2287 lod_pool_putref(pool);
2290 lod_obj_set_pool(lo, pos, pool_name);
2294 * Parse suggested striping configuration.
2296 * The caller gets a suggested striping configuration from a number of sources
2297 * including per-directory default and applications. Then it needs to verify
2298 * the suggested striping is valid, apply missing bits and store the resulting
2299 * configuration in the object to be used by the allocator later. Must not be
2300 * called concurrently against the same object. It's OK to provide a
2301 * fully-defined striping.
2303 * \param[in] env execution environment for this thread
2304 * \param[in] lo LOD object
2305 * \param[in] buf buffer containing the striping
2307 * \retval 0 on success
2308 * \retval negative negated errno on error
2310 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
2311 const struct lu_buf *buf)
2313 struct lod_layout_component *lod_comp;
2314 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2315 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2316 struct lov_user_md_v1 *v1 = NULL;
2317 struct lov_user_md_v3 *v3 = NULL;
2318 struct lov_comp_md_v1 *comp_v1 = NULL;
2319 struct lov_foreign_md *lfm = NULL;
2320 char def_pool[LOV_MAXPOOLNAME + 1];
2327 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
2330 memset(def_pool, 0, sizeof(def_pool));
2331 if (lo->ldo_comp_entries != NULL)
2332 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
2333 def_pool, sizeof(def_pool));
2335 /* free default striping info */
2336 if (lo->ldo_is_foreign)
2337 lod_free_foreign_lov(lo);
2339 lod_free_comp_entries(lo);
2341 rc = lod_verify_striping(env, d, lo, buf, false);
2347 comp_v1 = buf->lb_buf;
2348 /* {lmm,lfm}_magic position/length work for all LOV formats */
2349 magic = v1->lmm_magic;
2351 if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
2352 /* try to use as fully defined striping */
2353 rc = lod_use_defined_striping(env, lo, buf);
2358 case __swab32(LOV_USER_MAGIC_V1):
2359 lustre_swab_lov_user_md_v1(v1);
2360 magic = v1->lmm_magic;
2362 case LOV_USER_MAGIC_V1:
2364 case __swab32(LOV_USER_MAGIC_V3):
2365 lustre_swab_lov_user_md_v3(v3);
2366 magic = v3->lmm_magic;
2368 case LOV_USER_MAGIC_V3:
2370 case __swab32(LOV_USER_MAGIC_SPECIFIC):
2371 lustre_swab_lov_user_md_v3(v3);
2372 lustre_swab_lov_user_md_objects(v3->lmm_objects,
2373 v3->lmm_stripe_count);
2374 magic = v3->lmm_magic;
2376 case LOV_USER_MAGIC_SPECIFIC:
2378 case __swab32(LOV_USER_MAGIC_COMP_V1):
2379 lustre_swab_lov_comp_md_v1(comp_v1);
2380 magic = comp_v1->lcm_magic;
2382 case LOV_USER_MAGIC_COMP_V1:
2384 case __swab32(LOV_USER_MAGIC_FOREIGN):
2386 __swab32s(&lfm->lfm_magic);
2387 __swab32s(&lfm->lfm_length);
2388 __swab32s(&lfm->lfm_type);
2389 __swab32s(&lfm->lfm_flags);
2390 magic = lfm->lfm_magic;
2392 case LOV_USER_MAGIC_FOREIGN:
2395 rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
2398 memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
2401 CERROR("%s: unrecognized magic %X\n",
2402 lod2obd(d)->obd_name, magic);
2406 lustre_print_user_md(D_OTHER, v1, "parse config");
2408 if (magic == LOV_USER_MAGIC_COMP_V1) {
2409 comp_cnt = comp_v1->lcm_entry_count;
2412 mirror_cnt = comp_v1->lcm_mirror_count + 1;
2414 lo->ldo_flr_state = LCM_FL_RDONLY;
2415 lo->ldo_is_composite = 1;
2419 lo->ldo_is_composite = 0;
2422 rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2426 LASSERT(lo->ldo_comp_entries);
2428 for (i = 0; i < comp_cnt; i++) {
2429 struct lu_extent *ext;
2432 lod_comp = &lo->ldo_comp_entries[i];
2434 if (lo->ldo_is_composite) {
2435 v1 = (struct lov_user_md *)((char *)comp_v1 +
2436 comp_v1->lcm_entries[i].lcme_offset);
2437 ext = &comp_v1->lcm_entries[i].lcme_extent;
2438 lod_comp->llc_extent = *ext;
2439 lod_comp->llc_flags =
2440 comp_v1->lcm_entries[i].lcme_flags &
2445 if (def_pool[0] != '\0')
2446 pool_name = def_pool;
2448 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2449 v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2450 v3 = (struct lov_user_md_v3 *)v1;
2452 if (lov_pool_is_ignored(v3->lmm_pool_name))
2454 else if (v3->lmm_pool_name[0] != '\0' &&
2455 !lov_pool_is_inherited(v3->lmm_pool_name))
2456 pool_name = v3->lmm_pool_name;
2458 if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2459 rc = lod_comp_copy_ost_lists(lod_comp, v3);
2461 GOTO(free_comp, rc);
2465 if (v1->lmm_pattern == 0)
2466 v1->lmm_pattern = LOV_PATTERN_RAID0;
2467 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2468 lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
2469 lov_pattern(v1->lmm_pattern) !=
2470 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
2471 CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2472 lod2obd(d)->obd_name, v1->lmm_pattern);
2473 GOTO(free_comp, rc = -EINVAL);
2476 lod_comp->llc_pattern = v1->lmm_pattern;
2477 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2478 lod_adjust_stripe_size(lod_comp, desc->ld_default_stripe_size);
2480 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2481 if (v1->lmm_stripe_count ||
2482 lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2483 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2485 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT &&
2486 lod_comp->llc_stripe_count != 0) {
2487 CDEBUG(D_LAYOUT, "%s: invalid stripe count: %u\n",
2488 lod2obd(d)->obd_name,
2489 lod_comp->llc_stripe_count);
2490 GOTO(free_comp, rc = -EINVAL);
2493 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2494 lod_qos_set_pool(lo, i, pool_name, v1);
2500 lod_free_comp_entries(lo);
2505 * prepare enough OST avoidance bitmap space
2507 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2509 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2510 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2511 unsigned long *bitmap = NULL;
2512 __u32 *new_oss = NULL;
2514 lag->lag_ost_avail = lod->lod_ost_count;
2516 /* reset OSS avoid guide array */
2517 lag->lag_oaa_count = 0;
2518 if (lag->lag_oss_avoid_array &&
2519 lag->lag_oaa_size < lod->lod_ost_count) {
2520 OBD_FREE_PTR_ARRAY(lag->lag_oss_avoid_array, lag->lag_oaa_size);
2521 lag->lag_oss_avoid_array = NULL;
2522 lag->lag_oaa_size = 0;
2525 /* init OST avoid guide bitmap */
2526 if (lag->lag_ost_avoid_bitmap) {
2527 if (lod->lod_ost_count <= lag->lag_ost_avoid_size) {
2528 bitmap_zero(lag->lag_ost_avoid_bitmap,
2529 lag->lag_ost_avoid_size);
2531 bitmap_free(lag->lag_ost_avoid_bitmap);
2532 lag->lag_ost_avoid_bitmap = NULL;
2536 if (!lag->lag_ost_avoid_bitmap) {
2537 bitmap = bitmap_zalloc(lod->lod_ost_count, GFP_KERNEL);
2542 if (!lag->lag_oss_avoid_array) {
2544 * usually there are multiple OSTs in one OSS, but we don't
2545 * know the exact OSS number, so we choose a safe option,
2546 * using OST count to allocate the array to store the OSS
2549 OBD_ALLOC_PTR_ARRAY(new_oss, lod->lod_ost_count);
2551 bitmap_free(bitmap);
2557 lag->lag_oss_avoid_array = new_oss;
2558 lag->lag_oaa_size = lod->lod_ost_count;
2561 lag->lag_ost_avoid_bitmap = bitmap;
2562 lag->lag_ost_avoid_size = lod->lod_ost_count;
2569 * Collect information of used OSTs and OSSs in the overlapped components
2572 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2575 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2576 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2577 unsigned long *bitmap = lag->lag_ost_avoid_bitmap;
2580 /* iterate components */
2581 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2582 struct lod_layout_component *comp;
2585 * skip mirror containing component[comp_idx], we only
2586 * collect OSTs info of conflicting component in other mirrors,
2587 * so that during read, if OSTs of a mirror's component are
2588 * not available, we still have other mirror with different
2589 * OSTs to read the data.
2591 comp = &lo->ldo_comp_entries[i];
2592 if (comp->llc_id != LCME_ID_INVAL &&
2593 mirror_id_of(comp->llc_id) ==
2594 mirror_id_of(lod_comp->llc_id))
2598 * skip non-overlapped or un-instantiated components,
2599 * NOTE: don't use lod_comp_inited(comp) to judge
2600 * whether @comp has been inited, since during
2601 * declare phase, comp->llc_stripe has been allocated
2602 * while it's init flag not been set until the exec
2605 if (!lu_extent_is_overlapped(&comp->llc_extent,
2606 &lod_comp->llc_extent) ||
2611 * collect used OSTs index and OSS info from a
2614 for (j = 0; j < comp->llc_stripe_count; j++) {
2615 struct lod_tgt_desc *ost;
2616 struct lu_svr_qos *lsq;
2619 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2620 lsq = ost->ltd_qos.ltq_svr;
2622 if (test_bit(ost->ltd_index, bitmap))
2625 QOS_DEBUG("OST%d used in conflicting mirror "
2626 "component\n", ost->ltd_index);
2627 set_bit(ost->ltd_index, bitmap);
2628 lag->lag_ost_avail--;
2630 for (k = 0; k < lag->lag_oaa_count; k++) {
2631 if (lag->lag_oss_avoid_array[k] ==
2635 if (k == lag->lag_oaa_count) {
2636 lag->lag_oss_avoid_array[k] =
2638 lag->lag_oaa_count++;
2645 * Create a striping for an obejct.
2647 * The function creates a new striping for the object. The function tries QoS
2648 * algorithm first unless free space is distributed evenly among OSTs, but
2649 * by default RR algorithm is preferred due to internal concurrency (QoS is
2650 * serialized). The caller must ensure no concurrent calls to the function
2651 * are made against the same object.
2653 * \param[in] env execution environment for this thread
2654 * \param[in] lo LOD object
2655 * \param[in] attr attributes OST objects will be declared with
2656 * \param[in] th transaction handle
2657 * \param[in] comp_idx index of ldo_comp_entries
2659 * \retval 0 on success
2660 * \retval negative negated errno on error
2662 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2663 struct lu_attr *attr, struct thandle *th,
2664 int comp_idx, __u64 reserve)
2666 struct lod_layout_component *lod_comp;
2667 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2669 int flag = LOV_USES_ASSIGNED_STRIPE;
2671 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2672 struct dt_object **stripe = NULL;
2673 __u32 *ost_indices = NULL;
2677 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2678 lod_comp = &lo->ldo_comp_entries[comp_idx];
2679 LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
2681 /* A released component is being created */
2682 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2685 /* A Data-on-MDT component is being created */
2686 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2689 if (lod_comp->llc_pool)
2690 lod_check_and_spill_pool(env, d, &lod_comp->llc_pool);
2692 if (likely(lod_comp->llc_stripe == NULL)) {
2694 * no striping has been created so far
2696 LASSERT(lod_comp->llc_stripe_count);
2698 * statfs and check OST targets now, since ld_active_tgt_count
2699 * could be changed if some OSTs are [de]activated manually.
2701 lod_qos_statfs_update(env, d, &d->lod_ost_descs);
2702 stripe_len = lod_get_stripe_count(d, lo, comp_idx,
2703 lod_comp->llc_stripe_count,
2704 lod_comp->llc_pattern &
2705 LOV_PATTERN_OVERSTRIPING);
2707 if (stripe_len == 0)
2708 GOTO(out, rc = -ERANGE);
2709 lod_comp->llc_stripe_count = stripe_len;
2710 OBD_ALLOC_PTR_ARRAY(stripe, stripe_len);
2712 GOTO(out, rc = -ENOMEM);
2713 OBD_ALLOC_PTR_ARRAY(ost_indices, stripe_len);
2715 GOTO(out, rc = -ENOMEM);
2718 lod_getref(&d->lod_ost_descs);
2719 /* XXX: support for non-0 files w/o objects */
2720 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2721 d->lod_ost_count, stripe_len);
2723 if (lod_comp->llc_ostlist.op_array &&
2724 lod_comp->llc_ostlist.op_count) {
2725 rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2726 th, comp_idx, reserve);
2727 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2729 * collect OSTs and OSSs used in other mirrors whose
2730 * components cross the ldo_comp_entries[comp_idx]
2732 rc = lod_prepare_avoidance(env, lo);
2736 QOS_DEBUG("collecting conflict osts for comp[%d]\n",
2738 lod_collect_avoidance(lo, lag, comp_idx);
2740 rc = lod_ost_alloc_qos(env, lo, stripe, ost_indices,
2741 flag, th, comp_idx, reserve);
2743 rc = lod_ost_alloc_rr(env, lo, stripe,
2744 ost_indices, flag, th,
2747 rc = lod_ost_alloc_specific(env, lo, stripe,
2748 ost_indices, flag, th,
2752 lod_putref(d, &d->lod_ost_descs);
2754 for (i = 0; i < stripe_len; i++)
2755 if (stripe[i] != NULL)
2756 dt_object_put(env, stripe[i]);
2758 /* In case there is no space on any OST, let's ignore
2759 * the @reserve space to avoid an error at the init
2760 * time, probably the actual IO will be less than the
2761 * given @reserve space (aka extension_size). */
2766 lod_comp->llc_stripe_count = 0;
2768 lod_comp->llc_layout_gen = 0;
2769 lod_comp->llc_stripe = stripe;
2770 lod_comp->llc_ost_indices = ost_indices;
2771 lod_comp->llc_stripes_allocated = stripe_len;
2775 * lod_qos_parse_config() found supplied buf as a predefined
2776 * striping (not a hint), so it allocated all the object
2777 * now we need to create them
2779 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2780 struct dt_object *o;
2782 o = lod_comp->llc_stripe[i];
2785 rc = lod_sub_declare_create(env, o, attr, NULL,
2788 CERROR("can't declare create: %d\n", rc);
2793 * Clear LCME_FL_INIT for the component so that
2794 * lod_striping_create() can create the striping objects
2797 lod_comp_unset_init(lod_comp);
2803 OBD_FREE_PTR_ARRAY(stripe, stripe_len);
2805 OBD_FREE_PTR_ARRAY(ost_indices, stripe_len);
2810 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2811 struct lu_attr *attr, const struct lu_buf *buf,
2815 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2823 /* no OST available */
2824 /* XXX: should we be waiting a bit to prevent failures during
2825 * cluster initialization? */
2826 if (!d->lod_ost_count)
2830 * by this time, the object's ldo_stripe_count and ldo_stripe_size
2831 * contain default value for striping: taken from the parent
2832 * or from filesystem defaults
2834 * in case the caller is passing lovea with new striping config,
2835 * we may need to parse lovea and apply new configuration
2837 rc = lod_qos_parse_config(env, lo, buf);
2841 if (attr->la_valid & LA_SIZE)
2842 size = attr->la_size;
2845 * prepare OST object creation for the component covering file's
2846 * size, the 1st component (including plain layout file) is always
2849 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2850 struct lod_layout_component *lod_comp;
2851 struct lu_extent *extent;
2853 lod_comp = &lo->ldo_comp_entries[i];
2854 extent = &lod_comp->llc_extent;
2855 QOS_DEBUG("comp[%d] %lld "DEXT"\n", i, size, PEXT(extent));
2856 if (!lo->ldo_is_composite || size >= extent->e_start) {
2857 rc = lod_qos_prep_create(env, lo, attr, th, i, 0);