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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LOV
40 #include <libcfs/libcfs.h>
42 #include <liblustre.h>
45 #include <obd_class.h>
47 #include <lustre/lustre_idl.h>
48 #include "lov_internal.h"
50 /* #define QOS_DEBUG 1 */
53 #define TGT_BAVAIL(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail *\
54 lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize)
57 int qos_add_tgt(struct obd_device *obd, __u32 index)
59 struct lov_obd *lov = &obd->u.lov;
60 struct lov_qos_oss *oss, *temposs;
61 struct obd_export *exp = lov->lov_tgts[index]->ltd_exp;
62 int rc = 0, found = 0;
65 /* We only need this QOS struct on MDT, not clients - but we may not
66 * have registered the LOV's observer yet, so there's no way to know */
67 if (!exp || !exp->exp_connection) {
68 CERROR("Missing connection\n");
72 cfs_down_write(&lov->lov_qos.lq_rw_sem);
73 cfs_mutex_lock(&lov->lov_lock);
74 cfs_list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
75 if (obd_uuid_equals(&oss->lqo_uuid,
76 &exp->exp_connection->c_remote_uuid)) {
85 GOTO(out, rc = -ENOMEM);
86 memcpy(&oss->lqo_uuid,
87 &exp->exp_connection->c_remote_uuid,
88 sizeof(oss->lqo_uuid));
90 /* Assume we have to move this one */
91 cfs_list_del(&oss->lqo_oss_list);
95 lov->lov_tgts[index]->ltd_qos.ltq_oss = oss;
97 /* Add sorted by # of OSTs. Find the first entry that we're
99 cfs_list_for_each_entry(temposs, &lov->lov_qos.lq_oss_list,
101 if (oss->lqo_ost_count > temposs->lqo_ost_count)
104 /* ...and add before it. If we're the first or smallest, temposs
105 points to the list head, and we add to the end. */
106 cfs_list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
108 lov->lov_qos.lq_dirty = 1;
109 lov->lov_qos.lq_rr.lqr_dirty = 1;
111 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
112 obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
113 obd_uuid2str(&oss->lqo_uuid),
117 cfs_mutex_unlock(&lov->lov_lock);
118 cfs_up_write(&lov->lov_qos.lq_rw_sem);
122 int qos_del_tgt(struct obd_device *obd, struct lov_tgt_desc *tgt)
124 struct lov_obd *lov = &obd->u.lov;
125 struct lov_qos_oss *oss;
129 cfs_down_write(&lov->lov_qos.lq_rw_sem);
131 oss = tgt->ltd_qos.ltq_oss;
133 GOTO(out, rc = -ENOENT);
135 oss->lqo_ost_count--;
136 if (oss->lqo_ost_count == 0) {
137 CDEBUG(D_QOS, "removing OSS %s\n",
138 obd_uuid2str(&oss->lqo_uuid));
139 cfs_list_del(&oss->lqo_oss_list);
143 lov->lov_qos.lq_dirty = 1;
144 lov->lov_qos.lq_rr.lqr_dirty = 1;
146 cfs_up_write(&lov->lov_qos.lq_rw_sem);
150 /* Recalculate per-object penalties for OSSs and OSTs,
151 depends on size of each ost in an oss */
152 static int qos_calc_ppo(struct obd_device *obd)
154 struct lov_obd *lov = &obd->u.lov;
155 struct lov_qos_oss *oss;
156 __u64 ba_max, ba_min, temp;
158 int rc, i, prio_wide;
162 if (!lov->lov_qos.lq_dirty)
165 num_active = lov->desc.ld_active_tgt_count - 1;
167 GOTO(out, rc = -EAGAIN);
169 /* find bavail on each OSS */
170 cfs_list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
173 lov->lov_qos.lq_active_oss_count = 0;
175 /* How badly user wants to select osts "widely" (not recently chosen
176 and not on recent oss's). As opposed to "freely" (free space
178 prio_wide = 256 - lov->lov_qos.lq_prio_free;
180 ba_min = (__u64)(-1);
182 now = cfs_time_current_sec();
183 /* Calculate OST penalty per object */
184 /* (lov ref taken in alloc_qos) */
185 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
186 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
188 temp = TGT_BAVAIL(i);
191 ba_min = min(temp, ba_min);
192 ba_max = max(temp, ba_max);
194 /* Count the number of usable OSS's */
195 if (lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail == 0)
196 lov->lov_qos.lq_active_oss_count++;
197 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail += temp;
199 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
201 lov_do_div64(temp, num_active);
202 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj =
203 (temp * prio_wide) >> 8;
205 age = (now - lov->lov_tgts[i]->ltd_qos.ltq_used) >> 3;
206 if (lov->lov_qos.lq_reset || age > 32 * lov->desc.ld_qos_maxage)
207 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
208 else if (age > lov->desc.ld_qos_maxage)
209 /* Decay the penalty by half for every 8x the update
210 * interval that the device has been idle. That gives
211 * lots of time for the statfs information to be
212 * updated (which the penalty is only a proxy for),
213 * and avoids penalizing OSS/OSTs under light load. */
214 lov->lov_tgts[i]->ltd_qos.ltq_penalty >>=
215 (age / lov->desc.ld_qos_maxage);
218 num_active = lov->lov_qos.lq_active_oss_count - 1;
219 if (num_active < 1) {
220 /* If there's only 1 OSS, we can't penalize it, so instead
221 we have to double the OST penalty */
223 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
224 if (lov->lov_tgts[i] == NULL)
226 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
230 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
231 cfs_list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
232 temp = oss->lqo_bavail >> 1;
233 lov_do_div64(temp, oss->lqo_ost_count * num_active);
234 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
236 age = (now - oss->lqo_used) >> 3;
237 if (lov->lov_qos.lq_reset || age > 32 * lov->desc.ld_qos_maxage)
238 oss->lqo_penalty = 0;
239 else if (age > lov->desc.ld_qos_maxage)
240 /* Decay the penalty by half for every 8x the update
241 * interval that the device has been idle. That gives
242 * lots of time for the statfs information to be
243 * updated (which the penalty is only a proxy for),
244 * and avoids penalizing OSS/OSTs under light load. */
245 oss->lqo_penalty >>= (age / lov->desc.ld_qos_maxage);
248 lov->lov_qos.lq_dirty = 0;
249 lov->lov_qos.lq_reset = 0;
251 /* If each ost has almost same free space,
252 * do rr allocation for better creation performance */
253 lov->lov_qos.lq_same_space = 0;
254 if ((ba_max * (256 - lov->lov_qos.lq_threshold_rr)) >> 8 < ba_min) {
255 lov->lov_qos.lq_same_space = 1;
256 /* Reset weights for the next time we enter qos mode */
257 lov->lov_qos.lq_reset = 1;
262 if (!rc && lov->lov_qos.lq_same_space)
267 static int qos_calc_weight(struct lov_obd *lov, int i)
271 /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
272 temp = TGT_BAVAIL(i);
273 temp2 = lov->lov_tgts[i]->ltd_qos.ltq_penalty +
274 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty;
276 lov->lov_tgts[i]->ltd_qos.ltq_weight = 0;
278 lov->lov_tgts[i]->ltd_qos.ltq_weight = temp - temp2;
282 /* We just used this index for a stripe; adjust everyone's weights */
283 static int qos_used(struct lov_obd *lov, struct ost_pool *osts,
284 __u32 index, __u64 *total_wt)
286 struct lov_qos_oss *oss;
290 /* Don't allocate from this stripe anymore, until the next alloc_qos */
291 lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
293 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
295 /* Decay old penalty by half (we're adding max penalty, and don't
296 want it to run away.) */
297 lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
298 oss->lqo_penalty >>= 1;
300 /* mark the OSS and OST as recently used */
301 lov->lov_tgts[index]->ltd_qos.ltq_used =
302 oss->lqo_used = cfs_time_current_sec();
304 /* Set max penalties for this OST and OSS */
305 lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
306 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
307 lov->desc.ld_active_tgt_count;
308 oss->lqo_penalty += oss->lqo_penalty_per_obj *
309 lov->lov_qos.lq_active_oss_count;
311 /* Decrease all OSS penalties */
312 cfs_list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
313 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
314 oss->lqo_penalty = 0;
316 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
320 /* Decrease all OST penalties */
321 for (j = 0; j < osts->op_count; j++) {
324 i = osts->op_array[j];
325 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
327 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
328 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
329 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
331 lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
332 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
334 qos_calc_weight(lov, i);
336 /* Recalc the total weight of usable osts */
337 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
338 *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
341 CDEBUG(D_QOS, "recalc tgt %d usable=%d avail="LPU64
342 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
343 " ossp="LPU64" wt="LPU64"\n",
344 i, lov->lov_tgts[i]->ltd_qos.ltq_usable,
346 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj >> 10,
347 lov->lov_tgts[i]->ltd_qos.ltq_penalty >> 10,
348 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj>>10,
349 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty >> 10,
350 lov->lov_tgts[i]->ltd_qos.ltq_weight >> 10);
357 #define LOV_QOS_EMPTY ((__u32)-1)
358 /* compute optimal round-robin order, based on OSTs per OSS */
359 static int qos_calc_rr(struct lov_obd *lov, struct ost_pool *src_pool,
360 struct lov_qos_rr *lqr)
362 struct lov_qos_oss *oss;
363 unsigned placed, real_count;
367 if (!lqr->lqr_dirty) {
368 LASSERT(lqr->lqr_pool.op_size);
372 /* Do actual allocation. */
373 cfs_down_write(&lov->lov_qos.lq_rw_sem);
376 * Check again. While we were sleeping on @lq_rw_sem something could
379 if (!lqr->lqr_dirty) {
380 LASSERT(lqr->lqr_pool.op_size);
381 cfs_up_write(&lov->lov_qos.lq_rw_sem);
385 real_count = src_pool->op_count;
387 /* Zero the pool array */
388 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
389 deleting from the pool. The lq_rw_sem insures that nobody else
391 lqr->lqr_pool.op_count = real_count;
392 rc = lov_ost_pool_extend(&lqr->lqr_pool, real_count);
394 cfs_up_write(&lov->lov_qos.lq_rw_sem);
397 for (i = 0; i < lqr->lqr_pool.op_count; i++)
398 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
400 /* Place all the OSTs from 1 OSS at the same time. */
402 cfs_list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
404 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
405 if (lov->lov_tgts[src_pool->op_array[i]] &&
406 (lov->lov_tgts[src_pool->op_array[i]]->ltd_qos.ltq_oss == oss)) {
407 /* Evenly space these OSTs across arrayspace */
408 int next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
409 while (lqr->lqr_pool.op_array[next] !=
411 next = (next + 1) % lqr->lqr_pool.op_count;
412 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
420 cfs_up_write(&lov->lov_qos.lq_rw_sem);
422 if (placed != real_count) {
423 /* This should never happen */
424 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
425 "round-robin list (%d of %d).\n",
427 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
428 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
429 lqr->lqr_pool.op_array[i]);
436 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
437 LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i,
438 lqr->lqr_pool.op_array[i]);
446 void qos_shrink_lsm(struct lov_request_set *set)
448 struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
449 /* XXX LOV STACKING call into osc for sizes */
450 unsigned oldsize, newsize;
452 if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
453 struct llog_cookie *cookies;
454 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
455 newsize = set->set_count * sizeof(*cookies);
457 cookies = set->set_cookies;
458 oti_alloc_cookies(set->set_oti, set->set_count);
459 if (set->set_oti->oti_logcookies) {
460 memcpy(set->set_oti->oti_logcookies, cookies, newsize);
461 OBD_FREE_LARGE(cookies, oldsize);
462 set->set_cookies = set->set_oti->oti_logcookies;
464 CWARN("'leaking' %d bytes\n", oldsize - newsize);
468 CWARN("using fewer stripes for object "LPU64": old %u new %u\n",
469 lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
470 LASSERT(lsm->lsm_stripe_count >= set->set_count);
472 newsize = lov_stripe_md_size(set->set_count);
473 OBD_ALLOC_LARGE(lsm_new, newsize);
474 if (lsm_new != NULL) {
476 memcpy(lsm_new, lsm, sizeof(*lsm));
477 for (i = 0; i < lsm->lsm_stripe_count; i++) {
478 if (i < set->set_count) {
479 lsm_new->lsm_oinfo[i] = lsm->lsm_oinfo[i];
482 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
483 sizeof(struct lov_oinfo));
485 lsm_new->lsm_stripe_count = set->set_count;
486 OBD_FREE_LARGE(lsm, sizeof(struct lov_stripe_md) +
487 lsm->lsm_stripe_count*sizeof(struct lov_oinfo*));
488 set->set_oi->oi_md = lsm_new;
490 CWARN("'leaking' few bytes\n");
495 * Check whether we can create the object on the OST(refered by ost_idx)
497 * 0: create the object.
498 * other value: did not create the object.
500 static int lov_check_and_create_object(struct lov_obd *lov, int ost_idx,
501 struct lov_stripe_md *lsm,
502 struct lov_request *req,
503 struct obd_trans_info *oti)
509 CDEBUG(D_QOS, "Check and create on idx %d \n", ost_idx);
510 if (!lov->lov_tgts[ost_idx] ||
511 !lov->lov_tgts[ost_idx]->ltd_active)
514 /* check if objects has been created on this ost */
515 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
516 /* already have object at this stripe */
517 if (ost_idx == lsm->lsm_oinfo[stripe]->loi_ost_idx)
521 if (stripe >= lsm->lsm_stripe_count) {
522 req->rq_idx = ost_idx;
523 rc = obd_create(NULL, lov->lov_tgts[ost_idx]->ltd_exp,
524 req->rq_oi.oi_oa, &req->rq_oi.oi_md,
530 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
532 struct lov_stripe_md *lsm = set->set_oi->oi_md;
533 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
534 unsigned ost_idx = 0, ost_count;
535 struct pool_desc *pool;
536 struct ost_pool *osts = NULL;
540 /* First check whether we can create the objects on the pool */
541 pool = lov_find_pool(lov, lsm->lsm_pool_name);
543 cfs_down_read(&pool_tgt_rw_sem(pool));
544 osts = &(pool->pool_obds);
545 ost_count = osts->op_count;
546 for (i = 0; i < ost_count; i++, ost_idx = osts->op_array[i]) {
547 rc = lov_check_and_create_object(lov, ost_idx, lsm, req,
552 cfs_up_read(&pool_tgt_rw_sem(pool));
553 lov_pool_putref(pool);
557 ost_count = lov->desc.ld_tgt_count;
558 /* Then check whether we can create the objects on other OSTs */
559 ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
560 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
561 rc = lov_check_and_create_object(lov, ost_idx, lsm, req,
571 static int min_stripe_count(int stripe_cnt, int flags)
573 return (flags & LOV_USES_DEFAULT_STRIPE ?
574 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
577 #define LOV_CREATE_RESEED_MULT 30
578 #define LOV_CREATE_RESEED_MIN 2000
579 /* Allocate objects on osts with round-robin algorithm */
580 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt,
581 char *poolname, int flags)
586 int ost_start_idx_temp;
588 int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
589 struct pool_desc *pool;
590 struct ost_pool *osts;
591 struct lov_qos_rr *lqr;
594 pool = lov_find_pool(lov, poolname);
596 osts = &(lov->lov_packed);
597 lqr = &(lov->lov_qos.lq_rr);
599 cfs_down_read(&pool_tgt_rw_sem(pool));
600 osts = &(pool->pool_obds);
601 lqr = &(pool->pool_rr);
604 rc = qos_calc_rr(lov, osts, lqr);
608 if (--lqr->lqr_start_count <= 0) {
609 lqr->lqr_start_idx = cfs_rand() % osts->op_count;
610 lqr->lqr_start_count =
611 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
612 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
613 } else if (stripe_cnt_min >= osts->op_count ||
614 lqr->lqr_start_idx > osts->op_count) {
615 /* If we have allocated from all of the OSTs, slowly
616 * precess the next start if the OST/stripe count isn't
617 * already doing this for us. */
618 lqr->lqr_start_idx %= osts->op_count;
619 if (*stripe_cnt > 1 && (osts->op_count % (*stripe_cnt)) != 1)
620 ++lqr->lqr_offset_idx;
622 cfs_down_read(&lov->lov_qos.lq_rw_sem);
623 ost_start_idx_temp = lqr->lqr_start_idx;
626 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) % osts->op_count;
629 CDEBUG(D_QOS, "pool '%s' want %d startidx %d startcnt %d offset %d "
630 "active %d count %d arrayidx %d\n", poolname,
631 *stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
632 lqr->lqr_offset_idx, osts->op_count, osts->op_count, array_idx);
635 for (i = 0; i < osts->op_count;
636 i++, array_idx=(array_idx + 1) % osts->op_count) {
637 ++lqr->lqr_start_idx;
638 ost_idx = lqr->lqr_pool.op_array[array_idx];
640 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
641 i, lqr->lqr_start_idx,
642 ((ost_idx != LOV_QOS_EMPTY) && lov->lov_tgts[ost_idx]) ?
643 lov->lov_tgts[ost_idx]->ltd_active : 0,
644 idx_pos - idx_arr, array_idx, ost_idx);
646 if ((ost_idx == LOV_QOS_EMPTY) || !lov->lov_tgts[ost_idx] ||
647 !lov->lov_tgts[ost_idx]->ltd_active)
650 /* Fail Check before osc_precreate() is called
651 so we can only 'fail' single OSC. */
652 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
655 /* Drop slow OSCs if we can */
656 if (obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed)
661 /* We have enough stripes */
662 if (idx_pos - idx_arr == *stripe_cnt)
665 if ((speed < 2) && (idx_pos - idx_arr < stripe_cnt_min)) {
666 /* Try again, allowing slower OSCs */
668 lqr->lqr_start_idx = ost_start_idx_temp;
672 cfs_up_read(&lov->lov_qos.lq_rw_sem);
674 *stripe_cnt = idx_pos - idx_arr;
677 cfs_up_read(&pool_tgt_rw_sem(pool));
678 /* put back ref got by lov_find_pool() */
679 lov_pool_putref(pool);
685 /* alloc objects on osts with specific stripe offset */
686 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
689 unsigned ost_idx, array_idx, ost_count;
692 struct pool_desc *pool;
693 struct ost_pool *osts;
696 pool = lov_find_pool(lov, lsm->lsm_pool_name);
698 osts = &(lov->lov_packed);
700 cfs_down_read(&pool_tgt_rw_sem(pool));
701 osts = &(pool->pool_obds);
704 ost_count = osts->op_count;
707 /* search loi_ost_idx in ost array */
709 for (i = 0; i < ost_count; i++) {
710 if (osts->op_array[i] == lsm->lsm_oinfo[0]->loi_ost_idx) {
715 if (i == ost_count) {
716 CERROR("Start index %d not found in pool '%s'\n",
717 lsm->lsm_oinfo[0]->loi_ost_idx, lsm->lsm_pool_name);
718 GOTO(out, rc = -EINVAL);
722 for (i = 0; i < ost_count;
723 i++, array_idx = (array_idx + 1) % ost_count) {
724 ost_idx = osts->op_array[array_idx];
726 if (!lov->lov_tgts[ost_idx] ||
727 !lov->lov_tgts[ost_idx]->ltd_active) {
731 /* Fail Check before osc_precreate() is called
732 so we can only 'fail' single OSC. */
733 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
736 /* Drop slow OSCs if we can, but not for requested start idx.
738 * This means "if OSC is slow and it is not the requested
739 * start OST, then it can be skipped, otherwise skip it only
740 * if it is inactive/recovering/out-of-space." */
741 if ((obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed) &&
742 (i != 0 || speed >= 2))
747 /* We have enough stripes */
748 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
752 /* Try again, allowing slower OSCs */
757 /* If we were passed specific striping params, then a failure to
758 * meet those requirements is an error, since we can't reallocate
759 * that memory (it might be part of a larger array or something).
761 * We can only get here if lsm_stripe_count was originally > 1.
763 CERROR("can't lstripe objid "LPX64": have %d want %u\n",
764 lsm->lsm_object_id, (int)(idx_pos - idx_arr),
765 lsm->lsm_stripe_count);
769 cfs_up_read(&pool_tgt_rw_sem(pool));
770 /* put back ref got by lov_find_pool() */
771 lov_pool_putref(pool);
777 /* Alloc objects on osts with optimization based on:
779 - network resources (shared OSS's)
781 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt,
782 char *poolname, int flags)
784 struct lov_obd *lov = &exp->exp_obd->u.lov;
785 __u64 total_weight = 0;
786 int nfound, good_osts, i, rc = 0;
787 int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
788 struct pool_desc *pool;
789 struct ost_pool *osts;
792 if (stripe_cnt_min < 1)
795 pool = lov_find_pool(lov, poolname);
797 osts = &(lov->lov_packed);
799 cfs_down_read(&pool_tgt_rw_sem(pool));
800 osts = &(pool->pool_obds);
803 obd_getref(exp->exp_obd);
805 /* wait for fresh statfs info if needed, the rpcs are sent in
807 qos_statfs_update(exp->exp_obd,
808 cfs_time_shift_64(-2 * lov->desc.ld_qos_maxage), 1);
810 /* Detect -EAGAIN early, before expensive lock is taken. */
811 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
812 GOTO(out_nolock, rc = -EAGAIN);
814 /* Do actual allocation, use write lock here. */
815 cfs_down_write(&lov->lov_qos.lq_rw_sem);
818 * Check again, while we were sleeping on @lq_rw_sem things could
821 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
822 GOTO(out, rc = -EAGAIN);
824 if (lov->desc.ld_active_tgt_count < 2)
825 GOTO(out, rc = -EAGAIN);
827 rc = qos_calc_ppo(exp->exp_obd);
832 /* Find all the OSTs that are valid stripe candidates */
833 for (i = 0; i < osts->op_count; i++) {
834 if (!lov->lov_tgts[osts->op_array[i]] ||
835 !lov->lov_tgts[osts->op_array[i]]->ltd_active)
838 /* Fail Check before osc_precreate() is called
839 so we can only 'fail' single OSC. */
840 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && osts->op_array[i] == 0)
843 if (obd_precreate(lov->lov_tgts[osts->op_array[i]]->ltd_exp) > 2)
846 lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable = 1;
847 qos_calc_weight(lov, osts->op_array[i]);
848 total_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
854 CDEBUG(D_QOS, "found %d good osts\n", good_osts);
857 if (good_osts < stripe_cnt_min)
858 GOTO(out, rc = -EAGAIN);
860 /* We have enough osts */
861 if (good_osts < *stripe_cnt)
862 *stripe_cnt = good_osts;
865 GOTO(out, rc = -EAGAIN);
867 /* Find enough OSTs with weighted random allocation. */
869 while (nfound < *stripe_cnt) {
870 __u64 rand, cur_weight;
876 #if BITS_PER_LONG == 32
877 rand = cfs_rand() % (unsigned)total_weight;
878 /* If total_weight > 32-bit, first generate the high
879 * 32 bits of the random number, then add in the low
880 * 32 bits (truncated to the upper limit, if needed) */
881 if (total_weight > 0xffffffffULL)
882 rand = (__u64)(cfs_rand() %
883 (unsigned)(total_weight >> 32)) << 32;
887 if (rand == (total_weight & 0xffffffff00000000ULL))
888 rand |= cfs_rand() % (unsigned)total_weight;
893 rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
900 /* On average, this will hit larger-weighted osts more often.
901 0-weight osts will always get used last (only when rand=0).*/
902 for (i = 0; i < osts->op_count; i++) {
903 if (!lov->lov_tgts[osts->op_array[i]] ||
904 !lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable)
907 cur_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
909 CDEBUG(D_QOS, "stripe_cnt=%d nfound=%d cur_weight="LPU64
910 " rand="LPU64" total_weight="LPU64"\n",
911 *stripe_cnt, nfound, cur_weight, rand, total_weight);
913 if (cur_weight >= rand) {
915 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
916 nfound, osts->op_array[i]);
918 idx_arr[nfound++] = osts->op_array[i];
919 qos_used(lov, osts, osts->op_array[i], &total_weight);
924 /* should never satisfy below condition */
926 CERROR("Didn't find any OSTs?\n");
930 LASSERT(nfound == *stripe_cnt);
933 cfs_up_write(&lov->lov_qos.lq_rw_sem);
937 cfs_up_read(&pool_tgt_rw_sem(pool));
938 /* put back ref got by lov_find_pool() */
939 lov_pool_putref(pool);
943 rc = alloc_rr(lov, idx_arr, stripe_cnt, poolname, flags);
945 obd_putref(exp->exp_obd);
949 /* return new alloced stripe count on success */
950 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
951 int newea, int **idx_arr, int *arr_cnt, int flags)
953 struct lov_obd *lov = &exp->exp_obd->u.lov;
954 int stripe_cnt = lsm->lsm_stripe_count;
959 *arr_cnt = stripe_cnt;
960 OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
963 for (i = 0; i < *arr_cnt; i++)
967 lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count)
968 rc = alloc_qos(exp, tmp_arr, &stripe_cnt,
969 lsm->lsm_pool_name, flags);
971 rc = alloc_specific(lov, lsm, tmp_arr);
979 OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
984 static void free_idx_array(int *idx_arr, int arr_cnt)
987 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
990 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
992 struct lov_obd *lov = &exp->exp_obd->u.lov;
993 struct lov_stripe_md *lsm;
994 struct obdo *src_oa = set->set_oi->oi_oa;
995 struct obd_trans_info *oti = set->set_oti;
996 int i, stripes, rc = 0, newea = 0;
997 int flag = LOV_USES_ASSIGNED_STRIPE;
998 int *idx_arr = NULL, idx_cnt = 0;
1001 LASSERT(src_oa->o_valid & OBD_MD_FLID);
1002 LASSERT(src_oa->o_valid & OBD_MD_FLGROUP);
1004 if (set->set_oi->oi_md == NULL) {
1005 __u16 stripes_def = lov_get_stripecnt(lov, LOV_MAGIC, 0);
1007 /* If the MDS file was truncated up to some size, stripe over
1008 * enough OSTs to allow the file to be created at that size.
1009 * This may mean we use more than the default # of stripes. */
1010 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1011 obd_size min_bavail = LUSTRE_STRIPE_MAXBYTES;
1013 /* Find a small number of stripes we can use
1014 (up to # of active osts). */
1016 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1017 if (!lov->lov_tgts[i] ||
1018 !lov->lov_tgts[i]->ltd_active)
1020 min_bavail = min(min_bavail, TGT_BAVAIL(i));
1021 if (min_bavail * stripes > src_oa->o_size)
1026 if (stripes < stripes_def)
1027 stripes = stripes_def;
1029 flag = LOV_USES_DEFAULT_STRIPE;
1030 stripes = stripes_def;
1033 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
1034 lov->desc.ld_pattern ?
1035 lov->desc.ld_pattern : LOV_PATTERN_RAID0,
1043 lsm = set->set_oi->oi_md;
1044 lsm->lsm_object_id = src_oa->o_id;
1045 lsm->lsm_object_seq = src_oa->o_seq;
1046 lsm->lsm_layout_gen = 0; /* actual generation set in mdd_lov_create() */
1048 if (!lsm->lsm_stripe_size)
1049 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
1050 if (!lsm->lsm_pattern) {
1051 LASSERT(lov->desc.ld_pattern);
1052 lsm->lsm_pattern = lov->desc.ld_pattern;
1055 stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt, flag);
1057 GOTO(out_err, rc = stripes ? stripes : -EIO);
1058 LASSERTF(stripes <= lsm->lsm_stripe_count,"requested %d allocated %d\n",
1059 lsm->lsm_stripe_count, stripes);
1061 for (i = 0; i < stripes; i++) {
1062 struct lov_request *req;
1063 int ost_idx = idx_arr[i];
1064 LASSERT(ost_idx >= 0);
1066 OBD_ALLOC(req, sizeof(*req));
1068 GOTO(out_err, rc = -ENOMEM);
1069 lov_set_add_req(req, set);
1071 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
1072 OBD_ALLOC_LARGE(req->rq_oi.oi_md, req->rq_buflen);
1073 if (req->rq_oi.oi_md == NULL)
1074 GOTO(out_err, rc = -ENOMEM);
1076 OBDO_ALLOC(req->rq_oi.oi_oa);
1077 if (req->rq_oi.oi_oa == NULL)
1078 GOTO(out_err, rc = -ENOMEM);
1080 req->rq_idx = ost_idx;
1082 /* create data objects with "parent" OA */
1083 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
1084 req->rq_oi.oi_cb_up = cb_create_update;
1086 /* XXX When we start creating objects on demand, we need to
1087 * make sure that we always create the object on the
1088 * stripe which holds the existing file size.
1090 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1091 req->rq_oi.oi_oa->o_size =
1092 lov_size_to_stripe(lsm, src_oa->o_size, i);
1094 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1095 i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
1098 LASSERT(set->set_count == stripes);
1100 if (stripes < lsm->lsm_stripe_count)
1101 qos_shrink_lsm(set);
1102 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LOV_PREP_CREATE)) {
1103 qos_shrink_lsm(set);
1107 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
1108 oti_alloc_cookies(oti, set->set_count);
1109 if (!oti->oti_logcookies)
1110 GOTO(out_err, rc = -ENOMEM);
1111 set->set_cookies = oti->oti_logcookies;
1115 obd_free_memmd(exp, &set->set_oi->oi_md);
1117 free_idx_array(idx_arr, idx_cnt);
1122 void qos_update(struct lov_obd *lov)
1125 lov->lov_qos.lq_dirty = 1;
1128 void qos_statfs_done(struct lov_obd *lov)
1130 cfs_down_write(&lov->lov_qos.lq_rw_sem);
1131 if (lov->lov_qos.lq_statfs_in_progress) {
1132 lov->lov_qos.lq_statfs_in_progress = 0;
1133 /* wake up any threads waiting for the statfs rpcs to complete*/
1134 cfs_waitq_signal(&lov->lov_qos.lq_statfs_waitq);
1136 cfs_up_write(&lov->lov_qos.lq_rw_sem);
1139 static int qos_statfs_ready(struct obd_device *obd, __u64 max_age)
1141 struct lov_obd *lov = &obd->u.lov;
1144 cfs_down_read(&lov->lov_qos.lq_rw_sem);
1145 rc = lov->lov_qos.lq_statfs_in_progress == 0 ||
1146 cfs_time_beforeq_64(max_age, obd->obd_osfs_age);
1147 cfs_up_read(&lov->lov_qos.lq_rw_sem);
1152 * Update statfs data if the current osfs age is older than max_age.
1153 * If wait is not set, it means that we are called from lov_create()
1154 * and we should just issue the rpcs without waiting for them to complete.
1155 * If wait is set, we are called from alloc_qos() and we just have
1156 * to wait for the request set to complete.
1158 void qos_statfs_update(struct obd_device *obd, __u64 max_age, int wait)
1160 struct lov_obd *lov = &obd->u.lov;
1161 struct obd_info *oinfo;
1163 struct ptlrpc_request_set *set = NULL;
1166 if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
1167 /* statfs data are quite recent, don't need to refresh it */
1170 if (!wait && lov->lov_qos.lq_statfs_in_progress)
1171 /* statfs already in progress */
1174 cfs_down_write(&lov->lov_qos.lq_rw_sem);
1175 if (lov->lov_qos.lq_statfs_in_progress) {
1176 cfs_up_write(&lov->lov_qos.lq_rw_sem);
1179 /* no statfs in flight, send rpcs */
1180 lov->lov_qos.lq_statfs_in_progress = 1;
1181 cfs_up_write(&lov->lov_qos.lq_rw_sem);
1184 CDEBUG(D_QOS, "%s: did not manage to get fresh statfs data "
1185 "in a timely manner (osfs age "LPU64", max age "LPU64")"
1186 ", sending new statfs rpcs\n",
1187 obd_uuid2str(&lov->desc.ld_uuid), obd->obd_osfs_age,
1190 /* need to send statfs rpcs */
1191 CDEBUG(D_QOS, "sending new statfs requests\n");
1192 memset(lov->lov_qos.lq_statfs_data, 0,
1193 sizeof(*lov->lov_qos.lq_statfs_data));
1194 oinfo = &lov->lov_qos.lq_statfs_data->lsd_oi;
1195 oinfo->oi_osfs = &lov->lov_qos.lq_statfs_data->lsd_statfs;
1196 oinfo->oi_flags = OBD_STATFS_NODELAY;
1197 set = ptlrpc_prep_set();
1199 GOTO(out_failed, rc = -ENOMEM);
1201 rc = obd_statfs_async(obd->obd_self_export, oinfo, max_age, set);
1202 if (rc || cfs_list_empty(&set->set_requests)) {
1204 CWARN("statfs failed with %d\n", rc);
1205 GOTO(out_failed, rc);
1207 /* send requests via ptlrpcd */
1208 oinfo->oi_flags |= OBD_STATFS_PTLRPCD;
1209 ptlrpcd_add_rqset(set);
1213 cfs_down_write(&lov->lov_qos.lq_rw_sem);
1214 lov->lov_qos.lq_statfs_in_progress = 0;
1215 /* wake up any threads waiting for the statfs rpcs to complete */
1216 cfs_waitq_signal(&lov->lov_qos.lq_statfs_waitq);
1217 cfs_up_write(&lov->lov_qos.lq_rw_sem);
1221 ptlrpc_set_destroy(set);
1223 struct l_wait_info lwi = { 0 };
1224 CDEBUG(D_QOS, "waiting for statfs requests to complete\n");
1225 l_wait_event(lov->lov_qos.lq_statfs_waitq,
1226 qos_statfs_ready(obd, max_age), &lwi);
1227 if (cfs_time_before_64(obd->obd_osfs_age, max_age))
1228 CDEBUG(D_QOS, "%s: still no fresh statfs data after "
1229 "waiting (osfs age "LPU64", max age "
1231 obd_uuid2str(&lov->desc.ld_uuid),
1232 obd->obd_osfs_age, max_age);