tmpfs_subr.c revision 1.21.4.10 1 1.21.4.10 yamt /* $NetBSD: tmpfs_subr.c,v 1.21.4.10 2008/02/11 14:59:53 yamt Exp $ */
2 1.21.4.2 yamt
3 1.21.4.2 yamt /*
4 1.21.4.8 yamt * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 1.21.4.2 yamt * All rights reserved.
6 1.21.4.2 yamt *
7 1.21.4.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.21.4.2 yamt * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.21.4.2 yamt * 2005 program.
10 1.21.4.2 yamt *
11 1.21.4.2 yamt * Redistribution and use in source and binary forms, with or without
12 1.21.4.2 yamt * modification, are permitted provided that the following conditions
13 1.21.4.2 yamt * are met:
14 1.21.4.2 yamt * 1. Redistributions of source code must retain the above copyright
15 1.21.4.2 yamt * notice, this list of conditions and the following disclaimer.
16 1.21.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
17 1.21.4.2 yamt * notice, this list of conditions and the following disclaimer in the
18 1.21.4.2 yamt * documentation and/or other materials provided with the distribution.
19 1.21.4.2 yamt * 3. All advertising materials mentioning features or use of this software
20 1.21.4.2 yamt * must display the following acknowledgement:
21 1.21.4.2 yamt * This product includes software developed by the NetBSD
22 1.21.4.2 yamt * Foundation, Inc. and its contributors.
23 1.21.4.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.21.4.2 yamt * contributors may be used to endorse or promote products derived
25 1.21.4.2 yamt * from this software without specific prior written permission.
26 1.21.4.2 yamt *
27 1.21.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.21.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.21.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.21.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.21.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.21.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.21.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.21.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.21.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.21.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.21.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
38 1.21.4.2 yamt */
39 1.21.4.2 yamt
40 1.21.4.2 yamt /*
41 1.21.4.2 yamt * Efficient memory file system supporting functions.
42 1.21.4.2 yamt */
43 1.21.4.2 yamt
44 1.21.4.2 yamt #include <sys/cdefs.h>
45 1.21.4.10 yamt __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.21.4.10 2008/02/11 14:59:53 yamt Exp $");
46 1.21.4.2 yamt
47 1.21.4.2 yamt #include <sys/param.h>
48 1.21.4.2 yamt #include <sys/dirent.h>
49 1.21.4.2 yamt #include <sys/event.h>
50 1.21.4.8 yamt #include <sys/kmem.h>
51 1.21.4.2 yamt #include <sys/mount.h>
52 1.21.4.2 yamt #include <sys/namei.h>
53 1.21.4.2 yamt #include <sys/time.h>
54 1.21.4.2 yamt #include <sys/stat.h>
55 1.21.4.2 yamt #include <sys/systm.h>
56 1.21.4.2 yamt #include <sys/swap.h>
57 1.21.4.2 yamt #include <sys/vnode.h>
58 1.21.4.2 yamt #include <sys/kauth.h>
59 1.21.4.5 yamt #include <sys/proc.h>
60 1.21.4.8 yamt #include <sys/atomic.h>
61 1.21.4.2 yamt
62 1.21.4.2 yamt #include <uvm/uvm.h>
63 1.21.4.2 yamt
64 1.21.4.2 yamt #include <miscfs/specfs/specdev.h>
65 1.21.4.2 yamt #include <fs/tmpfs/tmpfs.h>
66 1.21.4.2 yamt #include <fs/tmpfs/tmpfs_fifoops.h>
67 1.21.4.2 yamt #include <fs/tmpfs/tmpfs_specops.h>
68 1.21.4.2 yamt #include <fs/tmpfs/tmpfs_vnops.h>
69 1.21.4.2 yamt
70 1.21.4.2 yamt /* --------------------------------------------------------------------- */
71 1.21.4.2 yamt
72 1.21.4.2 yamt /*
73 1.21.4.2 yamt * Allocates a new node of type 'type' inside the 'tmp' mount point, with
74 1.21.4.2 yamt * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
75 1.21.4.2 yamt * using the credentials of the process 'p'.
76 1.21.4.2 yamt *
77 1.21.4.2 yamt * If the node type is set to 'VDIR', then the parent parameter must point
78 1.21.4.2 yamt * to the parent directory of the node being created. It may only be NULL
79 1.21.4.2 yamt * while allocating the root node.
80 1.21.4.2 yamt *
81 1.21.4.2 yamt * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
82 1.21.4.2 yamt * specifies the device the node represents.
83 1.21.4.2 yamt *
84 1.21.4.2 yamt * If the node type is set to 'VLNK', then the parameter target specifies
85 1.21.4.2 yamt * the file name of the target file for the symbolic link that is being
86 1.21.4.2 yamt * created.
87 1.21.4.2 yamt *
88 1.21.4.2 yamt * Note that new nodes are retrieved from the available list if it has
89 1.21.4.2 yamt * items or, if it is empty, from the node pool as long as there is enough
90 1.21.4.2 yamt * space to create them.
91 1.21.4.2 yamt *
92 1.21.4.2 yamt * Returns zero on success or an appropriate error code on failure.
93 1.21.4.2 yamt */
94 1.21.4.2 yamt int
95 1.21.4.2 yamt tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
96 1.21.4.2 yamt uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
97 1.21.4.8 yamt char *target, dev_t rdev, struct tmpfs_node **node)
98 1.21.4.2 yamt {
99 1.21.4.2 yamt struct tmpfs_node *nnode;
100 1.21.4.2 yamt
101 1.21.4.2 yamt /* If the root directory of the 'tmp' file system is not yet
102 1.21.4.2 yamt * allocated, this must be the request to do it. */
103 1.21.4.2 yamt KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
104 1.21.4.2 yamt
105 1.21.4.2 yamt KASSERT(IFF(type == VLNK, target != NULL));
106 1.21.4.2 yamt KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
107 1.21.4.2 yamt
108 1.21.4.2 yamt KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
109 1.21.4.2 yamt
110 1.21.4.2 yamt nnode = NULL;
111 1.21.4.8 yamt if (atomic_inc_uint_nv(&tmp->tm_nodes_cnt) >= tmp->tm_nodes_max) {
112 1.21.4.8 yamt atomic_dec_uint(&tmp->tm_nodes_cnt);
113 1.21.4.8 yamt return ENOSPC;
114 1.21.4.8 yamt }
115 1.21.4.2 yamt
116 1.21.4.8 yamt nnode = (struct tmpfs_node *)TMPFS_POOL_GET(&tmp->tm_node_pool, 0);
117 1.21.4.8 yamt if (nnode == NULL) {
118 1.21.4.8 yamt atomic_dec_uint(&tmp->tm_nodes_cnt);
119 1.21.4.8 yamt return ENOSPC;
120 1.21.4.2 yamt }
121 1.21.4.8 yamt
122 1.21.4.8 yamt /*
123 1.21.4.8 yamt * XXX Where the pool is backed by a map larger than (4GB *
124 1.21.4.8 yamt * sizeof(*nnode)), this may produce duplicate inode numbers
125 1.21.4.8 yamt * for applications that do not understand 64-bit ino_t.
126 1.21.4.8 yamt */
127 1.21.4.8 yamt nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode));
128 1.21.4.8 yamt nnode->tn_gen = arc4random();
129 1.21.4.2 yamt
130 1.21.4.2 yamt /* Generic initialization. */
131 1.21.4.2 yamt nnode->tn_type = type;
132 1.21.4.2 yamt nnode->tn_size = 0;
133 1.21.4.2 yamt nnode->tn_status = 0;
134 1.21.4.2 yamt nnode->tn_flags = 0;
135 1.21.4.2 yamt nnode->tn_links = 0;
136 1.21.4.2 yamt getnanotime(&nnode->tn_atime);
137 1.21.4.2 yamt nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
138 1.21.4.2 yamt nnode->tn_atime;
139 1.21.4.2 yamt nnode->tn_uid = uid;
140 1.21.4.2 yamt nnode->tn_gid = gid;
141 1.21.4.2 yamt nnode->tn_mode = mode;
142 1.21.4.2 yamt nnode->tn_lockf = NULL;
143 1.21.4.2 yamt nnode->tn_vnode = NULL;
144 1.21.4.2 yamt
145 1.21.4.2 yamt /* Type-specific initialization. */
146 1.21.4.2 yamt switch (nnode->tn_type) {
147 1.21.4.2 yamt case VBLK:
148 1.21.4.2 yamt case VCHR:
149 1.21.4.2 yamt nnode->tn_spec.tn_dev.tn_rdev = rdev;
150 1.21.4.2 yamt break;
151 1.21.4.2 yamt
152 1.21.4.2 yamt case VDIR:
153 1.21.4.2 yamt TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
154 1.21.4.2 yamt nnode->tn_spec.tn_dir.tn_parent =
155 1.21.4.2 yamt (parent == NULL) ? nnode : parent;
156 1.21.4.2 yamt nnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
157 1.21.4.2 yamt nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
158 1.21.4.2 yamt nnode->tn_links++;
159 1.21.4.2 yamt break;
160 1.21.4.2 yamt
161 1.21.4.2 yamt case VFIFO:
162 1.21.4.2 yamt /* FALLTHROUGH */
163 1.21.4.2 yamt case VSOCK:
164 1.21.4.2 yamt break;
165 1.21.4.2 yamt
166 1.21.4.2 yamt case VLNK:
167 1.21.4.2 yamt KASSERT(strlen(target) < MAXPATHLEN);
168 1.21.4.2 yamt nnode->tn_size = strlen(target);
169 1.21.4.2 yamt nnode->tn_spec.tn_lnk.tn_link =
170 1.21.4.2 yamt tmpfs_str_pool_get(&tmp->tm_str_pool, nnode->tn_size, 0);
171 1.21.4.2 yamt if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
172 1.21.4.8 yamt atomic_dec_uint(&tmp->tm_nodes_cnt);
173 1.21.4.8 yamt TMPFS_POOL_PUT(&tmp->tm_node_pool, nnode);
174 1.21.4.2 yamt return ENOSPC;
175 1.21.4.2 yamt }
176 1.21.4.2 yamt memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
177 1.21.4.2 yamt break;
178 1.21.4.2 yamt
179 1.21.4.2 yamt case VREG:
180 1.21.4.2 yamt nnode->tn_spec.tn_reg.tn_aobj =
181 1.21.4.2 yamt uao_create(INT32_MAX - PAGE_SIZE, 0);
182 1.21.4.2 yamt nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
183 1.21.4.2 yamt break;
184 1.21.4.2 yamt
185 1.21.4.2 yamt default:
186 1.21.4.2 yamt KASSERT(0);
187 1.21.4.2 yamt }
188 1.21.4.2 yamt
189 1.21.4.8 yamt mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE);
190 1.21.4.8 yamt
191 1.21.4.8 yamt mutex_enter(&tmp->tm_lock);
192 1.21.4.8 yamt LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
193 1.21.4.8 yamt mutex_exit(&tmp->tm_lock);
194 1.21.4.8 yamt
195 1.21.4.2 yamt *node = nnode;
196 1.21.4.2 yamt return 0;
197 1.21.4.2 yamt }
198 1.21.4.2 yamt
199 1.21.4.2 yamt /* --------------------------------------------------------------------- */
200 1.21.4.2 yamt
201 1.21.4.2 yamt /*
202 1.21.4.2 yamt * Destroys the node pointed to by node from the file system 'tmp'.
203 1.21.4.2 yamt * If the node does not belong to the given mount point, the results are
204 1.21.4.2 yamt * unpredicted.
205 1.21.4.2 yamt *
206 1.21.4.2 yamt * If the node references a directory; no entries are allowed because
207 1.21.4.2 yamt * their removal could need a recursive algorithm, something forbidden in
208 1.21.4.2 yamt * kernel space. Furthermore, there is not need to provide such
209 1.21.4.2 yamt * functionality (recursive removal) because the only primitives offered
210 1.21.4.2 yamt * to the user are the removal of empty directories and the deletion of
211 1.21.4.2 yamt * individual files.
212 1.21.4.2 yamt *
213 1.21.4.2 yamt * Note that nodes are not really deleted; in fact, when a node has been
214 1.21.4.2 yamt * allocated, it cannot be deleted during the whole life of the file
215 1.21.4.2 yamt * system. Instead, they are moved to the available list and remain there
216 1.21.4.2 yamt * until reused.
217 1.21.4.2 yamt */
218 1.21.4.2 yamt void
219 1.21.4.2 yamt tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
220 1.21.4.2 yamt {
221 1.21.4.2 yamt
222 1.21.4.8 yamt if (node->tn_type == VREG) {
223 1.21.4.8 yamt atomic_add_int(&tmp->tm_pages_used,
224 1.21.4.8 yamt -node->tn_spec.tn_reg.tn_aobj_pages);
225 1.21.4.8 yamt }
226 1.21.4.8 yamt atomic_dec_uint(&tmp->tm_nodes_cnt);
227 1.21.4.8 yamt mutex_enter(&tmp->tm_lock);
228 1.21.4.8 yamt LIST_REMOVE(node, tn_entries);
229 1.21.4.8 yamt mutex_exit(&tmp->tm_lock);
230 1.21.4.2 yamt
231 1.21.4.8 yamt switch (node->tn_type) {
232 1.21.4.2 yamt case VLNK:
233 1.21.4.2 yamt tmpfs_str_pool_put(&tmp->tm_str_pool,
234 1.21.4.2 yamt node->tn_spec.tn_lnk.tn_link, node->tn_size);
235 1.21.4.2 yamt break;
236 1.21.4.2 yamt
237 1.21.4.2 yamt case VREG:
238 1.21.4.2 yamt if (node->tn_spec.tn_reg.tn_aobj != NULL)
239 1.21.4.2 yamt uao_detach(node->tn_spec.tn_reg.tn_aobj);
240 1.21.4.2 yamt break;
241 1.21.4.2 yamt
242 1.21.4.2 yamt default:
243 1.21.4.2 yamt break;
244 1.21.4.2 yamt }
245 1.21.4.2 yamt
246 1.21.4.8 yamt mutex_destroy(&node->tn_vlock);
247 1.21.4.8 yamt TMPFS_POOL_PUT(&tmp->tm_node_pool, node);
248 1.21.4.2 yamt }
249 1.21.4.2 yamt
250 1.21.4.2 yamt /* --------------------------------------------------------------------- */
251 1.21.4.2 yamt
252 1.21.4.2 yamt /*
253 1.21.4.2 yamt * Allocates a new directory entry for the node node with a name of name.
254 1.21.4.2 yamt * The new directory entry is returned in *de.
255 1.21.4.2 yamt *
256 1.21.4.2 yamt * The link count of node is increased by one to reflect the new object
257 1.21.4.3 yamt * referencing it. This takes care of notifying kqueue listeners about
258 1.21.4.3 yamt * this change.
259 1.21.4.2 yamt *
260 1.21.4.2 yamt * Returns zero on success or an appropriate error code on failure.
261 1.21.4.2 yamt */
262 1.21.4.2 yamt int
263 1.21.4.2 yamt tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
264 1.21.4.2 yamt const char *name, uint16_t len, struct tmpfs_dirent **de)
265 1.21.4.2 yamt {
266 1.21.4.2 yamt struct tmpfs_dirent *nde;
267 1.21.4.2 yamt
268 1.21.4.2 yamt nde = (struct tmpfs_dirent *)TMPFS_POOL_GET(&tmp->tm_dirent_pool, 0);
269 1.21.4.2 yamt if (nde == NULL)
270 1.21.4.2 yamt return ENOSPC;
271 1.21.4.2 yamt
272 1.21.4.2 yamt nde->td_name = tmpfs_str_pool_get(&tmp->tm_str_pool, len, 0);
273 1.21.4.2 yamt if (nde->td_name == NULL) {
274 1.21.4.2 yamt TMPFS_POOL_PUT(&tmp->tm_dirent_pool, nde);
275 1.21.4.2 yamt return ENOSPC;
276 1.21.4.2 yamt }
277 1.21.4.2 yamt nde->td_namelen = len;
278 1.21.4.2 yamt memcpy(nde->td_name, name, len);
279 1.21.4.2 yamt nde->td_node = node;
280 1.21.4.2 yamt
281 1.21.4.2 yamt node->tn_links++;
282 1.21.4.3 yamt if (node->tn_links > 1 && node->tn_vnode != NULL)
283 1.21.4.3 yamt VN_KNOTE(node->tn_vnode, NOTE_LINK);
284 1.21.4.2 yamt *de = nde;
285 1.21.4.2 yamt
286 1.21.4.2 yamt return 0;
287 1.21.4.2 yamt }
288 1.21.4.2 yamt
289 1.21.4.2 yamt /* --------------------------------------------------------------------- */
290 1.21.4.2 yamt
291 1.21.4.2 yamt /*
292 1.21.4.2 yamt * Frees a directory entry. It is the caller's responsibility to destroy
293 1.21.4.2 yamt * the node referenced by it if needed.
294 1.21.4.2 yamt *
295 1.21.4.2 yamt * The link count of node is decreased by one to reflect the removal of an
296 1.21.4.2 yamt * object that referenced it. This only happens if 'node_exists' is true;
297 1.21.4.2 yamt * otherwise the function will not access the node referred to by the
298 1.21.4.2 yamt * directory entry, as it may already have been released from the outside.
299 1.21.4.3 yamt *
300 1.21.4.3 yamt * Interested parties (kqueue) are notified of the link count change; note
301 1.21.4.3 yamt * that this can include both the node pointed to by the directory entry
302 1.21.4.3 yamt * as well as its parent.
303 1.21.4.2 yamt */
304 1.21.4.2 yamt void
305 1.21.4.2 yamt tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
306 1.21.4.4 yamt bool node_exists)
307 1.21.4.2 yamt {
308 1.21.4.2 yamt if (node_exists) {
309 1.21.4.2 yamt struct tmpfs_node *node;
310 1.21.4.2 yamt
311 1.21.4.2 yamt node = de->td_node;
312 1.21.4.2 yamt
313 1.21.4.2 yamt KASSERT(node->tn_links > 0);
314 1.21.4.2 yamt node->tn_links--;
315 1.21.4.3 yamt if (node->tn_vnode != NULL)
316 1.21.4.3 yamt VN_KNOTE(node->tn_vnode, node->tn_links == 0 ?
317 1.21.4.3 yamt NOTE_DELETE : NOTE_LINK);
318 1.21.4.3 yamt if (node->tn_type == VDIR)
319 1.21.4.3 yamt VN_KNOTE(node->tn_spec.tn_dir.tn_parent->tn_vnode,
320 1.21.4.3 yamt NOTE_LINK);
321 1.21.4.2 yamt }
322 1.21.4.2 yamt
323 1.21.4.2 yamt tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name, de->td_namelen);
324 1.21.4.2 yamt TMPFS_POOL_PUT(&tmp->tm_dirent_pool, de);
325 1.21.4.2 yamt }
326 1.21.4.2 yamt
327 1.21.4.2 yamt /* --------------------------------------------------------------------- */
328 1.21.4.2 yamt
329 1.21.4.2 yamt /*
330 1.21.4.2 yamt * Allocates a new vnode for the node node or returns a new reference to
331 1.21.4.2 yamt * an existing one if the node had already a vnode referencing it. The
332 1.21.4.2 yamt * resulting locked vnode is returned in *vpp.
333 1.21.4.2 yamt *
334 1.21.4.2 yamt * Returns zero on success or an appropriate error code on failure.
335 1.21.4.2 yamt */
336 1.21.4.2 yamt int
337 1.21.4.2 yamt tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp)
338 1.21.4.2 yamt {
339 1.21.4.2 yamt int error;
340 1.21.4.2 yamt struct vnode *vp;
341 1.21.4.2 yamt
342 1.21.4.8 yamt /* If there is already a vnode, then lock it. */
343 1.21.4.8 yamt for (;;) {
344 1.21.4.8 yamt mutex_enter(&node->tn_vlock);
345 1.21.4.8 yamt if ((vp = node->tn_vnode) != NULL) {
346 1.21.4.8 yamt mutex_enter(&vp->v_interlock);
347 1.21.4.8 yamt mutex_exit(&node->tn_vlock);
348 1.21.4.8 yamt error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK);
349 1.21.4.8 yamt if (error == ENOENT) {
350 1.21.4.8 yamt /* vnode was reclaimed. */
351 1.21.4.8 yamt continue;
352 1.21.4.8 yamt }
353 1.21.4.8 yamt *vpp = vp;
354 1.21.4.8 yamt return error;
355 1.21.4.8 yamt }
356 1.21.4.8 yamt break;
357 1.21.4.2 yamt }
358 1.21.4.2 yamt
359 1.21.4.2 yamt /* Get a new vnode and associate it with our node. */
360 1.21.4.2 yamt error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, &vp);
361 1.21.4.8 yamt if (error != 0) {
362 1.21.4.8 yamt mutex_exit(&node->tn_vlock);
363 1.21.4.8 yamt return error;
364 1.21.4.8 yamt }
365 1.21.4.2 yamt
366 1.21.4.2 yamt error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
367 1.21.4.2 yamt if (error != 0) {
368 1.21.4.8 yamt mutex_exit(&node->tn_vlock);
369 1.21.4.2 yamt ungetnewvnode(vp);
370 1.21.4.8 yamt return error;
371 1.21.4.2 yamt }
372 1.21.4.2 yamt
373 1.21.4.2 yamt vp->v_type = node->tn_type;
374 1.21.4.2 yamt
375 1.21.4.2 yamt /* Type-specific initialization. */
376 1.21.4.2 yamt switch (node->tn_type) {
377 1.21.4.2 yamt case VBLK:
378 1.21.4.2 yamt /* FALLTHROUGH */
379 1.21.4.2 yamt case VCHR:
380 1.21.4.2 yamt vp->v_op = tmpfs_specop_p;
381 1.21.4.9 yamt spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
382 1.21.4.2 yamt break;
383 1.21.4.2 yamt
384 1.21.4.2 yamt case VDIR:
385 1.21.4.6 yamt vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
386 1.21.4.6 yamt VV_ROOT : 0;
387 1.21.4.2 yamt break;
388 1.21.4.2 yamt
389 1.21.4.2 yamt case VFIFO:
390 1.21.4.2 yamt vp->v_op = tmpfs_fifoop_p;
391 1.21.4.2 yamt break;
392 1.21.4.2 yamt
393 1.21.4.2 yamt case VLNK:
394 1.21.4.2 yamt /* FALLTHROUGH */
395 1.21.4.2 yamt case VREG:
396 1.21.4.2 yamt /* FALLTHROUGH */
397 1.21.4.2 yamt case VSOCK:
398 1.21.4.2 yamt break;
399 1.21.4.2 yamt
400 1.21.4.2 yamt default:
401 1.21.4.2 yamt KASSERT(0);
402 1.21.4.2 yamt }
403 1.21.4.2 yamt
404 1.21.4.2 yamt uvm_vnp_setsize(vp, node->tn_size);
405 1.21.4.8 yamt vp->v_data = node;
406 1.21.4.8 yamt node->tn_vnode = vp;
407 1.21.4.8 yamt mutex_exit(&node->tn_vlock);
408 1.21.4.8 yamt *vpp = vp;
409 1.21.4.2 yamt
410 1.21.4.2 yamt KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
411 1.21.4.2 yamt KASSERT(*vpp == node->tn_vnode);
412 1.21.4.2 yamt
413 1.21.4.2 yamt return error;
414 1.21.4.2 yamt }
415 1.21.4.2 yamt
416 1.21.4.2 yamt /* --------------------------------------------------------------------- */
417 1.21.4.2 yamt
418 1.21.4.2 yamt /*
419 1.21.4.2 yamt * Destroys the association between the vnode vp and the node it
420 1.21.4.2 yamt * references.
421 1.21.4.2 yamt */
422 1.21.4.2 yamt void
423 1.21.4.2 yamt tmpfs_free_vp(struct vnode *vp)
424 1.21.4.2 yamt {
425 1.21.4.2 yamt struct tmpfs_node *node;
426 1.21.4.2 yamt
427 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
428 1.21.4.2 yamt
429 1.21.4.8 yamt mutex_enter(&node->tn_vlock);
430 1.21.4.2 yamt node->tn_vnode = NULL;
431 1.21.4.8 yamt mutex_exit(&node->tn_vlock);
432 1.21.4.2 yamt vp->v_data = NULL;
433 1.21.4.2 yamt }
434 1.21.4.2 yamt
435 1.21.4.2 yamt /* --------------------------------------------------------------------- */
436 1.21.4.2 yamt
437 1.21.4.2 yamt /*
438 1.21.4.2 yamt * Allocates a new file of type 'type' and adds it to the parent directory
439 1.21.4.2 yamt * 'dvp'; this addition is done using the component name given in 'cnp'.
440 1.21.4.2 yamt * The ownership of the new file is automatically assigned based on the
441 1.21.4.2 yamt * credentials of the caller (through 'cnp'), the group is set based on
442 1.21.4.2 yamt * the parent directory and the mode is determined from the 'vap' argument.
443 1.21.4.2 yamt * If successful, *vpp holds a vnode to the newly created file and zero
444 1.21.4.2 yamt * is returned. Otherwise *vpp is NULL and the function returns an
445 1.21.4.2 yamt * appropriate error code.
446 1.21.4.2 yamt */
447 1.21.4.2 yamt int
448 1.21.4.2 yamt tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
449 1.21.4.2 yamt struct componentname *cnp, char *target)
450 1.21.4.2 yamt {
451 1.21.4.2 yamt int error;
452 1.21.4.2 yamt struct tmpfs_dirent *de;
453 1.21.4.2 yamt struct tmpfs_mount *tmp;
454 1.21.4.2 yamt struct tmpfs_node *dnode;
455 1.21.4.2 yamt struct tmpfs_node *node;
456 1.21.4.2 yamt struct tmpfs_node *parent;
457 1.21.4.2 yamt
458 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(dvp));
459 1.21.4.2 yamt KASSERT(cnp->cn_flags & HASBUF);
460 1.21.4.2 yamt
461 1.21.4.2 yamt tmp = VFS_TO_TMPFS(dvp->v_mount);
462 1.21.4.2 yamt dnode = VP_TO_TMPFS_DIR(dvp);
463 1.21.4.2 yamt *vpp = NULL;
464 1.21.4.2 yamt
465 1.21.4.2 yamt /* If the entry we are creating is a directory, we cannot overflow
466 1.21.4.2 yamt * the number of links of its parent, because it will get a new
467 1.21.4.2 yamt * link. */
468 1.21.4.2 yamt if (vap->va_type == VDIR) {
469 1.21.4.2 yamt /* Ensure that we do not overflow the maximum number of links
470 1.21.4.2 yamt * imposed by the system. */
471 1.21.4.2 yamt KASSERT(dnode->tn_links <= LINK_MAX);
472 1.21.4.2 yamt if (dnode->tn_links == LINK_MAX) {
473 1.21.4.2 yamt error = EMLINK;
474 1.21.4.2 yamt goto out;
475 1.21.4.2 yamt }
476 1.21.4.2 yamt
477 1.21.4.2 yamt parent = dnode;
478 1.21.4.2 yamt } else
479 1.21.4.2 yamt parent = NULL;
480 1.21.4.2 yamt
481 1.21.4.2 yamt /* Allocate a node that represents the new file. */
482 1.21.4.2 yamt error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
483 1.21.4.8 yamt dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
484 1.21.4.2 yamt if (error != 0)
485 1.21.4.2 yamt goto out;
486 1.21.4.2 yamt
487 1.21.4.2 yamt /* Allocate a directory entry that points to the new file. */
488 1.21.4.2 yamt error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
489 1.21.4.2 yamt &de);
490 1.21.4.2 yamt if (error != 0) {
491 1.21.4.2 yamt tmpfs_free_node(tmp, node);
492 1.21.4.2 yamt goto out;
493 1.21.4.2 yamt }
494 1.21.4.2 yamt
495 1.21.4.2 yamt /* Allocate a vnode for the new file. */
496 1.21.4.2 yamt error = tmpfs_alloc_vp(dvp->v_mount, node, vpp);
497 1.21.4.2 yamt if (error != 0) {
498 1.21.4.4 yamt tmpfs_free_dirent(tmp, de, true);
499 1.21.4.2 yamt tmpfs_free_node(tmp, node);
500 1.21.4.2 yamt goto out;
501 1.21.4.2 yamt }
502 1.21.4.2 yamt
503 1.21.4.2 yamt /* Now that all required items are allocated, we can proceed to
504 1.21.4.2 yamt * insert the new node into the directory, an operation that
505 1.21.4.2 yamt * cannot fail. */
506 1.21.4.2 yamt tmpfs_dir_attach(dvp, de);
507 1.21.4.8 yamt if (vap->va_type == VDIR) {
508 1.21.4.8 yamt VN_KNOTE(dvp, NOTE_LINK);
509 1.21.4.8 yamt dnode->tn_links++;
510 1.21.4.8 yamt KASSERT(dnode->tn_links <= LINK_MAX);
511 1.21.4.8 yamt }
512 1.21.4.2 yamt
513 1.21.4.2 yamt out:
514 1.21.4.2 yamt if (error != 0 || !(cnp->cn_flags & SAVESTART))
515 1.21.4.2 yamt PNBUF_PUT(cnp->cn_pnbuf);
516 1.21.4.2 yamt vput(dvp);
517 1.21.4.2 yamt
518 1.21.4.2 yamt KASSERT(IFF(error == 0, *vpp != NULL));
519 1.21.4.2 yamt
520 1.21.4.2 yamt return error;
521 1.21.4.2 yamt }
522 1.21.4.2 yamt
523 1.21.4.2 yamt /* --------------------------------------------------------------------- */
524 1.21.4.2 yamt
525 1.21.4.2 yamt /*
526 1.21.4.2 yamt * Attaches the directory entry de to the directory represented by vp.
527 1.21.4.2 yamt * Note that this does not change the link count of the node pointed by
528 1.21.4.2 yamt * the directory entry, as this is done by tmpfs_alloc_dirent.
529 1.21.4.3 yamt *
530 1.21.4.3 yamt * As the "parent" directory changes, interested parties are notified of
531 1.21.4.3 yamt * a write to it.
532 1.21.4.2 yamt */
533 1.21.4.2 yamt void
534 1.21.4.2 yamt tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
535 1.21.4.2 yamt {
536 1.21.4.2 yamt struct tmpfs_node *dnode;
537 1.21.4.2 yamt
538 1.21.4.2 yamt dnode = VP_TO_TMPFS_DIR(vp);
539 1.21.4.2 yamt
540 1.21.4.2 yamt TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
541 1.21.4.2 yamt dnode->tn_size += sizeof(struct tmpfs_dirent);
542 1.21.4.2 yamt dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
543 1.21.4.2 yamt TMPFS_NODE_MODIFIED;
544 1.21.4.2 yamt uvm_vnp_setsize(vp, dnode->tn_size);
545 1.21.4.3 yamt
546 1.21.4.3 yamt VN_KNOTE(vp, NOTE_WRITE);
547 1.21.4.2 yamt }
548 1.21.4.2 yamt
549 1.21.4.2 yamt /* --------------------------------------------------------------------- */
550 1.21.4.2 yamt
551 1.21.4.2 yamt /*
552 1.21.4.2 yamt * Detaches the directory entry de from the directory represented by vp.
553 1.21.4.2 yamt * Note that this does not change the link count of the node pointed by
554 1.21.4.2 yamt * the directory entry, as this is done by tmpfs_free_dirent.
555 1.21.4.3 yamt *
556 1.21.4.3 yamt * As the "parent" directory changes, interested parties are notified of
557 1.21.4.3 yamt * a write to it.
558 1.21.4.2 yamt */
559 1.21.4.2 yamt void
560 1.21.4.2 yamt tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
561 1.21.4.2 yamt {
562 1.21.4.2 yamt struct tmpfs_node *dnode;
563 1.21.4.2 yamt
564 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
565 1.21.4.2 yamt
566 1.21.4.2 yamt dnode = VP_TO_TMPFS_DIR(vp);
567 1.21.4.2 yamt
568 1.21.4.2 yamt if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
569 1.21.4.2 yamt dnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
570 1.21.4.2 yamt dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
571 1.21.4.2 yamt }
572 1.21.4.2 yamt
573 1.21.4.2 yamt TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
574 1.21.4.2 yamt dnode->tn_size -= sizeof(struct tmpfs_dirent);
575 1.21.4.2 yamt dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
576 1.21.4.2 yamt TMPFS_NODE_MODIFIED;
577 1.21.4.2 yamt uvm_vnp_setsize(vp, dnode->tn_size);
578 1.21.4.3 yamt
579 1.21.4.3 yamt VN_KNOTE(vp, NOTE_WRITE);
580 1.21.4.2 yamt }
581 1.21.4.2 yamt
582 1.21.4.2 yamt /* --------------------------------------------------------------------- */
583 1.21.4.2 yamt
584 1.21.4.2 yamt /*
585 1.21.4.2 yamt * Looks for a directory entry in the directory represented by node.
586 1.21.4.2 yamt * 'cnp' describes the name of the entry to look for. Note that the .
587 1.21.4.2 yamt * and .. components are not allowed as they do not physically exist
588 1.21.4.2 yamt * within directories.
589 1.21.4.2 yamt *
590 1.21.4.2 yamt * Returns a pointer to the entry when found, otherwise NULL.
591 1.21.4.2 yamt */
592 1.21.4.2 yamt struct tmpfs_dirent *
593 1.21.4.2 yamt tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp)
594 1.21.4.2 yamt {
595 1.21.4.4 yamt bool found;
596 1.21.4.2 yamt struct tmpfs_dirent *de;
597 1.21.4.2 yamt
598 1.21.4.2 yamt KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
599 1.21.4.2 yamt KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
600 1.21.4.2 yamt cnp->cn_nameptr[1] == '.')));
601 1.21.4.2 yamt TMPFS_VALIDATE_DIR(node);
602 1.21.4.2 yamt
603 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_ACCESSED;
604 1.21.4.2 yamt
605 1.21.4.2 yamt found = 0;
606 1.21.4.2 yamt TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
607 1.21.4.2 yamt KASSERT(cnp->cn_namelen < 0xffff);
608 1.21.4.2 yamt if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
609 1.21.4.2 yamt memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
610 1.21.4.2 yamt found = 1;
611 1.21.4.2 yamt break;
612 1.21.4.2 yamt }
613 1.21.4.2 yamt }
614 1.21.4.2 yamt
615 1.21.4.2 yamt return found ? de : NULL;
616 1.21.4.2 yamt }
617 1.21.4.2 yamt
618 1.21.4.2 yamt /* --------------------------------------------------------------------- */
619 1.21.4.2 yamt
620 1.21.4.2 yamt /*
621 1.21.4.2 yamt * Helper function for tmpfs_readdir. Creates a '.' entry for the given
622 1.21.4.2 yamt * directory and returns it in the uio space. The function returns 0
623 1.21.4.2 yamt * on success, -1 if there was not enough space in the uio structure to
624 1.21.4.2 yamt * hold the directory entry or an appropriate error code if another
625 1.21.4.2 yamt * error happens.
626 1.21.4.2 yamt */
627 1.21.4.2 yamt int
628 1.21.4.2 yamt tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
629 1.21.4.2 yamt {
630 1.21.4.2 yamt int error;
631 1.21.4.6 yamt struct dirent *dentp;
632 1.21.4.2 yamt
633 1.21.4.2 yamt TMPFS_VALIDATE_DIR(node);
634 1.21.4.2 yamt KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
635 1.21.4.2 yamt
636 1.21.4.8 yamt dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
637 1.21.4.6 yamt
638 1.21.4.6 yamt dentp->d_fileno = node->tn_id;
639 1.21.4.6 yamt dentp->d_type = DT_DIR;
640 1.21.4.6 yamt dentp->d_namlen = 1;
641 1.21.4.6 yamt dentp->d_name[0] = '.';
642 1.21.4.6 yamt dentp->d_name[1] = '\0';
643 1.21.4.6 yamt dentp->d_reclen = _DIRENT_SIZE(dentp);
644 1.21.4.2 yamt
645 1.21.4.6 yamt if (dentp->d_reclen > uio->uio_resid)
646 1.21.4.2 yamt error = -1;
647 1.21.4.2 yamt else {
648 1.21.4.6 yamt error = uiomove(dentp, dentp->d_reclen, uio);
649 1.21.4.2 yamt if (error == 0)
650 1.21.4.2 yamt uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
651 1.21.4.2 yamt }
652 1.21.4.2 yamt
653 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_ACCESSED;
654 1.21.4.2 yamt
655 1.21.4.8 yamt kmem_free(dentp, sizeof(struct dirent));
656 1.21.4.2 yamt return error;
657 1.21.4.2 yamt }
658 1.21.4.2 yamt
659 1.21.4.2 yamt /* --------------------------------------------------------------------- */
660 1.21.4.2 yamt
661 1.21.4.2 yamt /*
662 1.21.4.2 yamt * Helper function for tmpfs_readdir. Creates a '..' entry for the given
663 1.21.4.2 yamt * directory and returns it in the uio space. The function returns 0
664 1.21.4.2 yamt * on success, -1 if there was not enough space in the uio structure to
665 1.21.4.2 yamt * hold the directory entry or an appropriate error code if another
666 1.21.4.2 yamt * error happens.
667 1.21.4.2 yamt */
668 1.21.4.2 yamt int
669 1.21.4.2 yamt tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
670 1.21.4.2 yamt {
671 1.21.4.2 yamt int error;
672 1.21.4.6 yamt struct dirent *dentp;
673 1.21.4.2 yamt
674 1.21.4.2 yamt TMPFS_VALIDATE_DIR(node);
675 1.21.4.2 yamt KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
676 1.21.4.2 yamt
677 1.21.4.8 yamt dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
678 1.21.4.6 yamt
679 1.21.4.6 yamt dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
680 1.21.4.6 yamt dentp->d_type = DT_DIR;
681 1.21.4.6 yamt dentp->d_namlen = 2;
682 1.21.4.6 yamt dentp->d_name[0] = '.';
683 1.21.4.6 yamt dentp->d_name[1] = '.';
684 1.21.4.6 yamt dentp->d_name[2] = '\0';
685 1.21.4.6 yamt dentp->d_reclen = _DIRENT_SIZE(dentp);
686 1.21.4.2 yamt
687 1.21.4.6 yamt if (dentp->d_reclen > uio->uio_resid)
688 1.21.4.2 yamt error = -1;
689 1.21.4.2 yamt else {
690 1.21.4.6 yamt error = uiomove(dentp, dentp->d_reclen, uio);
691 1.21.4.2 yamt if (error == 0) {
692 1.21.4.2 yamt struct tmpfs_dirent *de;
693 1.21.4.2 yamt
694 1.21.4.2 yamt de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
695 1.21.4.2 yamt if (de == NULL)
696 1.21.4.2 yamt uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
697 1.21.4.2 yamt else
698 1.21.4.3 yamt uio->uio_offset = tmpfs_dircookie(de);
699 1.21.4.2 yamt }
700 1.21.4.2 yamt }
701 1.21.4.2 yamt
702 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_ACCESSED;
703 1.21.4.2 yamt
704 1.21.4.8 yamt kmem_free(dentp, sizeof(struct dirent));
705 1.21.4.2 yamt return error;
706 1.21.4.2 yamt }
707 1.21.4.2 yamt
708 1.21.4.2 yamt /* --------------------------------------------------------------------- */
709 1.21.4.2 yamt
710 1.21.4.2 yamt /*
711 1.21.4.2 yamt * Lookup a directory entry by its associated cookie.
712 1.21.4.2 yamt */
713 1.21.4.2 yamt struct tmpfs_dirent *
714 1.21.4.2 yamt tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
715 1.21.4.2 yamt {
716 1.21.4.2 yamt struct tmpfs_dirent *de;
717 1.21.4.2 yamt
718 1.21.4.2 yamt if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn &&
719 1.21.4.2 yamt node->tn_spec.tn_dir.tn_readdir_lastp != NULL) {
720 1.21.4.2 yamt return node->tn_spec.tn_dir.tn_readdir_lastp;
721 1.21.4.2 yamt }
722 1.21.4.2 yamt
723 1.21.4.2 yamt TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
724 1.21.4.3 yamt if (tmpfs_dircookie(de) == cookie) {
725 1.21.4.2 yamt break;
726 1.21.4.2 yamt }
727 1.21.4.2 yamt }
728 1.21.4.2 yamt
729 1.21.4.2 yamt return de;
730 1.21.4.2 yamt }
731 1.21.4.2 yamt
732 1.21.4.2 yamt /* --------------------------------------------------------------------- */
733 1.21.4.2 yamt
734 1.21.4.2 yamt /*
735 1.21.4.2 yamt * Helper function for tmpfs_readdir. Returns as much directory entries
736 1.21.4.2 yamt * as can fit in the uio space. The read starts at uio->uio_offset.
737 1.21.4.2 yamt * The function returns 0 on success, -1 if there was not enough space
738 1.21.4.2 yamt * in the uio structure to hold the directory entry or an appropriate
739 1.21.4.2 yamt * error code if another error happens.
740 1.21.4.2 yamt */
741 1.21.4.2 yamt int
742 1.21.4.2 yamt tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
743 1.21.4.2 yamt {
744 1.21.4.2 yamt int error;
745 1.21.4.2 yamt off_t startcookie;
746 1.21.4.6 yamt struct dirent *dentp;
747 1.21.4.2 yamt struct tmpfs_dirent *de;
748 1.21.4.2 yamt
749 1.21.4.2 yamt TMPFS_VALIDATE_DIR(node);
750 1.21.4.2 yamt
751 1.21.4.2 yamt /* Locate the first directory entry we have to return. We have cached
752 1.21.4.2 yamt * the last readdir in the node, so use those values if appropriate.
753 1.21.4.2 yamt * Otherwise do a linear scan to find the requested entry. */
754 1.21.4.2 yamt startcookie = uio->uio_offset;
755 1.21.4.2 yamt KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
756 1.21.4.2 yamt KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
757 1.21.4.2 yamt if (startcookie == TMPFS_DIRCOOKIE_EOF) {
758 1.21.4.2 yamt return 0;
759 1.21.4.2 yamt } else {
760 1.21.4.2 yamt de = tmpfs_dir_lookupbycookie(node, startcookie);
761 1.21.4.2 yamt }
762 1.21.4.2 yamt if (de == NULL) {
763 1.21.4.2 yamt return EINVAL;
764 1.21.4.2 yamt }
765 1.21.4.2 yamt
766 1.21.4.8 yamt dentp = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
767 1.21.4.6 yamt
768 1.21.4.2 yamt /* Read as much entries as possible; i.e., until we reach the end of
769 1.21.4.2 yamt * the directory or we exhaust uio space. */
770 1.21.4.2 yamt do {
771 1.21.4.2 yamt /* Create a dirent structure representing the current
772 1.21.4.2 yamt * tmpfs_node and fill it. */
773 1.21.4.6 yamt dentp->d_fileno = de->td_node->tn_id;
774 1.21.4.2 yamt switch (de->td_node->tn_type) {
775 1.21.4.2 yamt case VBLK:
776 1.21.4.6 yamt dentp->d_type = DT_BLK;
777 1.21.4.2 yamt break;
778 1.21.4.2 yamt
779 1.21.4.2 yamt case VCHR:
780 1.21.4.6 yamt dentp->d_type = DT_CHR;
781 1.21.4.2 yamt break;
782 1.21.4.2 yamt
783 1.21.4.2 yamt case VDIR:
784 1.21.4.6 yamt dentp->d_type = DT_DIR;
785 1.21.4.2 yamt break;
786 1.21.4.2 yamt
787 1.21.4.2 yamt case VFIFO:
788 1.21.4.6 yamt dentp->d_type = DT_FIFO;
789 1.21.4.2 yamt break;
790 1.21.4.2 yamt
791 1.21.4.2 yamt case VLNK:
792 1.21.4.6 yamt dentp->d_type = DT_LNK;
793 1.21.4.2 yamt break;
794 1.21.4.2 yamt
795 1.21.4.2 yamt case VREG:
796 1.21.4.6 yamt dentp->d_type = DT_REG;
797 1.21.4.2 yamt break;
798 1.21.4.2 yamt
799 1.21.4.2 yamt case VSOCK:
800 1.21.4.6 yamt dentp->d_type = DT_SOCK;
801 1.21.4.2 yamt break;
802 1.21.4.2 yamt
803 1.21.4.2 yamt default:
804 1.21.4.2 yamt KASSERT(0);
805 1.21.4.2 yamt }
806 1.21.4.6 yamt dentp->d_namlen = de->td_namelen;
807 1.21.4.6 yamt KASSERT(de->td_namelen < sizeof(dentp->d_name));
808 1.21.4.6 yamt (void)memcpy(dentp->d_name, de->td_name, de->td_namelen);
809 1.21.4.6 yamt dentp->d_name[de->td_namelen] = '\0';
810 1.21.4.6 yamt dentp->d_reclen = _DIRENT_SIZE(dentp);
811 1.21.4.2 yamt
812 1.21.4.2 yamt /* Stop reading if the directory entry we are treating is
813 1.21.4.2 yamt * bigger than the amount of data that can be returned. */
814 1.21.4.6 yamt if (dentp->d_reclen > uio->uio_resid) {
815 1.21.4.2 yamt error = -1;
816 1.21.4.2 yamt break;
817 1.21.4.2 yamt }
818 1.21.4.2 yamt
819 1.21.4.2 yamt /* Copy the new dirent structure into the output buffer and
820 1.21.4.2 yamt * advance pointers. */
821 1.21.4.6 yamt error = uiomove(dentp, dentp->d_reclen, uio);
822 1.21.4.2 yamt
823 1.21.4.2 yamt (*cntp)++;
824 1.21.4.2 yamt de = TAILQ_NEXT(de, td_entries);
825 1.21.4.2 yamt } while (error == 0 && uio->uio_resid > 0 && de != NULL);
826 1.21.4.2 yamt
827 1.21.4.2 yamt /* Update the offset and cache. */
828 1.21.4.2 yamt if (de == NULL) {
829 1.21.4.2 yamt uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
830 1.21.4.2 yamt node->tn_spec.tn_dir.tn_readdir_lastn = 0;
831 1.21.4.2 yamt node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
832 1.21.4.2 yamt } else {
833 1.21.4.2 yamt node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset =
834 1.21.4.3 yamt tmpfs_dircookie(de);
835 1.21.4.2 yamt node->tn_spec.tn_dir.tn_readdir_lastp = de;
836 1.21.4.2 yamt }
837 1.21.4.2 yamt
838 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_ACCESSED;
839 1.21.4.2 yamt
840 1.21.4.8 yamt kmem_free(dentp, sizeof(struct dirent));
841 1.21.4.2 yamt return error;
842 1.21.4.2 yamt }
843 1.21.4.2 yamt
844 1.21.4.2 yamt /* --------------------------------------------------------------------- */
845 1.21.4.2 yamt
846 1.21.4.2 yamt /*
847 1.21.4.2 yamt * Resizes the aobj associated to the regular file pointed to by vp to
848 1.21.4.2 yamt * the size newsize. 'vp' must point to a vnode that represents a regular
849 1.21.4.2 yamt * file. 'newsize' must be positive.
850 1.21.4.2 yamt *
851 1.21.4.3 yamt * If the file is extended, the appropriate kevent is raised. This does
852 1.21.4.3 yamt * not rise a write event though because resizing is not the same as
853 1.21.4.3 yamt * writing.
854 1.21.4.3 yamt *
855 1.21.4.2 yamt * Returns zero on success or an appropriate error code on failure.
856 1.21.4.2 yamt */
857 1.21.4.2 yamt int
858 1.21.4.2 yamt tmpfs_reg_resize(struct vnode *vp, off_t newsize)
859 1.21.4.2 yamt {
860 1.21.4.2 yamt int error;
861 1.21.4.10 yamt unsigned int newpages, oldpages;
862 1.21.4.2 yamt struct tmpfs_mount *tmp;
863 1.21.4.2 yamt struct tmpfs_node *node;
864 1.21.4.2 yamt off_t oldsize;
865 1.21.4.2 yamt
866 1.21.4.2 yamt KASSERT(vp->v_type == VREG);
867 1.21.4.2 yamt KASSERT(newsize >= 0);
868 1.21.4.2 yamt
869 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
870 1.21.4.2 yamt tmp = VFS_TO_TMPFS(vp->v_mount);
871 1.21.4.2 yamt
872 1.21.4.2 yamt /* Convert the old and new sizes to the number of pages needed to
873 1.21.4.2 yamt * store them. It may happen that we do not need to do anything
874 1.21.4.2 yamt * because the last allocated page can accommodate the change on
875 1.21.4.2 yamt * its own. */
876 1.21.4.2 yamt oldsize = node->tn_size;
877 1.21.4.2 yamt oldpages = round_page(oldsize) / PAGE_SIZE;
878 1.21.4.2 yamt KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
879 1.21.4.2 yamt newpages = round_page(newsize) / PAGE_SIZE;
880 1.21.4.2 yamt
881 1.21.4.2 yamt if (newpages > oldpages &&
882 1.21.4.8 yamt (ssize_t)(newpages - oldpages) > TMPFS_PAGES_AVAIL(tmp)) {
883 1.21.4.2 yamt error = ENOSPC;
884 1.21.4.2 yamt goto out;
885 1.21.4.2 yamt }
886 1.21.4.8 yamt atomic_add_int(&tmp->tm_pages_used, newpages - oldpages);
887 1.21.4.2 yamt
888 1.21.4.2 yamt if (newsize < oldsize) {
889 1.21.4.2 yamt int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
890 1.21.4.2 yamt
891 1.21.4.2 yamt /*
892 1.21.4.2 yamt * zero out the truncated part of the last page.
893 1.21.4.2 yamt */
894 1.21.4.2 yamt
895 1.21.4.2 yamt uvm_vnp_zerorange(vp, newsize, zerolen);
896 1.21.4.2 yamt }
897 1.21.4.2 yamt
898 1.21.4.5 yamt node->tn_spec.tn_reg.tn_aobj_pages = newpages;
899 1.21.4.5 yamt node->tn_size = newsize;
900 1.21.4.5 yamt uvm_vnp_setsize(vp, newsize);
901 1.21.4.5 yamt
902 1.21.4.8 yamt /*
903 1.21.4.8 yamt * free "backing store"
904 1.21.4.8 yamt */
905 1.21.4.8 yamt
906 1.21.4.8 yamt if (newpages < oldpages) {
907 1.21.4.8 yamt struct uvm_object *uobj;
908 1.21.4.8 yamt
909 1.21.4.8 yamt uobj = node->tn_spec.tn_reg.tn_aobj;
910 1.21.4.8 yamt
911 1.21.4.8 yamt mutex_enter(&uobj->vmobjlock);
912 1.21.4.8 yamt uao_dropswap_range(uobj, newpages, oldpages);
913 1.21.4.8 yamt mutex_exit(&uobj->vmobjlock);
914 1.21.4.8 yamt }
915 1.21.4.5 yamt
916 1.21.4.2 yamt error = 0;
917 1.21.4.2 yamt
918 1.21.4.3 yamt if (newsize > oldsize)
919 1.21.4.3 yamt VN_KNOTE(vp, NOTE_EXTEND);
920 1.21.4.3 yamt
921 1.21.4.2 yamt out:
922 1.21.4.2 yamt return error;
923 1.21.4.2 yamt }
924 1.21.4.2 yamt
925 1.21.4.2 yamt /* --------------------------------------------------------------------- */
926 1.21.4.2 yamt
927 1.21.4.2 yamt /*
928 1.21.4.2 yamt * Returns information about the number of available memory pages,
929 1.21.4.2 yamt * including physical and virtual ones.
930 1.21.4.2 yamt *
931 1.21.4.10 yamt * If 'total' is true, the value returned is the total amount of memory
932 1.21.4.2 yamt * pages configured for the system (either in use or free).
933 1.21.4.2 yamt * If it is FALSE, the value returned is the amount of free memory pages.
934 1.21.4.2 yamt *
935 1.21.4.2 yamt * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
936 1.21.4.2 yamt * excessive memory usage.
937 1.21.4.2 yamt *
938 1.21.4.2 yamt */
939 1.21.4.2 yamt size_t
940 1.21.4.4 yamt tmpfs_mem_info(bool total)
941 1.21.4.2 yamt {
942 1.21.4.2 yamt size_t size;
943 1.21.4.2 yamt
944 1.21.4.2 yamt size = 0;
945 1.21.4.2 yamt size += uvmexp.swpgavail;
946 1.21.4.2 yamt if (!total) {
947 1.21.4.2 yamt size -= uvmexp.swpgonly;
948 1.21.4.2 yamt }
949 1.21.4.2 yamt size += uvmexp.free;
950 1.21.4.2 yamt size += uvmexp.filepages;
951 1.21.4.2 yamt if (size > uvmexp.wired) {
952 1.21.4.2 yamt size -= uvmexp.wired;
953 1.21.4.2 yamt } else {
954 1.21.4.2 yamt size = 0;
955 1.21.4.2 yamt }
956 1.21.4.2 yamt
957 1.21.4.2 yamt return size;
958 1.21.4.2 yamt }
959 1.21.4.2 yamt
960 1.21.4.2 yamt /* --------------------------------------------------------------------- */
961 1.21.4.2 yamt
962 1.21.4.2 yamt /*
963 1.21.4.2 yamt * Change flags of the given vnode.
964 1.21.4.2 yamt * Caller should execute tmpfs_update on vp after a successful execution.
965 1.21.4.2 yamt * The vnode must be locked on entry and remain locked on exit.
966 1.21.4.2 yamt */
967 1.21.4.2 yamt int
968 1.21.4.3 yamt tmpfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
969 1.21.4.2 yamt {
970 1.21.4.2 yamt int error;
971 1.21.4.2 yamt struct tmpfs_node *node;
972 1.21.4.2 yamt
973 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
974 1.21.4.2 yamt
975 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
976 1.21.4.2 yamt
977 1.21.4.2 yamt /* Disallow this operation if the file system is mounted read-only. */
978 1.21.4.2 yamt if (vp->v_mount->mnt_flag & MNT_RDONLY)
979 1.21.4.2 yamt return EROFS;
980 1.21.4.2 yamt
981 1.21.4.2 yamt /* XXX: The following comes from UFS code, and can be found in
982 1.21.4.2 yamt * several other file systems. Shouldn't this be centralized
983 1.21.4.2 yamt * somewhere? */
984 1.21.4.2 yamt if (kauth_cred_geteuid(cred) != node->tn_uid &&
985 1.21.4.2 yamt (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
986 1.21.4.4 yamt NULL)))
987 1.21.4.2 yamt return error;
988 1.21.4.4 yamt if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0) {
989 1.21.4.2 yamt /* The super-user is only allowed to change flags if the file
990 1.21.4.2 yamt * wasn't protected before and the securelevel is zero. */
991 1.21.4.2 yamt if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) &&
992 1.21.4.4 yamt kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHSYSFLAGS,
993 1.21.4.4 yamt 0, NULL, NULL, NULL))
994 1.21.4.2 yamt return EPERM;
995 1.21.4.2 yamt node->tn_flags = flags;
996 1.21.4.2 yamt } else {
997 1.21.4.2 yamt /* Regular users can change flags provided they only want to
998 1.21.4.2 yamt * change user-specific ones, not those reserved for the
999 1.21.4.2 yamt * super-user. */
1000 1.21.4.2 yamt if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) ||
1001 1.21.4.2 yamt (flags & UF_SETTABLE) != flags)
1002 1.21.4.2 yamt return EPERM;
1003 1.21.4.2 yamt if ((node->tn_flags & SF_SETTABLE) != (flags & SF_SETTABLE))
1004 1.21.4.2 yamt return EPERM;
1005 1.21.4.2 yamt node->tn_flags &= SF_SETTABLE;
1006 1.21.4.2 yamt node->tn_flags |= (flags & UF_SETTABLE);
1007 1.21.4.2 yamt }
1008 1.21.4.2 yamt
1009 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_CHANGED;
1010 1.21.4.2 yamt VN_KNOTE(vp, NOTE_ATTRIB);
1011 1.21.4.2 yamt
1012 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1013 1.21.4.2 yamt
1014 1.21.4.2 yamt return 0;
1015 1.21.4.2 yamt }
1016 1.21.4.2 yamt
1017 1.21.4.2 yamt /* --------------------------------------------------------------------- */
1018 1.21.4.2 yamt
1019 1.21.4.2 yamt /*
1020 1.21.4.2 yamt * Change access mode on the given vnode.
1021 1.21.4.2 yamt * Caller should execute tmpfs_update on vp after a successful execution.
1022 1.21.4.2 yamt * The vnode must be locked on entry and remain locked on exit.
1023 1.21.4.2 yamt */
1024 1.21.4.2 yamt int
1025 1.21.4.3 yamt tmpfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
1026 1.21.4.2 yamt {
1027 1.21.4.2 yamt int error, ismember = 0;
1028 1.21.4.2 yamt struct tmpfs_node *node;
1029 1.21.4.2 yamt
1030 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1031 1.21.4.2 yamt
1032 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
1033 1.21.4.2 yamt
1034 1.21.4.2 yamt /* Disallow this operation if the file system is mounted read-only. */
1035 1.21.4.2 yamt if (vp->v_mount->mnt_flag & MNT_RDONLY)
1036 1.21.4.2 yamt return EROFS;
1037 1.21.4.2 yamt
1038 1.21.4.2 yamt /* Immutable or append-only files cannot be modified, either. */
1039 1.21.4.2 yamt if (node->tn_flags & (IMMUTABLE | APPEND))
1040 1.21.4.2 yamt return EPERM;
1041 1.21.4.2 yamt
1042 1.21.4.2 yamt /* XXX: The following comes from UFS code, and can be found in
1043 1.21.4.2 yamt * several other file systems. Shouldn't this be centralized
1044 1.21.4.2 yamt * somewhere? */
1045 1.21.4.2 yamt if (kauth_cred_geteuid(cred) != node->tn_uid &&
1046 1.21.4.2 yamt (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1047 1.21.4.4 yamt NULL)))
1048 1.21.4.2 yamt return error;
1049 1.21.4.4 yamt if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
1050 1.21.4.2 yamt if (vp->v_type != VDIR && (mode & S_ISTXT))
1051 1.21.4.2 yamt return EFTYPE;
1052 1.21.4.2 yamt
1053 1.21.4.2 yamt if ((kauth_cred_ismember_gid(cred, node->tn_gid,
1054 1.21.4.2 yamt &ismember) != 0 || !ismember) && (mode & S_ISGID))
1055 1.21.4.2 yamt return EPERM;
1056 1.21.4.2 yamt }
1057 1.21.4.2 yamt
1058 1.21.4.2 yamt node->tn_mode = (mode & ALLPERMS);
1059 1.21.4.2 yamt
1060 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_CHANGED;
1061 1.21.4.2 yamt VN_KNOTE(vp, NOTE_ATTRIB);
1062 1.21.4.2 yamt
1063 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1064 1.21.4.2 yamt
1065 1.21.4.2 yamt return 0;
1066 1.21.4.2 yamt }
1067 1.21.4.2 yamt
1068 1.21.4.2 yamt /* --------------------------------------------------------------------- */
1069 1.21.4.2 yamt
1070 1.21.4.2 yamt /*
1071 1.21.4.2 yamt * Change ownership of the given vnode. At least one of uid or gid must
1072 1.21.4.2 yamt * be different than VNOVAL. If one is set to that value, the attribute
1073 1.21.4.2 yamt * is unchanged.
1074 1.21.4.2 yamt * Caller should execute tmpfs_update on vp after a successful execution.
1075 1.21.4.2 yamt * The vnode must be locked on entry and remain locked on exit.
1076 1.21.4.2 yamt */
1077 1.21.4.2 yamt int
1078 1.21.4.2 yamt tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
1079 1.21.4.3 yamt struct lwp *l)
1080 1.21.4.2 yamt {
1081 1.21.4.2 yamt int error, ismember = 0;
1082 1.21.4.2 yamt struct tmpfs_node *node;
1083 1.21.4.2 yamt
1084 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1085 1.21.4.2 yamt
1086 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
1087 1.21.4.2 yamt
1088 1.21.4.2 yamt /* Assign default values if they are unknown. */
1089 1.21.4.2 yamt KASSERT(uid != VNOVAL || gid != VNOVAL);
1090 1.21.4.2 yamt if (uid == VNOVAL)
1091 1.21.4.2 yamt uid = node->tn_uid;
1092 1.21.4.2 yamt if (gid == VNOVAL)
1093 1.21.4.2 yamt gid = node->tn_gid;
1094 1.21.4.2 yamt KASSERT(uid != VNOVAL && gid != VNOVAL);
1095 1.21.4.2 yamt
1096 1.21.4.2 yamt /* Disallow this operation if the file system is mounted read-only. */
1097 1.21.4.2 yamt if (vp->v_mount->mnt_flag & MNT_RDONLY)
1098 1.21.4.2 yamt return EROFS;
1099 1.21.4.2 yamt
1100 1.21.4.2 yamt /* Immutable or append-only files cannot be modified, either. */
1101 1.21.4.2 yamt if (node->tn_flags & (IMMUTABLE | APPEND))
1102 1.21.4.2 yamt return EPERM;
1103 1.21.4.2 yamt
1104 1.21.4.2 yamt /* XXX: The following comes from UFS code, and can be found in
1105 1.21.4.2 yamt * several other file systems. Shouldn't this be centralized
1106 1.21.4.2 yamt * somewhere? */
1107 1.21.4.2 yamt if ((kauth_cred_geteuid(cred) != node->tn_uid || uid != node->tn_uid ||
1108 1.21.4.2 yamt (gid != node->tn_gid && !(kauth_cred_getegid(cred) == node->tn_gid ||
1109 1.21.4.3 yamt (kauth_cred_ismember_gid(cred, gid, &ismember) == 0 && ismember)))) &&
1110 1.21.4.2 yamt ((error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1111 1.21.4.4 yamt NULL)) != 0))
1112 1.21.4.2 yamt return error;
1113 1.21.4.2 yamt
1114 1.21.4.2 yamt node->tn_uid = uid;
1115 1.21.4.2 yamt node->tn_gid = gid;
1116 1.21.4.2 yamt
1117 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_CHANGED;
1118 1.21.4.2 yamt VN_KNOTE(vp, NOTE_ATTRIB);
1119 1.21.4.2 yamt
1120 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1121 1.21.4.2 yamt
1122 1.21.4.2 yamt return 0;
1123 1.21.4.2 yamt }
1124 1.21.4.2 yamt
1125 1.21.4.2 yamt /* --------------------------------------------------------------------- */
1126 1.21.4.2 yamt
1127 1.21.4.2 yamt /*
1128 1.21.4.2 yamt * Change size of the given vnode.
1129 1.21.4.2 yamt * Caller should execute tmpfs_update on vp after a successful execution.
1130 1.21.4.2 yamt * The vnode must be locked on entry and remain locked on exit.
1131 1.21.4.2 yamt */
1132 1.21.4.2 yamt int
1133 1.21.4.2 yamt tmpfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred,
1134 1.21.4.3 yamt struct lwp *l)
1135 1.21.4.2 yamt {
1136 1.21.4.2 yamt int error;
1137 1.21.4.2 yamt struct tmpfs_node *node;
1138 1.21.4.2 yamt
1139 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1140 1.21.4.2 yamt
1141 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
1142 1.21.4.2 yamt
1143 1.21.4.2 yamt /* Decide whether this is a valid operation based on the file type. */
1144 1.21.4.2 yamt error = 0;
1145 1.21.4.2 yamt switch (vp->v_type) {
1146 1.21.4.2 yamt case VDIR:
1147 1.21.4.2 yamt return EISDIR;
1148 1.21.4.2 yamt
1149 1.21.4.2 yamt case VREG:
1150 1.21.4.2 yamt if (vp->v_mount->mnt_flag & MNT_RDONLY)
1151 1.21.4.2 yamt return EROFS;
1152 1.21.4.2 yamt break;
1153 1.21.4.2 yamt
1154 1.21.4.2 yamt case VBLK:
1155 1.21.4.2 yamt /* FALLTHROUGH */
1156 1.21.4.2 yamt case VCHR:
1157 1.21.4.2 yamt /* FALLTHROUGH */
1158 1.21.4.2 yamt case VFIFO:
1159 1.21.4.2 yamt /* Allow modifications of special files even if in the file
1160 1.21.4.2 yamt * system is mounted read-only (we are not modifying the
1161 1.21.4.2 yamt * files themselves, but the objects they represent). */
1162 1.21.4.2 yamt return 0;
1163 1.21.4.2 yamt
1164 1.21.4.2 yamt default:
1165 1.21.4.2 yamt /* Anything else is unsupported. */
1166 1.21.4.2 yamt return EOPNOTSUPP;
1167 1.21.4.2 yamt }
1168 1.21.4.2 yamt
1169 1.21.4.2 yamt /* Immutable or append-only files cannot be modified, either. */
1170 1.21.4.2 yamt if (node->tn_flags & (IMMUTABLE | APPEND))
1171 1.21.4.2 yamt return EPERM;
1172 1.21.4.2 yamt
1173 1.21.4.2 yamt error = tmpfs_truncate(vp, size);
1174 1.21.4.2 yamt /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1175 1.21.4.2 yamt * for us, as will update tn_status; no need to do that here. */
1176 1.21.4.2 yamt
1177 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1178 1.21.4.2 yamt
1179 1.21.4.2 yamt return error;
1180 1.21.4.2 yamt }
1181 1.21.4.2 yamt
1182 1.21.4.2 yamt /* --------------------------------------------------------------------- */
1183 1.21.4.2 yamt
1184 1.21.4.2 yamt /*
1185 1.21.4.2 yamt * Change access and modification times of the given vnode.
1186 1.21.4.2 yamt * Caller should execute tmpfs_update on vp after a successful execution.
1187 1.21.4.2 yamt * The vnode must be locked on entry and remain locked on exit.
1188 1.21.4.2 yamt */
1189 1.21.4.2 yamt int
1190 1.21.4.2 yamt tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1191 1.21.4.2 yamt int vaflags, kauth_cred_t cred, struct lwp *l)
1192 1.21.4.2 yamt {
1193 1.21.4.2 yamt int error;
1194 1.21.4.2 yamt struct tmpfs_node *node;
1195 1.21.4.2 yamt
1196 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1197 1.21.4.2 yamt
1198 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
1199 1.21.4.2 yamt
1200 1.21.4.2 yamt /* Disallow this operation if the file system is mounted read-only. */
1201 1.21.4.2 yamt if (vp->v_mount->mnt_flag & MNT_RDONLY)
1202 1.21.4.2 yamt return EROFS;
1203 1.21.4.2 yamt
1204 1.21.4.2 yamt /* Immutable or append-only files cannot be modified, either. */
1205 1.21.4.2 yamt if (node->tn_flags & (IMMUTABLE | APPEND))
1206 1.21.4.2 yamt return EPERM;
1207 1.21.4.2 yamt
1208 1.21.4.2 yamt /* XXX: The following comes from UFS code, and can be found in
1209 1.21.4.2 yamt * several other file systems. Shouldn't this be centralized
1210 1.21.4.2 yamt * somewhere? */
1211 1.21.4.2 yamt if (kauth_cred_geteuid(cred) != node->tn_uid &&
1212 1.21.4.2 yamt (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
1213 1.21.4.4 yamt NULL)) && ((vaflags & VA_UTIMES_NULL) == 0 ||
1214 1.21.4.7 yamt (error = VOP_ACCESS(vp, VWRITE, cred))))
1215 1.21.4.2 yamt return error;
1216 1.21.4.2 yamt
1217 1.21.4.2 yamt if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1218 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_ACCESSED;
1219 1.21.4.2 yamt
1220 1.21.4.2 yamt if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1221 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_MODIFIED;
1222 1.21.4.2 yamt
1223 1.21.4.2 yamt tmpfs_update(vp, atime, mtime, 0);
1224 1.21.4.3 yamt VN_KNOTE(vp, NOTE_ATTRIB);
1225 1.21.4.2 yamt
1226 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1227 1.21.4.2 yamt
1228 1.21.4.2 yamt return 0;
1229 1.21.4.2 yamt }
1230 1.21.4.2 yamt
1231 1.21.4.2 yamt /* --------------------------------------------------------------------- */
1232 1.21.4.2 yamt
1233 1.21.4.2 yamt /* Sync timestamps */
1234 1.21.4.2 yamt void
1235 1.21.4.2 yamt tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1236 1.21.4.2 yamt const struct timespec *mod)
1237 1.21.4.2 yamt {
1238 1.21.4.2 yamt struct timespec now;
1239 1.21.4.2 yamt struct tmpfs_node *node;
1240 1.21.4.2 yamt
1241 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
1242 1.21.4.2 yamt
1243 1.21.4.2 yamt if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1244 1.21.4.2 yamt TMPFS_NODE_CHANGED)) == 0)
1245 1.21.4.2 yamt return;
1246 1.21.4.2 yamt
1247 1.21.4.2 yamt getnanotime(&now);
1248 1.21.4.2 yamt if (node->tn_status & TMPFS_NODE_ACCESSED) {
1249 1.21.4.2 yamt if (acc == NULL)
1250 1.21.4.2 yamt acc = &now;
1251 1.21.4.2 yamt node->tn_atime = *acc;
1252 1.21.4.2 yamt }
1253 1.21.4.2 yamt if (node->tn_status & TMPFS_NODE_MODIFIED) {
1254 1.21.4.2 yamt if (mod == NULL)
1255 1.21.4.2 yamt mod = &now;
1256 1.21.4.2 yamt node->tn_mtime = *mod;
1257 1.21.4.2 yamt }
1258 1.21.4.2 yamt if (node->tn_status & TMPFS_NODE_CHANGED)
1259 1.21.4.2 yamt node->tn_ctime = now;
1260 1.21.4.2 yamt
1261 1.21.4.2 yamt node->tn_status &=
1262 1.21.4.2 yamt ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1263 1.21.4.2 yamt }
1264 1.21.4.2 yamt
1265 1.21.4.2 yamt /* --------------------------------------------------------------------- */
1266 1.21.4.2 yamt
1267 1.21.4.2 yamt void
1268 1.21.4.2 yamt tmpfs_update(struct vnode *vp, const struct timespec *acc,
1269 1.21.4.2 yamt const struct timespec *mod, int flags)
1270 1.21.4.2 yamt {
1271 1.21.4.2 yamt
1272 1.21.4.2 yamt struct tmpfs_node *node;
1273 1.21.4.2 yamt
1274 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1275 1.21.4.2 yamt
1276 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
1277 1.21.4.2 yamt
1278 1.21.4.3 yamt #if 0
1279 1.21.4.2 yamt if (flags & UPDATE_CLOSE)
1280 1.21.4.2 yamt ; /* XXX Need to do anything special? */
1281 1.21.4.3 yamt #endif
1282 1.21.4.2 yamt
1283 1.21.4.2 yamt tmpfs_itimes(vp, acc, mod);
1284 1.21.4.2 yamt
1285 1.21.4.2 yamt KASSERT(VOP_ISLOCKED(vp));
1286 1.21.4.2 yamt }
1287 1.21.4.2 yamt
1288 1.21.4.2 yamt /* --------------------------------------------------------------------- */
1289 1.21.4.2 yamt
1290 1.21.4.2 yamt int
1291 1.21.4.2 yamt tmpfs_truncate(struct vnode *vp, off_t length)
1292 1.21.4.2 yamt {
1293 1.21.4.4 yamt bool extended;
1294 1.21.4.2 yamt int error;
1295 1.21.4.2 yamt struct tmpfs_node *node;
1296 1.21.4.2 yamt
1297 1.21.4.2 yamt node = VP_TO_TMPFS_NODE(vp);
1298 1.21.4.2 yamt extended = length > node->tn_size;
1299 1.21.4.2 yamt
1300 1.21.4.2 yamt if (length < 0) {
1301 1.21.4.2 yamt error = EINVAL;
1302 1.21.4.2 yamt goto out;
1303 1.21.4.2 yamt }
1304 1.21.4.2 yamt
1305 1.21.4.2 yamt if (node->tn_size == length) {
1306 1.21.4.2 yamt error = 0;
1307 1.21.4.2 yamt goto out;
1308 1.21.4.2 yamt }
1309 1.21.4.2 yamt
1310 1.21.4.2 yamt error = tmpfs_reg_resize(vp, length);
1311 1.21.4.3 yamt if (error == 0)
1312 1.21.4.2 yamt node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1313 1.21.4.2 yamt
1314 1.21.4.2 yamt out:
1315 1.21.4.2 yamt tmpfs_update(vp, NULL, NULL, 0);
1316 1.21.4.2 yamt
1317 1.21.4.2 yamt return error;
1318 1.21.4.2 yamt }
1319