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, 2016, 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 <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;
82 down_write(&lod->lod_qos.lq_rw_sem);
84 * a bit hacky approach to learn NID of corresponding connection
85 * but there is no official API to access information like this
88 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
89 if (obd_uuid_equals(&oss->lqo_uuid,
90 &exp->exp_connection->c_remote_uuid)) {
99 GOTO(out, rc = -ENOMEM);
100 memcpy(&oss->lqo_uuid, &exp->exp_connection->c_remote_uuid,
101 sizeof(oss->lqo_uuid));
103 /* Assume we have to move this one */
104 list_del(&oss->lqo_oss_list);
107 oss->lqo_ost_count++;
108 ost_desc->ltd_qos.ltq_oss = oss;
110 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
111 obd_uuid2str(&ost_desc->ltd_uuid), obd_uuid2str(&oss->lqo_uuid),
114 /* Add sorted by # of OSTs. Find the first entry that we're
116 list = &lod->lod_qos.lq_oss_list;
117 list_for_each_entry(temposs, list, lqo_oss_list) {
118 if (oss->lqo_ost_count > temposs->lqo_ost_count)
121 /* ...and add before it. If we're the first or smallest, temposs
122 points to the list head, and we add to the end. */
123 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
125 lod->lod_qos.lq_dirty = 1;
126 lod->lod_qos.lq_rr.lqr_dirty = 1;
129 up_write(&lod->lod_qos.lq_rw_sem);
134 * Remove OST target from QoS table.
136 * Removes given OST target from QoS table and releases related OSS structure
137 * if no OSTs remain on the OSS.
139 * \param[in] lod LOD device
140 * \param[in] ost_desc OST description
142 * \retval 0 on success
143 * \retval -ENOENT if no OSS was found
145 int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
147 struct lod_qos_oss *oss;
151 down_write(&lod->lod_qos.lq_rw_sem);
152 oss = ost_desc->ltd_qos.ltq_oss;
154 GOTO(out, rc = -ENOENT);
156 oss->lqo_ost_count--;
157 if (oss->lqo_ost_count == 0) {
158 CDEBUG(D_QOS, "removing OSS %s\n",
159 obd_uuid2str(&oss->lqo_uuid));
160 list_del(&oss->lqo_oss_list);
161 ost_desc->ltd_qos.ltq_oss = NULL;
165 lod->lod_qos.lq_dirty = 1;
166 lod->lod_qos.lq_rr.lqr_dirty = 1;
168 up_write(&lod->lod_qos.lq_rw_sem);
173 * Check whether the target is available for new OST objects.
175 * Request statfs data from the given target and verify it's active and not
176 * read-only. If so, then it can be used to place new OST objects. This
177 * function also maintains the number of active/inactive targets and sets
178 * dirty flags if those numbers change so others can run re-balance procedures.
179 * No external locking is required.
181 * \param[in] env execution environment for this thread
182 * \param[in] d LOD device
183 * \param[in] index index of OST target to check
184 * \param[out] sfs buffer for statfs data
186 * \retval 0 if the target is good
187 * \retval negative negated errno on error
190 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
191 int index, struct obd_statfs *sfs)
193 struct lod_tgt_desc *ost;
198 ost = OST_TGT(d,index);
201 rc = dt_statfs(env, ost->ltd_ost, sfs);
203 if (rc == 0 && ((sfs->os_state & OS_STATE_ENOSPC) ||
204 (sfs->os_state & OS_STATE_ENOINO && sfs->os_fprecreated == 0)))
207 if (rc && rc != -ENOTCONN)
208 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
210 /* If the OST is readonly then we can't allocate objects there */
211 if (sfs->os_state & OS_STATE_READONLY)
214 /* check whether device has changed state (active, inactive) */
215 if (rc != 0 && ost->ltd_active) {
216 /* turned inactive? */
217 spin_lock(&d->lod_lock);
218 if (ost->ltd_active) {
221 ost->ltd_connecting = 1;
223 LASSERT(d->lod_desc.ld_active_tgt_count > 0);
224 d->lod_desc.ld_active_tgt_count--;
225 d->lod_qos.lq_dirty = 1;
226 d->lod_qos.lq_rr.lqr_dirty = 1;
227 CDEBUG(D_CONFIG, "%s: turns inactive\n",
228 ost->ltd_exp->exp_obd->obd_name);
230 spin_unlock(&d->lod_lock);
231 } else if (rc == 0 && ost->ltd_active == 0) {
233 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
234 "active tgt count %d, ost nr %d\n",
235 d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
236 spin_lock(&d->lod_lock);
237 if (ost->ltd_active == 0) {
239 ost->ltd_connecting = 0;
240 d->lod_desc.ld_active_tgt_count++;
241 d->lod_qos.lq_dirty = 1;
242 d->lod_qos.lq_rr.lqr_dirty = 1;
243 CDEBUG(D_CONFIG, "%s: turns active\n",
244 ost->ltd_exp->exp_obd->obd_name);
246 spin_unlock(&d->lod_lock);
253 * Maintain per-target statfs data.
255 * The function refreshes statfs data for all the targets every N seconds.
256 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
259 * \param[in] env execution environment for this thread
260 * \param[in] lod LOD device
262 static void lod_qos_statfs_update(const struct lu_env *env,
263 struct lod_device *lod)
265 struct obd_device *obd = lod2obd(lod);
266 struct ost_pool *osts = &(lod->lod_pool_info);
269 __u64 max_age, avail;
272 max_age = cfs_time_shift_64(-2 * lod->lod_desc.ld_qos_maxage);
274 if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
275 /* statfs data are quite recent, don't need to refresh it */
278 down_write(&lod->lod_qos.lq_rw_sem);
279 if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
282 for (i = 0; i < osts->op_count; i++) {
283 idx = osts->op_array[i];
284 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
285 if (lod_statfs_and_check(env, lod, idx,
286 &OST_TGT(lod, idx)->ltd_statfs))
288 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
289 /* recalculate weigths */
290 lod->lod_qos.lq_dirty = 1;
292 obd->obd_osfs_age = cfs_time_current_64();
295 up_write(&lod->lod_qos.lq_rw_sem);
300 * Calculate per-OST and per-OSS penalties
302 * Re-calculate penalties when the configuration changes, active targets
303 * change and after statfs refresh (all these are reflected by lq_dirty flag).
304 * On every OST and OSS: decay the penalty by half for every 8x the update
305 * interval that the device has been idle. That gives lots of time for the
306 * statfs information to be updated (which the penalty is only a proxy for),
307 * and avoids penalizing OSS/OSTs under light load.
308 * See lod_qos_calc_weight() for how penalties are factored into the weight.
310 * \param[in] lod LOD device
312 * \retval 0 on success
313 * \retval -EAGAIN the number of OSTs isn't enough
315 static int lod_qos_calc_ppo(struct lod_device *lod)
317 struct lod_qos_oss *oss;
318 __u64 ba_max, ba_min, temp;
325 if (!lod->lod_qos.lq_dirty)
328 num_active = lod->lod_desc.ld_active_tgt_count - 1;
330 GOTO(out, rc = -EAGAIN);
332 /* find bavail on each OSS */
333 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
335 lod->lod_qos.lq_active_oss_count = 0;
338 * How badly user wants to select OSTs "widely" (not recently chosen
339 * and not on recent OSS's). As opposed to "freely" (free space
342 prio_wide = 256 - lod->lod_qos.lq_prio_free;
344 ba_min = (__u64)(-1);
346 now = cfs_time_current_sec();
347 /* Calculate OST penalty per object
348 * (lod ref taken in lod_qos_prep_create()) */
349 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
350 LASSERT(OST_TGT(lod,i));
351 temp = TGT_BAVAIL(i);
354 ba_min = min(temp, ba_min);
355 ba_max = max(temp, ba_max);
357 /* Count the number of usable OSS's */
358 if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
359 lod->lod_qos.lq_active_oss_count++;
360 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
362 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
364 do_div(temp, num_active);
365 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
366 (temp * prio_wide) >> 8;
368 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
369 if (lod->lod_qos.lq_reset ||
370 age > 32 * lod->lod_desc.ld_qos_maxage)
371 OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
372 else if (age > lod->lod_desc.ld_qos_maxage)
373 /* Decay OST penalty. */
374 OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
375 (age / lod->lod_desc.ld_qos_maxage);
378 num_active = lod->lod_qos.lq_active_oss_count - 1;
379 if (num_active < 1) {
380 /* If there's only 1 OSS, we can't penalize it, so instead
381 we have to double the OST penalty */
383 cfs_foreach_bit(lod->lod_ost_bitmap, i)
384 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
387 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
388 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
389 temp = oss->lqo_bavail >> 1;
390 do_div(temp, oss->lqo_ost_count * num_active);
391 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
393 age = (now - oss->lqo_used) >> 3;
394 if (lod->lod_qos.lq_reset ||
395 age > 32 * lod->lod_desc.ld_qos_maxage)
396 oss->lqo_penalty = 0;
397 else if (age > lod->lod_desc.ld_qos_maxage)
398 /* Decay OSS penalty. */
399 oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
402 lod->lod_qos.lq_dirty = 0;
403 lod->lod_qos.lq_reset = 0;
405 /* If each ost has almost same free space,
406 * do rr allocation for better creation performance */
407 lod->lod_qos.lq_same_space = 0;
408 if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
409 lod->lod_qos.lq_same_space = 1;
410 /* Reset weights for the next time we enter qos mode */
411 lod->lod_qos.lq_reset = 1;
417 if (!rc && lod->lod_qos.lq_same_space)
424 * Calculate weight for a given OST target.
426 * The final OST weight is the number of bytes available minus the OST and
427 * OSS penalties. See lod_qos_calc_ppo() for how penalties are calculated.
429 * \param[in] lod LOD device, where OST targets are listed
430 * \param[in] i OST target index
434 static int lod_qos_calc_weight(struct lod_device *lod, int i)
438 temp = TGT_BAVAIL(i);
439 temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
440 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
442 OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
444 OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
449 * Re-calculate weights.
451 * The function is called when some OST target was used for a new object. In
452 * this case we should re-calculate all the weights to keep new allocations
455 * \param[in] lod LOD device
456 * \param[in] osts OST pool where a new object was placed
457 * \param[in] index OST target where a new object was placed
458 * \param[out] total_wt new total weight for the pool
462 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
463 __u32 index, __u64 *total_wt)
465 struct lod_tgt_desc *ost;
466 struct lod_qos_oss *oss;
470 ost = OST_TGT(lod,index);
473 /* Don't allocate on this devuce anymore, until the next alloc_qos */
474 ost->ltd_qos.ltq_usable = 0;
476 oss = ost->ltd_qos.ltq_oss;
478 /* Decay old penalty by half (we're adding max penalty, and don't
479 want it to run away.) */
480 ost->ltd_qos.ltq_penalty >>= 1;
481 oss->lqo_penalty >>= 1;
483 /* mark the OSS and OST as recently used */
484 ost->ltd_qos.ltq_used = oss->lqo_used = cfs_time_current_sec();
486 /* Set max penalties for this OST and OSS */
487 ost->ltd_qos.ltq_penalty +=
488 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
489 oss->lqo_penalty += oss->lqo_penalty_per_obj *
490 lod->lod_qos.lq_active_oss_count;
492 /* Decrease all OSS penalties */
493 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
494 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
495 oss->lqo_penalty = 0;
497 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
501 /* Decrease all OST penalties */
502 for (j = 0; j < osts->op_count; j++) {
505 i = osts->op_array[j];
506 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
509 ost = OST_TGT(lod,i);
512 if (ost->ltd_qos.ltq_penalty <
513 ost->ltd_qos.ltq_penalty_per_obj)
514 ost->ltd_qos.ltq_penalty = 0;
516 ost->ltd_qos.ltq_penalty -=
517 ost->ltd_qos.ltq_penalty_per_obj;
519 lod_qos_calc_weight(lod, i);
521 /* Recalc the total weight of usable osts */
522 if (ost->ltd_qos.ltq_usable)
523 *total_wt += ost->ltd_qos.ltq_weight;
525 QOS_DEBUG("recalc tgt %d usable=%d avail=%llu"
526 " ostppo=%llu ostp=%llu ossppo=%llu"
527 " ossp=%llu wt=%llu\n",
528 i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
529 ost->ltd_qos.ltq_penalty_per_obj >> 10,
530 ost->ltd_qos.ltq_penalty >> 10,
531 ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
532 ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
533 ost->ltd_qos.ltq_weight >> 10);
539 void lod_qos_rr_init(struct lod_qos_rr *lqr)
541 spin_lock_init(&lqr->lqr_alloc);
546 #define LOV_QOS_EMPTY ((__u32)-1)
549 * Calculate optimal round-robin order with regard to OSSes.
551 * Place all the OSTs from pool \a src_pool in a special array to be used for
552 * round-robin (RR) stripe allocation. The placement algorithm interleaves
553 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
554 * Resorts the targets when the number of active targets changes (because of
555 * a new target or activation/deactivation).
557 * \param[in] lod LOD device
558 * \param[in] src_pool OST pool
559 * \param[in] lqr round-robin list
561 * \retval 0 on success
562 * \retval -ENOMEM fails to allocate the array
564 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
565 struct lod_qos_rr *lqr)
567 struct lod_qos_oss *oss;
568 struct lod_tgt_desc *ost;
569 unsigned placed, real_count;
574 if (!lqr->lqr_dirty) {
575 LASSERT(lqr->lqr_pool.op_size);
579 /* Do actual allocation. */
580 down_write(&lod->lod_qos.lq_rw_sem);
583 * Check again. While we were sleeping on @lq_rw_sem something could
586 if (!lqr->lqr_dirty) {
587 LASSERT(lqr->lqr_pool.op_size);
588 up_write(&lod->lod_qos.lq_rw_sem);
592 real_count = src_pool->op_count;
594 /* Zero the pool array */
595 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
596 deleting from the pool. The lq_rw_sem insures that nobody else
598 lqr->lqr_pool.op_count = real_count;
599 rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
601 up_write(&lod->lod_qos.lq_rw_sem);
604 for (i = 0; i < lqr->lqr_pool.op_count; i++)
605 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
607 /* Place all the OSTs from 1 OSS at the same time. */
609 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
612 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
615 if (!cfs_bitmap_check(lod->lod_ost_bitmap,
616 src_pool->op_array[i]))
619 ost = OST_TGT(lod,src_pool->op_array[i]);
620 LASSERT(ost && ost->ltd_ost);
621 if (ost->ltd_qos.ltq_oss != oss)
624 /* Evenly space these OSTs across arrayspace */
625 next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
626 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
627 next = (next + 1) % lqr->lqr_pool.op_count;
629 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
636 up_write(&lod->lod_qos.lq_rw_sem);
638 if (placed != real_count) {
639 /* This should never happen */
640 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
641 "round-robin list (%d of %d).\n",
643 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
644 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
645 lqr->lqr_pool.op_array[i]);
652 for (i = 0; i < lqr->lqr_pool.op_count; i++)
653 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
660 * Instantiate and declare creation of a new object.
662 * The function instantiates LU representation for a new object on the
663 * specified device. Also it declares an intention to create that
664 * object on the storage target.
666 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
667 * infrastructure - in the existing precreation framework we can't assign FID
668 * at this moment, we do this later once a transaction is started. So the
669 * special method instantiates FID-less object in the cache and later it
670 * will get a FID and proper placement in LU cache.
672 * \param[in] env execution environment for this thread
673 * \param[in] d LOD device
674 * \param[in] ost_idx OST target index where the object is being created
675 * \param[in] th transaction handle
677 * \retval object ptr on success, ERR_PTR() otherwise
679 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
680 struct lod_device *d,
684 struct lod_tgt_desc *ost;
685 struct lu_object *o, *n;
686 struct lu_device *nd;
687 struct dt_object *dt;
692 LASSERT(ost_idx < d->lod_osts_size);
693 ost = OST_TGT(d,ost_idx);
695 LASSERT(ost->ltd_ost);
697 nd = &ost->ltd_ost->dd_lu_dev;
700 * allocate anonymous object with zero fid, real fid
701 * will be assigned by OSP within transaction
702 * XXX: to be fixed with fully-functional OST fids
704 o = lu_object_anon(env, nd, NULL);
706 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
708 n = lu_object_locate(o->lo_header, nd->ld_type);
709 if (unlikely(n == NULL)) {
710 CERROR("can't find slice\n");
711 lu_object_put(env, o);
712 GOTO(out, dt = ERR_PTR(-EINVAL));
715 dt = container_of(n, struct dt_object, do_lu);
717 rc = lod_sub_object_declare_create(env, dt, NULL, NULL, NULL, th);
719 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
721 lu_object_put(env, o);
730 * Calculate a minimum acceptable stripe count.
732 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
733 * all stripes or 3/4 of stripes.
735 * \param[in] stripe_cnt number of stripes requested
736 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
738 * \retval acceptable stripecount
740 static int min_stripe_count(__u32 stripe_cnt, int flags)
742 return (flags & LOV_USES_DEFAULT_STRIPE ?
743 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
746 #define LOV_CREATE_RESEED_MULT 30
747 #define LOV_CREATE_RESEED_MIN 2000
750 * Initialize temporary OST-in-use array.
752 * Allocate or extend the array used to mark targets already assigned to a new
753 * striping so they are not used more than once.
755 * \param[in] env execution environment for this thread
756 * \param[in] stripes number of items needed in the array
758 * \retval 0 on success
759 * \retval -ENOMEM on error
761 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
764 struct lod_thread_info *info = lod_env_info(env);
766 if (info->lti_ea_store_size < sizeof(int) * stripes)
767 lod_ea_store_resize(info, stripes * sizeof(int));
768 if (info->lti_ea_store_size < sizeof(int) * stripes) {
769 CERROR("can't allocate memory for ost-in-use array\n");
772 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
777 * Remember a target in the array of used targets.
779 * Mark the given target as used for a new striping being created. The status
780 * of an OST in a striping can be checked with lod_qos_is_ost_used().
782 * \param[in] env execution environment for this thread
783 * \param[in] idx index in the array
784 * \param[in] ost OST target index to mark as used
786 static inline void lod_qos_ost_in_use(const struct lu_env *env,
789 struct lod_thread_info *info = lod_env_info(env);
790 int *osts = info->lti_ea_store;
792 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
797 * Check is OST used in a striping.
799 * Checks whether OST with the given index is marked as used in the temporary
800 * array (see lod_qos_ost_in_use()).
802 * \param[in] env execution environment for this thread
803 * \param[in] ost OST target index to check
804 * \param[in] stripes the number of items used in the array already
809 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
811 struct lod_thread_info *info = lod_env_info(env);
812 int *osts = info->lti_ea_store;
815 for (j = 0; j < stripes; j++) {
822 static int lod_check_and_reserve_ost(const struct lu_env *env,
823 struct lod_device *m,
824 struct obd_statfs *sfs, __u32 ost_idx,
825 __u32 speed, __u32 *s_idx,
826 struct dt_object **stripe,
830 __u32 stripe_idx = *s_idx;
833 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
835 /* this OSP doesn't feel well */
840 * We expect number of precreated objects in f_ffree at
841 * the first iteration, skip OSPs with no objects ready
843 if (sfs->os_fprecreated == 0 && speed == 0) {
844 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
849 * try to use another OSP if this one is degraded
851 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
852 QOS_DEBUG("#%d: degraded\n", ost_idx);
857 * do not put >1 objects on a single OST
859 if (lod_qos_is_ost_used(env, ost_idx, stripe_idx))
862 o = lod_qos_declare_object_on(env, m, ost_idx, th);
864 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
865 ost_idx, (int) PTR_ERR(o));
871 * We've successfully declared (reserved) an object
873 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
874 stripe[stripe_idx] = o;
875 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_LOV_CREATE_RACE, 2);
884 * Allocate a striping using round-robin algorithm.
886 * Allocates a new striping using round-robin algorithm. The function refreshes
887 * all the internal structures (statfs cache, array of available OSTs sorted
888 * with regard to OSS, etc). The number of stripes required is taken from the
889 * object (must be prepared by the caller), but can change if the flag
890 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
891 * is trying to create a striping on the object in parallel. All the internal
892 * structures (like pools, etc) are protected and no additional locking is
893 * required. The function succeeds even if a single stripe is allocated. To save
894 * time we give priority to targets which already have objects precreated.
895 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
897 * \param[in] env execution environment for this thread
898 * \param[in] lo LOD object
899 * \param[out] stripe striping created
900 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
901 * \param[in] th transaction handle
903 * \retval 0 on success
904 * \retval -ENOSPC if not enough OSTs are found
905 * \retval negative negated errno for other failures
907 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
908 struct dt_object **stripe, int flags,
911 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
912 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
913 struct pool_desc *pool = NULL;
914 struct ost_pool *osts;
915 struct lod_qos_rr *lqr;
916 unsigned int i, array_idx;
918 __u32 ost_start_idx_temp;
920 int ost_connecting = 0;
921 __u32 stripe_idx = 0;
922 __u32 stripe_cnt = lo->ldo_stripenr;
923 __u32 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
928 pool = lod_find_pool(m, lo->ldo_pool);
931 down_read(&pool_tgt_rw_sem(pool));
932 osts = &(pool->pool_obds);
933 lqr = &(pool->pool_rr);
935 osts = &(m->lod_pool_info);
936 lqr = &(m->lod_qos.lq_rr);
939 rc = lod_qos_calc_rr(m, osts, lqr);
943 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
947 down_read(&m->lod_qos.lq_rw_sem);
948 spin_lock(&lqr->lqr_alloc);
949 if (--lqr->lqr_start_count <= 0) {
950 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
951 lqr->lqr_start_count =
952 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
953 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
954 } else if (stripe_cnt_min >= osts->op_count ||
955 lqr->lqr_start_idx > osts->op_count) {
956 /* If we have allocated from all of the OSTs, slowly
957 * precess the next start if the OST/stripe count isn't
958 * already doing this for us. */
959 lqr->lqr_start_idx %= osts->op_count;
960 if (stripe_cnt > 1 && (osts->op_count % stripe_cnt) != 1)
961 ++lqr->lqr_offset_idx;
963 ost_start_idx_temp = lqr->lqr_start_idx;
967 QOS_DEBUG("pool '%s' want %d startidx %d startcnt %d offset %d "
968 "active %d count %d\n",
969 lo->ldo_pool ? lo->ldo_pool : "",
970 stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
971 lqr->lqr_offset_idx, osts->op_count, osts->op_count);
973 for (i = 0; i < osts->op_count && stripe_idx < lo->ldo_stripenr; i++) {
974 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
976 ++lqr->lqr_start_idx;
977 ost_idx = lqr->lqr_pool.op_array[array_idx];
979 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
980 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
981 stripe_idx, array_idx, ost_idx);
983 if ((ost_idx == LOV_QOS_EMPTY) ||
984 !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
987 /* Fail Check before osc_precreate() is called
988 so we can only 'fail' single OSC. */
989 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
992 spin_unlock(&lqr->lqr_alloc);
993 rc = lod_check_and_reserve_ost(env, m, sfs, ost_idx, speed,
994 &stripe_idx, stripe, th);
995 spin_lock(&lqr->lqr_alloc);
997 if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
1000 if ((speed < 2) && (stripe_idx < stripe_cnt_min)) {
1001 /* Try again, allowing slower OSCs */
1003 lqr->lqr_start_idx = ost_start_idx_temp;
1009 spin_unlock(&lqr->lqr_alloc);
1010 up_read(&m->lod_qos.lq_rw_sem);
1013 lo->ldo_stripenr = stripe_idx;
1014 /* at least one stripe is allocated */
1017 /* nobody provided us with a single object */
1026 up_read(&pool_tgt_rw_sem(pool));
1027 /* put back ref got by lod_find_pool() */
1028 lod_pool_putref(pool);
1035 * Allocate a specific striping layout on a user defined set of OSTs.
1037 * Allocates new striping using the OST index range provided by the data from
1038 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1039 * OSTs are not considered. The exact order of OSTs requested by the user
1040 * is respected as much as possible depending on OST status. The number of
1041 * stripes needed and stripe offset are taken from the object. If that number
1042 * can not be met, then the function returns a failure and then it's the
1043 * caller's responsibility to release the stripes allocated. All the internal
1044 * structures are protected, but no concurrent allocation is allowed on the
1047 * \param[in] env execution environment for this thread
1048 * \param[in] lo LOD object
1049 * \param[out] stripe striping created
1050 * \param[in] lum stripe md to specify list of OSTs
1051 * \param[in] th transaction handle
1053 * \retval 0 on success
1054 * \retval -ENODEV OST index does not exist on file system
1055 * \retval -EINVAL requested OST index is invalid
1056 * \retval negative negated errno on error
1058 static int lod_alloc_ost_list(const struct lu_env *env,
1059 struct lod_object *lo, struct dt_object **stripe,
1060 struct lov_user_md *lum, struct thandle *th)
1062 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1063 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1064 struct dt_object *o;
1065 struct lov_user_md_v3 *v3;
1066 unsigned int array_idx = 0;
1067 int stripe_count = 0;
1072 /* for specific OSTs layout */
1073 LASSERT(lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC);
1074 lustre_print_user_md(D_OTHER, lum, __func__);
1076 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1080 v3 = (struct lov_user_md_v3 *)lum;
1081 for (i = 0; i < lo->ldo_stripenr; i++) {
1082 if (v3->lmm_objects[i].l_ost_idx == lo->ldo_stripe_offset) {
1087 if (i == lo->ldo_stripenr) {
1089 "%s: start index %d not in the specified list of OSTs\n",
1090 lod2obd(m)->obd_name, lo->ldo_stripe_offset);
1094 for (i = 0; i < lo->ldo_stripenr;
1095 i++, array_idx = (array_idx + 1) % lo->ldo_stripenr) {
1096 __u32 ost_idx = v3->lmm_objects[array_idx].l_ost_idx;
1098 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1104 * do not put >1 objects on a single OST
1106 if (lod_qos_is_ost_used(env, ost_idx, stripe_count)) {
1111 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1112 if (rc < 0) /* this OSP doesn't feel well */
1115 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1119 "%s: can't declare new object on #%u: %d\n",
1120 lod2obd(m)->obd_name, ost_idx, rc);
1125 * We've successfully declared (reserved) an object
1127 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1128 stripe[stripe_count] = o;
1136 * Allocate a striping on a predefined set of OSTs.
1138 * Allocates new layout starting from OST index in lo->ldo_stripe_offset.
1139 * Full OSTs are not considered. The exact order of OSTs is not important and
1140 * varies depending on OST status. The allocation procedure prefers the targets
1141 * with precreated objects ready. The number of stripes needed and stripe
1142 * offset are taken from the object. If that number cannot be met, then the
1143 * function returns an error and then it's the caller's responsibility to
1144 * release the stripes allocated. All the internal structures are protected,
1145 * but no concurrent allocation is allowed on the same objects.
1147 * \param[in] env execution environment for this thread
1148 * \param[in] lo LOD object
1149 * \param[out] stripe striping created
1150 * \param[in] flags not used
1151 * \param[in] th transaction handle
1153 * \retval 0 on success
1154 * \retval -ENOSPC if no OST objects are available at all
1155 * \retval -EFBIG if not enough OST objects are found
1156 * \retval -EINVAL requested offset is invalid
1157 * \retval negative errno on failure
1159 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1160 struct dt_object **stripe, int flags,
1163 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1164 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1165 struct dt_object *o;
1167 unsigned int i, array_idx, ost_count;
1168 int rc, stripe_num = 0;
1170 struct pool_desc *pool = NULL;
1171 struct ost_pool *osts;
1174 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1179 pool = lod_find_pool(m, lo->ldo_pool);
1182 down_read(&pool_tgt_rw_sem(pool));
1183 osts = &(pool->pool_obds);
1185 osts = &(m->lod_pool_info);
1188 ost_count = osts->op_count;
1191 /* search loi_ost_idx in ost array */
1193 for (i = 0; i < ost_count; i++) {
1194 if (osts->op_array[i] == lo->ldo_stripe_offset) {
1199 if (i == ost_count) {
1200 CERROR("Start index %d not found in pool '%s'\n",
1201 lo->ldo_stripe_offset, lo->ldo_pool ?: "");
1202 GOTO(out, rc = -EINVAL);
1205 for (i = 0; i < ost_count;
1206 i++, array_idx = (array_idx + 1) % ost_count) {
1207 ost_idx = osts->op_array[array_idx];
1209 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1212 /* Fail Check before osc_precreate() is called
1213 so we can only 'fail' single OSC. */
1214 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1218 * do not put >1 objects on a single OST
1220 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
1223 /* Drop slow OSCs if we can, but not for requested start idx.
1225 * This means "if OSC is slow and it is not the requested
1226 * start OST, then it can be skipped, otherwise skip it only
1227 * if it is inactive/recovering/out-of-space." */
1229 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1231 /* this OSP doesn't feel well */
1236 * We expect number of precreated objects in f_ffree at
1237 * the first iteration, skip OSPs with no objects ready
1238 * don't apply this logic to OST specified with stripe_offset
1240 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1243 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1245 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1246 ost_idx, (int) PTR_ERR(o));
1251 * We've successfully declared (reserved) an object
1253 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1254 stripe[stripe_num] = o;
1257 /* We have enough stripes */
1258 if (stripe_num == lo->ldo_stripenr)
1262 /* Try again, allowing slower OSCs */
1267 /* If we were passed specific striping params, then a failure to
1268 * meet those requirements is an error, since we can't reallocate
1269 * that memory (it might be part of a larger array or something).
1271 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1272 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1274 rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
1277 up_read(&pool_tgt_rw_sem(pool));
1278 /* put back ref got by lod_find_pool() */
1279 lod_pool_putref(pool);
1286 * Check whether QoS allocation should be used.
1288 * A simple helper to decide when QoS allocation should be used:
1289 * if it's just a single available target or the used space is
1290 * evenly distributed among the targets at the moment, then QoS
1291 * allocation algorithm should not be used.
1293 * \param[in] lod LOD device
1295 * \retval 0 should not be used
1296 * \retval 1 should be used
1298 static inline int lod_qos_is_usable(struct lod_device *lod)
1301 /* to be able to debug QoS code */
1305 /* Detect -EAGAIN early, before expensive lock is taken. */
1306 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1309 if (lod->lod_desc.ld_active_tgt_count < 2)
1316 * Allocate a striping using an algorithm with weights.
1318 * The function allocates OST objects to create a striping. The algorithm
1319 * used is based on weights (currently only using the free space), and it's
1320 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1321 * configuration (# of stripes, offset, pool) is taken from the object and
1322 * is prepared by the caller.
1324 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1325 * be met due to too few OSTs, then allocation fails. If the flag is passed
1326 * fewer than 3/4 of the requested number of stripes can be allocated, then
1329 * No concurrent allocation is allowed on the object and this must be ensured
1330 * by the caller. All the internal structures are protected by the function.
1332 * The algorithm has two steps: find available OSTs and calculate their
1333 * weights, then select the OSTs with their weights used as the probability.
1334 * An OST with a higher weight is proportionately more likely to be selected
1335 * than one with a lower weight.
1337 * \param[in] env execution environment for this thread
1338 * \param[in] lo LOD object
1339 * \param[out] stripe striping created
1340 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1341 * \param[in] th transaction handle
1343 * \retval 0 on success
1344 * \retval -EAGAIN not enough OSTs are found for specified stripe count
1345 * \retval -EINVAL requested OST index is invalid
1346 * \retval negative errno on failure
1348 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1349 struct dt_object **stripe, int flags,
1352 struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1353 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1354 struct lod_tgt_desc *ost;
1355 struct dt_object *o;
1356 __u64 total_weight = 0;
1357 __u32 nfound, good_osts;
1358 __u32 stripe_cnt = lo->ldo_stripenr;
1359 __u32 stripe_cnt_min;
1360 struct pool_desc *pool = NULL;
1361 struct ost_pool *osts;
1366 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
1367 if (stripe_cnt_min < 1)
1371 pool = lod_find_pool(lod, lo->ldo_pool);
1374 down_read(&pool_tgt_rw_sem(pool));
1375 osts = &(pool->pool_obds);
1377 osts = &(lod->lod_pool_info);
1380 /* Detect -EAGAIN early, before expensive lock is taken. */
1381 if (!lod_qos_is_usable(lod))
1382 GOTO(out_nolock, rc = -EAGAIN);
1384 /* Do actual allocation, use write lock here. */
1385 down_write(&lod->lod_qos.lq_rw_sem);
1388 * Check again, while we were sleeping on @lq_rw_sem things could
1391 if (!lod_qos_is_usable(lod))
1392 GOTO(out, rc = -EAGAIN);
1394 rc = lod_qos_calc_ppo(lod);
1398 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1403 /* Find all the OSTs that are valid stripe candidates */
1404 for (i = 0; i < osts->op_count; i++) {
1405 if (!cfs_bitmap_check(lod->lod_ost_bitmap, osts->op_array[i]))
1408 ost = OST_TGT(lod, osts->op_array[i]);
1409 ost->ltd_qos.ltq_usable = 0;
1411 rc = lod_statfs_and_check(env, lod, osts->op_array[i], sfs);
1413 /* this OSP doesn't feel well */
1417 if (sfs->os_state & OS_STATE_DEGRADED)
1420 /* Fail Check before osc_precreate() is called
1421 so we can only 'fail' single OSC. */
1422 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1423 osts->op_array[i] == 0)
1426 ost->ltd_qos.ltq_usable = 1;
1427 lod_qos_calc_weight(lod, osts->op_array[i]);
1428 total_weight += ost->ltd_qos.ltq_weight;
1433 QOS_DEBUG("found %d good osts\n", good_osts);
1435 if (good_osts < stripe_cnt_min)
1436 GOTO(out, rc = -EAGAIN);
1438 /* We have enough osts */
1439 if (good_osts < stripe_cnt)
1440 stripe_cnt = good_osts;
1442 /* Find enough OSTs with weighted random allocation. */
1444 while (nfound < stripe_cnt) {
1445 __u64 rand, cur_weight;
1451 #if BITS_PER_LONG == 32
1452 rand = cfs_rand() % (unsigned)total_weight;
1453 /* If total_weight > 32-bit, first generate the high
1454 * 32 bits of the random number, then add in the low
1455 * 32 bits (truncated to the upper limit, if needed) */
1456 if (total_weight > 0xffffffffULL)
1457 rand = (__u64)(cfs_rand() %
1458 (unsigned)(total_weight >> 32)) << 32;
1462 if (rand == (total_weight & 0xffffffff00000000ULL))
1463 rand |= cfs_rand() % (unsigned)total_weight;
1468 rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1475 /* On average, this will hit larger-weighted OSTs more often.
1476 * 0-weight OSTs will always get used last (only when rand=0) */
1477 for (i = 0; i < osts->op_count; i++) {
1478 __u32 idx = osts->op_array[i];
1480 if (!cfs_bitmap_check(lod->lod_ost_bitmap, idx))
1483 ost = OST_TGT(lod, idx);
1485 if (!ost->ltd_qos.ltq_usable)
1488 cur_weight += ost->ltd_qos.ltq_weight;
1489 QOS_DEBUG("stripe_cnt=%d nfound=%d cur_weight=%llu"
1490 " rand=%llu total_weight=%llu\n",
1491 stripe_cnt, nfound, cur_weight, rand,
1494 if (cur_weight < rand)
1497 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1500 * do not put >1 objects on a single OST
1502 if (lod_qos_is_ost_used(env, idx, nfound))
1504 lod_qos_ost_in_use(env, nfound, idx);
1506 o = lod_qos_declare_object_on(env, lod, idx, th);
1508 QOS_DEBUG("can't declare object on #%u: %d\n",
1509 idx, (int) PTR_ERR(o));
1512 stripe[nfound++] = o;
1513 lod_qos_used(lod, osts, idx, &total_weight);
1519 /* no OST found on this iteration, give up */
1524 if (unlikely(nfound != stripe_cnt)) {
1526 * when the decision to use weighted algorithm was made
1527 * we had enough appropriate OSPs, but this state can
1528 * change anytime (no space on OST, broken connection, etc)
1529 * so it's possible OSP won't be able to provide us with
1530 * an object due to just changed state
1532 QOS_DEBUG("%s: wanted %d objects, found only %d\n",
1533 lod2obd(lod)->obd_name, stripe_cnt, nfound);
1534 for (i = 0; i < nfound; i++) {
1535 LASSERT(stripe[i] != NULL);
1536 lu_object_put(env, &stripe[i]->do_lu);
1540 /* makes sense to rebalance next time */
1541 lod->lod_qos.lq_dirty = 1;
1542 lod->lod_qos.lq_same_space = 0;
1548 up_write(&lod->lod_qos.lq_rw_sem);
1552 up_read(&pool_tgt_rw_sem(pool));
1553 /* put back ref got by lod_find_pool() */
1554 lod_pool_putref(pool);
1561 * Find largest stripe count the caller can use.
1563 * Find the maximal possible stripe count not greater than \a stripe_count.
1564 * Sometimes suggested stripecount can't be reached for a number of reasons:
1565 * lack of enough active OSTs or the backend does not support EAs that large.
1566 * If the passed one is 0, then the filesystem's default one is used.
1568 * \param[in] lod LOD device
1569 * \param[in] magic the format if striping
1570 * \param[in] stripe_count count the caller would like to use
1572 * \retval the maximum usable stripe count
1574 static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
1577 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1580 stripe_count = lod->lod_desc.ld_default_stripe_count;
1581 if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1582 stripe_count = lod->lod_desc.ld_active_tgt_count;
1586 /* stripe count is based on whether OSD can handle larger EA sizes */
1587 if (lod->lod_osd_max_easize > 0)
1588 max_stripes = lov_mds_md_max_stripe_count(
1589 lod->lod_osd_max_easize, magic);
1591 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1595 * Create in-core respresentation for a fully-defined striping
1597 * When the caller passes a fully-defined striping (i.e. everything including
1598 * OST object FIDs are defined), then we still need to instantiate LU-cache
1599 * with the objects representing the stripes defined. This function completes
1602 * \param[in] env execution environment for this thread
1603 * \param[in] mo LOD object
1604 * \param[in] buf buffer containing the striping
1606 * \retval 0 on success
1607 * \retval negative negated errno on error
1609 static int lod_use_defined_striping(const struct lu_env *env,
1610 struct lod_object *mo,
1611 const struct lu_buf *buf)
1613 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1614 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1615 struct lov_ost_data_v1 *objs;
1620 magic = le32_to_cpu(v1->lmm_magic);
1621 if (magic == LOV_MAGIC_V1_DEF) {
1622 magic = LOV_MAGIC_V1;
1623 objs = &v1->lmm_objects[0];
1624 } else if (magic == LOV_MAGIC_V3_DEF) {
1625 magic = LOV_MAGIC_V3;
1626 objs = &v3->lmm_objects[0];
1627 lod_object_set_pool(mo, v3->lmm_pool_name);
1629 GOTO(out, rc = -EINVAL);
1632 mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
1633 mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1634 mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
1635 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1637 /* fixup for released file before object initialization */
1638 if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1639 mo->ldo_released_stripenr = mo->ldo_stripenr;
1640 mo->ldo_stripenr = 0;
1643 LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
1645 if (mo->ldo_stripenr > 0)
1646 rc = lod_initialize_objects(env, mo, objs);
1653 * Parse suggested striping configuration.
1655 * The caller gets a suggested striping configuration from a number of sources
1656 * including per-directory default and applications. Then it needs to verify
1657 * the suggested striping is valid, apply missing bits and store the resulting
1658 * configuration in the object to be used by the allocator later. Must not be
1659 * called concurrently against the same object. It's OK to provide a
1660 * fully-defined striping.
1662 * \param[in] env execution environment for this thread
1663 * \param[in] lo LOD object
1664 * \param[in] buf buffer containing the striping
1666 * \retval 0 on success
1667 * \retval negative negated errno on error
1669 static int lod_qos_parse_config(const struct lu_env *env,
1670 struct lod_object *lo,
1671 const struct lu_buf *buf)
1673 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1674 struct lov_user_md_v1 *v1 = NULL;
1675 struct lov_user_md_v3 *v3 = NULL;
1676 char *pool_name = NULL;
1682 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1687 magic = v1->lmm_magic;
1689 if (unlikely(magic == LOV_MAGIC_V1_DEF || magic == LOV_MAGIC_V3_DEF)) {
1690 /* try to use as fully defined striping */
1691 rc = lod_use_defined_striping(env, lo, buf);
1696 case __swab32(LOV_USER_MAGIC_V1):
1697 lustre_swab_lov_user_md_v1(v1);
1698 magic = v1->lmm_magic;
1700 case LOV_USER_MAGIC_V1:
1704 case __swab32(LOV_USER_MAGIC_V3):
1705 lustre_swab_lov_user_md_v3(v3);
1706 magic = v3->lmm_magic;
1708 case LOV_USER_MAGIC_V3:
1710 pool_name = v3->lmm_pool_name;
1713 case __swab32(LOV_USER_MAGIC_SPECIFIC):
1714 lustre_swab_lov_user_md_v3(v3);
1715 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1716 v3->lmm_stripe_count);
1717 magic = v3->lmm_magic;
1719 case LOV_USER_MAGIC_SPECIFIC:
1720 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
1721 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
1722 if (v3->lmm_pool_name[0] != '\0')
1723 pool_name = v3->lmm_pool_name;
1724 size = lov_user_md_size(v3->lmm_stripe_count,
1725 LOV_USER_MAGIC_SPECIFIC);
1729 CERROR("%s: unrecognized magic %X\n",
1730 lod2obd(d)->obd_name, magic);
1734 if (unlikely(buf->lb_len < size)) {
1735 CERROR("%s: wrong size: %zd, expect: %u\n",
1736 lod2obd(d)->obd_name, buf->lb_len, size);
1740 lustre_print_user_md(D_OTHER, v1, "parse config");
1742 v1->lmm_magic = magic;
1743 if (v1->lmm_pattern == 0)
1744 v1->lmm_pattern = LOV_PATTERN_RAID0;
1745 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
1746 CERROR("%s: invalid pattern: %x\n",
1747 lod2obd(d)->obd_name, v1->lmm_pattern);
1750 lo->ldo_pattern = v1->lmm_pattern;
1752 if (v1->lmm_stripe_size > 0)
1753 lo->ldo_stripe_size = v1->lmm_stripe_size;
1755 if (lo->ldo_stripe_size & (LOV_MIN_STRIPE_SIZE - 1))
1756 lo->ldo_stripe_size = LOV_MIN_STRIPE_SIZE;
1758 if (v1->lmm_stripe_count > 0)
1759 lo->ldo_stripenr = v1->lmm_stripe_count;
1761 lo->ldo_stripe_offset = v1->lmm_stripe_offset;
1763 lod_object_set_pool(lo, NULL);
1764 if (pool_name != NULL) {
1765 struct pool_desc *pool;
1767 /* In the function below, .hs_keycmp resolves to
1768 * pool_hashkey_keycmp() */
1769 /* coverity[overrun-buffer-val] */
1770 pool = lod_find_pool(d, pool_name);
1772 if (lo->ldo_stripe_offset != LOV_OFFSET_DEFAULT) {
1773 rc = lod_check_index_in_pool(
1774 lo->ldo_stripe_offset, pool);
1776 lod_pool_putref(pool);
1777 CERROR("%s: invalid offset, %u\n",
1778 lod2obd(d)->obd_name,
1779 lo->ldo_stripe_offset);
1784 if (lo->ldo_stripenr > pool_tgt_count(pool))
1785 lo->ldo_stripenr = pool_tgt_count(pool);
1787 lod_pool_putref(pool);
1790 lod_object_set_pool(lo, pool_name);
1793 /* fixup for released file */
1794 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1795 lo->ldo_released_stripenr = lo->ldo_stripenr;
1796 lo->ldo_stripenr = 0;
1803 * Create a striping for an obejct.
1805 * The function creates a new striping for the object. A buffer containing
1806 * configuration hints can be provided optionally. The function tries QoS
1807 * algorithm first unless free space is distributed evenly among OSTs, but
1808 * by default RR algorithm is preferred due to internal concurrency (QoS is
1809 * serialized). The caller must ensure no concurrent calls to the function
1810 * are made against the same object.
1812 * \param[in] env execution environment for this thread
1813 * \param[in] lo LOD object
1814 * \param[in] attr attributes OST objects will be declared with
1815 * \param[in] buf suggested striping configuration or NULL
1816 * \param[in] th transaction handle
1818 * \retval 0 on success
1819 * \retval negative negated errno on error
1821 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
1822 struct lu_attr *attr, const struct lu_buf *buf,
1825 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1826 struct dt_object **stripe;
1828 int flag = LOV_USES_ASSIGNED_STRIPE;
1834 /* no OST available */
1835 /* XXX: should we be waiting a bit to prevent failures during
1836 * cluster initialization? */
1837 if (d->lod_ostnr == 0)
1838 GOTO(out, rc = -EIO);
1841 * by this time, the object's ldo_stripenr and ldo_stripe_size
1842 * contain default value for striping: taken from the parent
1843 * or from filesystem defaults
1845 * in case the caller is passing lovea with new striping config,
1846 * we may need to parse lovea and apply new configuration
1848 rc = lod_qos_parse_config(env, lo, buf);
1852 /* A released file is being created */
1853 if (lo->ldo_stripenr == 0)
1856 if (likely(lo->ldo_stripe == NULL)) {
1857 struct lov_user_md *lum = NULL;
1860 * no striping has been created so far
1862 LASSERT(lo->ldo_stripenr > 0);
1864 * statfs and check OST targets now, since ld_active_tgt_count
1865 * could be changed if some OSTs are [de]activated manually.
1867 lod_qos_statfs_update(env, d);
1868 lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
1871 stripe_len = lo->ldo_stripenr;
1872 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
1874 GOTO(out, rc = -ENOMEM);
1876 lod_getref(&d->lod_ost_descs);
1877 /* XXX: support for non-0 files w/o objects */
1878 CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
1879 d->lod_desc.ld_tgt_count, stripe_len);
1881 if (buf != NULL && buf->lb_buf != NULL)
1884 if (lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
1885 rc = lod_alloc_ost_list(env, lo, stripe, lum, th);
1886 } else if (lo->ldo_stripe_offset == LOV_OFFSET_DEFAULT) {
1887 rc = lod_alloc_qos(env, lo, stripe, flag, th);
1889 rc = lod_alloc_rr(env, lo, stripe, flag, th);
1891 rc = lod_alloc_specific(env, lo, stripe, flag, th);
1893 lod_putref(d, &d->lod_ost_descs);
1896 for (i = 0; i < stripe_len; i++)
1897 if (stripe[i] != NULL)
1898 lu_object_put(env, &stripe[i]->do_lu);
1900 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
1901 lo->ldo_stripenr = 0;
1903 lo->ldo_stripe = stripe;
1904 lo->ldo_stripes_allocated = stripe_len;
1908 * lod_qos_parse_config() found supplied buf as a predefined
1909 * striping (not a hint), so it allocated all the object
1910 * now we need to create them
1912 for (i = 0; i < lo->ldo_stripenr; i++) {
1913 struct dt_object *o;
1915 o = lo->ldo_stripe[i];
1918 rc = lod_sub_object_declare_create(env, o, attr, NULL,
1921 CERROR("can't declare create: %d\n", rc);