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