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"
40 /* #define QOS_DEBUG 1 */
43 #define TGT_BAVAIL(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail*\
44 lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize)
45 #define TGT_FFREE(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_ffree)
48 int qos_add_tgt(struct obd_device *obd, __u32 index)
50 struct lov_obd *lov = &obd->u.lov;
51 struct lov_qos_oss *oss, *temposs;
52 struct obd_export *exp = lov->lov_tgts[index]->ltd_exp;
53 int rc = 0, found = 0;
56 /* We only need this QOS struct on MDT, not clients - but we may not
57 * have registered the LOV's observer yet, so there's no way to know */
58 if (!exp || !exp->exp_connection) {
59 CERROR("Missing connection\n");
63 down_write(&lov->lov_qos.lq_rw_sem);
64 mutex_down(&lov->lov_lock);
65 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
66 if (obd_uuid_equals(&oss->lqo_uuid,
67 &exp->exp_connection->c_remote_uuid)) {
76 GOTO(out, rc = -ENOMEM);
77 memcpy(&oss->lqo_uuid,
78 &exp->exp_connection->c_remote_uuid,
79 sizeof(oss->lqo_uuid));
81 /* Assume we have to move this one */
82 list_del(&oss->lqo_oss_list);
86 lov->lov_tgts[index]->ltd_qos.ltq_oss = oss;
88 /* Add sorted by # of OSTs. Find the first entry that we're
90 list_for_each_entry(temposs, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
91 if (oss->lqo_ost_count > temposs->lqo_ost_count)
94 /* ...and add before it. If we're the first or smallest, temposs
95 points to the list head, and we add to the end. */
96 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
98 lov->lov_qos.lq_dirty = 1;
99 lov->lov_qos.lq_dirty_rr = 1;
101 CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
102 obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
103 obd_uuid2str(&oss->lqo_uuid),
107 mutex_up(&lov->lov_lock);
108 up_write(&lov->lov_qos.lq_rw_sem);
112 int qos_del_tgt(struct obd_device *obd, __u32 index)
114 struct lov_obd *lov = &obd->u.lov;
115 struct lov_qos_oss *oss;
119 if (!lov->lov_tgts[index])
122 down_write(&lov->lov_qos.lq_rw_sem);
124 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
126 GOTO(out, rc = -ENOENT);
128 oss->lqo_ost_count--;
129 if (oss->lqo_ost_count == 0) {
130 CDEBUG(D_QOS, "removing OSS %s\n",
131 obd_uuid2str(&oss->lqo_uuid));
132 list_del(&oss->lqo_oss_list);
136 lov->lov_qos.lq_dirty = 1;
137 lov->lov_qos.lq_dirty_rr = 1;
139 up_write(&lov->lov_qos.lq_rw_sem);
143 /* Recalculate per-object penalties for OSSs and OSTs,
144 depends on size of each ost in an oss */
145 static int qos_calc_ppo(struct obd_device *obd)
147 struct lov_obd *lov = &obd->u.lov;
148 struct lov_qos_oss *oss;
149 __u64 ba_max, ba_min, temp;
151 int rc, i, prio_wide;
154 if (!lov->lov_qos.lq_dirty)
157 num_active = lov->desc.ld_active_tgt_count - 1;
159 GOTO(out, rc = -EAGAIN);
161 /* find bavail on each OSS */
162 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
165 lov->lov_qos.lq_active_oss_count = 0;
167 /* How badly user wants to select osts "widely" (not recently chosen
168 and not on recent oss's). As opposed to "freely" (free space
170 prio_wide = 256 - lov->lov_qos.lq_prio_free;
172 ba_min = (__u64)(-1);
174 /* Calculate OST penalty per object */
175 /* (lov ref taken in alloc_qos) */
176 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
177 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
179 temp = TGT_BAVAIL(i);
182 ba_min = min(temp, ba_min);
183 ba_max = max(temp, ba_max);
185 /* Count the number of usable OSS's */
186 if (lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail == 0)
187 lov->lov_qos.lq_active_oss_count++;
188 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_bavail += temp;
190 /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
192 do_div(temp, num_active);
193 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj =
194 (temp * prio_wide) >> 8;
196 if (lov->lov_qos.lq_reset == 0)
197 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
200 num_active = lov->lov_qos.lq_active_oss_count - 1;
201 if (num_active < 1) {
202 /* If there's only 1 OSS, we can't penalize it, so instead
203 we have to double the OST penalty */
205 for (i = 0; i < lov->desc.ld_tgt_count; i++)
206 if (lov->lov_tgts[i])
207 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
210 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
211 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
212 temp = oss->lqo_bavail >> 1;
213 do_div(temp, oss->lqo_ost_count * num_active);
214 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
215 if (lov->lov_qos.lq_reset == 0)
216 oss->lqo_penalty = 0;
219 lov->lov_qos.lq_dirty = 0;
220 lov->lov_qos.lq_reset = 0;
222 /* If each ost has almost same free space,
223 * do rr allocation for better creation performance */
224 lov->lov_qos.lq_same_space = 0;
225 temp = ba_max - ba_min;
226 ba_min = (ba_min * 51) >> 8; /* 51/256 = .20 */
228 /* Difference is less than 20% */
229 lov->lov_qos.lq_same_space = 1;
230 /* Reset weights for the next time we enter qos mode */
231 lov->lov_qos.lq_reset = 0;
236 if (!rc && lov->lov_qos.lq_same_space)
241 static int qos_calc_weight(struct lov_obd *lov, int i)
245 /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
246 temp = TGT_BAVAIL(i);
247 temp2 = lov->lov_tgts[i]->ltd_qos.ltq_penalty +
248 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty;
250 lov->lov_tgts[i]->ltd_qos.ltq_weight = 0;
252 lov->lov_tgts[i]->ltd_qos.ltq_weight = temp - temp2;
256 /* We just used this index for a stripe; adjust everyone's weights */
257 static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
259 struct lov_qos_oss *oss;
263 /* Don't allocate from this stripe anymore, until the next alloc_qos */
264 lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
266 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
268 /* Decay old penalty by half (we're adding max penalty, and don't
269 want it to run away.) */
270 lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
271 oss->lqo_penalty >>= 1;
273 /* Set max penalties for this OST and OSS */
274 lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
275 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
276 lov->desc.ld_active_tgt_count;
277 oss->lqo_penalty += oss->lqo_penalty_per_obj *
278 lov->lov_qos.lq_active_oss_count;
280 /* Decrease all OSS penalties */
281 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
282 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
283 oss->lqo_penalty = 0;
285 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
289 /* Decrease all OST penalties */
290 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
291 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
293 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
294 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
295 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
297 lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
298 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
300 qos_calc_weight(lov, i);
302 /* Recalc the total weight of usable osts */
303 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
304 *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
307 CDEBUG(D_QOS, "recalc tgt %d avail="LPU64
308 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
309 " ossp="LPU64" wt="LPU64"\n",
310 i, TGT_BAVAIL(i) >> 10,
311 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj >> 10,
312 lov->lov_tgts[i]->ltd_qos.ltq_penalty >> 10,
313 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj>>10,
314 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty>>10,
315 lov->lov_tgts[i]->ltd_qos.ltq_weight>>10);
322 #define LOV_QOS_EMPTY ((__u32)-1)
323 /* compute optimal round-robin order, based on OSTs per OSS */
324 static int qos_calc_rr(struct lov_obd *lov)
326 struct lov_qos_oss *oss;
327 unsigned ost_count, placed, real_count;
331 if (!lov->lov_qos.lq_dirty_rr) {
332 LASSERT(lov->lov_qos.lq_rr_size);
336 down_write(&lov->lov_qos.lq_rw_sem);
337 ost_count = lov->desc.ld_tgt_count;
339 if (lov->lov_qos.lq_rr_size)
340 OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
341 lov->lov_qos.lq_rr_size = ost_count *
342 sizeof(lov->lov_qos.lq_rr_array[0]);
343 OBD_ALLOC(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
344 if (!lov->lov_qos.lq_rr_array) {
345 lov->lov_qos.lq_rr_size = 0;
346 up_write(&lov->lov_qos.lq_rw_sem);
351 for (i = 0; i < ost_count; i++) {
352 lov->lov_qos.lq_rr_array[i] = LOV_QOS_EMPTY;
353 if (lov->lov_tgts[i])
357 /* Place all the OSTs from 1 OSS at the same time. */
359 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
361 for (i = 0; i < ost_count; i++) {
362 if (lov->lov_tgts[i] &&
363 (lov->lov_tgts[i]->ltd_qos.ltq_oss == oss)) {
364 /* Evenly space these OSTs across arrayspace */
365 int next = j * ost_count / oss->lqo_ost_count;
366 while (lov->lov_qos.lq_rr_array[next] !=
368 next = (next + 1) % ost_count;
369 lov->lov_qos.lq_rr_array[next] = i;
374 LASSERT(j == oss->lqo_ost_count);
377 lov->lov_qos.lq_dirty_rr = 0;
378 up_write(&lov->lov_qos.lq_rw_sem);
380 if (placed != real_count) {
381 /* This should never happen */
382 LCONSOLE_ERROR(0x14e, "Failed to place all OSTs in the "
383 "round-robin list (%d of %d).\n",
385 for (i = 0; i < ost_count; i++) {
386 LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
387 lov->lov_qos.lq_rr_array[i]);
389 lov->lov_qos.lq_dirty_rr = 1;
394 for (i = 0; i < ost_count; i++) {
395 LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i,
396 lov->lov_qos.lq_rr_array[i]);
404 void qos_shrink_lsm(struct lov_request_set *set)
406 struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
407 /* XXX LOV STACKING call into osc for sizes */
408 unsigned oldsize, newsize;
410 if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
411 struct llog_cookie *cookies;
412 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
413 newsize = set->set_count * sizeof(*cookies);
415 cookies = set->set_cookies;
416 oti_alloc_cookies(set->set_oti, set->set_count);
417 if (set->set_oti->oti_logcookies) {
418 memcpy(set->set_oti->oti_logcookies, cookies, newsize);
419 OBD_FREE(cookies, oldsize);
420 set->set_cookies = set->set_oti->oti_logcookies;
422 CWARN("'leaking' %d bytes\n", oldsize - newsize);
426 CWARN("using fewer stripes for object "LPU64": old %u new %u\n",
427 lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
428 LASSERT(lsm->lsm_stripe_count >= set->set_count);
430 newsize = lov_stripe_md_size(set->set_count);
431 OBD_ALLOC(lsm_new, newsize);
432 if (lsm_new != NULL) {
434 memcpy(lsm_new, lsm, sizeof(*lsm));
435 for (i = 0; i < lsm->lsm_stripe_count; i++) {
436 if (i < set->set_count) {
437 lsm_new->lsm_oinfo[i] = lsm->lsm_oinfo[i];
440 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
441 sizeof(struct lov_oinfo));
443 lsm_new->lsm_stripe_count = set->set_count;
444 OBD_FREE(lsm, sizeof(struct lov_stripe_md) +
445 lsm->lsm_stripe_count * sizeof(struct lov_oinfo *));
446 set->set_oi->oi_md = lsm_new;
448 CWARN("'leaking' few bytes\n");
452 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
454 struct lov_stripe_md *lsm = set->set_oi->oi_md;
455 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
456 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
457 int stripe, i, rc = -EIO;
460 ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
461 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
462 if (!lov->lov_tgts[ost_idx] ||
463 !lov->lov_tgts[ost_idx]->ltd_active)
465 /* check if objects has been created on this ost */
466 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
467 if (stripe == req->rq_stripe)
469 if (ost_idx == lsm->lsm_oinfo[stripe]->loi_ost_idx)
473 if (stripe >= lsm->lsm_stripe_count) {
474 req->rq_idx = ost_idx;
475 rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp,
476 req->rq_oi.oi_oa, &req->rq_oi.oi_md,
485 #define LOV_CREATE_RESEED_MULT 4
486 #define LOV_CREATE_RESEED_MIN 1000
487 /* Allocate objects on osts with round-robin algorithm */
488 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt)
490 unsigned array_idx, ost_count = lov->desc.ld_tgt_count;
491 unsigned ost_active_count = lov->desc.ld_active_tgt_count;
492 int i, *idx_pos = idx_arr;
496 i = qos_calc_rr(lov);
500 if (--lov->lov_start_count <= 0) {
501 lov->lov_start_idx = ll_rand() % ost_count;
502 lov->lov_start_count =
503 (LOV_CREATE_RESEED_MIN / max(ost_active_count, 1U) +
504 LOV_CREATE_RESEED_MULT) * max(ost_active_count, 1U);
505 } else if (*stripe_cnt >= ost_active_count ||
506 lov->lov_start_idx > ost_count) {
507 /* If we have allocated from all of the OSTs, slowly
508 precess the next start */
509 lov->lov_start_idx %= ost_count;
510 ++lov->lov_offset_idx;
512 array_idx = (lov->lov_start_idx + lov->lov_offset_idx) % ost_count;
514 CDEBUG(D_QOS, "want %d startidx %d startcnt %d offset %d arrayidx %d\n",
515 *stripe_cnt, lov->lov_start_idx, lov->lov_start_count,
516 lov->lov_offset_idx, array_idx);
519 down_read(&lov->lov_qos.lq_rw_sem);
520 for (i = 0; i < ost_count; i++, array_idx=(array_idx + 1) % ost_count) {
521 ++lov->lov_start_idx;
522 ost_idx = lov->lov_qos.lq_rr_array[array_idx];
524 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
525 i, lov->lov_start_idx,
526 ((ost_idx != LOV_QOS_EMPTY) && lov->lov_tgts[ost_idx]) ?
527 lov->lov_tgts[ost_idx]->ltd_active : 0,
528 idx_pos - idx_arr, array_idx, ost_idx);
530 if ((ost_idx == LOV_QOS_EMPTY) || !lov->lov_tgts[ost_idx] ||
531 !lov->lov_tgts[ost_idx]->ltd_active)
535 /* We have enough stripes */
536 if (idx_pos - idx_arr == *stripe_cnt)
539 up_read(&lov->lov_qos.lq_rw_sem);
541 *stripe_cnt = idx_pos - idx_arr;
545 /* alloc objects on osts with specific stripe offset */
546 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
549 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
550 int i, *idx_pos = idx_arr;
553 ost_idx = lsm->lsm_oinfo[0]->loi_ost_idx;
554 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
555 if (!lov->lov_tgts[ost_idx] ||
556 !lov->lov_tgts[ost_idx]->ltd_active) {
562 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
565 /* If we were passed specific striping params, then a failure to
566 * meet those requirements is an error, since we can't reallocate
567 * that memory (it might be part of a larger array or something).
569 * We can only get here if lsm_stripe_count was originally > 1.
571 CERROR("can't lstripe objid "LPX64": have "LPSZ" want %u\n",
572 lsm->lsm_object_id, idx_pos - idx_arr, lsm->lsm_stripe_count);
576 /* Alloc objects on osts with optimization based on:
578 - network resources (shared OSS's)
580 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
582 struct lov_obd *lov = &exp->exp_obd->u.lov;
583 static time_t last_warn = 0;
584 time_t now = cfs_time_current_sec();
585 __u64 total_bavail, total_weight = 0;
587 int nfound, good_osts, i, warn = 0, rc = 0;
590 lov_getref(exp->exp_obd);
591 down_write(&lov->lov_qos.lq_rw_sem);
593 ost_count = lov->desc.ld_tgt_count;
595 if (lov->desc.ld_active_tgt_count < 2)
596 GOTO(out, rc = -EAGAIN);
598 rc = qos_calc_ppo(exp->exp_obd);
604 /* Warn users about zero available space/inode every 30 min */
605 if (cfs_time_sub(now, last_warn) > 60 * 30)
607 /* Find all the OSTs that are valid stripe candidates */
608 for (i = 0; i < ost_count; i++) {
611 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
613 bavail = TGT_BAVAIL(i);
616 CDEBUG(D_QOS, "no free space on %s\n",
617 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
624 CDEBUG(D_QOS, "no free inodes on %s\n",
625 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
631 lov->lov_tgts[i]->ltd_qos.ltq_usable = 1;
632 qos_calc_weight(lov, i);
633 total_bavail += bavail;
634 total_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
640 GOTO(out, rc = -ENOSPC);
642 /* if we don't have enough good OSTs, we reduce the stripe count. */
643 if (good_osts < *stripe_cnt)
644 *stripe_cnt = good_osts;
647 GOTO(out, rc = -EAGAIN);
649 /* Find enough OSTs with weighted random allocation. */
651 while (nfound < *stripe_cnt) {
652 __u64 rand, cur_weight;
658 #if BITS_PER_LONG == 32
659 rand = ll_rand() % (unsigned)total_weight;
660 /* If total_weight > 32-bit, first generate the high
661 * 32 bits of the random number, then add in the low
662 * 32 bits (truncated to the upper limit, if needed) */
663 if (total_weight > 0xffffffffULL)
664 rand = (__u64)(ll_rand() %
665 (unsigned)(total_weight >> 32)) << 32;
669 if (rand == (total_weight & 0xffffffff00000000ULL))
670 rand |= ll_rand() % (unsigned)total_weight;
675 rand = ((__u64)ll_rand() << 32 | ll_rand()) %
682 /* On average, this will hit larger-weighted osts more often.
683 0-weight osts will always get used last (only when rand=0).*/
684 for (i = 0; i < ost_count; i++) {
685 if (!lov->lov_tgts[i] ||
686 !lov->lov_tgts[i]->ltd_qos.ltq_usable)
688 cur_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
689 if (cur_weight >= rand) {
691 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
694 idx_arr[nfound++] = i;
695 qos_used(lov, i, &total_weight);
700 /* should never satisfy below condition */
702 CERROR("Didn't find any OSTs?\n");
706 LASSERT(nfound == *stripe_cnt);
709 up_write(&lov->lov_qos.lq_rw_sem);
712 rc = alloc_rr(lov, idx_arr, stripe_cnt);
714 lov_putref(exp->exp_obd);
718 /* return new alloced stripe count on success */
719 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
720 int newea, int **idx_arr, int *arr_cnt)
722 struct lov_obd *lov = &exp->exp_obd->u.lov;
723 int stripe_cnt = lsm->lsm_stripe_count;
728 *arr_cnt = stripe_cnt;
729 OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
732 for (i = 0; i < *arr_cnt; i++)
736 lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count)
737 rc = alloc_qos(exp, tmp_arr, &stripe_cnt);
739 rc = alloc_specific(lov, lsm, tmp_arr);
747 OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
752 static void free_idx_array(int *idx_arr, int arr_cnt)
755 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
758 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
760 struct lov_obd *lov = &exp->exp_obd->u.lov;
761 struct lov_stripe_md *lsm;
762 struct obdo *src_oa = set->set_oi->oi_oa;
763 struct obd_trans_info *oti = set->set_oti;
764 int i, stripes, rc = 0, newea = 0;
765 int *idx_arr, idx_cnt = 0;
768 LASSERT(src_oa->o_valid & OBD_MD_FLID);
770 if (set->set_oi->oi_md == NULL) {
771 int stripe_cnt = lov_get_stripecnt(lov, 0);
773 /* If the MDS file was truncated up to some size, stripe over
774 * enough OSTs to allow the file to be created at that size.
775 * This may mean we use more than the default # of stripes. */
776 if (src_oa->o_valid & OBD_MD_FLSIZE) {
777 obd_size min_bavail = LUSTRE_STRIPE_MAXBYTES;
779 /* Find a small number of stripes we can use
780 (up to # of active osts). */
782 lov_getref(exp->exp_obd);
783 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
784 if (!lov->lov_tgts[i] ||
785 !lov->lov_tgts[i]->ltd_active)
787 min_bavail = min(min_bavail, TGT_BAVAIL(i));
788 if (min_bavail * stripes > src_oa->o_size)
792 lov_putref(exp->exp_obd);
794 if (stripes < stripe_cnt)
795 stripes = stripe_cnt;
797 stripes = stripe_cnt;
800 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
801 lov->desc.ld_pattern ?
802 lov->desc.ld_pattern : LOV_PATTERN_RAID0,
810 lsm = set->set_oi->oi_md;
811 lsm->lsm_object_id = src_oa->o_id;
812 if (!lsm->lsm_stripe_size)
813 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
814 if (!lsm->lsm_pattern) {
815 LASSERT(lov->desc.ld_pattern);
816 lsm->lsm_pattern = lov->desc.ld_pattern;
819 stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt);
821 GOTO(out_err, rc = stripes ? stripes : -EIO);
822 LASSERTF(stripes <= lsm->lsm_stripe_count,"requested %d allocated %d\n",
823 lsm->lsm_stripe_count, stripes);
825 for (i = 0; i < stripes; i++) {
826 struct lov_request *req;
827 int ost_idx = idx_arr[i];
828 LASSERT(ost_idx >= 0);
830 OBD_ALLOC(req, sizeof(*req));
832 GOTO(out_err, rc = -ENOMEM);
833 lov_set_add_req(req, set);
835 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
836 OBD_ALLOC(req->rq_oi.oi_md, req->rq_buflen);
837 if (req->rq_oi.oi_md == NULL)
838 GOTO(out_err, rc = -ENOMEM);
840 req->rq_oi.oi_oa = obdo_alloc();
841 if (req->rq_oi.oi_oa == NULL)
842 GOTO(out_err, rc = -ENOMEM);
844 req->rq_idx = ost_idx;
846 /* create data objects with "parent" OA */
847 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
849 /* XXX When we start creating objects on demand, we need to
850 * make sure that we always create the object on the
851 * stripe which holds the existing file size.
853 if (src_oa->o_valid & OBD_MD_FLSIZE) {
854 req->rq_oi.oi_oa->o_size =
855 lov_size_to_stripe(lsm, src_oa->o_size, i);
857 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
858 i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
862 LASSERT(set->set_count == stripes);
864 if (stripes < lsm->lsm_stripe_count)
867 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
868 oti_alloc_cookies(oti, set->set_count);
869 if (!oti->oti_logcookies)
870 GOTO(out_err, rc = -ENOMEM);
871 set->set_cookies = oti->oti_logcookies;
875 obd_free_memmd(exp, &set->set_oi->oi_md);
876 free_idx_array(idx_arr, idx_cnt);
881 void qos_update(struct lov_obd *lov)
884 lov->lov_qos.lq_dirty = 1;