Whamcloud - gitweb
LU-371 call OBD_ALLOC_LARGE to allocate epp_pools
[fs/lustre-release.git] / lustre / ptlrpc / sec_bulk.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
42 #define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_SEC
45
46 #include <libcfs/libcfs.h>
47 #ifndef __KERNEL__
48 #include <liblustre.h>
49 #include <libcfs/list.h>
50 #else
51 #include <linux/crypto.h>
52 #endif
53
54 #include <obd.h>
55 #include <obd_cksum.h>
56 #include <obd_class.h>
57 #include <obd_support.h>
58 #include <lustre_net.h>
59 #include <lustre_import.h>
60 #include <lustre_dlm.h>
61 #include <lustre_sec.h>
62
63 #include "ptlrpc_internal.h"
64
65 /****************************************
66  * bulk encryption page pools           *
67  ****************************************/
68
69 #ifdef __KERNEL__
70
71 #define PTRS_PER_PAGE   (CFS_PAGE_SIZE / sizeof(void *))
72 #define PAGES_PER_POOL  (PTRS_PER_PAGE)
73
74 #define IDLE_IDX_MAX            (100)
75 #define IDLE_IDX_WEIGHT         (3)
76
77 #define CACHE_QUIESCENT_PERIOD  (20)
78
79 static struct ptlrpc_enc_page_pool {
80         /*
81          * constants
82          */
83         unsigned long    epp_max_pages;   /* maximum pages can hold, const */
84         unsigned int     epp_max_pools;   /* number of pools, const */
85
86         /*
87          * wait queue in case of not enough free pages.
88          */
89         cfs_waitq_t      epp_waitq;       /* waiting threads */
90         unsigned int     epp_waitqlen;    /* wait queue length */
91         unsigned long    epp_pages_short; /* # of pages wanted of in-q users */
92         unsigned int     epp_growing:1;   /* during adding pages */
93
94         /*
95          * indicating how idle the pools are, from 0 to MAX_IDLE_IDX
96          * this is counted based on each time when getting pages from
97          * the pools, not based on time. which means in case that system
98          * is idled for a while but the idle_idx might still be low if no
99          * activities happened in the pools.
100          */
101         unsigned long    epp_idle_idx;
102
103         /* last shrink time due to mem tight */
104         long             epp_last_shrink;
105         long             epp_last_access;
106
107         /*
108          * in-pool pages bookkeeping
109          */
110         cfs_spinlock_t   epp_lock;        /* protect following fields */
111         unsigned long    epp_total_pages; /* total pages in pools */
112         unsigned long    epp_free_pages;  /* current pages available */
113
114         /*
115          * statistics
116          */
117         unsigned long    epp_st_max_pages;      /* # of pages ever reached */
118         unsigned int     epp_st_grows;          /* # of grows */
119         unsigned int     epp_st_grow_fails;     /* # of add pages failures */
120         unsigned int     epp_st_shrinks;        /* # of shrinks */
121         unsigned long    epp_st_access;         /* # of access */
122         unsigned long    epp_st_missings;       /* # of cache missing */
123         unsigned long    epp_st_lowfree;        /* lowest free pages reached */
124         unsigned int     epp_st_max_wqlen;      /* highest waitqueue length */
125         cfs_time_t       epp_st_max_wait;       /* in jeffies */
126         /*
127          * pointers to pools
128          */
129         cfs_page_t    ***epp_pools;
130 } page_pools;
131
132 /*
133  * memory shrinker
134  */
135 const int pools_shrinker_seeks = CFS_DEFAULT_SEEKS;
136 static struct cfs_shrinker *pools_shrinker = NULL;
137
138
139 /*
140  * /proc/fs/lustre/sptlrpc/encrypt_page_pools
141  */
142 int sptlrpc_proc_read_enc_pool(char *page, char **start, off_t off, int count,
143                                int *eof, void *data)
144 {
145         int     rc;
146
147         cfs_spin_lock(&page_pools.epp_lock);
148
149         rc = snprintf(page, count,
150                       "physical pages:          %lu\n"
151                       "pages per pool:          %lu\n"
152                       "max pages:               %lu\n"
153                       "max pools:               %u\n"
154                       "total pages:             %lu\n"
155                       "total free:              %lu\n"
156                       "idle index:              %lu/100\n"
157                       "last shrink:             %lds\n"
158                       "last access:             %lds\n"
159                       "max pages reached:       %lu\n"
160                       "grows:                   %u\n"
161                       "grows failure:           %u\n"
162                       "shrinks:                 %u\n"
163                       "cache access:            %lu\n"
164                       "cache missing:           %lu\n"
165                       "low free mark:           %lu\n"
166                       "max waitqueue depth:     %u\n"
167                       "max wait time:           "CFS_TIME_T"/%u\n"
168                       ,
169                       cfs_num_physpages,
170                       PAGES_PER_POOL,
171                       page_pools.epp_max_pages,
172                       page_pools.epp_max_pools,
173                       page_pools.epp_total_pages,
174                       page_pools.epp_free_pages,
175                       page_pools.epp_idle_idx,
176                       cfs_time_current_sec() - page_pools.epp_last_shrink,
177                       cfs_time_current_sec() - page_pools.epp_last_access,
178                       page_pools.epp_st_max_pages,
179                       page_pools.epp_st_grows,
180                       page_pools.epp_st_grow_fails,
181                       page_pools.epp_st_shrinks,
182                       page_pools.epp_st_access,
183                       page_pools.epp_st_missings,
184                       page_pools.epp_st_lowfree,
185                       page_pools.epp_st_max_wqlen,
186                       page_pools.epp_st_max_wait, CFS_HZ
187                      );
188
189         cfs_spin_unlock(&page_pools.epp_lock);
190         return rc;
191 }
192
193 static void enc_pools_release_free_pages(long npages)
194 {
195         int     p_idx, g_idx;
196         int     p_idx_max1, p_idx_max2;
197
198         LASSERT(npages > 0);
199         LASSERT(npages <= page_pools.epp_free_pages);
200         LASSERT(page_pools.epp_free_pages <= page_pools.epp_total_pages);
201
202         /* max pool index before the release */
203         p_idx_max2 = (page_pools.epp_total_pages - 1) / PAGES_PER_POOL;
204
205         page_pools.epp_free_pages -= npages;
206         page_pools.epp_total_pages -= npages;
207
208         /* max pool index after the release */
209         p_idx_max1 = page_pools.epp_total_pages == 0 ? -1 :
210                      ((page_pools.epp_total_pages - 1) / PAGES_PER_POOL);
211
212         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
213         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
214         LASSERT(page_pools.epp_pools[p_idx]);
215
216         while (npages--) {
217                 LASSERT(page_pools.epp_pools[p_idx]);
218                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
219
220                 cfs_free_page(page_pools.epp_pools[p_idx][g_idx]);
221                 page_pools.epp_pools[p_idx][g_idx] = NULL;
222
223                 if (++g_idx == PAGES_PER_POOL) {
224                         p_idx++;
225                         g_idx = 0;
226                 }
227         };
228
229         /* free unused pools */
230         while (p_idx_max1 < p_idx_max2) {
231                 LASSERT(page_pools.epp_pools[p_idx_max2]);
232                 OBD_FREE(page_pools.epp_pools[p_idx_max2], CFS_PAGE_SIZE);
233                 page_pools.epp_pools[p_idx_max2] = NULL;
234                 p_idx_max2--;
235         }
236 }
237
238 /*
239  * could be called frequently for query (@nr_to_scan == 0).
240  * we try to keep at least PTLRPC_MAX_BRW_PAGES pages in the pool.
241  */
242 static int enc_pools_shrink(SHRINKER_FIRST_ARG int nr_to_scan,
243                             unsigned int gfp_mask)
244 {
245         if (unlikely(nr_to_scan != 0)) {
246                 cfs_spin_lock(&page_pools.epp_lock);
247                 nr_to_scan = min(nr_to_scan, (int) page_pools.epp_free_pages -
248                                  PTLRPC_MAX_BRW_PAGES);
249                 if (nr_to_scan > 0) {
250                         enc_pools_release_free_pages(nr_to_scan);
251                         CDEBUG(D_SEC, "released %d pages, %ld left\n",
252                                nr_to_scan, page_pools.epp_free_pages);
253
254                         page_pools.epp_st_shrinks++;
255                         page_pools.epp_last_shrink = cfs_time_current_sec();
256                 }
257                 cfs_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                 cfs_spin_lock(&page_pools.epp_lock);
267                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
268                 cfs_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(cfs_page_t ***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                                         cfs_free_page(pools[i][j]);
295                                         cleaned++;
296                                 }
297                         }
298                         OBD_FREE(pools[i], CFS_PAGE_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(cfs_page_t ***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         cfs_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         cfs_spin_unlock(&page_pools.epp_lock);
392 }
393
394 static int enc_pools_add_pages(int npages)
395 {
396         static CFS_DECLARE_MUTEX(sem_add_pages);
397         cfs_page_t   ***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         cfs_down(&sem_add_pages);
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], CFS_PAGE_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] = cfs_alloc_page(CFS_ALLOC_IO |
424                                                      CFS_ALLOC_HIGH);
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         cfs_up(&sem_add_pages);
447         return rc;
448 }
449
450 static inline void enc_pools_wakeup(void)
451 {
452         LASSERT_SPIN_LOCKED(&page_pools.epp_lock);
453         LASSERT(page_pools.epp_waitqlen >= 0);
454
455         if (unlikely(page_pools.epp_waitqlen)) {
456                 LASSERT(cfs_waitq_active(&page_pools.epp_waitq));
457                 cfs_waitq_broadcast(&page_pools.epp_waitq);
458         }
459 }
460
461 static int enc_pools_should_grow(int page_needed, long now)
462 {
463         /* don't grow if someone else is growing the pools right now,
464          * or the pools has reached its full capacity
465          */
466         if (page_pools.epp_growing ||
467             page_pools.epp_total_pages == page_pools.epp_max_pages)
468                 return 0;
469
470         /* if total pages is not enough, we need to grow */
471         if (page_pools.epp_total_pages < page_needed)
472                 return 1;
473
474         /*
475          * we wanted to return 0 here if there was a shrink just happened
476          * moment ago, but this may cause deadlock if both client and ost
477          * live on single node.
478          */
479 #if 0
480         if (now - page_pools.epp_last_shrink < 2)
481                 return 0;
482 #endif
483
484         /*
485          * here we perhaps need consider other factors like wait queue
486          * length, idle index, etc. ?
487          */
488
489         /* grow the pools in any other cases */
490         return 1;
491 }
492
493 /*
494  * we allocate the requested pages atomically.
495  */
496 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
497 {
498         cfs_waitlink_t  waitlink;
499         unsigned long   this_idle = -1;
500         cfs_time_t      tick = 0;
501         long            now;
502         int             p_idx, g_idx;
503         int             i;
504
505         LASSERT(desc->bd_iov_count > 0);
506         LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
507
508         /* resent bulk, enc iov might have been allocated previously */
509         if (desc->bd_enc_iov != NULL)
510                 return 0;
511
512         OBD_ALLOC(desc->bd_enc_iov,
513                   desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
514         if (desc->bd_enc_iov == NULL)
515                 return -ENOMEM;
516
517         cfs_spin_lock(&page_pools.epp_lock);
518
519         page_pools.epp_st_access++;
520 again:
521         if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
522                 if (tick == 0)
523                         tick = cfs_time_current();
524
525                 now = cfs_time_current_sec();
526
527                 page_pools.epp_st_missings++;
528                 page_pools.epp_pages_short += desc->bd_iov_count;
529
530                 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
531                         page_pools.epp_growing = 1;
532
533                         cfs_spin_unlock(&page_pools.epp_lock);
534                         enc_pools_add_pages(page_pools.epp_pages_short / 2);
535                         cfs_spin_lock(&page_pools.epp_lock);
536
537                         page_pools.epp_growing = 0;
538
539                         enc_pools_wakeup();
540                 } else {
541                         if (++page_pools.epp_waitqlen >
542                             page_pools.epp_st_max_wqlen)
543                                 page_pools.epp_st_max_wqlen =
544                                                 page_pools.epp_waitqlen;
545
546                         cfs_set_current_state(CFS_TASK_UNINT);
547                         cfs_waitlink_init(&waitlink);
548                         cfs_waitq_add(&page_pools.epp_waitq, &waitlink);
549
550                         cfs_spin_unlock(&page_pools.epp_lock);
551                         cfs_waitq_wait(&waitlink, CFS_TASK_UNINT);
552                         cfs_waitq_del(&page_pools.epp_waitq, &waitlink);
553                         LASSERT(page_pools.epp_waitqlen > 0);
554                         cfs_spin_lock(&page_pools.epp_lock);
555                         page_pools.epp_waitqlen--;
556                 }
557
558                 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
559                 page_pools.epp_pages_short -= desc->bd_iov_count;
560
561                 this_idle = 0;
562                 goto again;
563         }
564
565         /* record max wait time */
566         if (unlikely(tick != 0)) {
567                 tick = cfs_time_current() - tick;
568                 if (tick > page_pools.epp_st_max_wait)
569                         page_pools.epp_st_max_wait = tick;
570         }
571
572         /* proceed with rest of allocation */
573         page_pools.epp_free_pages -= desc->bd_iov_count;
574
575         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
576         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
577
578         for (i = 0; i < desc->bd_iov_count; i++) {
579                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
580                 desc->bd_enc_iov[i].kiov_page =
581                                         page_pools.epp_pools[p_idx][g_idx];
582                 page_pools.epp_pools[p_idx][g_idx] = NULL;
583
584                 if (++g_idx == PAGES_PER_POOL) {
585                         p_idx++;
586                         g_idx = 0;
587                 }
588         }
589
590         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
591                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
592
593         /*
594          * new idle index = (old * weight + new) / (weight + 1)
595          */
596         if (this_idle == -1) {
597                 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
598                             page_pools.epp_total_pages;
599         }
600         page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
601                                    this_idle) /
602                                   (IDLE_IDX_WEIGHT + 1);
603
604         page_pools.epp_last_access = cfs_time_current_sec();
605
606         cfs_spin_unlock(&page_pools.epp_lock);
607         return 0;
608 }
609 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
610
611 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
612 {
613         int     p_idx, g_idx;
614         int     i;
615
616         if (desc->bd_enc_iov == NULL)
617                 return;
618
619         LASSERT(desc->bd_iov_count > 0);
620
621         cfs_spin_lock(&page_pools.epp_lock);
622
623         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
624         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
625
626         LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
627                 page_pools.epp_total_pages);
628         LASSERT(page_pools.epp_pools[p_idx]);
629
630         for (i = 0; i < desc->bd_iov_count; i++) {
631                 LASSERT(desc->bd_enc_iov[i].kiov_page != NULL);
632                 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
633                 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
634
635                 page_pools.epp_pools[p_idx][g_idx] =
636                                         desc->bd_enc_iov[i].kiov_page;
637
638                 if (++g_idx == PAGES_PER_POOL) {
639                         p_idx++;
640                         g_idx = 0;
641                 }
642         }
643
644         page_pools.epp_free_pages += desc->bd_iov_count;
645
646         enc_pools_wakeup();
647
648         cfs_spin_unlock(&page_pools.epp_lock);
649
650         OBD_FREE(desc->bd_enc_iov,
651                  desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
652         desc->bd_enc_iov = NULL;
653 }
654 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
655
656 /*
657  * we don't do much stuff for add_user/del_user anymore, except adding some
658  * initial pages in add_user() if current pools are empty, rest would be
659  * handled by the pools's self-adaption.
660  */
661 int sptlrpc_enc_pool_add_user(void)
662 {
663         int     need_grow = 0;
664
665         cfs_spin_lock(&page_pools.epp_lock);
666         if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
667                 page_pools.epp_growing = 1;
668                 need_grow = 1;
669         }
670         cfs_spin_unlock(&page_pools.epp_lock);
671
672         if (need_grow) {
673                 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
674                                     PTLRPC_MAX_BRW_PAGES);
675
676                 cfs_spin_lock(&page_pools.epp_lock);
677                 page_pools.epp_growing = 0;
678                 enc_pools_wakeup();
679                 cfs_spin_unlock(&page_pools.epp_lock);
680         }
681         return 0;
682 }
683 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
684
685 int sptlrpc_enc_pool_del_user(void)
686 {
687         return 0;
688 }
689 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
690
691 static inline void enc_pools_alloc(void)
692 {
693         LASSERT(page_pools.epp_max_pools);
694         OBD_ALLOC_LARGE(page_pools.epp_pools,
695                         page_pools.epp_max_pools *
696                         sizeof(*page_pools.epp_pools));
697 }
698
699 static inline void enc_pools_free(void)
700 {
701         LASSERT(page_pools.epp_max_pools);
702         LASSERT(page_pools.epp_pools);
703
704         OBD_FREE_LARGE(page_pools.epp_pools,
705                        page_pools.epp_max_pools *
706                        sizeof(*page_pools.epp_pools));
707 }
708
709 int sptlrpc_enc_pool_init(void)
710 {
711         /*
712          * maximum capacity is 1/8 of total physical memory.
713          * is the 1/8 a good number?
714          */
715         page_pools.epp_max_pages = cfs_num_physpages / 8;
716         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
717
718         cfs_waitq_init(&page_pools.epp_waitq);
719         page_pools.epp_waitqlen = 0;
720         page_pools.epp_pages_short = 0;
721
722         page_pools.epp_growing = 0;
723
724         page_pools.epp_idle_idx = 0;
725         page_pools.epp_last_shrink = cfs_time_current_sec();
726         page_pools.epp_last_access = cfs_time_current_sec();
727
728         cfs_spin_lock_init(&page_pools.epp_lock);
729         page_pools.epp_total_pages = 0;
730         page_pools.epp_free_pages = 0;
731
732         page_pools.epp_st_max_pages = 0;
733         page_pools.epp_st_grows = 0;
734         page_pools.epp_st_grow_fails = 0;
735         page_pools.epp_st_shrinks = 0;
736         page_pools.epp_st_access = 0;
737         page_pools.epp_st_missings = 0;
738         page_pools.epp_st_lowfree = 0;
739         page_pools.epp_st_max_wqlen = 0;
740         page_pools.epp_st_max_wait = 0;
741
742         enc_pools_alloc();
743         if (page_pools.epp_pools == NULL)
744                 return -ENOMEM;
745
746         pools_shrinker = cfs_set_shrinker(pools_shrinker_seeks,
747                                           enc_pools_shrink);
748         if (pools_shrinker == NULL) {
749                 enc_pools_free();
750                 return -ENOMEM;
751         }
752
753         return 0;
754 }
755
756 void sptlrpc_enc_pool_fini(void)
757 {
758         unsigned long cleaned, npools;
759
760         LASSERT(pools_shrinker);
761         LASSERT(page_pools.epp_pools);
762         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
763
764         cfs_remove_shrinker(pools_shrinker);
765
766         npools = npages_to_npools(page_pools.epp_total_pages);
767         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
768         LASSERT(cleaned == page_pools.epp_total_pages);
769
770         enc_pools_free();
771
772         if (page_pools.epp_st_access > 0) {
773                 CWARN("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, CFS_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 /****************************************
806  * Helpers to assist policy modules to  *
807  * implement checksum funcationality    *
808  ****************************************/
809
810 static struct sptlrpc_hash_type hash_types[] = {
811         [BULK_HASH_ALG_NULL]    = { "null",     "null",         0 },
812         [BULK_HASH_ALG_ADLER32] = { "adler32",  "adler32",      4 },
813         [BULK_HASH_ALG_CRC32]   = { "crc32",    "crc32",        4 },
814         [BULK_HASH_ALG_MD5]     = { "md5",      "md5",          16 },
815         [BULK_HASH_ALG_SHA1]    = { "sha1",     "sha1",         20 },
816         [BULK_HASH_ALG_SHA256]  = { "sha256",   "sha256",       32 },
817         [BULK_HASH_ALG_SHA384]  = { "sha384",   "sha384",       48 },
818         [BULK_HASH_ALG_SHA512]  = { "sha512",   "sha512",       64 },
819 };
820
821 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg)
822 {
823         struct sptlrpc_hash_type *ht;
824
825         if (hash_alg < BULK_HASH_ALG_MAX) {
826                 ht = &hash_types[hash_alg];
827                 if (ht->sht_tfm_name)
828                         return ht;
829         }
830         return NULL;
831 }
832 EXPORT_SYMBOL(sptlrpc_get_hash_type);
833
834 const char * sptlrpc_get_hash_name(__u8 hash_alg)
835 {
836         const struct sptlrpc_hash_type *ht;
837
838         ht = sptlrpc_get_hash_type(hash_alg);
839         if (ht)
840                 return ht->sht_name;
841         else
842                 return "unknown";
843 }
844 EXPORT_SYMBOL(sptlrpc_get_hash_name);
845
846 __u8 sptlrpc_get_hash_alg(const char *algname)
847 {
848         int     i;
849
850         for (i = 0; i < BULK_HASH_ALG_MAX; i++)
851                 if (!strcmp(hash_types[i].sht_name, algname))
852                         break;
853         return i;
854 }
855 EXPORT_SYMBOL(sptlrpc_get_hash_alg);
856
857 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
858 {
859         struct ptlrpc_bulk_sec_desc *bsd;
860         int                          size = msg->lm_buflens[offset];
861
862         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
863         if (bsd == NULL) {
864                 CERROR("Invalid bulk sec desc: size %d\n", size);
865                 return -EINVAL;
866         }
867
868         if (swabbed) {
869                 __swab32s(&bsd->bsd_nob);
870         }
871
872         if (unlikely(bsd->bsd_version != 0)) {
873                 CERROR("Unexpected version %u\n", bsd->bsd_version);
874                 return -EPROTO;
875         }
876
877         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
878                 CERROR("Invalid type %u\n", bsd->bsd_type);
879                 return -EPROTO;
880         }
881
882         /* FIXME more sanity check here */
883
884         if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
885                      bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
886                      bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
887                 CERROR("Invalid svc %u\n", bsd->bsd_svc);
888                 return -EPROTO;
889         }
890
891         return 0;
892 }
893 EXPORT_SYMBOL(bulk_sec_desc_unpack);
894
895 #ifdef __KERNEL__
896
897 #ifdef HAVE_ADLER
898 static int do_bulk_checksum_adler32(struct ptlrpc_bulk_desc *desc, void *buf)
899 {
900         struct page    *page;
901         int             off;
902         char           *ptr;
903         __u32           adler32 = 1;
904         int             len, i;
905
906         for (i = 0; i < desc->bd_iov_count; i++) {
907                 page = desc->bd_iov[i].kiov_page;
908                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
909                 ptr = cfs_kmap(page) + off;
910                 len = desc->bd_iov[i].kiov_len;
911
912                 adler32 = adler32(adler32, ptr, len);
913
914                 cfs_kunmap(page);
915         }
916
917         adler32 = cpu_to_le32(adler32);
918         memcpy(buf, &adler32, sizeof(adler32));
919         return 0;
920 }
921 #endif
922
923 static int do_bulk_checksum_crc32(struct ptlrpc_bulk_desc *desc, void *buf)
924 {
925         struct page    *page;
926         int             off;
927         char           *ptr;
928         __u32           crc32 = ~0;
929         int             len, i;
930
931         for (i = 0; i < desc->bd_iov_count; i++) {
932                 page = desc->bd_iov[i].kiov_page;
933                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
934                 ptr = cfs_kmap(page) + off;
935                 len = desc->bd_iov[i].kiov_len;
936
937                 crc32 = crc32_le(crc32, ptr, len);
938
939                 cfs_kunmap(page);
940         }
941
942         crc32 = cpu_to_le32(crc32);
943         memcpy(buf, &crc32, sizeof(crc32));
944         return 0;
945 }
946
947 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
948                               void *buf, int buflen)
949 {
950         struct hash_desc    hdesc;
951         int                 hashsize;
952         char                hashbuf[64];
953         struct scatterlist  sl;
954         int                 i;
955
956         LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
957         LASSERT(buflen >= 4);
958
959         switch (alg) {
960         case BULK_HASH_ALG_ADLER32:
961 #ifdef HAVE_ADLER
962                 return do_bulk_checksum_adler32(desc, buf);
963 #else
964                 CERROR("Adler32 not supported\n");
965                 return -EINVAL;
966 #endif
967         case BULK_HASH_ALG_CRC32:
968                 return do_bulk_checksum_crc32(desc, buf);
969         }
970
971         hdesc.tfm = ll_crypto_alloc_hash(hash_types[alg].sht_tfm_name, 0, 0);
972         if (hdesc.tfm == NULL) {
973                 CERROR("Unable to allocate TFM %s\n", hash_types[alg].sht_name);
974                 return -ENOMEM;
975         }
976
977         hdesc.flags = 0;
978         ll_crypto_hash_init(&hdesc);
979
980         hashsize = ll_crypto_hash_digestsize(hdesc.tfm);
981
982         for (i = 0; i < desc->bd_iov_count; i++) {
983                 sg_set_page(&sl, desc->bd_iov[i].kiov_page,
984                              desc->bd_iov[i].kiov_len,
985                              desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK);
986                 ll_crypto_hash_update(&hdesc, &sl, sl.length);
987         }
988
989         if (hashsize > buflen) {
990                 ll_crypto_hash_final(&hdesc, hashbuf);
991                 memcpy(buf, hashbuf, buflen);
992         } else {
993                 ll_crypto_hash_final(&hdesc, buf);
994         }
995
996         ll_crypto_free_hash(hdesc.tfm);
997         return 0;
998 }
999 EXPORT_SYMBOL(sptlrpc_get_bulk_checksum);
1000
1001 #else /* !__KERNEL__ */
1002
1003 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
1004                               void *buf, int buflen)
1005 {
1006         __u32   csum32;
1007         int     i;
1008
1009         LASSERT(alg == BULK_HASH_ALG_ADLER32 || alg == BULK_HASH_ALG_CRC32);
1010
1011         if (alg == BULK_HASH_ALG_ADLER32)
1012                 csum32 = 1;
1013         else
1014                 csum32 = ~0;
1015
1016         for (i = 0; i < desc->bd_iov_count; i++) {
1017                 unsigned char *ptr = desc->bd_iov[i].iov_base;
1018                 int len = desc->bd_iov[i].iov_len;
1019
1020                 switch (alg) {
1021                 case BULK_HASH_ALG_ADLER32:
1022 #ifdef HAVE_ADLER
1023                         csum32 = adler32(csum32, ptr, len);
1024 #else
1025                         CERROR("Adler32 not supported\n");
1026                         return -EINVAL;
1027 #endif
1028                         break;
1029                 case BULK_HASH_ALG_CRC32:
1030                         csum32 = crc32_le(csum32, ptr, len);
1031                         break;
1032                 }
1033         }
1034
1035         csum32 = cpu_to_le32(csum32);
1036         memcpy(buf, &csum32, sizeof(csum32));
1037         return 0;
1038 }
1039
1040 #endif /* __KERNEL__ */