Whamcloud - gitweb
LU-4788 lfsck: replace cfs_list_t with list_head
[fs/lustre-release.git] / lustre / lod / lod_qos.c
index e5b4565..a0c5b03 100644 (file)
@@ -23,7 +23,7 @@
  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2012, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -35,9 +35,9 @@
 
 #define DEBUG_SUBSYSTEM S_LOV
 
+#include <asm/div64.h>
 #include <libcfs/libcfs.h>
 #include <obd_class.h>
-#include <obd_lov.h>
 #include <lustre/lustre_idl.h>
 #include "lod_internal.h"
 
 #define TGT_BAVAIL(i) (OST_TGT(lod,i)->ltd_statfs.os_bavail * \
                       OST_TGT(lod,i)->ltd_statfs.os_bsize)
 
-int qos_add_tgt(struct lod_device *lod, struct lod_ost_desc *ost_desc)
+int qos_add_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
 {
-       struct lov_qos_oss *oss = NULL, *temposs;
+       struct lod_qos_oss *oss = NULL, *temposs;
        struct obd_export  *exp = ost_desc->ltd_exp;
        int                 rc = 0, found = 0;
-       cfs_list_t         *list;
+       struct list_head   *list;
        ENTRY;
 
-       cfs_down_write(&lod->lod_qos.lq_rw_sem);
+       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.
         */
-       cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
+       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++;
@@ -89,7 +89,7 @@ int qos_add_tgt(struct lod_device *lod, struct lod_ost_desc *ost_desc)
                       sizeof(oss->lqo_uuid));
        } else {
                /* Assume we have to move this one */
-               cfs_list_del(&oss->lqo_oss_list);
+               list_del(&oss->lqo_oss_list);
        }
 
        oss->lqo_ost_count++;
@@ -102,29 +102,29 @@ int qos_add_tgt(struct lod_device *lod, struct lod_ost_desc *ost_desc)
        /* Add sorted by # of OSTs.  Find the first entry that we're
           bigger than... */
        list = &lod->lod_qos.lq_oss_list;
-       cfs_list_for_each_entry(temposs, list, lqo_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. */
-       cfs_list_add_tail(&oss->lqo_oss_list, &temposs->lqo_oss_list);
+       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:
-       cfs_up_write(&lod->lod_qos.lq_rw_sem);
+       up_write(&lod->lod_qos.lq_rw_sem);
        RETURN(rc);
 }
 
