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