xref: /openbsd-src/sys/tmpfs/tmpfs_mem.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: tmpfs_mem.c,v 1.5 2014/11/02 03:47:28 tedu 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/sysctl.h>
48 #include <sys/malloc.h>
49 
50 #include <tmpfs/tmpfs.h>
51 
52 extern struct pool	tmpfs_dirent_pool;
53 extern struct pool	tmpfs_node_pool;
54 
55 void
56 tmpfs_mntmem_init(struct tmpfs_mount *mp, uint64_t memlimit)
57 {
58 
59 	rw_init(&mp->tm_acc_lock, "tacclk");
60 	mp->tm_mem_limit = memlimit;
61 	mp->tm_bytes_used = 0;
62 }
63 
64 void
65 tmpfs_mntmem_destroy(struct tmpfs_mount *mp)
66 {
67 
68 	KASSERT(mp->tm_bytes_used == 0);
69 	/* mutex_destroy(&mp->tm_acc_lock); */
70 }
71 
72 /*
73  * tmpfs_mem_info: return the number of available memory pages.
74  *
75  * => If 'total' is true, then return _total_ amount of pages.
76  * => If false, then return the amount of _free_ memory pages.
77  *
78  * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
79  * excessive memory usage.
80  */
81 size_t
82 tmpfs_mem_info(int total)
83 {
84 	int size = 0;
85 
86 	/* XXX: unlocked */
87 	size += uvmexp.swpages;
88 	if (!total) {
89 		size -= uvmexp.swpgonly;
90 	}
91 
92 	size += uvmexp.free;
93 	/* size += uvmexp.filepages; */
94 	if (size > uvmexp.wired) {
95 		size -= uvmexp.wired;
96 	} else {
97 		size = 0;
98 	}
99 
100 	KASSERT(size >= 0);
101 
102 	return (size_t)size;
103 }
104 
105 uint64_t
106 tmpfs_bytes_max(struct tmpfs_mount *mp)
107 {
108 	size_t freepages = tmpfs_mem_info(0);
109 	uint64_t avail_mem;
110 
111 	if (freepages < TMPFS_PAGES_RESERVED) {
112 		freepages = 0;
113 	} else {
114 		freepages -= TMPFS_PAGES_RESERVED;
115 	}
116 	avail_mem = round_page(mp->tm_bytes_used) + (freepages << PAGE_SHIFT);
117 	return MIN(mp->tm_mem_limit, avail_mem);
118 }
119 
120 uint64_t
121 tmpfs_pages_avail(struct tmpfs_mount *mp)
122 {
123 
124 	return (tmpfs_bytes_max(mp) - mp->tm_bytes_used) >> PAGE_SHIFT;
125 }
126 
127 int
128 tmpfs_mem_incr(struct tmpfs_mount *mp, size_t sz)
129 {
130 	uint64_t lim;
131 
132 	rw_enter_write(&mp->tm_acc_lock);
133 	lim = tmpfs_bytes_max(mp);
134 	if (mp->tm_bytes_used + sz >= lim) {
135 		rw_exit_write(&mp->tm_acc_lock);
136 		return 0;
137 	}
138 	mp->tm_bytes_used += sz;
139 	rw_exit_write(&mp->tm_acc_lock);
140 	return 1;
141 }
142 
143 void
144 tmpfs_mem_decr(struct tmpfs_mount *mp, size_t sz)
145 {
146 
147 	rw_enter_write(&mp->tm_acc_lock);
148 	KASSERT(mp->tm_bytes_used >= sz);
149 	mp->tm_bytes_used -= sz;
150 	rw_exit_write(&mp->tm_acc_lock);
151 }
152 
153 struct tmpfs_dirent *
154 tmpfs_dirent_get(struct tmpfs_mount *mp)
155 {
156 
157 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_dirent))) {
158 		return NULL;
159 	}
160 	return pool_get(&tmpfs_dirent_pool, PR_WAITOK);
161 }
162 
163 void
164 tmpfs_dirent_put(struct tmpfs_mount *mp, struct tmpfs_dirent *de)
165 {
166 
167 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_dirent));
168 	pool_put(&tmpfs_dirent_pool, de);
169 }
170 
171 struct tmpfs_node *
172 tmpfs_node_get(struct tmpfs_mount *mp)
173 {
174 
175 	mp->tm_nodes_cnt++;
176 	if (mp->tm_nodes_cnt > mp->tm_nodes_max) {
177 		mp->tm_nodes_cnt--;
178 		return NULL;
179 	}
180 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_node))) {
181 		return NULL;
182 	}
183 	return pool_get(&tmpfs_node_pool, PR_WAITOK);
184 }
185 
186 void
187 tmpfs_node_put(struct tmpfs_mount *mp, struct tmpfs_node *tn)
188 {
189 
190 	mp->tm_nodes_cnt--;
191 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_node));
192 	pool_put(&tmpfs_node_pool, tn);
193 }
194 
195 /*
196  * Quantum size to round-up the tmpfs names in order to reduce re-allocations.
197  */
198 
199 #define	TMPFS_NAME_QUANTUM	(32)
200 #define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
201 
202 char *
203 tmpfs_strname_alloc(struct tmpfs_mount *mp, size_t len)
204 {
205 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
206 
207 	KASSERT(sz > 0 && sz <= 1024);
208 	if (!tmpfs_mem_incr(mp, sz)) {
209 		return NULL;
210 	}
211 	return malloc(sz, M_TEMP, M_WAITOK); /* XXX */
212 }
213 
214 void
215 tmpfs_strname_free(struct tmpfs_mount *mp, char *str, size_t len)
216 {
217 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
218 
219 	KASSERT(sz > 0 && sz <= 1024);
220 	tmpfs_mem_decr(mp, sz);
221 	free(str, M_TEMP, sz);
222 }
223 
224 int
225 tmpfs_strname_neqlen(struct componentname *fcnp, struct componentname *tcnp)
226 {
227 	const size_t fln = roundup2(fcnp->cn_namelen, TMPFS_NAME_QUANTUM);
228 	const size_t tln = roundup2(tcnp->cn_namelen, TMPFS_NAME_QUANTUM);
229 
230 	return (fln != tln) || memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fln);
231 }
232