1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_LOV
31 #include <libcfs/libcfs.h>
33 #include <liblustre.h>
36 #include <obd_class.h>
38 #include "lov_internal.h"
41 /* #define QOS_DEBUG 1 */
44 #define TGT_BAVAIL(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail * \
45 lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize)
46 #define TGT_FFREE(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_ffree)
49 int qos_add_tgt(struct obd_device *obd, __u32 index)
51 struct lov_obd *lov = &obd->u.lov;
52 struct lov_qos_oss *oss, *temposs;
53 struct obd_export *exp = lov->lov_tgts[index]->ltd_exp;
54 int rc = 0, found = 0;
57 /* We only need this QOS struct on MDT, not clients - but we may not
58 have registered the LOV's observer yet, so there's no way to know */
59 if (!exp || !exp->exp_connection) {
60 CERROR("Missing connection\n");
64 down_write(&lov->lov_qos.lq_rw_sem);
65 mutex_down(&lov->lov_lock);
66 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
67 if (obd_uuid_equals(&oss->lqo_uuid,
68 &exp->exp_connection->c_remote_uuid)) {
77 GOTO(out, rc = -ENOMEM);
78 memcpy(&oss->lqo_uuid,
79 &exp->exp_connection->c_remote_uuid,
80 sizeof(oss->lqo_uuid));
82 /* Assume we have to move this one */
83 list_del(&oss->lqo_oss_list);
87 lov->lov_tgts[index]->ltd_qos.ltq_oss = oss;
89 /* Add sorted by # of OSTs. Find the first entry that we're
91 list_for_each_entry(temposs, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
92 if (oss->lqo_ost_count > temposs->lqo_ost_count)
95 /* ...and add before it. If we're the first or smallest, temposs
96 points to the list head, and we add to the end. */
97 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
99 lov->lov_qos.lq_dirty = 1;
100 lov->lov_qos.lq_dirty_rr = 1;
102 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
103 obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
104 obd_uuid2str(&oss->lqo_uuid),
108 mutex_up(&lov->lov_lock);
109 up_write(&lov->lov_qos.lq_rw_sem);
113 int qos_del_tgt(struct obd_device *obd, __u32 index)
115 struct lov_obd *lov = &obd->u.lov;
116 struct lov_qos_oss *oss;
120 if (!lov->lov_tgts[index])
123 down_write(&lov->lov_qos.lq_rw_sem);
125 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
127 GOTO(out, rc = -ENOENT);
129 oss->lqo_ost_count--;
130 if (oss->lqo_ost_count == 0) {
131 CDEBUG(D_QOS, "removing OSS %s\n",
132 obd_uuid2str(&oss->lqo_uuid));
133 list_del(&oss->lqo_oss_list);
137 lov->lov_qos.lq_dirty = 1;
138 lov->lov_qos.lq_dirty_rr = 1;
140 up_write(&lov->lov_qos.lq_rw_sem);
144 /* Recalculate per-object penalties for OSSs and OSTs,
145 depends on size of each ost in an oss */
146 static int qos_calc_ppo(struct obd_device *obd)
148 struct lov_obd *lov = &obd->u.lov;
149 struct lov_qos_oss *oss;
150 __u64 ba_max, ba_min, temp;
152 int rc, i, prio_wide;
155 if (!lov->lov_qos.lq_dirty)
158 num_active = lov->desc.ld_active_tgt_count - 1;
160 GOTO(out, rc = -EAGAIN);
162 /* find bavail on each OSS */
163 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
166 lov->lov_qos.lq_active_oss_count = 0;
168 /* How badly user wants to select osts "widely" (not recently chosen
169 and not on recent oss's). As opposed to "freely" (free space
171 prio_wide = 256 - lov->lov_qos.lq_prio_free;
173 ba_min = (__u64)(-1);
175 /* Calculate OST penalty per object */
176 /* (lov ref taken in alloc_qos) */
177 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
178 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
180 temp = TGT_BAVAIL(i);
183 ba_min = min(temp, ba_min);
184 ba_max = max(temp, ba_max);
186 /* Count the number of usable OSS's */
187 if (lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail == 0)
188 lov->lov_qos.lq_active_oss_count++;
189 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail += temp;
191 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
193 do_div(temp, num_active);
194 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj =
195 (temp * prio_wide) >> 8;
197 if (lov->lov_qos.lq_reset == 0)
198 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
201 num_active = lov->lov_qos.lq_active_oss_count - 1;
202 if (num_active < 1) {
203 /* If there's only 1 OSS, we can't penalize it, so instead
204 we have to double the OST penalty */
206 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
207 if (lov->lov_tgts[i] == NULL)
209 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
213 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
214 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
215 temp = oss->lqo_bavail >> 1;
216 do_div(temp, oss->lqo_ost_count * num_active);
217 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
218 if (lov->lov_qos.lq_reset == 0)
219 oss->lqo_penalty = 0;
222 lov->lov_qos.lq_dirty = 0;
223 lov->lov_qos.lq_reset = 0;
225 /* If each ost has almost same free space,
226 * do rr allocation for better creation performance */
227 lov->lov_qos.lq_same_space = 0;
228 temp = ba_max - ba_min;
229 ba_min = (ba_min * 51) >> 8; /* 51/256 = .20 */
231 /* Difference is less than 20% */
232 lov->lov_qos.lq_same_space = 1;
233 /* Reset weights for the next time we enter qos mode */
234 lov->lov_qos.lq_reset = 0;
239 if (!rc && lov->lov_qos.lq_same_space)
244 static int qos_calc_weight(struct lov_obd *lov, int i)
248 /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
249 temp = TGT_BAVAIL(i);
250 temp2 = lov->lov_tgts[i]->ltd_qos.ltq_penalty +
251 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty;
253 lov->lov_tgts[i]->ltd_qos.ltq_weight = 0;
255 lov->lov_tgts[i]->ltd_qos.ltq_weight = temp - temp2;
259 /* We just used this index for a stripe; adjust everyone's weights */
260 static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
262 struct lov_qos_oss *oss;
266 /* Don't allocate from this stripe anymore, until the next alloc_qos */
267 lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
269 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
271 /* Decay old penalty by half (we're adding max penalty, and don't
272 want it to run away.) */
273 lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
274 oss->lqo_penalty >>= 1;
276 /* Set max penalties for this OST and OSS */
277 lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
278 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
279 lov->desc.ld_active_tgt_count;
280 oss->lqo_penalty += oss->lqo_penalty_per_obj *
281 lov->lov_qos.lq_active_oss_count;
283 /* Decrease all OSS penalties */
284 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
285 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
286 oss->lqo_penalty = 0;
288 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
292 /* Decrease all OST penalties */
293 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
294 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
296 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
297 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
298 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
300 lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
301 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
303 qos_calc_weight(lov, i);
305 /* Recalc the total weight of usable osts */
306 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
307 *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
310 CDEBUG(D_QOS, "recalc tgt %d avail="LPU64
311 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
312 " ossp="LPU64" wt="LPU64"\n",
313 i, TGT_BAVAIL(i) >> 10,
314 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj >> 10,
315 lov->lov_tgts[i]->ltd_qos.ltq_penalty >> 10,
316 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
317 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty >> 10,
318 lov->lov_tgts[i]->ltd_qos.ltq_weight >> 10);
325 #define LOV_QOS_EMPTY ((__u32)-1)
326 /* compute optimal round-robin order, based on OSTs per OSS */
327 static int qos_calc_rr(struct lov_obd *lov)
329 struct lov_qos_oss *oss;
330 unsigned ost_count, placed, real_count;
334 if (!lov->lov_qos.lq_dirty_rr) {
335 LASSERT(lov->lov_qos.lq_rr_size);
339 /* Do actuall allocation. */
340 down_write(&lov->lov_qos.lq_rw_sem);
343 * Check again. While we were sleeping on @lq_rw_sem something could
346 if (!lov->lov_qos.lq_dirty_rr) {
347 LASSERT(lov->lov_qos.lq_rr_size);
348 up_write(&lov->lov_qos.lq_rw_sem);
352 ost_count = lov->desc.ld_tgt_count;
354 if (lov->lov_qos.lq_rr_size)
355 OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
356 lov->lov_qos.lq_rr_size = ost_count *
357 sizeof(lov->lov_qos.lq_rr_array[0]);
358 OBD_ALLOC(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
359 if (!lov->lov_qos.lq_rr_array) {
360 lov->lov_qos.lq_rr_size = 0;
361 up_write(&lov->lov_qos.lq_rw_sem);
366 for (i = 0; i < ost_count; i++) {
367 lov->lov_qos.lq_rr_array[i] = LOV_QOS_EMPTY;
368 if (lov->lov_tgts[i])
372 /* Place all the OSTs from 1 OSS at the same time. */
374 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
376 for (i = 0; i < ost_count; i++) {
377 LASSERT(lov->lov_tgts[i] != NULL);
378 if (lov->lov_tgts[i]->ltd_qos.ltq_oss == oss) {
379 /* Evenly space these OSTs across arrayspace */
380 int next = j * ost_count / oss->lqo_ost_count;
381 LASSERT(next < ost_count);
382 while (lov->lov_qos.lq_rr_array[next] !=
384 next = (next + 1) % ost_count;
385 lov->lov_qos.lq_rr_array[next] = i;
390 LASSERT(j == oss->lqo_ost_count);
393 lov->lov_qos.lq_dirty_rr = 0;
394 up_write(&lov->lov_qos.lq_rw_sem);
396 if (placed != real_count) {
397 /* This should never happen */
398 LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
399 "round-robin list (%d of %d).\n",
401 for (i = 0; i < ost_count; i++) {
402 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
403 lov->lov_qos.lq_rr_array[i]);
405 lov->lov_qos.lq_dirty_rr = 1;
410 for (i = 0; i < ost_count; i++) {
411 LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i,
412 lov->lov_qos.lq_rr_array[i]);
420 void qos_shrink_lsm(struct lov_request_set *set)
422 struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
423 /* XXX LOV STACKING call into osc for sizes */
424 unsigned oldsize, newsize;
426 if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
427 struct llog_cookie *cookies;
428 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
429 newsize = set->set_count * sizeof(*cookies);
431 cookies = set->set_cookies;
432 oti_alloc_cookies(set->set_oti, set->set_count);
433 if (set->set_oti->oti_logcookies) {
434 memcpy(set->set_oti->oti_logcookies, cookies, newsize);
435 OBD_FREE(cookies, oldsize);
436 set->set_cookies = set->set_oti->oti_logcookies;
438 CWARN("'leaking' %d bytes\n", oldsize - newsize);
442 CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
443 lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
444 LASSERT(lsm->lsm_stripe_count >= set->set_count);
446 newsize = lov_stripe_md_size(set->set_count);
447 OBD_ALLOC(lsm_new, newsize);
448 if (lsm_new != NULL) {
450 memcpy(lsm_new, lsm, sizeof(*lsm));
451 for (i = 0; i < lsm->lsm_stripe_count; i++) {
452 if (i < set->set_count) {
453 lsm_new->lsm_oinfo[i] = lsm->lsm_oinfo[i];
456 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
457 sizeof(struct lov_oinfo));
459 lsm_new->lsm_stripe_count = set->set_count;
460 OBD_FREE(lsm, sizeof(struct lov_stripe_md) +
461 lsm->lsm_stripe_count * sizeof(struct lov_oinfo *));
462 set->set_oi->oi_md = lsm_new;
464 CWARN("'leaking' few bytes\n");
468 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
470 struct lov_stripe_md *lsm = set->set_oi->oi_md;
471 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
472 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
473 int stripe, i, rc = -EIO;
476 ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
477 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
478 if (!lov->lov_tgts[ost_idx] ||
479 !lov->lov_tgts[ost_idx]->ltd_active)
481 /* check if objects has been created on this ost */
482 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
483 if (stripe == req->rq_stripe)
485 if (ost_idx == lsm->lsm_oinfo[stripe]->loi_ost_idx)
489 if (stripe >= lsm->lsm_stripe_count) {
490 req->rq_idx = ost_idx;
491 rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp,
492 req->rq_oi.oi_oa, &req->rq_oi.oi_md,
501 #define LOV_CREATE_RESEED_MULT 4
502 #define LOV_CREATE_RESEED_MIN 1000
503 /* Allocate objects on osts with round-robin algorithm */
504 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt)
506 unsigned array_idx, ost_count = lov->desc.ld_tgt_count;
507 unsigned ost_active_count = lov->desc.ld_active_tgt_count;
508 int i, *idx_pos = idx_arr;
512 i = qos_calc_rr(lov);
516 if (--lov->lov_start_count <= 0) {
517 lov->lov_start_idx = ll_rand() % ost_count;
518 lov->lov_start_count =
519 (LOV_CREATE_RESEED_MIN / max(ost_active_count, 1U) +
520 LOV_CREATE_RESEED_MULT) * max(ost_active_count, 1U);
521 } else if (*stripe_cnt >= ost_active_count ||
522 lov->lov_start_idx > ost_count) {
523 /* If we have allocated from all of the OSTs, slowly
524 precess the next start */
525 lov->lov_start_idx %= ost_count;
526 ++lov->lov_offset_idx;
528 array_idx = (lov->lov_start_idx + lov->lov_offset_idx) % ost_count;
530 CDEBUG(D_QOS, "want %d startidx %d startcnt %d offset %d arrayidx %d\n",
531 *stripe_cnt, lov->lov_start_idx, lov->lov_start_count,
532 lov->lov_offset_idx, array_idx);
535 down_read(&lov->lov_qos.lq_rw_sem);
536 for (i = 0; i < ost_count; i++, array_idx=(array_idx + 1) % ost_count) {
537 ++lov->lov_start_idx;
538 ost_idx = lov->lov_qos.lq_rr_array[array_idx];
540 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
541 i, lov->lov_start_idx,
542 ((ost_idx != LOV_QOS_EMPTY) && lov->lov_tgts[ost_idx]) ?
543 lov->lov_tgts[ost_idx]->ltd_active : 0,
544 idx_pos - idx_arr, array_idx, ost_idx);
546 if ((ost_idx == LOV_QOS_EMPTY) || !lov->lov_tgts[ost_idx] ||
547 !lov->lov_tgts[ost_idx]->ltd_active)
551 /* We have enough stripes */
552 if (idx_pos - idx_arr == *stripe_cnt)
555 up_read(&lov->lov_qos.lq_rw_sem);
557 *stripe_cnt = idx_pos - idx_arr;
561 /* alloc objects on osts with specific stripe offset */
562 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
565 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
566 int i, *idx_pos = idx_arr;
569 ost_idx = lsm->lsm_oinfo[0]->loi_ost_idx;
570 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
571 if (!lov->lov_tgts[ost_idx] ||
572 !lov->lov_tgts[ost_idx]->ltd_active) {
578 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
581 /* If we were passed specific striping params, then a failure to
582 * meet those requirements is an error, since we can't reallocate
583 * that memory (it might be part of a larger array or something).
585 * We can only get here if lsm_stripe_count was originally > 1.
587 CERROR("can't lstripe objid "LPX64": have "LPSZ" want %u\n",
588 lsm->lsm_object_id, idx_pos - idx_arr, lsm->lsm_stripe_count);
592 /* Alloc objects on osts with optimization based on:
594 - network resources (shared OSS's)
596 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
598 struct lov_obd *lov = &exp->exp_obd->u.lov;
599 static time_t last_warn = 0;
600 time_t now = cfs_time_current_sec();
601 __u64 total_bavail, total_weight = 0;
603 int nfound, good_osts, i, warn = 0, rc = 0;
606 lov_getref(exp->exp_obd);
608 /* Detect -EAGAIN early, before expensive lock is taken. */
609 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
610 GOTO(out, rc = -EAGAIN);
612 /* Do actuall allocation, use write lock here. */
613 down_write(&lov->lov_qos.lq_rw_sem);
616 * Check again, while we were sleeping on @lq_rw_sem things could
619 if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space) {
620 up_write(&lov->lov_qos.lq_rw_sem);
621 GOTO(out, rc = -EAGAIN);
623 ost_count = lov->desc.ld_tgt_count;
625 if (lov->desc.ld_active_tgt_count < 2)
626 GOTO(out_up_write, rc = -EAGAIN);
628 rc = qos_calc_ppo(exp->exp_obd);
630 GOTO(out_up_write, rc);
634 /* Warn users about zero available space/inode every 30 min */
635 if (cfs_time_sub(now, last_warn) > 60 * 30)
637 /* Find all the OSTs that are valid stripe candidates */
638 for (i = 0; i < ost_count; i++) {
641 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
643 bavail = TGT_BAVAIL(i);
646 CDEBUG(D_QOS, "no free space on %s\n",
647 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
654 CDEBUG(D_QOS, "no free inodes on %s\n",
655 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
661 lov->lov_tgts[i]->ltd_qos.ltq_usable = 1;
662 qos_calc_weight(lov, i);
663 total_bavail += bavail;
664 total_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
670 GOTO(out_up_write, rc = -ENOSPC);
672 /* if we don't have enough good OSTs, we reduce the stripe count. */
673 if (good_osts < *stripe_cnt)
674 *stripe_cnt = good_osts;
677 GOTO(out_up_write, rc = -EAGAIN);
679 /* Find enough OSTs with weighted random allocation. */
681 while (nfound < *stripe_cnt) {
682 __u64 rand, cur_weight = 0;
687 #if BITS_PER_LONG == 32
688 rand = ll_rand() % (unsigned)total_weight;
689 /* If total_weight > 32-bit, first generate the high
690 * 32 bits of the random number, then add in the low
691 * 32 bits (truncated to the upper limit, if needed) */
692 if (total_weight > 0xffffffffULL)
693 rand = (__u64)(ll_rand() %
694 (unsigned)(total_weight >> 32)) << 32;
698 if (rand == (total_weight & 0xffffffff00000000ULL))
699 rand |= ll_rand() % (unsigned)total_weight;
703 rand = ((__u64)ll_rand() << 32 | ll_rand()) %
710 /* On average, this will hit larger-weighted osts more often.
711 0-weight osts will always get used last (only when rand=0).*/
712 for (i = 0; i < ost_count; i++) {
713 if (!lov->lov_tgts[i] ||
714 !lov->lov_tgts[i]->ltd_qos.ltq_usable)
716 cur_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
717 if (cur_weight >= rand) {
719 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
722 idx_arr[nfound++] = i;
723 qos_used(lov, i, &total_weight);
728 /* should never satisfy below condition */
730 CERROR("Didn't find any OSTs?\n");
734 LASSERT(nfound == *stripe_cnt);
737 up_write(&lov->lov_qos.lq_rw_sem);
741 rc = alloc_rr(lov, idx_arr, stripe_cnt);
743 lov_putref(exp->exp_obd);
747 /* return new alloced stripe count on success */
748 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
749 int newea, int **idx_arr, int *arr_cnt)
751 struct lov_obd *lov = &exp->exp_obd->u.lov;
752 int stripe_cnt = lsm->lsm_stripe_count;
757 *arr_cnt = stripe_cnt;
758 OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
761 for (i = 0; i < *arr_cnt; i++)
765 lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count)
766 rc = alloc_qos(exp, tmp_arr, &stripe_cnt);
768 rc = alloc_specific(lov, lsm, tmp_arr);
776 OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
781 static void free_idx_array(int *idx_arr, int arr_cnt)
784 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
787 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
789 struct lov_obd *lov = &exp->exp_obd->u.lov;
790 struct lov_stripe_md *lsm;
791 struct obdo *src_oa = set->set_oi->oi_oa;
792 struct obd_trans_info *oti = set->set_oti;
793 int i, stripes, rc = 0, newea = 0;
794 int *idx_arr, idx_cnt = 0;
797 LASSERT(src_oa->o_valid & OBD_MD_FLID);
798 LASSERT(src_oa->o_valid & OBD_MD_FLGROUP);
800 if (set->set_oi->oi_md == NULL) {
801 int stripe_cnt = lov_get_stripecnt(lov, 0);
803 /* If the MDS file was truncated up to some size, stripe over
804 * enough OSTs to allow the file to be created at that size.
805 * This may mean we use more than the default # of stripes. */
806 if (src_oa->o_valid & OBD_MD_FLSIZE) {
807 obd_size min_bavail = LUSTRE_STRIPE_MAXBYTES;
809 /* Find a small number of stripes we can use
810 (up to # of active osts). */
812 lov_getref(exp->exp_obd);
813 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
814 if (!lov->lov_tgts[i] ||
815 !lov->lov_tgts[i]->ltd_active)
817 min_bavail = min(min_bavail, TGT_BAVAIL(i));
818 if (min_bavail * stripes > src_oa->o_size)
822 lov_putref(exp->exp_obd);
824 if (stripes < stripe_cnt)
825 stripes = stripe_cnt;
827 stripes = stripe_cnt;
830 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
831 lov->desc.ld_pattern ?
832 lov->desc.ld_pattern : LOV_PATTERN_RAID0,
840 lsm = set->set_oi->oi_md;
841 lsm->lsm_object_id = src_oa->o_id;
842 lsm->lsm_object_gr = src_oa->o_gr;
844 if (!lsm->lsm_stripe_size)
845 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
846 if (!lsm->lsm_pattern) {
847 LASSERT(lov->desc.ld_pattern);
848 lsm->lsm_pattern = lov->desc.ld_pattern;
851 stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt);
853 GOTO(out_err, rc = stripes ? stripes : -EIO);
854 LASSERTF(stripes <= lsm->lsm_stripe_count,"requested %d allocated %d\n",
855 lsm->lsm_stripe_count, stripes);
857 for (i = 0; i < stripes; i++) {
858 struct lov_request *req;
859 int ost_idx = idx_arr[i];
860 LASSERT(ost_idx >= 0);
862 OBD_ALLOC(req, sizeof(*req));
864 GOTO(out_err, rc = -ENOMEM);
865 lov_set_add_req(req, set);
867 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
868 OBD_ALLOC(req->rq_oi.oi_md, req->rq_buflen);
869 if (req->rq_oi.oi_md == NULL)
870 GOTO(out_err, rc = -ENOMEM);
872 OBDO_ALLOC(req->rq_oi.oi_oa);
873 if (req->rq_oi.oi_oa == NULL)
874 GOTO(out_err, rc = -ENOMEM);
876 req->rq_idx = ost_idx;
878 /* create data objects with "parent" OA */
879 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
881 /* XXX When we start creating objects on demand, we need to
882 * make sure that we always create the object on the
883 * stripe which holds the existing file size.
885 if (src_oa->o_valid & OBD_MD_FLSIZE) {
886 req->rq_oi.oi_oa->o_size =
887 lov_size_to_stripe(lsm, src_oa->o_size, i);
889 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
890 i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
893 LASSERT(set->set_count == stripes);
895 if (stripes < lsm->lsm_stripe_count)
898 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
899 oti_alloc_cookies(oti, set->set_count);
900 if (!oti->oti_logcookies)
901 GOTO(out_err, rc = -ENOMEM);
902 set->set_cookies = oti->oti_logcookies;
906 obd_free_memmd(exp, &set->set_oi->oi_md);
907 free_idx_array(idx_arr, idx_cnt);
912 void qos_update(struct lov_obd *lov)
915 lov->lov_qos.lq_dirty = 1;