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 <libcfs/libcfs.h>
42 #include <uapi/linux/lustre/lustre_idl.h>
43 #include <lustre_swab.h>
44 #include <obd_class.h>
46 #include "lod_internal.h"
49 * force QoS policy (not RR) to be used for testing purposes
55 #define QOS_DEBUG(fmt, ...) CDEBUG(D_QOS, fmt, ## __VA_ARGS__)
56 #define QOS_CONSOLE(fmt, ...) LCONSOLE(D_QOS, fmt, ## __VA_ARGS__)
58 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
59 OST_TGT(lod,i)->ltd_statfs.os_bsize)
62 * Add a new target to Quality of Service (QoS) target table.
64 * Add a new OST target to the structure representing an OSS. Resort the list
65 * of known OSSs by the number of OSTs attached to each OSS. The OSS list is
66 * protected internally and no external locking is required.
68 * \param[in] lod LOD device
69 * \param[in] ost_desc OST description
71 * \retval 0 on success
72 * \retval -ENOMEM on error
74 int qos_add_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
76 struct lod_qos_oss *oss = NULL, *temposs;
77 struct obd_export *exp = ost_desc->ltd_exp;
78 int rc = 0, found = 0;
79 struct list_head *list;
83 down_write(&lod->lod_qos.lq_rw_sem);
85 * a bit hacky approach to learn NID of corresponding connection
86 * but there is no official API to access information like this
89 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
90 if (obd_uuid_equals(&oss->lqo_uuid,
91 &exp->exp_connection->c_remote_uuid)) {
102 GOTO(out, rc = -ENOMEM);
103 memcpy(&oss->lqo_uuid, &exp->exp_connection->c_remote_uuid,
104 sizeof(oss->lqo_uuid));
108 /* Assume we have to move this one */
109 list_del(&oss->lqo_oss_list);
112 oss->lqo_ost_count++;
113 ost_desc->ltd_qos.ltq_oss = oss;
115 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
116 obd_uuid2str(&ost_desc->ltd_uuid), obd_uuid2str(&oss->lqo_uuid),
119 /* Add sorted by # of OSTs. Find the first entry that we're
121 list = &lod->lod_qos.lq_oss_list;
122 list_for_each_entry(temposs, list, lqo_oss_list) {
123 if (oss->lqo_ost_count > temposs->lqo_ost_count)
126 /* ...and add before it. If we're the first or smallest, temposs
127 points to the list head, and we add to the end. */
128 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
130 lod->lod_qos.lq_dirty = 1;
131 lod->lod_qos.lq_rr.lqr_dirty = 1;
134 up_write(&lod->lod_qos.lq_rw_sem);
139 * Remove OST target from QoS table.
141 * Removes given OST target from QoS table and releases related OSS structure
142 * if no OSTs remain on the OSS.
144 * \param[in] lod LOD device
145 * \param[in] ost_desc OST description
147 * \retval 0 on success
148 * \retval -ENOENT if no OSS was found
150 int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
152 struct lod_qos_oss *oss;
156 down_write(&lod->lod_qos.lq_rw_sem);
157 oss = ost_desc->ltd_qos.ltq_oss;
159 GOTO(out, rc = -ENOENT);
161 oss->lqo_ost_count--;
162 if (oss->lqo_ost_count == 0) {
163 CDEBUG(D_QOS, "removing OSS %s\n",
164 obd_uuid2str(&oss->lqo_uuid));
165 list_del(&oss->lqo_oss_list);
166 ost_desc->ltd_qos.ltq_oss = NULL;
170 lod->lod_qos.lq_dirty = 1;
171 lod->lod_qos.lq_rr.lqr_dirty = 1;
173 up_write(&lod->lod_qos.lq_rw_sem);
178 * Check whether the target is available for new OST objects.
180 * Request statfs data from the given target and verify it's active and not
181 * read-only. If so, then it can be used to place new OST objects. This
182 * function also maintains the number of active/inactive targets and sets
183 * dirty flags if those numbers change so others can run re-balance procedures.
184 * No external locking is required.
186 * \param[in] env execution environment for this thread
187 * \param[in] d LOD device
188 * \param[in] index index of OST target to check
189 * \param[out] sfs buffer for statfs data
191 * \retval 0 if the target is good
192 * \retval negative negated errno on error
195 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
196 int index, struct obd_statfs *sfs)
198 struct lod_tgt_desc *ost;
203 ost = OST_TGT(d,index);
206 rc = dt_statfs(env, ost->ltd_ost, sfs);
208 if (rc == 0 && ((sfs->os_state & OS_STATE_ENOSPC) ||
209 (sfs->os_state & OS_STATE_ENOINO && sfs->os_fprecreated == 0)))
212 if (rc && rc != -ENOTCONN)
213 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
215 /* If the OST is readonly then we can't allocate objects there */
216 if (sfs->os_state & OS_STATE_READONLY)
219 /* object precreation is skipped on the OST with max_create_count=0 */
220 if (sfs->os_state & OS_STATE_NOPRECREATE)
223 /* check whether device has changed state (active, inactive) */
224 if (rc != 0 && ost->ltd_active) {
225 /* turned inactive? */
226 spin_lock(&d->lod_lock);
227 if (ost->ltd_active) {
230 ost->ltd_connecting = 1;
232 LASSERT(d->lod_desc.ld_active_tgt_count > 0);
233 d->lod_desc.ld_active_tgt_count--;
234 d->lod_qos.lq_dirty = 1;
235 d->lod_qos.lq_rr.lqr_dirty = 1;
236 CDEBUG(D_CONFIG, "%s: turns inactive\n",
237 ost->ltd_exp->exp_obd->obd_name);
239 spin_unlock(&d->lod_lock);
240 } else if (rc == 0 && ost->ltd_active == 0) {
242 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
243 "active tgt count %d, ost nr %d\n",
244 d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
245 spin_lock(&d->lod_lock);
246 if (ost->ltd_active == 0) {
248 ost->ltd_connecting = 0;
249 d->lod_desc.ld_active_tgt_count++;
250 d->lod_qos.lq_dirty = 1;
251 d->lod_qos.lq_rr.lqr_dirty = 1;
252 CDEBUG(D_CONFIG, "%s: turns active\n",
253 ost->ltd_exp->exp_obd->obd_name);
255 spin_unlock(&d->lod_lock);
262 * Maintain per-target statfs data.
264 * The function refreshes statfs data for all the targets every N seconds.
265 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
268 * \param[in] env execution environment for this thread
269 * \param[in] lod LOD device
271 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod)
273 struct obd_device *obd = lod2obd(lod);
274 struct ost_pool *osts = &(lod->lod_pool_info);
281 max_age = ktime_get_seconds() - 2 * lod->lod_desc.ld_qos_maxage;
283 if (obd->obd_osfs_age > max_age)
284 /* statfs data are quite recent, don't need to refresh it */
287 down_write(&lod->lod_qos.lq_rw_sem);
289 if (obd->obd_osfs_age > max_age)
292 for (i = 0; i < osts->op_count; i++) {
293 idx = osts->op_array[i];
294 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
295 if (lod_statfs_and_check(env, lod, idx,
296 &OST_TGT(lod, idx)->ltd_statfs))
298 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
299 /* recalculate weigths */
300 lod->lod_qos.lq_dirty = 1;
302 obd->obd_osfs_age = ktime_get_seconds();
305 up_write(&lod->lod_qos.lq_rw_sem);
310 * Calculate per-OST and per-OSS penalties
312 * Re-calculate penalties when the configuration changes, active targets
313 * change and after statfs refresh (all these are reflected by lq_dirty flag).
314 * On every OST and OSS: decay the penalty by half for every 8x the update
315 * interval that the device has been idle. That gives lots of time for the
316 * statfs information to be updated (which the penalty is only a proxy for),
317 * and avoids penalizing OSS/OSTs under light load.
318 * See lod_qos_calc_weight() for how penalties are factored into the weight.
320 * \param[in] lod LOD device
322 * \retval 0 on success
323 * \retval -EAGAIN the number of OSTs isn't enough
325 static int lod_qos_calc_ppo(struct lod_device *lod)
327 struct lod_qos_oss *oss;
328 __u64 ba_max, ba_min, temp;
335 if (!lod->lod_qos.lq_dirty)
338 num_active = lod->lod_desc.ld_active_tgt_count - 1;
340 GOTO(out, rc = -EAGAIN);
342 /* find bavail on each OSS */
343 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
345 lod->lod_qos.lq_active_oss_count = 0;
348 * How badly user wants to select OSTs "widely" (not recently chosen
349 * and not on recent OSS's). As opposed to "freely" (free space
352 prio_wide = 256 - lod->lod_qos.lq_prio_free;
354 ba_min = (__u64)(-1);
356 now = ktime_get_real_seconds();
357 /* Calculate OST penalty per object
358 * (lod ref taken in lod_qos_prep_create())
360 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
361 LASSERT(OST_TGT(lod,i));
362 temp = TGT_BAVAIL(i);
365 ba_min = min(temp, ba_min);
366 ba_max = max(temp, ba_max);
368 /* Count the number of usable OSS's */
369 if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
370 lod->lod_qos.lq_active_oss_count++;
371 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
373 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
375 do_div(temp, num_active);
376 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
377 (temp * prio_wide) >> 8;
379 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
380 if (lod->lod_qos.lq_reset ||
381 age > 32 * lod->lod_desc.ld_qos_maxage)
382 OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
383 else if (age > lod->lod_desc.ld_qos_maxage)
384 /* Decay OST penalty. */
385 OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
386 (age / lod->lod_desc.ld_qos_maxage);
389 num_active = lod->lod_qos.lq_active_oss_count - 1;
390 if (num_active < 1) {
391 /* If there's only 1 OSS, we can't penalize it, so instead
392 we have to double the OST penalty */
394 cfs_foreach_bit(lod->lod_ost_bitmap, i)
395 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
398 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
399 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
400 temp = oss->lqo_bavail >> 1;
401 do_div(temp, oss->lqo_ost_count * num_active);
402 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
404 age = (now - oss->lqo_used) >> 3;
405 if (lod->lod_qos.lq_reset ||
406 age > 32 * lod->lod_desc.ld_qos_maxage)
407 oss->lqo_penalty = 0;
408 else if (age > lod->lod_desc.ld_qos_maxage)
409 /* Decay OSS penalty. */
410 oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
413 lod->lod_qos.lq_dirty = 0;
414 lod->lod_qos.lq_reset = 0;
416 /* If each ost has almost same free space,
417 * do rr allocation for better creation performance */
418 lod->lod_qos.lq_same_space = 0;
419 if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
420 lod->lod_qos.lq_same_space = 1;
421 /* Reset weights for the next time we enter qos mode */
422 lod->lod_qos.lq_reset = 1;
428 if (!rc && lod->lod_qos.lq_same_space)
435 * Calculate weight for a given OST target.
437 * The final OST weight is the number of bytes available minus the OST and
438 * OSS penalties. See lod_qos_calc_ppo() for how penalties are calculated.
440 * \param[in] lod LOD device, where OST targets are listed
441 * \param[in] i OST target index
445 static int lod_qos_calc_weight(struct lod_device *lod, int i)
449 temp = TGT_BAVAIL(i);
450 temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
451 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
453 OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
455 OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
460 * Re-calculate weights.
462 * The function is called when some OST target was used for a new object. In
463 * this case we should re-calculate all the weights to keep new allocations
466 * \param[in] lod LOD device
467 * \param[in] osts OST pool where a new object was placed
468 * \param[in] index OST target where a new object was placed
469 * \param[out] total_wt new total weight for the pool
473 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
474 __u32 index, __u64 *total_wt)
476 struct lod_tgt_desc *ost;
477 struct lod_qos_oss *oss;
481 ost = OST_TGT(lod,index);
484 /* Don't allocate on this devuce anymore, until the next alloc_qos */
485 ost->ltd_qos.ltq_usable = 0;
487 oss = ost->ltd_qos.ltq_oss;
489 /* Decay old penalty by half (we're adding max penalty, and don't
490 want it to run away.) */
491 ost->ltd_qos.ltq_penalty >>= 1;
492 oss->lqo_penalty >>= 1;
494 /* mark the OSS and OST as recently used */
495 ost->ltd_qos.ltq_used = oss->lqo_used = ktime_get_real_seconds();
497 /* Set max penalties for this OST and OSS */
498 ost->ltd_qos.ltq_penalty +=
499 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
500 oss->lqo_penalty += oss->lqo_penalty_per_obj *
501 lod->lod_qos.lq_active_oss_count;
503 /* Decrease all OSS penalties */
504 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
505 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
506 oss->lqo_penalty = 0;
508 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
512 /* Decrease all OST penalties */
513 for (j = 0; j < osts->op_count; j++) {
516 i = osts->op_array[j];
517 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
520 ost = OST_TGT(lod,i);
523 if (ost->ltd_qos.ltq_penalty <
524 ost->ltd_qos.ltq_penalty_per_obj)
525 ost->ltd_qos.ltq_penalty = 0;
527 ost->ltd_qos.ltq_penalty -=
528 ost->ltd_qos.ltq_penalty_per_obj;
530 lod_qos_calc_weight(lod, i);
532 /* Recalc the total weight of usable osts */
533 if (ost->ltd_qos.ltq_usable)
534 *total_wt += ost->ltd_qos.ltq_weight;
536 QOS_DEBUG("recalc tgt %d usable=%d avail=%llu"
537 " ostppo=%llu ostp=%llu ossppo=%llu"
538 " ossp=%llu wt=%llu\n",
539 i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
540 ost->ltd_qos.ltq_penalty_per_obj >> 10,
541 ost->ltd_qos.ltq_penalty >> 10,
542 ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
543 ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
544 ost->ltd_qos.ltq_weight >> 10);
550 void lod_qos_rr_init(struct lod_qos_rr *lqr)
552 spin_lock_init(&lqr->lqr_alloc);
557 #define LOV_QOS_EMPTY ((__u32)-1)
560 * Calculate optimal round-robin order with regard to OSSes.
562 * Place all the OSTs from pool \a src_pool in a special array to be used for
563 * round-robin (RR) stripe allocation. The placement algorithm interleaves
564 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
565 * Resorts the targets when the number of active targets changes (because of
566 * a new target or activation/deactivation).
568 * \param[in] lod LOD device
569 * \param[in] src_pool OST pool
570 * \param[in] lqr round-robin list
572 * \retval 0 on success
573 * \retval -ENOMEM fails to allocate the array
575 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
576 struct lod_qos_rr *lqr)
578 struct lod_qos_oss *oss;
579 struct lod_tgt_desc *ost;
580 unsigned placed, real_count;
585 if (!lqr->lqr_dirty) {
586 LASSERT(lqr->lqr_pool.op_size);
590 /* Do actual allocation. */
591 down_write(&lod->lod_qos.lq_rw_sem);
594 * Check again. While we were sleeping on @lq_rw_sem something could
597 if (!lqr->lqr_dirty) {
598 LASSERT(lqr->lqr_pool.op_size);
599 up_write(&lod->lod_qos.lq_rw_sem);
603 real_count = src_pool->op_count;
605 /* Zero the pool array */
606 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
607 deleting from the pool. The lq_rw_sem insures that nobody else
609 lqr->lqr_pool.op_count = real_count;
610 rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
612 up_write(&lod->lod_qos.lq_rw_sem);
615 for (i = 0; i < lqr->lqr_pool.op_count; i++)
616 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
618 /* Place all the OSTs from 1 OSS at the same time. */
620 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
623 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
626 if (!cfs_bitmap_check(lod->lod_ost_bitmap,
627 src_pool->op_array[i]))
630 ost = OST_TGT(lod,src_pool->op_array[i]);
631 LASSERT(ost && ost->ltd_ost);
632 if (ost->ltd_qos.ltq_oss != oss)
635 /* Evenly space these OSTs across arrayspace */
636 next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
637 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
638 next = (next + 1) % lqr->lqr_pool.op_count;
640 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
647 up_write(&lod->lod_qos.lq_rw_sem);
649 if (placed != real_count) {
650 /* This should never happen */
651 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
652 "round-robin list (%d of %d).\n",
654 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
655 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
656 lqr->lqr_pool.op_array[i]);
663 for (i = 0; i < lqr->lqr_pool.op_count; i++)
664 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
671 * Instantiate and declare creation of a new object.
673 * The function instantiates LU representation for a new object on the
674 * specified device. Also it declares an intention to create that
675 * object on the storage target.
677 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
678 * infrastructure - in the existing precreation framework we can't assign FID
679 * at this moment, we do this later once a transaction is started. So the
680 * special method instantiates FID-less object in the cache and later it
681 * will get a FID and proper placement in LU cache.
683 * \param[in] env execution environment for this thread
684 * \param[in] d LOD device
685 * \param[in] ost_idx OST target index where the object is being created
686 * \param[in] th transaction handle
688 * \retval object ptr on success, ERR_PTR() otherwise
690 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
691 struct lod_device *d,
695 struct lod_tgt_desc *ost;
696 struct lu_object *o, *n;
697 struct lu_device *nd;
698 struct dt_object *dt;
703 LASSERT(ost_idx < d->lod_osts_size);
704 ost = OST_TGT(d,ost_idx);
706 LASSERT(ost->ltd_ost);
708 nd = &ost->ltd_ost->dd_lu_dev;
711 * allocate anonymous object with zero fid, real fid
712 * will be assigned by OSP within transaction
713 * XXX: to be fixed with fully-functional OST fids
715 o = lu_object_anon(env, nd, NULL);
717 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
719 n = lu_object_locate(o->lo_header, nd->ld_type);
720 if (unlikely(n == NULL)) {
721 CERROR("can't find slice\n");
722 lu_object_put(env, o);
723 GOTO(out, dt = ERR_PTR(-EINVAL));
726 dt = container_of(n, struct dt_object, do_lu);
728 rc = lod_sub_declare_create(env, dt, NULL, NULL, NULL, th);
730 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
732 lu_object_put(env, o);
741 * Calculate a minimum acceptable stripe count.
743 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
744 * all stripes or 3/4 of stripes.
746 * \param[in] stripe_count number of stripes requested
747 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
749 * \retval acceptable stripecount
751 static int min_stripe_count(__u32 stripe_count, int flags)
753 return (flags & LOV_USES_DEFAULT_STRIPE ?
754 stripe_count - (stripe_count / 4) : stripe_count);
757 #define LOV_CREATE_RESEED_MULT 30
758 #define LOV_CREATE_RESEED_MIN 2000
761 * Initialize temporary OST-in-use array.
763 * Allocate or extend the array used to mark targets already assigned to a new
764 * striping so they are not used more than once.
766 * \param[in] env execution environment for this thread
767 * \param[in] stripes number of items needed in the array
769 * \retval 0 on success
770 * \retval -ENOMEM on error
772 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
775 struct lod_thread_info *info = lod_env_info(env);
777 if (info->lti_ea_store_size < sizeof(int) * stripes)
778 lod_ea_store_resize(info, stripes * sizeof(int));
779 if (info->lti_ea_store_size < sizeof(int) * stripes) {
780 CERROR("can't allocate memory for ost-in-use array\n");
783 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
788 * Remember a target in the array of used targets.
790 * Mark the given target as used for a new striping being created. The status
791 * of an OST in a striping can be checked with lod_qos_is_ost_used().
793 * \param[in] env execution environment for this thread
794 * \param[in] idx index in the array
795 * \param[in] ost OST target index to mark as used
797 static inline void lod_qos_ost_in_use(const struct lu_env *env,
800 struct lod_thread_info *info = lod_env_info(env);
801 int *osts = info->lti_ea_store;
803 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
808 * Check is OST used in a striping.
810 * Checks whether OST with the given index is marked as used in the temporary
811 * array (see lod_qos_ost_in_use()).
813 * \param[in] env execution environment for this thread
814 * \param[in] ost OST target index to check
815 * \param[in] stripes the number of items used in the array already
820 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
822 struct lod_thread_info *info = lod_env_info(env);
823 int *osts = info->lti_ea_store;
826 for (j = 0; j < stripes; j++) {
834 * Check is OST used in a composite layout
836 * \param[in] inuse all inuse ost indexs
837 * \param[in] ost OST target index to check
842 static inline int lod_comp_is_ost_used(struct ost_pool *inuse, int ost)
845 LASSERT(inuse != NULL);
847 if (inuse->op_size == 0)
850 LASSERT(inuse->op_count * sizeof(inuse->op_array[0]) <= inuse->op_size);
851 for (j = 0; j < inuse->op_count; j++) {
852 if (inuse->op_array[j] == ost)
859 * Mark the given target as used for a composite layout
861 * \param[in] inuse inuse ost index array
862 * \param[in] idx index in the array
864 static inline void lod_comp_ost_in_use(struct ost_pool *inuse, int ost)
866 LASSERT(inuse != NULL);
867 if (inuse->op_size && !lod_comp_is_ost_used(inuse, ost)) {
868 LASSERTF(inuse->op_count * sizeof(inuse->op_array[0]) <
870 "count %d size %u", inuse->op_count, inuse->op_size);
871 inuse->op_array[inuse->op_count] = ost;
876 static inline void lod_avoid_update(struct lod_object *lo,
877 struct lod_avoid_guide *lag)
882 lag->lag_ost_avail--;
885 static inline bool lod_should_avoid_ost(struct lod_object *lo,
886 struct lod_avoid_guide *lag,
889 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
890 struct lod_tgt_desc *ost = OST_TGT(lod, index);
891 struct lod_qos_oss *lqo = ost->ltd_qos.ltq_oss;
895 if (!cfs_bitmap_check(lod->lod_ost_bitmap, index))
899 * we've tried our best, all available OSTs have been used in
900 * overlapped components in the other mirror
902 if (lag->lag_ost_avail == 0)
906 for (i = 0; i < lag->lag_oaa_count; i++) {
907 if (lag->lag_oss_avoid_array[i] == lqo->lqo_id) {
913 * if the OSS which OST[index] resides has not been used, we'd like to
919 /* if the OSS has been used, check whether the OST has been used */
920 if (!cfs_bitmap_check(lag->lag_ost_avoid_bitmap, index))
923 QOS_DEBUG("OST%d: has been used in overlapped component "
924 "in other mirror\n", index);
928 static int lod_check_and_reserve_ost(const struct lu_env *env,
929 struct lod_object *lo,
930 struct obd_statfs *sfs, __u32 ost_idx,
931 __u32 speed, __u32 *s_idx,
932 struct dt_object **stripe,
935 struct ost_pool *inuse)
937 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
938 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
940 __u32 stripe_idx = *s_idx;
943 rc = lod_statfs_and_check(env, lod, ost_idx, sfs);
945 /* this OSP doesn't feel well */
950 * We expect number of precreated objects in f_ffree at
951 * the first iteration, skip OSPs with no objects ready
953 if (sfs->os_fprecreated == 0 && speed == 0) {
954 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
959 * try to use another OSP if this one is degraded
961 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
962 QOS_DEBUG("#%d: degraded\n", ost_idx);
967 * try not allocate on OST which has been used by other
970 if (speed == 0 && lod_comp_is_ost_used(inuse, ost_idx)) {
971 QOS_DEBUG("#%d: used by other component\n", ost_idx);
976 * try not allocate OSTs used by conflicting component of other mirrors
977 * for the first and second time.
979 if (speed < 2 && lod_should_avoid_ost(lo, lag, ost_idx)) {
980 QOS_DEBUG("#%d: used by overlapped component of other mirror\n",
985 * do not put >1 objects on a single OST
987 if (lod_qos_is_ost_used(env, ost_idx, stripe_idx))
990 o = lod_qos_declare_object_on(env, lod, ost_idx, th);
992 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
993 ost_idx, (int) PTR_ERR(o));
999 * We've successfully declared (reserved) an object
1001 lod_avoid_update(lo, lag);
1002 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
1003 lod_comp_ost_in_use(inuse, ost_idx);
1004 stripe[stripe_idx] = o;
1005 ost_indices[stripe_idx] = ost_idx;
1006 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
1008 *s_idx = stripe_idx;
1015 * Allocate a striping using round-robin algorithm.
1017 * Allocates a new striping using round-robin algorithm. The function refreshes
1018 * all the internal structures (statfs cache, array of available OSTs sorted
1019 * with regard to OSS, etc). The number of stripes required is taken from the
1020 * object (must be prepared by the caller), but can change if the flag
1021 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
1022 * is trying to create a striping on the object in parallel. All the internal
1023 * structures (like pools, etc) are protected and no additional locking is
1024 * required. The function succeeds even if a single stripe is allocated. To save
1025 * time we give priority to targets which already have objects precreated.
1026 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
1028 * \param[in] env execution environment for this thread
1029 * \param[in] lo LOD object
1030 * \param[out] stripe striping created
1031 * \param[out] ost_indices ost indices of striping created
1032 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
1033 * \param[in] th transaction handle
1034 * \param[in] comp_idx index of ldo_comp_entries
1035 * \param[in|out] inuse array of inuse ost index
1037 * \retval 0 on success
1038 * \retval -ENOSPC if not enough OSTs are found
1039 * \retval negative negated errno for other failures
1041 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
1042 struct dt_object **stripe, __u32 *ost_indices,
1043 int flags, struct thandle *th, int comp_idx,
1044 struct ost_pool *inuse)
1046 struct lod_layout_component *lod_comp;
1047 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1048 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1049 struct pool_desc *pool = NULL;
1050 struct ost_pool *osts;
1051 struct lod_qos_rr *lqr;
1052 unsigned int i, array_idx;
1053 __u32 ost_start_idx_temp;
1054 __u32 stripe_idx = 0;
1055 __u32 stripe_count, stripe_count_min, ost_idx;
1056 int rc, speed = 0, ost_connecting = 0;
1059 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1060 lod_comp = &lo->ldo_comp_entries[comp_idx];
1061 stripe_count = lod_comp->llc_stripe_count;
1062 stripe_count_min = min_stripe_count(stripe_count, flags);
1064 if (lod_comp->llc_pool != NULL)
1065 pool = lod_find_pool(m, lod_comp->llc_pool);
1068 down_read(&pool_tgt_rw_sem(pool));
1069 osts = &(pool->pool_obds);
1070 lqr = &(pool->pool_rr);
1072 osts = &(m->lod_pool_info);
1073 lqr = &(m->lod_qos.lq_rr);
1076 rc = lod_qos_calc_rr(m, osts, lqr);
1080 rc = lod_qos_ost_in_use_clear(env, stripe_count);
1084 down_read(&m->lod_qos.lq_rw_sem);
1085 spin_lock(&lqr->lqr_alloc);
1086 if (--lqr->lqr_start_count <= 0) {
1087 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
1088 lqr->lqr_start_count =
1089 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
1090 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
1091 } else if (stripe_count_min >= osts->op_count ||
1092 lqr->lqr_start_idx > osts->op_count) {
1093 /* If we have allocated from all of the OSTs, slowly
1094 * precess the next start if the OST/stripe count isn't
1095 * already doing this for us. */
1096 lqr->lqr_start_idx %= osts->op_count;
1097 if (stripe_count > 1 && (osts->op_count % stripe_count) != 1)
1098 ++lqr->lqr_offset_idx;
1100 ost_start_idx_temp = lqr->lqr_start_idx;
1104 QOS_DEBUG("pool '%s' want %d start_idx %d start_count %d offset %d "
1105 "active %d count %d\n",
1106 lod_comp->llc_pool ? lod_comp->llc_pool : "",
1107 stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
1108 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
1110 for (i = 0; i < osts->op_count && stripe_idx < stripe_count; i++) {
1111 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
1113 ++lqr->lqr_start_idx;
1114 ost_idx = lqr->lqr_pool.op_array[array_idx];
1116 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
1117 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1118 stripe_idx, array_idx, ost_idx);
1120 if ((ost_idx == LOV_QOS_EMPTY) ||
1121 !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1124 /* Fail Check before osc_precreate() is called
1125 so we can only 'fail' single OSC. */
1126 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1129 spin_unlock(&lqr->lqr_alloc);
1130 rc = lod_check_and_reserve_ost(env, lo, sfs, ost_idx, speed,
1131 &stripe_idx, stripe, ost_indices,
1133 spin_lock(&lqr->lqr_alloc);
1135 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
1138 if ((speed < 2) && (stripe_idx < stripe_count_min)) {
1139 /* Try again, allowing slower OSCs */
1141 lqr->lqr_start_idx = ost_start_idx_temp;
1147 spin_unlock(&lqr->lqr_alloc);
1148 up_read(&m->lod_qos.lq_rw_sem);
1151 lod_comp->llc_stripe_count = stripe_idx;
1152 /* at least one stripe is allocated */
1155 /* nobody provided us with a single object */
1164 up_read(&pool_tgt_rw_sem(pool));
1165 /* put back ref got by lod_find_pool() */
1166 lod_pool_putref(pool);
1173 * Allocate a specific striping layout on a user defined set of OSTs.
1175 * Allocates new striping using the OST index range provided by the data from
1176 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1177 * OSTs are not considered. The exact order of OSTs requested by the user
1178 * is respected as much as possible depending on OST status. The number of
1179 * stripes needed and stripe offset are taken from the object. If that number
1180 * can not be met, then the function returns a failure and then it's the
1181 * caller's responsibility to release the stripes allocated. All the internal
1182 * structures are protected, but no concurrent allocation is allowed on the
1185 * \param[in] env execution environment for this thread
1186 * \param[in] lo LOD object
1187 * \param[out] stripe striping created
1188 * \param[out] ost_indices ost indices of striping created
1189 * \param[in] th transaction handle
1190 * \param[in] comp_idx index of ldo_comp_entries
1191 * \param[in|out] inuse array of inuse ost index
1193 * \retval 0 on success
1194 * \retval -ENODEV OST index does not exist on file system
1195 * \retval -EINVAL requested OST index is invalid
1196 * \retval negative negated errno on error
1198 static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
1199 struct dt_object **stripe, __u32 *ost_indices,
1200 struct thandle *th, int comp_idx,
1201 struct ost_pool *inuse)
1203 struct lod_layout_component *lod_comp;
1204 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1205 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1206 struct dt_object *o;
1207 unsigned int array_idx = 0;
1208 int stripe_count = 0;
1213 /* for specific OSTs layout */
1214 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1215 lod_comp = &lo->ldo_comp_entries[comp_idx];
1216 LASSERT(lod_comp->llc_ostlist.op_array);
1218 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1222 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
1223 if (lod_comp->llc_ostlist.op_array[i] ==
1224 lod_comp->llc_stripe_offset) {
1229 if (i == lod_comp->llc_stripe_count) {
1231 "%s: start index %d not in the specified list of OSTs\n",
1232 lod2obd(m)->obd_name, lod_comp->llc_stripe_offset);
1236 for (i = 0; i < lod_comp->llc_stripe_count;
1237 i++, array_idx = (array_idx + 1) % lod_comp->llc_stripe_count) {
1238 __u32 ost_idx = lod_comp->llc_ostlist.op_array[array_idx];
1240 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1246 * do not put >1 objects on a single OST
1248 if (lod_qos_is_ost_used(env, ost_idx, stripe_count)) {
1253 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1254 if (rc < 0) /* this OSP doesn't feel well */
1257 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1261 "%s: can't declare new object on #%u: %d\n",
1262 lod2obd(m)->obd_name, ost_idx, rc);
1267 * We've successfully declared (reserved) an object
1269 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1270 lod_comp_ost_in_use(inuse, ost_idx);
1271 stripe[stripe_count] = o;
1272 ost_indices[stripe_count] = ost_idx;
1280 * Allocate a striping on a predefined set of OSTs.
1282 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1283 * Full OSTs are not considered. The exact order of OSTs is not important and
1284 * varies depending on OST status. The allocation procedure prefers the targets
1285 * with precreated objects ready. The number of stripes needed and stripe
1286 * offset are taken from the object. If that number cannot be met, then the
1287 * function returns an error and then it's the caller's responsibility to
1288 * release the stripes allocated. All the internal structures are protected,
1289 * but no concurrent allocation is allowed on the same objects.
1291 * \param[in] env execution environment for this thread
1292 * \param[in] lo LOD object
1293 * \param[out] stripe striping created
1294 * \param[out] ost_indices ost indices of striping created
1295 * \param[in] flags not used
1296 * \param[in] th transaction handle
1297 * \param[in] comp_idx index of ldo_comp_entries
1298 * \param[in|out]inuse array of inuse ost index
1300 * \retval 0 on success
1301 * \retval -ENOSPC if no OST objects are available at all
1302 * \retval -EFBIG if not enough OST objects are found
1303 * \retval -EINVAL requested offset is invalid
1304 * \retval negative errno on failure
1306 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1307 struct dt_object **stripe, __u32 *ost_indices,
1308 int flags, struct thandle *th, int comp_idx,
1309 struct ost_pool *inuse)
1311 struct lod_layout_component *lod_comp;
1312 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1313 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1314 struct dt_object *o;
1316 unsigned int i, array_idx, ost_count;
1317 int rc, stripe_num = 0;
1319 struct pool_desc *pool = NULL;
1320 struct ost_pool *osts;
1323 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1324 lod_comp = &lo->ldo_comp_entries[comp_idx];
1326 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1330 if (lod_comp->llc_pool != NULL)
1331 pool = lod_find_pool(m, lod_comp->llc_pool);
1334 down_read(&pool_tgt_rw_sem(pool));
1335 osts = &(pool->pool_obds);
1337 osts = &(m->lod_pool_info);
1340 ost_count = osts->op_count;
1343 /* search loi_ost_idx in ost array */
1345 for (i = 0; i < ost_count; i++) {
1346 if (osts->op_array[i] == lod_comp->llc_stripe_offset) {
1351 if (i == ost_count) {
1352 CERROR("Start index %d not found in pool '%s'\n",
1353 lod_comp->llc_stripe_offset,
1354 lod_comp->llc_pool ? lod_comp->llc_pool : "");
1355 GOTO(out, rc = -EINVAL);
1358 for (i = 0; i < ost_count;
1359 i++, array_idx = (array_idx + 1) % ost_count) {
1360 ost_idx = osts->op_array[array_idx];
1362 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1365 /* Fail Check before osc_precreate() is called
1366 so we can only 'fail' single OSC. */
1367 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1371 * do not put >1 objects on a single OST
1373 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
1377 * try not allocate on the OST used by other component
1379 if (speed == 0 && i != 0 &&
1380 lod_comp_is_ost_used(inuse, ost_idx))
1383 /* Drop slow OSCs if we can, but not for requested start idx.
1385 * This means "if OSC is slow and it is not the requested
1386 * start OST, then it can be skipped, otherwise skip it only
1387 * if it is inactive/recovering/out-of-space." */
1389 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1391 /* this OSP doesn't feel well */
1396 * We expect number of precreated objects at the first
1397 * iteration. Skip OSPs with no objects ready. Don't apply
1398 * this logic to OST specified with stripe_offset.
1400 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1403 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1405 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1406 ost_idx, (int) PTR_ERR(o));
1411 * We've successfully declared (reserved) an object
1413 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1414 lod_comp_ost_in_use(inuse, ost_idx);
1415 stripe[stripe_num] = o;
1416 ost_indices[stripe_num] = ost_idx;
1419 /* We have enough stripes */
1420 if (stripe_num == lod_comp->llc_stripe_count)
1424 /* Try again, allowing slower OSCs */
1429 /* If we were passed specific striping params, then a failure to
1430 * meet those requirements is an error, since we can't reallocate
1431 * that memory (it might be part of a larger array or something).
1433 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1434 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1435 lod_comp->llc_stripe_count);
1436 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1439 up_read(&pool_tgt_rw_sem(pool));
1440 /* put back ref got by lod_find_pool() */
1441 lod_pool_putref(pool);
1448 * Check whether QoS allocation should be used.
1450 * A simple helper to decide when QoS allocation should be used:
1451 * if it's just a single available target or the used space is
1452 * evenly distributed among the targets at the moment, then QoS
1453 * allocation algorithm should not be used.
1455 * \param[in] lod LOD device
1457 * \retval 0 should not be used
1458 * \retval 1 should be used
1460 static inline int lod_qos_is_usable(struct lod_device *lod)
1463 /* to be able to debug QoS code */
1467 /* Detect -EAGAIN early, before expensive lock is taken. */
1468 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1471 if (lod->lod_desc.ld_active_tgt_count < 2)
1478 * Allocate a striping using an algorithm with weights.
1480 * The function allocates OST objects to create a striping. The algorithm
1481 * used is based on weights (currently only using the free space), and it's
1482 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1483 * configuration (# of stripes, offset, pool) is taken from the object and
1484 * is prepared by the caller.
1486 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1487 * be met due to too few OSTs, then allocation fails. If the flag is passed
1488 * fewer than 3/4 of the requested number of stripes can be allocated, then
1491 * No concurrent allocation is allowed on the object and this must be ensured
1492 * by the caller. All the internal structures are protected by the function.
1494 * The algorithm has two steps: find available OSTs and calculate their
1495 * weights, then select the OSTs with their weights used as the probability.
1496 * An OST with a higher weight is proportionately more likely to be selected
1497 * than one with a lower weight.
1499 * \param[in] env execution environment for this thread
1500 * \param[in] lo LOD object
1501 * \param[out] stripe striping created
1502 * \param[out] ost_indices ost indices of striping created
1503 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1504 * \param[in] th transaction handle
1505 * \param[in] comp_idx index of ldo_comp_entries
1506 * \param[in|out]inuse array of inuse ost index
1508 * \retval 0 on success
1509 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1510 * \retval -EINVAL requested OST index is invalid
1511 * \retval negative errno on failure
1513 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1514 struct dt_object **stripe, __u32 *ost_indices,
1515 int flags, struct thandle *th, int comp_idx,
1516 struct ost_pool *inuse)
1518 struct lod_layout_component *lod_comp;
1519 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1520 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1521 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
1522 struct lod_tgt_desc *ost;
1523 struct dt_object *o;
1524 __u64 total_weight = 0;
1525 struct pool_desc *pool = NULL;
1526 struct ost_pool *osts;
1528 __u32 nfound, good_osts, stripe_count, stripe_count_min;
1529 __u32 inuse_old_count = inuse->op_count;
1533 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
1534 lod_comp = &lo->ldo_comp_entries[comp_idx];
1535 stripe_count = lod_comp->llc_stripe_count;
1536 stripe_count_min = min_stripe_count(stripe_count, flags);
1537 if (stripe_count_min < 1)
1540 if (lod_comp->llc_pool != NULL)
1541 pool = lod_find_pool(lod, lod_comp->llc_pool);
1544 down_read(&pool_tgt_rw_sem(pool));
1545 osts = &(pool->pool_obds);
1547 osts = &(lod->lod_pool_info);
1550 /* Detect -EAGAIN early, before expensive lock is taken. */
1551 if (!lod_qos_is_usable(lod))
1552 GOTO(out_nolock, rc = -EAGAIN);
1554 /* Do actual allocation, use write lock here. */
1555 down_write(&lod->lod_qos.lq_rw_sem);
1558 * Check again, while we were sleeping on @lq_rw_sem things could
1561 if (!lod_qos_is_usable(lod))
1562 GOTO(out, rc = -EAGAIN);
1564 rc = lod_qos_calc_ppo(lod);
1568 rc = lod_qos_ost_in_use_clear(env, lod_comp->llc_stripe_count);
1573 /* Find all the OSTs that are valid stripe candidates */
1574 for (i = 0; i < osts->op_count; i++) {
1575 if (!cfs_bitmap_check(lod->lod_ost_bitmap, osts->op_array[i]))
1578 ost = OST_TGT(lod, osts->op_array[i]);
1579 ost->ltd_qos.ltq_usable = 0;
1581 rc = lod_statfs_and_check(env, lod, osts->op_array[i], sfs);
1583 /* this OSP doesn't feel well */
1587 if (sfs->os_state & OS_STATE_DEGRADED)
1590 /* Fail Check before osc_precreate() is called
1591 so we can only 'fail' single OSC. */
1592 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1593 osts->op_array[i] == 0)
1596 ost->ltd_qos.ltq_usable = 1;
1597 lod_qos_calc_weight(lod, osts->op_array[i]);
1598 total_weight += ost->ltd_qos.ltq_weight;
1603 QOS_DEBUG("found %d good osts\n", good_osts);
1605 if (good_osts < stripe_count_min)
1606 GOTO(out, rc = -EAGAIN);
1608 /* We have enough osts */
1609 if (good_osts < stripe_count)
1610 stripe_count = good_osts;
1612 /* Find enough OSTs with weighted random allocation. */
1614 while (nfound < stripe_count) {
1615 __u64 rand, cur_weight;
1621 #if BITS_PER_LONG == 32
1622 rand = cfs_rand() % (unsigned)total_weight;
1623 /* If total_weight > 32-bit, first generate the high
1624 * 32 bits of the random number, then add in the low
1625 * 32 bits (truncated to the upper limit, if needed) */
1626 if (total_weight > 0xffffffffULL)
1627 rand = (__u64)(cfs_rand() %
1628 (unsigned)(total_weight >> 32)) << 32;
1632 if (rand == (total_weight & 0xffffffff00000000ULL))
1633 rand |= cfs_rand() % (unsigned)total_weight;
1638 rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1645 /* On average, this will hit larger-weighted OSTs more often.
1646 * 0-weight OSTs will always get used last (only when rand=0) */
1647 for (i = 0; i < osts->op_count; i++) {
1648 __u32 idx = osts->op_array[i];
1650 if (lod_should_avoid_ost(lo, lag, idx))
1653 ost = OST_TGT(lod, idx);
1655 if (!ost->ltd_qos.ltq_usable)
1658 cur_weight += ost->ltd_qos.ltq_weight;
1659 QOS_DEBUG("stripe_count=%d nfound=%d cur_weight=%llu "
1660 "rand=%llu total_weight=%llu\n",
1661 stripe_count, nfound, cur_weight, rand,
1664 if (cur_weight < rand)
1667 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1669 * do not put >1 objects on a single OST
1671 if (lod_qos_is_ost_used(env, idx, nfound) ||
1672 lod_comp_is_ost_used(inuse, idx))
1675 o = lod_qos_declare_object_on(env, lod, idx, th);
1677 QOS_DEBUG("can't declare object on #%u: %d\n",
1678 idx, (int) PTR_ERR(o));
1682 lod_avoid_update(lo, lag);
1683 lod_qos_ost_in_use(env, nfound, idx);
1684 lod_comp_ost_in_use(inuse, idx);
1686 ost_indices[nfound] = idx;
1687 lod_qos_used(lod, osts, idx, &total_weight);
1694 /* no OST found on this iteration, give up */
1699 if (unlikely(nfound != stripe_count)) {
1701 * when the decision to use weighted algorithm was made
1702 * we had enough appropriate OSPs, but this state can
1703 * change anytime (no space on OST, broken connection, etc)
1704 * so it's possible OSP won't be able to provide us with
1705 * an object due to just changed state
1707 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1708 lod2obd(lod)->obd_name, stripe_count, nfound);
1709 for (i = 0; i < nfound; i++) {
1710 LASSERT(stripe[i] != NULL);
1711 dt_object_put(env, stripe[i]);
1714 LASSERTF(nfound <= inuse->op_count,
1715 "nfound:%d, op_count:%u\n", nfound, inuse->op_count);
1716 inuse->op_count = inuse_old_count;
1718 /* makes sense to rebalance next time */
1719 lod->lod_qos.lq_dirty = 1;
1720 lod->lod_qos.lq_same_space = 0;
1726 up_write(&lod->lod_qos.lq_rw_sem);
1730 up_read(&pool_tgt_rw_sem(pool));
1731 /* put back ref got by lod_find_pool() */
1732 lod_pool_putref(pool);
1739 * Find largest stripe count the caller can use.
1741 * Find the maximal possible stripe count not greater than \a stripe_count.
1742 * Sometimes suggested stripecount can't be reached for a number of reasons:
1743 * lack of enough active OSTs or the backend does not support EAs that large.
1744 * If the passed one is 0, then the filesystem's default one is used.
1746 * \param[in] lod LOD device
1747 * \param[in] lo The lod_object
1748 * \param[in] stripe_count count the caller would like to use
1750 * \retval the maximum usable stripe count
1752 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
1755 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1758 stripe_count = lod->lod_desc.ld_default_stripe_count;
1759 if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1760 stripe_count = lod->lod_desc.ld_active_tgt_count;
1764 /* stripe count is based on whether OSD can handle larger EA sizes */
1765 if (lod->lod_osd_max_easize > 0) {
1766 unsigned int easize = lod->lod_osd_max_easize;
1769 if (lo->ldo_is_composite) {
1770 struct lod_layout_component *lod_comp;
1771 unsigned int header_sz = sizeof(struct lov_comp_md_v1);
1773 header_sz += sizeof(struct lov_comp_md_entry_v1) *
1775 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1776 lod_comp = &lo->ldo_comp_entries[i];
1777 if (lod_comp->llc_flags & LCME_FL_INIT)
1778 header_sz += lov_mds_md_size(
1779 lod_comp->llc_stripe_count,
1782 if (easize > header_sz)
1783 easize -= header_sz;
1788 max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
1791 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1795 * Create in-core respresentation for a fully-defined striping
1797 * When the caller passes a fully-defined striping (i.e. everything including
1798 * OST object FIDs are defined), then we still need to instantiate LU-cache
1799 * with the objects representing the stripes defined. This function completes
1802 * \param[in] env execution environment for this thread
1803 * \param[in] mo LOD object
1804 * \param[in] buf buffer containing the striping
1806 * \retval 0 on success
1807 * \retval negative negated errno on error
1809 int lod_use_defined_striping(const struct lu_env *env,
1810 struct lod_object *mo,
1811 const struct lu_buf *buf)
1813 struct lod_layout_component *lod_comp;
1814 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1815 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1816 struct lov_comp_md_v1 *comp_v1 = NULL;
1817 struct lov_ost_data_v1 *objs;
1824 magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
1826 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
1827 magic != LOV_MAGIC_COMP_V1)
1830 if (magic == LOV_MAGIC_COMP_V1) {
1831 comp_v1 = buf->lb_buf;
1832 comp_cnt = le16_to_cpu(comp_v1->lcm_entry_count);
1835 mirror_cnt = le16_to_cpu(comp_v1->lcm_mirror_count) + 1;
1836 mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
1838 mo->ldo_is_composite = 1;
1840 mo->ldo_is_composite = 0;
1844 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1846 rc = lod_alloc_comp_entries(mo, mirror_cnt, comp_cnt);
1850 for (i = 0; i < comp_cnt; i++) {
1851 struct lu_extent *ext;
1855 lod_comp = &mo->ldo_comp_entries[i];
1857 if (mo->ldo_is_composite) {
1858 offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
1859 v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
1860 magic = le32_to_cpu(v1->lmm_magic);
1862 ext = &comp_v1->lcm_entries[i].lcme_extent;
1863 lod_comp->llc_extent.e_start =
1864 le64_to_cpu(ext->e_start);
1865 lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
1866 lod_comp->llc_flags =
1867 le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
1869 le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
1870 if (lod_comp->llc_id == LCME_ID_INVAL)
1871 GOTO(out, rc = -EINVAL);
1875 if (magic == LOV_MAGIC_V1) {
1876 objs = &v1->lmm_objects[0];
1877 } else if (magic == LOV_MAGIC_V3) {
1878 objs = &v3->lmm_objects[0];
1879 if (v3->lmm_pool_name[0] != '\0')
1880 pool_name = v3->lmm_pool_name;
1882 CDEBUG(D_LAYOUT, "Invalid magic %x\n", magic);
1883 GOTO(out, rc = -EINVAL);
1886 lod_comp->llc_pattern = le32_to_cpu(v1->lmm_pattern);
1887 lod_comp->llc_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1888 lod_comp->llc_stripe_count = le16_to_cpu(v1->lmm_stripe_count);
1889 lod_comp->llc_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1891 * The stripe_offset of an uninit-ed component is stored in
1892 * the lmm_layout_gen
1894 if (mo->ldo_is_composite && !lod_comp_inited(lod_comp))
1895 lod_comp->llc_stripe_offset = lod_comp->llc_layout_gen;
1896 lod_obj_set_pool(mo, i, pool_name);
1898 if ((!mo->ldo_is_composite || lod_comp_inited(lod_comp)) &&
1899 !(lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED) &&
1900 !(lod_comp->llc_pattern & LOV_PATTERN_MDT)) {
1901 rc = lod_initialize_objects(env, mo, objs, i);
1907 rc = lod_fill_mirrors(mo);
1912 lod_object_free_striping(env, mo);
1918 * Parse suggested striping configuration.
1920 * The caller gets a suggested striping configuration from a number of sources
1921 * including per-directory default and applications. Then it needs to verify
1922 * the suggested striping is valid, apply missing bits and store the resulting
1923 * configuration in the object to be used by the allocator later. Must not be
1924 * called concurrently against the same object. It's OK to provide a
1925 * fully-defined striping.
1927 * \param[in] env execution environment for this thread
1928 * \param[in] lo LOD object
1929 * \param[in] buf buffer containing the striping
1931 * \retval 0 on success
1932 * \retval negative negated errno on error
1934 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
1935 const struct lu_buf *buf)
1937 struct lod_layout_component *lod_comp;
1938 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1939 struct lov_desc *desc = &d->lod_desc;
1940 struct lov_user_md_v1 *v1 = NULL;
1941 struct lov_user_md_v3 *v3 = NULL;
1942 struct lov_comp_md_v1 *comp_v1 = NULL;
1943 char def_pool[LOV_MAXPOOLNAME + 1];
1950 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1953 memset(def_pool, 0, sizeof(def_pool));
1954 if (lo->ldo_comp_entries != NULL)
1955 lod_layout_get_pool(lo->ldo_comp_entries, lo->ldo_comp_cnt,
1956 def_pool, sizeof(def_pool));
1958 /* free default striping info */
1959 lod_free_comp_entries(lo);
1961 rc = lod_verify_striping(d, lo, buf, false);
1967 comp_v1 = buf->lb_buf;
1968 magic = v1->lmm_magic;
1970 if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
1971 /* try to use as fully defined striping */
1972 rc = lod_use_defined_striping(env, lo, buf);
1977 case __swab32(LOV_USER_MAGIC_V1):
1978 lustre_swab_lov_user_md_v1(v1);
1979 magic = v1->lmm_magic;
1981 case LOV_USER_MAGIC_V1:
1983 case __swab32(LOV_USER_MAGIC_V3):
1984 lustre_swab_lov_user_md_v3(v3);
1985 magic = v3->lmm_magic;
1987 case LOV_USER_MAGIC_V3:
1989 case __swab32(LOV_USER_MAGIC_SPECIFIC):
1990 lustre_swab_lov_user_md_v3(v3);
1991 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1992 v3->lmm_stripe_count);
1993 magic = v3->lmm_magic;
1995 case LOV_USER_MAGIC_SPECIFIC:
1997 case __swab32(LOV_USER_MAGIC_COMP_V1):
1998 lustre_swab_lov_comp_md_v1(comp_v1);
1999 magic = comp_v1->lcm_magic;
2001 case LOV_USER_MAGIC_COMP_V1:
2004 CERROR("%s: unrecognized magic %X\n",
2005 lod2obd(d)->obd_name, magic);
2009 lustre_print_user_md(D_OTHER, v1, "parse config");
2011 if (magic == LOV_USER_MAGIC_COMP_V1) {
2012 comp_cnt = comp_v1->lcm_entry_count;
2015 mirror_cnt = comp_v1->lcm_mirror_count + 1;
2017 lo->ldo_flr_state = LCM_FL_RDONLY;
2018 lo->ldo_is_composite = 1;
2022 lo->ldo_is_composite = 0;
2025 rc = lod_alloc_comp_entries(lo, mirror_cnt, comp_cnt);
2029 LASSERT(lo->ldo_comp_entries);
2031 for (i = 0; i < comp_cnt; i++) {
2032 struct pool_desc *pool;
2033 struct lu_extent *ext;
2036 lod_comp = &lo->ldo_comp_entries[i];
2038 if (lo->ldo_is_composite) {
2039 v1 = (struct lov_user_md *)((char *)comp_v1 +
2040 comp_v1->lcm_entries[i].lcme_offset);
2041 ext = &comp_v1->lcm_entries[i].lcme_extent;
2042 lod_comp->llc_extent = *ext;
2043 lod_comp->llc_flags =
2044 comp_v1->lcm_entries[i].lcme_flags &
2049 if (v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2050 v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2053 v3 = (struct lov_user_md_v3 *)v1;
2054 if (v3->lmm_pool_name[0] != '\0')
2055 pool_name = v3->lmm_pool_name;
2057 if (v3->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2058 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
2059 v3->lmm_stripe_offset =
2060 v3->lmm_objects[0].l_ost_idx;
2062 /* copy ost list from lmm */
2063 lod_comp->llc_ostlist.op_count =
2064 v3->lmm_stripe_count;
2065 lod_comp->llc_ostlist.op_size =
2066 v3->lmm_stripe_count * sizeof(__u32);
2067 OBD_ALLOC(lod_comp->llc_ostlist.op_array,
2068 lod_comp->llc_ostlist.op_size);
2069 if (!lod_comp->llc_ostlist.op_array)
2070 GOTO(free_comp, rc = -ENOMEM);
2072 for (j = 0; j < v3->lmm_stripe_count; j++)
2073 lod_comp->llc_ostlist.op_array[j] =
2074 v3->lmm_objects[j].l_ost_idx;
2078 if (pool_name == NULL && def_pool[0] != '\0')
2079 pool_name = def_pool;
2081 if (v1->lmm_pattern == 0)
2082 v1->lmm_pattern = LOV_PATTERN_RAID0;
2083 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
2084 lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT) {
2085 CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
2086 lod2obd(d)->obd_name, v1->lmm_pattern);
2087 GOTO(free_comp, rc = -EINVAL);
2090 lod_comp->llc_pattern = v1->lmm_pattern;
2091 lod_comp->llc_stripe_size = desc->ld_default_stripe_size;
2092 if (v1->lmm_stripe_size)
2093 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2095 lod_comp->llc_stripe_count = desc->ld_default_stripe_count;
2096 if (v1->lmm_stripe_count ||
2097 lov_pattern(v1->lmm_pattern) == LOV_PATTERN_MDT)
2098 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2100 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2101 lod_obj_set_pool(lo, i, pool_name);
2103 LASSERT(ergo(lov_pattern(lod_comp->llc_pattern) ==
2104 LOV_PATTERN_MDT, lod_comp->llc_stripe_count == 0));
2106 if (pool_name == NULL)
2109 /* In the function below, .hs_keycmp resolves to
2110 * pool_hashkey_keycmp() */
2111 /* coverity[overrun-buffer-val] */
2112 pool = lod_find_pool(d, pool_name);
2116 if (lod_comp->llc_stripe_offset != LOV_OFFSET_DEFAULT) {
2117 rc = lod_check_index_in_pool(
2118 lod_comp->llc_stripe_offset, pool);
2120 lod_pool_putref(pool);
2121 CDEBUG(D_LAYOUT, "%s: invalid offset, %u\n",
2122 lod2obd(d)->obd_name,
2123 lod_comp->llc_stripe_offset);
2124 GOTO(free_comp, rc = -EINVAL);
2128 if (lod_comp->llc_stripe_count > pool_tgt_count(pool))
2129 lod_comp->llc_stripe_count = pool_tgt_count(pool);
2131 lod_pool_putref(pool);
2137 lod_free_comp_entries(lo);
2142 * prepare enough OST avoidance bitmap space
2144 int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
2146 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2147 struct lod_tgt_descs *ltds = &lod->lod_ost_descs;
2148 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2149 struct cfs_bitmap *bitmap = NULL;
2150 __u32 *new_oss = NULL;
2152 lag->lag_ost_avail = ltds->ltd_tgtnr;
2154 /* reset OSS avoid guide array */
2155 lag->lag_oaa_count = 0;
2156 if (lag->lag_oss_avoid_array && lag->lag_oaa_size < ltds->ltd_tgtnr) {
2157 OBD_FREE(lag->lag_oss_avoid_array,
2158 sizeof(__u32) * lag->lag_oaa_size);
2159 lag->lag_oss_avoid_array = NULL;
2160 lag->lag_oaa_size = 0;
2163 /* init OST avoid guide bitmap */
2164 if (lag->lag_ost_avoid_bitmap) {
2165 if (ltds->ltd_tgtnr <= lag->lag_ost_avoid_bitmap->size) {
2166 CFS_RESET_BITMAP(lag->lag_ost_avoid_bitmap);
2168 CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
2169 lag->lag_ost_avoid_bitmap = NULL;
2173 if (!lag->lag_ost_avoid_bitmap) {
2174 bitmap = CFS_ALLOCATE_BITMAP(ltds->ltd_tgtnr);
2179 if (!lag->lag_oss_avoid_array) {
2181 * usually there are multiple OSTs in one OSS, but we don't
2182 * know the exact OSS number, so we choose a safe option,
2183 * using OST count to allocate the array to store the OSS
2186 OBD_ALLOC(new_oss, sizeof(*new_oss) * ltds->ltd_tgtnr);
2188 CFS_FREE_BITMAP(bitmap);
2194 lag->lag_oss_avoid_array = new_oss;
2195 lag->lag_oaa_size = ltds->ltd_tgtnr;
2198 lag->lag_ost_avoid_bitmap = bitmap;
2204 * Collect information of used OSTs and OSSs in the overlapped components
2207 void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
2210 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
2211 struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
2212 struct cfs_bitmap *bitmap = lag->lag_ost_avoid_bitmap;
2215 /* iterate mirrors */
2216 for (i = 0; i < lo->ldo_mirror_count; i++) {
2217 struct lod_layout_component *comp;
2220 * skip mirror containing component[comp_idx], we only
2221 * collect OSTs info of conflicting component in other mirrors,
2222 * so that during read, if OSTs of a mirror's component are
2223 * not available, we still have other mirror with different
2224 * OSTs to read the data.
2226 comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
2227 if (comp->llc_id == LCME_ID_INVAL ||
2228 mirror_id_of(comp->llc_id) ==
2229 mirror_id_of(lod_comp->llc_id))
2232 /* iterate components of a mirror */
2233 lod_foreach_mirror_comp(comp, lo, i) {
2234 /* skip non-overlapped or un-instantiated components */
2235 if (!lu_extent_is_overlapped(&comp->llc_extent,
2236 &lod_comp->llc_extent) ||
2237 !lod_comp_inited(comp) || !comp->llc_stripe)
2241 * collect used OSTs index and OSS info from a
2244 for (j = 0; j < comp->llc_stripe_count; j++) {
2245 struct lod_tgt_desc *ost;
2246 struct lod_qos_oss *lqo;
2249 ost = OST_TGT(lod, comp->llc_ost_indices[j]);
2250 lqo = ost->ltd_qos.ltq_oss;
2252 if (cfs_bitmap_check(bitmap, ost->ltd_index))
2255 cfs_bitmap_set(bitmap, ost->ltd_index);
2256 lag->lag_ost_avail--;
2258 for (k = 0; k < lag->lag_oaa_count; k++) {
2259 if (lag->lag_oss_avoid_array[k] ==
2263 if (k == lag->lag_oaa_count) {
2264 lag->lag_oss_avoid_array[k] =
2266 lag->lag_oaa_count++;
2274 * Create a striping for an obejct.
2276 * The function creates a new striping for the object. The function tries QoS
2277 * algorithm first unless free space is distributed evenly among OSTs, but
2278 * by default RR algorithm is preferred due to internal concurrency (QoS is
2279 * serialized). The caller must ensure no concurrent calls to the function
2280 * are made against the same object.
2282 * \param[in] env execution environment for this thread
2283 * \param[in] lo LOD object
2284 * \param[in] attr attributes OST objects will be declared with
2285 * \param[in] th transaction handle
2286 * \param[in] comp_idx index of ldo_comp_entries
2287 * \param[in|out] inuse array of inuse ost index
2289 * \retval 0 on success
2290 * \retval negative negated errno on error
2292 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
2293 struct lu_attr *attr, struct thandle *th,
2294 int comp_idx, struct ost_pool *inuse)
2296 struct lod_layout_component *lod_comp;
2297 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2299 int flag = LOV_USES_ASSIGNED_STRIPE;
2301 struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
2302 struct dt_object **stripe = NULL;
2303 __u32 *ost_indices = NULL;
2307 LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
2308 lod_comp = &lo->ldo_comp_entries[comp_idx];
2310 /* A released component is being created */
2311 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
2314 /* A Data-on-MDT component is being created */
2315 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
2318 if (likely(lod_comp->llc_stripe == NULL)) {
2320 * no striping has been created so far
2322 LASSERT(lod_comp->llc_stripe_count);
2324 * statfs and check OST targets now, since ld_active_tgt_count
2325 * could be changed if some OSTs are [de]activated manually.
2327 lod_qos_statfs_update(env, d);
2328 stripe_len = lod_get_stripe_count(d, lo,
2329 lod_comp->llc_stripe_count);
2330 if (stripe_len == 0)
2331 GOTO(out, rc = -ERANGE);
2332 lod_comp->llc_stripe_count = stripe_len;
2333 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
2335 GOTO(out, rc = -ENOMEM);
2336 OBD_ALLOC(ost_indices, sizeof(*ost_indices) * stripe_len);
2338 GOTO(out, rc = -ENOMEM);
2340 lod_getref(&d->lod_ost_descs);
2341 /* XXX: support for non-0 files w/o objects */
2342 CDEBUG(D_OTHER, "tgt_count %d stripe_count %d\n",
2343 d->lod_desc.ld_tgt_count, stripe_len);
2345 if (lod_comp->llc_ostlist.op_array) {
2346 rc = lod_alloc_ost_list(env, lo, stripe, ost_indices,
2347 th, comp_idx, inuse);
2348 } else if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT) {
2350 * collect OSTs and OSSs used in other mirrors whose
2351 * components cross the ldo_comp_entries[comp_idx]
2353 rc = lod_prepare_avoidance(env, lo);
2357 lod_collect_avoidance(lo, lag, comp_idx);
2359 rc = lod_alloc_qos(env, lo, stripe, ost_indices, flag,
2360 th, comp_idx, inuse);
2362 rc = lod_alloc_rr(env, lo, stripe, ost_indices,
2363 flag, th, comp_idx, inuse);
2365 rc = lod_alloc_specific(env, lo, stripe, ost_indices,
2366 flag, th, comp_idx, inuse);
2369 lod_putref(d, &d->lod_ost_descs);
2371 for (i = 0; i < stripe_len; i++)
2372 if (stripe[i] != NULL)
2373 dt_object_put(env, stripe[i]);
2374 lod_comp->llc_stripe_count = 0;
2376 lod_comp->llc_stripe = stripe;
2377 lod_comp->llc_ost_indices = ost_indices;
2378 lod_comp->llc_stripes_allocated = stripe_len;
2382 * lod_qos_parse_config() found supplied buf as a predefined
2383 * striping (not a hint), so it allocated all the object
2384 * now we need to create them
2386 for (i = 0; i < lod_comp->llc_stripe_count; i++) {
2387 struct dt_object *o;
2389 o = lod_comp->llc_stripe[i];
2392 rc = lod_sub_declare_create(env, o, attr, NULL,
2395 CERROR("can't declare create: %d\n", rc);
2400 * Clear LCME_FL_INIT for the component so that
2401 * lod_striping_create() can create the striping objects
2404 lod_comp_unset_init(lod_comp);
2410 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
2412 OBD_FREE(ost_indices,
2413 sizeof(*ost_indices) * stripe_len);
2418 int lod_obj_stripe_set_inuse_cb(const struct lu_env *env,
2419 struct lod_object *lo,
2420 struct dt_object *dt, struct thandle *th,
2421 int comp_idx, int stripe_idx,
2422 struct lod_obj_stripe_cb_data *data)
2424 struct lod_thread_info *info = lod_env_info(env);
2425 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2426 struct lu_fid *fid = &info->lti_fid;
2428 int rc, type = LU_SEQ_RANGE_OST;
2430 *fid = *lu_object_fid(&dt->do_lu);
2431 rc = lod_fld_lookup(env, d, fid, &index, &type);
2433 CERROR("%s: fail to locate "DFID": rc = %d\n",
2434 lod2obd(d)->obd_name, PFID(fid), rc);
2437 lod_comp_ost_in_use(data->locd_inuse, index);
2442 * Resize per-thread ost list to hold OST target index list already used.
2444 * \param[in,out] inuse structure contains ost list array
2445 * \param[in] cnt total stripe count of all components
2446 * \param[in] max array's max size if @max > 0
2448 * \retval 0 on success
2449 * \retval -ENOMEM reallocation failed
2451 static int lod_inuse_resize(struct ost_pool *inuse, __u16 cnt, __u16 max)
2454 __u32 new = cnt * sizeof(inuse->op_array[0]);
2456 inuse->op_count = 0;
2458 if (new <= inuse->op_size)
2462 new = min_t(__u32, new, max);
2464 OBD_ALLOC(array, new);
2468 if (inuse->op_array)
2469 OBD_FREE(inuse->op_array, inuse->op_size);
2471 inuse->op_array = array;
2472 inuse->op_size = new;
2477 int lod_prepare_inuse(const struct lu_env *env, struct lod_object *lo)
2479 struct lod_thread_info *info = lod_env_info(env);
2480 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2481 struct ost_pool *inuse = &info->lti_inuse_osts;
2482 struct lod_obj_stripe_cb_data data = { { 0 } };
2483 __u32 stripe_count = 0;
2487 for (i = 0; i < lo->ldo_comp_cnt; i++)
2488 stripe_count += lod_comp_entry_stripe_count(lo,
2489 &lo->ldo_comp_entries[i], false);
2490 rc = lod_inuse_resize(inuse, stripe_count, d->lod_osd_max_easize);
2494 data.locd_inuse = inuse;
2495 data.locd_stripe_cb = lod_obj_stripe_set_inuse_cb;
2496 return lod_obj_for_each_stripe(env, lo, NULL, &data);
2499 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
2500 struct lu_attr *attr, const struct lu_buf *buf,
2504 struct lod_thread_info *info = lod_env_info(env);
2505 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2506 struct ost_pool *inuse = &info->lti_inuse_osts;
2514 /* no OST available */
2515 /* XXX: should we be waiting a bit to prevent failures during
2516 * cluster initialization? */
2517 if (d->lod_ostnr == 0)
2521 * by this time, the object's ldo_stripe_count and ldo_stripe_size
2522 * contain default value for striping: taken from the parent
2523 * or from filesystem defaults
2525 * in case the caller is passing lovea with new striping config,
2526 * we may need to parse lovea and apply new configuration
2528 rc = lod_qos_parse_config(env, lo, buf);
2532 if (attr->la_valid & LA_SIZE)
2533 size = attr->la_size;
2536 rc = lod_prepare_inuse(env, lo);
2541 * prepare OST object creation for the component covering file's
2542 * size, the 1st component (including plain layout file) is always
2545 for (i = 0; i < lo->ldo_comp_cnt; i++) {
2546 struct lod_layout_component *lod_comp;
2547 struct lu_extent *extent;
2549 lod_comp = &lo->ldo_comp_entries[i];
2550 extent = &lod_comp->llc_extent;
2551 CDEBUG(D_QOS, "%lld [%lld, %lld)\n",
2552 size, extent->e_start, extent->e_end);
2553 if (!lo->ldo_is_composite || size >= extent->e_start) {
2554 rc = lod_qos_prep_create(env, lo, attr, th, i, inuse);