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, 2014, 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 <obd_class.h>
43 #include <lustre/lustre_idl.h>
44 #include "lod_internal.h"
47 * force QoS policy (not RR) to be used for testing purposes
53 #define QOS_DEBUG(fmt, ...) CDEBUG(D_QOS, fmt, ## __VA_ARGS__)
54 #define QOS_CONSOLE(fmt, ...) LCONSOLE(D_QOS, fmt, ## __VA_ARGS__)
56 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
57 OST_TGT(lod,i)->ltd_statfs.os_bsize)
60 * Add a new target to Quality of Service (QoS) target table.
62 * Add a new OST target to the structure representing an OSS. Resort the list
63 * of known OSSs by the number of OSTs attached to each OSS. The OSS list is
64 * protected internally and no external locking is required.
66 * \param[in] lod LOD device
67 * \param[in] ost_desc OST description
69 * \retval 0 on success
70 * \retval -ENOMEM on error
72 int qos_add_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
74 struct lod_qos_oss *oss = NULL, *temposs;
75 struct obd_export *exp = ost_desc->ltd_exp;
76 int rc = 0, found = 0;
77 struct list_head *list;
80 down_write(&lod->lod_qos.lq_rw_sem);
82 * a bit hacky approach to learn NID of corresponding connection
83 * but there is no official API to access information like this
86 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
87 if (obd_uuid_equals(&oss->lqo_uuid,
88 &exp->exp_connection->c_remote_uuid)) {
97 GOTO(out, rc = -ENOMEM);
98 memcpy(&oss->lqo_uuid, &exp->exp_connection->c_remote_uuid,
99 sizeof(oss->lqo_uuid));
101 /* Assume we have to move this one */
102 list_del(&oss->lqo_oss_list);
105 oss->lqo_ost_count++;
106 ost_desc->ltd_qos.ltq_oss = oss;
108 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
109 obd_uuid2str(&ost_desc->ltd_uuid), obd_uuid2str(&oss->lqo_uuid),
112 /* Add sorted by # of OSTs. Find the first entry that we're
114 list = &lod->lod_qos.lq_oss_list;
115 list_for_each_entry(temposs, list, lqo_oss_list) {
116 if (oss->lqo_ost_count > temposs->lqo_ost_count)
119 /* ...and add before it. If we're the first or smallest, temposs
120 points to the list head, and we add to the end. */
121 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
123 lod->lod_qos.lq_dirty = 1;
124 lod->lod_qos.lq_rr.lqr_dirty = 1;
127 up_write(&lod->lod_qos.lq_rw_sem);
132 * Remove OST target from QoS table.
134 * Removes given OST target from QoS table and releases related OSS structure
135 * if no OSTs remain on the OSS.
137 * \param[in] lod LOD device
138 * \param[in] ost_desc OST description
140 * \retval 0 on success
141 * \retval -ENOENT if no OSS was found
143 int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
145 struct lod_qos_oss *oss;
149 down_write(&lod->lod_qos.lq_rw_sem);
150 oss = ost_desc->ltd_qos.ltq_oss;
152 GOTO(out, rc = -ENOENT);
154 oss->lqo_ost_count--;
155 if (oss->lqo_ost_count == 0) {
156 CDEBUG(D_QOS, "removing OSS %s\n",
157 obd_uuid2str(&oss->lqo_uuid));
158 list_del(&oss->lqo_oss_list);
159 ost_desc->ltd_qos.ltq_oss = NULL;
163 lod->lod_qos.lq_dirty = 1;
164 lod->lod_qos.lq_rr.lqr_dirty = 1;
166 up_write(&lod->lod_qos.lq_rw_sem);
171 * Check whether the target is available for new OST objects.
173 * Request statfs data from the given target and verify it's active and not
174 * read-only. If so, then it can be used to place new OST objects. This
175 * function also maintains the number of active/inactive targets and sets
176 * dirty flags if those numbers change so others can run re-balance procedures.
177 * No external locking is required.
179 * \param[in] env execution environment for this thread
180 * \param[in] d LOD device
181 * \param[in] index index of OST target to check
182 * \param[out] sfs buffer for statfs data
184 * \retval 0 if the target is good
185 * \retval negative negated errno on error
188 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
189 int index, struct obd_statfs *sfs)
191 struct lod_tgt_desc *ost;
196 ost = OST_TGT(d,index);
199 rc = dt_statfs(env, ost->ltd_ost, sfs);
200 if (rc && rc != -ENOTCONN)
201 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
203 /* If the OST is readonly then we can't allocate objects there */
204 if (sfs->os_state & OS_STATE_READONLY)
207 /* check whether device has changed state (active, inactive) */
208 if (rc != 0 && ost->ltd_active) {
209 /* turned inactive? */
210 spin_lock(&d->lod_desc_lock);
211 if (ost->ltd_active) {
213 LASSERT(d->lod_desc.ld_active_tgt_count > 0);
214 d->lod_desc.ld_active_tgt_count--;
215 d->lod_qos.lq_dirty = 1;
216 d->lod_qos.lq_rr.lqr_dirty = 1;
217 CDEBUG(D_CONFIG, "%s: turns inactive\n",
218 ost->ltd_exp->exp_obd->obd_name);
220 spin_unlock(&d->lod_desc_lock);
221 } else if (rc == 0 && ost->ltd_active == 0) {
223 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
224 "active tgt count %d, ost nr %d\n",
225 d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
226 spin_lock(&d->lod_desc_lock);
227 if (ost->ltd_active == 0) {
229 d->lod_desc.ld_active_tgt_count++;
230 d->lod_qos.lq_dirty = 1;
231 d->lod_qos.lq_rr.lqr_dirty = 1;
232 CDEBUG(D_CONFIG, "%s: turns active\n",
233 ost->ltd_exp->exp_obd->obd_name);
235 spin_unlock(&d->lod_desc_lock);
242 * Maintain per-target statfs data.
244 * The function refreshes statfs data for all the targets every N seconds.
245 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
248 * \param[in] env execution environment for this thread
249 * \param[in] lod LOD device
251 static void lod_qos_statfs_update(const struct lu_env *env,
252 struct lod_device *lod)
254 struct obd_device *obd = lod2obd(lod);
255 struct ost_pool *osts = &(lod->lod_pool_info);
258 __u64 max_age, avail;
261 max_age = cfs_time_shift_64(-2 * lod->lod_desc.ld_qos_maxage);
263 if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
264 /* statfs data are quite recent, don't need to refresh it */
267 down_write(&lod->lod_qos.lq_rw_sem);
268 if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
271 for (i = 0; i < osts->op_count; i++) {
272 idx = osts->op_array[i];
273 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
274 if (lod_statfs_and_check(env, lod, idx,
275 &OST_TGT(lod, idx)->ltd_statfs))
277 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
278 /* recalculate weigths */
279 lod->lod_qos.lq_dirty = 1;
281 obd->obd_osfs_age = cfs_time_current_64();
284 up_write(&lod->lod_qos.lq_rw_sem);
289 * Calculate per-OST and per-OSS penalties
291 * Re-calculate penalties when the configuration changes, active targets
292 * change and after statfs refresh (all these are reflected by lq_dirty flag).
293 * On every OST and OSS: decay the penalty by half for every 8x the update
294 * interval that the device has been idle. That gives lots of time for the
295 * statfs information to be updated (which the penalty is only a proxy for),
296 * and avoids penalizing OSS/OSTs under light load.
297 * See lod_qos_calc_weight() for how penalties are factored into the weight.
299 * \param[in] lod LOD device
301 * \retval 0 on success
302 * \retval -EAGAIN the number of OSTs isn't enough
304 static int lod_qos_calc_ppo(struct lod_device *lod)
306 struct lod_qos_oss *oss;
307 __u64 ba_max, ba_min, temp;
314 if (!lod->lod_qos.lq_dirty)
317 num_active = lod->lod_desc.ld_active_tgt_count - 1;
319 GOTO(out, rc = -EAGAIN);
321 /* find bavail on each OSS */
322 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
324 lod->lod_qos.lq_active_oss_count = 0;
327 * How badly user wants to select OSTs "widely" (not recently chosen
328 * and not on recent OSS's). As opposed to "freely" (free space
331 prio_wide = 256 - lod->lod_qos.lq_prio_free;
333 ba_min = (__u64)(-1);
335 now = cfs_time_current_sec();
336 /* Calculate OST penalty per object
337 * (lod ref taken in lod_qos_prep_create()) */
338 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
339 LASSERT(OST_TGT(lod,i));
340 temp = TGT_BAVAIL(i);
343 ba_min = min(temp, ba_min);
344 ba_max = max(temp, ba_max);
346 /* Count the number of usable OSS's */
347 if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
348 lod->lod_qos.lq_active_oss_count++;
349 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
351 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
353 do_div(temp, num_active);
354 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
355 (temp * prio_wide) >> 8;
357 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
358 if (lod->lod_qos.lq_reset ||
359 age > 32 * lod->lod_desc.ld_qos_maxage)
360 OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
361 else if (age > lod->lod_desc.ld_qos_maxage)
362 /* Decay OST penalty. */
363 OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
364 (age / lod->lod_desc.ld_qos_maxage);
367 num_active = lod->lod_qos.lq_active_oss_count - 1;
368 if (num_active < 1) {
369 /* If there's only 1 OSS, we can't penalize it, so instead
370 we have to double the OST penalty */
372 cfs_foreach_bit(lod->lod_ost_bitmap, i)
373 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
376 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
377 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
378 temp = oss->lqo_bavail >> 1;
379 do_div(temp, oss->lqo_ost_count * num_active);
380 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
382 age = (now - oss->lqo_used) >> 3;
383 if (lod->lod_qos.lq_reset ||
384 age > 32 * lod->lod_desc.ld_qos_maxage)
385 oss->lqo_penalty = 0;
386 else if (age > lod->lod_desc.ld_qos_maxage)
387 /* Decay OSS penalty. */
388 oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
391 lod->lod_qos.lq_dirty = 0;
392 lod->lod_qos.lq_reset = 0;
394 /* If each ost has almost same free space,
395 * do rr allocation for better creation performance */
396 lod->lod_qos.lq_same_space = 0;
397 if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
398 lod->lod_qos.lq_same_space = 1;
399 /* Reset weights for the next time we enter qos mode */
400 lod->lod_qos.lq_reset = 1;
406 if (!rc && lod->lod_qos.lq_same_space)
413 * Calculate weight for a given OST target.
415 * The final OST weight is the number of bytes available minus the OST and
416 * OSS penalties. See lod_qos_calc_ppo() for how penalties are calculated.
418 * \param[in] lod LOD device, where OST targets are listed
419 * \param[in] i OST target index
423 static int lod_qos_calc_weight(struct lod_device *lod, int i)
427 temp = TGT_BAVAIL(i);
428 temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
429 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
431 OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
433 OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
438 * Re-calculate weights.
440 * The function is called when some OST target was used for a new object. In
441 * this case we should re-calculate all the weights to keep new allocations
444 * \param[in] lod LOD device
445 * \param[in] osts OST pool where a new object was placed
446 * \param[in] index OST target where a new object was placed
447 * \param[out] total_wt new total weight for the pool
451 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
452 __u32 index, __u64 *total_wt)
454 struct lod_tgt_desc *ost;
455 struct lod_qos_oss *oss;
459 ost = OST_TGT(lod,index);
462 /* Don't allocate on this devuce anymore, until the next alloc_qos */
463 ost->ltd_qos.ltq_usable = 0;
465 oss = ost->ltd_qos.ltq_oss;
467 /* Decay old penalty by half (we're adding max penalty, and don't
468 want it to run away.) */
469 ost->ltd_qos.ltq_penalty >>= 1;
470 oss->lqo_penalty >>= 1;
472 /* mark the OSS and OST as recently used */
473 ost->ltd_qos.ltq_used = oss->lqo_used = cfs_time_current_sec();
475 /* Set max penalties for this OST and OSS */
476 ost->ltd_qos.ltq_penalty +=
477 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
478 oss->lqo_penalty += oss->lqo_penalty_per_obj *
479 lod->lod_qos.lq_active_oss_count;
481 /* Decrease all OSS penalties */
482 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
483 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
484 oss->lqo_penalty = 0;
486 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
490 /* Decrease all OST penalties */
491 for (j = 0; j < osts->op_count; j++) {
494 i = osts->op_array[j];
495 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
498 ost = OST_TGT(lod,i);
501 if (ost->ltd_qos.ltq_penalty <
502 ost->ltd_qos.ltq_penalty_per_obj)
503 ost->ltd_qos.ltq_penalty = 0;
505 ost->ltd_qos.ltq_penalty -=
506 ost->ltd_qos.ltq_penalty_per_obj;
508 lod_qos_calc_weight(lod, i);
510 /* Recalc the total weight of usable osts */
511 if (ost->ltd_qos.ltq_usable)
512 *total_wt += ost->ltd_qos.ltq_weight;
514 QOS_DEBUG("recalc tgt %d usable=%d avail="LPU64
515 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
516 " ossp="LPU64" wt="LPU64"\n",
517 i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
518 ost->ltd_qos.ltq_penalty_per_obj >> 10,
519 ost->ltd_qos.ltq_penalty >> 10,
520 ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
521 ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
522 ost->ltd_qos.ltq_weight >> 10);
528 void lod_qos_rr_init(struct lod_qos_rr *lqr)
530 spin_lock_init(&lqr->lqr_alloc);
535 #define LOV_QOS_EMPTY ((__u32)-1)
538 * Calculate optimal round-robin order with regard to OSSes.
540 * Place all the OSTs from pool \a src_pool in a special array to be used for
541 * round-robin (RR) stripe allocation. The placement algorithm interleaves
542 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
543 * Resorts the targets when the number of active targets changes (because of
544 * a new target or activation/deactivation).
546 * \param[in] lod LOD device
547 * \param[in] src_pool OST pool
548 * \param[in] lqr round-robin list
550 * \retval 0 on success
551 * \retval -ENOMEM fails to allocate the array
553 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
554 struct lod_qos_rr *lqr)
556 struct lod_qos_oss *oss;
557 struct lod_tgt_desc *ost;
558 unsigned placed, real_count;
563 if (!lqr->lqr_dirty) {
564 LASSERT(lqr->lqr_pool.op_size);
568 /* Do actual allocation. */
569 down_write(&lod->lod_qos.lq_rw_sem);
572 * Check again. While we were sleeping on @lq_rw_sem something could
575 if (!lqr->lqr_dirty) {
576 LASSERT(lqr->lqr_pool.op_size);
577 up_write(&lod->lod_qos.lq_rw_sem);
581 real_count = src_pool->op_count;
583 /* Zero the pool array */
584 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
585 deleting from the pool. The lq_rw_sem insures that nobody else
587 lqr->lqr_pool.op_count = real_count;
588 rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
590 up_write(&lod->lod_qos.lq_rw_sem);
593 for (i = 0; i < lqr->lqr_pool.op_count; i++)
594 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
596 /* Place all the OSTs from 1 OSS at the same time. */
598 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
601 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
604 if (!cfs_bitmap_check(lod->lod_ost_bitmap,
605 src_pool->op_array[i]))
608 ost = OST_TGT(lod,src_pool->op_array[i]);
609 LASSERT(ost && ost->ltd_ost);
610 if (ost->ltd_qos.ltq_oss != oss)
613 /* Evenly space these OSTs across arrayspace */
614 next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
615 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
616 next = (next + 1) % lqr->lqr_pool.op_count;
618 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
625 up_write(&lod->lod_qos.lq_rw_sem);
627 if (placed != real_count) {
628 /* This should never happen */
629 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
630 "round-robin list (%d of %d).\n",
632 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
633 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
634 lqr->lqr_pool.op_array[i]);
641 for (i = 0; i < lqr->lqr_pool.op_count; i++)
642 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
649 * Instantiate and declare creation of a new object.
651 * The function instantiates LU representation for a new object on the
652 * specified device. Also it declares an intention to create that
653 * object on the storage target.
655 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
656 * infrastructure - in the existing precreation framework we can't assign FID
657 * at this moment, we do this later once a transaction is started. So the
658 * special method instantiates FID-less object in the cache and later it
659 * will get a FID and proper placement in LU cache.
661 * \param[in] env execution environment for this thread
662 * \param[in] d LOD device
663 * \param[in] ost_idx OST target index where the object is being created
664 * \param[in] th transaction handle
666 * \retval object ptr on success, ERR_PTR() otherwise
668 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
669 struct lod_device *d,
673 struct lod_tgt_desc *ost;
674 struct lu_object *o, *n;
675 struct lu_device *nd;
676 struct dt_object *dt;
681 LASSERT(ost_idx < d->lod_osts_size);
682 ost = OST_TGT(d,ost_idx);
684 LASSERT(ost->ltd_ost);
686 nd = &ost->ltd_ost->dd_lu_dev;
689 * allocate anonymous object with zero fid, real fid
690 * will be assigned by OSP within transaction
691 * XXX: to be fixed with fully-functional OST fids
693 o = lu_object_anon(env, nd, NULL);
695 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
697 n = lu_object_locate(o->lo_header, nd->ld_type);
698 if (unlikely(n == NULL)) {
699 CERROR("can't find slice\n");
700 lu_object_put(env, o);
701 GOTO(out, dt = ERR_PTR(-EINVAL));
704 dt = container_of(n, struct dt_object, do_lu);
706 rc = lod_sub_object_declare_create(env, dt, NULL, NULL, NULL, th);
708 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
710 lu_object_put(env, o);
719 * Calculate a minimum acceptable stripe count.
721 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
722 * all stripes or 3/4 of stripes.
724 * \param[in] stripe_cnt number of stripes requested
725 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
727 * \retval acceptable stripecount
729 static int min_stripe_count(__u32 stripe_cnt, int flags)
731 return (flags & LOV_USES_DEFAULT_STRIPE ?
732 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
735 #define LOV_CREATE_RESEED_MULT 30
736 #define LOV_CREATE_RESEED_MIN 2000
739 * Check if an OST is full.
741 * Check whether an OST should be considered full based
742 * on the given statfs data.
744 * \param[in] msfs statfs data
746 * \retval false not full
749 static int inline lod_qos_dev_is_full(struct obd_statfs *msfs)
752 int bs = msfs->os_bsize;
754 LASSERT(((bs - 1) & bs) == 0);
756 /* the minimum of 0.1% used blocks and 1GB bytes. */
757 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
758 1 << (31 - ffs(bs)));
759 return (msfs->os_bavail < used);
763 * Initialize temporary OST-in-use array.
765 * Allocate or extend the array used to mark targets already assigned to a new
766 * striping so they are not used more than once.
768 * \param[in] env execution environment for this thread
769 * \param[in] stripes number of items needed in the array
771 * \retval 0 on success
772 * \retval -ENOMEM on error
774 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
777 struct lod_thread_info *info = lod_env_info(env);
779 if (info->lti_ea_store_size < sizeof(int) * stripes)
780 lod_ea_store_resize(info, stripes * sizeof(int));
781 if (info->lti_ea_store_size < sizeof(int) * stripes) {
782 CERROR("can't allocate memory for ost-in-use array\n");
785 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
790 * Remember a target in the array of used targets.
792 * Mark the given target as used for a new striping being created. The status
793 * of an OST in a striping can be checked with lod_qos_is_ost_used().
795 * \param[in] env execution environment for this thread
796 * \param[in] idx index in the array
797 * \param[in] ost OST target index to mark as used
799 static inline void lod_qos_ost_in_use(const struct lu_env *env,
802 struct lod_thread_info *info = lod_env_info(env);
803 int *osts = info->lti_ea_store;
805 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
810 * Check is OST used in a striping.
812 * Checks whether OST with the given index is marked as used in the temporary
813 * array (see lod_qos_ost_in_use()).
815 * \param[in] env execution environment for this thread
816 * \param[in] ost OST target index to check
817 * \param[in] stripes the number of items used in the array already
822 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
824 struct lod_thread_info *info = lod_env_info(env);
825 int *osts = info->lti_ea_store;
828 for (j = 0; j < stripes; j++) {
835 static int lod_check_and_reserve_ost(const struct lu_env *env,
836 struct lod_device *m,
837 struct obd_statfs *sfs, __u32 ost_idx,
838 __u32 speed, __u32 *s_idx,
839 struct dt_object **stripe,
843 __u32 stripe_idx = *s_idx;
846 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
848 /* this OSP doesn't feel well */
855 if (lod_qos_dev_is_full(sfs)) {
856 QOS_DEBUG("#%d is full\n", ost_idx);
861 * We expect number of precreated objects in f_ffree at
862 * the first iteration, skip OSPs with no objects ready
864 if (sfs->os_fprecreated == 0 && speed == 0) {
865 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
870 * try to use another OSP if this one is degraded
872 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
873 QOS_DEBUG("#%d: degraded\n", ost_idx);
878 * do not put >1 objects on a single OST
880 if (speed && lod_qos_is_ost_used(env, ost_idx, stripe_idx))
883 o = lod_qos_declare_object_on(env, m, ost_idx, th);
885 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
886 ost_idx, (int) PTR_ERR(o));
892 * We've successfully declared (reserved) an object
894 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
895 stripe[stripe_idx] = o;
904 * Allocate a striping using round-robin algorithm.
906 * Allocates a new striping using round-robin algorithm. The function refreshes
907 * all the internal structures (statfs cache, array of available OSTs sorted
908 * with regard to OSS, etc). The number of stripes required is taken from the
909 * object (must be prepared by the caller), but can change if the flag
910 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
911 * is trying to create a striping on the object in parallel. All the internal
912 * structures (like pools, etc) are protected and no additional locking is
913 * required. The function succeeds even if a single stripe is allocated. To save
914 * time we give priority to targets which already have objects precreated.
915 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
917 * \param[in] env execution environment for this thread
918 * \param[in] lo LOD object
919 * \param[out] stripe striping created
920 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
921 * \param[in] th transaction handle
923 * \retval 0 on success
924 * \retval -ENOSPC if not enough OSTs are found
925 * \retval negative negated errno for other failures
927 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
928 struct dt_object **stripe, int flags,
931 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
932 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
933 struct pool_desc *pool = NULL;
934 struct ost_pool *osts;
935 struct lod_qos_rr *lqr;
936 unsigned int i, array_idx;
938 __u32 ost_start_idx_temp;
940 __u32 stripe_idx = 0;
941 __u32 stripe_cnt = lo->ldo_stripenr;
942 __u32 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
947 pool = lod_find_pool(m, lo->ldo_pool);
950 down_read(&pool_tgt_rw_sem(pool));
951 osts = &(pool->pool_obds);
952 lqr = &(pool->pool_rr);
954 osts = &(m->lod_pool_info);
955 lqr = &(m->lod_qos.lq_rr);
958 rc = lod_qos_calc_rr(m, osts, lqr);
962 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
966 down_read(&m->lod_qos.lq_rw_sem);
967 spin_lock(&lqr->lqr_alloc);
968 if (--lqr->lqr_start_count <= 0) {
969 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
970 lqr->lqr_start_count =
971 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
972 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
973 } else if (stripe_cnt_min >= osts->op_count ||
974 lqr->lqr_start_idx > osts->op_count) {
975 /* If we have allocated from all of the OSTs, slowly
976 * precess the next start if the OST/stripe count isn't
977 * already doing this for us. */
978 lqr->lqr_start_idx %= osts->op_count;
979 if (stripe_cnt > 1 && (osts->op_count % stripe_cnt) != 1)
980 ++lqr->lqr_offset_idx;
982 ost_start_idx_temp = lqr->lqr_start_idx;
986 QOS_DEBUG("pool '%s' want %d startidx %d startcnt %d offset %d "
987 "active %d count %d\n",
988 lo->ldo_pool ? lo->ldo_pool : "",
989 stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
990 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
992 for (i = 0; i < osts->op_count && stripe_idx < lo->ldo_stripenr; i++) {
993 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
995 ++lqr->lqr_start_idx;
996 ost_idx = lqr->lqr_pool.op_array[array_idx];
998 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
999 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
1000 stripe_idx, array_idx, ost_idx);
1002 if ((ost_idx == LOV_QOS_EMPTY) ||
1003 !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1006 /* Fail Check before osc_precreate() is called
1007 so we can only 'fail' single OSC. */
1008 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1011 spin_unlock(&lqr->lqr_alloc);
1012 rc = lod_check_and_reserve_ost(env, m, sfs, ost_idx, speed,
1013 &stripe_idx, stripe, th);
1014 spin_lock(&lqr->lqr_alloc);
1016 if ((speed < 2) && (stripe_idx < stripe_cnt_min)) {
1017 /* Try again, allowing slower OSCs */
1019 lqr->lqr_start_idx = ost_start_idx_temp;
1023 spin_unlock(&lqr->lqr_alloc);
1024 up_read(&m->lod_qos.lq_rw_sem);
1027 lo->ldo_stripenr = stripe_idx;
1028 /* at least one stripe is allocated */
1031 /* nobody provided us with a single object */
1037 up_read(&pool_tgt_rw_sem(pool));
1038 /* put back ref got by lod_find_pool() */
1039 lod_pool_putref(pool);
1046 * Allocate a specific striping layout on a user defined set of OSTs.
1048 * Allocates new striping using the OST index range provided by the data from
1049 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1050 * OSTs are not considered. The exact order of OSTs requested by the user
1051 * is respected as much as possible depending on OST status. The number of
1052 * stripes needed and stripe offset are taken from the object. If that number
1053 * can not be met, then the function returns a failure and then it's the
1054 * caller's responsibility to release the stripes allocated. All the internal
1055 * structures are protected, but no concurrent allocation is allowed on the
1058 * \param[in] env execution environment for this thread
1059 * \param[in] lo LOD object
1060 * \param[out] stripe striping created
1061 * \param[in] lum stripe md to specify list of OSTs
1062 * \param[in] th transaction handle
1064 * \retval 0 on success
1065 * \retval -ENODEV OST index does not exist on file system
1066 * \retval -EINVAL requested OST index is invalid
1067 * \retval negative negated errno on error
1069 static int lod_alloc_ost_list(const struct lu_env *env,
1070 struct lod_object *lo, struct dt_object **stripe,
1071 struct lov_user_md *lum, struct thandle *th)
1073 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1074 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1075 struct dt_object *o;
1076 struct lov_user_md_v3 *v3;
1077 unsigned int array_idx = 0;
1078 int stripe_count = 0;
1083 /* for specific OSTs layout */
1084 LASSERT(lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC);
1085 lustre_print_user_md(D_OTHER, lum, __func__);
1087 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1091 v3 = (struct lov_user_md_v3 *)lum;
1092 for (i = 0; i < lo->ldo_stripenr; i++) {
1093 if (v3->lmm_objects[i].l_ost_idx == lo->ldo_def_stripe_offset) {
1098 if (i == lo->ldo_stripenr) {
1100 "%s: start index %d not in the specified list of OSTs\n",
1101 lod2obd(m)->obd_name, lo->ldo_def_stripe_offset);
1105 for (i = 0; i < lo->ldo_stripenr;
1106 i++, array_idx = (array_idx + 1) % lo->ldo_stripenr) {
1107 __u32 ost_idx = v3->lmm_objects[array_idx].l_ost_idx;
1109 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1115 * do not put >1 objects on a single OST
1117 if (lod_qos_is_ost_used(env, ost_idx, stripe_count)) {
1122 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1123 if (rc < 0) /* this OSP doesn't feel well */
1126 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1130 "%s: can't declare new object on #%u: %d\n",
1131 lod2obd(m)->obd_name, ost_idx, rc);
1136 * We've successfully declared (reserved) an object
1138 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1139 stripe[stripe_count] = o;
1147 * Allocate a striping on a predefined set of OSTs.
1149 * Allocates new striping starting from OST provided lo->ldo_def_stripe_offset.
1150 * Full OSTs are not considered. The exact order of OSTs is not important and
1151 * varies depending on OST status. The allocation procedure prefers the targets
1152 * with precreated objects ready. The number of stripes needed and stripe
1153 * offset are taken from the object. If that number can not be met, then the
1154 * function returns a failure and then it's the caller's responsibility to
1155 * release the stripes allocated. All the internal structures are protected,
1156 * but no concurrent allocation is allowed on the same objects.
1158 * \param[in] env execution environment for this thread
1159 * \param[in] lo LOD object
1160 * \param[out] stripe striping created
1161 * \param[in] flags not used
1162 * \param[in] th transaction handle
1164 * \retval 0 on success
1165 * \retval -E2BIG if no enough OSTs are found
1166 * \retval -EINVAL requested offset is invalid
1167 * \retval negative negated errno on error
1169 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1170 struct dt_object **stripe, int flags,
1173 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1174 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1175 struct dt_object *o;
1177 unsigned int i, array_idx, ost_count;
1178 int rc, stripe_num = 0;
1180 struct pool_desc *pool = NULL;
1181 struct ost_pool *osts;
1184 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1189 pool = lod_find_pool(m, lo->ldo_pool);
1192 down_read(&pool_tgt_rw_sem(pool));
1193 osts = &(pool->pool_obds);
1195 osts = &(m->lod_pool_info);
1198 ost_count = osts->op_count;
1201 /* search loi_ost_idx in ost array */
1203 for (i = 0; i < ost_count; i++) {
1204 if (osts->op_array[i] == lo->ldo_def_stripe_offset) {
1209 if (i == ost_count) {
1210 CERROR("Start index %d not found in pool '%s'\n",
1211 lo->ldo_def_stripe_offset,
1212 lo->ldo_pool ? lo->ldo_pool : "");
1213 GOTO(out, rc = -EINVAL);
1216 for (i = 0; i < ost_count;
1217 i++, array_idx = (array_idx + 1) % ost_count) {
1218 ost_idx = osts->op_array[array_idx];
1220 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1223 /* Fail Check before osc_precreate() is called
1224 so we can only 'fail' single OSC. */
1225 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1229 * do not put >1 objects on a single OST
1231 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
1234 /* Drop slow OSCs if we can, but not for requested start idx.
1236 * This means "if OSC is slow and it is not the requested
1237 * start OST, then it can be skipped, otherwise skip it only
1238 * if it is inactive/recovering/out-of-space." */
1240 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1242 /* this OSP doesn't feel well */
1247 * We expect number of precreated objects in f_ffree at
1248 * the first iteration, skip OSPs with no objects ready
1249 * don't apply this logic to OST specified with stripe_offset
1251 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1254 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1256 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1257 ost_idx, (int) PTR_ERR(o));
1262 * We've successfully declared (reserved) an object
1264 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1265 stripe[stripe_num] = o;
1268 /* We have enough stripes */
1269 if (stripe_num == lo->ldo_stripenr)
1273 /* Try again, allowing slower OSCs */
1278 /* If we were passed specific striping params, then a failure to
1279 * meet those requirements is an error, since we can't reallocate
1280 * that memory (it might be part of a larger array or something).
1282 * We can only get here if lsm_stripe_count was originally > 1.
1284 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1285 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1290 up_read(&pool_tgt_rw_sem(pool));
1291 /* put back ref got by lod_find_pool() */
1292 lod_pool_putref(pool);
1299 * Check whether QoS allocation should be used.
1301 * A simple helper to decide when QoS allocation should be used:
1302 * if it's just a single available target or the used space is
1303 * evenly distributed among the targets at the moment, then QoS
1304 * allocation algorithm should not be used.
1306 * \param[in] lod LOD device
1308 * \retval 0 should not be used
1309 * \retval 1 should be used
1311 static inline int lod_qos_is_usable(struct lod_device *lod)
1314 /* to be able to debug QoS code */
1318 /* Detect -EAGAIN early, before expensive lock is taken. */
1319 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1322 if (lod->lod_desc.ld_active_tgt_count < 2)
1329 * Allocate a striping using an algorithm with weights.
1331 * The function allocates OST objects to create a striping. The algorithm
1332 * used is based on weights (currently only using the free space), and it's
1333 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1334 * configuration (# of stripes, offset,
1335 * pool) is taken from the object and is prepared by the caller.
1336 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1337 * be met due to too few OSTs, then allocation fails. If the flag is
1338 * passed and less than 75% of the requested number of stripes can be
1339 * allocated, then allocation fails.
1340 * No concurrent allocation is allowed on the object and this must be
1341 * ensured by the caller. All the internal structures are protected by the
1343 * The algorithm has two steps: find available OSTs and calucate their weights,
1344 * then select the OSTs the weights used as the probability. An OST with a
1345 * higher weight is proportionately more likely to be selected than one with
1348 * \param[in] env execution environment for this thread
1349 * \param[in] lo LOD object
1350 * \param[out] stripe striping created
1351 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1352 * \param[in] th transaction handle
1354 * \retval 0 on success
1355 * \retval -E2BIG if no enough OSTs are found
1356 * \retval -EINVAL requested offset is invalid
1357 * \retval negative negated errno on error
1359 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1360 struct dt_object **stripe, int flags,
1363 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1364 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1365 struct lod_tgt_desc *ost;
1366 struct dt_object *o;
1367 __u64 total_weight = 0;
1370 __u32 nfound, good_osts;
1371 __u32 stripe_cnt = lo->ldo_stripenr;
1372 __u32 stripe_cnt_min;
1373 struct pool_desc *pool = NULL;
1374 struct ost_pool *osts;
1377 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
1378 if (stripe_cnt_min < 1)
1382 pool = lod_find_pool(m, lo->ldo_pool);
1385 down_read(&pool_tgt_rw_sem(pool));
1386 osts = &(pool->pool_obds);
1388 osts = &(m->lod_pool_info);
1391 /* Detect -EAGAIN early, before expensive lock is taken. */
1392 if (!lod_qos_is_usable(m))
1393 GOTO(out_nolock, rc = -EAGAIN);
1395 /* Do actual allocation, use write lock here. */
1396 down_write(&m->lod_qos.lq_rw_sem);
1399 * Check again, while we were sleeping on @lq_rw_sem things could
1402 if (!lod_qos_is_usable(m))
1403 GOTO(out, rc = -EAGAIN);
1405 rc = lod_qos_calc_ppo(m);
1409 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1414 /* Find all the OSTs that are valid stripe candidates */
1415 for (i = 0; i < osts->op_count; i++) {
1416 if (!cfs_bitmap_check(m->lod_ost_bitmap, osts->op_array[i]))
1419 rc = lod_statfs_and_check(env, m, osts->op_array[i], sfs);
1421 /* this OSP doesn't feel well */
1428 if (lod_qos_dev_is_full(sfs))
1431 /* Fail Check before osc_precreate() is called
1432 so we can only 'fail' single OSC. */
1433 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1434 osts->op_array[i] == 0)
1437 ost = OST_TGT(m,osts->op_array[i]);
1438 ost->ltd_qos.ltq_usable = 1;
1439 lod_qos_calc_weight(m, osts->op_array[i]);
1440 total_weight += ost->ltd_qos.ltq_weight;
1445 QOS_DEBUG("found %d good osts\n", good_osts);
1447 if (good_osts < stripe_cnt_min)
1448 GOTO(out, rc = -EAGAIN);
1450 /* We have enough osts */
1451 if (good_osts < stripe_cnt)
1452 stripe_cnt = good_osts;
1454 /* Find enough OSTs with weighted random allocation. */
1456 while (nfound < stripe_cnt) {
1457 __u64 rand, cur_weight;
1463 #if BITS_PER_LONG == 32
1464 rand = cfs_rand() % (unsigned)total_weight;
1465 /* If total_weight > 32-bit, first generate the high
1466 * 32 bits of the random number, then add in the low
1467 * 32 bits (truncated to the upper limit, if needed) */
1468 if (total_weight > 0xffffffffULL)
1469 rand = (__u64)(cfs_rand() %
1470 (unsigned)(total_weight >> 32)) << 32;
1474 if (rand == (total_weight & 0xffffffff00000000ULL))
1475 rand |= cfs_rand() % (unsigned)total_weight;
1480 rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1487 /* On average, this will hit larger-weighted osts more often.
1488 0-weight osts will always get used last (only when rand=0) */
1489 for (i = 0; i < osts->op_count; i++) {
1490 __u32 idx = osts->op_array[i];
1492 if (!cfs_bitmap_check(m->lod_ost_bitmap, idx))
1495 ost = OST_TGT(m,idx);
1497 if (!ost->ltd_qos.ltq_usable)
1500 cur_weight += ost->ltd_qos.ltq_weight;
1501 QOS_DEBUG("stripe_cnt=%d nfound=%d cur_weight="LPU64
1502 " rand="LPU64" total_weight="LPU64"\n",
1503 stripe_cnt, nfound, cur_weight, rand,
1506 if (cur_weight < rand)
1509 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1512 * do not put >1 objects on a single OST
1514 if (lod_qos_is_ost_used(env, idx, nfound))
1516 lod_qos_ost_in_use(env, nfound, idx);
1518 o = lod_qos_declare_object_on(env, m, idx, th);
1520 QOS_DEBUG("can't declare object on #%u: %d\n",
1521 idx, (int) PTR_ERR(o));
1524 stripe[nfound++] = o;
1525 lod_qos_used(m, osts, idx, &total_weight);
1531 /* no OST found on this iteration, give up */
1536 if (unlikely(nfound != stripe_cnt)) {
1538 * when the decision to use weighted algorithm was made
1539 * we had enough appropriate OSPs, but this state can
1540 * change anytime (no space on OST, broken connection, etc)
1541 * so it's possible OSP won't be able to provide us with
1542 * an object due to just changed state
1544 LCONSOLE_INFO("wanted %d, found %d\n", stripe_cnt, nfound);
1545 for (i = 0; i < nfound; i++) {
1546 LASSERT(stripe[i] != NULL);
1547 lu_object_put(env, &stripe[i]->do_lu);
1551 /* makes sense to rebalance next time */
1552 m->lod_qos.lq_dirty = 1;
1553 m->lod_qos.lq_same_space = 0;
1559 up_write(&m->lod_qos.lq_rw_sem);
1563 up_read(&pool_tgt_rw_sem(pool));
1564 /* put back ref got by lod_find_pool() */
1565 lod_pool_putref(pool);
1572 * Find largest stripe count the caller can use.
1574 * Find the maximal possible stripe count not greater than \a stripe_count.
1575 * Sometimes suggested stripecount can't be reached for a number of reasons:
1576 * lack of enough active OSTs or the backend does not support EAs that large.
1577 * If the passed one is 0, then the filesystem's default one is used.
1579 * \param[in] lod LOD device
1580 * \param[in] magic the format if striping
1581 * \param[in] stripe_count count the caller would like to use
1583 * \retval the maximum usable stripe count
1585 static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
1588 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1591 stripe_count = lod->lod_desc.ld_default_stripe_count;
1592 if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1593 stripe_count = lod->lod_desc.ld_active_tgt_count;
1597 /* stripe count is based on whether OSD can handle larger EA sizes */
1598 if (lod->lod_osd_max_easize > 0)
1599 max_stripes = lov_mds_md_max_stripe_count(
1600 lod->lod_osd_max_easize, magic);
1602 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1606 * Create in-core respresentation for a fully-defined striping
1608 * When the caller passes a fully-defined striping (i.e. everything including
1609 * OST object FIDs are defined), then we still need to instantiate LU-cache
1610 * with the objects representing the stripes defined. This function completes
1613 * \param[in] env execution environment for this thread
1614 * \param[in] mo LOD object
1615 * \param[in] buf buffer containing the striping
1617 * \retval 0 on success
1618 * \retval negative negated errno on error
1620 static int lod_use_defined_striping(const struct lu_env *env,
1621 struct lod_object *mo,
1622 const struct lu_buf *buf)
1624 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1625 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1626 struct lov_ost_data_v1 *objs;
1631 magic = le32_to_cpu(v1->lmm_magic);
1632 if (magic == LOV_MAGIC_V1_DEF) {
1633 magic = LOV_MAGIC_V1;
1634 objs = &v1->lmm_objects[0];
1635 } else if (magic == LOV_MAGIC_V3_DEF) {
1636 magic = LOV_MAGIC_V3;
1637 objs = &v3->lmm_objects[0];
1638 lod_object_set_pool(mo, v3->lmm_pool_name);
1640 GOTO(out, rc = -EINVAL);
1643 mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
1644 mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1645 mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
1646 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1648 /* fixup for released file before object initialization */
1649 if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1650 mo->ldo_released_stripenr = mo->ldo_stripenr;
1651 mo->ldo_stripenr = 0;
1654 LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
1656 if (mo->ldo_stripenr > 0)
1657 rc = lod_initialize_objects(env, mo, objs);
1664 * Parse suggested striping configuration.
1666 * The caller gets a suggested striping configuration from a number of sources
1667 * including per-directory default and applications. Then it needs to verify
1668 * the suggested striping is valid, apply missing bits and store the resulting
1669 * configuration in the object to be used by the allocator later. Must not be
1670 * called concurrently against the same object. It's OK to provide a
1671 * fully-defined striping.
1673 * \param[in] env execution environment for this thread
1674 * \param[in] lo LOD object
1675 * \param[in] buf buffer containing the striping
1677 * \retval 0 on success
1678 * \retval negative negated errno on error
1680 static int lod_qos_parse_config(const struct lu_env *env,
1681 struct lod_object *lo,
1682 const struct lu_buf *buf)
1684 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1685 struct lov_user_md_v1 *v1 = NULL;
1686 struct lov_user_md_v3 *v3 = NULL;
1687 char *pool_name = NULL;
1693 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1698 magic = v1->lmm_magic;
1700 if (unlikely(magic == LOV_MAGIC_V1_DEF || magic == LOV_MAGIC_V3_DEF)) {
1701 /* try to use as fully defined striping */
1702 rc = lod_use_defined_striping(env, lo, buf);
1707 case __swab32(LOV_USER_MAGIC_V1):
1708 lustre_swab_lov_user_md_v1(v1);
1709 magic = v1->lmm_magic;
1711 case LOV_USER_MAGIC_V1:
1715 case __swab32(LOV_USER_MAGIC_V3):
1716 lustre_swab_lov_user_md_v3(v3);
1717 magic = v3->lmm_magic;
1719 case LOV_USER_MAGIC_V3:
1721 pool_name = v3->lmm_pool_name;
1724 case __swab32(LOV_USER_MAGIC_SPECIFIC):
1725 lustre_swab_lov_user_md_v3(v3);
1726 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1727 v3->lmm_stripe_count);
1728 magic = v3->lmm_magic;
1730 case LOV_USER_MAGIC_SPECIFIC:
1731 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
1732 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
1733 if (v3->lmm_pool_name[0] != '\0')
1734 pool_name = v3->lmm_pool_name;
1735 size = lov_user_md_size(v3->lmm_stripe_count,
1736 LOV_USER_MAGIC_SPECIFIC);
1740 CERROR("%s: unrecognized magic %X\n",
1741 lod2obd(d)->obd_name, magic);
1745 if (unlikely(buf->lb_len < size)) {
1746 CERROR("%s: wrong size: %zd, expect: %u\n",
1747 lod2obd(d)->obd_name, buf->lb_len, size);
1751 lustre_print_user_md(D_OTHER, v1, "parse config");
1753 v1->lmm_magic = magic;
1754 if (v1->lmm_pattern == 0)
1755 v1->lmm_pattern = LOV_PATTERN_RAID0;
1756 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
1757 CERROR("%s: invalid pattern: %x\n",
1758 lod2obd(d)->obd_name, v1->lmm_pattern);
1761 lo->ldo_pattern = v1->lmm_pattern;
1763 if (v1->lmm_stripe_size > 0)
1764 lo->ldo_stripe_size = v1->lmm_stripe_size;
1766 if (lo->ldo_stripe_size & (LOV_MIN_STRIPE_SIZE - 1))
1767 lo->ldo_stripe_size = LOV_MIN_STRIPE_SIZE;
1769 if (v1->lmm_stripe_count > 0)
1770 lo->ldo_stripenr = v1->lmm_stripe_count;
1772 lo->ldo_def_stripe_offset = v1->lmm_stripe_offset;
1774 lod_object_set_pool(lo, NULL);
1775 if (pool_name != NULL) {
1776 struct pool_desc *pool;
1778 /* In the function below, .hs_keycmp resolves to
1779 * pool_hashkey_keycmp() */
1780 /* coverity[overrun-buffer-val] */
1781 pool = lod_find_pool(d, pool_name);
1783 if (lo->ldo_def_stripe_offset != LOV_OFFSET_DEFAULT) {
1784 rc = lod_check_index_in_pool(
1785 lo->ldo_def_stripe_offset, pool);
1787 lod_pool_putref(pool);
1788 CERROR("%s: invalid offset, %u\n",
1789 lod2obd(d)->obd_name,
1790 lo->ldo_def_stripe_offset);
1795 if (lo->ldo_stripenr > pool_tgt_count(pool))
1796 lo->ldo_stripenr = pool_tgt_count(pool);
1798 lod_pool_putref(pool);
1801 lod_object_set_pool(lo, pool_name);
1804 /* fixup for released file */
1805 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1806 lo->ldo_released_stripenr = lo->ldo_stripenr;
1807 lo->ldo_stripenr = 0;
1814 * Create a striping for an obejct.
1816 * The function creates a new striping for the object. A buffer containing
1817 * configuration hints can be provided optionally. The function tries QoS
1818 * algorithm first unless free space is distributed evenly among OSTs, but
1819 * by default RR algorithm is preferred due to internal concurrency (QoS is
1820 * serialized). The caller must ensure no concurrent calls to the function
1821 * are made against the same object.
1823 * \param[in] env execution environment for this thread
1824 * \param[in] lo LOD object
1825 * \param[in] attr attributes OST objects will be declared with
1826 * \param[in] buf suggested striping configuration or NULL
1827 * \param[in] th transaction handle
1829 * \retval 0 on success
1830 * \retval negative negated errno on error
1832 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
1833 struct lu_attr *attr, const struct lu_buf *buf,
1836 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1837 struct dt_object **stripe;
1839 int flag = LOV_USES_ASSIGNED_STRIPE;
1845 /* no OST available */
1846 /* XXX: should we be waiting a bit to prevent failures during
1847 * cluster initialization? */
1848 if (d->lod_ostnr == 0)
1849 GOTO(out, rc = -EIO);
1852 * by this time, the object's ldo_stripenr and ldo_stripe_size
1853 * contain default value for striping: taken from the parent
1854 * or from filesystem defaults
1856 * in case the caller is passing lovea with new striping config,
1857 * we may need to parse lovea and apply new configuration
1859 rc = lod_qos_parse_config(env, lo, buf);
1863 /* A released file is being created */
1864 if (lo->ldo_stripenr == 0)
1867 if (likely(lo->ldo_stripe == NULL)) {
1868 struct lov_user_md *lum = NULL;
1871 * no striping has been created so far
1873 LASSERT(lo->ldo_stripenr > 0);
1875 * statfs and check OST targets now, since ld_active_tgt_count
1876 * could be changed if some OSTs are [de]activated manually.
1878 lod_qos_statfs_update(env, d);
1879 lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
1882 stripe_len = lo->ldo_stripenr;
1883 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
1885 GOTO(out, rc = -ENOMEM);
1887 lod_getref(&d->lod_ost_descs);
1888 /* XXX: support for non-0 files w/o objects */
1889 CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
1890 d->lod_desc.ld_tgt_count, stripe_len);
1892 if (buf != NULL && buf->lb_buf != NULL)
1895 if (lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
1896 rc = lod_alloc_ost_list(env, lo, stripe, lum, th);
1897 } else if (lo->ldo_def_stripe_offset == LOV_OFFSET_DEFAULT) {
1898 rc = lod_alloc_qos(env, lo, stripe, flag, th);
1900 rc = lod_alloc_rr(env, lo, stripe, flag, th);
1902 rc = lod_alloc_specific(env, lo, stripe, flag, th);
1904 lod_putref(d, &d->lod_ost_descs);
1907 for (i = 0; i < stripe_len; i++)
1908 if (stripe[i] != NULL)
1909 lu_object_put(env, &stripe[i]->do_lu);
1911 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
1912 lo->ldo_stripenr = 0;
1914 lo->ldo_stripe = stripe;
1915 lo->ldo_stripes_allocated = stripe_len;
1919 * lod_qos_parse_config() found supplied buf as a predefined
1920 * striping (not a hint), so it allocated all the object
1921 * now we need to create them
1923 for (i = 0; i < lo->ldo_stripenr; i++) {
1924 struct dt_object *o;
1926 o = lo->ldo_stripe[i];
1929 rc = lod_sub_object_declare_create(env, o, attr, NULL,
1932 CERROR("can't declare create: %d\n", rc);