tmpfs_subr.c revision 1.95 1 1.95 hannken /* $NetBSD: tmpfs_subr.c,v 1.95 2014/01/17 10:55:02 hannken Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.83 rmind * Copyright (c) 2005-2013 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.8 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.71 rmind * 2005 program, and by Mindaugas Rasiukevicius.
10 1.1 jmmv *
11 1.1 jmmv * Redistribution and use in source and binary forms, with or without
12 1.1 jmmv * modification, are permitted provided that the following conditions
13 1.1 jmmv * are met:
14 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
15 1.1 jmmv * notice, this list of conditions and the following disclaimer.
16 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
18 1.1 jmmv * documentation and/or other materials provided with the distribution.
19 1.1 jmmv *
20 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
31 1.1 jmmv */
32 1.1 jmmv
33 1.1 jmmv /*
34 1.71 rmind * Efficient memory file system: interfaces for inode and directory entry
35 1.71 rmind * construction, destruction and manipulation.
36 1.71 rmind *
37 1.71 rmind * Reference counting
38 1.71 rmind *
39 1.71 rmind * The link count of inode (tmpfs_node_t::tn_links) is used as a
40 1.71 rmind * reference counter. However, it has slightly different semantics.
41 1.71 rmind *
42 1.71 rmind * For directories - link count represents directory entries, which
43 1.71 rmind * refer to the directories. In other words, it represents the count
44 1.71 rmind * of sub-directories. It also takes into account the virtual '.'
45 1.71 rmind * entry (which has no real entry in the list). For files - link count
46 1.71 rmind * represents the hard links. Since only empty directories can be
47 1.71 rmind * removed - link count aligns the reference counting requirements
48 1.71 rmind * enough. Note: to check whether directory is not empty, the inode
49 1.71 rmind * size (tmpfs_node_t::tn_size) can be used.
50 1.71 rmind *
51 1.71 rmind * The inode itself, as an object, gathers its first reference when
52 1.71 rmind * directory entry is attached via tmpfs_dir_attach(9). For instance,
53 1.71 rmind * after regular tmpfs_create(), a file would have a link count of 1,
54 1.71 rmind * while directory after tmpfs_mkdir() would have 2 (due to '.').
55 1.71 rmind *
56 1.71 rmind * Reclamation
57 1.71 rmind *
58 1.71 rmind * It should be noted that tmpfs inodes rely on a combination of vnode
59 1.71 rmind * reference counting and link counting. That is, an inode can only be
60 1.71 rmind * destroyed if its associated vnode is inactive. The destruction is
61 1.71 rmind * done on vnode reclamation i.e. tmpfs_reclaim(). It should be noted
62 1.90 rmind * that tmpfs_node_t::tn_links being 0 is a destruction criterion.
63 1.71 rmind *
64 1.71 rmind * If an inode has references within the file system (tn_links > 0) and
65 1.71 rmind * its inactive vnode gets reclaimed/recycled - then the association is
66 1.71 rmind * broken in tmpfs_reclaim(). In such case, an inode will always pass
67 1.71 rmind * tmpfs_lookup() and thus tmpfs_vnode_get() to associate a new vnode.
68 1.71 rmind *
69 1.71 rmind * Lock order
70 1.71 rmind *
71 1.71 rmind * tmpfs_node_t::tn_vlock ->
72 1.71 rmind * vnode_t::v_vlock ->
73 1.71 rmind * vnode_t::v_interlock
74 1.1 jmmv */
75 1.1 jmmv
76 1.1 jmmv #include <sys/cdefs.h>
77 1.95 hannken __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.95 2014/01/17 10:55:02 hannken Exp $");
78 1.1 jmmv
79 1.1 jmmv #include <sys/param.h>
80 1.86 rmind #include <sys/cprng.h>
81 1.1 jmmv #include <sys/dirent.h>
82 1.1 jmmv #include <sys/event.h>
83 1.43 ad #include <sys/kmem.h>
84 1.1 jmmv #include <sys/mount.h>
85 1.1 jmmv #include <sys/namei.h>
86 1.1 jmmv #include <sys/time.h>
87 1.1 jmmv #include <sys/stat.h>
88 1.1 jmmv #include <sys/systm.h>
89 1.1 jmmv #include <sys/vnode.h>
90 1.20 christos #include <sys/kauth.h>
91 1.43 ad #include <sys/atomic.h>
92 1.1 jmmv
93 1.1 jmmv #include <uvm/uvm.h>
94 1.1 jmmv
95 1.1 jmmv #include <miscfs/specfs/specdev.h>
96 1.53 elad #include <miscfs/genfs/genfs.h>
97 1.1 jmmv #include <fs/tmpfs/tmpfs.h>
98 1.1 jmmv #include <fs/tmpfs/tmpfs_fifoops.h>
99 1.1 jmmv #include <fs/tmpfs/tmpfs_specops.h>
100 1.1 jmmv #include <fs/tmpfs/tmpfs_vnops.h>
101 1.1 jmmv
102 1.83 rmind static void tmpfs_dir_putseq(tmpfs_node_t *, tmpfs_dirent_t *);
103 1.83 rmind
104 1.8 jmmv /*
105 1.65 rmind * tmpfs_alloc_node: allocate a new inode of a specified type and
106 1.65 rmind * insert it into the list of specified mount point.
107 1.8 jmmv */
108 1.1 jmmv int
109 1.71 rmind tmpfs_alloc_node(tmpfs_mount_t *tmp, enum vtype type, uid_t uid, gid_t gid,
110 1.71 rmind mode_t mode, char *target, dev_t rdev, tmpfs_node_t **node)
111 1.1 jmmv {
112 1.67 rmind tmpfs_node_t *nnode;
113 1.1 jmmv
114 1.57 rmind nnode = tmpfs_node_get(tmp);
115 1.43 ad if (nnode == NULL) {
116 1.43 ad return ENOSPC;
117 1.1 jmmv }
118 1.43 ad
119 1.71 rmind /* Initially, no references and no associations. */
120 1.71 rmind nnode->tn_links = 0;
121 1.71 rmind nnode->tn_vnode = NULL;
122 1.71 rmind nnode->tn_dirent_hint = NULL;
123 1.71 rmind
124 1.43 ad /*
125 1.43 ad * XXX Where the pool is backed by a map larger than (4GB *
126 1.43 ad * sizeof(*nnode)), this may produce duplicate inode numbers
127 1.43 ad * for applications that do not understand 64-bit ino_t.
128 1.43 ad */
129 1.43 ad nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode));
130 1.93 hannken /*
131 1.93 hannken * Make sure the generation number is not zero.
132 1.93 hannken * tmpfs_inactive() uses generation zero to mark dead nodes.
133 1.93 hannken */
134 1.93 hannken do {
135 1.93 hannken nnode->tn_gen = TMPFS_NODE_GEN_MASK & cprng_fast32();
136 1.93 hannken } while (nnode->tn_gen == 0);
137 1.1 jmmv
138 1.1 jmmv /* Generic initialization. */
139 1.1 jmmv nnode->tn_type = type;
140 1.1 jmmv nnode->tn_size = 0;
141 1.1 jmmv nnode->tn_flags = 0;
142 1.65 rmind nnode->tn_lockf = NULL;
143 1.56 rmind
144 1.56 rmind vfs_timestamp(&nnode->tn_atime);
145 1.56 rmind nnode->tn_birthtime = nnode->tn_atime;
146 1.56 rmind nnode->tn_ctime = nnode->tn_atime;
147 1.56 rmind nnode->tn_mtime = nnode->tn_atime;
148 1.56 rmind
149 1.65 rmind KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
150 1.1 jmmv nnode->tn_uid = uid;
151 1.1 jmmv nnode->tn_gid = gid;
152 1.1 jmmv nnode->tn_mode = mode;
153 1.1 jmmv
154 1.1 jmmv /* Type-specific initialization. */
155 1.1 jmmv switch (nnode->tn_type) {
156 1.1 jmmv case VBLK:
157 1.1 jmmv case VCHR:
158 1.65 rmind /* Character/block special device. */
159 1.65 rmind KASSERT(rdev != VNOVAL);
160 1.18 jmmv nnode->tn_spec.tn_dev.tn_rdev = rdev;
161 1.1 jmmv break;
162 1.65 rmind case VDIR:
163 1.71 rmind /* Directory. */
164 1.18 jmmv TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
165 1.71 rmind nnode->tn_spec.tn_dir.tn_parent = NULL;
166 1.83 rmind nnode->tn_spec.tn_dir.tn_seq_arena = NULL;
167 1.83 rmind nnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
168 1.18 jmmv nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
169 1.71 rmind
170 1.71 rmind /* Extra link count for the virtual '.' entry. */
171 1.1 jmmv nnode->tn_links++;
172 1.1 jmmv break;
173 1.1 jmmv case VFIFO:
174 1.1 jmmv case VSOCK:
175 1.1 jmmv break;
176 1.65 rmind case VLNK:
177 1.65 rmind /* Symbolic link. Target specifies the file name. */
178 1.81 rmind KASSERT(target != NULL);
179 1.82 rmind nnode->tn_size = strlen(target);
180 1.82 rmind
181 1.82 rmind if (nnode->tn_size == 0) {
182 1.82 rmind /* Zero-length targets are supported. */
183 1.82 rmind nnode->tn_spec.tn_lnk.tn_link = NULL;
184 1.82 rmind break;
185 1.82 rmind }
186 1.1 jmmv
187 1.81 rmind KASSERT(nnode->tn_size < MAXPATHLEN);
188 1.82 rmind nnode->tn_size++; /* include the NUL terminator */
189 1.81 rmind
190 1.18 jmmv nnode->tn_spec.tn_lnk.tn_link =
191 1.57 rmind tmpfs_strname_alloc(tmp, nnode->tn_size);
192 1.18 jmmv if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
193 1.57 rmind tmpfs_node_put(tmp, nnode);
194 1.1 jmmv return ENOSPC;
195 1.1 jmmv }
196 1.18 jmmv memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
197 1.1 jmmv break;
198 1.1 jmmv case VREG:
199 1.65 rmind /* Regular file. Create an underlying UVM object. */
200 1.18 jmmv nnode->tn_spec.tn_reg.tn_aobj =
201 1.18 jmmv uao_create(INT32_MAX - PAGE_SIZE, 0);
202 1.18 jmmv nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
203 1.1 jmmv break;
204 1.1 jmmv default:
205 1.65 rmind KASSERT(false);
206 1.1 jmmv }
207 1.1 jmmv
208 1.43 ad mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE);
209 1.43 ad
210 1.43 ad mutex_enter(&tmp->tm_lock);
211 1.43 ad LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
212 1.43 ad mutex_exit(&tmp->tm_lock);
213 1.43 ad
214 1.1 jmmv *node = nnode;
215 1.1 jmmv return 0;
216 1.1 jmmv }
217 1.1 jmmv
218 1.8 jmmv /*
219 1.65 rmind * tmpfs_free_node: remove the inode from a list in the mount point and
220 1.65 rmind * destroy the inode structures.
221 1.8 jmmv */
222 1.1 jmmv void
223 1.67 rmind tmpfs_free_node(tmpfs_mount_t *tmp, tmpfs_node_t *node)
224 1.1 jmmv {
225 1.57 rmind size_t objsz;
226 1.43 ad
227 1.43 ad mutex_enter(&tmp->tm_lock);
228 1.43 ad LIST_REMOVE(node, tn_entries);
229 1.43 ad mutex_exit(&tmp->tm_lock);
230 1.1 jmmv
231 1.40 ad switch (node->tn_type) {
232 1.1 jmmv case VLNK:
233 1.65 rmind if (node->tn_size > 0) {
234 1.63 hannken tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link,
235 1.63 hannken node->tn_size);
236 1.65 rmind }
237 1.1 jmmv break;
238 1.1 jmmv case VREG:
239 1.57 rmind /*
240 1.65 rmind * Calculate the size of inode data, decrease the used-memory
241 1.65 rmind * counter, and destroy the unerlying UVM object (if any).
242 1.57 rmind */
243 1.57 rmind objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages;
244 1.57 rmind if (objsz != 0) {
245 1.57 rmind tmpfs_mem_decr(tmp, objsz);
246 1.57 rmind }
247 1.57 rmind if (node->tn_spec.tn_reg.tn_aobj != NULL) {
248 1.18 jmmv uao_detach(node->tn_spec.tn_reg.tn_aobj);
249 1.57 rmind }
250 1.1 jmmv break;
251 1.65 rmind case VDIR:
252 1.90 rmind KASSERT(node->tn_size == 0);
253 1.83 rmind KASSERT(node->tn_spec.tn_dir.tn_seq_arena == NULL);
254 1.83 rmind KASSERT(TAILQ_EMPTY(&node->tn_spec.tn_dir.tn_dir));
255 1.83 rmind KASSERT(node->tn_spec.tn_dir.tn_parent == NULL ||
256 1.83 rmind node == tmp->tm_root);
257 1.65 rmind break;
258 1.1 jmmv default:
259 1.1 jmmv break;
260 1.1 jmmv }
261 1.93 hannken KASSERT(node->tn_vnode == NULL);
262 1.90 rmind KASSERT(node->tn_links == 0);
263 1.1 jmmv
264 1.43 ad mutex_destroy(&node->tn_vlock);
265 1.57 rmind tmpfs_node_put(tmp, node);
266 1.1 jmmv }
267 1.1 jmmv
268 1.8 jmmv /*
269 1.71 rmind * tmpfs_vnode_get: allocate or reclaim a vnode for a specified inode.
270 1.8 jmmv *
271 1.71 rmind * => Must be called with tmpfs_node_t::tn_vlock held.
272 1.64 rmind * => Returns vnode (*vpp) locked.
273 1.8 jmmv */
274 1.1 jmmv int
275 1.71 rmind tmpfs_vnode_get(struct mount *mp, tmpfs_node_t *node, vnode_t **vpp)
276 1.1 jmmv {
277 1.64 rmind vnode_t *vp;
278 1.73 rmind kmutex_t *slock;
279 1.1 jmmv int error;
280 1.64 rmind again:
281 1.64 rmind /* If there is already a vnode, try to reclaim it. */
282 1.64 rmind if ((vp = node->tn_vnode) != NULL) {
283 1.88 rmind atomic_or_32(&node->tn_gen, TMPFS_RECLAIMING_BIT);
284 1.73 rmind mutex_enter(vp->v_interlock);
285 1.64 rmind mutex_exit(&node->tn_vlock);
286 1.64 rmind error = vget(vp, LK_EXCLUSIVE);
287 1.64 rmind if (error == ENOENT) {
288 1.71 rmind mutex_enter(&node->tn_vlock);
289 1.64 rmind goto again;
290 1.43 ad }
291 1.88 rmind atomic_and_32(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
292 1.64 rmind *vpp = vp;
293 1.64 rmind return error;
294 1.1 jmmv }
295 1.71 rmind if (TMPFS_NODE_RECLAIMING(node)) {
296 1.88 rmind atomic_and_32(&node->tn_gen, ~TMPFS_RECLAIMING_BIT);
297 1.71 rmind }
298 1.1 jmmv
299 1.73 rmind /*
300 1.73 rmind * Get a new vnode and associate it with our inode. Share the
301 1.73 rmind * lock with underlying UVM object, if there is one (VREG case).
302 1.73 rmind */
303 1.73 rmind if (node->tn_type == VREG) {
304 1.73 rmind struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
305 1.73 rmind slock = uobj->vmobjlock;
306 1.73 rmind } else {
307 1.73 rmind slock = NULL;
308 1.73 rmind }
309 1.73 rmind error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, slock, &vp);
310 1.67 rmind if (error) {
311 1.43 ad mutex_exit(&node->tn_vlock);
312 1.43 ad return error;
313 1.43 ad }
314 1.1 jmmv
315 1.64 rmind vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
316 1.1 jmmv vp->v_type = node->tn_type;
317 1.1 jmmv
318 1.1 jmmv /* Type-specific initialization. */
319 1.1 jmmv switch (node->tn_type) {
320 1.1 jmmv case VBLK:
321 1.1 jmmv case VCHR:
322 1.1 jmmv vp->v_op = tmpfs_specop_p;
323 1.44 ad spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
324 1.1 jmmv break;
325 1.1 jmmv case VDIR:
326 1.40 ad vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
327 1.40 ad VV_ROOT : 0;
328 1.1 jmmv break;
329 1.1 jmmv case VFIFO:
330 1.1 jmmv vp->v_op = tmpfs_fifoop_p;
331 1.1 jmmv break;
332 1.1 jmmv case VLNK:
333 1.1 jmmv case VREG:
334 1.1 jmmv case VSOCK:
335 1.1 jmmv break;
336 1.1 jmmv default:
337 1.67 rmind KASSERT(false);
338 1.1 jmmv }
339 1.1 jmmv
340 1.1 jmmv uvm_vnp_setsize(vp, node->tn_size);
341 1.43 ad vp->v_data = node;
342 1.43 ad node->tn_vnode = vp;
343 1.43 ad mutex_exit(&node->tn_vlock);
344 1.67 rmind
345 1.67 rmind KASSERT(VOP_ISLOCKED(vp));
346 1.43 ad *vpp = vp;
347 1.67 rmind return 0;
348 1.1 jmmv }
349 1.1 jmmv
350 1.8 jmmv /*
351 1.90 rmind * tmpfs_construct_node: allocate a new file of specified type and adds it
352 1.67 rmind * into the parent directory.
353 1.67 rmind *
354 1.67 rmind * => Credentials of the caller are used.
355 1.9 jmmv */
356 1.1 jmmv int
357 1.90 rmind tmpfs_construct_node(vnode_t *dvp, vnode_t **vpp, struct vattr *vap,
358 1.1 jmmv struct componentname *cnp, char *target)
359 1.1 jmmv {
360 1.67 rmind tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
361 1.71 rmind tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp), *node;
362 1.77 hannken tmpfs_dirent_t *de, *wde;
363 1.1 jmmv int error;
364 1.1 jmmv
365 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
366 1.1 jmmv *vpp = NULL;
367 1.1 jmmv
368 1.92 rmind /*
369 1.92 rmind * If directory was removed, prevent from node creation. The vnode
370 1.92 rmind * might still be referenced, but it is about to be reclaimed.
371 1.92 rmind */
372 1.92 rmind if (dnode->tn_links == 0) {
373 1.92 rmind error = ENOENT;
374 1.92 rmind goto out;
375 1.92 rmind }
376 1.92 rmind
377 1.67 rmind /* Check for the maximum number of links limit. */
378 1.1 jmmv if (vap->va_type == VDIR) {
379 1.67 rmind /* Check for maximum links limit. */
380 1.1 jmmv if (dnode->tn_links == LINK_MAX) {
381 1.1 jmmv error = EMLINK;
382 1.1 jmmv goto out;
383 1.1 jmmv }
384 1.71 rmind KASSERT(dnode->tn_links < LINK_MAX);
385 1.67 rmind }
386 1.1 jmmv
387 1.1 jmmv /* Allocate a node that represents the new file. */
388 1.19 elad error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
389 1.71 rmind dnode->tn_gid, vap->va_mode, target, vap->va_rdev, &node);
390 1.67 rmind if (error)
391 1.1 jmmv goto out;
392 1.1 jmmv
393 1.1 jmmv /* Allocate a directory entry that points to the new file. */
394 1.71 rmind error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, cnp->cn_namelen, &de);
395 1.67 rmind if (error) {
396 1.1 jmmv tmpfs_free_node(tmp, node);
397 1.1 jmmv goto out;
398 1.1 jmmv }
399 1.1 jmmv
400 1.71 rmind /* Get a vnode for the new file. */
401 1.71 rmind mutex_enter(&node->tn_vlock);
402 1.71 rmind error = tmpfs_vnode_get(dvp->v_mount, node, vpp);
403 1.67 rmind if (error) {
404 1.71 rmind tmpfs_free_dirent(tmp, de);
405 1.1 jmmv tmpfs_free_node(tmp, node);
406 1.1 jmmv goto out;
407 1.1 jmmv }
408 1.1 jmmv
409 1.77 hannken /* Remove whiteout before adding the new entry. */
410 1.77 hannken if (cnp->cn_flags & ISWHITEOUT) {
411 1.77 hannken wde = tmpfs_dir_lookup(dnode, cnp);
412 1.77 hannken KASSERT(wde != NULL && wde->td_node == TMPFS_NODE_WHITEOUT);
413 1.83 rmind tmpfs_dir_detach(dnode, wde);
414 1.77 hannken tmpfs_free_dirent(tmp, wde);
415 1.77 hannken }
416 1.77 hannken
417 1.71 rmind /* Associate inode and attach the entry into the directory. */
418 1.83 rmind tmpfs_dir_attach(dnode, de, node);
419 1.77 hannken
420 1.77 hannken /* Make node opaque if requested. */
421 1.77 hannken if (cnp->cn_flags & ISWHITEOUT)
422 1.77 hannken node->tn_flags |= UF_OPAQUE;
423 1.90 rmind
424 1.90 rmind /* Update the parent's timestamps. */
425 1.90 rmind tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
426 1.1 jmmv out:
427 1.1 jmmv return error;
428 1.1 jmmv }
429 1.1 jmmv
430 1.8 jmmv /*
431 1.68 rmind * tmpfs_alloc_dirent: allocates a new directory entry for the inode.
432 1.71 rmind * The directory entry contains a path name component.
433 1.68 rmind */
434 1.68 rmind int
435 1.71 rmind tmpfs_alloc_dirent(tmpfs_mount_t *tmp, const char *name, uint16_t len,
436 1.71 rmind tmpfs_dirent_t **de)
437 1.68 rmind {
438 1.68 rmind tmpfs_dirent_t *nde;
439 1.68 rmind
440 1.68 rmind nde = tmpfs_dirent_get(tmp);
441 1.68 rmind if (nde == NULL)
442 1.68 rmind return ENOSPC;
443 1.68 rmind
444 1.68 rmind nde->td_name = tmpfs_strname_alloc(tmp, len);
445 1.68 rmind if (nde->td_name == NULL) {
446 1.68 rmind tmpfs_dirent_put(tmp, nde);
447 1.68 rmind return ENOSPC;
448 1.68 rmind }
449 1.68 rmind nde->td_namelen = len;
450 1.68 rmind memcpy(nde->td_name, name, len);
451 1.83 rmind nde->td_seq = TMPFS_DIRSEQ_NONE;
452 1.68 rmind
453 1.68 rmind *de = nde;
454 1.68 rmind return 0;
455 1.68 rmind }
456 1.68 rmind
457 1.68 rmind /*
458 1.68 rmind * tmpfs_free_dirent: free a directory entry.
459 1.68 rmind */
460 1.68 rmind void
461 1.71 rmind tmpfs_free_dirent(tmpfs_mount_t *tmp, tmpfs_dirent_t *de)
462 1.68 rmind {
463 1.83 rmind KASSERT(de->td_node == NULL);
464 1.83 rmind KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
465 1.68 rmind tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
466 1.68 rmind tmpfs_dirent_put(tmp, de);
467 1.68 rmind }
468 1.68 rmind
469 1.68 rmind /*
470 1.71 rmind * tmpfs_dir_attach: associate directory entry with a specified inode,
471 1.71 rmind * and attach the entry into the directory, specified by vnode.
472 1.29 jmmv *
473 1.71 rmind * => Increases link count on the associated node.
474 1.90 rmind * => Increases link count on directory node if our node is VDIR.
475 1.90 rmind * => It is caller's responsibility to check for the LINK_MAX limit.
476 1.71 rmind * => Triggers kqueue events here.
477 1.8 jmmv */
478 1.1 jmmv void
479 1.83 rmind tmpfs_dir_attach(tmpfs_node_t *dnode, tmpfs_dirent_t *de, tmpfs_node_t *node)
480 1.1 jmmv {
481 1.83 rmind vnode_t *dvp = dnode->tn_vnode;
482 1.71 rmind int events = NOTE_WRITE;
483 1.71 rmind
484 1.83 rmind KASSERT(dvp != NULL);
485 1.71 rmind KASSERT(VOP_ISLOCKED(dvp));
486 1.71 rmind
487 1.83 rmind /* Get a new sequence number. */
488 1.83 rmind KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
489 1.83 rmind de->td_seq = tmpfs_dir_getseq(dnode, de);
490 1.83 rmind
491 1.71 rmind /* Associate directory entry and the inode. */
492 1.77 hannken de->td_node = node;
493 1.71 rmind if (node != TMPFS_NODE_WHITEOUT) {
494 1.71 rmind KASSERT(node->tn_links < LINK_MAX);
495 1.71 rmind node->tn_links++;
496 1.1 jmmv
497 1.71 rmind /* Save the hint (might overwrite). */
498 1.71 rmind node->tn_dirent_hint = de;
499 1.90 rmind } else if ((dnode->tn_gen & TMPFS_WHITEOUT_BIT) == 0) {
500 1.90 rmind /* Flag that there are whiteout entries. */
501 1.90 rmind atomic_or_32(&dnode->tn_gen, TMPFS_WHITEOUT_BIT);
502 1.71 rmind }
503 1.1 jmmv
504 1.71 rmind /* Insert the entry to the directory (parent of inode). */
505 1.18 jmmv TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
506 1.67 rmind dnode->tn_size += sizeof(tmpfs_dirent_t);
507 1.71 rmind uvm_vnp_setsize(dvp, dnode->tn_size);
508 1.71 rmind
509 1.71 rmind if (node != TMPFS_NODE_WHITEOUT && node->tn_type == VDIR) {
510 1.71 rmind /* Set parent. */
511 1.71 rmind KASSERT(node->tn_spec.tn_dir.tn_parent == NULL);
512 1.71 rmind node->tn_spec.tn_dir.tn_parent = dnode;
513 1.71 rmind
514 1.71 rmind /* Increase the link count of parent. */
515 1.71 rmind KASSERT(dnode->tn_links < LINK_MAX);
516 1.71 rmind dnode->tn_links++;
517 1.71 rmind events |= NOTE_LINK;
518 1.71 rmind
519 1.71 rmind TMPFS_VALIDATE_DIR(node);
520 1.71 rmind }
521 1.71 rmind VN_KNOTE(dvp, events);
522 1.1 jmmv }
523 1.1 jmmv
524 1.8 jmmv /*
525 1.71 rmind * tmpfs_dir_detach: disassociate directory entry and its inode,
526 1.71 rmind * and detach the entry from the directory, specified by vnode.
527 1.29 jmmv *
528 1.71 rmind * => Decreases link count on the associated node.
529 1.71 rmind * => Decreases the link count on directory node, if our node is VDIR.
530 1.71 rmind * => Triggers kqueue events here.
531 1.83 rmind *
532 1.83 rmind * => Note: dvp and vp may be NULL only if called by tmpfs_unmount().
533 1.8 jmmv */
534 1.1 jmmv void
535 1.83 rmind tmpfs_dir_detach(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
536 1.1 jmmv {
537 1.71 rmind tmpfs_node_t *node = de->td_node;
538 1.83 rmind vnode_t *vp, *dvp = dnode->tn_vnode;
539 1.71 rmind int events = NOTE_WRITE;
540 1.71 rmind
541 1.83 rmind KASSERT(dvp == NULL || VOP_ISLOCKED(dvp));
542 1.71 rmind
543 1.83 rmind if (__predict_true(node != TMPFS_NODE_WHITEOUT)) {
544 1.71 rmind /* Deassociate the inode and entry. */
545 1.71 rmind node->tn_dirent_hint = NULL;
546 1.1 jmmv
547 1.71 rmind KASSERT(node->tn_links > 0);
548 1.71 rmind node->tn_links--;
549 1.83 rmind
550 1.83 rmind if ((vp = node->tn_vnode) != NULL) {
551 1.83 rmind KASSERT(VOP_ISLOCKED(vp));
552 1.83 rmind VN_KNOTE(vp, node->tn_links ? NOTE_LINK : NOTE_DELETE);
553 1.83 rmind }
554 1.71 rmind
555 1.71 rmind /* If directory - decrease the link count of parent. */
556 1.71 rmind if (node->tn_type == VDIR) {
557 1.71 rmind KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
558 1.71 rmind node->tn_spec.tn_dir.tn_parent = NULL;
559 1.71 rmind
560 1.71 rmind KASSERT(dnode->tn_links > 0);
561 1.71 rmind dnode->tn_links--;
562 1.71 rmind events |= NOTE_LINK;
563 1.71 rmind }
564 1.71 rmind }
565 1.85 rmind de->td_node = NULL;
566 1.1 jmmv
567 1.71 rmind /* Remove the entry from the directory. */
568 1.18 jmmv if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
569 1.18 jmmv dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
570 1.5 yamt }
571 1.67 rmind TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
572 1.67 rmind dnode->tn_size -= sizeof(tmpfs_dirent_t);
573 1.83 rmind tmpfs_dir_putseq(dnode, de);
574 1.83 rmind
575 1.83 rmind if (dvp) {
576 1.83 rmind uvm_vnp_setsize(dvp, dnode->tn_size);
577 1.83 rmind VN_KNOTE(dvp, events);
578 1.83 rmind }
579 1.1 jmmv }
580 1.1 jmmv
581 1.8 jmmv /*
582 1.67 rmind * tmpfs_dir_lookup: find a directory entry in the specified inode.
583 1.8 jmmv *
584 1.67 rmind * Note that the . and .. components are not allowed as they do not
585 1.67 rmind * physically exist within directories.
586 1.8 jmmv */
587 1.67 rmind tmpfs_dirent_t *
588 1.67 rmind tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp)
589 1.1 jmmv {
590 1.67 rmind const char *name = cnp->cn_nameptr;
591 1.67 rmind const uint16_t nlen = cnp->cn_namelen;
592 1.67 rmind tmpfs_dirent_t *de;
593 1.1 jmmv
594 1.49 yamt KASSERT(VOP_ISLOCKED(node->tn_vnode));
595 1.67 rmind KASSERT(nlen != 1 || !(name[0] == '.'));
596 1.67 rmind KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.'));
597 1.71 rmind TMPFS_VALIDATE_DIR(node);
598 1.1 jmmv
599 1.18 jmmv TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
600 1.67 rmind if (de->td_namelen != nlen)
601 1.67 rmind continue;
602 1.69 rmind if (memcmp(de->td_name, name, nlen) != 0)
603 1.67 rmind continue;
604 1.67 rmind break;
605 1.1 jmmv }
606 1.49 yamt return de;
607 1.1 jmmv }
608 1.1 jmmv
609 1.9 jmmv /*
610 1.71 rmind * tmpfs_dir_cached: get a cached directory entry if it is valid. Used to
611 1.83 rmind * avoid unnecessary tmpfs_dir_lookup().
612 1.71 rmind *
613 1.71 rmind * => The vnode must be locked.
614 1.71 rmind */
615 1.71 rmind tmpfs_dirent_t *
616 1.71 rmind tmpfs_dir_cached(tmpfs_node_t *node)
617 1.71 rmind {
618 1.71 rmind tmpfs_dirent_t *de = node->tn_dirent_hint;
619 1.71 rmind
620 1.71 rmind KASSERT(VOP_ISLOCKED(node->tn_vnode));
621 1.71 rmind
622 1.71 rmind if (de == NULL) {
623 1.71 rmind return NULL;
624 1.71 rmind }
625 1.71 rmind KASSERT(de->td_node == node);
626 1.71 rmind
627 1.71 rmind /*
628 1.71 rmind * Directories always have a valid hint. For files, check if there
629 1.71 rmind * are any hard links. If there are - hint might be invalid.
630 1.71 rmind */
631 1.71 rmind return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de;
632 1.71 rmind }
633 1.71 rmind
634 1.71 rmind /*
635 1.83 rmind * tmpfs_dir_getseq: get a per-directory sequence number for the entry.
636 1.83 rmind *
637 1.83 rmind * => Shall not be larger than 2^31 for linux32 compatibility.
638 1.9 jmmv */
639 1.83 rmind uint32_t
640 1.83 rmind tmpfs_dir_getseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
641 1.1 jmmv {
642 1.83 rmind uint32_t seq = de->td_seq;
643 1.83 rmind vmem_t *seq_arena;
644 1.83 rmind vmem_addr_t off;
645 1.84 christos int error __diagused;
646 1.1 jmmv
647 1.83 rmind TMPFS_VALIDATE_DIR(dnode);
648 1.83 rmind
649 1.83 rmind if (__predict_true(seq != TMPFS_DIRSEQ_NONE)) {
650 1.83 rmind /* Already set. */
651 1.83 rmind KASSERT(seq >= TMPFS_DIRSEQ_START);
652 1.83 rmind return seq;
653 1.83 rmind }
654 1.83 rmind
655 1.83 rmind /*
656 1.83 rmind * The "." and ".." and the end-of-directory have reserved numbers.
657 1.83 rmind * The other sequence numbers are allocated as following:
658 1.83 rmind *
659 1.83 rmind * - The first half of the 2^31 is assigned incrementally.
660 1.83 rmind *
661 1.83 rmind * - If that range is exceeded, then the second half of 2^31
662 1.83 rmind * is used, but managed by vmem(9).
663 1.83 rmind */
664 1.83 rmind
665 1.83 rmind seq = dnode->tn_spec.tn_dir.tn_next_seq;
666 1.83 rmind KASSERT(seq >= TMPFS_DIRSEQ_START);
667 1.83 rmind
668 1.83 rmind if (__predict_true(seq < TMPFS_DIRSEQ_END)) {
669 1.83 rmind /* First half: just increment and return. */
670 1.83 rmind dnode->tn_spec.tn_dir.tn_next_seq++;
671 1.83 rmind return seq;
672 1.83 rmind }
673 1.83 rmind
674 1.83 rmind /*
675 1.83 rmind * First half exceeded, use the second half. May need to create
676 1.83 rmind * vmem(9) arena for the directory first.
677 1.83 rmind */
678 1.83 rmind if ((seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena) == NULL) {
679 1.83 rmind seq_arena = vmem_create("tmpfscoo", 0,
680 1.83 rmind TMPFS_DIRSEQ_END - 1, 1, NULL, NULL, NULL, 0,
681 1.83 rmind VM_SLEEP, IPL_NONE);
682 1.83 rmind dnode->tn_spec.tn_dir.tn_seq_arena = seq_arena;
683 1.83 rmind KASSERT(seq_arena != NULL);
684 1.83 rmind }
685 1.83 rmind error = vmem_alloc(seq_arena, 1, VM_SLEEP | VM_BESTFIT, &off);
686 1.83 rmind KASSERT(error == 0);
687 1.83 rmind
688 1.83 rmind KASSERT(off < TMPFS_DIRSEQ_END);
689 1.83 rmind seq = off | TMPFS_DIRSEQ_END;
690 1.83 rmind return seq;
691 1.83 rmind }
692 1.83 rmind
693 1.83 rmind static void
694 1.83 rmind tmpfs_dir_putseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
695 1.83 rmind {
696 1.83 rmind vmem_t *seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena;
697 1.83 rmind uint32_t seq = de->td_seq;
698 1.83 rmind
699 1.83 rmind TMPFS_VALIDATE_DIR(dnode);
700 1.83 rmind
701 1.83 rmind if (seq == TMPFS_DIRSEQ_NONE || seq < TMPFS_DIRSEQ_END) {
702 1.83 rmind /* First half (or no sequence number set yet). */
703 1.83 rmind KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
704 1.83 rmind } else {
705 1.83 rmind /* Second half. */
706 1.83 rmind KASSERT(seq_arena != NULL);
707 1.83 rmind KASSERT(seq >= TMPFS_DIRSEQ_END);
708 1.83 rmind seq &= ~TMPFS_DIRSEQ_END;
709 1.83 rmind vmem_free(seq_arena, seq, 1);
710 1.83 rmind }
711 1.83 rmind de->td_seq = TMPFS_DIRSEQ_NONE;
712 1.1 jmmv
713 1.83 rmind /* Empty? We can reset. */
714 1.83 rmind if (seq_arena && dnode->tn_size == 0) {
715 1.83 rmind dnode->tn_spec.tn_dir.tn_seq_arena = NULL;
716 1.83 rmind dnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
717 1.83 rmind vmem_destroy(seq_arena);
718 1.1 jmmv }
719 1.1 jmmv }
720 1.1 jmmv
721 1.9 jmmv /*
722 1.83 rmind * tmpfs_dir_lookupbyseq: lookup a directory entry by the sequence number.
723 1.9 jmmv */
724 1.83 rmind tmpfs_dirent_t *
725 1.83 rmind tmpfs_dir_lookupbyseq(tmpfs_node_t *node, off_t seq)
726 1.1 jmmv {
727 1.83 rmind tmpfs_dirent_t *de = node->tn_spec.tn_dir.tn_readdir_lastp;
728 1.1 jmmv
729 1.1 jmmv TMPFS_VALIDATE_DIR(node);
730 1.1 jmmv
731 1.83 rmind /*
732 1.83 rmind * First, check the cache. If does not match - perform a lookup.
733 1.83 rmind */
734 1.83 rmind if (de && de->td_seq == seq) {
735 1.83 rmind KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
736 1.83 rmind KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
737 1.83 rmind return de;
738 1.83 rmind }
739 1.83 rmind TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
740 1.83 rmind KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
741 1.83 rmind KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
742 1.83 rmind if (de->td_seq == seq)
743 1.83 rmind return de;
744 1.1 jmmv }
745 1.83 rmind return NULL;
746 1.1 jmmv }
747 1.1 jmmv
748 1.9 jmmv /*
749 1.83 rmind * tmpfs_dir_getdotents: helper function for tmpfs_readdir() to get the
750 1.83 rmind * dot meta entries, that is, "." or "..". Copy it to the UIO space.
751 1.9 jmmv */
752 1.83 rmind static int
753 1.83 rmind tmpfs_dir_getdotents(tmpfs_node_t *node, struct dirent *dp, struct uio *uio)
754 1.5 yamt {
755 1.67 rmind tmpfs_dirent_t *de;
756 1.83 rmind off_t next = 0;
757 1.83 rmind int error;
758 1.83 rmind
759 1.83 rmind switch (uio->uio_offset) {
760 1.83 rmind case TMPFS_DIRSEQ_DOT:
761 1.89 rmind dp->d_fileno = node->tn_id;
762 1.83 rmind strlcpy(dp->d_name, ".", sizeof(dp->d_name));
763 1.83 rmind next = TMPFS_DIRSEQ_DOTDOT;
764 1.83 rmind break;
765 1.83 rmind case TMPFS_DIRSEQ_DOTDOT:
766 1.89 rmind dp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
767 1.83 rmind strlcpy(dp->d_name, "..", sizeof(dp->d_name));
768 1.83 rmind de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
769 1.83 rmind next = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
770 1.83 rmind break;
771 1.83 rmind default:
772 1.83 rmind KASSERT(false);
773 1.83 rmind }
774 1.89 rmind dp->d_type = DT_DIR;
775 1.83 rmind dp->d_namlen = strlen(dp->d_name);
776 1.83 rmind dp->d_reclen = _DIRENT_SIZE(dp);
777 1.49 yamt
778 1.83 rmind if (dp->d_reclen > uio->uio_resid) {
779 1.83 rmind return EJUSTRETURN;
780 1.5 yamt }
781 1.83 rmind if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
782 1.83 rmind return error;
783 1.5 yamt }
784 1.83 rmind
785 1.83 rmind uio->uio_offset = next;
786 1.83 rmind return error;
787 1.5 yamt }
788 1.5 yamt
789 1.9 jmmv /*
790 1.83 rmind * tmpfs_dir_getdents: helper function for tmpfs_readdir.
791 1.67 rmind *
792 1.67 rmind * => Returns as much directory entries as can fit in the uio space.
793 1.67 rmind * => The read starts at uio->uio_offset.
794 1.9 jmmv */
795 1.1 jmmv int
796 1.67 rmind tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio, off_t *cntp)
797 1.1 jmmv {
798 1.67 rmind tmpfs_dirent_t *de;
799 1.94 pedro struct dirent dent;
800 1.83 rmind int error = 0;
801 1.1 jmmv
802 1.49 yamt KASSERT(VOP_ISLOCKED(node->tn_vnode));
803 1.1 jmmv TMPFS_VALIDATE_DIR(node);
804 1.1 jmmv
805 1.67 rmind /*
806 1.94 pedro * First check for the "." and ".." cases.
807 1.83 rmind * Note: tmpfs_dir_getdotents() will "seek" for us.
808 1.67 rmind */
809 1.94 pedro memset(&dent, 0, sizeof(dent));
810 1.83 rmind
811 1.83 rmind if (uio->uio_offset == TMPFS_DIRSEQ_DOT) {
812 1.94 pedro if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) {
813 1.83 rmind goto done;
814 1.83 rmind }
815 1.83 rmind (*cntp)++;
816 1.83 rmind }
817 1.83 rmind if (uio->uio_offset == TMPFS_DIRSEQ_DOTDOT) {
818 1.94 pedro if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) {
819 1.83 rmind goto done;
820 1.83 rmind }
821 1.83 rmind (*cntp)++;
822 1.83 rmind }
823 1.83 rmind
824 1.83 rmind /* Done if we reached the end. */
825 1.83 rmind if (uio->uio_offset == TMPFS_DIRSEQ_EOF) {
826 1.83 rmind goto done;
827 1.5 yamt }
828 1.83 rmind
829 1.83 rmind /* Locate the directory entry given by the given sequence number. */
830 1.83 rmind de = tmpfs_dir_lookupbyseq(node, uio->uio_offset);
831 1.5 yamt if (de == NULL) {
832 1.83 rmind error = EINVAL;
833 1.83 rmind goto done;
834 1.1 jmmv }
835 1.1 jmmv
836 1.67 rmind /*
837 1.83 rmind * Read as many entries as possible; i.e., until we reach the end
838 1.83 rmind * of the directory or we exhaust UIO space.
839 1.67 rmind */
840 1.1 jmmv do {
841 1.62 pooka if (de->td_node == TMPFS_NODE_WHITEOUT) {
842 1.94 pedro dent.d_fileno = 1;
843 1.94 pedro dent.d_type = DT_WHT;
844 1.62 pooka } else {
845 1.94 pedro dent.d_fileno = de->td_node->tn_id;
846 1.94 pedro dent.d_type = vtype2dt(de->td_node->tn_type);
847 1.1 jmmv }
848 1.94 pedro dent.d_namlen = de->td_namelen;
849 1.94 pedro KASSERT(de->td_namelen < sizeof(dent.d_name));
850 1.94 pedro memcpy(dent.d_name, de->td_name, de->td_namelen);
851 1.94 pedro dent.d_name[de->td_namelen] = '\0';
852 1.94 pedro dent.d_reclen = _DIRENT_SIZE(&dent);
853 1.1 jmmv
854 1.94 pedro if (dent.d_reclen > uio->uio_resid) {
855 1.83 rmind /* Exhausted UIO space. */
856 1.83 rmind error = EJUSTRETURN;
857 1.1 jmmv break;
858 1.1 jmmv }
859 1.1 jmmv
860 1.83 rmind /* Copy out the directory entry and continue. */
861 1.94 pedro error = uiomove(&dent, dent.d_reclen, uio);
862 1.83 rmind if (error) {
863 1.83 rmind break;
864 1.83 rmind }
865 1.5 yamt (*cntp)++;
866 1.1 jmmv de = TAILQ_NEXT(de, td_entries);
867 1.1 jmmv
868 1.83 rmind } while (uio->uio_resid > 0 && de);
869 1.83 rmind
870 1.83 rmind /* Cache the last entry or clear and mark EOF. */
871 1.83 rmind uio->uio_offset = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
872 1.83 rmind node->tn_spec.tn_dir.tn_readdir_lastp = de;
873 1.83 rmind done:
874 1.90 rmind tmpfs_update(node->tn_vnode, TMPFS_UPDATE_ATIME);
875 1.83 rmind
876 1.83 rmind if (error == EJUSTRETURN) {
877 1.83 rmind /* Exhausted UIO space - just return. */
878 1.83 rmind error = 0;
879 1.83 rmind }
880 1.83 rmind KASSERT(error >= 0);
881 1.1 jmmv return error;
882 1.1 jmmv }
883 1.1 jmmv
884 1.8 jmmv /*
885 1.67 rmind * tmpfs_reg_resize: resize the underlying UVM object associated with the
886 1.67 rmind * specified regular file.
887 1.8 jmmv */
888 1.1 jmmv int
889 1.1 jmmv tmpfs_reg_resize(struct vnode *vp, off_t newsize)
890 1.1 jmmv {
891 1.67 rmind tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
892 1.67 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
893 1.74 hannken struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
894 1.57 rmind size_t newpages, oldpages;
895 1.13 yamt off_t oldsize;
896 1.1 jmmv
897 1.1 jmmv KASSERT(vp->v_type == VREG);
898 1.1 jmmv KASSERT(newsize >= 0);
899 1.1 jmmv
900 1.13 yamt oldsize = node->tn_size;
901 1.57 rmind oldpages = round_page(oldsize) >> PAGE_SHIFT;
902 1.57 rmind newpages = round_page(newsize) >> PAGE_SHIFT;
903 1.18 jmmv KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
904 1.1 jmmv
905 1.57 rmind if (newpages > oldpages) {
906 1.57 rmind /* Increase the used-memory counter if getting extra pages. */
907 1.57 rmind if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
908 1.57 rmind return ENOSPC;
909 1.57 rmind }
910 1.57 rmind } else if (newsize < oldsize) {
911 1.91 rmind size_t zerolen;
912 1.13 yamt
913 1.91 rmind zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
914 1.74 hannken ubc_zerorange(uobj, newsize, zerolen, UBC_UNMAP_FLAG(vp));
915 1.13 yamt }
916 1.1 jmmv
917 1.36 pooka node->tn_spec.tn_reg.tn_aobj_pages = newpages;
918 1.36 pooka node->tn_size = newsize;
919 1.36 pooka uvm_vnp_setsize(vp, newsize);
920 1.36 pooka
921 1.76 enami /*
922 1.76 enami * Free "backing store".
923 1.76 enami */
924 1.43 ad if (newpages < oldpages) {
925 1.76 enami KASSERT(uobj->vmobjlock == vp->v_interlock);
926 1.76 enami
927 1.76 enami mutex_enter(uobj->vmobjlock);
928 1.76 enami uao_dropswap_range(uobj, newpages, oldpages);
929 1.76 enami mutex_exit(uobj->vmobjlock);
930 1.76 enami
931 1.57 rmind /* Decrease the used-memory counter. */
932 1.57 rmind tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
933 1.43 ad }
934 1.67 rmind if (newsize > oldsize) {
935 1.29 jmmv VN_KNOTE(vp, NOTE_EXTEND);
936 1.67 rmind }
937 1.57 rmind return 0;
938 1.1 jmmv }
939 1.1 jmmv
940 1.9 jmmv /*
941 1.67 rmind * tmpfs_chflags: change flags of the given vnode.
942 1.9 jmmv */
943 1.1 jmmv int
944 1.67 rmind tmpfs_chflags(vnode_t *vp, int flags, kauth_cred_t cred, lwp_t *l)
945 1.1 jmmv {
946 1.67 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
947 1.55 pooka kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
948 1.79 elad int error;
949 1.79 elad bool changing_sysflags = false;
950 1.1 jmmv
951 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
952 1.1 jmmv
953 1.1 jmmv /* Disallow this operation if the file system is mounted read-only. */
954 1.1 jmmv if (vp->v_mount->mnt_flag & MNT_RDONLY)
955 1.1 jmmv return EROFS;
956 1.1 jmmv
957 1.54 elad /*
958 1.54 elad * If the new flags have non-user flags that are different than
959 1.54 elad * those on the node, we need special permission to change them.
960 1.54 elad */
961 1.54 elad if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
962 1.54 elad action |= KAUTH_VNODE_WRITE_SYSFLAGS;
963 1.79 elad changing_sysflags = true;
964 1.54 elad }
965 1.54 elad
966 1.54 elad /*
967 1.54 elad * Indicate that this node's flags have system attributes in them if
968 1.54 elad * that's the case.
969 1.54 elad */
970 1.54 elad if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
971 1.54 elad action |= KAUTH_VNODE_HAS_SYSFLAGS;
972 1.54 elad }
973 1.54 elad
974 1.79 elad error = kauth_authorize_vnode(cred, action, vp, NULL,
975 1.79 elad genfs_can_chflags(cred, vp->v_type, node->tn_uid,
976 1.79 elad changing_sysflags));
977 1.54 elad if (error)
978 1.1 jmmv return error;
979 1.54 elad
980 1.54 elad /*
981 1.54 elad * Set the flags. If we're not setting non-user flags, be careful not
982 1.54 elad * to overwrite them.
983 1.54 elad *
984 1.54 elad * XXX: Can't we always assign here? if the system flags are different,
985 1.54 elad * the code above should catch attempts to change them without
986 1.54 elad * proper permissions, and if we're here it means it's okay to
987 1.54 elad * change them...
988 1.54 elad */
989 1.79 elad if (!changing_sysflags) {
990 1.54 elad /* Clear all user-settable flags and re-set them. */
991 1.1 jmmv node->tn_flags &= SF_SETTABLE;
992 1.1 jmmv node->tn_flags |= (flags & UF_SETTABLE);
993 1.67 rmind } else {
994 1.67 rmind node->tn_flags = flags;
995 1.1 jmmv }
996 1.90 rmind tmpfs_update(vp, TMPFS_UPDATE_CTIME);
997 1.1 jmmv VN_KNOTE(vp, NOTE_ATTRIB);
998 1.1 jmmv return 0;
999 1.1 jmmv }
1000 1.1 jmmv
1001 1.9 jmmv /*
1002 1.67 rmind * tmpfs_chmod: change access mode on the given vnode.
1003 1.9 jmmv */
1004 1.1 jmmv int
1005 1.67 rmind tmpfs_chmod(vnode_t *vp, mode_t mode, kauth_cred_t cred, lwp_t *l)
1006 1.1 jmmv {
1007 1.67 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1008 1.51 elad int error;
1009 1.1 jmmv
1010 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1011 1.1 jmmv
1012 1.1 jmmv /* Disallow this operation if the file system is mounted read-only. */
1013 1.1 jmmv if (vp->v_mount->mnt_flag & MNT_RDONLY)
1014 1.1 jmmv return EROFS;
1015 1.1 jmmv
1016 1.1 jmmv /* Immutable or append-only files cannot be modified, either. */
1017 1.1 jmmv if (node->tn_flags & (IMMUTABLE | APPEND))
1018 1.1 jmmv return EPERM;
1019 1.1 jmmv
1020 1.54 elad error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
1021 1.79 elad NULL, genfs_can_chmod(vp->v_type, cred, node->tn_uid, node->tn_gid, mode));
1022 1.67 rmind if (error) {
1023 1.67 rmind return error;
1024 1.67 rmind }
1025 1.1 jmmv node->tn_mode = (mode & ALLPERMS);
1026 1.90 rmind tmpfs_update(vp, TMPFS_UPDATE_CTIME);
1027 1.1 jmmv VN_KNOTE(vp, NOTE_ATTRIB);
1028 1.1 jmmv return 0;
1029 1.1 jmmv }
1030 1.1 jmmv
1031 1.9 jmmv /*
1032 1.67 rmind * tmpfs_chown: change ownership of the given vnode.
1033 1.67 rmind *
1034 1.67 rmind * => At least one of uid or gid must be different than VNOVAL.
1035 1.67 rmind * => Attribute is unchanged for VNOVAL case.
1036 1.9 jmmv */
1037 1.1 jmmv int
1038 1.67 rmind tmpfs_chown(vnode_t *vp, uid_t uid, gid_t gid, kauth_cred_t cred, lwp_t *l)
1039 1.1 jmmv {
1040 1.67 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1041 1.51 elad int error;
1042 1.1 jmmv
1043 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1044 1.1 jmmv
1045 1.1 jmmv /* Assign default values if they are unknown. */
1046 1.1 jmmv KASSERT(uid != VNOVAL || gid != VNOVAL);
1047 1.67 rmind if (uid == VNOVAL) {
1048 1.1 jmmv uid = node->tn_uid;
1049 1.67 rmind }
1050 1.67 rmind if (gid == VNOVAL) {
1051 1.1 jmmv gid = node->tn_gid;
1052 1.67 rmind }
1053 1.1 jmmv
1054 1.1 jmmv /* Disallow this operation if the file system is mounted read-only. */
1055 1.1 jmmv if (vp->v_mount->mnt_flag & MNT_RDONLY)
1056 1.1 jmmv return EROFS;
1057 1.1 jmmv
1058 1.1 jmmv /* Immutable or append-only files cannot be modified, either. */
1059 1.1 jmmv if (node->tn_flags & (IMMUTABLE | APPEND))
1060 1.1 jmmv return EPERM;
1061 1.1 jmmv
1062 1.54 elad error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
1063 1.79 elad NULL, genfs_can_chown(cred, node->tn_uid, node->tn_gid, uid,
1064 1.67 rmind gid));
1065 1.67 rmind if (error) {
1066 1.67 rmind return error;
1067 1.67 rmind }
1068 1.1 jmmv node->tn_uid = uid;
1069 1.1 jmmv node->tn_gid = gid;
1070 1.90 rmind tmpfs_update(vp, TMPFS_UPDATE_CTIME);
1071 1.1 jmmv VN_KNOTE(vp, NOTE_ATTRIB);
1072 1.1 jmmv return 0;
1073 1.1 jmmv }
1074 1.1 jmmv
1075 1.9 jmmv /*
1076 1.67 rmind * tmpfs_chsize: change size of the given vnode.
1077 1.9 jmmv */
1078 1.1 jmmv int
1079 1.67 rmind tmpfs_chsize(vnode_t *vp, u_quad_t size, kauth_cred_t cred, lwp_t *l)
1080 1.1 jmmv {
1081 1.67 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1082 1.90 rmind const off_t length = size;
1083 1.90 rmind int error;
1084 1.1 jmmv
1085 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1086 1.1 jmmv
1087 1.1 jmmv /* Decide whether this is a valid operation based on the file type. */
1088 1.1 jmmv switch (vp->v_type) {
1089 1.1 jmmv case VDIR:
1090 1.1 jmmv return EISDIR;
1091 1.1 jmmv case VREG:
1092 1.67 rmind if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1093 1.1 jmmv return EROFS;
1094 1.67 rmind }
1095 1.1 jmmv break;
1096 1.1 jmmv case VBLK:
1097 1.1 jmmv case VCHR:
1098 1.1 jmmv case VFIFO:
1099 1.67 rmind /*
1100 1.67 rmind * Allow modifications of special files even if in the file
1101 1.1 jmmv * system is mounted read-only (we are not modifying the
1102 1.67 rmind * files themselves, but the objects they represent).
1103 1.67 rmind */
1104 1.14 yamt return 0;
1105 1.1 jmmv default:
1106 1.14 yamt return EOPNOTSUPP;
1107 1.1 jmmv }
1108 1.1 jmmv
1109 1.1 jmmv /* Immutable or append-only files cannot be modified, either. */
1110 1.67 rmind if (node->tn_flags & (IMMUTABLE | APPEND)) {
1111 1.1 jmmv return EPERM;
1112 1.67 rmind }
1113 1.1 jmmv
1114 1.90 rmind if (length < 0) {
1115 1.90 rmind return EINVAL;
1116 1.90 rmind }
1117 1.90 rmind if (node->tn_size == length) {
1118 1.90 rmind return 0;
1119 1.90 rmind }
1120 1.90 rmind
1121 1.90 rmind /* Note: tmpfs_reg_resize() will raise NOTE_EXTEND and NOTE_ATTRIB. */
1122 1.90 rmind if ((error = tmpfs_reg_resize(vp, length)) != 0) {
1123 1.90 rmind return error;
1124 1.90 rmind }
1125 1.90 rmind tmpfs_update(vp, TMPFS_UPDATE_CTIME | TMPFS_UPDATE_MTIME);
1126 1.90 rmind return 0;
1127 1.1 jmmv }
1128 1.1 jmmv
1129 1.9 jmmv /*
1130 1.67 rmind * tmpfs_chtimes: change access and modification times for vnode.
1131 1.9 jmmv */
1132 1.1 jmmv int
1133 1.67 rmind tmpfs_chtimes(vnode_t *vp, const struct timespec *atime,
1134 1.48 christos const struct timespec *mtime, const struct timespec *btime,
1135 1.67 rmind int vaflags, kauth_cred_t cred, lwp_t *l)
1136 1.1 jmmv {
1137 1.67 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1138 1.1 jmmv int error;
1139 1.1 jmmv
1140 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1141 1.1 jmmv
1142 1.1 jmmv /* Disallow this operation if the file system is mounted read-only. */
1143 1.1 jmmv if (vp->v_mount->mnt_flag & MNT_RDONLY)
1144 1.1 jmmv return EROFS;
1145 1.1 jmmv
1146 1.1 jmmv /* Immutable or append-only files cannot be modified, either. */
1147 1.1 jmmv if (node->tn_flags & (IMMUTABLE | APPEND))
1148 1.1 jmmv return EPERM;
1149 1.1 jmmv
1150 1.54 elad error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
1151 1.67 rmind genfs_can_chtimes(vp, vaflags, node->tn_uid, cred));
1152 1.53 elad if (error)
1153 1.67 rmind return error;
1154 1.1 jmmv
1155 1.90 rmind if (atime->tv_sec != VNOVAL) {
1156 1.90 rmind node->tn_atime = *atime;
1157 1.90 rmind }
1158 1.90 rmind if (mtime->tv_sec != VNOVAL) {
1159 1.90 rmind node->tn_mtime = *mtime;
1160 1.90 rmind }
1161 1.90 rmind if (btime->tv_sec != VNOVAL) {
1162 1.90 rmind node->tn_birthtime = *btime;
1163 1.90 rmind }
1164 1.29 jmmv VN_KNOTE(vp, NOTE_ATTRIB);
1165 1.12 yamt return 0;
1166 1.1 jmmv }
1167 1.10 yamt
1168 1.67 rmind /*
1169 1.90 rmind * tmpfs_update: update the timestamps as indicated by the flags.
1170 1.67 rmind */
1171 1.10 yamt void
1172 1.90 rmind tmpfs_update(vnode_t *vp, unsigned tflags)
1173 1.10 yamt {
1174 1.67 rmind tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1175 1.56 rmind struct timespec nowtm;
1176 1.10 yamt
1177 1.90 rmind if (tflags == 0) {
1178 1.10 yamt return;
1179 1.67 rmind }
1180 1.56 rmind vfs_timestamp(&nowtm);
1181 1.48 christos
1182 1.90 rmind if (tflags & TMPFS_UPDATE_ATIME) {
1183 1.90 rmind node->tn_atime = nowtm;
1184 1.10 yamt }
1185 1.90 rmind if (tflags & TMPFS_UPDATE_MTIME) {
1186 1.90 rmind node->tn_mtime = nowtm;
1187 1.10 yamt }
1188 1.90 rmind if (tflags & TMPFS_UPDATE_CTIME) {
1189 1.56 rmind node->tn_ctime = nowtm;
1190 1.48 christos }
1191 1.12 yamt }
1192