Whamcloud - gitweb
LU-8130 lov: convert lo[v|d]_pool to use rhashtable
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 /*
33  * lustre/lod/lod_pool.c
34  *
35  * OST pool methods
36  *
37  * This file provides code related to the Logical Object Device (LOD)
38  * handling of OST Pools on the MDT.  Pools are named lists of targets
39  * that allow userspace to group targets that share a particlar property
40  * together so that users or kernel helpers can make decisions about file
41  * allocation based on these properties.  For example, pools could be
42  * defined based on fault domains (e.g. separate racks of server nodes) so
43  * that RAID-1 mirroring could select targets from independent fault
44  * domains, or pools could define target performance characteristics so
45  * that applicatins could select IOP-optimized storage or stream-optimized
46  * storage for a particular output file.
47  *
48  * This file handles creation, lookup, and removal of pools themselves, as
49  * well as adding and removing targets to pools.  It also handles lprocfs
50  * display of configured pool.  The pools are accessed by name in the pool
51  * hash, and are refcounted to ensure proper pool structure lifetimes.
52  *
53  * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
54  * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
55  * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
56  */
57
58 #define DEBUG_SUBSYSTEM S_LOV
59
60 #include <libcfs/libcfs.h>
61 #include <libcfs/linux/linux-hash.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 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 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                 lod_tgt_pool_free(&(pool->pool_rr.lqr_pool));
103                 lod_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 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 pool_desc, pool_name),
128         .head_offset    = offsetof(struct 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 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         /* test if end of file */
169         if (*pos >= pool_tgt_count(iter->lpi_pool))
170                 return NULL;
171
172         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_LIST_ASSERT, cfs_fail_val);
173
174         /* iterate to find a non empty entry */
175         prev_idx = iter->lpi_idx;
176         iter->lpi_idx++;
177         if (iter->lpi_idx >= pool_tgt_count(iter->lpi_pool)) {
178                 iter->lpi_idx = prev_idx; /* we stay on the last entry */
179                 return NULL;
180         }
181         (*pos)++;
182         /* return != NULL to continue */
183         return iter;
184 }
185
186 /**
187  * Start seq_file iteration via /proc for a single pool.
188  *
189  * The \a pos parameter may be non-zero, indicating that the iteration
190  * is starting at some offset in the target list.  Use the seq_file
191  * private field to memorize the iterator so we can free it at stop().
192  * Need to restore the private pointer to the pool before freeing it.
193  *
194  * \param[in] seq       new sequence file structure to initialize
195  * \param[in] pos       initial target number at which to start iteration
196  *
197  * \retval              initialized pool iterator private structure
198  * \retval              NULL if \a pos exceeds the number of targets in \a pool
199  * \retval              negative error number on failure
200  */
201 static void *pool_proc_start(struct seq_file *seq, loff_t *pos)
202 {
203         struct pool_desc *pool = seq->private;
204         struct lod_pool_iterator *iter;
205
206         pool_getref(pool);
207         if ((pool_tgt_count(pool) == 0) ||
208             (*pos >= pool_tgt_count(pool))) {
209                 /* iter is not created, so stop() has no way to
210                  * find pool to dec ref */
211                 lod_pool_putref(pool);
212                 return NULL;
213         }
214
215         OBD_ALLOC_PTR(iter);
216         if (iter == NULL)
217                 return ERR_PTR(-ENOMEM);
218         iter->lpi_magic = POOL_IT_MAGIC;
219         iter->lpi_pool = pool;
220         iter->lpi_idx = 0;
221
222         seq->private = iter;
223         down_read(&pool_tgt_rw_sem(pool));
224         if (*pos > 0) {
225                 loff_t i;
226                 void *ptr;
227
228                 i = 0;
229                 do {
230                         ptr = pool_proc_next(seq, &iter, &i);
231                 } while ((i < *pos) && (ptr != NULL));
232
233                 return ptr;
234         }
235
236         return iter;
237 }
238
239 /**
240  * Finish seq_file iteration for a single pool.
241  *
242  * Once iteration has been completed, the pool_iterator struct must be
243  * freed, and the seq_file private pointer restored to the pool, as it
244  * was initially when pool_proc_start() was called.
245  *
246  * In some cases the stop() method may be called 2 times, without calling
247  * the start() method (see seq_read() from fs/seq_file.c). We have to free
248  * the private iterator struct only if seq->private points to the iterator.
249  *
250  * \param[in] seq       sequence file structure to clean up
251  * \param[in] v         (unused)
252  */
253 static void pool_proc_stop(struct seq_file *seq, void *v)
254 {
255         struct lod_pool_iterator *iter = seq->private;
256
257         if (iter != NULL && iter->lpi_magic == POOL_IT_MAGIC) {
258                 up_read(&pool_tgt_rw_sem(iter->lpi_pool));
259                 seq->private = iter->lpi_pool;
260                 lod_pool_putref(iter->lpi_pool);
261                 OBD_FREE_PTR(iter);
262         }
263 }
264
265 /**
266  * Print out one target entry from the pool for seq_file iteration.
267  *
268  * The currently referenced pool target is given by op_array[lpi_idx].
269  *
270  * \param[in] seq       new sequence file structure to initialize
271  * \param[in] v         (unused)
272  */
273 static int pool_proc_show(struct seq_file *seq, void *v)
274 {
275         struct lod_pool_iterator *iter = v;
276         struct lod_tgt_desc  *tgt;
277
278         LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X\n", iter->lpi_magic);
279         LASSERT(iter->lpi_pool != NULL);
280         LASSERT(iter->lpi_idx <= pool_tgt_count(iter->lpi_pool));
281
282         tgt = pool_tgt(iter->lpi_pool, iter->lpi_idx);
283         if (tgt != NULL)
284                 seq_printf(seq, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
285
286         return 0;
287 }
288
289 static const struct seq_operations pool_proc_ops = {
290         .start  = pool_proc_start,
291         .next   = pool_proc_next,
292         .stop   = pool_proc_stop,
293         .show   = pool_proc_show,
294 };
295
296 /**
297  * Open a new /proc file for seq_file iteration of targets in one pool.
298  *
299  * Initialize the seq_file private pointer to reference the pool.
300  *
301  * \param inode inode to store iteration state for /proc
302  * \param file  file descriptor to store iteration methods
303  *
304  * \retval      0 for success
305  * \retval      negative error number on failure
306  */
307 static int pool_proc_open(struct inode *inode, struct file *file)
308 {
309         int rc;
310
311         rc = seq_open(file, &pool_proc_ops);
312         if (!rc) {
313                 struct seq_file *seq = file->private_data;
314                 seq->private = PDE_DATA(inode);
315         }
316         return rc;
317 }
318
319 static struct file_operations pool_proc_operations = {
320         .open           = pool_proc_open,
321         .read           = seq_read,
322         .llseek         = seq_lseek,
323         .release        = seq_release,
324 };
325
326 /**
327  * Dump the pool target list into the Lustre debug log.
328  *
329  * This is a debugging function to allow dumping the list of targets
330  * in \a pool to the Lustre kernel debug log at the given \a level.
331  *
332  * This is not currently called by any existing code, but can be called
333  * from within gdb/crash to display the contents of the pool, or from
334  * code under development.
335  *
336  * \param[in] level     Lustre debug level (D_INFO, D_WARN, D_ERROR, etc)
337  * \param[in] pool      pool descriptor to be dumped
338  */
339 void lod_dump_pool(int level, struct pool_desc *pool)
340 {
341         unsigned int i;
342
343         pool_getref(pool);
344
345         CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
346                pool->pool_name, pool->pool_obds.op_count);
347         down_read(&pool_tgt_rw_sem(pool));
348
349         for (i = 0; i < pool_tgt_count(pool) ; i++) {
350                 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
351                         continue;
352                 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
353                        pool->pool_name, i,
354                        obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
355         }
356
357         up_read(&pool_tgt_rw_sem(pool));
358         lod_pool_putref(pool);
359 }
360
361 /**
362  * Initialize the pool data structures at startup.
363  *
364  * Allocate and initialize the pool data structures with the specified
365  * array size.  If pool count is not specified (\a count == 0), then
366  * POOL_INIT_COUNT will be used.  Allocating a non-zero initial array
367  * size avoids the need to reallocate as new pools are added.
368  *
369  * \param[in] op        pool structure
370  * \param[in] count     initial size of the target op_array[] array
371  *
372  * \retval              0 indicates successful pool initialization
373  * \retval              negative error number on failure
374  */
375 #define POOL_INIT_COUNT 2
376 int lod_tgt_pool_init(struct lu_tgt_pool *op, unsigned int count)
377 {
378         ENTRY;
379
380         if (count == 0)
381                 count = POOL_INIT_COUNT;
382         op->op_array = NULL;
383         op->op_count = 0;
384         init_rwsem(&op->op_rw_sem);
385         op->op_size = count * sizeof(op->op_array[0]);
386         OBD_ALLOC(op->op_array, op->op_size);
387         if (op->op_array == NULL) {
388                 op->op_size = 0;
389                 RETURN(-ENOMEM);
390         }
391         EXIT;
392         return 0;
393 }
394
395 /**
396  * Increase the op_array size to hold more targets in this pool.
397  *
398  * The size is increased to at least \a min_count, but may be larger
399  * for an existing pool since ->op_array[] is growing exponentially.
400  * Caller must hold write op_rwlock.
401  *
402  * \param[in] op        pool structure
403  * \param[in] min_count minimum number of entries to handle
404  *
405  * \retval              0 on success
406  * \retval              negative error number on failure.
407  */
408 int lod_tgt_pool_extend(struct lu_tgt_pool *op, unsigned int min_count)
409 {
410         __u32 *new;
411         __u32 new_size;
412
413         LASSERT(min_count != 0);
414
415         if (op->op_count * sizeof(op->op_array[0]) < op->op_size)
416                 return 0;
417
418         new_size = max_t(__u32, min_count * sizeof(op->op_array[0]),
419                          2 * op->op_size);
420         OBD_ALLOC(new, new_size);
421         if (new == NULL)
422                 return -ENOMEM;
423
424         /* copy old array to new one */
425         memcpy(new, op->op_array, op->op_size);
426         OBD_FREE(op->op_array, op->op_size);
427         op->op_array = new;
428         op->op_size = new_size;
429
430         return 0;
431 }
432
433 /**
434  * Add a new target to an existing pool.
435  *
436  * Add a new target device to the pool previously created and returned by
437  * lod_pool_new().  Each target can only be in each pool at most one time.
438  *
439  * \param[in] op        target pool to add new entry
440  * \param[in] idx       pool index number to add to the \a op array
441  * \param[in] min_count minimum number of entries to expect in the pool
442  *
443  * \retval              0 if target could be added to the pool
444  * \retval              negative error if target \a idx was not added
445  */
446 int lod_tgt_pool_add(struct lu_tgt_pool *op, __u32 idx, unsigned int min_count)
447 {
448         unsigned int i;
449         int rc = 0;
450         ENTRY;
451
452         down_write(&op->op_rw_sem);
453
454         rc = lod_tgt_pool_extend(op, min_count);
455         if (rc)
456                 GOTO(out, rc);
457
458         /* search ost in pool array */
459         for (i = 0; i < op->op_count; i++) {
460                 if (op->op_array[i] == idx)
461                         GOTO(out, rc = -EEXIST);
462         }
463         /* ost not found we add it */
464         op->op_array[op->op_count] = idx;
465         op->op_count++;
466         EXIT;
467 out:
468         up_write(&op->op_rw_sem);
469         return rc;
470 }
471
472 /**
473  * Remove an existing pool from the system.
474  *
475  * The specified pool must have previously been allocated by
476  * lod_pool_new() and not have any target members in the pool.
477  * If the removed target is not the last, compact the array
478  * to remove empty spaces.
479  *
480  * \param[in] op        pointer to the original data structure
481  * \param[in] idx       target index to be removed
482  *
483  * \retval              0 on success
484  * \retval              negative error number on failure
485  */
486 int lod_tgt_pool_remove(struct lu_tgt_pool *op, __u32 idx)
487 {
488         unsigned int i;
489         ENTRY;
490
491         down_write(&op->op_rw_sem);
492
493         for (i = 0; i < op->op_count; i++) {
494                 if (op->op_array[i] == idx) {
495                         memmove(&op->op_array[i], &op->op_array[i + 1],
496                                 (op->op_count - i - 1) *
497                                 sizeof(op->op_array[0]));
498                         op->op_count--;
499                         up_write(&op->op_rw_sem);
500                         EXIT;
501                         return 0;
502                 }
503         }
504
505         up_write(&op->op_rw_sem);
506         RETURN(-EINVAL);
507 }
508
509 /**
510  * Free the pool after it was emptied and removed from /proc.
511  *
512  * Note that all of the child/target entries referenced by this pool
513  * must have been removed by lod_ost_pool_remove() before it can be
514  * deleted from memory.
515  *
516  * \param[in] op        pool to be freed.
517  *
518  * \retval              0 on success or if pool was already freed
519  */
520 int lod_tgt_pool_free(struct lu_tgt_pool *op)
521 {
522         ENTRY;
523
524         if (op->op_size == 0)
525                 RETURN(0);
526
527         down_write(&op->op_rw_sem);
528
529         OBD_FREE(op->op_array, op->op_size);
530         op->op_array = NULL;
531         op->op_count = 0;
532         op->op_size = 0;
533
534         up_write(&op->op_rw_sem);
535         RETURN(0);
536 }
537
538 static void pools_hash_exit(void *vpool, void *data)
539 {
540         struct pool_desc *pool = vpool;
541
542         lod_pool_putref(pool);
543 }
544
545 int lod_pool_hash_init(struct rhashtable *tbl)
546 {
547         return rhashtable_init(tbl, &pools_hash_params);
548 }
549
550 void lod_pool_hash_destroy(struct rhashtable *tbl)
551 {
552         rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
553 }
554
555 /**
556  * Allocate a new pool for the specified device.
557  *
558  * Allocate a new pool_desc structure for the specified \a new_pool
559  * device to create a pool with the given \a poolname.  The new pool
560  * structure is created with a single reference, and is freed when the
561  * reference count drops to zero.
562  *
563  * \param[in] obd       Lustre OBD device on which to add a pool iterator
564  * \param[in] poolname  the name of the pool to be created
565  *
566  * \retval              0 in case of success
567  * \retval              negative error code in case of error
568  */
569 int lod_pool_new(struct obd_device *obd, char *poolname)
570 {
571         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
572         struct pool_desc  *new_pool;
573         int rc;
574         ENTRY;
575
576         if (strlen(poolname) > LOV_MAXPOOLNAME)
577                 RETURN(-ENAMETOOLONG);
578
579         /* OBD_ALLOC_* doesn't work with direct kfree_rcu use */
580         new_pool = kmalloc(sizeof(*new_pool), GFP_KERNEL);
581         if (new_pool == NULL)
582                 RETURN(-ENOMEM);
583
584         strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
585         new_pool->pool_lobd = obd;
586         atomic_set(&new_pool->pool_refcount, 1);
587         rc = lod_tgt_pool_init(&new_pool->pool_obds, 0);
588         if (rc)
589                 GOTO(out_err, rc);
590
591         lu_qos_rr_init(&new_pool->pool_rr);
592
593         rc = lod_tgt_pool_init(&new_pool->pool_rr.lqr_pool, 0);
594         if (rc)
595                 GOTO(out_free_pool_obds, rc);
596
597 #ifdef CONFIG_PROC_FS
598         pool_getref(new_pool);
599         new_pool->pool_proc_entry = lprocfs_add_simple(lod->lod_pool_proc_entry,
600                                                        poolname, new_pool,
601                                                        &pool_proc_operations);
602         if (IS_ERR(new_pool->pool_proc_entry)) {
603                 CDEBUG(D_CONFIG, "%s: cannot add proc entry "LOV_POOLNAMEF"\n",
604                        obd->obd_name, poolname);
605                 new_pool->pool_proc_entry = NULL;
606                 lod_pool_putref(new_pool);
607         }
608         CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool,
609                new_pool->pool_proc_entry);
610 #endif
611
612         spin_lock(&obd->obd_dev_lock);
613         list_add_tail(&new_pool->pool_list, &lod->lod_pool_list);
614         lod->lod_pool_count++;
615         spin_unlock(&obd->obd_dev_lock);
616
617         /* Add to hash table only when it is fully ready. */
618         rc = rhashtable_lookup_insert_fast(&lod->lod_pools_hash_body,
619                                            &new_pool->pool_hash,
620                                            pools_hash_params);
621         if (rc) {
622                 if (rc != -EEXIST)
623                         /*
624                          * Hide -E2BIG and -EBUSY which
625                          * are not helpful.
626                          */
627                         rc = -ENOMEM;
628                 GOTO(out_err, rc);
629         }
630
631         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
632                         poolname, lod->lod_pool_count);
633
634         RETURN(0);
635
636 out_err:
637         spin_lock(&obd->obd_dev_lock);
638         list_del_init(&new_pool->pool_list);
639         lod->lod_pool_count--;
640         spin_unlock(&obd->obd_dev_lock);
641
642         lprocfs_remove(&new_pool->pool_proc_entry);
643
644         lod_tgt_pool_free(&new_pool->pool_rr.lqr_pool);
645 out_free_pool_obds:
646         lod_tgt_pool_free(&new_pool->pool_obds);
647         OBD_FREE_PTR(new_pool);
648         return rc;
649 }
650
651 /**
652  * Remove the named pool from the OBD device.
653  *
654  * \param[in] obd       OBD device on which pool was previously created
655  * \param[in] poolname  name of pool to remove from \a obd
656  *
657  * \retval              0 on successfully removing the pool
658  * \retval              negative error numbers for failures
659  */
660 int lod_pool_del(struct obd_device *obd, char *poolname)
661 {
662         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
663         struct pool_desc  *pool;
664         ENTRY;
665
666         /* lookup and kill hash reference */
667         rcu_read_lock();
668         pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
669                                  pools_hash_params);
670         if (pool && rhashtable_remove_fast(&lod->lod_pools_hash_body,
671                                            &pool->pool_hash,
672                                            pools_hash_params) != 0)
673                 pool = NULL;
674         rcu_read_unlock();
675         if (!pool)
676                 RETURN(-ENOENT);
677
678         if (pool->pool_proc_entry != NULL) {
679                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
680                 lprocfs_remove(&pool->pool_proc_entry);
681                 lod_pool_putref(pool);
682         }
683
684         spin_lock(&obd->obd_dev_lock);
685         list_del_init(&pool->pool_list);
686         lod->lod_pool_count--;
687         spin_unlock(&obd->obd_dev_lock);
688
689         /* release last reference */
690         lod_pool_putref(pool);
691
692         RETURN(0);
693 }
694
695 /**
696  * Add a single target device to the named pool.
697  *
698  * Add the target specified by \a ostname to the specified \a poolname.
699  *
700  * \param[in] obd       OBD device on which to add the pool
701  * \param[in] poolname  name of the pool to which to add the target \a ostname
702  * \param[in] ostname   name of the target device to be added
703  *
704  * \retval              0 if \a ostname was (previously) added to the named pool
705  * \retval              negative error number on failure
706  */
707 int lod_pool_add(struct obd_device *obd, char *poolname, char *ostname)
708 {
709         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
710         struct obd_uuid ost_uuid;
711         struct pool_desc *pool;
712         struct lu_tgt_desc *tgt;
713         int rc = -EINVAL;
714         ENTRY;
715
716         rcu_read_lock();
717         pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
718                                  pools_hash_params);
719         if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
720                 pool = NULL;
721         rcu_read_unlock();
722         if (!pool)
723                 RETURN(-ENOENT);
724
725         obd_str2uuid(&ost_uuid, ostname);
726
727         /* search ost in lod array */
728         lod_getref(&lod->lod_ost_descs);
729         lod_foreach_ost(lod, tgt) {
730                 if (obd_uuid_equals(&ost_uuid, &tgt->ltd_uuid)) {
731                         rc = 0;
732                         break;
733                 }
734         }
735
736         if (rc)
737                 GOTO(out, rc);
738
739         rc = lod_tgt_pool_add(&pool->pool_obds, tgt->ltd_index,
740                               lod->lod_ost_count);
741         if (rc)
742                 GOTO(out, rc);
743
744         pool->pool_rr.lqr_dirty = 1;
745
746         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
747                         ostname, poolname,  pool_tgt_count(pool));
748
749         EXIT;
750 out:
751         lod_putref(lod, &lod->lod_ost_descs);
752         lod_pool_putref(pool);
753         return rc;
754 }
755
756 /**
757  * Remove the named target from the specified pool.
758  *
759  * Remove one target named \a ostname from \a poolname.  The \a ostname
760  * is searched for in the lod_device lod_ost_bitmap array, to ensure the
761  * specified name actually exists in the pool.
762  *
763  * \param[in] obd       OBD device from which to remove \a poolname
764  * \param[in] poolname  name of the pool to be changed
765  * \param[in] ostname   name of the target to remove from \a poolname
766  *
767  * \retval              0 on successfully removing \a ostname from the pool
768  * \retval              negative number on error (e.g. \a ostname not in pool)
769  */
770 int lod_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
771 {
772         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
773         struct lu_tgt_desc *ost;
774         struct obd_uuid ost_uuid;
775         struct pool_desc *pool;
776         int rc = -EINVAL;
777         ENTRY;
778
779         /* lookup and kill hash reference */
780         rcu_read_lock();
781         pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
782                                  pools_hash_params);
783         if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
784                 pool = NULL;
785         rcu_read_unlock();
786         if (!pool)
787                 RETURN(-ENOENT);
788
789         obd_str2uuid(&ost_uuid, ostname);
790
791         lod_getref(&lod->lod_ost_descs);
792         lod_foreach_ost(lod, ost) {
793                 if (obd_uuid_equals(&ost_uuid, &ost->ltd_uuid)) {
794                         rc = 0;
795                         break;
796                 }
797         }
798
799         /* test if ost found in lod array */
800         if (rc)
801                 GOTO(out, rc);
802
803         lod_tgt_pool_remove(&pool->pool_obds, ost->ltd_index);
804         pool->pool_rr.lqr_dirty = 1;
805
806         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
807                poolname);
808
809         EXIT;
810 out:
811         lod_putref(lod, &lod->lod_ost_descs);
812         lod_pool_putref(pool);
813         return rc;
814 }
815
816 /**
817  * Check if the specified target exists in the pool.
818  *
819  * The caller may not have a reference on \a pool if it got the pool without
820  * calling lod_find_pool() (e.g. directly from the lod pool list)
821  *
822  * \param[in] idx       Target index to check
823  * \param[in] pool      Pool in which to check if target is added.
824  *
825  * \retval              0 successfully found index in \a pool
826  * \retval              negative error if device not found in \a pool
827  */
828 int lod_check_index_in_pool(__u32 idx, struct pool_desc *pool)
829 {
830         unsigned int i;
831         int rc;
832         ENTRY;
833
834         pool_getref(pool);
835
836         down_read(&pool_tgt_rw_sem(pool));
837
838         for (i = 0; i < pool_tgt_count(pool); i++) {
839                 if (pool_tgt_array(pool)[i] == idx)
840                         GOTO(out, rc = 0);
841         }
842         rc = -ENOENT;
843         EXIT;
844 out:
845         up_read(&pool_tgt_rw_sem(pool));
846
847         lod_pool_putref(pool);
848         return rc;
849 }
850
851 /**
852  * Find the pool descriptor for the specified pool and return it with a
853  * reference to the caller if found.
854  *
855  * \param[in] lod       LOD on which the pools are configured
856  * \param[in] poolname  NUL-terminated name of the pool
857  *
858  * \retval      pointer to pool descriptor on success
859  * \retval      NULL if \a poolname could not be found or poolname is empty
860  */
861 struct pool_desc *lod_find_pool(struct lod_device *lod, char *poolname)
862 {
863         struct pool_desc *pool;
864
865         pool = NULL;
866         if (poolname[0] != '\0') {
867                 rcu_read_lock();
868                 pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
869                                          pools_hash_params);
870                 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
871                         pool = NULL;
872                 rcu_read_unlock();
873                 if (!pool)
874                         CDEBUG(D_CONFIG,
875                                "%s: request for an unknown pool (" LOV_POOLNAMEF ")\n",
876                                lod->lod_child_exp->exp_obd->obd_name, poolname);
877                 if (pool != NULL && pool_tgt_count(pool) == 0) {
878                         CDEBUG(D_CONFIG, "%s: request for an empty pool ("
879                                LOV_POOLNAMEF")\n",
880                                lod->lod_child_exp->exp_obd->obd_name, poolname);
881                         /* pool is ignored, so we remove ref on it */
882                         lod_pool_putref(pool);
883                         pool = NULL;
884                 }
885         }
886         return pool;
887 }
888