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