Whamcloud - gitweb
LU-12538 lod: Add missed qos_rr_init
[fs/lustre-release.git] / lustre / lod / lod_qos.c
index cf578b5..7d738f3 100644 (file)
@@ -38,6 +38,8 @@
 #define DEBUG_SUBSYSTEM S_LOV
 
 #include <asm/div64.h>
+#include <linux/random.h>
+
 #include <libcfs/libcfs.h>
 #include <uapi/linux/lustre/lustre_idl.h>
 #include <lustre_swab.h>
                       OST_TGT(lod,i)->ltd_statfs.os_bsize)
 
 /**
- * Add a new target to Quality of Service (QoS) target table.
- *
- * Add a new OST target to the structure representing an OSS. Resort the list
- * of known OSSs by the number of OSTs attached to each OSS. The OSS list is
- * protected internally and no external locking is required.
- *
- * \param[in] lod              LOD device
- * \param[in] ost_desc         OST description
- *
- * \retval 0                   on success
- * \retval -ENOMEM             on error
- */
-int qos_add_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
-{
-       struct lod_qos_oss *oss = NULL, *temposs;
-       struct obd_export  *exp = ost_desc->ltd_exp;
-       int                 rc = 0, found = 0;
-       struct list_head   *list;
-       __u32 id = 0;
-       ENTRY;
-
-       down_write(&lod->lod_qos.lq_rw_sem);
-       /*
-        * a bit hacky approach to learn NID of corresponding connection
-        * but there is no official API to access information like this
-        * with OSD API.
-        */
-       list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
-               if (obd_uuid_equals(&oss->lqo_uuid,
-                                   &exp->exp_connection->c_remote_uuid)) {
-                       found++;
-                       break;
-               }
-               if (oss->lqo_id > id)
-                       id = oss->lqo_id;
-       }
-
-       if (!found) {
-               OBD_ALLOC_PTR(oss);
-               if (!oss)
-                       GOTO(out, rc = -ENOMEM);
-               memcpy(&oss->lqo_uuid, &exp->exp_connection->c_remote_uuid,
-                      sizeof(oss->lqo_uuid));
-               ++id;
-               oss->lqo_id = id;
-       } else {
-               /* Assume we have to move this one */
-               list_del(&oss->lqo_oss_list);
-       }
-
-       oss->lqo_ost_count++;
-       ost_desc->ltd_qos.ltq_oss = oss;
-
-       CDEBUG(D_QOS, "add tgt %s to OSS %s (%d OSTs)\n",
-              obd_uuid2str(&ost_desc->ltd_uuid), obd_uuid2str(&oss->lqo_uuid),
-              oss->lqo_ost_count);
-
-       /* Add sorted by # of OSTs.  Find the first entry that we're
-          bigger than... */
-       list = &lod->lod_qos.lq_oss_list;
-       list_for_each_entry(temposs, list, lqo_oss_list) {
-               if (oss->lqo_ost_count > temposs->lqo_ost_count)
-                       break;
-       }
-       /* ...and add before it.  If we're the first or smallest, temposs
-          points to the list head, and we add to the end. */
-       list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
-
-       lod->lod_qos.lq_dirty = 1;
-       lod->lod_qos.lq_rr.lqr_dirty = 1;
-
-out:
-       up_write(&lod->lod_qos.lq_rw_sem);
-       RETURN(rc);
-}
-
-/**
- * Remove OST target from QoS table.
- *
- * Removes given OST target from QoS table and releases related OSS structure
- * if no OSTs remain on the OSS.
- *
- * \param[in] lod              LOD device
- * \param[in] ost_desc         OST description
- *
- * \retval 0                   on success
- * \retval -ENOENT             if no OSS was found
- */
-int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
-{
-       struct lod_qos_oss *oss;
-       int                 rc = 0;
-       ENTRY;
-
-       down_write(&lod->lod_qos.lq_rw_sem);
-       oss = ost_desc->ltd_qos.ltq_oss;
-       if (!oss)
-               GOTO(out, rc = -ENOENT);
-
-       oss->lqo_ost_count--;
-       if (oss->lqo_ost_count == 0) {
-               CDEBUG(D_QOS, "removing OSS %s\n",
-                      obd_uuid2str(&oss->lqo_uuid));
-               list_del(&oss->lqo_oss_list);
-               ost_desc->ltd_qos.ltq_oss = NULL;
-               OBD_FREE_PTR(oss);
-       }
-
-       lod->lod_qos.lq_dirty = 1;
-       lod->lod_qos.lq_rr.lqr_dirty = 1;
-out:
-       up_write(&lod->lod_qos.lq_rw_sem);
-       RETURN(rc);
-}
-
-/**
  * Check whether the target is available for new OST objects.
  *
  * Request statfs data from the given target and verify it's active and not
@@ -192,8 +78,9 @@ out:
  * \retval negative    negated errno on error
 
  */
