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