1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LOV
43 #include <libcfs/libcfs.h>
45 #include <liblustre.h>
48 #include <obd_class.h>
50 #include "lov_internal.h"
52 /* #define QOS_DEBUG 1 */
55 #define TGT_BAVAIL(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail *\
56 lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize)
57 #define TGT_FFREE(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_ffree)
60 int qos_add_tgt(struct obd_device *obd, __u32 index)
62 struct lov_obd *lov = &obd->u.lov;
63 struct lov_qos_oss *oss, *temposs;
64 struct obd_export *exp = lov->lov_tgts[index]->ltd_exp;
65 int rc = 0, found = 0;
68 /* We only need this QOS struct on MDT, not clients - but we may not
69 * have registered the LOV's observer yet, so there's no way to know */
70 if (!exp || !exp->exp_connection) {
71 CERROR("Missing connection\n");
75 down_write(&lov->lov_qos.lq_rw_sem);
76 mutex_down(&lov->lov_lock);
77 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
78 if (obd_uuid_equals(&oss->lqo_uuid,
79 &exp->exp_connection->c_remote_uuid)) {
88 GOTO(out, rc = -ENOMEM);
89 memcpy(&oss->lqo_uuid,
90 &exp->exp_connection->c_remote_uuid,
91 sizeof(oss->lqo_uuid));
93 /* Assume we have to move this one */
94 list_del(&oss->lqo_oss_list);
98 lov->lov_tgts[index]->ltd_qos.ltq_oss = oss;
100 /* Add sorted by # of OSTs. Find the first entry that we're
102 list_for_each_entry(temposs, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
103 if (oss->lqo_ost_count > temposs->lqo_ost_count)
106 /* ...and add before it. If we're the first or smallest, temposs
107 points to the list head, and we add to the end. */
108 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
110 lov->lov_qos.lq_dirty = 1;
111 lov->lov_qos.lq_rr.lqr_dirty = 1;
113 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
114 obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
115 obd_uuid2str(&oss->lqo_uuid),
119 mutex_up(&lov->lov_lock);
120 up_write(&lov->lov_qos.lq_rw_sem);
124 int qos_del_tgt(struct obd_device *obd, __u32 index)
126 struct lov_obd *lov = &obd->u.lov;
127 struct lov_qos_oss *oss;
131 if (!lov->lov_tgts[index])
134 down_write(&lov->lov_qos.lq_rw_sem);
136 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
138 GOTO(out, rc = -ENOENT);
140 oss->lqo_ost_count--;
141 if (oss->lqo_ost_count == 0) {
142 CDEBUG(D_QOS, "removing OSS %s\n",
143 obd_uuid2str(&oss->lqo_uuid));
144 list_del(&oss->lqo_oss_list);
148 lov->lov_qos.lq_dirty = 1;
149 lov->lov_qos.lq_rr.lqr_dirty = 1;
151 up_write(&lov->lov_qos.lq_rw_sem);
155 /* Recalculate per-object penalties for OSSs and OSTs,
156 depends on size of each ost in an oss */
157 static int qos_calc_ppo(struct obd_device *obd)
159 struct lov_obd *lov = &obd->u.lov;
160 struct lov_qos_oss *oss;
161 __u64 ba_max, ba_min, temp;
163 int rc, i, prio_wide;
166 if (!lov->lov_qos.lq_dirty)
169 num_active = lov->desc.ld_active_tgt_count - 1;
171 GOTO(out, rc = -EAGAIN);
173 /* find bavail on each OSS */
174 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
177 lov->lov_qos.lq_active_oss_count = 0;
179 /* How badly user wants to select osts "widely" (not recently chosen
180 and not on recent oss's). As opposed to "freely" (free space
182 prio_wide = 256 - lov->lov_qos.lq_prio_free;
184 ba_min = (__u64)(-1);
186 /* Calculate OST penalty per object */
187 /* (lov ref taken in alloc_qos) */
188 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
189 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
191 temp = TGT_BAVAIL(i);
194 ba_min = min(temp, ba_min);
195 ba_max = max(temp, ba_max);
197 /* Count the number of usable OSS's */
198 if (lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail == 0)
199 lov->lov_qos.lq_active_oss_count++;
200 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail += temp;
202 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
204 do_div(temp, num_active);
205 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj =
206 (temp * prio_wide) >> 8;
208 if (lov->lov_qos.lq_reset == 0)
209 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
212 num_active = lov->lov_qos.lq_active_oss_count - 1;
213 if (num_active < 1) {
214 /* If there's only 1 OSS, we can't penalize it, so instead
215 we have to double the OST penalty */
217 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
218 if (lov->lov_tgts[i] == NULL)
220 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
224 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
225 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
226 temp = oss->lqo_bavail >> 1;
227 do_div(temp, oss->lqo_ost_count * num_active);
228 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
229 if (lov->lov_qos.lq_reset == 0)
230 oss->lqo_penalty = 0;
233 lov->lov_qos.lq_dirty = 0;
234 lov->lov_qos.lq_reset = 0;
236 /* If each ost has almost same free space,
237 * do rr allocation for better creation performance */
238 lov->lov_qos.lq_same_space = 0;
239 temp = ba_max - ba_min;
240 ba_min = (ba_min * 51) >> 8; /* 51/256 = .20 */
242 /* Difference is less than 20% */
243 lov->lov_qos.lq_same_space = 1;
244 /* Reset weights for the next time we enter qos mode */
245 lov->lov_qos.lq_reset = 0;
250 if (!rc && lov->lov_qos.lq_same_space)
255 static int qos_calc_weight(struct lov_obd *lov, int i)
259 /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
260 temp = TGT_BAVAIL(i);
261 temp2 = lov->lov_tgts[i]->ltd_qos.ltq_penalty +
262 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty;
264 lov->lov_tgts[i]->ltd_qos.ltq_weight = 0;
266 lov->lov_tgts[i]->ltd_qos.ltq_weight = temp - temp2;
270 /* We just used this index for a stripe; adjust everyone's weights */
271 static int qos_used(struct lov_obd *lov, struct ost_pool *osts,
272 __u32 index, __u64 *total_wt)
274 struct lov_qos_oss *oss;
278 /* Don't allocate from this stripe anymore, until the next alloc_qos */
279 lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
281 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
283 /* Decay old penalty by half (we're adding max penalty, and don't
284 want it to run away.) */
285 lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
286 oss->lqo_penalty >>= 1;
288 /* Set max penalties for this OST and OSS */
289 lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
290 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
291 lov->desc.ld_active_tgt_count;
292 oss->lqo_penalty += oss->lqo_penalty_per_obj *
293 lov->lov_qos.lq_active_oss_count;
295 /* Decrease all OSS penalties */
296 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
297 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
298 oss->lqo_penalty = 0;
300 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
304 /* Decrease all OST penalties */
305 for (j = 0; j < osts->op_count; j++) {
308 i = osts->op_array[j];
309 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
311 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
312 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
313 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
315 lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
316 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
318 qos_calc_weight(lov, i);
320 /* Recalc the total weight of usable osts */
321 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
322 *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
325 CDEBUG(D_QOS, "recalc tgt %d usable=%d avail="LPU64
326 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
327 " ossp="LPU64" wt="LPU64"\n",
328 i, lov->lov_tgts[i]->ltd_qos.ltq_usable,
330 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj >> 10,
331 lov->lov_tgts[i]->ltd_qos.ltq_penalty >> 10,
332 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj>>10,
333 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty >> 10,
334 lov->lov_tgts[i]->ltd_qos.ltq_weight >> 10);
341 #define LOV_QOS_EMPTY ((__u32)-1)
342 /* compute optimal round-robin order, based on OSTs per OSS */
343 static int qos_calc_rr(struct lov_obd *lov, struct ost_pool *src_pool,
344 struct lov_qos_rr *lqr)
346 struct lov_qos_oss *oss;
347 unsigned placed, real_count;
351 if (!lqr->lqr_dirty) {
352 LASSERT(lqr->lqr_pool.op_size);
356 /* Do actual allocation. */
357 down_write(&lov->lov_qos.lq_rw_sem);
360 * Check again. While we were sleeping on @lq_rw_sem something could
363 if (!lqr->lqr_dirty) {
364 LASSERT(lqr->lqr_pool.op_size);
365 up_write(&lov->lov_qos.lq_rw_sem);
369 real_count = src_pool->op_count;
371 /* Zero the pool array */
372 /* alloc_rr is holding a read lock on the pool, so nobody is adding/
373 deleting from the pool. The lq_rw_sem insures that nobody else
375 lqr->lqr_pool.op_count = real_count;
376 rc = lov_ost_pool_extend(&lqr->lqr_pool, real_count);
378 up_write(&lov->lov_qos.lq_rw_sem);
381 for (i = 0; i < lqr->lqr_pool.op_count; i++)
382 lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
384 /* Place all the OSTs from 1 OSS at the same time. */
386 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
388 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
389 if (lov->lov_tgts[src_pool->op_array[i]] &&
390 (lov->lov_tgts[src_pool->op_array[i]]->ltd_qos.ltq_oss == oss)) {
391 /* Evenly space these OSTs across arrayspace */
392 int next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
393 while (lqr->lqr_pool.op_array[next] !=
395 next = (next + 1) % lqr->lqr_pool.op_count;
396 lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
404 up_write(&lov->lov_qos.lq_rw_sem);
406 if (placed != real_count) {
407 /* This should never happen */
408 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
409 "round-robin list (%d of %d).\n",
411 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
412 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
413 lqr->lqr_pool.op_array[i]);
420 for (i = 0; i < lqr->lqr_pool.op_count; i++) {
421 LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i,
422 lqr->lqr_pool.op_array[i]);
430 void qos_shrink_lsm(struct lov_request_set *set)
432 struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
433 /* XXX LOV STACKING call into osc for sizes */
434 unsigned oldsize, newsize;
436 if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
437 struct llog_cookie *cookies;
438 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
439 newsize = set->set_count * sizeof(*cookies);
441 cookies = set->set_cookies;
442 oti_alloc_cookies(set->set_oti, set->set_count);
443 if (set->set_oti->oti_logcookies) {
444 memcpy(set->set_oti->oti_logcookies, cookies, newsize);
445 OBD_FREE(cookies, oldsize);
446 set->set_cookies = set->set_oti->oti_logcookies;
448 CWARN("'leaking' %d bytes\n", oldsize - newsize);
452 CWARN("using fewer stripes for object "LPU64": old %u new %u\n",
453 lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
454 LASSERT(lsm->lsm_stripe_count >= set->set_count);
456 newsize = lov_stripe_md_size(set->set_count);
457 OBD_ALLOC(lsm_new, newsize);
458 if (lsm_new != NULL) {
460 memcpy(lsm_new, lsm, sizeof(*lsm));
461 for (i = 0; i < lsm->lsm_stripe_count; i++) {
462 if (i < set->set_count) {
463 lsm_new->lsm_oinfo[i] = lsm->lsm_oinfo[i];
466 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
467 sizeof(struct lov_oinfo));
469 lsm_new->lsm_stripe_count = set->set_count;
470 OBD_FREE(lsm, sizeof(struct lov_stripe_md) +
471 lsm->lsm_stripe_count * sizeof(struct lov_oinfo *));
472 set->set_oi->oi_md = lsm_new;
474 CWARN("'leaking' few bytes\n");
478 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
480 struct lov_stripe_md *lsm = set->set_oi->oi_md;
481 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
482 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
483 int stripe, i, rc = -EIO;
486 ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
487 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
488 if (!lov->lov_tgts[ost_idx] ||
489 !lov->lov_tgts[ost_idx]->ltd_active)
491 /* check if objects has been created on this ost */
492 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
493 if (stripe == req->rq_stripe)
495 if (ost_idx == lsm->lsm_oinfo[stripe]->loi_ost_idx)
499 if (stripe >= lsm->lsm_stripe_count) {
500 req->rq_idx = ost_idx;
501 rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp,
502 req->rq_oi.oi_oa, &req->rq_oi.oi_md,
511 static int min_stripe_count(int stripe_cnt, int flags)
513 return (flags & LOV_USES_DEFAULT_STRIPE ?
514 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
517 #define LOV_CREATE_RESEED_MULT 4
518 #define LOV_CREATE_RESEED_MIN 1000
519 /* Allocate objects on osts with round-robin algorithm */
520 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt,
521 char *poolname, int flags)
526 int ost_start_idx_temp;
528 int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
529 struct pool_desc *pool;
530 struct ost_pool *osts;
531 struct lov_qos_rr *lqr;
534 pool = lov_find_pool(lov, poolname);
536 osts = &(lov->lov_packed);
537 lqr = &(lov->lov_qos.lq_rr);
539 read_lock(&pool_tgt_rwlock(pool));
540 osts = &(pool->pool_obds);
541 lqr = &(pool->pool_rr);
544 rc = qos_calc_rr(lov, osts, lqr);
548 if (--lqr->lqr_start_count <= 0) {
549 lqr->lqr_start_idx = ll_rand() % osts->op_count;
550 lqr->lqr_start_count =
551 (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
552 LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
553 } else if (stripe_cnt_min >= osts->op_count ||
554 lqr->lqr_start_idx > osts->op_count) {
555 /* If we have allocated from all of the OSTs, slowly
556 * precess the next start if the OST/stripe count isn't
557 * already doing this for us. */
558 lqr->lqr_start_idx %= osts->op_count;
559 if (*stripe_cnt > 1 && (osts->op_count % (*stripe_cnt)) != 1)
560 ++lqr->lqr_offset_idx;
562 down_read(&lov->lov_qos.lq_rw_sem);
563 ost_start_idx_temp = lqr->lqr_start_idx;
566 array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) % osts->op_count;
569 CDEBUG(D_QOS, "pool '%s' want %d startidx %d startcnt %d offset %d "
570 "active %d count %d arrayidx %d\n", poolname,
571 *stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
572 lqr->lqr_offset_idx, osts->op_count, osts->op_count, array_idx);
575 for (i = 0; i < osts->op_count;
576 i++, array_idx=(array_idx + 1) % osts->op_count) {
577 ++lqr->lqr_start_idx;
578 ost_idx = lqr->lqr_pool.op_array[array_idx];
580 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
581 i, lqr->lqr_start_idx,
582 ((ost_idx != LOV_QOS_EMPTY) && lov->lov_tgts[ost_idx]) ?
583 lov->lov_tgts[ost_idx]->ltd_active : 0,
584 idx_pos - idx_arr, array_idx, ost_idx);
586 if ((ost_idx == LOV_QOS_EMPTY) || !lov->lov_tgts[ost_idx] ||
587 !lov->lov_tgts[ost_idx]->ltd_active)
590 /* Fail Check before osc_precreate() is called
591 so we can only 'fail' single OSC. */
592 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
595 /* Drop slow OSCs if we can */
596 if (obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed)
601 /* We have enough stripes */
602 if (idx_pos - idx_arr == *stripe_cnt)
605 if ((speed < 2) && (idx_pos - idx_arr < stripe_cnt_min)) {
606 /* Try again, allowing slower OSCs */
608 lqr->lqr_start_idx = ost_start_idx_temp;
612 up_read(&lov->lov_qos.lq_rw_sem);
614 *stripe_cnt = idx_pos - idx_arr;
617 read_unlock(&pool_tgt_rwlock(pool));
621 /* alloc objects on osts with specific stripe offset */
622 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
625 unsigned ost_idx, array_idx, ost_count;
628 struct pool_desc *pool;
629 struct ost_pool *osts;
632 pool = lov_find_pool(lov, lsm->lsm_pool_name);
634 osts = &(lov->lov_packed);
636 read_lock(&pool_tgt_rwlock(pool));
637 osts = &(pool->pool_obds);
640 ost_count = osts->op_count;
643 /* search loi_ost_idx in ost array */
645 for (i = 0; i < ost_count; i++) {
646 if (osts->op_array[i] == lsm->lsm_oinfo[0]->loi_ost_idx) {
651 if (i == ost_count) {
652 CERROR("Start index %d not found in pool '%s'\n",
653 lsm->lsm_oinfo[0]->loi_ost_idx, lsm->lsm_pool_name);
654 GOTO(out, rc = -EINVAL);
658 for (i = 0; i < ost_count;
659 i++, array_idx = (array_idx + 1) % ost_count) {
660 ost_idx = osts->op_array[array_idx];
662 if (!lov->lov_tgts[ost_idx] ||
663 !lov->lov_tgts[ost_idx]->ltd_active) {
667 /* Fail Check before osc_precreate() is called
668 so we can only 'fail' single OSC. */
669 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
672 /* Drop slow OSCs if we can, but not for requested start idx */
673 if ((obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed) &&
674 (i != 0 || speed < 2))
679 /* We have enough stripes */
680 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
684 /* Try again, allowing slower OSCs */
689 /* If we were passed specific striping params, then a failure to
690 * meet those requirements is an error, since we can't reallocate
691 * that memory (it might be part of a larger array or something).
693 * We can only get here if lsm_stripe_count was originally > 1.
695 CERROR("can't lstripe objid "LPX64": have %d want %u\n",
696 lsm->lsm_object_id, (int)(idx_pos - idx_arr),
697 lsm->lsm_stripe_count);
701 read_unlock(&pool_tgt_rwlock(pool));
705 /* Alloc objects on osts with optimization based on:
707 - network resources (shared OSS's)
709 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt,
710 char *poolname, int flags)
712 struct lov_obd *lov = &exp->exp_obd->u.lov;
713 static time_t last_warn = 0;
714 time_t now = cfs_time_current_sec();
715 __u64 total_bavail, total_weight = 0;
716 int nfound, good_osts, i, warn = 0, rc = 0;
717 int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
718 struct pool_desc *pool;
719 struct ost_pool *osts;
720 struct lov_qos_rr *lqr;
723 if (stripe_cnt_min < 1)
726 pool = lov_find_pool(lov, poolname);
728 osts = &(lov->lov_packed);
729 lqr = &(lov->lov_qos.lq_rr);
731 read_lock(&pool_tgt_rwlock(pool));
732 osts = &(pool->pool_obds);
733 lqr = &(pool->pool_rr);
736 lov_getref(exp->exp_obd);
738 /* Detect -EAGAIN early, before expensive lock is taken. */
739 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
740 GOTO(out_nolock, rc = -EAGAIN);
742 /* Do actual allocation, use write lock here. */
743 down_write(&lov->lov_qos.lq_rw_sem);
746 * Check again, while we were sleeping on @lq_rw_sem things could
749 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
750 GOTO(out, rc = -EAGAIN);
752 if (lov->desc.ld_active_tgt_count < 2)
753 GOTO(out, rc = -EAGAIN);
755 rc = qos_calc_ppo(exp->exp_obd);
761 /* Warn users about zero available space/inode every 30 min */
762 if (cfs_time_sub(now, last_warn) > 60 * 30)
764 /* Find all the OSTs that are valid stripe candidates */
765 for (i = 0; i < osts->op_count; i++) {
768 if (!lov->lov_tgts[osts->op_array[i]] ||
769 !lov->lov_tgts[osts->op_array[i]]->ltd_active)
771 bavail = TGT_BAVAIL(osts->op_array[i]);
774 CDEBUG(D_QOS, "no free space on %s\n",
775 obd_uuid2str(&lov->lov_tgts[osts->op_array[i]]->ltd_uuid));
780 if (!TGT_FFREE(osts->op_array[i])) {
782 CDEBUG(D_QOS, "no free inodes on %s\n",
783 obd_uuid2str(&lov->lov_tgts[osts->op_array[i]]->ltd_uuid));
789 /* Fail Check before osc_precreate() is called
790 so we can only 'fail' single OSC. */
791 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && osts->op_array[i] == 0)
794 if (obd_precreate(lov->lov_tgts[osts->op_array[i]]->ltd_exp) > 2)
797 lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable = 1;
798 qos_calc_weight(lov, osts->op_array[i]);
799 total_bavail += bavail;
800 total_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
806 CDEBUG(D_QOS, "found %d good osts\n", good_osts);
809 if (good_osts < stripe_cnt_min)
810 GOTO(out, rc = -EAGAIN);
813 GOTO(out, rc = -ENOSPC);
815 /* We have enough osts */
816 if (good_osts < *stripe_cnt)
817 *stripe_cnt = good_osts;
820 GOTO(out, rc = -EAGAIN);
822 /* Find enough OSTs with weighted random allocation. */
824 while (nfound < *stripe_cnt) {
825 __u64 rand, cur_weight;
831 #if BITS_PER_LONG == 32
832 rand = ll_rand() % (unsigned)total_weight;
833 /* If total_weight > 32-bit, first generate the high
834 * 32 bits of the random number, then add in the low
835 * 32 bits (truncated to the upper limit, if needed) */
836 if (total_weight > 0xffffffffULL)
837 rand = (__u64)(ll_rand() %
838 (unsigned)(total_weight >> 32)) << 32;
842 if (rand == (total_weight & 0xffffffff00000000ULL))
843 rand |= ll_rand() % (unsigned)total_weight;
848 rand = ((__u64)ll_rand() << 32 | ll_rand()) %
855 /* On average, this will hit larger-weighted osts more often.
856 0-weight osts will always get used last (only when rand=0).*/
857 for (i = 0; i < osts->op_count; i++) {
858 if (!lov->lov_tgts[osts->op_array[i]] ||
859 !lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable)
862 cur_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
864 CDEBUG(D_QOS, "stripe_cnt=%d nfound=%d cur_weight="LPU64
865 " rand="LPU64" total_weight="LPU64"\n",
866 *stripe_cnt, nfound, cur_weight, rand, total_weight);
868 if (cur_weight >= rand) {
870 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
871 nfound, osts->op_array[i]);
873 idx_arr[nfound++] = osts->op_array[i];
874 qos_used(lov, osts, osts->op_array[i], &total_weight);
879 /* should never satisfy below condition */
881 CERROR("Didn't find any OSTs?\n");
885 LASSERT(nfound == *stripe_cnt);
888 up_write(&lov->lov_qos.lq_rw_sem);
892 read_unlock(&pool_tgt_rwlock(pool));
895 rc = alloc_rr(lov, idx_arr, stripe_cnt, poolname, flags);
897 lov_putref(exp->exp_obd);
901 /* return new alloced stripe count on success */
902 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
903 int newea, int **idx_arr, int *arr_cnt, int flags)
905 struct lov_obd *lov = &exp->exp_obd->u.lov;
906 int stripe_cnt = lsm->lsm_stripe_count;
911 *arr_cnt = stripe_cnt;
912 OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
915 for (i = 0; i < *arr_cnt; i++)
919 lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count)
920 rc = alloc_qos(exp, tmp_arr, &stripe_cnt,
921 lsm->lsm_pool_name, flags);
923 rc = alloc_specific(lov, lsm, tmp_arr);
931 OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
936 static void free_idx_array(int *idx_arr, int arr_cnt)
939 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
942 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
944 struct lov_obd *lov = &exp->exp_obd->u.lov;
945 struct lov_stripe_md *lsm;
946 struct obdo *src_oa = set->set_oi->oi_oa;
947 struct obd_trans_info *oti = set->set_oti;
948 int i, stripes, rc = 0, newea = 0;
949 int flag = LOV_USES_ASSIGNED_STRIPE;
950 int *idx_arr = NULL, idx_cnt = 0;
953 LASSERT(src_oa->o_valid & OBD_MD_FLID);
954 LASSERT(src_oa->o_valid & OBD_MD_FLGROUP);
956 if (set->set_oi->oi_md == NULL) {
957 int stripes_def = lov_get_stripecnt(lov, 0);
959 /* If the MDS file was truncated up to some size, stripe over
960 * enough OSTs to allow the file to be created at that size.
961 * This may mean we use more than the default # of stripes. */
962 if (src_oa->o_valid & OBD_MD_FLSIZE) {
963 obd_size min_bavail = LUSTRE_STRIPE_MAXBYTES;
965 /* Find a small number of stripes we can use
966 (up to # of active osts). */
968 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
969 if (!lov->lov_tgts[i] ||
970 !lov->lov_tgts[i]->ltd_active)
972 min_bavail = min(min_bavail, TGT_BAVAIL(i));
973 if (min_bavail * stripes > src_oa->o_size)
978 if (stripes < stripes_def)
979 stripes = stripes_def;
981 flag = LOV_USES_DEFAULT_STRIPE;
982 stripes = stripes_def;
985 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
986 lov->desc.ld_pattern ?
987 lov->desc.ld_pattern : LOV_PATTERN_RAID0,
995 lsm = set->set_oi->oi_md;
996 lsm->lsm_object_id = src_oa->o_id;
997 lsm->lsm_object_gr = src_oa->o_gr;
999 if (!lsm->lsm_stripe_size)
1000 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
1001 if (!lsm->lsm_pattern) {
1002 LASSERT(lov->desc.ld_pattern);
1003 lsm->lsm_pattern = lov->desc.ld_pattern;
1006 stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt, flag);
1008 GOTO(out_err, rc = stripes ? stripes : -EIO);
1009 LASSERTF(stripes <= lsm->lsm_stripe_count,"requested %d allocated %d\n",
1010 lsm->lsm_stripe_count, stripes);
1012 for (i = 0; i < stripes; i++) {
1013 struct lov_request *req;
1014 int ost_idx = idx_arr[i];
1015 LASSERT(ost_idx >= 0);
1017 OBD_ALLOC(req, sizeof(*req));
1019 GOTO(out_err, rc = -ENOMEM);
1020 lov_set_add_req(req, set);
1022 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
1023 OBD_ALLOC(req->rq_oi.oi_md, req->rq_buflen);
1024 if (req->rq_oi.oi_md == NULL)
1025 GOTO(out_err, rc = -ENOMEM);
1027 OBDO_ALLOC(req->rq_oi.oi_oa);
1028 if (req->rq_oi.oi_oa == NULL)
1029 GOTO(out_err, rc = -ENOMEM);
1031 req->rq_idx = ost_idx;
1033 /* create data objects with "parent" OA */
1034 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
1036 /* XXX When we start creating objects on demand, we need to
1037 * make sure that we always create the object on the
1038 * stripe which holds the existing file size.
1040 if (src_oa->o_valid & OBD_MD_FLSIZE) {
1041 req->rq_oi.oi_oa->o_size =
1042 lov_size_to_stripe(lsm, src_oa->o_size, i);
1044 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
1045 i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
1048 LASSERT(set->set_count == stripes);
1050 if (stripes < lsm->lsm_stripe_count)
1051 qos_shrink_lsm(set);
1053 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
1054 oti_alloc_cookies(oti, set->set_count);
1055 if (!oti->oti_logcookies)
1056 GOTO(out_err, rc = -ENOMEM);
1057 set->set_cookies = oti->oti_logcookies;
1061 obd_free_memmd(exp, &set->set_oi->oi_md);
1063 free_idx_array(idx_arr, idx_cnt);
1068 void qos_update(struct lov_obd *lov)
1071 lov->lov_qos.lq_dirty = 1;