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