xref: /freebsd/sys/contrib/openzfs/include/os/linux/spl/sys/shrinker.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
4  *  Copyright (C) 2007 The Regents of the University of California.
5  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
7  *  UCRL-CODE-235197
8  *
9  *  This file is part of the SPL, Solaris Porting Layer.
10  *
11  *  The SPL is free software; you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License as published by the
13  *  Free Software Foundation; either version 2 of the License, or (at your
14  *  option) any later version.
15  *
16  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
17  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19  *  for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef _SPL_SHRINKER_H
26 #define	_SPL_SHRINKER_H
27 
28 #include <linux/mm.h>
29 #include <linux/fs.h>
30 
31 /*
32  * Due to frequent changes in the shrinker API the following
33  * compatibility wrapper should be used.
34  *
35  *   shrinker = spl_register_shrinker(name, countfunc, scanfunc, seek_cost);
36  *   spl_unregister_shrinker(shrinker);
37  *
38  * spl_register_shrinker is used to create and register a shrinker with the
39  * given name.
40  * The countfunc returns the number of free-able objects.
41  * The scanfunc returns the number of objects that were freed.
42  * The callbacks can return SHRINK_STOP if further calls can't make any more
43  * progress.  Note that a return value of SHRINK_EMPTY is currently not
44  * supported.
45  *
46  * Example:
47  *
48  * static unsigned long
49  * my_count(struct shrinker *shrink, struct shrink_control *sc)
50  * {
51  *	...calculate number of objects in the cache...
52  *
53  *	return (number of objects in the cache);
54  * }
55  *
56  * static unsigned long
57  * my_scan(struct shrinker *shrink, struct shrink_control *sc)
58  * {
59  *	...scan objects in the cache and reclaim them...
60  * }
61  *
62  * static struct shrinker *my_shrinker;
63  *
64  * void my_init_func(void) {
65  *	my_shrinker = spl_register_shrinker("my-shrinker",
66  *	    my_count, my_scan, DEFAULT_SEEKS);
67  * }
68  *
69  * void my_fini_func(void) {
70  *	spl_unregister_shrinker(my_shrinker);
71  * }
72  */
73 
74 typedef unsigned long (*spl_shrinker_cb)
75 	(struct shrinker *, struct shrink_control *);
76 
77 struct shrinker *spl_register_shrinker(const char *name,
78     spl_shrinker_cb countfunc, spl_shrinker_cb scanfunc, int seek_cost);
79 void spl_unregister_shrinker(struct shrinker *);
80 
81 #ifndef SHRINK_STOP
82 /* 3.0-3.11 compatibility */
83 #define	SHRINK_STOP	(-1)
84 #endif
85 
86 #endif /* SPL_SHRINKER_H */
87