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 [sun.com URL with a
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_dirty_rr = 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_dirty_rr = 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, __u32 index, __u64 *total_wt)
273 struct lov_qos_oss *oss;
277 /* Don't allocate from this stripe anymore, until the next alloc_qos */
278 lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
280 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
282 /* Decay old penalty by half (we're adding max penalty, and don't
283 want it to run away.) */
284 lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
285 oss->lqo_penalty >>= 1;
287 /* Set max penalties for this OST and OSS */
288 lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
289 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
290 lov->desc.ld_active_tgt_count;
291 oss->lqo_penalty += oss->lqo_penalty_per_obj *
292 lov->lov_qos.lq_active_oss_count;
294 /* Decrease all OSS penalties */
295 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
296 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
297 oss->lqo_penalty = 0;
299 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
303 /* Decrease all OST penalties */
304 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
305 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
307 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
308 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
309 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
311 lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
312 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
314 qos_calc_weight(lov, i);
316 /* Recalc the total weight of usable osts */
317 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
318 *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
321 CDEBUG(D_QOS, "recalc tgt %d avail="LPU64
322 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
323 " ossp="LPU64" wt="LPU64"\n",
324 i, TGT_BAVAIL(i) >> 10,
325 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj >> 10,
326 lov->lov_tgts[i]->ltd_qos.ltq_penalty >> 10,
327 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj>>10,
328 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty >> 10,
329 lov->lov_tgts[i]->ltd_qos.ltq_weight >> 10);
336 #define LOV_QOS_EMPTY ((__u32)-1)
337 /* compute optimal round-robin order, based on OSTs per OSS */
338 static int qos_calc_rr(struct lov_obd *lov)
340 struct lov_qos_oss *oss;
341 unsigned ost_count, placed, real_count;
345 if (!lov->lov_qos.lq_dirty_rr) {
346 LASSERT(lov->lov_qos.lq_rr_size);
350 /* Do actual allocation. */
351 down_write(&lov->lov_qos.lq_rw_sem);
354 * Check again. While we were sleeping on @lq_rw_sem something could
357 if (!lov->lov_qos.lq_dirty_rr) {
358 LASSERT(lov->lov_qos.lq_rr_size);
359 up_write(&lov->lov_qos.lq_rw_sem);
363 ost_count = lov->desc.ld_tgt_count;
365 if (lov->lov_qos.lq_rr_size)
366 OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
367 lov->lov_qos.lq_rr_size = ost_count *
368 sizeof(lov->lov_qos.lq_rr_array[0]);
369 OBD_ALLOC(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
370 if (!lov->lov_qos.lq_rr_array) {
371 lov->lov_qos.lq_rr_size = 0;
372 up_write(&lov->lov_qos.lq_rw_sem);
377 for (i = 0; i < ost_count; i++) {
378 lov->lov_qos.lq_rr_array[i] = LOV_QOS_EMPTY;
379 if (lov->lov_tgts[i])
383 /* Place all the OSTs from 1 OSS at the same time. */
385 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
387 for (i = 0; i < ost_count; i++) {
388 if (lov->lov_tgts[i] &&
389 lov->lov_tgts[i]->ltd_qos.ltq_oss == oss) {
390 /* Evenly space these OSTs across arrayspace */
391 int next = j * ost_count / oss->lqo_ost_count;
392 LASSERT(next < ost_count);
393 while (lov->lov_qos.lq_rr_array[next] !=
395 next = (next + 1) % ost_count;
396 lov->lov_qos.lq_rr_array[next] = i;
401 LASSERT(j == oss->lqo_ost_count);
404 lov->lov_qos.lq_dirty_rr = 0;
405 up_write(&lov->lov_qos.lq_rw_sem);
407 if (placed != real_count) {
408 /* This should never happen */
409 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
410 "round-robin list (%d of %d).\n",
412 for (i = 0; i < ost_count; i++) {
413 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
414 lov->lov_qos.lq_rr_array[i]);
416 lov->lov_qos.lq_dirty_rr = 1;
421 for (i = 0; i < ost_count; i++) {
422 LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i,
423 lov->lov_qos.lq_rr_array[i]);
431 void qos_shrink_lsm(struct lov_request_set *set)
433 struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
434 /* XXX LOV STACKING call into osc for sizes */
435 unsigned oldsize, newsize;
437 if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
438 struct llog_cookie *cookies;
439 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
440 newsize = set->set_count * sizeof(*cookies);
442 cookies = set->set_cookies;
443 oti_alloc_cookies(set->set_oti, set->set_count);
444 if (set->set_oti->oti_logcookies) {
445 memcpy(set->set_oti->oti_logcookies, cookies, newsize);
446 OBD_FREE(cookies, oldsize);
447 set->set_cookies = set->set_oti->oti_logcookies;
449 CWARN("'leaking' %d bytes\n", oldsize - newsize);
453 CWARN("using fewer stripes for object "LPU64": old %u new %u\n",
454 lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
455 LASSERT(lsm->lsm_stripe_count >= set->set_count);
457 newsize = lov_stripe_md_size(set->set_count);
458 OBD_ALLOC(lsm_new, newsize);
459 if (lsm_new != NULL) {
461 memcpy(lsm_new, lsm, sizeof(*lsm));
462 for (i = 0; i < lsm->lsm_stripe_count; i++) {
463 if (i < set->set_count) {
464 lsm_new->lsm_oinfo[i] = lsm->lsm_oinfo[i];
467 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
468 sizeof(struct lov_oinfo));
470 lsm_new->lsm_stripe_count = set->set_count;
471 OBD_FREE(lsm, sizeof(struct lov_stripe_md) +
472 lsm->lsm_stripe_count * sizeof(struct lov_oinfo *));
473 set->set_oi->oi_md = lsm_new;
475 CWARN("'leaking' few bytes\n");
479 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
481 struct lov_stripe_md *lsm = set->set_oi->oi_md;
482 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
483 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
484 int stripe, i, rc = -EIO;
487 ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
488 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
489 if (!lov->lov_tgts[ost_idx] ||
490 !lov->lov_tgts[ost_idx]->ltd_active)
492 /* check if objects has been created on this ost */
493 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
494 if (stripe == req->rq_stripe)
496 if (ost_idx == lsm->lsm_oinfo[stripe]->loi_ost_idx)
500 if (stripe >= lsm->lsm_stripe_count) {
501 req->rq_idx = ost_idx;
502 rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp,
503 req->rq_oi.oi_oa, &req->rq_oi.oi_md,
512 static int min_stripe_count(int stripe_cnt, int flags)
514 return (flags & LOV_USES_DEFAULT_STRIPE ?
515 stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
518 #define LOV_CREATE_RESEED_MULT 4
519 #define LOV_CREATE_RESEED_MIN 1000
520 /* Allocate objects on osts with round-robin algorithm */
521 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt,
524 unsigned array_idx, ost_count = lov->desc.ld_tgt_count;
525 unsigned ost_active_count = lov->desc.ld_active_tgt_count;
528 int ost_start_idx_temp;
530 int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
533 i = qos_calc_rr(lov);
537 if (--lov->lov_start_count <= 0) {
538 lov->lov_start_idx = ll_rand() % ost_count;
539 lov->lov_start_count =
540 (LOV_CREATE_RESEED_MIN / max(ost_active_count, 1U) +
541 LOV_CREATE_RESEED_MULT) * max(ost_active_count, 1U);
542 } else if (stripe_cnt_min >= ost_active_count ||
543 lov->lov_start_idx > ost_count) {
544 /* If we have allocated from all of the OSTs, slowly
545 * precess the next start if the OST/stripe count isn't
546 * already doing this for us. */
547 lov->lov_start_idx %= ost_count;
548 if (*stripe_cnt > 1 && (ost_active_count % (*stripe_cnt)) != 1)
549 ++lov->lov_offset_idx;
551 down_read(&lov->lov_qos.lq_rw_sem);
552 ost_start_idx_temp = lov->lov_start_idx;
555 array_idx = (lov->lov_start_idx + lov->lov_offset_idx) % ost_count;
558 CDEBUG(D_QOS, "want %d startidx %d startcnt %d offset %d active %d "
559 "count %d arrayidx %d\n",
560 stripe_cnt, lov->lov_start_idx, lov->lov_start_count,
561 lov->lov_offset_idx, ost_active_count, ost_count, array_idx);
564 for (i = 0; i < ost_count; i++, array_idx=(array_idx + 1) % ost_count) {
565 ++lov->lov_start_idx;
566 ost_idx = lov->lov_qos.lq_rr_array[array_idx];
568 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
569 i, lov->lov_start_idx,
570 ((ost_idx != LOV_QOS_EMPTY) && lov->lov_tgts[ost_idx]) ?
571 lov->lov_tgts[ost_idx]->ltd_active : 0,
572 idx_pos - idx_arr, array_idx, ost_idx);
574 if ((ost_idx == LOV_QOS_EMPTY) || !lov->lov_tgts[ost_idx] ||
575 !lov->lov_tgts[ost_idx]->ltd_active)
578 /* Fail Check before osc_precreate() is called
579 so we can only 'fail' single OSC. */
580 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
583 /* Drop slow OSCs if we can */
584 if (obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed)
589 /* We have enough stripes */
590 if (idx_pos - idx_arr == *stripe_cnt)
593 if ((speed < 2) && (idx_pos - idx_arr < stripe_cnt_min)) {
594 /* Try again, allowing slower OSCs */
596 lov->lov_start_idx = ost_start_idx_temp;
600 up_read(&lov->lov_qos.lq_rw_sem);
602 *stripe_cnt = idx_pos - idx_arr;
606 /* alloc objects on osts with specific stripe offset */
607 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
610 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
616 ost_idx = lsm->lsm_oinfo[0]->loi_ost_idx;
618 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
619 if (!lov->lov_tgts[ost_idx] ||
620 !lov->lov_tgts[ost_idx]->ltd_active) {
624 /* Fail Check before osc_precreate() is called
625 so we can only 'fail' single OSC. */
626 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
629 /* Drop slow OSCs if we can, but not for requested start idx */
630 if ((obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed) &&
631 (i != 0 || speed < 2))
636 /* We have enough stripes */
637 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
641 /* Try again, allowing slower OSCs */
646 /* If we were passed specific striping params, then a failure to
647 * meet those requirements is an error, since we can't reallocate
648 * that memory (it might be part of a larger array or something).
650 * We can only get here if lsm_stripe_count was originally > 1.
652 CERROR("can't lstripe objid "LPX64": have %d want %u\n",
653 lsm->lsm_object_id, (int)(idx_pos - idx_arr),
654 lsm->lsm_stripe_count);
658 /* Alloc objects on osts with optimization based on:
660 - network resources (shared OSS's)
662 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt,
665 struct lov_obd *lov = &exp->exp_obd->u.lov;
666 static time_t last_warn = 0;
667 time_t now = cfs_time_current_sec();
668 __u64 total_bavail, total_weight = 0;
670 int nfound, good_osts, i, warn = 0, rc = 0;
671 int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
674 if (stripe_cnt_min < 1)
675 GOTO(out_nolock, rc = -EINVAL);
677 lov_getref(exp->exp_obd);
679 /* Detect -EAGAIN early, before expensive lock is taken. */
680 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
681 GOTO(out_nolock, rc = -EAGAIN);
683 /* Do actual allocation, use write lock here. */
684 down_write(&lov->lov_qos.lq_rw_sem);
687 * Check again, while we were sleeping on @lq_rw_sem things could
690 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
691 GOTO(out, rc = -EAGAIN);
693 ost_count = lov->desc.ld_tgt_count;
695 if (lov->desc.ld_active_tgt_count < 2)
696 GOTO(out, rc = -EAGAIN);
698 rc = qos_calc_ppo(exp->exp_obd);
704 /* Warn users about zero available space/inode every 30 min */
705 if (cfs_time_sub(now, last_warn) > 60 * 30)
707 /* Find all the OSTs that are valid stripe candidates */
708 for (i = 0; i < ost_count; i++) {
711 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
713 bavail = TGT_BAVAIL(i);
716 CDEBUG(D_QOS, "no free space on %s\n",
717 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
724 CDEBUG(D_QOS, "no free inodes on %s\n",
725 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
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) && i == 0)
736 if (obd_precreate(lov->lov_tgts[i]->ltd_exp) > 2)
739 lov->lov_tgts[i]->ltd_qos.ltq_usable = 1;
740 qos_calc_weight(lov, i);
741 total_bavail += bavail;
742 total_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
747 if (good_osts < stripe_cnt_min)
748 GOTO(out, rc = -EAGAIN);
751 GOTO(out, rc = -ENOSPC);
753 /* We have enough osts */
754 if (good_osts < *stripe_cnt)
755 *stripe_cnt = good_osts;
758 GOTO(out, rc = -EAGAIN);
760 /* Find enough OSTs with weighted random allocation. */
762 while (nfound < *stripe_cnt) {
763 __u64 rand, cur_weight;
769 #if BITS_PER_LONG == 32
770 rand = ll_rand() % (unsigned)total_weight;
771 /* If total_weight > 32-bit, first generate the high
772 * 32 bits of the random number, then add in the low
773 * 32 bits (truncated to the upper limit, if needed) */
774 if (total_weight > 0xffffffffULL)
775 rand = (__u64)(ll_rand() %
776 (unsigned)(total_weight >> 32)) << 32;
780 if (rand == (total_weight & 0xffffffff00000000ULL))
781 rand |= ll_rand() % (unsigned)total_weight;
786 rand = ((__u64)ll_rand() << 32 | ll_rand()) %
793 /* On average, this will hit larger-weighted osts more often.
794 0-weight osts will always get used last (only when rand=0).*/
795 for (i = 0; i < ost_count; i++) {
796 if (!lov->lov_tgts[i] ||
797 !lov->lov_tgts[i]->ltd_qos.ltq_usable)
800 cur_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
801 if (cur_weight >= rand) {
803 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
806 idx_arr[nfound++] = i;
807 qos_used(lov, i, &total_weight);
812 /* should never satisfy below condition */
814 CERROR("Didn't find any OSTs?\n");
818 LASSERT(nfound == *stripe_cnt);
821 up_write(&lov->lov_qos.lq_rw_sem);
825 rc = alloc_rr(lov, idx_arr, stripe_cnt, flags);
827 lov_putref(exp->exp_obd);
831 /* return new alloced stripe count on success */
832 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
833 int newea, int **idx_arr, int *arr_cnt, int flags)
835 struct lov_obd *lov = &exp->exp_obd->u.lov;
836 int stripe_cnt = lsm->lsm_stripe_count;
841 *arr_cnt = stripe_cnt;
842 OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
845 for (i = 0; i < *arr_cnt; i++)
849 lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count)
850 rc = alloc_qos(exp, tmp_arr, &stripe_cnt, flags);
852 rc = alloc_specific(lov, lsm, tmp_arr);
860 OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
865 static void free_idx_array(int *idx_arr, int arr_cnt)
868 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
871 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
873 struct lov_obd *lov = &exp->exp_obd->u.lov;
874 struct lov_stripe_md *lsm;
875 struct obdo *src_oa = set->set_oi->oi_oa;
876 struct obd_trans_info *oti = set->set_oti;
877 int i, stripes, rc = 0, newea = 0;
878 int flag = LOV_USES_ASSIGNED_STRIPE;
879 int *idx_arr = NULL, idx_cnt = 0;
882 LASSERT(src_oa->o_valid & OBD_MD_FLID);
883 LASSERT(src_oa->o_valid & OBD_MD_FLGROUP);
885 if (set->set_oi->oi_md == NULL) {
886 int stripes_def = lov_get_stripecnt(lov, 0);
888 /* If the MDS file was truncated up to some size, stripe over
889 * enough OSTs to allow the file to be created at that size.
890 * This may mean we use more than the default # of stripes. */
891 if (src_oa->o_valid & OBD_MD_FLSIZE) {
892 obd_size min_bavail = LUSTRE_STRIPE_MAXBYTES;
894 /* Find a small number of stripes we can use
895 (up to # of active osts). */
897 lov_getref(exp->exp_obd);
898 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
899 if (!lov->lov_tgts[i] ||
900 !lov->lov_tgts[i]->ltd_active)
902 min_bavail = min(min_bavail, TGT_BAVAIL(i));
903 if (min_bavail * stripes > src_oa->o_size)
907 lov_putref(exp->exp_obd);
909 if (stripes < stripes_def)
910 stripes = stripes_def;
912 flag = LOV_USES_DEFAULT_STRIPE;
913 stripes = stripes_def;
916 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
917 lov->desc.ld_pattern ?
918 lov->desc.ld_pattern : LOV_PATTERN_RAID0,
926 lsm = set->set_oi->oi_md;
927 lsm->lsm_object_id = src_oa->o_id;
928 lsm->lsm_object_gr = src_oa->o_gr;
930 if (!lsm->lsm_stripe_size)
931 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
932 if (!lsm->lsm_pattern) {
933 LASSERT(lov->desc.ld_pattern);
934 lsm->lsm_pattern = lov->desc.ld_pattern;
937 stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt, flag);
939 GOTO(out_err, rc = stripes ? stripes : -EIO);
940 LASSERTF(stripes <= lsm->lsm_stripe_count,"requested %d allocated %d\n",
941 lsm->lsm_stripe_count, stripes);
943 for (i = 0; i < stripes; i++) {
944 struct lov_request *req;
945 int ost_idx = idx_arr[i];
946 LASSERT(ost_idx >= 0);
948 OBD_ALLOC(req, sizeof(*req));
950 GOTO(out_err, rc = -ENOMEM);
951 lov_set_add_req(req, set);
953 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
954 OBD_ALLOC(req->rq_oi.oi_md, req->rq_buflen);
955 if (req->rq_oi.oi_md == NULL)
956 GOTO(out_err, rc = -ENOMEM);
958 OBDO_ALLOC(req->rq_oi.oi_oa);
959 if (req->rq_oi.oi_oa == NULL)
960 GOTO(out_err, rc = -ENOMEM);
962 req->rq_idx = ost_idx;
964 /* create data objects with "parent" OA */
965 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
967 /* XXX When we start creating objects on demand, we need to
968 * make sure that we always create the object on the
969 * stripe which holds the existing file size.
971 if (src_oa->o_valid & OBD_MD_FLSIZE) {
972 req->rq_oi.oi_oa->o_size =
973 lov_size_to_stripe(lsm, src_oa->o_size, i);
975 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
976 i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
979 LASSERT(set->set_count == stripes);
981 if (stripes < lsm->lsm_stripe_count)
984 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
985 oti_alloc_cookies(oti, set->set_count);
986 if (!oti->oti_logcookies)
987 GOTO(out_err, rc = -ENOMEM);
988 set->set_cookies = oti->oti_logcookies;
992 obd_free_memmd(exp, &set->set_oi->oi_md);
994 free_idx_array(idx_arr, idx_cnt);
999 void qos_update(struct lov_obd *lov)
1002 lov->lov_qos.lq_dirty = 1;