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>
34 #include <linux/crypto.h>
35 #include <linux/zutil.h>
39 #include <obd_class.h>
40 #include <obd_support.h>
41 #include <lustre_net.h>
42 #include <lustre_import.h>
43 #include <lustre_dlm.h>
44 #include <lustre_sec.h>
46 #include "ptlrpc_internal.h"
48 /****************************************
49 * bulk encryption page pools *
50 ****************************************/
54 #define PTRS_PER_PAGE (CFS_PAGE_SIZE / sizeof(void *))
55 #define PAGES_PER_POOL (PTRS_PER_PAGE)
57 #define IDLE_IDX_MAX (100)
58 #define IDLE_IDX_WEIGHT (3)
60 #define CACHE_QUIESCENCE_PERIOD (20)
62 static struct ptlrpc_enc_page_pool {
66 unsigned long epp_max_pages; /* maximum pages can hold, const */
67 unsigned int epp_max_pools; /* number of pools, const */
70 * wait queue in case of not enough free pages.
72 cfs_waitq_t epp_waitq; /* waiting threads */
73 unsigned int epp_waitqlen; /* wait queue length */
74 unsigned long epp_pages_short; /* # of pages wanted of in-q users */
75 unsigned int epp_growing:1; /* during adding pages */
78 * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
79 * this is counted based on each time when getting pages from
80 * the pools, not based on time. which means in case that system
81 * is idled for a while but the idle_idx might still be low if no
82 * activities happened in the pools.
84 unsigned long epp_idle_idx;
86 /* last shrink time due to mem tight */
91 * in-pool pages bookkeeping
93 spinlock_t epp_lock; /* protect following fields */
94 unsigned long epp_total_pages; /* total pages in pools */
95 unsigned long epp_free_pages; /* current pages available */
100 unsigned int epp_st_grows; /* # of grows */
101 unsigned int epp_st_grow_fails; /* # of add pages failures */
102 unsigned int epp_st_shrinks; /* # of shrinks */
103 unsigned long epp_st_access; /* # of access */
104 unsigned long epp_st_missings; /* # of cache missing */
105 unsigned long epp_st_lowfree; /* lowest free pages reached */
106 unsigned long epp_st_max_wqlen; /* highest waitqueue length */
107 cfs_time_t epp_st_max_wait; /* in jeffies */
111 cfs_page_t ***epp_pools;
117 const int pools_shrinker_seeks = DEFAULT_SEEKS;
118 static struct shrinker *pools_shrinker = NULL;
122 * /proc/fs/lustre/sptlrpc/encrypt_page_pools
124 int sptlrpc_proc_read_enc_pool(char *page, char **start, off_t off, int count,
125 int *eof, void *data)
129 spin_lock(&page_pools.epp_lock);
131 rc = snprintf(page, count,
132 "physical pages: %lu\n"
133 "pages per pool: %lu\n"
138 "idle index: %lu/100\n"
139 "last shrink: %lds\n"
140 "last access: %lds\n"
142 "grows failure: %u\n"
144 "cache access: %lu\n"
145 "cache missing: %lu\n"
146 "low free mark: %lu\n"
147 "max waitqueue depth: %lu\n"
148 "max wait time: "CFS_TIME_T"/%u\n"
152 page_pools.epp_max_pages,
153 page_pools.epp_max_pools,
154 page_pools.epp_total_pages,
155 page_pools.epp_free_pages,
156 page_pools.epp_idle_idx,
157 cfs_time_current_sec() - page_pools.epp_last_shrink,
158 cfs_time_current_sec() - page_pools.epp_last_access,
159 page_pools.epp_st_grows,
160 page_pools.epp_st_grow_fails,
161 page_pools.epp_st_shrinks,
162 page_pools.epp_st_access,
163 page_pools.epp_st_missings,
164 page_pools.epp_st_lowfree,
165 page_pools.epp_st_max_wqlen,
166 page_pools.epp_st_max_wait, HZ
169 spin_unlock(&page_pools.epp_lock);
173 static void enc_pools_release_free_pages(long npages)
177 LASSERT(npages <= page_pools.epp_free_pages);
179 p_idx = (page_pools.epp_free_pages - 1) / PAGES_PER_POOL;
180 g_idx = (page_pools.epp_free_pages - 1) % PAGES_PER_POOL;
181 LASSERT(page_pools.epp_pools[p_idx]);
183 page_pools.epp_free_pages -= npages;
184 page_pools.epp_total_pages -= npages;
186 while (npages-- > 0) {
187 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
189 cfs_free_page(page_pools.epp_pools[p_idx][g_idx]);
190 page_pools.epp_pools[p_idx][g_idx] = NULL;
194 g_idx = PAGES_PER_POOL - 1;
196 LASSERT(page_pools.epp_pools[p_idx]);
202 * could be called frequently for query (@nr_to_scan == 0)
204 static int enc_pools_shrink(int nr_to_scan, unsigned int gfp_mask)
208 spin_lock(&page_pools.epp_lock);
211 if (nr_to_scan > page_pools.epp_free_pages)
212 nr_to_scan = page_pools.epp_free_pages;
214 enc_pools_release_free_pages(nr_to_scan);
215 CDEBUG(D_SEC, "released %d pages, %ld left\n",
216 nr_to_scan, page_pools.epp_free_pages);
218 page_pools.epp_st_shrinks++;
219 page_pools.epp_last_shrink = cfs_time_current_sec();
223 * try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool
225 if (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES) {
231 * if no pool access for a long time, we consider it's fully idle
233 if (cfs_time_current_sec() - page_pools.epp_last_access >
234 CACHE_QUIESCENCE_PERIOD)
235 page_pools.epp_idle_idx = IDLE_IDX_MAX;
237 LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
238 ret = (page_pools.epp_free_pages * page_pools.epp_idle_idx /
240 if (page_pools.epp_free_pages - ret < PTLRPC_MAX_BRW_PAGES)
241 ret = page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES;
244 spin_unlock(&page_pools.epp_lock);
249 int npages_to_npools(unsigned long npages)
251 return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
255 * return how many pages cleaned up.
257 static unsigned long enc_pools_cleanup(cfs_page_t ***pools, int npools)
259 unsigned long cleaned = 0;
262 for (i = 0; i < npools; i++) {
264 for (j = 0; j < PAGES_PER_POOL; j++) {
266 cfs_free_page(pools[i][j]);
270 OBD_FREE(pools[i], CFS_PAGE_SIZE);
279 * merge @npools pointed by @pools which contains @npages new pages
280 * into current pools.
282 * we have options to avoid most memory copy with some tricks. but we choose
283 * the simplest way to avoid complexity. It's not frequently called.
285 static void enc_pools_insert(cfs_page_t ***pools, int npools, int npages)
288 int op_idx, np_idx, og_idx, ng_idx;
289 int cur_npools, end_npools;
292 LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
293 LASSERT(npages_to_npools(npages) == npools);
295 spin_lock(&page_pools.epp_lock);
298 * (1) fill all the free slots of current pools.
300 /* free slots are those left by rent pages, and the extra ones with
301 * index >= eep_total_pages, locate at the tail of last pool. */
302 freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
304 freeslot = PAGES_PER_POOL - freeslot;
305 freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
307 op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
308 og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
310 ng_idx = (npages - 1) % PAGES_PER_POOL;
313 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
314 LASSERT(pools[np_idx][ng_idx] != NULL);
316 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
317 pools[np_idx][ng_idx] = NULL;
321 if (++og_idx == PAGES_PER_POOL) {
329 ng_idx = PAGES_PER_POOL - 1;
334 * (2) add pools if needed.
336 cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
338 end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
340 LASSERT(end_npools <= page_pools.epp_max_pools);
343 while (cur_npools < end_npools) {
344 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
345 LASSERT(np_idx < npools);
346 LASSERT(pools[np_idx] != NULL);
348 page_pools.epp_pools[cur_npools++] = pools[np_idx];
349 pools[np_idx++] = NULL;
352 page_pools.epp_total_pages += npages;
353 page_pools.epp_free_pages += npages;
354 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
356 CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
357 page_pools.epp_total_pages);
359 spin_unlock(&page_pools.epp_lock);
362 static int enc_pools_add_pages(int npages)
364 static DECLARE_MUTEX(sem_add_pages);
366 int npools, alloced = 0;
367 int i, j, rc = -ENOMEM;
369 if (npages < PTLRPC_MAX_BRW_PAGES)
370 npages = PTLRPC_MAX_BRW_PAGES;
372 down(&sem_add_pages);
374 if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
375 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
378 page_pools.epp_st_grows++;
380 npools = npages_to_npools(npages);
381 OBD_ALLOC(pools, npools * sizeof(*pools));
385 for (i = 0; i < npools; i++) {
386 OBD_ALLOC(pools[i], CFS_PAGE_SIZE);
387 if (pools[i] == NULL)
390 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
391 pools[i][j] = cfs_alloc_page(CFS_ALLOC_IO |
393 if (pools[i][j] == NULL)
400 enc_pools_insert(pools, npools, npages);
401 CDEBUG(D_SEC, "added %d pages into pools\n", npages);
405 enc_pools_cleanup(pools, npools);
406 OBD_FREE(pools, npools * sizeof(*pools));
409 page_pools.epp_st_grow_fails++;
410 CERROR("Failed to allocate %d enc pages\n", npages);
417 static inline void enc_pools_wakeup(void)
419 if (unlikely(page_pools.epp_waitqlen)) {
420 LASSERT(page_pools.epp_waitqlen > 0);
421 LASSERT(cfs_waitq_active(&page_pools.epp_waitq));
422 cfs_waitq_broadcast(&page_pools.epp_waitq);
426 static int enc_pools_should_grow(int page_needed, long now)
428 /* don't grow if someone else is growing the pools right now,
429 * or the pools has reached its full capacity
431 if (page_pools.epp_growing ||
432 page_pools.epp_total_pages == page_pools.epp_max_pages)
435 /* if total pages is not enough, we need to grow */
436 if (page_pools.epp_total_pages < page_needed)
439 /* if we just did a shrink due to memory tight, we'd better
440 * wait a while to grow again.
442 if (now - page_pools.epp_last_shrink < 2)
446 * here we perhaps need consider other factors like wait queue
447 * length, idle index, etc. ?
450 /* grow the pools in any other cases */
455 * we allocate the requested pages atomically.
457 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
459 cfs_waitlink_t waitlink;
460 unsigned long this_idle = -1;
466 LASSERT(desc->bd_max_iov > 0);
467 LASSERT(desc->bd_max_iov <= page_pools.epp_max_pages);
469 /* resent bulk, enc pages might have been allocated previously */
470 if (desc->bd_enc_pages != NULL)
473 OBD_ALLOC(desc->bd_enc_pages,
474 desc->bd_max_iov * sizeof(*desc->bd_enc_pages));
475 if (desc->bd_enc_pages == NULL)
478 spin_lock(&page_pools.epp_lock);
480 page_pools.epp_st_access++;
482 if (unlikely(page_pools.epp_free_pages < desc->bd_max_iov)) {
484 tick = cfs_time_current();
486 now = cfs_time_current_sec();
488 page_pools.epp_st_missings++;
489 page_pools.epp_pages_short += desc->bd_max_iov;
491 if (enc_pools_should_grow(desc->bd_max_iov, now)) {
492 page_pools.epp_growing = 1;
494 spin_unlock(&page_pools.epp_lock);
495 enc_pools_add_pages(page_pools.epp_pages_short / 2);
496 spin_lock(&page_pools.epp_lock);
498 page_pools.epp_growing = 0;
500 if (++page_pools.epp_waitqlen >
501 page_pools.epp_st_max_wqlen)
502 page_pools.epp_st_max_wqlen =
503 page_pools.epp_waitqlen;
505 set_current_state(TASK_UNINTERRUPTIBLE);
506 cfs_waitlink_init(&waitlink);
507 cfs_waitq_add(&page_pools.epp_waitq, &waitlink);
509 spin_unlock(&page_pools.epp_lock);
511 spin_lock(&page_pools.epp_lock);
513 LASSERT(page_pools.epp_waitqlen > 0);
514 page_pools.epp_waitqlen--;
517 LASSERT(page_pools.epp_pages_short >= desc->bd_max_iov);
518 page_pools.epp_pages_short -= desc->bd_max_iov;
524 /* record max wait time */
525 if (unlikely(tick != 0)) {
526 tick = cfs_time_current() - tick;
527 if (tick > page_pools.epp_st_max_wait)
528 page_pools.epp_st_max_wait = tick;
531 /* proceed with rest of allocation */
532 page_pools.epp_free_pages -= desc->bd_max_iov;
534 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
535 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
537 for (i = 0; i < desc->bd_max_iov; i++) {
538 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
539 desc->bd_enc_pages[i] = page_pools.epp_pools[p_idx][g_idx];
540 page_pools.epp_pools[p_idx][g_idx] = NULL;
542 if (++g_idx == PAGES_PER_POOL) {
548 if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
549 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
552 * new idle index = (old * weight + new) / (weight + 1)
554 if (this_idle == -1) {
555 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
556 page_pools.epp_total_pages;
558 page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
560 (IDLE_IDX_WEIGHT + 1);
562 page_pools.epp_last_access = cfs_time_current_sec();
564 spin_unlock(&page_pools.epp_lock);
567 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
569 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
574 if (desc->bd_enc_pages == NULL)
576 if (desc->bd_max_iov == 0)
579 spin_lock(&page_pools.epp_lock);
581 p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
582 g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
584 LASSERT(page_pools.epp_free_pages + desc->bd_max_iov <=
585 page_pools.epp_total_pages);
586 LASSERT(page_pools.epp_pools[p_idx]);
588 for (i = 0; i < desc->bd_max_iov; i++) {
589 LASSERT(desc->bd_enc_pages[i] != NULL);
590 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
591 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
593 page_pools.epp_pools[p_idx][g_idx] = desc->bd_enc_pages[i];
595 if (++g_idx == PAGES_PER_POOL) {
601 page_pools.epp_free_pages += desc->bd_max_iov;
605 spin_unlock(&page_pools.epp_lock);
607 OBD_FREE(desc->bd_enc_pages,
608 desc->bd_max_iov * sizeof(*desc->bd_enc_pages));
609 desc->bd_enc_pages = NULL;
611 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
614 * we don't do much stuff for add_user/del_user anymore, except adding some
615 * initial pages in add_user() if current pools are empty, rest would be
616 * handled by the pools's self-adaption.
618 int sptlrpc_enc_pool_add_user(void)
622 spin_lock(&page_pools.epp_lock);
623 if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
624 page_pools.epp_growing = 1;
627 spin_unlock(&page_pools.epp_lock);
630 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES);
632 spin_lock(&page_pools.epp_lock);
633 page_pools.epp_growing = 0;
635 spin_unlock(&page_pools.epp_lock);
639 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
641 int sptlrpc_enc_pool_del_user(void)
645 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
647 static inline void enc_pools_alloc(void)
649 LASSERT(page_pools.epp_max_pools);
651 * on system with huge memory but small page size, this might lead to
652 * high-order allocation. but it's not common, and we suppose memory
653 * be not too much fragmented at module loading time.
655 OBD_ALLOC(page_pools.epp_pools,
656 page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
659 static inline void enc_pools_free(void)
661 LASSERT(page_pools.epp_max_pools);
662 LASSERT(page_pools.epp_pools);
664 OBD_FREE(page_pools.epp_pools,
665 page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
668 int sptlrpc_enc_pool_init(void)
671 * maximum capacity is 1/8 of total physical memory.
672 * is the 1/8 a good number?
674 page_pools.epp_max_pages = num_physpages / 8;
675 page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
677 cfs_waitq_init(&page_pools.epp_waitq);
678 page_pools.epp_waitqlen = 0;
679 page_pools.epp_pages_short = 0;
681 page_pools.epp_growing = 0;
683 page_pools.epp_idle_idx = 0;
684 page_pools.epp_last_shrink = cfs_time_current_sec();
685 page_pools.epp_last_access = cfs_time_current_sec();
687 spin_lock_init(&page_pools.epp_lock);
688 page_pools.epp_total_pages = 0;
689 page_pools.epp_free_pages = 0;
691 page_pools.epp_st_grows = 0;
692 page_pools.epp_st_grow_fails = 0;
693 page_pools.epp_st_shrinks = 0;
694 page_pools.epp_st_access = 0;
695 page_pools.epp_st_missings = 0;
696 page_pools.epp_st_lowfree = 0;
697 page_pools.epp_st_max_wqlen = 0;
698 page_pools.epp_st_max_wait = 0;
701 if (page_pools.epp_pools == NULL)
704 pools_shrinker = set_shrinker(pools_shrinker_seeks, enc_pools_shrink);
705 if (pools_shrinker == NULL) {
713 void sptlrpc_enc_pool_fini(void)
715 unsigned long cleaned, npools;
717 LASSERT(pools_shrinker);
718 LASSERT(page_pools.epp_pools);
719 LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
721 remove_shrinker(pools_shrinker);
723 npools = npages_to_npools(page_pools.epp_total_pages);
724 cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
725 LASSERT(cleaned == page_pools.epp_total_pages);
730 #else /* !__KERNEL__ */
732 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
737 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
741 int sptlrpc_enc_pool_init(void)
746 void sptlrpc_enc_pool_fini(void)
751 /****************************************
752 * Helpers to assist policy modules to *
753 * implement checksum funcationality *
754 ****************************************/
756 static struct sptlrpc_hash_type hash_types[] = {
757 [BULK_HASH_ALG_NULL] = { "null", "null", 0 },
758 [BULK_HASH_ALG_ADLER32] = { "adler32", "adler32", 4 },
759 [BULK_HASH_ALG_CRC32] = { "crc32", "crc32", 4 },
760 [BULK_HASH_ALG_MD5] = { "md5", "md5", 16 },
761 [BULK_HASH_ALG_SHA1] = { "sha1", "sha1", 20 },
762 [BULK_HASH_ALG_SHA256] = { "sha256", "sha256", 32 },
763 [BULK_HASH_ALG_SHA384] = { "sha384", "sha384", 48 },
764 [BULK_HASH_ALG_SHA512] = { "sha512", "sha512", 64 },
765 [BULK_HASH_ALG_WP256] = { "wp256", "wp256", 32 },
766 [BULK_HASH_ALG_WP384] = { "wp384", "wp384", 48 },
767 [BULK_HASH_ALG_WP512] = { "wp512", "wp512", 64 },
770 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg)
772 struct sptlrpc_hash_type *ht;
774 if (hash_alg < BULK_HASH_ALG_MAX) {
775 ht = &hash_types[hash_alg];
776 if (ht->sht_tfm_name)
781 EXPORT_SYMBOL(sptlrpc_get_hash_type);
783 const char * sptlrpc_get_hash_name(__u8 hash_alg)
785 const struct sptlrpc_hash_type *ht;
787 ht = sptlrpc_get_hash_type(hash_alg);
793 EXPORT_SYMBOL(sptlrpc_get_hash_name);
795 int bulk_sec_desc_size(__u8 hash_alg, int request, int read)
797 int size = sizeof(struct ptlrpc_bulk_sec_desc);
799 LASSERT(hash_alg < BULK_HASH_ALG_MAX);
801 /* read request don't need extra data */
802 if (!(read && request))
803 size += hash_types[hash_alg].sht_size;
807 EXPORT_SYMBOL(bulk_sec_desc_size);
809 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset)
811 struct ptlrpc_bulk_sec_desc *bsd;
812 int size = msg->lm_buflens[offset];
814 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
816 CERROR("Invalid bulk sec desc: size %d\n", size);
820 /* nothing to swab */
822 if (unlikely(bsd->bsd_version != 0)) {
823 CERROR("Unexpected version %u\n", bsd->bsd_version);
827 if (unlikely(bsd->bsd_flags != 0)) {
828 CERROR("Unexpected flags %x\n", bsd->bsd_flags);
832 if (unlikely(!sptlrpc_get_hash_type(bsd->bsd_hash_alg))) {
833 CERROR("Unsupported checksum algorithm %u\n",
838 if (unlikely(!sptlrpc_get_ciph_type(bsd->bsd_ciph_alg))) {
839 CERROR("Unsupported cipher algorithm %u\n",
844 if (unlikely(size > sizeof(*bsd)) &&
845 size < sizeof(*bsd) + hash_types[bsd->bsd_hash_alg].sht_size) {
846 CERROR("Mal-formed checksum data: csum alg %u, size %d\n",
847 bsd->bsd_hash_alg, size);
853 EXPORT_SYMBOL(bulk_sec_desc_unpack);
857 static int do_bulk_checksum_adler32(struct ptlrpc_bulk_desc *desc, void *buf)
865 for (i = 0; i < desc->bd_iov_count; i++) {
866 page = desc->bd_iov[i].kiov_page;
867 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
868 ptr = cfs_kmap(page) + off;
869 len = desc->bd_iov[i].kiov_len;
871 adler32 = zlib_adler32(adler32, ptr, len);
876 adler32 = cpu_to_le32(adler32);
877 memcpy(buf, &adler32, sizeof(adler32));
881 static int do_bulk_checksum_crc32(struct ptlrpc_bulk_desc *desc, void *buf)
889 for (i = 0; i < desc->bd_iov_count; i++) {
890 page = desc->bd_iov[i].kiov_page;
891 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
892 ptr = cfs_kmap(page) + off;
893 len = desc->bd_iov[i].kiov_len;
895 crc32 = crc32_le(crc32, ptr, len);
900 crc32 = cpu_to_le32(crc32);
901 memcpy(buf, &crc32, sizeof(crc32));
905 static int do_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u32 alg, void *buf)
907 struct crypto_tfm *tfm;
908 struct scatterlist *sl;
911 LASSERT(alg > BULK_HASH_ALG_NULL &&
912 alg < BULK_HASH_ALG_MAX);
914 if (alg == BULK_HASH_ALG_ADLER32)
915 return do_bulk_checksum_adler32(desc, buf);
916 if (alg == BULK_HASH_ALG_CRC32)
917 return do_bulk_checksum_crc32(desc, buf);
919 tfm = crypto_alloc_tfm(hash_types[alg].sht_tfm_name, 0);
921 CERROR("Unable to allocate TFM %s\n", hash_types[alg].sht_name);
925 OBD_ALLOC(sl, sizeof(*sl) * desc->bd_iov_count);
931 for (i = 0; i < desc->bd_iov_count; i++) {
932 sl[i].page = desc->bd_iov[i].kiov_page;
933 sl[i].offset = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
934 sl[i].length = desc->bd_iov[i].kiov_len;
937 crypto_digest_init(tfm);
938 crypto_digest_update(tfm, sl, desc->bd_iov_count);
939 crypto_digest_final(tfm, buf);
941 OBD_FREE(sl, sizeof(*sl) * desc->bd_iov_count);
944 crypto_free_tfm(tfm);
948 #else /* !__KERNEL__ */
950 static int do_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u32 alg, void *buf)
955 LASSERT(alg == BULK_HASH_ALG_ADLER32 || alg == BULK_HASH_ALG_CRC32);
957 if (alg == BULK_HASH_ALG_ADLER32)
962 for (i = 0; i < desc->bd_iov_count; i++) {
963 char *ptr = desc->bd_iov[i].iov_base;
964 int len = desc->bd_iov[i].iov_len;
966 if (alg == BULK_HASH_ALG_ADLER32)
967 csum32 = zlib_adler32(csum32, ptr, len);
969 csum32 = crc32_le(csum32, ptr, len);
972 *((__u32 *) buf) = csum32;
979 * perform algorithm @alg checksum on @desc, store result in @buf.
980 * if anything goes wrong, leave 'alg' be BULK_HASH_ALG_NULL.
983 int generate_bulk_csum(struct ptlrpc_bulk_desc *desc, __u32 alg,
984 struct ptlrpc_bulk_sec_desc *bsd, int bsdsize)
989 LASSERT(alg < BULK_HASH_ALG_MAX);
991 bsd->bsd_hash_alg = BULK_HASH_ALG_NULL;
993 if (alg == BULK_HASH_ALG_NULL)
996 LASSERT(bsdsize >= sizeof(*bsd) + hash_types[alg].sht_size);
998 rc = do_bulk_checksum(desc, alg, bsd->bsd_csum);
1000 bsd->bsd_hash_alg = alg;
1006 int verify_bulk_csum(struct ptlrpc_bulk_desc *desc, int read,
1007 struct ptlrpc_bulk_sec_desc *bsdv, int bsdvsize,
1008 struct ptlrpc_bulk_sec_desc *bsdr, int bsdrsize)
1012 int csum_size, rc = 0;
1015 LASSERT(bsdv->bsd_hash_alg < BULK_HASH_ALG_MAX);
1018 bsdr->bsd_hash_alg = BULK_HASH_ALG_NULL;
1020 if (bsdv->bsd_hash_alg == BULK_HASH_ALG_NULL)
1023 /* for all supported algorithms */
1024 csum_size = hash_types[bsdv->bsd_hash_alg].sht_size;
1026 if (bsdvsize < sizeof(*bsdv) + csum_size) {
1027 CERROR("verifier size %d too small, require %d\n",
1028 bsdvsize, (int) sizeof(*bsdv) + csum_size);
1033 LASSERT(bsdrsize >= sizeof(*bsdr) + csum_size);
1034 csum_p = (char *) bsdr->bsd_csum;
1036 OBD_ALLOC(buf, csum_size);
1042 rc = do_bulk_checksum(desc, bsdv->bsd_hash_alg, csum_p);
1044 if (memcmp(bsdv->bsd_csum, csum_p, csum_size)) {
1045 CERROR("BAD %s CHECKSUM (%s), data mutated during "
1046 "transfer!\n", read ? "READ" : "WRITE",
1047 hash_types[bsdv->bsd_hash_alg].sht_name);
1050 CDEBUG(D_SEC, "bulk %s checksum (%s) verified\n",
1051 read ? "read" : "write",
1052 hash_types[bsdv->bsd_hash_alg].sht_name);
1056 bsdr->bsd_hash_alg = bsdv->bsd_hash_alg;
1057 memcpy(bsdr->bsd_csum, csum_p, csum_size);
1060 OBD_FREE(buf, csum_size);
1066 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
1067 __u32 alg, struct lustre_msg *rmsg, int roff)
1069 struct ptlrpc_bulk_sec_desc *bsdr;
1072 rsize = rmsg->lm_buflens[roff];
1073 bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
1076 LASSERT(rsize >= sizeof(*bsdr));
1077 LASSERT(alg < BULK_HASH_ALG_MAX);
1080 bsdr->bsd_hash_alg = alg;
1082 rc = generate_bulk_csum(desc, alg, bsdr, rsize);
1084 CERROR("bulk write: client failed to compute "
1085 "checksum: %d\n", rc);
1087 /* For sending we only compute the wrong checksum instead
1088 * of corrupting the data so it is still correct on a redo */
1089 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
1090 bsdr->bsd_hash_alg != BULK_HASH_ALG_NULL)
1091 bsdr->bsd_csum[0] ^= 0x1;
1096 EXPORT_SYMBOL(bulk_csum_cli_request);
1098 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
1099 struct lustre_msg *rmsg, int roff,
1100 struct lustre_msg *vmsg, int voff)
1102 struct ptlrpc_bulk_sec_desc *bsdv, *bsdr;
1105 rsize = rmsg->lm_buflens[roff];
1106 vsize = vmsg->lm_buflens[voff];
1107 bsdr = lustre_msg_buf(rmsg, roff, 0);
1108 bsdv = lustre_msg_buf(vmsg, voff, 0);
1110 if (bsdv == NULL || vsize < sizeof(*bsdv)) {
1111 CERROR("Invalid checksum verifier from server: size %d\n",
1117 LASSERT(rsize >= sizeof(*bsdr));
1118 LASSERT(vsize >= sizeof(*bsdv));
1120 if (bsdr->bsd_hash_alg != bsdv->bsd_hash_alg) {
1121 CERROR("bulk %s: checksum algorithm mismatch: client request "
1122 "%s but server reply with %s. try to use the new one "
1123 "for checksum verification\n",
1124 read ? "read" : "write",
1125 hash_types[bsdr->bsd_hash_alg].sht_name,
1126 hash_types[bsdv->bsd_hash_alg].sht_name);
1130 return verify_bulk_csum(desc, 1, bsdv, vsize, NULL, 0);
1132 char *cli, *srv, *new = NULL;
1133 int csum_size = hash_types[bsdr->bsd_hash_alg].sht_size;
1135 LASSERT(bsdr->bsd_hash_alg < BULK_HASH_ALG_MAX);
1136 if (bsdr->bsd_hash_alg == BULK_HASH_ALG_NULL)
1139 if (vsize < sizeof(*bsdv) + csum_size) {
1140 CERROR("verifier size %d too small, require %d\n",
1141 vsize, (int) sizeof(*bsdv) + csum_size);
1145 cli = (char *) (bsdr + 1);
1146 srv = (char *) (bsdv + 1);
1148 if (!memcmp(cli, srv, csum_size)) {
1149 /* checksum confirmed */
1150 CDEBUG(D_SEC, "bulk write checksum (%s) confirmed\n",
1151 hash_types[bsdr->bsd_hash_alg].sht_name);
1155 /* checksum mismatch, re-compute a new one and compare with
1156 * others, give out proper warnings. */
1157 OBD_ALLOC(new, csum_size);
1161 do_bulk_checksum(desc, bsdr->bsd_hash_alg, new);
1163 if (!memcmp(new, srv, csum_size)) {
1164 CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1165 "on the client after we checksummed them\n",
1166 hash_types[bsdr->bsd_hash_alg].sht_name);
1167 } else if (!memcmp(new, cli, csum_size)) {
1168 CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1170 hash_types[bsdr->bsd_hash_alg].sht_name);
1172 CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1173 "in transit, and the current page contents "
1174 "don't match the originals and what the server "
1176 hash_types[bsdr->bsd_hash_alg].sht_name);
1178 OBD_FREE(new, csum_size);
1183 EXPORT_SYMBOL(bulk_csum_cli_reply);
1186 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
1189 unsigned int off, i;
1191 for (i = 0; i < desc->bd_iov_count; i++) {
1192 if (desc->bd_iov[i].kiov_len == 0)
1195 ptr = cfs_kmap(desc->bd_iov[i].kiov_page);
1196 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
1198 cfs_kunmap(desc->bd_iov[i].kiov_page);
1203 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
1206 #endif /* __KERNEL__ */
1208 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
1209 struct ptlrpc_bulk_sec_desc *bsdv, int vsize,
1210 struct ptlrpc_bulk_sec_desc *bsdr, int rsize)
1214 LASSERT(vsize >= sizeof(*bsdv));
1215 LASSERT(rsize >= sizeof(*bsdr));
1216 LASSERT(bsdv && bsdr);
1219 rc = generate_bulk_csum(desc, bsdv->bsd_hash_alg, bsdr, rsize);
1221 CERROR("bulk read: server failed to generate %s "
1223 hash_types[bsdv->bsd_hash_alg].sht_name, rc);
1225 /* corrupt the data after we compute the checksum, to
1226 * simulate an OST->client data error */
1227 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
1228 corrupt_bulk_data(desc);
1230 rc = verify_bulk_csum(desc, 0, bsdv, vsize, bsdr, rsize);
1235 EXPORT_SYMBOL(bulk_csum_svc);
1237 /****************************************
1238 * Helpers to assist policy modules to *
1239 * implement encryption funcationality *
1240 ****************************************/
1244 #define CRYPTO_TFM_MODE_ECB (0)
1245 #define CRYPTO_TFM_MODE_CBC (1)
1248 static struct sptlrpc_ciph_type cipher_types[] = {
1249 [BULK_CIPH_ALG_NULL] = {
1250 "null", "null", 0, 0, 0
1252 [BULK_CIPH_ALG_ARC4] = {
1253 "arc4", "arc4", CRYPTO_TFM_MODE_ECB, 0, 16
1255 [BULK_CIPH_ALG_AES128] = {
1256 "aes128", "aes", CRYPTO_TFM_MODE_CBC, 16, 16
1258 [BULK_CIPH_ALG_AES192] = {
1259 "aes192", "aes", CRYPTO_TFM_MODE_CBC, 16, 24
1261 [BULK_CIPH_ALG_AES256] = {
1262 "aes256", "aes", CRYPTO_TFM_MODE_CBC, 16, 32
1264 [BULK_CIPH_ALG_CAST128] = {
1265 "cast128", "cast5", CRYPTO_TFM_MODE_CBC, 8, 16
1267 [BULK_CIPH_ALG_CAST256] = {
1268 "cast256", "cast6", CRYPTO_TFM_MODE_CBC, 16, 32
1270 [BULK_CIPH_ALG_TWOFISH128] = {
1271 "twofish128", "twofish", CRYPTO_TFM_MODE_CBC, 16, 16
1273 [BULK_CIPH_ALG_TWOFISH256] = {
1274 "twofish256", "twofish", CRYPTO_TFM_MODE_CBC, 16, 32
1278 const struct sptlrpc_ciph_type *sptlrpc_get_ciph_type(__u8 ciph_alg)
1280 struct sptlrpc_ciph_type *ct;
1282 if (ciph_alg < BULK_CIPH_ALG_MAX) {
1283 ct = &cipher_types[ciph_alg];
1284 if (ct->sct_tfm_name)
1289 EXPORT_SYMBOL(sptlrpc_get_ciph_type);
1291 const char *sptlrpc_get_ciph_name(__u8 ciph_alg)
1293 const struct sptlrpc_ciph_type *ct;
1295 ct = sptlrpc_get_ciph_type(ciph_alg);
1297 return ct->sct_name;
1301 EXPORT_SYMBOL(sptlrpc_get_ciph_name);