Whamcloud - gitweb
LU-506 FC15: update shrinker to use shrink_control callback
[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_ARGS(sc, nr_to_scan, gfp_mask))
243 {
244         if (unlikely(shrink_param(sc, nr_to_scan) != 0)) {
245                 cfs_spin_lock(&page_pools.epp_lock);
246                 shrink_param(sc, nr_to_scan) = min_t(unsigned long,
247                                                    shrink_param(sc, nr_to_scan),
248                                                    page_pools.epp_free_pages -
249                                                    PTLRPC_MAX_BRW_PAGES);
250                 if (shrink_param(sc, nr_to_scan) > 0) {
251                         enc_pools_release_free_pages(shrink_param(sc,
252                                                                   nr_to_scan));
253                         CDEBUG(D_SEC, "released %ld pages, %ld left\n",
254                                (long)shrink_param(sc, nr_to_scan),
255                                page_pools.epp_free_pages);
256
257                         page_pools.epp_st_shrinks++;
258                         page_pools.epp_last_shrink = cfs_time_current_sec();
259                 }
260                 cfs_spin_unlock(&page_pools.epp_lock);
261         }
262
263         /*
264          * if no pool access for a long time, we consider it's fully idle.
265          * a little race here is fine.
266          */
267         if (unlikely(cfs_time_current_sec() - page_pools.epp_last_access >
268                      CACHE_QUIESCENT_PERIOD)) {
269                 cfs_spin_lock(&page_pools.epp_lock);
270                 page_pools.epp_idle_idx = IDLE_IDX_MAX;
271                 cfs_spin_unlock(&page_pools.epp_lock);
272         }
273
274         LASSERT(page_pools.epp_idle_idx <= IDLE_IDX_MAX);
275         return max((int) page_pools.epp_free_pages - PTLRPC_MAX_BRW_PAGES, 0) *
276                (IDLE_IDX_MAX - page_pools.epp_idle_idx) / IDLE_IDX_MAX;
277 }
278
279 static inline
280 int npages_to_npools(unsigned long npages)
281 {
282         return (int) ((npages + PAGES_PER_POOL - 1) / PAGES_PER_POOL);
283 }
284
285 /*
286  * return how many pages cleaned up.
287  */
288 static unsigned long enc_pools_cleanup(cfs_page_t ***pools, int npools)
289 {
290         unsigned long cleaned = 0;
291         int           i, j;
292
293         for (i = 0; i < npools; i++) {
294                 if (pools[i]) {
295                         for (j = 0; j < PAGES_PER_POOL; j++) {
296                                 if (pools[i][j]) {
297                                         cfs_free_page(pools[i][j]);
298                                         cleaned++;
299                                 }
300                         }
301                         OBD_FREE(pools[i], CFS_PAGE_SIZE);
302                         pools[i] = NULL;
303                 }
304         }
305
306         return cleaned;
307 }
308
309 /*
310  * merge @npools pointed by @pools which contains @npages new pages
311  * into current pools.
312  *
313  * we have options to avoid most memory copy with some tricks. but we choose
314  * the simplest way to avoid complexity. It's not frequently called.
315  */
316 static void enc_pools_insert(cfs_page_t ***pools, int npools, int npages)
317 {
318         int     freeslot;
319         int     op_idx, np_idx, og_idx, ng_idx;
320         int     cur_npools, end_npools;
321
322         LASSERT(npages > 0);
323         LASSERT(page_pools.epp_total_pages+npages <= page_pools.epp_max_pages);
324         LASSERT(npages_to_npools(npages) == npools);
325         LASSERT(page_pools.epp_growing);
326
327         cfs_spin_lock(&page_pools.epp_lock);
328
329         /*
330          * (1) fill all the free slots of current pools.
331          */
332         /* free slots are those left by rent pages, and the extra ones with
333          * index >= total_pages, locate at the tail of last pool. */
334         freeslot = page_pools.epp_total_pages % PAGES_PER_POOL;
335         if (freeslot != 0)
336                 freeslot = PAGES_PER_POOL - freeslot;
337         freeslot += page_pools.epp_total_pages - page_pools.epp_free_pages;
338
339         op_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
340         og_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
341         np_idx = npools - 1;
342         ng_idx = (npages - 1) % PAGES_PER_POOL;
343
344         while (freeslot) {
345                 LASSERT(page_pools.epp_pools[op_idx][og_idx] == NULL);
346                 LASSERT(pools[np_idx][ng_idx] != NULL);
347
348                 page_pools.epp_pools[op_idx][og_idx] = pools[np_idx][ng_idx];
349                 pools[np_idx][ng_idx] = NULL;
350
351                 freeslot--;
352
353                 if (++og_idx == PAGES_PER_POOL) {
354                         op_idx++;
355                         og_idx = 0;
356                 }
357                 if (--ng_idx < 0) {
358                         if (np_idx == 0)
359                                 break;
360                         np_idx--;
361                         ng_idx = PAGES_PER_POOL - 1;
362                 }
363         }
364
365         /*
366          * (2) add pools if needed.
367          */
368         cur_npools = (page_pools.epp_total_pages + PAGES_PER_POOL - 1) /
369                      PAGES_PER_POOL;
370         end_npools = (page_pools.epp_total_pages + npages + PAGES_PER_POOL -1) /
371                      PAGES_PER_POOL;
372         LASSERT(end_npools <= page_pools.epp_max_pools);
373
374         np_idx = 0;
375         while (cur_npools < end_npools) {
376                 LASSERT(page_pools.epp_pools[cur_npools] == NULL);
377                 LASSERT(np_idx < npools);
378                 LASSERT(pools[np_idx] != NULL);
379
380                 page_pools.epp_pools[cur_npools++] = pools[np_idx];
381                 pools[np_idx++] = NULL;
382         }
383
384         page_pools.epp_total_pages += npages;
385         page_pools.epp_free_pages += npages;
386         page_pools.epp_st_lowfree = page_pools.epp_free_pages;
387
388         if (page_pools.epp_total_pages > page_pools.epp_st_max_pages)
389                 page_pools.epp_st_max_pages = page_pools.epp_total_pages;
390
391         CDEBUG(D_SEC, "add %d pages to total %lu\n", npages,
392                page_pools.epp_total_pages);
393
394         cfs_spin_unlock(&page_pools.epp_lock);
395 }
396
397 static int enc_pools_add_pages(int npages)
398 {
399         static CFS_DECLARE_MUTEX(sem_add_pages);
400         cfs_page_t   ***pools;
401         int             npools, alloced = 0;
402         int             i, j, rc = -ENOMEM;
403
404         if (npages < PTLRPC_MAX_BRW_PAGES)
405                 npages = PTLRPC_MAX_BRW_PAGES;
406
407         cfs_down(&sem_add_pages);
408
409         if (npages + page_pools.epp_total_pages > page_pools.epp_max_pages)
410                 npages = page_pools.epp_max_pages - page_pools.epp_total_pages;
411         LASSERT(npages > 0);
412
413         page_pools.epp_st_grows++;
414
415         npools = npages_to_npools(npages);
416         OBD_ALLOC(pools, npools * sizeof(*pools));
417         if (pools == NULL)
418                 goto out;
419
420         for (i = 0; i < npools; i++) {
421                 OBD_ALLOC(pools[i], CFS_PAGE_SIZE);
422                 if (pools[i] == NULL)
423                         goto out_pools;
424
425                 for (j = 0; j < PAGES_PER_POOL && alloced < npages; j++) {
426                         pools[i][j] = cfs_alloc_page(CFS_ALLOC_IO |
427                                                      CFS_ALLOC_HIGH);
428                         if (pools[i][j] == NULL)
429                                 goto out_pools;
430
431                         alloced++;
432                 }
433         }
434         LASSERT(alloced == npages);
435
436         enc_pools_insert(pools, npools, npages);
437         CDEBUG(D_SEC, "added %d pages into pools\n", npages);
438         rc = 0;
439
440 out_pools:
441         enc_pools_cleanup(pools, npools);
442         OBD_FREE(pools, npools * sizeof(*pools));
443 out:
444         if (rc) {
445                 page_pools.epp_st_grow_fails++;
446                 CERROR("Failed to allocate %d enc pages\n", npages);
447         }
448
449         cfs_up(&sem_add_pages);
450         return rc;
451 }
452
453 static inline void enc_pools_wakeup(void)
454 {
455         LASSERT_SPIN_LOCKED(&page_pools.epp_lock);
456         LASSERT(page_pools.epp_waitqlen >= 0);
457
458         if (unlikely(page_pools.epp_waitqlen)) {
459                 LASSERT(cfs_waitq_active(&page_pools.epp_waitq));
460                 cfs_waitq_broadcast(&page_pools.epp_waitq);
461         }
462 }
463
464 static int enc_pools_should_grow(int page_needed, long now)
465 {
466         /* don't grow if someone else is growing the pools right now,
467          * or the pools has reached its full capacity
468          */
469         if (page_pools.epp_growing ||
470             page_pools.epp_total_pages == page_pools.epp_max_pages)
471                 return 0;
472
473         /* if total pages is not enough, we need to grow */
474         if (page_pools.epp_total_pages < page_needed)
475                 return 1;
476
477         /*
478          * we wanted to return 0 here if there was a shrink just happened
479          * moment ago, but this may cause deadlock if both client and ost
480          * live on single node.
481          */
482 #if 0
483         if (now - page_pools.epp_last_shrink < 2)
484                 return 0;
485 #endif
486
487         /*
488          * here we perhaps need consider other factors like wait queue
489          * length, idle index, etc. ?
490          */
491
492         /* grow the pools in any other cases */
493         return 1;
494 }
495
496 /*
497  * we allocate the requested pages atomically.
498  */
499 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
500 {
501         cfs_waitlink_t  waitlink;
502         unsigned long   this_idle = -1;
503         cfs_time_t      tick = 0;
504         long            now;
505         int             p_idx, g_idx;
506         int             i;
507
508         LASSERT(desc->bd_iov_count > 0);
509         LASSERT(desc->bd_iov_count <= page_pools.epp_max_pages);
510
511         /* resent bulk, enc iov might have been allocated previously */
512         if (desc->bd_enc_iov != NULL)
513                 return 0;
514
515         OBD_ALLOC(desc->bd_enc_iov,
516                   desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
517         if (desc->bd_enc_iov == NULL)
518                 return -ENOMEM;
519
520         cfs_spin_lock(&page_pools.epp_lock);
521
522         page_pools.epp_st_access++;
523 again:
524         if (unlikely(page_pools.epp_free_pages < desc->bd_iov_count)) {
525                 if (tick == 0)
526                         tick = cfs_time_current();
527
528                 now = cfs_time_current_sec();
529
530                 page_pools.epp_st_missings++;
531                 page_pools.epp_pages_short += desc->bd_iov_count;
532
533                 if (enc_pools_should_grow(desc->bd_iov_count, now)) {
534                         page_pools.epp_growing = 1;
535
536                         cfs_spin_unlock(&page_pools.epp_lock);
537                         enc_pools_add_pages(page_pools.epp_pages_short / 2);
538                         cfs_spin_lock(&page_pools.epp_lock);
539
540                         page_pools.epp_growing = 0;
541
542                         enc_pools_wakeup();
543                 } else {
544                         if (++page_pools.epp_waitqlen >
545                             page_pools.epp_st_max_wqlen)
546                                 page_pools.epp_st_max_wqlen =
547                                                 page_pools.epp_waitqlen;
548
549                         cfs_set_current_state(CFS_TASK_UNINT);
550                         cfs_waitlink_init(&waitlink);
551                         cfs_waitq_add(&page_pools.epp_waitq, &waitlink);
552
553                         cfs_spin_unlock(&page_pools.epp_lock);
554                         cfs_waitq_wait(&waitlink, CFS_TASK_UNINT);
555                         cfs_waitq_del(&page_pools.epp_waitq, &waitlink);
556                         LASSERT(page_pools.epp_waitqlen > 0);
557                         cfs_spin_lock(&page_pools.epp_lock);
558                         page_pools.epp_waitqlen--;
559                 }
560
561                 LASSERT(page_pools.epp_pages_short >= desc->bd_iov_count);
562                 page_pools.epp_pages_short -= desc->bd_iov_count;
563
564                 this_idle = 0;
565                 goto again;
566         }
567
568         /* record max wait time */
569         if (unlikely(tick != 0)) {
570                 tick = cfs_time_current() - tick;
571                 if (tick > page_pools.epp_st_max_wait)
572                         page_pools.epp_st_max_wait = tick;
573         }
574
575         /* proceed with rest of allocation */
576         page_pools.epp_free_pages -= desc->bd_iov_count;
577
578         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
579         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
580
581         for (i = 0; i < desc->bd_iov_count; i++) {
582                 LASSERT(page_pools.epp_pools[p_idx][g_idx] != NULL);
583                 desc->bd_enc_iov[i].kiov_page =
584                                         page_pools.epp_pools[p_idx][g_idx];
585                 page_pools.epp_pools[p_idx][g_idx] = NULL;
586
587                 if (++g_idx == PAGES_PER_POOL) {
588                         p_idx++;
589                         g_idx = 0;
590                 }
591         }
592
593         if (page_pools.epp_free_pages < page_pools.epp_st_lowfree)
594                 page_pools.epp_st_lowfree = page_pools.epp_free_pages;
595
596         /*
597          * new idle index = (old * weight + new) / (weight + 1)
598          */
599         if (this_idle == -1) {
600                 this_idle = page_pools.epp_free_pages * IDLE_IDX_MAX /
601                             page_pools.epp_total_pages;
602         }
603         page_pools.epp_idle_idx = (page_pools.epp_idle_idx * IDLE_IDX_WEIGHT +
604                                    this_idle) /
605                                   (IDLE_IDX_WEIGHT + 1);
606
607         page_pools.epp_last_access = cfs_time_current_sec();
608
609         cfs_spin_unlock(&page_pools.epp_lock);
610         return 0;
611 }
612 EXPORT_SYMBOL(sptlrpc_enc_pool_get_pages);
613
614 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
615 {
616         int     p_idx, g_idx;
617         int     i;
618
619         if (desc->bd_enc_iov == NULL)
620                 return;
621
622         LASSERT(desc->bd_iov_count > 0);
623
624         cfs_spin_lock(&page_pools.epp_lock);
625
626         p_idx = page_pools.epp_free_pages / PAGES_PER_POOL;
627         g_idx = page_pools.epp_free_pages % PAGES_PER_POOL;
628
629         LASSERT(page_pools.epp_free_pages + desc->bd_iov_count <=
630                 page_pools.epp_total_pages);
631         LASSERT(page_pools.epp_pools[p_idx]);
632
633         for (i = 0; i < desc->bd_iov_count; i++) {
634                 LASSERT(desc->bd_enc_iov[i].kiov_page != NULL);
635                 LASSERT(g_idx != 0 || page_pools.epp_pools[p_idx]);
636                 LASSERT(page_pools.epp_pools[p_idx][g_idx] == NULL);
637
638                 page_pools.epp_pools[p_idx][g_idx] =
639                                         desc->bd_enc_iov[i].kiov_page;
640
641                 if (++g_idx == PAGES_PER_POOL) {
642                         p_idx++;
643                         g_idx = 0;
644                 }
645         }
646
647         page_pools.epp_free_pages += desc->bd_iov_count;
648
649         enc_pools_wakeup();
650
651         cfs_spin_unlock(&page_pools.epp_lock);
652
653         OBD_FREE(desc->bd_enc_iov,
654                  desc->bd_iov_count * sizeof(*desc->bd_enc_iov));
655         desc->bd_enc_iov = NULL;
656 }
657 EXPORT_SYMBOL(sptlrpc_enc_pool_put_pages);
658
659 /*
660  * we don't do much stuff for add_user/del_user anymore, except adding some
661  * initial pages in add_user() if current pools are empty, rest would be
662  * handled by the pools's self-adaption.
663  */
664 int sptlrpc_enc_pool_add_user(void)
665 {
666         int     need_grow = 0;
667
668         cfs_spin_lock(&page_pools.epp_lock);
669         if (page_pools.epp_growing == 0 && page_pools.epp_total_pages == 0) {
670                 page_pools.epp_growing = 1;
671                 need_grow = 1;
672         }
673         cfs_spin_unlock(&page_pools.epp_lock);
674
675         if (need_grow) {
676                 enc_pools_add_pages(PTLRPC_MAX_BRW_PAGES +
677                                     PTLRPC_MAX_BRW_PAGES);
678
679                 cfs_spin_lock(&page_pools.epp_lock);
680                 page_pools.epp_growing = 0;
681                 enc_pools_wakeup();
682                 cfs_spin_unlock(&page_pools.epp_lock);
683         }
684         return 0;
685 }
686 EXPORT_SYMBOL(sptlrpc_enc_pool_add_user);
687
688 int sptlrpc_enc_pool_del_user(void)
689 {
690         return 0;
691 }
692 EXPORT_SYMBOL(sptlrpc_enc_pool_del_user);
693
694 static inline void enc_pools_alloc(void)
695 {
696         LASSERT(page_pools.epp_max_pools);
697         OBD_ALLOC_LARGE(page_pools.epp_pools,
698                         page_pools.epp_max_pools *
699                         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_LARGE(page_pools.epp_pools,
708                        page_pools.epp_max_pools *
709                        sizeof(*page_pools.epp_pools));
710 }
711
712 int sptlrpc_enc_pool_init(void)
713 {
714         /*
715          * maximum capacity is 1/8 of total physical memory.
716          * is the 1/8 a good number?
717          */
718         page_pools.epp_max_pages = cfs_num_physpages / 8;
719         page_pools.epp_max_pools = npages_to_npools(page_pools.epp_max_pages);
720
721         cfs_waitq_init(&page_pools.epp_waitq);
722         page_pools.epp_waitqlen = 0;
723         page_pools.epp_pages_short = 0;
724
725         page_pools.epp_growing = 0;
726
727         page_pools.epp_idle_idx = 0;
728         page_pools.epp_last_shrink = cfs_time_current_sec();
729         page_pools.epp_last_access = cfs_time_current_sec();
730
731         cfs_spin_lock_init(&page_pools.epp_lock);
732         page_pools.epp_total_pages = 0;
733         page_pools.epp_free_pages = 0;
734
735         page_pools.epp_st_max_pages = 0;
736         page_pools.epp_st_grows = 0;
737         page_pools.epp_st_grow_fails = 0;
738         page_pools.epp_st_shrinks = 0;
739         page_pools.epp_st_access = 0;
740         page_pools.epp_st_missings = 0;
741         page_pools.epp_st_lowfree = 0;
742         page_pools.epp_st_max_wqlen = 0;
743         page_pools.epp_st_max_wait = 0;
744
745         enc_pools_alloc();
746         if (page_pools.epp_pools == NULL)
747                 return -ENOMEM;
748
749         pools_shrinker = cfs_set_shrinker(pools_shrinker_seeks,
750                                           enc_pools_shrink);
751         if (pools_shrinker == NULL) {
752                 enc_pools_free();
753                 return -ENOMEM;
754         }
755
756         return 0;
757 }
758
759 void sptlrpc_enc_pool_fini(void)
760 {
761         unsigned long cleaned, npools;
762
763         LASSERT(pools_shrinker);
764         LASSERT(page_pools.epp_pools);
765         LASSERT(page_pools.epp_total_pages == page_pools.epp_free_pages);
766
767         cfs_remove_shrinker(pools_shrinker);
768
769         npools = npages_to_npools(page_pools.epp_total_pages);
770         cleaned = enc_pools_cleanup(page_pools.epp_pools, npools);
771         LASSERT(cleaned == page_pools.epp_total_pages);
772
773         enc_pools_free();
774
775         if (page_pools.epp_st_access > 0) {
776                 CWARN("max pages %lu, grows %u, grow fails %u, shrinks %u, "
777                       "access %lu, missing %lu, max qlen %u, max wait "
778                       CFS_TIME_T"/%d\n",
779                       page_pools.epp_st_max_pages, page_pools.epp_st_grows,
780                       page_pools.epp_st_grow_fails,
781                       page_pools.epp_st_shrinks, page_pools.epp_st_access,
782                       page_pools.epp_st_missings, page_pools.epp_st_max_wqlen,
783                       page_pools.epp_st_max_wait, CFS_HZ);
784         }
785 }
786
787 #else /* !__KERNEL__ */
788
789 int sptlrpc_enc_pool_get_pages(struct ptlrpc_bulk_desc *desc)
790 {
791         return 0;
792 }
793
794 void sptlrpc_enc_pool_put_pages(struct ptlrpc_bulk_desc *desc)
795 {
796 }
797
798 int sptlrpc_enc_pool_init(void)
799 {
800         return 0;
801 }
802
803 void sptlrpc_enc_pool_fini(void)
804 {
805 }
806 #endif
807
808 /****************************************
809  * Helpers to assist policy modules to  *
810  * implement checksum funcationality    *
811  ****************************************/
812
813 static struct sptlrpc_hash_type hash_types[] = {
814         [BULK_HASH_ALG_NULL]    = { "null",     "null",         0 },
815         [BULK_HASH_ALG_ADLER32] = { "adler32",  "adler32",      4 },
816         [BULK_HASH_ALG_CRC32]   = { "crc32",    "crc32",        4 },
817         [BULK_HASH_ALG_MD5]     = { "md5",      "md5",          16 },
818         [BULK_HASH_ALG_SHA1]    = { "sha1",     "sha1",         20 },
819         [BULK_HASH_ALG_SHA256]  = { "sha256",   "sha256",       32 },
820         [BULK_HASH_ALG_SHA384]  = { "sha384",   "sha384",       48 },
821         [BULK_HASH_ALG_SHA512]  = { "sha512",   "sha512",       64 },
822 };
823
824 const struct sptlrpc_hash_type *sptlrpc_get_hash_type(__u8 hash_alg)
825 {
826         struct sptlrpc_hash_type *ht;
827
828         if (hash_alg < BULK_HASH_ALG_MAX) {
829                 ht = &hash_types[hash_alg];
830                 if (ht->sht_tfm_name)
831                         return ht;
832         }
833         return NULL;
834 }
835 EXPORT_SYMBOL(sptlrpc_get_hash_type);
836
837 const char * sptlrpc_get_hash_name(__u8 hash_alg)
838 {
839         const struct sptlrpc_hash_type *ht;
840
841         ht = sptlrpc_get_hash_type(hash_alg);
842         if (ht)
843                 return ht->sht_name;
844         else
845                 return "unknown";
846 }
847 EXPORT_SYMBOL(sptlrpc_get_hash_name);
848
849 __u8 sptlrpc_get_hash_alg(const char *algname)
850 {
851         int     i;
852
853         for (i = 0; i < BULK_HASH_ALG_MAX; i++)
854                 if (!strcmp(hash_types[i].sht_name, algname))
855                         break;
856         return i;
857 }
858 EXPORT_SYMBOL(sptlrpc_get_hash_alg);
859
860 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
861 {
862         struct ptlrpc_bulk_sec_desc *bsd;
863         int                          size = msg->lm_buflens[offset];
864
865         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
866         if (bsd == NULL) {
867                 CERROR("Invalid bulk sec desc: size %d\n", size);
868                 return -EINVAL;
869         }
870
871         if (swabbed) {
872                 __swab32s(&bsd->bsd_nob);
873         }
874
875         if (unlikely(bsd->bsd_version != 0)) {
876                 CERROR("Unexpected version %u\n", bsd->bsd_version);
877                 return -EPROTO;
878         }
879
880         if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
881                 CERROR("Invalid type %u\n", bsd->bsd_type);
882                 return -EPROTO;
883         }
884
885         /* FIXME more sanity check here */
886
887         if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
888                      bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
889                      bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
890                 CERROR("Invalid svc %u\n", bsd->bsd_svc);
891                 return -EPROTO;
892         }
893
894         return 0;
895 }
896 EXPORT_SYMBOL(bulk_sec_desc_unpack);
897
898 #ifdef __KERNEL__
899
900 #ifdef HAVE_ADLER
901 static int do_bulk_checksum_adler32(struct ptlrpc_bulk_desc *desc, void *buf)
902 {
903         struct page    *page;
904         int             off;
905         char           *ptr;
906         __u32           adler32 = 1;
907         int             len, i;
908
909         for (i = 0; i < desc->bd_iov_count; i++) {
910                 page = desc->bd_iov[i].kiov_page;
911                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
912                 ptr = cfs_kmap(page) + off;
913                 len = desc->bd_iov[i].kiov_len;
914
915                 adler32 = adler32(adler32, ptr, len);
916
917                 cfs_kunmap(page);
918         }
919
920         adler32 = cpu_to_le32(adler32);
921         memcpy(buf, &adler32, sizeof(adler32));
922         return 0;
923 }
924 #endif
925
926 static int do_bulk_checksum_crc32(struct ptlrpc_bulk_desc *desc, void *buf)
927 {
928         struct page    *page;
929         int             off;
930         char           *ptr;
931         __u32           crc32 = ~0;
932         int             len, i;
933
934         for (i = 0; i < desc->bd_iov_count; i++) {
935                 page = desc->bd_iov[i].kiov_page;
936                 off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
937                 ptr = cfs_kmap(page) + off;
938                 len = desc->bd_iov[i].kiov_len;
939
940                 crc32 = crc32_le(crc32, ptr, len);
941
942                 cfs_kunmap(page);
943         }
944
945         crc32 = cpu_to_le32(crc32);
946         memcpy(buf, &crc32, sizeof(crc32));
947         return 0;
948 }
949
950 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
951                               void *buf, int buflen)
952 {
953         struct hash_desc    hdesc;
954         int                 hashsize;
955         char                hashbuf[64];
956         struct scatterlist  sl;
957         int                 i;
958
959         LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
960         LASSERT(buflen >= 4);
961
962         switch (alg) {
963         case BULK_HASH_ALG_ADLER32:
964 #ifdef HAVE_ADLER
965                 return do_bulk_checksum_adler32(desc, buf);
966 #else
967                 CERROR("Adler32 not supported\n");
968                 return -EINVAL;
969 #endif
970         case BULK_HASH_ALG_CRC32:
971                 return do_bulk_checksum_crc32(desc, buf);
972         }
973
974         hdesc.tfm = ll_crypto_alloc_hash(hash_types[alg].sht_tfm_name, 0, 0);
975         if (hdesc.tfm == NULL) {
976                 CERROR("Unable to allocate TFM %s\n", hash_types[alg].sht_name);
977                 return -ENOMEM;
978         }
979
980         hdesc.flags = 0;
981         ll_crypto_hash_init(&hdesc);
982
983         hashsize = ll_crypto_hash_digestsize(hdesc.tfm);
984
985         for (i = 0; i < desc->bd_iov_count; i++) {
986                 sg_set_page(&sl, desc->bd_iov[i].kiov_page,
987                              desc->bd_iov[i].kiov_len,
988                              desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK);
989                 ll_crypto_hash_update(&hdesc, &sl, sl.length);
990         }
991
992         if (hashsize > buflen) {
993                 ll_crypto_hash_final(&hdesc, hashbuf);
994                 memcpy(buf, hashbuf, buflen);
995         } else {
996                 ll_crypto_hash_final(&hdesc, buf);
997         }
998
999         ll_crypto_free_hash(hdesc.tfm);
1000         return 0;
1001 }
1002 EXPORT_SYMBOL(sptlrpc_get_bulk_checksum);
1003
1004 #else /* !__KERNEL__ */
1005
1006 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
1007                               void *buf, int buflen)
1008 {
1009         __u32   csum32;
1010         int     i;
1011
1012         LASSERT(alg == BULK_HASH_ALG_ADLER32 || alg == BULK_HASH_ALG_CRC32);
1013
1014         if (alg == BULK_HASH_ALG_ADLER32)
1015                 csum32 = 1;
1016         else
1017                 csum32 = ~0;
1018
1019         for (i = 0; i < desc->bd_iov_count; i++) {
1020                 unsigned char *ptr = desc->bd_iov[i].iov_base;
1021                 int len = desc->bd_iov[i].iov_len;
1022
1023                 switch (alg) {
1024                 case BULK_HASH_ALG_ADLER32:
1025 #ifdef HAVE_ADLER
1026                         csum32 = adler32(csum32, ptr, len);
1027 #else
1028                         CERROR("Adler32 not supported\n");
1029                         return -EINVAL;
1030 #endif
1031                         break;
1032                 case BULK_HASH_ALG_CRC32:
1033                         csum32 = crc32_le(csum32, ptr, len);
1034                         break;
1035                 }
1036         }
1037
1038         csum32 = cpu_to_le32(csum32);
1039         memcpy(buf, &csum32, sizeof(csum32));
1040         return 0;
1041 }
1042
1043 #endif /* __KERNEL__ */