+void lod_spill_target_refresh(const struct lu_env *env, struct lod_device *lod,
+ struct pool_desc *pool)
+{
+ __u64 avail_bytes = 0, total_bytes = 0;
+ struct lu_tgt_pool *osts;
+ int i;
+
+ if (ktime_get_seconds() < pool->pool_spill_expire)
+ return;
+
+ if (pool->pool_spill_threshold_pct == 0)
+ return;
+
+ lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
+
+ down_write(&pool_tgt_rw_sem(pool));
+ if (ktime_get_seconds() < pool->pool_spill_expire)
+ goto out_sem;
+ pool->pool_spill_expire = ktime_get_seconds() +
+ lod->lod_ost_descs.ltd_lov_desc.ld_qos_maxage;
+
+ osts = &(pool->pool_obds);
+ for (i = 0; i < osts->op_count; i++) {
+ int idx = osts->op_array[i];
+ struct lod_tgt_desc *tgt;
+ struct obd_statfs *sfs;
+
+ if (!test_bit(idx, lod->lod_ost_bitmap))
+ continue;
+ tgt = OST_TGT(lod, idx);
+ if (tgt->ltd_active == 0)
+ continue;
+ sfs = &tgt->ltd_statfs;
+
+ avail_bytes += sfs->os_bavail * sfs->os_bsize;
+ total_bytes += sfs->os_blocks * sfs->os_bsize;
+ }
+ if (total_bytes - avail_bytes >=
+ total_bytes * pool->pool_spill_threshold_pct / 100)
+ pool->pool_spill_is_active = true;
+ else
+ pool->pool_spill_is_active = false;
+
+out_sem:
+ up_write(&pool_tgt_rw_sem(pool));
+}
+
+/*
+ * to prevent infinite loops during spilling, lets limit number of passes
+ */
+#define LOD_SPILL_MAX 10
+
+/*
+ * XXX: consider a better schema to detect loops
+ */
+void lod_check_and_spill_pool(const struct lu_env *env, struct lod_device *lod,
+ char **poolname)
+{
+ struct pool_desc *pool;
+ int replaced = 0;
+
+ if (!poolname || !*poolname || (*poolname)[0] == '\0')
+ return;
+repeat:
+ pool = lod_pool_find(lod, *poolname);
+ if (!pool)
+ return;
+
+ lod_spill_target_refresh(env, lod, pool);
+ if (pool->pool_spill_is_active) {
+ if (++replaced >= LOD_SPILL_MAX)
+ CWARN("%s: more than %d levels of pool spill for '%s->%s'\n",
+ lod2obd(lod)->obd_name, LOD_SPILL_MAX,
+ *poolname, pool->pool_spill_target);
+ lod_set_pool(poolname, pool->pool_spill_target);
+ lod_pool_putref(pool);
+ if (replaced >= LOD_SPILL_MAX)
+ return;
+ goto repeat;
+ }
+
+ lod_pool_putref(pool);
+}