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
lov_pool_getref(struct pool_desc * pool)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
lov_pool_putref(struct pool_desc * pool)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 == NULL);
68 lov_ost_pool_free(&(pool->pool_rr.lqr_pool));
69 lov_ost_pool_free(&(pool->pool_obds));
70 kfree(pool);
71 }
72 }
73
lov_pool_putref_locked(struct pool_desc * pool)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 */
pool_hashfn(struct cfs_hash * hash_body,const void * key,unsigned mask)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
pool_key(struct hlist_node * hnode)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
pool_hashkey_keycmp(const void * key,struct hlist_node * compared_hnode)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
pool_hashobject(struct hlist_node * hnode)123 static void *pool_hashobject(struct hlist_node *hnode)
124 {
125 return hlist_entry(hnode, struct pool_desc, pool_hash);
126 }
127
pool_hashrefcount_get(struct cfs_hash * hs,struct hlist_node * hnode)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
pool_hashrefcount_put_locked(struct cfs_hash * hs,struct hlist_node * hnode)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 /* ifdef needed for liblustre support */
156 /*
157 * pool /proc seq_file methods
158 */
159 /*
160 * iterator is used to go through the target pool entries
161 * index is the current entry index in the lp_array[] array
162 * index >= pos returned to the seq_file interface
163 * pos is from 0 to (pool->pool_obds.op_count - 1)
164 */
165 #define POOL_IT_MAGIC 0xB001CEA0
166 struct pool_iterator {
167 int magic;
168 struct pool_desc *pool;
169 int idx; /* from 0 to pool_tgt_size - 1 */
170 };
171
pool_proc_next(struct seq_file * s,void * v,loff_t * pos)172 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
173 {
174 struct pool_iterator *iter = (struct pool_iterator *)s->private;
175 int prev_idx;
176
177 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
178
179 /* test if end of file */
180 if (*pos >= pool_tgt_count(iter->pool))
181 return NULL;
182
183 /* iterate to find a non empty entry */
184 prev_idx = iter->idx;
185 down_read(&pool_tgt_rw_sem(iter->pool));
186 iter->idx++;
187 if (iter->idx == pool_tgt_count(iter->pool)) {
188 iter->idx = prev_idx; /* we stay on the last entry */
189 up_read(&pool_tgt_rw_sem(iter->pool));
190 return NULL;
191 }
192 up_read(&pool_tgt_rw_sem(iter->pool));
193 (*pos)++;
194 /* return != NULL to continue */
195 return iter;
196 }
197
pool_proc_start(struct seq_file * s,loff_t * pos)198 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
199 {
200 struct pool_desc *pool = (struct pool_desc *)s->private;
201 struct pool_iterator *iter;
202
203 lov_pool_getref(pool);
204 if ((pool_tgt_count(pool) == 0) ||
205 (*pos >= pool_tgt_count(pool))) {
206 /* iter is not created, so stop() has no way to
207 * find pool to dec ref */
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 /* /!\ 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 != NULL));
231 return ptr;
232 }
233 return iter;
234 }
235
pool_proc_stop(struct seq_file * s,void * v)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 if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
244 /* we restore s->private so next call to pool_proc_start()
245 * will work */
246 s->private = iter->pool;
247 lov_pool_putref(iter->pool);
248 kfree(iter);
249 }
250 return;
251 }
252
pool_proc_show(struct seq_file * s,void * v)253 static int pool_proc_show(struct seq_file *s, void *v)
254 {
255 struct pool_iterator *iter = (struct pool_iterator *)v;
256 struct lov_tgt_desc *tgt;
257
258 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
259 LASSERT(iter->pool != NULL);
260 LASSERT(iter->idx <= pool_tgt_count(iter->pool));
261
262 down_read(&pool_tgt_rw_sem(iter->pool));
263 tgt = pool_tgt(iter->pool, iter->idx);
264 up_read(&pool_tgt_rw_sem(iter->pool));
265 if (tgt)
266 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
267
268 return 0;
269 }
270
271 static const struct seq_operations pool_proc_ops = {
272 .start = pool_proc_start,
273 .next = pool_proc_next,
274 .stop = pool_proc_stop,
275 .show = pool_proc_show,
276 };
277
pool_proc_open(struct inode * inode,struct file * file)278 static int pool_proc_open(struct inode *inode, struct file *file)
279 {
280 int rc;
281
282 rc = seq_open(file, &pool_proc_ops);
283 if (!rc) {
284 struct seq_file *s = file->private_data;
285
286 s->private = inode->i_private;
287 }
288 return rc;
289 }
290
291 static struct file_operations pool_proc_operations = {
292 .open = pool_proc_open,
293 .read = seq_read,
294 .llseek = seq_lseek,
295 .release = seq_release,
296 };
297
298 #define LOV_POOL_INIT_COUNT 2
lov_ost_pool_init(struct ost_pool * op,unsigned int count)299 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
300 {
301 if (count == 0)
302 count = LOV_POOL_INIT_COUNT;
303 op->op_array = NULL;
304 op->op_count = 0;
305 init_rwsem(&op->op_rw_sem);
306 op->op_size = count;
307 op->op_array = kcalloc(op->op_size, sizeof(op->op_array[0]), GFP_NOFS);
308 if (op->op_array == NULL) {
309 op->op_size = 0;
310 return -ENOMEM;
311 }
312 return 0;
313 }
314
315 /* Caller must hold write op_rwlock */
lov_ost_pool_extend(struct ost_pool * op,unsigned int min_count)316 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
317 {
318 __u32 *new;
319 int new_size;
320
321 LASSERT(min_count != 0);
322
323 if (op->op_count < op->op_size)
324 return 0;
325
326 new_size = max(min_count, 2 * op->op_size);
327 new = kcalloc(new_size, sizeof(op->op_array[0]), GFP_NOFS);
328 if (new == NULL)
329 return -ENOMEM;
330
331 /* copy old array to new one */
332 memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
333 kfree(op->op_array);
334 op->op_array = new;
335 op->op_size = new_size;
336 return 0;
337 }
338
lov_ost_pool_add(struct ost_pool * op,__u32 idx,unsigned int min_count)339 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
340 {
341 int rc = 0, i;
342
343 down_write(&op->op_rw_sem);
344
345 rc = lov_ost_pool_extend(op, min_count);
346 if (rc)
347 goto out;
348
349 /* search ost in pool array */
350 for (i = 0; i < op->op_count; i++) {
351 if (op->op_array[i] == idx) {
352 rc = -EEXIST;
353 goto out;
354 }
355 }
356 /* ost not found we add it */
357 op->op_array[op->op_count] = idx;
358 op->op_count++;
359 out:
360 up_write(&op->op_rw_sem);
361 return rc;
362 }
363
lov_ost_pool_remove(struct ost_pool * op,__u32 idx)364 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
365 {
366 int i;
367
368 down_write(&op->op_rw_sem);
369
370 for (i = 0; i < op->op_count; i++) {
371 if (op->op_array[i] == idx) {
372 memmove(&op->op_array[i], &op->op_array[i + 1],
373 (op->op_count - i - 1) * sizeof(op->op_array[0]));
374 op->op_count--;
375 up_write(&op->op_rw_sem);
376 return 0;
377 }
378 }
379
380 up_write(&op->op_rw_sem);
381 return -EINVAL;
382 }
383
lov_ost_pool_free(struct ost_pool * op)384 int lov_ost_pool_free(struct ost_pool *op)
385 {
386 if (op->op_size == 0)
387 return 0;
388
389 down_write(&op->op_rw_sem);
390
391 kfree(op->op_array);
392 op->op_array = NULL;
393 op->op_count = 0;
394 op->op_size = 0;
395
396 up_write(&op->op_rw_sem);
397 return 0;
398 }
399
lov_pool_new(struct obd_device * obd,char * poolname)400 int lov_pool_new(struct obd_device *obd, char *poolname)
401 {
402 struct lov_obd *lov;
403 struct pool_desc *new_pool;
404 int rc;
405
406 lov = &(obd->u.lov);
407
408 if (strlen(poolname) > LOV_MAXPOOLNAME)
409 return -ENAMETOOLONG;
410
411 new_pool = kzalloc(sizeof(*new_pool), GFP_NOFS);
412 if (!new_pool)
413 return -ENOMEM;
414
415 strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
416 new_pool->pool_lobd = obd;
417 /* ref count init to 1 because when created a pool is always used
418 * up to deletion
419 */
420 atomic_set(&new_pool->pool_refcount, 1);
421 rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
422 if (rc)
423 goto out_err;
424
425 memset(&(new_pool->pool_rr), 0, sizeof(struct lov_qos_rr));
426 rc = lov_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
427 if (rc)
428 goto out_free_pool_obds;
429
430 INIT_HLIST_NODE(&new_pool->pool_hash);
431
432 /* we need this assert seq_file is not implemented for liblustre */
433 /* get ref for /proc file */
434 lov_pool_getref(new_pool);
435 new_pool->pool_debugfs_entry = ldebugfs_add_simple(
436 lov->lov_pool_debugfs_entry,
437 poolname, new_pool,
438 &pool_proc_operations);
439 if (IS_ERR_OR_NULL(new_pool->pool_debugfs_entry)) {
440 CWARN("Cannot add debugfs pool entry "LOV_POOLNAMEF"\n",
441 poolname);
442 new_pool->pool_debugfs_entry = NULL;
443 lov_pool_putref(new_pool);
444 }
445 CDEBUG(D_INFO, "pool %p - proc %p\n",
446 new_pool, new_pool->pool_debugfs_entry);
447
448 spin_lock(&obd->obd_dev_lock);
449 list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
450 lov->lov_pool_count++;
451 spin_unlock(&obd->obd_dev_lock);
452
453 /* add to find only when it fully ready */
454 rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
455 &new_pool->pool_hash);
456 if (rc) {
457 rc = -EEXIST;
458 goto out_err;
459 }
460
461 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
462 poolname, lov->lov_pool_count);
463
464 return 0;
465
466 out_err:
467 spin_lock(&obd->obd_dev_lock);
468 list_del_init(&new_pool->pool_list);
469 lov->lov_pool_count--;
470 spin_unlock(&obd->obd_dev_lock);
471
472 ldebugfs_remove(&new_pool->pool_debugfs_entry);
473
474 lov_ost_pool_free(&new_pool->pool_rr.lqr_pool);
475 out_free_pool_obds:
476 lov_ost_pool_free(&new_pool->pool_obds);
477 kfree(new_pool);
478 return rc;
479 }
480
lov_pool_del(struct obd_device * obd,char * poolname)481 int lov_pool_del(struct obd_device *obd, char *poolname)
482 {
483 struct lov_obd *lov;
484 struct pool_desc *pool;
485
486 lov = &(obd->u.lov);
487
488 /* lookup and kill hash reference */
489 pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
490 if (pool == NULL)
491 return -ENOENT;
492
493 if (!IS_ERR_OR_NULL(pool->pool_debugfs_entry)) {
494 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_debugfs_entry);
495 ldebugfs_remove(&pool->pool_debugfs_entry);
496 lov_pool_putref(pool);
497 }
498
499 spin_lock(&obd->obd_dev_lock);
500 list_del_init(&pool->pool_list);
501 lov->lov_pool_count--;
502 spin_unlock(&obd->obd_dev_lock);
503
504 /* release last reference */
505 lov_pool_putref(pool);
506
507 return 0;
508 }
509
lov_pool_add(struct obd_device * obd,char * poolname,char * ostname)510 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
511 {
512 struct obd_uuid ost_uuid;
513 struct lov_obd *lov;
514 struct pool_desc *pool;
515 unsigned int lov_idx;
516 int rc;
517
518 lov = &(obd->u.lov);
519
520 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
521 if (pool == NULL)
522 return -ENOENT;
523
524 obd_str2uuid(&ost_uuid, ostname);
525
526 /* search ost in lov array */
527 obd_getref(obd);
528 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
529 if (!lov->lov_tgts[lov_idx])
530 continue;
531 if (obd_uuid_equals(&ost_uuid,
532 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
533 break;
534 }
535 /* test if ost found in lov */
536 if (lov_idx == lov->desc.ld_tgt_count) {
537 rc = -EINVAL;
538 goto out;
539 }
540
541 rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
542 if (rc)
543 goto out;
544
545 pool->pool_rr.lqr_dirty = 1;
546
547 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
548 ostname, poolname, pool_tgt_count(pool));
549
550 out:
551 obd_putref(obd);
552 lov_pool_putref(pool);
553 return rc;
554 }
555
lov_pool_remove(struct obd_device * obd,char * poolname,char * ostname)556 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
557 {
558 struct obd_uuid ost_uuid;
559 struct lov_obd *lov;
560 struct pool_desc *pool;
561 unsigned int lov_idx;
562 int rc = 0;
563
564 lov = &(obd->u.lov);
565
566 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
567 if (pool == NULL)
568 return -ENOENT;
569
570 obd_str2uuid(&ost_uuid, ostname);
571
572 obd_getref(obd);
573 /* search ost in lov array, to get index */
574 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
575 if (!lov->lov_tgts[lov_idx])
576 continue;
577
578 if (obd_uuid_equals(&ost_uuid,
579 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
580 break;
581 }
582
583 /* test if ost found in lov */
584 if (lov_idx == lov->desc.ld_tgt_count) {
585 rc = -EINVAL;
586 goto out;
587 }
588
589 lov_ost_pool_remove(&pool->pool_obds, lov_idx);
590
591 pool->pool_rr.lqr_dirty = 1;
592
593 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
594 poolname);
595
596 out:
597 obd_putref(obd);
598 lov_pool_putref(pool);
599 return rc;
600 }
601
lov_check_index_in_pool(__u32 idx,struct pool_desc * pool)602 int lov_check_index_in_pool(__u32 idx, struct pool_desc *pool)
603 {
604 int i, rc;
605
606 /* caller may no have a ref on pool if it got the pool
607 * without calling lov_find_pool() (e.g. go through the lov pool
608 * list)
609 */
610 lov_pool_getref(pool);
611
612 down_read(&pool_tgt_rw_sem(pool));
613
614 for (i = 0; i < pool_tgt_count(pool); i++) {
615 if (pool_tgt_array(pool)[i] == idx) {
616 rc = 0;
617 goto out;
618 }
619 }
620 rc = -ENOENT;
621 out:
622 up_read(&pool_tgt_rw_sem(pool));
623
624 lov_pool_putref(pool);
625 return rc;
626 }
627
lov_find_pool(struct lov_obd * lov,char * poolname)628 struct pool_desc *lov_find_pool(struct lov_obd *lov, char *poolname)
629 {
630 struct pool_desc *pool;
631
632 pool = NULL;
633 if (poolname[0] != '\0') {
634 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
635 if (pool == NULL)
636 CWARN("Request for an unknown pool ("LOV_POOLNAMEF")\n",
637 poolname);
638 if ((pool != NULL) && (pool_tgt_count(pool) == 0)) {
639 CWARN("Request for an empty pool ("LOV_POOLNAMEF")\n",
640 poolname);
641 /* pool is ignored, so we remove ref on it */
642 lov_pool_putref(pool);
643 pool = NULL;
644 }
645 }
646 return pool;
647 }
648