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