-static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
-                               int index, struct obd_statfs *sfs)
+int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
+                        int index, struct obd_statfs *sfs,
+                        struct obd_statfs_info *info)
 {
        struct lod_tgt_desc *ost;
        int                  rc;
@@ -203,7 +90,7 @@ static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
        ost = OST_TGT(d,index);
        LASSERT(ost);
 
-       rc = dt_statfs(env, ost->ltd_ost, sfs);
+       rc = dt_statfs(env, ost->ltd_ost, sfs, info);
 
        if (rc == 0 && ((sfs->os_state & OS_STATE_ENOSPC) ||
            (sfs->os_state & OS_STATE_ENOINO && sfs->os_fprecreated == 0)))
@@ -293,7 +180,7 @@ void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod)
                idx = osts->op_array[i];
                avail = OST_TGT(lod,idx)->ltd_statfs.os_bavail;
                if (lod_statfs_and_check(env, lod, idx,
-                                        &OST_TGT(lod, idx)->ltd_statfs))
+                                        &OST_TGT(lod, idx)->ltd_statfs, NULL))
                        continue;
                if (OST_TGT(lod,idx)->ltd_statfs.os_bavail != avail)
                        /* recalculate weigths */
@@ -324,12 +211,13 @@ out:
  */
 static int lod_qos_calc_ppo(struct lod_device *lod)
 {
-       struct lod_qos_oss *oss;
-       __u64               ba_max, ba_min, temp;
-       __u32               num_active;
-       unsigned int        i;
-       int                 rc, prio_wide;
-       time64_t            now, age;
+       struct lu_svr_qos *oss;
+       __u64 ba_max, ba_min, temp;
+       __u32 num_active;
+       unsigned int i;
+       int rc, prio_wide;
+       time64_t now, age;
+
        ENTRY;
 
        if (!lod->lod_qos.lq_dirty)
@@ -340,9 +228,9 @@ static int lod_qos_calc_ppo(struct lod_device *lod)
                GOTO(out, rc = -EAGAIN);
 
        /* find bavail on each OSS */
-       list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
-                           oss->lqo_bavail = 0;
-       lod->lod_qos.lq_active_oss_count = 0;
+       list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list)
+               oss->lsq_bavail = 0;
+       lod->lod_qos.lq_active_svr_count = 0;
 
        /*
         * How badly user wants to select OSTs "widely" (not recently chosen
@@ -366,9 +254,9 @@ static int lod_qos_calc_ppo(struct lod_device *lod)
                ba_max = max(temp, ba_max);
 
                /* Count the number of usable OSS's */
-               if (OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail == 0)
-                       lod->lod_qos.lq_active_oss_count++;
-               OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_bavail += temp;
+               if (OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail == 0)
+                       lod->lod_qos.lq_active_svr_count++;
+               OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_bavail += temp;
 
                /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
                temp >>= 1;
@@ -386,7 +274,7 @@ static int lod_qos_calc_ppo(struct lod_device *lod)
                                (age / lod->lod_desc.ld_qos_maxage);
        }
 
-       num_active = lod->lod_qos.lq_active_oss_count - 1;
+       num_active = lod->lod_qos.lq_active_svr_count - 1;
        if (num_active < 1) {
                /* If there's only 1 OSS, we can't penalize it, so instead
                   we have to double the OST penalty */
@@ -396,18 +284,18 @@ static int lod_qos_calc_ppo(struct lod_device *lod)
        }
 
        /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
-       list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
-               temp = oss->lqo_bavail >> 1;
-               do_div(temp, oss->lqo_ost_count * num_active);
-               oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
+       list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
+               temp = oss->lsq_bavail >> 1;
+               do_div(temp, oss->lsq_tgt_count * num_active);
+               oss->lsq_penalty_per_obj = (temp * prio_wide) >> 8;
 
-               age = (now - oss->lqo_used) >> 3;
+               age = (now - oss->lsq_used) >> 3;
                if (lod->lod_qos.lq_reset ||
                    age > 32 * lod->lod_desc.ld_qos_maxage)
-                       oss->lqo_penalty = 0;
+                       oss->lsq_penalty = 0;
                else if (age > lod->lod_desc.ld_qos_maxage)
                        /* Decay OSS penalty. */
-                       oss->lqo_penalty >>= age / lod->lod_desc.ld_qos_maxage;
+                       oss->lsq_penalty >>= age / lod->lod_desc.ld_qos_maxage;
        }
 
        lod->lod_qos.lq_dirty = 0;
@@ -447,12 +335,12 @@ static int lod_qos_calc_weight(struct lod_device *lod, int i)
        __u64 temp, temp2;
 
        temp = TGT_BAVAIL(i);
-       temp2 = OST_TGT(lod,i)->ltd_qos.ltq_penalty +
-               OST_TGT(lod,i)->ltd_qos.ltq_oss->lqo_penalty;
+       temp2 = OST_TGT(lod, i)->ltd_qos.ltq_penalty +
+               OST_TGT(lod, i)->ltd_qos.ltq_svr->lsq_penalty;
        if (temp < temp2)
-               OST_TGT(lod,i)->ltd_qos.ltq_weight = 0;
+               OST_TGT(lod, i)->ltd_qos.ltq_weight = 0;
        else
-               OST_TGT(lod,i)->ltd_qos.ltq_weight = temp - temp2;
+               OST_TGT(lod, i)->ltd_qos.ltq_weight = temp - temp2;
        return 0;
 }
 
