1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006-2007 Cluster File Systems, Inc.
5 * Author: Eric Mei <ericm@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DEBUG_SUBSYSTEM S_SEC
28 #include <libcfs/libcfs.h>
30 #include <liblustre.h>
31 #include <libcfs/list.h>
33 #include <linux/crypto.h>
37 #include <obd_class.h>
38 #include <obd_support.h>
39 #include <lustre_net.h>
40 #include <lustre_import.h>
41 #include <lustre_dlm.h>
42 #include <lustre_sec.h>
44 #include "ptlrpc_internal.h"
46 /****************************************
47 * bulk encryption page pools *
48 ****************************************/
52 #define PTRS_PER_PAGE (CFS_PAGE_SIZE / sizeof(void *))
53 #define PAGES_PER_POOL (PTRS_PER_PAGE)
55 #define IDLE_IDX_MAX (100)
56 #define IDLE_IDX_WEIGHT (3)
58 #define CACHE_QUIESCENCE_PERIOD (20)
60 static struct ptlrpc_enc_page_pool {
64 unsigned long epp_max_pages; /* maximum pages can hold, const */
65 unsigned int epp_max_pools; /* number of pools, const */
68 * wait queue in case of not enough free pages.
70 cfs_waitq_t epp_waitq; /* waiting threads */
71 unsigned int epp_waitqlen; /* wait queue length */
72 unsigned long epp_pages_short; /* # of pages wanted of in-q users */
73 unsigned int epp_growing:1; /* during adding pages */
76 * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
77 * this is counted based on each time when getting pages from
78 * the pools, not based on time. which means in case that system
79 * is idled for a while but the idle_idx might still be low if no
80 * activities happened in the pools.
82 unsigned long epp_idle_idx;
84 /* last shrink time due to mem tight */
89 * in-pool pages bookkeeping
91 spinlock_t epp_lock; /* protect following fields */
92 unsigned long epp_total_pages; /* total pages in pools */
93 unsigned long epp_free_pages; /* current pages available */
98 unsigned int epp_st_grows; /* # of grows */
99 unsigned int epp_st_grow_fails; /* # of add pages failures */
100 unsigned int epp_st_shrinks; /* # of shrinks */
101 unsigned long epp_st_access; /* # of access */
102 unsigned long epp_st_missings; /* # of cache missing */
103 unsigned long epp_st_lowfree; /* lowest free pages reached */
104 unsigned long epp_st_max_wqlen; /* highest waitqueue length */
105 cfs_time_t epp_st_max_wait; /* in jeffies */
109 cfs_page_t ***epp_pools;
115 const int pools_shrinker_seeks = DEFAULT_SEEKS;
116 static struct shrinker *pools_shrinker = NULL;
120 * /proc/fs/lustre/sptlrpc/encrypt_page_pools
122 int sptlrpc_proc_read_enc_pool(char *page, char **start, off_t off, int count,
123 int *eof, void *data)
127 spin_lock(&page_pools.epp_lock);
129 rc = snprintf(page, count,
130 "physical pages: %lu\n"
131 "pages per pool: %lu\n"
136 "idle index: %lu/100\n"
137 "last shrink: %lds\n"
138 "last access: %lds\n"
140 "grows failure: %u\n"
142 "cache access: %lu\n"
143 "cache missing: %lu\n"
144 "low free mark: %lu\n"
145 "max waitqueue depth: %lu\n"
146 "max wait time: "CFS_TIME_T"/%u\n"
150 page_pools.epp_max_pages,
151 page_pools.epp_max_pools,
152 page_pools.epp_total_pages,
153 page_pools.epp_free_pages,
154 page_pools.epp_idle_idx,
155 cfs_time_current_sec() - page_pools.epp_last_shrink,
156 cfs_time_current_sec() - page_pools.epp_last_access,
157 page_pools.epp_st_grows,
158 page_pools.epp_st_grow_fails,
159 page_pools.epp_st_shrinks,
160 page_pools.epp_st_access,
161 page_pools.epp_st_missings,
162 page_pools.epp_st_lowfree,
163 page_pools.epp_st_max_wqlen,
164 page_pools.epp_st_max_wait, HZ
167 spin_unlock(&page_pools.epp_lock);
171 static void enc_pools_release_free_pages(long npages)
175 LASSERT(npages <= page_pools.epp_free_pages);
177 p_idx = (page_pools.epp_free_pages - 1) / PAGES_PER_POOL;
178 g_idx = (page_pools.epp_free_pages - 1) % PAGES_PER_POOL;
179 LASSERT(page_pools.epp_pools[p_idx]);
181 page_pools.epp_free_pages -= npages;
182 page_pools.epp_total_pages -= npages;
184 while (npages-- > 0) {
185 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
187 cfs_free_page(page_pools.epp_pools[p_idx][g_idx]);
188 page_pools.epp_pools[p_idx][g_idx] = NULL;
192 g_idx = PAGES_PER_POOL - 1;
194 LASSERT(page_pools.epp_pools[p_idx]);
200 * could be called frequently for query (@nr_to_scan == 0)
202 static int enc_pools_shrink(int nr_to_scan, unsigned int gfp_mask)
206 spin_lock(&page_pools.epp_lock);
209 if (nr_to_scan > page_pools.epp_free_pages)
210 nr_to_scan = page_pools.epp_free_pages;
212 enc_pools_release_free_pages(nr_to_scan);
213 CDEBUG(D_SEC, "released %d pages, %ld left\n",
214 nr_to_scan, page_pools.epp_free_pages);
216 page_pools.epp_st_shrinks++;
217 page_pools.epp_last_shrink = cfs_time_current_sec();
221 * try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool
223 if (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES) {
229 * if no pool access for a long time, we consider it's fully idle
231 if (cfs_time_current_sec() - page_pools.epp_last_access >
232 CACHE_QUIESCENCE_PERIOD)
233 page_pools.epp_idle_idx = IDLE_IDX_MAX;
235 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
236 ret = (page_pools.epp_free_pages * page_pools.epp_idle_idx /
238 if (page_pools.epp_free_pages - ret < PTLRPC_MAX_BRW_PAGES)
239 ret = page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES;
242 spin_unlock(&page_pools.epp_lock);
247 int npages_to_npools(unsigned long npages)
249 return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
253 * return how many pages cleaned up.
255 static unsigned long enc_pools_cleanup(cfs_page_t ***pools, int npools)
257 unsigned long cleaned = 0;
260 for (i = 0; i < npools; i++) {
262 for (j = 0; j < PAGES_PER_POOL; j++) {
264 cfs_free_page(pools[i][j]);
268 OBD_FREE(pools[i], CFS_PAGE_SIZE);
277 * merge @npools pointed by @pools which contains @npages new pages
278 * into current pools.
280 * we have options to avoid most memory copy with some tricks. but we choose
281 * the simplest way to avoid complexity. It's not frequently called.
283 static void enc_pools_insert(cfs_page_t ***pools, int npools, int npages)
286 int op_idx, np_idx, og_idx, ng_idx;
287 int cur_npools, end_npools;
290 LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
291 LASSERT(npages_to_npools(npages) == npools);
293 spin_lock(&page_pools.epp_lock);
296 * (1) fill all the free slots of current pools.
298 /* free slots are those left by rent pages, and the extra ones with
299 * index >= eep_total_pages, locate at the tail of last pool. */
300 freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
302 freeslot = PAGES_PER_POOL - freeslot;
303 freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
305 op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
306 og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
308 ng_idx = (npages - 1) % PAGES_PER_POOL;
311 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
312 LASSERT(pools[np_idx][ng_idx] != NULL);
314 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
315 pools[np_idx][ng_idx] = NULL;
319 if (++og_idx == PAGES_PER_POOL) {
327 ng_idx = PAGES_PER_POOL - 1;
332 * (2) add pools if needed.
334 cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
336 end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
338 LASSERT(end_npools <= page_pools.epp_max_pools);
341 while (cur_npools < end_npools) {
342 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
343 LASSERT(np_idx < npools);
344 LASSERT(pools[np_idx] != NULL);
346 page_pools.epp_pools[cur_npools++] = pools[np_idx];
347 pools[np_idx++] = NULL;
350 page_pools.epp_total_pages += npages;
351 page_pools.epp_free_pages += npages;
352 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
354 CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
355 page_pools.epp_total_pages);
357 spin_unlock(&page_pools.epp_lock);
360 static int enc_pools_add_pages(int npages)
362 static DECLARE_MUTEX(sem_add_pages);
364 int npools, alloced = 0;
365 int i, j, rc = -ENOMEM;
367 if (npages < PTLRPC_MAX_BRW_PAGES)
368 npages = PTLRPC_MAX_BRW_PAGES;
370 down(&sem_add_pages);
372 if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
373 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
376 page_pools.epp_st_grows++;
378 npools = npages_to_npools(npages);
379 OBD_ALLOC(pools, npools * sizeof(*pools));
383 for (i = 0; i < npools; i++) {
384 OBD_ALLOC(pools[i], CFS_PAGE_SIZE);
385 if (pools[i] == NULL)
388 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
389 pools[i][j] = cfs_alloc_page(CFS_ALLOC_IO |
391 if (pools[i][j] == NULL)
398 enc_pools_insert(pools, npools, npages);
399 CDEBUG(D_SEC, "added %d pages into pools\n", npages);
403 enc_pools_cleanup(pools, npools);
404 OBD_FREE(pools, npools * sizeof(*pools));
407 page_pools.epp_st_grow_fails++;
408 CERROR("Failed to allocate %d enc pages\n", npages);
415 static inline void enc_pools_wakeup(void)
417 if (unlikely(page_pools.epp_waitqlen)) {
418 LASSERT(page_pools.epp_waitqlen > 0);
419 LASSERT(cfs_waitq_active(&page_pools.epp_waitq));
420 cfs_waitq_broadcast(&page_pools.epp_waitq);
424 static int enc_pools_should_grow(int page_needed, long now)
426 /* don't grow if someone else is growing the pools right now,
427 * or the pools has reached its full capacity
429 if (page_pools.epp_growing ||
430 page_pools.epp_total_pages == page_pools.epp_max_pages)
433 /* if total pages is not enough, we need to grow */
434 if (page_pools.epp_total_pages < page_needed)
437 /* if we just did a shrink due to memory tight, we'd better
438 * wait a while to grow again.
440 if (now - page_pools.epp_last_shrink < 2)
444 * here we perhaps need consider other factors like wait queue
445 * length, idle index, etc. ?
448 /* grow the pools in any other cases */
453 * we allocate the requested pages atomically.
455 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
457 cfs_waitlink_t waitlink;
458 unsigned long this_idle = -1;
464 LASSERT(desc->bd_max_iov > 0);
465 LASSERT(desc->bd_max_iov <= page_pools.epp_max_pages);
467 /* resent bulk, enc pages might have been allocated previously */
468 if (desc->bd_enc_pages != NULL)
471 OBD_ALLOC(desc->bd_enc_pages,
472 desc->bd_max_iov * sizeof(*desc->bd_enc_pages));
473 if (desc->bd_enc_pages == NULL)
476 spin_lock(&page_pools.epp_lock);
478 page_pools.epp_st_access++;
480 if (unlikely(page_pools.epp_free_pages < desc->bd_max_iov)) {
482 tick = cfs_time_current();
484 now = cfs_time_current_sec();
486 page_pools.epp_st_missings++;
487 page_pools.epp_pages_short += desc->bd_max_iov;
489 if (enc_pools_should_grow(desc->bd_max_iov, now)) {
490 page_pools.epp_growing = 1;
492 spin_unlock(&page_pools.epp_lock);
493 enc_pools_add_pages(page_pools.epp_pages_short / 2);
494 spin_lock(&page_pools.epp_lock);
496 page_pools.epp_growing = 0;
498 if (++page_pools.epp_waitqlen >
499 page_pools.epp_st_max_wqlen)
500 page_pools.epp_st_max_wqlen =
501 page_pools.epp_waitqlen;
503 set_current_state(TASK_UNINTERRUPTIBLE);
504 cfs_waitlink_init(&waitlink);
505 cfs_waitq_add(&page_pools.epp_waitq, &waitlink);
507 spin_unlock(&page_pools.epp_lock);
509 spin_lock(&page_pools.epp_lock);
511 LASSERT(page_pools.epp_waitqlen > 0);
512 page_pools.epp_waitqlen--;
515 LASSERT(page_pools.epp_pages_short >= desc->bd_max_iov);
516 page_pools.epp_pages_short -= desc->bd_max_iov;
522 /* record max wait time */
523 if (unlikely(tick != 0)) {
524 tick = cfs_time_current() - tick;
525 if (tick > page_pools.epp_st_max_wait)
526 page_pools.epp_st_max_wait = tick;
529 /* proceed with rest of allocation */
530 page_pools.epp_free_pages -= desc->bd_max_iov;
532 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
533 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
535 for (i = 0; i < desc->bd_max_iov; i++) {
536 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
537 desc->bd_enc_pages[i] = page_pools.epp_pools[p_idx][g_idx];
538 page_pools.epp_pools[p_idx][g_idx] = NULL;
540 if (++g_idx == PAGES_PER_POOL) {
546 if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
547 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
550 * new idle index = (old * weight + new) / (weight + 1)
552 if (this_idle == -1) {
553 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
554 page_pools.epp_total_pages;
556 page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
558 (IDLE_IDX_WEIGHT + 1);
560 page_pools.epp_last_access = cfs_time_current_sec();
562 spin_unlock(&page_pools.epp_lock);
565 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
567 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
572 if (desc->bd_enc_pages == NULL)
574 if (desc->bd_max_iov == 0)
577 spin_lock(&page_pools.epp_lock);
579 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
580 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
582 LASSERT(page_pools.epp_free_pages + desc->bd_max_iov <=
583 page_pools.epp_total_pages);
584 LASSERT(page_pools.epp_pools[p_idx]);
586 for (i = 0; i < desc->bd_max_iov; i++) {
587 LASSERT(desc->bd_enc_pages[i] != NULL);
588 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
589 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
591 page_pools.epp_pools[p_idx][g_idx] = desc->bd_enc_pages[i];
593 if (++g_idx == PAGES_PER_POOL) {
599 page_pools.epp_free_pages += desc->bd_max_iov;
603 spin_unlock(&page_pools.epp_lock);
605 OBD_FREE(desc->bd_enc_pages,
606 desc->bd_max_iov * sizeof(*desc->bd_enc_pages));
607 desc->bd_enc_pages = NULL;
609 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
612 * we don't do much stuff for add_user/del_user anymore, except adding some
613 * initial pages in add_user() if current pools are empty, rest would be
614 * handled by the pools's self-adaption.
616 int sptlrpc_enc_pool_add_user(void)
620 spin_lock(&page_pools.epp_lock);
621 if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
622 page_pools.epp_growing = 1;
625 spin_unlock(&page_pools.epp_lock);
628 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES);
630 spin_lock(&page_pools.epp_lock);
631 page_pools.epp_growing = 0;
633 spin_unlock(&page_pools.epp_lock);
637 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
639 int sptlrpc_enc_pool_del_user(void)
643 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
645 static inline void enc_pools_alloc(void)
647 LASSERT(page_pools.epp_max_pools);
649 * on system with huge memory but small page size, this might lead to
650 * high-order allocation. but it's not common, and we suppose memory
651 * be not too much fragmented at module loading time.
653 OBD_ALLOC(page_pools.epp_pools,
654 page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
657 static inline void enc_pools_free(void)
659 LASSERT(page_pools.epp_max_pools);
660 LASSERT(page_pools.epp_pools);
662 OBD_FREE(page_pools.epp_pools,
663 page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
666 int sptlrpc_enc_pool_init(void)
669 * maximum capacity is 1/8 of total physical memory.
670 * is the 1/8 a good number?
672 page_pools.epp_max_pages = num_physpages / 8;
673 page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
675 cfs_waitq_init(&page_pools.epp_waitq);
676 page_pools.epp_waitqlen = 0;
677 page_pools.epp_pages_short = 0;
679 page_pools.epp_growing = 0;
681 page_pools.epp_idle_idx = 0;
682 page_pools.epp_last_shrink = cfs_time_current_sec();
683 page_pools.epp_last_access = cfs_time_current_sec();
685 spin_lock_init(&page_pools.epp_lock);
686 page_pools.epp_total_pages = 0;
687 page_pools.epp_free_pages = 0;
689 page_pools.epp_st_grows = 0;
690 page_pools.epp_st_grow_fails = 0;
691 page_pools.epp_st_shrinks = 0;
692 page_pools.epp_st_access = 0;
693 page_pools.epp_st_missings = 0;
694 page_pools.epp_st_lowfree = 0;
695 page_pools.epp_st_max_wqlen = 0;
696 page_pools.epp_st_max_wait = 0;
699 if (page_pools.epp_pools == NULL)
702 pools_shrinker = set_shrinker(pools_shrinker_seeks, enc_pools_shrink);
703 if (pools_shrinker == NULL) {
711 void sptlrpc_enc_pool_fini(void)
713 unsigned long cleaned, npools;
715 LASSERT(pools_shrinker);
716 LASSERT(page_pools.epp_pools);
717 LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
719 remove_shrinker(pools_shrinker);
721 npools = npages_to_npools(page_pools.epp_total_pages);
722 cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
723 LASSERT(cleaned == page_pools.epp_total_pages);
728 #else /* !__KERNEL__ */
730 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
735 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
739 int sptlrpc_enc_pool_init(void)
744 void sptlrpc_enc_pool_fini(void)
749 /****************************************
750 * Helpers to assist policy modules to *
751 * implement checksum funcationality *
752 ****************************************/
758 [BULK_CSUM_ALG_NULL] = { "null", 0 },
759 [BULK_CSUM_ALG_CRC32] = { "crc32", 4 },
760 [BULK_CSUM_ALG_MD5] = { "md5", 16 },
761 [BULK_CSUM_ALG_SHA1] = { "sha1", 20 },
762 [BULK_CSUM_ALG_SHA256] = { "sha256", 32 },
763 [BULK_CSUM_ALG_SHA384] = { "sha384", 48 },
764 [BULK_CSUM_ALG_SHA512] = { "sha512", 64 },
767 const char * sptlrpc_bulk_csum_alg2name(__u8 csum_alg)
769 if (csum_alg < BULK_CSUM_ALG_MAX)
770 return csum_types[csum_alg].name;
773 EXPORT_SYMBOL(sptlrpc_bulk_csum_alg2name);
775 int bulk_sec_desc_size(__u8 csum_alg, int request, int read)
777 int size = sizeof(struct ptlrpc_bulk_sec_desc);
779 LASSERT(csum_alg < BULK_CSUM_ALG_MAX);
781 /* read request don't need extra data */
782 if (!(read && request))
783 size += csum_types[csum_alg].size;
787 EXPORT_SYMBOL(bulk_sec_desc_size);
789 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset)
791 struct ptlrpc_bulk_sec_desc *bsd;
792 int size = msg->lm_buflens[offset];
794 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
796 CERROR("Invalid bulk sec desc: size %d\n", size);
800 if (lustre_msg_swabbed(msg)) {
801 __swab32s(&bsd->bsd_version);
802 __swab16s(&bsd->bsd_pad);
805 if (bsd->bsd_version != 0) {
806 CERROR("Unexpected version %u\n", bsd->bsd_version);
810 if (bsd->bsd_csum_alg >= BULK_CSUM_ALG_MAX) {
811 CERROR("Unsupported checksum algorithm %u\n",
815 if (bsd->bsd_priv_alg >= BULK_PRIV_ALG_MAX) {
816 CERROR("Unsupported cipher algorithm %u\n",
821 if (size > sizeof(*bsd) &&
822 size < sizeof(*bsd) + csum_types[bsd->bsd_csum_alg].size) {
823 CERROR("Mal-formed checksum data: csum alg %u, size %d\n",
824 bsd->bsd_csum_alg, size);
830 EXPORT_SYMBOL(bulk_sec_desc_unpack);
834 int do_bulk_checksum_crc32(struct ptlrpc_bulk_desc *desc, void *buf)
842 for (i = 0; i < desc->bd_iov_count; i++) {
843 page = desc->bd_iov[i].kiov_page;
844 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
845 ptr = cfs_kmap(page) + off;
846 len = desc->bd_iov[i].kiov_len;
848 crc32 = crc32_le(crc32, ptr, len);
853 *((__u32 *) buf) = crc32;
858 int do_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u32 alg, void *buf)
860 struct crypto_tfm *tfm;
861 struct scatterlist *sl;
864 LASSERT(alg > BULK_CSUM_ALG_NULL &&
865 alg < BULK_CSUM_ALG_MAX);
867 if (alg == BULK_CSUM_ALG_CRC32)
868 return do_bulk_checksum_crc32(desc, buf);
870 tfm = crypto_alloc_tfm(csum_types[alg].name, 0);
872 CERROR("Unable to allocate tfm %s\n", csum_types[alg].name);
876 OBD_ALLOC(sl, sizeof(*sl) * desc->bd_iov_count);
882 for (i = 0; i < desc->bd_iov_count; i++) {
883 sl[i].page = desc->bd_iov[i].kiov_page;
884 sl[i].offset = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
885 sl[i].length = desc->bd_iov[i].kiov_len;
888 crypto_digest_init(tfm);
889 crypto_digest_update(tfm, sl, desc->bd_iov_count);
890 crypto_digest_final(tfm, buf);
892 OBD_FREE(sl, sizeof(*sl) * desc->bd_iov_count);
895 crypto_free_tfm(tfm);
899 #else /* !__KERNEL__ */
901 int do_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u32 alg, void *buf)
906 LASSERT(alg == BULK_CSUM_ALG_CRC32);
908 for (i = 0; i < desc->bd_iov_count; i++) {
909 char *ptr = desc->bd_iov[i].iov_base;
910 int len = desc->bd_iov[i].iov_len;
912 crc32 = crc32_le(crc32, ptr, len);
915 *((__u32 *) buf) = crc32;
921 * perform algorithm @alg checksum on @desc, store result in @buf.
922 * if anything goes wrong, leave 'alg' be BULK_CSUM_ALG_NULL.
925 int generate_bulk_csum(struct ptlrpc_bulk_desc *desc, __u32 alg,
926 struct ptlrpc_bulk_sec_desc *bsd, int bsdsize)
931 LASSERT(alg < BULK_CSUM_ALG_MAX);
933 bsd->bsd_csum_alg = BULK_CSUM_ALG_NULL;
935 if (alg == BULK_CSUM_ALG_NULL)
938 LASSERT(bsdsize >= sizeof(*bsd) + csum_types[alg].size);
940 rc = do_bulk_checksum(desc, alg, bsd->bsd_csum);
942 bsd->bsd_csum_alg = alg;
948 int verify_bulk_csum(struct ptlrpc_bulk_desc *desc, int read,
949 struct ptlrpc_bulk_sec_desc *bsdv, int bsdvsize,
950 struct ptlrpc_bulk_sec_desc *bsdr, int bsdrsize)
954 int csum_size, rc = 0;
957 LASSERT(bsdv->bsd_csum_alg < BULK_CSUM_ALG_MAX);
960 bsdr->bsd_csum_alg = BULK_CSUM_ALG_NULL;
962 if (bsdv->bsd_csum_alg == BULK_CSUM_ALG_NULL)
965 /* for all supported algorithms */
966 csum_size = csum_types[bsdv->bsd_csum_alg].size;
968 if (bsdvsize < sizeof(*bsdv) + csum_size) {
969 CERROR("verifier size %d too small, require %d\n",
970 bsdvsize, (int) sizeof(*bsdv) + csum_size);
975 LASSERT(bsdrsize >= sizeof(*bsdr) + csum_size);
976 csum_p = (char *) bsdr->bsd_csum;
978 OBD_ALLOC(buf, csum_size);
984 rc = do_bulk_checksum(desc, bsdv->bsd_csum_alg, csum_p);
986 if (memcmp(bsdv->bsd_csum, csum_p, csum_size)) {
987 CERROR("BAD %s CHECKSUM (%s), data mutated during "
988 "transfer!\n", read ? "READ" : "WRITE",
989 csum_types[bsdv->bsd_csum_alg].name);
992 CDEBUG(D_SEC, "bulk %s checksum (%s) verified\n",
993 read ? "read" : "write",
994 csum_types[bsdv->bsd_csum_alg].name);
998 bsdr->bsd_csum_alg = bsdv->bsd_csum_alg;
999 memcpy(bsdr->bsd_csum, csum_p, csum_size);
1002 OBD_FREE(buf, csum_size);
1008 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
1009 __u32 alg, struct lustre_msg *rmsg, int roff)
1011 struct ptlrpc_bulk_sec_desc *bsdr;
1014 rsize = rmsg->lm_buflens[roff];
1015 bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
1018 LASSERT(rsize >= sizeof(*bsdr));
1019 LASSERT(alg < BULK_CSUM_ALG_MAX);
1022 bsdr->bsd_csum_alg = alg;
1024 rc = generate_bulk_csum(desc, alg, bsdr, rsize);
1026 CERROR("bulk write: client failed to compute "
1027 "checksum: %d\n", rc);
1029 /* For sending we only compute the wrong checksum instead
1030 * of corrupting the data so it is still correct on a redo */
1031 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
1032 bsdr->bsd_csum_alg != BULK_CSUM_ALG_NULL)
1033 bsdr->bsd_csum[0] ^= 0x1;
1038 EXPORT_SYMBOL(bulk_csum_cli_request);
1040 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
1041 struct lustre_msg *rmsg, int roff,
1042 struct lustre_msg *vmsg, int voff)
1044 struct ptlrpc_bulk_sec_desc *bsdv, *bsdr;
1047 rsize = rmsg->lm_buflens[roff];
1048 vsize = vmsg->lm_buflens[voff];
1049 bsdr = lustre_msg_buf(rmsg, roff, 0);
1050 bsdv = lustre_msg_buf(vmsg, voff, 0);
1052 if (bsdv == NULL || vsize < sizeof(*bsdv)) {
1053 CERROR("Invalid checksum verifier from server: size %d\n",
1059 LASSERT(rsize >= sizeof(*bsdr));
1060 LASSERT(vsize >= sizeof(*bsdv));
1062 if (bsdr->bsd_csum_alg != bsdv->bsd_csum_alg) {
1063 CERROR("bulk %s: checksum algorithm mismatch: client request "
1064 "%s but server reply with %s. try to use the new one "
1065 "for checksum verification\n",
1066 read ? "read" : "write",
1067 csum_types[bsdr->bsd_csum_alg].name,
1068 csum_types[bsdv->bsd_csum_alg].name);
1072 return verify_bulk_csum(desc, 1, bsdv, vsize, NULL, 0);
1074 char *cli, *srv, *new = NULL;
1075 int csum_size = csum_types[bsdr->bsd_csum_alg].size;
1077 LASSERT(bsdr->bsd_csum_alg < BULK_CSUM_ALG_MAX);
1078 if (bsdr->bsd_csum_alg == BULK_CSUM_ALG_NULL)
1081 if (vsize < sizeof(*bsdv) + csum_size) {
1082 CERROR("verifier size %d too small, require %d\n",
1083 vsize, (int) sizeof(*bsdv) + csum_size);
1087 cli = (char *) (bsdr + 1);
1088 srv = (char *) (bsdv + 1);
1090 if (!memcmp(cli, srv, csum_size)) {
1091 /* checksum confirmed */
1092 CDEBUG(D_SEC, "bulk write checksum (%s) confirmed\n",
1093 csum_types[bsdr->bsd_csum_alg].name);
1097 /* checksum mismatch, re-compute a new one and compare with
1098 * others, give out proper warnings. */
1099 OBD_ALLOC(new, csum_size);
1103 do_bulk_checksum(desc, bsdr->bsd_csum_alg, new);
1105 if (!memcmp(new, srv, csum_size)) {
1106 CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1107 "on the client after we checksummed them\n",
1108 csum_types[bsdr->bsd_csum_alg].name);
1109 } else if (!memcmp(new, cli, csum_size)) {
1110 CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1112 csum_types[bsdr->bsd_csum_alg].name);
1114 CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1115 "in transit, and the current page contents "
1116 "don't match the originals and what the server "
1118 csum_types[bsdr->bsd_csum_alg].name);
1120 OBD_FREE(new, csum_size);
1125 EXPORT_SYMBOL(bulk_csum_cli_reply);
1128 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
1131 unsigned int off, i;
1133 for (i = 0; i < desc->bd_iov_count; i++) {
1134 if (desc->bd_iov[i].kiov_len == 0)
1137 ptr = cfs_kmap(desc->bd_iov[i].kiov_page);
1138 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
1140 cfs_kunmap(desc->bd_iov[i].kiov_page);
1145 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
1148 #endif /* __KERNEL__ */
1150 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
1151 struct ptlrpc_bulk_sec_desc *bsdv, int vsize,
1152 struct ptlrpc_bulk_sec_desc *bsdr, int rsize)
1156 LASSERT(vsize >= sizeof(*bsdv));
1157 LASSERT(rsize >= sizeof(*bsdr));
1158 LASSERT(bsdv && bsdr);
1161 rc = generate_bulk_csum(desc, bsdv->bsd_csum_alg, bsdr, rsize);
1163 CERROR("bulk read: server failed to generate %s "
1165 csum_types[bsdv->bsd_csum_alg].name, rc);
1167 /* corrupt the data after we compute the checksum, to
1168 * simulate an OST->client data error */
1169 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
1170 corrupt_bulk_data(desc);
1172 rc = verify_bulk_csum(desc, 0, bsdv, vsize, bsdr, rsize);
1177 EXPORT_SYMBOL(bulk_csum_svc);
1179 /****************************************
1180 * Helpers to assist policy modules to *
1181 * implement encryption funcationality *
1182 ****************************************/
1185 * NOTE: These algorithms must be stream cipher!
1191 [BULK_PRIV_ALG_NULL] = { "null", 0 },
1192 [BULK_PRIV_ALG_ARC4] = { "arc4", 0 },
1195 const char * sptlrpc_bulk_priv_alg2name(__u8 priv_alg)
1197 if (priv_alg < BULK_PRIV_ALG_MAX)
1198 return priv_types[priv_alg].name;
1201 EXPORT_SYMBOL(sptlrpc_bulk_priv_alg2name);
1203 __u32 sptlrpc_bulk_priv_alg2flags(__u8 priv_alg)
1205 if (priv_alg < BULK_PRIV_ALG_MAX)
1206 return priv_types[priv_alg].flags;
1209 EXPORT_SYMBOL(sptlrpc_bulk_priv_alg2flags);