Deleted Added
full compact
tmpfs_vnops.c (236208) tmpfs_vnops.c (236209)
1/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
2
3/*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * tmpfs vnode interface.
35 */
36#include <sys/cdefs.h>
1/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
2
3/*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * tmpfs vnode interface.
35 */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: stable/9/sys/fs/tmpfs/tmpfs_vnops.c 236208 2012-05-28 22:58:13Z alc $");
37__FBSDID("$FreeBSD: stable/9/sys/fs/tmpfs/tmpfs_vnops.c 236209 2012-05-28 23:31:48Z alc $");
38
39#include <sys/param.h>
40#include <sys/fcntl.h>
41#include <sys/lockf.h>
42#include <sys/namei.h>
43#include <sys/priv.h>
44#include <sys/proc.h>
45#include <sys/sched.h>
46#include <sys/sf_buf.h>
47#include <sys/stat.h>
48#include <sys/systm.h>
49#include <sys/sysctl.h>
50#include <sys/unistd.h>
51#include <sys/vnode.h>
52
53#include <vm/vm.h>
54#include <vm/vm_object.h>
55#include <vm/vm_page.h>
56#include <vm/vm_pager.h>
57
58#include <machine/_inttypes.h>
59
60#include <fs/fifofs/fifo.h>
61#include <fs/tmpfs/tmpfs_vnops.h>
62#include <fs/tmpfs/tmpfs.h>
63
64SYSCTL_DECL(_vfs_tmpfs);
65
66static volatile int tmpfs_rename_restarts;
67SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
68 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
69 "Times rename had to restart due to lock contention");
70
71/* --------------------------------------------------------------------- */
72
73static int
74tmpfs_lookup(struct vop_cachedlookup_args *v)
75{
76 struct vnode *dvp = v->a_dvp;
77 struct vnode **vpp = v->a_vpp;
78 struct componentname *cnp = v->a_cnp;
79
80 int error;
81 struct tmpfs_dirent *de;
82 struct tmpfs_node *dnode;
83
84 dnode = VP_TO_TMPFS_DIR(dvp);
85 *vpp = NULLVP;
86
87 /* Check accessibility of requested node as a first step. */
88 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
89 if (error != 0)
90 goto out;
91
92 /* We cannot be requesting the parent directory of the root node. */
93 MPASS(IMPLIES(dnode->tn_type == VDIR &&
94 dnode->tn_dir.tn_parent == dnode,
95 !(cnp->cn_flags & ISDOTDOT)));
96
97 TMPFS_ASSERT_LOCKED(dnode);
98 if (dnode->tn_dir.tn_parent == NULL) {
99 error = ENOENT;
100 goto out;
101 }
102 if (cnp->cn_flags & ISDOTDOT) {
103 int ltype = 0;
104
105 ltype = VOP_ISLOCKED(dvp);
106 vhold(dvp);
107 VOP_UNLOCK(dvp, 0);
108 /* Allocate a new vnode on the matching entry. */
109 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
110 cnp->cn_lkflags, vpp);
111
112 vn_lock(dvp, ltype | LK_RETRY);
113 vdrop(dvp);
114 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
115 VREF(dvp);
116 *vpp = dvp;
117 error = 0;
118 } else {
119 de = tmpfs_dir_lookup(dnode, NULL, cnp);
120 if (de != NULL && de->td_node == NULL)
121 cnp->cn_flags |= ISWHITEOUT;
122 if (de == NULL || de->td_node == NULL) {
123 /* The entry was not found in the directory.
124 * This is OK if we are creating or renaming an
125 * entry and are working on the last component of
126 * the path name. */
127 if ((cnp->cn_flags & ISLASTCN) &&
128 (cnp->cn_nameiop == CREATE || \
129 cnp->cn_nameiop == RENAME ||
130 (cnp->cn_nameiop == DELETE &&
131 cnp->cn_flags & DOWHITEOUT &&
132 cnp->cn_flags & ISWHITEOUT))) {
133 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
134 cnp->cn_thread);
135 if (error != 0)
136 goto out;
137
138 /* Keep the component name in the buffer for
139 * future uses. */
140 cnp->cn_flags |= SAVENAME;
141
142 error = EJUSTRETURN;
143 } else
144 error = ENOENT;
145 } else {
146 struct tmpfs_node *tnode;
147
148 /* The entry was found, so get its associated
149 * tmpfs_node. */
150 tnode = de->td_node;
151
152 /* If we are not at the last path component and
153 * found a non-directory or non-link entry (which
154 * may itself be pointing to a directory), raise
155 * an error. */
156 if ((tnode->tn_type != VDIR &&
157 tnode->tn_type != VLNK) &&
158 !(cnp->cn_flags & ISLASTCN)) {
159 error = ENOTDIR;
160 goto out;
161 }
162
163 /* If we are deleting or renaming the entry, keep
164 * track of its tmpfs_dirent so that it can be
165 * easily deleted later. */
166 if ((cnp->cn_flags & ISLASTCN) &&
167 (cnp->cn_nameiop == DELETE ||
168 cnp->cn_nameiop == RENAME)) {
169 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
170 cnp->cn_thread);
171 if (error != 0)
172 goto out;
173
174 /* Allocate a new vnode on the matching entry. */
175 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
176 cnp->cn_lkflags, vpp);
177 if (error != 0)
178 goto out;
179
180 if ((dnode->tn_mode & S_ISTXT) &&
181 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
182 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
183 error = EPERM;
184 vput(*vpp);
185 *vpp = NULL;
186 goto out;
187 }
188 cnp->cn_flags |= SAVENAME;
189 } else {
190 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
191 cnp->cn_lkflags, vpp);
192 }
193 }
194 }
195
196 /* Store the result of this lookup in the cache. Avoid this if the
197 * request was for creation, as it does not improve timings on
198 * emprical tests. */
199 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
200 cache_enter(dvp, *vpp, cnp);
201
202out:
203 /* If there were no errors, *vpp cannot be null and it must be
204 * locked. */
205 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
206
207 return error;
208}
209
210/* --------------------------------------------------------------------- */
211
212static int
213tmpfs_create(struct vop_create_args *v)
214{
215 struct vnode *dvp = v->a_dvp;
216 struct vnode **vpp = v->a_vpp;
217 struct componentname *cnp = v->a_cnp;
218 struct vattr *vap = v->a_vap;
219
220 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
221
222 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
223}
224/* --------------------------------------------------------------------- */
225
226static int
227tmpfs_mknod(struct vop_mknod_args *v)
228{
229 struct vnode *dvp = v->a_dvp;
230 struct vnode **vpp = v->a_vpp;
231 struct componentname *cnp = v->a_cnp;
232 struct vattr *vap = v->a_vap;
233
234 if (vap->va_type != VBLK && vap->va_type != VCHR &&
235 vap->va_type != VFIFO)
236 return EINVAL;
237
238 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
239}
240
241/* --------------------------------------------------------------------- */
242
243static int
244tmpfs_open(struct vop_open_args *v)
245{
246 struct vnode *vp = v->a_vp;
247 int mode = v->a_mode;
248
249 int error;
250 struct tmpfs_node *node;
251
252 MPASS(VOP_ISLOCKED(vp));
253
254 node = VP_TO_TMPFS_NODE(vp);
255
256 /* The file is still active but all its names have been removed
257 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
258 * it is about to die. */
259 if (node->tn_links < 1)
260 return (ENOENT);
261
262 /* If the file is marked append-only, deny write requests. */
263 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
264 error = EPERM;
265 else {
266 error = 0;
267 vnode_create_vobject(vp, node->tn_size, v->a_td);
268 }
269
270 MPASS(VOP_ISLOCKED(vp));
271 return error;
272}
273
274/* --------------------------------------------------------------------- */
275
276static int
277tmpfs_close(struct vop_close_args *v)
278{
279 struct vnode *vp = v->a_vp;
280
281 MPASS(VOP_ISLOCKED(vp));
282
283 /* Update node times. */
284 tmpfs_update(vp);
285
286 return (0);
287}
288
289/* --------------------------------------------------------------------- */
290
291int
292tmpfs_access(struct vop_access_args *v)
293{
294 struct vnode *vp = v->a_vp;
295 accmode_t accmode = v->a_accmode;
296 struct ucred *cred = v->a_cred;
297
298 int error;
299 struct tmpfs_node *node;
300
301 MPASS(VOP_ISLOCKED(vp));
302
303 node = VP_TO_TMPFS_NODE(vp);
304
305 switch (vp->v_type) {
306 case VDIR:
307 /* FALLTHROUGH */
308 case VLNK:
309 /* FALLTHROUGH */
310 case VREG:
311 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
312 error = EROFS;
313 goto out;
314 }
315 break;
316
317 case VBLK:
318 /* FALLTHROUGH */
319 case VCHR:
320 /* FALLTHROUGH */
321 case VSOCK:
322 /* FALLTHROUGH */
323 case VFIFO:
324 break;
325
326 default:
327 error = EINVAL;
328 goto out;
329 }
330
331 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
332 error = EPERM;
333 goto out;
334 }
335
336 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
337 node->tn_gid, accmode, cred, NULL);
338
339out:
340 MPASS(VOP_ISLOCKED(vp));
341
342 return error;
343}
344
345/* --------------------------------------------------------------------- */
346
347int
348tmpfs_getattr(struct vop_getattr_args *v)
349{
350 struct vnode *vp = v->a_vp;
351 struct vattr *vap = v->a_vap;
352
353 struct tmpfs_node *node;
354
355 node = VP_TO_TMPFS_NODE(vp);
356
357 tmpfs_update(vp);
358
359 vap->va_type = vp->v_type;
360 vap->va_mode = node->tn_mode;
361 vap->va_nlink = node->tn_links;
362 vap->va_uid = node->tn_uid;
363 vap->va_gid = node->tn_gid;
364 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
365 vap->va_fileid = node->tn_id;
366 vap->va_size = node->tn_size;
367 vap->va_blocksize = PAGE_SIZE;
368 vap->va_atime = node->tn_atime;
369 vap->va_mtime = node->tn_mtime;
370 vap->va_ctime = node->tn_ctime;
371 vap->va_birthtime = node->tn_birthtime;
372 vap->va_gen = node->tn_gen;
373 vap->va_flags = node->tn_flags;
374 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
375 node->tn_rdev : NODEV;
376 vap->va_bytes = round_page(node->tn_size);
377 vap->va_filerev = 0;
378
379 return 0;
380}
381
382/* --------------------------------------------------------------------- */
383
384/* XXX Should this operation be atomic? I think it should, but code in
385 * XXX other places (e.g., ufs) doesn't seem to be... */
386int
387tmpfs_setattr(struct vop_setattr_args *v)
388{
389 struct vnode *vp = v->a_vp;
390 struct vattr *vap = v->a_vap;
391 struct ucred *cred = v->a_cred;
392 struct thread *td = curthread;
393
394 int error;
395
396 MPASS(VOP_ISLOCKED(vp));
397
398 error = 0;
399
400 /* Abort if any unsettable attribute is given. */
401 if (vap->va_type != VNON ||
402 vap->va_nlink != VNOVAL ||
403 vap->va_fsid != VNOVAL ||
404 vap->va_fileid != VNOVAL ||
405 vap->va_blocksize != VNOVAL ||
406 vap->va_gen != VNOVAL ||
407 vap->va_rdev != VNOVAL ||
408 vap->va_bytes != VNOVAL)
409 error = EINVAL;
410
411 if (error == 0 && (vap->va_flags != VNOVAL))
412 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
413
414 if (error == 0 && (vap->va_size != VNOVAL))
415 error = tmpfs_chsize(vp, vap->va_size, cred, td);
416
417 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
418 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
419
420 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
421 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
422
423 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
424 vap->va_atime.tv_nsec != VNOVAL) ||
425 (vap->va_mtime.tv_sec != VNOVAL &&
426 vap->va_mtime.tv_nsec != VNOVAL) ||
427 (vap->va_birthtime.tv_sec != VNOVAL &&
428 vap->va_birthtime.tv_nsec != VNOVAL)))
429 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
430 &vap->va_birthtime, vap->va_vaflags, cred, td);
431
432 /* Update the node times. We give preference to the error codes
433 * generated by this function rather than the ones that may arise
434 * from tmpfs_update. */
435 tmpfs_update(vp);
436
437 MPASS(VOP_ISLOCKED(vp));
438
439 return error;
440}
441
442/* --------------------------------------------------------------------- */
443static int
444tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
445 vm_offset_t offset, size_t tlen, struct uio *uio)
446{
447 vm_page_t m;
38
39#include <sys/param.h>
40#include <sys/fcntl.h>
41#include <sys/lockf.h>
42#include <sys/namei.h>
43#include <sys/priv.h>
44#include <sys/proc.h>
45#include <sys/sched.h>
46#include <sys/sf_buf.h>
47#include <sys/stat.h>
48#include <sys/systm.h>
49#include <sys/sysctl.h>
50#include <sys/unistd.h>
51#include <sys/vnode.h>
52
53#include <vm/vm.h>
54#include <vm/vm_object.h>
55#include <vm/vm_page.h>
56#include <vm/vm_pager.h>
57
58#include <machine/_inttypes.h>
59
60#include <fs/fifofs/fifo.h>
61#include <fs/tmpfs/tmpfs_vnops.h>
62#include <fs/tmpfs/tmpfs.h>
63
64SYSCTL_DECL(_vfs_tmpfs);
65
66static volatile int tmpfs_rename_restarts;
67SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
68 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
69 "Times rename had to restart due to lock contention");
70
71/* --------------------------------------------------------------------- */
72
73static int
74tmpfs_lookup(struct vop_cachedlookup_args *v)
75{
76 struct vnode *dvp = v->a_dvp;
77 struct vnode **vpp = v->a_vpp;
78 struct componentname *cnp = v->a_cnp;
79
80 int error;
81 struct tmpfs_dirent *de;
82 struct tmpfs_node *dnode;
83
84 dnode = VP_TO_TMPFS_DIR(dvp);
85 *vpp = NULLVP;
86
87 /* Check accessibility of requested node as a first step. */
88 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
89 if (error != 0)
90 goto out;
91
92 /* We cannot be requesting the parent directory of the root node. */
93 MPASS(IMPLIES(dnode->tn_type == VDIR &&
94 dnode->tn_dir.tn_parent == dnode,
95 !(cnp->cn_flags & ISDOTDOT)));
96
97 TMPFS_ASSERT_LOCKED(dnode);
98 if (dnode->tn_dir.tn_parent == NULL) {
99 error = ENOENT;
100 goto out;
101 }
102 if (cnp->cn_flags & ISDOTDOT) {
103 int ltype = 0;
104
105 ltype = VOP_ISLOCKED(dvp);
106 vhold(dvp);
107 VOP_UNLOCK(dvp, 0);
108 /* Allocate a new vnode on the matching entry. */
109 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
110 cnp->cn_lkflags, vpp);
111
112 vn_lock(dvp, ltype | LK_RETRY);
113 vdrop(dvp);
114 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
115 VREF(dvp);
116 *vpp = dvp;
117 error = 0;
118 } else {
119 de = tmpfs_dir_lookup(dnode, NULL, cnp);
120 if (de != NULL && de->td_node == NULL)
121 cnp->cn_flags |= ISWHITEOUT;
122 if (de == NULL || de->td_node == NULL) {
123 /* The entry was not found in the directory.
124 * This is OK if we are creating or renaming an
125 * entry and are working on the last component of
126 * the path name. */
127 if ((cnp->cn_flags & ISLASTCN) &&
128 (cnp->cn_nameiop == CREATE || \
129 cnp->cn_nameiop == RENAME ||
130 (cnp->cn_nameiop == DELETE &&
131 cnp->cn_flags & DOWHITEOUT &&
132 cnp->cn_flags & ISWHITEOUT))) {
133 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
134 cnp->cn_thread);
135 if (error != 0)
136 goto out;
137
138 /* Keep the component name in the buffer for
139 * future uses. */
140 cnp->cn_flags |= SAVENAME;
141
142 error = EJUSTRETURN;
143 } else
144 error = ENOENT;
145 } else {
146 struct tmpfs_node *tnode;
147
148 /* The entry was found, so get its associated
149 * tmpfs_node. */
150 tnode = de->td_node;
151
152 /* If we are not at the last path component and
153 * found a non-directory or non-link entry (which
154 * may itself be pointing to a directory), raise
155 * an error. */
156 if ((tnode->tn_type != VDIR &&
157 tnode->tn_type != VLNK) &&
158 !(cnp->cn_flags & ISLASTCN)) {
159 error = ENOTDIR;
160 goto out;
161 }
162
163 /* If we are deleting or renaming the entry, keep
164 * track of its tmpfs_dirent so that it can be
165 * easily deleted later. */
166 if ((cnp->cn_flags & ISLASTCN) &&
167 (cnp->cn_nameiop == DELETE ||
168 cnp->cn_nameiop == RENAME)) {
169 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
170 cnp->cn_thread);
171 if (error != 0)
172 goto out;
173
174 /* Allocate a new vnode on the matching entry. */
175 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
176 cnp->cn_lkflags, vpp);
177 if (error != 0)
178 goto out;
179
180 if ((dnode->tn_mode & S_ISTXT) &&
181 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
182 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
183 error = EPERM;
184 vput(*vpp);
185 *vpp = NULL;
186 goto out;
187 }
188 cnp->cn_flags |= SAVENAME;
189 } else {
190 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
191 cnp->cn_lkflags, vpp);
192 }
193 }
194 }
195
196 /* Store the result of this lookup in the cache. Avoid this if the
197 * request was for creation, as it does not improve timings on
198 * emprical tests. */
199 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
200 cache_enter(dvp, *vpp, cnp);
201
202out:
203 /* If there were no errors, *vpp cannot be null and it must be
204 * locked. */
205 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
206
207 return error;
208}
209
210/* --------------------------------------------------------------------- */
211
212static int
213tmpfs_create(struct vop_create_args *v)
214{
215 struct vnode *dvp = v->a_dvp;
216 struct vnode **vpp = v->a_vpp;
217 struct componentname *cnp = v->a_cnp;
218 struct vattr *vap = v->a_vap;
219
220 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
221
222 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
223}
224/* --------------------------------------------------------------------- */
225
226static int
227tmpfs_mknod(struct vop_mknod_args *v)
228{
229 struct vnode *dvp = v->a_dvp;
230 struct vnode **vpp = v->a_vpp;
231 struct componentname *cnp = v->a_cnp;
232 struct vattr *vap = v->a_vap;
233
234 if (vap->va_type != VBLK && vap->va_type != VCHR &&
235 vap->va_type != VFIFO)
236 return EINVAL;
237
238 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
239}
240
241/* --------------------------------------------------------------------- */
242
243static int
244tmpfs_open(struct vop_open_args *v)
245{
246 struct vnode *vp = v->a_vp;
247 int mode = v->a_mode;
248
249 int error;
250 struct tmpfs_node *node;
251
252 MPASS(VOP_ISLOCKED(vp));
253
254 node = VP_TO_TMPFS_NODE(vp);
255
256 /* The file is still active but all its names have been removed
257 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
258 * it is about to die. */
259 if (node->tn_links < 1)
260 return (ENOENT);
261
262 /* If the file is marked append-only, deny write requests. */
263 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
264 error = EPERM;
265 else {
266 error = 0;
267 vnode_create_vobject(vp, node->tn_size, v->a_td);
268 }
269
270 MPASS(VOP_ISLOCKED(vp));
271 return error;
272}
273
274/* --------------------------------------------------------------------- */
275
276static int
277tmpfs_close(struct vop_close_args *v)
278{
279 struct vnode *vp = v->a_vp;
280
281 MPASS(VOP_ISLOCKED(vp));
282
283 /* Update node times. */
284 tmpfs_update(vp);
285
286 return (0);
287}
288
289/* --------------------------------------------------------------------- */
290
291int
292tmpfs_access(struct vop_access_args *v)
293{
294 struct vnode *vp = v->a_vp;
295 accmode_t accmode = v->a_accmode;
296 struct ucred *cred = v->a_cred;
297
298 int error;
299 struct tmpfs_node *node;
300
301 MPASS(VOP_ISLOCKED(vp));
302
303 node = VP_TO_TMPFS_NODE(vp);
304
305 switch (vp->v_type) {
306 case VDIR:
307 /* FALLTHROUGH */
308 case VLNK:
309 /* FALLTHROUGH */
310 case VREG:
311 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
312 error = EROFS;
313 goto out;
314 }
315 break;
316
317 case VBLK:
318 /* FALLTHROUGH */
319 case VCHR:
320 /* FALLTHROUGH */
321 case VSOCK:
322 /* FALLTHROUGH */
323 case VFIFO:
324 break;
325
326 default:
327 error = EINVAL;
328 goto out;
329 }
330
331 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
332 error = EPERM;
333 goto out;
334 }
335
336 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
337 node->tn_gid, accmode, cred, NULL);
338
339out:
340 MPASS(VOP_ISLOCKED(vp));
341
342 return error;
343}
344
345/* --------------------------------------------------------------------- */
346
347int
348tmpfs_getattr(struct vop_getattr_args *v)
349{
350 struct vnode *vp = v->a_vp;
351 struct vattr *vap = v->a_vap;
352
353 struct tmpfs_node *node;
354
355 node = VP_TO_TMPFS_NODE(vp);
356
357 tmpfs_update(vp);
358
359 vap->va_type = vp->v_type;
360 vap->va_mode = node->tn_mode;
361 vap->va_nlink = node->tn_links;
362 vap->va_uid = node->tn_uid;
363 vap->va_gid = node->tn_gid;
364 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
365 vap->va_fileid = node->tn_id;
366 vap->va_size = node->tn_size;
367 vap->va_blocksize = PAGE_SIZE;
368 vap->va_atime = node->tn_atime;
369 vap->va_mtime = node->tn_mtime;
370 vap->va_ctime = node->tn_ctime;
371 vap->va_birthtime = node->tn_birthtime;
372 vap->va_gen = node->tn_gen;
373 vap->va_flags = node->tn_flags;
374 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
375 node->tn_rdev : NODEV;
376 vap->va_bytes = round_page(node->tn_size);
377 vap->va_filerev = 0;
378
379 return 0;
380}
381
382/* --------------------------------------------------------------------- */
383
384/* XXX Should this operation be atomic? I think it should, but code in
385 * XXX other places (e.g., ufs) doesn't seem to be... */
386int
387tmpfs_setattr(struct vop_setattr_args *v)
388{
389 struct vnode *vp = v->a_vp;
390 struct vattr *vap = v->a_vap;
391 struct ucred *cred = v->a_cred;
392 struct thread *td = curthread;
393
394 int error;
395
396 MPASS(VOP_ISLOCKED(vp));
397
398 error = 0;
399
400 /* Abort if any unsettable attribute is given. */
401 if (vap->va_type != VNON ||
402 vap->va_nlink != VNOVAL ||
403 vap->va_fsid != VNOVAL ||
404 vap->va_fileid != VNOVAL ||
405 vap->va_blocksize != VNOVAL ||
406 vap->va_gen != VNOVAL ||
407 vap->va_rdev != VNOVAL ||
408 vap->va_bytes != VNOVAL)
409 error = EINVAL;
410
411 if (error == 0 && (vap->va_flags != VNOVAL))
412 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
413
414 if (error == 0 && (vap->va_size != VNOVAL))
415 error = tmpfs_chsize(vp, vap->va_size, cred, td);
416
417 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
418 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
419
420 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
421 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
422
423 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
424 vap->va_atime.tv_nsec != VNOVAL) ||
425 (vap->va_mtime.tv_sec != VNOVAL &&
426 vap->va_mtime.tv_nsec != VNOVAL) ||
427 (vap->va_birthtime.tv_sec != VNOVAL &&
428 vap->va_birthtime.tv_nsec != VNOVAL)))
429 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
430 &vap->va_birthtime, vap->va_vaflags, cred, td);
431
432 /* Update the node times. We give preference to the error codes
433 * generated by this function rather than the ones that may arise
434 * from tmpfs_update. */
435 tmpfs_update(vp);
436
437 MPASS(VOP_ISLOCKED(vp));
438
439 return error;
440}
441
442/* --------------------------------------------------------------------- */
443static int
444tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
445 vm_offset_t offset, size_t tlen, struct uio *uio)
446{
447 vm_page_t m;
448 int error;
448 int error, rv;
449
450 VM_OBJECT_LOCK(tobj);
449
450 VM_OBJECT_LOCK(tobj);
451 vm_object_pip_add(tobj, 1);
452 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
453 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
454 if (m->valid != VM_PAGE_BITS_ALL) {
455 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
451 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
452 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
453 if (m->valid != VM_PAGE_BITS_ALL) {
454 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
456 error = vm_pager_get_pages(tobj, &m, 1, 0);
457 if (error != 0) {
458 printf("tmpfs get pages from pager error [read]\n");
459 goto out;
455 rv = vm_pager_get_pages(tobj, &m, 1, 0);
456 if (rv != VM_PAGER_OK) {
457 vm_page_lock(m);
458 vm_page_free(m);
459 vm_page_unlock(m);
460 VM_OBJECT_UNLOCK(tobj);
461 return (EIO);
460 }
461 } else
462 vm_page_zero_invalid(m, TRUE);
463 }
464 VM_OBJECT_UNLOCK(tobj);
465 error = uiomove_fromphys(&m, offset, tlen, uio);
466 VM_OBJECT_LOCK(tobj);
462 }
463 } else
464 vm_page_zero_invalid(m, TRUE);
465 }
466 VM_OBJECT_UNLOCK(tobj);
467 error = uiomove_fromphys(&m, offset, tlen, uio);
468 VM_OBJECT_LOCK(tobj);
467out:
468 vm_page_lock(m);
469 vm_page_unwire(m, TRUE);
470 vm_page_unlock(m);
471 vm_page_wakeup(m);
469 vm_page_lock(m);
470 vm_page_unwire(m, TRUE);
471 vm_page_unlock(m);
472 vm_page_wakeup(m);
472 vm_object_pip_subtract(tobj, 1);
473 VM_OBJECT_UNLOCK(tobj);
474
475 return (error);
476}
477
478static __inline int
479tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
480 vm_offset_t offset, size_t tlen, void *buf)
481{
482 struct uio uio;
483 struct iovec iov;
484
485 uio.uio_iovcnt = 1;
486 uio.uio_iov = &iov;
487 iov.iov_base = buf;
488 iov.iov_len = tlen;
489
490 uio.uio_offset = 0;
491 uio.uio_resid = tlen;
492 uio.uio_rw = UIO_READ;
493 uio.uio_segflg = UIO_SYSSPACE;
494 uio.uio_td = curthread;
495
496 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
497}
498
499static int
500tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
501{
502 struct sf_buf *sf;
503 vm_pindex_t idx;
504 vm_page_t m;
505 vm_offset_t offset;
506 off_t addr;
507 size_t tlen;
508 char *ma;
509 int error;
510
511 addr = uio->uio_offset;
512 idx = OFF_TO_IDX(addr);
513 offset = addr & PAGE_MASK;
514 tlen = MIN(PAGE_SIZE - offset, len);
515
516 if ((vobj == NULL) ||
517 (vobj->resident_page_count == 0 && vobj->cache == NULL))
518 goto nocache;
519
520 VM_OBJECT_LOCK(vobj);
521lookupvpg:
522 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
523 vm_page_is_valid(m, offset, tlen)) {
524 if ((m->oflags & VPO_BUSY) != 0) {
525 /*
526 * Reference the page before unlocking and sleeping so
527 * that the page daemon is less likely to reclaim it.
528 */
529 vm_page_reference(m);
530 vm_page_sleep(m, "tmfsmr");
531 goto lookupvpg;
532 }
533 vm_page_busy(m);
534 VM_OBJECT_UNLOCK(vobj);
535 error = uiomove_fromphys(&m, offset, tlen, uio);
536 VM_OBJECT_LOCK(vobj);
537 vm_page_wakeup(m);
538 VM_OBJECT_UNLOCK(vobj);
539 return (error);
540 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
541 KASSERT(offset == 0,
542 ("unexpected offset in tmpfs_mappedread for sendfile"));
543 if ((m->oflags & VPO_BUSY) != 0) {
544 /*
545 * Reference the page before unlocking and sleeping so
546 * that the page daemon is less likely to reclaim it.
547 */
548 vm_page_reference(m);
549 vm_page_sleep(m, "tmfsmr");
550 goto lookupvpg;
551 }
552 vm_page_busy(m);
553 VM_OBJECT_UNLOCK(vobj);
554 sched_pin();
555 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
556 ma = (char *)sf_buf_kva(sf);
557 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
558 if (error == 0) {
559 if (tlen != PAGE_SIZE)
560 bzero(ma + tlen, PAGE_SIZE - tlen);
561 uio->uio_offset += tlen;
562 uio->uio_resid -= tlen;
563 }
564 sf_buf_free(sf);
565 sched_unpin();
566 VM_OBJECT_LOCK(vobj);
567 if (error == 0)
568 m->valid = VM_PAGE_BITS_ALL;
569 vm_page_wakeup(m);
570 VM_OBJECT_UNLOCK(vobj);
571 return (error);
572 }
573 VM_OBJECT_UNLOCK(vobj);
574nocache:
575 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
576
577 return (error);
578}
579
580static int
581tmpfs_read(struct vop_read_args *v)
582{
583 struct vnode *vp = v->a_vp;
584 struct uio *uio = v->a_uio;
585
586 struct tmpfs_node *node;
587 vm_object_t uobj;
588 size_t len;
589 int resid;
590
591 int error = 0;
592
593 node = VP_TO_TMPFS_NODE(vp);
594
595 if (vp->v_type != VREG) {
596 error = EISDIR;
597 goto out;
598 }
599
600 if (uio->uio_offset < 0) {
601 error = EINVAL;
602 goto out;
603 }
604
605 node->tn_status |= TMPFS_NODE_ACCESSED;
606
607 uobj = node->tn_reg.tn_aobj;
608 while ((resid = uio->uio_resid) > 0) {
609 error = 0;
610 if (node->tn_size <= uio->uio_offset)
611 break;
612 len = MIN(node->tn_size - uio->uio_offset, resid);
613 if (len == 0)
614 break;
615 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
616 if ((error != 0) || (resid == uio->uio_resid))
617 break;
618 }
619
620out:
621
622 return error;
623}
624
625/* --------------------------------------------------------------------- */
626
627static int
628tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
629{
630 vm_pindex_t idx;
631 vm_page_t vpg, tpg;
632 vm_offset_t offset;
633 off_t addr;
634 size_t tlen;
473 VM_OBJECT_UNLOCK(tobj);
474
475 return (error);
476}
477
478static __inline int
479tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
480 vm_offset_t offset, size_t tlen, void *buf)
481{
482 struct uio uio;
483 struct iovec iov;
484
485 uio.uio_iovcnt = 1;
486 uio.uio_iov = &iov;
487 iov.iov_base = buf;
488 iov.iov_len = tlen;
489
490 uio.uio_offset = 0;
491 uio.uio_resid = tlen;
492 uio.uio_rw = UIO_READ;
493 uio.uio_segflg = UIO_SYSSPACE;
494 uio.uio_td = curthread;
495
496 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
497}
498
499static int
500tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
501{
502 struct sf_buf *sf;
503 vm_pindex_t idx;
504 vm_page_t m;
505 vm_offset_t offset;
506 off_t addr;
507 size_t tlen;
508 char *ma;
509 int error;
510
511 addr = uio->uio_offset;
512 idx = OFF_TO_IDX(addr);
513 offset = addr & PAGE_MASK;
514 tlen = MIN(PAGE_SIZE - offset, len);
515
516 if ((vobj == NULL) ||
517 (vobj->resident_page_count == 0 && vobj->cache == NULL))
518 goto nocache;
519
520 VM_OBJECT_LOCK(vobj);
521lookupvpg:
522 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
523 vm_page_is_valid(m, offset, tlen)) {
524 if ((m->oflags & VPO_BUSY) != 0) {
525 /*
526 * Reference the page before unlocking and sleeping so
527 * that the page daemon is less likely to reclaim it.
528 */
529 vm_page_reference(m);
530 vm_page_sleep(m, "tmfsmr");
531 goto lookupvpg;
532 }
533 vm_page_busy(m);
534 VM_OBJECT_UNLOCK(vobj);
535 error = uiomove_fromphys(&m, offset, tlen, uio);
536 VM_OBJECT_LOCK(vobj);
537 vm_page_wakeup(m);
538 VM_OBJECT_UNLOCK(vobj);
539 return (error);
540 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
541 KASSERT(offset == 0,
542 ("unexpected offset in tmpfs_mappedread for sendfile"));
543 if ((m->oflags & VPO_BUSY) != 0) {
544 /*
545 * Reference the page before unlocking and sleeping so
546 * that the page daemon is less likely to reclaim it.
547 */
548 vm_page_reference(m);
549 vm_page_sleep(m, "tmfsmr");
550 goto lookupvpg;
551 }
552 vm_page_busy(m);
553 VM_OBJECT_UNLOCK(vobj);
554 sched_pin();
555 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
556 ma = (char *)sf_buf_kva(sf);
557 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
558 if (error == 0) {
559 if (tlen != PAGE_SIZE)
560 bzero(ma + tlen, PAGE_SIZE - tlen);
561 uio->uio_offset += tlen;
562 uio->uio_resid -= tlen;
563 }
564 sf_buf_free(sf);
565 sched_unpin();
566 VM_OBJECT_LOCK(vobj);
567 if (error == 0)
568 m->valid = VM_PAGE_BITS_ALL;
569 vm_page_wakeup(m);
570 VM_OBJECT_UNLOCK(vobj);
571 return (error);
572 }
573 VM_OBJECT_UNLOCK(vobj);
574nocache:
575 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
576
577 return (error);
578}
579
580static int
581tmpfs_read(struct vop_read_args *v)
582{
583 struct vnode *vp = v->a_vp;
584 struct uio *uio = v->a_uio;
585
586 struct tmpfs_node *node;
587 vm_object_t uobj;
588 size_t len;
589 int resid;
590
591 int error = 0;
592
593 node = VP_TO_TMPFS_NODE(vp);
594
595 if (vp->v_type != VREG) {
596 error = EISDIR;
597 goto out;
598 }
599
600 if (uio->uio_offset < 0) {
601 error = EINVAL;
602 goto out;
603 }
604
605 node->tn_status |= TMPFS_NODE_ACCESSED;
606
607 uobj = node->tn_reg.tn_aobj;
608 while ((resid = uio->uio_resid) > 0) {
609 error = 0;
610 if (node->tn_size <= uio->uio_offset)
611 break;
612 len = MIN(node->tn_size - uio->uio_offset, resid);
613 if (len == 0)
614 break;
615 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
616 if ((error != 0) || (resid == uio->uio_resid))
617 break;
618 }
619
620out:
621
622 return error;
623}
624
625/* --------------------------------------------------------------------- */
626
627static int
628tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
629{
630 vm_pindex_t idx;
631 vm_page_t vpg, tpg;
632 vm_offset_t offset;
633 off_t addr;
634 size_t tlen;
635 int error;
635 int error, rv;
636
637 error = 0;
638
639 addr = uio->uio_offset;
640 idx = OFF_TO_IDX(addr);
641 offset = addr & PAGE_MASK;
642 tlen = MIN(PAGE_SIZE - offset, len);
643
644 if ((vobj == NULL) ||
645 (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
646 vpg = NULL;
647 goto nocache;
648 }
649
650 VM_OBJECT_LOCK(vobj);
651lookupvpg:
652 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
653 vm_page_is_valid(vpg, offset, tlen)) {
654 if ((vpg->oflags & VPO_BUSY) != 0) {
655 /*
656 * Reference the page before unlocking and sleeping so
657 * that the page daemon is less likely to reclaim it.
658 */
659 vm_page_reference(vpg);
660 vm_page_sleep(vpg, "tmfsmw");
661 goto lookupvpg;
662 }
663 vm_page_busy(vpg);
664 vm_page_undirty(vpg);
665 VM_OBJECT_UNLOCK(vobj);
666 error = uiomove_fromphys(&vpg, offset, tlen, uio);
667 } else {
668 if (__predict_false(vobj->cache != NULL))
669 vm_page_cache_free(vobj, idx, idx + 1);
670 VM_OBJECT_UNLOCK(vobj);
671 vpg = NULL;
672 }
673nocache:
674 VM_OBJECT_LOCK(tobj);
636
637 error = 0;
638
639 addr = uio->uio_offset;
640 idx = OFF_TO_IDX(addr);
641 offset = addr & PAGE_MASK;
642 tlen = MIN(PAGE_SIZE - offset, len);
643
644 if ((vobj == NULL) ||
645 (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
646 vpg = NULL;
647 goto nocache;
648 }
649
650 VM_OBJECT_LOCK(vobj);
651lookupvpg:
652 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
653 vm_page_is_valid(vpg, offset, tlen)) {
654 if ((vpg->oflags & VPO_BUSY) != 0) {
655 /*
656 * Reference the page before unlocking and sleeping so
657 * that the page daemon is less likely to reclaim it.
658 */
659 vm_page_reference(vpg);
660 vm_page_sleep(vpg, "tmfsmw");
661 goto lookupvpg;
662 }
663 vm_page_busy(vpg);
664 vm_page_undirty(vpg);
665 VM_OBJECT_UNLOCK(vobj);
666 error = uiomove_fromphys(&vpg, offset, tlen, uio);
667 } else {
668 if (__predict_false(vobj->cache != NULL))
669 vm_page_cache_free(vobj, idx, idx + 1);
670 VM_OBJECT_UNLOCK(vobj);
671 vpg = NULL;
672 }
673nocache:
674 VM_OBJECT_LOCK(tobj);
675 vm_object_pip_add(tobj, 1);
676 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
677 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
678 if (tpg->valid != VM_PAGE_BITS_ALL) {
679 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
675 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
676 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
677 if (tpg->valid != VM_PAGE_BITS_ALL) {
678 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
680 error = vm_pager_get_pages(tobj, &tpg, 1, 0);
681 if (error != 0) {
682 printf("tmpfs get pages from pager error [write]\n");
679 rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
680 if (rv != VM_PAGER_OK) {
681 vm_page_lock(tpg);
682 vm_page_free(tpg);
683 vm_page_unlock(tpg);
684 error = EIO;
683 goto out;
684 }
685 } else
686 vm_page_zero_invalid(tpg, TRUE);
687 }
688 VM_OBJECT_UNLOCK(tobj);
689 if (vpg == NULL)
690 error = uiomove_fromphys(&tpg, offset, tlen, uio);
691 else {
692 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
693 pmap_copy_page(vpg, tpg);
694 }
695 VM_OBJECT_LOCK(tobj);
685 goto out;
686 }
687 } else
688 vm_page_zero_invalid(tpg, TRUE);
689 }
690 VM_OBJECT_UNLOCK(tobj);
691 if (vpg == NULL)
692 error = uiomove_fromphys(&tpg, offset, tlen, uio);
693 else {
694 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
695 pmap_copy_page(vpg, tpg);
696 }
697 VM_OBJECT_LOCK(tobj);
696out:
697 if (vobj != NULL)
698 VM_OBJECT_LOCK(vobj);
699 if (error == 0) {
700 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
701 ("parts of tpg invalid"));
702 vm_page_dirty(tpg);
703 }
704 vm_page_lock(tpg);
705 vm_page_unwire(tpg, TRUE);
706 vm_page_unlock(tpg);
707 vm_page_wakeup(tpg);
698 if (error == 0) {
699 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
700 ("parts of tpg invalid"));
701 vm_page_dirty(tpg);
702 }
703 vm_page_lock(tpg);
704 vm_page_unwire(tpg, TRUE);
705 vm_page_unlock(tpg);
706 vm_page_wakeup(tpg);
708 if (vpg != NULL)
707out:
708 VM_OBJECT_UNLOCK(tobj);
709 if (vpg != NULL) {
710 VM_OBJECT_LOCK(vobj);
709 vm_page_wakeup(vpg);
711 vm_page_wakeup(vpg);
710 if (vobj != NULL)
711 VM_OBJECT_UNLOCK(vobj);
712 VM_OBJECT_UNLOCK(vobj);
712 vm_object_pip_subtract(tobj, 1);
713 VM_OBJECT_UNLOCK(tobj);
713 }
714
715 return (error);
716}
717
718static int
719tmpfs_write(struct vop_write_args *v)
720{
721 struct vnode *vp = v->a_vp;
722 struct uio *uio = v->a_uio;
723 int ioflag = v->a_ioflag;
724
725 boolean_t extended;
726 int error = 0;
727 off_t oldsize;
728 struct tmpfs_node *node;
729 vm_object_t uobj;
730 size_t len;
731 int resid;
732
733 node = VP_TO_TMPFS_NODE(vp);
734 oldsize = node->tn_size;
735
736 if (uio->uio_offset < 0 || vp->v_type != VREG) {
737 error = EINVAL;
738 goto out;
739 }
740
741 if (uio->uio_resid == 0) {
742 error = 0;
743 goto out;
744 }
745
746 if (ioflag & IO_APPEND)
747 uio->uio_offset = node->tn_size;
748
749 if (uio->uio_offset + uio->uio_resid >
750 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
751 return (EFBIG);
752
753 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
754 return (EFBIG);
755
756 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
757 if (extended) {
758 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
759 FALSE);
760 if (error != 0)
761 goto out;
762 }
763
764 uobj = node->tn_reg.tn_aobj;
765 while ((resid = uio->uio_resid) > 0) {
766 if (node->tn_size <= uio->uio_offset)
767 break;
768 len = MIN(node->tn_size - uio->uio_offset, resid);
769 if (len == 0)
770 break;
771 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
772 if ((error != 0) || (resid == uio->uio_resid))
773 break;
774 }
775
776 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
777 (extended ? TMPFS_NODE_CHANGED : 0);
778
779 if (node->tn_mode & (S_ISUID | S_ISGID)) {
780 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
781 node->tn_mode &= ~(S_ISUID | S_ISGID);
782 }
783
784 if (error != 0)
785 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
786
787out:
788 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
789 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
790
791 return error;
792}
793
794/* --------------------------------------------------------------------- */
795
796static int
797tmpfs_fsync(struct vop_fsync_args *v)
798{
799 struct vnode *vp = v->a_vp;
800
801 MPASS(VOP_ISLOCKED(vp));
802
803 tmpfs_update(vp);
804
805 return 0;
806}
807
808/* --------------------------------------------------------------------- */
809
810static int
811tmpfs_remove(struct vop_remove_args *v)
812{
813 struct vnode *dvp = v->a_dvp;
814 struct vnode *vp = v->a_vp;
815
816 int error;
817 struct tmpfs_dirent *de;
818 struct tmpfs_mount *tmp;
819 struct tmpfs_node *dnode;
820 struct tmpfs_node *node;
821
822 MPASS(VOP_ISLOCKED(dvp));
823 MPASS(VOP_ISLOCKED(vp));
824
825 if (vp->v_type == VDIR) {
826 error = EISDIR;
827 goto out;
828 }
829
830 dnode = VP_TO_TMPFS_DIR(dvp);
831 node = VP_TO_TMPFS_NODE(vp);
832 tmp = VFS_TO_TMPFS(vp->v_mount);
833 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
834 MPASS(de != NULL);
835
836 /* Files marked as immutable or append-only cannot be deleted. */
837 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
838 (dnode->tn_flags & APPEND)) {
839 error = EPERM;
840 goto out;
841 }
842
843 /* Remove the entry from the directory; as it is a file, we do not
844 * have to change the number of hard links of the directory. */
845 tmpfs_dir_detach(dvp, de);
846 if (v->a_cnp->cn_flags & DOWHITEOUT)
847 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
848
849 /* Free the directory entry we just deleted. Note that the node
850 * referred by it will not be removed until the vnode is really
851 * reclaimed. */
852 tmpfs_free_dirent(tmp, de, TRUE);
853
854 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
855 error = 0;
856
857out:
858
859 return error;
860}
861
862/* --------------------------------------------------------------------- */
863
864static int
865tmpfs_link(struct vop_link_args *v)
866{
867 struct vnode *dvp = v->a_tdvp;
868 struct vnode *vp = v->a_vp;
869 struct componentname *cnp = v->a_cnp;
870
871 int error;
872 struct tmpfs_dirent *de;
873 struct tmpfs_node *node;
874
875 MPASS(VOP_ISLOCKED(dvp));
876 MPASS(cnp->cn_flags & HASBUF);
877 MPASS(dvp != vp); /* XXX When can this be false? */
878
879 node = VP_TO_TMPFS_NODE(vp);
880
881 /* XXX: Why aren't the following two tests done by the caller? */
882
883 /* Hard links of directories are forbidden. */
884 if (vp->v_type == VDIR) {
885 error = EPERM;
886 goto out;
887 }
888
889 /* Cannot create cross-device links. */
890 if (dvp->v_mount != vp->v_mount) {
891 error = EXDEV;
892 goto out;
893 }
894
895 /* Ensure that we do not overflow the maximum number of links imposed
896 * by the system. */
897 MPASS(node->tn_links <= LINK_MAX);
898 if (node->tn_links == LINK_MAX) {
899 error = EMLINK;
900 goto out;
901 }
902
903 /* We cannot create links of files marked immutable or append-only. */
904 if (node->tn_flags & (IMMUTABLE | APPEND)) {
905 error = EPERM;
906 goto out;
907 }
908
909 /* Allocate a new directory entry to represent the node. */
910 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
911 cnp->cn_nameptr, cnp->cn_namelen, &de);
912 if (error != 0)
913 goto out;
914
915 /* Insert the new directory entry into the appropriate directory. */
916 if (cnp->cn_flags & ISWHITEOUT)
917 tmpfs_dir_whiteout_remove(dvp, cnp);
918 tmpfs_dir_attach(dvp, de);
919
920 /* vp link count has changed, so update node times. */
921 node->tn_status |= TMPFS_NODE_CHANGED;
922 tmpfs_update(vp);
923
924 error = 0;
925
926out:
927 return error;
928}
929
930/* --------------------------------------------------------------------- */
931
932/*
933 * We acquire all but fdvp locks using non-blocking acquisitions. If we
934 * fail to acquire any lock in the path we will drop all held locks,
935 * acquire the new lock in a blocking fashion, and then release it and
936 * restart the rename. This acquire/release step ensures that we do not
937 * spin on a lock waiting for release. On error release all vnode locks
938 * and decrement references the way tmpfs_rename() would do.
939 */
940static int
941tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
942 struct vnode *tdvp, struct vnode **tvpp,
943 struct componentname *fcnp, struct componentname *tcnp)
944{
945 struct vnode *nvp;
946 struct mount *mp;
947 struct tmpfs_dirent *de;
948 int error, restarts = 0;
949
950 VOP_UNLOCK(tdvp, 0);
951 if (*tvpp != NULL && *tvpp != tdvp)
952 VOP_UNLOCK(*tvpp, 0);
953 mp = fdvp->v_mount;
954
955relock:
956 restarts += 1;
957 error = vn_lock(fdvp, LK_EXCLUSIVE);
958 if (error)
959 goto releout;
960 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
961 VOP_UNLOCK(fdvp, 0);
962 error = vn_lock(tdvp, LK_EXCLUSIVE);
963 if (error)
964 goto releout;
965 VOP_UNLOCK(tdvp, 0);
966 goto relock;
967 }
968 /*
969 * Re-resolve fvp to be certain it still exists and fetch the
970 * correct vnode.
971 */
972 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
973 if (de == NULL) {
974 VOP_UNLOCK(fdvp, 0);
975 VOP_UNLOCK(tdvp, 0);
976 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
977 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
978 error = EINVAL;
979 else
980 error = ENOENT;
981 goto releout;
982 }
983 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
984 if (error != 0) {
985 VOP_UNLOCK(fdvp, 0);
986 VOP_UNLOCK(tdvp, 0);
987 if (error != EBUSY)
988 goto releout;
989 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
990 if (error != 0)
991 goto releout;
992 VOP_UNLOCK(nvp, 0);
993 /*
994 * Concurrent rename race.
995 */
996 if (nvp == tdvp) {
997 vrele(nvp);
998 error = EINVAL;
999 goto releout;
1000 }
1001 vrele(*fvpp);
1002 *fvpp = nvp;
1003 goto relock;
1004 }
1005 vrele(*fvpp);
1006 *fvpp = nvp;
1007 VOP_UNLOCK(*fvpp, 0);
1008 /*
1009 * Re-resolve tvp and acquire the vnode lock if present.
1010 */
1011 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
1012 /*
1013 * If tvp disappeared we just carry on.
1014 */
1015 if (de == NULL && *tvpp != NULL) {
1016 vrele(*tvpp);
1017 *tvpp = NULL;
1018 }
1019 /*
1020 * Get the tvp ino if the lookup succeeded. We may have to restart
1021 * if the non-blocking acquire fails.
1022 */
1023 if (de != NULL) {
1024 nvp = NULL;
1025 error = tmpfs_alloc_vp(mp, de->td_node,
1026 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1027 if (*tvpp != NULL)
1028 vrele(*tvpp);
1029 *tvpp = nvp;
1030 if (error != 0) {
1031 VOP_UNLOCK(fdvp, 0);
1032 VOP_UNLOCK(tdvp, 0);
1033 if (error != EBUSY)
1034 goto releout;
1035 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
1036 &nvp);
1037 if (error != 0)
1038 goto releout;
1039 VOP_UNLOCK(nvp, 0);
1040 /*
1041 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
1042 */
1043 if (nvp == fdvp) {
1044 error = ENOTEMPTY;
1045 goto releout;
1046 }
1047 goto relock;
1048 }
1049 }
1050 tmpfs_rename_restarts += restarts;
1051
1052 return (0);
1053
1054releout:
1055 vrele(fdvp);
1056 vrele(*fvpp);
1057 vrele(tdvp);
1058 if (*tvpp != NULL)
1059 vrele(*tvpp);
1060 tmpfs_rename_restarts += restarts;
1061
1062 return (error);
1063}
1064
1065static int
1066tmpfs_rename(struct vop_rename_args *v)
1067{
1068 struct vnode *fdvp = v->a_fdvp;
1069 struct vnode *fvp = v->a_fvp;
1070 struct componentname *fcnp = v->a_fcnp;
1071 struct vnode *tdvp = v->a_tdvp;
1072 struct vnode *tvp = v->a_tvp;
1073 struct componentname *tcnp = v->a_tcnp;
1074 struct mount *mp = NULL;
1075
1076 char *newname;
1077 int error;
1078 struct tmpfs_dirent *de;
1079 struct tmpfs_mount *tmp;
1080 struct tmpfs_node *fdnode;
1081 struct tmpfs_node *fnode;
1082 struct tmpfs_node *tnode;
1083 struct tmpfs_node *tdnode;
1084
1085 MPASS(VOP_ISLOCKED(tdvp));
1086 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
1087 MPASS(fcnp->cn_flags & HASBUF);
1088 MPASS(tcnp->cn_flags & HASBUF);
1089
1090 /* Disallow cross-device renames.
1091 * XXX Why isn't this done by the caller? */
1092 if (fvp->v_mount != tdvp->v_mount ||
1093 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1094 error = EXDEV;
1095 goto out;
1096 }
1097
1098 /* If source and target are the same file, there is nothing to do. */
1099 if (fvp == tvp) {
1100 error = 0;
1101 goto out;
1102 }
1103
1104 /* If we need to move the directory between entries, lock the
1105 * source so that we can safely operate on it. */
1106 if (fdvp != tdvp && fdvp != tvp) {
1107 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1108 mp = tdvp->v_mount;
1109 error = vfs_busy(mp, 0);
1110 if (error != 0) {
1111 mp = NULL;
1112 goto out;
1113 }
1114 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1115 fcnp, tcnp);
1116 if (error != 0) {
1117 vfs_unbusy(mp);
1118 return (error);
1119 }
1120 ASSERT_VOP_ELOCKED(fdvp,
1121 "tmpfs_rename: fdvp not locked");
1122 ASSERT_VOP_ELOCKED(tdvp,
1123 "tmpfs_rename: tdvp not locked");
1124 if (tvp != NULL)
1125 ASSERT_VOP_ELOCKED(tvp,
1126 "tmpfs_rename: tvp not locked");
1127 if (fvp == tvp) {
1128 error = 0;
1129 goto out_locked;
1130 }
1131 }
1132 }
1133
1134 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1135 tdnode = VP_TO_TMPFS_DIR(tdvp);
1136 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1137 fdnode = VP_TO_TMPFS_DIR(fdvp);
1138 fnode = VP_TO_TMPFS_NODE(fvp);
1139 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1140
1141 /* Entry can disappear before we lock fdvp,
1142 * also avoid manipulating '.' and '..' entries. */
1143 if (de == NULL) {
1144 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1145 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1146 error = EINVAL;
1147 else
1148 error = ENOENT;
1149 goto out_locked;
1150 }
1151 MPASS(de->td_node == fnode);
1152
1153 /* If re-naming a directory to another preexisting directory
1154 * ensure that the target directory is empty so that its
1155 * removal causes no side effects.
1156 * Kern_rename gurantees the destination to be a directory
1157 * if the source is one. */
1158 if (tvp != NULL) {
1159 MPASS(tnode != NULL);
1160
1161 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1162 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1163 error = EPERM;
1164 goto out_locked;
1165 }
1166
1167 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1168 if (tnode->tn_size > 0) {
1169 error = ENOTEMPTY;
1170 goto out_locked;
1171 }
1172 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1173 error = ENOTDIR;
1174 goto out_locked;
1175 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1176 error = EISDIR;
1177 goto out_locked;
1178 } else {
1179 MPASS(fnode->tn_type != VDIR &&
1180 tnode->tn_type != VDIR);
1181 }
1182 }
1183
1184 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1185 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1186 error = EPERM;
1187 goto out_locked;
1188 }
1189
1190 /* Ensure that we have enough memory to hold the new name, if it
1191 * has to be changed. */
1192 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1193 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1194 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1195 } else
1196 newname = NULL;
1197
1198 /* If the node is being moved to another directory, we have to do
1199 * the move. */
1200 if (fdnode != tdnode) {
1201 /* In case we are moving a directory, we have to adjust its
1202 * parent to point to the new parent. */
1203 if (de->td_node->tn_type == VDIR) {
1204 struct tmpfs_node *n;
1205
1206 /* Ensure the target directory is not a child of the
1207 * directory being moved. Otherwise, we'd end up
1208 * with stale nodes. */
1209 n = tdnode;
1210 /* TMPFS_LOCK garanties that no nodes are freed while
1211 * traversing the list. Nodes can only be marked as
1212 * removed: tn_parent == NULL. */
1213 TMPFS_LOCK(tmp);
1214 TMPFS_NODE_LOCK(n);
1215 while (n != n->tn_dir.tn_parent) {
1216 struct tmpfs_node *parent;
1217
1218 if (n == fnode) {
1219 TMPFS_NODE_UNLOCK(n);
1220 TMPFS_UNLOCK(tmp);
1221 error = EINVAL;
1222 if (newname != NULL)
1223 free(newname, M_TMPFSNAME);
1224 goto out_locked;
1225 }
1226 parent = n->tn_dir.tn_parent;
1227 TMPFS_NODE_UNLOCK(n);
1228 if (parent == NULL) {
1229 n = NULL;
1230 break;
1231 }
1232 TMPFS_NODE_LOCK(parent);
1233 if (parent->tn_dir.tn_parent == NULL) {
1234 TMPFS_NODE_UNLOCK(parent);
1235 n = NULL;
1236 break;
1237 }
1238 n = parent;
1239 }
1240 TMPFS_UNLOCK(tmp);
1241 if (n == NULL) {
1242 error = EINVAL;
1243 if (newname != NULL)
1244 free(newname, M_TMPFSNAME);
1245 goto out_locked;
1246 }
1247 TMPFS_NODE_UNLOCK(n);
1248
1249 /* Adjust the parent pointer. */
1250 TMPFS_VALIDATE_DIR(fnode);
1251 TMPFS_NODE_LOCK(de->td_node);
1252 de->td_node->tn_dir.tn_parent = tdnode;
1253 TMPFS_NODE_UNLOCK(de->td_node);
1254
1255 /* As a result of changing the target of the '..'
1256 * entry, the link count of the source and target
1257 * directories has to be adjusted. */
1258 TMPFS_NODE_LOCK(tdnode);
1259 TMPFS_ASSERT_LOCKED(tdnode);
1260 tdnode->tn_links++;
1261 TMPFS_NODE_UNLOCK(tdnode);
1262
1263 TMPFS_NODE_LOCK(fdnode);
1264 TMPFS_ASSERT_LOCKED(fdnode);
1265 fdnode->tn_links--;
1266 TMPFS_NODE_UNLOCK(fdnode);
1267 }
1268
1269 /* Do the move: just remove the entry from the source directory
1270 * and insert it into the target one. */
1271 tmpfs_dir_detach(fdvp, de);
1272 if (fcnp->cn_flags & DOWHITEOUT)
1273 tmpfs_dir_whiteout_add(fdvp, fcnp);
1274 if (tcnp->cn_flags & ISWHITEOUT)
1275 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1276 tmpfs_dir_attach(tdvp, de);
1277 }
1278
1279 /* If the name has changed, we need to make it effective by changing
1280 * it in the directory entry. */
1281 if (newname != NULL) {
1282 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1283
1284 free(de->td_name, M_TMPFSNAME);
1285 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1286 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1287 de->td_name = newname;
1288
1289 fnode->tn_status |= TMPFS_NODE_CHANGED;
1290 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1291 }
1292
1293 /* If we are overwriting an entry, we have to remove the old one
1294 * from the target directory. */
1295 if (tvp != NULL) {
1296 /* Remove the old entry from the target directory. */
1297 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1298 tmpfs_dir_detach(tdvp, de);
1299
1300 /* Free the directory entry we just deleted. Note that the
1301 * node referred by it will not be removed until the vnode is
1302 * really reclaimed. */
1303 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1304 }
1305 cache_purge(fvp);
1306 if (tvp != NULL)
1307 cache_purge(tvp);
1308
1309 error = 0;
1310
1311out_locked:
1312 if (fdvp != tdvp && fdvp != tvp)
1313 VOP_UNLOCK(fdvp, 0);
1314
1315out:
1316 /* Release target nodes. */
1317 /* XXX: I don't understand when tdvp can be the same as tvp, but
1318 * other code takes care of this... */
1319 if (tdvp == tvp)
1320 vrele(tdvp);
1321 else
1322 vput(tdvp);
1323 if (tvp != NULL)
1324 vput(tvp);
1325
1326 /* Release source nodes. */
1327 vrele(fdvp);
1328 vrele(fvp);
1329
1330 if (mp != NULL)
1331 vfs_unbusy(mp);
1332
1333 return error;
1334}
1335
1336/* --------------------------------------------------------------------- */
1337
1338static int
1339tmpfs_mkdir(struct vop_mkdir_args *v)
1340{
1341 struct vnode *dvp = v->a_dvp;
1342 struct vnode **vpp = v->a_vpp;
1343 struct componentname *cnp = v->a_cnp;
1344 struct vattr *vap = v->a_vap;
1345
1346 MPASS(vap->va_type == VDIR);
1347
1348 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1349}
1350
1351/* --------------------------------------------------------------------- */
1352
1353static int
1354tmpfs_rmdir(struct vop_rmdir_args *v)
1355{
1356 struct vnode *dvp = v->a_dvp;
1357 struct vnode *vp = v->a_vp;
1358
1359 int error;
1360 struct tmpfs_dirent *de;
1361 struct tmpfs_mount *tmp;
1362 struct tmpfs_node *dnode;
1363 struct tmpfs_node *node;
1364
1365 MPASS(VOP_ISLOCKED(dvp));
1366 MPASS(VOP_ISLOCKED(vp));
1367
1368 tmp = VFS_TO_TMPFS(dvp->v_mount);
1369 dnode = VP_TO_TMPFS_DIR(dvp);
1370 node = VP_TO_TMPFS_DIR(vp);
1371
1372 /* Directories with more than two entries ('.' and '..') cannot be
1373 * removed. */
1374 if (node->tn_size > 0) {
1375 error = ENOTEMPTY;
1376 goto out;
1377 }
1378
1379 if ((dnode->tn_flags & APPEND)
1380 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1381 error = EPERM;
1382 goto out;
1383 }
1384
1385 /* This invariant holds only if we are not trying to remove "..".
1386 * We checked for that above so this is safe now. */
1387 MPASS(node->tn_dir.tn_parent == dnode);
1388
1389 /* Get the directory entry associated with node (vp). This was
1390 * filled by tmpfs_lookup while looking up the entry. */
1391 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1392 MPASS(TMPFS_DIRENT_MATCHES(de,
1393 v->a_cnp->cn_nameptr,
1394 v->a_cnp->cn_namelen));
1395
1396 /* Check flags to see if we are allowed to remove the directory. */
1397 if (dnode->tn_flags & APPEND
1398 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1399 error = EPERM;
1400 goto out;
1401 }
1402
1403
1404 /* Detach the directory entry from the directory (dnode). */
1405 tmpfs_dir_detach(dvp, de);
1406 if (v->a_cnp->cn_flags & DOWHITEOUT)
1407 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1408
1409 /* No vnode should be allocated for this entry from this point */
1410 TMPFS_NODE_LOCK(node);
1411 TMPFS_ASSERT_ELOCKED(node);
1412 node->tn_links--;
1413 node->tn_dir.tn_parent = NULL;
1414 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1415 TMPFS_NODE_MODIFIED;
1416
1417 TMPFS_NODE_UNLOCK(node);
1418
1419 TMPFS_NODE_LOCK(dnode);
1420 TMPFS_ASSERT_ELOCKED(dnode);
1421 dnode->tn_links--;
1422 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1423 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1424 TMPFS_NODE_UNLOCK(dnode);
1425
1426 cache_purge(dvp);
1427 cache_purge(vp);
1428
1429 /* Free the directory entry we just deleted. Note that the node
1430 * referred by it will not be removed until the vnode is really
1431 * reclaimed. */
1432 tmpfs_free_dirent(tmp, de, TRUE);
1433
1434 /* Release the deleted vnode (will destroy the node, notify
1435 * interested parties and clean it from the cache). */
1436
1437 dnode->tn_status |= TMPFS_NODE_CHANGED;
1438 tmpfs_update(dvp);
1439
1440 error = 0;
1441
1442out:
1443 return error;
1444}
1445
1446/* --------------------------------------------------------------------- */
1447
1448static int
1449tmpfs_symlink(struct vop_symlink_args *v)
1450{
1451 struct vnode *dvp = v->a_dvp;
1452 struct vnode **vpp = v->a_vpp;
1453 struct componentname *cnp = v->a_cnp;
1454 struct vattr *vap = v->a_vap;
1455 char *target = v->a_target;
1456
1457#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1458 MPASS(vap->va_type == VLNK);
1459#else
1460 vap->va_type = VLNK;
1461#endif
1462
1463 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1464}
1465
1466/* --------------------------------------------------------------------- */
1467
1468static int
1469tmpfs_readdir(struct vop_readdir_args *v)
1470{
1471 struct vnode *vp = v->a_vp;
1472 struct uio *uio = v->a_uio;
1473 int *eofflag = v->a_eofflag;
1474 u_long **cookies = v->a_cookies;
1475 int *ncookies = v->a_ncookies;
1476
1477 int error;
1478 off_t startoff;
1479 off_t cnt = 0;
1480 struct tmpfs_node *node;
1481
1482 /* This operation only makes sense on directory nodes. */
1483 if (vp->v_type != VDIR)
1484 return ENOTDIR;
1485
1486 node = VP_TO_TMPFS_DIR(vp);
1487
1488 startoff = uio->uio_offset;
1489
1490 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1491 error = tmpfs_dir_getdotdent(node, uio);
1492 if (error != 0)
1493 goto outok;
1494 cnt++;
1495 }
1496
1497 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1498 error = tmpfs_dir_getdotdotdent(node, uio);
1499 if (error != 0)
1500 goto outok;
1501 cnt++;
1502 }
1503
1504 error = tmpfs_dir_getdents(node, uio, &cnt);
1505
1506outok:
1507 MPASS(error >= -1);
1508
1509 if (error == -1)
1510 error = (cnt != 0) ? 0 : EINVAL;
1511
1512 if (eofflag != NULL)
1513 *eofflag =
1514 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1515
1516 /* Update NFS-related variables. */
1517 if (error == 0 && cookies != NULL && ncookies != NULL) {
1518 off_t i;
1519 off_t off = startoff;
1520 struct tmpfs_dirent *de = NULL;
1521
1522 *ncookies = cnt;
1523 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1524
1525 for (i = 0; i < cnt; i++) {
1526 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1527 if (off == TMPFS_DIRCOOKIE_DOT) {
1528 off = TMPFS_DIRCOOKIE_DOTDOT;
1529 } else {
1530 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1531 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1532 } else if (de != NULL) {
1533 de = TAILQ_NEXT(de, td_entries);
1534 } else {
1535 de = tmpfs_dir_lookupbycookie(node,
1536 off);
1537 MPASS(de != NULL);
1538 de = TAILQ_NEXT(de, td_entries);
1539 }
1540 if (de == NULL)
1541 off = TMPFS_DIRCOOKIE_EOF;
1542 else
1543 off = tmpfs_dircookie(de);
1544 }
1545
1546 (*cookies)[i] = off;
1547 }
1548 MPASS(uio->uio_offset == off);
1549 }
1550
1551 return error;
1552}
1553
1554/* --------------------------------------------------------------------- */
1555
1556static int
1557tmpfs_readlink(struct vop_readlink_args *v)
1558{
1559 struct vnode *vp = v->a_vp;
1560 struct uio *uio = v->a_uio;
1561
1562 int error;
1563 struct tmpfs_node *node;
1564
1565 MPASS(uio->uio_offset == 0);
1566 MPASS(vp->v_type == VLNK);
1567
1568 node = VP_TO_TMPFS_NODE(vp);
1569
1570 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1571 uio);
1572 node->tn_status |= TMPFS_NODE_ACCESSED;
1573
1574 return error;
1575}
1576
1577/* --------------------------------------------------------------------- */
1578
1579static int
1580tmpfs_inactive(struct vop_inactive_args *v)
1581{
1582 struct vnode *vp = v->a_vp;
1583 struct thread *l = v->a_td;
1584
1585 struct tmpfs_node *node;
1586
1587 MPASS(VOP_ISLOCKED(vp));
1588
1589 node = VP_TO_TMPFS_NODE(vp);
1590
1591 if (node->tn_links == 0)
1592 vrecycle(vp, l);
1593
1594 return 0;
1595}
1596
1597/* --------------------------------------------------------------------- */
1598
1599int
1600tmpfs_reclaim(struct vop_reclaim_args *v)
1601{
1602 struct vnode *vp = v->a_vp;
1603
1604 struct tmpfs_mount *tmp;
1605 struct tmpfs_node *node;
1606
1607 node = VP_TO_TMPFS_NODE(vp);
1608 tmp = VFS_TO_TMPFS(vp->v_mount);
1609
1610 vnode_destroy_vobject(vp);
1611 cache_purge(vp);
1612
1613 TMPFS_NODE_LOCK(node);
1614 TMPFS_ASSERT_ELOCKED(node);
1615 tmpfs_free_vp(vp);
1616
1617 /* If the node referenced by this vnode was deleted by the user,
1618 * we must free its associated data structures (now that the vnode
1619 * is being reclaimed). */
1620 if (node->tn_links == 0 &&
1621 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1622 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1623 TMPFS_NODE_UNLOCK(node);
1624 tmpfs_free_node(tmp, node);
1625 } else
1626 TMPFS_NODE_UNLOCK(node);
1627
1628 MPASS(vp->v_data == NULL);
1629 return 0;
1630}
1631
1632/* --------------------------------------------------------------------- */
1633
1634static int
1635tmpfs_print(struct vop_print_args *v)
1636{
1637 struct vnode *vp = v->a_vp;
1638
1639 struct tmpfs_node *node;
1640
1641 node = VP_TO_TMPFS_NODE(vp);
1642
1643 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1644 node, node->tn_flags, node->tn_links);
1645 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1646 ", status 0x%x\n",
1647 node->tn_mode, node->tn_uid, node->tn_gid,
1648 (uintmax_t)node->tn_size, node->tn_status);
1649
1650 if (vp->v_type == VFIFO)
1651 fifo_printinfo(vp);
1652
1653 printf("\n");
1654
1655 return 0;
1656}
1657
1658/* --------------------------------------------------------------------- */
1659
1660static int
1661tmpfs_pathconf(struct vop_pathconf_args *v)
1662{
1663 int name = v->a_name;
1664 register_t *retval = v->a_retval;
1665
1666 int error;
1667
1668 error = 0;
1669
1670 switch (name) {
1671 case _PC_LINK_MAX:
1672 *retval = LINK_MAX;
1673 break;
1674
1675 case _PC_NAME_MAX:
1676 *retval = NAME_MAX;
1677 break;
1678
1679 case _PC_PATH_MAX:
1680 *retval = PATH_MAX;
1681 break;
1682
1683 case _PC_PIPE_BUF:
1684 *retval = PIPE_BUF;
1685 break;
1686
1687 case _PC_CHOWN_RESTRICTED:
1688 *retval = 1;
1689 break;
1690
1691 case _PC_NO_TRUNC:
1692 *retval = 1;
1693 break;
1694
1695 case _PC_SYNC_IO:
1696 *retval = 1;
1697 break;
1698
1699 case _PC_FILESIZEBITS:
1700 *retval = 0; /* XXX Don't know which value should I return. */
1701 break;
1702
1703 default:
1704 error = EINVAL;
1705 }
1706
1707 return error;
1708}
1709
1710static int
1711tmpfs_vptofh(struct vop_vptofh_args *ap)
1712{
1713 struct tmpfs_fid *tfhp;
1714 struct tmpfs_node *node;
1715
1716 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1717 node = VP_TO_TMPFS_NODE(ap->a_vp);
1718
1719 tfhp->tf_len = sizeof(struct tmpfs_fid);
1720 tfhp->tf_id = node->tn_id;
1721 tfhp->tf_gen = node->tn_gen;
1722
1723 return (0);
1724}
1725
1726static int
1727tmpfs_whiteout(struct vop_whiteout_args *ap)
1728{
1729 struct vnode *dvp = ap->a_dvp;
1730 struct componentname *cnp = ap->a_cnp;
1731 struct tmpfs_dirent *de;
1732
1733 switch (ap->a_flags) {
1734 case LOOKUP:
1735 return (0);
1736 case CREATE:
1737 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1738 if (de != NULL)
1739 return (de->td_node == NULL ? 0 : EEXIST);
1740 return (tmpfs_dir_whiteout_add(dvp, cnp));
1741 case DELETE:
1742 tmpfs_dir_whiteout_remove(dvp, cnp);
1743 return (0);
1744 default:
1745 panic("tmpfs_whiteout: unknown op");
1746 }
1747}
1748
1749/* --------------------------------------------------------------------- */
1750
1751/*
1752 * vnode operations vector used for files stored in a tmpfs file system.
1753 */
1754struct vop_vector tmpfs_vnodeop_entries = {
1755 .vop_default = &default_vnodeops,
1756 .vop_lookup = vfs_cache_lookup,
1757 .vop_cachedlookup = tmpfs_lookup,
1758 .vop_create = tmpfs_create,
1759 .vop_mknod = tmpfs_mknod,
1760 .vop_open = tmpfs_open,
1761 .vop_close = tmpfs_close,
1762 .vop_access = tmpfs_access,
1763 .vop_getattr = tmpfs_getattr,
1764 .vop_setattr = tmpfs_setattr,
1765 .vop_read = tmpfs_read,
1766 .vop_write = tmpfs_write,
1767 .vop_fsync = tmpfs_fsync,
1768 .vop_remove = tmpfs_remove,
1769 .vop_link = tmpfs_link,
1770 .vop_rename = tmpfs_rename,
1771 .vop_mkdir = tmpfs_mkdir,
1772 .vop_rmdir = tmpfs_rmdir,
1773 .vop_symlink = tmpfs_symlink,
1774 .vop_readdir = tmpfs_readdir,
1775 .vop_readlink = tmpfs_readlink,
1776 .vop_inactive = tmpfs_inactive,
1777 .vop_reclaim = tmpfs_reclaim,
1778 .vop_print = tmpfs_print,
1779 .vop_pathconf = tmpfs_pathconf,
1780 .vop_vptofh = tmpfs_vptofh,
1781 .vop_whiteout = tmpfs_whiteout,
1782 .vop_bmap = VOP_EOPNOTSUPP,
1783};
1784
714
715 return (error);
716}
717
718static int
719tmpfs_write(struct vop_write_args *v)
720{
721 struct vnode *vp = v->a_vp;
722 struct uio *uio = v->a_uio;
723 int ioflag = v->a_ioflag;
724
725 boolean_t extended;
726 int error = 0;
727 off_t oldsize;
728 struct tmpfs_node *node;
729 vm_object_t uobj;
730 size_t len;
731 int resid;
732
733 node = VP_TO_TMPFS_NODE(vp);
734 oldsize = node->tn_size;
735
736 if (uio->uio_offset < 0 || vp->v_type != VREG) {
737 error = EINVAL;
738 goto out;
739 }
740
741 if (uio->uio_resid == 0) {
742 error = 0;
743 goto out;
744 }
745
746 if (ioflag & IO_APPEND)
747 uio->uio_offset = node->tn_size;
748
749 if (uio->uio_offset + uio->uio_resid >
750 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
751 return (EFBIG);
752
753 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
754 return (EFBIG);
755
756 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
757 if (extended) {
758 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
759 FALSE);
760 if (error != 0)
761 goto out;
762 }
763
764 uobj = node->tn_reg.tn_aobj;
765 while ((resid = uio->uio_resid) > 0) {
766 if (node->tn_size <= uio->uio_offset)
767 break;
768 len = MIN(node->tn_size - uio->uio_offset, resid);
769 if (len == 0)
770 break;
771 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
772 if ((error != 0) || (resid == uio->uio_resid))
773 break;
774 }
775
776 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
777 (extended ? TMPFS_NODE_CHANGED : 0);
778
779 if (node->tn_mode & (S_ISUID | S_ISGID)) {
780 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
781 node->tn_mode &= ~(S_ISUID | S_ISGID);
782 }
783
784 if (error != 0)
785 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
786
787out:
788 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
789 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
790
791 return error;
792}
793
794/* --------------------------------------------------------------------- */
795
796static int
797tmpfs_fsync(struct vop_fsync_args *v)
798{
799 struct vnode *vp = v->a_vp;
800
801 MPASS(VOP_ISLOCKED(vp));
802
803 tmpfs_update(vp);
804
805 return 0;
806}
807
808/* --------------------------------------------------------------------- */
809
810static int
811tmpfs_remove(struct vop_remove_args *v)
812{
813 struct vnode *dvp = v->a_dvp;
814 struct vnode *vp = v->a_vp;
815
816 int error;
817 struct tmpfs_dirent *de;
818 struct tmpfs_mount *tmp;
819 struct tmpfs_node *dnode;
820 struct tmpfs_node *node;
821
822 MPASS(VOP_ISLOCKED(dvp));
823 MPASS(VOP_ISLOCKED(vp));
824
825 if (vp->v_type == VDIR) {
826 error = EISDIR;
827 goto out;
828 }
829
830 dnode = VP_TO_TMPFS_DIR(dvp);
831 node = VP_TO_TMPFS_NODE(vp);
832 tmp = VFS_TO_TMPFS(vp->v_mount);
833 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
834 MPASS(de != NULL);
835
836 /* Files marked as immutable or append-only cannot be deleted. */
837 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
838 (dnode->tn_flags & APPEND)) {
839 error = EPERM;
840 goto out;
841 }
842
843 /* Remove the entry from the directory; as it is a file, we do not
844 * have to change the number of hard links of the directory. */
845 tmpfs_dir_detach(dvp, de);
846 if (v->a_cnp->cn_flags & DOWHITEOUT)
847 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
848
849 /* Free the directory entry we just deleted. Note that the node
850 * referred by it will not be removed until the vnode is really
851 * reclaimed. */
852 tmpfs_free_dirent(tmp, de, TRUE);
853
854 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
855 error = 0;
856
857out:
858
859 return error;
860}
861
862/* --------------------------------------------------------------------- */
863
864static int
865tmpfs_link(struct vop_link_args *v)
866{
867 struct vnode *dvp = v->a_tdvp;
868 struct vnode *vp = v->a_vp;
869 struct componentname *cnp = v->a_cnp;
870
871 int error;
872 struct tmpfs_dirent *de;
873 struct tmpfs_node *node;
874
875 MPASS(VOP_ISLOCKED(dvp));
876 MPASS(cnp->cn_flags & HASBUF);
877 MPASS(dvp != vp); /* XXX When can this be false? */
878
879 node = VP_TO_TMPFS_NODE(vp);
880
881 /* XXX: Why aren't the following two tests done by the caller? */
882
883 /* Hard links of directories are forbidden. */
884 if (vp->v_type == VDIR) {
885 error = EPERM;
886 goto out;
887 }
888
889 /* Cannot create cross-device links. */
890 if (dvp->v_mount != vp->v_mount) {
891 error = EXDEV;
892 goto out;
893 }
894
895 /* Ensure that we do not overflow the maximum number of links imposed
896 * by the system. */
897 MPASS(node->tn_links <= LINK_MAX);
898 if (node->tn_links == LINK_MAX) {
899 error = EMLINK;
900 goto out;
901 }
902
903 /* We cannot create links of files marked immutable or append-only. */
904 if (node->tn_flags & (IMMUTABLE | APPEND)) {
905 error = EPERM;
906 goto out;
907 }
908
909 /* Allocate a new directory entry to represent the node. */
910 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
911 cnp->cn_nameptr, cnp->cn_namelen, &de);
912 if (error != 0)
913 goto out;
914
915 /* Insert the new directory entry into the appropriate directory. */
916 if (cnp->cn_flags & ISWHITEOUT)
917 tmpfs_dir_whiteout_remove(dvp, cnp);
918 tmpfs_dir_attach(dvp, de);
919
920 /* vp link count has changed, so update node times. */
921 node->tn_status |= TMPFS_NODE_CHANGED;
922 tmpfs_update(vp);
923
924 error = 0;
925
926out:
927 return error;
928}
929
930/* --------------------------------------------------------------------- */
931
932/*
933 * We acquire all but fdvp locks using non-blocking acquisitions. If we
934 * fail to acquire any lock in the path we will drop all held locks,
935 * acquire the new lock in a blocking fashion, and then release it and
936 * restart the rename. This acquire/release step ensures that we do not
937 * spin on a lock waiting for release. On error release all vnode locks
938 * and decrement references the way tmpfs_rename() would do.
939 */
940static int
941tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
942 struct vnode *tdvp, struct vnode **tvpp,
943 struct componentname *fcnp, struct componentname *tcnp)
944{
945 struct vnode *nvp;
946 struct mount *mp;
947 struct tmpfs_dirent *de;
948 int error, restarts = 0;
949
950 VOP_UNLOCK(tdvp, 0);
951 if (*tvpp != NULL && *tvpp != tdvp)
952 VOP_UNLOCK(*tvpp, 0);
953 mp = fdvp->v_mount;
954
955relock:
956 restarts += 1;
957 error = vn_lock(fdvp, LK_EXCLUSIVE);
958 if (error)
959 goto releout;
960 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
961 VOP_UNLOCK(fdvp, 0);
962 error = vn_lock(tdvp, LK_EXCLUSIVE);
963 if (error)
964 goto releout;
965 VOP_UNLOCK(tdvp, 0);
966 goto relock;
967 }
968 /*
969 * Re-resolve fvp to be certain it still exists and fetch the
970 * correct vnode.
971 */
972 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
973 if (de == NULL) {
974 VOP_UNLOCK(fdvp, 0);
975 VOP_UNLOCK(tdvp, 0);
976 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
977 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
978 error = EINVAL;
979 else
980 error = ENOENT;
981 goto releout;
982 }
983 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
984 if (error != 0) {
985 VOP_UNLOCK(fdvp, 0);
986 VOP_UNLOCK(tdvp, 0);
987 if (error != EBUSY)
988 goto releout;
989 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
990 if (error != 0)
991 goto releout;
992 VOP_UNLOCK(nvp, 0);
993 /*
994 * Concurrent rename race.
995 */
996 if (nvp == tdvp) {
997 vrele(nvp);
998 error = EINVAL;
999 goto releout;
1000 }
1001 vrele(*fvpp);
1002 *fvpp = nvp;
1003 goto relock;
1004 }
1005 vrele(*fvpp);
1006 *fvpp = nvp;
1007 VOP_UNLOCK(*fvpp, 0);
1008 /*
1009 * Re-resolve tvp and acquire the vnode lock if present.
1010 */
1011 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
1012 /*
1013 * If tvp disappeared we just carry on.
1014 */
1015 if (de == NULL && *tvpp != NULL) {
1016 vrele(*tvpp);
1017 *tvpp = NULL;
1018 }
1019 /*
1020 * Get the tvp ino if the lookup succeeded. We may have to restart
1021 * if the non-blocking acquire fails.
1022 */
1023 if (de != NULL) {
1024 nvp = NULL;
1025 error = tmpfs_alloc_vp(mp, de->td_node,
1026 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1027 if (*tvpp != NULL)
1028 vrele(*tvpp);
1029 *tvpp = nvp;
1030 if (error != 0) {
1031 VOP_UNLOCK(fdvp, 0);
1032 VOP_UNLOCK(tdvp, 0);
1033 if (error != EBUSY)
1034 goto releout;
1035 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
1036 &nvp);
1037 if (error != 0)
1038 goto releout;
1039 VOP_UNLOCK(nvp, 0);
1040 /*
1041 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
1042 */
1043 if (nvp == fdvp) {
1044 error = ENOTEMPTY;
1045 goto releout;
1046 }
1047 goto relock;
1048 }
1049 }
1050 tmpfs_rename_restarts += restarts;
1051
1052 return (0);
1053
1054releout:
1055 vrele(fdvp);
1056 vrele(*fvpp);
1057 vrele(tdvp);
1058 if (*tvpp != NULL)
1059 vrele(*tvpp);
1060 tmpfs_rename_restarts += restarts;
1061
1062 return (error);
1063}
1064
1065static int
1066tmpfs_rename(struct vop_rename_args *v)
1067{
1068 struct vnode *fdvp = v->a_fdvp;
1069 struct vnode *fvp = v->a_fvp;
1070 struct componentname *fcnp = v->a_fcnp;
1071 struct vnode *tdvp = v->a_tdvp;
1072 struct vnode *tvp = v->a_tvp;
1073 struct componentname *tcnp = v->a_tcnp;
1074 struct mount *mp = NULL;
1075
1076 char *newname;
1077 int error;
1078 struct tmpfs_dirent *de;
1079 struct tmpfs_mount *tmp;
1080 struct tmpfs_node *fdnode;
1081 struct tmpfs_node *fnode;
1082 struct tmpfs_node *tnode;
1083 struct tmpfs_node *tdnode;
1084
1085 MPASS(VOP_ISLOCKED(tdvp));
1086 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
1087 MPASS(fcnp->cn_flags & HASBUF);
1088 MPASS(tcnp->cn_flags & HASBUF);
1089
1090 /* Disallow cross-device renames.
1091 * XXX Why isn't this done by the caller? */
1092 if (fvp->v_mount != tdvp->v_mount ||
1093 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1094 error = EXDEV;
1095 goto out;
1096 }
1097
1098 /* If source and target are the same file, there is nothing to do. */
1099 if (fvp == tvp) {
1100 error = 0;
1101 goto out;
1102 }
1103
1104 /* If we need to move the directory between entries, lock the
1105 * source so that we can safely operate on it. */
1106 if (fdvp != tdvp && fdvp != tvp) {
1107 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1108 mp = tdvp->v_mount;
1109 error = vfs_busy(mp, 0);
1110 if (error != 0) {
1111 mp = NULL;
1112 goto out;
1113 }
1114 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1115 fcnp, tcnp);
1116 if (error != 0) {
1117 vfs_unbusy(mp);
1118 return (error);
1119 }
1120 ASSERT_VOP_ELOCKED(fdvp,
1121 "tmpfs_rename: fdvp not locked");
1122 ASSERT_VOP_ELOCKED(tdvp,
1123 "tmpfs_rename: tdvp not locked");
1124 if (tvp != NULL)
1125 ASSERT_VOP_ELOCKED(tvp,
1126 "tmpfs_rename: tvp not locked");
1127 if (fvp == tvp) {
1128 error = 0;
1129 goto out_locked;
1130 }
1131 }
1132 }
1133
1134 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1135 tdnode = VP_TO_TMPFS_DIR(tdvp);
1136 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1137 fdnode = VP_TO_TMPFS_DIR(fdvp);
1138 fnode = VP_TO_TMPFS_NODE(fvp);
1139 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1140
1141 /* Entry can disappear before we lock fdvp,
1142 * also avoid manipulating '.' and '..' entries. */
1143 if (de == NULL) {
1144 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1145 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1146 error = EINVAL;
1147 else
1148 error = ENOENT;
1149 goto out_locked;
1150 }
1151 MPASS(de->td_node == fnode);
1152
1153 /* If re-naming a directory to another preexisting directory
1154 * ensure that the target directory is empty so that its
1155 * removal causes no side effects.
1156 * Kern_rename gurantees the destination to be a directory
1157 * if the source is one. */
1158 if (tvp != NULL) {
1159 MPASS(tnode != NULL);
1160
1161 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1162 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1163 error = EPERM;
1164 goto out_locked;
1165 }
1166
1167 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1168 if (tnode->tn_size > 0) {
1169 error = ENOTEMPTY;
1170 goto out_locked;
1171 }
1172 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1173 error = ENOTDIR;
1174 goto out_locked;
1175 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1176 error = EISDIR;
1177 goto out_locked;
1178 } else {
1179 MPASS(fnode->tn_type != VDIR &&
1180 tnode->tn_type != VDIR);
1181 }
1182 }
1183
1184 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1185 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1186 error = EPERM;
1187 goto out_locked;
1188 }
1189
1190 /* Ensure that we have enough memory to hold the new name, if it
1191 * has to be changed. */
1192 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1193 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1194 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1195 } else
1196 newname = NULL;
1197
1198 /* If the node is being moved to another directory, we have to do
1199 * the move. */
1200 if (fdnode != tdnode) {
1201 /* In case we are moving a directory, we have to adjust its
1202 * parent to point to the new parent. */
1203 if (de->td_node->tn_type == VDIR) {
1204 struct tmpfs_node *n;
1205
1206 /* Ensure the target directory is not a child of the
1207 * directory being moved. Otherwise, we'd end up
1208 * with stale nodes. */
1209 n = tdnode;
1210 /* TMPFS_LOCK garanties that no nodes are freed while
1211 * traversing the list. Nodes can only be marked as
1212 * removed: tn_parent == NULL. */
1213 TMPFS_LOCK(tmp);
1214 TMPFS_NODE_LOCK(n);
1215 while (n != n->tn_dir.tn_parent) {
1216 struct tmpfs_node *parent;
1217
1218 if (n == fnode) {
1219 TMPFS_NODE_UNLOCK(n);
1220 TMPFS_UNLOCK(tmp);
1221 error = EINVAL;
1222 if (newname != NULL)
1223 free(newname, M_TMPFSNAME);
1224 goto out_locked;
1225 }
1226 parent = n->tn_dir.tn_parent;
1227 TMPFS_NODE_UNLOCK(n);
1228 if (parent == NULL) {
1229 n = NULL;
1230 break;
1231 }
1232 TMPFS_NODE_LOCK(parent);
1233 if (parent->tn_dir.tn_parent == NULL) {
1234 TMPFS_NODE_UNLOCK(parent);
1235 n = NULL;
1236 break;
1237 }
1238 n = parent;
1239 }
1240 TMPFS_UNLOCK(tmp);
1241 if (n == NULL) {
1242 error = EINVAL;
1243 if (newname != NULL)
1244 free(newname, M_TMPFSNAME);
1245 goto out_locked;
1246 }
1247 TMPFS_NODE_UNLOCK(n);
1248
1249 /* Adjust the parent pointer. */
1250 TMPFS_VALIDATE_DIR(fnode);
1251 TMPFS_NODE_LOCK(de->td_node);
1252 de->td_node->tn_dir.tn_parent = tdnode;
1253 TMPFS_NODE_UNLOCK(de->td_node);
1254
1255 /* As a result of changing the target of the '..'
1256 * entry, the link count of the source and target
1257 * directories has to be adjusted. */
1258 TMPFS_NODE_LOCK(tdnode);
1259 TMPFS_ASSERT_LOCKED(tdnode);
1260 tdnode->tn_links++;
1261 TMPFS_NODE_UNLOCK(tdnode);
1262
1263 TMPFS_NODE_LOCK(fdnode);
1264 TMPFS_ASSERT_LOCKED(fdnode);
1265 fdnode->tn_links--;
1266 TMPFS_NODE_UNLOCK(fdnode);
1267 }
1268
1269 /* Do the move: just remove the entry from the source directory
1270 * and insert it into the target one. */
1271 tmpfs_dir_detach(fdvp, de);
1272 if (fcnp->cn_flags & DOWHITEOUT)
1273 tmpfs_dir_whiteout_add(fdvp, fcnp);
1274 if (tcnp->cn_flags & ISWHITEOUT)
1275 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1276 tmpfs_dir_attach(tdvp, de);
1277 }
1278
1279 /* If the name has changed, we need to make it effective by changing
1280 * it in the directory entry. */
1281 if (newname != NULL) {
1282 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1283
1284 free(de->td_name, M_TMPFSNAME);
1285 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1286 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1287 de->td_name = newname;
1288
1289 fnode->tn_status |= TMPFS_NODE_CHANGED;
1290 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1291 }
1292
1293 /* If we are overwriting an entry, we have to remove the old one
1294 * from the target directory. */
1295 if (tvp != NULL) {
1296 /* Remove the old entry from the target directory. */
1297 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1298 tmpfs_dir_detach(tdvp, de);
1299
1300 /* Free the directory entry we just deleted. Note that the
1301 * node referred by it will not be removed until the vnode is
1302 * really reclaimed. */
1303 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1304 }
1305 cache_purge(fvp);
1306 if (tvp != NULL)
1307 cache_purge(tvp);
1308
1309 error = 0;
1310
1311out_locked:
1312 if (fdvp != tdvp && fdvp != tvp)
1313 VOP_UNLOCK(fdvp, 0);
1314
1315out:
1316 /* Release target nodes. */
1317 /* XXX: I don't understand when tdvp can be the same as tvp, but
1318 * other code takes care of this... */
1319 if (tdvp == tvp)
1320 vrele(tdvp);
1321 else
1322 vput(tdvp);
1323 if (tvp != NULL)
1324 vput(tvp);
1325
1326 /* Release source nodes. */
1327 vrele(fdvp);
1328 vrele(fvp);
1329
1330 if (mp != NULL)
1331 vfs_unbusy(mp);
1332
1333 return error;
1334}
1335
1336/* --------------------------------------------------------------------- */
1337
1338static int
1339tmpfs_mkdir(struct vop_mkdir_args *v)
1340{
1341 struct vnode *dvp = v->a_dvp;
1342 struct vnode **vpp = v->a_vpp;
1343 struct componentname *cnp = v->a_cnp;
1344 struct vattr *vap = v->a_vap;
1345
1346 MPASS(vap->va_type == VDIR);
1347
1348 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1349}
1350
1351/* --------------------------------------------------------------------- */
1352
1353static int
1354tmpfs_rmdir(struct vop_rmdir_args *v)
1355{
1356 struct vnode *dvp = v->a_dvp;
1357 struct vnode *vp = v->a_vp;
1358
1359 int error;
1360 struct tmpfs_dirent *de;
1361 struct tmpfs_mount *tmp;
1362 struct tmpfs_node *dnode;
1363 struct tmpfs_node *node;
1364
1365 MPASS(VOP_ISLOCKED(dvp));
1366 MPASS(VOP_ISLOCKED(vp));
1367
1368 tmp = VFS_TO_TMPFS(dvp->v_mount);
1369 dnode = VP_TO_TMPFS_DIR(dvp);
1370 node = VP_TO_TMPFS_DIR(vp);
1371
1372 /* Directories with more than two entries ('.' and '..') cannot be
1373 * removed. */
1374 if (node->tn_size > 0) {
1375 error = ENOTEMPTY;
1376 goto out;
1377 }
1378
1379 if ((dnode->tn_flags & APPEND)
1380 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1381 error = EPERM;
1382 goto out;
1383 }
1384
1385 /* This invariant holds only if we are not trying to remove "..".
1386 * We checked for that above so this is safe now. */
1387 MPASS(node->tn_dir.tn_parent == dnode);
1388
1389 /* Get the directory entry associated with node (vp). This was
1390 * filled by tmpfs_lookup while looking up the entry. */
1391 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1392 MPASS(TMPFS_DIRENT_MATCHES(de,
1393 v->a_cnp->cn_nameptr,
1394 v->a_cnp->cn_namelen));
1395
1396 /* Check flags to see if we are allowed to remove the directory. */
1397 if (dnode->tn_flags & APPEND
1398 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1399 error = EPERM;
1400 goto out;
1401 }
1402
1403
1404 /* Detach the directory entry from the directory (dnode). */
1405 tmpfs_dir_detach(dvp, de);
1406 if (v->a_cnp->cn_flags & DOWHITEOUT)
1407 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1408
1409 /* No vnode should be allocated for this entry from this point */
1410 TMPFS_NODE_LOCK(node);
1411 TMPFS_ASSERT_ELOCKED(node);
1412 node->tn_links--;
1413 node->tn_dir.tn_parent = NULL;
1414 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1415 TMPFS_NODE_MODIFIED;
1416
1417 TMPFS_NODE_UNLOCK(node);
1418
1419 TMPFS_NODE_LOCK(dnode);
1420 TMPFS_ASSERT_ELOCKED(dnode);
1421 dnode->tn_links--;
1422 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1423 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1424 TMPFS_NODE_UNLOCK(dnode);
1425
1426 cache_purge(dvp);
1427 cache_purge(vp);
1428
1429 /* Free the directory entry we just deleted. Note that the node
1430 * referred by it will not be removed until the vnode is really
1431 * reclaimed. */
1432 tmpfs_free_dirent(tmp, de, TRUE);
1433
1434 /* Release the deleted vnode (will destroy the node, notify
1435 * interested parties and clean it from the cache). */
1436
1437 dnode->tn_status |= TMPFS_NODE_CHANGED;
1438 tmpfs_update(dvp);
1439
1440 error = 0;
1441
1442out:
1443 return error;
1444}
1445
1446/* --------------------------------------------------------------------- */
1447
1448static int
1449tmpfs_symlink(struct vop_symlink_args *v)
1450{
1451 struct vnode *dvp = v->a_dvp;
1452 struct vnode **vpp = v->a_vpp;
1453 struct componentname *cnp = v->a_cnp;
1454 struct vattr *vap = v->a_vap;
1455 char *target = v->a_target;
1456
1457#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1458 MPASS(vap->va_type == VLNK);
1459#else
1460 vap->va_type = VLNK;
1461#endif
1462
1463 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1464}
1465
1466/* --------------------------------------------------------------------- */
1467
1468static int
1469tmpfs_readdir(struct vop_readdir_args *v)
1470{
1471 struct vnode *vp = v->a_vp;
1472 struct uio *uio = v->a_uio;
1473 int *eofflag = v->a_eofflag;
1474 u_long **cookies = v->a_cookies;
1475 int *ncookies = v->a_ncookies;
1476
1477 int error;
1478 off_t startoff;
1479 off_t cnt = 0;
1480 struct tmpfs_node *node;
1481
1482 /* This operation only makes sense on directory nodes. */
1483 if (vp->v_type != VDIR)
1484 return ENOTDIR;
1485
1486 node = VP_TO_TMPFS_DIR(vp);
1487
1488 startoff = uio->uio_offset;
1489
1490 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1491 error = tmpfs_dir_getdotdent(node, uio);
1492 if (error != 0)
1493 goto outok;
1494 cnt++;
1495 }
1496
1497 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1498 error = tmpfs_dir_getdotdotdent(node, uio);
1499 if (error != 0)
1500 goto outok;
1501 cnt++;
1502 }
1503
1504 error = tmpfs_dir_getdents(node, uio, &cnt);
1505
1506outok:
1507 MPASS(error >= -1);
1508
1509 if (error == -1)
1510 error = (cnt != 0) ? 0 : EINVAL;
1511
1512 if (eofflag != NULL)
1513 *eofflag =
1514 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1515
1516 /* Update NFS-related variables. */
1517 if (error == 0 && cookies != NULL && ncookies != NULL) {
1518 off_t i;
1519 off_t off = startoff;
1520 struct tmpfs_dirent *de = NULL;
1521
1522 *ncookies = cnt;
1523 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1524
1525 for (i = 0; i < cnt; i++) {
1526 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1527 if (off == TMPFS_DIRCOOKIE_DOT) {
1528 off = TMPFS_DIRCOOKIE_DOTDOT;
1529 } else {
1530 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1531 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1532 } else if (de != NULL) {
1533 de = TAILQ_NEXT(de, td_entries);
1534 } else {
1535 de = tmpfs_dir_lookupbycookie(node,
1536 off);
1537 MPASS(de != NULL);
1538 de = TAILQ_NEXT(de, td_entries);
1539 }
1540 if (de == NULL)
1541 off = TMPFS_DIRCOOKIE_EOF;
1542 else
1543 off = tmpfs_dircookie(de);
1544 }
1545
1546 (*cookies)[i] = off;
1547 }
1548 MPASS(uio->uio_offset == off);
1549 }
1550
1551 return error;
1552}
1553
1554/* --------------------------------------------------------------------- */
1555
1556static int
1557tmpfs_readlink(struct vop_readlink_args *v)
1558{
1559 struct vnode *vp = v->a_vp;
1560 struct uio *uio = v->a_uio;
1561
1562 int error;
1563 struct tmpfs_node *node;
1564
1565 MPASS(uio->uio_offset == 0);
1566 MPASS(vp->v_type == VLNK);
1567
1568 node = VP_TO_TMPFS_NODE(vp);
1569
1570 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1571 uio);
1572 node->tn_status |= TMPFS_NODE_ACCESSED;
1573
1574 return error;
1575}
1576
1577/* --------------------------------------------------------------------- */
1578
1579static int
1580tmpfs_inactive(struct vop_inactive_args *v)
1581{
1582 struct vnode *vp = v->a_vp;
1583 struct thread *l = v->a_td;
1584
1585 struct tmpfs_node *node;
1586
1587 MPASS(VOP_ISLOCKED(vp));
1588
1589 node = VP_TO_TMPFS_NODE(vp);
1590
1591 if (node->tn_links == 0)
1592 vrecycle(vp, l);
1593
1594 return 0;
1595}
1596
1597/* --------------------------------------------------------------------- */
1598
1599int
1600tmpfs_reclaim(struct vop_reclaim_args *v)
1601{
1602 struct vnode *vp = v->a_vp;
1603
1604 struct tmpfs_mount *tmp;
1605 struct tmpfs_node *node;
1606
1607 node = VP_TO_TMPFS_NODE(vp);
1608 tmp = VFS_TO_TMPFS(vp->v_mount);
1609
1610 vnode_destroy_vobject(vp);
1611 cache_purge(vp);
1612
1613 TMPFS_NODE_LOCK(node);
1614 TMPFS_ASSERT_ELOCKED(node);
1615 tmpfs_free_vp(vp);
1616
1617 /* If the node referenced by this vnode was deleted by the user,
1618 * we must free its associated data structures (now that the vnode
1619 * is being reclaimed). */
1620 if (node->tn_links == 0 &&
1621 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1622 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1623 TMPFS_NODE_UNLOCK(node);
1624 tmpfs_free_node(tmp, node);
1625 } else
1626 TMPFS_NODE_UNLOCK(node);
1627
1628 MPASS(vp->v_data == NULL);
1629 return 0;
1630}
1631
1632/* --------------------------------------------------------------------- */
1633
1634static int
1635tmpfs_print(struct vop_print_args *v)
1636{
1637 struct vnode *vp = v->a_vp;
1638
1639 struct tmpfs_node *node;
1640
1641 node = VP_TO_TMPFS_NODE(vp);
1642
1643 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1644 node, node->tn_flags, node->tn_links);
1645 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1646 ", status 0x%x\n",
1647 node->tn_mode, node->tn_uid, node->tn_gid,
1648 (uintmax_t)node->tn_size, node->tn_status);
1649
1650 if (vp->v_type == VFIFO)
1651 fifo_printinfo(vp);
1652
1653 printf("\n");
1654
1655 return 0;
1656}
1657
1658/* --------------------------------------------------------------------- */
1659
1660static int
1661tmpfs_pathconf(struct vop_pathconf_args *v)
1662{
1663 int name = v->a_name;
1664 register_t *retval = v->a_retval;
1665
1666 int error;
1667
1668 error = 0;
1669
1670 switch (name) {
1671 case _PC_LINK_MAX:
1672 *retval = LINK_MAX;
1673 break;
1674
1675 case _PC_NAME_MAX:
1676 *retval = NAME_MAX;
1677 break;
1678
1679 case _PC_PATH_MAX:
1680 *retval = PATH_MAX;
1681 break;
1682
1683 case _PC_PIPE_BUF:
1684 *retval = PIPE_BUF;
1685 break;
1686
1687 case _PC_CHOWN_RESTRICTED:
1688 *retval = 1;
1689 break;
1690
1691 case _PC_NO_TRUNC:
1692 *retval = 1;
1693 break;
1694
1695 case _PC_SYNC_IO:
1696 *retval = 1;
1697 break;
1698
1699 case _PC_FILESIZEBITS:
1700 *retval = 0; /* XXX Don't know which value should I return. */
1701 break;
1702
1703 default:
1704 error = EINVAL;
1705 }
1706
1707 return error;
1708}
1709
1710static int
1711tmpfs_vptofh(struct vop_vptofh_args *ap)
1712{
1713 struct tmpfs_fid *tfhp;
1714 struct tmpfs_node *node;
1715
1716 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1717 node = VP_TO_TMPFS_NODE(ap->a_vp);
1718
1719 tfhp->tf_len = sizeof(struct tmpfs_fid);
1720 tfhp->tf_id = node->tn_id;
1721 tfhp->tf_gen = node->tn_gen;
1722
1723 return (0);
1724}
1725
1726static int
1727tmpfs_whiteout(struct vop_whiteout_args *ap)
1728{
1729 struct vnode *dvp = ap->a_dvp;
1730 struct componentname *cnp = ap->a_cnp;
1731 struct tmpfs_dirent *de;
1732
1733 switch (ap->a_flags) {
1734 case LOOKUP:
1735 return (0);
1736 case CREATE:
1737 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1738 if (de != NULL)
1739 return (de->td_node == NULL ? 0 : EEXIST);
1740 return (tmpfs_dir_whiteout_add(dvp, cnp));
1741 case DELETE:
1742 tmpfs_dir_whiteout_remove(dvp, cnp);
1743 return (0);
1744 default:
1745 panic("tmpfs_whiteout: unknown op");
1746 }
1747}
1748
1749/* --------------------------------------------------------------------- */
1750
1751/*
1752 * vnode operations vector used for files stored in a tmpfs file system.
1753 */
1754struct vop_vector tmpfs_vnodeop_entries = {
1755 .vop_default = &default_vnodeops,
1756 .vop_lookup = vfs_cache_lookup,
1757 .vop_cachedlookup = tmpfs_lookup,
1758 .vop_create = tmpfs_create,
1759 .vop_mknod = tmpfs_mknod,
1760 .vop_open = tmpfs_open,
1761 .vop_close = tmpfs_close,
1762 .vop_access = tmpfs_access,
1763 .vop_getattr = tmpfs_getattr,
1764 .vop_setattr = tmpfs_setattr,
1765 .vop_read = tmpfs_read,
1766 .vop_write = tmpfs_write,
1767 .vop_fsync = tmpfs_fsync,
1768 .vop_remove = tmpfs_remove,
1769 .vop_link = tmpfs_link,
1770 .vop_rename = tmpfs_rename,
1771 .vop_mkdir = tmpfs_mkdir,
1772 .vop_rmdir = tmpfs_rmdir,
1773 .vop_symlink = tmpfs_symlink,
1774 .vop_readdir = tmpfs_readdir,
1775 .vop_readlink = tmpfs_readlink,
1776 .vop_inactive = tmpfs_inactive,
1777 .vop_reclaim = tmpfs_reclaim,
1778 .vop_print = tmpfs_print,
1779 .vop_pathconf = tmpfs_pathconf,
1780 .vop_vptofh = tmpfs_vptofh,
1781 .vop_whiteout = tmpfs_whiteout,
1782 .vop_bmap = VOP_EOPNOTSUPP,
1783};
1784