Whamcloud - gitweb
b=15699
[fs/lustre-release.git] / lustre / lov / lov_qos.c
index 8dc03e6..5ad5848 100644 (file)
@@ -1,25 +1,37 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
+ * GPL HEADER START
  *
- *   This file is part of the Lustre file system, http://www.lustre.org
- *   Lustre is a trademark of Cluster File Systems, Inc.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   You may have signed or agreed to another license before downloading
- *   this software.  If so, you are bound by the terms and conditions
- *   of that agreement, and the following does not apply to you.  See the
- *   LICENSE file included with this distribution for more information.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   If you did not agree to a different license, then this copy of Lustre
- *   is open source software; you can redistribute it and/or modify it
- *   under the terms of version 2 of the GNU General Public License as
- *   published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   In either case, Lustre is distributed in the hope that it will be
- *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
- *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   license text for more details.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
  */
 
 #ifndef EXPORT_SYMTAB
@@ -40,9 +52,9 @@
 /* #define QOS_DEBUG 1 */
 #define D_QOS D_OTHER
 
-#define TGT_BAVAIL(i)  (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail*\
-                        lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize)
-#define TGT_FFREE(i)   (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_ffree)
+#define TGT_BAVAIL(i) (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bavail *\
+                       lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_bsize)
+#define TGT_FFREE(i)  (lov->lov_tgts[i]->ltd_exp->exp_obd->obd_osfs.os_ffree)
 
 
 int qos_add_tgt(struct obd_device *obd, __u32 index)
@@ -96,7 +108,7 @@ int qos_add_tgt(struct obd_device *obd, __u32 index)
         list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
 
         lov->lov_qos.lq_dirty = 1;
-        lov->lov_qos.lq_dirty_rr = 1;
+        lov->lov_qos.lq_rr.lqr_dirty = 1;
 
         CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
                obd_uuid2str(&lov->lov_tgts[index]->ltd_uuid),
@@ -116,6 +128,9 @@ int qos_del_tgt(struct obd_device *obd, __u32 index)
         int rc = 0;
         ENTRY;
 
+        if (!lov->lov_tgts[index])
+                RETURN(0);
+
         down_write(&lov->lov_qos.lq_rw_sem);
 
         oss = lov->lov_tgts[index]->ltd_qos.ltq_oss;
@@ -131,7 +146,7 @@ int qos_del_tgt(struct obd_device *obd, __u32 index)
         }
 
         lov->lov_qos.lq_dirty = 1;
-        lov->lov_qos.lq_dirty_rr = 1;
+        lov->lov_qos.lq_rr.lqr_dirty = 1;
 out:
         up_write(&lov->lov_qos.lq_rw_sem);
         RETURN(rc);
@@ -146,6 +161,7 @@ static int qos_calc_ppo(struct obd_device *obd)
         __u64 ba_max, ba_min, temp;
         __u32 num_active;
         int rc, i, prio_wide;
+        time_t now, age;
         ENTRY;
 
         if (!lov->lov_qos.lq_dirty)
@@ -168,6 +184,7 @@ static int qos_calc_ppo(struct obd_device *obd)
 
         ba_min = (__u64)(-1);
         ba_max = 0;
+        now = cfs_time_current_sec();
         /* Calculate OST penalty per object */
         /* (lov ref taken in alloc_qos) */
         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
@@ -190,8 +207,17 @@ static int qos_calc_ppo(struct obd_device *obd)
                 lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj =
                         (temp * prio_wide) >> 8;
 
-                if (lov->lov_qos.lq_reset == 0)
+                age = (now - lov->lov_tgts[i]->ltd_qos.ltq_used) >> 3;
+                if (lov->lov_qos.lq_reset || age > 32 * lov->desc.ld_qos_maxage)
                         lov->lov_tgts[i]->ltd_qos.ltq_penalty = 0;
+                else if (age > lov->desc.ld_qos_maxage)
+                        /* Decay the penalty by half for every 8x the update
+                         * interval that the device has been idle.  That gives
+                         * lots of time for the statfs information to be
+                         * updated (which the penalty is only a proxy for),
+                         * and avoids penalizing OSS/OSTs under light load. */
+                        lov->lov_tgts[i]->ltd_qos.ltq_penalty >>=
+                                (age / lov->desc.ld_qos_maxage);
         }
 
         num_active = lov->lov_qos.lq_active_oss_count - 1;
@@ -199,8 +225,11 @@ static int qos_calc_ppo(struct obd_device *obd)
                 /* If there's only 1 OSS, we can't penalize it, so instead
                    we have to double the OST penalty */
                 num_active = 1;
