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, 2013, 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
54 #define QOS_DEBUG(fmt, ...) CDEBUG(D_OTHER, fmt, ## __VA_ARGS__)
55 #define QOS_CONSOLE(fmt, ...) LCONSOLE(D_OTHER, fmt, ## __VA_ARGS__)
57 #define QOS_DEBUG(fmt, ...)
58 #define QOS_CONSOLE(fmt, ...)
61 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
62 OST_TGT(lod,i)->ltd_statfs.os_bsize)
65 * Add a new target to Quality of Service (QoS) target table.
67 * Add a new OST target to the structure representing an OSS. Resort the list
68 * of known OSSs by the number of OSTs attached to each OSS. The OSS list is
69 * protected internally and no external locking is required.
71 * \param[in] lod LOD device
72 * \param[in] ost_desc OST description
74 * \retval 0 on success
75 * \retval -ENOMEM on error
77 int qos_add_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
79 struct lod_qos_oss *oss = NULL, *temposs;
80 struct obd_export *exp = ost_desc->ltd_exp;
81 int rc = 0, found = 0;
82 struct list_head *list;
85 down_write(&lod->lod_qos.lq_rw_sem);
87 * a bit hacky approach to learn NID of corresponding connection
88 * but there is no official API to access information like this
91 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
92 if (obd_uuid_equals(&oss->lqo_uuid,
93 &exp->exp_connection->c_remote_uuid)) {
102 GOTO(out, rc = -ENOMEM);
103 memcpy(&oss->lqo_uuid, &exp->exp_connection->c_remote_uuid,
104 sizeof(oss->lqo_uuid));
106 /* Assume we have to move this one */
107 list_del(&oss->lqo_oss_list);
110 oss->lqo_ost_count++;
111 ost_desc->ltd_qos.ltq_oss = oss;
113 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
114 obd_uuid2str(&ost_desc->ltd_uuid), obd_uuid2str(&oss->lqo_uuid),
117 /* Add sorted by # of OSTs. Find the first entry that we're
119 list = &lod->lod_qos.lq_oss_list;
120 list_for_each_entry(temposs, list, lqo_oss_list) {
121 if (oss->lqo_ost_count > temposs->lqo_ost_count)
124 /* ...and add before it. If we're the first or smallest, temposs
125 points to the list head, and we add to the end. */
126 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
128 lod->lod_qos.lq_dirty = 1;
129 lod->lod_qos.lq_rr.lqr_dirty = 1;
132 up_write(&lod->lod_qos.lq_rw_sem);
137 * Remove OST target from QoS table.
139 * Removes given OST target from QoS table and releases related OSS structure
140 * if no OSTs remain on the OSS.
142 * \param[in] lod LOD device
143 * \param[in] ost_desc OST description
145 * \retval 0 on success
146 * \retval -ENOENT if no OSS was found
148 int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
150 struct lod_qos_oss *oss;
154 down_write(&lod->lod_qos.lq_rw_sem);
155 oss = ost_desc->ltd_qos.ltq_oss;
157 GOTO(out, rc = -ENOENT);
159 oss->lqo_ost_count--;
160 if (oss->lqo_ost_count == 0) {
161 CDEBUG(D_QOS, "removing OSS %s\n",
162 obd_uuid2str(&oss->lqo_uuid));
163 list_del(&oss->lqo_oss_list);
164 ost_desc->ltd_qos.ltq_oss = NULL;
168 lod->lod_qos.lq_dirty = 1;
169 lod->lod_qos.lq_rr.lqr_dirty = 1;
171 up_write(&lod->lod_qos.lq_rw_sem);
176 * Check whether the target is available for new OST objects.
178 * Request statfs data from the given target and verify it's active and not
179 * read-only. If so, then it can be used to place new OST objects. This
180 * function also maintains the number of active/inactive targets and sets
181 * dirty flags if those numbers change so others can run re-balance procedures.
182 * No external locking is required.
184 * \param[in] env execution environment for this thread
185 * \param[in] d LOD device
186 * \param[in] index index of OST target to check
187 * \param[out] sfs buffer for statfs data
189 * \retval 0 if the target is good
190 * \retval negative negated errno on error
193 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
194 int index, struct obd_statfs *sfs)
196 struct lod_tgt_desc *ost;
200 ost = OST_TGT(d,index);
203 rc = dt_statfs(env, ost->ltd_ost, sfs);
204 if (rc && rc != -ENOTCONN)
205 CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
207 /* If the OST is readonly then we can't allocate objects there */
208 if (sfs->os_state & OS_STATE_READONLY)
211 /* check whether device has changed state (active, inactive) */
212 if (rc != 0 && ost->ltd_active) {
213 /* turned inactive? */
214 spin_lock(&d->lod_desc_lock);
215 if (ost->ltd_active) {
217 LASSERT(d->lod_desc.ld_active_tgt_count > 0);
218 d->lod_desc.ld_active_tgt_count--;
219 d->lod_qos.lq_dirty = 1;
220 d->lod_qos.lq_rr.lqr_dirty = 1;
221 CDEBUG(D_CONFIG, "%s: turns inactive\n",
222 ost->ltd_exp->exp_obd->obd_name);
224 spin_unlock(&d->lod_desc_lock);
225 } else if (rc == 0 && ost->ltd_active == 0) {
227 LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
228 "active tgt count %d, ost nr %d\n",
229 d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
230 spin_lock(&d->lod_desc_lock);
231 if (ost->ltd_active == 0) {
233 d->lod_desc.ld_active_tgt_count++;
234 d->lod_qos.lq_dirty = 1;
235 d->lod_qos.lq_rr.lqr_dirty = 1;
236 CDEBUG(D_CONFIG, "%s: turns active\n",
237 ost->ltd_exp->exp_obd->obd_name);
239 spin_unlock(&d->lod_desc_lock);
246 * Maintain per-target statfs data.
248 * The function refreshes statfs data for all the targets every N seconds.
249 * The actual N is controlled via procfs and set to LOV_DESC_QOS_MAXAGE_DEFAULT
252 * \param[in] env execution environment for this thread
253 * \param[in] lod LOD device
255 static void lod_qos_statfs_update(const struct lu_env *env,
256 struct lod_device *lod)
258 struct obd_device *obd = lod2obd(lod);
259 struct ost_pool *osts = &(lod->lod_pool_info);
262 __u64 max_age, avail;
265 max_age = cfs_time_shift_64(-2 * lod->lod_desc.ld_qos_maxage);
267 if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
268 /* statfs data are quite recent, don't need to refresh it */
271 down_write(&lod->lod_qos.lq_rw_sem);
272 if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
275 for (i = 0; i < osts->op_count; i++) {
276 idx = osts->op_array[i];
277 avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
278 rc = lod_statfs_and_check(env, lod, idx,
279 &OST_TGT(lod,idx)->ltd_statfs);
282 if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
283 /* recalculate weigths */
284 lod->lod_qos.lq_dirty = 1;
286 obd->obd_osfs_age = cfs_time_current_64();
289 up_write(&lod->lod_qos.lq_rw_sem);
294 * Calculate per-OST and per-OSS penalties
296 * Re-calculate penalties when the configuration changes, active targets
297 * change and after statfs refresh (all these are reflected by lq_dirty flag).
298 * On every OST and OSS: decay the penalty by half for every 8x the update
299 * interval that the device has been idle. That gives lots of time for the
300 * statfs information to be updated (which the penalty is only a proxy for),
301 * and avoids penalizing OSS/OSTs under light load.
302 * See lod_qos_calc_weight() for how penalties are factored into the weight.
304 * \param[in] lod LOD device
306 * \retval 0 on success
307 * \retval -EAGAIN the number of OSTs isn't enough
309 static int lod_qos_calc_ppo(struct lod_device *lod)
311 struct lod_qos_oss *oss;
312 __u64 ba_max, ba_min, temp;
319 if (!lod->lod_qos.lq_dirty)
322 num_active = lod->lod_desc.ld_active_tgt_count - 1;
324 GOTO(out, rc = -EAGAIN);
326 /* find bavail on each OSS */
327 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
329 lod->lod_qos.lq_active_oss_count = 0;
332 * How badly user wants to select OSTs "widely" (not recently chosen
333 * and not on recent OSS's). As opposed to "freely" (free space
336 prio_wide = 256 - lod->lod_qos.lq_prio_free;
338 ba_min = (__u64)(-1);
340 now = cfs_time_current_sec();
341 /* Calculate OST penalty per object
342 * (lod ref taken in lod_qos_prep_create()) */
343 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
344 LASSERT(OST_TGT(lod,i));
345 temp = TGT_BAVAIL(i);
348 ba_min = min(temp, ba_min);
349 ba_max = max(temp, ba_max);
351 /* Count the number of usable OSS's */
352 if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
353 lod->lod_qos.lq_active_oss_count++;
354 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
356 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
358 do_div(temp, num_active);
359 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
360 (temp * prio_wide) >> 8;
362 age = (now - OST_TGT(lod,i)->ltd_qos.ltq_used) >> 3;
363 if (lod->lod_qos.lq_reset ||
364 age > 32 * lod->lod_desc.ld_qos_maxage)
365 OST_TGT(lod,i)->ltd_qos.ltq_penalty = 0;
366 else if (age > lod->lod_desc.ld_qos_maxage)
367 /* Decay OST penalty. */
368 OST_TGT(lod,i)->ltd_qos.ltq_penalty >>=
369 (age / lod->lod_desc.ld_qos_maxage);
372 num_active = lod->lod_qos.lq_active_oss_count - 1;
373 if (num_active < 1) {
374 /* If there's only 1 OSS, we can't penalize it, so instead
375 we have to double the OST penalty */
377 cfs_foreach_bit(lod->lod_ost_bitmap, i)
378 OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj <<= 1;
381 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
382 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
383 temp = oss->lqo_bavail >> 1;
384 do_div(temp, oss->lqo_ost_count * num_active);
385 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
387 age = (now - oss->lqo_used) >> 3;
388 if (lod->lod_qos.lq_reset ||
389 age > 32 * lod->lod_desc.ld_qos_maxage)
390 oss->lqo_penalty = 0;
391 else if (age > lod->lod_desc.ld_qos_maxage)
392 /* Decay OSS penalty. */
393 oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
396 lod->lod_qos.lq_dirty = 0;
397 lod->lod_qos.lq_reset = 0;
399 /* If each ost has almost same free space,
400 * do rr allocation for better creation performance */
401 lod->lod_qos.lq_same_space = 0;
402 if ((ba_max * (256 - lod->lod_qos.lq_threshold_rr)) >> 8 < ba_min) {
403 lod->lod_qos.lq_same_space = 1;
404 /* Reset weights for the next time we enter qos mode */
405 lod->lod_qos.lq_reset = 1;
411 if (!rc && lod->lod_qos.lq_same_space)
418 * Calculate weight for a given OST target.
420 * The final OST weight is the number of bytes available minus the OST and
421 * OSS penalties. See lod_qos_calc_ppo() for how penalties are calculated.
423 * \param[in] lod LOD device, where OST targets are listed
424 * \param[in] i OST target index
428 static int lod_qos_calc_weight(struct lod_device *lod, int i)
432 temp = TGT_BAVAIL(i);
433 temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
434 OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
436 OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
438 OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
443 * Re-calculate weights.
445 * The function is called when some OST target was used for a new object. In
446 * this case we should re-calculate all the weights to keep new allocations
449 * \param[in] lod LOD device
450 * \param[in] osts OST pool where a new object was placed
451 * \param[in] index OST target where a new object was placed
452 * \param[out] total_wt new total weight for the pool
456 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
457 __u32 index, __u64 *total_wt)
459 struct lod_tgt_desc *ost;
460 struct lod_qos_oss *oss;
464 ost = OST_TGT(lod,index);
467 /* Don't allocate on this devuce anymore, until the next alloc_qos */
468 ost->ltd_qos.ltq_usable = 0;
470 oss = ost->ltd_qos.ltq_oss;
472 /* Decay old penalty by half (we're adding max penalty, and don't
473 want it to run away.) */
474 ost->ltd_qos.ltq_penalty >>= 1;
475 oss->lqo_penalty >>= 1;
477 /* mark the OSS and OST as recently used */
478 ost->ltd_qos.ltq_used = oss->lqo_used = cfs_time_current_sec();
480 /* Set max penalties for this OST and OSS */
481 ost->ltd_qos.ltq_penalty +=
482 ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
483 oss->lqo_penalty += oss->lqo_penalty_per_obj *
484 lod->lod_qos.lq_active_oss_count;
486 /* Decrease all OSS penalties */
487 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
488 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
489 oss->lqo_penalty = 0;
491 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
495 /* Decrease all OST penalties */
496 for (j = 0; j < osts->op_count; j++) {
499 i = osts->op_array[j];
500 if (!cfs_bitmap_check(lod->lod_ost_bitmap, i))
503 ost = OST_TGT(lod,i);
506 if (ost->ltd_qos.ltq_penalty <
507 ost->ltd_qos.ltq_penalty_per_obj)
508 ost->ltd_qos.ltq_penalty = 0;
510 ost->ltd_qos.ltq_penalty -=
511 ost->ltd_qos.ltq_penalty_per_obj;
513 lod_qos_calc_weight(lod, i);
515 /* Recalc the total weight of usable osts */
516 if (ost->ltd_qos.ltq_usable)
517 *total_wt += ost->ltd_qos.ltq_weight;
519 QOS_DEBUG("recalc tgt %d usable=%d avail="LPU64
520 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
521 " ossp="LPU64" wt="LPU64"\n",
522 i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
523 ost->ltd_qos.ltq_penalty_per_obj >> 10,
524 ost->ltd_qos.ltq_penalty >> 10,
525 ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
526 ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
527 ost->ltd_qos.ltq_weight >> 10);
533 #define LOV_QOS_EMPTY ((__u32)-1)
536 * Calculate optimal round-robin order with regard to OSSes.
538 * Place all the OSTs from pool \a src_pool in a special array to be used for
539 * round-robin (RR) stripe allocation. The placement algorithm interleaves
540 * OSTs from the different OSSs so that RR allocation can balance OSSs evenly.
541 * Resorts the targets when the number of active targets changes (because of
542 * a new target or activation/deactivation).
544 * \param[in] lod LOD device
545 * \param[in] src_pool OST pool
546 * \param[in] lqr round-robin list
548 * \retval 0 on success
549 * \retval -ENOMEM fails to allocate the array
551 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
552 struct lod_qos_rr *lqr)
554 struct lod_qos_oss *oss;
555 struct lod_tgt_desc *ost;
556 unsigned placed, real_count;
561 if (!lqr->lqr_dirty) {
562 LASSERT(lqr->lqr_pool.op_size);
566 /* Do actual allocation. */
567 down_write(&lod->lod_qos.lq_rw_sem);
570 * Check again. While we were sleeping on @lq_rw_sem something could
573 if (!lqr->lqr_dirty) {
574 LASSERT(lqr->lqr_pool.op_size);
575 up_write(&lod->lod_qos.lq_rw_sem);
579 real_count = src_pool->op_count;
581 /* Zero the pool array */
582 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
583 deleting from the pool. The lq_rw_sem insures that nobody else
585 lqr->lqr_pool.op_count = real_count;
586 rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
588 up_write(&lod->lod_qos.lq_rw_sem);
591 for (i = 0; i < lqr->lqr_pool.op_count; i++)
592 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
594 /* Place all the OSTs from 1 OSS at the same time. */
596 list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
599 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
602 if (!cfs_bitmap_check(lod->lod_ost_bitmap,
603 src_pool->op_array[i]))
606 ost = OST_TGT(lod,src_pool->op_array[i]);
607 LASSERT(ost && ost->ltd_ost);
608 if (ost->ltd_qos.ltq_oss != oss)
611 /* Evenly space these OSTs across arrayspace */
612 next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
613 while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
614 next = (next + 1) % lqr->lqr_pool.op_count;
616 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
623 up_write(&lod->lod_qos.lq_rw_sem);
625 if (placed != real_count) {
626 /* This should never happen */
627 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
628 "round-robin list (%d of %d).\n",
630 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
631 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
632 lqr->lqr_pool.op_array[i]);
639 for (i = 0; i < lqr->lqr_pool.op_count; i++)
640 QOS_CONSOLE("rr #%d ost idx=%d\n", i, lqr->lqr_pool.op_array[i]);
647 * Instantiate and declare creation of a new object.
649 * The function instantiates LU representation for a new object on the
650 * specified device. Also it declares an intention to create that
651 * object on the storage target.
653 * Note lu_object_anon() is used which is a trick with regard to LU/OSD
654 * infrastructure - in the existing precreation framework we can't assign FID
655 * at this moment, we do this later once a transaction is started. So the
656 * special method instantiates FID-less object in the cache and later it
657 * will get a FID and proper placement in LU cache.
659 * \param[in] env execution environment for this thread
660 * \param[in] d LOD device
661 * \param[in] ost_idx OST target index where the object is being created
662 * \param[in] th transaction handle
664 * \retval object ptr on success, ERR_PTR() otherwise
666 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
667 struct lod_device *d,
671 struct lod_tgt_desc *ost;
672 struct lu_object *o, *n;
673 struct lu_device *nd;
674 struct dt_object *dt;
679 LASSERT(ost_idx < d->lod_osts_size);
680 ost = OST_TGT(d,ost_idx);
682 LASSERT(ost->ltd_ost);
684 nd = &ost->ltd_ost->dd_lu_dev;
687 * allocate anonymous object with zero fid, real fid
688 * will be assigned by OSP within transaction
689 * XXX: to be fixed with fully-functional OST fids
691 o = lu_object_anon(env, nd, NULL);
693 GOTO(out, dt = ERR_PTR(PTR_ERR(o)));
695 n = lu_object_locate(o->lo_header, nd->ld_type);
696 if (unlikely(n == NULL)) {
697 CERROR("can't find slice\n");
698 lu_object_put(env, o);
699 GOTO(out, dt = ERR_PTR(-EINVAL));
702 dt = container_of(n, struct dt_object, do_lu);
704 rc = dt_declare_create(env, dt, NULL, NULL, NULL, th);
706 CDEBUG(D_OTHER, "can't declare creation on #%u: %d\n",
708 lu_object_put(env, o);
717 * Calculate a minimum acceptable stripe count.
719 * Return an acceptable stripe count depending on flag LOV_USES_DEFAULT_STRIPE:
720 * all stripes or 3/4 of stripes.
722 * \param[in] stripe_cnt number of stripes requested
723 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
725 * \retval acceptable stripecount
727 static int min_stripe_count(__u32 stripe_cnt, int flags)
729 return (flags & LOV_USES_DEFAULT_STRIPE ?
730 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
733 #define LOV_CREATE_RESEED_MULT 30
734 #define LOV_CREATE_RESEED_MIN 2000
737 * Check if an OST is full.
739 * Check whether an OST should be considered full based
740 * on the given statfs data.
742 * \param[in] msfs statfs data
744 * \retval false not full
747 static int inline lod_qos_dev_is_full(struct obd_statfs *msfs)
750 int bs = msfs->os_bsize;
752 LASSERT(((bs - 1) & bs) == 0);
754 /* the minimum of 0.1% used blocks and 1GB bytes. */
755 used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
756 1 << (31 - ffs(bs)));
757 return (msfs->os_bavail < used);
761 * Initialize temporary OST-in-use array.
763 * Allocate or extend the array used to mark targets already assigned to a new
764 * striping so they are not used more than once.
766 * \param[in] env execution environment for this thread
767 * \param[in] stripes number of items needed in the array
769 * \retval 0 on success
770 * \retval -ENOMEM on error
772 static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
775 struct lod_thread_info *info = lod_env_info(env);
777 if (info->lti_ea_store_size < sizeof(int) * stripes)
778 lod_ea_store_resize(info, stripes * sizeof(int));
779 if (info->lti_ea_store_size < sizeof(int) * stripes) {
780 CERROR("can't allocate memory for ost-in-use array\n");
783 memset(info->lti_ea_store, -1, sizeof(int) * stripes);
788 * Remember a target in the array of used targets.
790 * Mark the given target as used for a new striping being created. The status
791 * of an OST in a striping can be checked with lod_qos_is_ost_used().
793 * \param[in] env execution environment for this thread
794 * \param[in] idx index in the array
795 * \param[in] ost OST target index to mark as used
797 static inline void lod_qos_ost_in_use(const struct lu_env *env,
800 struct lod_thread_info *info = lod_env_info(env);
801 int *osts = info->lti_ea_store;
803 LASSERT(info->lti_ea_store_size >= idx * sizeof(int));
808 * Check is OST used in a striping.
810 * Checks whether OST with the given index is marked as used in the temporary
811 * array (see lod_qos_ost_in_use()).
813 * \param[in] env execution environment for this thread
814 * \param[in] ost OST target index to check
815 * \param[in] stripes the number of items used in the array already
820 static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
822 struct lod_thread_info *info = lod_env_info(env);
823 int *osts = info->lti_ea_store;
826 for (j = 0; j < stripes; j++) {
834 * Allocate a striping using round-robin algorigthm.
836 * Allocates a new striping using round-robin algorithm. The function refreshes
837 * all the internal structures (statfs cache, array of available OSTs sorted
838 * with regard to OSS, etc). The number of stripes required is taken from the
839 * object (must be prepared by the caller), but can change if the flag
840 * LOV_USES_DEFAULT_STRIPE is supplied. The caller should ensure nobody else
841 * is trying to create a striping on the object in parallel. All the internal
842 * structures (like pools, etc) are protected and no additional locking is
843 * required. The function succeeds even if a single stripe is allocated. To save
844 * time we give priority to targets which already have objects precreated.
845 * Full OSTs are skipped (see lod_qos_dev_is_full() for the details).
847 * \param[in] env execution environment for this thread
848 * \param[in] lo LOD object
849 * \param[out] stripe striping created
850 * \param[in] flags allocation flags (0 or LOV_USES_DEFAULT_STRIPE)
851 * \param[in] th transaction handle
853 * \retval 0 on success
854 * \retval -ENOSPC if not enough OSTs are found
855 * \retval negative negated errno for other failures
857 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
858 struct dt_object **stripe, int flags,
861 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
862 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
863 struct pool_desc *pool = NULL;
864 struct ost_pool *osts;
865 struct lod_qos_rr *lqr;
867 unsigned int i, array_idx;
869 __u32 ost_start_idx_temp;
871 __u32 stripe_idx = 0;
872 __u32 stripe_cnt = lo->ldo_stripenr;
873 __u32 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
878 pool = lod_find_pool(m, lo->ldo_pool);
881 down_read(&pool_tgt_rw_sem(pool));
882 osts = &(pool->pool_obds);
883 lqr = &(pool->pool_rr);
885 osts = &(m->lod_pool_info);
886 lqr = &(m->lod_qos.lq_rr);
889 rc = lod_qos_calc_rr(m, osts, lqr);
893 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
897 if (--lqr->lqr_start_count <= 0) {
898 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
899 lqr->lqr_start_count =
900 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
901 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
902 } else if (stripe_cnt_min >= osts->op_count ||
903 lqr->lqr_start_idx > osts->op_count) {
904 /* If we have allocated from all of the OSTs, slowly
905 * precess the next start if the OST/stripe count isn't
906 * already doing this for us. */
907 lqr->lqr_start_idx %= osts->op_count;
908 if (stripe_cnt > 1 && (osts->op_count % stripe_cnt) != 1)
909 ++lqr->lqr_offset_idx;
911 down_read(&m->lod_qos.lq_rw_sem);
912 ost_start_idx_temp = lqr->lqr_start_idx;
915 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
918 QOS_DEBUG("pool '%s' want %d startidx %d startcnt %d offset %d "
919 "active %d count %d arrayidx %d\n",
920 lo->ldo_pool ? lo->ldo_pool : "",
921 stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
922 lqr->lqr_offset_idx, osts->op_count, osts->op_count,
925 for (i = 0; i < osts->op_count && stripe_idx < lo->ldo_stripenr;
926 i++, array_idx = (array_idx + 1) % osts->op_count) {
927 ++lqr->lqr_start_idx;
928 ost_idx = lqr->lqr_pool.op_array[array_idx];
930 QOS_DEBUG("#%d strt %d act %d strp %d ary %d idx %d\n",
931 i, lqr->lqr_start_idx, /* XXX: active*/ 0,
932 stripe_idx, array_idx, ost_idx);
934 if ((ost_idx == LOV_QOS_EMPTY) ||
935 !cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
938 /* Fail Check before osc_precreate() is called
939 so we can only 'fail' single OSC. */
940 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
943 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
945 /* this OSP doesn't feel well */
952 if (lod_qos_dev_is_full(sfs)) {
953 QOS_DEBUG("#%d is full\n", ost_idx);
958 * We expect number of precreated objects in f_ffree at
959 * the first iteration, skip OSPs with no objects ready
961 if (sfs->os_fprecreated == 0 && speed == 0) {
962 QOS_DEBUG("#%d: precreation is empty\n", ost_idx);
967 * try to use another OSP if this one is degraded
969 if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
970 QOS_DEBUG("#%d: degraded\n", ost_idx);
975 * do not put >1 objects on a single OST
977 if (speed && lod_qos_is_ost_used(env, ost_idx, stripe_idx))
980 o = lod_qos_declare_object_on(env, m, ost_idx, th);
982 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
983 ost_idx, (int) PTR_ERR(o));
989 * We've successfuly declared (reserved) an object
991 lod_qos_ost_in_use(env, stripe_idx, ost_idx);
992 stripe[stripe_idx] = o;
996 if ((speed < 2) && (stripe_idx < stripe_cnt_min)) {
997 /* Try again, allowing slower OSCs */
999 lqr->lqr_start_idx = ost_start_idx_temp;
1003 up_read(&m->lod_qos.lq_rw_sem);
1006 lo->ldo_stripenr = stripe_idx;
1007 /* at least one stripe is allocated */
1010 /* nobody provided us with a single object */
1016 up_read(&pool_tgt_rw_sem(pool));
1017 /* put back ref got by lod_find_pool() */
1018 lod_pool_putref(pool);
1025 * Allocate a specific striping layout on a user defined set of OSTs.
1027 * Allocates new striping using the OST index range provided by the data from
1028 * the lmm_obejcts contained in the lov_user_md passed to this method. Full
1029 * OSTs are not considered. The exact order of OSTs requested by the user
1030 * is respected as much as possible depending on OST status. The number of
1031 * stripes needed and stripe offset are taken from the object. If that number
1032 * can not be met, then the function returns a failure and then it's the
1033 * caller's responsibility to release the stripes allocated. All the internal
1034 * structures are protected, but no concurrent allocation is allowed on the
1037 * \param[in] env execution environment for this thread
1038 * \param[in] lo LOD object
1039 * \param[out] stripe striping created
1040 * \param[in] lum stripe md to specify list of OSTs
1041 * \param[in] th transaction handle
1043 * \retval 0 on success
1044 * \retval -ENODEV OST index does not exist on file system
1045 * \retval -EINVAL requested OST index is invalid
1046 * \retval negative negated errno on error
1048 static int lod_alloc_ost_list(const struct lu_env *env,
1049 struct lod_object *lo, struct dt_object **stripe,
1050 struct lov_user_md *lum, struct thandle *th)
1052 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1053 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1054 struct dt_object *o;
1055 struct lov_user_md_v3 *v3;
1056 unsigned int array_idx = 0;
1057 int stripe_count = 0;
1062 /* for specific OSTs layout */
1063 LASSERT(lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC);
1064 lustre_print_user_md(D_OTHER, lum, __func__);
1066 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1070 v3 = (struct lov_user_md_v3 *)lum;
1071 for (i = 0; i < lo->ldo_stripenr; i++) {
1072 if (v3->lmm_objects[i].l_ost_idx == lo->ldo_def_stripe_offset) {
1077 if (i == lo->ldo_stripenr) {
1079 "%s: start index %d not in the specified list of OSTs\n",
1080 lod2obd(m)->obd_name, lo->ldo_def_stripe_offset);
1084 for (i = 0; i < lo->ldo_stripenr;
1085 i++, array_idx = (array_idx + 1) % lo->ldo_stripenr) {
1086 __u32 ost_idx = v3->lmm_objects[array_idx].l_ost_idx;
1088 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx)) {
1094 * do not put >1 objects on a single OST
1096 if (lod_qos_is_ost_used(env, ost_idx, stripe_count)) {
1101 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1102 if (rc < 0) /* this OSP doesn't feel well */
1105 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1109 "%s: can't declare new object on #%u: %d\n",
1110 lod2obd(m)->obd_name, ost_idx, rc);
1115 * We've successfuly declared (reserved) an object
1117 lod_qos_ost_in_use(env, stripe_count, ost_idx);
1118 stripe[stripe_count] = o;
1126 * Allocate a striping on a predefined set of OSTs.
1128 * Allocates new striping starting from OST provided lo->ldo_def_stripe_offset.
1129 * Full OSTs are not considered. The exact order of OSTs is not important and
1130 * varies depending on OST status. The allocation procedure prefers the targets
1131 * with precreated objects ready. The number of stripes needed and stripe
1132 * offset are taken from the object. If that number can not be met, then the
1133 * function returns a failure and then it's the caller's responsibility to
1134 * release the stripes allocated. All the internal structures are protected,
1135 * but no concurrent allocation is allowed on the same objects.
1137 * \param[in] env execution environment for this thread
1138 * \param[in] lo LOD object
1139 * \param[out] stripe striping created
1140 * \param[in] flags not used
1141 * \param[in] th transaction handle
1143 * \retval 0 on success
1144 * \retval -E2BIG if no enough OSTs are found
1145 * \retval -EINVAL requested offset is invalid
1146 * \retval negative negated errno on error
1148 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1149 struct dt_object **stripe, int flags,
1152 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1153 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1154 struct dt_object *o;
1156 unsigned int i, array_idx, ost_count;
1157 int rc, stripe_num = 0;
1159 struct pool_desc *pool = NULL;
1160 struct ost_pool *osts;
1163 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1168 pool = lod_find_pool(m, lo->ldo_pool);
1171 down_read(&pool_tgt_rw_sem(pool));
1172 osts = &(pool->pool_obds);
1174 osts = &(m->lod_pool_info);
1177 ost_count = osts->op_count;
1180 /* search loi_ost_idx in ost array */
1182 for (i = 0; i < ost_count; i++) {
1183 if (osts->op_array[i] == lo->ldo_def_stripe_offset) {
1188 if (i == ost_count) {
1189 CERROR("Start index %d not found in pool '%s'\n",
1190 lo->ldo_def_stripe_offset,
1191 lo->ldo_pool ? lo->ldo_pool : "");
1192 GOTO(out, rc = -EINVAL);
1195 for (i = 0; i < ost_count;
1196 i++, array_idx = (array_idx + 1) % ost_count) {
1197 ost_idx = osts->op_array[array_idx];
1199 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1202 /* Fail Check before osc_precreate() is called
1203 so we can only 'fail' single OSC. */
1204 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1208 * do not put >1 objects on a single OST
1210 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
1213 /* Drop slow OSCs if we can, but not for requested start idx.
1215 * This means "if OSC is slow and it is not the requested
1216 * start OST, then it can be skipped, otherwise skip it only
1217 * if it is inactive/recovering/out-of-space." */
1219 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1221 /* this OSP doesn't feel well */
1226 * We expect number of precreated objects in f_ffree at
1227 * the first iteration, skip OSPs with no objects ready
1228 * don't apply this logic to OST specified with stripe_offset
1230 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1233 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1235 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1236 ost_idx, (int) PTR_ERR(o));
1241 * We've successfuly declared (reserved) an object
1243 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1244 stripe[stripe_num] = o;
1247 /* We have enough stripes */
1248 if (stripe_num == lo->ldo_stripenr)
1252 /* Try again, allowing slower OSCs */
1257 /* If we were passed specific striping params, then a failure to
1258 * meet those requirements is an error, since we can't reallocate
1259 * that memory (it might be part of a larger array or something).
1261 * We can only get here if lsm_stripe_count was originally > 1.
1263 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1264 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1269 up_read(&pool_tgt_rw_sem(pool));
1270 /* put back ref got by lod_find_pool() */
1271 lod_pool_putref(pool);
1278 * Check whether QoS allocation should be used.
1280 * A simple helper to decide when QoS allocation should be used:
1281 * if it's just a single available target or the used space is
1282 * evenly distributed among the targets at the moment, then QoS
1283 * allocation algorithm should not be used.
1285 * \param[in] lod LOD device
1287 * \retval 0 should not be used
1288 * \retval 1 should be used
1290 static inline int lod_qos_is_usable(struct lod_device *lod)
1293 /* to be able to debug QoS code */
1297 /* Detect -EAGAIN early, before expensive lock is taken. */
1298 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1301 if (lod->lod_desc.ld_active_tgt_count < 2)
1308 * Allocate a striping using an algorithm with weights.
1310 * The function allocates OST objects to create a striping. The algorithm
1311 * used is based on weights (currently only using the free space), and it's
1312 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1313 * configuration (# of stripes, offset,
1314 * pool) is taken from the object and is prepared by the caller.
1315 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1316 * be met due to too few OSTs, then allocation fails. If the flag is
1317 * passed and less than 75% of the requested number of stripes can be
1318 * allocated, then allocation fails.
1319 * No concurrent allocation is allowed on the object and this must be
1320 * ensured by the caller. All the internal structures are protected by the
1322 * The algorithm has two steps: find available OSTs and calucate their weights,
1323 * then select the OSTs the weights used as the probability. An OST with a
1324 * higher weight is proportionately more likely to be selected than one with
1327 * \param[in] env execution environment for this thread
1328 * \param[in] lo LOD object
1329 * \param[out] stripe striping created
1330 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1331 * \param[in] th transaction handle
1333 * \retval 0 on success
1334 * \retval -E2BIG if no enough OSTs are found
1335 * \retval -EINVAL requested offset is invalid
1336 * \retval negative negated errno on error
1338 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1339 struct dt_object **stripe, int flags,
1342 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1343 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1344 struct lod_tgt_desc *ost;
1345 struct dt_object *o;
1346 __u64 total_weight = 0;
1349 __u32 nfound, good_osts;
1350 __u32 stripe_cnt = lo->ldo_stripenr;
1351 __u32 stripe_cnt_min;
1352 struct pool_desc *pool = NULL;
1353 struct ost_pool *osts;
1356 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
1357 if (stripe_cnt_min < 1)
1361 pool = lod_find_pool(m, lo->ldo_pool);
1364 down_read(&pool_tgt_rw_sem(pool));
1365 osts = &(pool->pool_obds);
1367 osts = &(m->lod_pool_info);
1370 /* Detect -EAGAIN early, before expensive lock is taken. */
1371 if (!lod_qos_is_usable(m))
1372 GOTO(out_nolock, rc = -EAGAIN);
1374 /* Do actual allocation, use write lock here. */
1375 down_write(&m->lod_qos.lq_rw_sem);
1378 * Check again, while we were sleeping on @lq_rw_sem things could
1381 if (!lod_qos_is_usable(m))
1382 GOTO(out, rc = -EAGAIN);
1384 rc = lod_qos_calc_ppo(m);
1388 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1393 /* Find all the OSTs that are valid stripe candidates */
1394 for (i = 0; i < osts->op_count; i++) {
1395 if (!cfs_bitmap_check(m->lod_ost_bitmap, osts->op_array[i]))
1398 rc = lod_statfs_and_check(env, m, osts->op_array[i], sfs);
1400 /* this OSP doesn't feel well */
1407 if (lod_qos_dev_is_full(sfs))
1410 /* Fail Check before osc_precreate() is called
1411 so we can only 'fail' single OSC. */
1412 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1413 osts->op_array[i] == 0)
1416 ost = OST_TGT(m,osts->op_array[i]);
1417 ost->ltd_qos.ltq_usable = 1;
1418 lod_qos_calc_weight(m, osts->op_array[i]);
1419 total_weight += ost->ltd_qos.ltq_weight;
1424 QOS_DEBUG("found %d good osts\n", good_osts);
1426 if (good_osts < stripe_cnt_min)
1427 GOTO(out, rc = -EAGAIN);
1429 /* We have enough osts */
1430 if (good_osts < stripe_cnt)
1431 stripe_cnt = good_osts;
1433 /* Find enough OSTs with weighted random allocation. */
1435 while (nfound < stripe_cnt) {
1436 __u64 rand, cur_weight;
1442 #if BITS_PER_LONG == 32
1443 rand = cfs_rand() % (unsigned)total_weight;
1444 /* If total_weight > 32-bit, first generate the high
1445 * 32 bits of the random number, then add in the low
1446 * 32 bits (truncated to the upper limit, if needed) */
1447 if (total_weight > 0xffffffffULL)
1448 rand = (__u64)(cfs_rand() %
1449 (unsigned)(total_weight >> 32)) << 32;
1453 if (rand == (total_weight & 0xffffffff00000000ULL))
1454 rand |= cfs_rand() % (unsigned)total_weight;
1459 rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1466 /* On average, this will hit larger-weighted osts more often.
1467 0-weight osts will always get used last (only when rand=0) */
1468 for (i = 0; i < osts->op_count; i++) {
1469 __u32 idx = osts->op_array[i];
1471 if (!cfs_bitmap_check(m->lod_ost_bitmap, idx))
1474 ost = OST_TGT(m,idx);
1476 if (!ost->ltd_qos.ltq_usable)
1479 cur_weight += ost->ltd_qos.ltq_weight;
1480 QOS_DEBUG("stripe_cnt=%d nfound=%d cur_weight="LPU64
1481 " rand="LPU64" total_weight="LPU64"\n",
1482 stripe_cnt, nfound, cur_weight, rand,
1485 if (cur_weight < rand)
1488 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1491 * do not put >1 objects on a single OST
1493 if (lod_qos_is_ost_used(env, idx, nfound))
1495 lod_qos_ost_in_use(env, nfound, idx);
1497 o = lod_qos_declare_object_on(env, m, idx, th);
1499 QOS_DEBUG("can't declare object on #%u: %d\n",
1500 idx, (int) PTR_ERR(o));
1503 stripe[nfound++] = o;
1504 lod_qos_used(m, osts, idx, &total_weight);
1510 /* no OST found on this iteration, give up */
1515 if (unlikely(nfound != stripe_cnt)) {
1517 * when the decision to use weighted algorithm was made
1518 * we had enough appropriate OSPs, but this state can
1519 * change anytime (no space on OST, broken connection, etc)
1520 * so it's possible OSP won't be able to provide us with
1521 * an object due to just changed state
1523 LCONSOLE_INFO("wanted %d, found %d\n", stripe_cnt, nfound);
1524 for (i = 0; i < nfound; i++) {
1525 LASSERT(stripe[i] != NULL);
1526 lu_object_put(env, &stripe[i]->do_lu);
1530 /* makes sense to rebalance next time */
1531 m->lod_qos.lq_dirty = 1;
1532 m->lod_qos.lq_same_space = 0;
1538 up_write(&m->lod_qos.lq_rw_sem);
1542 up_read(&pool_tgt_rw_sem(pool));
1543 /* put back ref got by lod_find_pool() */
1544 lod_pool_putref(pool);
1551 * Find largest stripe count the caller can use.
1553 * Find the maximal possible stripe count not greater than \a stripe_count.
1554 * Sometimes suggested stripecount can't be reached for a number of reasons:
1555 * lack of enough active OSTs or the backend does not support EAs that large.
1556 * If the passed one is 0, then the filesystem's default one is used.
1558 * \param[in] lod LOD device
1559 * \param[in] magic the format if striping
1560 * \param[in] stripe_count count the caller would like to use
1562 * \retval the maximum usable stripe count
1564 static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
1567 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1570 stripe_count = lod->lod_desc.ld_default_stripe_count;
1571 if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1572 stripe_count = lod->lod_desc.ld_active_tgt_count;
1576 /* stripe count is based on whether OSD can handle larger EA sizes */
1577 if (lod->lod_osd_max_easize > 0)
1578 max_stripes = lov_mds_md_max_stripe_count(
1579 lod->lod_osd_max_easize, magic);
1581 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1585 * Create in-core respresentation for a fully-defined striping
1587 * When the caller passes a fully-defined striping (i.e. everything including
1588 * OST object FIDs are defined), then we still need to instantiate LU-cache
1589 * with the objects representing the stripes defined. This function completes
1592 * \param[in] env execution environment for this thread
1593 * \param[in] mo LOD object
1594 * \param[in] buf buffer containing the striping
1596 * \retval 0 on success
1597 * \retval negative negated errno on error
1599 static int lod_use_defined_striping(const struct lu_env *env,
1600 struct lod_object *mo,
1601 const struct lu_buf *buf)
1603 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1604 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1605 struct lov_ost_data_v1 *objs;
1610 magic = le32_to_cpu(v1->lmm_magic);
1611 if (magic == LOV_MAGIC_V1_DEF) {
1612 magic = LOV_MAGIC_V1;
1613 objs = &v1->lmm_objects[0];
1614 } else if (magic == LOV_MAGIC_V3_DEF) {
1615 magic = LOV_MAGIC_V3;
1616 objs = &v3->lmm_objects[0];
1617 lod_object_set_pool(mo, v3->lmm_pool_name);
1619 GOTO(out, rc = -EINVAL);
1622 mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
1623 mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1624 mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
1625 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1627 /* fixup for released file before object initialization */
1628 if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1629 mo->ldo_released_stripenr = mo->ldo_stripenr;
1630 mo->ldo_stripenr = 0;
1633 LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
1635 if (mo->ldo_stripenr > 0)
1636 rc = lod_initialize_objects(env, mo, objs);
1643 * Parse suggested striping configuration.
1645 * The caller gets a suggested striping configuration from a number of sources
1646 * including per-directory default and applications. Then it needs to verify
1647 * the suggested striping is valid, apply missing bits and store the resulting
1648 * configuration in the object to be used by the allocator later. Must not be
1649 * called concurrently against the same object. It's OK to provide a
1650 * fully-defined striping.
1652 * \param[in] env execution environment for this thread
1653 * \param[in] lo LOD object
1654 * \param[in] buf buffer containing the striping
1656 * \retval 0 on success
1657 * \retval negative negated errno on error
1659 static int lod_qos_parse_config(const struct lu_env *env,
1660 struct lod_object *lo,
1661 const struct lu_buf *buf)
1663 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1664 struct lov_user_md_v1 *v1 = NULL;
1665 struct lov_user_md_v3 *v3 = NULL;
1666 char *pool_name = NULL;
1672 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1677 magic = v1->lmm_magic;
1679 if (unlikely(magic == LOV_MAGIC_V1_DEF || magic == LOV_MAGIC_V3_DEF)) {
1680 /* try to use as fully defined striping */
1681 rc = lod_use_defined_striping(env, lo, buf);
1686 case __swab32(LOV_USER_MAGIC_V1):
1687 lustre_swab_lov_user_md_v1(v1);
1688 magic = v1->lmm_magic;
1690 case LOV_USER_MAGIC_V1:
1694 case __swab32(LOV_USER_MAGIC_V3):
1695 lustre_swab_lov_user_md_v3(v3);
1696 magic = v3->lmm_magic;
1698 case LOV_USER_MAGIC_V3:
1700 pool_name = v3->lmm_pool_name;
1703 case __swab32(LOV_USER_MAGIC_SPECIFIC):
1704 lustre_swab_lov_user_md_v3(v3);
1705 lustre_swab_lov_user_md_objects(v3->lmm_objects,
1706 v3->lmm_stripe_count);
1707 magic = v3->lmm_magic;
1709 case LOV_USER_MAGIC_SPECIFIC:
1710 if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
1711 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
1712 if (v3->lmm_pool_name[0] != '\0')
1713 pool_name = v3->lmm_pool_name;
1714 size = lov_user_md_size(v3->lmm_stripe_count,
1715 LOV_USER_MAGIC_SPECIFIC);
1719 CERROR("%s: unrecognized magic %X\n",
1720 lod2obd(d)->obd_name, magic);
1724 if (unlikely(buf->lb_len < size)) {
1725 CERROR("%s: wrong size: %zd, expect: %u\n",
1726 lod2obd(d)->obd_name, buf->lb_len, size);
1730 lustre_print_user_md(D_OTHER, v1, "parse config");
1732 v1->lmm_magic = magic;
1733 if (v1->lmm_pattern == 0)
1734 v1->lmm_pattern = LOV_PATTERN_RAID0;
1735 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
1736 CERROR("%s: invalid pattern: %x\n",
1737 lod2obd(d)->obd_name, v1->lmm_pattern);
1740 lo->ldo_pattern = v1->lmm_pattern;
1742 if (v1->lmm_stripe_size > 0)
1743 lo->ldo_stripe_size = v1->lmm_stripe_size;
1745 if (lo->ldo_stripe_size & (LOV_MIN_STRIPE_SIZE - 1))
1746 lo->ldo_stripe_size = LOV_MIN_STRIPE_SIZE;
1748 if (v1->lmm_stripe_count > 0)
1749 lo->ldo_stripenr = v1->lmm_stripe_count;
1751 lo->ldo_def_stripe_offset = v1->lmm_stripe_offset;
1753 lod_object_set_pool(lo, NULL);
1754 if (pool_name != NULL) {
1755 struct pool_desc *pool;
1757 /* In the function below, .hs_keycmp resolves to
1758 * pool_hashkey_keycmp() */
1759 /* coverity[overrun-buffer-val] */
1760 pool = lod_find_pool(d, pool_name);
1762 if (lo->ldo_def_stripe_offset != LOV_OFFSET_DEFAULT) {
1763 rc = lod_check_index_in_pool(
1764 lo->ldo_def_stripe_offset, pool);
1766 lod_pool_putref(pool);
1767 CERROR("%s: invalid offset, %u\n",
1768 lod2obd(d)->obd_name,
1769 lo->ldo_def_stripe_offset);
1774 if (lo->ldo_stripenr > pool_tgt_count(pool))
1775 lo->ldo_stripenr = pool_tgt_count(pool);
1777 lod_pool_putref(pool);
1780 lod_object_set_pool(lo, pool_name);
1783 /* fixup for released file */
1784 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1785 lo->ldo_released_stripenr = lo->ldo_stripenr;
1786 lo->ldo_stripenr = 0;
1793 * Create a striping for an obejct.
1795 * The function creates a new striping for the object. A buffer containing
1796 * configuration hints can be provided optionally. The function tries QoS
1797 * algorithm first unless free space is distributed evenly among OSTs, but
1798 * by default RR algorithm is preferred due to internal concurrency (QoS is
1799 * serialized). The caller must ensure no concurrent calls to the function
1800 * are made against the same object.
1802 * \param[in] env execution environment for this thread
1803 * \param[in] lo LOD object
1804 * \param[in] attr attributes OST objects will be declared with
1805 * \param[in] buf suggested striping configuration or NULL
1806 * \param[in] th transaction handle
1808 * \retval 0 on success
1809 * \retval negative negated errno on error
1811 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
1812 struct lu_attr *attr, const struct lu_buf *buf,
1815 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1816 struct dt_object **stripe;
1818 int flag = LOV_USES_ASSIGNED_STRIPE;
1824 /* no OST available */
1825 /* XXX: should we be waiting a bit to prevent failures during
1826 * cluster initialization? */
1827 if (d->lod_ostnr == 0)
1828 GOTO(out, rc = -EIO);
1831 * by this time, the object's ldo_stripenr and ldo_stripe_size
1832 * contain default value for striping: taken from the parent
1833 * or from filesystem defaults
1835 * in case the caller is passing lovea with new striping config,
1836 * we may need to parse lovea and apply new configuration
1838 rc = lod_qos_parse_config(env, lo, buf);
1842 /* A released file is being created */
1843 if (lo->ldo_stripenr == 0)
1846 if (likely(lo->ldo_stripe == NULL)) {
1847 struct lov_user_md *lum = NULL;
1850 * no striping has been created so far
1852 LASSERT(lo->ldo_stripenr > 0);
1854 * statfs and check OST targets now, since ld_active_tgt_count
1855 * could be changed if some OSTs are [de]activated manually.
1857 lod_qos_statfs_update(env, d);
1858 lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
1861 stripe_len = lo->ldo_stripenr;
1862 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
1864 GOTO(out, rc = -ENOMEM);
1866 lod_getref(&d->lod_ost_descs);
1867 /* XXX: support for non-0 files w/o objects */
1868 CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
1869 d->lod_desc.ld_tgt_count, stripe_len);
1871 if (buf != NULL && buf->lb_buf != NULL)
1874 if (lum != NULL && lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
1875 rc = lod_alloc_ost_list(env, lo, stripe, lum, th);
1876 } else if (lo->ldo_def_stripe_offset == LOV_OFFSET_DEFAULT) {
1877 rc = lod_alloc_qos(env, lo, stripe, flag, th);
1879 rc = lod_alloc_rr(env, lo, stripe, flag, th);
1881 rc = lod_alloc_specific(env, lo, stripe, flag, th);
1883 lod_putref(d, &d->lod_ost_descs);
1886 for (i = 0; i < stripe_len; i++)
1887 if (stripe[i] != NULL)
1888 lu_object_put(env, &stripe[i]->do_lu);
1890 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
1891 lo->ldo_stripenr = 0;
1893 lo->ldo_stripe = stripe;
1894 lo->ldo_stripes_allocated = stripe_len;
1898 * lod_qos_parse_config() found supplied buf as a predefined
1899 * striping (not a hint), so it allocated all the object
1900 * now we need to create them
1902 for (i = 0; i < lo->ldo_stripenr; i++) {
1903 struct dt_object *o;
1905 o = lo->ldo_stripe[i];
1908 rc = dt_declare_create(env, o, attr, NULL, NULL, th);
1910 CERROR("can't declare create: %d\n", rc);