4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/lod/lod_qos.c
34 * Implementation of different allocation algorithm used
35 * to distribute objects and data among OSTs.
38 #define DEBUG_SUBSYSTEM S_LOV
40 #include <asm/div64.h>
41 #include <linux/random.h>
43 #include <libcfs/libcfs.h>
44 #include <uapi/linux/lustre/lustre_idl.h>
45 #include <lustre_swab.h>
46 #include <obd_class.h>
48 #include "lod_internal.h"
51 * force QoS policy (not RR) to be used for testing purposes
57 #define QOS_DEBUG(fmt, ...) CDEBUG(D_QOS, fmt, ## __VA_ARGS__)
58 #define QOS_CONSOLE(fmt, ...) LCONSOLE(D_QOS, fmt, ## __VA_ARGS__)
60 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
61 OST_TGT(lod,i)->ltd_statfs.os_bsize)
64 * Check whether the target is available for new OST objects.
66 * Request statfs data from the given target and verify it's active and not
67 * read-only. If so, then it can be used to place new OST objects. This
68 * function also maintains the number of active/inactive targets and sets
69 * dirty flags if those numbers change so others can run re-balance procedures.
70 * No external locking is required.
72 * \param[in] env execution environment for this thread
73 * \param[in] d LOD device
74 * \param[in] index index of OST target to check
75 * \param[out] sfs buffer for statfs data
77 * \retval 0 if the target is good
78 * \retval negative negated errno on error
81 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
82 int index, struct obd_statfs *sfs)
84 struct lod_tgt_desc *ost;
89 ost = OST_TGT(d,index);
92 rc = dt_statfs(env, ost->ltd_ost, sfs);
94 if (rc == 0 && ((sfs->os_state & OS_STATE_ENOSPC) ||
95 (sfs->os_state & OS_STATE_ENOINO && sfs->os_fprecreated == 0)))
98 if (rc && rc != -ENOTCONN)
99 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
101 /* If the OST is readonly then we can't allocate objects there */
102 if (sfs->os_state & OS_STATE_READONLY)
105 /* object precreation is skipped on the OST with max_create_count=0 */
106 if (sfs->os_state & OS_STATE_NOPRECREATE)
109 /* check whether device has changed state (active, inactive) */
110 if (rc != 0 && ost->ltd_active) {
111 /* turned inactive? */
112 spin_lock(&d->lod_lock);
113 if (ost->ltd_active) {
116 ost->ltd_connecting = 1;
118 LASSERT(d->lod_desc.ld_active_tgt_count > 0);
119 d->lod_desc.ld_active_tgt_count--;
120 d->lod_qos.lq_dirty = 1;
121 d->lod_qos.lq_rr.lqr_dirty = 1;
122 CDEBUG(D_CONFIG, "%s: turns inactive\n",
123 ost->ltd_exp->exp_obd->obd_name);
125 spin_unlock(&d->lod_lock);
126 } else if (rc == 0 && ost->ltd_active == 0) {
128 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
129 "active tgt count %d, ost nr %d\n",
130 d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
131 spin_lock(&d->lod_lock);
132 if (ost->ltd_active == 0) {
134 ost->ltd_connecting = 0;
135 d->lod_desc.ld_active_tgt_count++;
136 d->lod_qos.lq_dirty = 1;
137 d->lod_qos.lq_rr.lqr_dirty = 1;
138 CDEBUG(D_CONFIG, "%s: turns active\n",
139 ost->ltd_exp->exp_obd->obd_name);
141 spin_unlock(&d->lod_lock);
148 * Maintain per-target statfs data.
150 * The function refreshes statfs data for all the targets every N seconds.
151 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
154 * \param[in] env execution environment for this thread
155 * \param[in] lod LOD device
157 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod)
159 struct obd_device *obd = lod2obd(lod);
160 struct ost_pool *osts = &(lod->lod_pool_info);
167 max_age = ktime_get_seconds() - 2 * lod->lod_desc.ld_qos_maxage;
169 if (obd->obd_osfs_age > max_age)
170 /* statfs data are quite recent, don't need to refresh it */
173 down_write(&lod->lod_qos.lq_rw_sem);
175 if (obd->obd_osfs_age > max_age)
178 for (i = 0; i < osts->op_count; i++) {
179 idx = osts->op_array[i];
180 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
181 if (lod_statfs_and_check(env, lod, idx,
182 &OST_TGT(lod, idx)->ltd_statfs))
184 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
185 /* recalculate weigths */
186 lod->lod_qos.lq_dirty = 1;
188 obd->obd_osfs_age = ktime_get_seconds();
191 up_write(&lod->lod_qos.lq_rw_sem);
196 * Calculate per-OST and per-OSS penalties
198 * Re-calculate penalties when the configuration changes, active targets
199 * change and after statfs refresh (all these are reflected by lq_dirty flag).
200 * On every OST and OSS: decay the penalty by half for every 8x the update
201 * interval that the device has been idle. That gives lots of time for the
202 * statfs information to be updated (which the penalty is only a proxy for),
203 * and avoids penalizing OSS/OSTs under light load.
204 * See lod_qos_calc_weight() for how penalties are factored into the weight.
206 * \param[in] lod LOD device
208 * \retval 0 on success
209 * \retval -EAGAIN the number of OSTs isn't enough
211 static int lod_qos_calc_ppo(struct lod_device *lod)
213 struct lu_svr_qos *oss;
214 __u64 ba_max, ba_min, temp;
222 if (!lod->lod_qos.lq_dirty)
225 num_active = lod->lod_desc.ld_active_tgt_count - 1;
227 GOTO(out, rc = -EAGAIN);
229 /* find bavail on each OSS */
230 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list)
232 lod->lod_qos.lq_active_svr_count = 0;
235 * How badly user wants to select OSTs "widely" (not recently chosen
236 * and not on recent OSS's). As opposed to "freely" (free space
239 prio_wide = 256 - lod->lod_qos.lq_prio_free;
241 ba_min = (__u64)(-1);
243 now = ktime_get_real_seconds();
244 /* Calculate OST penalty per object
245 * (lod ref taken in lod_qos_prep_create())
247 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
248 LASSERT(OST_TGT(lod,i));
249 temp = TGT_BAVAIL(i);
252 ba_min = min(temp, ba_min);
253 ba_max = max(temp, ba_max);
255 /* Count the number of usable OSS's */
256 if (OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail == 0)
257 lod->lod_qos.lq_active_svr_count++;
258 OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail += temp;
260 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
262 do_div(temp, num_active);
263 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
264 (temp * prio_wide) >> 8;
266 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
267 if (lod->lod_qos.lq_reset ||
268 age > 32 * lod->lod_desc.ld_qos_maxage)
269 OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
270 else if (age > lod->lod_desc.ld_qos_maxage)
271 /* Decay OST penalty. */
272 OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
273 (age / lod->lod_desc.ld_qos_maxage);
276 num_active = lod->lod_qos.lq_active_svr_count - 1;
277 if (num_active < 1) {
278 /* If there's only 1 OSS, we can't penalize it, so instead
279 we have to double the OST penalty */
281 cfs_foreach_bit(lod->lod_ost_bitmap, i)
282 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
285 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
286 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
287 temp = oss->lsq_bavail >> 1;
288 do_div(temp, oss->lsq_tgt_count * num_active);
289 oss->lsq_penalty_per_obj = (temp * prio_wide) >> 8;
291 age = (now - oss->lsq_used) >> 3;
292 if (lod->lod_qos.lq_reset ||
293 age > 32 * lod->lod_desc.ld_qos_maxage)
294 oss->lsq_penalty = 0;
295 else if (age > lod->lod_desc.ld_qos_maxage)
296 /* Decay OSS penalty. */
297 oss->lsq_penalty >>= age / lod->lod_desc.ld_qos_maxage;
300 lod->lod_qos.lq_dirty = 0;
301 lod->lod_qos.lq_reset = 0;
303 /* If each ost has almost same free space,
304 * do rr allocation for better creation performance */
305 lod->lod_qos.lq_same_space = 0;
306 if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
307 lod->lod_qos.lq_same_space = 1;
308 /* Reset weights for the next time we enter qos mode */
309 lod->lod_qos.lq_reset = 1;
315 if (!rc && lod->lod_qos.lq_same_space)
322 * Calculate weight for a given OST target.
324 * The final OST weight is the number of bytes available minus the OST and
325 * OSS penalties. See lod_qos_calc_ppo() for how penalties are calculated.
327 * \param[in] lod LOD device, where OST targets are listed
328 * \param[in] i OST target index
332 static int lod_qos_calc_weight(struct lod_device *lod, int i)
336 temp = TGT_BAVAIL(i);
337 temp2 = OST_TGT(lod, i)->ltd_qos.ltq_penalty +
338 OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_penalty;
340 OST_TGT(lod, i)->ltd_qos.ltq_weight = 0;
342 OST_TGT(lod, i)->ltd_qos.ltq_weight = temp - temp2;
347 * Re-calculate weights.
349 * The function is called when some OST target was used for a new object. In
350 * this case we should re-calculate all the weights to keep new allocations
353 * \param[in] lod LOD device
354 * \param[in] osts OST pool where a new object was placed
355 * \param[in] index OST target where a new object was placed
356 * \param[out] total_wt new total weight for the pool
360 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
361 __u32 index, __u64 *total_wt)
363 struct lod_tgt_desc *ost;
364 struct lu_svr_qos *oss;
368 ost = OST_TGT(lod,index);
371 /* Don't allocate on this devuce anymore, until the next alloc_qos */
372 ost->ltd_qos.ltq_usable = 0;
374 oss = ost->ltd_qos.ltq_svr;
376 /* Decay old penalty by half (we're adding max penalty, and don't
377 want it to run away.) */
378 ost->ltd_qos.ltq_penalty >>= 1;
379 oss->lsq_penalty >>= 1;
381 /* mark the OSS and OST as recently used */
382 ost->ltd_qos.ltq_used = oss->lsq_used = ktime_get_real_seconds();
384 /* Set max penalties for this OST and OSS */
385 ost->ltd_qos.ltq_penalty +=
386 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
387 oss->lsq_penalty += oss->lsq_penalty_per_obj *
388 lod->lod_qos.lq_active_svr_count;
390 /* Decrease all OSS penalties */
391 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
392 if (oss->lsq_penalty < oss->lsq_penalty_per_obj)
393 oss->lsq_penalty = 0;
395 oss->lsq_penalty -= oss->lsq_penalty_per_obj;
399 /* Decrease all OST penalties */
400 for (j = 0; j < osts->op_count; j++) {
403 i = osts->op_array[j];
404 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
407 ost = OST_TGT(lod,i);
410 if (ost->ltd_qos.ltq_penalty <
411 ost->ltd_qos.ltq_penalty_per_obj)
412 ost->ltd_qos.ltq_penalty = 0;
414 ost->ltd_qos.ltq_penalty -=
415 ost->ltd_qos.ltq_penalty_per_obj;
417 lod_qos_calc_weight(lod, i);
419 /* Recalc the total weight of usable osts */
420 if (ost->ltd_qos.ltq_usable)
421 *total_wt += ost->ltd_qos.ltq_weight;
423 QOS_DEBUG("recalc tgt %d usable=%d avail=%llu"
424 " ostppo=%llu ostp=%llu ossppo=%llu"
425 " ossp=%llu wt=%llu\n",
426 i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
427 ost->ltd_qos.ltq_penalty_per_obj >> 10,
428 ost->ltd_qos.ltq_penalty >> 10,
429 ost->ltd_qos.ltq_svr->lsq_penalty_per_obj >> 10,
430 ost->ltd_qos.ltq_svr->lsq_penalty >> 10,
431 ost->ltd_qos.ltq_weight >> 10);
437 #define LOV_QOS_EMPTY ((__u32)-1)
440 * Calculate optimal round-robin order with regard to OSSes.
442 * Place all the OSTs from pool \a src_pool in a special array to be used for
443 * round-robin (RR) stripe allocation. The placement algorithm interleaves
444 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
445 * Resorts the targets when the number of active targets changes (because of
446 * a new target or activation/deactivation).
448 * \param[in] lod LOD device
449 * \param[in] src_pool OST pool
450 * \param[in] lqr round-robin list
452 * \retval 0 on success
453 * \retval -ENOMEM fails to allocate the array
455 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
456 struct lu_qos_rr *lqr)
458 struct lu_svr_qos *oss;
459 struct lod_tgt_desc *ost;
460 unsigned placed, real_count;
465 if (!lqr->lqr_dirty) {
466 LASSERT(lqr->lqr_pool.op_size);
470 /* Do actual allocation. */
471 down_write(&lod->lod_qos.lq_rw_sem);
474 * Check again. While we were sleeping on @lq_rw_sem something could
477 if (!lqr->lqr_dirty) {
478 LASSERT(lqr->lqr_pool.op_size);
479 up_write(&lod->lod_qos.lq_rw_sem);
483 real_count = src_pool->op_count;
485 /* Zero the pool array */
486 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
487 deleting from the pool. The lq_rw_sem insures that nobody else
489 lqr->lqr_pool.op_count = real_count;
490 rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
492 up_write(&lod->lod_qos.lq_rw_sem);
495 for (i = 0; i < lqr->lqr_pool.op_count; i++)
496 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
498 /* Place all the OSTs from 1 OSS at the same time. */
500 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
503 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
506 if (!cfs_bitmap_check(lod->lod_ost_bitmap,
507 src_pool->op_array[i]))
510 ost = OST_TGT(lod,src_pool->op_array[i]);
511 LASSERT(ost && ost->ltd_ost);
512 if (ost->ltd_qos.ltq_svr != oss)
515 /* Evenly space these OSTs across arrayspace */
516 next = j * lqr->lqr_pool.op_count / oss->lsq_tgt_count;
517 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
518 next = (next + 1) % lqr->lqr_pool.op_count;
520 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
527 up_write(&lod->lod_qos.lq_rw_sem);
529 if (placed != real_count) {
530 /* This should never happen */
531 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
532 "round-robin list (%d of %d).\n",
534 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
535 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
536 lqr->lqr_pool.op_array[i]);
543 for (i = 0; i < lqr->lqr_pool.op_count; i++)
544 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
551 * Instantiate and declare creation of a new object.
553 * The function instantiates LU representation for a new object on the
554 * specified device. Also it declares an intention to create that
555 * object on the storage target.
557 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
558 * infrastructure - in the existing precreation framework we can't assign FID
559 * at this moment, we do this later once a transaction is started. So the
560 * special method instantiates FID-less object in the cache and later it
561 * will get a FID and proper placement in LU cache.
563 * \param[in] env execution environment for this thread
564 * \param[in] d LOD device
565 * \param[in] ost_idx OST target index where the object is being created
566 * \param[in] th transaction handle
568 * \retval object ptr on success, ERR_PTR() otherwise
570 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
571 struct lod_device *d,
575 struct lod_tgt_desc *ost;
576 struct lu_object *o, *n;
577 struct lu_device *nd;
578 struct dt_object *dt;
583 LASSERT(ost_idx < d->lod_osts_size);
584 ost = OST_TGT(d,ost_idx);
586 LASSERT(ost->ltd_ost);
588 nd = &ost->ltd_ost->dd_lu_dev;
591 * allocate anonymous object with zero fid, real fid
592 * will be assigned by OSP within transaction
593 * XXX: to be fixed with fully-functional OST fids
595 o = lu_object_anon(env, nd, NULL);
597 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
599 n = lu_object_locate(o->lo_header, nd->ld_type);
600 if (unlikely(n == NULL)) {
601 CERROR("can't find slice\n");
602 lu_object_put(env, o);
603 GOTO(out, dt = ERR_PTR(-EINVAL));
606 dt = container_of(n, struct dt_object, do_lu);
608 rc = lod_sub_declare_create(env, dt, NULL, NULL, NULL, th);
610 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
612 lu_object_put(env, o);
621 * Calculate a minimum acceptable stripe count.
623 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
624 * all stripes or 3/4 of stripes.
626 * \param[in] stripe_count number of stripes requested
627 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
629 * \retval acceptable stripecount
631 static int min_stripe_count(__u32 stripe_count, int flags)
633 return (flags & LOV_USES_DEFAULT_STRIPE ?
634 stripe_count - (stripe_count / 4) : stripe_count);
637 #define LOV_CREATE_RESEED_MULT 30
638 #define LOV_CREATE_RESEED_MIN 2000
641 * Initialize temporary OST-in-use array.
643 * Allocate or extend the array used to mark targets already assigned to a new
644 * striping so they are not used more than once.
646 * \param[in] env execution environment for this thread
647 * \param[in] stripes number of items needed in the array
649 * \retval 0 on success
650 * \retval -ENOMEM on error
652 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
655 struct lod_thread_info *info = lod_env_info(env);
657 if (info->lti_ea_store_size < sizeof(int) * stripes)
658 lod_ea_store_resize(info, stripes * sizeof(int));
659 if (info->lti_ea_store_size < sizeof(int) * stripes) {
660 CERROR("can't allocate memory for ost-in-use array\n");
663 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
668 * Remember a target in the array of used targets.
670 * Mark the given target as used for a new striping being created. The status
671 * of an OST in a striping can be checked with lod_qos_is_ost_used().
673 * \param[in] env execution environment for this thread
674 * \param[in] idx index in the array
675 * \param[in] ost OST target index to mark as used
677 static inline void lod_qos_ost_in_use(const struct lu_env *env,
680 struct lod_thread_info *info = lod_env_info(env);
681 int *osts = info->lti_ea_store;
683 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
688 * Check is OST used in a striping.
690 * Checks whether OST with the given index is marked as used in the temporary
691 * array (see lod_qos_ost_in_use()).
693 * \param[in] env execution environment for this thread
694 * \param[in] ost OST target index to check
695 * \param[in] stripes the number of items used in the array already
700 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
702 struct lod_thread_info *info = lod_env_info(env);
703 int *osts = info->lti_ea_store;
706 for (j = 0; j < stripes; j++) {
714 lod_obj_is_ost_use_skip_cb(const struct lu_env *env, struct lod_object *lo,
715 int comp_idx, struct lod_obj_stripe_cb_data *data)
717 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
719 return comp->llc_ost_indices == NULL;
723 lod_obj_is_ost_use_cb(const struct lu_env *env, struct lod_object *lo,
724 int comp_idx, struct lod_obj_stripe_cb_data *data)
726 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
729 for (i = 0; i < comp->llc_stripe_count; i++) {
730 if (comp->llc_ost_indices[i] == data->locd_ost_index) {
731 data->locd_ost_index = -1;
740 * Check is OST used in a composite layout
742 * \param[in] lo lod object
743 * \param[in] ost OST target index to check
745 * \retval false not used
748 static inline bool lod_comp_is_ost_used(const struct lu_env *env,
749 struct lod_object *lo, int ost)
751 struct lod_obj_stripe_cb_data data = { { 0 } };
753 data.locd_ost_index = ost;
754 data.locd_comp_skip_cb = lod_obj_is_ost_use_skip_cb;
755 data.locd_comp_cb = lod_obj_is_ost_use_cb;
757 (void)lod_obj_for_each_stripe(env, lo, NULL, &data);
759 return data.locd_ost_index == -1;
762 static inline void lod_avoid_update(struct lod_object *lo,
763 struct lod_avoid_guide *lag)
768 lag->lag_ost_avail--;
771 static inline bool lod_should_avoid_ost(struct lod_object *lo,
772 struct lod_avoid_guide *lag,
775 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
776 struct lod_tgt_desc *ost = OST_TGT(lod, index);
777 struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
781 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
782 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
788 * we've tried our best, all available OSTs have been used in
789 * overlapped components in the other mirror
791 if (lag->lag_ost_avail == 0)
795 for (i = 0; i < lag->lag_oaa_count; i++) {
796 if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
802 * if the OSS which OST[index] resides has not been used, we'd like to
808 /* if the OSS has been used, check whether the OST has been used */
809 if (!cfs_bitmap_check(lag->lag_ost_avoid_bitmap, index))
812 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
817 static int lod_check_and_reserve_ost(const struct lu_env *env,
818 struct lod_object *lo,
819 struct lod_layout_component *lod_comp,
820 struct obd_statfs *sfs, __u32 ost_idx,
821 __u32 speed, __u32 *s_idx,
822 struct dt_object **stripe,
827 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
828 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
830 __u32 stripe_idx = *s_idx;
834 rc = lod_statfs_and_check(env, lod, ost_idx, sfs);
839 * We expect number of precreated objects in f_ffree at
840 * the first iteration, skip OSPs with no objects ready
842 if (sfs->os_fprecreated == 0 && speed == 0) {
843 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
848 * try to use another OSP if this one is degraded
850 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
851 QOS_DEBUG("#%d: degraded\n", ost_idx);
856 * try not allocate on OST which has been used by other
859 if (speed == 0 && lod_comp_is_ost_used(env, lo, ost_idx)) {
860 QOS_DEBUG("iter %d: OST%d used by other component\n",
866 * try not allocate OSTs used by conflicting component of other mirrors
867 * for the first and second time.
869 if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
870 QOS_DEBUG("iter %d: OST%d used by conflicting mirror "
871 "component\n", speed, ost_idx);
875 /* do not put >1 objects on a single OST, except for overstriping */
876 if (lod_qos_is_ost_used(env, ost_idx, stripe_idx)) {
877 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
883 o = lod_qos_declare_object_on(env, lod, ost_idx, th);
885 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
886 ost_idx, (int) PTR_ERR(o));
892 * We've successfully declared (reserved) an object
894 lod_avoid_update(lo, lag);
895 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
896 stripe[stripe_idx] = o;
897 ost_indices[stripe_idx] = ost_idx;
898 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
906 * Allocate a striping using round-robin algorithm.
908 * Allocates a new striping using round-robin algorithm. The function refreshes
909 * all the internal structures (statfs cache, array of available OSTs sorted
910 * with regard to OSS, etc). The number of stripes required is taken from the
911 * object (must be prepared by the caller), but can change if the flag
912 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
913 * is trying to create a striping on the object in parallel. All the internal
914 * structures (like pools, etc) are protected and no additional locking is
915 * required. The function succeeds even if a single stripe is allocated. To save
916 * time we give priority to targets which already have objects precreated.
917 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
919 * \param[in] env execution environment for this thread
920 * \param[in] lo LOD object
921 * \param[out] stripe striping created
922 * \param[out] ost_indices ost indices of striping created
923 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
924 * \param[in] th transaction handle
925 * \param[in] comp_idx index of ldo_comp_entries
927 * \retval 0 on success
928 * \retval -ENOSPC if not enough OSTs are found
929 * \retval negative negated errno for other failures
931 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
932 struct dt_object **stripe, __u32 *ost_indices,
933 int flags, struct thandle *th, int comp_idx)
935 struct lod_layout_component *lod_comp;
936 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
937 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
938 struct pool_desc *pool = NULL;
939 struct ost_pool *osts;
940 struct lu_qos_rr *lqr;
941 unsigned int i, array_idx;
942 __u32 ost_start_idx_temp;
943 __u32 stripe_idx = 0;
944 __u32 stripe_count, stripe_count_min, ost_idx;
945 int rc, speed = 0, ost_connecting = 0;
946 int stripes_per_ost = 1;
947 bool overstriped = false;
950 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
951 lod_comp = &lo->ldo_comp_entries[comp_idx];
952 stripe_count = lod_comp->llc_stripe_count;
953 stripe_count_min = min_stripe_count(stripe_count, flags);
955 if (lod_comp->llc_pool != NULL)
956 pool = lod_find_pool(m, lod_comp->llc_pool);
959 down_read(&pool_tgt_rw_sem(pool));
960 osts = &(pool->pool_obds);
961 lqr = &(pool->pool_rr);
963 osts = &(m->lod_pool_info);
964 lqr = &(m->lod_qos.lq_rr);
967 rc = lod_qos_calc_rr(m, osts, lqr);
971 rc = lod_qos_ost_in_use_clear(env, stripe_count);
975 down_read(&m->lod_qos.lq_rw_sem);
976 spin_lock(&lqr->lqr_alloc);
977 if (--lqr->lqr_start_count <= 0) {
978 lqr->lqr_start_idx = prandom_u32_max(osts->op_count);
979 lqr->lqr_start_count =
980 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
981 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
982 } else if (stripe_count_min >= osts->op_count ||
983 lqr->lqr_start_idx > osts->op_count) {
984 /* If we have allocated from all of the OSTs, slowly
985 * precess the next start if the OST/stripe count isn't
986 * already doing this for us. */
987 lqr->lqr_start_idx %= osts->op_count;
988 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
989 ++lqr->lqr_offset_idx;
991 ost_start_idx_temp = lqr->lqr_start_idx;
995 QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
996 "active %d count %d\n",
997 lod_comp->llc_pool ? lod_comp->llc_pool : "",
998 stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
999 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
1001 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1003 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1005 for (i = 0; i < osts->op_count * stripes_per_ost
1006 && stripe_idx < stripe_count; i++) {
1007 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
1009 ++lqr->lqr_start_idx;
1010 ost_idx = lqr->lqr_pool.op_array[array_idx];
1012 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1013 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1014 stripe_idx, array_idx, ost_idx);
1016 if ((ost_idx == LOV_QOS_EMPTY) ||
1017 !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1020 /* Fail Check before osc_precreate() is called
1021 so we can only 'fail' single OSC. */
1022 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1025 spin_unlock(&lqr->lqr_alloc);
1026 rc = lod_check_and_reserve_ost(env, lo, lod_comp, sfs, ost_idx,
1027 speed, &stripe_idx, stripe,
1028 ost_indices, th, &overstriped);
1029 spin_lock(&lqr->lqr_alloc);
1031 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
1034 if ((speed < 2) && (stripe_idx < stripe_count_min)) {
1035 /* Try again, allowing slower OSCs */
1037 lqr->lqr_start_idx = ost_start_idx_temp;
1043 spin_unlock(&lqr->lqr_alloc);
1044 up_read(&m->lod_qos.lq_rw_sem);
1046 /* If there are enough OSTs, a component with overstriping requested
1047 * will not actually end up overstriped. The comp should reflect this.
1050 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1053 lod_comp->llc_stripe_count = stripe_idx;
1054 /* at least one stripe is allocated */
1057 /* nobody provided us with a single object */
1066 up_read(&pool_tgt_rw_sem(pool));
1067 /* put back ref got by lod_find_pool() */
1068 lod_pool_putref(pool);
1075 * Allocate a specific striping layout on a user defined set of OSTs.
1077 * Allocates new striping using the OST index range provided by the data from
1078 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1079 * OSTs are not considered. The exact order of OSTs requested by the user
1080 * is respected as much as possible depending on OST status. The number of
1081 * stripes needed and stripe offset are taken from the object. If that number
1082 * can not be met, then the function returns a failure and then it's the
1083 * caller's responsibility to release the stripes allocated. All the internal
1084 * structures are protected, but no concurrent allocation is allowed on the
1087 * \param[in] env execution environment for this thread
1088 * \param[in] lo LOD object
1089 * \param[out] stripe striping created
1090 * \param[out] ost_indices ost indices of striping created
1091 * \param[in] th transaction handle
1092 * \param[in] comp_idx index of ldo_comp_entries
1094 * \retval 0 on success
1095 * \retval -ENODEV OST index does not exist on file system
1096 * \retval -EINVAL requested OST index is invalid
1097 * \retval negative negated errno on error
1099 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1100 struct dt_object **stripe, __u32 *ost_indices,
1101 struct thandle *th, int comp_idx)
1103 struct lod_layout_component *lod_comp;
1104 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1105 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1106 struct dt_object *o;
1107 unsigned int array_idx = 0;
1108 int stripe_count = 0;
1113 /* for specific OSTs layout */
1114 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1115 lod_comp = &lo->ldo_comp_entries[comp_idx];
1116 LASSERT(lod_comp->llc_ostlist.op_array);
1117 LASSERT(lod_comp->llc_ostlist.op_count);
1119 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1123 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
1124 lod_comp->llc_stripe_offset =
1125 lod_comp->llc_ostlist.op_array[0];
1127 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1128 if (lod_comp->llc_ostlist.op_array[i] ==
1129 lod_comp->llc_stripe_offset) {
1134 if (i == lod_comp->llc_stripe_count) {
1136 "%s: start index %d not in the specified list of OSTs\n",
1137 lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1141 for (i = 0; i < lod_comp->llc_stripe_count;
1142 i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1143 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1145 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1150 /* do not put >1 objects on a single OST, except for
1153 if (lod_qos_is_ost_used(env, ost_idx, stripe_count) &&
1154 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
1159 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1160 if (rc < 0) /* this OSP doesn't feel well */
1163 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1167 "%s: can't declare new object on #%u: %d\n",
1168 lod2obd(m)->obd_name, ost_idx, rc);
1173 * We've successfully declared (reserved) an object
1175 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1176 stripe[stripe_count] = o;
1177 ost_indices[stripe_count] = ost_idx;
1185 * Allocate a striping on a predefined set of OSTs.
1187 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1188 * Full OSTs are not considered. The exact order of OSTs is not important and
1189 * varies depending on OST status. The allocation procedure prefers the targets
1190 * with precreated objects ready. The number of stripes needed and stripe
1191 * offset are taken from the object. If that number cannot be met, then the
1192 * function returns an error and then it's the caller's responsibility to
1193 * release the stripes allocated. All the internal structures are protected,
1194 * but no concurrent allocation is allowed on the same objects.
1196 * \param[in] env execution environment for this thread
1197 * \param[in] lo LOD object
1198 * \param[out] stripe striping created
1199 * \param[out] ost_indices ost indices of striping created
1200 * \param[in] flags not used
1201 * \param[in] th transaction handle
1202 * \param[in] comp_idx index of ldo_comp_entries
1204 * \retval 0 on success
1205 * \retval -ENOSPC if no OST objects are available at all
1206 * \retval -EFBIG if not enough OST objects are found
1207 * \retval -EINVAL requested offset is invalid
1208 * \retval negative errno on failure
1210 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1211 struct dt_object **stripe, __u32 *ost_indices,
1212 int flags, struct thandle *th, int comp_idx)
1214 struct lod_layout_component *lod_comp;
1215 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1216 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1217 struct dt_object *o;
1219 unsigned int i, array_idx, ost_count;
1220 int rc, stripe_num = 0;
1222 struct pool_desc *pool = NULL;
1223 struct ost_pool *osts;
1224 int stripes_per_ost = 1;
1225 bool overstriped = false;
1228 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1229 lod_comp = &lo->ldo_comp_entries[comp_idx];
1231 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1235 if (lod_comp->llc_pool != NULL)
1236 pool = lod_find_pool(m, lod_comp->llc_pool);
1239 down_read(&pool_tgt_rw_sem(pool));
1240 osts = &(pool->pool_obds);
1242 osts = &(m->lod_pool_info);
1245 ost_count = osts->op_count;
1248 /* search loi_ost_idx in ost array */
1250 for (i = 0; i < ost_count; i++) {
1251 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1256 if (i == ost_count) {
1257 CERROR("Start index %d not found in pool '%s'\n",
1258 lod_comp->llc_stripe_offset,
1259 lod_comp->llc_pool ? lod_comp->llc_pool : "");
1260 GOTO(out, rc = -EINVAL);
1263 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1265 (lod_comp->llc_stripe_count - 1)/ost_count + 1;
1267 for (i = 0; i < ost_count * stripes_per_ost;
1268 i++, array_idx = (array_idx + 1) % ost_count) {
1269 ost_idx = osts->op_array[array_idx];
1271 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1274 /* Fail Check before osc_precreate() is called
1275 so we can only 'fail' single OSC. */
1276 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1280 * do not put >1 objects on a single OST, except for
1281 * overstriping, where it is intended
1283 if (lod_qos_is_ost_used(env, ost_idx, stripe_num)) {
1284 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1291 * try not allocate on the OST used by other component
1293 if (speed == 0 && i != 0 &&
1294 lod_comp_is_ost_used(env, lo, ost_idx))
1297 /* Drop slow OSCs if we can, but not for requested start idx.
1299 * This means "if OSC is slow and it is not the requested
1300 * start OST, then it can be skipped, otherwise skip it only
1301 * if it is inactive/recovering/out-of-space." */
1303 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1305 /* this OSP doesn't feel well */
1310 * We expect number of precreated objects at the first
1311 * iteration. Skip OSPs with no objects ready. Don't apply
1312 * this logic to OST specified with stripe_offset.
1314 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1317 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1319 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1320 ost_idx, (int) PTR_ERR(o));
1325 * We've successfully declared (reserved) an object
1327 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1328 stripe[stripe_num] = o;
1329 ost_indices[stripe_num] = ost_idx;
1332 /* We have enough stripes */
1333 if (stripe_num == lod_comp->llc_stripe_count)
1337 /* Try again, allowing slower OSCs */
1342 /* If we were passed specific striping params, then a failure to
1343 * meet those requirements is an error, since we can't reallocate
1344 * that memory (it might be part of a larger array or something).
1346 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1347 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1348 lod_comp->llc_stripe_count);
1349 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1351 /* If there are enough OSTs, a component with overstriping requessted
1352 * will not actually end up overstriped. The comp should reflect this.
1354 if (rc == 0 && !overstriped)
1355 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1359 up_read(&pool_tgt_rw_sem(pool));
1360 /* put back ref got by lod_find_pool() */
1361 lod_pool_putref(pool);
1368 * Check whether QoS allocation should be used.
1370 * A simple helper to decide when QoS allocation should be used:
1371 * if it's just a single available target or the used space is
1372 * evenly distributed among the targets at the moment, then QoS
1373 * allocation algorithm should not be used.
1375 * \param[in] lod LOD device
1377 * \retval 0 should not be used
1378 * \retval 1 should be used
1380 static inline int lod_qos_is_usable(struct lod_device *lod)
1383 /* to be able to debug QoS code */
1387 /* Detect -EAGAIN early, before expensive lock is taken. */
1388 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1391 if (lod->lod_desc.ld_active_tgt_count < 2)
1398 * Allocate a striping using an algorithm with weights.
1400 * The function allocates OST objects to create a striping. The algorithm
1401 * used is based on weights (currently only using the free space), and it's
1402 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1403 * configuration (# of stripes, offset, pool) is taken from the object and
1404 * is prepared by the caller.
1406 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1407 * be met due to too few OSTs, then allocation fails. If the flag is passed
1408 * fewer than 3/4 of the requested number of stripes can be allocated, then
1411 * No concurrent allocation is allowed on the object and this must be ensured
1412 * by the caller. All the internal structures are protected by the function.
1414 * The algorithm has two steps: find available OSTs and calculate their
1415 * weights, then select the OSTs with their weights used as the probability.
1416 * An OST with a higher weight is proportionately more likely to be selected
1417 * than one with a lower weight.
1419 * \param[in] env execution environment for this thread
1420 * \param[in] lo LOD object
1421 * \param[out] stripe striping created
1422 * \param[out] ost_indices ost indices of striping created
1423 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1424 * \param[in] th transaction handle
1425 * \param[in] comp_idx index of ldo_comp_entries
1427 * \retval 0 on success
1428 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1429 * \retval -EINVAL requested OST index is invalid
1430 * \retval negative errno on failure
1432 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1433 struct dt_object **stripe, __u32 *ost_indices,
1434 int flags, struct thandle *th, int comp_idx)
1436 struct lod_layout_component *lod_comp;
1437 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1438 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1439 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1440 struct lod_tgt_desc *ost;
1441 struct dt_object *o;
1442 __u64 total_weight = 0;
1443 struct pool_desc *pool = NULL;
1444 struct ost_pool *osts;
1446 __u32 nfound, good_osts, stripe_count, stripe_count_min;
1447 bool overstriped = false;
1448 int stripes_per_ost = 1;
1452 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1453 lod_comp = &lo->ldo_comp_entries[comp_idx];
1454 stripe_count = lod_comp->llc_stripe_count;
1455 stripe_count_min = min_stripe_count(stripe_count, flags);
1456 if (stripe_count_min < 1)
1459 if (lod_comp->llc_pool != NULL)
1460 pool = lod_find_pool(lod, lod_comp->llc_pool);
1463 down_read(&pool_tgt_rw_sem(pool));
1464 osts = &(pool->pool_obds);
1466 osts = &(lod->lod_pool_info);
1469 /* Detect -EAGAIN early, before expensive lock is taken. */
1470 if (!lod_qos_is_usable(lod))
1471 GOTO(out_nolock, rc = -EAGAIN);
1473 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1475 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1477 /* Do actual allocation, use write lock here. */
1478 down_write(&lod->lod_qos.lq_rw_sem);
1481 * Check again, while we were sleeping on @lq_rw_sem things could
1484 if (!lod_qos_is_usable(lod))
1485 GOTO(out, rc = -EAGAIN);
1487 rc = lod_qos_calc_ppo(lod);
1491 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1496 /* Find all the OSTs that are valid stripe candidates */
1497 for (i = 0; i < osts->op_count; i++) {
1498 if (!cfs_bitmap_check(lod->lod_ost_bitmap, osts->op_array[i]))
1501 ost = OST_TGT(lod, osts->op_array[i]);
1502 ost->ltd_qos.ltq_usable = 0;
1504 rc = lod_statfs_and_check(env, lod, osts->op_array[i], sfs);
1506 /* this OSP doesn't feel well */
1510 if (sfs->os_state & OS_STATE_DEGRADED)
1513 /* Fail Check before osc_precreate() is called
1514 so we can only 'fail' single OSC. */
1515 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1516 osts->op_array[i] == 0)
1519 ost->ltd_qos.ltq_usable = 1;
1520 lod_qos_calc_weight(lod, osts->op_array[i]);
1521 total_weight += ost->ltd_qos.ltq_weight;
1526 QOS_DEBUG("found %d good osts\n", good_osts);
1528 if (good_osts < stripe_count_min)
1529 GOTO(out, rc = -EAGAIN);
1531 /* If we do not have enough OSTs for the requested stripe count, do not
1532 * put more stripes per OST than requested.
1534 if (stripe_count / stripes_per_ost > good_osts)
1535 stripe_count = good_osts * stripes_per_ost;
1537 /* Find enough OSTs with weighted random allocation. */
1539 while (nfound < stripe_count) {
1540 u64 rand, cur_weight;
1545 rand = lu_prandom_u64_max(total_weight);
1547 /* On average, this will hit larger-weighted OSTs more often.
1548 * 0-weight OSTs will always get used last (only when rand=0) */
1549 for (i = 0; i < osts->op_count; i++) {
1550 __u32 idx = osts->op_array[i];
1552 if (lod_should_avoid_ost(lo, lag, idx))
1555 ost = OST_TGT(lod, idx);
1557 if (!ost->ltd_qos.ltq_usable)
1560 cur_weight += ost->ltd_qos.ltq_weight;
1561 QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1562 "rand=%llu total_weight=%llu\n",
1563 stripe_count, nfound, cur_weight, rand,
1566 if (cur_weight < rand)
1569 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1571 * do not put >1 objects on a single OST, except for
1574 if ((lod_comp_is_ost_used(env, lo, idx)) &&
1575 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
1578 if (lod_qos_is_ost_used(env, idx, nfound)) {
1579 if (lod_comp->llc_pattern &
1580 LOV_PATTERN_OVERSTRIPING)
1586 o = lod_qos_declare_object_on(env, lod, idx, th);
1588 QOS_DEBUG("can't declare object on #%u: %d\n",
1589 idx, (int) PTR_ERR(o));
1593 lod_avoid_update(lo, lag);
1594 lod_qos_ost_in_use(env, nfound, idx);
1596 ost_indices[nfound] = idx;
1597 lod_qos_used(lod, osts, idx, &total_weight);
1604 /* no OST found on this iteration, give up */
1609 if (unlikely(nfound != stripe_count)) {
1611 * when the decision to use weighted algorithm was made
1612 * we had enough appropriate OSPs, but this state can
1613 * change anytime (no space on OST, broken connection, etc)
1614 * so it's possible OSP won't be able to provide us with
1615 * an object due to just changed state
1617 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1618 lod2obd(lod)->obd_name, stripe_count, nfound);
1619 for (i = 0; i < nfound; i++) {
1620 LASSERT(stripe[i] != NULL);
1621 dt_object_put(env, stripe[i]);
1625 /* makes sense to rebalance next time */
1626 lod->lod_qos.lq_dirty = 1;
1627 lod->lod_qos.lq_same_space = 0;
1632 /* If there are enough OSTs, a component with overstriping requessted
1633 * will not actually end up overstriped. The comp should reflect this.
1635 if (rc == 0 && !overstriped)
1636 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1639 up_write(&lod->lod_qos.lq_rw_sem);
1643 up_read(&pool_tgt_rw_sem(pool));
1644 /* put back ref got by lod_find_pool() */
1645 lod_pool_putref(pool);
1652 * Check stripe count the caller can use.
1654 * For new layouts (no initialized components), check the total size of the
1655 * layout against the maximum EA size from the backing file system. This
1656 * stops us from creating a layout which will be too large once initialized.
1658 * For existing layouts (with initialized components):
1659 * Find the maximal possible stripe count not greater than \a stripe_count.
1660 * If the provided stripe count is 0, then the filesystem's default is used.
1662 * \param[in] lod LOD device
1663 * \param[in] lo The lod_object
1664 * \param[in] stripe_count count the caller would like to use
1666 * \retval the maximum usable stripe count
1668 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
1669 __u16 stripe_count, bool overstriping)
1671 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1672 /* max stripe count is based on OSD ea size */
1673 unsigned int easize = lod->lod_osd_max_easize;
1678 stripe_count = lod->lod_desc.ld_default_stripe_count;
1681 /* Overstriping allows more stripes than targets */
1682 if (stripe_count > lod->lod_desc.ld_active_tgt_count && !overstriping)
1683 stripe_count = lod->lod_desc.ld_active_tgt_count;
1685 if (lo->ldo_is_composite) {
1686 struct lod_layout_component *lod_comp;
1687 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
1688 unsigned int init_comp_sz = 0;
1689 unsigned int total_comp_sz = 0;
1690 unsigned int comp_sz;
1692 header_sz += sizeof(struct lov_comp_md_entry_v1) *
1695 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1696 lod_comp = &lo->ldo_comp_entries[i];
1697 comp_sz = lov_mds_md_size(lod_comp->llc_stripe_count,
1699 total_comp_sz += comp_sz;
1700 if (lod_comp->llc_flags & LCME_FL_INIT)
1701 init_comp_sz += comp_sz;
1704 if (init_comp_sz > 0)
1705 total_comp_sz = init_comp_sz;
1707 header_sz += total_comp_sz;
1709 if (easize > header_sz)
1710 easize -= header_sz;
1715 max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
1717 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1721 * Create in-core respresentation for a fully-defined striping
1723 * When the caller passes a fully-defined striping (i.e. everything including
1724 * OST object FIDs are defined), then we still need to instantiate LU-cache
1725 * with the objects representing the stripes defined. This function completes
1728 * \param[in] env execution environment for this thread
1729 * \param[in] mo LOD object
1730 * \param[in] buf buffer containing the striping
1732 * \retval 0 on success
1733 * \retval negative negated errno on error
1735 int lod_use_defined_striping(const struct lu_env *env,
1736 struct lod_object *mo,
1737 const struct lu_buf *buf)
1739 struct lod_layout_component *lod_comp;
1740 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1741 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1742 struct lov_comp_md_v1 *comp_v1 = NULL;
1743 struct lov_ost_data_v1 *objs;
1750 mutex_lock(&mo->ldo_layout_mutex);
1751 lod_striping_free_nolock(env, mo);
1753 magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
1755 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
1756 magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
1757 GOTO(unlock, rc = -EINVAL);
1759 if (magic == LOV_MAGIC_COMP_V1) {
1760 comp_v1 = buf->lb_buf;
1761 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1763 GOTO(unlock, rc = -EINVAL);
1764 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1765 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
1767 mo->ldo_is_composite = 1;
1768 } else if (magic == LOV_MAGIC_FOREIGN) {
1769 struct lov_foreign_md *foreign;
1772 if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
1774 "buf len %zu < min lov_foreign_md size (%zu)\n",
1776 offsetof(typeof(*foreign), lfm_value));
1777 GOTO(out, rc = -EINVAL);
1779 foreign = (struct lov_foreign_md *)buf->lb_buf;
1780 length = foreign_size_le(foreign);
1781 if (buf->lb_len < length) {
1783 "buf len %zu < this lov_foreign_md size (%zu)\n",
1784 buf->lb_len, length);
1785 GOTO(out, rc = -EINVAL);
1788 /* just cache foreign LOV EA raw */
1789 rc = lod_alloc_foreign_lov(mo, length);
1792 memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
1795 mo->ldo_is_composite = 0;
1799 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1801 rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
1805 for (i = 0; i < comp_cnt; i++) {
1806 struct lu_extent *ext;
1810 lod_comp = &mo->ldo_comp_entries[i];
1812 if (mo->ldo_is_composite) {
1813 offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1814 v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
1815 v3 = (struct lov_mds_md_v3 *)v1;
1816 magic = le32_to_cpu(v1->lmm_magic);
1818 ext = &comp_v1->lcm_entries[i].lcme_extent;
1819 lod_comp->llc_extent.e_start =
1820 le64_to_cpu(ext->e_start);
1821 lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
1822 lod_comp->llc_flags =
1823 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
1824 if (lod_comp->llc_flags & LCME_FL_NOSYNC)
1825 lod_comp->llc_timestamp = le64_to_cpu(
1826 comp_v1->lcm_entries[i].lcme_timestamp);
1828 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
1829 if (lod_comp->llc_id == LCME_ID_INVAL)
1830 GOTO(out, rc = -EINVAL);
1834 if (magic == LOV_MAGIC_V1) {
1835 objs = &v1->lmm_objects[0];
1836 } else if (magic == LOV_MAGIC_V3) {
1837 objs = &v3->lmm_objects[0];
1838 if (v3->lmm_pool_name[0] != '\0')
1839 pool_name = v3->lmm_pool_name;
1841 CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
1842 GOTO(out, rc = -EINVAL);
1845 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
1846 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1847 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
1848 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1850 * The stripe_offset of an uninit-ed component is stored in
1851 * the lmm_layout_gen
1853 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
1854 lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
1855 lod_obj_set_pool(mo, i, pool_name);
1857 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
1858 !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
1859 !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
1860 rc = lod_initialize_objects(env, mo, objs, i);
1866 rc = lod_fill_mirrors(mo);
1870 lod_striping_free_nolock(env, mo);
1872 mutex_unlock(&mo->ldo_layout_mutex);
1878 * Parse suggested striping configuration.
1880 * The caller gets a suggested striping configuration from a number of sources
1881 * including per-directory default and applications. Then it needs to verify
1882 * the suggested striping is valid, apply missing bits and store the resulting
1883 * configuration in the object to be used by the allocator later. Must not be
1884 * called concurrently against the same object. It's OK to provide a
1885 * fully-defined striping.
1887 * \param[in] env execution environment for this thread
1888 * \param[in] lo LOD object
1889 * \param[in] buf buffer containing the striping
1891 * \retval 0 on success
1892 * \retval negative negated errno on error
1894 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
1895 const struct lu_buf *buf)
1897 struct lod_layout_component *lod_comp;
1898 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1899 struct lov_desc *desc = &d->lod_desc;
1900 struct lov_user_md_v1 *v1 = NULL;
1901 struct lov_user_md_v3 *v3 = NULL;
1902 struct lov_comp_md_v1 *comp_v1 = NULL;
1903 struct lov_foreign_md *lfm = NULL;
1904 char def_pool[LOV_MAXPOOLNAME + 1];
1911 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1914 memset(def_pool, 0, sizeof(def_pool));
1915 if (lo->ldo_comp_entries != NULL)
1916 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
1917 def_pool, sizeof(def_pool));
1919 /* free default striping info */
1920 if (lo->ldo_is_foreign)
1921 lod_free_foreign_lov(lo);
1923 lod_free_comp_entries(lo);
1925 rc = lod_verify_striping(d, lo, buf, false);
1931 comp_v1 = buf->lb_buf;
1932 /* {lmm,lfm}_magic position/length work for all LOV formats */
1933 magic = v1->lmm_magic;
1935 if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
1936 /* try to use as fully defined striping */
1937 rc = lod_use_defined_striping(env, lo, buf);
1942 case __swab32(LOV_USER_MAGIC_V1):
1943 lustre_swab_lov_user_md_v1(v1);
1944 magic = v1->lmm_magic;
1946 case LOV_USER_MAGIC_V1:
1948 case __swab32(LOV_USER_MAGIC_V3):
1949 lustre_swab_lov_user_md_v3(v3);
1950 magic = v3->lmm_magic;
1952 case LOV_USER_MAGIC_V3:
1954 case __swab32(LOV_USER_MAGIC_SPECIFIC):
1955 lustre_swab_lov_user_md_v3(v3);
1956 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1957 v3->lmm_stripe_count);
1958 magic = v3->lmm_magic;
1960 case LOV_USER_MAGIC_SPECIFIC:
1962 case __swab32(LOV_USER_MAGIC_COMP_V1):
1963 lustre_swab_lov_comp_md_v1(comp_v1);
1964 magic = comp_v1->lcm_magic;
1966 case LOV_USER_MAGIC_COMP_V1:
1968 case __swab32(LOV_USER_MAGIC_FOREIGN):
1970 __swab32s(&lfm->lfm_magic);
1971 __swab32s(&lfm->lfm_length);
1972 __swab32s(&lfm->lfm_type);
1973 __swab32s(&lfm->lfm_flags);
1974 magic = lfm->lfm_magic;
1976 case LOV_USER_MAGIC_FOREIGN:
1979 rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
1982 memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
1985 CERROR("%s: unrecognized magic %X\n",
1986 lod2obd(d)->obd_name, magic);
1990 lustre_print_user_md(D_OTHER, v1, "parse config");
1992 if (magic == LOV_USER_MAGIC_COMP_V1) {
1993 comp_cnt = comp_v1->lcm_entry_count;
1996 mirror_cnt = comp_v1->lcm_mirror_count + 1;
1998 lo->ldo_flr_state = LCM_FL_RDONLY;
1999 lo->ldo_is_composite = 1;
2003 lo->ldo_is_composite = 0;
2006 rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2010 LASSERT(lo->ldo_comp_entries);
2012 for (i = 0; i < comp_cnt; i++) {
2013 struct pool_desc *pool;
2014 struct lu_extent *ext;
2017 lod_comp = &lo->ldo_comp_entries[i];
2019 if (lo->ldo_is_composite) {
2020 v1 = (struct lov_user_md *)((char *)comp_v1 +
2021 comp_v1->lcm_entries[i].lcme_offset);
2022 ext = &comp_v1->lcm_entries[i].lcme_extent;
2023 lod_comp->llc_extent = *ext;
2024 lod_comp->llc_flags =
2025 comp_v1->lcm_entries[i].lcme_flags &
2030 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2031 v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2032 v3 = (struct lov_user_md_v3 *)v1;
2033 if (v3->lmm_pool_name[0] != '\0')
2034 pool_name = v3->lmm_pool_name;
2036 if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2037 rc = lod_comp_copy_ost_lists(lod_comp, v3);
2039 GOTO(free_comp, rc);
2043 if (pool_name == NULL && def_pool[0] != '\0')
2044 pool_name = def_pool;
2046 if (v1->lmm_pattern == 0)
2047 v1->lmm_pattern = LOV_PATTERN_RAID0;
2048 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2049 lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
2050 lov_pattern(v1->lmm_pattern) !=
2051 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
2052 CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2053 lod2obd(d)->obd_name, v1->lmm_pattern);
2054 GOTO(free_comp, rc = -EINVAL);
2057 lod_comp->llc_pattern = v1->lmm_pattern;
2058 lod_comp->llc_stripe_size = desc->ld_default_stripe_size;
2059 if (v1->lmm_stripe_size)
2060 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2062 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2063 if (v1->lmm_stripe_count ||
2064 lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2065 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2067 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2068 lod_obj_set_pool(lo, i, pool_name);
2070 LASSERT(ergo(lov_pattern(lod_comp->llc_pattern) ==
2071 LOV_PATTERN_MDT, lod_comp->llc_stripe_count == 0));
2073 if (pool_name == NULL)
2076 /* In the function below, .hs_keycmp resolves to
2077 * pool_hashkey_keycmp() */
2078 /* coverity[overrun-buffer-val] */
2079 pool = lod_find_pool(d, pool_name);
2083 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2084 rc = lod_check_index_in_pool(
2085 lod_comp->llc_stripe_offset, pool);
2087 lod_pool_putref(pool);
2088 CDEBUG(D_LAYOUT, "%s: invalid offset, %u\n",
2089 lod2obd(d)->obd_name,
2090 lod_comp->llc_stripe_offset);
2091 GOTO(free_comp, rc = -EINVAL);
2095 if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
2096 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
2097 lod_comp->llc_stripe_count = pool_tgt_count(pool);
2099 lod_pool_putref(pool);
2105 lod_free_comp_entries(lo);
2110 * prepare enough OST avoidance bitmap space
2112 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2114 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2115 struct lod_tgt_descs *ltds = &lod->lod_ost_descs;
2116 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2117 struct cfs_bitmap *bitmap = NULL;
2118 __u32 *new_oss = NULL;
2120 lag->lag_ost_avail = ltds->ltd_tgtnr;
2122 /* reset OSS avoid guide array */
2123 lag->lag_oaa_count = 0;
2124 if (lag->lag_oss_avoid_array && lag->lag_oaa_size < ltds->ltd_tgtnr) {
2125 OBD_FREE(lag->lag_oss_avoid_array,
2126 sizeof(__u32) * lag->lag_oaa_size);
2127 lag->lag_oss_avoid_array = NULL;
2128 lag->lag_oaa_size = 0;
2131 /* init OST avoid guide bitmap */
2132 if (lag->lag_ost_avoid_bitmap) {
2133 if (ltds->ltd_tgtnr <= lag->lag_ost_avoid_bitmap->size) {
2134 CFS_RESET_BITMAP(lag->lag_ost_avoid_bitmap);
2136 CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
2137 lag->lag_ost_avoid_bitmap = NULL;
2141 if (!lag->lag_ost_avoid_bitmap) {
2142 bitmap = CFS_ALLOCATE_BITMAP(ltds->ltd_tgtnr);
2147 if (!lag->lag_oss_avoid_array) {
2149 * usually there are multiple OSTs in one OSS, but we don't
2150 * know the exact OSS number, so we choose a safe option,
2151 * using OST count to allocate the array to store the OSS
2154 OBD_ALLOC(new_oss, sizeof(*new_oss) * ltds->ltd_tgtnr);
2156 CFS_FREE_BITMAP(bitmap);
2162 lag->lag_oss_avoid_array = new_oss;
2163 lag->lag_oaa_size = ltds->ltd_tgtnr;
2166 lag->lag_ost_avoid_bitmap = bitmap;
2172 * Collect information of used OSTs and OSSs in the overlapped components
2175 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2178 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2179 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2180 struct cfs_bitmap *bitmap = lag->lag_ost_avoid_bitmap;
2183 /* iterate mirrors */
2184 for (i = 0; i < lo->ldo_mirror_count; i++) {
2185 struct lod_layout_component *comp;
2188 * skip mirror containing component[comp_idx], we only
2189 * collect OSTs info of conflicting component in other mirrors,
2190 * so that during read, if OSTs of a mirror's component are
2191 * not available, we still have other mirror with different
2192 * OSTs to read the data.
2194 comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
2195 if (comp->llc_id != LCME_ID_INVAL &&
2196 mirror_id_of(comp->llc_id) ==
2197 mirror_id_of(lod_comp->llc_id))
2200 /* iterate components of a mirror */
2201 lod_foreach_mirror_comp(comp, lo, i) {
2203 * skip non-overlapped or un-instantiated components,
2204 * NOTE: don't use lod_comp_inited(comp) to judge
2205 * whether @comp has been inited, since during
2206 * declare phase, comp->llc_stripe has been allocated
2207 * while it's init flag not been set until the exec
2210 if (!lu_extent_is_overlapped(&comp->llc_extent,
2211 &lod_comp->llc_extent) ||
2216 * collect used OSTs index and OSS info from a
2219 for (j = 0; j < comp->llc_stripe_count; j++) {
2220 struct lod_tgt_desc *ost;
2221 struct lu_svr_qos *lsq;
2224 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2225 lsq = ost->ltd_qos.ltq_svr;
2227 if (cfs_bitmap_check(bitmap, ost->ltd_index))
2230 QOS_DEBUG("OST%d used in conflicting mirror "
2231 "component\n", ost->ltd_index);
2232 cfs_bitmap_set(bitmap, ost->ltd_index);
2233 lag->lag_ost_avail--;
2235 for (k = 0; k < lag->lag_oaa_count; k++) {
2236 if (lag->lag_oss_avoid_array[k] ==
2240 if (k == lag->lag_oaa_count) {
2241 lag->lag_oss_avoid_array[k] =
2243 lag->lag_oaa_count++;
2251 * Create a striping for an obejct.
2253 * The function creates a new striping for the object. The function tries QoS
2254 * algorithm first unless free space is distributed evenly among OSTs, but
2255 * by default RR algorithm is preferred due to internal concurrency (QoS is
2256 * serialized). The caller must ensure no concurrent calls to the function
2257 * are made against the same object.
2259 * \param[in] env execution environment for this thread
2260 * \param[in] lo LOD object
2261 * \param[in] attr attributes OST objects will be declared with
2262 * \param[in] th transaction handle
2263 * \param[in] comp_idx index of ldo_comp_entries
2265 * \retval 0 on success
2266 * \retval negative negated errno on error
2268 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2269 struct lu_attr *attr, struct thandle *th,
2272 struct lod_layout_component *lod_comp;
2273 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2275 int flag = LOV_USES_ASSIGNED_STRIPE;
2277 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2278 struct dt_object **stripe = NULL;
2279 __u32 *ost_indices = NULL;
2283 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2284 lod_comp = &lo->ldo_comp_entries[comp_idx];
2285 LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
2287 /* A released component is being created */
2288 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2291 /* A Data-on-MDT component is being created */
2292 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2295 if (likely(lod_comp->llc_stripe == NULL)) {
2297 * no striping has been created so far
2299 LASSERT(lod_comp->llc_stripe_count);
2301 * statfs and check OST targets now, since ld_active_tgt_count
2302 * could be changed if some OSTs are [de]activated manually.
2304 lod_qos_statfs_update(env, d);
2305 stripe_len = lod_get_stripe_count(d, lo,
2306 lod_comp->llc_stripe_count,
2307 lod_comp->llc_pattern &
2308 LOV_PATTERN_OVERSTRIPING);
2310 if (stripe_len == 0)
2311 GOTO(out, rc = -ERANGE);
2312 lod_comp->llc_stripe_count = stripe_len;
2313 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
2315 GOTO(out, rc = -ENOMEM);
2316 OBD_ALLOC(ost_indices, sizeof(*ost_indices) * stripe_len);
2318 GOTO(out, rc = -ENOMEM);
2320 lod_getref(&d->lod_ost_descs);
2321 /* XXX: support for non-0 files w/o objects */
2322 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2323 d->lod_desc.ld_tgt_count, stripe_len);
2325 if (lod_comp->llc_ostlist.op_array &&
2326 lod_comp->llc_ostlist.op_count) {
2327 rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2329 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2331 * collect OSTs and OSSs used in other mirrors whose
2332 * components cross the ldo_comp_entries[comp_idx]
2334 rc = lod_prepare_avoidance(env, lo);
2338 QOS_DEBUG("collecting conflict osts for comp[%d]\n",
2340 lod_collect_avoidance(lo, lag, comp_idx);
2342 rc = lod_alloc_qos(env, lo, stripe, ost_indices, flag,
2345 rc = lod_alloc_rr(env, lo, stripe, ost_indices,
2346 flag, th, comp_idx);
2348 rc = lod_alloc_specific(env, lo, stripe, ost_indices,
2349 flag, th, comp_idx);
2352 lod_putref(d, &d->lod_ost_descs);
2354 for (i = 0; i < stripe_len; i++)
2355 if (stripe[i] != NULL)
2356 dt_object_put(env, stripe[i]);
2357 lod_comp->llc_stripe_count = 0;
2359 lod_comp->llc_stripe = stripe;
2360 lod_comp->llc_ost_indices = ost_indices;
2361 lod_comp->llc_stripes_allocated = stripe_len;
2365 * lod_qos_parse_config() found supplied buf as a predefined
2366 * striping (not a hint), so it allocated all the object
2367 * now we need to create them
2369 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2370 struct dt_object *o;
2372 o = lod_comp->llc_stripe[i];
2375 rc = lod_sub_declare_create(env, o, attr, NULL,
2378 CERROR("can't declare create: %d\n", rc);
2383 * Clear LCME_FL_INIT for the component so that
2384 * lod_striping_create() can create the striping objects
2387 lod_comp_unset_init(lod_comp);
2393 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
2395 OBD_FREE(ost_indices,
2396 sizeof(*ost_indices) * stripe_len);
2401 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2402 struct lu_attr *attr, const struct lu_buf *buf,
2406 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2414 /* no OST available */
2415 /* XXX: should we be waiting a bit to prevent failures during
2416 * cluster initialization? */
2417 if (d->lod_ostnr == 0)
2421 * by this time, the object's ldo_stripe_count and ldo_stripe_size
2422 * contain default value for striping: taken from the parent
2423 * or from filesystem defaults
2425 * in case the caller is passing lovea with new striping config,
2426 * we may need to parse lovea and apply new configuration
2428 rc = lod_qos_parse_config(env, lo, buf);
2432 if (attr->la_valid & LA_SIZE)
2433 size = attr->la_size;
2436 * prepare OST object creation for the component covering file's
2437 * size, the 1st component (including plain layout file) is always
2440 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2441 struct lod_layout_component *lod_comp;
2442 struct lu_extent *extent;
2444 lod_comp = &lo->ldo_comp_entries[i];
2445 extent = &lod_comp->llc_extent;
2446 QOS_DEBUG("comp[%d] %lld "DEXT"\n", i, size, PEXT(extent));
2447 if (!lo->ldo_is_composite || size >= extent->e_start) {
2448 rc = lod_qos_prep_create(env, lo, attr, th, i);