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 striping on a predefined set of OSTs.
1027 * Allocates new striping starting from OST provided lo->ldo_def_stripe_offset.
1028 * Full OSTs are not considered. The exact order of OSTs is not important and
1029 * varies depending on OST status. The allocation procedure prefers the targets
1030 * with precreated objects ready. The number of stripes needed and stripe
1031 * offset are taken from the object. If that number can not be met, then the
1032 * function returns a failure and then it's the caller's responsibility to
1033 * release the stripes allocated. All the internal structures are protected,
1034 * but no concurrent allocation is allowed on the same objects.
1036 * \param[in] env execution environment for this thread
1037 * \param[in] lo LOD object
1038 * \param[out] stripe striping created
1039 * \param[in] flags not used
1040 * \param[in] th transaction handle
1042 * \retval 0 on success
1043 * \retval -E2BIG if no enough OSTs are found
1044 * \retval -EINVAL requested offset is invalid
1045 * \retval negative negated errno on error
1047 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
1048 struct dt_object **stripe, int flags,
1051 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1052 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1053 struct dt_object *o;
1055 unsigned int i, array_idx, ost_count;
1056 int rc, stripe_num = 0;
1058 struct pool_desc *pool = NULL;
1059 struct ost_pool *osts;
1062 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1067 pool = lod_find_pool(m, lo->ldo_pool);
1070 down_read(&pool_tgt_rw_sem(pool));
1071 osts = &(pool->pool_obds);
1073 osts = &(m->lod_pool_info);
1076 ost_count = osts->op_count;
1079 /* search loi_ost_idx in ost array */
1081 for (i = 0; i < ost_count; i++) {
1082 if (osts->op_array[i] == lo->ldo_def_stripe_offset) {
1087 if (i == ost_count) {
1088 CERROR("Start index %d not found in pool '%s'\n",
1089 lo->ldo_def_stripe_offset,
1090 lo->ldo_pool ? lo->ldo_pool : "");
1091 GOTO(out, rc = -EINVAL);
1094 for (i = 0; i < ost_count;
1095 i++, array_idx = (array_idx + 1) % ost_count) {
1096 ost_idx = osts->op_array[array_idx];
1098 if (!cfs_bitmap_check(m->lod_ost_bitmap, ost_idx))
1101 /* Fail Check before osc_precreate() is called
1102 so we can only 'fail' single OSC. */
1103 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
1107 * do not put >1 objects on a single OST
1109 if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
1112 /* Drop slow OSCs if we can, but not for requested start idx.
1114 * This means "if OSC is slow and it is not the requested
1115 * start OST, then it can be skipped, otherwise skip it only
1116 * if it is inactive/recovering/out-of-space." */
1118 rc = lod_statfs_and_check(env, m, ost_idx, sfs);
1120 /* this OSP doesn't feel well */
1125 * We expect number of precreated objects in f_ffree at
1126 * the first iteration, skip OSPs with no objects ready
1127 * don't apply this logic to OST specified with stripe_offset
1129 if (i != 0 && sfs->os_fprecreated == 0 && speed == 0)
1132 o = lod_qos_declare_object_on(env, m, ost_idx, th);
1134 CDEBUG(D_OTHER, "can't declare new object on #%u: %d\n",
1135 ost_idx, (int) PTR_ERR(o));
1140 * We've successfuly declared (reserved) an object
1142 lod_qos_ost_in_use(env, stripe_num, ost_idx);
1143 stripe[stripe_num] = o;
1146 /* We have enough stripes */
1147 if (stripe_num == lo->ldo_stripenr)
1151 /* Try again, allowing slower OSCs */
1156 /* If we were passed specific striping params, then a failure to
1157 * meet those requirements is an error, since we can't reallocate
1158 * that memory (it might be part of a larger array or something).
1160 * We can only get here if lsm_stripe_count was originally > 1.
1162 CERROR("can't lstripe objid "DFID": have %d want %u\n",
1163 PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
1168 up_read(&pool_tgt_rw_sem(pool));
1169 /* put back ref got by lod_find_pool() */
1170 lod_pool_putref(pool);
1177 * Check whether QoS allocation should be used.
1179 * A simple helper to decide when QoS allocation should be used:
1180 * if it's just a single available target or the used space is
1181 * evenly distributed among the targets at the moment, then QoS
1182 * allocation algorithm should not be used.
1184 * \param[in] lod LOD device
1186 * \retval 0 should not be used
1187 * \retval 1 should be used
1189 static inline int lod_qos_is_usable(struct lod_device *lod)
1192 /* to be able to debug QoS code */
1196 /* Detect -EAGAIN early, before expensive lock is taken. */
1197 if (!lod->lod_qos.lq_dirty && lod->lod_qos.lq_same_space)
1200 if (lod->lod_desc.ld_active_tgt_count < 2)
1207 * Allocate a striping using an algorithm with weights.
1209 * The function allocates OST objects to create a striping. The algorithm
1210 * used is based on weights (currently only using the free space), and it's
1211 * trying to ensure the space is used evenly by OSTs and OSSs. The striping
1212 * configuration (# of stripes, offset,
1213 * pool) is taken from the object and is prepared by the caller.
1214 * If LOV_USES_DEFAULT_STRIPE is not passed and prepared configuration can't
1215 * be met due to too few OSTs, then allocation fails. If the flag is
1216 * passed and less than 75% of the requested number of stripes can be
1217 * allocated, then allocation fails.
1218 * No concurrent allocation is allowed on the object and this must be
1219 * ensured by the caller. All the internal structures are protected by the
1221 * The algorithm has two steps: find available OSTs and calucate their weights,
1222 * then select the OSTs the weights used as the probability. An OST with a
1223 * higher weight is proportionately more likely to be selected than one with
1226 * \param[in] env execution environment for this thread
1227 * \param[in] lo LOD object
1228 * \param[out] stripe striping created
1229 * \param[in] flags 0 or LOV_USES_DEFAULT_STRIPE
1230 * \param[in] th transaction handle
1232 * \retval 0 on success
1233 * \retval -E2BIG if no enough OSTs are found
1234 * \retval -EINVAL requested offset is invalid
1235 * \retval negative negated errno on error
1237 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
1238 struct dt_object **stripe, int flags,
1241 struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1242 struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
1243 struct lod_tgt_desc *ost;
1244 struct dt_object *o;
1245 __u64 total_weight = 0;
1248 __u32 nfound, good_osts;
1249 __u32 stripe_cnt = lo->ldo_stripenr;
1250 __u32 stripe_cnt_min;
1251 struct pool_desc *pool = NULL;
1252 struct ost_pool *osts;
1255 stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
1256 if (stripe_cnt_min < 1)
1260 pool = lod_find_pool(m, lo->ldo_pool);
1263 down_read(&pool_tgt_rw_sem(pool));
1264 osts = &(pool->pool_obds);
1266 osts = &(m->lod_pool_info);
1269 /* Detect -EAGAIN early, before expensive lock is taken. */
1270 if (!lod_qos_is_usable(m))
1271 GOTO(out_nolock, rc = -EAGAIN);
1273 /* Do actual allocation, use write lock here. */
1274 down_write(&m->lod_qos.lq_rw_sem);
1277 * Check again, while we were sleeping on @lq_rw_sem things could
1280 if (!lod_qos_is_usable(m))
1281 GOTO(out, rc = -EAGAIN);
1283 rc = lod_qos_calc_ppo(m);
1287 rc = lod_qos_ost_in_use_clear(env, lo->ldo_stripenr);
1292 /* Find all the OSTs that are valid stripe candidates */
1293 for (i = 0; i < osts->op_count; i++) {
1294 if (!cfs_bitmap_check(m->lod_ost_bitmap, osts->op_array[i]))
1297 rc = lod_statfs_and_check(env, m, osts->op_array[i], sfs);
1299 /* this OSP doesn't feel well */
1306 if (lod_qos_dev_is_full(sfs))
1309 /* Fail Check before osc_precreate() is called
1310 so we can only 'fail' single OSC. */
1311 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) &&
1312 osts->op_array[i] == 0)
1315 ost = OST_TGT(m,osts->op_array[i]);
1316 ost->ltd_qos.ltq_usable = 1;
1317 lod_qos_calc_weight(m, osts->op_array[i]);
1318 total_weight += ost->ltd_qos.ltq_weight;
1323 QOS_DEBUG("found %d good osts\n", good_osts);
1325 if (good_osts < stripe_cnt_min)
1326 GOTO(out, rc = -EAGAIN);
1328 /* We have enough osts */
1329 if (good_osts < stripe_cnt)
1330 stripe_cnt = good_osts;
1332 /* Find enough OSTs with weighted random allocation. */
1334 while (nfound < stripe_cnt) {
1335 __u64 rand, cur_weight;
1341 #if BITS_PER_LONG == 32
1342 rand = cfs_rand() % (unsigned)total_weight;
1343 /* If total_weight > 32-bit, first generate the high
1344 * 32 bits of the random number, then add in the low
1345 * 32 bits (truncated to the upper limit, if needed) */
1346 if (total_weight > 0xffffffffULL)
1347 rand = (__u64)(cfs_rand() %
1348 (unsigned)(total_weight >> 32)) << 32;
1352 if (rand == (total_weight & 0xffffffff00000000ULL))
1353 rand |= cfs_rand() % (unsigned)total_weight;
1358 rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
1365 /* On average, this will hit larger-weighted osts more often.
1366 0-weight osts will always get used last (only when rand=0) */
1367 for (i = 0; i < osts->op_count; i++) {
1368 __u32 idx = osts->op_array[i];
1370 if (!cfs_bitmap_check(m->lod_ost_bitmap, idx))
1373 ost = OST_TGT(m,idx);
1375 if (!ost->ltd_qos.ltq_usable)
1378 cur_weight += ost->ltd_qos.ltq_weight;
1379 QOS_DEBUG("stripe_cnt=%d nfound=%d cur_weight="LPU64
1380 " rand="LPU64" total_weight="LPU64"\n",
1381 stripe_cnt, nfound, cur_weight, rand,
1384 if (cur_weight < rand)
1387 QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
1390 * do not put >1 objects on a single OST
1392 if (lod_qos_is_ost_used(env, idx, nfound))
1394 lod_qos_ost_in_use(env, nfound, idx);
1396 o = lod_qos_declare_object_on(env, m, idx, th);
1398 QOS_DEBUG("can't declare object on #%u: %d\n",
1399 idx, (int) PTR_ERR(o));
1402 stripe[nfound++] = o;
1403 lod_qos_used(m, osts, idx, &total_weight);
1409 /* no OST found on this iteration, give up */
1414 if (unlikely(nfound != stripe_cnt)) {
1416 * when the decision to use weighted algorithm was made
1417 * we had enough appropriate OSPs, but this state can
1418 * change anytime (no space on OST, broken connection, etc)
1419 * so it's possible OSP won't be able to provide us with
1420 * an object due to just changed state
1422 LCONSOLE_INFO("wanted %d, found %d\n", stripe_cnt, nfound);
1423 for (i = 0; i < nfound; i++) {
1424 LASSERT(stripe[i] != NULL);
1425 lu_object_put(env, &stripe[i]->do_lu);
1429 /* makes sense to rebalance next time */
1430 m->lod_qos.lq_dirty = 1;
1431 m->lod_qos.lq_same_space = 0;
1437 up_write(&m->lod_qos.lq_rw_sem);
1441 up_read(&pool_tgt_rw_sem(pool));
1442 /* put back ref got by lod_find_pool() */
1443 lod_pool_putref(pool);
1450 * Find largest stripe count the caller can use.
1452 * Find the maximal possible stripe count not greater than \a stripe_count.
1453 * Sometimes suggested stripecount can't be reached for a number of reasons:
1454 * lack of enough active OSTs or the backend does not support EAs that large.
1455 * If the passed one is 0, then the filesystem's default one is used.
1457 * \param[in] lod LOD device
1458 * \param[in] magic the format if striping
1459 * \param[in] stripe_count count the caller would like to use
1461 * \retval the maximum usable stripe count
1463 static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
1466 __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
1469 stripe_count = lod->lod_desc.ld_default_stripe_count;
1470 if (stripe_count > lod->lod_desc.ld_active_tgt_count)
1471 stripe_count = lod->lod_desc.ld_active_tgt_count;
1475 /* stripe count is based on whether OSD can handle larger EA sizes */
1476 if (lod->lod_osd_max_easize > 0)
1477 max_stripes = lov_mds_md_max_stripe_count(
1478 lod->lod_osd_max_easize, magic);
1480 return (stripe_count < max_stripes) ? stripe_count : max_stripes;
1484 * Create in-core respresentation for a fully-defined striping
1486 * When the caller passes a fully-defined striping (i.e. everything including
1487 * OST object FIDs are defined), then we still need to instantiate LU-cache
1488 * with the objects representing the stripes defined. This function completes
1491 * \param[in] env execution environment for this thread
1492 * \param[in] mo LOD object
1493 * \param[in] buf buffer containing the striping
1495 * \retval 0 on success
1496 * \retval negative negated errno on error
1498 static int lod_use_defined_striping(const struct lu_env *env,
1499 struct lod_object *mo,
1500 const struct lu_buf *buf)
1502 struct lov_mds_md_v1 *v1 = buf->lb_buf;
1503 struct lov_mds_md_v3 *v3 = buf->lb_buf;
1504 struct lov_ost_data_v1 *objs;
1509 magic = le32_to_cpu(v1->lmm_magic);
1510 if (magic == LOV_MAGIC_V1_DEF) {
1511 magic = LOV_MAGIC_V1;
1512 objs = &v1->lmm_objects[0];
1513 } else if (magic == LOV_MAGIC_V3_DEF) {
1514 magic = LOV_MAGIC_V3;
1515 objs = &v3->lmm_objects[0];
1516 lod_object_set_pool(mo, v3->lmm_pool_name);
1518 GOTO(out, rc = -EINVAL);
1521 mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
1522 mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
1523 mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
1524 mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
1526 /* fixup for released file before object initialization */
1527 if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1528 mo->ldo_released_stripenr = mo->ldo_stripenr;
1529 mo->ldo_stripenr = 0;
1532 LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
1534 if (mo->ldo_stripenr > 0)
1535 rc = lod_initialize_objects(env, mo, objs);
1542 * Parse suggested striping configuration.
1544 * The caller gets a suggested striping configuration from a number of sources
1545 * including per-directory default and applications. Then it needs to verify
1546 * the suggested striping is valid, apply missing bits and store the resulting
1547 * configuration in the object to be used by the allocator later. Must not be
1548 * called concurrently against the same object. It's OK to provide a
1549 * fully-defined striping.
1551 * \param[in] env execution environment for this thread
1552 * \param[in] lo LOD object
1553 * \param[in] buf buffer containing the striping
1555 * \retval 0 on success
1556 * \retval negative negated errno on error
1558 static int lod_qos_parse_config(const struct lu_env *env,
1559 struct lod_object *lo,
1560 const struct lu_buf *buf)
1562 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1563 struct lov_user_md_v1 *v1 = NULL;
1564 struct lov_user_md_v3 *v3 = NULL;
1565 struct pool_desc *pool;
1570 if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
1574 magic = v1->lmm_magic;
1576 if (magic == __swab32(LOV_USER_MAGIC_V1)) {
1577 lustre_swab_lov_user_md_v1(v1);
1578 magic = v1->lmm_magic;
1579 } else if (magic == __swab32(LOV_USER_MAGIC_V3)) {
1581 lustre_swab_lov_user_md_v3(v3);
1582 magic = v3->lmm_magic;
1585 if (unlikely(magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)) {
1586 /* try to use as fully defined striping */
1587 rc = lod_use_defined_striping(env, lo, buf);
1591 if (unlikely(buf->lb_len < sizeof(*v1))) {
1592 CERROR("wrong size: %u\n", (unsigned) buf->lb_len);
1596 v1->lmm_magic = magic;
1597 if (v1->lmm_pattern == 0)
1598 v1->lmm_pattern = LOV_PATTERN_RAID0;
1599 if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
1600 CERROR("invalid pattern: %x\n", v1->lmm_pattern);
1603 lo->ldo_pattern = v1->lmm_pattern;
1605 if (v1->lmm_stripe_size)
1606 lo->ldo_stripe_size = v1->lmm_stripe_size;
1607 if (lo->ldo_stripe_size & (LOV_MIN_STRIPE_SIZE - 1))
1608 lo->ldo_stripe_size = LOV_MIN_STRIPE_SIZE;
1610 if (v1->lmm_stripe_count)
1611 lo->ldo_stripenr = v1->lmm_stripe_count;
1613 if ((v1->lmm_stripe_offset >= d->lod_desc.ld_tgt_count) &&
1614 (v1->lmm_stripe_offset != (typeof(v1->lmm_stripe_offset))(-1))) {
1615 CERROR("invalid offset: %x\n", v1->lmm_stripe_offset);
1618 lo->ldo_def_stripe_offset = v1->lmm_stripe_offset;
1620 CDEBUG(D_OTHER, "lsm: %u size, %u stripes, %u offset\n",
1621 v1->lmm_stripe_size, v1->lmm_stripe_count,
1622 v1->lmm_stripe_offset);
1624 if (v1->lmm_magic == LOV_MAGIC_V3) {
1625 if (buf->lb_len < sizeof(*v3)) {
1626 CERROR("wrong size: %u\n", (unsigned) buf->lb_len);
1631 lod_object_set_pool(lo, v3->lmm_pool_name);
1633 /* In the function below, .hs_keycmp resolves to
1634 * pool_hashkey_keycmp() */
1635 /* coverity[overrun-buffer-val] */
1636 pool = lod_find_pool(d, v3->lmm_pool_name);
1638 if (lo->ldo_def_stripe_offset !=
1639 (typeof(v1->lmm_stripe_offset))(-1)) {
1640 rc = lo->ldo_def_stripe_offset;
1641 rc = lod_check_index_in_pool(rc, pool);
1643 lod_pool_putref(pool);
1644 CERROR("invalid offset\n");
1649 if (lo->ldo_stripenr > pool_tgt_count(pool))
1650 lo->ldo_stripenr= pool_tgt_count(pool);
1652 lod_pool_putref(pool);
1655 lod_object_set_pool(lo, NULL);
1657 /* fixup for released file */
1658 if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
1659 lo->ldo_released_stripenr = lo->ldo_stripenr;
1660 lo->ldo_stripenr = 0;
1667 * Create a striping for an obejct.
1669 * The function creates a new striping for the object. A buffer containing
1670 * configuration hints can be provided optionally. The function tries QoS
1671 * algorithm first unless free space is distributed evenly among OSTs, but
1672 * by default RR algorithm is preferred due to internal concurrency (QoS is
1673 * serialized). The caller must ensure no concurrent calls to the function
1674 * are made against the same object.
1676 * \param[in] env execution environment for this thread
1677 * \param[in] lo LOD object
1678 * \param[in] attr attributes OST objects will be declared with
1679 * \param[in] buf suggested striping configuration or NULL
1680 * \param[in] th transaction handle
1682 * \retval 0 on success
1683 * \retval negative negated errno on error
1685 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
1686 struct lu_attr *attr, const struct lu_buf *buf,
1689 struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
1690 struct dt_object **stripe;
1692 int flag = LOV_USES_ASSIGNED_STRIPE;
1698 /* no OST available */
1699 /* XXX: should we be waiting a bit to prevent failures during
1700 * cluster initialization? */
1701 if (d->lod_ostnr == 0)
1702 GOTO(out, rc = -EIO);
1705 * by this time, the object's ldo_stripenr and ldo_stripe_size
1706 * contain default value for striping: taken from the parent
1707 * or from filesystem defaults
1709 * in case the caller is passing lovea with new striping config,
1710 * we may need to parse lovea and apply new configuration
1712 rc = lod_qos_parse_config(env, lo, buf);
1716 /* A released file is being created */
1717 if (lo->ldo_stripenr == 0)
1720 if (likely(lo->ldo_stripe == NULL)) {
1722 * no striping has been created so far
1724 LASSERT(lo->ldo_stripenr > 0);
1726 * statfs and check OST targets now, since ld_active_tgt_count
1727 * could be changed if some OSTs are [de]activated manually.
1729 lod_qos_statfs_update(env, d);
1730 lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
1733 stripe_len = lo->ldo_stripenr;
1734 OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
1736 GOTO(out, rc = -ENOMEM);
1738 lod_getref(&d->lod_ost_descs);
1739 /* XXX: support for non-0 files w/o objects */
1740 CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
1741 d->lod_desc.ld_tgt_count, stripe_len);
1742 if (lo->ldo_def_stripe_offset >= d->lod_desc.ld_tgt_count) {
1743 rc = lod_alloc_qos(env, lo, stripe, flag, th);
1745 rc = lod_alloc_rr(env, lo, stripe, flag, th);
1747 rc = lod_alloc_specific(env, lo, stripe, flag, th);
1749 lod_putref(d, &d->lod_ost_descs);
1752 for (i = 0; i < stripe_len; i++)
1753 if (stripe[i] != NULL)
1754 lu_object_put(env, &stripe[i]->do_lu);
1756 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
1757 lo->ldo_stripenr = 0;
1759 lo->ldo_stripe = stripe;
1760 lo->ldo_stripes_allocated = stripe_len;
1764 * lod_qos_parse_config() found supplied buf as a predefined
1765 * striping (not a hint), so it allocated all the object
1766 * now we need to create them
1768 for (i = 0; i < lo->ldo_stripenr; i++) {
1769 struct dt_object *o;
1771 o = lo->ldo_stripe[i];
1774 rc = dt_declare_create(env, o, attr, NULL, NULL, th);
1776 CERROR("can't declare create: %d\n", rc);