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 int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
82 int index, struct obd_statfs *sfs,
83 struct obd_statfs_info *info)
85 struct lod_tgt_desc *ost;
90 ost = OST_TGT(d,index);
93 rc = dt_statfs(env, ost->ltd_ost, sfs, info);
95 if (rc == 0 && ((sfs->os_state & OS_STATE_ENOSPC) ||
96 (sfs->os_state & OS_STATE_ENOINO && sfs->os_fprecreated == 0)))
99 if (rc && rc != -ENOTCONN)
100 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
102 /* If the OST is readonly then we can't allocate objects there */
103 if (sfs->os_state & OS_STATE_READONLY)
106 /* object precreation is skipped on the OST with max_create_count=0 */
107 if (sfs->os_state & OS_STATE_NOPRECREATE)
110 /* check whether device has changed state (active, inactive) */
111 if (rc != 0 && ost->ltd_active) {
112 /* turned inactive? */
113 spin_lock(&d->lod_lock);
114 if (ost->ltd_active) {
117 ost->ltd_connecting = 1;
119 LASSERT(d->lod_desc.ld_active_tgt_count > 0);
120 d->lod_desc.ld_active_tgt_count--;
121 d->lod_qos.lq_dirty = 1;
122 d->lod_qos.lq_rr.lqr_dirty = 1;
123 CDEBUG(D_CONFIG, "%s: turns inactive\n",
124 ost->ltd_exp->exp_obd->obd_name);
126 spin_unlock(&d->lod_lock);
127 } else if (rc == 0 && ost->ltd_active == 0) {
129 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
130 "active tgt count %d, ost nr %d\n",
131 d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
132 spin_lock(&d->lod_lock);
133 if (ost->ltd_active == 0) {
135 ost->ltd_connecting = 0;
136 d->lod_desc.ld_active_tgt_count++;
137 d->lod_qos.lq_dirty = 1;
138 d->lod_qos.lq_rr.lqr_dirty = 1;
139 CDEBUG(D_CONFIG, "%s: turns active\n",
140 ost->ltd_exp->exp_obd->obd_name);
142 spin_unlock(&d->lod_lock);
149 * Maintain per-target statfs data.
151 * The function refreshes statfs data for all the targets every N seconds.
152 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
155 * \param[in] env execution environment for this thread
156 * \param[in] lod LOD device
158 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod)
160 struct obd_device *obd = lod2obd(lod);
161 struct ost_pool *osts = &(lod->lod_pool_info);
168 max_age = ktime_get_seconds() - 2 * lod->lod_desc.ld_qos_maxage;
170 if (obd->obd_osfs_age > max_age)
171 /* statfs data are quite recent, don't need to refresh it */
174 down_write(&lod->lod_qos.lq_rw_sem);
176 if (obd->obd_osfs_age > max_age)
179 for (i = 0; i < osts->op_count; i++) {
180 idx = osts->op_array[i];
181 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
182 if (lod_statfs_and_check(env, lod, idx,
183 &OST_TGT(lod, idx)->ltd_statfs, NULL))
185 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
186 /* recalculate weigths */
187 lod->lod_qos.lq_dirty = 1;
189 obd->obd_osfs_age = ktime_get_seconds();
192 up_write(&lod->lod_qos.lq_rw_sem);
197 * Calculate per-OST and per-OSS penalties
199 * Re-calculate penalties when the configuration changes, active targets
200 * change and after statfs refresh (all these are reflected by lq_dirty flag).
201 * On every OST and OSS: decay the penalty by half for every 8x the update
202 * interval that the device has been idle. That gives lots of time for the
203 * statfs information to be updated (which the penalty is only a proxy for),
204 * and avoids penalizing OSS/OSTs under light load.
205 * See lod_qos_calc_weight() for how penalties are factored into the weight.
207 * \param[in] lod LOD device
209 * \retval 0 on success
210 * \retval -EAGAIN the number of OSTs isn't enough
212 static int lod_qos_calc_ppo(struct lod_device *lod)
214 struct lu_svr_qos *oss;
215 __u64 ba_max, ba_min, temp;
223 if (!lod->lod_qos.lq_dirty)
226 num_active = lod->lod_desc.ld_active_tgt_count - 1;
228 GOTO(out, rc = -EAGAIN);
230 /* find bavail on each OSS */
231 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list)
233 lod->lod_qos.lq_active_svr_count = 0;
236 * How badly user wants to select OSTs "widely" (not recently chosen
237 * and not on recent OSS's). As opposed to "freely" (free space
240 prio_wide = 256 - lod->lod_qos.lq_prio_free;
242 ba_min = (__u64)(-1);
244 now = ktime_get_real_seconds();
245 /* Calculate OST penalty per object
246 * (lod ref taken in lod_qos_prep_create())
248 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
249 LASSERT(OST_TGT(lod,i));
250 temp = TGT_BAVAIL(i);
253 ba_min = min(temp, ba_min);
254 ba_max = max(temp, ba_max);
256 /* Count the number of usable OSS's */
257 if (OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail == 0)
258 lod->lod_qos.lq_active_svr_count++;
259 OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail += temp;
261 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
263 do_div(temp, num_active);
264 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
265 (temp * prio_wide) >> 8;
267 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
268 if (lod->lod_qos.lq_reset ||
269 age > 32 * lod->lod_desc.ld_qos_maxage)
270 OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
271 else if (age > lod->lod_desc.ld_qos_maxage)
272 /* Decay OST penalty. */
273 OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
274 (age / lod->lod_desc.ld_qos_maxage);
277 num_active = lod->lod_qos.lq_active_svr_count - 1;
278 if (num_active < 1) {
279 /* If there's only 1 OSS, we can't penalize it, so instead
280 we have to double the OST penalty */
282 cfs_foreach_bit(lod->lod_ost_bitmap, i)
283 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
286 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
287 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
288 temp = oss->lsq_bavail >> 1;
289 do_div(temp, oss->lsq_tgt_count * num_active);
290 oss->lsq_penalty_per_obj = (temp * prio_wide) >> 8;
292 age = (now - oss->lsq_used) >> 3;
293 if (lod->lod_qos.lq_reset ||
294 age > 32 * lod->lod_desc.ld_qos_maxage)
295 oss->lsq_penalty = 0;
296 else if (age > lod->lod_desc.ld_qos_maxage)
297 /* Decay OSS penalty. */
298 oss->lsq_penalty >>= age / lod->lod_desc.ld_qos_maxage;
301 lod->lod_qos.lq_dirty = 0;
302 lod->lod_qos.lq_reset = 0;
304 /* If each ost has almost same free space,
305 * do rr allocation for better creation performance */
306 lod->lod_qos.lq_same_space = 0;
307 if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
308 lod->lod_qos.lq_same_space = 1;
309 /* Reset weights for the next time we enter qos mode */
310 lod->lod_qos.lq_reset = 1;
316 if (!rc && lod->lod_qos.lq_same_space)
323 * Calculate weight for a given OST target.
325 * The final OST weight is the number of bytes available minus the OST and
326 * OSS penalties. See lod_qos_calc_ppo() for how penalties are calculated.
328 * \param[in] lod LOD device, where OST targets are listed
329 * \param[in] i OST target index
333 static int lod_qos_calc_weight(struct lod_device *lod, int i)
337 temp = TGT_BAVAIL(i);
338 temp2 = OST_TGT(lod, i)->ltd_qos.ltq_penalty +
339 OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_penalty;
341 OST_TGT(lod, i)->ltd_qos.ltq_weight = 0;
343 OST_TGT(lod, i)->ltd_qos.ltq_weight = temp - temp2;
348 * Re-calculate weights.
350 * The function is called when some OST target was used for a new object. In
351 * this case we should re-calculate all the weights to keep new allocations
354 * \param[in] lod LOD device
355 * \param[in] osts OST pool where a new object was placed
356 * \param[in] index OST target where a new object was placed
357 * \param[out] total_wt new total weight for the pool
361 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
362 __u32 index, __u64 *total_wt)
364 struct lod_tgt_desc *ost;
365 struct lu_svr_qos *oss;
369 ost = OST_TGT(lod,index);
372 /* Don't allocate on this devuce anymore, until the next alloc_qos */
373 ost->ltd_qos.ltq_usable = 0;
375 oss = ost->ltd_qos.ltq_svr;
377 /* Decay old penalty by half (we're adding max penalty, and don't
378 want it to run away.) */
379 ost->ltd_qos.ltq_penalty >>= 1;
380 oss->lsq_penalty >>= 1;
382 /* mark the OSS and OST as recently used */
383 ost->ltd_qos.ltq_used = oss->lsq_used = ktime_get_real_seconds();
385 /* Set max penalties for this OST and OSS */
386 ost->ltd_qos.ltq_penalty +=
387 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
388 oss->lsq_penalty += oss->lsq_penalty_per_obj *
389 lod->lod_qos.lq_active_svr_count;
391 /* Decrease all OSS penalties */
392 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
393 if (oss->lsq_penalty < oss->lsq_penalty_per_obj)
394 oss->lsq_penalty = 0;
396 oss->lsq_penalty -= oss->lsq_penalty_per_obj;
400 /* Decrease all OST penalties */
401 for (j = 0; j < osts->op_count; j++) {
404 i = osts->op_array[j];
405 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
408 ost = OST_TGT(lod,i);
411 if (ost->ltd_qos.ltq_penalty <
412 ost->ltd_qos.ltq_penalty_per_obj)
413 ost->ltd_qos.ltq_penalty = 0;
415 ost->ltd_qos.ltq_penalty -=
416 ost->ltd_qos.ltq_penalty_per_obj;
418 lod_qos_calc_weight(lod, i);
420 /* Recalc the total weight of usable osts */
421 if (ost->ltd_qos.ltq_usable)
422 *total_wt += ost->ltd_qos.ltq_weight;
424 QOS_DEBUG("recalc tgt %d usable=%d avail=%llu"
425 " ostppo=%llu ostp=%llu ossppo=%llu"
426 " ossp=%llu wt=%llu\n",
427 i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
428 ost->ltd_qos.ltq_penalty_per_obj >> 10,
429 ost->ltd_qos.ltq_penalty >> 10,
430 ost->ltd_qos.ltq_svr->lsq_penalty_per_obj >> 10,
431 ost->ltd_qos.ltq_svr->lsq_penalty >> 10,
432 ost->ltd_qos.ltq_weight >> 10);
438 void lod_qos_rr_init(struct lu_qos_rr *lqr)
440 spin_lock_init(&lqr->lqr_alloc);
444 #define LOV_QOS_EMPTY ((__u32)-1)
447 * Calculate optimal round-robin order with regard to OSSes.
449 * Place all the OSTs from pool \a src_pool in a special array to be used for
450 * round-robin (RR) stripe allocation. The placement algorithm interleaves
451 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
452 * Resorts the targets when the number of active targets changes (because of
453 * a new target or activation/deactivation).
455 * \param[in] lod LOD device
456 * \param[in] src_pool OST pool
457 * \param[in] lqr round-robin list
459 * \retval 0 on success
460 * \retval -ENOMEM fails to allocate the array
462 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
463 struct lu_qos_rr *lqr)
465 struct lu_svr_qos *oss;
466 struct lod_tgt_desc *ost;
467 unsigned placed, real_count;
472 if (!lqr->lqr_dirty) {
473 LASSERT(lqr->lqr_pool.op_size);
477 /* Do actual allocation. */
478 down_write(&lod->lod_qos.lq_rw_sem);
481 * Check again. While we were sleeping on @lq_rw_sem something could
484 if (!lqr->lqr_dirty) {
485 LASSERT(lqr->lqr_pool.op_size);
486 up_write(&lod->lod_qos.lq_rw_sem);
490 real_count = src_pool->op_count;
492 /* Zero the pool array */
493 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
494 deleting from the pool. The lq_rw_sem insures that nobody else
496 lqr->lqr_pool.op_count = real_count;
497 rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
499 up_write(&lod->lod_qos.lq_rw_sem);
502 for (i = 0; i < lqr->lqr_pool.op_count; i++)
503 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
505 /* Place all the OSTs from 1 OSS at the same time. */
507 list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
510 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
513 if (!cfs_bitmap_check(lod->lod_ost_bitmap,
514 src_pool->op_array[i]))
517 ost = OST_TGT(lod,src_pool->op_array[i]);
518 LASSERT(ost && ost->ltd_ost);
519 if (ost->ltd_qos.ltq_svr != oss)
522 /* Evenly space these OSTs across arrayspace */
523 next = j * lqr->lqr_pool.op_count / oss->lsq_tgt_count;
524 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
525 next = (next + 1) % lqr->lqr_pool.op_count;
527 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
534 up_write(&lod->lod_qos.lq_rw_sem);
536 if (placed != real_count) {
537 /* This should never happen */
538 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
539 "round-robin list (%d of %d).\n",
541 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
542 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
543 lqr->lqr_pool.op_array[i]);
550 for (i = 0; i < lqr->lqr_pool.op_count; i++)
551 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
558 * Instantiate and declare creation of a new object.
560 * The function instantiates LU representation for a new object on the
561 * specified device. Also it declares an intention to create that
562 * object on the storage target.
564 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
565 * infrastructure - in the existing precreation framework we can't assign FID
566 * at this moment, we do this later once a transaction is started. So the
567 * special method instantiates FID-less object in the cache and later it
568 * will get a FID and proper placement in LU cache.
570 * \param[in] env execution environment for this thread
571 * \param[in] d LOD device
572 * \param[in] ost_idx OST target index where the object is being created
573 * \param[in] th transaction handle
575 * \retval object ptr on success, ERR_PTR() otherwise
577 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
578 struct lod_device *d,
582 struct lod_tgt_desc *ost;
583 struct lu_object *o, *n;
584 struct lu_device *nd;
585 struct dt_object *dt;
590 LASSERT(ost_idx < d->lod_osts_size);
591 ost = OST_TGT(d,ost_idx);
593 LASSERT(ost->ltd_ost);
595 nd = &ost->ltd_ost->dd_lu_dev;
598 * allocate anonymous object with zero fid, real fid
599 * will be assigned by OSP within transaction
600 * XXX: to be fixed with fully-functional OST fids
602 o = lu_object_anon(env, nd, NULL);
604 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
606 n = lu_object_locate(o->lo_header, nd->ld_type);
607 if (unlikely(n == NULL)) {
608 CERROR("can't find slice\n");
609 lu_object_put(env, o);
610 GOTO(out, dt = ERR_PTR(-EINVAL));
613 dt = container_of(n, struct dt_object, do_lu);
615 rc = lod_sub_declare_create(env, dt, NULL, NULL, NULL, th);
617 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
619 lu_object_put(env, o);
628 * Calculate a minimum acceptable stripe count.
630 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
631 * all stripes or 3/4 of stripes.
633 * \param[in] stripe_count number of stripes requested
634 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
636 * \retval acceptable stripecount
638 static int min_stripe_count(__u32 stripe_count, int flags)
640 return (flags & LOV_USES_DEFAULT_STRIPE ?
641 stripe_count - (stripe_count / 4) : stripe_count);
644 #define LOV_CREATE_RESEED_MULT 30
645 #define LOV_CREATE_RESEED_MIN 2000
648 * Initialize temporary OST-in-use array.
650 * Allocate or extend the array used to mark targets already assigned to a new
651 * striping so they are not used more than once.
653 * \param[in] env execution environment for this thread
654 * \param[in] stripes number of items needed in the array
656 * \retval 0 on success
657 * \retval -ENOMEM on error
659 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
662 struct lod_thread_info *info = lod_env_info(env);
664 if (info->lti_ea_store_size < sizeof(int) * stripes)
665 lod_ea_store_resize(info, stripes * sizeof(int));
666 if (info->lti_ea_store_size < sizeof(int) * stripes) {
667 CERROR("can't allocate memory for ost-in-use array\n");
670 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
675 * Remember a target in the array of used targets.
677 * Mark the given target as used for a new striping being created. The status
678 * of an OST in a striping can be checked with lod_qos_is_ost_used().
680 * \param[in] env execution environment for this thread
681 * \param[in] idx index in the array
682 * \param[in] ost OST target index to mark as used
684 static inline void lod_qos_ost_in_use(const struct lu_env *env,
687 struct lod_thread_info *info = lod_env_info(env);
688 int *osts = info->lti_ea_store;
690 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
695 * Check is OST used in a striping.
697 * Checks whether OST with the given index is marked as used in the temporary
698 * array (see lod_qos_ost_in_use()).
700 * \param[in] env execution environment for this thread
701 * \param[in] ost OST target index to check
702 * \param[in] stripes the number of items used in the array already
707 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
709 struct lod_thread_info *info = lod_env_info(env);
710 int *osts = info->lti_ea_store;
713 for (j = 0; j < stripes; j++) {
721 lod_obj_is_ost_use_skip_cb(const struct lu_env *env, struct lod_object *lo,
722 int comp_idx, struct lod_obj_stripe_cb_data *data)
724 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
726 return comp->llc_ost_indices == NULL;
730 lod_obj_is_ost_use_cb(const struct lu_env *env, struct lod_object *lo,
731 int comp_idx, struct lod_obj_stripe_cb_data *data)
733 struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
736 for (i = 0; i < comp->llc_stripe_count; i++) {
737 if (comp->llc_ost_indices[i] == data->locd_ost_index) {
738 data->locd_ost_index = -1;
747 * Check is OST used in a composite layout
749 * \param[in] lo lod object
750 * \param[in] ost OST target index to check
752 * \retval false not used
755 static inline bool lod_comp_is_ost_used(const struct lu_env *env,
756 struct lod_object *lo, int ost)
758 struct lod_obj_stripe_cb_data data = { { 0 } };
760 data.locd_ost_index = ost;
761 data.locd_comp_skip_cb = lod_obj_is_ost_use_skip_cb;
762 data.locd_comp_cb = lod_obj_is_ost_use_cb;
764 (void)lod_obj_for_each_stripe(env, lo, NULL, &data);
766 return data.locd_ost_index == -1;
769 static inline void lod_avoid_update(struct lod_object *lo,
770 struct lod_avoid_guide *lag)
775 lag->lag_ost_avail--;
778 static inline bool lod_should_avoid_ost(struct lod_object *lo,
779 struct lod_avoid_guide *lag,
782 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
783 struct lod_tgt_desc *ost = OST_TGT(lod, index);
784 struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
788 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index)) {
789 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
795 * we've tried our best, all available OSTs have been used in
796 * overlapped components in the other mirror
798 if (lag->lag_ost_avail == 0)
802 for (i = 0; i < lag->lag_oaa_count; i++) {
803 if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
809 * if the OSS which OST[index] resides has not been used, we'd like to
815 /* if the OSS has been used, check whether the OST has been used */
816 if (!cfs_bitmap_check(lag->lag_ost_avoid_bitmap, index))
819 QOS_DEBUG("OST%d: been used in conflicting mirror component\n",
824 static int lod_check_and_reserve_ost(const struct lu_env *env,
825 struct lod_object *lo,
826 struct lod_layout_component *lod_comp,
827 struct obd_statfs *sfs, __u32 ost_idx,
828 __u32 speed, __u32 *s_idx,
829 struct dt_object **stripe,
834 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
835 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
837 __u32 stripe_idx = *s_idx;
841 rc = lod_statfs_and_check(env, lod, ost_idx, sfs, NULL);
846 * We expect number of precreated objects in f_ffree at
847 * the first iteration, skip OSPs with no objects ready
849 if (sfs->os_fprecreated == 0 && speed == 0) {
850 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
855 * try to use another OSP if this one is degraded
857 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
858 QOS_DEBUG("#%d: degraded\n", ost_idx);
863 * try not allocate on OST which has been used by other
866 if (speed == 0 && lod_comp_is_ost_used(env, lo, ost_idx)) {
867 QOS_DEBUG("iter %d: OST%d used by other component\n",
873 * try not allocate OSTs used by conflicting component of other mirrors
874 * for the first and second time.
876 if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
877 QOS_DEBUG("iter %d: OST%d used by conflicting mirror "
878 "component\n", speed, ost_idx);
882 /* do not put >1 objects on a single OST, except for overstriping */
883 if (lod_qos_is_ost_used(env, ost_idx, stripe_idx)) {
884 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
890 o = lod_qos_declare_object_on(env, lod, ost_idx, th);
892 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
893 ost_idx, (int) PTR_ERR(o));
899 * We've successfully declared (reserved) an object
901 lod_avoid_update(lo, lag);
902 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
903 stripe[stripe_idx] = o;
904 ost_indices[stripe_idx] = ost_idx;
905 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
913 * Allocate a striping using round-robin algorithm.
915 * Allocates a new striping using round-robin algorithm. The function refreshes
916 * all the internal structures (statfs cache, array of available OSTs sorted
917 * with regard to OSS, etc). The number of stripes required is taken from the
918 * object (must be prepared by the caller), but can change if the flag
919 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
920 * is trying to create a striping on the object in parallel. All the internal
921 * structures (like pools, etc) are protected and no additional locking is
922 * required. The function succeeds even if a single stripe is allocated. To save
923 * time we give priority to targets which already have objects precreated.
924 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
926 * \param[in] env execution environment for this thread
927 * \param[in] lo LOD object
928 * \param[out] stripe striping created
929 * \param[out] ost_indices ost indices of striping created
930 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
931 * \param[in] th transaction handle
932 * \param[in] comp_idx index of ldo_comp_entries
934 * \retval 0 on success
935 * \retval -ENOSPC if not enough OSTs are found
936 * \retval negative negated errno for other failures
938 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
939 struct dt_object **stripe, __u32 *ost_indices,
940 int flags, struct thandle *th, int comp_idx)
942 struct lod_layout_component *lod_comp;
943 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
944 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
945 struct pool_desc *pool = NULL;
946 struct ost_pool *osts;
947 struct lu_qos_rr *lqr;
948 unsigned int i, array_idx;
949 __u32 ost_start_idx_temp;
950 __u32 stripe_idx = 0;
951 __u32 stripe_count, stripe_count_min, ost_idx;
952 int rc, speed = 0, ost_connecting = 0;
953 int stripes_per_ost = 1;
954 bool overstriped = false;
957 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
958 lod_comp = &lo->ldo_comp_entries[comp_idx];
959 stripe_count = lod_comp->llc_stripe_count;
960 stripe_count_min = min_stripe_count(stripe_count, flags);
962 if (lod_comp->llc_pool != NULL)
963 pool = lod_find_pool(m, lod_comp->llc_pool);
966 down_read(&pool_tgt_rw_sem(pool));
967 osts = &(pool->pool_obds);
968 lqr = &(pool->pool_rr);
970 osts = &(m->lod_pool_info);
971 lqr = &(m->lod_qos.lq_rr);
974 rc = lod_qos_calc_rr(m, osts, lqr);
978 rc = lod_qos_ost_in_use_clear(env, stripe_count);
982 down_read(&m->lod_qos.lq_rw_sem);
983 spin_lock(&lqr->lqr_alloc);
984 if (--lqr->lqr_start_count <= 0) {
985 lqr->lqr_start_idx = prandom_u32_max(osts->op_count);
986 lqr->lqr_start_count =
987 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
988 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
989 } else if (stripe_count_min >= osts->op_count ||
990 lqr->lqr_start_idx > osts->op_count) {
991 /* If we have allocated from all of the OSTs, slowly
992 * precess the next start if the OST/stripe count isn't
993 * already doing this for us. */
994 lqr->lqr_start_idx %= osts->op_count;
995 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
996 ++lqr->lqr_offset_idx;
998 ost_start_idx_temp = lqr->lqr_start_idx;
1002 QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
1003 "active %d count %d\n",
1004 lod_comp->llc_pool ? lod_comp->llc_pool : "",
1005 stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
1006 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
1008 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1010 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1012 for (i = 0; i < osts->op_count * stripes_per_ost
1013 && stripe_idx < stripe_count; i++) {
1014 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
1016 ++lqr->lqr_start_idx;
1017 ost_idx = lqr->lqr_pool.op_array[array_idx];
1019 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1020 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1021 stripe_idx, array_idx, ost_idx);
1023 if ((ost_idx == LOV_QOS_EMPTY) ||
1024 !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1027 /* Fail Check before osc_precreate() is called
1028 so we can only 'fail' single OSC. */
1029 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1032 spin_unlock(&lqr->lqr_alloc);
1033 rc = lod_check_and_reserve_ost(env, lo, lod_comp, sfs, ost_idx,
1034 speed, &stripe_idx, stripe,
1035 ost_indices, th, &overstriped);
1036 spin_lock(&lqr->lqr_alloc);
1038 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
1041 if ((speed < 2) && (stripe_idx < stripe_count_min)) {
1042 /* Try again, allowing slower OSCs */
1044 lqr->lqr_start_idx = ost_start_idx_temp;
1050 spin_unlock(&lqr->lqr_alloc);
1051 up_read(&m->lod_qos.lq_rw_sem);
1053 /* If there are enough OSTs, a component with overstriping requested
1054 * will not actually end up overstriped. The comp should reflect this.
1057 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1060 lod_comp->llc_stripe_count = stripe_idx;
1061 /* at least one stripe is allocated */
1064 /* nobody provided us with a single object */
1073 up_read(&pool_tgt_rw_sem(pool));
1074 /* put back ref got by lod_find_pool() */
1075 lod_pool_putref(pool);
1082 * Allocate a specific striping layout on a user defined set of OSTs.
1084 * Allocates new striping using the OST index range provided by the data from
1085 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1086 * OSTs are not considered. The exact order of OSTs requested by the user
1087 * is respected as much as possible depending on OST status. The number of
1088 * stripes needed and stripe offset are taken from the object. If that number
1089 * can not be met, then the function returns a failure and then it's the
1090 * caller's responsibility to release the stripes allocated. All the internal
1091 * structures are protected, but no concurrent allocation is allowed on the
1094 * \param[in] env execution environment for this thread
1095 * \param[in] lo LOD object
1096 * \param[out] stripe striping created
1097 * \param[out] ost_indices ost indices of striping created
1098 * \param[in] th transaction handle
1099 * \param[in] comp_idx index of ldo_comp_entries
1101 * \retval 0 on success
1102 * \retval -ENODEV OST index does not exist on file system
1103 * \retval -EINVAL requested OST index is invalid
1104 * \retval negative negated errno on error
1106 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1107 struct dt_object **stripe, __u32 *ost_indices,
1108 struct thandle *th, int comp_idx)
1110 struct lod_layout_component *lod_comp;
1111 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1112 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1113 struct dt_object *o;
1114 unsigned int array_idx = 0;
1115 int stripe_count = 0;
1120 /* for specific OSTs layout */
1121 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1122 lod_comp = &lo->ldo_comp_entries[comp_idx];
1123 LASSERT(lod_comp->llc_ostlist.op_array);
1124 LASSERT(lod_comp->llc_ostlist.op_count);
1126 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1130 if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
1131 lod_comp->llc_stripe_offset =
1132 lod_comp->llc_ostlist.op_array[0];
1134 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1135 if (lod_comp->llc_ostlist.op_array[i] ==
1136 lod_comp->llc_stripe_offset) {
1141 if (i == lod_comp->llc_stripe_count) {
1143 "%s: start index %d not in the specified list of OSTs\n",
1144 lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1148 for (i = 0; i < lod_comp->llc_stripe_count;
1149 i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1150 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1152 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1157 /* do not put >1 objects on a single OST, except for
1160 if (lod_qos_is_ost_used(env, ost_idx, stripe_count) &&
1161 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
1166 rc = lod_statfs_and_check(env, m, ost_idx, sfs, NULL);
1167 if (rc < 0) /* this OSP doesn't feel well */
1170 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1174 "%s: can't declare new object on #%u: %d\n",
1175 lod2obd(m)->obd_name, ost_idx, rc);
1180 * We've successfully declared (reserved) an object
1182 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1183 stripe[stripe_count] = o;
1184 ost_indices[stripe_count] = ost_idx;
1192 * Allocate a striping on a predefined set of OSTs.
1194 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1195 * Full OSTs are not considered. The exact order of OSTs is not important and
1196 * varies depending on OST status. The allocation procedure prefers the targets
1197 * with precreated objects ready. The number of stripes needed and stripe
1198 * offset are taken from the object. If that number cannot be met, then the
1199 * function returns an error and then it's the caller's responsibility to
1200 * release the stripes allocated. All the internal structures are protected,
1201 * but no concurrent allocation is allowed on the same objects.
1203 * \param[in] env execution environment for this thread
1204 * \param[in] lo LOD object
1205 * \param[out] stripe striping created
1206 * \param[out] ost_indices ost indices of striping created
1207 * \param[in] flags not used
1208 * \param[in] th transaction handle
1209 * \param[in] comp_idx index of ldo_comp_entries
1211 * \retval 0 on success
1212 * \retval -ENOSPC if no OST objects are available at all
1213 * \retval -EFBIG if not enough OST objects are found
1214 * \retval -EINVAL requested offset is invalid
1215 * \retval negative errno on failure
1217 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1218 struct dt_object **stripe, __u32 *ost_indices,
1219 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 obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1224 struct dt_object *o;
1226 unsigned int i, array_idx, ost_count;
1227 int rc, stripe_num = 0;
1229 struct pool_desc *pool = NULL;
1230 struct ost_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_ost_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_pool_info);
1252 ost_count = osts->op_count;
1255 /* search loi_ost_idx in ost array */
1257 for (i = 0; i < ost_count; i++) {
1258 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1263 if (i == ost_count) {
1264 CERROR("Start index %d not found in pool '%s'\n",
1265 lod_comp->llc_stripe_offset,
1266 lod_comp->llc_pool ? lod_comp->llc_pool : "");
1267 GOTO(out, rc = -EINVAL);
1270 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1272 (lod_comp->llc_stripe_count - 1)/ost_count + 1;
1274 for (i = 0; i < ost_count * stripes_per_ost;
1275 i++, array_idx = (array_idx + 1) % ost_count) {
1276 ost_idx = osts->op_array[array_idx];
1278 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1281 /* Fail Check before osc_precreate() is called
1282 so we can only 'fail' single OSC. */
1283 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1287 * do not put >1 objects on a single OST, except for
1288 * overstriping, where it is intended
1290 if (lod_qos_is_ost_used(env, ost_idx, stripe_num)) {
1291 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1298 * try not allocate on the OST used by other component
1300 if (speed == 0 && i != 0 &&
1301 lod_comp_is_ost_used(env, lo, ost_idx))
1304 /* Drop slow OSCs if we can, but not for requested start idx.
1306 * This means "if OSC is slow and it is not the requested
1307 * start OST, then it can be skipped, otherwise skip it only
1308 * if it is inactive/recovering/out-of-space." */
1310 rc = lod_statfs_and_check(env, m, ost_idx, sfs, NULL);
1312 /* this OSP doesn't feel well */
1317 * We expect number of precreated objects at the first
1318 * iteration. Skip OSPs with no objects ready. Don't apply
1319 * this logic to OST specified with stripe_offset.
1321 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1324 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1326 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1327 ost_idx, (int) PTR_ERR(o));
1332 * We've successfully declared (reserved) an object
1334 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1335 stripe[stripe_num] = o;
1336 ost_indices[stripe_num] = ost_idx;
1339 /* We have enough stripes */
1340 if (stripe_num == lod_comp->llc_stripe_count)
1344 /* Try again, allowing slower OSCs */
1349 /* If we were passed specific striping params, then a failure to
1350 * meet those requirements is an error, since we can't reallocate
1351 * that memory (it might be part of a larger array or something).
1353 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1354 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1355 lod_comp->llc_stripe_count);
1356 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1358 /* If there are enough OSTs, a component with overstriping requessted
1359 * will not actually end up overstriped. The comp should reflect this.
1361 if (rc == 0 && !overstriped)
1362 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1366 up_read(&pool_tgt_rw_sem(pool));
1367 /* put back ref got by lod_find_pool() */
1368 lod_pool_putref(pool);
1375 * Check whether QoS allocation should be used.
1377 * A simple helper to decide when QoS allocation should be used:
1378 * if it's just a single available target or the used space is
1379 * evenly distributed among the targets at the moment, then QoS
1380 * allocation algorithm should not be used.
1382 * \param[in] lod LOD device
1384 * \retval 0 should not be used
1385 * \retval 1 should be used
1387 static inline int lod_qos_is_usable(struct lod_device *lod)
1390 /* to be able to debug QoS code */
1394 /* Detect -EAGAIN early, before expensive lock is taken. */
1395 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1398 if (lod->lod_desc.ld_active_tgt_count < 2)
1405 * Allocate a striping using an algorithm with weights.
1407 * The function allocates OST objects to create a striping. The algorithm
1408 * used is based on weights (currently only using the free space), and it's
1409 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1410 * configuration (# of stripes, offset, pool) is taken from the object and
1411 * is prepared by the caller.
1413 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1414 * be met due to too few OSTs, then allocation fails. If the flag is passed
1415 * fewer than 3/4 of the requested number of stripes can be allocated, then
1418 * No concurrent allocation is allowed on the object and this must be ensured
1419 * by the caller. All the internal structures are protected by the function.
1421 * The algorithm has two steps: find available OSTs and calculate their
1422 * weights, then select the OSTs with their weights used as the probability.
1423 * An OST with a higher weight is proportionately more likely to be selected
1424 * than one with a lower weight.
1426 * \param[in] env execution environment for this thread
1427 * \param[in] lo LOD object
1428 * \param[out] stripe striping created
1429 * \param[out] ost_indices ost indices of striping created
1430 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1431 * \param[in] th transaction handle
1432 * \param[in] comp_idx index of ldo_comp_entries
1434 * \retval 0 on success
1435 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1436 * \retval -EINVAL requested OST index is invalid
1437 * \retval negative errno on failure
1439 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1440 struct dt_object **stripe, __u32 *ost_indices,
1441 int flags, struct thandle *th, int comp_idx)
1443 struct lod_layout_component *lod_comp;
1444 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1445 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1446 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1447 struct lod_tgt_desc *ost;
1448 struct dt_object *o;
1449 __u64 total_weight = 0;
1450 struct pool_desc *pool = NULL;
1451 struct ost_pool *osts;
1453 __u32 nfound, good_osts, stripe_count, stripe_count_min;
1454 bool overstriped = false;
1455 int stripes_per_ost = 1;
1459 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1460 lod_comp = &lo->ldo_comp_entries[comp_idx];
1461 stripe_count = lod_comp->llc_stripe_count;
1462 stripe_count_min = min_stripe_count(stripe_count, flags);
1463 if (stripe_count_min < 1)
1466 if (lod_comp->llc_pool != NULL)
1467 pool = lod_find_pool(lod, lod_comp->llc_pool);
1470 down_read(&pool_tgt_rw_sem(pool));
1471 osts = &(pool->pool_obds);
1473 osts = &(lod->lod_pool_info);
1476 /* Detect -EAGAIN early, before expensive lock is taken. */
1477 if (!lod_qos_is_usable(lod))
1478 GOTO(out_nolock, rc = -EAGAIN);
1480 if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
1482 (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
1484 /* Do actual allocation, use write lock here. */
1485 down_write(&lod->lod_qos.lq_rw_sem);
1488 * Check again, while we were sleeping on @lq_rw_sem things could
1491 if (!lod_qos_is_usable(lod))
1492 GOTO(out, rc = -EAGAIN);
1494 rc = lod_qos_calc_ppo(lod);
1498 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1503 /* Find all the OSTs that are valid stripe candidates */
1504 for (i = 0; i < osts->op_count; i++) {
1505 if (!cfs_bitmap_check(lod->lod_ost_bitmap, osts->op_array[i]))
1508 ost = OST_TGT(lod, osts->op_array[i]);
1509 ost->ltd_qos.ltq_usable = 0;
1511 rc = lod_statfs_and_check(env, lod, osts->op_array[i],
1514 /* this OSP doesn't feel well */
1518 if (sfs->os_state & OS_STATE_DEGRADED)
1521 /* Fail Check before osc_precreate() is called
1522 so we can only 'fail' single OSC. */
1523 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1524 osts->op_array[i] == 0)
1527 ost->ltd_qos.ltq_usable = 1;
1528 lod_qos_calc_weight(lod, osts->op_array[i]);
1529 total_weight += ost->ltd_qos.ltq_weight;
1534 QOS_DEBUG("found %d good osts\n", good_osts);
1536 if (good_osts < stripe_count_min)
1537 GOTO(out, rc = -EAGAIN);
1539 /* If we do not have enough OSTs for the requested stripe count, do not
1540 * put more stripes per OST than requested.
1542 if (stripe_count / stripes_per_ost > good_osts)
1543 stripe_count = good_osts * stripes_per_ost;
1545 /* Find enough OSTs with weighted random allocation. */
1547 while (nfound < stripe_count) {
1548 u64 rand, cur_weight;
1553 rand = lu_prandom_u64_max(total_weight);
1555 /* On average, this will hit larger-weighted OSTs more often.
1556 * 0-weight OSTs will always get used last (only when rand=0) */
1557 for (i = 0; i < osts->op_count; i++) {
1558 __u32 idx = osts->op_array[i];
1560 if (lod_should_avoid_ost(lo, lag, idx))
1563 ost = OST_TGT(lod, idx);
1565 if (!ost->ltd_qos.ltq_usable)
1568 cur_weight += ost->ltd_qos.ltq_weight;
1569 QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1570 "rand=%llu total_weight=%llu\n",
1571 stripe_count, nfound, cur_weight, rand,
1574 if (cur_weight < rand)
1577 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1579 * do not put >1 objects on a single OST, except for
1582 if ((lod_comp_is_ost_used(env, lo, idx)) &&
1583 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
1586 if (lod_qos_is_ost_used(env, idx, nfound)) {
1587 if (lod_comp->llc_pattern &
1588 LOV_PATTERN_OVERSTRIPING)
1594 o = lod_qos_declare_object_on(env, lod, idx, th);
1596 QOS_DEBUG("can't declare object on #%u: %d\n",
1597 idx, (int) PTR_ERR(o));
1601 lod_avoid_update(lo, lag);
1602 lod_qos_ost_in_use(env, nfound, idx);
1604 ost_indices[nfound] = idx;
1605 lod_qos_used(lod, osts, idx, &total_weight);
1612 /* no OST found on this iteration, give up */
1617 if (unlikely(nfound != stripe_count)) {
1619 * when the decision to use weighted algorithm was made
1620 * we had enough appropriate OSPs, but this state can
1621 * change anytime (no space on OST, broken connection, etc)
1622 * so it's possible OSP won't be able to provide us with
1623 * an object due to just changed state
1625 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1626 lod2obd(lod)->obd_name, stripe_count, nfound);
1627 for (i = 0; i < nfound; i++) {
1628 LASSERT(stripe[i] != NULL);
1629 dt_object_put(env, stripe[i]);
1633 /* makes sense to rebalance next time */
1634 lod->lod_qos.lq_dirty = 1;
1635 lod->lod_qos.lq_same_space = 0;
1640 /* If there are enough OSTs, a component with overstriping requessted
1641 * will not actually end up overstriped. The comp should reflect this.
1643 if (rc == 0 && !overstriped)
1644 lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
1647 up_write(&lod->lod_qos.lq_rw_sem);
1651 up_read(&pool_tgt_rw_sem(pool));
1652 /* put back ref got by lod_find_pool() */
1653 lod_pool_putref(pool);
1660 * Check stripe count the caller can use.
1662 * For new layouts (no initialized components), check the total size of the
1663 * layout against the maximum EA size from the backing file system. This
1664 * stops us from creating a layout which will be too large once initialized.
1666 * For existing layouts (with initialized components):
1667 * Find the maximal possible stripe count not greater than \a stripe_count.
1668 * If the provided stripe count is 0, then the filesystem's default is used.
1670 * \param[in] lod LOD device
1671 * \param[in] lo The lod_object
1672 * \param[in] stripe_count count the caller would like to use
1674 * \retval the maximum usable stripe count
1676 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
1677 __u16 stripe_count, bool overstriping)
1679 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1680 /* max stripe count is based on OSD ea size */
1681 unsigned int easize = lod->lod_osd_max_easize;
1686 stripe_count = lod->lod_desc.ld_default_stripe_count;
1689 /* Overstriping allows more stripes than targets */
1690 if (stripe_count > lod->lod_desc.ld_active_tgt_count && !overstriping)
1691 stripe_count = lod->lod_desc.ld_active_tgt_count;
1693 if (lo->ldo_is_composite) {
1694 struct lod_layout_component *lod_comp;
1695 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
1696 unsigned int init_comp_sz = 0;
1697 unsigned int total_comp_sz = 0;
1698 unsigned int comp_sz;
1700 header_sz += sizeof(struct lov_comp_md_entry_v1) *
1703 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1704 lod_comp = &lo->ldo_comp_entries[i];
1705 comp_sz = lov_mds_md_size(lod_comp->llc_stripe_count,
1707 total_comp_sz += comp_sz;
1708 if (lod_comp->llc_flags & LCME_FL_INIT)
1709 init_comp_sz += comp_sz;
1712 if (init_comp_sz > 0)
1713 total_comp_sz = init_comp_sz;
1715 header_sz += total_comp_sz;
1717 if (easize > header_sz)
1718 easize -= header_sz;
1723 max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
1725 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1729 * Create in-core respresentation for a fully-defined striping
1731 * When the caller passes a fully-defined striping (i.e. everything including
1732 * OST object FIDs are defined), then we still need to instantiate LU-cache
1733 * with the objects representing the stripes defined. This function completes
1736 * \param[in] env execution environment for this thread
1737 * \param[in] mo LOD object
1738 * \param[in] buf buffer containing the striping
1740 * \retval 0 on success
1741 * \retval negative negated errno on error
1743 int lod_use_defined_striping(const struct lu_env *env,
1744 struct lod_object *mo,
1745 const struct lu_buf *buf)
1747 struct lod_layout_component *lod_comp;
1748 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1749 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1750 struct lov_comp_md_v1 *comp_v1 = NULL;
1751 struct lov_ost_data_v1 *objs;
1758 mutex_lock(&mo->ldo_layout_mutex);
1759 lod_striping_free_nolock(env, mo);
1761 magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
1763 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
1764 magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
1765 GOTO(unlock, rc = -EINVAL);
1767 if (magic == LOV_MAGIC_COMP_V1) {
1768 comp_v1 = buf->lb_buf;
1769 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1771 GOTO(unlock, rc = -EINVAL);
1772 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1773 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
1775 mo->ldo_is_composite = 1;
1776 } else if (magic == LOV_MAGIC_FOREIGN) {
1777 struct lov_foreign_md *foreign;
1780 if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
1782 "buf len %zu < min lov_foreign_md size (%zu)\n",
1784 offsetof(typeof(*foreign), lfm_value));
1785 GOTO(out, rc = -EINVAL);
1787 foreign = (struct lov_foreign_md *)buf->lb_buf;
1788 length = foreign_size_le(foreign);
1789 if (buf->lb_len < length) {
1791 "buf len %zu < this lov_foreign_md size (%zu)\n",
1792 buf->lb_len, length);
1793 GOTO(out, rc = -EINVAL);
1796 /* just cache foreign LOV EA raw */
1797 rc = lod_alloc_foreign_lov(mo, length);
1800 memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
1803 mo->ldo_is_composite = 0;
1807 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1809 rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
1813 for (i = 0; i < comp_cnt; i++) {
1814 struct lu_extent *ext;
1818 lod_comp = &mo->ldo_comp_entries[i];
1820 if (mo->ldo_is_composite) {
1821 offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1822 v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
1823 v3 = (struct lov_mds_md_v3 *)v1;
1824 magic = le32_to_cpu(v1->lmm_magic);
1826 ext = &comp_v1->lcm_entries[i].lcme_extent;
1827 lod_comp->llc_extent.e_start =
1828 le64_to_cpu(ext->e_start);
1829 lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
1830 lod_comp->llc_flags =
1831 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
1832 if (lod_comp->llc_flags & LCME_FL_NOSYNC)
1833 lod_comp->llc_timestamp = le64_to_cpu(
1834 comp_v1->lcm_entries[i].lcme_timestamp);
1836 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
1837 if (lod_comp->llc_id == LCME_ID_INVAL)
1838 GOTO(out, rc = -EINVAL);
1842 if (magic == LOV_MAGIC_V1) {
1843 objs = &v1->lmm_objects[0];
1844 } else if (magic == LOV_MAGIC_V3) {
1845 objs = &v3->lmm_objects[0];
1846 if (v3->lmm_pool_name[0] != '\0')
1847 pool_name = v3->lmm_pool_name;
1849 CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
1850 GOTO(out, rc = -EINVAL);
1853 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
1854 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1855 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
1856 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1858 * The stripe_offset of an uninit-ed component is stored in
1859 * the lmm_layout_gen
1861 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
1862 lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
1863 lod_obj_set_pool(mo, i, pool_name);
1865 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
1866 !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
1867 !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
1868 rc = lod_initialize_objects(env, mo, objs, i);
1874 rc = lod_fill_mirrors(mo);
1878 lod_striping_free_nolock(env, mo);
1880 mutex_unlock(&mo->ldo_layout_mutex);
1886 * Parse suggested striping configuration.
1888 * The caller gets a suggested striping configuration from a number of sources
1889 * including per-directory default and applications. Then it needs to verify
1890 * the suggested striping is valid, apply missing bits and store the resulting
1891 * configuration in the object to be used by the allocator later. Must not be
1892 * called concurrently against the same object. It's OK to provide a
1893 * fully-defined striping.
1895 * \param[in] env execution environment for this thread
1896 * \param[in] lo LOD object
1897 * \param[in] buf buffer containing the striping
1899 * \retval 0 on success
1900 * \retval negative negated errno on error
1902 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
1903 const struct lu_buf *buf)
1905 struct lod_layout_component *lod_comp;
1906 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1907 struct lov_desc *desc = &d->lod_desc;
1908 struct lov_user_md_v1 *v1 = NULL;
1909 struct lov_user_md_v3 *v3 = NULL;
1910 struct lov_comp_md_v1 *comp_v1 = NULL;
1911 struct lov_foreign_md *lfm = NULL;
1912 char def_pool[LOV_MAXPOOLNAME + 1];
1919 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1922 memset(def_pool, 0, sizeof(def_pool));
1923 if (lo->ldo_comp_entries != NULL)
1924 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
1925 def_pool, sizeof(def_pool));
1927 /* free default striping info */
1928 if (lo->ldo_is_foreign)
1929 lod_free_foreign_lov(lo);
1931 lod_free_comp_entries(lo);
1933 rc = lod_verify_striping(d, lo, buf, false);
1939 comp_v1 = buf->lb_buf;
1940 /* {lmm,lfm}_magic position/length work for all LOV formats */
1941 magic = v1->lmm_magic;
1943 if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
1944 /* try to use as fully defined striping */
1945 rc = lod_use_defined_striping(env, lo, buf);
1950 case __swab32(LOV_USER_MAGIC_V1):
1951 lustre_swab_lov_user_md_v1(v1);
1952 magic = v1->lmm_magic;
1954 case LOV_USER_MAGIC_V1:
1956 case __swab32(LOV_USER_MAGIC_V3):
1957 lustre_swab_lov_user_md_v3(v3);
1958 magic = v3->lmm_magic;
1960 case LOV_USER_MAGIC_V3:
1962 case __swab32(LOV_USER_MAGIC_SPECIFIC):
1963 lustre_swab_lov_user_md_v3(v3);
1964 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1965 v3->lmm_stripe_count);
1966 magic = v3->lmm_magic;
1968 case LOV_USER_MAGIC_SPECIFIC:
1970 case __swab32(LOV_USER_MAGIC_COMP_V1):
1971 lustre_swab_lov_comp_md_v1(comp_v1);
1972 magic = comp_v1->lcm_magic;
1974 case LOV_USER_MAGIC_COMP_V1:
1976 case __swab32(LOV_USER_MAGIC_FOREIGN):
1978 __swab32s(&lfm->lfm_magic);
1979 __swab32s(&lfm->lfm_length);
1980 __swab32s(&lfm->lfm_type);
1981 __swab32s(&lfm->lfm_flags);
1982 magic = lfm->lfm_magic;
1984 case LOV_USER_MAGIC_FOREIGN:
1987 rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
1990 memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
1993 CERROR("%s: unrecognized magic %X\n",
1994 lod2obd(d)->obd_name, magic);
1998 lustre_print_user_md(D_OTHER, v1, "parse config");
2000 if (magic == LOV_USER_MAGIC_COMP_V1) {
2001 comp_cnt = comp_v1->lcm_entry_count;
2004 mirror_cnt = comp_v1->lcm_mirror_count + 1;
2006 lo->ldo_flr_state = LCM_FL_RDONLY;
2007 lo->ldo_is_composite = 1;
2011 lo->ldo_is_composite = 0;
2014 rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2018 LASSERT(lo->ldo_comp_entries);
2020 for (i = 0; i < comp_cnt; i++) {
2021 struct pool_desc *pool;
2022 struct lu_extent *ext;
2025 lod_comp = &lo->ldo_comp_entries[i];
2027 if (lo->ldo_is_composite) {
2028 v1 = (struct lov_user_md *)((char *)comp_v1 +
2029 comp_v1->lcm_entries[i].lcme_offset);
2030 ext = &comp_v1->lcm_entries[i].lcme_extent;
2031 lod_comp->llc_extent = *ext;
2032 lod_comp->llc_flags =
2033 comp_v1->lcm_entries[i].lcme_flags &
2038 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2039 v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2040 v3 = (struct lov_user_md_v3 *)v1;
2041 if (v3->lmm_pool_name[0] != '\0')
2042 pool_name = v3->lmm_pool_name;
2044 if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2045 rc = lod_comp_copy_ost_lists(lod_comp, v3);
2047 GOTO(free_comp, rc);
2051 if (pool_name == NULL && def_pool[0] != '\0')
2052 pool_name = def_pool;
2054 if (v1->lmm_pattern == 0)
2055 v1->lmm_pattern = LOV_PATTERN_RAID0;
2056 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2057 lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
2058 lov_pattern(v1->lmm_pattern) !=
2059 (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
2060 CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2061 lod2obd(d)->obd_name, v1->lmm_pattern);
2062 GOTO(free_comp, rc = -EINVAL);
2065 lod_comp->llc_pattern = v1->lmm_pattern;
2066 lod_comp->llc_stripe_size = desc->ld_default_stripe_size;
2067 if (v1->lmm_stripe_size)
2068 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2070 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2071 if (v1->lmm_stripe_count ||
2072 lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2073 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2075 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2076 lod_obj_set_pool(lo, i, pool_name);
2078 LASSERT(ergo(lov_pattern(lod_comp->llc_pattern) ==
2079 LOV_PATTERN_MDT, lod_comp->llc_stripe_count == 0));
2081 if (pool_name == NULL)
2084 /* In the function below, .hs_keycmp resolves to
2085 * pool_hashkey_keycmp() */
2086 /* coverity[overrun-buffer-val] */
2087 pool = lod_find_pool(d, pool_name);
2091 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2092 rc = lod_check_index_in_pool(
2093 lod_comp->llc_stripe_offset, pool);
2095 lod_pool_putref(pool);
2096 CDEBUG(D_LAYOUT, "%s: invalid offset, %u\n",
2097 lod2obd(d)->obd_name,
2098 lod_comp->llc_stripe_offset);
2099 GOTO(free_comp, rc = -EINVAL);
2103 if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
2104 !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
2105 lod_comp->llc_stripe_count = pool_tgt_count(pool);
2107 lod_pool_putref(pool);
2113 lod_free_comp_entries(lo);
2118 * prepare enough OST avoidance bitmap space
2120 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2122 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2123 struct lod_tgt_descs *ltds = &lod->lod_ost_descs;
2124 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2125 struct cfs_bitmap *bitmap = NULL;
2126 __u32 *new_oss = NULL;
2128 lag->lag_ost_avail = ltds->ltd_tgtnr;
2130 /* reset OSS avoid guide array */
2131 lag->lag_oaa_count = 0;
2132 if (lag->lag_oss_avoid_array && lag->lag_oaa_size < ltds->ltd_tgtnr) {
2133 OBD_FREE(lag->lag_oss_avoid_array,
2134 sizeof(__u32) * lag->lag_oaa_size);
2135 lag->lag_oss_avoid_array = NULL;
2136 lag->lag_oaa_size = 0;
2139 /* init OST avoid guide bitmap */
2140 if (lag->lag_ost_avoid_bitmap) {
2141 if (ltds->ltd_tgtnr <= lag->lag_ost_avoid_bitmap->size) {
2142 CFS_RESET_BITMAP(lag->lag_ost_avoid_bitmap);
2144 CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
2145 lag->lag_ost_avoid_bitmap = NULL;
2149 if (!lag->lag_ost_avoid_bitmap) {
2150 bitmap = CFS_ALLOCATE_BITMAP(ltds->ltd_tgtnr);
2155 if (!lag->lag_oss_avoid_array) {
2157 * usually there are multiple OSTs in one OSS, but we don't
2158 * know the exact OSS number, so we choose a safe option,
2159 * using OST count to allocate the array to store the OSS
2162 OBD_ALLOC(new_oss, sizeof(*new_oss) * ltds->ltd_tgtnr);
2164 CFS_FREE_BITMAP(bitmap);
2170 lag->lag_oss_avoid_array = new_oss;
2171 lag->lag_oaa_size = ltds->ltd_tgtnr;
2174 lag->lag_ost_avoid_bitmap = bitmap;
2180 * Collect information of used OSTs and OSSs in the overlapped components
2183 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2186 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2187 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2188 struct cfs_bitmap *bitmap = lag->lag_ost_avoid_bitmap;
2191 /* iterate mirrors */
2192 for (i = 0; i < lo->ldo_mirror_count; i++) {
2193 struct lod_layout_component *comp;
2196 * skip mirror containing component[comp_idx], we only
2197 * collect OSTs info of conflicting component in other mirrors,
2198 * so that during read, if OSTs of a mirror's component are
2199 * not available, we still have other mirror with different
2200 * OSTs to read the data.
2202 comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
2203 if (comp->llc_id != LCME_ID_INVAL &&
2204 mirror_id_of(comp->llc_id) ==
2205 mirror_id_of(lod_comp->llc_id))
2208 /* iterate components of a mirror */
2209 lod_foreach_mirror_comp(comp, lo, i) {
2211 * skip non-overlapped or un-instantiated components,
2212 * NOTE: don't use lod_comp_inited(comp) to judge
2213 * whether @comp has been inited, since during
2214 * declare phase, comp->llc_stripe has been allocated
2215 * while it's init flag not been set until the exec
2218 if (!lu_extent_is_overlapped(&comp->llc_extent,
2219 &lod_comp->llc_extent) ||
2224 * collect used OSTs index and OSS info from a
2227 for (j = 0; j < comp->llc_stripe_count; j++) {
2228 struct lod_tgt_desc *ost;
2229 struct lu_svr_qos *lsq;
2232 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2233 lsq = ost->ltd_qos.ltq_svr;
2235 if (cfs_bitmap_check(bitmap, ost->ltd_index))
2238 QOS_DEBUG("OST%d used in conflicting mirror "
2239 "component\n", ost->ltd_index);
2240 cfs_bitmap_set(bitmap, ost->ltd_index);
2241 lag->lag_ost_avail--;
2243 for (k = 0; k < lag->lag_oaa_count; k++) {
2244 if (lag->lag_oss_avoid_array[k] ==
2248 if (k == lag->lag_oaa_count) {
2249 lag->lag_oss_avoid_array[k] =
2251 lag->lag_oaa_count++;
2259 * Create a striping for an obejct.
2261 * The function creates a new striping for the object. The function tries QoS
2262 * algorithm first unless free space is distributed evenly among OSTs, but
2263 * by default RR algorithm is preferred due to internal concurrency (QoS is
2264 * serialized). The caller must ensure no concurrent calls to the function
2265 * are made against the same object.
2267 * \param[in] env execution environment for this thread
2268 * \param[in] lo LOD object
2269 * \param[in] attr attributes OST objects will be declared with
2270 * \param[in] th transaction handle
2271 * \param[in] comp_idx index of ldo_comp_entries
2273 * \retval 0 on success
2274 * \retval negative negated errno on error
2276 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2277 struct lu_attr *attr, struct thandle *th,
2280 struct lod_layout_component *lod_comp;
2281 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2283 int flag = LOV_USES_ASSIGNED_STRIPE;
2285 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2286 struct dt_object **stripe = NULL;
2287 __u32 *ost_indices = NULL;
2291 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2292 lod_comp = &lo->ldo_comp_entries[comp_idx];
2293 LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
2295 /* A released component is being created */
2296 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2299 /* A Data-on-MDT component is being created */
2300 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2303 if (likely(lod_comp->llc_stripe == NULL)) {
2305 * no striping has been created so far
2307 LASSERT(lod_comp->llc_stripe_count);
2309 * statfs and check OST targets now, since ld_active_tgt_count
2310 * could be changed if some OSTs are [de]activated manually.
2312 lod_qos_statfs_update(env, d);
2313 stripe_len = lod_get_stripe_count(d, lo,
2314 lod_comp->llc_stripe_count,
2315 lod_comp->llc_pattern &
2316 LOV_PATTERN_OVERSTRIPING);
2318 if (stripe_len == 0)
2319 GOTO(out, rc = -ERANGE);
2320 lod_comp->llc_stripe_count = stripe_len;
2321 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
2323 GOTO(out, rc = -ENOMEM);
2324 OBD_ALLOC(ost_indices, sizeof(*ost_indices) * stripe_len);
2326 GOTO(out, rc = -ENOMEM);
2328 lod_getref(&d->lod_ost_descs);
2329 /* XXX: support for non-0 files w/o objects */
2330 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2331 d->lod_desc.ld_tgt_count, stripe_len);
2333 if (lod_comp->llc_ostlist.op_array &&
2334 lod_comp->llc_ostlist.op_count) {
2335 rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2337 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2339 * collect OSTs and OSSs used in other mirrors whose
2340 * components cross the ldo_comp_entries[comp_idx]
2342 rc = lod_prepare_avoidance(env, lo);
2346 QOS_DEBUG("collecting conflict osts for comp[%d]\n",
2348 lod_collect_avoidance(lo, lag, comp_idx);
2350 rc = lod_alloc_qos(env, lo, stripe, ost_indices, flag,
2353 rc = lod_alloc_rr(env, lo, stripe, ost_indices,
2354 flag, th, comp_idx);
2356 rc = lod_alloc_specific(env, lo, stripe, ost_indices,
2357 flag, th, comp_idx);
2360 lod_putref(d, &d->lod_ost_descs);
2362 for (i = 0; i < stripe_len; i++)
2363 if (stripe[i] != NULL)
2364 dt_object_put(env, stripe[i]);
2365 lod_comp->llc_stripe_count = 0;
2367 lod_comp->llc_stripe = stripe;
2368 lod_comp->llc_ost_indices = ost_indices;
2369 lod_comp->llc_stripes_allocated = stripe_len;
2373 * lod_qos_parse_config() found supplied buf as a predefined
2374 * striping (not a hint), so it allocated all the object
2375 * now we need to create them
2377 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2378 struct dt_object *o;
2380 o = lod_comp->llc_stripe[i];
2383 rc = lod_sub_declare_create(env, o, attr, NULL,
2386 CERROR("can't declare create: %d\n", rc);
2391 * Clear LCME_FL_INIT for the component so that
2392 * lod_striping_create() can create the striping objects
2395 lod_comp_unset_init(lod_comp);
2401 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
2403 OBD_FREE(ost_indices,
2404 sizeof(*ost_indices) * stripe_len);
2409 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2410 struct lu_attr *attr, const struct lu_buf *buf,
2414 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2422 /* no OST available */
2423 /* XXX: should we be waiting a bit to prevent failures during
2424 * cluster initialization? */
2425 if (d->lod_ostnr == 0)
2429 * by this time, the object's ldo_stripe_count and ldo_stripe_size
2430 * contain default value for striping: taken from the parent
2431 * or from filesystem defaults
2433 * in case the caller is passing lovea with new striping config,
2434 * we may need to parse lovea and apply new configuration
2436 rc = lod_qos_parse_config(env, lo, buf);
2440 if (attr->la_valid & LA_SIZE)
2441 size = attr->la_size;
2444 * prepare OST object creation for the component covering file's
2445 * size, the 1st component (including plain layout file) is always
2448 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2449 struct lod_layout_component *lod_comp;
2450 struct lu_extent *extent;
2452 lod_comp = &lo->ldo_comp_entries[i];
2453 extent = &lod_comp->llc_extent;
2454 QOS_DEBUG("comp[%d] %lld "DEXT"\n", i, size, PEXT(extent));
2455 if (!lo->ldo_is_composite || size >= extent->e_start) {
2456 rc = lod_qos_prep_create(env, lo, attr, th, i);