xref: /openbsd-src/sys/tmpfs/tmpfs_mem.c (revision aa5e9e10509ffd51558f081f01cd78bfa3c4f2a5)
1 /*	$OpenBSD: tmpfs_mem.c,v 1.2 2013/06/03 10:37:02 espie Exp $	*/
2 /*	$NetBSD: tmpfs_mem.c,v 1.4 2011/05/24 01:09:47 rmind Exp $	*/
3 
4 /*
5  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Mindaugas Rasiukevicius.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * tmpfs memory allocation routines.
35  * Implements memory usage accounting and limiting.
36  */
37 
38 #if 0
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: tmpfs_mem.c,v 1.4 2011/05/24 01:09:47 rmind Exp $");
41 #endif
42 
43 #include <sys/param.h>
44 #include <sys/namei.h>
45 #include <sys/pool.h>
46 #include <sys/vnode.h>
47 #include <sys/malloc.h>
48 
49 #include <tmpfs/tmpfs.h>
50 
51 extern struct pool	tmpfs_dirent_pool;
52 extern struct pool	tmpfs_node_pool;
53 
54 void
55 tmpfs_mntmem_init(struct tmpfs_mount *mp, uint64_t memlimit)
56 {
57 
58 	rw_init(&mp->tm_acc_lock, "tacclk");
59 	mp->tm_mem_limit = memlimit;
60 	mp->tm_bytes_used = 0;
61 }
62 
63 void
64 tmpfs_mntmem_destroy(struct tmpfs_mount *mp)
65 {
66 
67 	KASSERT(mp->tm_bytes_used == 0);
68 	/* mutex_destroy(&mp->tm_acc_lock); */
69 }
70 
71 /*
72  * tmpfs_mem_info: return the number of available memory pages.
73  *
74  * => If 'total' is true, then return _total_ amount of pages.
75  * => If false, then return the amount of _free_ memory pages.
76  *
77  * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
78  * excessive memory usage.
79  */
80 size_t
81 tmpfs_mem_info(int total)
82 {
83 	int size = 0;
84 
85 	/* XXX: unlocked */
86 	size += uvmexp.swpages;
87 	if (!total) {
88 		size -= uvmexp.swpgonly;
89 	}
90 
91 	size += uvmexp.free;
92 	/* size += uvmexp.filepages; */
93 	if (size > uvmexp.wired) {
94 		size -= uvmexp.wired;
95 	} else {
96 		size = 0;
97 	}
98 
99 	KASSERT(size >= 0);
100 
101 	return (size_t)size;
102 }
103 
104 uint64_t
105 tmpfs_bytes_max(struct tmpfs_mount *mp)
106 {
107 	size_t freepages = tmpfs_mem_info(0);
108 	uint64_t avail_mem;
109 
110 	if (freepages < TMPFS_PAGES_RESERVED) {
111 		freepages = 0;
112 	} else {
113 		freepages -= TMPFS_PAGES_RESERVED;
114 	}
115 	avail_mem = round_page(mp->tm_bytes_used) + (freepages << PAGE_SHIFT);
116 	return MIN(mp->tm_mem_limit, avail_mem);
117 }
118 
119 uint64_t
120 tmpfs_pages_avail(struct tmpfs_mount *mp)
121 {
122 
123 	return (tmpfs_bytes_max(mp) - mp->tm_bytes_used) >> PAGE_SHIFT;
124 }
125 
126 int
127 tmpfs_mem_incr(struct tmpfs_mount *mp, size_t sz)
128 {
129 	uint64_t lim;
130 
131 	rw_enter_write(&mp->tm_acc_lock);
132 	lim = tmpfs_bytes_max(mp);
133 	if (mp->tm_bytes_used + sz >= lim) {
134 		rw_exit_write(&mp->tm_acc_lock);
135 		return 0;
136 	}
137 	mp->tm_bytes_used += sz;
138 	rw_exit_write(&mp->tm_acc_lock);
139 	return 1;
140 }
141 
142 void
143 tmpfs_mem_decr(struct tmpfs_mount *mp, size_t sz)
144 {
145 
146 	rw_enter_write(&mp->tm_acc_lock);
147 	KASSERT(mp->tm_bytes_used >= sz);
148 	mp->tm_bytes_used -= sz;
149 	rw_exit_write(&mp->tm_acc_lock);
150 }
151 
152 struct tmpfs_dirent *
153 tmpfs_dirent_get(struct tmpfs_mount *mp)
154 {
155 
156 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_dirent))) {
157 		return NULL;
158 	}
159 	return pool_get(&tmpfs_dirent_pool, PR_WAITOK);
160 }
161 
162 void
163 tmpfs_dirent_put(struct tmpfs_mount *mp, struct tmpfs_dirent *de)
164 {
165 
166 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_dirent));
167 	pool_put(&tmpfs_dirent_pool, de);
168 }
169 
170 struct tmpfs_node *
171 tmpfs_node_get(struct tmpfs_mount *mp)
172 {
173 
174 	mp->tm_nodes_cnt++;
175 	if (mp->tm_nodes_cnt > mp->tm_nodes_max) {
176 		mp->tm_nodes_cnt--;
177 		return NULL;
178 	}
179 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_node))) {
180 		return NULL;
181 	}
182 	return pool_get(&tmpfs_node_pool, PR_WAITOK);
183 }
184 
185 void
186 tmpfs_node_put(struct tmpfs_mount *mp, struct tmpfs_node *tn)
187 {
188 
189 	mp->tm_nodes_cnt--;
190 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_node));
191 	pool_put(&tmpfs_node_pool, tn);
192 }
193 
194 /*
195  * Quantum size to round-up the tmpfs names in order to reduce re-allocations.
196  */
197 
198 #define	TMPFS_NAME_QUANTUM	(32)
199 #define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
200 
201 char *
202 tmpfs_strname_alloc(struct tmpfs_mount *mp, size_t len)
203 {
204 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
205 
206 	KASSERT(sz > 0 && sz <= 1024);
207 	if (!tmpfs_mem_incr(mp, sz)) {
208 		return NULL;
209 	}
210 	return malloc(sz, M_TEMP, M_WAITOK); /* XXX */
211 }
212 
213 void
214 tmpfs_strname_free(struct tmpfs_mount *mp, char *str, size_t len)
215 {
216 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
217 
218 	KASSERT(sz > 0 && sz <= 1024);
219 	tmpfs_mem_decr(mp, sz);
220 	free(str, M_TEMP);
221 }
222 
223 int
224 tmpfs_strname_neqlen(struct componentname *fcnp, struct componentname *tcnp)
225 {
226 	const size_t fln = roundup2(fcnp->cn_namelen, TMPFS_NAME_QUANTUM);
227 	const size_t tln = roundup2(tcnp->cn_namelen, TMPFS_NAME_QUANTUM);
228 
229 	return (fln != tln) || memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fln);
230 }
231