@@ -474,7 +362,7 @@ static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
                        __u32 index, __u64 *total_wt)
 {
        struct lod_tgt_desc *ost;
-       struct lod_qos_oss  *oss;
+       struct lu_svr_qos  *oss;
        unsigned int j;
        ENTRY;
 
@@ -484,28 +372,28 @@ static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
        /* Don't allocate on this devuce anymore, until the next alloc_qos */
        ost->ltd_qos.ltq_usable = 0;
 
-       oss = ost->ltd_qos.ltq_oss;
+       oss = ost->ltd_qos.ltq_svr;
 
        /* Decay old penalty by half (we're adding max penalty, and don't
           want it to run away.) */
        ost->ltd_qos.ltq_penalty >>= 1;
-       oss->lqo_penalty >>= 1;
+       oss->lsq_penalty >>= 1;
 
        /* mark the OSS and OST as recently used */
-       ost->ltd_qos.ltq_used = oss->lqo_used = ktime_get_real_seconds();
+       ost->ltd_qos.ltq_used = oss->lsq_used = ktime_get_real_seconds();
 
        /* Set max penalties for this OST and OSS */
        ost->ltd_qos.ltq_penalty +=
                ost->ltd_qos.ltq_penalty_per_obj * lod->lod_ostnr;
-       oss->lqo_penalty += oss->lqo_penalty_per_obj *
-               lod->lod_qos.lq_active_oss_count;
+       oss->lsq_penalty += oss->lsq_penalty_per_obj *
+               lod->lod_qos.lq_active_svr_count;
 
        /* Decrease all OSS penalties */
-       list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
-               if (oss->lqo_penalty < oss->lqo_penalty_per_obj)
-                       oss->lqo_penalty = 0;
+       list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
+               if (oss->lsq_penalty < oss->lsq_penalty_per_obj)
+                       oss->lsq_penalty = 0;
                else
-                       oss->lqo_penalty -= oss->lqo_penalty_per_obj;
+                       oss->lsq_penalty -= oss->lsq_penalty_per_obj;
        }
 
        *total_wt = 0;
@@ -539,21 +427,14 @@ static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
                          i, ost->ltd_qos.ltq_usable, TGT_BAVAIL(i) >> 10,
                          ost->ltd_qos.ltq_penalty_per_obj >> 10,
                          ost->ltd_qos.ltq_penalty >> 10,
-                         ost->ltd_qos.ltq_oss->lqo_penalty_per_obj >> 10,
-                         ost->ltd_qos.ltq_oss->lqo_penalty >> 10,
+                         ost->ltd_qos.ltq_svr->lsq_penalty_per_obj >> 10,
+                         ost->ltd_qos.ltq_svr->lsq_penalty >> 10,
                          ost->ltd_qos.ltq_weight >> 10);
        }
 
        RETURN(0);
 }
 
-void lod_qos_rr_init(struct lod_qos_rr *lqr)
-{
-       spin_lock_init(&lqr->lqr_alloc);
-       lqr->lqr_dirty = 1;
-}
-
-
 #define LOV_QOS_EMPTY ((__u32)-1)
 
 /**
@@ -573,9 +454,9 @@ void lod_qos_rr_init(struct lod_qos_rr *lqr)
  * \retval -ENOMEM     fails to allocate the array
  */
 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
