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