1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/ptlrpc/sec_bulk.c
40 * Author: Eric Mei <ericm@clusterfs.com>
46 #define DEBUG_SUBSYSTEM S_SEC
48 #include <libcfs/libcfs.h>
50 #include <liblustre.h>
51 #include <libcfs/list.h>
53 #include <linux/crypto.h>
57 #include <obd_cksum.h>
58 #include <obd_class.h>
59 #include <obd_support.h>
60 #include <lustre_net.h>
61 #include <lustre_import.h>
62 #include <lustre_dlm.h>
63 #include <lustre_sec.h>
65 #include "ptlrpc_internal.h"
67 /****************************************
68 * bulk encryption page pools *
69 ****************************************/
73 #define PTRS_PER_PAGE (CFS_PAGE_SIZE / sizeof(void *))
74 #define PAGES_PER_POOL (PTRS_PER_PAGE)
76 #define IDLE_IDX_MAX (100)
77 #define IDLE_IDX_WEIGHT (3)
79 #define CACHE_QUIESCENT_PERIOD (20)
81 static struct ptlrpc_enc_page_pool {
85 unsigned long epp_max_pages; /* maximum pages can hold, const */
86 unsigned int epp_max_pools; /* number of pools, const */
89 * wait queue in case of not enough free pages.
91 cfs_waitq_t epp_waitq; /* waiting threads */
92 unsigned int epp_waitqlen; /* wait queue length */
93 unsigned long epp_pages_short; /* # of pages wanted of in-q users */
94 unsigned int epp_growing:1; /* during adding pages */
97 * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
98 * this is counted based on each time when getting pages from
99 * the pools, not based on time. which means in case that system
100 * is idled for a while but the idle_idx might still be low if no
101 * activities happened in the pools.
103 unsigned long epp_idle_idx;
105 /* last shrink time due to mem tight */
106 long epp_last_shrink;
107 long epp_last_access;
110 * in-pool pages bookkeeping
112 cfs_spinlock_t epp_lock; /* protect following fields */
113 unsigned long epp_total_pages; /* total pages in pools */
114 unsigned long epp_free_pages; /* current pages available */
119 unsigned long epp_st_max_pages; /* # of pages ever reached */
120 unsigned int epp_st_grows; /* # of grows */
121 unsigned int epp_st_grow_fails; /* # of add pages failures */
122 unsigned int epp_st_shrinks; /* # of shrinks */
123 unsigned long epp_st_access; /* # of access */
124 unsigned long epp_st_missings; /* # of cache missing */
125 unsigned long epp_st_lowfree; /* lowest free pages reached */
126 unsigned int epp_st_max_wqlen; /* highest waitqueue length */
127 cfs_time_t epp_st_max_wait; /* in jeffies */
131 cfs_page_t ***epp_pools;
137 const int pools_shrinker_seeks = CFS_DEFAULT_SEEKS;
138 static struct cfs_shrinker *pools_shrinker = NULL;
142 * /proc/fs/lustre/sptlrpc/encrypt_page_pools
144 int sptlrpc_proc_read_enc_pool(char *page, char **start, off_t off, int count,
145 int *eof, void *data)
149 cfs_spin_lock(&page_pools.epp_lock);
151 rc = snprintf(page, count,
152 "physical pages: %lu\n"
153 "pages per pool: %lu\n"
158 "idle index: %lu/100\n"
159 "last shrink: %lds\n"
160 "last access: %lds\n"
161 "max pages reached: %lu\n"
163 "grows failure: %u\n"
165 "cache access: %lu\n"
166 "cache missing: %lu\n"
167 "low free mark: %lu\n"
168 "max waitqueue depth: %u\n"
169 "max wait time: "CFS_TIME_T"/%u\n"
173 page_pools.epp_max_pages,
174 page_pools.epp_max_pools,
175 page_pools.epp_total_pages,
176 page_pools.epp_free_pages,
177 page_pools.epp_idle_idx,
178 cfs_time_current_sec() - page_pools.epp_last_shrink,
179 cfs_time_current_sec() - page_pools.epp_last_access,
180 page_pools.epp_st_max_pages,
181 page_pools.epp_st_grows,
182 page_pools.epp_st_grow_fails,
183 page_pools.epp_st_shrinks,
184 page_pools.epp_st_access,
185 page_pools.epp_st_missings,
186 page_pools.epp_st_lowfree,
187 page_pools.epp_st_max_wqlen,
188 page_pools.epp_st_max_wait, CFS_HZ
191 cfs_spin_unlock(&page_pools.epp_lock);
195 static void enc_pools_release_free_pages(long npages)
198 int p_idx_max1, p_idx_max2;
201 LASSERT(npages <= page_pools.epp_free_pages);
202 LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
204 /* max pool index before the release */
205 p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
207 page_pools.epp_free_pages -= npages;
208 page_pools.epp_total_pages -= npages;
210 /* max pool index after the release */
211 p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
212 ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
214 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
215 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
216 LASSERT(page_pools.epp_pools[p_idx]);
219 LASSERT(page_pools.epp_pools[p_idx]);
220 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
222 cfs_free_page(page_pools.epp_pools[p_idx][g_idx]);
223 page_pools.epp_pools[p_idx][g_idx] = NULL;
225 if (++g_idx == PAGES_PER_POOL) {
231 /* free unused pools */
232 while (p_idx_max1 < p_idx_max2) {
233 LASSERT(page_pools.epp_pools[p_idx_max2]);
234 OBD_FREE(page_pools.epp_pools[p_idx_max2], CFS_PAGE_SIZE);
235 page_pools.epp_pools[p_idx_max2] = NULL;
241 * could be called frequently for query (@nr_to_scan == 0).
242 * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
244 static int enc_pools_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
246 if (unlikely(shrink_param(sc, nr_to_scan) != 0)) {
247 cfs_spin_lock(&page_pools.epp_lock);
248 shrink_param(sc, nr_to_scan) = min_t(unsigned long,
249 shrink_param(sc, nr_to_scan),
250 page_pools.epp_free_pages -
251 PTLRPC_MAX_BRW_PAGES);
252 if (shrink_param(sc, nr_to_scan) > 0) {
253 enc_pools_release_free_pages(shrink_param(sc,
255 CDEBUG(D_SEC, "released %ld pages, %ld left\n",
256 (long)shrink_param(sc, nr_to_scan),
257 page_pools.epp_free_pages);
259 page_pools.epp_st_shrinks++;
260 page_pools.epp_last_shrink = cfs_time_current_sec();
262 cfs_spin_unlock(&page_pools.epp_lock);
266 * if no pool access for a long time, we consider it's fully idle.
267 * a little race here is fine.
269 if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
270 CACHE_QUIESCENT_PERIOD)) {
271 cfs_spin_lock(&page_pools.epp_lock);
272 page_pools.epp_idle_idx = IDLE_IDX_MAX;
273 cfs_spin_unlock(&page_pools.epp_lock);
276 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
277 return max((int) page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES, 0) *
278 (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
282 int npages_to_npools(unsigned long npages)
284 return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
288 * return how many pages cleaned up.
290 static unsigned long enc_pools_cleanup(cfs_page_t ***pools, int npools)
292 unsigned long cleaned = 0;
295 for (i = 0; i < npools; i++) {
297 for (j = 0; j < PAGES_PER_POOL; j++) {
299 cfs_free_page(pools[i][j]);
303 OBD_FREE(pools[i], CFS_PAGE_SIZE);
312 * merge @npools pointed by @pools which contains @npages new pages
313 * into current pools.
315 * we have options to avoid most memory copy with some tricks. but we choose
316 * the simplest way to avoid complexity. It's not frequently called.
318 static void enc_pools_insert(cfs_page_t ***pools, int npools, int npages)
321 int op_idx, np_idx, og_idx, ng_idx;
322 int cur_npools, end_npools;
325 LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
326 LASSERT(npages_to_npools(npages) == npools);
327 LASSERT(page_pools.epp_growing);
329 cfs_spin_lock(&page_pools.epp_lock);
332 * (1) fill all the free slots of current pools.
334 /* free slots are those left by rent pages, and the extra ones with
335 * index >= total_pages, locate at the tail of last pool. */
336 freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
338 freeslot = PAGES_PER_POOL - freeslot;
339 freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
341 op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
342 og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
344 ng_idx = (npages - 1) % PAGES_PER_POOL;
347 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
348 LASSERT(pools[np_idx][ng_idx] != NULL);
350 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
351 pools[np_idx][ng_idx] = NULL;
355 if (++og_idx == PAGES_PER_POOL) {
363 ng_idx = PAGES_PER_POOL - 1;
368 * (2) add pools if needed.
370 cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
372 end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
374 LASSERT(end_npools <= page_pools.epp_max_pools);
377 while (cur_npools < end_npools) {
378 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
379 LASSERT(np_idx < npools);
380 LASSERT(pools[np_idx] != NULL);
382 page_pools.epp_pools[cur_npools++] = pools[np_idx];
383 pools[np_idx++] = NULL;
386 page_pools.epp_total_pages += npages;
387 page_pools.epp_free_pages += npages;
388 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
390 if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
391 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
393 CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
394 page_pools.epp_total_pages);
396 cfs_spin_unlock(&page_pools.epp_lock);
399 static int enc_pools_add_pages(int npages)
401 static CFS_DECLARE_MUTEX(sem_add_pages);
403 int npools, alloced = 0;
404 int i, j, rc = -ENOMEM;
406 if (npages < PTLRPC_MAX_BRW_PAGES)
407 npages = PTLRPC_MAX_BRW_PAGES;
409 cfs_down(&sem_add_pages);
411 if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
412 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
415 page_pools.epp_st_grows++;
417 npools = npages_to_npools(npages);
418 OBD_ALLOC(pools, npools * sizeof(*pools));
422 for (i = 0; i < npools; i++) {
423 OBD_ALLOC(pools[i], CFS_PAGE_SIZE);
424 if (pools[i] == NULL)
427 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
428 pools[i][j] = cfs_alloc_page(CFS_ALLOC_IO |
430 if (pools[i][j] == NULL)
436 LASSERT(alloced == npages);
438 enc_pools_insert(pools, npools, npages);
439 CDEBUG(D_SEC, "added %d pages into pools\n", npages);
443 enc_pools_cleanup(pools, npools);
444 OBD_FREE(pools, npools * sizeof(*pools));
447 page_pools.epp_st_grow_fails++;
448 CERROR("Failed to allocate %d enc pages\n", npages);
451 cfs_up(&sem_add_pages);
455 static inline void enc_pools_wakeup(void)
457 LASSERT_SPIN_LOCKED(&page_pools.epp_lock);
458 LASSERT(page_pools.epp_waitqlen >= 0);
460 if (unlikely(page_pools.epp_waitqlen)) {
461 LASSERT(cfs_waitq_active(&page_pools.epp_waitq));
462 cfs_waitq_broadcast(&page_pools.epp_waitq);
466 static int enc_pools_should_grow(int page_needed, long now)
468 /* don't grow if someone else is growing the pools right now,
469 * or the pools has reached its full capacity
471 if (page_pools.epp_growing ||
472 page_pools.epp_total_pages == page_pools.epp_max_pages)
475 /* if total pages is not enough, we need to grow */
476 if (page_pools.epp_total_pages < page_needed)
480 * we wanted to return 0 here if there was a shrink just happened
481 * moment ago, but this may cause deadlock if both client and ost
482 * live on single node.
485 if (now - page_pools.epp_last_shrink < 2)
490 * here we perhaps need consider other factors like wait queue
491 * length, idle index, etc. ?
494 /* grow the pools in any other cases */
499 * we allocate the requested pages atomically.
501 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
503 cfs_waitlink_t waitlink;
504 unsigned long this_idle = -1;
510 LASSERT(desc->bd_iov_count > 0);
511 LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
513 /* resent bulk, enc iov might have been allocated previously */
514 if (desc->bd_enc_iov != NULL)
517 OBD_ALLOC(desc->bd_enc_iov,
518 desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
519 if (desc->bd_enc_iov == NULL)
522 cfs_spin_lock(&page_pools.epp_lock);
524 page_pools.epp_st_access++;
526 if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
528 tick = cfs_time_current();
530 now = cfs_time_current_sec();
532 page_pools.epp_st_missings++;
533 page_pools.epp_pages_short += desc->bd_iov_count;
535 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
536 page_pools.epp_growing = 1;
538 cfs_spin_unlock(&page_pools.epp_lock);
539 enc_pools_add_pages(page_pools.epp_pages_short / 2);
540 cfs_spin_lock(&page_pools.epp_lock);
542 page_pools.epp_growing = 0;
546 if (++page_pools.epp_waitqlen >
547 page_pools.epp_st_max_wqlen)
548 page_pools.epp_st_max_wqlen =
549 page_pools.epp_waitqlen;
551 cfs_set_current_state(CFS_TASK_UNINT);
552 cfs_waitlink_init(&waitlink);
553 cfs_waitq_add(&page_pools.epp_waitq, &waitlink);
555 cfs_spin_unlock(&page_pools.epp_lock);
556 cfs_waitq_wait(&waitlink, CFS_TASK_UNINT);
557 cfs_waitq_del(&page_pools.epp_waitq, &waitlink);
558 LASSERT(page_pools.epp_waitqlen > 0);
559 cfs_spin_lock(&page_pools.epp_lock);
560 page_pools.epp_waitqlen--;
563 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
564 page_pools.epp_pages_short -= desc->bd_iov_count;
570 /* record max wait time */
571 if (unlikely(tick != 0)) {
572 tick = cfs_time_current() - tick;
573 if (tick > page_pools.epp_st_max_wait)
574 page_pools.epp_st_max_wait = tick;
577 /* proceed with rest of allocation */
578 page_pools.epp_free_pages -= desc->bd_iov_count;
580 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
581 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
583 for (i = 0; i < desc->bd_iov_count; i++) {
584 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
585 desc->bd_enc_iov[i].kiov_page =
586 page_pools.epp_pools[p_idx][g_idx];
587 page_pools.epp_pools[p_idx][g_idx] = NULL;
589 if (++g_idx == PAGES_PER_POOL) {
595 if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
596 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
599 * new idle index = (old * weight + new) / (weight + 1)
601 if (this_idle == -1) {
602 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
603 page_pools.epp_total_pages;
605 page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
607 (IDLE_IDX_WEIGHT + 1);
609 page_pools.epp_last_access = cfs_time_current_sec();
611 cfs_spin_unlock(&page_pools.epp_lock);
614 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
616 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
621 if (desc->bd_enc_iov == NULL)
624 LASSERT(desc->bd_iov_count > 0);
626 cfs_spin_lock(&page_pools.epp_lock);
628 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
629 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
631 LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
632 page_pools.epp_total_pages);
633 LASSERT(page_pools.epp_pools[p_idx]);
635 for (i = 0; i < desc->bd_iov_count; i++) {
636 LASSERT(desc->bd_enc_iov[i].kiov_page != NULL);
637 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
638 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
640 page_pools.epp_pools[p_idx][g_idx] =
641 desc->bd_enc_iov[i].kiov_page;
643 if (++g_idx == PAGES_PER_POOL) {
649 page_pools.epp_free_pages += desc->bd_iov_count;
653 cfs_spin_unlock(&page_pools.epp_lock);
655 OBD_FREE(desc->bd_enc_iov,
656 desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
657 desc->bd_enc_iov = NULL;
659 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
662 * we don't do much stuff for add_user/del_user anymore, except adding some
663 * initial pages in add_user() if current pools are empty, rest would be
664 * handled by the pools's self-adaption.
666 int sptlrpc_enc_pool_add_user(void)
670 cfs_spin_lock(&page_pools.epp_lock);
671 if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
672 page_pools.epp_growing = 1;
675 cfs_spin_unlock(&page_pools.epp_lock);
678 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
679 PTLRPC_MAX_BRW_PAGES);
681 cfs_spin_lock(&page_pools.epp_lock);
682 page_pools.epp_growing = 0;
684 cfs_spin_unlock(&page_pools.epp_lock);
688 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
690 int sptlrpc_enc_pool_del_user(void)
694 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
696 static inline void enc_pools_alloc(void)
698 LASSERT(page_pools.epp_max_pools);
699 OBD_ALLOC_LARGE(page_pools.epp_pools,
700 page_pools.epp_max_pools *
701 sizeof(*page_pools.epp_pools));
704 static inline void enc_pools_free(void)
706 LASSERT(page_pools.epp_max_pools);
707 LASSERT(page_pools.epp_pools);
709 OBD_FREE_LARGE(page_pools.epp_pools,
710 page_pools.epp_max_pools *
711 sizeof(*page_pools.epp_pools));
714 int sptlrpc_enc_pool_init(void)
717 * maximum capacity is 1/8 of total physical memory.
718 * is the 1/8 a good number?
720 page_pools.epp_max_pages = cfs_num_physpages / 8;
721 page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
723 cfs_waitq_init(&page_pools.epp_waitq);
724 page_pools.epp_waitqlen = 0;
725 page_pools.epp_pages_short = 0;
727 page_pools.epp_growing = 0;
729 page_pools.epp_idle_idx = 0;
730 page_pools.epp_last_shrink = cfs_time_current_sec();
731 page_pools.epp_last_access = cfs_time_current_sec();
733 cfs_spin_lock_init(&page_pools.epp_lock);
734 page_pools.epp_total_pages = 0;
735 page_pools.epp_free_pages = 0;
737 page_pools.epp_st_max_pages = 0;
738 page_pools.epp_st_grows = 0;
739 page_pools.epp_st_grow_fails = 0;
740 page_pools.epp_st_shrinks = 0;
741 page_pools.epp_st_access = 0;
742 page_pools.epp_st_missings = 0;
743 page_pools.epp_st_lowfree = 0;
744 page_pools.epp_st_max_wqlen = 0;
745 page_pools.epp_st_max_wait = 0;
748 if (page_pools.epp_pools == NULL)
751 pools_shrinker = cfs_set_shrinker(pools_shrinker_seeks,
753 if (pools_shrinker == NULL) {
761 void sptlrpc_enc_pool_fini(void)
763 unsigned long cleaned, npools;
765 LASSERT(pools_shrinker);
766 LASSERT(page_pools.epp_pools);
767 LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
769 cfs_remove_shrinker(pools_shrinker);
771 npools = npages_to_npools(page_pools.epp_total_pages);
772 cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
773 LASSERT(cleaned == page_pools.epp_total_pages);
777 if (page_pools.epp_st_access > 0) {
779 "max pages %lu, grows %u, grow fails %u, shrinks %u, "
780 "access %lu, missing %lu, max qlen %u, max wait "
782 page_pools.epp_st_max_pages, page_pools.epp_st_grows,
783 page_pools.epp_st_grow_fails,
784 page_pools.epp_st_shrinks, page_pools.epp_st_access,
785 page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
786 page_pools.epp_st_max_wait, CFS_HZ);
790 #else /* !__KERNEL__ */
792 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
797 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
801 int sptlrpc_enc_pool_init(void)
806 void sptlrpc_enc_pool_fini(void)
811 /****************************************
812 * Helpers to assist policy modules to *
813 * implement checksum funcationality *
814 ****************************************/
816 static struct sptlrpc_hash_type hash_types[] = {
817 [BULK_HASH_ALG_NULL] = { "null", "null", 0 },
818 [BULK_HASH_ALG_ADLER32] = { "adler32", "adler32", 4 },
819 [BULK_HASH_ALG_CRC32] = { "crc32", "crc32", 4 },
820 [BULK_HASH_ALG_MD5] = { "md5", "md5", 16 },
821 [BULK_HASH_ALG_SHA1] = { "sha1", "sha1", 20 },
822 [BULK_HASH_ALG_SHA256] = { "sha256", "sha256", 32 },
823 [BULK_HASH_ALG_SHA384] = { "sha384", "sha384", 48 },
824 [BULK_HASH_ALG_SHA512] = { "sha512", "sha512", 64 },
827 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg)
829 struct sptlrpc_hash_type *ht;
831 if (hash_alg < BULK_HASH_ALG_MAX) {
832 ht = &hash_types[hash_alg];
833 if (ht->sht_tfm_name)
838 EXPORT_SYMBOL(sptlrpc_get_hash_type);
840 const char * sptlrpc_get_hash_name(__u8 hash_alg)
842 const struct sptlrpc_hash_type *ht;
844 ht = sptlrpc_get_hash_type(hash_alg);
850 EXPORT_SYMBOL(sptlrpc_get_hash_name);
852 __u8 sptlrpc_get_hash_alg(const char *algname)
856 for (i = 0; i < BULK_HASH_ALG_MAX; i++)
857 if (!strcmp(hash_types[i].sht_name, algname))
861 EXPORT_SYMBOL(sptlrpc_get_hash_alg);
863 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
865 struct ptlrpc_bulk_sec_desc *bsd;
866 int size = msg->lm_buflens[offset];
868 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
870 CERROR("Invalid bulk sec desc: size %d\n", size);
875 __swab32s(&bsd->bsd_nob);
878 if (unlikely(bsd->bsd_version != 0)) {
879 CERROR("Unexpected version %u\n", bsd->bsd_version);
883 if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
884 CERROR("Invalid type %u\n", bsd->bsd_type);
888 /* FIXME more sanity check here */
890 if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
891 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
892 bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
893 CERROR("Invalid svc %u\n", bsd->bsd_svc);
899 EXPORT_SYMBOL(bulk_sec_desc_unpack);
904 static int do_bulk_checksum_adler32(struct ptlrpc_bulk_desc *desc, void *buf)
912 for (i = 0; i < desc->bd_iov_count; i++) {
913 page = desc->bd_iov[i].kiov_page;
914 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
915 ptr = cfs_kmap(page) + off;
916 len = desc->bd_iov[i].kiov_len;
918 adler32 = adler32(adler32, ptr, len);
923 adler32 = cpu_to_le32(adler32);
924 memcpy(buf, &adler32, sizeof(adler32));
929 static int do_bulk_checksum_crc32(struct ptlrpc_bulk_desc *desc, void *buf)
937 for (i = 0; i < desc->bd_iov_count; i++) {
938 page = desc->bd_iov[i].kiov_page;
939 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
940 ptr = cfs_kmap(page) + off;
941 len = desc->bd_iov[i].kiov_len;
943 crc32 = crc32_le(crc32, ptr, len);
948 crc32 = cpu_to_le32(crc32);
949 memcpy(buf, &crc32, sizeof(crc32));
953 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
954 void *buf, int buflen)
956 struct hash_desc hdesc;
959 struct scatterlist sl;
962 LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
963 LASSERT(buflen >= 4);
966 case BULK_HASH_ALG_ADLER32:
968 return do_bulk_checksum_adler32(desc, buf);
970 CERROR("Adler32 not supported\n");
973 case BULK_HASH_ALG_CRC32:
974 return do_bulk_checksum_crc32(desc, buf);
977 hdesc.tfm = ll_crypto_alloc_hash(hash_types[alg].sht_tfm_name, 0, 0);
978 if (hdesc.tfm == NULL) {
979 CERROR("Unable to allocate TFM %s\n", hash_types[alg].sht_name);
984 ll_crypto_hash_init(&hdesc);
986 hashsize = ll_crypto_hash_digestsize(hdesc.tfm);
988 for (i = 0; i < desc->bd_iov_count; i++) {
989 sg_set_page(&sl, desc->bd_iov[i].kiov_page,
990 desc->bd_iov[i].kiov_len,
991 desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK);
992 ll_crypto_hash_update(&hdesc, &sl, sl.length);
995 if (hashsize > buflen) {
996 ll_crypto_hash_final(&hdesc, hashbuf);
997 memcpy(buf, hashbuf, buflen);
999 ll_crypto_hash_final(&hdesc, buf);
1002 ll_crypto_free_hash(hdesc.tfm);
1005 EXPORT_SYMBOL(sptlrpc_get_bulk_checksum);
1007 #else /* !__KERNEL__ */
1009 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
1010 void *buf, int buflen)
1015 LASSERT(alg == BULK_HASH_ALG_ADLER32 || alg == BULK_HASH_ALG_CRC32);
1017 if (alg == BULK_HASH_ALG_ADLER32)
1022 for (i = 0; i < desc->bd_iov_count; i++) {
1023 unsigned char *ptr = desc->bd_iov[i].iov_base;
1024 int len = desc->bd_iov[i].iov_len;
1027 case BULK_HASH_ALG_ADLER32:
1029 csum32 = adler32(csum32, ptr, len);
1031 CERROR("Adler32 not supported\n");
1035 case BULK_HASH_ALG_CRC32:
1036 csum32 = crc32_le(csum32, ptr, len);
1041 csum32 = cpu_to_le32(csum32);
1042 memcpy(buf, &csum32, sizeof(csum32));
1046 #endif /* __KERNEL__ */