-                          struct lod_qos_rr *lqr)
+                          struct lu_qos_rr *lqr)
 {
-       struct lod_qos_oss  *oss;
+       struct lu_svr_qos  *oss;
        struct lod_tgt_desc *ost;
        unsigned placed, real_count;
        unsigned int i;
@@ -617,7 +498,7 @@ static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
 
        /* Place all the OSTs from 1 OSS at the same time. */
        placed = 0;
-       list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
+       list_for_each_entry(oss, &lod->lod_qos.lq_svr_list, lsq_svr_list) {
                int j = 0;
 
                for (i = 0; i < lqr->lqr_pool.op_count; i++) {
@@ -629,11 +510,11 @@ static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
 
                        ost = OST_TGT(lod,src_pool->op_array[i]);
                        LASSERT(ost && ost->ltd_ost);
-                       if (ost->ltd_qos.ltq_oss != oss)
+                       if (ost->ltd_qos.ltq_svr != oss)
                                continue;
 
                        /* Evenly space these OSTs across arrayspace */
-                       next = j * lqr->lqr_pool.op_count / oss->lqo_ost_count;
+                       next = j * lqr->lqr_pool.op_count / oss->lsq_tgt_count;
                        while (lqr->lqr_pool.op_array[next] != LOV_QOS_EMPTY)
                                next = (next + 1) % lqr->lqr_pool.op_count;
 
@@ -894,7 +775,7 @@ static inline bool lod_should_avoid_ost(struct lod_object *lo,
 {
        struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
        struct lod_tgt_desc *ost = OST_TGT(lod, index);
-       struct lod_qos_oss *lqo = ost->ltd_qos.ltq_oss;
+       struct lu_svr_qos *lsq = ost->ltd_qos.ltq_svr;
        bool used = false;
        int i;
 
@@ -913,7 +794,7 @@ static inline bool lod_should_avoid_ost(struct lod_object *lo,
 
        /* check OSS use */
        for (i = 0; i < lag->lag_oaa_count; i++) {
-               if (lag->lag_oss_avoid_array[i] == lqo->lqo_id) {
+               if (lag->lag_oss_avoid_array[i] == lsq->lsq_id) {
                        used = true;
                        break;
                }
@@ -936,11 +817,13 @@ static inline bool lod_should_avoid_ost(struct lod_object *lo,
 
 static int lod_check_and_reserve_ost(const struct lu_env *env,
                                     struct lod_object *lo,
+                                    struct lod_layout_component *lod_comp,
                                     struct obd_statfs *sfs, __u32 ost_idx,
                                     __u32 speed, __u32 *s_idx,
                                     struct dt_object **stripe,
                                     __u32 *ost_indices,
-                                    struct thandle *th)
+                                    struct thandle *th,
+                                    bool *overstriped)
 {
        struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
        struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
@@ -949,7 +832,7 @@ static int lod_check_and_reserve_ost(const struct lu_env *env,
        int rc;
        ENTRY;
 
-       rc = lod_statfs_and_check(env, lod, ost_idx, sfs);
+       rc = lod_statfs_and_check(env, lod, ost_idx, sfs, NULL);
        if (rc)
                RETURN(rc);
 
@@ -989,11 +872,14 @@ static int lod_check_and_reserve_ost(const struct lu_env *env,
                          "component\n", speed, ost_idx);
                RETURN(rc);
        }
-       /*
-        * do not put >1 objects on a single OST
-        */
-       if (lod_qos_is_ost_used(env, ost_idx, stripe_idx))
-               RETURN(rc);
+
+       /* do not put >1 objects on a single OST, except for overstriping */
+       if (lod_qos_is_ost_used(env, ost_idx, stripe_idx)) {
+               if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
+                       *overstriped = true;
+               else
+                       RETURN(rc);
+       }
 
        o = lod_qos_declare_object_on(env, lod, ost_idx, th);
        if (IS_ERR(o)) {
@@ -1052,12 +938,14 @@ static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
        struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
        struct pool_desc  *pool = NULL;
        struct ost_pool   *osts;
-       struct lod_qos_rr *lqr;
+       struct lu_qos_rr *lqr;
        unsigned int    i, array_idx;
        __u32 ost_start_idx_temp;
        __u32 stripe_idx = 0;
        __u32 stripe_count, stripe_count_min, ost_idx;
        int rc, speed = 0, ost_connecting = 0;
+       int stripes_per_ost = 1;
+       bool overstriped = false;
        ENTRY;
 
        LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
@@ -1088,7 +976,7 @@ static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
        down_read(&m->lod_qos.lq_rw_sem);
        spin_lock(&lqr->lqr_alloc);
        if (--lqr->lqr_start_count <= 0) {
-               lqr->lqr_start_idx = cfs_rand() % osts->op_count;
+               lqr->lqr_start_idx = prandom_u32_max(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);
@@ -1111,7 +999,12 @@ repeat_find:
                  stripe_count, lqr->lqr_start_idx, lqr->lqr_start_count,
                  lqr->lqr_offset_idx, osts->op_count, osts->op_count);
 
-       for (i = 0; i < osts->op_count && stripe_idx < stripe_count; i++) {
+       if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
+               stripes_per_ost =
+                       (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
+
+       for (i = 0; i < osts->op_count * stripes_per_ost
+            && stripe_idx < stripe_count; i++) {
                array_idx = (lqr->lqr_start_idx + lqr->lqr_offset_idx) %
                                osts->op_count;
                ++lqr->lqr_start_idx;
@@ -1131,9 +1024,9 @@ repeat_find:
                        continue;
 
                spin_unlock(&lqr->lqr_alloc);
-               rc = lod_check_and_reserve_ost(env, lo, sfs, ost_idx, speed,
-                                              &stripe_idx, stripe, ost_indices,
-                                              th);
+               rc = lod_check_and_reserve_ost(env, lo, lod_comp, sfs, ost_idx,
+                                              speed, &stripe_idx, stripe,
+                                              ost_indices, th, &overstriped);
                spin_lock(&lqr->lqr_alloc);
 
                if (rc != 0 && OST_TGT(m, ost_idx)->ltd_connecting)
@@ -1151,6 +1044,12 @@ repeat_find:
        spin_unlock(&lqr->lqr_alloc);
        up_read(&m->lod_qos.lq_rw_sem);
 
+       /* If there are enough OSTs, a component with overstriping requested
+        * will not actually end up overstriped.  The comp should reflect this.
+        */
+       if (!overstriped)
+               lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
+
        if (stripe_idx) {
                lod_comp->llc_stripe_count = stripe_idx;
                /* at least one stripe is allocated */
@@ -1249,15 +1148,16 @@ static int lod_alloc_ost_list(const struct lu_env *env, struct lod_object *lo,
                        break;
                }
 
-               /*
-                * do not put >1 objects on a single OST
+               /* do not put >1 objects on a single OST, except for
+                * overstriping
                 */
-               if (lod_qos_is_ost_used(env, ost_idx, stripe_count)) {
+               if (lod_qos_is_ost_used(env, ost_idx, stripe_count) &&
+                   !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)) {
                        rc = -EINVAL;
                        break;
                }
 
-               rc = lod_statfs_and_check(env, m, ost_idx, sfs);
+               rc = lod_statfs_and_check(env, m, ost_idx, sfs, NULL);
                if (rc < 0) /* this OSP doesn't feel well */
                        break;
 
@@ -1315,13 +1215,15 @@ static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
        struct lod_layout_component *lod_comp;
        struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
        struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
-       struct dt_object  *o;
-       __u32              ost_idx;
-       unsigned int       i, array_idx, ost_count;
-       int                rc, stripe_num = 0;
-       int                speed = 0;
+       struct dt_object *o;
+       __u32 ost_idx;
+       unsigned int i, array_idx, ost_count;
+       int rc, stripe_num = 0;
+       int speed = 0;
        struct pool_desc  *pool = NULL;
        struct ost_pool   *osts;
+       int stripes_per_ost = 1;
+       bool overstriped = false;
        ENTRY;
 
        LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
@@ -1359,7 +1261,11 @@ repeat_find:
                GOTO(out, rc = -EINVAL);
        }
 
-       for (i = 0; i < ost_count;
+       if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
+               stripes_per_ost =
+                       (lod_comp->llc_stripe_count - 1)/ost_count + 1;
+
+       for (i = 0; i < ost_count * stripes_per_ost;
                        i++, array_idx = (array_idx + 1) % ost_count) {
                ost_idx = osts->op_array[array_idx];
 
@@ -1372,10 +1278,15 @@ repeat_find:
                        continue;
 
                /*
-                * do not put >1 objects on a single OST
+                * do not put >1 objects on a single OST, except for
+                * overstriping, where it is intended
                 */
-               if (lod_qos_is_ost_used(env, ost_idx, stripe_num))
-                       continue;
+               if (lod_qos_is_ost_used(env, ost_idx, stripe_num)) {
+                       if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
+                               overstriped = true;
+                       else
+                               continue;
+               }
 
                /*
                 * try not allocate on the OST used by other component
@@ -1390,7 +1301,7 @@ repeat_find:
                 * start OST, then it can be skipped, otherwise skip it only
                 * if it is inactive/recovering/out-of-space." */
 
-               rc = lod_statfs_and_check(env, m, ost_idx, sfs);
+               rc = lod_statfs_and_check(env, m, ost_idx, sfs, NULL);
                if (rc) {
                        /* this OSP doesn't feel well */
                        continue;
@@ -1437,6 +1348,13 @@ repeat_find:
               PFID(lu_object_fid(lod2lu_obj(lo))), stripe_num,
               lod_comp->llc_stripe_count);
        rc = stripe_num == 0 ? -ENOSPC : -EFBIG;
+
+       /* If there are enough OSTs, a component with overstriping requessted
+        * will not actually end up overstriped.  The comp should reflect this.
+        */
+       if (rc == 0 && !overstriped)
+               lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
+
 out:
        if (pool != NULL) {
                up_read(&pool_tgt_rw_sem(pool));
@@ -1527,6 +1445,8 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
        struct ost_pool *osts;
        unsigned int i;
        __u32 nfound, good_osts, stripe_count, stripe_count_min;
+       bool overstriped = false;
+       int stripes_per_ost = 1;
        int rc = 0;
        ENTRY;
 
@@ -1551,6 +1471,10 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
        if (!lod_qos_is_usable(lod))
                GOTO(out_nolock, rc = -EAGAIN);
 
+       if (lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING)
+               stripes_per_ost =
+                       (lod_comp->llc_stripe_count - 1)/osts->op_count + 1;
+
        /* Do actual allocation, use write lock here. */
        down_write(&lod->lod_qos.lq_rw_sem);
 
@@ -1578,7 +1502,8 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
                ost = OST_TGT(lod, osts->op_array[i]);
                ost->ltd_qos.ltq_usable = 0;
 
-               rc = lod_statfs_and_check(env, lod, osts->op_array[i], sfs);
+               rc = lod_statfs_and_check(env, lod, osts->op_array[i],
+                                         sfs, NULL);
                if (rc) {
                        /* this OSP doesn't feel well */
                        continue;
@@ -1605,42 +1530,21 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
        if (good_osts < stripe_count_min)
                GOTO(out, rc = -EAGAIN);
 
-       /* We have enough osts */
-       if (good_osts < stripe_count)
-               stripe_count = good_osts;
+       /* If we do not have enough OSTs for the requested stripe count, do not
+        * put more stripes per OST than requested.
+        */
+       if (stripe_count / stripes_per_ost > good_osts)
+               stripe_count = good_osts * stripes_per_ost;
 
        /* Find enough OSTs with weighted random allocation. */
        nfound = 0;
        while (nfound < stripe_count) {
-               __u64 rand, cur_weight;
+               u64 rand, cur_weight;
 
                cur_weight = 0;
                rc = -ENOSPC;
 
-               if (total_weight) {
-#if BITS_PER_LONG == 32
-                       rand = cfs_rand() % (unsigned)total_weight;
-                       /* If total_weight > 32-bit, first generate the high
-                        * 32 bits of the random number, then add in the low
-                        * 32 bits (truncated to the upper limit, if needed) */
-                       if (total_weight > 0xffffffffULL)
-                               rand = (__u64)(cfs_rand() %
-                                       (unsigned)(total_weight >> 32)) << 32;
-                       else
-                               rand = 0;
-
-                       if (rand == (total_weight & 0xffffffff00000000ULL))
-                               rand |= cfs_rand() % (unsigned)total_weight;
-                       else
-                               rand |= cfs_rand();
-
-#else
-                       rand = ((__u64)cfs_rand() << 32 | cfs_rand()) %
-                               total_weight;
-#endif
-               } else {
-                       rand = 0;
-               }
+               rand = lu_prandom_u64_max(total_weight);
 
                /* On average, this will hit larger-weighted OSTs more often.
                 * 0-weight OSTs will always get used last (only when rand=0) */
@@ -1666,12 +1570,21 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
 
                        QOS_DEBUG("stripe=%d to idx=%d\n", nfound, idx);
                        /*
-                        * do not put >1 objects on a single OST
+                        * do not put >1 objects on a single OST, except for
+                        * overstriping
                         */
-                       if (lod_qos_is_ost_used(env, idx, nfound) ||
-                           lod_comp_is_ost_used(env, lo, idx))
+                       if ((lod_comp_is_ost_used(env, lo, idx)) &&
+                           !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
                                continue;
 
+                       if (lod_qos_is_ost_used(env, idx, nfound)) {
+                               if (lod_comp->llc_pattern &
+                                   LOV_PATTERN_OVERSTRIPING)
+                                       overstriped = true;
+                               else
+                                       continue;
+                       }
+
                        o = lod_qos_declare_object_on(env, lod, idx, th);
                        if (IS_ERR(o)) {
                                QOS_DEBUG("can't declare object on #%u: %d\n",
@@ -1718,6 +1631,12 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
                rc = -EAGAIN;
        }
 
+       /* If there are enough OSTs, a component with overstriping requessted
+        * will not actually end up overstriped.  The comp should reflect this.
+        */
+       if (rc == 0 && !overstriped)
+               lod_comp->llc_pattern &= ~LOV_PATTERN_OVERSTRIPING;
+
 out:
        up_write(&lod->lod_qos.lq_rw_sem);
 
@@ -1732,12 +1651,15 @@ out_nolock:
 }
 
 /**
- * Find largest stripe count the caller can use.
+ * Check stripe count the caller can use.
+ *
+ * For new layouts (no initialized components), check the total size of the
+ * layout against the maximum EA size from the backing file system.  This
+ * stops us from creating a layout which will be too large once initialized.
  *
+ * For existing layouts (with initialized components):
  * Find the maximal possible stripe count not greater than \a stripe_count.
- * Sometimes suggested stripecount can't be reached for a number of reasons:
- * lack of enough active OSTs or the backend does not support EAs that large.
- * If the passed one is 0, then the filesystem's default one is used.
+ * If the provided stripe count is 0, then the filesystem's default is used.
  *
  * \param[in] lod      LOD device
  * \param[in] lo       The lod_object
@@ -1746,44 +1668,54 @@ out_nolock:
  * \retval             the maximum usable stripe count
  */
 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
-                          __u16 stripe_count)
+                          __u16 stripe_count, bool overstriping)
 {
        __u32 max_stripes = LOV_MAX_STRIPE_COUNT_OLD;
+       /* max stripe count is based on OSD ea size */
+       unsigned int easize = lod->lod_osd_max_easize;
+       int i;
+
 
        if (!stripe_count)
                stripe_count = lod->lod_desc.ld_default_stripe_count;
-       if (stripe_count > lod->lod_desc.ld_active_tgt_count)
-               stripe_count = lod->lod_desc.ld_active_tgt_count;
        if (!stripe_count)
                stripe_count = 1;
+       /* Overstriping allows more stripes than targets */
+       if (stripe_count > lod->lod_desc.ld_active_tgt_count && !overstriping)
+               stripe_count = lod->lod_desc.ld_active_tgt_count;
 
-       /* stripe count is based on whether OSD can handle larger EA sizes */
-       if (lod->lod_osd_max_easize > 0) {
-               unsigned int easize = lod->lod_osd_max_easize;
-               int i;
-
-               if (lo->ldo_is_composite) {
-                       struct lod_layout_component *lod_comp;
-                       unsigned int header_sz = sizeof(struct lov_comp_md_v1);
-
-                       header_sz += sizeof(struct lov_comp_md_entry_v1) *
-                                       lo->ldo_comp_cnt;
-                       for (i = 0; i < lo->ldo_comp_cnt; i++) {
-                               lod_comp = &lo->ldo_comp_entries[i];
-                               if (lod_comp->llc_flags & LCME_FL_INIT)
-                                       header_sz += lov_mds_md_size(
-                                               lod_comp->llc_stripe_count,
-                                               LOV_MAGIC_V3);
-                       }
-                       if (easize > header_sz)
-                               easize -= header_sz;
-                       else
-                               easize = 0;
+       if (lo->ldo_is_composite) {
+               struct lod_layout_component *lod_comp;
+               unsigned int header_sz = sizeof(struct lov_comp_md_v1);
+               unsigned int init_comp_sz = 0;
+               unsigned int total_comp_sz = 0;
+               unsigned int comp_sz;
+
+               header_sz += sizeof(struct lov_comp_md_entry_v1) *
+                               lo->ldo_comp_cnt;
+
+               for (i = 0; i < lo->ldo_comp_cnt; i++) {
+                       lod_comp = &lo->ldo_comp_entries[i];
+                       comp_sz = lov_mds_md_size(lod_comp->llc_stripe_count,
+                                                 LOV_MAGIC_V3);
+                       total_comp_sz += comp_sz;
+                       if (lod_comp->llc_flags & LCME_FL_INIT)
+                               init_comp_sz += comp_sz;
                }
 
-               max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
+               if (init_comp_sz > 0)
+                       total_comp_sz = init_comp_sz;
+
+               header_sz += total_comp_sz;
+
+               if (easize > header_sz)
+                       easize -= header_sz;
+               else
+                       easize = 0;
        }
 
+       max_stripes = lov_mds_md_max_stripe_count(easize, LOV_MAGIC_V3);
+
        return (stripe_count < max_stripes) ? stripe_count : max_stripes;
 }
 
@@ -1823,7 +1755,7 @@ int lod_use_defined_striping(const struct lu_env *env,
        magic = le32_to_cpu(v1->lmm_magic) & ~LOV_MAGIC_DEFINED;
 
        if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3 &&
-           magic != LOV_MAGIC_COMP_V1)
+           magic != LOV_MAGIC_COMP_V1 && magic != LOV_MAGIC_FOREIGN)
                GOTO(unlock, rc = -EINVAL);
 
        if (magic == LOV_MAGIC_COMP_V1) {
@@ -1835,6 +1767,32 @@ int lod_use_defined_striping(const struct lu_env *env,
                mo->ldo_flr_state = le16_to_cpu(comp_v1->lcm_flags) &
                                        LCM_FL_FLR_MASK;
                mo->ldo_is_composite = 1;
+       } else if (magic == LOV_MAGIC_FOREIGN) {
+               struct lov_foreign_md *foreign;
+               size_t length;
+
+               if (buf->lb_len < offsetof(typeof(*foreign), lfm_value)) {
+                       CDEBUG(D_LAYOUT,
+                              "buf len %zu < min lov_foreign_md size (%zu)\n",
+                              buf->lb_len,
+                              offsetof(typeof(*foreign), lfm_value));
+                       GOTO(out, rc = -EINVAL);
+               }
+               foreign = (struct lov_foreign_md *)buf->lb_buf;
+               length = foreign_size_le(foreign);
+               if (buf->lb_len < length) {
+                       CDEBUG(D_LAYOUT,
+                              "buf len %zu < this lov_foreign_md size (%zu)\n",
+                              buf->lb_len, length);
+                       GOTO(out, rc = -EINVAL);
+               }
+
+               /* just cache foreign LOV EA raw */
+               rc = lod_alloc_foreign_lov(mo, length);
+               if (rc)
+                       GOTO(out, rc);
+               memcpy(mo->ldo_foreign_lov, buf->lb_buf, length);
+               GOTO(out, rc);
        } else {
                mo->ldo_is_composite = 0;
                comp_cnt = 1;
@@ -1856,6 +1814,7 @@ int lod_use_defined_striping(const struct lu_env *env,
                if (mo->ldo_is_composite) {
                        offs = le32_to_cpu(comp_v1->lcm_entries[i].lcme_offset);
                        v1 = (struct lov_mds_md_v1 *)((char *)comp_v1 + offs);
+                       v3 = (struct lov_mds_md_v3 *)v1;
                        magic = le32_to_cpu(v1->lmm_magic);
 
                        ext = &comp_v1->lcm_entries[i].lcme_extent;
@@ -1864,6 +1823,9 @@ int lod_use_defined_striping(const struct lu_env *env,
                        lod_comp->llc_extent.e_end = le64_to_cpu(ext->e_end);
                        lod_comp->llc_flags =
                                le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags);
+                       if (lod_comp->llc_flags & LCME_FL_NOSYNC)
+                               lod_comp->llc_timestamp = le64_to_cpu(
+                                       comp_v1->lcm_entries[i].lcme_timestamp);
                        lod_comp->llc_id =
                                le32_to_cpu(comp_v1->lcm_entries[i].lcme_id);
                        if (lod_comp->llc_id == LCME_ID_INVAL)
@@ -1935,16 +1897,17 @@ int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
                         const struct lu_buf *buf)
 {
        struct lod_layout_component *lod_comp;
-       struct lod_device       *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
-       struct lov_desc         *desc = &d->lod_desc;
-       struct lov_user_md_v1   *v1 = NULL;
-       struct lov_user_md_v3   *v3 = NULL;
-       struct lov_comp_md_v1   *comp_v1 = NULL;
-       char    def_pool[LOV_MAXPOOLNAME + 1];
-       __u32   magic;
-       __u16   comp_cnt;
-       __u16   mirror_cnt;
-       int     i, rc;
+       struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
+       struct lov_desc *desc = &d->lod_desc;
+       struct lov_user_md_v1 *v1 = NULL;
+       struct lov_user_md_v3 *v3 = NULL;
+       struct lov_comp_md_v1 *comp_v1 = NULL;
+       struct lov_foreign_md *lfm = NULL;
+       char def_pool[LOV_MAXPOOLNAME + 1];
+       __u32 magic;
+       __u16 comp_cnt;
+       __u16 mirror_cnt;
+       int i, rc;
        ENTRY;
 
        if (buf == NULL || buf->lb_buf == NULL || buf->lb_len == 0)
@@ -1956,7 +1919,10 @@ int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
                                    def_pool, sizeof(def_pool));
 
        /* free default striping info */
-       lod_free_comp_entries(lo);
+       if (lo->ldo_is_foreign)
+               lod_free_foreign_lov(lo);
+       else
+               lod_free_comp_entries(lo);
 
        rc = lod_verify_striping(d, lo, buf, false);
        if (rc)
@@ -1965,6 +1931,7 @@ int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
        v3 = buf->lb_buf;
        v1 = buf->lb_buf;
        comp_v1 = buf->lb_buf;
+       /* {lmm,lfm}_magic position/length work for all LOV formats */
        magic = v1->lmm_magic;
 
        if (unlikely(le32_to_cpu(magic) & LOV_MAGIC_DEFINED)) {
@@ -2000,6 +1967,22 @@ int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
                /* fall trhough */
        case LOV_USER_MAGIC_COMP_V1:
                break;
+       case __swab32(LOV_USER_MAGIC_FOREIGN):
+               lfm = buf->lb_buf;
+               __swab32s(&lfm->lfm_magic);
+               __swab32s(&lfm->lfm_length);
+               __swab32s(&lfm->lfm_type);
+               __swab32s(&lfm->lfm_flags);
+               magic = lfm->lfm_magic;
+               /* fall through */
+       case LOV_USER_MAGIC_FOREIGN:
+               if (!lfm)
+                       lfm = buf->lb_buf;
+               rc = lod_alloc_foreign_lov(lo, foreign_size(lfm));
+               if (rc)
+                       RETURN(rc);
+               memcpy(lo->ldo_foreign_lov, buf->lb_buf, foreign_size(lfm));
+               RETURN(0);
        default:
                CERROR("%s: unrecognized magic %X\n",
                       lod2obd(d)->obd_name, magic);
@@ -2042,7 +2025,7 @@ int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
                        lod_comp->llc_extent = *ext;
                        lod_comp->llc_flags =
                                comp_v1->lcm_entries[i].lcme_flags &
-                                       LCME_USER_FLAGS;
+                                       LCME_CL_COMP_FLAGS;
                }
 
                pool_name = NULL;
@@ -2065,7 +2048,9 @@ int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
                if (v1->lmm_pattern == 0)
                        v1->lmm_pattern = LOV_PATTERN_RAID0;
                if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0 &&
-                   lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT) {
+                   lov_pattern(v1->lmm_pattern) != LOV_PATTERN_MDT &&
+                   lov_pattern(v1->lmm_pattern) !=
+                       (LOV_PATTERN_RAID0 | LOV_PATTERN_OVERSTRIPING)) {
                        CDEBUG(D_LAYOUT, "%s: invalid pattern: %x\n",
                               lod2obd(d)->obd_name, v1->lmm_pattern);
                        GOTO(free_comp, rc = -EINVAL);
@@ -2109,7 +2094,8 @@ int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
                        }
                }
 
-               if (lod_comp->llc_stripe_count > pool_tgt_count(pool))
+               if (lod_comp->llc_stripe_count > pool_tgt_count(pool) &&
+                   !(lod_comp->llc_pattern & LOV_PATTERN_OVERSTRIPING))
                        lod_comp->llc_stripe_count = pool_tgt_count(pool);
 
                lod_pool_putref(pool);
@@ -2234,11 +2220,11 @@ void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
                         */
                        for (j = 0; j < comp->llc_stripe_count; j++) {
                                struct lod_tgt_desc *ost;
-                               struct lod_qos_oss *lqo;
+                               struct lu_svr_qos *lsq;
                                int k;
 
                                ost = OST_TGT(lod, comp->llc_ost_indices[j]);
-                               lqo = ost->ltd_qos.ltq_oss;
+                               lsq = ost->ltd_qos.ltq_svr;
 
                                if (cfs_bitmap_check(bitmap, ost->ltd_index))
                                        continue;
@@ -2250,12 +2236,12 @@ void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
 
                                for (k = 0; k < lag->lag_oaa_count; k++) {
                                        if (lag->lag_oss_avoid_array[k] ==
-                                           lqo->lqo_id)
+                                           lsq->lsq_id)
                                                break;
                                }
                                if (k == lag->lag_oaa_count) {
                                        lag->lag_oss_avoid_array[k] =
-                                                               lqo->lqo_id;
+                                                               lsq->lsq_id;
                                        lag->lag_oaa_count++;
                                }
                        }
@@ -2298,6 +2284,7 @@ int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
        LASSERT(lo);
        LASSERT(lo->ldo_comp_cnt > comp_idx && lo->ldo_comp_entries != NULL);
        lod_comp = &lo->ldo_comp_entries[comp_idx];
+       LASSERT(!(lod_comp->llc_flags & LCME_FL_EXTENSION));
 
        /* A released component is being created */
        if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
@@ -2318,7 +2305,10 @@ int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
                 */
                lod_qos_statfs_update(env, d);
                stripe_len = lod_get_stripe_count(d, lo,
-                                                 lod_comp->llc_stripe_count);
+                                                 lod_comp->llc_stripe_count,
+                                                 lod_comp->llc_pattern &
+                                                 LOV_PATTERN_OVERSTRIPING);
+
                if (stripe_len == 0)
                        GOTO(out, rc = -ERANGE);
                lod_comp->llc_stripe_count = stripe_len;