-                for (i = 0; i < lov->desc.ld_tgt_count; i++)
+                for (i = 0; i < lov->desc.ld_tgt_count; i++) {
+                        if (lov->lov_tgts[i] == NULL)
+                                continue;
                         lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj <<= 1;
+                }
         }
 
         /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
@@ -208,8 +237,17 @@ static int qos_calc_ppo(struct obd_device *obd)
                 temp = oss->lqo_bavail >> 1;
                 do_div(temp, oss->lqo_ost_count * num_active);
                 oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
-                if (lov->lov_qos.lq_reset == 0)
+
+                age = (now - oss->lqo_used) >> 3;
+                if (lov->lov_qos.lq_reset || age > 32 * lov->desc.ld_qos_maxage)
                         oss->lqo_penalty = 0;
+                else if (age > lov->desc.ld_qos_maxage)
+                        /* Decay the penalty by half for every 8x the update
+                         * interval that the device has been idle.  That gives
+                         * lots of time for the statfs information to be
+                         * updated (which the penalty is only a proxy for),
+                         * and avoids penalizing OSS/OSTs under light load. */
+                        oss->lqo_penalty >>= (age / lov->desc.ld_qos_maxage);
         }
 
         lov->lov_qos.lq_dirty = 0;
@@ -224,7 +262,7 @@ static int qos_calc_ppo(struct obd_device *obd)
                 /* Difference is less than 20% */
                 lov->lov_qos.lq_same_space = 1;
                 /* Reset weights for the next time we enter qos mode */
-                lov->lov_qos.lq_reset = 0;
+                lov->lov_qos.lq_reset = 1;
         }
         rc = 0;
 
@@ -250,10 +288,11 @@ static int qos_calc_weight(struct lov_obd *lov, int i)
 }
 
 /* We just used this index for a stripe; adjust everyone's weights */
