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