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