xref: /netbsd-src/sys/fs/tmpfs/tmpfs_mem.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: tmpfs_mem.c,v 1.2 2010/06/28 19:32:43 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * tmpfs memory allocation routines.
31  * Implements memory usage accounting and limiting.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: tmpfs_mem.c,v 1.2 2010/06/28 19:32:43 rmind Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/atomic.h>
39 #include <sys/kmem.h>
40 #include <sys/namei.h>
41 #include <sys/pool.h>
42 
43 #include <fs/tmpfs/tmpfs.h>
44 
45 void
46 tmpfs_mntmem_init(struct tmpfs_mount *mp, uint64_t memlimit)
47 {
48 
49 	sprintf(mp->tm_dwchan, "tmpfs_dirent_%p", mp);
50 	pool_init(&mp->tm_dirent_pool, sizeof(struct tmpfs_dirent), 0, 0, 0,
51 	    mp->tm_dwchan, &pool_allocator_nointr, IPL_NONE);
52 
53 	sprintf(mp->tm_nwchan, "tmpfs_node_%p", mp);
54 	pool_init(&mp->tm_node_pool, sizeof(struct tmpfs_node), 0, 0, 0,
55 	    mp->tm_dwchan, &pool_allocator_nointr, IPL_NONE);
56 
57 	mutex_init(&mp->tm_acc_lock, MUTEX_DEFAULT, IPL_NONE);
58 	mp->tm_mem_limit = memlimit;
59 	mp->tm_bytes_used = 0;
60 }
61 
62 void
63 tmpfs_mntmem_destroy(struct tmpfs_mount *mp)
64 {
65 
66 	KASSERT(mp->tm_bytes_used == 0);
67 	mutex_destroy(&mp->tm_acc_lock);
68 	pool_destroy(&mp->tm_dirent_pool);
69 	pool_destroy(&mp->tm_node_pool);
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(bool total)
83 {
84 	size_t size = 0;
85 
86 	/* XXX: unlocked */
87 	size += uvmexp.swpgavail;
88 	if (!total) {
89 		size -= uvmexp.swpgonly;
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 	return size;
99 }
100 
101 uint64_t
102 tmpfs_bytes_max(struct tmpfs_mount *mp)
103 {
104 	size_t freepages = tmpfs_mem_info(false);
105 	uint64_t avail_mem;
106 
107 	if (freepages < TMPFS_PAGES_RESERVED) {
108 		freepages = 0;
109 	} else {
110 		freepages -= TMPFS_PAGES_RESERVED;
111 	}
112 	avail_mem = round_page(mp->tm_bytes_used) + (freepages << PAGE_SHIFT);
113 	return MIN(mp->tm_mem_limit, avail_mem);
114 }
115 
116 size_t
117 tmpfs_pages_avail(struct tmpfs_mount *mp)
118 {
119 
120 	return (tmpfs_bytes_max(mp) - mp->tm_bytes_used) >> PAGE_SHIFT;
121 }
122 
123 bool
124 tmpfs_mem_incr(struct tmpfs_mount *mp, size_t sz)
125 {
126 	uint64_t lim;
127 
128 	mutex_enter(&mp->tm_acc_lock);
129 	lim = tmpfs_bytes_max(mp);
130 	if (mp->tm_bytes_used + sz >= lim) {
131 		mutex_exit(&mp->tm_acc_lock);
132 		return false;
133 	}
134 	mp->tm_bytes_used += sz;
135 	mutex_exit(&mp->tm_acc_lock);
136 	return true;
137 }
138 
139 void
140 tmpfs_mem_decr(struct tmpfs_mount *mp, size_t sz)
141 {
142 
143 	mutex_enter(&mp->tm_acc_lock);
144 	KASSERT(mp->tm_bytes_used >= sz);
145 	mp->tm_bytes_used -= sz;
146 	mutex_exit(&mp->tm_acc_lock);
147 }
148 
149 struct tmpfs_dirent *
150 tmpfs_dirent_get(struct tmpfs_mount *mp)
151 {
152 
153 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_dirent))) {
154 		return NULL;
155 	}
156 	return pool_get(&mp->tm_dirent_pool, PR_WAITOK);
157 }
158 
159 void
160 tmpfs_dirent_put(struct tmpfs_mount *mp, struct tmpfs_dirent *de)
161 {
162 
163 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_dirent));
164 	pool_put(&mp->tm_dirent_pool, de);
165 }
166 
167 struct tmpfs_node *
168 tmpfs_node_get(struct tmpfs_mount *mp)
169 {
170 
171 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_node))) {
172 		return NULL;
173 	}
174 	return pool_get(&mp->tm_node_pool, PR_WAITOK);
175 }
176 
177 void
178 tmpfs_node_put(struct tmpfs_mount *mp, struct tmpfs_node *tn)
179 {
180 
181 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_node));
182 	pool_put(&mp->tm_node_pool, tn);
183 }
184 
185 /*
186  * Quantum size to round-up the tmpfs names in order to reduce re-allocations.
187  */
188 
189 #define	TMPFS_NAME_QUANTUM	(32)
190 
191 char *
192 tmpfs_strname_alloc(struct tmpfs_mount *mp, size_t len)
193 {
194 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
195 
196 	KASSERT(sz > 0 && sz <= 1024);
197 	if (!tmpfs_mem_incr(mp, sz)) {
198 		return NULL;
199 	}
200 	return kmem_alloc(sz, KM_SLEEP);
201 }
202 
203 void
204 tmpfs_strname_free(struct tmpfs_mount *mp, char *str, size_t len)
205 {
206 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
207 
208 	KASSERT(sz > 0 && sz <= 1024);
209 	tmpfs_mem_decr(mp, sz);
210 	kmem_free(str, sz);
211 }
212 
213 bool
214 tmpfs_strname_neqlen(struct componentname *fcnp, struct componentname *tcnp)
215 {
216 	const size_t fln = roundup2(fcnp->cn_namelen, TMPFS_NAME_QUANTUM);
217 	const size_t tln = roundup2(tcnp->cn_namelen, TMPFS_NAME_QUANTUM);
218 
219 	return (fln != tln) || memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fln);
220 }
221