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