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