+ * prepare enough OST avoidance bitmap space
+ */
+int lod_prepare_avoidance(const struct lu_env *env, struct lod_object *lo)
+{
+ struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
+ struct lod_tgt_descs *ltds = &lod->lod_ost_descs;
+ struct lod_avoid_guide *lag = &lod_env_info(env)->lti_avoid;
+ struct cfs_bitmap *bitmap = NULL;
+ __u32 *new_oss = NULL;
+
+ lag->lag_ost_avail = ltds->ltd_tgtnr;
+
+ /* reset OSS avoid guide array */
+ lag->lag_oaa_count = 0;
+ if (lag->lag_oss_avoid_array && lag->lag_oaa_size < ltds->ltd_tgtnr) {
+ OBD_FREE(lag->lag_oss_avoid_array,
+ sizeof(__u32) * lag->lag_oaa_size);
+ lag->lag_oss_avoid_array = NULL;
+ lag->lag_oaa_size = 0;
+ }
+
+ /* init OST avoid guide bitmap */
+ if (lag->lag_ost_avoid_bitmap) {
+ if (ltds->ltd_tgtnr <= lag->lag_ost_avoid_bitmap->size) {
+ CFS_RESET_BITMAP(lag->lag_ost_avoid_bitmap);
+ } else {
+ CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
+ lag->lag_ost_avoid_bitmap = NULL;
+ }
+ }
+
+ if (!lag->lag_ost_avoid_bitmap) {
+ bitmap = CFS_ALLOCATE_BITMAP(ltds->ltd_tgtnr);
+ if (!bitmap)
+ return -ENOMEM;
+ }
+
+ if (!lag->lag_oss_avoid_array) {
+ /**
+ * usually there are multiple OSTs in one OSS, but we don't
+ * know the exact OSS number, so we choose a safe option,
+ * using OST count to allocate the array to store the OSS
+ * id.
+ */
+ OBD_ALLOC(new_oss, sizeof(*new_oss) * ltds->ltd_tgtnr);
+ if (!new_oss) {
+ CFS_FREE_BITMAP(bitmap);
+ return -ENOMEM;
+ }
+ }
+
+ if (new_oss) {
+ lag->lag_oss_avoid_array = new_oss;
+ lag->lag_oaa_size = ltds->ltd_tgtnr;
+ }
+ if (bitmap)
+ lag->lag_ost_avoid_bitmap = bitmap;
+
+ return 0;
+}
+
+/**
+ * Collect information of used OSTs and OSSs in the overlapped components
+ * of other mirrors
+ */
+void lod_collect_avoidance(struct lod_object *lo, struct lod_avoid_guide *lag,
+ int comp_idx)
+{
+ struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
+ struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
+ struct cfs_bitmap *bitmap = lag->lag_ost_avoid_bitmap;
+ int i, j;
+
+ /* iterate mirrors */
+ for (i = 0; i < lo->ldo_mirror_count; i++) {
+ struct lod_layout_component *comp;
+
+ /**
+ * skip mirror containing component[comp_idx], we only
+ * collect OSTs info of conflicting component in other mirrors,
+ * so that during read, if OSTs of a mirror's component are
+ * not available, we still have other mirror with different
+ * OSTs to read the data.
+ */
+ comp = &lo->ldo_comp_entries[lo->ldo_mirrors[i].lme_start];
+ if (comp->llc_id == LCME_ID_INVAL ||
+ mirror_id_of(comp->llc_id) ==
+ mirror_id_of(lod_comp->llc_id))
+ continue;
+
+ /* iterate components of a mirror */
+ lod_foreach_mirror_comp(comp, lo, i) {
+ /* skip non-overlapped or un-instantiated components */
+ if (!lu_extent_is_overlapped(&comp->llc_extent,
+ &lod_comp->llc_extent) ||
+ !lod_comp_inited(comp) || !comp->llc_stripe)
+ continue;
+
+ /**
+ * collect used OSTs index and OSS info from a
+ * component
+ */
+ for (j = 0; j < comp->llc_stripe_count; j++) {
+ struct lod_tgt_desc *ost;
+ struct lod_qos_oss *lqo;
+ int k;
+
+ ost = OST_TGT(lod, comp->llc_ost_indices[j]);
+ lqo = ost->ltd_qos.ltq_oss;
+
+ if (cfs_bitmap_check(bitmap, ost->ltd_index))
+ continue;
+
+ cfs_bitmap_set(bitmap, ost->ltd_index);
+ lag->lag_ost_avail--;
+
+ for (k = 0; k < lag->lag_oaa_count; k++) {
+ if (lag->lag_oss_avoid_array[k] ==
+ lqo->lqo_id)
+ break;
+ }
+ if (k == lag->lag_oaa_count) {
+ lag->lag_oss_avoid_array[k] =
+ lqo->lqo_id;
+ lag->lag_oaa_count++;
+ }
+ }
+ }
+ }
+}
+
+/**