Whamcloud - gitweb
LU-147 hash code cleanup
[fs/lustre-release.git] / lustre / lov / lov_pool.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lov/lov_pool.c
37  *
38  * OST pool methods
39  *
40  * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
41  * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
42  * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOV
46
47 #ifdef __KERNEL__
48 #include <libcfs/libcfs.h>
49 #else
50 #include <liblustre.h>
51 #endif
52
53 #include <obd.h>
54 #include "lov_internal.h"
55
56 static void lov_pool_getref(struct pool_desc *pool)
57 {
58         CDEBUG(D_INFO, "pool %p\n", pool);
59         cfs_atomic_inc(&pool->pool_refcount);
60 }
61
62 void lov_pool_putref(struct pool_desc *pool) 
63 {
64         CDEBUG(D_INFO, "pool %p\n", pool);
65         if (cfs_atomic_dec_and_test(&pool->pool_refcount)) {
66                 LASSERT(cfs_hlist_unhashed(&pool->pool_hash));
67                 LASSERT(cfs_list_empty(&pool->pool_list));
68                 LASSERT(pool->pool_proc_entry == NULL);
69                 lov_ost_pool_free(&(pool->pool_rr.lqr_pool));
70                 lov_ost_pool_free(&(pool->pool_obds));
71                 OBD_FREE_PTR(pool);
72                 EXIT;
73         }
74 }
75
76 void lov_pool_putref_locked(struct pool_desc *pool)
77 {
78         CDEBUG(D_INFO, "pool %p\n", pool);
79         LASSERT(cfs_atomic_read(&pool->pool_refcount) > 1);
80
81         cfs_atomic_dec(&pool->pool_refcount);
82 }
83
84 /*
85  * hash function using a Rotating Hash algorithm
86  * Knuth, D. The Art of Computer Programming,
87  * Volume 3: Sorting and Searching,
88  * Chapter 6.4.
89  * Addison Wesley, 1973
90  */
91 static __u32 pool_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
92 {
93         int i;
94         __u32 result;
95         char *poolname;
96
97         result = 0;
98         poolname = (char *)key;
99         for (i = 0; i < LOV_MAXPOOLNAME; i++) {
100                 if (poolname[i] == '\0')
101                         break;
102                 result = (result << 4)^(result >> 28) ^  poolname[i];
103         }
104         return (result % mask);
105 }
106
107 static void *pool_key(cfs_hlist_node_t *hnode)
108 {
109         struct pool_desc *pool;
110
111         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
112         return (pool->pool_name);
113 }
114
115 static int pool_hashkey_keycmp(const void *key, cfs_hlist_node_t *compared_hnode)
116 {
117         char *pool_name;
118         struct pool_desc *pool;
119
120         pool_name = (char *)key;
121         pool = cfs_hlist_entry(compared_hnode, struct pool_desc, pool_hash);
122         return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
123 }
124
125 static void *pool_hashobject(cfs_hlist_node_t *hnode)
126 {
127         return cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
128 }
129
130 static void pool_hashrefcount_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
131 {
132         struct pool_desc *pool;
133
134         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
135         lov_pool_getref(pool);
136 }
137
138 static void pool_hashrefcount_put_locked(cfs_hash_t *hs,
139                                          cfs_hlist_node_t *hnode)
140 {
141         struct pool_desc *pool;
142
143         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
144         lov_pool_putref_locked(pool);
145 }
146
147 cfs_hash_ops_t pool_hash_operations = {
148         .hs_hash        = pool_hashfn,
149         .hs_key         = pool_key,
150         .hs_keycmp      = pool_hashkey_keycmp,
151         .hs_object      = pool_hashobject,
152         .hs_get         = pool_hashrefcount_get,
153         .hs_put_locked  = pool_hashrefcount_put_locked,
154
155 };
156
157 #ifdef LPROCFS
158 /* ifdef needed for liblustre support */
159 /*
160  * pool /proc seq_file methods
161  */
162 /*
163  * iterator is used to go through the target pool entries
164  * index is the current entry index in the lp_array[] array
165  * index >= pos returned to the seq_file interface
166  * pos is from 0 to (pool->pool_obds.op_count - 1)
167  */
168 #define POOL_IT_MAGIC 0xB001CEA0
169 struct pool_iterator {
170         int magic;
171         struct pool_desc *pool;
172         int idx;        /* from 0 to pool_tgt_size - 1 */
173 };
174
175 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
176 {
177         struct pool_iterator *iter = (struct pool_iterator *)s->private;
178         int prev_idx;
179
180         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
181
182         /* test if end of file */
183         if (*pos >= pool_tgt_count(iter->pool))
184                 return NULL;
185
186         /* iterate to find a non empty entry */
187         prev_idx = iter->idx;
188         cfs_down_read(&pool_tgt_rw_sem(iter->pool));
189         iter->idx++;
190         if (iter->idx == pool_tgt_count(iter->pool)) {
191                 iter->idx = prev_idx; /* we stay on the last entry */
192                 cfs_up_read(&pool_tgt_rw_sem(iter->pool));
193                 return NULL;
194         }
195         cfs_up_read(&pool_tgt_rw_sem(iter->pool));
196         (*pos)++;
197         /* return != NULL to continue */
198         return iter;
199 }
200
201 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
202 {
203         struct pool_desc *pool = (struct pool_desc *)s->private;
204         struct pool_iterator *iter;
205
206         lov_pool_getref(pool);
207         if ((pool_tgt_count(pool) == 0) ||
208             (*pos >= pool_tgt_count(pool))) {
209                 /* iter is not created, so stop() has no way to
210                  * find pool to dec ref */
211                 lov_pool_putref(pool);
212                 return NULL;
213         }
214
215         OBD_ALLOC_PTR(iter);
216         if (!iter)
217                 return ERR_PTR(-ENOMEM);
218         iter->magic = POOL_IT_MAGIC;
219         iter->pool = pool;
220         iter->idx = 0;
221
222         /* we use seq_file private field to memorized iterator so
223          * we can free it at stop() */
224         /* /!\ do not forget to restore it to pool before freeing it */
225         s->private = iter;
226         if (*pos > 0) {
227                 loff_t i;
228                 void *ptr;
229
230                 i = 0;
231                 do {
232                      ptr = pool_proc_next(s, &iter, &i);
233                 } while ((i < *pos) && (ptr != NULL));
234                 return ptr;
235         }
236         return iter;
237 }
238
239 static void pool_proc_stop(struct seq_file *s, void *v)
240 {
241         struct pool_iterator *iter = (struct pool_iterator *)s->private;
242
243         /* in some cases stop() method is called 2 times, without
244          * calling start() method (see seq_read() from fs/seq_file.c)
245          * we have to free only if s->private is an iterator */
246         if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
247                 /* we restore s->private so next call to pool_proc_start()
248                  * will work */
249                 s->private = iter->pool;
250                 lov_pool_putref(iter->pool);
251                 OBD_FREE_PTR(iter);
252         }
253         return;
254 }
255
256 static int pool_proc_show(struct seq_file *s, void *v)
257 {
258         struct pool_iterator *iter = (struct pool_iterator *)v;
259         struct lov_tgt_desc *tgt;
260
261         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
262         LASSERT(iter->pool != NULL);
263         LASSERT(iter->idx <= pool_tgt_count(iter->pool));
264
265         cfs_down_read(&pool_tgt_rw_sem(iter->pool));
266         tgt = pool_tgt(iter->pool, iter->idx);
267         cfs_up_read(&pool_tgt_rw_sem(iter->pool));
268         if (tgt)
269                 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
270
271         return 0;
272 }
273
274 static struct seq_operations pool_proc_ops = {
275         .start          = pool_proc_start,
276         .next           = pool_proc_next,
277         .stop           = pool_proc_stop,
278         .show           = pool_proc_show,
279 };
280
281 static int pool_proc_open(struct inode *inode, struct file *file)
282 {
283         int rc;
284
285         rc = seq_open(file, &pool_proc_ops);
286         if (!rc) {
287                 struct seq_file *s = file->private_data;
288                 s->private = PROC_I(inode)->pde->data;
289         }
290         return rc;
291 }
292
293 static struct file_operations pool_proc_operations = {
294         .open           = pool_proc_open,
295         .read           = seq_read,
296         .llseek         = seq_lseek,
297         .release        = seq_release,
298 };
299 #endif /* LPROCFS */
300
301 void lov_dump_pool(int level, struct pool_desc *pool)
302 {
303         int i;
304
305         lov_pool_getref(pool);
306
307         CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
308                pool->pool_name, pool->pool_obds.op_count);
309         cfs_down_read(&pool_tgt_rw_sem(pool));
310
311         for (i = 0; i < pool_tgt_count(pool) ; i++) {
312                 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
313                         continue;
314                 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
315                        pool->pool_name, i,
316                        obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
317         }
318
319         cfs_up_read(&pool_tgt_rw_sem(pool));
320         lov_pool_putref(pool);
321 }
322
323 #define LOV_POOL_INIT_COUNT 2
324 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
325 {
326         ENTRY;
327
328         if (count == 0)
329                 count = LOV_POOL_INIT_COUNT;
330         op->op_array = NULL;
331         op->op_count = 0;
332         cfs_init_rwsem(&op->op_rw_sem);
333         op->op_size = count;
334         OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
335         if (op->op_array == NULL) {
336                 op->op_size = 0;
337                 RETURN(-ENOMEM);
338         }
339         EXIT;
340         return 0;
341 }
342
343 /* Caller must hold write op_rwlock */
344 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
345 {
346         __u32 *new;
347         int new_size;
348
349         LASSERT(min_count != 0);
350
351         if (op->op_count < op->op_size)
352                 return 0;
353
354         new_size = max(min_count, 2 * op->op_size);
355         OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
356         if (new == NULL)
357                 return -ENOMEM;
358
359         /* copy old array to new one */
360         memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
361         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
362         op->op_array = new;
363         op->op_size = new_size;
364         return 0;
365 }
366
367 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
368 {
369         int rc = 0, i;
370         ENTRY;
371
372         cfs_down_write(&op->op_rw_sem);
373
374         rc = lov_ost_pool_extend(op, min_count);
375         if (rc)
376                 GOTO(out, rc);
377
378         /* search ost in pool array */
379         for (i = 0; i < op->op_count; i++) {
380                 if (op->op_array[i] == idx)
381                         GOTO(out, rc = -EEXIST);
382         }
383         /* ost not found we add it */
384         op->op_array[op->op_count] = idx;
385         op->op_count++;
386         EXIT;
387 out:
388         cfs_up_write(&op->op_rw_sem);
389         return rc;
390 }
391
392 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
393 {
394         int i;
395         ENTRY;
396
397         cfs_down_write(&op->op_rw_sem);
398
399         for (i = 0; i < op->op_count; i++) {
400                 if (op->op_array[i] == idx) {
401                         memmove(&op->op_array[i], &op->op_array[i + 1],
402                                 (op->op_count - i - 1) * sizeof(op->op_array[0]));
403                         op->op_count--;
404                         cfs_up_write(&op->op_rw_sem);
405                         EXIT;
406                         return 0;
407                 }
408         }
409
410         cfs_up_write(&op->op_rw_sem);
411         RETURN(-EINVAL);
412 }
413
414 int lov_ost_pool_free(struct ost_pool *op)
415 {
416         ENTRY;
417
418         if (op->op_size == 0)
419                 RETURN(0);
420
421         cfs_down_write(&op->op_rw_sem);
422
423         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
424         op->op_array = NULL;
425         op->op_count = 0;
426         op->op_size = 0;
427
428         cfs_up_write(&op->op_rw_sem);
429         RETURN(0);
430 }
431
432
433 int lov_pool_new(struct obd_device *obd, char *poolname)
434 {
435         struct lov_obd *lov;
436         struct pool_desc *new_pool;
437         int rc;
438         ENTRY;
439
440         lov = &(obd->u.lov);
441
442         if (strlen(poolname) > LOV_MAXPOOLNAME)
443                 RETURN(-ENAMETOOLONG);
444
445         OBD_ALLOC_PTR(new_pool);
446         if (new_pool == NULL)
447                 RETURN(-ENOMEM);
448
449         strncpy(new_pool->pool_name, poolname, LOV_MAXPOOLNAME);
450         new_pool->pool_name[LOV_MAXPOOLNAME] = '\0';
451         new_pool->pool_lov = lov;
452         /* ref count init to 1 because when created a pool is always used
453          * up to deletion
454          */
455         cfs_atomic_set(&new_pool->pool_refcount, 1);
456         rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
457         if (rc)
458                GOTO(out_err, rc);
459
460         memset(&(new_pool->pool_rr), 0, sizeof(struct lov_qos_rr));
461         rc = lov_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
462         if (rc)
463                 GOTO(out_free_pool_obds, rc);
464
465         CFS_INIT_HLIST_NODE(&new_pool->pool_hash);
466
467 #ifdef LPROCFS
468         /* we need this assert seq_file is not implementated for liblustre */
469         /* get ref for /proc file */
470         lov_pool_getref(new_pool);
471         new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
472                                                        poolname, NULL, NULL,
473                                                        new_pool,
474                                                        &pool_proc_operations);
475         if (IS_ERR(new_pool->pool_proc_entry)) {
476                 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
477                 new_pool->pool_proc_entry = NULL;
478                 lov_pool_putref(new_pool);
479         }
480         CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool, new_pool->pool_proc_entry);
481 #endif
482
483         cfs_spin_lock(&obd->obd_dev_lock);
484         cfs_list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
485         lov->lov_pool_count++;
486         cfs_spin_unlock(&obd->obd_dev_lock);
487
488         /* add to find only when it fully ready  */
489         rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
490                                  &new_pool->pool_hash);
491         if (rc)
492                 GOTO(out_err, rc = -EEXIST);
493
494         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
495                poolname, lov->lov_pool_count);
496
497         RETURN(0);
498
499 out_err:
500         cfs_spin_lock(&obd->obd_dev_lock);
501         cfs_list_del_init(&new_pool->pool_list);
502         lov->lov_pool_count--;
503         cfs_spin_unlock(&obd->obd_dev_lock);
504
505         lprocfs_remove(&new_pool->pool_proc_entry);
506
507         lov_ost_pool_free(&new_pool->pool_rr.lqr_pool);
508 out_free_pool_obds:
509         lov_ost_pool_free(&new_pool->pool_obds);
510         OBD_FREE_PTR(new_pool);
511         return rc;
512 }
513
514 int lov_pool_del(struct obd_device *obd, char *poolname)
515 {
516         struct lov_obd *lov;
517         struct pool_desc *pool;
518         ENTRY;
519
520         lov = &(obd->u.lov);
521
522         /* lookup and kill hash reference */
523         pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
524         if (pool == NULL)
525                 RETURN(-ENOENT);
526
527         if (pool->pool_proc_entry != NULL) {
528                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
529                 lprocfs_remove(&pool->pool_proc_entry);
530                 lov_pool_putref(pool);
531         }
532
533         cfs_spin_lock(&obd->obd_dev_lock);
534         cfs_list_del_init(&pool->pool_list);
535         lov->lov_pool_count--;
536         cfs_spin_unlock(&obd->obd_dev_lock);
537
538         /* release last reference */
539         lov_pool_putref(pool);
540
541         RETURN(0);
542 }
543
544
545 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
546 {
547         struct obd_uuid ost_uuid;
548         struct lov_obd *lov;
549         struct pool_desc *pool;
550         unsigned int lov_idx;
551         int rc;
552         ENTRY;
553
554         lov = &(obd->u.lov);
555
556         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
557         if (pool == NULL)
558                 RETURN(-ENOENT);
559
560         obd_str2uuid(&ost_uuid, ostname);
561
562
563         /* search ost in lov array */
564         obd_getref(obd);
565         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
566                 if (!lov->lov_tgts[lov_idx])
567                         continue;
568                 if (obd_uuid_equals(&ost_uuid,
569                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
570                         break;
571         }
572         /* test if ost found in lov */
573         if (lov_idx == lov->desc.ld_tgt_count)
574                 GOTO(out, rc = -EINVAL);
575
576         rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
577         if (rc)
578                 GOTO(out, rc);
579
580         pool->pool_rr.lqr_dirty = 1;
581
582         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
583                ostname, poolname,  pool_tgt_count(pool));
584
585         EXIT;
586 out:
587         obd_putref(obd);
588         lov_pool_putref(pool);
589         return rc;
590 }
591
592 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
593 {
594         struct obd_uuid ost_uuid;
595         struct lov_obd *lov;
596         struct pool_desc *pool;
597         unsigned int lov_idx;
598         int rc = 0;
599         ENTRY;
600
601         lov = &(obd->u.lov);
602
603         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
604         if (pool == NULL)
605                 RETURN(-ENOENT);
606
607         obd_str2uuid(&ost_uuid, ostname);
608
609         obd_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         lov_ost_pool_remove(&pool->pool_obds, lov_idx);
625
626         pool->pool_rr.lqr_dirty = 1;
627
628         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
629                poolname);
630
631         EXIT;
632 out:
633         obd_putref(obd);
634         lov_pool_putref(pool);
635         return rc;
636 }
637
638 int lov_check_index_in_pool(__u32 idx, struct pool_desc *pool)
639 {
640         int i, rc;
641         ENTRY;
642
643         /* caller may no have a ref on pool if it got the pool
644          * without calling lov_find_pool() (e.g. go through the lov pool
645          * list)
646          */
647         lov_pool_getref(pool);
648
649         cfs_down_read(&pool_tgt_rw_sem(pool));
650
651         for (i = 0; i < pool_tgt_count(pool); i++) {
652                 if (pool_tgt_array(pool)[i] == idx)
653                         GOTO(out, rc = 0);
654         }
655         rc = -ENOENT;
656         EXIT;
657 out:
658         cfs_up_read(&pool_tgt_rw_sem(pool));
659
660         lov_pool_putref(pool);
661         return rc;
662 }
663
664 struct pool_desc *lov_find_pool(struct lov_obd *lov, char *poolname)
665 {
666         struct pool_desc *pool;
667
668         pool = NULL;
669         if (poolname[0] != '\0') {
670                 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
671                 if (pool == NULL)
672                         CWARN("Request for an unknown pool ("LOV_POOLNAMEF")\n",
673                               poolname);
674                 if ((pool != NULL) && (pool_tgt_count(pool) == 0)) {
675                         CWARN("Request for an empty pool ("LOV_POOLNAMEF")\n",
676                                poolname);
677                         /* pool is ignored, so we remove ref on it */
678                         lov_pool_putref(pool);
679                         pool = NULL;
680                 }
681         }
682         return pool;
683 }
684