tmpfs_vnops.c revision 1.63
1/*	$NetBSD: tmpfs_vnops.c,v 1.63 2009/10/06 00:17:24 rmind Exp $	*/
2
3/*
4 * Copyright (c) 2005, 2006, 2007 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
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.63 2009/10/06 00:17:24 rmind Exp $");
39
40#include <sys/param.h>
41#include <sys/dirent.h>
42#include <sys/fcntl.h>
43#include <sys/event.h>
44#include <sys/malloc.h>
45#include <sys/namei.h>
46#include <sys/proc.h>
47#include <sys/stat.h>
48#include <sys/uio.h>
49#include <sys/unistd.h>
50#include <sys/vnode.h>
51#include <sys/lockf.h>
52#include <sys/kauth.h>
53
54#include <uvm/uvm.h>
55
56#include <miscfs/fifofs/fifo.h>
57#include <miscfs/genfs/genfs.h>
58#include <fs/tmpfs/tmpfs_vnops.h>
59#include <fs/tmpfs/tmpfs.h>
60
61/* --------------------------------------------------------------------- */
62
63/*
64 * vnode operations vector used for files stored in a tmpfs file system.
65 */
66int (**tmpfs_vnodeop_p)(void *);
67const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
68	{ &vop_default_desc,		vn_default_error },
69	{ &vop_lookup_desc,		tmpfs_lookup },
70	{ &vop_create_desc,		tmpfs_create },
71	{ &vop_mknod_desc,		tmpfs_mknod },
72	{ &vop_open_desc,		tmpfs_open },
73	{ &vop_close_desc,		tmpfs_close },
74	{ &vop_access_desc,		tmpfs_access },
75	{ &vop_getattr_desc,		tmpfs_getattr },
76	{ &vop_setattr_desc,		tmpfs_setattr },
77	{ &vop_read_desc,		tmpfs_read },
78	{ &vop_write_desc,		tmpfs_write },
79	{ &vop_ioctl_desc,		tmpfs_ioctl },
80	{ &vop_fcntl_desc,		tmpfs_fcntl },
81	{ &vop_poll_desc,		tmpfs_poll },
82	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
83	{ &vop_revoke_desc,		tmpfs_revoke },
84	{ &vop_mmap_desc,		tmpfs_mmap },
85	{ &vop_fsync_desc,		tmpfs_fsync },
86	{ &vop_seek_desc,		tmpfs_seek },
87	{ &vop_remove_desc,		tmpfs_remove },
88	{ &vop_link_desc,		tmpfs_link },
89	{ &vop_rename_desc,		tmpfs_rename },
90	{ &vop_mkdir_desc,		tmpfs_mkdir },
91	{ &vop_rmdir_desc,		tmpfs_rmdir },
92	{ &vop_symlink_desc,		tmpfs_symlink },
93	{ &vop_readdir_desc,		tmpfs_readdir },
94	{ &vop_readlink_desc,		tmpfs_readlink },
95	{ &vop_abortop_desc,		tmpfs_abortop },
96	{ &vop_inactive_desc,		tmpfs_inactive },
97	{ &vop_reclaim_desc,		tmpfs_reclaim },
98	{ &vop_lock_desc,		tmpfs_lock },
99	{ &vop_unlock_desc,		tmpfs_unlock },
100	{ &vop_bmap_desc,		tmpfs_bmap },
101	{ &vop_strategy_desc,		tmpfs_strategy },
102	{ &vop_print_desc,		tmpfs_print },
103	{ &vop_pathconf_desc,		tmpfs_pathconf },
104	{ &vop_islocked_desc,		tmpfs_islocked },
105	{ &vop_advlock_desc,		tmpfs_advlock },
106	{ &vop_bwrite_desc,		tmpfs_bwrite },
107	{ &vop_getpages_desc,		tmpfs_getpages },
108	{ &vop_putpages_desc,		tmpfs_putpages },
109	{ NULL, NULL }
110};
111const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
112	{ &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
113
114/* --------------------------------------------------------------------- */
115
116int
117tmpfs_lookup(void *v)
118{
119	struct vnode *dvp = ((struct vop_lookup_args *)v)->a_dvp;
120	struct vnode **vpp = ((struct vop_lookup_args *)v)->a_vpp;
121	struct componentname *cnp = ((struct vop_lookup_args *)v)->a_cnp;
122
123	int error;
124	struct tmpfs_dirent *de;
125	struct tmpfs_node *dnode;
126
127	KASSERT(VOP_ISLOCKED(dvp));
128
129	dnode = VP_TO_TMPFS_DIR(dvp);
130	*vpp = NULL;
131
132	/* Check accessibility of requested node as a first step. */
133	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
134	if (error != 0)
135		goto out;
136
137	/* If requesting the last path component on a read-only file system
138	 * with a write operation, deny it. */
139	if ((cnp->cn_flags & ISLASTCN) &&
140	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
141	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
142		error = EROFS;
143		goto out;
144	}
145
146	/* Avoid doing a linear scan of the directory if the requested
147	 * directory/name couple is already in the cache. */
148	error = cache_lookup(dvp, vpp, cnp);
149	if (error >= 0)
150		goto out;
151
152	/* We cannot be requesting the parent directory of the root node. */
153	KASSERT(IMPLIES(dnode->tn_type == VDIR &&
154	    dnode->tn_spec.tn_dir.tn_parent == dnode,
155	    !(cnp->cn_flags & ISDOTDOT)));
156
157	if (cnp->cn_flags & ISDOTDOT) {
158		VOP_UNLOCK(dvp, 0);
159
160		/* Allocate a new vnode on the matching entry. */
161		error = tmpfs_alloc_vp(dvp->v_mount,
162		    dnode->tn_spec.tn_dir.tn_parent, vpp);
163
164		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
165	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
166		VREF(dvp);
167		*vpp = dvp;
168		error = 0;
169	} else {
170		de = tmpfs_dir_lookup(dnode, cnp);
171		if (de == NULL) {
172			/* The entry was not found in the directory.
173			 * This is OK iff we are creating or renaming an
174			 * entry and are working on the last component of
175			 * the path name. */
176			if ((cnp->cn_flags & ISLASTCN) &&
177			    (cnp->cn_nameiop == CREATE || \
178			    cnp->cn_nameiop == RENAME)) {
179				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
180				if (error != 0)
181					goto out;
182
183				/* Keep the component name in the buffer for
184				 * future uses. */
185				cnp->cn_flags |= SAVENAME;
186
187				error = EJUSTRETURN;
188			} else
189				error = ENOENT;
190		} else {
191			struct tmpfs_node *tnode;
192
193			/* The entry was found, so get its associated
194			 * tmpfs_node. */
195			tnode = de->td_node;
196
197			/* If we are not at the last path component and
198			 * found a non-directory or non-link entry (which
199			 * may itself be pointing to a directory), raise
200			 * an error. */
201			if ((tnode->tn_type != VDIR &&
202			    tnode->tn_type != VLNK) &&
203			    !(cnp->cn_flags & ISLASTCN)) {
204				error = ENOTDIR;
205				goto out;
206			}
207
208			/* Check permissions */
209			if ((cnp->cn_flags & ISLASTCN) &&
210			    (cnp->cn_nameiop == DELETE ||
211			    cnp->cn_nameiop == RENAME)) {
212				kauth_action_t action = 0;
213
214				/* This is the file-system's decision. */
215				if ((dnode->tn_mode & S_ISTXT) != 0 &&
216				    kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
217				    kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid)
218					error = EPERM;
219				else
220					error = 0;
221
222				/* Only bother if we're not already failing it. */
223				if (!error) {
224					error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
225				}
226
227				if (cnp->cn_nameiop == DELETE)
228					action |= KAUTH_VNODE_DELETE;
229				else /* if (cnp->cn_nameiop == RENAME) */
230					action |= KAUTH_VNODE_RENAME;
231
232				error = kauth_authorize_vnode(cnp->cn_cred,
233				    action, *vpp, dvp, error);
234				if (error != 0)
235					goto out;
236
237				cnp->cn_flags |= SAVENAME;
238			} else
239				de = NULL;
240
241			/* Allocate a new vnode on the matching entry. */
242			error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
243		}
244	}
245
246	/* Store the result of this lookup in the cache.  Avoid this if the
247	 * request was for creation, as it does not improve timings on
248	 * emprical tests. */
249	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
250	    (cnp->cn_flags & ISDOTDOT) == 0)
251		cache_enter(dvp, *vpp, cnp);
252
253out:
254	/* If there were no errors, *vpp cannot be null and it must be
255	 * locked. */
256	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
257
258	/* dvp must always be locked. */
259	KASSERT(VOP_ISLOCKED(dvp));
260
261	return error;
262}
263
264/* --------------------------------------------------------------------- */
265
266int
267tmpfs_create(void *v)
268{
269	struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
270	struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
271	struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
272	struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
273
274	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
275
276	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
277}
278/* --------------------------------------------------------------------- */
279
280int
281tmpfs_mknod(void *v)
282{
283	struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
284	struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
285	struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
286	struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
287
288	if (vap->va_type != VBLK && vap->va_type != VCHR &&
289	    vap->va_type != VFIFO) {
290		vput(dvp);
291		return EINVAL;
292	}
293
294	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
295}
296
297/* --------------------------------------------------------------------- */
298
299int
300tmpfs_open(void *v)
301{
302	struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
303	int mode = ((struct vop_open_args *)v)->a_mode;
304
305	int error;
306	struct tmpfs_node *node;
307
308	KASSERT(VOP_ISLOCKED(vp));
309
310	node = VP_TO_TMPFS_NODE(vp);
311
312	/* The file is still active but all its names have been removed
313	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
314	 * it is about to die. */
315	if (node->tn_links < 1) {
316		error = ENOENT;
317		goto out;
318	}
319
320	/* If the file is marked append-only, deny write requests. */
321	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
322		error = EPERM;
323	else
324		error = 0;
325
326out:
327	KASSERT(VOP_ISLOCKED(vp));
328
329	return error;
330}
331
332/* --------------------------------------------------------------------- */
333
334int
335tmpfs_close(void *v)
336{
337	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
338
339	struct tmpfs_node *node;
340
341	KASSERT(VOP_ISLOCKED(vp));
342
343	node = VP_TO_TMPFS_NODE(vp);
344
345	if (node->tn_links > 0) {
346		/* Update node times.  No need to do it if the node has
347		 * been deleted, because it will vanish after we return. */
348		tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
349	}
350
351	return 0;
352}
353
354/* --------------------------------------------------------------------- */
355
356static int
357tmpfs_check_possible(struct vnode *vp, struct tmpfs_node *node, mode_t mode)
358{
359	int error = 0;
360
361	switch (vp->v_type) {
362	case VDIR:
363		/* FALLTHROUGH */
364	case VLNK:
365		/* FALLTHROUGH */
366	case VREG:
367		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
368			error = EROFS;
369			goto out;
370		}
371		break;
372
373	case VBLK:
374		/* FALLTHROUGH */
375	case VCHR:
376		/* FALLTHROUGH */
377	case VSOCK:
378		/* FALLTHROUGH */
379	case VFIFO:
380		break;
381
382	default:
383		error = EINVAL;
384		goto out;
385	}
386
387	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
388		error = EPERM;
389		goto out;
390	}
391
392 out:
393	return error;
394}
395
396static int
397tmpfs_check_permitted(struct vnode *vp, struct tmpfs_node *node, mode_t mode,
398    kauth_cred_t cred)
399{
400
401	return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
402	    node->tn_gid, mode, cred);
403}
404
405int
406tmpfs_access(void *v)
407{
408	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
409	int mode = ((struct vop_access_args *)v)->a_mode;
410	kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred;
411
412	int error;
413	struct tmpfs_node *node;
414
415	KASSERT(VOP_ISLOCKED(vp));
416
417	node = VP_TO_TMPFS_NODE(vp);
418
419	error = tmpfs_check_possible(vp, node, mode);
420	if (error)
421		goto out;
422
423	error = tmpfs_check_permitted(vp, node, mode, cred);
424
425	error = kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp,
426	    NULL, error);
427
428out:
429	KASSERT(VOP_ISLOCKED(vp));
430
431	return error;
432}
433
434/* --------------------------------------------------------------------- */
435
436int
437tmpfs_getattr(void *v)
438{
439	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
440	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
441
442	struct tmpfs_node *node;
443
444	node = VP_TO_TMPFS_NODE(vp);
445
446	VATTR_NULL(vap);
447
448	tmpfs_itimes(vp, NULL, NULL, NULL);
449
450	vap->va_type = vp->v_type;
451	vap->va_mode = node->tn_mode;
452	vap->va_nlink = node->tn_links;
453	vap->va_uid = node->tn_uid;
454	vap->va_gid = node->tn_gid;
455	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
456	vap->va_fileid = node->tn_id;
457	vap->va_size = node->tn_size;
458	vap->va_blocksize = PAGE_SIZE;
459	vap->va_atime = node->tn_atime;
460	vap->va_mtime = node->tn_mtime;
461	vap->va_ctime = node->tn_ctime;
462	vap->va_birthtime = node->tn_birthtime;
463	vap->va_gen = node->tn_gen;
464	vap->va_flags = node->tn_flags;
465	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
466		node->tn_spec.tn_dev.tn_rdev : VNOVAL;
467	vap->va_bytes = round_page(node->tn_size);
468	vap->va_filerev = VNOVAL;
469	vap->va_vaflags = 0;
470	vap->va_spare = VNOVAL; /* XXX */
471
472	return 0;
473}
474
475/* --------------------------------------------------------------------- */
476
477#define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
478/* XXX Should this operation be atomic?  I think it should, but code in
479 * XXX other places (e.g., ufs) doesn't seem to be... */
480int
481tmpfs_setattr(void *v)
482{
483	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
484	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
485	kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred;
486	struct lwp *l = curlwp;
487
488	int error;
489
490	KASSERT(VOP_ISLOCKED(vp));
491
492	error = 0;
493
494	/* Abort if any unsettable attribute is given. */
495	if (vap->va_type != VNON ||
496	    vap->va_nlink != VNOVAL ||
497	    vap->va_fsid != VNOVAL ||
498	    vap->va_fileid != VNOVAL ||
499	    vap->va_blocksize != VNOVAL ||
500	    GOODTIME(&vap->va_ctime) ||
501	    vap->va_gen != VNOVAL ||
502	    vap->va_rdev != VNOVAL ||
503	    vap->va_bytes != VNOVAL)
504		error = EINVAL;
505
506	if (error == 0 && (vap->va_flags != VNOVAL))
507		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
508
509	if (error == 0 && (vap->va_size != VNOVAL))
510		error = tmpfs_chsize(vp, vap->va_size, cred, l);
511
512	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
513		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
514
515	if (error == 0 && (vap->va_mode != VNOVAL))
516		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
517
518	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
519	    || GOODTIME(&vap->va_birthtime)))
520		if ((error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
521		    &vap->va_birthtime, vap->va_vaflags, cred, l)) == 0)
522			return 0;
523
524	/* Update the node times.  We give preference to the error codes
525	 * generated by this function rather than the ones that may arise
526	 * from tmpfs_update. */
527	tmpfs_update(vp, NULL, NULL, NULL, 0);
528
529	KASSERT(VOP_ISLOCKED(vp));
530
531	return error;
532}
533
534/* --------------------------------------------------------------------- */
535
536int
537tmpfs_read(void *v)
538{
539	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
540	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
541	int ioflag = ((struct vop_read_args *)v)->a_ioflag;
542
543	int error;
544	struct tmpfs_node *node;
545	struct uvm_object *uobj;
546
547	KASSERT(VOP_ISLOCKED(vp));
548
549	node = VP_TO_TMPFS_NODE(vp);
550
551	if (vp->v_type != VREG) {
552		error = EISDIR;
553		goto out;
554	}
555
556	if (uio->uio_offset < 0) {
557		error = EINVAL;
558		goto out;
559	}
560
561	node->tn_status |= TMPFS_NODE_ACCESSED;
562
563	uobj = node->tn_spec.tn_reg.tn_aobj;
564	error = 0;
565	while (error == 0 && uio->uio_resid > 0) {
566		vsize_t len;
567
568		if (node->tn_size <= uio->uio_offset)
569			break;
570
571		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
572		if (len == 0)
573			break;
574
575		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
576		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
577	}
578
579out:
580	KASSERT(VOP_ISLOCKED(vp));
581
582	return error;
583}
584
585/* --------------------------------------------------------------------- */
586
587int
588tmpfs_write(void *v)
589{
590	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
591	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
592	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
593
594	bool extended;
595	int error;
596	off_t oldsize;
597	struct tmpfs_node *node;
598	struct uvm_object *uobj;
599
600	KASSERT(VOP_ISLOCKED(vp));
601
602	node = VP_TO_TMPFS_NODE(vp);
603	oldsize = node->tn_size;
604
605	if (uio->uio_offset < 0 || vp->v_type != VREG) {
606		error = EINVAL;
607		goto out;
608	}
609
610	if (uio->uio_resid == 0) {
611		error = 0;
612		goto out;
613	}
614
615	if (ioflag & IO_APPEND)
616		uio->uio_offset = node->tn_size;
617
618	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
619	if (extended) {
620		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
621		if (error != 0)
622			goto out;
623	}
624
625	uobj = node->tn_spec.tn_reg.tn_aobj;
626	error = 0;
627	while (error == 0 && uio->uio_resid > 0) {
628		vsize_t len;
629
630		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
631		if (len == 0)
632			break;
633
634		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
635		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
636	}
637
638	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
639	    (extended ? TMPFS_NODE_CHANGED : 0);
640
641	if (error != 0)
642		(void)tmpfs_reg_resize(vp, oldsize);
643
644	VN_KNOTE(vp, NOTE_WRITE);
645
646out:
647	KASSERT(VOP_ISLOCKED(vp));
648	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
649	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
650
651	return error;
652}
653
654/* --------------------------------------------------------------------- */
655
656int
657tmpfs_fsync(void *v)
658{
659	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
660
661	KASSERT(VOP_ISLOCKED(vp));
662
663	tmpfs_update(vp, NULL, NULL, NULL, 0);
664
665	return 0;
666}
667
668/* --------------------------------------------------------------------- */
669
670int
671tmpfs_remove(void *v)
672{
673	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
674	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
675	struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp);
676
677	int error;
678	struct tmpfs_dirent *de;
679	struct tmpfs_mount *tmp;
680	struct tmpfs_node *dnode;
681	struct tmpfs_node *node;
682
683	KASSERT(VOP_ISLOCKED(dvp));
684	KASSERT(VOP_ISLOCKED(vp));
685
686	if (vp->v_type == VDIR) {
687		error = EPERM;
688		goto out;
689	}
690
691	dnode = VP_TO_TMPFS_DIR(dvp);
692	node = VP_TO_TMPFS_NODE(vp);
693	tmp = VFS_TO_TMPFS(vp->v_mount);
694	de = tmpfs_dir_lookup(dnode, cnp);
695	KASSERT(de);
696	KASSERT(de->td_node == node);
697
698	/* Files marked as immutable or append-only cannot be deleted. */
699	if (node->tn_flags & (IMMUTABLE | APPEND)) {
700		error = EPERM;
701		goto out;
702	}
703
704	/* Remove the entry from the directory; as it is a file, we do not
705	 * have to change the number of hard links of the directory. */
706	tmpfs_dir_detach(dvp, de);
707
708	/* Free the directory entry we just deleted.  Note that the node
709	 * referred by it will not be removed until the vnode is really
710	 * reclaimed. */
711	tmpfs_free_dirent(tmp, de, true);
712
713	error = 0;
714
715out:
716	vput(vp);
717	if (dvp == vp)
718		vrele(dvp);
719	else
720		vput(dvp);
721	PNBUF_PUT(cnp->cn_pnbuf);
722
723	return error;
724}
725
726/* --------------------------------------------------------------------- */
727
728int
729tmpfs_link(void *v)
730{
731	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
732	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
733	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
734
735	int error;
736	struct tmpfs_dirent *de;
737	struct tmpfs_node *dnode;
738	struct tmpfs_node *node;
739
740	KASSERT(VOP_ISLOCKED(dvp));
741	KASSERT(cnp->cn_flags & HASBUF);
742	KASSERT(dvp != vp); /* XXX When can this be false? */
743
744	dnode = VP_TO_TMPFS_DIR(dvp);
745	node = VP_TO_TMPFS_NODE(vp);
746
747	/* Lock vp because we will need to run tmpfs_update over it, which
748	 * needs the vnode to be locked. */
749	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
750
751	/* XXX: Why aren't the following two tests done by the caller? */
752
753	/* Hard links of directories are forbidden. */
754	if (vp->v_type == VDIR) {
755		error = EPERM;
756		goto out;
757	}
758
759	/* Cannot create cross-device links. */
760	if (dvp->v_mount != vp->v_mount) {
761		error = EXDEV;
762		goto out;
763	}
764
765	/* Ensure that we do not overflow the maximum number of links imposed
766	 * by the system. */
767	KASSERT(node->tn_links <= LINK_MAX);
768	if (node->tn_links == LINK_MAX) {
769		error = EMLINK;
770		goto out;
771	}
772
773	/* We cannot create links of files marked immutable or append-only. */
774	if (node->tn_flags & (IMMUTABLE | APPEND)) {
775		error = EPERM;
776		goto out;
777	}
778
779	/* Allocate a new directory entry to represent the node. */
780	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
781	    cnp->cn_nameptr, cnp->cn_namelen, &de);
782	if (error != 0)
783		goto out;
784
785	/* Insert the new directory entry into the appropriate directory. */
786	tmpfs_dir_attach(dvp, de);
787
788	/* vp link count has changed, so update node times. */
789	node->tn_status |= TMPFS_NODE_CHANGED;
790	tmpfs_update(vp, NULL, NULL, NULL, 0);
791
792	error = 0;
793
794out:
795	VOP_UNLOCK(vp, 0);
796	PNBUF_PUT(cnp->cn_pnbuf);
797	vput(dvp);
798
799	return error;
800}
801
802/*
803 * tmpfs_rename: rename routine.
804 *
805 * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
806 * and tvp (to-leaf), if exists (NULL if not).
807 *
808 * => Caller holds a reference on fdvp and fvp, they are unlocked.
809 *    Note: fdvp and fvp can refer to the same object (i.e. when it is root).
810 *
811 * => Both tdvp and tvp are referenced and locked.  It is our responsibility
812 *    to release the references and unlock them (or destroy).
813 */
814int
815tmpfs_rename(void *v)
816{
817	struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
818	struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
819	struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
820	struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
821	struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
822	struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
823
824	char *newname;
825	int error;
826	struct tmpfs_dirent *de, *de2;
827	struct tmpfs_mount *tmp;
828	struct tmpfs_node *fdnode;
829	struct tmpfs_node *fnode;
830	struct tmpfs_node *tnode;
831	struct tmpfs_node *tdnode;
832	size_t namelen;
833
834	KASSERT(VOP_ISLOCKED(tdvp));
835	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
836	KASSERT(fcnp->cn_flags & HASBUF);
837	KASSERT(tcnp->cn_flags & HASBUF);
838
839	newname = NULL;
840	namelen = 0;
841	tmp = NULL;
842
843	/* Disallow cross-device renames. */
844	if (fvp->v_mount != tdvp->v_mount ||
845	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
846		error = EXDEV;
847		goto out_unlocked;
848	}
849
850	fnode = VP_TO_TMPFS_NODE(fvp);
851	fdnode = VP_TO_TMPFS_DIR(fdvp);
852	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
853	tdnode = VP_TO_TMPFS_DIR(tdvp);
854	tmp = VFS_TO_TMPFS(tdvp->v_mount);
855
856	if (fdvp == tvp) {
857		error = 0;
858		goto out_unlocked;
859	}
860
861	/* If we need to move the directory between entries, lock the
862	 * source so that we can safely operate on it. */
863
864	/* XXX: this is a potential locking order violation! */
865	if (fdnode != tdnode) {
866		vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
867	}
868
869	/*
870	 * If the node we were renaming has scarpered, just give up.
871	 */
872	de = tmpfs_dir_lookup(fdnode, fcnp);
873	if (de == NULL || de->td_node != fnode) {
874		error = ENOENT;
875		goto out;
876	}
877
878	/* If source and target is the same vnode, remove the source link. */
879	if (fvp == tvp) {
880		/*
881		 * Detach and free the directory entry.  Drops the link
882		 * count on the node.
883		 */
884		tmpfs_dir_detach(fdvp, de);
885		tmpfs_free_dirent(VFS_TO_TMPFS(fvp->v_mount), de, true);
886		VN_KNOTE(fdvp, NOTE_WRITE);
887		goto out_ok;
888	}
889
890	/* If replacing an existing entry, ensure we can do the operation. */
891	if (tvp != NULL) {
892		KASSERT(tnode != NULL);
893		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
894			if (tnode->tn_size > 0) {
895				error = ENOTEMPTY;
896				goto out;
897			}
898		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
899			error = ENOTDIR;
900			goto out;
901		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
902			error = EISDIR;
903			goto out;
904		} else {
905			KASSERT(fnode->tn_type != VDIR &&
906			        tnode->tn_type != VDIR);
907		}
908	}
909
910	/* Ensure that we have enough memory to hold the new name, if it
911	 * has to be changed. */
912	namelen = tcnp->cn_namelen;
913	if (fcnp->cn_namelen != tcnp->cn_namelen ||
914	    memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
915		newname = tmpfs_str_pool_get(&tmp->tm_str_pool, namelen, 0);
916		if (newname == NULL) {
917			error = ENOSPC;
918			goto out;
919		}
920	}
921
922	/* If the node is being moved to another directory, we have to do
923	 * the move. */
924	if (fdnode != tdnode) {
925		/* In case we are moving a directory, we have to adjust its
926		 * parent to point to the new parent. */
927		if (de->td_node->tn_type == VDIR) {
928			struct tmpfs_node *n;
929
930			/* Ensure the target directory is not a child of the
931			 * directory being moved.  Otherwise, we'd end up
932			 * with stale nodes. */
933			n = tdnode;
934			while (n != n->tn_spec.tn_dir.tn_parent) {
935				if (n == fnode) {
936					error = EINVAL;
937					goto out;
938				}
939				n = n->tn_spec.tn_dir.tn_parent;
940			}
941
942			/* Adjust the parent pointer. */
943			TMPFS_VALIDATE_DIR(fnode);
944			de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
945
946			/* As a result of changing the target of the '..'
947			 * entry, the link count of the source and target
948			 * directories has to be adjusted. */
949			fdnode->tn_links--;
950			tdnode->tn_links++;
951		}
952
953		/* Do the move: just remove the entry from the source directory
954		 * and insert it into the target one. */
955		tmpfs_dir_detach(fdvp, de);
956		tmpfs_dir_attach(tdvp, de);
957
958		/* Notify listeners of fdvp about the change in the directory.
959		 * We can do it at this point because we aren't touching fdvp
960		 * any more below. */
961		VN_KNOTE(fdvp, NOTE_WRITE);
962	}
963
964	/* If we are overwriting an entry, we have to remove the old one
965	 * from the target directory. */
966	if (tvp != NULL) {
967		KASSERT(tnode != NULL);
968
969		/* Remove the old entry from the target directory.
970		 * Note! This relies on tmpfs_dir_attach() putting the new
971		 * node on the end of the target's node list. */
972		de2 = tmpfs_dir_lookup(tdnode, tcnp);
973		KASSERT(de2 != NULL);
974		KASSERT(de2->td_node == tnode);
975		tmpfs_dir_detach(tdvp, de2);
976
977		/* Free the directory entry we just deleted.  Note that the
978		 * node referred by it will not be removed until the vnode is
979		 * really reclaimed. */
980		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true);
981	}
982
983	/* If the name has changed, we need to make it effective by changing
984	 * it in the directory entry. */
985	if (newname != NULL) {
986		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
987		KASSERT(tcnp->cn_namelen < 0xffff);
988
989		tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
990		    de->td_namelen);
991		de->td_namelen = (uint16_t)namelen;
992		memcpy(newname, tcnp->cn_nameptr, namelen);
993		de->td_name = newname;
994		newname = NULL;
995
996		fnode->tn_status |= TMPFS_NODE_CHANGED;
997		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
998	}
999 out_ok:
1000	/* Notify listeners of tdvp about the change in the directory (either
1001	 * because a new entry was added or because one was removed) and
1002	 * listeners of fvp about the rename. */
1003	VN_KNOTE(tdvp, NOTE_WRITE);
1004	VN_KNOTE(fvp, NOTE_RENAME);
1005
1006	error = 0;
1007
1008 out:
1009	if (fdnode != tdnode)
1010		VOP_UNLOCK(fdvp, 0);
1011
1012 out_unlocked:
1013	/* Release target nodes. */
1014	if (tdvp == tvp)
1015		vrele(tdvp);
1016	else
1017		vput(tdvp);
1018	if (tvp != NULL)
1019		vput(tvp);
1020
1021	/* Release source nodes. */
1022	vrele(fdvp);
1023	vrele(fvp);
1024
1025	if (newname != NULL)
1026		tmpfs_str_pool_put(&tmp->tm_str_pool, newname, namelen);
1027
1028	return error;
1029}
1030
1031/* --------------------------------------------------------------------- */
1032
1033int
1034tmpfs_mkdir(void *v)
1035{
1036	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
1037	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1038	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
1039	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
1040
1041	KASSERT(vap->va_type == VDIR);
1042
1043	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1044}
1045
1046/* --------------------------------------------------------------------- */
1047
1048int
1049tmpfs_rmdir(void *v)
1050{
1051	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
1052	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
1053	struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp;
1054
1055	int error;
1056	struct tmpfs_dirent *de;
1057	struct tmpfs_mount *tmp;
1058	struct tmpfs_node *dnode;
1059	struct tmpfs_node *node;
1060
1061	KASSERT(VOP_ISLOCKED(dvp));
1062	KASSERT(VOP_ISLOCKED(vp));
1063
1064	tmp = VFS_TO_TMPFS(dvp->v_mount);
1065	dnode = VP_TO_TMPFS_DIR(dvp);
1066	node = VP_TO_TMPFS_DIR(vp);
1067	error = 0;
1068
1069	/* Directories with more than two entries ('.' and '..') cannot be
1070	 * removed. */
1071	if (node->tn_size > 0) {
1072		error = ENOTEMPTY;
1073		goto out;
1074	}
1075
1076	/* This invariant holds only if we are not trying to remove "..".
1077	 * We checked for that above so this is safe now. */
1078	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
1079
1080	/* Get the directory entry associated with node (vp). */
1081	de = tmpfs_dir_lookup(dnode, cnp);
1082	KASSERT(de);
1083	KASSERT(de->td_node == node);
1084
1085	/* Check flags to see if we are allowed to remove the directory. */
1086	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
1087		error = EPERM;
1088		goto out;
1089	}
1090
1091	/* Detach the directory entry from the directory (dnode). */
1092	tmpfs_dir_detach(dvp, de);
1093
1094	node->tn_links--;
1095	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1096	    TMPFS_NODE_MODIFIED;
1097	node->tn_spec.tn_dir.tn_parent->tn_links--;
1098	node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1099	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1100
1101	/* Release the parent. */
1102	cache_purge(dvp); /* XXX Is this needed? */
1103
1104	/* Free the directory entry we just deleted.  Note that the node
1105	 * referred by it will not be removed until the vnode is really
1106	 * reclaimed. */
1107	tmpfs_free_dirent(tmp, de, true);
1108
1109	KASSERT(node->tn_links == 0);
1110 out:
1111	/* Release the nodes. */
1112	vput(dvp);
1113	vput(vp);
1114	PNBUF_PUT(cnp->cn_pnbuf);
1115
1116	return error;
1117}
1118
1119/* --------------------------------------------------------------------- */
1120
1121int
1122tmpfs_symlink(void *v)
1123{
1124	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
1125	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
1126	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
1127	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
1128	char *target = ((struct vop_symlink_args *)v)->a_target;
1129
1130	KASSERT(vap->va_type == VLNK);
1131
1132	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1133}
1134
1135/* --------------------------------------------------------------------- */
1136
1137int
1138tmpfs_readdir(void *v)
1139{
1140	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
1141	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
1142	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
1143	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
1144	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
1145
1146	int error;
1147	off_t startoff;
1148	off_t cnt;
1149	struct tmpfs_node *node;
1150
1151	KASSERT(VOP_ISLOCKED(vp));
1152
1153	/* This operation only makes sense on directory nodes. */
1154	if (vp->v_type != VDIR) {
1155		error = ENOTDIR;
1156		goto out;
1157	}
1158
1159	node = VP_TO_TMPFS_DIR(vp);
1160
1161	startoff = uio->uio_offset;
1162
1163	cnt = 0;
1164	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1165		error = tmpfs_dir_getdotdent(node, uio);
1166		if (error == -1) {
1167			error = 0;
1168			goto outok;
1169		} else if (error != 0)
1170			goto outok;
1171		cnt++;
1172	}
1173
1174	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1175		error = tmpfs_dir_getdotdotdent(node, uio);
1176		if (error == -1) {
1177			error = 0;
1178			goto outok;
1179		} else if (error != 0)
1180			goto outok;
1181		cnt++;
1182	}
1183
1184	error = tmpfs_dir_getdents(node, uio, &cnt);
1185	if (error == -1)
1186		error = 0;
1187	KASSERT(error >= 0);
1188
1189outok:
1190	/* This label assumes that startoff has been
1191	 * initialized.  If the compiler didn't spit out warnings, we'd
1192	 * simply make this one be 'out' and drop 'outok'. */
1193
1194	if (eofflag != NULL)
1195		*eofflag =
1196		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1197
1198	/* Update NFS-related variables. */
1199	if (error == 0 && cookies != NULL && ncookies != NULL) {
1200		off_t i;
1201		off_t off = startoff;
1202		struct tmpfs_dirent *de = NULL;
1203
1204		*ncookies = cnt;
1205		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1206
1207		for (i = 0; i < cnt; i++) {
1208			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
1209			if (off == TMPFS_DIRCOOKIE_DOT) {
1210				off = TMPFS_DIRCOOKIE_DOTDOT;
1211			} else {
1212				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1213					de = TAILQ_FIRST(&node->tn_spec.
1214					    tn_dir.tn_dir);
1215				} else if (de != NULL) {
1216					de = TAILQ_NEXT(de, td_entries);
1217				} else {
1218					de = tmpfs_dir_lookupbycookie(node,
1219					    off);
1220					KASSERT(de != NULL);
1221					de = TAILQ_NEXT(de, td_entries);
1222				}
1223				if (de == NULL) {
1224					off = TMPFS_DIRCOOKIE_EOF;
1225				} else {
1226					off = tmpfs_dircookie(de);
1227				}
1228			}
1229
1230			(*cookies)[i] = off;
1231		}
1232		KASSERT(uio->uio_offset == off);
1233	}
1234
1235out:
1236	KASSERT(VOP_ISLOCKED(vp));
1237
1238	return error;
1239}
1240
1241/* --------------------------------------------------------------------- */
1242
1243int
1244tmpfs_readlink(void *v)
1245{
1246	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
1247	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
1248
1249	int error;
1250	struct tmpfs_node *node;
1251
1252	KASSERT(VOP_ISLOCKED(vp));
1253	KASSERT(uio->uio_offset == 0);
1254	KASSERT(vp->v_type == VLNK);
1255
1256	node = VP_TO_TMPFS_NODE(vp);
1257
1258	error = uiomove(node->tn_spec.tn_lnk.tn_link,
1259	    MIN(node->tn_size, uio->uio_resid), uio);
1260	node->tn_status |= TMPFS_NODE_ACCESSED;
1261
1262	KASSERT(VOP_ISLOCKED(vp));
1263
1264	return error;
1265}
1266
1267/* --------------------------------------------------------------------- */
1268
1269int
1270tmpfs_inactive(void *v)
1271{
1272	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
1273
1274	struct tmpfs_node *node;
1275
1276	KASSERT(VOP_ISLOCKED(vp));
1277
1278	node = VP_TO_TMPFS_NODE(vp);
1279	*((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
1280	VOP_UNLOCK(vp, 0);
1281
1282	return 0;
1283}
1284
1285/* --------------------------------------------------------------------- */
1286
1287int
1288tmpfs_reclaim(void *v)
1289{
1290	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
1291
1292	struct tmpfs_mount *tmp;
1293	struct tmpfs_node *node;
1294
1295	node = VP_TO_TMPFS_NODE(vp);
1296	tmp = VFS_TO_TMPFS(vp->v_mount);
1297
1298	cache_purge(vp);
1299	tmpfs_free_vp(vp);
1300
1301	/* If the node referenced by this vnode was deleted by the user,
1302	 * we must free its associated data structures (now that the vnode
1303	 * is being reclaimed). */
1304	if (node->tn_links == 0)
1305		tmpfs_free_node(tmp, node);
1306
1307	KASSERT(vp->v_data == NULL);
1308
1309	return 0;
1310}
1311
1312/* --------------------------------------------------------------------- */
1313
1314int
1315tmpfs_print(void *v)
1316{
1317	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
1318
1319	struct tmpfs_node *node;
1320
1321	node = VP_TO_TMPFS_NODE(vp);
1322
1323	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1324	    node, node->tn_flags, node->tn_links);
1325	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1326	    ", status 0x%x\n",
1327	    node->tn_mode, node->tn_uid, node->tn_gid,
1328	    (uintmax_t)node->tn_size, node->tn_status);
1329	if (vp->v_type == VFIFO)
1330		fifo_printinfo(vp);
1331	printf("\n");
1332
1333	return 0;
1334}
1335
1336/* --------------------------------------------------------------------- */
1337
1338int
1339tmpfs_pathconf(void *v)
1340{
1341	int name = ((struct vop_pathconf_args *)v)->a_name;
1342	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
1343
1344	int error;
1345
1346	error = 0;
1347
1348	switch (name) {
1349	case _PC_LINK_MAX:
1350		*retval = LINK_MAX;
1351		break;
1352
1353	case _PC_NAME_MAX:
1354		*retval = NAME_MAX;
1355		break;
1356
1357	case _PC_PATH_MAX:
1358		*retval = PATH_MAX;
1359		break;
1360
1361	case _PC_PIPE_BUF:
1362		*retval = PIPE_BUF;
1363		break;
1364
1365	case _PC_CHOWN_RESTRICTED:
1366		*retval = 1;
1367		break;
1368
1369	case _PC_NO_TRUNC:
1370		*retval = 1;
1371		break;
1372
1373	case _PC_SYNC_IO:
1374		*retval = 1;
1375		break;
1376
1377	case _PC_FILESIZEBITS:
1378		*retval = 0; /* XXX Don't know which value should I return. */
1379		break;
1380
1381	default:
1382		error = EINVAL;
1383	}
1384
1385	return error;
1386}
1387
1388/* --------------------------------------------------------------------- */
1389
1390int
1391tmpfs_advlock(void *v)
1392{
1393	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
1394
1395	struct tmpfs_node *node;
1396
1397	node = VP_TO_TMPFS_NODE(vp);
1398
1399	return lf_advlock(v, &node->tn_lockf, node->tn_size);
1400}
1401
1402/* --------------------------------------------------------------------- */
1403
1404int
1405tmpfs_getpages(void *v)
1406{
1407	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
1408	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
1409	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
1410	int *count = ((struct vop_getpages_args *)v)->a_count;
1411	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
1412	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
1413	int advice = ((struct vop_getpages_args *)v)->a_advice;
1414	int flags = ((struct vop_getpages_args *)v)->a_flags;
1415
1416	int error;
1417	int i;
1418	struct tmpfs_node *node;
1419	struct uvm_object *uobj;
1420	int npages = *count;
1421
1422	KASSERT(vp->v_type == VREG);
1423	KASSERT(mutex_owned(&vp->v_interlock));
1424
1425	node = VP_TO_TMPFS_NODE(vp);
1426	uobj = node->tn_spec.tn_reg.tn_aobj;
1427
1428	/* We currently don't rely on PGO_PASTEOF. */
1429
1430	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1431		if ((flags & PGO_LOCKED) == 0)
1432			mutex_exit(&vp->v_interlock);
1433		return EINVAL;
1434	}
1435
1436	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1437		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1438	}
1439
1440	if ((flags & PGO_LOCKED) != 0)
1441		return EBUSY;
1442
1443	if ((flags & PGO_NOTIMESTAMP) == 0) {
1444		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1445			node->tn_status |= TMPFS_NODE_ACCESSED;
1446
1447		if ((access_type & VM_PROT_WRITE) != 0)
1448			node->tn_status |= TMPFS_NODE_MODIFIED;
1449	}
1450
1451	mutex_exit(&vp->v_interlock);
1452
1453	/*
1454	 * Make sure that the array on which we will store the
1455	 * gotten pages is clean.  Otherwise uao_get (pointed to by
1456	 * the pgo_get below) gets confused and does not return the
1457	 * appropriate pages.
1458	 *
1459	 * XXX This shall be revisited when kern/32166 is addressed
1460	 * because the loop to clean m[i] will most likely be redundant
1461	 * as well as the PGO_ALLPAGES flag.
1462	 */
1463	if (m != NULL)
1464		for (i = 0; i < npages; i++)
1465			m[i] = NULL;
1466	mutex_enter(&uobj->vmobjlock);
1467	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
1468	    access_type, advice, flags | PGO_ALLPAGES);
1469#if defined(DEBUG)
1470	{
1471		/* Make sure that all the pages we return are valid. */
1472		int dbgi;
1473		if (error == 0 && m != NULL)
1474			for (dbgi = 0; dbgi < npages; dbgi++)
1475				KASSERT(m[dbgi] != NULL);
1476	}
1477#endif
1478
1479	return error;
1480}
1481
1482/* --------------------------------------------------------------------- */
1483
1484int
1485tmpfs_putpages(void *v)
1486{
1487	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
1488	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
1489	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
1490	int flags = ((struct vop_putpages_args *)v)->a_flags;
1491
1492	int error;
1493	struct tmpfs_node *node;
1494	struct uvm_object *uobj;
1495
1496	KASSERT(mutex_owned(&vp->v_interlock));
1497
1498	node = VP_TO_TMPFS_NODE(vp);
1499
1500	if (vp->v_type != VREG) {
1501		mutex_exit(&vp->v_interlock);
1502		return 0;
1503	}
1504
1505	uobj = node->tn_spec.tn_reg.tn_aobj;
1506	mutex_exit(&vp->v_interlock);
1507
1508	mutex_enter(&uobj->vmobjlock);
1509	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1510
1511	/* XXX mtime */
1512
1513	return error;
1514}
1515