-int qos_del_tgt(struct lod_device *lod, struct lod_ost_desc *ost_desc)
+int qos_del_tgt(struct lod_device *lod, struct lod_tgt_desc *ost_desc)
 {
-       struct lov_qos_oss *oss;
+       struct lod_qos_oss *oss;
        int                 rc = 0;
        ENTRY;
 
-       cfs_down_write(&lod->lod_qos.lq_rw_sem);
+       down_write(&lod->lod_qos.lq_rw_sem);
        oss = ost_desc->ltd_qos.ltq_oss;
        if (!oss)
                GOTO(out, rc = -ENOENT);
@@ -133,7 +133,7 @@ int qos_del_tgt(struct lod_device *lod, struct lod_ost_desc *ost_desc)
        if (oss->lqo_ost_count == 0) {
                CDEBUG(D_QOS, "removing OSS %s\n",
                       obd_uuid2str(&oss->lqo_uuid));
-               cfs_list_del(&oss->lqo_oss_list);
+               list_del(&oss->lqo_oss_list);
                ost_desc->ltd_qos.ltq_oss = NULL;
                OBD_FREE_PTR(oss);
        }
@@ -141,14 +141,14 @@ int qos_del_tgt(struct lod_device *lod, struct lod_ost_desc *ost_desc)
        lod->lod_qos.lq_dirty = 1;
        lod->lod_qos.lq_rr.lqr_dirty = 1;
 out:
-       cfs_up_write(&lod->lod_qos.lq_rw_sem);
+       up_write(&lod->lod_qos.lq_rw_sem);
        RETURN(rc);
 }
 
 static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
                                int index, struct obd_statfs *sfs)
 {
-       struct lod_ost_desc *ost;
+       struct lod_tgt_desc *ost;
        int                  rc;
 
        LASSERT(d);
@@ -159,10 +159,14 @@ static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
        if (rc && rc != -ENOTCONN)
                CERROR("%s: statfs: rc = %d\n", lod2obd(d)->obd_name, rc);
 
+       /* If the OST is readonly then we can't allocate objects there */
+       if (sfs->os_state & OS_STATE_READONLY)
+               rc = -EROFS;
+
        /* check whether device has changed state (active, inactive) */
        if (rc != 0 && ost->ltd_active) {
                /* turned inactive? */
-               cfs_spin_lock(&d->lod_desc_lock);
+               spin_lock(&d->lod_desc_lock);
                if (ost->ltd_active) {
                        ost->ltd_active = 0;
                        LASSERT(d->lod_desc.ld_active_tgt_count > 0);
@@ -172,11 +176,13 @@ static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
                        CDEBUG(D_CONFIG, "%s: turns inactive\n",
                               ost->ltd_exp->exp_obd->obd_name);
                }
-               cfs_spin_unlock(&d->lod_desc_lock);
+               spin_unlock(&d->lod_desc_lock);
        } else if (rc == 0 && ost->ltd_active == 0) {
                /* turned active? */
-               LASSERT(d->lod_desc.ld_active_tgt_count < d->lod_ostnr);
-               cfs_spin_lock(&d->lod_desc_lock);
+               LASSERTF(d->lod_desc.ld_active_tgt_count < d->lod_ostnr,
+                        "active tgt count %d, ost nr %d\n",
+                        d->lod_desc.ld_active_tgt_count, d->lod_ostnr);
+               spin_lock(&d->lod_desc_lock);
                if (ost->ltd_active == 0) {
                        ost->ltd_active = 1;
                        d->lod_desc.ld_active_tgt_count++;
@@ -185,10 +191,10 @@ static int lod_statfs_and_check(const struct lu_env *env, struct lod_device *d,
                        CDEBUG(D_CONFIG, "%s: turns active\n",
                               ost->ltd_exp->exp_obd->obd_name);
                }
-               cfs_spin_unlock(&d->lod_desc_lock);
+               spin_unlock(&d->lod_desc_lock);
        }
 
-       return rc;
+       RETURN(rc);
 }
 
 static void lod_qos_statfs_update(const struct lu_env *env,
@@ -196,7 +202,8 @@ static void lod_qos_statfs_update(const struct lu_env *env,
 {
        struct obd_device *obd = lod2obd(lod);
        struct ost_pool   *osts = &(lod->lod_pool_info);
-       int                i, idx, rc = 0;
+       unsigned int       i;
+       int                idx, rc = 0;
        __u64              max_age, avail;
        ENTRY;
 
@@ -206,7 +213,7 @@ static void lod_qos_statfs_update(const struct lu_env *env,
                /* statfs data are quite recent, don't need to refresh it */
                RETURN_EXIT;
 
-       cfs_down_write(&lod->lod_qos.lq_rw_sem);
+       down_write(&lod->lod_qos.lq_rw_sem);
        if (cfs_time_beforeq_64(max_age, obd->obd_osfs_age))
                GOTO(out, rc = 0);
 
@@ -224,17 +231,19 @@ static void lod_qos_statfs_update(const struct lu_env *env,
        obd->obd_osfs_age = cfs_time_current_64();
 
 out:
-       cfs_up_write(&lod->lod_qos.lq_rw_sem);
+       up_write(&lod->lod_qos.lq_rw_sem);
+       EXIT;
 }
 
 /* Recalculate per-object penalties for OSSs and OSTs,
    depends on size of each ost in an oss */
 static int lod_qos_calc_ppo(struct lod_device *lod)
 {
-       struct lov_qos_oss *oss;
+       struct lod_qos_oss *oss;
        __u64               ba_max, ba_min, temp;
        __u32               num_active;
-       int                 rc, i, prio_wide;
+       unsigned int        i;
+       int                 rc, prio_wide;
        time_t              now, age;
        ENTRY;
 
@@ -246,7 +255,7 @@ static int lod_qos_calc_ppo(struct lod_device *lod)
                GOTO(out, rc = -EAGAIN);
 
        /* find bavail on each OSS */
-       cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list)
+       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;
 
@@ -277,7 +286,7 @@ static int lod_qos_calc_ppo(struct lod_device *lod)
 
                /* per-OST penalty is prio * TGT_bavail / (num_ost - 1) / 2 */
                temp >>= 1;
-               lov_do_div64(temp, num_active);
+               do_div(temp, num_active);
                OST_TGT(lod,i)->ltd_qos.ltq_penalty_per_obj =
                        (temp * prio_wide) >> 8;
 
@@ -305,9 +314,9 @@ static int lod_qos_calc_ppo(struct lod_device *lod)
        }
 
        /* Per-OSS penalty is prio * oss_avail / oss_osts / (num_oss - 1) / 2 */
-       cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
+       list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
                temp = oss->lqo_bavail >> 1;
-               lov_do_div64(temp, oss->lqo_ost_count * num_active);
+               do_div(temp, oss->lqo_ost_count * num_active);
                oss->lqo_penalty_per_obj = (temp * prio_wide) >> 8;
 
                age = (now - oss->lqo_used) >> 3;
@@ -363,9 +372,9 @@ static int lod_qos_calc_weight(struct lod_device *lod, int i)
 static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
                        __u32 index, __u64 *total_wt)
 {
-       struct lod_ost_desc *ost;
-       struct lov_qos_oss  *oss;
-       int j;
+       struct lod_tgt_desc *ost;
+       struct lod_qos_oss  *oss;
+       unsigned int j;
        ENTRY;
 
        ost = OST_TGT(lod,index);
@@ -391,7 +400,7 @@ static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
                lod->lod_qos.lq_active_oss_count;
 
        /* Decrease all OSS penalties */
-       cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
+       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;
                else
@@ -440,12 +449,13 @@ static int lod_qos_used(struct lod_device *lod, struct ost_pool *osts,
 #define LOV_QOS_EMPTY ((__u32)-1)
 /* compute optimal round-robin order, based on OSTs per OSS */
 static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
-                          struct lov_qos_rr *lqr)
+                          struct lod_qos_rr *lqr)
 {
-       struct lov_qos_oss  *oss;
-       struct lod_ost_desc *ost;
+       struct lod_qos_oss  *oss;
+       struct lod_tgt_desc *ost;
        unsigned placed, real_count;
-       int i, rc;
+       unsigned int i;
+       int rc;
        ENTRY;
 
        if (!lqr->lqr_dirty) {
@@ -454,7 +464,7 @@ static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
        }
 
        /* Do actual allocation. */
-       cfs_down_write(&lod->lod_qos.lq_rw_sem);
+       down_write(&lod->lod_qos.lq_rw_sem);
 
        /*
         * Check again. While we were sleeping on @lq_rw_sem something could
@@ -462,7 +472,7 @@ static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
         */
        if (!lqr->lqr_dirty) {
                LASSERT(lqr->lqr_pool.op_size);
-               cfs_up_write(&lod->lod_qos.lq_rw_sem);
+               up_write(&lod->lod_qos.lq_rw_sem);
                RETURN(0);
        }
 
@@ -475,7 +485,7 @@ static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
        lqr->lqr_pool.op_count = real_count;
        rc = lod_ost_pool_extend(&lqr->lqr_pool, real_count);
        if (rc) {
-               cfs_up_write(&lod->lod_qos.lq_rw_sem);
+               up_write(&lod->lod_qos.lq_rw_sem);
                RETURN(rc);
        }
        for (i = 0; i < lqr->lqr_pool.op_count; i++)
@@ -483,7 +493,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;
-       cfs_list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
+       list_for_each_entry(oss, &lod->lod_qos.lq_oss_list, lqo_oss_list) {
                int j = 0;
 
                for (i = 0; i < lqr->lqr_pool.op_count; i++) {
@@ -510,7 +520,7 @@ static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
        }
 
        lqr->lqr_dirty = 0;
-       cfs_up_write(&lod->lod_qos.lq_rw_sem);
+       up_write(&lod->lod_qos.lq_rw_sem);
 
        if (placed != real_count) {
                /* This should never happen */
@@ -546,10 +556,10 @@ static int lod_qos_calc_rr(struct lod_device *lod, struct ost_pool *src_pool,
  */
 static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
                                                   struct lod_device *d,
-                                                  int ost_idx,
+                                                  __u32 ost_idx,
                                                   struct thandle *th)
 {
-       struct lod_ost_desc *ost;
+       struct lod_tgt_desc *ost;
        struct lu_object *o, *n;
        struct lu_device *nd;
        struct dt_object *dt;
@@ -557,7 +567,6 @@ static struct dt_object *lod_qos_declare_object_on(const struct lu_env *env,
        ENTRY;
 
        LASSERT(d);
-       LASSERT(ost_idx >= 0);
        LASSERT(ost_idx < d->lod_osts_size);
        ost = OST_TGT(d,ost_idx);
        LASSERT(ost);
@@ -595,7 +604,7 @@ out:
        RETURN(dt);
 }
 
-static int min_stripe_count(int stripe_cnt, int flags)
+static int min_stripe_count(__u32 stripe_cnt, int flags)
 {
        return (flags & LOV_USES_DEFAULT_STRIPE ?
                        stripe_cnt - (stripe_cnt / 4) : stripe_cnt);
@@ -613,13 +622,12 @@ static int inline lod_qos_dev_is_full(struct obd_statfs *msfs)
 
        /* the minimum of 0.1% used blocks and 1GB bytes. */
        used = min_t(__u64, (msfs->os_blocks - msfs->os_bfree) >> 10,
-                       1 << (31 - cfs_ffs(bs)));
+                       1 << (31 - ffs(bs)));
        return (msfs->os_bavail < used);
 }
 
-int lod_ea_store_resize(struct lod_thread_info *info, int size);
-
-static inline int lod_qos_ost_in_use_clear(const struct lu_env *env, int stripes)
+static inline int lod_qos_ost_in_use_clear(const struct lu_env *env,
+                                          __u32 stripes)
 {
        struct lod_thread_info *info = lod_env_info(env);
 
@@ -629,7 +637,7 @@ static inline int lod_qos_ost_in_use_clear(const struct lu_env *env, int stripes
                CERROR("can't allocate memory for ost-in-use array\n");
                return -ENOMEM;
        }
-       memset(info->lti_ea_store, 0, sizeof(int) * stripes);
+       memset(info->lti_ea_store, -1, sizeof(int) * stripes);
        return 0;
 }
 
@@ -642,11 +650,11 @@ static inline void lod_qos_ost_in_use(const struct lu_env *env, int idx, int ost
        osts[idx] = ost;
 }
 
-static int lod_qos_is_ost_used(const struct lu_env *env, int ost, int stripes)
+static int lod_qos_is_ost_used(const struct lu_env *env, int ost, __u32 stripes)
 {
        struct lod_thread_info *info = lod_env_info(env);
        int *osts = info->lti_ea_store;
-       int j;
+       __u32 j;
 
        for (j = 0; j < stripes; j++) {
                if (osts[j] == ost)
@@ -657,21 +665,22 @@ static int lod_qos_is_ost_used(const struct lu_env *env, int ost, int stripes)
 
 /* Allocate objects on OSTs with round-robin algorithm */
 static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
-                       int flags, struct thandle *th)
+                       struct dt_object **stripe, int flags,
+                       struct thandle *th)
 {
        struct lod_device *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
        struct obd_statfs *sfs = &lod_env_info(env)->lti_osfs;
        struct pool_desc  *pool = NULL;
        struct ost_pool   *osts;
-       struct lov_qos_rr *lqr;
+       struct lod_qos_rr *lqr;
        struct dt_object  *o;
-       unsigned           array_idx;
-       int                i, rc;
-       int                ost_start_idx_temp;
+       unsigned int       i, array_idx;
+       int                rc;
+       __u32              ost_start_idx_temp;
        int                speed = 0;
-       int                stripe_idx = 0;
-       int                stripe_cnt = lo->ldo_stripenr;
-       int                stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
+       __u32              stripe_idx = 0;
+       __u32              stripe_cnt = lo->ldo_stripenr;
+       __u32              stripe_cnt_min = min_stripe_count(stripe_cnt, flags);
        __u32              ost_idx;
        ENTRY;
 
@@ -679,7 +688,7 @@ static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
                pool = lod_find_pool(m, lo->ldo_pool);
 
        if (pool != NULL) {
-               cfs_down_read(&pool_tgt_rw_sem(pool));
+               down_read(&pool_tgt_rw_sem(pool));
                osts = &(pool->pool_obds);
                lqr = &(pool->pool_rr);
        } else {
@@ -709,7 +718,7 @@ static int lod_alloc_rr(const struct lu_env *env, struct lod_object *lo,
                if (stripe_cnt > 1 && (osts->op_count % stripe_cnt) != 1)
                        ++lqr->lqr_offset_idx;
        }
-       cfs_down_read(&m->lod_qos.lq_rw_sem);
+       down_read(&m->lod_qos.lq_rw_sem);
        ost_start_idx_temp = lqr->lqr_start_idx;
 
 repeat_find:
@@ -767,7 +776,7 @@ repeat_find:
                /*
                 * try to use another OSP if this one is degraded
                 */
-               if (sfs->os_state == OS_STATE_DEGRADED && speed < 2) {
+               if (sfs->os_state & OS_STATE_DEGRADED && speed < 2) {
                        QOS_DEBUG("#%d: degraded\n", ost_idx);
                        continue;
                }
@@ -790,7 +799,7 @@ repeat_find:
                 * We've successfuly declared (reserved) an object
                 */
                lod_qos_ost_in_use(env, stripe_idx, ost_idx);
-               lo->ldo_stripe[stripe_idx] = o;
+               stripe[stripe_idx] = o;
                stripe_idx++;
 
        }
@@ -801,7 +810,7 @@ repeat_find:
                goto repeat_find;
        }
 
-       cfs_up_read(&m->lod_qos.lq_rw_sem);
+       up_read(&m->lod_qos.lq_rw_sem);
 
        if (stripe_idx) {
                lo->ldo_stripenr = stripe_idx;
@@ -814,7 +823,7 @@ repeat_find:
 
 out:
        if (pool != NULL) {
-               cfs_up_read(&pool_tgt_rw_sem(pool));
+               up_read(&pool_tgt_rw_sem(pool));
                /* put back ref got by lod_find_pool() */
                lod_pool_putref(pool);
        }
@@ -824,13 +833,15 @@ out:
 
 /* alloc objects on osts with specific stripe offset */
 static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
-                             int flags, struct thandle *th)
+                             struct dt_object **stripe, int flags,
+                             struct thandle *th)
 {
        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;
-       unsigned           ost_idx, array_idx, ost_count;
-       int                i, rc, stripe_num = 0;
+       __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;
@@ -844,7 +855,7 @@ static int lod_alloc_specific(const struct lu_env *env, struct lod_object *lo,
                pool = lod_find_pool(m, lo->ldo_pool);
 
        if (pool != NULL) {
-               cfs_down_read(&pool_tgt_rw_sem(pool));
+               down_read(&pool_tgt_rw_sem(pool));
                osts = &(pool->pool_obds);
        } else {
                osts = &(m->lod_pool_info);
@@ -916,7 +927,8 @@ repeat_find:
                /*
                 * We've successfuly declared (reserved) an object
                 */
-               lo->ldo_stripe[stripe_num] = o;
+               lod_qos_ost_in_use(env, stripe_num, ost_idx);
+               stripe[stripe_num] = o;
                stripe_num++;
 
                /* We have enough stripes */
@@ -941,7 +953,7 @@ repeat_find:
        rc = -EFBIG;
 out:
        if (pool != NULL) {
-               cfs_up_read(&pool_tgt_rw_sem(pool));
+               up_read(&pool_tgt_rw_sem(pool));
                /* put back ref got by lod_find_pool() */
                lod_pool_putref(pool);
        }
@@ -971,16 +983,19 @@ static inline int lod_qos_is_usable(struct lod_device *lod)
    - network resources (shared OSS's)
  */
 static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
-                        int flags, struct thandle *th)
+                        struct dt_object **stripe, int flags,
+                        struct thandle *th)
 {
        struct lod_device   *m = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
        struct obd_statfs   *sfs = &lod_env_info(env)->lti_osfs;
-       struct lod_ost_desc *ost;
+       struct lod_tgt_desc *ost;
        struct dt_object    *o;
        __u64                total_weight = 0;
-       int                  nfound, good_osts, i, rc = 0;
-       int                  stripe_cnt = lo->ldo_stripenr;
-       int                  stripe_cnt_min;
+       unsigned int         i;
+       int                  rc = 0;
+       __u32                nfound, good_osts;
+       __u32                stripe_cnt = lo->ldo_stripenr;
+       __u32                stripe_cnt_min;
        struct pool_desc    *pool = NULL;
        struct ost_pool    *osts;
        ENTRY;
@@ -993,7 +1008,7 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
                pool = lod_find_pool(m, lo->ldo_pool);
 
        if (pool != NULL) {
-               cfs_down_read(&pool_tgt_rw_sem(pool));
+               down_read(&pool_tgt_rw_sem(pool));
                osts = &(pool->pool_obds);
        } else {
                osts = &(m->lod_pool_info);
@@ -1004,7 +1019,7 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
                GOTO(out_nolock, rc = -EAGAIN);
 
        /* Do actual allocation, use write lock here. */
-       cfs_down_write(&m->lod_qos.lq_rw_sem);
+       down_write(&m->lod_qos.lq_rw_sem);
 
        /*
         * Check again, while we were sleeping on @lq_rw_sem things could
@@ -1098,7 +1113,7 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
                /* 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 < osts->op_count; i++) {
-                       int idx = osts->op_array[i];
+                       __u32 idx = osts->op_array[i];
 
                        if (!cfs_bitmap_check(m->lod_ost_bitmap, idx))
                                continue;
@@ -1132,7 +1147,7 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
                                          idx, (int) PTR_ERR(o));
                                continue;
                        }
-                       lo->ldo_stripe[nfound++] = o;
+                       stripe[nfound++] = o;
                        lod_qos_used(m, osts, idx, &total_weight);
                        rc = 0;
                        break;
@@ -1154,9 +1169,9 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
                 */
                LCONSOLE_INFO("wanted %d, found %d\n", stripe_cnt, nfound);
                for (i = 0; i < nfound; i++) {
-                       LASSERT(lo->ldo_stripe[i]);
-                       lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
-                       lo->ldo_stripe[i] = NULL;
+                       LASSERT(stripe[i] != NULL);
+                       lu_object_put(env, &stripe[i]->do_lu);
+                       stripe[i] = NULL;
                }
 
                /* makes sense to rebalance next time */
@@ -1167,11 +1182,11 @@ static int lod_alloc_qos(const struct lu_env *env, struct lod_object *lo,
        }
 
 out:
-       cfs_up_write(&m->lod_qos.lq_rw_sem);
+       up_write(&m->lod_qos.lq_rw_sem);
 
 out_nolock:
        if (pool != NULL) {
-               cfs_up_read(&pool_tgt_rw_sem(pool));
+               up_read(&pool_tgt_rw_sem(pool));
                /* put back ref got by lod_find_pool() */
                lod_pool_putref(pool);
        }
@@ -1194,8 +1209,8 @@ static __u16 lod_get_stripecnt(struct lod_device *lod, __u32 magic,
 
        /* stripe count is based on whether OSD can handle larger EA sizes */
        if (lod->lod_osd_max_easize > 0)
-               max_stripes = lov_mds_md_stripecnt(lod->lod_osd_max_easize,
-                                                  magic);
+               max_stripes = lov_mds_md_max_stripe_count(
+                       lod->lod_osd_max_easize, magic);
 
        return (stripe_count < max_stripes) ? stripe_count : max_stripes;
 }
@@ -1204,41 +1219,40 @@ static int lod_use_defined_striping(const struct lu_env *env,
                                    struct lod_object *mo,
                                    const struct lu_buf *buf)
 {
-       struct lod_device      *d = lu2lod_dev(lod2lu_obj(mo)->lo_dev);
        struct lov_mds_md_v1   *v1 = buf->lb_buf;
        struct lov_mds_md_v3   *v3 = buf->lb_buf;
        struct lov_ost_data_v1 *objs;
        __u32                   magic;
-       int                     rc;
+       int                     rc = 0;
        ENTRY;
 
-       rc = lod_verify_striping(d, buf, 1);
-       if (rc)
-               RETURN(rc);
-
        magic = le32_to_cpu(v1->lmm_magic);
        if (magic == LOV_MAGIC_V1_DEF) {
+               magic = LOV_MAGIC_V1;
                objs = &v1->lmm_objects[0];
        } else if (magic == LOV_MAGIC_V3_DEF) {
+               magic = LOV_MAGIC_V3;
                objs = &v3->lmm_objects[0];
                lod_object_set_pool(mo, v3->lmm_pool_name);
        } else {
                GOTO(out, rc = -EINVAL);
        }
 
-       /*
-        * LOD shouldn't be aware of recovery at all,
-        * but this track recovery status (to some extent)
-        * to be do additional checks like this one
-        */
-       LASSERT(d->lod_recovery_completed == 0);
-
+       mo->ldo_pattern = le32_to_cpu(v1->lmm_pattern);
        mo->ldo_stripe_size = le32_to_cpu(v1->lmm_stripe_size);
        mo->ldo_stripenr = le16_to_cpu(v1->lmm_stripe_count);
        mo->ldo_layout_gen = le16_to_cpu(v1->lmm_layout_gen);
+
+       /* fixup for released file before object initialization */
+       if (mo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
+               mo->ldo_released_stripenr = mo->ldo_stripenr;
+               mo->ldo_stripenr = 0;
+       }
+
        LASSERT(buf->lb_len >= lov_mds_md_size(mo->ldo_stripenr, magic));
 
-       rc = lod_initialize_objects(env, mo, objs);
+       if (mo->ldo_stripenr > 0)
+               rc = lod_initialize_objects(env, mo, objs);
 
 out:
        RETURN(rc);
@@ -1262,10 +1276,14 @@ static int lod_qos_parse_config(const struct lu_env *env,
        v1 = buf->lb_buf;
        magic = v1->lmm_magic;
 
-       if (magic == __swab32(LOV_USER_MAGIC_V1))
+       if (magic == __swab32(LOV_USER_MAGIC_V1)) {
                lustre_swab_lov_user_md_v1(v1);
-       else if (magic == __swab32(LOV_USER_MAGIC_V3))
+               magic = v1->lmm_magic;
+       } else if (magic == __swab32(LOV_USER_MAGIC_V3)) {
+               v3 = buf->lb_buf;
                lustre_swab_lov_user_md_v3(v3);
+               magic = v3->lmm_magic;
+       }
 
        if (unlikely(magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)) {
                /* try to use as fully defined striping */
@@ -1278,10 +1296,14 @@ static int lod_qos_parse_config(const struct lu_env *env,
                RETURN(-EINVAL);
        }
 
-       if (v1->lmm_pattern != 0 && v1->lmm_pattern != LOV_PATTERN_RAID0) {
+       v1->lmm_magic = magic;
+       if (v1->lmm_pattern == 0)
+               v1->lmm_pattern = LOV_PATTERN_RAID0;
+       if (lov_pattern(v1->lmm_pattern) != LOV_PATTERN_RAID0) {
                CERROR("invalid pattern: %x\n", v1->lmm_pattern);
                RETURN(-EINVAL);
        }
+       lo->ldo_pattern = v1->lmm_pattern;
 
        if (v1->lmm_stripe_size)
                lo->ldo_stripe_size = v1->lmm_stripe_size;
@@ -1311,6 +1333,9 @@ static int lod_qos_parse_config(const struct lu_env *env,
                v3 = buf->lb_buf;
                lod_object_set_pool(lo, v3->lmm_pool_name);
 
+               /* In the function below, .hs_keycmp resolves to
+                * pool_hashkey_keycmp() */
+               /* coverity[overrun-buffer-val] */
                pool = lod_find_pool(d, v3->lmm_pool_name);
                if (pool != NULL) {
                        if (lo->ldo_def_stripe_offset !=
@@ -1332,6 +1357,12 @@ static int lod_qos_parse_config(const struct lu_env *env,
        } else
                lod_object_set_pool(lo, NULL);
 
+       /* fixup for released file */
+       if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED) {
+               lo->ldo_released_stripenr = lo->ldo_stripenr;
+               lo->ldo_stripenr = 0;
+       }
+
        RETURN(0);
 }
 
@@ -1342,9 +1373,11 @@ int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
                        struct lu_attr *attr, const struct lu_buf *buf,
                        struct thandle *th)
 {
-       struct lod_device *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
-       int flag = LOV_USES_ASSIGNED_STRIPE;
-       int i, rc = 0;
+       struct lod_device      *d = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
+       struct dt_object      **stripe;
+       int                     stripe_len;
+       int                     flag = LOV_USES_ASSIGNED_STRIPE;
+       int                     i, rc;
        ENTRY;
 
        LASSERT(lo);
@@ -1367,29 +1400,52 @@ int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
        if (rc)
                GOTO(out, rc);
 
+       /* A released file is being created */
+       if (lo->ldo_stripenr == 0)
+               GOTO(out, rc = 0);
+
        if (likely(lo->ldo_stripe == NULL)) {
                /*
                 * no striping has been created so far
                 */
                LASSERT(lo->ldo_stripenr > 0);
+               /*
+                * statfs and check OST targets now, since ld_active_tgt_count
+                * could be changed if some OSTs are [de]activated manually.
+                */
+               lod_qos_statfs_update(env, d);
                lo->ldo_stripenr = lod_get_stripecnt(d, LOV_MAGIC,
                                lo->ldo_stripenr);
-               i = sizeof(struct dt_object *) * lo->ldo_stripenr;
-               OBD_ALLOC(lo->ldo_stripe, i);
-               if (lo->ldo_stripe == NULL)
+
+               stripe_len = lo->ldo_stripenr;
+               OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
+               if (stripe == NULL)
                        GOTO(out, rc = -ENOMEM);
-               lo->ldo_stripes_allocated = lo->ldo_stripenr;
 
-               lod_getref(d);
+               lod_getref(&d->lod_ost_descs);
                /* XXX: support for non-0 files w/o objects */
+               CDEBUG(D_OTHER, "tgt_count %d stripenr %d\n",
+                               d->lod_desc.ld_tgt_count, stripe_len);
                if (lo->ldo_def_stripe_offset >= d->lod_desc.ld_tgt_count) {
-                       lod_qos_statfs_update(env, d);
-                       rc = lod_alloc_qos(env, lo, flag, th);
+                       rc = lod_alloc_qos(env, lo, stripe, flag, th);
                        if (rc == -EAGAIN)
-                               rc = lod_alloc_rr(env, lo, flag, th);
-               } else
-                       rc = lod_alloc_specific(env, lo, flag, th);
-               lod_putref(d);
+                               rc = lod_alloc_rr(env, lo, stripe, flag, th);
+               } else {
+                       rc = lod_alloc_specific(env, lo, stripe, flag, th);
+               }
+               lod_putref(d, &d->lod_ost_descs);
+
+               if (rc < 0) {
+                       for (i = 0; i < stripe_len; i++)
+                               if (stripe[i] != NULL)
+                                       lu_object_put(env, &stripe[i]->do_lu);
+
+                       OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
+                       lo->ldo_stripenr = 0;
+               } else {
+                       lo->ldo_stripe = stripe;
+                       lo->ldo_stripes_allocated = stripe_len;
+               }
        } else {
                /*
                 * lod_qos_parse_config() found supplied buf as a predefined