Whamcloud - gitweb
LU-4974 lod: Change pool_desc to "[lod|lov]_pool_desc"
[fs/lustre-release.git] / lustre / lod / lod_pool.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31 /*
32  * lustre/lod/lod_pool.c
33  *
34  * OST pool methods
35  *
36  * This file provides code related to the Logical Object Device (LOD)
37  * handling of OST Pools on the MDT.  Pools are named lists of targets
38  * that allow userspace to group targets that share a particlar property
39  * together so that users or kernel helpers can make decisions about file
40  * allocation based on these properties.  For example, pools could be
41  * defined based on fault domains (e.g. separate racks of server nodes) so
42  * that RAID-1 mirroring could select targets from independent fault
43  * domains, or pools could define target performance characteristics so
44  * that applicatins could select IOP-optimized storage or stream-optimized
45  * storage for a particular output file.
46  *
47  * This file handles creation, lookup, and removal of pools themselves, as
48  * well as adding and removing targets to pools.  It also handles lprocfs
49  * display of configured pool.  The pools are accessed by name in the pool
50  * hash, and are refcounted to ensure proper pool structure lifetimes.
51  *
52  * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
53  * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
54  * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
55  */
56
57 #define DEBUG_SUBSYSTEM S_LOV
58
59 #include <libcfs/libcfs.h>
60 #include <libcfs/linux/linux-hash.h>
61 #include <libcfs/linux/linux-fs.h>
62 #include <obd.h>
63 #include "lod_internal.h"
64
65 #define pool_tgt(_p, _i) OST_TGT(lu2lod_dev((_p)->pool_lobd->obd_lu_dev), \
66                                  (_p)->pool_obds.op_array[_i])
67
68 /**
69  * Get a reference on the specified pool.
70  *
71  * To ensure the pool descriptor is not freed before the caller is finished
72  * with it.  Any process that is accessing \a pool directly needs to hold
73  * reference on it, including /proc since a userspace thread may be holding
74  * the /proc file open and busy in the kernel.
75  *
76  * \param[in] pool      pool descriptor on which to gain reference
77  */
78 static void pool_getref(struct lod_pool_desc *pool)
79 {
80         CDEBUG(D_INFO, "pool %p\n", pool);
81         atomic_inc(&pool->pool_refcount);
82 }
83
84 /**
85  * Drop a reference on the specified pool and free its memory if needed.
86  *
87  * One reference is held by the LOD OBD device while it is configured, from
88  * the time the configuration log defines the pool until the time when it is
89  * dropped when the LOD OBD is cleaned up or the pool is deleted.  This means
90  * that the pool will not be freed while the LOD device is configured, unless
91  * it is explicitly destroyed by the sysadmin.  The pool structure is freed
92  * after the last reference on the structure is released.
93  *
94  * \param[in] pool      pool descriptor to drop reference on and possibly free
95  */
96 void lod_pool_putref(struct lod_pool_desc *pool)
97 {
98         CDEBUG(D_INFO, "pool %p\n", pool);
99         if (atomic_dec_and_test(&pool->pool_refcount)) {
100                 LASSERT(list_empty(&pool->pool_list));
101                 LASSERT(pool->pool_proc_entry == NULL);
102                 lu_tgt_pool_free(&(pool->pool_rr.lqr_pool));
103                 lu_tgt_pool_free(&(pool->pool_obds));
104                 kfree_rcu(pool, pool_rcu);
105                 EXIT;
106         }
107 }
108
109 static u32 pool_hashfh(const void *data, u32 len, u32 seed)
110 {
111         const char *pool_name = data;
112
113         return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
114                                                pool_name));
115 }
116
117 static int pool_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
118 {
119         const struct lod_pool_desc *pool = obj;
120         const char *pool_name = arg->key;
121
122         return strcmp(pool_name, pool->pool_name);
123 }
124
125 static const struct rhashtable_params pools_hash_params = {
126         .key_len        = 1, /* actually variable */
127         .key_offset     = offsetof(struct lod_pool_desc, pool_name),
128         .head_offset    = offsetof(struct lod_pool_desc, pool_hash),
129         .hashfn         = pool_hashfh,
130         .obj_cmpfn      = pool_cmpfn,
131         .automatic_shrinking = true,
132 };
133
134 /*
135  * Methods for /proc seq_file iteration of the defined pools.
136  */
137
138 #define POOL_IT_MAGIC 0xB001CEA0
139 struct lod_pool_iterator {
140         unsigned int      lpi_magic;    /* POOL_IT_MAGIC */
141         unsigned int      lpi_idx;      /* from 0 to pool_tgt_size - 1 */
142         struct lod_pool_desc *lpi_pool;
143 };
144
145 /**
146  * Return the next configured target within one pool for seq_file iteration.
147  *
148  * Iterator is used to go through the target entries of a single pool
149  * (i.e. the list of OSTs configured for a named pool).
150  * lpi_idx is the current target index in the pool's op_array[].
151  *
152  * The return type is a void * because this function is one of the
153  * struct seq_operations methods and must match the function template.
154  *
155  * \param[in] seq       /proc sequence file iteration tracking structure
156  * \param[in] v         unused
157  * \param[in] pos       position within iteration; 0 to number of targets - 1
158  *
159  * \retval      struct pool_iterator of the next pool descriptor
160  */
161 static void *pool_proc_next(struct seq_file *seq, void *v, loff_t *pos)
162 {
163         struct lod_pool_iterator *iter = seq->private;
164         int prev_idx;
165
166         LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X\n", iter->lpi_magic);
167
168         (*pos)++;
169         /* test if end of file */
170         if (*pos > pool_tgt_count(iter->lpi_pool))
171                 return NULL;
172
173         CFS_FAIL_TIMEOUT(OBD_FAIL_OST_LIST_ASSERT, cfs_fail_val);
174
175         /* iterate to find a non empty entry */
176         prev_idx = iter->lpi_idx;
177         iter->lpi_idx++;
178         if (iter->lpi_idx >= pool_tgt_count(iter->lpi_pool)) {
179                 iter->lpi_idx = prev_idx; /* we stay on the last entry */
180                 return NULL;
181         }
182
183         /* return != NULL to continue */
184         return iter;
185 }
186
187 /**
188  * Start seq_file iteration via /proc for a single pool.
189  *
190  * The \a pos parameter may be non-zero, indicating that the iteration
191  * is starting at some offset in the target list.  Use the seq_file
192  * private field to memorize the iterator so we can free it at stop().
193  * Need to restore the private pointer to the pool before freeing it.
194  *
195  * \param[in] seq       new sequence file structure to initialize
196  * \param[in] pos       initial target number at which to start iteration
197  *
198  * \retval              initialized pool iterator private structure
199  * \retval              NULL if \a pos exceeds the number of targets in \a pool
200  * \retval              negative error number on failure
201  */
202 static void *pool_proc_start(struct seq_file *seq, loff_t *pos)
203 {
204         struct lod_pool_desc *pool = seq->private;
205         struct lod_pool_iterator *iter;
206
207         pool_getref(pool);
208         if ((pool_tgt_count(pool) == 0) ||
209             (*pos >= pool_tgt_count(pool))) {
210                 /* iter is not created, so stop() has no way to
211                  * find pool to dec ref */
212                 lod_pool_putref(pool);
213                 return NULL;
214         }
215
216         OBD_ALLOC_PTR(iter);
217         if (iter == NULL)
218                 return ERR_PTR(-ENOMEM);
219         iter->lpi_magic = POOL_IT_MAGIC;
220         iter->lpi_pool = pool;
221         iter->lpi_idx = 0;
222
223         seq->private = iter;
224         down_read(&pool_tgt_rw_sem(pool));
225         if (*pos > 0) {
226                 loff_t i;
227                 void *ptr;
228
229                 i = 0;
230                 do {
231                         ptr = pool_proc_next(seq, &iter, &i);
232                 } while ((i < *pos) && (ptr != NULL));
233
234                 return ptr;
235         }
236
237         return iter;
238 }
239
240 /**
241  * Finish seq_file iteration for a single pool.
242  *
243  * Once iteration has been completed, the pool_iterator struct must be
244  * freed, and the seq_file private pointer restored to the pool, as it
245  * was initially when pool_proc_start() was called.
246  *
247  * In some cases the stop() method may be called 2 times, without calling
248  * the start() method (see seq_read() from fs/seq_file.c). We have to free
249  * the private iterator struct only if seq->private points to the iterator.
250  *
251  * \param[in] seq       sequence file structure to clean up
252  * \param[in] v         (unused)
253  */
254 static void pool_proc_stop(struct seq_file *seq, void *v)
255 {
256         struct lod_pool_iterator *iter = seq->private;
257
258         if (iter != NULL && iter->lpi_magic == POOL_IT_MAGIC) {
259                 up_read(&pool_tgt_rw_sem(iter->lpi_pool));
260                 seq->private = iter->lpi_pool;
261                 lod_pool_putref(iter->lpi_pool);
262                 OBD_FREE_PTR(iter);
263         }
264 }
265
266 /**
267  * Print out one target entry from the pool for seq_file iteration.
268  *
269  * The currently referenced pool target is given by op_array[lpi_idx].
270  *
271  * \param[in] seq       new sequence file structure to initialize
272  * \param[in] v         (unused)
273  */
274 static int pool_proc_show(struct seq_file *seq, void *v)
275 {
276         struct lod_pool_iterator *iter = v;
277         struct lod_tgt_desc  *tgt;
278
279         LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X\n", iter->lpi_magic);
280         LASSERT(iter->lpi_pool != NULL);
281         LASSERT(iter->lpi_idx <= pool_tgt_count(iter->lpi_pool));
282
283         tgt = pool_tgt(iter->lpi_pool, iter->lpi_idx);
284         if (tgt != NULL)
285                 seq_printf(seq, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
286
287         return 0;
288 }
289
290 static const struct seq_operations pool_proc_ops = {
291         .start  = pool_proc_start,
292         .next   = pool_proc_next,
293         .stop   = pool_proc_stop,
294         .show   = pool_proc_show,
295 };
296
297 /**
298  * Open a new /proc file for seq_file iteration of targets in one pool.
299  *
300  * Initialize the seq_file private pointer to reference the pool.
301  *
302  * \param inode inode to store iteration state for /proc
303  * \param file  file descriptor to store iteration methods
304  *
305  * \retval      0 for success
306  * \retval      negative error number on failure
307  */
308 static int pool_proc_open(struct inode *inode, struct file *file)
309 {
310         int rc;
311
312         rc = seq_open(file, &pool_proc_ops);
313         if (!rc) {
314                 struct seq_file *seq = file->private_data;
315                 seq->private = pde_data(inode);
316         }
317         return rc;
318 }
319
320 const static struct proc_ops pool_proc_operations = {
321         .proc_open      = pool_proc_open,
322         .proc_read      = seq_read,
323         .proc_lseek     = seq_lseek,
324         .proc_release   = seq_release,
325 };
326
327 /**
328  * Dump the pool target list into the Lustre debug log.
329  *
330  * This is a debugging function to allow dumping the list of targets
331  * in \a pool to the Lustre kernel debug log at the given \a level.
332  *
333  * This is not currently called by any existing code, but can be called
334  * from within gdb/crash to display the contents of the pool, or from
335  * code under development.
336  *
337  * \param[in] level     Lustre debug level (D_INFO, D_WARN, D_ERROR, etc)
338  * \param[in] pool      pool descriptor to be dumped
339  */
340 void lod_dump_pool(int level, struct lod_pool_desc *pool)
341 {
342         unsigned int i;
343
344         pool_getref(pool);
345
346         CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
347                pool->pool_name, pool->pool_obds.op_count);
348         down_read(&pool_tgt_rw_sem(pool));
349
350         for (i = 0; i < pool_tgt_count(pool) ; i++) {
351                 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
352                         continue;
353                 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
354                        pool->pool_name, i,
355                        obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
356         }
357
358         up_read(&pool_tgt_rw_sem(pool));
359         lod_pool_putref(pool);
360 }
361
362 static void pools_hash_exit(void *vpool, void *data)
363 {
364         struct lod_pool_desc *pool = vpool;
365
366         lod_pool_putref(pool);
367 }
368
369 int lod_pool_hash_init(struct rhashtable *tbl)
370 {
371         return rhashtable_init(tbl, &pools_hash_params);
372 }
373
374 void lod_pool_hash_destroy(struct rhashtable *tbl)
375 {
376         rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
377 }
378
379 bool lod_pool_exists(struct lod_device *lod, char *poolname)
380 {
381         struct lod_pool_desc *pool;
382
383         rcu_read_lock();
384         pool = rhashtable_lookup(&lod->lod_pools_hash_body,
385                                 poolname,
386                                 pools_hash_params);
387         rcu_read_unlock();
388         return pool != NULL;
389 }
390
391 struct lod_pool_desc *lod_pool_find(struct lod_device *lod, char *poolname)
392 {
393         struct lod_pool_desc *pool;
394
395         rcu_read_lock();
396         pool = rhashtable_lookup(&lod->lod_pools_hash_body,
397                                 poolname,
398                                 pools_hash_params);
399         if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
400                 pool = NULL;
401         rcu_read_unlock();
402         return pool;
403 }
404
405 static int lod_ost_pool_weights_seq_show(struct seq_file *m, void *data)
406 {
407         struct lod_pool_desc *pool = m->private;
408         struct lod_device *lod = lu2lod_dev(pool->pool_lobd->obd_lu_dev);
409
410         return lod_tgt_weights_seq_show(m, lod, &pool->pool_obds, false);
411 }
412
413 static ssize_t
414 lod_ost_pool_weights_seq_write(struct file *file, const char __user *buf,
415                                size_t count, loff_t *off)
416 {
417         struct seq_file *m = file->private_data;
418         struct lod_pool_desc *pool = m->private;
419         struct lod_device *lod = lu2lod_dev(pool->pool_lobd->obd_lu_dev);
420
421         return lod_tgt_weights_seq_write(m, buf, count, lod, &pool->pool_obds,
422                                          false);
423 }
424 LDEBUGFS_SEQ_FOPS(lod_ost_pool_weights);
425
426 static struct ldebugfs_vars ldebugfs_lod_pool_vars[] = {
427         { .name =       "qos_ost_weights",
428           .fops =       &lod_ost_pool_weights_fops,
429           .proc_mode =  0444 },
430         { 0 }
431 };
432
433 /**
434  * Allocate a new pool for the specified device.
435  *
436  * Allocate a new pool_desc structure for the specified \a new_pool
437  * device to create a pool with the given \a poolname.  The new pool
438  * structure is created with a single reference, and is freed when the
439  * reference count drops to zero.
440  *
441  * \param[in] obd       Lustre OBD device on which to add a pool iterator
442  * \param[in] poolname  the name of the pool to be created
443  *
444  * \retval              0 in case of success
445  * \retval              negative error code in case of error
446  */
447 int lod_pool_new(struct obd_device *obd, char *poolname)
448 {
449         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
450         struct lod_pool_desc  *new_pool;
451         int rc;
452         ENTRY;
453
454         if (strlen(poolname) > LOV_MAXPOOLNAME)
455                 RETURN(-ENAMETOOLONG);
456
457         /* OBD_ALLOC_* doesn't work with direct kfree_rcu use */
458         new_pool = kmalloc(sizeof(*new_pool), __GFP_ZERO);
459         if (new_pool == NULL)
460                 RETURN(-ENOMEM);
461
462         strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
463         new_pool->pool_spill_target[0] = '\0';
464         atomic_set(&new_pool->pool_spill_hit, 0);
465         new_pool->pool_lobd = obd;
466         atomic_set(&new_pool->pool_refcount, 1);
467         rc = lu_tgt_pool_init(&new_pool->pool_obds, 0);
468         if (rc)
469                 GOTO(out_free_pool, rc);
470
471         lu_qos_rr_init(&new_pool->pool_rr);
472
473         rc = lu_tgt_pool_init(&new_pool->pool_rr.lqr_pool, 0);
474         if (rc)
475                 GOTO(out_free_pool_obds, rc);
476
477 #ifdef CONFIG_PROC_FS
478         pool_getref(new_pool);
479         new_pool->pool_proc_entry = lprocfs_add_simple(lod->lod_pool_proc_entry,
480                                                        poolname, new_pool,
481                                                        &pool_proc_operations);
482         if (IS_ERR(new_pool->pool_proc_entry)) {
483                 CDEBUG(D_CONFIG, "%s: cannot add proc entry "LOV_POOLNAMEF"\n",
484                        obd->obd_name, poolname);
485                 new_pool->pool_proc_entry = NULL;
486                 lod_pool_putref(new_pool);
487         }
488
489         pool_getref(new_pool);
490         new_pool->pool_spill_proc_entry =
491                 lprocfs_register(poolname, lod->lod_spill_proc_entry,
492                         lprocfs_lod_spill_vars, new_pool);
493         if (IS_ERR(new_pool->pool_spill_proc_entry)) {
494                 rc = PTR_ERR(new_pool->pool_spill_proc_entry);
495                 new_pool->pool_proc_entry = NULL;
496                 lod_pool_putref(new_pool);
497         }
498
499         CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool,
500                new_pool->pool_proc_entry);
501 #endif
502
503         spin_lock(&obd->obd_dev_lock);
504         list_add_tail(&new_pool->pool_list, &lod->lod_pool_list);
505         lod->lod_pool_count++;
506         spin_unlock(&obd->obd_dev_lock);
507
508         /* Add to hash table only when it is fully ready. */
509         rc = rhashtable_lookup_insert_fast(&lod->lod_pools_hash_body,
510                                            &new_pool->pool_hash,
511                                            pools_hash_params);
512         if (rc) {
513                 if (rc != -EEXIST)
514                         /*
515                          * Hide -E2BIG and -EBUSY which
516                          * are not helpful.
517                          */
518                         rc = -ENOMEM;
519                 GOTO(out_err, rc);
520         }
521
522         new_pool->pool_debugfs = debugfs_create_dir(poolname,
523                                                     lod->lod_pool_debugfs);
524         ldebugfs_add_vars(new_pool->pool_debugfs, ldebugfs_lod_pool_vars,
525                           new_pool);
526
527         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
528                         poolname, lod->lod_pool_count);
529
530         RETURN(0);
531
532 out_err:
533         spin_lock(&obd->obd_dev_lock);
534         list_del_init(&new_pool->pool_list);
535         lod->lod_pool_count--;
536         spin_unlock(&obd->obd_dev_lock);
537
538         lprocfs_remove(&new_pool->pool_spill_proc_entry);
539         lprocfs_remove(&new_pool->pool_proc_entry);
540
541         lu_tgt_pool_free(&new_pool->pool_rr.lqr_pool);
542 out_free_pool_obds:
543         lu_tgt_pool_free(&new_pool->pool_obds);
544 out_free_pool:
545         OBD_FREE_PTR(new_pool);
546         return rc;
547 }
548
549 /**
550  * Remove the named pool from the OBD device.
551  *
552  * \param[in] obd       OBD device on which pool was previously created
553  * \param[in] poolname  name of pool to remove from \a obd
554  *
555  * \retval              0 on successfully removing the pool
556  * \retval              negative error numbers for failures
557  */
558 int lod_pool_del(struct obd_device *obd, char *poolname)
559 {
560         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
561         struct lod_pool_desc  *pool;
562         ENTRY;
563
564         /* lookup and kill hash reference */
565         rcu_read_lock();
566         pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
567                                  pools_hash_params);
568         if (pool && rhashtable_remove_fast(&lod->lod_pools_hash_body,
569                                            &pool->pool_hash,
570                                            pools_hash_params) != 0)
571                 pool = NULL;
572         rcu_read_unlock();
573         if (!pool)
574                 RETURN(-ENOENT);
575
576         debugfs_remove_recursive(pool->pool_debugfs);
577
578         if (pool->pool_proc_entry != NULL) {
579                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
580                 lprocfs_remove(&pool->pool_proc_entry);
581                 lod_pool_putref(pool);
582         }
583         if (pool->pool_spill_proc_entry != NULL) {
584                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_spill_proc_entry);
585                 lprocfs_remove(&pool->pool_spill_proc_entry);
586                 lod_pool_putref(pool);
587         }
588
589         spin_lock(&obd->obd_dev_lock);
590         list_del_init(&pool->pool_list);
591         lod->lod_pool_count--;
592         spin_unlock(&obd->obd_dev_lock);
593
594         /* release last reference */
595         lod_pool_putref(pool);
596
597         RETURN(0);
598 }
599
600 /**
601  * Add a single target device to the named pool.
602  *
603  * Add the target specified by \a ostname to the specified \a poolname.
604  *
605  * \param[in] obd       OBD device on which to add the pool
606  * \param[in] poolname  name of the pool to which to add the target \a ostname
607  * \param[in] ostname   name of the target device to be added
608  *
609  * \retval              0 if \a ostname was (previously) added to the named pool
610  * \retval              negative error number on failure
611  */
612 int lod_pool_add(struct obd_device *obd, char *poolname, char *ostname)
613 {
614         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
615         struct obd_uuid ost_uuid;
616         struct lod_pool_desc *pool;
617         struct lu_tgt_desc *tgt;
618         int rc = -EINVAL;
619         ENTRY;
620
621         pool = lod_pool_find(lod, poolname);
622         if (!pool)
623                 RETURN(-ENOENT);
624
625         obd_str2uuid(&ost_uuid, ostname);
626
627         /* search ost in lod array */
628         lod_getref(&lod->lod_ost_descs);
629         lod_foreach_ost(lod, tgt) {
630                 if (obd_uuid_equals(&ost_uuid, &tgt->ltd_uuid)) {
631                         rc = 0;
632                         break;
633                 }
634         }
635
636         if (rc)
637                 GOTO(out, rc);
638
639         rc = lu_tgt_pool_add(&pool->pool_obds, tgt->ltd_index,
640                              lod->lod_ost_count);
641         if (rc)
642                 GOTO(out, rc);
643
644         set_bit(LQ_DIRTY, &pool->pool_rr.lqr_flags);
645
646         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
647                         ostname, poolname,  pool_tgt_count(pool));
648
649         EXIT;
650 out:
651         lod_putref(lod, &lod->lod_ost_descs);
652         lod_pool_putref(pool);
653         return rc;
654 }
655
656 /**
657  * Remove the named target from the specified pool.
658  *
659  * Remove one target named \a ostname from \a poolname.  The \a ostname
660  * is searched for in the lod_device lod_ost_bitmap array, to ensure the
661  * specified name actually exists in the pool.
662  *
663  * \param[in] obd       OBD device from which to remove \a poolname
664  * \param[in] poolname  name of the pool to be changed
665  * \param[in] ostname   name of the target to remove from \a poolname
666  *
667  * \retval              0 on successfully removing \a ostname from the pool
668  * \retval              negative number on error (e.g. \a ostname not in pool)
669  */
670 int lod_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
671 {
672         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
673         struct lu_tgt_desc *ost;
674         struct obd_uuid ost_uuid;
675         struct lod_pool_desc *pool;
676         int rc = -EINVAL;
677         ENTRY;
678
679         /* lookup and kill hash reference */
680         pool = lod_pool_find(lod, poolname);
681         if (!pool)
682                 RETURN(-ENOENT);
683
684         obd_str2uuid(&ost_uuid, ostname);
685
686         lod_getref(&lod->lod_ost_descs);
687         lod_foreach_ost(lod, ost) {
688                 if (obd_uuid_equals(&ost_uuid, &ost->ltd_uuid)) {
689                         rc = 0;
690                         break;
691                 }
692         }
693
694         /* test if ost found in lod array */
695         if (rc)
696                 GOTO(out, rc);
697
698         lu_tgt_pool_remove(&pool->pool_obds, ost->ltd_index);
699         set_bit(LQ_DIRTY, &pool->pool_rr.lqr_flags);
700
701         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
702                poolname);
703
704         EXIT;
705 out:
706         lod_putref(lod, &lod->lod_ost_descs);
707         lod_pool_putref(pool);
708         return rc;
709 }
710
711 /**
712  * Check if the specified target exists in the pool.
713  *
714  * The caller may not have a reference on \a pool if it got the pool without
715  * calling lod_find_pool() (e.g. directly from the lod pool list)
716  *
717  * \param[in] idx       Target index to check
718  * \param[in] pool      Pool in which to check if target is added.
719  *
720  * \retval              0 successfully found index in \a pool
721  * \retval              negative error if device not found in \a pool
722  */
723 int lod_check_index_in_pool(__u32 idx, struct lod_pool_desc *pool)
724 {
725         int rc;
726
727         pool_getref(pool);
728         rc = lu_tgt_check_index(idx, &pool->pool_obds);
729         lod_pool_putref(pool);
730         return rc;
731 }
732
733 /**
734  * Find the pool descriptor for the specified pool and return it with a
735  * reference to the caller if found.
736  *
737  * \param[in] lod       LOD on which the pools are configured
738  * \param[in] poolname  NUL-terminated name of the pool
739  *
740  * \retval      pointer to pool descriptor on success
741  * \retval      NULL if \a poolname could not be found or poolname is empty
742  */
743 struct lod_pool_desc *lod_find_pool(struct lod_device *lod, char *poolname)
744 {
745         struct lod_pool_desc *pool;
746
747         if (poolname[0] == '\0' || lov_pool_is_reserved(poolname))
748                 return NULL;
749
750         pool = lod_pool_find(lod, poolname);
751         if (!pool)
752                 CDEBUG(D_CONFIG,
753                        "%s: request for an unknown pool (" LOV_POOLNAMEF ")\n",
754                        lod->lod_child_exp->exp_obd->obd_name, poolname);
755         if (pool != NULL && pool_tgt_count(pool) == 0) {
756                 CDEBUG(D_CONFIG, "%s: request for an empty pool ("
757                        LOV_POOLNAMEF")\n",
758                        lod->lod_child_exp->exp_obd->obd_name, poolname);
759                 /* pool is ignored, so we remove ref on it */
760                 lod_pool_putref(pool);
761                 pool = NULL;
762         }
763
764         return pool;
765 }
766
767 void lod_spill_target_refresh(const struct lu_env *env, struct lod_device *lod,
768                               struct lod_pool_desc *pool)
769 {
770         __u64 avail_bytes = 0, total_bytes = 0;
771         struct lu_tgt_pool *osts;
772         int i;
773
774         if (ktime_get_seconds() < pool->pool_spill_expire)
775                 return;
776
777         if (pool->pool_spill_threshold_pct == 0)
778                 return;
779
780         lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
781
782         down_write(&pool_tgt_rw_sem(pool));
783         if (ktime_get_seconds() < pool->pool_spill_expire)
784                 goto out_sem;
785         pool->pool_spill_expire = ktime_get_seconds() +
786                 lod->lod_ost_descs.ltd_lov_desc.ld_qos_maxage;
787
788         osts = &(pool->pool_obds);
789         for (i = 0; i < osts->op_count; i++) {
790                 int idx = osts->op_array[i];
791                 struct lod_tgt_desc *tgt;
792                 struct obd_statfs *sfs;
793
794                 if (!test_bit(idx, lod->lod_ost_bitmap))
795                         continue;
796                 tgt = OST_TGT(lod, idx);
797                 if (!tgt->ltd_active)
798                         continue;
799                 sfs = &tgt->ltd_statfs;
800
801                 avail_bytes += sfs->os_bavail * sfs->os_bsize;
802                 total_bytes += sfs->os_blocks * sfs->os_bsize;
803         }
804         if (total_bytes - avail_bytes >=
805             total_bytes * pool->pool_spill_threshold_pct / 100)
806                 pool->pool_spill_is_active = true;
807         else
808                 pool->pool_spill_is_active = false;
809
810 out_sem:
811         up_write(&pool_tgt_rw_sem(pool));
812 }
813
814 /*
815  * XXX: consider a better schema to detect loops
816  */
817 void lod_check_and_spill_pool(const struct lu_env *env, struct lod_device *lod,
818                               char **poolname)
819 {
820         struct lod_pool_desc *pool;
821
822         if (!poolname || !*poolname || (*poolname)[0] == '\0')
823                 return;
824 repeat:
825         pool = lod_pool_find(lod, *poolname);
826         if (!pool)
827                 return;
828
829         lod_spill_target_refresh(env, lod, pool);
830         if (pool->pool_spill_is_active) {
831                 lod_set_pool(poolname, pool->pool_spill_target);
832                 atomic_inc(&pool->pool_spill_hit);
833                 lod_pool_putref(pool);
834                 goto repeat;
835         }
836
837         lod_pool_putref(pool);
838 }