Merge remote-tracking branches 'regmap/topic/doc' and 'regmap/topic/flat' into regmap...
[cascardo/linux.git] / drivers / staging / lustre / 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 #include "../../include/linux/libcfs/libcfs.h"
48
49 #include "../include/obd.h"
50 #include "lov_internal.h"
51
52 #define pool_tgt(_p, _i) \
53                 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
54
55 static void lov_pool_getref(struct pool_desc *pool)
56 {
57         CDEBUG(D_INFO, "pool %p\n", pool);
58         atomic_inc(&pool->pool_refcount);
59 }
60
61 void lov_pool_putref(struct pool_desc *pool)
62 {
63         CDEBUG(D_INFO, "pool %p\n", pool);
64         if (atomic_dec_and_test(&pool->pool_refcount)) {
65                 LASSERT(hlist_unhashed(&pool->pool_hash));
66                 LASSERT(list_empty(&pool->pool_list));
67                 LASSERT(!pool->pool_debugfs_entry);
68                 lov_ost_pool_free(&(pool->pool_rr.lqr_pool));
69                 lov_ost_pool_free(&(pool->pool_obds));
70                 kfree(pool);
71         }
72 }
73
74 static void lov_pool_putref_locked(struct pool_desc *pool)
75 {
76         CDEBUG(D_INFO, "pool %p\n", pool);
77         LASSERT(atomic_read(&pool->pool_refcount) > 1);
78
79         atomic_dec(&pool->pool_refcount);
80 }
81
82 /*
83  * hash function using a Rotating Hash algorithm
84  * Knuth, D. The Art of Computer Programming,
85  * Volume 3: Sorting and Searching,
86  * Chapter 6.4.
87  * Addison Wesley, 1973
88  */
89 static __u32 pool_hashfn(struct cfs_hash *hash_body, const void *key, unsigned mask)
90 {
91         int i;
92         __u32 result;
93         char *poolname;
94
95         result = 0;
96         poolname = (char *)key;
97         for (i = 0; i < LOV_MAXPOOLNAME; i++) {
98                 if (poolname[i] == '\0')
99                         break;
100                 result = (result << 4)^(result >> 28) ^  poolname[i];
101         }
102         return (result % mask);
103 }
104
105 static void *pool_key(struct hlist_node *hnode)
106 {
107         struct pool_desc *pool;
108
109         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
110         return pool->pool_name;
111 }
112
113 static int pool_hashkey_keycmp(const void *key, struct hlist_node *compared_hnode)
114 {
115         char *pool_name;
116         struct pool_desc *pool;
117
118         pool_name = (char *)key;
119         pool = hlist_entry(compared_hnode, struct pool_desc, pool_hash);
120         return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
121 }
122
123 static void *pool_hashobject(struct hlist_node *hnode)
124 {
125         return hlist_entry(hnode, struct pool_desc, pool_hash);
126 }
127
128 static void pool_hashrefcount_get(struct cfs_hash *hs, struct hlist_node *hnode)
129 {
130         struct pool_desc *pool;
131
132         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
133         lov_pool_getref(pool);
134 }
135
136 static void pool_hashrefcount_put_locked(struct cfs_hash *hs,
137                                          struct hlist_node *hnode)
138 {
139         struct pool_desc *pool;
140
141         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
142         lov_pool_putref_locked(pool);
143 }
144
145 struct cfs_hash_ops pool_hash_operations = {
146         .hs_hash        = pool_hashfn,
147         .hs_key         = pool_key,
148         .hs_keycmp      = pool_hashkey_keycmp,
149         .hs_object      = pool_hashobject,
150         .hs_get         = pool_hashrefcount_get,
151         .hs_put_locked  = pool_hashrefcount_put_locked,
152
153 };
154
155 /*
156  * pool debugfs 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                  */
208                 lov_pool_putref(pool);
209                 return NULL;
210         }
211
212         iter = kzalloc(sizeof(*iter), GFP_NOFS);
213         if (!iter)
214                 return ERR_PTR(-ENOMEM);
215         iter->magic = POOL_IT_MAGIC;
216         iter->pool = pool;
217         iter->idx = 0;
218
219         /* we use seq_file private field to memorized iterator so
220          * we can free it at stop()
221          */
222         /* /!\ do not forget to restore it to pool before freeing it */
223         s->private = iter;
224         if (*pos > 0) {
225                 loff_t i;
226                 void *ptr;
227
228                 i = 0;
229                 do {
230                         ptr = pool_proc_next(s, &iter, &i);
231                 } while ((i < *pos) && ptr);
232                 return ptr;
233         }
234         return iter;
235 }
236
237 static void pool_proc_stop(struct seq_file *s, void *v)
238 {
239         struct pool_iterator *iter = (struct pool_iterator *)s->private;
240
241         /* in some cases stop() method is called 2 times, without
242          * calling start() method (see seq_read() from fs/seq_file.c)
243          * we have to free only if s->private is an iterator
244          */
245         if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
246                 /* we restore s->private so next call to pool_proc_start()
247                  * will work
248                  */
249                 s->private = iter->pool;
250                 lov_pool_putref(iter->pool);
251                 kfree(iter);
252         }
253 }
254
255 static int pool_proc_show(struct seq_file *s, void *v)
256 {
257         struct pool_iterator *iter = (struct pool_iterator *)v;
258         struct lov_tgt_desc *tgt;
259
260         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
261         LASSERT(iter->pool);
262         LASSERT(iter->idx <= pool_tgt_count(iter->pool));
263
264         down_read(&pool_tgt_rw_sem(iter->pool));
265         tgt = pool_tgt(iter->pool, iter->idx);
266         up_read(&pool_tgt_rw_sem(iter->pool));
267         if (tgt)
268                 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
269
270         return 0;
271 }
272
273 static const struct seq_operations pool_proc_ops = {
274         .start    = pool_proc_start,
275         .next      = pool_proc_next,
276         .stop      = pool_proc_stop,
277         .show      = pool_proc_show,
278 };
279
280 static int pool_proc_open(struct inode *inode, struct file *file)
281 {
282         int rc;
283
284         rc = seq_open(file, &pool_proc_ops);
285         if (!rc) {
286                 struct seq_file *s = file->private_data;
287
288                 s->private = inode->i_private;
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
300 #define LOV_POOL_INIT_COUNT 2
301 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
302 {
303         if (count == 0)
304                 count = LOV_POOL_INIT_COUNT;
305         op->op_array = NULL;
306         op->op_count = 0;
307         init_rwsem(&op->op_rw_sem);
308         op->op_size = count;
309         op->op_array = kcalloc(op->op_size, sizeof(op->op_array[0]), GFP_NOFS);
310         if (!op->op_array) {
311                 op->op_size = 0;
312                 return -ENOMEM;
313         }
314         return 0;
315 }
316
317 /* Caller must hold write op_rwlock */
318 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
319 {
320         __u32 *new;
321         int new_size;
322
323         LASSERT(min_count != 0);
324
325         if (op->op_count < op->op_size)
326                 return 0;
327
328         new_size = max(min_count, 2 * op->op_size);
329         new = kcalloc(new_size, sizeof(op->op_array[0]), GFP_NOFS);
330         if (!new)
331                 return -ENOMEM;
332
333         /* copy old array to new one */
334         memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
335         kfree(op->op_array);
336         op->op_array = new;
337         op->op_size = new_size;
338         return 0;
339 }
340
341 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
342 {
343         int rc = 0, i;
344
345         down_write(&op->op_rw_sem);
346
347         rc = lov_ost_pool_extend(op, min_count);
348         if (rc)
349                 goto out;
350
351         /* search ost in pool array */
352         for (i = 0; i < op->op_count; i++) {
353                 if (op->op_array[i] == idx) {
354                         rc = -EEXIST;
355                         goto out;
356                 }
357         }
358         /* ost not found we add it */
359         op->op_array[op->op_count] = idx;
360         op->op_count++;
361 out:
362         up_write(&op->op_rw_sem);
363         return rc;
364 }
365
366 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
367 {
368         int i;
369
370         down_write(&op->op_rw_sem);
371
372         for (i = 0; i < op->op_count; i++) {
373                 if (op->op_array[i] == idx) {
374                         memmove(&op->op_array[i], &op->op_array[i + 1],
375                                 (op->op_count - i - 1) * sizeof(op->op_array[0]));
376                         op->op_count--;
377                         up_write(&op->op_rw_sem);
378                         return 0;
379                 }
380         }
381
382         up_write(&op->op_rw_sem);
383         return -EINVAL;
384 }
385
386 int lov_ost_pool_free(struct ost_pool *op)
387 {
388         if (op->op_size == 0)
389                 return 0;
390
391         down_write(&op->op_rw_sem);
392
393         kfree(op->op_array);
394         op->op_array = NULL;
395         op->op_count = 0;
396         op->op_size = 0;
397
398         up_write(&op->op_rw_sem);
399         return 0;
400 }
401
402 int lov_pool_new(struct obd_device *obd, char *poolname)
403 {
404         struct lov_obd *lov;
405         struct pool_desc *new_pool;
406         int rc;
407
408         lov = &(obd->u.lov);
409
410         if (strlen(poolname) > LOV_MAXPOOLNAME)
411                 return -ENAMETOOLONG;
412
413         new_pool = kzalloc(sizeof(*new_pool), GFP_NOFS);
414         if (!new_pool)
415                 return -ENOMEM;
416
417         strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
418         new_pool->pool_lobd = obd;
419         /* ref count init to 1 because when created a pool is always used
420          * up to deletion
421          */
422         atomic_set(&new_pool->pool_refcount, 1);
423         rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
424         if (rc)
425                 goto out_err;
426
427         memset(&(new_pool->pool_rr), 0, sizeof(struct lov_qos_rr));
428         rc = lov_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
429         if (rc)
430                 goto out_free_pool_obds;
431
432         INIT_HLIST_NODE(&new_pool->pool_hash);
433
434         /* get ref for debugfs file */
435         lov_pool_getref(new_pool);
436         new_pool->pool_debugfs_entry = ldebugfs_add_simple(
437                                                 lov->lov_pool_debugfs_entry,
438                                                 poolname, new_pool,
439                                                 &pool_proc_operations);
440         if (IS_ERR_OR_NULL(new_pool->pool_debugfs_entry)) {
441                 CWARN("Cannot add debugfs pool entry "LOV_POOLNAMEF"\n",
442                       poolname);
443                 new_pool->pool_debugfs_entry = NULL;
444                 lov_pool_putref(new_pool);
445         }
446         CDEBUG(D_INFO, "pool %p - proc %p\n",
447                new_pool, new_pool->pool_debugfs_entry);
448
449         spin_lock(&obd->obd_dev_lock);
450         list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
451         lov->lov_pool_count++;
452         spin_unlock(&obd->obd_dev_lock);
453
454         /* add to find only when it fully ready  */
455         rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
456                                  &new_pool->pool_hash);
457         if (rc) {
458                 rc = -EEXIST;
459                 goto out_err;
460         }
461
462         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
463                poolname, lov->lov_pool_count);
464
465         return 0;
466
467 out_err:
468         spin_lock(&obd->obd_dev_lock);
469         list_del_init(&new_pool->pool_list);
470         lov->lov_pool_count--;
471         spin_unlock(&obd->obd_dev_lock);
472
473         ldebugfs_remove(&new_pool->pool_debugfs_entry);
474
475         lov_ost_pool_free(&new_pool->pool_rr.lqr_pool);
476 out_free_pool_obds:
477         lov_ost_pool_free(&new_pool->pool_obds);
478         kfree(new_pool);
479         return rc;
480 }
481
482 int lov_pool_del(struct obd_device *obd, char *poolname)
483 {
484         struct lov_obd *lov;
485         struct pool_desc *pool;
486
487         lov = &(obd->u.lov);
488
489         /* lookup and kill hash reference */
490         pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
491         if (!pool)
492                 return -ENOENT;
493
494         if (!IS_ERR_OR_NULL(pool->pool_debugfs_entry)) {
495                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_debugfs_entry);
496                 ldebugfs_remove(&pool->pool_debugfs_entry);
497                 lov_pool_putref(pool);
498         }
499
500         spin_lock(&obd->obd_dev_lock);
501         list_del_init(&pool->pool_list);
502         lov->lov_pool_count--;
503         spin_unlock(&obd->obd_dev_lock);
504
505         /* release last reference */
506         lov_pool_putref(pool);
507
508         return 0;
509 }
510
511 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
512 {
513         struct obd_uuid ost_uuid;
514         struct lov_obd *lov;
515         struct pool_desc *pool;
516         unsigned int lov_idx;
517         int rc;
518
519         lov = &(obd->u.lov);
520
521         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
522         if (!pool)
523                 return -ENOENT;
524
525         obd_str2uuid(&ost_uuid, ostname);
526
527         /* search ost in lov array */
528         obd_getref(obd);
529         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
530                 if (!lov->lov_tgts[lov_idx])
531                         continue;
532                 if (obd_uuid_equals(&ost_uuid,
533                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
534                         break;
535         }
536         /* test if ost found in lov */
537         if (lov_idx == lov->desc.ld_tgt_count) {
538                 rc = -EINVAL;
539                 goto out;
540         }
541
542         rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
543         if (rc)
544                 goto out;
545
546         pool->pool_rr.lqr_dirty = 1;
547
548         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
549                ostname, poolname,  pool_tgt_count(pool));
550
551 out:
552         obd_putref(obd);
553         lov_pool_putref(pool);
554         return rc;
555 }
556
557 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
558 {
559         struct obd_uuid ost_uuid;
560         struct lov_obd *lov;
561         struct pool_desc *pool;
562         unsigned int lov_idx;
563         int rc = 0;
564
565         lov = &(obd->u.lov);
566
567         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
568         if (!pool)
569                 return -ENOENT;
570
571         obd_str2uuid(&ost_uuid, ostname);
572
573         obd_getref(obd);
574         /* search ost in lov array, to get index */
575         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
576                 if (!lov->lov_tgts[lov_idx])
577                         continue;
578
579                 if (obd_uuid_equals(&ost_uuid,
580                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
581                         break;
582         }
583
584         /* test if ost found in lov */
585         if (lov_idx == lov->desc.ld_tgt_count) {
586                 rc = -EINVAL;
587                 goto out;
588         }
589
590         lov_ost_pool_remove(&pool->pool_obds, lov_idx);
591
592         pool->pool_rr.lqr_dirty = 1;
593
594         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
595                poolname);
596
597 out:
598         obd_putref(obd);
599         lov_pool_putref(pool);
600         return rc;
601 }
602
603 int lov_check_index_in_pool(__u32 idx, struct pool_desc *pool)
604 {
605         int i, rc;
606
607         /* caller may no have a ref on pool if it got the pool
608          * without calling lov_find_pool() (e.g. go through the lov pool
609          * list)
610          */
611         lov_pool_getref(pool);
612
613         down_read(&pool_tgt_rw_sem(pool));
614
615         for (i = 0; i < pool_tgt_count(pool); i++) {
616                 if (pool_tgt_array(pool)[i] == idx) {
617                         rc = 0;
618                         goto out;
619                 }
620         }
621         rc = -ENOENT;
622 out:
623         up_read(&pool_tgt_rw_sem(pool));
624
625         lov_pool_putref(pool);
626         return rc;
627 }
628
629 struct pool_desc *lov_find_pool(struct lov_obd *lov, char *poolname)
630 {
631         struct pool_desc *pool;
632
633         pool = NULL;
634         if (poolname[0] != '\0') {
635                 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
636                 if (!pool)
637                         CWARN("Request for an unknown pool ("LOV_POOLNAMEF")\n",
638                               poolname);
639                 if (pool && (pool_tgt_count(pool) == 0)) {
640                         CWARN("Request for an empty pool ("LOV_POOLNAMEF")\n",
641                               poolname);
642                         /* pool is ignored, so we remove ref on it */
643                         lov_pool_putref(pool);
644                         pool = NULL;
645                 }
646         }
647         return pool;
648 }