Whamcloud - gitweb
LU-4974 lod: Change pool_desc to "[lod|lov]_pool_desc"
[fs/lustre-release.git] / lustre / lov / lov_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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/lov/lov_pool.c
32  *
33  * OST pool methods
34  *
35  * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
36  * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
37  * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
38  */
39
40 #define DEBUG_SUBSYSTEM S_LOV
41
42 #include <libcfs/libcfs.h>
43 #include <libcfs/linux/linux-hash.h>
44 #include <libcfs/linux/linux-fs.h>
45
46 #include <obd.h>
47 #include "lov_internal.h"
48
49 #define pool_tgt(_p, _i) \
50                 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
51
52 /**
53  * Hash the pool name for use by the hashtable handlers.
54  *
55  * \param[in] data      poolname (null-terminated string to be hashed or key)
56  * \param[in] len       length of key
57  * \param[in] seed      Random seed or previous hash
58  *
59  * \retval              computed hash value of the key(poolname)
60  */
61 static u32 pool_hashfh(const void *data, u32 len, u32 seed)
62 {
63         const char *pool_name = data;
64
65         return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
66                                                pool_name));
67 }
68
69 /**
70  * Compare the pool name with key
71  *
72  * \param[in] arg       key (poolname) to compare against
73  * \param[in] obj       Entry that is being compared
74  *
75  * \retval              0 if matched
76  * \retval              1 if not matched
77  */
78 static int pool_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
79 {
80         const struct lov_pool_desc *pool = obj;
81         const char *pool_name = arg->key;
82
83         return strcmp(pool_name, pool->pool_name);
84 }
85
86 static const struct rhashtable_params pools_hash_params = {
87         .key_len        = 1, /* actually variable */
88         .key_offset     = offsetof(struct lov_pool_desc, pool_name),
89         .head_offset    = offsetof(struct lov_pool_desc, pool_hash),
90         .hashfn         = pool_hashfh,
91         .obj_cmpfn      = pool_cmpfn,
92         .automatic_shrinking = true,
93 };
94
95 /**
96  * Get a reference on the specified lov pool.
97  *
98  * To ensure the pool descriptor is not freed before the caller is finished
99  * with it.  Any process that is accessing \a pool directly needs to hold
100  * reference on it, including /proc since a userspace thread may be holding
101  * the /proc file open and busy in the kernel.
102  *
103  * \param[in] pool      pool descriptor on which to gain reference
104  */
105 static void lov_pool_getref(struct lov_pool_desc *pool)
106 {
107         CDEBUG(D_INFO, "pool %p\n", pool);
108         atomic_inc(&pool->pool_refcount);
109 }
110
111 /**
112  * Drop a reference on the specified lov pool and free its memory if needed
113  *
114  * One reference is held by the LOD OBD device while it is configured, from
115  * the time the configuration log defines the pool until the time when it is
116  * dropped when the LOD OBD is cleaned up or the pool is deleted.  This means
117  * that the pool will not be freed while the LOD device is configured, unless
118  * it is explicitly destroyed by the sysadmin.  The pool structure is freed
119  * after the last reference on the structure is released.
120  *
121  * \param[in] pool      pool descriptor to drop reference on and possibly free
122  */
123 void lov_pool_putref(struct lov_pool_desc *pool)
124 {
125         CDEBUG(D_INFO, "pool %p\n", pool);
126         if (atomic_dec_and_test(&pool->pool_refcount)) {
127                 LASSERT(list_empty(&pool->pool_list));
128                 LASSERT(pool->pool_proc_entry == NULL);
129                 lu_tgt_pool_free(&(pool->pool_obds));
130                 kfree_rcu(pool, pool_rcu);
131                 EXIT;
132         }
133 }
134
135 #ifdef CONFIG_PROC_FS
136 /*
137  * pool /proc seq_file methods
138  */
139 /*
140  * iterator is used to go through the target pool entries
141  * index is the current entry index in the lp_array[] array
142  * index >= pos returned to the seq_file interface
143  * pos is from 0 to (pool->pool_obds.op_count - 1)
144  */
145 #define POOL_IT_MAGIC 0xB001CEA0
146 struct pool_iterator {
147         int magic; /* POOL_IT_MAGIC */
148         int idx;   /* from 0 to pool_tgt_size - 1 */
149         struct lov_pool_desc *pool;
150 };
151
152 /**
153  * Return the next configured target within one pool for seq_file iteration
154  *
155  * Iterator is used to go through the target entries of a single pool
156  * (i.e. the list of OSTs configured for a named pool).
157  * lpi_idx is the current target index in the pool's op_array[].
158  *
159  * The return type is a void * because this function is one of the
160  * struct seq_operations methods and must match the function template.
161  *
162  * \param[in] seq       /proc sequence file iteration tracking structure
163  * \param[in] v         unused
164  * \param[in] pos       position within iteration; 0 to number of targets - 1
165  *
166  * \retval      struct pool_iterator of the next pool descriptor
167  */
168 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
169 {
170         struct pool_iterator *iter = (struct pool_iterator *)s->private;
171         int prev_idx;
172
173         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
174
175         (*pos)++;
176         /* test if end of file */
177         if (*pos > pool_tgt_count(iter->pool))
178                 return NULL;
179
180         /* iterate to find a non empty entry */
181         prev_idx = iter->idx;
182         iter->idx++;
183         if (iter->idx >= pool_tgt_count(iter->pool)) {
184                 iter->idx = prev_idx; /* we stay on the last entry */
185                 return NULL;
186         }
187         /* return != NULL to continue */
188         return iter;
189 }
190
191 /**
192  * Start seq_file iteration via /proc for a single pool
193  *
194  * The \a pos parameter may be non-zero, indicating that the iteration
195  * is starting at some offset in the target list.  Use the seq_file
196  * private field to memorize the iterator so we can free it at stop().
197  * Need to restore the private pointer to the pool before freeing it.
198  *
199  * \param[in] seq       new sequence file structure to initialize
200  * \param[in] pos       initial target number at which to start iteration
201  *
202  * \retval              initialized pool iterator private structure
203  * \retval              NULL if \a pos exceeds the number of targets in \a pool
204  * \retval              negative error number on failure
205  */
206 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
207 {
208         struct lov_pool_desc *pool = (struct lov_pool_desc *)s->private;
209         struct pool_iterator *iter;
210
211         lov_pool_getref(pool);
212         if ((pool_tgt_count(pool) == 0) || (*pos >= pool_tgt_count(pool))) {
213                 /* iter is not created, so stop() has no way to
214                  * find pool to dec ref */
215                 lov_pool_putref(pool);
216                 return NULL;
217         }
218
219         OBD_ALLOC_PTR(iter);
220         if (!iter)
221                 return ERR_PTR(-ENOMEM);
222         iter->magic = POOL_IT_MAGIC;
223         iter->pool = pool;
224         iter->idx = 0;
225
226         /* we use seq_file private field to memorized iterator so
227          * we can free it at stop() */
228         /* /!\ do not forget to restore it to pool before freeing it */
229         s->private = iter;
230         down_read(&pool_tgt_rw_sem(pool));
231         if (*pos > 0) {
232                 loff_t i;
233                 void *ptr;
234
235                 i = 0;
236                 do {
237                         ptr = pool_proc_next(s, &iter, &i);
238                 } while ((i < *pos) && (ptr != NULL));
239                 return ptr;
240         }
241         return iter;
242 }
243
244 /**
245  * Finish seq_file iteration for a single pool
246  *
247  * Once iteration has been completed, the pool_iterator struct must be
248  * freed, and the seq_file private pointer restored to the pool, as it
249  * was initially when pool_proc_start() was called.
250  *
251  * In some cases the stop() method may be called 2 times, without calling
252  * the start() method (see seq_read() from fs/seq_file.c). We have to free
253  * the private iterator struct only if seq->private points to the iterator.
254  *
255  * \param[in] seq       sequence file structure to clean up
256  * \param[in] v         (unused)
257  */
258 static void pool_proc_stop(struct seq_file *s, void *v)
259 {
260         struct pool_iterator *iter = (struct pool_iterator *)s->private;
261
262         /* in some cases stop() method is called 2 times, without
263          * calling start() method (see seq_read() from fs/seq_file.c)
264          * we have to free only if s->private is an iterator */
265         if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
266                 up_read(&pool_tgt_rw_sem(iter->pool));
267                 /* we restore s->private so next call to pool_proc_start()
268                  * will work */
269                 s->private = iter->pool;
270                 lov_pool_putref(iter->pool);
271                 OBD_FREE_PTR(iter);
272         }
273 }
274
275 /**
276  * Print out one target entry from the pool for seq_file iteration
277  *
278  * The currently referenced pool target is given by op_array[lpi_idx].
279  *
280  * \param[in] seq       new sequence file structure to initialize
281  * \param[in] v         (unused)
282  */
283 static int pool_proc_show(struct seq_file *s, void *v)
284 {
285         struct pool_iterator *iter = (struct pool_iterator *)v;
286         struct lov_tgt_desc *tgt;
287
288         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
289         LASSERT(iter->pool != NULL);
290         LASSERT(iter->idx <= pool_tgt_count(iter->pool));
291
292         tgt = pool_tgt(iter->pool, iter->idx);
293         if (tgt)
294                 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
295
296         return 0;
297 }
298
299 static const struct seq_operations pool_proc_ops = {
300         .start          = pool_proc_start,
301         .next           = pool_proc_next,
302         .stop           = pool_proc_stop,
303         .show           = pool_proc_show,
304 };
305
306 /**
307  * Open a new /proc file for seq_file iteration of targets in one pool
308  *
309  * Initialize the seq_file private pointer to reference the pool.
310  *
311  * \param[in] inode     inode to store iteration state for /proc
312  * \param[in] file      file descriptor to store iteration methods
313  *
314  * \retval      0 for success
315  * \retval      negative error number on failure
316  */
317 static int pool_proc_open(struct inode *inode, struct file *file)
318 {
319         int rc;
320
321         rc = seq_open(file, &pool_proc_ops);
322         if (!rc) {
323                 struct seq_file *s = file->private_data;
324
325                 s->private = pde_data(inode);
326         }
327         return rc;
328 }
329
330 const static struct proc_ops pool_proc_operations = {
331         .proc_open      = pool_proc_open,
332         .proc_read      = seq_read,
333         .proc_lseek     = seq_lseek,
334         .proc_release   = seq_release,
335 };
336 #endif /* CONFIG_PROC_FS */
337
338 static void pools_hash_exit(void *vpool, void *data)
339 {
340         struct lov_pool_desc *pool = vpool;
341
342         lov_pool_putref(pool);
343 }
344
345 int lov_pool_hash_init(struct rhashtable *tbl)
346 {
347         return rhashtable_init(tbl, &pools_hash_params);
348 }
349
350 void lov_pool_hash_destroy(struct rhashtable *tbl)
351 {
352         rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
353 }
354
355 /**
356  * Allocate a new pool for the specified device
357  *
358  * Allocate a new pool_desc structure for the specified \a new_pool
359  * device to create a pool with the given \a poolname.  The new pool
360  * structure is created with a single refrence, and is freed when the
361  * reference count drops to zero.
362  *
363  * \param[in] obd       Lustre OBD device on which to add a pool iterator
364  * \param[in] poolname  the name of the pool to be created
365  *
366  * \retval              0 in case of success
367  * \retval              negative error code in case of error
368  */
369 int lov_pool_new(struct obd_device *obd, char *poolname)
370 {
371         struct lov_obd *lov;
372         struct lov_pool_desc *new_pool;
373         int rc;
374         ENTRY;
375
376         lov = &(obd->u.lov);
377
378         if (strlen(poolname) > LOV_MAXPOOLNAME)
379                 RETURN(-ENAMETOOLONG);
380
381         /* OBD_ALLOC doesn't work with direct use of kfree_rcu */
382         new_pool = kmalloc(sizeof(*new_pool), GFP_KERNEL);
383         if (new_pool == NULL)
384                 RETURN(-ENOMEM);
385
386         strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
387         new_pool->pool_lobd = obd;
388         /* ref count init to 1 because when created a pool is always used
389          * up to deletion
390          */
391         atomic_set(&new_pool->pool_refcount, 1);
392         rc = lu_tgt_pool_init(&new_pool->pool_obds, 0);
393         if (rc)
394                 GOTO(out_free_pool, rc);
395
396 #ifdef CONFIG_PROC_FS
397         /* get ref for /proc file */
398         lov_pool_getref(new_pool);
399         new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
400                                                        poolname, new_pool,
401                                                        &pool_proc_operations);
402         if (IS_ERR(new_pool->pool_proc_entry)) {
403                 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
404                 new_pool->pool_proc_entry = NULL;
405                 lov_pool_putref(new_pool);
406         }
407         CDEBUG(D_INFO, "pool %p - proc %p\n",
408                new_pool, new_pool->pool_proc_entry);
409 #endif
410
411         spin_lock(&obd->obd_dev_lock);
412         list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
413         lov->lov_pool_count++;
414         spin_unlock(&obd->obd_dev_lock);
415
416         /* Add to hash table only when it is fully ready. */
417         rc = rhashtable_lookup_insert_fast(&lov->lov_pools_hash_body,
418                                            &new_pool->pool_hash,
419                                            pools_hash_params);
420         if (rc) {
421                 if (rc != -EEXIST)
422                         /*
423                          * Hide -E2BIG and -EBUSY which
424                          * are not helpful.
425                          */
426                         rc = -ENOMEM;
427                 GOTO(out_err, rc);
428         }
429
430         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n", poolname,
431                lov->lov_pool_count);
432
433         RETURN(0);
434
435 out_err:
436         spin_lock(&obd->obd_dev_lock);
437         list_del_init(&new_pool->pool_list);
438         lov->lov_pool_count--;
439         spin_unlock(&obd->obd_dev_lock);
440         lprocfs_remove(&new_pool->pool_proc_entry);
441         lu_tgt_pool_free(&new_pool->pool_obds);
442 out_free_pool:
443         OBD_FREE_PTR(new_pool);
444
445         return rc;
446 }
447
448 struct lov_pool_desc *lov_pool_find(struct obd_device *obd, char *poolname)
449 {
450         struct lov_pool_desc *pool;
451         struct lov_obd *lov = &obd->u.lov;
452
453         rcu_read_lock();
454         pool = rhashtable_lookup(&lov->lov_pools_hash_body,
455                                  poolname,
456                                  pools_hash_params);
457         if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
458                 pool = NULL;
459         rcu_read_unlock();
460
461         return pool;
462 }
463
464 /**
465  * Remove the named pool from the OBD device
466  *
467  * \param[in] obd       OBD device on which pool was previously created
468  * \param[in] poolname  name of pool to remove from \a obd
469  *
470  * \retval              0 on successfully removing the pool
471  * \retval              negative error numbers for failures
472  */
473 int lov_pool_del(struct obd_device *obd, char *poolname)
474 {
475         struct lov_obd *lov;
476         struct lov_pool_desc *pool;
477         ENTRY;
478
479         lov = &(obd->u.lov);
480
481         /* lookup and kill hash reference */
482         rcu_read_lock();
483         pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
484                                  pools_hash_params);
485         if (pool && rhashtable_remove_fast(&lov->lov_pools_hash_body,
486                                            &pool->pool_hash,
487                                            pools_hash_params) != 0)
488                 pool = NULL;
489         rcu_read_unlock();
490         if (!pool)
491                 RETURN(-ENOENT);
492
493         if (pool->pool_proc_entry != NULL) {
494                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
495                 lprocfs_remove(&pool->pool_proc_entry);
496                 lov_pool_putref(pool);
497         }
498
499         spin_lock(&obd->obd_dev_lock);
500         list_del_init(&pool->pool_list);
501         lov->lov_pool_count--;
502         spin_unlock(&obd->obd_dev_lock);
503
504         /* release last reference */
505         lov_pool_putref(pool);
506
507         RETURN(0);
508 }
509
510 /**
511  * Add a single target device to the named pool
512  *
513  * Add the target specified by \a ostname to the specified \a poolname.
514  *
515  * \param[in] obd       OBD device on which to add the pool
516  * \param[in] poolname  name of the pool to which to add the target \a ostname
517  * \param[in] ostname   name of the target device to be added
518  *
519  * \retval              0 if \a ostname was (previously) added to the named pool
520  * \retval              negative error number on failure
521  */
522 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
523 {
524         struct obd_uuid ost_uuid;
525         struct lov_obd *lov;
526         struct lov_pool_desc *pool;
527         unsigned int lov_idx;
528         int rc;
529         ENTRY;
530
531         lov = &(obd->u.lov);
532
533         rcu_read_lock();
534         pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
535                                  pools_hash_params);
536         if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
537                 pool = NULL;
538         rcu_read_unlock();
539         if (!pool)
540                 RETURN(-ENOENT);
541
542         obd_str2uuid(&ost_uuid, ostname);
543
544         /* search ost in lov array */
545         lov_tgts_getref(obd);
546         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
547                 if (!lov->lov_tgts[lov_idx])
548                         continue;
549                 if (obd_uuid_equals(&ost_uuid,
550                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
551                         break;
552         }
553         /* test if ost found in lov */
554         if (lov_idx == lov->desc.ld_tgt_count)
555                 GOTO(out, rc = -EINVAL);
556
557         rc = lu_tgt_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
558         if (rc)
559                 GOTO(out, rc);
560
561         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
562                ostname, poolname,  pool_tgt_count(pool));
563
564         EXIT;
565 out:
566         lov_tgts_putref(obd);
567         lov_pool_putref(pool);
568
569         return rc;
570 }
571
572 /**
573  * Remove the named target from the specified pool
574  *
575  * Remove one target named \a ostname from \a poolname.  The \a ostname
576  * is searched for in the lod_device lod_ost_bitmap array, to ensure the
577  * specified name actually exists in the pool.
578  *
579  * \param[in] obd       OBD device from which to remove \a poolname
580  * \param[in] poolname  name of the pool to be changed
581  * \param[in] ostname   name of the target to remove from \a poolname
582  *
583  * \retval              0 on successfully removing \a ostname from the pool
584  * \retval              negative number on error (e.g. \a ostname not in pool)
585  */
586 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
587 {
588         struct obd_uuid ost_uuid;
589         struct lov_obd *lov;
590         struct lov_pool_desc *pool;
591         unsigned int lov_idx;
592         int rc = 0;
593         ENTRY;
594
595         lov = &(obd->u.lov);
596
597         /* lookup and kill hash reference */
598         rcu_read_lock();
599         pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
600                                  pools_hash_params);
601         if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
602                 pool = NULL;
603         rcu_read_unlock();
604         if (!pool)
605                 RETURN(-ENOENT);
606
607         obd_str2uuid(&ost_uuid, ostname);
608
609         lov_tgts_getref(obd);
610         /* search ost in lov array, to get index */
611         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
612                 if (!lov->lov_tgts[lov_idx])
613                         continue;
614
615                 if (obd_uuid_equals(&ost_uuid,
616                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
617                         break;
618         }
619
620         /* test if ost found in lov */
621         if (lov_idx == lov->desc.ld_tgt_count)
622                 GOTO(out, rc = -EINVAL);
623
624         lu_tgt_pool_remove(&pool->pool_obds, lov_idx);
625
626         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
627                poolname);
628
629         EXIT;
630 out:
631         lov_tgts_putref(obd);
632         lov_pool_putref(pool);
633
634         return rc;
635 }