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