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