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