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