Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / ptlrpc / sec_bulk.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/sec_bulk.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 #define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_SEC
45
46 #include <libcfs/libcfs.h>
47 #ifndef __KERNEL__
48 #include <liblustre.h>
49 #include <libcfs/list.h>
50 #else
51 #include <linux/crypto.h>
52 #endif
53
54 #include <obd.h>
55 #include <obd_cksum.h>
56 #include <obd_class.h>
57 #include <obd_support.h>
58 #include <lustre_net.h>
59 #include <lustre_import.h>
60 #include <lustre_dlm.h>
61 #include <lustre_sec.h>
62
63 #include "ptlrpc_internal.h"
64
65 /****************************************
66  * bulk encryption page pools           *
67  ****************************************/
68
69 #ifdef __KERNEL__
70
71 #define PTRS_PER_PAGE   (CFS_PAGE_SIZE / sizeof(void *))
72 #define PAGES_PER_POOL  (PTRS_PER_PAGE)
73
74 #define IDLE_IDX_MAX            (100)
75 #define IDLE_IDX_WEIGHT         (3)
76
77 #define CACHE_QUIESCENT_PERIOD  (20)
78
79 static struct ptlrpc_enc_page_pool {
80         /*
81          * constants
82          */
83         unsigned long    epp_max_pages;   /* maximum pages can hold, const */
84         unsigned int     epp_max_pools;   /* number of pools, const */
85
86         /*
87          * wait queue in case of not enough free pages.
88          */
89         cfs_waitq_t      epp_waitq;       /* waiting threads */
90         unsigned int     epp_waitqlen;    /* wait queue length */
91         unsigned long    epp_pages_short; /* # of pages wanted of in-q users */
92         unsigned int     epp_growing:1;   /* during adding pages */
93
94         /*
95          * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
96          * this is counted based on each time when getting pages from
97          * the pools, not based on time. which means in case that system
98          * is idled for a while but the idle_idx might still be low if no
99          * activities happened in the pools.
100          */
101         unsigned long    epp_idle_idx;
102
103         /* last shrink time due to mem tight */
104         long             epp_last_shrink;
105         long             epp_last_access;
106
107         /*
108          * in-pool pages bookkeeping
109          */
110         spinlock_t       epp_lock;        /* protect following fields */
111         unsigned long    epp_total_pages; /* total pages in pools */
112         unsigned long    epp_free_pages;  /* current pages available */
113
114         /*
115          * statistics
116          */
117         unsigned long    epp_st_max_pages;      /* # of pages ever reached */
118         unsigned int     epp_st_grows;          /* # of grows */
119         unsigned int     epp_st_grow_fails;     /* # of add pages failures */
120         unsigned int     epp_st_shrinks;        /* # of shrinks */
121         unsigned long    epp_st_access;         /* # of access */
122         unsigned long    epp_st_missings;       /* # of cache missing */
123         unsigned long    epp_st_lowfree;        /* lowest free pages reached */
124         unsigned int     epp_st_max_wqlen;      /* highest waitqueue length */
125         cfs_time_t       epp_st_max_wait;       /* in jeffies */
126         /*
127          * pointers to pools
128          */
129         cfs_page_t    ***epp_pools;
130 } page_pools;
131
132 /*
133  * memory shrinker
134  */
135 const int pools_shrinker_seeks = DEFAULT_SEEKS;
136 static struct shrinker *pools_shrinker = NULL;
137
138
139 /*
140  * /proc/fs/lustre/sptlrpc/encrypt_page_pools
141  */
142 int sptlrpc_proc_read_enc_pool(char *page, char **start, off_t off, int count,
143                                int *eof, void *data)
144 {
145         int     rc;
146
147         spin_lock(&page_pools.epp_lock);
148
149         rc = snprintf(page, count,
150                       "physical pages:          %lu\n"
151                       "pages per pool:          %lu\n"
152                       "max pages:               %lu\n"
153                       "max pools:               %u\n"
154                       "total pages:             %lu\n"
155                       "total free:              %lu\n"
156                       "idle index:              %lu/100\n"
157                       "last shrink:             %lds\n"
158                       "last access:             %lds\n"
159                       "max pages reached:       %lu\n"
160                       "grows:                   %u\n"
161                       "grows failure:           %u\n"
162                       "shrinks:                 %u\n"
163                       "cache access:            %lu\n"
164                       "cache missing:           %lu\n"
165                       "low free mark:           %lu\n"
166                       "max waitqueue depth:     %u\n"
167                       "max wait time:           "CFS_TIME_T"/%u\n"
168                       ,
169                       num_physpages,
170                       PAGES_PER_POOL,
171                       page_pools.epp_max_pages,
172                       page_pools.epp_max_pools,
173                       page_pools.epp_total_pages,
174                       page_pools.epp_free_pages,
175                       page_pools.epp_idle_idx,
176                       cfs_time_current_sec() - page_pools.epp_last_shrink,
177                       cfs_time_current_sec() - page_pools.epp_last_access,
178                       page_pools.epp_st_max_pages,
179                       page_pools.epp_st_grows,
180                       page_pools.epp_st_grow_fails,
181                       page_pools.epp_st_shrinks,
182                       page_pools.epp_st_access,
183                       page_pools.epp_st_missings,
184                       page_pools.epp_st_lowfree,
185                       page_pools.epp_st_max_wqlen,
186                       page_pools.epp_st_max_wait, HZ
187                      );
188
189         spin_unlock(&page_pools.epp_lock);
190         return rc;
191 }
192
193 static void enc_pools_release_free_pages(long npages)
194 {
195         int     p_idx, g_idx;
196         int     p_idx_max1, p_idx_max2;
197
198         LASSERT(npages > 0);
199         LASSERT(npages <= page_pools.epp_free_pages);
200         LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
201
202         /* max pool index before the release */
203         p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
204
205         page_pools.epp_free_pages -= npages;
206         page_pools.epp_total_pages -= npages;
207
208         /* max pool index after the release */
209         p_idx_max1 = page_pools.epp_total_pages == 0 ? 0 :
210                      ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
211
212         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
213         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
214         LASSERT(page_pools.epp_pools[p_idx]);
215
216         while (npages--) {
217                 LASSERT(page_pools.epp_pools[p_idx]);
218                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
219
220                 cfs_free_page(page_pools.epp_pools[p_idx][g_idx]);
221                 page_pools.epp_pools[p_idx][g_idx] = NULL;
222
223                 if (++g_idx == PAGES_PER_POOL) {
224                         p_idx++;
225                         g_idx = 0;
226                 }
227         };
228
229         /* free unused pools */
230         while (p_idx_max1 < p_idx_max2) {
231                 LASSERT(page_pools.epp_pools[p_idx_max2]);
232                 OBD_FREE(page_pools.epp_pools[p_idx_max2], CFS_PAGE_SIZE);
233                 page_pools.epp_pools[p_idx_max2] = NULL;
234                 p_idx_max2--;
235         }
236 }
237
238 /*
239  * could be called frequently for query (@nr_to_scan == 0)
240  */
241 static int enc_pools_shrink(int nr_to_scan, unsigned int gfp_mask)
242 {
243         unsigned long   ret;
244
245         spin_lock(&page_pools.epp_lock);
246
247         if (nr_to_scan > page_pools.epp_free_pages)
248                 nr_to_scan = page_pools.epp_free_pages;
249
250         if (nr_to_scan > 0) {
251                 enc_pools_release_free_pages(nr_to_scan);
252                 CDEBUG(D_SEC, "released %d pages, %ld left\n",
253                        nr_to_scan, page_pools.epp_free_pages);
254
255                 page_pools.epp_st_shrinks++;
256                 page_pools.epp_last_shrink = cfs_time_current_sec();
257         }
258
259         /*
260          * try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool
261          */
262         if (page_pools.epp_free_pages <= PTLRPC_MAX_BRW_PAGES) {
263                 ret = 0;
264                 goto out_unlock;
265         }
266
267         /*
268          * if no pool access for a long time, we consider it's fully idle
269          */
270         if (cfs_time_current_sec() - page_pools.epp_last_access >
271             CACHE_QUIESCENT_PERIOD)
272                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
273
274         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
275         ret = (page_pools.epp_free_pages * page_pools.epp_idle_idx /
276                IDLE_IDX_MAX);
277         if (page_pools.epp_free_pages - ret < PTLRPC_MAX_BRW_PAGES)
278                 ret = page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES;
279
280 out_unlock:
281         spin_unlock(&page_pools.epp_lock);
282         return ret;
283 }
284
285 static inline
286 int npages_to_npools(unsigned long npages)
287 {
288         return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
289 }
290
291 /*
292  * return how many pages cleaned up.
293  */
294 static unsigned long enc_pools_cleanup(cfs_page_t ***pools, int npools)
295 {
296         unsigned long cleaned = 0;
297         int           i, j;
298
299         for (i = 0; i < npools; i++) {
300                 if (pools[i]) {
301                         for (j = 0; j < PAGES_PER_POOL; j++) {
302                                 if (pools[i][j]) {
303                                         cfs_free_page(pools[i][j]);
304                                         cleaned++;
305                                 }
306                         }
307                         OBD_FREE(pools[i], CFS_PAGE_SIZE);
308                         pools[i] = NULL;
309                 }
310         }
311
312         return cleaned;
313 }
314
315 /*
316  * merge @npools pointed by @pools which contains @npages new pages
317  * into current pools.
318  *
319  * we have options to avoid most memory copy with some tricks. but we choose
320  * the simplest way to avoid complexity. It's not frequently called.
321  */
322 static void enc_pools_insert(cfs_page_t ***pools, int npools, int npages)
323 {
324         int     freeslot;
325         int     op_idx, np_idx, og_idx, ng_idx;
326         int     cur_npools, end_npools;
327
328         LASSERT(npages > 0);
329         LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
330         LASSERT(npages_to_npools(npages) == npools);
331
332         spin_lock(&page_pools.epp_lock);
333
334         /*
335          * (1) fill all the free slots of current pools.
336          */
337         /* free slots are those left by rent pages, and the extra ones with
338          * index >= total_pages, locate at the tail of last pool. */
339         freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
340         if (freeslot != 0)
341                 freeslot = PAGES_PER_POOL - freeslot;
342         freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
343
344         op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
345         og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
346         np_idx = npools - 1;
347         ng_idx = (npages - 1) % PAGES_PER_POOL;
348
349         while (freeslot) {
350                 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
351                 LASSERT(pools[np_idx][ng_idx] != NULL);
352
353                 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
354                 pools[np_idx][ng_idx] = NULL;
355
356                 freeslot--;
357
358                 if (++og_idx == PAGES_PER_POOL) {
359                         op_idx++;
360                         og_idx = 0;
361                 }
362                 if (--ng_idx < 0) {
363                         if (np_idx == 0)
364                                 break;
365                         np_idx--;
366                         ng_idx = PAGES_PER_POOL - 1;
367                 }
368         }
369
370         /*
371          * (2) add pools if needed.
372          */
373         cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
374                      PAGES_PER_POOL;
375         end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
376                      PAGES_PER_POOL;
377         LASSERT(end_npools <= page_pools.epp_max_pools);
378
379         np_idx = 0;
380         while (cur_npools < end_npools) {
381                 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
382                 LASSERT(np_idx < npools);
383                 LASSERT(pools[np_idx] != NULL);
384
385                 page_pools.epp_pools[cur_npools++] = pools[np_idx];
386                 pools[np_idx++] = NULL;
387         }
388
389         page_pools.epp_total_pages += npages;
390         page_pools.epp_free_pages += npages;
391         page_pools.epp_st_lowfree = page_pools.epp_free_pages;
392
393         if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
394                 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
395
396         CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
397                page_pools.epp_total_pages);
398
399         spin_unlock(&page_pools.epp_lock);
400 }
401
402 static int enc_pools_add_pages(int npages)
403 {
404         static DECLARE_MUTEX(sem_add_pages);
405         cfs_page_t   ***pools;
406         int             npools, alloced = 0;
407         int             i, j, rc = -ENOMEM;
408
409         if (npages < PTLRPC_MAX_BRW_PAGES)
410                 npages = PTLRPC_MAX_BRW_PAGES;
411
412         down(&sem_add_pages);
413
414         if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
415                 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
416         LASSERT(npages > 0);
417
418         page_pools.epp_st_grows++;
419
420         npools = npages_to_npools(npages);
421         OBD_ALLOC(pools, npools * sizeof(*pools));
422         if (pools == NULL)
423                 goto out;
424
425         for (i = 0; i < npools; i++) {
426                 OBD_ALLOC(pools[i], CFS_PAGE_SIZE);
427                 if (pools[i] == NULL)
428                         goto out_pools;
429
430                 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
431                         pools[i][j] = cfs_alloc_page(CFS_ALLOC_IO |
432                                                      CFS_ALLOC_HIGH);
433                         if (pools[i][j] == NULL)
434                                 goto out_pools;
435
436                         alloced++;
437                 }
438         }
439
440         enc_pools_insert(pools, npools, npages);
441         CDEBUG(D_SEC, "added %d pages into pools\n", npages);
442         rc = 0;
443
444 out_pools:
445         enc_pools_cleanup(pools, npools);
446         OBD_FREE(pools, npools * sizeof(*pools));
447 out:
448         if (rc) {
449                 page_pools.epp_st_grow_fails++;
450                 CERROR("Failed to allocate %d enc pages\n", npages);
451         }
452
453         up(&sem_add_pages);
454         return rc;
455 }
456
457 static inline void enc_pools_wakeup(void)
458 {
459         if (unlikely(page_pools.epp_waitqlen)) {
460                 LASSERT(page_pools.epp_waitqlen > 0);
461                 LASSERT(cfs_waitq_active(&page_pools.epp_waitq));
462                 cfs_waitq_broadcast(&page_pools.epp_waitq);
463         }
464 }
465
466 static int enc_pools_should_grow(int page_needed, long now)
467 {
468         /* don't grow if someone else is growing the pools right now,
469          * or the pools has reached its full capacity
470          */
471         if (page_pools.epp_growing ||
472             page_pools.epp_total_pages == page_pools.epp_max_pages)
473                 return 0;
474
475         /* if total pages is not enough, we need to grow */
476         if (page_pools.epp_total_pages < page_needed)
477                 return 1;
478
479         /* if we just did a shrink due to memory tight, we'd better
480          * wait a while to grow again.
481          */
482         if (now - page_pools.epp_last_shrink < 2)
483                 return 0;
484
485         /*
486          * here we perhaps need consider other factors like wait queue
487          * length, idle index, etc. ?
488          */
489
490         /* grow the pools in any other cases */
491         return 1;
492 }
493
494 /*
495  * we allocate the requested pages atomically.
496  */
497 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
498 {
499         cfs_waitlink_t  waitlink;
500         unsigned long   this_idle = -1;
501         cfs_time_t      tick = 0;
502         long            now;
503         int             p_idx, g_idx;
504         int             i;
505
506         LASSERT(desc->bd_max_iov > 0);
507         LASSERT(desc->bd_max_iov <= page_pools.epp_max_pages);
508
509         /* resent bulk, enc pages might have been allocated previously */
510         if (desc->bd_enc_pages != NULL)
511                 return 0;
512
513         OBD_ALLOC(desc->bd_enc_pages,
514                   desc->bd_max_iov * sizeof(*desc->bd_enc_pages));
515         if (desc->bd_enc_pages == NULL)
516                 return -ENOMEM;
517
518         spin_lock(&page_pools.epp_lock);
519
520         page_pools.epp_st_access++;
521 again:
522         if (unlikely(page_pools.epp_free_pages < desc->bd_max_iov)) {
523                 if (tick == 0)
524                         tick = cfs_time_current();
525
526                 now = cfs_time_current_sec();
527
528                 page_pools.epp_st_missings++;
529                 page_pools.epp_pages_short += desc->bd_max_iov;
530
531                 if (enc_pools_should_grow(desc->bd_max_iov, now)) {
532                         page_pools.epp_growing = 1;
533
534                         spin_unlock(&page_pools.epp_lock);
535                         enc_pools_add_pages(page_pools.epp_pages_short / 2);
536                         spin_lock(&page_pools.epp_lock);
537
538                         page_pools.epp_growing = 0;
539                 } else {
540                         if (++page_pools.epp_waitqlen >
541                             page_pools.epp_st_max_wqlen)
542                                 page_pools.epp_st_max_wqlen =
543                                                 page_pools.epp_waitqlen;
544
545                         set_current_state(TASK_UNINTERRUPTIBLE);
546                         cfs_waitlink_init(&waitlink);
547                         cfs_waitq_add(&page_pools.epp_waitq, &waitlink);
548
549                         spin_unlock(&page_pools.epp_lock);
550                         cfs_schedule();
551                         spin_lock(&page_pools.epp_lock);
552
553                         LASSERT(page_pools.epp_waitqlen > 0);
554                         page_pools.epp_waitqlen--;
555                 }
556
557                 LASSERT(page_pools.epp_pages_short >= desc->bd_max_iov);
558                 page_pools.epp_pages_short -= desc->bd_max_iov;
559
560                 this_idle = 0;
561                 goto again;
562         }
563
564         /* record max wait time */
565         if (unlikely(tick != 0)) {
566                 tick = cfs_time_current() - tick;
567                 if (tick > page_pools.epp_st_max_wait)
568                         page_pools.epp_st_max_wait = tick;
569         }
570
571         /* proceed with rest of allocation */
572         page_pools.epp_free_pages -= desc->bd_max_iov;
573
574         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
575         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
576
577         for (i = 0; i < desc->bd_max_iov; i++) {
578                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
579                 desc->bd_enc_pages[i] = page_pools.epp_pools[p_idx][g_idx];
580                 page_pools.epp_pools[p_idx][g_idx] = NULL;
581
582                 if (++g_idx == PAGES_PER_POOL) {
583                         p_idx++;
584                         g_idx = 0;
585                 }
586         }
587
588         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
589                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
590
591         /*
592          * new idle index = (old * weight + new) / (weight + 1)
593          */
594         if (this_idle == -1) {
595                 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
596                             page_pools.epp_total_pages;
597         }
598         page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
599                                    this_idle) /
600                                   (IDLE_IDX_WEIGHT + 1);
601
602         page_pools.epp_last_access = cfs_time_current_sec();
603
604         spin_unlock(&page_pools.epp_lock);
605         return 0;
606 }
607 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
608
609 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
610 {
611         int     p_idx, g_idx;
612         int     i;
613
614         if (desc->bd_enc_pages == NULL)
615                 return;
616         if (desc->bd_max_iov == 0)
617                 return;
618
619         spin_lock(&page_pools.epp_lock);
620
621         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
622         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
623
624         LASSERT(page_pools.epp_free_pages + desc->bd_max_iov <=
625                 page_pools.epp_total_pages);
626         LASSERT(page_pools.epp_pools[p_idx]);
627
628         for (i = 0; i < desc->bd_max_iov; i++) {
629                 LASSERT(desc->bd_enc_pages[i] != NULL);
630                 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
631                 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
632
633                 page_pools.epp_pools[p_idx][g_idx] = desc->bd_enc_pages[i];
634
635                 if (++g_idx == PAGES_PER_POOL) {
636                         p_idx++;
637                         g_idx = 0;
638                 }
639         }
640
641         page_pools.epp_free_pages += desc->bd_max_iov;
642
643         enc_pools_wakeup();
644
645         spin_unlock(&page_pools.epp_lock);
646
647         OBD_FREE(desc->bd_enc_pages,
648                  desc->bd_max_iov * sizeof(*desc->bd_enc_pages));
649         desc->bd_enc_pages = NULL;
650 }
651 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
652
653 /*
654  * we don't do much stuff for add_user/del_user anymore, except adding some
655  * initial pages in add_user() if current pools are empty, rest would be
656  * handled by the pools's self-adaption.
657  */
658 int sptlrpc_enc_pool_add_user(void)
659 {
660         int     need_grow = 0;
661
662         spin_lock(&page_pools.epp_lock);
663         if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
664                 page_pools.epp_growing = 1;
665                 need_grow = 1;
666         }
667         spin_unlock(&page_pools.epp_lock);
668
669         if (need_grow) {
670                 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES);
671
672                 spin_lock(&page_pools.epp_lock);
673                 page_pools.epp_growing = 0;
674                 enc_pools_wakeup();
675                 spin_unlock(&page_pools.epp_lock);
676         }
677         return 0;
678 }
679 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
680
681 int sptlrpc_enc_pool_del_user(void)
682 {
683         return 0;
684 }
685 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
686
687 static inline void enc_pools_alloc(void)
688 {
689         LASSERT(page_pools.epp_max_pools);
690         /*
691          * on system with huge memory but small page size, this might lead to
692          * high-order allocation. but it's not common, and we suppose memory
693          * be not too much fragmented at module loading time.
694          */
695         OBD_ALLOC(page_pools.epp_pools,
696                   page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
697 }
698
699 static inline void enc_pools_free(void)
700 {
701         LASSERT(page_pools.epp_max_pools);
702         LASSERT(page_pools.epp_pools);
703
704         OBD_FREE(page_pools.epp_pools,
705                  page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
706 }
707
708 int sptlrpc_enc_pool_init(void)
709 {
710         /*
711          * maximum capacity is 1/8 of total physical memory.
712          * is the 1/8 a good number?
713          */
714         page_pools.epp_max_pages = num_physpages / 8;
715         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
716
717         cfs_waitq_init(&page_pools.epp_waitq);
718         page_pools.epp_waitqlen = 0;
719         page_pools.epp_pages_short = 0;
720
721         page_pools.epp_growing = 0;
722
723         page_pools.epp_idle_idx = 0;
724         page_pools.epp_last_shrink = cfs_time_current_sec();
725         page_pools.epp_last_access = cfs_time_current_sec();
726
727         spin_lock_init(&page_pools.epp_lock);
728         page_pools.epp_total_pages = 0;
729         page_pools.epp_free_pages = 0;
730
731         page_pools.epp_st_max_pages = 0;
732         page_pools.epp_st_grows = 0;
733         page_pools.epp_st_grow_fails = 0;
734         page_pools.epp_st_shrinks = 0;
735         page_pools.epp_st_access = 0;
736         page_pools.epp_st_missings = 0;
737         page_pools.epp_st_lowfree = 0;
738         page_pools.epp_st_max_wqlen = 0;
739         page_pools.epp_st_max_wait = 0;
740
741         enc_pools_alloc();
742         if (page_pools.epp_pools == NULL)
743                 return -ENOMEM;
744
745         pools_shrinker = set_shrinker(pools_shrinker_seeks, enc_pools_shrink);
746         if (pools_shrinker == NULL) {
747                 enc_pools_free();
748                 return -ENOMEM;
749         }
750
751         return 0;
752 }
753
754 void sptlrpc_enc_pool_fini(void)
755 {
756         unsigned long cleaned, npools;
757
758         LASSERT(pools_shrinker);
759         LASSERT(page_pools.epp_pools);
760         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
761
762         remove_shrinker(pools_shrinker);
763
764         npools = npages_to_npools(page_pools.epp_total_pages);
765         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
766         LASSERT(cleaned == page_pools.epp_total_pages);
767
768         enc_pools_free();
769
770         if (page_pools.epp_st_access > 0) {
771                 CWARN("max pages %lu, grows %u, grow fails %u, shrinks %u, "
772                       "access %lu, missing %lu, max qlen %u, max wait "
773                       CFS_TIME_T"/%d\n",
774                       page_pools.epp_st_max_pages, page_pools.epp_st_grows,
775                       page_pools.epp_st_grow_fails,
776                       page_pools.epp_st_shrinks, page_pools.epp_st_access,
777                       page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
778                       page_pools.epp_st_max_wait, HZ);
779         }
780 }
781
782 #else /* !__KERNEL__ */
783
784 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
785 {
786         return 0;
787 }
788
789 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
790 {
791 }
792
793 int sptlrpc_enc_pool_init(void)
794 {
795         return 0;
796 }
797
798 void sptlrpc_enc_pool_fini(void)
799 {
800 }
801 #endif
802
803 /****************************************
804  * Helpers to assist policy modules to  *
805  * implement checksum funcationality    *
806  ****************************************/
807
808 static struct sptlrpc_hash_type hash_types[] = {
809         [BULK_HASH_ALG_NULL]    = { "null",     "null",         0 },
810         [BULK_HASH_ALG_ADLER32] = { "adler32",  "adler32",      4 },
811         [BULK_HASH_ALG_CRC32]   = { "crc32",    "crc32",        4 },
812         [BULK_HASH_ALG_MD5]     = { "md5",      "md5",          16 },
813         [BULK_HASH_ALG_SHA1]    = { "sha1",     "sha1",         20 },
814         [BULK_HASH_ALG_SHA256]  = { "sha256",   "sha256",       32 },
815         [BULK_HASH_ALG_SHA384]  = { "sha384",   "sha384",       48 },
816         [BULK_HASH_ALG_SHA512]  = { "sha512",   "sha512",       64 },
817         [BULK_HASH_ALG_WP256]   = { "wp256",    "wp256",        32 },
818         [BULK_HASH_ALG_WP384]   = { "wp384",    "wp384",        48 },
819         [BULK_HASH_ALG_WP512]   = { "wp512",    "wp512",        64 },
820 };
821
822 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg)
823 {
824         struct sptlrpc_hash_type *ht;
825
826         if (hash_alg < BULK_HASH_ALG_MAX) {
827                 ht = &hash_types[hash_alg];
828                 if (ht->sht_tfm_name)
829                         return ht;
830         }
831         return NULL;
832 }
833 EXPORT_SYMBOL(sptlrpc_get_hash_type);
834
835 const char * sptlrpc_get_hash_name(__u8 hash_alg)
836 {
837         const struct sptlrpc_hash_type *ht;
838
839         ht = sptlrpc_get_hash_type(hash_alg);
840         if (ht)
841                 return ht->sht_name;
842         else
843                 return "unknown";
844 }
845 EXPORT_SYMBOL(sptlrpc_get_hash_name);
846
847 int bulk_sec_desc_size(__u8 hash_alg, int request, int read)
848 {
849         int size = sizeof(struct ptlrpc_bulk_sec_desc);
850
851         LASSERT(hash_alg < BULK_HASH_ALG_MAX);
852
853         /* read request don't need extra data */
854         if (!(read && request))
855                 size += hash_types[hash_alg].sht_size;
856
857         return size;
858 }
859 EXPORT_SYMBOL(bulk_sec_desc_size);
860
861 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset)
862 {
863         struct ptlrpc_bulk_sec_desc *bsd;
864         int    size = msg->lm_buflens[offset];
865
866         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
867         if (bsd == NULL) {
868                 CERROR("Invalid bulk sec desc: size %d\n", size);
869                 return -EINVAL;
870         }
871
872         /* nothing to swab */
873
874         if (unlikely(bsd->bsd_version != 0)) {
875                 CERROR("Unexpected version %u\n", bsd->bsd_version);
876                 return -EPROTO;
877         }
878
879         if (unlikely(bsd->bsd_flags != 0)) {
880                 CERROR("Unexpected flags %x\n", bsd->bsd_flags);
881                 return -EPROTO;
882         }
883
884         if (unlikely(!sptlrpc_get_hash_type(bsd->bsd_hash_alg))) {
885                 CERROR("Unsupported checksum algorithm %u\n",
886                        bsd->bsd_hash_alg);
887                 return -EINVAL;
888         }
889
890         if (unlikely(!sptlrpc_get_ciph_type(bsd->bsd_ciph_alg))) {
891                 CERROR("Unsupported cipher algorithm %u\n",
892                        bsd->bsd_ciph_alg);
893                 return -EINVAL;
894         }
895
896         if (unlikely(size > sizeof(*bsd)) &&
897             size < sizeof(*bsd) + hash_types[bsd->bsd_hash_alg].sht_size) {
898                 CERROR("Mal-formed checksum data: csum alg %u, size %d\n",
899                        bsd->bsd_hash_alg, size);
900                 return -EINVAL;
901         }
902
903         return 0;
904 }
905 EXPORT_SYMBOL(bulk_sec_desc_unpack);
906
907 #ifdef __KERNEL__
908
909 #ifdef HAVE_ADLER
910 static int do_bulk_checksum_adler32(struct ptlrpc_bulk_desc *desc, void *buf)
911 {
912         struct page    *page;
913         int             off;
914         char           *ptr;
915         __u32           adler32 = 1;
916         int             len, i;
917
918         for (i = 0; i < desc->bd_iov_count; i++) {
919                 page = desc->bd_iov[i].kiov_page;
920                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
921                 ptr = cfs_kmap(page) + off;
922                 len = desc->bd_iov[i].kiov_len;
923
924                 adler32 = adler32(adler32, ptr, len);
925
926                 cfs_kunmap(page);
927         }
928
929         adler32 = cpu_to_le32(adler32);
930         memcpy(buf, &adler32, sizeof(adler32));
931         return 0;
932 }
933 #endif
934
935 static int do_bulk_checksum_crc32(struct ptlrpc_bulk_desc *desc, void *buf)
936 {
937         struct page    *page;
938         int             off;
939         char           *ptr;
940         __u32           crc32 = ~0;
941         int             len, i;
942
943         for (i = 0; i < desc->bd_iov_count; i++) {
944                 page = desc->bd_iov[i].kiov_page;
945                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
946                 ptr = cfs_kmap(page) + off;
947                 len = desc->bd_iov[i].kiov_len;
948
949                 crc32 = crc32_le(crc32, ptr, len);
950
951                 cfs_kunmap(page);
952         }
953
954         crc32 = cpu_to_le32(crc32);
955         memcpy(buf, &crc32, sizeof(crc32));
956         return 0;
957 }
958
959 static int do_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u32 alg, void *buf)
960 {
961         struct hash_desc    hdesc;
962         struct scatterlist *sl;
963         int i, rc = 0, bytes = 0;
964
965         LASSERT(alg > BULK_HASH_ALG_NULL &&
966                 alg < BULK_HASH_ALG_MAX);
967
968         switch (alg) {
969         case BULK_HASH_ALG_ADLER32:
970 #ifdef HAVE_ADLER
971                 return do_bulk_checksum_adler32(desc, buf);
972 #else
973                 CERROR("Adler32 not supported\n");
974                 return -EINVAL;
975 #endif
976         case BULK_HASH_ALG_CRC32:
977                 return do_bulk_checksum_crc32(desc, buf);
978         }
979
980         hdesc.tfm = ll_crypto_alloc_hash(hash_types[alg].sht_tfm_name, 0, 0);
981         if (hdesc.tfm == NULL) {
982                 CERROR("Unable to allocate TFM %s\n", hash_types[alg].sht_name);
983                 return -ENOMEM;
984         }
985         hdesc.flags = 0;
986
987         OBD_ALLOC(sl, sizeof(*sl) * desc->bd_iov_count);
988         if (sl == NULL) {
989                 rc = -ENOMEM;
990                 goto out_tfm;
991         }
992
993         for (i = 0; i < desc->bd_iov_count; i++) {
994                 sl[i].page = desc->bd_iov[i].kiov_page;
995                 sl[i].offset = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
996                 sl[i].length = desc->bd_iov[i].kiov_len;
997                 bytes += desc->bd_iov[i].kiov_len;
998         }
999
1000         ll_crypto_hash_init(&hdesc);
1001         ll_crypto_hash_update(&hdesc, sl, bytes);
1002         ll_crypto_hash_final(&hdesc, buf);
1003
1004         OBD_FREE(sl, sizeof(*sl) * desc->bd_iov_count);
1005
1006 out_tfm:
1007         ll_crypto_free_hash(hdesc.tfm);
1008         return rc;
1009 }
1010
1011 #else /* !__KERNEL__ */
1012
1013 static int do_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u32 alg, void *buf)
1014 {
1015         __u32   csum32;
1016         int     i;
1017
1018         LASSERT(alg == BULK_HASH_ALG_ADLER32 || alg == BULK_HASH_ALG_CRC32);
1019
1020         if (alg == BULK_HASH_ALG_ADLER32)
1021                 csum32 = 1;
1022         else
1023                 csum32 = ~0;
1024
1025         for (i = 0; i < desc->bd_iov_count; i++) {
1026                 unsigned char *ptr = desc->bd_iov[i].iov_base;
1027                 int len = desc->bd_iov[i].iov_len;
1028
1029                 switch (alg) {
1030                 case BULK_HASH_ALG_ADLER32:
1031 #ifdef HAVE_ADLER
1032                         csum32 = adler32(csum32, ptr, len);
1033 #else
1034                         CERROR("Adler32 not supported\n");
1035                         return -EINVAL;
1036 #endif
1037                         break;
1038                 case BULK_HASH_ALG_CRC32:
1039                         csum32 = crc32_le(csum32, ptr, len);
1040                         break;
1041                 }
1042         }
1043
1044         csum32 = cpu_to_le32(csum32);
1045         memcpy(buf, &csum32, sizeof(csum32));
1046         return 0;
1047 }
1048
1049 #endif /* __KERNEL__ */
1050
1051 /*
1052  * perform algorithm @alg checksum on @desc, store result in @buf.
1053  * if anything goes wrong, leave 'alg' be BULK_HASH_ALG_NULL.
1054  */
1055 static
1056 int generate_bulk_csum(struct ptlrpc_bulk_desc *desc, __u32 alg,
1057                        struct ptlrpc_bulk_sec_desc *bsd, int bsdsize)
1058 {
1059         int rc;
1060
1061         LASSERT(bsd);
1062         LASSERT(alg < BULK_HASH_ALG_MAX);
1063
1064         bsd->bsd_hash_alg = BULK_HASH_ALG_NULL;
1065
1066         if (alg == BULK_HASH_ALG_NULL)
1067                 return 0;
1068
1069         LASSERT(bsdsize >= sizeof(*bsd) + hash_types[alg].sht_size);
1070
1071         rc = do_bulk_checksum(desc, alg, bsd->bsd_csum);
1072         if (rc == 0)
1073                 bsd->bsd_hash_alg = alg;
1074
1075         return rc;
1076 }
1077
1078 static
1079 int verify_bulk_csum(struct ptlrpc_bulk_desc *desc, int read,
1080                      struct ptlrpc_bulk_sec_desc *bsdv, int bsdvsize,
1081                      struct ptlrpc_bulk_sec_desc *bsdr, int bsdrsize)
1082 {
1083         char *csum_p;
1084         char *buf = NULL;
1085         int   csum_size, rc = 0;
1086
1087         LASSERT(bsdv);
1088         LASSERT(bsdv->bsd_hash_alg < BULK_HASH_ALG_MAX);
1089
1090         if (bsdr)
1091                 bsdr->bsd_hash_alg = BULK_HASH_ALG_NULL;
1092
1093         if (bsdv->bsd_hash_alg == BULK_HASH_ALG_NULL)
1094                 return 0;
1095
1096         /* for all supported algorithms */
1097         csum_size = hash_types[bsdv->bsd_hash_alg].sht_size;
1098
1099         if (bsdvsize < sizeof(*bsdv) + csum_size) {
1100                 CERROR("verifier size %d too small, require %d\n",
1101                        bsdvsize, (int) sizeof(*bsdv) + csum_size);
1102                 return -EINVAL;
1103         }
1104
1105         if (bsdr) {
1106                 LASSERT(bsdrsize >= sizeof(*bsdr) + csum_size);
1107                 csum_p = (char *) bsdr->bsd_csum;
1108         } else {
1109                 OBD_ALLOC(buf, csum_size);
1110                 if (buf == NULL)
1111                         return -EINVAL;
1112                 csum_p = buf;
1113         }
1114
1115         rc = do_bulk_checksum(desc, bsdv->bsd_hash_alg, csum_p);
1116
1117         if (memcmp(bsdv->bsd_csum, csum_p, csum_size)) {
1118                 CERROR("BAD %s CHECKSUM (%s), data mutated during "
1119                        "transfer!\n", read ? "READ" : "WRITE",
1120                        hash_types[bsdv->bsd_hash_alg].sht_name);
1121                 rc = -EINVAL;
1122         } else {
1123                 CDEBUG(D_SEC, "bulk %s checksum (%s) verified\n",
1124                       read ? "read" : "write",
1125                       hash_types[bsdv->bsd_hash_alg].sht_name);
1126         }
1127
1128         if (bsdr) {
1129                 bsdr->bsd_hash_alg = bsdv->bsd_hash_alg;
1130                 memcpy(bsdr->bsd_csum, csum_p, csum_size);
1131         } else {
1132                 LASSERT(buf);
1133                 OBD_FREE(buf, csum_size);
1134         }
1135
1136         return rc;
1137 }
1138
1139 int bulk_csum_cli_request(struct ptlrpc_bulk_desc *desc, int read,
1140                           __u32 alg, struct lustre_msg *rmsg, int roff)
1141 {
1142         struct ptlrpc_bulk_sec_desc *bsdr;
1143         int    rsize, rc = 0;
1144
1145         rsize = rmsg->lm_buflens[roff];
1146         bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
1147
1148         LASSERT(bsdr);
1149         LASSERT(rsize >= sizeof(*bsdr));
1150         LASSERT(alg < BULK_HASH_ALG_MAX);
1151
1152         if (read) {
1153                 bsdr->bsd_hash_alg = alg;
1154         } else {
1155                 rc = generate_bulk_csum(desc, alg, bsdr, rsize);
1156                 if (rc)
1157                         CERROR("bulk write: client failed to compute "
1158                                "checksum: %d\n", rc);
1159
1160                 /* For sending we only compute the wrong checksum instead
1161                  * of corrupting the data so it is still correct on a redo */
1162                 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
1163                     bsdr->bsd_hash_alg != BULK_HASH_ALG_NULL)
1164                         bsdr->bsd_csum[0] ^= 0x1;
1165         }
1166
1167         return rc;
1168 }
1169 EXPORT_SYMBOL(bulk_csum_cli_request);
1170
1171 int bulk_csum_cli_reply(struct ptlrpc_bulk_desc *desc, int read,
1172                         struct lustre_msg *rmsg, int roff,
1173                         struct lustre_msg *vmsg, int voff)
1174 {
1175         struct ptlrpc_bulk_sec_desc *bsdv, *bsdr;
1176         int    rsize, vsize;
1177
1178         rsize = rmsg->lm_buflens[roff];
1179         vsize = vmsg->lm_buflens[voff];
1180         bsdr = lustre_msg_buf(rmsg, roff, 0);
1181         bsdv = lustre_msg_buf(vmsg, voff, 0);
1182
1183         if (bsdv == NULL || vsize < sizeof(*bsdv)) {
1184                 CERROR("Invalid checksum verifier from server: size %d\n",
1185                        vsize);
1186                 return -EINVAL;
1187         }
1188
1189         LASSERT(bsdr);
1190         LASSERT(rsize >= sizeof(*bsdr));
1191         LASSERT(vsize >= sizeof(*bsdv));
1192
1193         if (bsdr->bsd_hash_alg != bsdv->bsd_hash_alg) {
1194                 CERROR("bulk %s: checksum algorithm mismatch: client request "
1195                        "%s but server reply with %s. try to use the new one "
1196                        "for checksum verification\n",
1197                        read ? "read" : "write",
1198                        hash_types[bsdr->bsd_hash_alg].sht_name,
1199                        hash_types[bsdv->bsd_hash_alg].sht_name);
1200         }
1201
1202         if (read)
1203                 return verify_bulk_csum(desc, 1, bsdv, vsize, NULL, 0);
1204         else {
1205                 char *cli, *srv, *new = NULL;
1206                 int csum_size = hash_types[bsdr->bsd_hash_alg].sht_size;
1207
1208                 LASSERT(bsdr->bsd_hash_alg < BULK_HASH_ALG_MAX);
1209                 if (bsdr->bsd_hash_alg == BULK_HASH_ALG_NULL)
1210                         return 0;
1211
1212                 if (vsize < sizeof(*bsdv) + csum_size) {
1213                         CERROR("verifier size %d too small, require %d\n",
1214                                vsize, (int) sizeof(*bsdv) + csum_size);
1215                         return -EINVAL;
1216                 }
1217
1218                 cli = (char *) (bsdr + 1);
1219                 srv = (char *) (bsdv + 1);
1220
1221                 if (!memcmp(cli, srv, csum_size)) {
1222                         /* checksum confirmed */
1223                         CDEBUG(D_SEC, "bulk write checksum (%s) confirmed\n",
1224                                hash_types[bsdr->bsd_hash_alg].sht_name);
1225                         return 0;
1226                 }
1227
1228                 /* checksum mismatch, re-compute a new one and compare with
1229                  * others, give out proper warnings. */
1230                 OBD_ALLOC(new, csum_size);
1231                 if (new == NULL)
1232                         return -ENOMEM;
1233
1234                 do_bulk_checksum(desc, bsdr->bsd_hash_alg, new);
1235
1236                 if (!memcmp(new, srv, csum_size)) {
1237                         CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1238                                "on the client after we checksummed them\n",
1239                                hash_types[bsdr->bsd_hash_alg].sht_name);
1240                 } else if (!memcmp(new, cli, csum_size)) {
1241                         CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1242                                "in transit\n",
1243                                hash_types[bsdr->bsd_hash_alg].sht_name);
1244                 } else {
1245                         CERROR("BAD WRITE CHECKSUM (%s): pages were mutated "
1246                                "in transit, and the current page contents "
1247                                "don't match the originals and what the server "
1248                                "received\n",
1249                                hash_types[bsdr->bsd_hash_alg].sht_name);
1250                 }
1251                 OBD_FREE(new, csum_size);
1252
1253                 return -EINVAL;
1254         }
1255 }
1256 EXPORT_SYMBOL(bulk_csum_cli_reply);
1257
1258 #ifdef __KERNEL__
1259 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
1260 {
1261         char           *ptr;
1262         unsigned int    off, i;
1263
1264         for (i = 0; i < desc->bd_iov_count; i++) {
1265                 if (desc->bd_iov[i].kiov_len == 0)
1266                         continue;
1267
1268                 ptr = cfs_kmap(desc->bd_iov[i].kiov_page);
1269                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
1270                 ptr[off] ^= 0x1;
1271                 cfs_kunmap(desc->bd_iov[i].kiov_page);
1272                 return;
1273         }
1274 }
1275 #else
1276 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
1277 {
1278 }
1279 #endif /* __KERNEL__ */
1280
1281 int bulk_csum_svc(struct ptlrpc_bulk_desc *desc, int read,
1282                   struct ptlrpc_bulk_sec_desc *bsdv, int vsize,
1283                   struct ptlrpc_bulk_sec_desc *bsdr, int rsize)
1284 {
1285         int    rc;
1286
1287         LASSERT(vsize >= sizeof(*bsdv));
1288         LASSERT(rsize >= sizeof(*bsdr));
1289         LASSERT(bsdv && bsdr);
1290
1291         if (read) {
1292                 rc = generate_bulk_csum(desc, bsdv->bsd_hash_alg, bsdr, rsize);
1293                 if (rc)
1294                         CERROR("bulk read: server failed to generate %s "
1295                                "checksum: %d\n",
1296                                hash_types[bsdv->bsd_hash_alg].sht_name, rc);
1297
1298                 /* corrupt the data after we compute the checksum, to
1299                  * simulate an OST->client data error */
1300                 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
1301                         corrupt_bulk_data(desc);
1302         } else {
1303                 rc = verify_bulk_csum(desc, 0, bsdv, vsize, bsdr, rsize);
1304         }
1305
1306         return rc;
1307 }
1308 EXPORT_SYMBOL(bulk_csum_svc);
1309
1310 /****************************************
1311  * Helpers to assist policy modules to  *
1312  * implement encryption funcationality  *
1313  ****************************************/
1314
1315 /* FIXME */
1316 #ifndef __KERNEL__
1317 #define CRYPTO_TFM_MODE_ECB     (0)
1318 #define CRYPTO_TFM_MODE_CBC     (1)
1319 #endif
1320
1321 static struct sptlrpc_ciph_type cipher_types[] = {
1322         [BULK_CIPH_ALG_NULL]    = {
1323                 "null",         "null",       0,                   0,  0
1324         },
1325         [BULK_CIPH_ALG_ARC4]    = {
1326                 "arc4",         "ecb(arc4)",       0, 0,  16
1327         },
1328         [BULK_CIPH_ALG_AES128]  = {
1329                 "aes128",       "cbc(aes)",        0, 16, 16
1330         },
1331         [BULK_CIPH_ALG_AES192]  = {
1332                 "aes192",       "cbc(aes)",        0, 16, 24
1333         },
1334         [BULK_CIPH_ALG_AES256]  = {
1335                 "aes256",       "cbc(aes)",        0, 16, 32
1336         },
1337         [BULK_CIPH_ALG_CAST128] = {
1338                 "cast128",      "cbc(cast5)",      0, 8,  16
1339         },
1340         [BULK_CIPH_ALG_CAST256] = {
1341                 "cast256",      "cbc(cast6)",      0, 16, 32
1342         },
1343         [BULK_CIPH_ALG_TWOFISH128] = {
1344                 "twofish128",   "cbc(twofish)",    0, 16, 16
1345         },
1346         [BULK_CIPH_ALG_TWOFISH256] = {
1347                 "twofish256",   "cbc(twofish)",    0, 16, 32
1348         },
1349 };
1350
1351 const struct sptlrpc_ciph_type *sptlrpc_get_ciph_type(__u8 ciph_alg)
1352 {
1353         struct sptlrpc_ciph_type *ct;
1354
1355         if (ciph_alg < BULK_CIPH_ALG_MAX) {
1356                 ct = &cipher_types[ciph_alg];
1357                 if (ct->sct_tfm_name)
1358                         return ct;
1359         }
1360         return NULL;
1361 }
1362 EXPORT_SYMBOL(sptlrpc_get_ciph_type);
1363
1364 const char *sptlrpc_get_ciph_name(__u8 ciph_alg)
1365 {
1366         const struct sptlrpc_ciph_type *ct;
1367
1368         ct = sptlrpc_get_ciph_type(ciph_alg);
1369         if (ct)
1370                 return ct->sct_name;
1371         else
1372                 return "unknown";
1373 }
1374 EXPORT_SYMBOL(sptlrpc_get_ciph_name);