Whamcloud - gitweb
LU-4499 nrs: adjust the order of REQ NRS initilization
[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, 2014, 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
45 #include <obd.h>
46 #include <obd_cksum.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lustre_net.h>
50 #include <lustre_import.h>
51 #include <lustre_dlm.h>
52 #include <lustre_sec.h>
53
54 #include "ptlrpc_internal.h"
55
56 static int mult = 20 - PAGE_CACHE_SHIFT;
57 static int enc_pool_max_memory_mb;
58 CFS_MODULE_PARM(enc_pool_max_memory_mb, "i", int, 0644,
59                 "Encoding pool max memory (MB), 1/8 of total physical memory by default");
60
61
62 /****************************************
63  * bulk encryption page pools           *
64  ****************************************/
65
66
67 #define PTRS_PER_PAGE   (PAGE_CACHE_SIZE / sizeof(void *))
68 #define PAGES_PER_POOL  (PTRS_PER_PAGE)
69
70 #define IDLE_IDX_MAX            (100)
71 #define IDLE_IDX_WEIGHT         (3)
72
73 #define CACHE_QUIESCENT_PERIOD  (20)
74
75 static struct ptlrpc_enc_page_pool {
76         /*
77          * constants
78          */
79         unsigned long    epp_max_pages;   /* maximum pages can hold, const */
80         unsigned int     epp_max_pools;   /* number of pools, const */
81
82         /*
83          * wait queue in case of not enough free pages.
84          */
85         wait_queue_head_t    epp_waitq;       /* waiting threads */
86         unsigned int     epp_waitqlen;    /* wait queue length */
87         unsigned long    epp_pages_short; /* # of pages wanted of in-q users */
88         unsigned int     epp_growing:1;   /* during adding pages */
89
90         /*
91          * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
92          * this is counted based on each time when getting pages from
93          * the pools, not based on time. which means in case that system
94          * is idled for a while but the idle_idx might still be low if no
95          * activities happened in the pools.
96          */
97         unsigned long    epp_idle_idx;
98
99         /* last shrink time due to mem tight */
100         long             epp_last_shrink;
101         long             epp_last_access;
102
103         /*
104          * in-pool pages bookkeeping
105          */
106         spinlock_t       epp_lock;         /* protect following fields */
107         unsigned long    epp_total_pages; /* total pages in pools */
108         unsigned long    epp_free_pages;  /* current pages available */
109
110         /*
111          * statistics
112          */
113         unsigned long    epp_st_max_pages;      /* # of pages ever reached */
114         unsigned int     epp_st_grows;          /* # of grows */
115         unsigned int     epp_st_grow_fails;     /* # of add pages failures */
116         unsigned int     epp_st_shrinks;        /* # of shrinks */
117         unsigned long    epp_st_access;         /* # of access */
118         unsigned long    epp_st_missings;       /* # of cache missing */
119         unsigned long    epp_st_lowfree;        /* lowest free pages reached */
120         unsigned int     epp_st_max_wqlen;      /* highest waitqueue length */
121         cfs_time_t       epp_st_max_wait;       /* in jeffies */
122         /*
123          * pointers to pools
124          */
125         struct page    ***epp_pools;
126 } page_pools;
127
128 /*
129  * memory shrinker
130  */
131 static const int pools_shrinker_seeks = DEFAULT_SEEKS;
132 static struct shrinker *pools_shrinker;
133
134
135 /*
136  * /proc/fs/lustre/sptlrpc/encrypt_page_pools
137  */
138 int sptlrpc_proc_enc_pool_seq_show(struct seq_file *m, void *v)
139 {
140         int     rc;
141
142         spin_lock(&page_pools.epp_lock);
143
144         rc = seq_printf(m,
145                       "physical pages:          %lu\n"
146                       "pages per pool:          %lu\n"
147                       "max pages:               %lu\n"
148                       "max pools:               %u\n"
149                       "total pages:             %lu\n"
150                       "total free:              %lu\n"
151                       "idle index:              %lu/100\n"
152                       "last shrink:             %lds\n"
153                       "last access:             %lds\n"
154                       "max pages reached:       %lu\n"
155                       "grows:                   %u\n"
156                       "grows failure:           %u\n"
157                       "shrinks:                 %u\n"
158                       "cache access:            %lu\n"
159                       "cache missing:           %lu\n"
160                       "low free mark:           %lu\n"
161                       "max waitqueue depth:     %u\n"
162                       "max wait time:           "CFS_TIME_T"/%lu\n"
163                       ,
164                       totalram_pages,
165                       PAGES_PER_POOL,
166                       page_pools.epp_max_pages,
167                       page_pools.epp_max_pools,
168                       page_pools.epp_total_pages,
169                       page_pools.epp_free_pages,
170                       page_pools.epp_idle_idx,
171                       cfs_time_current_sec() - page_pools.epp_last_shrink,
172                       cfs_time_current_sec() - page_pools.epp_last_access,
173                       page_pools.epp_st_max_pages,
174                       page_pools.epp_st_grows,
175                       page_pools.epp_st_grow_fails,
176                       page_pools.epp_st_shrinks,
177                       page_pools.epp_st_access,
178                       page_pools.epp_st_missings,
179                       page_pools.epp_st_lowfree,
180                       page_pools.epp_st_max_wqlen,
181                       page_pools.epp_st_max_wait,
182                       msecs_to_jiffies(MSEC_PER_SEC)
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(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
541         LASSERT(desc->bd_iov_count > 0);
542         LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
543
544         /* resent bulk, enc iov might have been allocated previously */
545         if (GET_ENC_KIOV(desc) != NULL)
546                 return 0;
547
548         OBD_ALLOC(GET_ENC_KIOV(desc),
549                   desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
550         if (GET_ENC_KIOV(desc) == NULL)
551                 return -ENOMEM;
552
553         spin_lock(&page_pools.epp_lock);
554
555         page_pools.epp_st_access++;
556 again:
557         if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
558                 if (tick == 0)
559                         tick = cfs_time_current();
560
561                 now = cfs_time_current_sec();
562
563                 page_pools.epp_st_missings++;
564                 page_pools.epp_pages_short += desc->bd_iov_count;
565
566                 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
567                         page_pools.epp_growing = 1;
568
569                         spin_unlock(&page_pools.epp_lock);
570                         enc_pools_add_pages(page_pools.epp_pages_short / 2);
571                         spin_lock(&page_pools.epp_lock);
572
573                         page_pools.epp_growing = 0;
574
575                         enc_pools_wakeup();
576                 } else {
577                         if (++page_pools.epp_waitqlen >
578                             page_pools.epp_st_max_wqlen)
579                                 page_pools.epp_st_max_wqlen =
580                                                 page_pools.epp_waitqlen;
581
582                         set_current_state(TASK_UNINTERRUPTIBLE);
583                         init_waitqueue_entry(&waitlink, current);
584                         add_wait_queue(&page_pools.epp_waitq, &waitlink);
585
586                         spin_unlock(&page_pools.epp_lock);
587                         schedule();
588                         remove_wait_queue(&page_pools.epp_waitq, &waitlink);
589                         LASSERT(page_pools.epp_waitqlen > 0);
590                         spin_lock(&page_pools.epp_lock);
591                         page_pools.epp_waitqlen--;
592                 }
593
594                 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
595                 page_pools.epp_pages_short -= desc->bd_iov_count;
596
597                 this_idle = 0;
598                 goto again;
599         }
600
601         /* record max wait time */
602         if (unlikely(tick != 0)) {
603                 tick = cfs_time_current() - tick;
604                 if (tick > page_pools.epp_st_max_wait)
605                         page_pools.epp_st_max_wait = tick;
606         }
607
608         /* proceed with rest of allocation */
609         page_pools.epp_free_pages -= desc->bd_iov_count;
610
611         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
612         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
613
614         for (i = 0; i < desc->bd_iov_count; i++) {
615                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
616                 BD_GET_ENC_KIOV(desc, i).kiov_page =
617                        page_pools.epp_pools[p_idx][g_idx];
618                 page_pools.epp_pools[p_idx][g_idx] = NULL;
619
620                 if (++g_idx == PAGES_PER_POOL) {
621                         p_idx++;
622                         g_idx = 0;
623                 }
624         }
625
626         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
627                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
628
629         /*
630          * new idle index = (old * weight + new) / (weight + 1)
631          */
632         if (this_idle == -1) {
633                 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
634                             page_pools.epp_total_pages;
635         }
636         page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
637                                    this_idle) /
638                                   (IDLE_IDX_WEIGHT + 1);
639
640         page_pools.epp_last_access = cfs_time_current_sec();
641
642         spin_unlock(&page_pools.epp_lock);
643         return 0;
644 }
645 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
646
647 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
648 {
649         int     p_idx, g_idx;
650         int     i;
651
652         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
653
654         if (GET_ENC_KIOV(desc) == NULL)
655                 return;
656
657         LASSERT(desc->bd_iov_count > 0);
658
659         spin_lock(&page_pools.epp_lock);
660
661         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
662         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
663
664         LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
665                 page_pools.epp_total_pages);
666         LASSERT(page_pools.epp_pools[p_idx]);
667
668         for (i = 0; i < desc->bd_iov_count; i++) {
669                 LASSERT(BD_GET_ENC_KIOV(desc, i).kiov_page != NULL);
670                 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
671                 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
672
673                 page_pools.epp_pools[p_idx][g_idx] =
674                         BD_GET_ENC_KIOV(desc, i).kiov_page;
675
676                 if (++g_idx == PAGES_PER_POOL) {
677                         p_idx++;
678                         g_idx = 0;
679                 }
680         }
681
682         page_pools.epp_free_pages += desc->bd_iov_count;
683
684         enc_pools_wakeup();
685
686         spin_unlock(&page_pools.epp_lock);
687
688         OBD_FREE(GET_ENC_KIOV(desc),
689                  desc->bd_iov_count * sizeof(*GET_ENC_KIOV(desc)));
690         GET_ENC_KIOV(desc) = NULL;
691 }
692
693 /*
694  * we don't do much stuff for add_user/del_user anymore, except adding some
695  * initial pages in add_user() if current pools are empty, rest would be
696  * handled by the pools's self-adaption.
697  */
698 int sptlrpc_enc_pool_add_user(void)
699 {
700         int     need_grow = 0;
701
702         spin_lock(&page_pools.epp_lock);
703         if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
704                 page_pools.epp_growing = 1;
705                 need_grow = 1;
706         }
707         spin_unlock(&page_pools.epp_lock);
708
709         if (need_grow) {
710                 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
711                                     PTLRPC_MAX_BRW_PAGES);
712
713                 spin_lock(&page_pools.epp_lock);
714                 page_pools.epp_growing = 0;
715                 enc_pools_wakeup();
716                 spin_unlock(&page_pools.epp_lock);
717         }
718         return 0;
719 }
720 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
721
722 int sptlrpc_enc_pool_del_user(void)
723 {
724         return 0;
725 }
726 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
727
728 static inline void enc_pools_alloc(void)
729 {
730         LASSERT(page_pools.epp_max_pools);
731         OBD_ALLOC_LARGE(page_pools.epp_pools,
732                         page_pools.epp_max_pools *
733                         sizeof(*page_pools.epp_pools));
734 }
735
736 static inline void enc_pools_free(void)
737 {
738         LASSERT(page_pools.epp_max_pools);
739         LASSERT(page_pools.epp_pools);
740
741         OBD_FREE_LARGE(page_pools.epp_pools,
742                        page_pools.epp_max_pools *
743                        sizeof(*page_pools.epp_pools));
744 }
745
746 int sptlrpc_enc_pool_init(void)
747 {
748         DEF_SHRINKER_VAR(shvar, enc_pools_shrink,
749                          enc_pools_shrink_count, enc_pools_shrink_scan);
750
751         page_pools.epp_max_pages = totalram_pages / 8;
752         if (enc_pool_max_memory_mb > 0 &&
753             enc_pool_max_memory_mb <= (totalram_pages >> mult))
754                 page_pools.epp_max_pages = enc_pool_max_memory_mb << mult;
755
756         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
757
758         init_waitqueue_head(&page_pools.epp_waitq);
759         page_pools.epp_waitqlen = 0;
760         page_pools.epp_pages_short = 0;
761
762         page_pools.epp_growing = 0;
763
764         page_pools.epp_idle_idx = 0;
765         page_pools.epp_last_shrink = cfs_time_current_sec();
766         page_pools.epp_last_access = cfs_time_current_sec();
767
768         spin_lock_init(&page_pools.epp_lock);
769         page_pools.epp_total_pages = 0;
770         page_pools.epp_free_pages = 0;
771
772         page_pools.epp_st_max_pages = 0;
773         page_pools.epp_st_grows = 0;
774         page_pools.epp_st_grow_fails = 0;
775         page_pools.epp_st_shrinks = 0;
776         page_pools.epp_st_access = 0;
777         page_pools.epp_st_missings = 0;
778         page_pools.epp_st_lowfree = 0;
779         page_pools.epp_st_max_wqlen = 0;
780         page_pools.epp_st_max_wait = 0;
781
782         enc_pools_alloc();
783         if (page_pools.epp_pools == NULL)
784                 return -ENOMEM;
785
786         pools_shrinker = set_shrinker(pools_shrinker_seeks, &shvar);
787         if (pools_shrinker == NULL) {
788                 enc_pools_free();
789                 return -ENOMEM;
790         }
791
792         return 0;
793 }
794
795 void sptlrpc_enc_pool_fini(void)
796 {
797         unsigned long cleaned, npools;
798
799         LASSERT(pools_shrinker);
800         LASSERT(page_pools.epp_pools);
801         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
802
803         remove_shrinker(pools_shrinker);
804
805         npools = npages_to_npools(page_pools.epp_total_pages);
806         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
807         LASSERT(cleaned == page_pools.epp_total_pages);
808
809         enc_pools_free();
810
811         if (page_pools.epp_st_access > 0) {
812                 CDEBUG(D_SEC,
813                        "max pages %lu, grows %u, grow fails %u, shrinks %u, "
814                        "access %lu, missing %lu, max qlen %u, max wait "
815                        CFS_TIME_T"/%lu\n",
816                        page_pools.epp_st_max_pages, page_pools.epp_st_grows,
817                        page_pools.epp_st_grow_fails,
818                        page_pools.epp_st_shrinks, page_pools.epp_st_access,
819                        page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
820                        page_pools.epp_st_max_wait,
821                        msecs_to_jiffies(MSEC_PER_SEC));
822         }
823 }
824
825
826 static int cfs_hash_alg_id[] = {
827         [BULK_HASH_ALG_NULL]    = CFS_HASH_ALG_NULL,
828         [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
829         [BULK_HASH_ALG_CRC32]   = CFS_HASH_ALG_CRC32,
830         [BULK_HASH_ALG_MD5]     = CFS_HASH_ALG_MD5,
831         [BULK_HASH_ALG_SHA1]    = CFS_HASH_ALG_SHA1,
832         [BULK_HASH_ALG_SHA256]  = CFS_HASH_ALG_SHA256,
833         [BULK_HASH_ALG_SHA384]  = CFS_HASH_ALG_SHA384,
834         [BULK_HASH_ALG_SHA512]  = CFS_HASH_ALG_SHA512,
835 };
836 const char * sptlrpc_get_hash_name(__u8 hash_alg)
837 {
838         return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
839 }
840
841 __u8 sptlrpc_get_hash_alg(const char *algname)
842 {
843         return cfs_crypto_hash_alg(algname);
844 }
845
846 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
847 {
848         struct ptlrpc_bulk_sec_desc *bsd;
849         int                          size = msg->lm_buflens[offset];
850
851         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
852         if (bsd == NULL) {
853                 CERROR("Invalid bulk sec desc: size %d\n", size);
854                 return -EINVAL;
855         }
856
857         if (swabbed) {
858                 __swab32s(&bsd->bsd_nob);
859         }
860
861         if (unlikely(bsd->bsd_version != 0)) {
862                 CERROR("Unexpected version %u\n", bsd->bsd_version);
863                 return -EPROTO;
864         }
865
866         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
867                 CERROR("Invalid type %u\n", bsd->bsd_type);
868                 return -EPROTO;
869         }
870
871         /* FIXME more sanity check here */
872
873         if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
874                      bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
875                      bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
876                 CERROR("Invalid svc %u\n", bsd->bsd_svc);
877                 return -EPROTO;
878         }
879
880         return 0;
881 }
882 EXPORT_SYMBOL(bulk_sec_desc_unpack);
883
884 /*
885  * Compute the checksum of an RPC buffer payload.  If the return \a buflen
886  * is not large enough, truncate the result to fit so that it is possible
887  * to use a hash function with a large hash space, but only use a part of
888  * the resulting hash.
889  */
890 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
891                               void *buf, int buflen)
892 {
893         struct cfs_crypto_hash_desc     *hdesc;
894         int                             hashsize;
895         unsigned int                    bufsize;
896         int                             i, err;
897
898         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
899         LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
900         LASSERT(buflen >= 4);
901
902         hdesc = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
903         if (IS_ERR(hdesc)) {
904                 CERROR("Unable to initialize checksum hash %s\n",
905                        cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
906                 return PTR_ERR(hdesc);
907         }
908
909         hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
910
911         for (i = 0; i < desc->bd_iov_count; i++) {
912                 cfs_crypto_hash_update_page(hdesc,
913                                   BD_GET_KIOV(desc, i).kiov_page,
914                                   BD_GET_KIOV(desc, i).kiov_offset &
915                                               ~PAGE_MASK,
916                                   BD_GET_KIOV(desc, i).kiov_len);
917         }
918
919         if (hashsize > buflen) {
920                 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
921
922                 bufsize = sizeof(hashbuf);
923                 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
924                          bufsize, hashsize);
925                 err = cfs_crypto_hash_final(hdesc, hashbuf, &bufsize);
926                 memcpy(buf, hashbuf, buflen);
927         } else {
928                 bufsize = buflen;
929                 err = cfs_crypto_hash_final(hdesc, buf, &bufsize);
930         }
931
932         return err;
933 }