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