Whamcloud - gitweb
LU-4974 lod: use pool_key() to access key
[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, 2014 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 <obd.h>
62 #include "lod_internal.h"
63
64 #define pool_tgt(_p, _i) OST_TGT(lu2lod_dev((_p)->pool_lobd->obd_lu_dev), \
65                                  (_p)->pool_obds.op_array[_i])
66
67 /**
68  * Get a reference on the specified pool.
69  *
70  * To ensure the pool descriptor is not freed before the caller is finished
71  * with it.  Any process that is accessing \a pool directly needs to hold
72  * reference on it, including /proc since a userspace thread may be holding
73  * the /proc file open and busy in the kernel.
74  *
75  * \param[in] pool      pool descriptor on which to gain reference
76  */
77 static void pool_getref(struct pool_desc *pool)
78 {
79         CDEBUG(D_INFO, "pool %p\n", pool);
80         atomic_inc(&pool->pool_refcount);
81 }
82
83 /**
84  * Drop a reference on the specified pool and free its memory if needed.
85  *
86  * One reference is held by the LOD OBD device while it is configured, from
87  * the time the configuration log defines the pool until the time when it is
88  * dropped when the LOD OBD is cleaned up or the pool is deleted.  This means
89  * that the pool will not be freed while the LOD device is configured, unless
90  * it is explicitly destroyed by the sysadmin.  The pool structure is freed
91  * after the last reference on the structure is released.
92  *
93  * \param[in] pool      pool descriptor to drop reference on and possibly free
94  */
95 void lod_pool_putref(struct pool_desc *pool)
96 {
97         CDEBUG(D_INFO, "pool %p\n", pool);
98         if (atomic_dec_and_test(&pool->pool_refcount)) {
99                 LASSERT(cfs_hlist_unhashed(&pool->pool_hash));
100                 LASSERT(cfs_list_empty(&pool->pool_list));
101                 LASSERT(pool->pool_proc_entry == NULL);
102                 lod_ost_pool_free(&(pool->pool_rr.lqr_pool));
103                 lod_ost_pool_free(&(pool->pool_obds));
104                 OBD_FREE_PTR(pool);
105                 EXIT;
106         }
107 }
108
109 /**
110  * Drop the refcount in cases where the caller holds a spinlock.
111  *
112  * This is needed if the caller cannot be blocked while freeing memory.
113  * It assumes that there is some other known refcount held on the \a pool
114  * and the memory cannot actually be freed, but the refcounting needs to
115  * be kept accurate.
116  *
117  * \param[in] pool      pool descriptor on which to drop reference
118  */
119 static void pool_putref_locked(struct pool_desc *pool)
120 {
121         CDEBUG(D_INFO, "pool %p\n", pool);
122         LASSERT(atomic_read(&pool->pool_refcount) > 1);
123
124         atomic_dec(&pool->pool_refcount);
125 }
126
127 /*
128  * Group of functions needed for cfs_hash implementation.  This
129  * includes pool lookup, refcounting, and cleanup.
130  */
131
132 /**
133  * Hash the pool name for use by the cfs_hash handlers.
134  *
135  * Use the standard DJB2 hash function for ASCII strings in Lustre.
136  *
137  * \param[in] hash_body hash structure where this key is embedded (unused)
138  * \param[in] key       key to be hashed (in this case the pool name)
139  * \param[in] mask      bitmask to limit the hash value to the desired size
140  *
141  * \retval              computed hash value from \a key and limited by \a mask
142  */
143 static __u32 pool_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
144 {
145         return cfs_hash_djb2_hash(key, strnlen(key, LOV_MAXPOOLNAME), mask);
146 }
147
148 /**
149  * Return the actual key (pool name) from the hashed \a hnode.
150  *
151  * Allows extracting the key name when iterating over all hash entries.
152  *
153  * \param[in] hnode     hash node found by lookup or iteration
154  *
155  * \retval              char array referencing the pool name (no refcount)
156  */
157 static void *pool_key(cfs_hlist_node_t *hnode)
158 {
159         struct pool_desc *pool;
160
161         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
162         return pool->pool_name;
163 }
164
165 /**
166  * Check if the specified hash key matches the hash node.
167  *
168  * This is needed in case there is a hash key collision, allowing the hash
169  * table lookup/iteration to distinguish between the two entries.
170  *
171  * \param[in] key       key (pool name) being searched for
172  * \param[in] compared  current entry being compared
173  *
174  * \retval              0 if \a key is the same as the key of \a compared
175  * \retval              1 if \a key is different from the key of \a compared
176  */
177 static int pool_hashkey_keycmp(const void *key, cfs_hlist_node_t *compared)
178 {
179         return !strncmp(key, pool_key(compared), LOV_MAXPOOLNAME);
180 }
181
182 /**
183  * Return the actual pool data structure from the hash table entry.
184  *
185  * Once the hash table entry is found, extract the pool data from it.
186  * The return type of this function is void * because it needs to be
187  * assigned to the generic hash operations table.
188  *
189  * \param[in] hnode     hash table entry
190  *
191  * \retval              struct pool_desc for the specified \a hnode
192  */
193 static void *pool_hashobject(cfs_hlist_node_t *hnode)
194 {
195         return cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
196 }
197
198 static void pool_hashrefcount_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
199 {
200         struct pool_desc *pool;
201
202         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
203         pool_getref(pool);
204 }
205
206 static void pool_hashrefcount_put_locked(cfs_hash_t *hs,
207                                          cfs_hlist_node_t *hnode)
208 {
209         struct pool_desc *pool;
210
211         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
212         pool_putref_locked(pool);
213 }
214
215 cfs_hash_ops_t pool_hash_operations = {
216         .hs_hash        = pool_hashfn,
217         .hs_key         = pool_key,
218         .hs_keycmp      = pool_hashkey_keycmp,
219         .hs_object      = pool_hashobject,
220         .hs_get         = pool_hashrefcount_get,
221         .hs_put_locked  = pool_hashrefcount_put_locked,
222 };
223
224 /*
225  * Methods for /proc seq_file iteration of the defined pools.
226  */
227
228 #define POOL_IT_MAGIC 0xB001CEA0
229 struct lod_pool_iterator {
230         int               lpi_magic;    /* POOL_IT_MAGIC */
231         int               lpi_idx;      /* from 0 to pool_tgt_size - 1 */
232         struct pool_desc *lpi_pool;
233 };
234
235 /**
236  * Return the next configured target within one pool for seq_file iteration.
237  *
238  * Iterator is used to go through the target entries of a single pool
239  * (i.e. the list of OSTs configured for a named pool).
240  * lpi_idx is the current target index in the pool's op_array[].
241  *
242  * The return type is a void * because this function is one of the
243  * struct seq_operations methods and must match the function template.
244  *
245  * \param[in] seq       /proc sequence file iteration tracking structure
246  * \param[in] v         unused
247  * \param[in] pos       position within iteration; 0 to number of targets - 1
248  *
249  * \retval      struct pool_iterator of the next pool descriptor
250  */
251 static void *pool_proc_next(struct seq_file *seq, void *v, loff_t *pos)
252 {
253         struct lod_pool_iterator *iter = seq->private;
254         int prev_idx;
255
256         LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X\n", iter->lpi_magic);
257
258         /* test if end of file */
259         if (*pos >= pool_tgt_count(iter->lpi_pool))
260                 return NULL;
261
262         /* iterate to find a non empty entry */
263         prev_idx = iter->lpi_idx;
264         down_read(&pool_tgt_rw_sem(iter->lpi_pool));
265         iter->lpi_idx++;
266         if (iter->lpi_idx == pool_tgt_count(iter->lpi_pool)) {
267                 iter->lpi_idx = prev_idx; /* we stay on the last entry */
268                 up_read(&pool_tgt_rw_sem(iter->lpi_pool));
269                 return NULL;
270         }
271         up_read(&pool_tgt_rw_sem(iter->lpi_pool));
272         (*pos)++;
273         /* return != NULL to continue */
274         return iter;
275 }
276
277 /**
278  * Start seq_file iteration via /proc for a single pool.
279  *
280  * The \a pos parameter may be non-zero, indicating that the iteration
281  * is starting at some offset in the target list.  Use the seq_file
282  * private field to memorize the iterator so we can free it at stop().
283  * Need to restore the private pointer to the pool before freeing it.
284  *
285  * \param[in] seq       new sequence file structure to initialize
286  * \param[in] pos       initial target number at which to start iteration
287  *
288  * \retval              initialized pool iterator private structure
289  * \retval              NULL if \a pos exceeds the number of targets in \a pool
290  * \retval              negative error number on failure
291  */
292 static void *pool_proc_start(struct seq_file *seq, loff_t *pos)
293 {
294         struct pool_desc *pool = seq->private;
295         struct lod_pool_iterator *iter;
296
297         pool_getref(pool);
298         if ((pool_tgt_count(pool) == 0) ||
299             (*pos >= pool_tgt_count(pool))) {
300                 /* iter is not created, so stop() has no way to
301                  * find pool to dec ref */
302                 lod_pool_putref(pool);
303                 return NULL;
304         }
305
306         OBD_ALLOC_PTR(iter);
307         if (iter == NULL)
308                 return ERR_PTR(-ENOMEM);
309         iter->lpi_magic = POOL_IT_MAGIC;
310         iter->lpi_pool = pool;
311         iter->lpi_idx = 0;
312
313         seq->private = iter;
314         if (*pos > 0) {
315                 loff_t i;
316                 void *ptr;
317
318                 i = 0;
319                 do {
320                         ptr = pool_proc_next(seq, &iter, &i);
321                 } while ((i < *pos) && (ptr != NULL));
322
323                 return ptr;
324         }
325
326         return iter;
327 }
328
329 /**
330  * Finish seq_file iteration for a single pool.
331  *
332  * Once iteration has been completed, the pool_iterator struct must be
333  * freed, and the seq_file private pointer restored to the pool, as it
334  * was initially when pool_proc_start() was called.
335  *
336  * In some cases the stop() method may be called 2 times, without calling
337  * the start() method (see seq_read() from fs/seq_file.c). We have to free
338  * the private iterator struct only if seq->private points to the iterator.
339  *
340  * \param[in] seq       sequence file structure to clean up
341  * \param[in] v         (unused)
342  */
343 static void pool_proc_stop(struct seq_file *seq, void *v)
344 {
345         struct lod_pool_iterator *iter = seq->private;
346
347         if (iter != NULL && iter->lpi_magic == POOL_IT_MAGIC) {
348                 seq->private = iter->lpi_pool;
349                 lod_pool_putref(iter->lpi_pool);
350                 OBD_FREE_PTR(iter);
351         }
352 }
353
354 /**
355  * Print out one target entry from the pool for seq_file iteration.
356  *
357  * The currently referenced pool target is given by op_array[lpi_idx].
358  *
359  * \param[in] seq       new sequence file structure to initialize
360  * \param[in] v         (unused)
361  */
362 static int pool_proc_show(struct seq_file *seq, void *v)
363 {
364         struct lod_pool_iterator *iter = v;
365         struct lod_tgt_desc  *tgt;
366
367         LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X\n", iter->lpi_magic);
368         LASSERT(iter->lpi_pool != NULL);
369         LASSERT(iter->lpi_idx <= pool_tgt_count(iter->lpi_pool));
370
371         down_read(&pool_tgt_rw_sem(iter->lpi_pool));
372         tgt = pool_tgt(iter->lpi_pool, iter->lpi_idx);
373         up_read(&pool_tgt_rw_sem(iter->lpi_pool));
374         if (tgt != NULL)
375                 seq_printf(seq, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
376
377         return 0;
378 }
379
380 static const struct seq_operations pool_proc_ops = {
381         .start  = pool_proc_start,
382         .next   = pool_proc_next,
383         .stop   = pool_proc_stop,
384         .show   = pool_proc_show,
385 };
386
387 /**
388  * Open a new /proc file for seq_file iteration of targets in one pool.
389  *
390  * Initialize the seq_file private pointer to reference the pool.
391  *
392  * \param inode inode to store iteration state for /proc
393  * \param file  file descriptor to store iteration methods
394  *
395  * \retval      0 for success
396  * \retval      negative error number on failure
397  */
398 static int pool_proc_open(struct inode *inode, struct file *file)
399 {
400         int rc;
401
402         rc = seq_open(file, &pool_proc_ops);
403         if (!rc) {
404                 struct seq_file *seq = file->private_data;
405                 seq->private = PDE_DATA(inode);
406         }
407         return rc;
408 }
409
410 static struct file_operations pool_proc_operations = {
411         .open           = pool_proc_open,
412         .read           = seq_read,
413         .llseek         = seq_lseek,
414         .release        = seq_release,
415 };
416
417 /**
418  * Dump the pool target list into the Lustre debug log.
419  *
420  * This is a debugging function to allow dumping the list of targets
421  * in \a pool to the Lustre kernel debug log at the given \a level.
422  *
423  * This is not currently called by any existing code, but can be called
424  * from within gdb/crash to display the contents of the pool, or from
425  * code under development.
426  *
427  * \param[in] level     Lustre debug level (D_INFO, D_WARN, D_ERROR, etc)
428  * \param[in] pool      pool descriptor to be dumped
429  */
430 void lod_dump_pool(int level, struct pool_desc *pool)
431 {
432         int i;
433
434         pool_getref(pool);
435
436         CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
437                pool->pool_name, pool->pool_obds.op_count);
438         down_read(&pool_tgt_rw_sem(pool));
439
440         for (i = 0; i < pool_tgt_count(pool) ; i++) {
441                 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
442                         continue;
443                 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
444                        pool->pool_name, i,
445                        obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
446         }
447
448         up_read(&pool_tgt_rw_sem(pool));
449         lod_pool_putref(pool);
450 }
451
452 /**
453  * Initialize the pool data structures at startup.
454  *
455  * Allocate and initialize the pool data structures with the specified
456  * array size.  If pool count is not specified (\a count == 0), then
457  * POOL_INIT_COUNT will be used.  Allocating a non-zero initial array
458  * size avoids the need to reallocate as new pools are added.
459  *
460  * \param[in] op        pool structure
461  * \param[in] count     initial size of the target op_array[] array
462  *
463  * \retval              0 indicates successful pool initialization
464  * \retval              negative error number on failure
465  */
466 #define POOL_INIT_COUNT 2
467 int lod_ost_pool_init(struct ost_pool *op, unsigned int count)
468 {
469         ENTRY;
470
471         if (count == 0)
472                 count = POOL_INIT_COUNT;
473         op->op_array = NULL;
474         op->op_count = 0;
475         init_rwsem(&op->op_rw_sem);
476         op->op_size = count;
477         OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
478         if (op->op_array == NULL) {
479                 op->op_size = 0;
480                 RETURN(-ENOMEM);
481         }
482         EXIT;
483         return 0;
484 }
485
486 /**
487  * Increase the op_array size to hold more targets in this pool.
488  *
489  * The size is increased to at least \a min_count, but may be larger
490  * for an existing pool since ->op_array[] is growing exponentially.
491  * Caller must hold write op_rwlock.
492  *
493  * \param[in] op        pool structure
494  * \param[in] min_count minimum number of entries to handle
495  *
496  * \retval              0 on success
497  * \retval              negative error number on failure.
498  */
499 int lod_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
500 {
501         __u32 *new;
502         int new_size;
503
504         LASSERT(min_count != 0);
505
506         if (op->op_count < op->op_size)
507                 return 0;
508
509         new_size = max(min_count, 2 * op->op_size);
510         OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
511         if (new == NULL)
512                 return -ENOMEM;
513
514         /* copy old array to new one */
515         memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
516         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
517         op->op_array = new;
518         op->op_size = new_size;
519
520         return 0;
521 }
522
523 /**
524  * Add a new target to an existing pool.
525  *
526  * Add a new target device to the pool previously created and returned by
527  * lod_pool_new().  Each target can only be in each pool at most one time.
528  *
529  * \param[in] op        target pool to add new entry
530  * \param[in] idx       pool index number to add to the \a op array
531  * \param[in] min_count minimum number of entries to expect in the pool
532  *
533  * \retval              0 if target could be added to the pool
534  * \retval              negative error if target \a idx was not added
535  */
536 int lod_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
537 {
538         int rc = 0, i;
539         ENTRY;
540
541         down_write(&op->op_rw_sem);
542
543         rc = lod_ost_pool_extend(op, min_count);
544         if (rc)
545                 GOTO(out, rc);
546
547         /* search ost in pool array */
548         for (i = 0; i < op->op_count; i++) {
549                 if (op->op_array[i] == idx)
550                         GOTO(out, rc = -EEXIST);
551         }
552         /* ost not found we add it */
553         op->op_array[op->op_count] = idx;
554         op->op_count++;
555         EXIT;
556 out:
557         up_write(&op->op_rw_sem);
558         return rc;
559 }
560
561 /**
562  * Remove an existing pool from the system.
563  *
564  * The specified pool must have previously been allocated by
565  * lod_pool_new() and not have any target members in the pool.
566  * If the removed target is not the last, compact the array
567  * to remove empty spaces.
568  *
569  * \param[in] op        pointer to the original data structure
570  * \param[in] idx       target index to be removed
571  *
572  * \retval              0 on success
573  * \retval              negative error number on failure
574  */
575 int lod_ost_pool_remove(struct ost_pool *op, __u32 idx)
576 {
577         int i;
578         ENTRY;
579
580         down_write(&op->op_rw_sem);
581
582         for (i = 0; i < op->op_count; i++) {
583                 if (op->op_array[i] == idx) {
584                         memmove(&op->op_array[i], &op->op_array[i + 1],
585                                 (op->op_count - i - 1) *
586                                 sizeof(op->op_array[0]));
587                         op->op_count--;
588                         up_write(&op->op_rw_sem);
589                         EXIT;
590                         return 0;
591                 }
592         }
593
594         up_write(&op->op_rw_sem);
595         RETURN(-EINVAL);
596 }
597
598 /**
599  * Free the pool after it was emptied and removed from /proc.
600  *
601  * Note that all of the child/target entries referenced by this pool
602  * must have been removed by lod_ost_pool_remove() before it can be
603  * deleted from memory.
604  *
605  * \param[in] op        pool to be freed.
606  *
607  * \retval              0 on success or if pool was already freed
608  */
609 int lod_ost_pool_free(struct ost_pool *op)
610 {
611         ENTRY;
612
613         if (op->op_size == 0)
614                 RETURN(0);
615
616         down_write(&op->op_rw_sem);
617
618         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
619         op->op_array = NULL;
620         op->op_count = 0;
621         op->op_size = 0;
622
623         up_write(&op->op_rw_sem);
624         RETURN(0);
625 }
626
627 /**
628  * Allocate a new pool for the specified device.
629  *
630  * Allocate a new pool_desc structure for the specified \a new_pool
631  * device to create a pool with the given \a poolname.  The new pool
632  * structure is created with a single reference, and is freed when the
633  * reference count drops to zero.
634  *
635  * \param[in] obd       Lustre OBD device on which to add a pool iterator
636  * \param[in] poolname  the name of the pool to be created
637  *
638  * \retval              0 in case of success
639  * \retval              negative error code in case of error
640  */
641 int lod_pool_new(struct obd_device *obd, char *poolname)
642 {
643         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
644         struct pool_desc  *new_pool;
645         int rc;
646         ENTRY;
647
648         if (strlen(poolname) > LOV_MAXPOOLNAME)
649                 RETURN(-ENAMETOOLONG);
650
651         OBD_ALLOC_PTR(new_pool);
652         if (new_pool == NULL)
653                 RETURN(-ENOMEM);
654
655         strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
656         new_pool->pool_lobd = obd;
657         atomic_set(&new_pool->pool_refcount, 1);
658         rc = lod_ost_pool_init(&new_pool->pool_obds, 0);
659         if (rc)
660                 GOTO(out_err, rc);
661
662         memset(&new_pool->pool_rr, 0, sizeof(new_pool->pool_rr));
663         rc = lod_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
664         if (rc)
665                 GOTO(out_free_pool_obds, rc);
666
667         INIT_HLIST_NODE(&new_pool->pool_hash);
668
669 #ifdef LPROCFS
670         pool_getref(new_pool);
671         new_pool->pool_proc_entry = lprocfs_add_simple(lod->lod_pool_proc_entry,
672                                                        poolname,
673 #ifndef HAVE_ONLY_PROCFS_SEQ
674                                                        NULL, NULL,
675 #endif
676                                                        new_pool,
677                                                        &pool_proc_operations);
678         if (IS_ERR(new_pool->pool_proc_entry)) {
679                 CDEBUG(D_CONFIG, "%s: cannot add proc entry "LOV_POOLNAMEF"\n",
680                        obd->obd_name, poolname);
681                 new_pool->pool_proc_entry = NULL;
682                 lod_pool_putref(new_pool);
683         }
684         CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool,
685                new_pool->pool_proc_entry);
686 #endif
687
688         spin_lock(&obd->obd_dev_lock);
689         cfs_list_add_tail(&new_pool->pool_list, &lod->lod_pool_list);
690         lod->lod_pool_count++;
691         spin_unlock(&obd->obd_dev_lock);
692
693         /* add to find only when it fully ready  */
694         rc = cfs_hash_add_unique(lod->lod_pools_hash_body, poolname,
695                                  &new_pool->pool_hash);
696         if (rc)
697                 GOTO(out_err, rc = -EEXIST);
698
699         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
700                         poolname, lod->lod_pool_count);
701
702         RETURN(0);
703
704 out_err:
705         spin_lock(&obd->obd_dev_lock);
706         cfs_list_del_init(&new_pool->pool_list);
707         lod->lod_pool_count--;
708         spin_unlock(&obd->obd_dev_lock);
709
710         lprocfs_remove(&new_pool->pool_proc_entry);
711
712         lod_ost_pool_free(&new_pool->pool_rr.lqr_pool);
713 out_free_pool_obds:
714         lod_ost_pool_free(&new_pool->pool_obds);
715         OBD_FREE_PTR(new_pool);
716         return rc;
717 }
718
719 /**
720  * Remove the named pool from the OBD device.
721  *
722  * \param[in] obd       OBD device on which pool was previously created
723  * \param[in] poolname  name of pool to remove from \a obd
724  *
725  * \retval              0 on successfully removing the pool
726  * \retval              negative error numbers for failures
727  */
728 int lod_pool_del(struct obd_device *obd, char *poolname)
729 {
730         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
731         struct pool_desc  *pool;
732         ENTRY;
733
734         /* lookup and kill hash reference */
735         pool = cfs_hash_del_key(lod->lod_pools_hash_body, poolname);
736         if (pool == NULL)
737                 RETURN(-ENOENT);
738
739         if (pool->pool_proc_entry != NULL) {
740                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
741                 lprocfs_remove(&pool->pool_proc_entry);
742                 lod_pool_putref(pool);
743         }
744
745         spin_lock(&obd->obd_dev_lock);
746         cfs_list_del_init(&pool->pool_list);
747         lod->lod_pool_count--;
748         spin_unlock(&obd->obd_dev_lock);
749
750         /* release last reference */
751         lod_pool_putref(pool);
752
753         RETURN(0);
754 }
755
756 /**
757  * Add a single target device to the named pool.
758  *
759  * Add the target specified by \a ostname to the specified \a poolname.
760  *
761  * \param[in] obd       OBD device on which to add the pool
762  * \param[in] poolname  name of the pool to which to add the target \a ostname
763  * \param[in] ostname   name of the target device to be added
764  *
765  * \retval              0 if \a ostname was (previously) added to the named pool
766  * \retval              negative error number on failure
767  */
768 int lod_pool_add(struct obd_device *obd, char *poolname, char *ostname)
769 {
770         struct lod_device       *lod = lu2lod_dev(obd->obd_lu_dev);
771         struct obd_uuid          ost_uuid;
772         struct pool_desc        *pool;
773         unsigned int             idx;
774         int                      rc = -EINVAL;
775         ENTRY;
776
777         pool = cfs_hash_lookup(lod->lod_pools_hash_body, poolname);
778         if (pool == NULL)
779                 RETURN(-ENOENT);
780
781         obd_str2uuid(&ost_uuid, ostname);
782
783         /* search ost in lod array */
784         lod_getref(&lod->lod_ost_descs);
785         lod_foreach_ost(lod, idx) {
786                 if (obd_uuid_equals(&ost_uuid, &OST_TGT(lod, idx)->ltd_uuid)) {
787                         rc = 0;
788                         break;
789                 }
790         }
791
792         if (rc)
793                 GOTO(out, rc);
794
795         rc = lod_ost_pool_add(&pool->pool_obds, idx, lod->lod_osts_size);
796         if (rc)
797                 GOTO(out, rc);
798
799         pool->pool_rr.lqr_dirty = 1;
800
801         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
802                         ostname, poolname,  pool_tgt_count(pool));
803
804         EXIT;
805 out:
806         lod_putref(lod, &lod->lod_ost_descs);
807         lod_pool_putref(pool);
808         return rc;
809 }
810
811 /**
812  * Remove the named target from the specified pool.
813  *
814  * Remove one target named \a ostname from \a poolname.  The \a ostname
815  * is searched for in the lod_device lod_ost_bitmap array, to ensure the
816  * specified name actually exists in the pool.
817  *
818  * \param[in] obd       OBD device from which to remove \a poolname
819  * \param[in] poolname  name of the pool to be changed
820  * \param[in] ostname   name of the target to remove from \a poolname
821  *
822  * \retval              0 on successfully removing \a ostname from the pool
823  * \retval              negative number on error (e.g. \a ostname not in pool)
824  */
825 int lod_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
826 {
827         struct lod_device       *lod = lu2lod_dev(obd->obd_lu_dev);
828         struct obd_uuid          ost_uuid;
829         struct pool_desc        *pool;
830         unsigned int             idx;
831         int                      rc = -EINVAL;
832         ENTRY;
833
834         pool = cfs_hash_lookup(lod->lod_pools_hash_body, poolname);
835         if (pool == NULL)
836                 RETURN(-ENOENT);
837
838         obd_str2uuid(&ost_uuid, ostname);
839
840         lod_getref(&lod->lod_ost_descs);
841         cfs_foreach_bit(lod->lod_ost_bitmap, idx) {
842                 if (obd_uuid_equals(&ost_uuid, &OST_TGT(lod, idx)->ltd_uuid)) {
843                         rc = 0;
844                         break;
845                 }
846         }
847
848         /* test if ost found in lod array */
849         if (rc)
850                 GOTO(out, rc);
851
852         lod_ost_pool_remove(&pool->pool_obds, idx);
853
854         pool->pool_rr.lqr_dirty = 1;
855
856         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
857                poolname);
858
859         EXIT;
860 out:
861         lod_putref(lod, &lod->lod_ost_descs);
862         lod_pool_putref(pool);
863         return rc;
864 }
865
866 /**
867  * Check if the specified target exists in the pool.
868  *
869  * The caller may not have a reference on \a pool if it got the pool without
870  * calling lod_find_pool() (e.g. directly from the lod pool list)
871  *
872  * \param[in] idx       Target index to check
873  * \param[in] pool      Pool in which to check if target is added.
874  *
875  * \retval              0 successfully found index in \a pool
876  * \retval              negative error if device not found in \a pool
877  */
878 int lod_check_index_in_pool(__u32 idx, struct pool_desc *pool)
879 {
880         int i, rc;
881         ENTRY;
882
883         pool_getref(pool);
884
885         down_read(&pool_tgt_rw_sem(pool));
886
887         for (i = 0; i < pool_tgt_count(pool); i++) {
888                 if (pool_tgt_array(pool)[i] == idx)
889                         GOTO(out, rc = 0);
890         }
891         rc = -ENOENT;
892         EXIT;
893 out:
894         up_read(&pool_tgt_rw_sem(pool));
895
896         lod_pool_putref(pool);
897         return rc;
898 }
899
900 /**
901  * Find the pool descriptor for the specified pool and return it with a
902  * reference to the caller if found.
903  *
904  * \param[in] lod       LOD on which the pools are configured
905  * \param[in] poolname  NUL-terminated name of the pool
906  *
907  * \retval      pointer to pool descriptor on success
908  * \retval      NULL if \a poolname could not be found or poolname is empty
909  */
910 struct pool_desc *lod_find_pool(struct lod_device *lod, char *poolname)
911 {
912         struct pool_desc *pool;
913
914         pool = NULL;
915         if (poolname[0] != '\0') {
916                 pool = cfs_hash_lookup(lod->lod_pools_hash_body, poolname);
917                 if (pool == NULL)
918                         CDEBUG(D_CONFIG, "%s: request for an unknown pool ("
919                                LOV_POOLNAMEF")\n",
920                                lod->lod_child_exp->exp_obd->obd_name, poolname);
921                 if (pool != NULL && pool_tgt_count(pool) == 0) {
922                         CDEBUG(D_CONFIG, "%s: request for an empty pool ("
923                                LOV_POOLNAMEF")\n",
924                                lod->lod_child_exp->exp_obd->obd_name, poolname);
925                         /* pool is ignored, so we remove ref on it */
926                         lod_pool_putref(pool);
927                         pool = NULL;
928                 }
929         }
930         return pool;
931 }
932