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