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 check
61 if (!exp || !exp->exp_connection) {
62 CERROR("Missing connection\n");
66 down_write(&lov->lov_qos.lq_rw_sem);
67 mutex_down(&lov->lov_lock);
68 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
69 if (obd_uuid_equals(&oss->lqo_uuid,
70 &exp->exp_connection->c_remote_uuid)) {
79 GOTO(out, rc = -ENOMEM);
80 memcpy(&oss->lqo_uuid,
81 &exp->exp_connection->c_remote_uuid,
82 sizeof(oss->lqo_uuid));
84 /* Assume we have to move this one */
85 list_del(&oss->lqo_oss_list);
89 lov->lov_tgts[index]->ltd_qos.ltq_oss = oss;
91 /* Add sorted by # of OSTs. Find the first entry that we're
93 list_for_each_entry(temposs, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
94 if (oss->lqo_ost_count > temposs->lqo_ost_count)
97 /* ...and add before it. If we're the first or smallest, temposs
98 points to the list head, and we add to the end. */
99 list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
101 lov->lov_qos.lq_dirty = 1;
102 lov->lov_qos.lq_dirty_rr = 1;
104 CDEBUG(D_QOS, "add tgt %s to OSS %s (#%d)\n",
105 obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
106 obd_uuid2str(&oss->lqo_uuid),
110 mutex_up(&lov->lov_lock);
111 up_write(&lov->lov_qos.lq_rw_sem);
115 int qos_del_tgt(struct obd_device *obd, __u32 index)
117 struct lov_obd *lov = &obd->u.lov;
118 struct lov_qos_oss *oss;
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 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
209 /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
210 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
211 temp = oss->lqo_bavail >> 1;
212 do_div(temp, oss->lqo_ost_count * num_active);
213 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
214 if (lov->lov_qos.lq_reset == 0)
215 oss->lqo_penalty = 0;
218 lov->lov_qos.lq_dirty = 0;
219 lov->lov_qos.lq_reset = 0;
221 /* If each ost has almost same free space,
222 * do rr allocation for better creation performance */
223 lov->lov_qos.lq_same_space = 0;
224 temp = ba_max - ba_min;
225 ba_min = (ba_min * 51) >> 8; /* 51/256 = .20 */
227 /* Difference is less than 20% */
228 lov->lov_qos.lq_same_space = 1;
229 /* Reset weights for the next time we enter qos mode */
230 lov->lov_qos.lq_reset = 0;
235 if (!rc && lov->lov_qos.lq_same_space)
240 static int qos_calc_weight(struct lov_obd *lov, int i)
244 /* Final ost weight = TGT_BAVAIL - ost_penalty - oss_penalty */
245 temp = TGT_BAVAIL(i);
246 temp2 = lov->lov_tgts[i]->ltd_qos.ltq_penalty +
247 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty;
249 lov->lov_tgts[i]->ltd_qos.ltq_weight = 0;
251 lov->lov_tgts[i]->ltd_qos.ltq_weight = temp - temp2;
255 /* We just used this index for a stripe; adjust everyone's weights */
256 static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
258 struct lov_qos_oss *oss;
262 /* Don't allocate from this stripe anymore, until the next alloc_qos */
263 lov->lov_tgts[index]->ltd_qos.ltq_usable = 0;
265 oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
267 /* Decay old penalty by half (we're adding max penalty, and don't
268 want it to run away.) */
269 lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
270 oss->lqo_penalty >>= 1;
272 /* Set max penalties for this OST and OSS */
273 lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
274 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
275 lov->desc.ld_active_tgt_count;
276 oss->lqo_penalty += oss->lqo_penalty_per_obj *
277 lov->lov_qos.lq_active_oss_count;
279 /* Decrease all OSS penalties */
280 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
281 if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
282 oss->lqo_penalty = 0;
284 oss->lqo_penalty -= oss->lqo_penalty_per_obj;
288 /* Decrease all OST penalties */
289 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
290 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
292 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
293 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj)
294 lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
296 lov->lov_tgts[i]->ltd_qos.ltq_penalty -=
297 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj;
299 qos_calc_weight(lov, i);
301 /* Recalc the total weight of usable osts */
302 if (lov->lov_tgts[i]->ltd_qos.ltq_usable)
303 *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
306 CDEBUG(D_QOS, "recalc tgt %d avail="LPU64
307 " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
308 " ossp="LPU64" wt="LPU64"\n",
310 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj,
311 lov->lov_tgts[i]->ltd_qos.ltq_penalty,
312 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj,
313 lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty,
314 lov->lov_tgts[i]->ltd_qos.ltq_weight);
321 #define LOV_QOS_EMPTY ((__u32)-1)
322 /* compute optimal round-robin order, based on OSTs per OSS */
323 static int qos_calc_rr(struct lov_obd *lov)
325 struct lov_qos_oss *oss;
326 unsigned ost_count, placed;
330 if (!lov->lov_qos.lq_dirty_rr) {
331 LASSERT(lov->lov_qos.lq_rr_size);
335 down_write(&lov->lov_qos.lq_rw_sem);
336 ost_count = lov->desc.ld_tgt_count;
338 if (lov->lov_qos.lq_rr_size)
339 OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
340 lov->lov_qos.lq_rr_size = ost_count *
341 sizeof(lov->lov_qos.lq_rr_array[0]);
342 OBD_ALLOC(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
343 if (!lov->lov_qos.lq_rr_array) {
344 lov->lov_qos.lq_rr_size = 0;
345 up_write(&lov->lov_qos.lq_rw_sem);
349 for (i = 0; i < ost_count; i++)
350 lov->lov_qos.lq_rr_array[i] = LOV_QOS_EMPTY;
352 /* Place all the OSTs from 1 OSS at the same time. */
354 list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
356 for (i = 0; i < ost_count; i++) {
357 LASSERT(lov->lov_tgts[i] != NULL);
358 if (lov->lov_tgts[i]->ltd_qos.ltq_oss == oss) {
359 /* Evenly space these OSTs across arrayspace */
360 int next = j * ost_count / oss->lqo_ost_count;
361 while (lov->lov_qos.lq_rr_array[next] !=
363 next = (next + 1) % ost_count;
364 lov->lov_qos.lq_rr_array[next] = i;
369 LASSERT(j == oss->lqo_ost_count);
372 lov->lov_qos.lq_dirty_rr = 0;
373 up_write(&lov->lov_qos.lq_rw_sem);
376 for (i = 0; i < ost_count; i++) {
377 LCONSOLE(D_QOS, "rr %d ost %d\n", i,
378 lov->lov_qos.lq_rr_array[i]);
382 if (placed != ost_count) {
383 /* This should never happen */
384 CERROR("Placed %d of %d osts\n", placed, ost_count);
385 for (i = 0; i < ost_count; i++) {
386 LCONSOLE(D_WARNING, "rr %d ost %d\n", i,
387 lov->lov_qos.lq_rr_array[i]);
396 void qos_shrink_lsm(struct lov_request_set *set)
398 struct lov_stripe_md *lsm = set->set_oi->oi_md, *lsm_new;
399 /* XXX LOV STACKING call into osc for sizes */
400 unsigned oldsize, newsize;
402 if (set->set_oti && set->set_cookies && set->set_cookie_sent) {
403 struct llog_cookie *cookies;
404 oldsize = lsm->lsm_stripe_count * sizeof(*cookies);
405 newsize = set->set_count * sizeof(*cookies);
407 cookies = set->set_cookies;
408 oti_alloc_cookies(set->set_oti, set->set_count);
409 if (set->set_oti->oti_logcookies) {
410 memcpy(set->set_oti->oti_logcookies, cookies, newsize);
411 OBD_FREE(cookies, oldsize);
412 set->set_cookies = set->set_oti->oti_logcookies;
414 CWARN("'leaking' %d bytes\n", oldsize - newsize);
418 CWARN("using fewer stripes for object "LPX64": old %u new %u\n",
419 lsm->lsm_object_id, lsm->lsm_stripe_count, set->set_count);
421 oldsize = lov_stripe_md_size(lsm->lsm_stripe_count);
422 newsize = lov_stripe_md_size(set->set_count);
423 OBD_ALLOC(lsm_new, newsize);
424 if (lsm_new != NULL) {
425 memcpy(lsm_new, lsm, newsize);
426 lsm_new->lsm_stripe_count = set->set_count;
427 OBD_FREE(lsm, oldsize);
428 set->set_oi->oi_md = lsm_new;
430 CWARN("'leaking' %d bytes\n", oldsize - newsize);
434 int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
436 struct lov_stripe_md *lsm = set->set_oi->oi_md;
437 struct lov_obd *lov = &set->set_exp->exp_obd->u.lov;
438 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
439 int stripe, i, rc = -EIO;
442 ost_idx = (req->rq_idx + lsm->lsm_stripe_count) % ost_count;
443 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
444 if (!lov->lov_tgts[ost_idx] ||
445 !lov->lov_tgts[ost_idx]->ltd_active)
447 /* check if objects has been created on this ost */
448 for (stripe = 0; stripe < lsm->lsm_stripe_count; stripe++) {
449 if (stripe == req->rq_stripe)
451 if (ost_idx == lsm->lsm_oinfo[stripe].loi_ost_idx)
455 if (stripe >= lsm->lsm_stripe_count) {
456 req->rq_idx = ost_idx;
457 rc = obd_create(lov->lov_tgts[ost_idx]->ltd_exp,
458 req->rq_oi.oi_oa, &req->rq_oi.oi_md,
467 #define LOV_CREATE_RESEED_MULT 4
468 #define LOV_CREATE_RESEED_MIN 1000
469 /* Allocate objects on osts with round-robin algorithm */
470 static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt)
472 unsigned array_idx, ost_count = lov->desc.ld_tgt_count;
473 unsigned ost_active_count = lov->desc.ld_active_tgt_count;
474 int i, *idx_pos = idx_arr;
478 i = qos_calc_rr(lov);
482 if (--lov->lov_start_count <= 0) {
483 lov->lov_start_idx = ll_rand() % ost_count;
484 lov->lov_start_count =
485 (LOV_CREATE_RESEED_MIN / max(ost_active_count, 1U) +
486 LOV_CREATE_RESEED_MULT) * max(ost_active_count, 1U);
487 } else if (*stripe_cnt >= ost_active_count ||
488 lov->lov_start_idx > ost_count) {
489 /* If we have allocated from all of the OSTs, slowly
490 precess the next start */
491 lov->lov_start_idx %= ost_count;
492 ++lov->lov_offset_idx;
494 array_idx = (lov->lov_start_idx + lov->lov_offset_idx) % ost_count;
496 CDEBUG(D_QOS, "want %d startidx %d startcnt %d offset %d arrayidx %d\n",
497 *stripe_cnt, lov->lov_start_idx, lov->lov_start_count,
498 lov->lov_offset_idx, array_idx);
501 down_read(&lov->lov_qos.lq_rw_sem);
502 for (i = 0; i < ost_count; i++, array_idx=(array_idx + 1) % ost_count) {
503 ++lov->lov_start_idx;
504 ost_idx = lov->lov_qos.lq_rr_array[array_idx];
506 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
507 i, lov->lov_start_idx,
508 lov->lov_tgts[ost_idx] ?
509 lov->lov_tgts[ost_idx].ltd_active : 0,
510 idx_pos - idx_arr, array_idx, ost_idx);
512 if (!lov->lov_tgts[ost_idx] ||
513 !lov->lov_tgts[ost_idx]->ltd_active)
517 /* We have enough stripes */
518 if (idx_pos - idx_arr == *stripe_cnt)
521 up_read(&lov->lov_qos.lq_rw_sem);
523 *stripe_cnt = idx_pos - idx_arr;
527 /* alloc objects on osts with specific stripe offset */
528 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
531 unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
532 int i, *idx_pos = idx_arr;
535 ost_idx = lsm->lsm_oinfo[0].loi_ost_idx;
536 for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
537 if (!lov->lov_tgts[ost_idx] ||
538 !lov->lov_tgts[ost_idx]->ltd_active) {
544 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
547 /* If we were passed specific striping params, then a failure to
548 * meet those requirements is an error, since we can't reallocate
549 * that memory (it might be part of a larger array or something).
551 * We can only get here if lsm_stripe_count was originally > 1.
553 CERROR("can't lstripe objid "LPX64": have %u want %u\n",
554 lsm->lsm_object_id, idx_pos - idx_arr, lsm->lsm_stripe_count);
558 /* Alloc objects on osts with optimization based on:
560 - network resources (shared OSS's)
562 static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
564 struct lov_obd *lov = &exp->exp_obd->u.lov;
565 static time_t last_warn = 0;
566 time_t now = cfs_time_current_sec();
567 __u64 cur_weight, temp, rand, bavail, total_bavail, total_weight = 0;
569 int nfound, good_osts, i, warn = 0, rc = 0;
572 lov_getref(exp->exp_obd);
573 down_write(&lov->lov_qos.lq_rw_sem);
575 ost_count = lov->desc.ld_tgt_count;
577 if (lov->desc.ld_active_tgt_count < 2)
578 GOTO(out, rc = -EAGAIN);
580 rc = qos_calc_ppo(exp->exp_obd);
586 /* Warn users about zero available space/inode every 30 min */
587 if (cfs_time_sub(now, last_warn) > 60 * 30)
589 /* Find all the OSTs that are valid stripe candidates */
590 for (i = 0; i < ost_count; i++) {
591 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
593 bavail = TGT_BAVAIL(i);
596 CDEBUG(D_QOS, "no free space on %s\n",
597 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
604 CDEBUG(D_QOS, "no free inodes on %s\n",
605 obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
611 lov->lov_tgts[i]->ltd_qos.ltq_usable = 1;
612 qos_calc_weight(lov, i);
613 total_bavail += bavail;
614 total_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
620 GOTO(out, rc = -ENOSPC);
622 /* if we don't have enough good OSTs, we reduce the stripe count. */
623 if (good_osts < *stripe_cnt)
624 *stripe_cnt = good_osts;
627 GOTO(out, rc = -EAGAIN);
629 /* Find enough OSTs with weighted random allocation. */
631 while (nfound < *stripe_cnt) {
636 /* If total_weight > 32-bit, make a 64-bit random # */
637 temp = (total_weight & 0xffffffff00000000ULL ?
638 (__u64)ll_rand() << 32 : 0ULL) | ll_rand();
639 /* Random number between 0 and total_weight */
640 rand = do_div(temp, total_weight);
645 /* On average, this will hit larger-weighted osts more often.
646 0-weight osts will always get used last (only when rand=0).*/
647 for (i = 0; i < ost_count; i++) {
648 if (!lov->lov_tgts[i]->ltd_qos.ltq_usable)
650 cur_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
651 if (cur_weight >= rand) {
653 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
656 idx_arr[nfound++] = i;
657 qos_used(lov, i, &total_weight);
662 /* should never satisfy below condition */
664 CERROR("Didn't find any OSTs?\n");
668 LASSERT(nfound == *stripe_cnt);
671 up_write(&lov->lov_qos.lq_rw_sem);
674 rc = alloc_rr(lov, idx_arr, stripe_cnt);
676 lov_putref(exp->exp_obd);
680 /* return new alloced stripe count on success */
681 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
682 int newea, int **idx_arr, int *arr_cnt)
684 struct lov_obd *lov = &exp->exp_obd->u.lov;
685 int stripe_cnt = lsm->lsm_stripe_count;
690 *arr_cnt = stripe_cnt;
691 OBD_ALLOC(tmp_arr, *arr_cnt * sizeof(int));
694 for (i = 0; i < *arr_cnt; i++)
698 lsm->lsm_oinfo[0].loi_ost_idx >= lov->desc.ld_tgt_count)
699 rc = alloc_qos(exp, tmp_arr, &stripe_cnt);
701 rc = alloc_specific(lov, lsm, tmp_arr);
709 OBD_FREE(tmp_arr, *arr_cnt * sizeof(int));
714 static void free_idx_array(int *idx_arr, int arr_cnt)
717 OBD_FREE(idx_arr, arr_cnt * sizeof(int));
720 int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
722 struct lov_obd *lov = &exp->exp_obd->u.lov;
723 struct lov_stripe_md *lsm;
724 struct obdo *src_oa = set->set_oi->oi_oa;
725 struct obd_trans_info *oti = set->set_oti;
726 int i, stripes, rc = 0, newea = 0;
727 int *idx_arr, idx_cnt = 0;
730 LASSERT(src_oa->o_valid & OBD_MD_FLID);
731 LASSERT(src_oa->o_valid & OBD_MD_FLGROUP);
733 if (set->set_oi->oi_md == NULL) {
734 int stripe_cnt = lov_get_stripecnt(lov, 0);
736 /* If the MDS file was truncated up to some size, stripe over
737 * enough OSTs to allow the file to be created at that size.
738 * This may mean we use more than the default # of stripes. */
739 if (src_oa->o_valid & OBD_MD_FLSIZE) {
740 obd_size min_bavail = (obd_size)-1;
742 /* Find a small number of stripes we can use
743 (up to # of active osts). */
745 lov_getref(exp->exp_obd);
746 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
747 if (!lov->lov_tgts[i] ||
748 !lov->lov_tgts[i]->ltd_active)
750 min_bavail = min(min_bavail, TGT_BAVAIL(i));
751 if (min_bavail * stripes > src_oa->o_size)
755 lov_putref(exp->exp_obd);
757 if (stripes < stripe_cnt)
758 stripes = stripe_cnt;
760 stripes = stripe_cnt;
763 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
764 lov->desc.ld_pattern ?
765 lov->desc.ld_pattern : LOV_PATTERN_RAID0,
773 lsm = set->set_oi->oi_md;
774 lsm->lsm_object_id = src_oa->o_id;
775 lsm->lsm_object_gr = src_oa->o_gr;
777 if (!lsm->lsm_stripe_size)
778 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
779 if (!lsm->lsm_pattern) {
780 LASSERT(lov->desc.ld_pattern);
781 lsm->lsm_pattern = lov->desc.ld_pattern;
784 stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt);
785 LASSERT(stripes <= lsm->lsm_stripe_count);
787 GOTO(out_err, rc = stripes ? stripes : -EIO);
789 for (i = 0; i < stripes; i++) {
790 struct lov_request *req;
791 int ost_idx = idx_arr[i];
792 LASSERT(ost_idx >= 0);
794 OBD_ALLOC(req, sizeof(*req));
796 GOTO(out_err, rc = -ENOMEM);
797 lov_set_add_req(req, set);
799 req->rq_buflen = sizeof(*req->rq_oi.oi_md);
800 OBD_ALLOC(req->rq_oi.oi_md, req->rq_buflen);
801 if (req->rq_oi.oi_md == NULL)
802 GOTO(out_err, rc = -ENOMEM);
804 req->rq_oi.oi_oa = obdo_alloc();
805 if (req->rq_oi.oi_oa == NULL)
806 GOTO(out_err, rc = -ENOMEM);
808 req->rq_idx = ost_idx;
810 /* create data objects with "parent" OA */
811 memcpy(req->rq_oi.oi_oa, src_oa, sizeof(*req->rq_oi.oi_oa));
813 /* XXX When we start creating objects on demand, we need to
814 * make sure that we always create the object on the
815 * stripe which holds the existing file size.
817 if (src_oa->o_valid & OBD_MD_FLSIZE) {
818 req->rq_oi.oi_oa->o_size =
819 lov_size_to_stripe(lsm, src_oa->o_size, i);
821 CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
822 i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
826 LASSERT(set->set_count == stripes);
828 if (stripes < lsm->lsm_stripe_count)
831 if (oti && (src_oa->o_valid & OBD_MD_FLCOOKIE)) {
832 oti_alloc_cookies(oti, set->set_count);
833 if (!oti->oti_logcookies)
834 GOTO(out_err, rc = -ENOMEM);
835 set->set_cookies = oti->oti_logcookies;
839 obd_free_memmd(exp, &set->set_oi->oi_md);
840 free_idx_array(idx_arr, idx_cnt);
845 void qos_update(struct lov_obd *lov, int idx, struct obd_statfs *osfs)
848 lov->lov_qos.lq_dirty = 1;