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