Whamcloud - gitweb
a42dacb3e3cf5eb595be339bbf08b4157585de2a
[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 KERN_SHRINKER(enc_pools_shrink)
243 {
244         if (unlikely(nr_to_scan != 0)) {
245                 cfs_spin_lock(&page_pools.epp_lock);
246                 nr_to_scan = min(nr_to_scan, (int) page_pools.epp_free_pages -
247                                              PTLRPC_MAX_BRW_PAGES);
248                 if (nr_to_scan > 0) {
249                         enc_pools_release_free_pages(nr_to_scan);
250                         CDEBUG(D_SEC, "released %d pages, %ld left\n",
251                                nr_to_scan, page_pools.epp_free_pages);
252
253                         page_pools.epp_st_shrinks++;
254                         page_pools.epp_last_shrink = cfs_time_current_sec();
255                 }
256                 cfs_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                 cfs_spin_lock(&page_pools.epp_lock);
266                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
267                 cfs_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(cfs_page_t ***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                                         cfs_free_page(pools[i][j]);
294                                         cleaned++;
295                                 }
296                         }
297                         OBD_FREE(pools[i], CFS_PAGE_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(cfs_page_t ***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         cfs_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         cfs_spin_unlock(&page_pools.epp_lock);
391 }
392
393 static int enc_pools_add_pages(int npages)
394 {
395         static CFS_DECLARE_MUTEX(sem_add_pages);
396         cfs_page_t   ***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         cfs_down(&sem_add_pages);
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], CFS_PAGE_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] = cfs_alloc_page(CFS_ALLOC_IO |
423                                                      CFS_ALLOC_HIGH);
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         cfs_up(&sem_add_pages);
446         return rc;
447 }
448
449 static inline void enc_pools_wakeup(void)
450 {
451         LASSERT_SPIN_LOCKED(&page_pools.epp_lock);
452         LASSERT(page_pools.epp_waitqlen >= 0);
453
454         if (unlikely(page_pools.epp_waitqlen)) {
455                 LASSERT(cfs_waitq_active(&page_pools.epp_waitq));
456                 cfs_waitq_broadcast(&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         cfs_waitlink_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         cfs_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                         cfs_spin_unlock(&page_pools.epp_lock);
533                         enc_pools_add_pages(page_pools.epp_pages_short / 2);
534                         cfs_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                         cfs_set_current_state(CFS_TASK_UNINT);
546                         cfs_waitlink_init(&waitlink);
547                         cfs_waitq_add(&page_pools.epp_waitq, &waitlink);
548
549                         cfs_spin_unlock(&page_pools.epp_lock);
550                         cfs_waitq_wait(&waitlink, CFS_TASK_UNINT);
551                         cfs_waitq_del(&page_pools.epp_waitq, &waitlink);
552                         LASSERT(page_pools.epp_waitqlen > 0);
553                         cfs_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         cfs_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         cfs_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         cfs_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         cfs_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         cfs_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                 cfs_spin_lock(&page_pools.epp_lock);
676                 page_pools.epp_growing = 0;
677                 enc_pools_wakeup();
678                 cfs_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         /*
694          * on system with huge memory but small page size, this might lead to
695          * high-order allocation. but it's not common, and we suppose memory
696          * be not too much fragmented at module loading time.
697          */
698         OBD_ALLOC(page_pools.epp_pools,
699                   page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
700 }
701
702 static inline void enc_pools_free(void)
703 {
704         LASSERT(page_pools.epp_max_pools);
705         LASSERT(page_pools.epp_pools);
706
707         OBD_FREE(page_pools.epp_pools,
708                  page_pools.epp_max_pools * sizeof(*page_pools.epp_pools));
709 }
710
711 int sptlrpc_enc_pool_init(void)
712 {
713         /*
714          * maximum capacity is 1/8 of total physical memory.
715          * is the 1/8 a good number?
716          */
717         page_pools.epp_max_pages = cfs_num_physpages / 8;
718         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
719
720         cfs_waitq_init(&page_pools.epp_waitq);
721         page_pools.epp_waitqlen = 0;
722         page_pools.epp_pages_short = 0;
723
724         page_pools.epp_growing = 0;
725
726         page_pools.epp_idle_idx = 0;
727         page_pools.epp_last_shrink = cfs_time_current_sec();
728         page_pools.epp_last_access = cfs_time_current_sec();
729
730         cfs_spin_lock_init(&page_pools.epp_lock);
731         page_pools.epp_total_pages = 0;
732         page_pools.epp_free_pages = 0;
733
734         page_pools.epp_st_max_pages = 0;
735         page_pools.epp_st_grows = 0;
736         page_pools.epp_st_grow_fails = 0;
737         page_pools.epp_st_shrinks = 0;
738         page_pools.epp_st_access = 0;
739         page_pools.epp_st_missings = 0;
740         page_pools.epp_st_lowfree = 0;
741         page_pools.epp_st_max_wqlen = 0;
742         page_pools.epp_st_max_wait = 0;
743
744         enc_pools_alloc();
745         if (page_pools.epp_pools == NULL)
746                 return -ENOMEM;
747
748         pools_shrinker = cfs_set_shrinker(pools_shrinker_seeks,
749                                           enc_pools_shrink);
750         if (pools_shrinker == NULL) {
751                 enc_pools_free();
752                 return -ENOMEM;
753         }
754
755         return 0;
756 }
757
758 void sptlrpc_enc_pool_fini(void)
759 {
760         unsigned long cleaned, npools;
761
762         LASSERT(pools_shrinker);
763         LASSERT(page_pools.epp_pools);
764         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
765
766         cfs_remove_shrinker(pools_shrinker);
767
768         npools = npages_to_npools(page_pools.epp_total_pages);
769         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
770         LASSERT(cleaned == page_pools.epp_total_pages);
771
772         enc_pools_free();
773
774         if (page_pools.epp_st_access > 0) {
775                 CWARN("max pages %lu, grows %u, grow fails %u, shrinks %u, "
776                       "access %lu, missing %lu, max qlen %u, max wait "
777                       CFS_TIME_T"/%d\n",
778                       page_pools.epp_st_max_pages, page_pools.epp_st_grows,
779                       page_pools.epp_st_grow_fails,
780                       page_pools.epp_st_shrinks, page_pools.epp_st_access,
781                       page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
782                       page_pools.epp_st_max_wait, CFS_HZ);
783         }
784 }
785
786 #else /* !__KERNEL__ */
787
788 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
789 {
790         return 0;
791 }
792
793 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
794 {
795 }
796
797 int sptlrpc_enc_pool_init(void)
798 {
799         return 0;
800 }
801
802 void sptlrpc_enc_pool_fini(void)
803 {
804 }
805 #endif
806
807 /****************************************
808  * Helpers to assist policy modules to  *
809  * implement checksum funcationality    *
810  ****************************************/
811
812 static struct sptlrpc_hash_type hash_types[] = {
813         [BULK_HASH_ALG_NULL]    = { "null",     "null",         0 },
814         [BULK_HASH_ALG_ADLER32] = { "adler32",  "adler32",      4 },
815         [BULK_HASH_ALG_CRC32]   = { "crc32",    "crc32",        4 },
816         [BULK_HASH_ALG_MD5]     = { "md5",      "md5",          16 },
817         [BULK_HASH_ALG_SHA1]    = { "sha1",     "sha1",         20 },
818         [BULK_HASH_ALG_SHA256]  = { "sha256",   "sha256",       32 },
819         [BULK_HASH_ALG_SHA384]  = { "sha384",   "sha384",       48 },
820         [BULK_HASH_ALG_SHA512]  = { "sha512",   "sha512",       64 },
821 };
822
823 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg)
824 {
825         struct sptlrpc_hash_type *ht;
826
827         if (hash_alg < BULK_HASH_ALG_MAX) {
828                 ht = &hash_types[hash_alg];
829                 if (ht->sht_tfm_name)
830                         return ht;
831         }
832         return NULL;
833 }
834 EXPORT_SYMBOL(sptlrpc_get_hash_type);
835
836 const char * sptlrpc_get_hash_name(__u8 hash_alg)
837 {
838         const struct sptlrpc_hash_type *ht;
839
840         ht = sptlrpc_get_hash_type(hash_alg);
841         if (ht)
842                 return ht->sht_name;
843         else
844                 return "unknown";
845 }
846 EXPORT_SYMBOL(sptlrpc_get_hash_name);
847
848 __u8 sptlrpc_get_hash_alg(const char *algname)
849 {
850         int     i;
851
852         for (i = 0; i < BULK_HASH_ALG_MAX; i++)
853                 if (!strcmp(hash_types[i].sht_name, algname))
854                         break;
855         return i;
856 }
857 EXPORT_SYMBOL(sptlrpc_get_hash_alg);
858
859 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
860 {
861         struct ptlrpc_bulk_sec_desc *bsd;
862         int                          size = msg->lm_buflens[offset];
863
864         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
865         if (bsd == NULL) {
866                 CERROR("Invalid bulk sec desc: size %d\n", size);
867                 return -EINVAL;
868         }
869
870         if (swabbed) {
871                 __swab32s(&bsd->bsd_nob);
872         }
873
874         if (unlikely(bsd->bsd_version != 0)) {
875                 CERROR("Unexpected version %u\n", bsd->bsd_version);
876                 return -EPROTO;
877         }
878
879         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
880                 CERROR("Invalid type %u\n", bsd->bsd_type);
881                 return -EPROTO;
882         }
883
884         /* FIXME more sanity check here */
885
886         if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
887                      bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
888                      bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
889                 CERROR("Invalid svc %u\n", bsd->bsd_svc);
890                 return -EPROTO;
891         }
892
893         return 0;
894 }
895 EXPORT_SYMBOL(bulk_sec_desc_unpack);
896
897 #ifdef __KERNEL__
898
899 #ifdef HAVE_ADLER
900 static int do_bulk_checksum_adler32(struct ptlrpc_bulk_desc *desc, void *buf)
901 {
902         struct page    *page;
903         int             off;
904         char           *ptr;
905         __u32           adler32 = 1;
906         int             len, i;
907
908         for (i = 0; i < desc->bd_iov_count; i++) {
909                 page = desc->bd_iov[i].kiov_page;
910                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
911                 ptr = cfs_kmap(page) + off;
912                 len = desc->bd_iov[i].kiov_len;
913
914                 adler32 = adler32(adler32, ptr, len);
915
916                 cfs_kunmap(page);
917         }
918
919         adler32 = cpu_to_le32(adler32);
920         memcpy(buf, &adler32, sizeof(adler32));
921         return 0;
922 }
923 #endif
924
925 static int do_bulk_checksum_crc32(struct ptlrpc_bulk_desc *desc, void *buf)
926 {
927         struct page    *page;
928         int             off;
929         char           *ptr;
930         __u32           crc32 = ~0;
931         int             len, i;
932
933         for (i = 0; i < desc->bd_iov_count; i++) {
934                 page = desc->bd_iov[i].kiov_page;
935                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
936                 ptr = cfs_kmap(page) + off;
937                 len = desc->bd_iov[i].kiov_len;
938
939                 crc32 = crc32_le(crc32, ptr, len);
940
941                 cfs_kunmap(page);
942         }
943
944         crc32 = cpu_to_le32(crc32);
945         memcpy(buf, &crc32, sizeof(crc32));
946         return 0;
947 }
948
949 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
950                               void *buf, int buflen)
951 {
952         struct hash_desc    hdesc;
953         int                 hashsize;
954         char                hashbuf[64];
955         struct scatterlist  sl;
956         int                 i;
957
958         LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
959         LASSERT(buflen >= 4);
960
961         switch (alg) {
962         case BULK_HASH_ALG_ADLER32:
963 #ifdef HAVE_ADLER
964                 return do_bulk_checksum_adler32(desc, buf);
965 #else
966                 CERROR("Adler32 not supported\n");
967                 return -EINVAL;
968 #endif
969         case BULK_HASH_ALG_CRC32:
970                 return do_bulk_checksum_crc32(desc, buf);
971         }
972
973         hdesc.tfm = ll_crypto_alloc_hash(hash_types[alg].sht_tfm_name, 0, 0);
974         if (hdesc.tfm == NULL) {
975                 CERROR("Unable to allocate TFM %s\n", hash_types[alg].sht_name);
976                 return -ENOMEM;
977         }
978
979         hdesc.flags = 0;
980         ll_crypto_hash_init(&hdesc);
981
982         hashsize = ll_crypto_hash_digestsize(hdesc.tfm);
983
984         for (i = 0; i < desc->bd_iov_count; i++) {
985                 sg_set_page(&sl, desc->bd_iov[i].kiov_page,
986                              desc->bd_iov[i].kiov_len,
987                              desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK);
988                 ll_crypto_hash_update(&hdesc, &sl, sl.length);
989         }
990
991         if (hashsize > buflen) {
992                 ll_crypto_hash_final(&hdesc, hashbuf);
993                 memcpy(buf, hashbuf, buflen);
994         } else {
995                 ll_crypto_hash_final(&hdesc, buf);
996         }
997
998         ll_crypto_free_hash(hdesc.tfm);
999         return 0;
1000 }
1001 EXPORT_SYMBOL(sptlrpc_get_bulk_checksum);
1002
1003 #else /* !__KERNEL__ */
1004
1005 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
1006                               void *buf, int buflen)
1007 {
1008         __u32   csum32;
1009         int     i;
1010
1011         LASSERT(alg == BULK_HASH_ALG_ADLER32 || alg == BULK_HASH_ALG_CRC32);
1012
1013         if (alg == BULK_HASH_ALG_ADLER32)
1014                 csum32 = 1;
1015         else
1016                 csum32 = ~0;
1017
1018         for (i = 0; i < desc->bd_iov_count; i++) {
1019                 unsigned char *ptr = desc->bd_iov[i].iov_base;
1020                 int len = desc->bd_iov[i].iov_len;
1021
1022                 switch (alg) {
1023                 case BULK_HASH_ALG_ADLER32:
1024 #ifdef HAVE_ADLER
1025                         csum32 = adler32(csum32, ptr, len);
1026 #else
1027                         CERROR("Adler32 not supported\n");
1028                         return -EINVAL;
1029 #endif
1030                         break;
1031                 case BULK_HASH_ALG_CRC32:
1032                         csum32 = crc32_le(csum32, ptr, len);
1033                         break;
1034                 }
1035         }
1036
1037         csum32 = cpu_to_le32(csum32);
1038         memcpy(buf, &csum32, sizeof(csum32));
1039         return 0;
1040 }
1041
1042 #endif /* __KERNEL__ */