-static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
+static int qos_used(struct lov_obd *lov, struct ost_pool *osts,
+                    __u32 index, __u64 *total_wt)
 {
         struct lov_qos_oss *oss;
-        int i;
+        int j;
         ENTRY;
 
         /* Don't allocate from this stripe anymore, until the next alloc_qos */
@@ -266,6 +305,10 @@ static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
         lov->lov_tgts[index]->ltd_qos.ltq_penalty >>= 1;
         oss->lqo_penalty >>= 1;
 
+        /* mark the OSS and OST as recently used */
+        lov->lov_tgts[index]->ltd_qos.ltq_used =
+                oss->lqo_used = cfs_time_current_sec();
+
         /* Set max penalties for this OST and OSS */
         lov->lov_tgts[index]->ltd_qos.ltq_penalty +=
                 lov->lov_tgts[index]->ltd_qos.ltq_penalty_per_obj *
@@ -283,7 +326,10 @@ static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
 
         *total_wt = 0;
         /* Decrease all OST penalties */
-        for (i = 0; i < lov->desc.ld_tgt_count; i++) {
+        for (j = 0; j < osts->op_count; j++) {
+                int i;
+
+                i = osts->op_array[j];
                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
                         continue;
                 if (lov->lov_tgts[i]->ltd_qos.ltq_penalty <
@@ -300,15 +346,16 @@ static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
                         *total_wt += lov->lov_tgts[i]->ltd_qos.ltq_weight;
 
 #ifdef QOS_DEBUG
-                CDEBUG(D_QOS, "recalc tgt %d avail="LPU64
+                CDEBUG(D_QOS, "recalc tgt %d usable=%d avail="LPU64
                        " ostppo="LPU64" ostp="LPU64" ossppo="LPU64
                        " ossp="LPU64" wt="LPU64"\n",
-                       i, TGT_BAVAIL(i) >> 10,
+                       i, lov->lov_tgts[i]->ltd_qos.ltq_usable,
+                       TGT_BAVAIL(i) >> 10,
                        lov->lov_tgts[i]->ltd_qos.ltq_penalty_per_obj >> 10,
                        lov->lov_tgts[i]->ltd_qos.ltq_penalty >> 10,
                        lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty_per_obj>>10,
-                       lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty>>10,
-                       lov->lov_tgts[i]->ltd_qos.ltq_weight>>10);
+                       lov->lov_tgts[i]->ltd_qos.ltq_oss->lqo_penalty >> 10,
+                       lov->lov_tgts[i]->ltd_qos.ltq_weight >> 10);
 #endif
         }
 
@@ -317,73 +364,86 @@ static int qos_used(struct lov_obd *lov, __u32 index, __u64 *total_wt)
 
 #define LOV_QOS_EMPTY ((__u32)-1)
 /* compute optimal round-robin order, based on OSTs per OSS */
-static int qos_calc_rr(struct lov_obd *lov)
+static int qos_calc_rr(struct lov_obd *lov, struct ost_pool *src_pool,
+                       struct lov_qos_rr *lqr)
 {
         struct lov_qos_oss *oss;
-        unsigned ost_count, placed;
-        int i;
+        unsigned placed, real_count;
+        int i, rc;
         ENTRY;
 
-        if (!lov->lov_qos.lq_dirty_rr) {
-                LASSERT(lov->lov_qos.lq_rr_size);
+        if (!lqr->lqr_dirty) {
+                LASSERT(lqr->lqr_pool.op_size);
                 RETURN(0);
         }
 
+        /* Do actual allocation. */
         down_write(&lov->lov_qos.lq_rw_sem);
-        ost_count = lov->desc.ld_tgt_count;
-
-        if (lov->lov_qos.lq_rr_size)
-                OBD_FREE(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
-        lov->lov_qos.lq_rr_size = ost_count *
-                sizeof(lov->lov_qos.lq_rr_array[0]);
-        OBD_ALLOC(lov->lov_qos.lq_rr_array, lov->lov_qos.lq_rr_size);
-        if (!lov->lov_qos.lq_rr_array) {
-                lov->lov_qos.lq_rr_size = 0;
+
+        /*
+         * Check again. While we were sleeping on @lq_rw_sem something could
+         * change.
+         */
+        if (!lqr->lqr_dirty) {
+                LASSERT(lqr->lqr_pool.op_size);
                 up_write(&lov->lov_qos.lq_rw_sem);
-                RETURN(-ENOMEM);
+                RETURN(0);
         }
 
-        for (i = 0; i < ost_count; i++)
-                lov->lov_qos.lq_rr_array[i] = LOV_QOS_EMPTY;
+        real_count = src_pool->op_count;
+
+        /* Zero the pool array */
+        /* alloc_rr is holding a read lock on the pool, so nobody is adding/
+           deleting from the pool. The lq_rw_sem insures that nobody else
+           is reading. */
+        lqr->lqr_pool.op_count = real_count;
+        rc = lov_ost_pool_extend(&lqr->lqr_pool, real_count);
+        if (rc) {
+                up_write(&lov->lov_qos.lq_rw_sem);
+                RETURN(rc);
+        }
+        for (i = 0; i < lqr->lqr_pool.op_count; i++)
+                lqr->lqr_pool.op_array[i] = LOV_QOS_EMPTY;
 
         /* Place all the OSTs from 1 OSS at the same time. */
         placed = 0;
         list_for_each_entry(oss, &lov->lov_qos.lq_oss_list, lqo_oss_list) {
                 int j = 0;
-                for (i = 0; i < ost_count; i++) {
-                      if (lov->lov_tgts[i]->ltd_qos.ltq_oss == oss) {
+                for (i = 0; i < lqr->lqr_pool.op_count; i++) {
+                        if (lov->lov_tgts[src_pool->op_array[i]] &&
+                            (lov->lov_tgts[src_pool->op_array[i]]->ltd_qos.ltq_oss == oss)) {
                               /* Evenly space these OSTs across arrayspace */
-                              int next = j * ost_count / oss->lqo_ost_count;
-                              while (lov->lov_qos.lq_rr_array[next] !=
+                              int next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
+                              while (lqr->lqr_pool.op_array[next] !=
                                      LOV_QOS_EMPTY)
-                                      next = (next + 1) % ost_count;
-                              lov->lov_qos.lq_rr_array[next] = i;
+                                        next = (next + 1) % lqr->lqr_pool.op_count;
+                              lqr->lqr_pool.op_array[next] = src_pool->op_array[i];
                               j++;
                               placed++;
-                      }
+                        }
                 }
-                LASSERT(j == oss->lqo_ost_count);
         }
 
-        lov->lov_qos.lq_dirty_rr = 0;
+        lqr->lqr_dirty = 0;
         up_write(&lov->lov_qos.lq_rw_sem);
 
-        if (placed != ost_count) {
+        if (placed != real_count) {
                 /* This should never happen */
-                LCONSOLE_ERROR("Failed to place all OSTs in the round-robin "
-                               "list (%d of %d).\n", placed, ost_count);
-                for (i = 0; i < ost_count; i++) {
+                LCONSOLE_ERROR_MSG(0x14e, "Failed to place all OSTs in the "
+                                   "round-robin list (%d of %d).\n",
+                                   placed, real_count);
+                for (i = 0; i < lqr->lqr_pool.op_count; i++) {
                         LCONSOLE(D_WARNING, "rr #%d ost idx=%d\n", i,
-                                 lov->lov_qos.lq_rr_array[i]);
+                                 lqr->lqr_pool.op_array[i]);
                 }
-                lov->lov_qos.lq_dirty_rr = 1;
+                lqr->lqr_dirty = 1;
                 RETURN(-EAGAIN);
         }
-        
+
 #ifdef QOS_DEBUG
-        for (i = 0; i < ost_count; i++) {
-                LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i, 
-                         lov->lov_qos.lq_rr_array[i]);
+        for (i = 0; i < lqr->lqr_pool.op_count; i++) {
+                LCONSOLE(D_QOS, "rr #%d ost idx=%d\n", i,
+                         lqr->lqr_pool.op_array[i]);
         }
 #endif
 
@@ -472,115 +532,250 @@ int qos_remedy_create(struct lov_request_set *set, struct lov_request *req)
         RETURN(rc);
 }
 
+static int min_stripe_count(int stripe_cnt, int flags)
+{
+        return (flags & LOV_USES_DEFAULT_STRIPE ?
+                stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
+}
+
 #define LOV_CREATE_RESEED_MULT 4
 #define LOV_CREATE_RESEED_MIN  1000
 /* Allocate objects on osts with round-robin algorithm */
-static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt)
+static int alloc_rr(struct lov_obd *lov, int *idx_arr, int *stripe_cnt,
+                    char *poolname, int flags)
 {
-        unsigned array_idx, ost_count = lov->desc.ld_tgt_count;
-        unsigned ost_active_count = lov->desc.ld_active_tgt_count;
-        int i, *idx_pos = idx_arr;
+        unsigned array_idx;
+        int i, rc, *idx_pos;
         __u32 ost_idx;
+        int ost_start_idx_temp;
+        int speed = 0;
+        int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
+        struct pool_desc *pool;
+        struct ost_pool *osts;
+        struct lov_qos_rr *lqr;
         ENTRY;
 
-        i = qos_calc_rr(lov);
-        if (i)
-                RETURN(i);
-
-        if (--lov->lov_start_count <= 0) {
-                lov->lov_start_idx = ll_rand() % ost_count;
-                lov->lov_start_count =
-                        (LOV_CREATE_RESEED_MIN / max(ost_active_count, 1U) +
-                         LOV_CREATE_RESEED_MULT) * max(ost_active_count, 1U);
-        } else if (*stripe_cnt >= ost_active_count ||
-                   lov->lov_start_idx > ost_count) {
+        pool = lov_find_pool(lov, poolname);
+        if (pool == NULL) {
+                osts = &(lov->lov_packed);
+                lqr = &(lov->lov_qos.lq_rr);
+        } else {
+                read_lock(&pool_tgt_rwlock(pool));
+                osts = &(pool->pool_obds);
+                lqr = &(pool->pool_rr);
+        }
+
+        rc = qos_calc_rr(lov, osts, lqr);
+        if (rc)
+                GOTO(out, rc);
+
+        if (--lqr->lqr_start_count <= 0) {
+                lqr->lqr_start_idx = ll_rand() % osts->op_count;
+                lqr->lqr_start_count =
+                        (LOV_CREATE_RESEED_MIN / max(osts->op_count, 1U) +
+                         LOV_CREATE_RESEED_MULT) * max(osts->op_count, 1U);
+        } else if (stripe_cnt_min >= osts->op_count ||
+                   lqr->lqr_start_idx > osts->op_count) {
                 /* If we have allocated from all of the OSTs, slowly
-                   precess the next start */
-                lov->lov_start_idx %= ost_count;
-                ++lov->lov_offset_idx;
+                 * precess the next start if the OST/stripe count isn't
+                 * already doing this for us. */
+                lqr->lqr_start_idx %= osts->op_count;
+                if (*stripe_cnt > 1 && (osts->op_count % (*stripe_cnt)) != 1)
+                        ++lqr->lqr_offset_idx;
         }
-        array_idx = (lov->lov_start_idx + lov->lov_offset_idx) % ost_count;
+        down_read(&lov->lov_qos.lq_rw_sem);
+        ost_start_idx_temp = lqr->lqr_start_idx;
+
+repeat_find:
+        array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) % osts->op_count;
+        idx_pos = idx_arr;
 #ifdef QOS_DEBUG
-        CDEBUG(D_QOS, "want %d startidx %d startcnt %d offset %d arrayidx %d\n",
-               *stripe_cnt, lov->lov_start_idx, lov->lov_start_count,
-               lov->lov_offset_idx, array_idx);
+        CDEBUG(D_QOS, "pool '%s' want %d startidx %d startcnt %d offset %d "
+               "active %d count %d arrayidx %d\n", poolname,
+               *stripe_cnt, lqr->lqr_start_idx, lqr->lqr_start_count,
+               lqr->lqr_offset_idx, osts->op_count, osts->op_count, array_idx);
 #endif
 
-        down_read(&lov->lov_qos.lq_rw_sem);
-        for (i = 0; i < ost_count; i++, array_idx=(array_idx + 1) % ost_count) {
-                ++lov->lov_start_idx;
-                ost_idx = lov->lov_qos.lq_rr_array[array_idx];
+        for (i = 0; i < osts->op_count;
+                    i++, array_idx=(array_idx + 1) % osts->op_count) {
+                ++lqr->lqr_start_idx;
+                ost_idx = lqr->lqr_pool.op_array[array_idx];
 #ifdef QOS_DEBUG
                 CDEBUG(D_QOS, "#%d strt %d act %d strp %d ary %d idx %d\n",
-                       i, lov->lov_start_idx,
-                       lov->lov_tgts[ost_idx] ?
+                       i, lqr->lqr_start_idx,
+                       ((ost_idx != LOV_QOS_EMPTY) && lov->lov_tgts[ost_idx]) ?
                        lov->lov_tgts[ost_idx]->ltd_active : 0,
                        idx_pos - idx_arr, array_idx, ost_idx);
 #endif
-                if (!lov->lov_tgts[ost_idx] ||
+                if ((ost_idx == LOV_QOS_EMPTY) || !lov->lov_tgts[ost_idx] ||
                     !lov->lov_tgts[ost_idx]->ltd_active)
                         continue;
+
+                /* Fail Check before osc_precreate() is called
+                   so we can only 'fail' single OSC. */
+                if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
+                        continue;
+
+                /* Drop slow OSCs if we can */
+                if (obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed)
+                        continue;
+
                 *idx_pos = ost_idx;
                 idx_pos++;
                 /* We have enough stripes */
                 if (idx_pos - idx_arr == *stripe_cnt)
                         break;
         }
+        if ((speed < 2) && (idx_pos - idx_arr < stripe_cnt_min)) {
+                /* Try again, allowing slower OSCs */
+                speed++;
+                lqr->lqr_start_idx = ost_start_idx_temp;
+                goto repeat_find;
+        }
+
         up_read(&lov->lov_qos.lq_rw_sem);
 
         *stripe_cnt = idx_pos - idx_arr;
-        RETURN(0);
+out:
+        if (pool != NULL)
+                read_unlock(&pool_tgt_rwlock(pool));
+        RETURN(rc);
 }
 
 /* alloc objects on osts with specific stripe offset */
 static int alloc_specific(struct lov_obd *lov, struct lov_stripe_md *lsm,
                           int *idx_arr)
 {
-        unsigned ost_idx, ost_count = lov->desc.ld_tgt_count;
-        int i, *idx_pos = idx_arr;
+        unsigned ost_idx, array_idx, ost_count;
+        int i, rc, *idx_pos;
+        int speed = 0;
+        struct pool_desc *pool;
+        struct ost_pool *osts;
         ENTRY;
 
-        ost_idx = lsm->lsm_oinfo[0]->loi_ost_idx;
-        for (i = 0; i < ost_count; i++, ost_idx = (ost_idx + 1) % ost_count) {
+        pool = lov_find_pool(lov, lsm->lsm_pool_name);
+        if (pool == NULL) {
+                osts = &(lov->lov_packed);
+        } else {
+                read_lock(&pool_tgt_rwlock(pool));
+                osts = &(pool->pool_obds);
+        }
+
+        ost_count = osts->op_count;
+
+repeat_find:
+        /* search loi_ost_idx in ost array */
+        array_idx = 0;
+        for (i = 0; i < ost_count; i++) {
+                if (osts->op_array[i] == lsm->lsm_oinfo[0]->loi_ost_idx) {
+                        array_idx = i;
+                        break;
+                }
+        }
+        if (i == ost_count) {
+                CERROR("Start index %d not found in pool '%s'\n",
+                       lsm->lsm_oinfo[0]->loi_ost_idx, lsm->lsm_pool_name);
+                GOTO(out, rc = -EINVAL);
+        }
+
+        idx_pos = idx_arr;
+        for (i = 0; i < ost_count;
+             i++, array_idx = (array_idx + 1) % ost_count) {
+                ost_idx = osts->op_array[array_idx];
+
                 if (!lov->lov_tgts[ost_idx] ||
                     !lov->lov_tgts[ost_idx]->ltd_active) {
                         continue;
                 }
+
+                /* Fail Check before osc_precreate() is called
+                   so we can only 'fail' single OSC. */
+                if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && ost_idx == 0)
+                        continue;
+
+                /* Drop slow OSCs if we can, but not for requested start idx.
+                 *
+                 * This means "if OSC is slow and it is not the requested
+                 * start OST, then it can be skipped, otherwise skip it only
+                 * if it is inactive/recovering/out-of-space." */
+                if ((obd_precreate(lov->lov_tgts[ost_idx]->ltd_exp) > speed) &&
+                    (i != 0 || speed >= 2))
+                        continue;
+
                 *idx_pos = ost_idx;
                 idx_pos++;
-                /* got enough ost */
+                /* We have enough stripes */
                 if (idx_pos - idx_arr == lsm->lsm_stripe_count)
-                        RETURN(0);
+                        GOTO(out, rc = 0);
         }
+        if (speed < 2) {
+                /* Try again, allowing slower OSCs */
+                speed++;
+                goto repeat_find;
+        }
+
         /* If we were passed specific striping params, then a failure to
          * meet those requirements is an error, since we can't reallocate
          * that memory (it might be part of a larger array or something).
          *
          * We can only get here if lsm_stripe_count was originally > 1.
          */
-        CERROR("can't lstripe objid "LPX64": have "LPSZ" want %u\n",
-               lsm->lsm_object_id, idx_pos - idx_arr, lsm->lsm_stripe_count);
-        RETURN(-EFBIG);
+        CERROR("can't lstripe objid "LPX64": have %d want %u\n",
+               lsm->lsm_object_id, (int)(idx_pos - idx_arr),
+               lsm->lsm_stripe_count);
+        rc = -EFBIG;
+out:
+        if (pool != NULL)
+                read_unlock(&pool_tgt_rwlock(pool));
+        RETURN(rc);
 }
 
 /* Alloc objects on osts with optimization based on:
    - free space
    - network resources (shared OSS's)
 */
-static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
+static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt,
+                     char *poolname, int flags)
 {
         struct lov_obd *lov = &exp->exp_obd->u.lov;
         static time_t last_warn = 0;
         time_t now = cfs_time_current_sec();
         __u64 total_bavail, total_weight = 0;
-        __u32 ost_count;
         int nfound, good_osts, i, warn = 0, rc = 0;
+        int stripe_cnt_min = min_stripe_count(*stripe_cnt, flags);
+        struct pool_desc *pool;
+        struct ost_pool *osts;
+        struct lov_qos_rr *lqr;
         ENTRY;
 
+        if (stripe_cnt_min < 1)
+                RETURN(-EINVAL);
+
+        pool = lov_find_pool(lov, poolname);
+        if (pool == NULL) {
+                osts = &(lov->lov_packed);
+                lqr = &(lov->lov_qos.lq_rr);
+        } else {
+                read_lock(&pool_tgt_rwlock(pool));
+                osts = &(pool->pool_obds);
+                lqr = &(pool->pool_rr);
+        }
+
         lov_getref(exp->exp_obd);
+
+        /* Detect -EAGAIN early, before expensive lock is taken. */
+        if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
+                GOTO(out_nolock, rc = -EAGAIN);
+
+        /* Do actual allocation, use write lock here. */
         down_write(&lov->lov_qos.lq_rw_sem);
 
-        ost_count = lov->desc.ld_tgt_count;
+        /*
+         * Check again, while we were sleeping on @lq_rw_sem things could
+         * change.
+         */
+        if (!lov->lov_qos.lq_dirty && lov->lov_qos.lq_same_space)
+                GOTO(out, rc = -EAGAIN);
 
         if (lov->desc.ld_active_tgt_count < 2)
                 GOTO(out, rc = -EAGAIN);
@@ -595,41 +790,57 @@ static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
         if (cfs_time_sub(now, last_warn) > 60 * 30)
                 warn = 1;
         /* Find all the OSTs that are valid stripe candidates */
-        for (i = 0; i < ost_count; i++) {
+        for (i = 0; i < osts->op_count; i++) {
                 __u64 bavail;
 
-                if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
+                if (!lov->lov_tgts[osts->op_array[i]] ||
+                    !lov->lov_tgts[osts->op_array[i]]->ltd_active)
                         continue;
-                bavail = TGT_BAVAIL(i);
+                bavail = TGT_BAVAIL(osts->op_array[i]);
                 if (!bavail) {
                         if (warn) {
                                 CDEBUG(D_QOS, "no free space on %s\n",
-                                     obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
+                                     obd_uuid2str(&lov->lov_tgts[osts->op_array[i]]->ltd_uuid));
                                 last_warn = now;
                         }
                         continue;
                 }
-                if (!TGT_FFREE(i)) {
+                if (!TGT_FFREE(osts->op_array[i])) {
                         if (warn) {
                                 CDEBUG(D_QOS, "no free inodes on %s\n",
-                                     obd_uuid2str(&lov->lov_tgts[i]->ltd_uuid));
+                                     obd_uuid2str(&lov->lov_tgts[osts->op_array[i]]->ltd_uuid));
                                 last_warn = now;
                         }
                         continue;
                 }
 
-                lov->lov_tgts[i]->ltd_qos.ltq_usable = 1;
-                qos_calc_weight(lov, i);
+                /* Fail Check before osc_precreate() is called
+                   so we can only 'fail' single OSC. */
+                if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_PRECREATE) && osts->op_array[i] == 0)
+                        continue;
+
+                if (obd_precreate(lov->lov_tgts[osts->op_array[i]]->ltd_exp) > 2)
+                        continue;
+
+                lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable = 1;
+                qos_calc_weight(lov, osts->op_array[i]);
                 total_bavail += bavail;
-                total_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
+                total_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
 
                 good_osts++;
         }
 
+#ifdef QOS_DEBUG
+        CDEBUG(D_QOS, "found %d good osts\n", good_osts);
+#endif
+
+        if (good_osts < stripe_cnt_min)
+                GOTO(out, rc = -EAGAIN);
+
         if (!total_bavail)
                 GOTO(out, rc = -ENOSPC);
 
-        /* if we don't have enough good OSTs, we reduce the stripe count. */
+        /* We have enough osts */
         if (good_osts < *stripe_cnt)
                 *stripe_cnt = good_osts;
 
@@ -671,17 +882,24 @@ static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
 
                 /* On average, this will hit larger-weighted osts more often.
                    0-weight osts will always get used last (only when rand=0).*/
-                for (i = 0; i < ost_count; i++) {
-                        if (!lov->lov_tgts[i]->ltd_qos.ltq_usable)
+                for (i = 0; i < osts->op_count; i++) {
+                        if (!lov->lov_tgts[osts->op_array[i]] ||
+                            !lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_usable)
                                 continue;
-                        cur_weight += lov->lov_tgts[i]->ltd_qos.ltq_weight;
+
+                        cur_weight += lov->lov_tgts[osts->op_array[i]]->ltd_qos.ltq_weight;
+#ifdef QOS_DEBUG
+                        CDEBUG(D_QOS, "stripe_cnt=%d nfound=%d cur_weight="LPU64
+                                      " rand="LPU64" total_weight="LPU64"\n",
+                               *stripe_cnt, nfound, cur_weight, rand, total_weight);
+#endif
                         if (cur_weight >= rand) {
 #ifdef QOS_DEBUG
                                 CDEBUG(D_QOS, "assigned stripe=%d to idx=%d\n",
-                                       nfound, i);
+                                       nfound, osts->op_array[i]);
 #endif
-                                idx_arr[nfound++] = i;
-                                qos_used(lov, i, &total_weight);
+                                idx_arr[nfound++] = osts->op_array[i];
+                                qos_used(lov, osts, osts->op_array[i], &total_weight);
                                 rc = 0;
                                 break;
                         }
@@ -697,8 +915,12 @@ static int alloc_qos(struct obd_export *exp, int *idx_arr, int *stripe_cnt)
 out:
         up_write(&lov->lov_qos.lq_rw_sem);
 
+out_nolock:
+        if (pool != NULL)
+                read_unlock(&pool_tgt_rwlock(pool));
+
         if (rc == -EAGAIN)
-                rc = alloc_rr(lov, idx_arr, stripe_cnt);
+                rc = alloc_rr(lov, idx_arr, stripe_cnt, poolname, flags);
 
         lov_putref(exp->exp_obd);
         RETURN(rc);
@@ -706,7 +928,7 @@ out:
 
 /* return new alloced stripe count on success */
 static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
-                           int newea, int **idx_arr, int *arr_cnt)
+                           int newea, int **idx_arr, int *arr_cnt, int flags)
 {
         struct lov_obd *lov = &exp->exp_obd->u.lov;
         int stripe_cnt = lsm->lsm_stripe_count;
@@ -722,8 +944,9 @@ static int alloc_idx_array(struct obd_export *exp, struct lov_stripe_md *lsm,
                 tmp_arr[i] = -1;
 
         if (newea ||
-            lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count) 
-                rc = alloc_qos(exp, tmp_arr, &stripe_cnt);
+            lsm->lsm_oinfo[0]->loi_ost_idx >= lov->desc.ld_tgt_count)
+                rc = alloc_qos(exp, tmp_arr, &stripe_cnt,
+                               lsm->lsm_pool_name, flags);
         else
                 rc = alloc_specific(lov, lsm, tmp_arr);
 
@@ -751,13 +974,15 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
         struct obdo *src_oa = set->set_oi->oi_oa;
         struct obd_trans_info *oti = set->set_oti;
         int i, stripes, rc = 0, newea = 0;
-        int *idx_arr, idx_cnt = 0;
+        int flag = LOV_USES_ASSIGNED_STRIPE;
+        int *idx_arr = NULL, idx_cnt = 0;
         ENTRY;
 
         LASSERT(src_oa->o_valid & OBD_MD_FLID);
+        LASSERT(src_oa->o_valid & OBD_MD_FLGROUP);
 
         if (set->set_oi->oi_md == NULL) {
-                int stripe_cnt = lov_get_stripecnt(lov, 0);
+                int stripes_def = lov_get_stripecnt(lov, 0);
 
                 /* If the MDS file was truncated up to some size, stripe over
                  * enough OSTs to allow the file to be created at that size.
@@ -768,7 +993,6 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
                         /* Find a small number of stripes we can use
                            (up to # of active osts). */
                         stripes = 1;
-                        lov_getref(exp->exp_obd);
                         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
                                 if (!lov->lov_tgts[i] ||
                                     !lov->lov_tgts[i]->ltd_active)
@@ -778,12 +1002,12 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
                                         break;
                                 stripes++;
                         }
-                        lov_putref(exp->exp_obd);
 
-                        if (stripes < stripe_cnt)
-                                stripes = stripe_cnt;
+                        if (stripes < stripes_def)
+                                stripes = stripes_def;
                 } else {
-                        stripes = stripe_cnt;
+                         flag = LOV_USES_DEFAULT_STRIPE;
+                         stripes = stripes_def;
                 }
 
                 rc = lov_alloc_memmd(&set->set_oi->oi_md, stripes,
@@ -792,12 +1016,14 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
                                      LOV_MAGIC);
                 if (rc < 0)
                         GOTO(out_err, rc);
-                rc = 0;
                 newea = 1;
+                rc = 0;
         }
 
         lsm = set->set_oi->oi_md;
         lsm->lsm_object_id = src_oa->o_id;
+        lsm->lsm_object_gr = src_oa->o_gr;
+
         if (!lsm->lsm_stripe_size)
                 lsm->lsm_stripe_size = lov->desc.ld_default_stripe_size;
         if (!lsm->lsm_pattern) {
@@ -805,10 +1031,11 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
                 lsm->lsm_pattern = lov->desc.ld_pattern;
         }
 
-        stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt);
-        LASSERT(stripes <= lsm->lsm_stripe_count);
+        stripes = alloc_idx_array(exp, lsm, newea, &idx_arr, &idx_cnt, flag);
         if (stripes <= 0)
                 GOTO(out_err, rc = stripes ? stripes : -EIO);
+        LASSERTF(stripes <= lsm->lsm_stripe_count,"requested %d allocated %d\n",
+                 lsm->lsm_stripe_count, stripes);
 
         for (i = 0; i < stripes; i++) {
                 struct lov_request *req;
@@ -825,7 +1052,7 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
                 if (req->rq_oi.oi_md == NULL)
                         GOTO(out_err, rc = -ENOMEM);
 
-                req->rq_oi.oi_oa = obdo_alloc();
+                OBDO_ALLOC(req->rq_oi.oi_oa);
                 if (req->rq_oi.oi_oa == NULL)
                         GOTO(out_err, rc = -ENOMEM);
 
@@ -845,7 +1072,6 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
                         CDEBUG(D_INODE, "stripe %d has size "LPU64"/"LPU64"\n",
                                i, req->rq_oi.oi_oa->o_size, src_oa->o_size);
                 }
-
         }
         LASSERT(set->set_count == stripes);
 
@@ -861,7 +1087,8 @@ int qos_prep_create(struct obd_export *exp, struct lov_request_set *set)
 out_err:
         if (newea && rc)
                 obd_free_memmd(exp, &set->set_oi->oi_md);
-        free_idx_array(idx_arr, idx_cnt);
+        if (idx_arr)
+                free_idx_array(idx_arr, idx_cnt);
         EXIT;
         return rc;
 }
@@ -871,4 +1098,3 @@ void qos_update(struct lov_obd *lov)
         ENTRY;
         lov->lov_qos.lq_dirty = 1;
 }
-