Whamcloud - gitweb
LU-8687 tests: list pool on mds when mgs is separate
[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, 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  * lustre/lov/lov_pool.c
33  *
34  * OST pool methods
35  *
36  * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
37  * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
38  * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LOV
42
43 #include <libcfs/libcfs.h>
44
45 #include <obd.h>
46 #include "lov_internal.h"
47
48 #define pool_tgt(_p, _i) \
49                 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
50
51 static void lov_pool_getref(struct pool_desc *pool)
52 {
53         CDEBUG(D_INFO, "pool %p\n", pool);
54         atomic_inc(&pool->pool_refcount);
55 }
56
57 static void lov_pool_putref(struct pool_desc *pool)
58 {
59         CDEBUG(D_INFO, "pool %p\n", pool);
60         if (atomic_dec_and_test(&pool->pool_refcount)) {
61                 LASSERT(hlist_unhashed(&pool->pool_hash));
62                 LASSERT(list_empty(&pool->pool_list));
63                 LASSERT(pool->pool_proc_entry == NULL);
64                 lov_ost_pool_free(&(pool->pool_obds));
65                 OBD_FREE_PTR(pool);
66                 EXIT;
67         }
68 }
69
70 static void lov_pool_putref_locked(struct pool_desc *pool)
71 {
72         CDEBUG(D_INFO, "pool %p\n", pool);
73         LASSERT(atomic_read(&pool->pool_refcount) > 1);
74
75         atomic_dec(&pool->pool_refcount);
76 }
77
78 /*
79  * hash function using a Rotating Hash algorithm
80  * Knuth, D. The Art of Computer Programming,
81  * Volume 3: Sorting and Searching,
82  * Chapter 6.4.
83  * Addison Wesley, 1973
84  */
85 static __u32 pool_hashfn(struct cfs_hash *hash_body, const void *key,
86                          unsigned mask)
87 {
88         int i;
89         __u32 result;
90         char *poolname;
91
92         result = 0;
93         poolname = (char *)key;
94         for (i = 0; i < LOV_MAXPOOLNAME; i++) {
95                 if (poolname[i] == '\0')
96                         break;
97                 result = (result << 4)^(result >> 28) ^  poolname[i];
98         }
99         return (result % mask);
100 }
101
102 static void *pool_key(struct hlist_node *hnode)
103 {
104         struct pool_desc *pool;
105
106         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
107         return (pool->pool_name);
108 }
109
110 static int
111 pool_hashkey_keycmp(const void *key, struct hlist_node *compared_hnode)
112 {
113         char *pool_name;
114         struct pool_desc *pool;
115
116         pool_name = (char *)key;
117         pool = hlist_entry(compared_hnode, struct pool_desc, pool_hash);
118         return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
119 }
120
121 static void *pool_hashobject(struct hlist_node *hnode)
122 {
123         return hlist_entry(hnode, struct pool_desc, pool_hash);
124 }
125
126 static void pool_hashrefcount_get(struct cfs_hash *hs, struct hlist_node *hnode)
127 {
128         struct pool_desc *pool;
129
130         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
131         lov_pool_getref(pool);
132 }
133
134 static void pool_hashrefcount_put_locked(struct cfs_hash *hs,
135                                          struct hlist_node *hnode)
136 {
137         struct pool_desc *pool;
138
139         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
140         lov_pool_putref_locked(pool);
141 }
142
143 struct cfs_hash_ops pool_hash_operations = {
144         .hs_hash        = pool_hashfn,
145         .hs_key         = pool_key,
146         .hs_keycmp      = pool_hashkey_keycmp,
147         .hs_object      = pool_hashobject,
148         .hs_get         = pool_hashrefcount_get,
149         .hs_put_locked  = pool_hashrefcount_put_locked,
150
151 };
152
153 #ifdef CONFIG_PROC_FS
154 /* ifdef needed for liblustre support */
155 /*
156  * pool /proc seq_file methods
157  */
158 /*
159  * iterator is used to go through the target pool entries
160  * index is the current entry index in the lp_array[] array
161  * index >= pos returned to the seq_file interface
162  * pos is from 0 to (pool->pool_obds.op_count - 1)
163  */
164 #define POOL_IT_MAGIC 0xB001CEA0
165 struct pool_iterator {
166         int magic;
167         struct pool_desc *pool;
168         int idx;        /* from 0 to pool_tgt_size - 1 */
169 };
170
171 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
172 {
173         struct pool_iterator *iter = (struct pool_iterator *)s->private;
174         int prev_idx;
175
176         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
177
178         /* test if end of file */
179         if (*pos >= pool_tgt_count(iter->pool))
180                 return NULL;
181
182         /* iterate to find a non empty entry */
183         prev_idx = iter->idx;
184         down_read(&pool_tgt_rw_sem(iter->pool));
185         iter->idx++;
186         if (iter->idx == pool_tgt_count(iter->pool)) {
187                 iter->idx = prev_idx; /* we stay on the last entry */
188                 up_read(&pool_tgt_rw_sem(iter->pool));
189                 return NULL;
190         }
191         up_read(&pool_tgt_rw_sem(iter->pool));
192         (*pos)++;
193         /* return != NULL to continue */
194         return iter;
195 }
196
197 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
198 {
199         struct pool_desc *pool = (struct pool_desc *)s->private;
200         struct pool_iterator *iter;
201
202         lov_pool_getref(pool);
203         if ((pool_tgt_count(pool) == 0) ||
204             (*pos >= pool_tgt_count(pool))) {
205                 /* iter is not created, so stop() has no way to
206                  * find pool to dec ref */
207                 lov_pool_putref(pool);
208                 return NULL;
209         }
210
211         OBD_ALLOC_PTR(iter);
212         if (!iter)
213                 return ERR_PTR(-ENOMEM);
214         iter->magic = POOL_IT_MAGIC;
215         iter->pool = pool;
216         iter->idx = 0;
217
218         /* we use seq_file private field to memorized iterator so
219          * we can free it at stop() */
220         /* /!\ do not forget to restore it to pool before freeing it */
221         s->private = iter;
222         if (*pos > 0) {
223                 loff_t i;
224                 void *ptr;
225
226                 i = 0;
227                 do {
228                      ptr = pool_proc_next(s, &iter, &i);
229                 } while ((i < *pos) && (ptr != NULL));
230                 return ptr;
231         }
232         return iter;
233 }
234
235 static void pool_proc_stop(struct seq_file *s, void *v)
236 {
237         struct pool_iterator *iter = (struct pool_iterator *)s->private;
238
239         /* in some cases stop() method is called 2 times, without
240          * calling start() method (see seq_read() from fs/seq_file.c)
241          * we have to free only if s->private is an iterator */
242         if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
243                 /* we restore s->private so next call to pool_proc_start()
244                  * will work */
245                 s->private = iter->pool;
246                 lov_pool_putref(iter->pool);
247                 OBD_FREE_PTR(iter);
248         }
249         return;
250 }
251
252 static int pool_proc_show(struct seq_file *s, void *v)
253 {
254         struct pool_iterator *iter = (struct pool_iterator *)v;
255         struct lov_tgt_desc *tgt;
256
257         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
258         LASSERT(iter->pool != NULL);
259         LASSERT(iter->idx <= pool_tgt_count(iter->pool));
260
261         down_read(&pool_tgt_rw_sem(iter->pool));
262         tgt = pool_tgt(iter->pool, iter->idx);
263         up_read(&pool_tgt_rw_sem(iter->pool));
264         if (tgt)
265                 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
266
267         return 0;
268 }
269
270 static struct seq_operations pool_proc_ops = {
271         .start          = pool_proc_start,
272         .next           = pool_proc_next,
273         .stop           = pool_proc_stop,
274         .show           = pool_proc_show,
275 };
276
277 static int pool_proc_open(struct inode *inode, struct file *file)
278 {
279         int rc;
280
281         rc = seq_open(file, &pool_proc_ops);
282         if (!rc) {
283                 struct seq_file *s = file->private_data;
284                 s->private = PDE_DATA(inode);
285         }
286         return rc;
287 }
288
289 static struct file_operations pool_proc_operations = {
290         .open           = pool_proc_open,
291         .read           = seq_read,
292         .llseek         = seq_lseek,
293         .release        = seq_release,
294 };
295 #endif /* CONFIG_PROC_FS */
296
297 void lov_dump_pool(int level, struct pool_desc *pool)
298 {
299         int i;
300
301         lov_pool_getref(pool);
302
303         CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
304                pool->pool_name, pool->pool_obds.op_count);
305         down_read(&pool_tgt_rw_sem(pool));
306
307         for (i = 0; i < pool_tgt_count(pool) ; i++) {
308                 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
309                         continue;
310                 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
311                        pool->pool_name, i,
312                        obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
313         }
314
315         up_read(&pool_tgt_rw_sem(pool));
316         lov_pool_putref(pool);
317 }
318
319 #define LOV_POOL_INIT_COUNT 2
320 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
321 {
322         ENTRY;
323
324         if (count == 0)
325                 count = LOV_POOL_INIT_COUNT;
326         op->op_array = NULL;
327         op->op_count = 0;
328         init_rwsem(&op->op_rw_sem);
329         op->op_size = count;
330         OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
331         if (op->op_array == NULL) {
332                 op->op_size = 0;
333                 RETURN(-ENOMEM);
334         }
335         EXIT;
336         return 0;
337 }
338
339 /* Caller must hold write op_rwlock */
340 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
341 {
342         __u32 *new;
343         int new_size;
344
345         LASSERT(min_count != 0);
346
347         if (op->op_count < op->op_size)
348                 return 0;
349
350         new_size = max(min_count, 2 * op->op_size);
351         OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
352         if (new == NULL)
353                 return -ENOMEM;
354
355         /* copy old array to new one */
356         memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
357         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
358         op->op_array = new;
359         op->op_size = new_size;
360         return 0;
361 }
362
363 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
364 {
365         int rc = 0, i;
366         ENTRY;
367
368         down_write(&op->op_rw_sem);
369
370         rc = lov_ost_pool_extend(op, min_count);
371         if (rc)
372                 GOTO(out, rc);
373
374         /* search ost in pool array */
375         for (i = 0; i < op->op_count; i++) {
376                 if (op->op_array[i] == idx)
377                         GOTO(out, rc = -EEXIST);
378         }
379         /* ost not found we add it */
380         op->op_array[op->op_count] = idx;
381         op->op_count++;
382         EXIT;
383 out:
384         up_write(&op->op_rw_sem);
385         return rc;
386 }
387
388 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
389 {
390         int i;
391         ENTRY;
392
393         down_write(&op->op_rw_sem);
394
395         for (i = 0; i < op->op_count; i++) {
396                 if (op->op_array[i] == idx) {
397                         memmove(&op->op_array[i], &op->op_array[i + 1],
398                                 (op->op_count - i - 1) * sizeof(op->op_array[0]));
399                         op->op_count--;
400                         up_write(&op->op_rw_sem);
401                         EXIT;
402                         return 0;
403                 }
404         }
405
406         up_write(&op->op_rw_sem);
407         RETURN(-EINVAL);
408 }
409
410 int lov_ost_pool_free(struct ost_pool *op)
411 {
412         ENTRY;
413
414         if (op->op_size == 0)
415                 RETURN(0);
416
417         down_write(&op->op_rw_sem);
418
419         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
420         op->op_array = NULL;
421         op->op_count = 0;
422         op->op_size = 0;
423
424         up_write(&op->op_rw_sem);
425         RETURN(0);
426 }
427
428
429 int lov_pool_new(struct obd_device *obd, char *poolname)
430 {
431         struct lov_obd *lov;
432         struct pool_desc *new_pool;
433         int rc;
434         ENTRY;
435
436         lov = &(obd->u.lov);
437
438         if (strlen(poolname) > LOV_MAXPOOLNAME)
439                 RETURN(-ENAMETOOLONG);
440
441         OBD_ALLOC_PTR(new_pool);
442         if (new_pool == NULL)
443                 RETURN(-ENOMEM);
444
445         strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
446         new_pool->pool_lobd = obd;
447         /* ref count init to 1 because when created a pool is always used
448          * up to deletion
449          */
450         atomic_set(&new_pool->pool_refcount, 1);
451         rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
452         if (rc)
453                 GOTO(out_err, rc);
454
455         INIT_HLIST_NODE(&new_pool->pool_hash);
456
457 #ifdef CONFIG_PROC_FS
458         /* get ref for /proc file */
459         lov_pool_getref(new_pool);
460         new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
461                                                        poolname, new_pool,
462                                                        &pool_proc_operations);
463         if (IS_ERR(new_pool->pool_proc_entry)) {
464                 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
465                 new_pool->pool_proc_entry = NULL;
466                 lov_pool_putref(new_pool);
467         }
468         CDEBUG(D_INFO, "pool %p - proc %p\n",
469                new_pool, new_pool->pool_proc_entry);
470 #endif
471
472         spin_lock(&obd->obd_dev_lock);
473         list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
474         lov->lov_pool_count++;
475         spin_unlock(&obd->obd_dev_lock);
476
477         /* add to find only when it fully ready  */
478         rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
479                                  &new_pool->pool_hash);
480         if (rc)
481                 GOTO(out_err, rc = -EEXIST);
482
483         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
484                poolname, lov->lov_pool_count);
485
486         RETURN(0);
487
488 out_err:
489         spin_lock(&obd->obd_dev_lock);
490         list_del_init(&new_pool->pool_list);
491         lov->lov_pool_count--;
492         spin_unlock(&obd->obd_dev_lock);
493         lprocfs_remove(&new_pool->pool_proc_entry);
494         lov_ost_pool_free(&new_pool->pool_obds);
495         OBD_FREE_PTR(new_pool);
496
497         return rc;
498 }
499
500 int lov_pool_del(struct obd_device *obd, char *poolname)
501 {
502         struct lov_obd *lov;
503         struct pool_desc *pool;
504         ENTRY;
505
506         lov = &(obd->u.lov);
507
508         /* lookup and kill hash reference */
509         pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
510         if (pool == NULL)
511                 RETURN(-ENOENT);
512
513         if (pool->pool_proc_entry != NULL) {
514                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
515                 lprocfs_remove(&pool->pool_proc_entry);
516                 lov_pool_putref(pool);
517         }
518
519         spin_lock(&obd->obd_dev_lock);
520         list_del_init(&pool->pool_list);
521         lov->lov_pool_count--;
522         spin_unlock(&obd->obd_dev_lock);
523
524         /* release last reference */
525         lov_pool_putref(pool);
526
527         RETURN(0);
528 }
529
530
531 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
532 {
533         struct obd_uuid ost_uuid;
534         struct lov_obd *lov;
535         struct pool_desc *pool;
536         unsigned int lov_idx;
537         int rc;
538         ENTRY;
539
540         lov = &(obd->u.lov);
541
542         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
543         if (pool == NULL)
544                 RETURN(-ENOENT);
545
546         obd_str2uuid(&ost_uuid, ostname);
547
548
549         /* search ost in lov array */
550         obd_getref(obd);
551         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
552                 if (!lov->lov_tgts[lov_idx])
553                         continue;
554                 if (obd_uuid_equals(&ost_uuid,
555                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
556                         break;
557         }
558         /* test if ost found in lov */
559         if (lov_idx == lov->desc.ld_tgt_count)
560                 GOTO(out, rc = -EINVAL);
561
562         rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
563         if (rc)
564                 GOTO(out, rc);
565
566         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
567                ostname, poolname,  pool_tgt_count(pool));
568
569         EXIT;
570 out:
571         obd_putref(obd);
572         lov_pool_putref(pool);
573         return rc;
574 }
575
576 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
577 {
578         struct obd_uuid ost_uuid;
579         struct lov_obd *lov;
580         struct pool_desc *pool;
581         unsigned int lov_idx;
582         int rc = 0;
583         ENTRY;
584
585         lov = &(obd->u.lov);
586
587         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
588         if (pool == NULL)
589                 RETURN(-ENOENT);
590
591         obd_str2uuid(&ost_uuid, ostname);
592
593         obd_getref(obd);
594         /* search ost in lov array, to get index */
595         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
596                 if (!lov->lov_tgts[lov_idx])
597                         continue;
598
599                 if (obd_uuid_equals(&ost_uuid,
600                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
601                         break;
602         }
603
604         /* test if ost found in lov */
605         if (lov_idx == lov->desc.ld_tgt_count)
606                 GOTO(out, rc = -EINVAL);
607
608         lov_ost_pool_remove(&pool->pool_obds, lov_idx);
609
610         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
611                poolname);
612
613         EXIT;
614 out:
615         obd_putref(obd);
616         lov_pool_putref(pool);
617         return rc;
618 }