Whamcloud - gitweb
LU-5162 mdc: Add exception entry check for radix_tree
[fs/lustre-release.git] / lustre / ptlrpc / sec_bulk.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/sec_bulk.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #include <libcfs/libcfs.h>
44 #ifndef __KERNEL__
45 #include <liblustre.h>
46 #include <libcfs/list.h>
47 #else
48 #include <linux/crypto.h>
49 #endif
50
51 #include <obd.h>
52 #include <obd_cksum.h>
53 #include <obd_class.h>
54 #include <obd_support.h>
55 #include <lustre_net.h>
56 #include <lustre_import.h>
57 #include <lustre_dlm.h>
58 #include <lustre_sec.h>
59
60 #include "ptlrpc_internal.h"
61
62 /****************************************
63  * bulk encryption page pools           *
64  ****************************************/
65
66 #ifdef __KERNEL__
67
68 #define PTRS_PER_PAGE   (PAGE_CACHE_SIZE / sizeof(void *))
69 #define PAGES_PER_POOL  (PTRS_PER_PAGE)
70
71 #define IDLE_IDX_MAX            (100)
72 #define IDLE_IDX_WEIGHT         (3)
73
74 #define CACHE_QUIESCENT_PERIOD  (20)
75
76 static struct ptlrpc_enc_page_pool {
77         /*
78          * constants
79          */
80         unsigned long    epp_max_pages;   /* maximum pages can hold, const */
81         unsigned int     epp_max_pools;   /* number of pools, const */
82
83         /*
84          * wait queue in case of not enough free pages.
85          */
86         wait_queue_head_t    epp_waitq;       /* waiting threads */
87         unsigned int     epp_waitqlen;    /* wait queue length */
88         unsigned long    epp_pages_short; /* # of pages wanted of in-q users */
89         unsigned int     epp_growing:1;   /* during adding pages */
90
91         /*
92          * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
93          * this is counted based on each time when getting pages from
94          * the pools, not based on time. which means in case that system
95          * is idled for a while but the idle_idx might still be low if no
96          * activities happened in the pools.
97          */
98         unsigned long    epp_idle_idx;
99
100         /* last shrink time due to mem tight */
101         long             epp_last_shrink;
102         long             epp_last_access;
103
104         /*
105          * in-pool pages bookkeeping
106          */
107         spinlock_t       epp_lock;         /* protect following fields */
108         unsigned long    epp_total_pages; /* total pages in pools */
109         unsigned long    epp_free_pages;  /* current pages available */
110
111         /*
112          * statistics
113          */
114         unsigned long    epp_st_max_pages;      /* # of pages ever reached */
115         unsigned int     epp_st_grows;          /* # of grows */
116         unsigned int     epp_st_grow_fails;     /* # of add pages failures */
117         unsigned int     epp_st_shrinks;        /* # of shrinks */
118         unsigned long    epp_st_access;         /* # of access */
119         unsigned long    epp_st_missings;       /* # of cache missing */
120         unsigned long    epp_st_lowfree;        /* lowest free pages reached */
121         unsigned int     epp_st_max_wqlen;      /* highest waitqueue length */
122         cfs_time_t       epp_st_max_wait;       /* in jeffies */
123         /*
124          * pointers to pools
125          */
126         struct page    ***epp_pools;
127 } page_pools;
128
129 /*
130  * memory shrinker
131  */
132 const int pools_shrinker_seeks = DEFAULT_SEEKS;
133 static struct shrinker *pools_shrinker;
134
135
136 /*
137  * /proc/fs/lustre/sptlrpc/encrypt_page_pools
138  */
139 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
140 {
141         int     rc;
142
143         spin_lock(&page_pools.epp_lock);
144
145         rc = seq_printf(m,
146                       "physical pages:          %lu\n"
147                       "pages per pool:          %lu\n"
148                       "max pages:               %lu\n"
149                       "max pools:               %u\n"
150                       "total pages:             %lu\n"
151                       "total free:              %lu\n"
152                       "idle index:              %lu/100\n"
153                       "last shrink:             %lds\n"
154                       "last access:             %lds\n"
155                       "max pages reached:       %lu\n"
156                       "grows:                   %u\n"
157                       "grows failure:           %u\n"
158                       "shrinks:                 %u\n"
159                       "cache access:            %lu\n"
160                       "cache missing:           %lu\n"
161                       "low free mark:           %lu\n"
162                       "max waitqueue depth:     %u\n"
163                       "max wait time:           "CFS_TIME_T"/%u\n"
164                       ,
165                       totalram_pages,
166                       PAGES_PER_POOL,
167                       page_pools.epp_max_pages,
168                       page_pools.epp_max_pools,
169                       page_pools.epp_total_pages,
170                       page_pools.epp_free_pages,
171                       page_pools.epp_idle_idx,
172                       cfs_time_current_sec() - page_pools.epp_last_shrink,
173                       cfs_time_current_sec() - page_pools.epp_last_access,
174                       page_pools.epp_st_max_pages,
175                       page_pools.epp_st_grows,
176                       page_pools.epp_st_grow_fails,
177                       page_pools.epp_st_shrinks,
178                       page_pools.epp_st_access,
179                       page_pools.epp_st_missings,
180                       page_pools.epp_st_lowfree,
181                       page_pools.epp_st_max_wqlen,
182                       page_pools.epp_st_max_wait, HZ
183                      );
184
185         spin_unlock(&page_pools.epp_lock);
186         return rc;
187 }
188
189 static void enc_pools_release_free_pages(long npages)
190 {
191         int     p_idx, g_idx;
192         int     p_idx_max1, p_idx_max2;
193
194         LASSERT(npages > 0);
195         LASSERT(npages <= page_pools.epp_free_pages);
196         LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
197
198         /* max pool index before the release */
199         p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
200
201         page_pools.epp_free_pages -= npages;
202         page_pools.epp_total_pages -= npages;
203
204         /* max pool index after the release */
205         p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
206                      ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
207
208         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
209         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
210         LASSERT(page_pools.epp_pools[p_idx]);
211
212         while (npages--) {
213                 LASSERT(page_pools.epp_pools[p_idx]);
214                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
215
216                 __free_page(page_pools.epp_pools[p_idx][g_idx]);
217                 page_pools.epp_pools[p_idx][g_idx] = NULL;
218
219                 if (++g_idx == PAGES_PER_POOL) {
220                         p_idx++;
221                         g_idx = 0;
222                 }
223         }
224
225         /* free unused pools */
226         while (p_idx_max1 < p_idx_max2) {
227                 LASSERT(page_pools.epp_pools[p_idx_max2]);
228                 OBD_FREE(page_pools.epp_pools[p_idx_max2], PAGE_CACHE_SIZE);
229                 page_pools.epp_pools[p_idx_max2] = NULL;
230                 p_idx_max2--;
231         }
232 }
233
234 /*
235  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
236  */
237 static unsigned long enc_pools_shrink_count(struct shrinker *s,
238                                             struct shrink_control *sc)
239 {
240         /*
241          * if no pool access for a long time, we consider it's fully idle.
242          * a little race here is fine.
243          */
244         if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
245                      CACHE_QUIESCENT_PERIOD)) {
246                 spin_lock(&page_pools.epp_lock);
247                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
248                 spin_unlock(&page_pools.epp_lock);
249         }
250
251         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
252         return max((int)page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES, 0) *
253                 (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
254 }
255
256 /*
257  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
258  */
259 static unsigned long enc_pools_shrink_scan(struct shrinker *s,
260                                            struct shrink_control *sc)
261 {
262         spin_lock(&page_pools.epp_lock);
263         sc->nr_to_scan = min_t(unsigned long, sc->nr_to_scan,
264                               page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES);
265         if (sc->nr_to_scan > 0) {
266                 enc_pools_release_free_pages(sc->nr_to_scan);
267                 CDEBUG(D_SEC, "released %ld pages, %ld left\n",
268                        (long)sc->nr_to_scan, page_pools.epp_free_pages);
269
270                 page_pools.epp_st_shrinks++;
271                 page_pools.epp_last_shrink = cfs_time_current_sec();
272         }
273         spin_unlock(&page_pools.epp_lock);
274
275         /*
276          * if no pool access for a long time, we consider it's fully idle.
277          * a little race here is fine.
278          */
279         if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
280                      CACHE_QUIESCENT_PERIOD)) {
281                 spin_lock(&page_pools.epp_lock);
282                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
283                 spin_unlock(&page_pools.epp_lock);
284         }
285
286         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
287         return sc->nr_to_scan;
288 }
289
290 #ifndef HAVE_SHRINKER_COUNT
291 /*
292  * could be called frequently for query (@nr_to_scan == 0).
293  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
294  */
295 static int enc_pools_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
296 {
297         struct shrink_control scv = {
298                 .nr_to_scan = shrink_param(sc, nr_to_scan),
299                 .gfp_mask   = shrink_param(sc, gfp_mask)
300         };
301 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
302         struct shrinker* shrinker = NULL;
303 #endif
304
305         enc_pools_shrink_scan(shrinker, &scv);
306
307         return enc_pools_shrink_count(shrinker, &scv);
308 }
309
310 #endif /* HAVE_SHRINKER_COUNT */
311
312 static inline
313 int npages_to_npools(unsigned long npages)
314 {
315         return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
316 }
317
318 /*
319  * return how many pages cleaned up.
320  */
321 static unsigned long enc_pools_cleanup(struct page ***pools, int npools)
322 {
323         unsigned long cleaned = 0;
324         int           i, j;
325
326         for (i = 0; i < npools; i++) {
327                 if (pools[i]) {
328                         for (j = 0; j < PAGES_PER_POOL; j++) {
329                                 if (pools[i][j]) {
330                                         __free_page(pools[i][j]);
331                                         cleaned++;
332                                 }
333                         }
334                         OBD_FREE(pools[i], PAGE_CACHE_SIZE);
335                         pools[i] = NULL;
336                 }
337         }
338
339         return cleaned;
340 }
341
342 /*
343  * merge @npools pointed by @pools which contains @npages new pages
344  * into current pools.
345  *
346  * we have options to avoid most memory copy with some tricks. but we choose
347  * the simplest way to avoid complexity. It's not frequently called.
348  */
349 static void enc_pools_insert(struct page ***pools, int npools, int npages)
350 {
351         int     freeslot;
352         int     op_idx, np_idx, og_idx, ng_idx;
353         int     cur_npools, end_npools;
354
355         LASSERT(npages > 0);
356         LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
357         LASSERT(npages_to_npools(npages) == npools);
358         LASSERT(page_pools.epp_growing);
359
360         spin_lock(&page_pools.epp_lock);
361
362         /*
363          * (1) fill all the free slots of current pools.
364          */
365         /* free slots are those left by rent pages, and the extra ones with
366          * index >= total_pages, locate at the tail of last pool. */
367         freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
368         if (freeslot != 0)
369                 freeslot = PAGES_PER_POOL - freeslot;
370         freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
371
372         op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
373         og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
374         np_idx = npools - 1;
375         ng_idx = (npages - 1) % PAGES_PER_POOL;
376
377         while (freeslot) {
378                 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
379                 LASSERT(pools[np_idx][ng_idx] != NULL);
380
381                 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
382                 pools[np_idx][ng_idx] = NULL;
383
384                 freeslot--;
385
386                 if (++og_idx == PAGES_PER_POOL) {
387                         op_idx++;
388                         og_idx = 0;
389                 }
390                 if (--ng_idx < 0) {
391                         if (np_idx == 0)
392                                 break;
393                         np_idx--;
394                         ng_idx = PAGES_PER_POOL - 1;
395                 }
396         }
397
398         /*
399          * (2) add pools if needed.
400          */
401         cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
402                      PAGES_PER_POOL;
403         end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
404                      PAGES_PER_POOL;
405         LASSERT(end_npools <= page_pools.epp_max_pools);
406
407         np_idx = 0;
408         while (cur_npools < end_npools) {
409                 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
410                 LASSERT(np_idx < npools);
411                 LASSERT(pools[np_idx] != NULL);
412
413                 page_pools.epp_pools[cur_npools++] = pools[np_idx];
414                 pools[np_idx++] = NULL;
415         }
416
417         page_pools.epp_total_pages += npages;
418         page_pools.epp_free_pages += npages;
419         page_pools.epp_st_lowfree = page_pools.epp_free_pages;
420
421         if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
422                 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
423
424         CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
425                page_pools.epp_total_pages);
426
427         spin_unlock(&page_pools.epp_lock);
428 }
429
430 static int enc_pools_add_pages(int npages)
431 {
432         static DEFINE_MUTEX(add_pages_mutex);
433         struct page   ***pools;
434         int             npools, alloced = 0;
435         int             i, j, rc = -ENOMEM;
436
437         if (npages < PTLRPC_MAX_BRW_PAGES)
438                 npages = PTLRPC_MAX_BRW_PAGES;
439
440         mutex_lock(&add_pages_mutex);
441
442         if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
443                 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
444         LASSERT(npages > 0);
445
446         page_pools.epp_st_grows++;
447
448         npools = npages_to_npools(npages);
449         OBD_ALLOC(pools, npools * sizeof(*pools));
450         if (pools == NULL)
451                 goto out;
452
453         for (i = 0; i < npools; i++) {
454                 OBD_ALLOC(pools[i], PAGE_CACHE_SIZE);
455                 if (pools[i] == NULL)
456                         goto out_pools;
457
458                 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
459                         pools[i][j] = alloc_page(GFP_NOFS |
460                                                  __GFP_HIGHMEM);
461                         if (pools[i][j] == NULL)
462                                 goto out_pools;
463
464                         alloced++;
465                 }
466         }
467         LASSERT(alloced == npages);
468
469         enc_pools_insert(pools, npools, npages);
470         CDEBUG(D_SEC, "added %d pages into pools\n", npages);
471         rc = 0;
472
473 out_pools:
474         enc_pools_cleanup(pools, npools);
475         OBD_FREE(pools, npools * sizeof(*pools));
476 out:
477         if (rc) {
478                 page_pools.epp_st_grow_fails++;
479                 CERROR("Failed to allocate %d enc pages\n", npages);
480         }
481
482         mutex_unlock(&add_pages_mutex);
483         return rc;
484 }
485
486 static inline void enc_pools_wakeup(void)
487 {
488         assert_spin_locked(&page_pools.epp_lock);
489
490         if (unlikely(page_pools.epp_waitqlen)) {
491                 LASSERT(waitqueue_active(&page_pools.epp_waitq));
492                 wake_up_all(&page_pools.epp_waitq);
493         }
494 }
495
496 static int enc_pools_should_grow(int page_needed, long now)
497 {
498         /* don't grow if someone else is growing the pools right now,
499          * or the pools has reached its full capacity
500          */
501         if (page_pools.epp_growing ||
502             page_pools.epp_total_pages == page_pools.epp_max_pages)
503                 return 0;
504
505         /* if total pages is not enough, we need to grow */
506         if (page_pools.epp_total_pages < page_needed)
507                 return 1;
508
509         /*
510          * we wanted to return 0 here if there was a shrink just happened
511          * moment ago, but this may cause deadlock if both client and ost
512          * live on single node.
513          */
514 #if 0
515         if (now - page_pools.epp_last_shrink < 2)
516                 return 0;
517 #endif
518
519         /*
520          * here we perhaps need consider other factors like wait queue
521          * length, idle index, etc. ?
522          */
523
524         /* grow the pools in any other cases */
525         return 1;
526 }
527
528 /*
529  * we allocate the requested pages atomically.
530  */
531 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
532 {
533         wait_queue_t  waitlink;
534         unsigned long   this_idle = -1;
535         cfs_time_t      tick = 0;
536         long            now;
537         int             p_idx, g_idx;
538         int             i;
539
540         LASSERT(desc->bd_iov_count > 0);
541         LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
542
543         /* resent bulk, enc iov might have been allocated previously */
544         if (desc->bd_enc_iov != NULL)
545                 return 0;
546
547         OBD_ALLOC(desc->bd_enc_iov,
548                   desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
549         if (desc->bd_enc_iov == NULL)
550                 return -ENOMEM;
551
552         spin_lock(&page_pools.epp_lock);
553
554         page_pools.epp_st_access++;
555 again:
556         if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
557                 if (tick == 0)
558                         tick = cfs_time_current();
559
560                 now = cfs_time_current_sec();
561
562                 page_pools.epp_st_missings++;
563                 page_pools.epp_pages_short += desc->bd_iov_count;
564
565                 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
566                         page_pools.epp_growing = 1;
567
568                         spin_unlock(&page_pools.epp_lock);
569                         enc_pools_add_pages(page_pools.epp_pages_short / 2);
570                         spin_lock(&page_pools.epp_lock);
571
572                         page_pools.epp_growing = 0;
573
574                         enc_pools_wakeup();
575                 } else {
576                         if (++page_pools.epp_waitqlen >
577                             page_pools.epp_st_max_wqlen)
578                                 page_pools.epp_st_max_wqlen =
579                                                 page_pools.epp_waitqlen;
580
581                         set_current_state(TASK_UNINTERRUPTIBLE);
582                         init_waitqueue_entry_current(&waitlink);
583                         add_wait_queue(&page_pools.epp_waitq, &waitlink);
584
585                         spin_unlock(&page_pools.epp_lock);
586                         waitq_wait(&waitlink, TASK_UNINTERRUPTIBLE);
587                         remove_wait_queue(&page_pools.epp_waitq, &waitlink);
588                         LASSERT(page_pools.epp_waitqlen > 0);
589                         spin_lock(&page_pools.epp_lock);
590                         page_pools.epp_waitqlen--;
591                 }
592
593                 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
594                 page_pools.epp_pages_short -= desc->bd_iov_count;
595
596                 this_idle = 0;
597                 goto again;
598         }
599
600         /* record max wait time */
601         if (unlikely(tick != 0)) {
602                 tick = cfs_time_current() - tick;
603                 if (tick > page_pools.epp_st_max_wait)
604                         page_pools.epp_st_max_wait = tick;
605         }
606
607         /* proceed with rest of allocation */
608         page_pools.epp_free_pages -= desc->bd_iov_count;
609
610         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
611         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
612
613         for (i = 0; i < desc->bd_iov_count; i++) {
614                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
615                 desc->bd_enc_iov[i].kiov_page =
616                                         page_pools.epp_pools[p_idx][g_idx];
617                 page_pools.epp_pools[p_idx][g_idx] = NULL;
618
619                 if (++g_idx == PAGES_PER_POOL) {
620                         p_idx++;
621                         g_idx = 0;
622                 }
623         }
624
625         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
626                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
627
628         /*
629          * new idle index = (old * weight + new) / (weight + 1)
630          */
631         if (this_idle == -1) {
632                 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
633                             page_pools.epp_total_pages;
634         }
635         page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
636                                    this_idle) /
637                                   (IDLE_IDX_WEIGHT + 1);
638
639         page_pools.epp_last_access = cfs_time_current_sec();
640
641         spin_unlock(&page_pools.epp_lock);
642         return 0;
643 }
644 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
645
646 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
647 {
648         int     p_idx, g_idx;
649         int     i;
650
651         if (desc->bd_enc_iov == NULL)
652                 return;
653
654         LASSERT(desc->bd_iov_count > 0);
655
656         spin_lock(&page_pools.epp_lock);
657
658         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
659         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
660
661         LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
662                 page_pools.epp_total_pages);
663         LASSERT(page_pools.epp_pools[p_idx]);
664
665         for (i = 0; i < desc->bd_iov_count; i++) {
666                 LASSERT(desc->bd_enc_iov[i].kiov_page != NULL);
667                 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
668                 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
669
670                 page_pools.epp_pools[p_idx][g_idx] =
671                                         desc->bd_enc_iov[i].kiov_page;
672
673                 if (++g_idx == PAGES_PER_POOL) {
674                         p_idx++;
675                         g_idx = 0;
676                 }
677         }
678
679         page_pools.epp_free_pages += desc->bd_iov_count;
680
681         enc_pools_wakeup();
682
683         spin_unlock(&page_pools.epp_lock);
684
685         OBD_FREE(desc->bd_enc_iov,
686                  desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
687         desc->bd_enc_iov = NULL;
688 }
689 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
690
691 /*
692  * we don't do much stuff for add_user/del_user anymore, except adding some
693  * initial pages in add_user() if current pools are empty, rest would be
694  * handled by the pools's self-adaption.
695  */
696 int sptlrpc_enc_pool_add_user(void)
697 {
698         int     need_grow = 0;
699
700         spin_lock(&page_pools.epp_lock);
701         if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
702                 page_pools.epp_growing = 1;
703                 need_grow = 1;
704         }
705         spin_unlock(&page_pools.epp_lock);
706
707         if (need_grow) {
708                 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
709                                     PTLRPC_MAX_BRW_PAGES);
710
711                 spin_lock(&page_pools.epp_lock);
712                 page_pools.epp_growing = 0;
713                 enc_pools_wakeup();
714                 spin_unlock(&page_pools.epp_lock);
715         }
716         return 0;
717 }
718 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
719
720 int sptlrpc_enc_pool_del_user(void)
721 {
722         return 0;
723 }
724 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
725
726 static inline void enc_pools_alloc(void)
727 {
728         LASSERT(page_pools.epp_max_pools);
729         OBD_ALLOC_LARGE(page_pools.epp_pools,
730                         page_pools.epp_max_pools *
731                         sizeof(*page_pools.epp_pools));
732 }
733
734 static inline void enc_pools_free(void)
735 {
736         LASSERT(page_pools.epp_max_pools);
737         LASSERT(page_pools.epp_pools);
738
739         OBD_FREE_LARGE(page_pools.epp_pools,
740                        page_pools.epp_max_pools *
741                        sizeof(*page_pools.epp_pools));
742 }
743
744 int sptlrpc_enc_pool_init(void)
745 {
746         DEF_SHRINKER_VAR(shvar, enc_pools_shrink,
747                          enc_pools_shrink_count, enc_pools_shrink_scan);
748         /*
749          * maximum capacity is 1/8 of total physical memory.
750          * is the 1/8 a good number?
751          */
752         page_pools.epp_max_pages = totalram_pages / 8;
753         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
754
755         init_waitqueue_head(&page_pools.epp_waitq);
756         page_pools.epp_waitqlen = 0;
757         page_pools.epp_pages_short = 0;
758
759         page_pools.epp_growing = 0;
760
761         page_pools.epp_idle_idx = 0;
762         page_pools.epp_last_shrink = cfs_time_current_sec();
763         page_pools.epp_last_access = cfs_time_current_sec();
764
765         spin_lock_init(&page_pools.epp_lock);
766         page_pools.epp_total_pages = 0;
767         page_pools.epp_free_pages = 0;
768
769         page_pools.epp_st_max_pages = 0;
770         page_pools.epp_st_grows = 0;
771         page_pools.epp_st_grow_fails = 0;
772         page_pools.epp_st_shrinks = 0;
773         page_pools.epp_st_access = 0;
774         page_pools.epp_st_missings = 0;
775         page_pools.epp_st_lowfree = 0;
776         page_pools.epp_st_max_wqlen = 0;
777         page_pools.epp_st_max_wait = 0;
778
779         enc_pools_alloc();
780         if (page_pools.epp_pools == NULL)
781                 return -ENOMEM;
782
783         pools_shrinker = set_shrinker(pools_shrinker_seeks, &shvar);
784         if (pools_shrinker == NULL) {
785                 enc_pools_free();
786                 return -ENOMEM;
787         }
788
789         return 0;
790 }
791
792 void sptlrpc_enc_pool_fini(void)
793 {
794         unsigned long cleaned, npools;
795
796         LASSERT(pools_shrinker);
797         LASSERT(page_pools.epp_pools);
798         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
799
800         remove_shrinker(pools_shrinker);
801
802         npools = npages_to_npools(page_pools.epp_total_pages);
803         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
804         LASSERT(cleaned == page_pools.epp_total_pages);
805
806         enc_pools_free();
807
808         if (page_pools.epp_st_access > 0) {
809                 CDEBUG(D_SEC,
810                        "max pages %lu, grows %u, grow fails %u, shrinks %u, "
811                        "access %lu, missing %lu, max qlen %u, max wait "
812                        CFS_TIME_T"/%d\n",
813                        page_pools.epp_st_max_pages, page_pools.epp_st_grows,
814                        page_pools.epp_st_grow_fails,
815                        page_pools.epp_st_shrinks, page_pools.epp_st_access,
816                        page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
817                        page_pools.epp_st_max_wait, HZ);
818         }
819 }
820
821 #else /* !__KERNEL__ */
822
823 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
824 {
825         return 0;
826 }
827
828 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
829 {
830 }
831
832 int sptlrpc_enc_pool_init(void)
833 {
834         return 0;
835 }
836
837 void sptlrpc_enc_pool_fini(void)
838 {
839 }
840 #endif
841
842 static int cfs_hash_alg_id[] = {
843         [BULK_HASH_ALG_NULL]    = CFS_HASH_ALG_NULL,
844         [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
845         [BULK_HASH_ALG_CRC32]   = CFS_HASH_ALG_CRC32,
846         [BULK_HASH_ALG_MD5]     = CFS_HASH_ALG_MD5,
847         [BULK_HASH_ALG_SHA1]    = CFS_HASH_ALG_SHA1,
848         [BULK_HASH_ALG_SHA256]  = CFS_HASH_ALG_SHA256,
849         [BULK_HASH_ALG_SHA384]  = CFS_HASH_ALG_SHA384,
850         [BULK_HASH_ALG_SHA512]  = CFS_HASH_ALG_SHA512,
851 };
852 const char * sptlrpc_get_hash_name(__u8 hash_alg)
853 {
854         return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
855 }
856 EXPORT_SYMBOL(sptlrpc_get_hash_name);
857
858 __u8 sptlrpc_get_hash_alg(const char *algname)
859 {
860         return cfs_crypto_hash_alg(algname);
861 }
862 EXPORT_SYMBOL(sptlrpc_get_hash_alg);
863
864 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
865 {
866         struct ptlrpc_bulk_sec_desc *bsd;
867         int                          size = msg->lm_buflens[offset];
868
869         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
870         if (bsd == NULL) {
871                 CERROR("Invalid bulk sec desc: size %d\n", size);
872                 return -EINVAL;
873         }
874
875         if (swabbed) {
876                 __swab32s(&bsd->bsd_nob);
877         }
878
879         if (unlikely(bsd->bsd_version != 0)) {
880                 CERROR("Unexpected version %u\n", bsd->bsd_version);
881                 return -EPROTO;
882         }
883
884         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
885                 CERROR("Invalid type %u\n", bsd->bsd_type);
886                 return -EPROTO;
887         }
888
889         /* FIXME more sanity check here */
890
891         if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
892                      bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
893                      bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
894                 CERROR("Invalid svc %u\n", bsd->bsd_svc);
895                 return -EPROTO;
896         }
897
898         return 0;
899 }
900 EXPORT_SYMBOL(bulk_sec_desc_unpack);
901
902 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
903                               void *buf, int buflen)
904 {
905         struct cfs_crypto_hash_desc     *hdesc;
906         int                             hashsize;
907         char                            hashbuf[64];
908         unsigned int                    bufsize;
909         int                             i, err;
910
911         LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
912         LASSERT(buflen >= 4);
913
914         hdesc = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
915         if (IS_ERR(hdesc)) {
916                 CERROR("Unable to initialize checksum hash %s\n",
917                        cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
918                 return PTR_ERR(hdesc);
919         }
920
921         hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
922
923         for (i = 0; i < desc->bd_iov_count; i++) {
924 #ifdef __KERNEL__
925                 cfs_crypto_hash_update_page(hdesc, desc->bd_iov[i].kiov_page,
926                                   desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK,
927                                   desc->bd_iov[i].kiov_len);
928 #else
929                 cfs_crypto_hash_update(hdesc, desc->bd_iov[i].iov_base,
930                                   desc->bd_iov[i].iov_len);
931 #endif
932         }
933         if (hashsize > buflen) {
934                 bufsize = sizeof(hashbuf);
935                 err = cfs_crypto_hash_final(hdesc, (unsigned char *)hashbuf,
936                                             &bufsize);
937                 memcpy(buf, hashbuf, buflen);
938         } else {
939                 bufsize = buflen;
940                 err = cfs_crypto_hash_final(hdesc, (unsigned char *)buf,
941                                             &bufsize);
942         }
943
944         if (err)
945                 cfs_crypto_hash_final(hdesc, NULL, NULL);
946         return err;
947 }
948 EXPORT_SYMBOL(sptlrpc_get_bulk_checksum);
949
950