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 #define LOV_QOS_EMPTY ((__u32)-1)
531 * Calculate optimal round-robin order with regard to OSSes.
533 * Place all the OSTs from pool \a src_pool in a special array to be used for
534 * round-robin (RR) stripe allocation. The placement algorithm interleaves
535 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
536 * Resorts the targets when the number of active targets changes (because of
537 * a new target or activation/deactivation).
539 * \param[in] lod LOD device
540 * \param[in] src_pool OST pool
541 * \param[in] lqr round-robin list
543 * \retval 0 on success
544 * \retval -ENOMEM fails to allocate the array
546 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
547 struct lod_qos_rr *lqr)
549 struct lod_qos_oss *oss;
550 struct lod_tgt_desc *ost;
551 unsigned placed, real_count;
556 if (!lqr->lqr_dirty) {
557 LASSERT(lqr->lqr_pool.op_size);
561 /* Do actual allocation. */
562 down_write(&lod->lod_qos.lq_rw_sem);
565 * Check again. While we were sleeping on @lq_rw_sem something could
568 if (!lqr->lqr_dirty) {
569 LASSERT(lqr->lqr_pool.op_size);
570 up_write(&lod->lod_qos.lq_rw_sem);
574 real_count = src_pool->op_count;
576 /* Zero the pool array */
577 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
578 deleting from the pool. The lq_rw_sem insures that nobody else
580 lqr->lqr_pool.op_count = real_count;
581 rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
583 up_write(&lod->lod_qos.lq_rw_sem);
586 for (i = 0; i < lqr->lqr_pool.op_count; i++)
587 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
589 /* Place all the OSTs from 1 OSS at the same time. */
591 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
594 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
597 if (!cfs_bitmap_check(lod->lod_ost_bitmap,
598 src_pool->op_array[i]))
601 ost = OST_TGT(lod,src_pool->op_array[i]);
602 LASSERT(ost && ost->ltd_ost);
603 if (ost->ltd_qos.ltq_oss != oss)
606 /* Evenly space these OSTs across arrayspace */
607 next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
608 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
609 next = (next + 1) % lqr->lqr_pool.op_count;
611 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
618 up_write(&lod->lod_qos.lq_rw_sem);
620 if (placed != real_count) {
621 /* This should never happen */
622 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
623 "round-robin list (%d of %d).\n",
625 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
626 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
627 lqr->lqr_pool.op_array[i]);
634 for (i = 0; i < lqr->lqr_pool.op_count; i++)
635 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
642 * Instantiate and declare creation of a new object.
644 * The function instantiates LU representation for a new object on the
645 * specified device. Also it declares an intention to create that
646 * object on the storage target.
648 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
649 * infrastructure - in the existing precreation framework we can't assign FID
650 * at this moment, we do this later once a transaction is started. So the
651 * special method instantiates FID-less object in the cache and later it
652 * will get a FID and proper placement in LU cache.
654 * \param[in] env execution environment for this thread
655 * \param[in] d LOD device
656 * \param[in] ost_idx OST target index where the object is being created
657 * \param[in] th transaction handle
659 * \retval object ptr on success, ERR_PTR() otherwise
661 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
662 struct lod_device *d,
666 struct lod_tgt_desc *ost;
667 struct lu_object *o, *n;
668 struct lu_device *nd;
669 struct dt_object *dt;
674 LASSERT(ost_idx < d->lod_osts_size);
675 ost = OST_TGT(d,ost_idx);
677 LASSERT(ost->ltd_ost);
679 nd = &ost->ltd_ost->dd_lu_dev;
682 * allocate anonymous object with zero fid, real fid
683 * will be assigned by OSP within transaction
684 * XXX: to be fixed with fully-functional OST fids
686 o = lu_object_anon(env, nd, NULL);
688 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
690 n = lu_object_locate(o->lo_header, nd->ld_type);
691 if (unlikely(n == NULL)) {
692 CERROR("can't find slice\n");
693 lu_object_put(env, o);
694 GOTO(out, dt = ERR_PTR(-EINVAL));
697 dt = container_of(n, struct dt_object, do_lu);
699 rc = dt_declare_create(env, dt, NULL, NULL, NULL, th);
701 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
703 lu_object_put(env, o);
712 * Calculate a minimum acceptable stripe count.
714 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
715 * all stripes or 3/4 of stripes.
717 * \param[in] stripe_cnt number of stripes requested
718 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
720 * \retval acceptable stripecount
722 static int min_stripe_count(__u32 stripe_cnt, int flags)
724 return (flags & LOV_USES_DEFAULT_STRIPE ?
725 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
728 #define LOV_CREATE_RESEED_MULT 30
729 #define LOV_CREATE_RESEED_MIN 2000
732 * Check if an OST is full.
734 * Check whether an OST should be considered full based
735 * on the given statfs data.
737 * \param[in] msfs statfs data
739 * \retval false not full
742 static int inline lod_qos_dev_is_full(struct obd_statfs *msfs)
745 int bs = msfs->os_bsize;
747 LASSERT(((bs - 1) & bs) == 0);
749 /* the minimum of 0.1% used blocks and 1GB bytes. */
750 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
751 1 << (31 - ffs(bs)));
752 return (msfs->os_bavail < used);
756 * Initialize temporary OST-in-use array.
758 * Allocate or extend the array used to mark targets already assigned to a new
759 * striping so they are not used more than once.
761 * \param[in] env execution environment for this thread
762 * \param[in] stripes number of items needed in the array
764 * \retval 0 on success
765 * \retval -ENOMEM on error
767 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
770 struct lod_thread_info *info = lod_env_info(env);
772 if (info->lti_ea_store_size < sizeof(int) * stripes)
773 lod_ea_store_resize(info, stripes * sizeof(int));
774 if (info->lti_ea_store_size < sizeof(int) * stripes) {
775 CERROR("can't allocate memory for ost-in-use array\n");
778 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
783 * Remember a target in the array of used targets.
785 * Mark the given target as used for a new striping being created. The status
786 * of an OST in a striping can be checked with lod_qos_is_ost_used().
788 * \param[in] env execution environment for this thread
789 * \param[in] idx index in the array
790 * \param[in] ost OST target index to mark as used
792 static inline void lod_qos_ost_in_use(const struct lu_env *env,
795 struct lod_thread_info *info = lod_env_info(env);
796 int *osts = info->lti_ea_store;
798 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
803 * Check is OST used in a striping.
805 * Checks whether OST with the given index is marked as used in the temporary
806 * array (see lod_qos_ost_in_use()).
808 * \param[in] env execution environment for this thread
809 * \param[in] ost OST target index to check
810 * \param[in] stripes the number of items used in the array already
815 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
817 struct lod_thread_info *info = lod_env_info(env);
818 int *osts = info->lti_ea_store;
821 for (j = 0; j < stripes; j++) {
829 * Allocate a striping using round-robin algorigthm.
831 * Allocates a new striping using round-robin algorithm. The function refreshes
832 * all the internal structures (statfs cache, array of available OSTs sorted
833 * with regard to OSS, etc). The number of stripes required is taken from the
834 * object (must be prepared by the caller), but can change if the flag
835 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
836 * is trying to create a striping on the object in parallel. All the internal
837 * structures (like pools, etc) are protected and no additional locking is
838 * required. The function succeeds even if a single stripe is allocated. To save
839 * time we give priority to targets which already have objects precreated.
840 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
842 * \param[in] env execution environment for this thread
843 * \param[in] lo LOD object
844 * \param[out] stripe striping created
845 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
846 * \param[in] th transaction handle
848 * \retval 0 on success
849 * \retval -ENOSPC if not enough OSTs are found
850 * \retval negative negated errno for other failures
852 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
853 struct dt_object **stripe, int flags,
856 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
857 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
858 struct pool_desc *pool = NULL;
859 struct ost_pool *osts;
860 struct lod_qos_rr *lqr;
862 unsigned int i, array_idx;
864 __u32 ost_start_idx_temp;
866 __u32 stripe_idx = 0;
867 __u32 stripe_cnt = lo->ldo_stripenr;
868 __u32 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
873 pool = lod_find_pool(m, lo->ldo_pool);
876 down_read(&pool_tgt_rw_sem(pool));
877 osts = &(pool->pool_obds);
878 lqr = &(pool->pool_rr);
880 osts = &(m->lod_pool_info);
881 lqr = &(m->lod_qos.lq_rr);
884 rc = lod_qos_calc_rr(m, osts, lqr);
888 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
892 if (--lqr->lqr_start_count <= 0) {
893 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
894 lqr->lqr_start_count =
895 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
896 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
897 } else if (stripe_cnt_min >= osts->op_count ||
898 lqr->lqr_start_idx > osts->op_count) {
899 /* If we have allocated from all of the OSTs, slowly
900 * precess the next start if the OST/stripe count isn't
901 * already doing this for us. */
902 lqr->lqr_start_idx %= osts->op_count;
903 if (stripe_cnt > 1 && (osts->op_count % stripe_cnt) != 1)
904 ++lqr->lqr_offset_idx;
906 down_read(&m->lod_qos.lq_rw_sem);
907 ost_start_idx_temp = lqr->lqr_start_idx;
910 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
913 QOS_DEBUG("pool '%s' want %d startidx %d startcnt %d offset %d "
914 "active %d count %d arrayidx %d\n",
915 lo->ldo_pool ? lo->ldo_pool : "",
916 stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
917 lqr->lqr_offset_idx, osts->op_count, osts->op_count,
920 for (i = 0; i < osts->op_count && stripe_idx < lo->ldo_stripenr;
921 i++, array_idx = (array_idx + 1) % osts->op_count) {
922 ++lqr->lqr_start_idx;
923 ost_idx = lqr->lqr_pool.op_array[array_idx];
925 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
926 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
927 stripe_idx, array_idx, ost_idx);
929 if ((ost_idx == LOV_QOS_EMPTY) ||
930 !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
933 /* Fail Check before osc_precreate() is called
934 so we can only 'fail' single OSC. */
935 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
938 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
940 /* this OSP doesn't feel well */
947 if (lod_qos_dev_is_full(sfs)) {
948 QOS_DEBUG("#%d is full\n", ost_idx);
953 * We expect number of precreated objects in f_ffree at
954 * the first iteration, skip OSPs with no objects ready
956 if (sfs->os_fprecreated == 0 && speed == 0) {
957 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
962 * try to use another OSP if this one is degraded
964 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
965 QOS_DEBUG("#%d: degraded\n", ost_idx);
970 * do not put >1 objects on a single OST
972 if (speed && lod_qos_is_ost_used(env, ost_idx, stripe_idx))
975 o = lod_qos_declare_object_on(env, m, ost_idx, th);
977 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
978 ost_idx, (int) PTR_ERR(o));
984 * We've successfuly declared (reserved) an object
986 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
987 stripe[stripe_idx] = o;
991 if ((speed < 2) && (stripe_idx < stripe_cnt_min)) {
992 /* Try again, allowing slower OSCs */
994 lqr->lqr_start_idx = ost_start_idx_temp;
998 up_read(&m->lod_qos.lq_rw_sem);
1001 lo->ldo_stripenr = stripe_idx;
1002 /* at least one stripe is allocated */
1005 /* nobody provided us with a single object */
1011 up_read(&pool_tgt_rw_sem(pool));
1012 /* put back ref got by lod_find_pool() */
1013 lod_pool_putref(pool);
1020 * Allocate a specific striping layout on a user defined set of OSTs.
1022 * Allocates new striping using the OST index range provided by the data from
1023 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1024 * OSTs are not considered. The exact order of OSTs requested by the user
1025 * is respected as much as possible depending on OST status. The number of
1026 * stripes needed and stripe offset are taken from the object. If that number
1027 * can not be met, then the function returns a failure and then it's the
1028 * caller's responsibility to release the stripes allocated. All the internal
1029 * structures are protected, but no concurrent allocation is allowed on the
1032 * \param[in] env execution environment for this thread
1033 * \param[in] lo LOD object
1034 * \param[out] stripe striping created
1035 * \param[in] lum stripe md to specify list of OSTs
1036 * \param[in] th transaction handle
1038 * \retval 0 on success
1039 * \retval -ENODEV OST index does not exist on file system
1040 * \retval -EINVAL requested OST index is invalid
1041 * \retval negative negated errno on error
1043 static int lod_alloc_ost_list(const struct lu_env *env,
1044 struct lod_object *lo, struct dt_object **stripe,
1045 struct lov_user_md *lum, struct thandle *th)
1047 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1048 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1049 struct dt_object *o;
1050 struct lov_user_md_v3 *v3;
1051 unsigned int array_idx = 0;
1052 int stripe_count = 0;
1057 /* for specific OSTs layout */
1058 LASSERT(lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC);
1059 lustre_print_user_md(D_OTHER, lum, __func__);
1061 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1065 v3 = (struct lov_user_md_v3 *)lum;
1066 for (i = 0; i < lo->ldo_stripenr; i++) {
1067 if (v3->lmm_objects[i].l_ost_idx == lo->ldo_def_stripe_offset) {
1072 if (i == lo->ldo_stripenr) {
1074 "%s: start index %d not in the specified list of OSTs\n",
1075 lod2obd(m)->obd_name, lo->ldo_def_stripe_offset);
1079 for (i = 0; i < lo->ldo_stripenr;
1080 i++, array_idx = (array_idx + 1) % lo->ldo_stripenr) {
1081 __u32 ost_idx = v3->lmm_objects[array_idx].l_ost_idx;
1083 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1089 * do not put >1 objects on a single OST
1091 if (lod_qos_is_ost_used(env, ost_idx, stripe_count)) {
1096 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1097 if (rc < 0) /* this OSP doesn't feel well */
1100 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1104 "%s: can't declare new object on #%u: %d\n",
1105 lod2obd(m)->obd_name, ost_idx, rc);
1110 * We've successfuly declared (reserved) an object
1112 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1113 stripe[stripe_count] = o;
1121 * Allocate a striping on a predefined set of OSTs.
1123 * Allocates new striping starting from OST provided lo->ldo_def_stripe_offset.
1124 * Full OSTs are not considered. The exact order of OSTs is not important and
1125 * varies depending on OST status. The allocation procedure prefers the targets
1126 * with precreated objects ready. The number of stripes needed and stripe
1127 * offset are taken from the object. If that number can not be met, then the
1128 * function returns a failure and then it's the caller's responsibility to
1129 * release the stripes allocated. All the internal structures are protected,
1130 * but no concurrent allocation is allowed on the same objects.
1132 * \param[in] env execution environment for this thread
1133 * \param[in] lo LOD object
1134 * \param[out] stripe striping created
1135 * \param[in] flags not used
1136 * \param[in] th transaction handle
1138 * \retval 0 on success
1139 * \retval -E2BIG if no enough OSTs are found
1140 * \retval -EINVAL requested offset is invalid
1141 * \retval negative negated errno on error
1143 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1144 struct dt_object **stripe, int flags,
1147 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1148 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1149 struct dt_object *o;
1151 unsigned int i, array_idx, ost_count;
1152 int rc, stripe_num = 0;
1154 struct pool_desc *pool = NULL;
1155 struct ost_pool *osts;
1158 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1163 pool = lod_find_pool(m, lo->ldo_pool);
1166 down_read(&pool_tgt_rw_sem(pool));
1167 osts = &(pool->pool_obds);
1169 osts = &(m->lod_pool_info);
1172 ost_count = osts->op_count;
1175 /* search loi_ost_idx in ost array */
1177 for (i = 0; i < ost_count; i++) {
1178 if (osts->op_array[i] == lo->ldo_def_stripe_offset) {
1183 if (i == ost_count) {
1184 CERROR("Start index %d not found in pool '%s'\n",
1185 lo->ldo_def_stripe_offset,
1186 lo->ldo_pool ? lo->ldo_pool : "");
1187 GOTO(out, rc = -EINVAL);
1190 for (i = 0; i < ost_count;
1191 i++, array_idx = (array_idx + 1) % ost_count) {
1192 ost_idx = osts->op_array[array_idx];
1194 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1197 /* Fail Check before osc_precreate() is called
1198 so we can only 'fail' single OSC. */
1199 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1203 * do not put >1 objects on a single OST
1205 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
1208 /* Drop slow OSCs if we can, but not for requested start idx.
1210 * This means "if OSC is slow and it is not the requested
1211 * start OST, then it can be skipped, otherwise skip it only
1212 * if it is inactive/recovering/out-of-space." */
1214 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1216 /* this OSP doesn't feel well */
1221 * We expect number of precreated objects in f_ffree at
1222 * the first iteration, skip OSPs with no objects ready
1223 * don't apply this logic to OST specified with stripe_offset
1225 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1228 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1230 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1231 ost_idx, (int) PTR_ERR(o));
1236 * We've successfuly declared (reserved) an object
1238 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1239 stripe[stripe_num] = o;
1242 /* We have enough stripes */
1243 if (stripe_num == lo->ldo_stripenr)
1247 /* Try again, allowing slower OSCs */
1252 /* If we were passed specific striping params, then a failure to
1253 * meet those requirements is an error, since we can't reallocate
1254 * that memory (it might be part of a larger array or something).
1256 * We can only get here if lsm_stripe_count was originally > 1.
1258 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1259 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1264 up_read(&pool_tgt_rw_sem(pool));
1265 /* put back ref got by lod_find_pool() */
1266 lod_pool_putref(pool);
1273 * Check whether QoS allocation should be used.
1275 * A simple helper to decide when QoS allocation should be used:
1276 * if it's just a single available target or the used space is
1277 * evenly distributed among the targets at the moment, then QoS
1278 * allocation algorithm should not be used.
1280 * \param[in] lod LOD device
1282 * \retval 0 should not be used
1283 * \retval 1 should be used
1285 static inline int lod_qos_is_usable(struct lod_device *lod)
1288 /* to be able to debug QoS code */
1292 /* Detect -EAGAIN early, before expensive lock is taken. */
1293 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1296 if (lod->lod_desc.ld_active_tgt_count < 2)
1303 * Allocate a striping using an algorithm with weights.
1305 * The function allocates OST objects to create a striping. The algorithm
1306 * used is based on weights (currently only using the free space), and it's
1307 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1308 * configuration (# of stripes, offset,
1309 * pool) is taken from the object and is prepared by the caller.
1310 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1311 * be met due to too few OSTs, then allocation fails. If the flag is
1312 * passed and less than 75% of the requested number of stripes can be
1313 * allocated, then allocation fails.
1314 * No concurrent allocation is allowed on the object and this must be
1315 * ensured by the caller. All the internal structures are protected by the
1317 * The algorithm has two steps: find available OSTs and calucate their weights,
1318 * then select the OSTs the weights used as the probability. An OST with a
1319 * higher weight is proportionately more likely to be selected than one with
1322 * \param[in] env execution environment for this thread
1323 * \param[in] lo LOD object
1324 * \param[out] stripe striping created
1325 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1326 * \param[in] th transaction handle
1328 * \retval 0 on success
1329 * \retval -E2BIG if no enough OSTs are found
1330 * \retval -EINVAL requested offset is invalid
1331 * \retval negative negated errno on error
1333 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1334 struct dt_object **stripe, int flags,
1337 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1338 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1339 struct lod_tgt_desc *ost;
1340 struct dt_object *o;
1341 __u64 total_weight = 0;
1344 __u32 nfound, good_osts;
1345 __u32 stripe_cnt = lo->ldo_stripenr;
1346 __u32 stripe_cnt_min;
1347 struct pool_desc *pool = NULL;
1348 struct ost_pool *osts;
1351 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
1352 if (stripe_cnt_min < 1)
1356 pool = lod_find_pool(m, lo->ldo_pool);
1359 down_read(&pool_tgt_rw_sem(pool));
1360 osts = &(pool->pool_obds);
1362 osts = &(m->lod_pool_info);
1365 /* Detect -EAGAIN early, before expensive lock is taken. */
1366 if (!lod_qos_is_usable(m))
1367 GOTO(out_nolock, rc = -EAGAIN);
1369 /* Do actual allocation, use write lock here. */
1370 down_write(&m->lod_qos.lq_rw_sem);
1373 * Check again, while we were sleeping on @lq_rw_sem things could
1376 if (!lod_qos_is_usable(m))
1377 GOTO(out, rc = -EAGAIN);
1379 rc = lod_qos_calc_ppo(m);
1383 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1388 /* Find all the OSTs that are valid stripe candidates */
1389 for (i = 0; i < osts->op_count; i++) {
1390 if (!cfs_bitmap_check(m->lod_ost_bitmap, osts->op_array[i]))
1393 rc = lod_statfs_and_check(env, m, osts->op_array[i], sfs);
1395 /* this OSP doesn't feel well */
1402 if (lod_qos_dev_is_full(sfs))
1405 /* Fail Check before osc_precreate() is called
1406 so we can only 'fail' single OSC. */
1407 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1408 osts->op_array[i] == 0)
1411 ost = OST_TGT(m,osts->op_array[i]);
1412 ost->ltd_qos.ltq_usable = 1;
1413 lod_qos_calc_weight(m, osts->op_array[i]);
1414 total_weight += ost->ltd_qos.ltq_weight;
1419 QOS_DEBUG("found %d good osts\n", good_osts);
1421 if (good_osts < stripe_cnt_min)
1422 GOTO(out, rc = -EAGAIN);
1424 /* We have enough osts */
1425 if (good_osts < stripe_cnt)
1426 stripe_cnt = good_osts;
1428 /* Find enough OSTs with weighted random allocation. */
1430 while (nfound < stripe_cnt) {
1431 __u64 rand, cur_weight;
1437 #if BITS_PER_LONG == 32
1438 rand = cfs_rand() % (unsigned)total_weight;
1439 /* If total_weight > 32-bit, first generate the high
1440 * 32 bits of the random number, then add in the low
1441 * 32 bits (truncated to the upper limit, if needed) */
1442 if (total_weight > 0xffffffffULL)
1443 rand = (__u64)(cfs_rand() %
1444 (unsigned)(total_weight >> 32)) << 32;
1448 if (rand == (total_weight & 0xffffffff00000000ULL))
1449 rand |= cfs_rand() % (unsigned)total_weight;
1454 rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1461 /* On average, this will hit larger-weighted osts more often.
1462 0-weight osts will always get used last (only when rand=0) */
1463 for (i = 0; i < osts->op_count; i++) {
1464 __u32 idx = osts->op_array[i];
1466 if (!cfs_bitmap_check(m->lod_ost_bitmap, idx))
1469 ost = OST_TGT(m,idx);
1471 if (!ost->ltd_qos.ltq_usable)
1474 cur_weight += ost->ltd_qos.ltq_weight;
1475 QOS_DEBUG("stripe_cnt=%d nfound=%d cur_weight="LPU64
1476 " rand="LPU64" total_weight="LPU64"\n",
1477 stripe_cnt, nfound, cur_weight, rand,
1480 if (cur_weight < rand)
1483 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1486 * do not put >1 objects on a single OST
1488 if (lod_qos_is_ost_used(env, idx, nfound))
1490 lod_qos_ost_in_use(env, nfound, idx);
1492 o = lod_qos_declare_object_on(env, m, idx, th);
1494 QOS_DEBUG("can't declare object on #%u: %d\n",
1495 idx, (int) PTR_ERR(o));
1498 stripe[nfound++] = o;
1499 lod_qos_used(m, osts, idx, &total_weight);
1505 /* no OST found on this iteration, give up */
1510 if (unlikely(nfound != stripe_cnt)) {
1512 * when the decision to use weighted algorithm was made
1513 * we had enough appropriate OSPs, but this state can
1514 * change anytime (no space on OST, broken connection, etc)
1515 * so it's possible OSP won't be able to provide us with
1516 * an object due to just changed state
1518 LCONSOLE_INFO("wanted %d, found %d\n", stripe_cnt, nfound);
1519 for (i = 0; i < nfound; i++) {
1520 LASSERT(stripe[i] != NULL);
1521 lu_object_put(env, &stripe[i]->do_lu);
1525 /* makes sense to rebalance next time */
1526 m->lod_qos.lq_dirty = 1;
1527 m->lod_qos.lq_same_space = 0;
1533 up_write(&m->lod_qos.lq_rw_sem);
1537 up_read(&pool_tgt_rw_sem(pool));
1538 /* put back ref got by lod_find_pool() */
1539 lod_pool_putref(pool);
1546 * Find largest stripe count the caller can use.
1548 * Find the maximal possible stripe count not greater than \a stripe_count.
1549 * Sometimes suggested stripecount can't be reached for a number of reasons:
1550 * lack of enough active OSTs or the backend does not support EAs that large.
1551 * If the passed one is 0, then the filesystem's default one is used.
1553 * \param[in] lod LOD device
1554 * \param[in] magic the format if striping
1555 * \param[in] stripe_count count the caller would like to use
1557 * \retval the maximum usable stripe count
1559 static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
1562 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1565 stripe_count = lod->lod_desc.ld_default_stripe_count;
1566 if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1567 stripe_count = lod->lod_desc.ld_active_tgt_count;
1571 /* stripe count is based on whether OSD can handle larger EA sizes */
1572 if (lod->lod_osd_max_easize > 0)
1573 max_stripes = lov_mds_md_max_stripe_count(
1574 lod->lod_osd_max_easize, magic);
1576 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1580 * Create in-core respresentation for a fully-defined striping
1582 * When the caller passes a fully-defined striping (i.e. everything including
1583 * OST object FIDs are defined), then we still need to instantiate LU-cache
1584 * with the objects representing the stripes defined. This function completes
1587 * \param[in] env execution environment for this thread
1588 * \param[in] mo LOD object
1589 * \param[in] buf buffer containing the striping
1591 * \retval 0 on success
1592 * \retval negative negated errno on error
1594 static int lod_use_defined_striping(const struct lu_env *env,
1595 struct lod_object *mo,
1596 const struct lu_buf *buf)
1598 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1599 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1600 struct lov_ost_data_v1 *objs;
1605 magic = le32_to_cpu(v1->lmm_magic);
1606 if (magic == LOV_MAGIC_V1_DEF) {
1607 magic = LOV_MAGIC_V1;
1608 objs = &v1->lmm_objects[0];
1609 } else if (magic == LOV_MAGIC_V3_DEF) {
1610 magic = LOV_MAGIC_V3;
1611 objs = &v3->lmm_objects[0];
1612 lod_object_set_pool(mo, v3->lmm_pool_name);
1614 GOTO(out, rc = -EINVAL);
1617 mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
1618 mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1619 mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
1620 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1622 /* fixup for released file before object initialization */
1623 if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1624 mo->ldo_released_stripenr = mo->ldo_stripenr;
1625 mo->ldo_stripenr = 0;
1628 LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
1630 if (mo->ldo_stripenr > 0)
1631 rc = lod_initialize_objects(env, mo, objs);
1638 * Parse suggested striping configuration.
1640 * The caller gets a suggested striping configuration from a number of sources
1641 * including per-directory default and applications. Then it needs to verify
1642 * the suggested striping is valid, apply missing bits and store the resulting
1643 * configuration in the object to be used by the allocator later. Must not be
1644 * called concurrently against the same object. It's OK to provide a
1645 * fully-defined striping.
1647 * \param[in] env execution environment for this thread
1648 * \param[in] lo LOD object
1649 * \param[in] buf buffer containing the striping
1651 * \retval 0 on success
1652 * \retval negative negated errno on error
1654 static int lod_qos_parse_config(const struct lu_env *env,
1655 struct lod_object *lo,
1656 const struct lu_buf *buf)
1658 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1659 struct lov_user_md_v1 *v1 = NULL;
1660 struct lov_user_md_v3 *v3 = NULL;
1661 char *pool_name = NULL;
1667 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1672 magic = v1->lmm_magic;
1674 if (unlikely(magic == LOV_MAGIC_V1_DEF || magic == LOV_MAGIC_V3_DEF)) {
1675 /* try to use as fully defined striping */
1676 rc = lod_use_defined_striping(env, lo, buf);
1681 case __swab32(LOV_USER_MAGIC_V1):
1682 lustre_swab_lov_user_md_v1(v1);
1683 magic = v1->lmm_magic;
1685 case LOV_USER_MAGIC_V1:
1689 case __swab32(LOV_USER_MAGIC_V3):
1690 lustre_swab_lov_user_md_v3(v3);
1691 magic = v3->lmm_magic;
1693 case LOV_USER_MAGIC_V3:
1695 pool_name = v3->lmm_pool_name;
1698 case __swab32(LOV_USER_MAGIC_SPECIFIC):
1699 lustre_swab_lov_user_md_v3(v3);
1700 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1701 v3->lmm_stripe_count);
1702 magic = v3->lmm_magic;
1704 case LOV_USER_MAGIC_SPECIFIC:
1705 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
1706 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
1707 if (v3->lmm_pool_name[0] != '\0')
1708 pool_name = v3->lmm_pool_name;
1709 size = lov_user_md_size(v3->lmm_stripe_count,
1710 LOV_USER_MAGIC_SPECIFIC);
1714 CERROR("%s: unrecognized magic %X\n",
1715 lod2obd(d)->obd_name, magic);
1719 if (unlikely(buf->lb_len < size)) {
1720 CERROR("%s: wrong size: %zd, expect: %u\n",
1721 lod2obd(d)->obd_name, buf->lb_len, size);
1725 lustre_print_user_md(D_OTHER, v1, "parse config");
1727 v1->lmm_magic = magic;
1728 if (v1->lmm_pattern == 0)
1729 v1->lmm_pattern = LOV_PATTERN_RAID0;
1730 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
1731 CERROR("%s: invalid pattern: %x\n",
1732 lod2obd(d)->obd_name, v1->lmm_pattern);
1735 lo->ldo_pattern = v1->lmm_pattern;
1737 if (v1->lmm_stripe_size > 0)
1738 lo->ldo_stripe_size = v1->lmm_stripe_size;
1740 if (lo->ldo_stripe_size & (LOV_MIN_STRIPE_SIZE - 1))
1741 lo->ldo_stripe_size = LOV_MIN_STRIPE_SIZE;
1743 if (v1->lmm_stripe_count > 0)
1744 lo->ldo_stripenr = v1->lmm_stripe_count;
1746 lo->ldo_def_stripe_offset = v1->lmm_stripe_offset;
1748 lod_object_set_pool(lo, NULL);
1749 if (pool_name != NULL) {
1750 struct pool_desc *pool;
1752 /* In the function below, .hs_keycmp resolves to
1753 * pool_hashkey_keycmp() */
1754 /* coverity[overrun-buffer-val] */
1755 pool = lod_find_pool(d, pool_name);
1757 if (lo->ldo_def_stripe_offset != LOV_OFFSET_DEFAULT) {
1758 rc = lod_check_index_in_pool(
1759 lo->ldo_def_stripe_offset, pool);
1761 lod_pool_putref(pool);
1762 CERROR("%s: invalid offset, %u\n",
1763 lod2obd(d)->obd_name,
1764 lo->ldo_def_stripe_offset);
1769 if (lo->ldo_stripenr > pool_tgt_count(pool))
1770 lo->ldo_stripenr = pool_tgt_count(pool);
1772 lod_pool_putref(pool);
1775 lod_object_set_pool(lo, pool_name);
1778 /* fixup for released file */
1779 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1780 lo->ldo_released_stripenr = lo->ldo_stripenr;
1781 lo->ldo_stripenr = 0;
1788 * Create a striping for an obejct.
1790 * The function creates a new striping for the object. A buffer containing
1791 * configuration hints can be provided optionally. The function tries QoS
1792 * algorithm first unless free space is distributed evenly among OSTs, but
1793 * by default RR algorithm is preferred due to internal concurrency (QoS is
1794 * serialized). The caller must ensure no concurrent calls to the function
1795 * are made against the same object.
1797 * \param[in] env execution environment for this thread
1798 * \param[in] lo LOD object
1799 * \param[in] attr attributes OST objects will be declared with
1800 * \param[in] buf suggested striping configuration or NULL
1801 * \param[in] th transaction handle
1803 * \retval 0 on success
1804 * \retval negative negated errno on error
1806 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
1807 struct lu_attr *attr, const struct lu_buf *buf,
1810 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1811 struct dt_object **stripe;
1813 int flag = LOV_USES_ASSIGNED_STRIPE;
1819 /* no OST available */
1820 /* XXX: should we be waiting a bit to prevent failures during
1821 * cluster initialization? */
1822 if (d->lod_ostnr == 0)
1823 GOTO(out, rc = -EIO);
1826 * by this time, the object's ldo_stripenr and ldo_stripe_size
1827 * contain default value for striping: taken from the parent
1828 * or from filesystem defaults
1830 * in case the caller is passing lovea with new striping config,
1831 * we may need to parse lovea and apply new configuration
1833 rc = lod_qos_parse_config(env, lo, buf);
1837 /* A released file is being created */
1838 if (lo->ldo_stripenr == 0)
1841 if (likely(lo->ldo_stripe == NULL)) {
1842 struct lov_user_md *lum = NULL;
1845 * no striping has been created so far
1847 LASSERT(lo->ldo_stripenr > 0);
1849 * statfs and check OST targets now, since ld_active_tgt_count
1850 * could be changed if some OSTs are [de]activated manually.
1852 lod_qos_statfs_update(env, d);
1853 lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
1856 stripe_len = lo->ldo_stripenr;
1857 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
1859 GOTO(out, rc = -ENOMEM);
1861 lod_getref(&d->lod_ost_descs);
1862 /* XXX: support for non-0 files w/o objects */
1863 CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
1864 d->lod_desc.ld_tgt_count, stripe_len);
1866 if (buf != NULL && buf->lb_buf != NULL)
1869 if (lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
1870 rc = lod_alloc_ost_list(env, lo, stripe, lum, th);
1871 } else if (lo->ldo_def_stripe_offset == LOV_OFFSET_DEFAULT) {
1872 rc = lod_alloc_qos(env, lo, stripe, flag, th);
1874 rc = lod_alloc_rr(env, lo, stripe, flag, th);
1876 rc = lod_alloc_specific(env, lo, stripe, flag, th);
1878 lod_putref(d, &d->lod_ost_descs);
1881 for (i = 0; i < stripe_len; i++)
1882 if (stripe[i] != NULL)
1883 lu_object_put(env, &stripe[i]->do_lu);
1885 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
1886 lo->ldo_stripenr = 0;
1888 lo->ldo_stripe = stripe;
1889 lo->ldo_stripes_allocated = stripe_len;
1893 * lod_qos_parse_config() found supplied buf as a predefined
1894 * striping (not a hint), so it allocated all the object
1895 * now we need to create them
1897 for (i = 0; i < lo->ldo_stripenr; i++) {
1898 struct dt_object *o;
1900 o = lo->ldo_stripe[i];
1903 rc = dt_declare_create(env, o, attr, NULL, NULL, th);
1905 CERROR("can't declare create: %d\n", rc);