No empty .Rs/.Re
[netbsd-mini2440.git] / sys / fs / tmpfs / tmpfs_vnops.c
blobf54316a084c0e68904ad13822563ebed9aa4eed1
1 /* $NetBSD: tmpfs_vnops.c,v 1.65 2009/11/22 17:09:58 jmmv Exp $ */
3 /*
4 * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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.
34 * tmpfs vnode interface.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.65 2009/11/22 17:09:58 jmmv Exp $");
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>
54 #include <uvm/uvm.h>
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>
61 /* --------------------------------------------------------------------- */
64 * vnode operations vector used for files stored in a tmpfs file system.
66 int (**tmpfs_vnodeop_p)(void *);
67 const 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 }
111 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
112 { &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
114 /* --------------------------------------------------------------------- */
117 tmpfs_lookup(void *v)
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;
123 int error;
124 struct tmpfs_dirent *de;
125 struct tmpfs_node *dnode;
127 KASSERT(VOP_ISLOCKED(dvp));
129 dnode = VP_TO_TMPFS_DIR(dvp);
130 *vpp = NULL;
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;
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;
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;
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)));
157 if (cnp->cn_flags & ISDOTDOT) {
158 VOP_UNLOCK(dvp, 0);
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);
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;
183 /* Keep the component name in the buffer for
184 * future uses. */
185 cnp->cn_flags |= SAVENAME;
187 error = EJUSTRETURN;
188 } else
189 error = ENOENT;
190 } else {
191 struct tmpfs_node *tnode;
193 /* The entry was found, so get its associated
194 * tmpfs_node. */
195 tnode = de->td_node;
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;
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;
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;
222 /* Only bother if we're not already failing it. */
223 if (!error) {
224 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
227 if (cnp->cn_nameiop == DELETE)
228 action |= KAUTH_VNODE_DELETE;
229 else /* if (cnp->cn_nameiop == RENAME) */
230 action |= KAUTH_VNODE_RENAME;
232 error = kauth_authorize_vnode(cnp->cn_cred,
233 action, *vpp, dvp, error);
234 if (error != 0)
235 goto out;
237 cnp->cn_flags |= SAVENAME;
238 } else
239 de = NULL;
241 /* Allocate a new vnode on the matching entry. */
242 error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
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);
253 out:
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)));
258 /* dvp must always be locked. */
259 KASSERT(VOP_ISLOCKED(dvp));
261 return error;
264 /* --------------------------------------------------------------------- */
267 tmpfs_create(void *v)
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;
274 KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
276 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
278 /* --------------------------------------------------------------------- */
281 tmpfs_mknod(void *v)
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;
288 if (vap->va_type != VBLK && vap->va_type != VCHR &&
289 vap->va_type != VFIFO) {
290 vput(dvp);
291 return EINVAL;
294 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
297 /* --------------------------------------------------------------------- */
300 tmpfs_open(void *v)
302 struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
303 int mode = ((struct vop_open_args *)v)->a_mode;
305 int error;
306 struct tmpfs_node *node;
308 KASSERT(VOP_ISLOCKED(vp));
310 node = VP_TO_TMPFS_NODE(vp);
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;
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;
326 out:
327 KASSERT(VOP_ISLOCKED(vp));
329 return error;
332 /* --------------------------------------------------------------------- */
335 tmpfs_close(void *v)
337 struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
339 struct tmpfs_node *node;
341 KASSERT(VOP_ISLOCKED(vp));
343 node = VP_TO_TMPFS_NODE(vp);
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);
351 return 0;
354 /* --------------------------------------------------------------------- */
356 static int
357 tmpfs_check_possible(struct vnode *vp, struct tmpfs_node *node, mode_t mode)
359 int error = 0;
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;
371 break;
373 case VBLK:
374 /* FALLTHROUGH */
375 case VCHR:
376 /* FALLTHROUGH */
377 case VSOCK:
378 /* FALLTHROUGH */
379 case VFIFO:
380 break;
382 default:
383 error = EINVAL;
384 goto out;
387 if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
388 error = EPERM;
389 goto out;
392 out:
393 return error;
396 static int
397 tmpfs_check_permitted(struct vnode *vp, struct tmpfs_node *node, mode_t mode,
398 kauth_cred_t cred)
401 return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
402 node->tn_gid, mode, cred);
406 tmpfs_access(void *v)
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;
412 int error;
413 struct tmpfs_node *node;
415 KASSERT(VOP_ISLOCKED(vp));
417 node = VP_TO_TMPFS_NODE(vp);
419 error = tmpfs_check_possible(vp, node, mode);
420 if (error)
421 goto out;
423 error = tmpfs_check_permitted(vp, node, mode, cred);
425 error = kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp,
426 NULL, error);
428 out:
429 KASSERT(VOP_ISLOCKED(vp));
431 return error;
434 /* --------------------------------------------------------------------- */
437 tmpfs_getattr(void *v)
439 struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
440 struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
442 struct tmpfs_node *node;
444 node = VP_TO_TMPFS_NODE(vp);
446 vattr_null(vap);
448 tmpfs_itimes(vp, NULL, NULL, NULL);
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 */
472 return 0;
475 /* --------------------------------------------------------------------- */
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... */
481 tmpfs_setattr(void *v)
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;
488 int error;
490 KASSERT(VOP_ISLOCKED(vp));
492 error = 0;
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;
506 if (error == 0 && (vap->va_flags != VNOVAL))
507 error = tmpfs_chflags(vp, vap->va_flags, cred, l);
509 if (error == 0 && (vap->va_size != VNOVAL))
510 error = tmpfs_chsize(vp, vap->va_size, cred, l);
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);
515 if (error == 0 && (vap->va_mode != VNOVAL))
516 error = tmpfs_chmod(vp, vap->va_mode, cred, l);
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;
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);
529 KASSERT(VOP_ISLOCKED(vp));
531 return error;
534 /* --------------------------------------------------------------------- */
537 tmpfs_read(void *v)
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;
543 int error;
544 struct tmpfs_node *node;
545 struct uvm_object *uobj;
547 KASSERT(VOP_ISLOCKED(vp));
549 node = VP_TO_TMPFS_NODE(vp);
551 if (vp->v_type != VREG) {
552 error = EISDIR;
553 goto out;
556 if (uio->uio_offset < 0) {
557 error = EINVAL;
558 goto out;
561 node->tn_status |= TMPFS_NODE_ACCESSED;
563 uobj = node->tn_spec.tn_reg.tn_aobj;
564 error = 0;
565 while (error == 0 && uio->uio_resid > 0) {
566 vsize_t len;
568 if (node->tn_size <= uio->uio_offset)
569 break;
571 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
572 if (len == 0)
573 break;
575 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
576 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
579 out:
580 KASSERT(VOP_ISLOCKED(vp));
582 return error;
585 /* --------------------------------------------------------------------- */
588 tmpfs_write(void *v)
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;
594 bool extended;
595 int error;
596 off_t oldsize;
597 struct proc *p = curproc;
598 struct tmpfs_node *node;
599 struct uvm_object *uobj;
601 KASSERT(VOP_ISLOCKED(vp));
603 node = VP_TO_TMPFS_NODE(vp);
604 oldsize = node->tn_size;
606 if (uio->uio_offset < 0 || vp->v_type != VREG) {
607 error = EINVAL;
608 goto out;
611 if (uio->uio_resid == 0) {
612 error = 0;
613 goto out;
616 if (((uio->uio_offset + uio->uio_resid) >
617 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
618 mutex_enter(proc_lock);
619 psignal(p, SIGXFSZ);
620 mutex_exit(proc_lock);
621 error = EFBIG;
622 goto out;
625 if (ioflag & IO_APPEND)
626 uio->uio_offset = node->tn_size;
628 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
629 if (extended) {
630 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
631 if (error != 0)
632 goto out;
635 uobj = node->tn_spec.tn_reg.tn_aobj;
636 error = 0;
637 while (error == 0 && uio->uio_resid > 0) {
638 vsize_t len;
640 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
641 if (len == 0)
642 break;
644 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
645 UBC_WRITE | UBC_UNMAP_FLAG(vp));
648 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
649 (extended ? TMPFS_NODE_CHANGED : 0);
651 if (error != 0)
652 (void)tmpfs_reg_resize(vp, oldsize);
654 VN_KNOTE(vp, NOTE_WRITE);
656 out:
657 KASSERT(VOP_ISLOCKED(vp));
658 KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
659 KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
661 return error;
664 /* --------------------------------------------------------------------- */
667 tmpfs_fsync(void *v)
669 struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
671 KASSERT(VOP_ISLOCKED(vp));
673 tmpfs_update(vp, NULL, NULL, NULL, 0);
675 return 0;
678 /* --------------------------------------------------------------------- */
681 tmpfs_remove(void *v)
683 struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
684 struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
685 struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp);
687 int error;
688 struct tmpfs_dirent *de;
689 struct tmpfs_mount *tmp;
690 struct tmpfs_node *dnode;
691 struct tmpfs_node *node;
693 KASSERT(VOP_ISLOCKED(dvp));
694 KASSERT(VOP_ISLOCKED(vp));
696 if (vp->v_type == VDIR) {
697 error = EPERM;
698 goto out;
701 dnode = VP_TO_TMPFS_DIR(dvp);
702 node = VP_TO_TMPFS_NODE(vp);
703 tmp = VFS_TO_TMPFS(vp->v_mount);
704 de = tmpfs_dir_lookup(dnode, cnp);
705 KASSERT(de);
706 KASSERT(de->td_node == node);
708 /* Files marked as immutable or append-only cannot be deleted. */
709 if (node->tn_flags & (IMMUTABLE | APPEND)) {
710 error = EPERM;
711 goto out;
714 /* Remove the entry from the directory; as it is a file, we do not
715 * have to change the number of hard links of the directory. */
716 tmpfs_dir_detach(dvp, de);
718 /* Free the directory entry we just deleted. Note that the node
719 * referred by it will not be removed until the vnode is really
720 * reclaimed. */
721 tmpfs_free_dirent(tmp, de, true);
723 error = 0;
725 out:
726 vput(vp);
727 if (dvp == vp)
728 vrele(dvp);
729 else
730 vput(dvp);
731 if (cnp->cn_flags & HASBUF) {
732 PNBUF_PUT(cnp->cn_pnbuf);
733 cnp->cn_flags &= ~HASBUF;
736 return error;
739 /* --------------------------------------------------------------------- */
742 tmpfs_link(void *v)
744 struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
745 struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
746 struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
748 int error;
749 struct tmpfs_dirent *de;
750 struct tmpfs_node *dnode;
751 struct tmpfs_node *node;
753 KASSERT(VOP_ISLOCKED(dvp));
754 KASSERT(cnp->cn_flags & HASBUF);
755 KASSERT(dvp != vp); /* XXX When can this be false? */
757 dnode = VP_TO_TMPFS_DIR(dvp);
758 node = VP_TO_TMPFS_NODE(vp);
760 /* Lock vp because we will need to run tmpfs_update over it, which
761 * needs the vnode to be locked. */
762 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
764 /* XXX: Why aren't the following two tests done by the caller? */
766 /* Hard links of directories are forbidden. */
767 if (vp->v_type == VDIR) {
768 error = EPERM;
769 goto out;
772 /* Cannot create cross-device links. */
773 if (dvp->v_mount != vp->v_mount) {
774 error = EXDEV;
775 goto out;
778 /* Ensure that we do not overflow the maximum number of links imposed
779 * by the system. */
780 KASSERT(node->tn_links <= LINK_MAX);
781 if (node->tn_links == LINK_MAX) {
782 error = EMLINK;
783 goto out;
786 /* We cannot create links of files marked immutable or append-only. */
787 if (node->tn_flags & (IMMUTABLE | APPEND)) {
788 error = EPERM;
789 goto out;
792 /* Allocate a new directory entry to represent the node. */
793 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
794 cnp->cn_nameptr, cnp->cn_namelen, &de);
795 if (error != 0)
796 goto out;
798 /* Insert the new directory entry into the appropriate directory. */
799 tmpfs_dir_attach(dvp, de);
801 /* vp link count has changed, so update node times. */
802 node->tn_status |= TMPFS_NODE_CHANGED;
803 tmpfs_update(vp, NULL, NULL, NULL, 0);
805 error = 0;
807 out:
808 VOP_UNLOCK(vp, 0);
809 PNBUF_PUT(cnp->cn_pnbuf);
810 vput(dvp);
812 return error;
816 * tmpfs_rename: rename routine.
818 * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
819 * and tvp (to-leaf), if exists (NULL if not).
821 * => Caller holds a reference on fdvp and fvp, they are unlocked.
822 * Note: fdvp and fvp can refer to the same object (i.e. when it is root).
824 * => Both tdvp and tvp are referenced and locked. It is our responsibility
825 * to release the references and unlock them (or destroy).
828 tmpfs_rename(void *v)
830 struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
831 struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
832 struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
833 struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
834 struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
835 struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
837 char *newname;
838 int error;
839 struct tmpfs_dirent *de, *de2;
840 struct tmpfs_mount *tmp;
841 struct tmpfs_node *fdnode;
842 struct tmpfs_node *fnode;
843 struct tmpfs_node *tnode;
844 struct tmpfs_node *tdnode;
845 size_t namelen;
847 KASSERT(VOP_ISLOCKED(tdvp));
848 KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
849 KASSERT(fcnp->cn_flags & HASBUF);
850 KASSERT(tcnp->cn_flags & HASBUF);
852 newname = NULL;
853 namelen = 0;
854 tmp = NULL;
856 /* Disallow cross-device renames. */
857 if (fvp->v_mount != tdvp->v_mount ||
858 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
859 error = EXDEV;
860 goto out_unlocked;
863 fnode = VP_TO_TMPFS_NODE(fvp);
864 fdnode = VP_TO_TMPFS_DIR(fdvp);
865 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
866 tdnode = VP_TO_TMPFS_DIR(tdvp);
867 tmp = VFS_TO_TMPFS(tdvp->v_mount);
869 if (fdvp == tvp) {
870 error = 0;
871 goto out_unlocked;
874 /* If we need to move the directory between entries, lock the
875 * source so that we can safely operate on it. */
877 /* XXX: this is a potential locking order violation! */
878 if (fdnode != tdnode) {
879 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
883 * If the node we were renaming has scarpered, just give up.
885 de = tmpfs_dir_lookup(fdnode, fcnp);
886 if (de == NULL || de->td_node != fnode) {
887 error = ENOENT;
888 goto out;
891 /* If source and target is the same vnode, remove the source link. */
892 if (fvp == tvp) {
894 * Detach and free the directory entry. Drops the link
895 * count on the node.
897 tmpfs_dir_detach(fdvp, de);
898 tmpfs_free_dirent(VFS_TO_TMPFS(fvp->v_mount), de, true);
899 VN_KNOTE(fdvp, NOTE_WRITE);
900 goto out_ok;
903 /* If replacing an existing entry, ensure we can do the operation. */
904 if (tvp != NULL) {
905 KASSERT(tnode != NULL);
906 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
907 if (tnode->tn_size > 0) {
908 error = ENOTEMPTY;
909 goto out;
911 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
912 error = ENOTDIR;
913 goto out;
914 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
915 error = EISDIR;
916 goto out;
917 } else {
918 KASSERT(fnode->tn_type != VDIR &&
919 tnode->tn_type != VDIR);
923 /* Ensure that we have enough memory to hold the new name, if it
924 * has to be changed. */
925 namelen = tcnp->cn_namelen;
926 if (fcnp->cn_namelen != tcnp->cn_namelen ||
927 memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
928 newname = tmpfs_str_pool_get(&tmp->tm_str_pool, namelen, 0);
929 if (newname == NULL) {
930 error = ENOSPC;
931 goto out;
935 /* If the node is being moved to another directory, we have to do
936 * the move. */
937 if (fdnode != tdnode) {
938 /* In case we are moving a directory, we have to adjust its
939 * parent to point to the new parent. */
940 if (de->td_node->tn_type == VDIR) {
941 struct tmpfs_node *n;
943 /* Ensure the target directory is not a child of the
944 * directory being moved. Otherwise, we'd end up
945 * with stale nodes. */
946 n = tdnode;
947 while (n != n->tn_spec.tn_dir.tn_parent) {
948 if (n == fnode) {
949 error = EINVAL;
950 goto out;
952 n = n->tn_spec.tn_dir.tn_parent;
955 /* Adjust the parent pointer. */
956 TMPFS_VALIDATE_DIR(fnode);
957 de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
959 /* As a result of changing the target of the '..'
960 * entry, the link count of the source and target
961 * directories has to be adjusted. */
962 fdnode->tn_links--;
963 tdnode->tn_links++;
966 /* Do the move: just remove the entry from the source directory
967 * and insert it into the target one. */
968 tmpfs_dir_detach(fdvp, de);
969 tmpfs_dir_attach(tdvp, de);
971 /* Notify listeners of fdvp about the change in the directory.
972 * We can do it at this point because we aren't touching fdvp
973 * any more below. */
974 VN_KNOTE(fdvp, NOTE_WRITE);
977 /* If we are overwriting an entry, we have to remove the old one
978 * from the target directory. */
979 if (tvp != NULL) {
980 KASSERT(tnode != NULL);
982 /* Remove the old entry from the target directory.
983 * Note! This relies on tmpfs_dir_attach() putting the new
984 * node on the end of the target's node list. */
985 de2 = tmpfs_dir_lookup(tdnode, tcnp);
986 KASSERT(de2 != NULL);
987 KASSERT(de2->td_node == tnode);
988 tmpfs_dir_detach(tdvp, de2);
990 /* Free the directory entry we just deleted. Note that the
991 * node referred by it will not be removed until the vnode is
992 * really reclaimed. */
993 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true);
996 /* If the name has changed, we need to make it effective by changing
997 * it in the directory entry. */
998 if (newname != NULL) {
999 KASSERT(tcnp->cn_namelen < MAXNAMLEN);
1000 KASSERT(tcnp->cn_namelen < 0xffff);
1002 tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
1003 de->td_namelen);
1004 de->td_namelen = (uint16_t)namelen;
1005 memcpy(newname, tcnp->cn_nameptr, namelen);
1006 de->td_name = newname;
1007 newname = NULL;
1009 fnode->tn_status |= TMPFS_NODE_CHANGED;
1010 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1012 out_ok:
1013 /* Notify listeners of tdvp about the change in the directory (either
1014 * because a new entry was added or because one was removed) and
1015 * listeners of fvp about the rename. */
1016 VN_KNOTE(tdvp, NOTE_WRITE);
1017 VN_KNOTE(fvp, NOTE_RENAME);
1019 error = 0;
1021 out:
1022 if (fdnode != tdnode)
1023 VOP_UNLOCK(fdvp, 0);
1025 out_unlocked:
1026 /* Release target nodes. */
1027 if (tdvp == tvp)
1028 vrele(tdvp);
1029 else
1030 vput(tdvp);
1031 if (tvp != NULL)
1032 vput(tvp);
1034 /* Release source nodes. */
1035 vrele(fdvp);
1036 vrele(fvp);
1038 if (newname != NULL)
1039 tmpfs_str_pool_put(&tmp->tm_str_pool, newname, namelen);
1041 return error;
1044 /* --------------------------------------------------------------------- */
1047 tmpfs_mkdir(void *v)
1049 struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
1050 struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1051 struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
1052 struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
1054 KASSERT(vap->va_type == VDIR);
1056 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1059 /* --------------------------------------------------------------------- */
1062 tmpfs_rmdir(void *v)
1064 struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
1065 struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
1066 struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp;
1068 int error;
1069 struct tmpfs_dirent *de;
1070 struct tmpfs_mount *tmp;
1071 struct tmpfs_node *dnode;
1072 struct tmpfs_node *node;
1074 KASSERT(VOP_ISLOCKED(dvp));
1075 KASSERT(VOP_ISLOCKED(vp));
1077 tmp = VFS_TO_TMPFS(dvp->v_mount);
1078 dnode = VP_TO_TMPFS_DIR(dvp);
1079 node = VP_TO_TMPFS_DIR(vp);
1080 error = 0;
1082 /* Directories with more than two entries ('.' and '..') cannot be
1083 * removed. */
1084 if (node->tn_size > 0) {
1085 error = ENOTEMPTY;
1086 goto out;
1089 /* This invariant holds only if we are not trying to remove "..".
1090 * We checked for that above so this is safe now. */
1091 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
1093 /* Get the directory entry associated with node (vp). */
1094 de = tmpfs_dir_lookup(dnode, cnp);
1095 KASSERT(de);
1096 KASSERT(de->td_node == node);
1098 /* Check flags to see if we are allowed to remove the directory. */
1099 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
1100 error = EPERM;
1101 goto out;
1104 /* Detach the directory entry from the directory (dnode). */
1105 tmpfs_dir_detach(dvp, de);
1107 node->tn_links--;
1108 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1109 TMPFS_NODE_MODIFIED;
1110 node->tn_spec.tn_dir.tn_parent->tn_links--;
1111 node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1112 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1114 /* Release the parent. */
1115 cache_purge(dvp); /* XXX Is this needed? */
1117 /* Free the directory entry we just deleted. Note that the node
1118 * referred by it will not be removed until the vnode is really
1119 * reclaimed. */
1120 tmpfs_free_dirent(tmp, de, true);
1122 KASSERT(node->tn_links == 0);
1123 out:
1124 /* Release the nodes. */
1125 vput(dvp);
1126 vput(vp);
1127 PNBUF_PUT(cnp->cn_pnbuf);
1129 return error;
1132 /* --------------------------------------------------------------------- */
1135 tmpfs_symlink(void *v)
1137 struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
1138 struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
1139 struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
1140 struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
1141 char *target = ((struct vop_symlink_args *)v)->a_target;
1143 KASSERT(vap->va_type == VLNK);
1145 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1148 /* --------------------------------------------------------------------- */
1151 tmpfs_readdir(void *v)
1153 struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
1154 struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
1155 int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
1156 off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
1157 int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
1159 int error;
1160 off_t startoff;
1161 off_t cnt;
1162 struct tmpfs_node *node;
1164 KASSERT(VOP_ISLOCKED(vp));
1166 /* This operation only makes sense on directory nodes. */
1167 if (vp->v_type != VDIR) {
1168 error = ENOTDIR;
1169 goto out;
1172 node = VP_TO_TMPFS_DIR(vp);
1174 startoff = uio->uio_offset;
1176 cnt = 0;
1177 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1178 error = tmpfs_dir_getdotdent(node, uio);
1179 if (error == -1) {
1180 error = 0;
1181 goto outok;
1182 } else if (error != 0)
1183 goto outok;
1184 cnt++;
1187 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1188 error = tmpfs_dir_getdotdotdent(node, uio);
1189 if (error == -1) {
1190 error = 0;
1191 goto outok;
1192 } else if (error != 0)
1193 goto outok;
1194 cnt++;
1197 error = tmpfs_dir_getdents(node, uio, &cnt);
1198 if (error == -1)
1199 error = 0;
1200 KASSERT(error >= 0);
1202 outok:
1203 /* This label assumes that startoff has been
1204 * initialized. If the compiler didn't spit out warnings, we'd
1205 * simply make this one be 'out' and drop 'outok'. */
1207 if (eofflag != NULL)
1208 *eofflag =
1209 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1211 /* Update NFS-related variables. */
1212 if (error == 0 && cookies != NULL && ncookies != NULL) {
1213 off_t i;
1214 off_t off = startoff;
1215 struct tmpfs_dirent *de = NULL;
1217 *ncookies = cnt;
1218 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1220 for (i = 0; i < cnt; i++) {
1221 KASSERT(off != TMPFS_DIRCOOKIE_EOF);
1222 if (off == TMPFS_DIRCOOKIE_DOT) {
1223 off = TMPFS_DIRCOOKIE_DOTDOT;
1224 } else {
1225 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1226 de = TAILQ_FIRST(&node->tn_spec.
1227 tn_dir.tn_dir);
1228 } else if (de != NULL) {
1229 de = TAILQ_NEXT(de, td_entries);
1230 } else {
1231 de = tmpfs_dir_lookupbycookie(node,
1232 off);
1233 KASSERT(de != NULL);
1234 de = TAILQ_NEXT(de, td_entries);
1236 if (de == NULL) {
1237 off = TMPFS_DIRCOOKIE_EOF;
1238 } else {
1239 off = tmpfs_dircookie(de);
1243 (*cookies)[i] = off;
1245 KASSERT(uio->uio_offset == off);
1248 out:
1249 KASSERT(VOP_ISLOCKED(vp));
1251 return error;
1254 /* --------------------------------------------------------------------- */
1257 tmpfs_readlink(void *v)
1259 struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
1260 struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
1262 int error;
1263 struct tmpfs_node *node;
1265 KASSERT(VOP_ISLOCKED(vp));
1266 KASSERT(uio->uio_offset == 0);
1267 KASSERT(vp->v_type == VLNK);
1269 node = VP_TO_TMPFS_NODE(vp);
1271 error = uiomove(node->tn_spec.tn_lnk.tn_link,
1272 MIN(node->tn_size, uio->uio_resid), uio);
1273 node->tn_status |= TMPFS_NODE_ACCESSED;
1275 KASSERT(VOP_ISLOCKED(vp));
1277 return error;
1280 /* --------------------------------------------------------------------- */
1283 tmpfs_inactive(void *v)
1285 struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
1287 struct tmpfs_node *node;
1289 KASSERT(VOP_ISLOCKED(vp));
1291 node = VP_TO_TMPFS_NODE(vp);
1292 *((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
1293 VOP_UNLOCK(vp, 0);
1295 return 0;
1298 /* --------------------------------------------------------------------- */
1301 tmpfs_reclaim(void *v)
1303 struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
1305 struct tmpfs_mount *tmp;
1306 struct tmpfs_node *node;
1308 node = VP_TO_TMPFS_NODE(vp);
1309 tmp = VFS_TO_TMPFS(vp->v_mount);
1311 cache_purge(vp);
1312 tmpfs_free_vp(vp);
1314 /* If the node referenced by this vnode was deleted by the user,
1315 * we must free its associated data structures (now that the vnode
1316 * is being reclaimed). */
1317 if (node->tn_links == 0)
1318 tmpfs_free_node(tmp, node);
1320 KASSERT(vp->v_data == NULL);
1322 return 0;
1325 /* --------------------------------------------------------------------- */
1328 tmpfs_print(void *v)
1330 struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
1332 struct tmpfs_node *node;
1334 node = VP_TO_TMPFS_NODE(vp);
1336 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1337 node, node->tn_flags, node->tn_links);
1338 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1339 ", status 0x%x\n",
1340 node->tn_mode, node->tn_uid, node->tn_gid,
1341 (uintmax_t)node->tn_size, node->tn_status);
1342 if (vp->v_type == VFIFO)
1343 fifo_printinfo(vp);
1344 printf("\n");
1346 return 0;
1349 /* --------------------------------------------------------------------- */
1352 tmpfs_pathconf(void *v)
1354 int name = ((struct vop_pathconf_args *)v)->a_name;
1355 register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
1357 int error;
1359 error = 0;
1361 switch (name) {
1362 case _PC_LINK_MAX:
1363 *retval = LINK_MAX;
1364 break;
1366 case _PC_NAME_MAX:
1367 *retval = NAME_MAX;
1368 break;
1370 case _PC_PATH_MAX:
1371 *retval = PATH_MAX;
1372 break;
1374 case _PC_PIPE_BUF:
1375 *retval = PIPE_BUF;
1376 break;
1378 case _PC_CHOWN_RESTRICTED:
1379 *retval = 1;
1380 break;
1382 case _PC_NO_TRUNC:
1383 *retval = 1;
1384 break;
1386 case _PC_SYNC_IO:
1387 *retval = 1;
1388 break;
1390 case _PC_FILESIZEBITS:
1391 *retval = 0; /* XXX Don't know which value should I return. */
1392 break;
1394 default:
1395 error = EINVAL;
1398 return error;
1401 /* --------------------------------------------------------------------- */
1404 tmpfs_advlock(void *v)
1406 struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
1408 struct tmpfs_node *node;
1410 node = VP_TO_TMPFS_NODE(vp);
1412 return lf_advlock(v, &node->tn_lockf, node->tn_size);
1415 /* --------------------------------------------------------------------- */
1418 tmpfs_getpages(void *v)
1420 struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
1421 voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
1422 struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
1423 int *count = ((struct vop_getpages_args *)v)->a_count;
1424 int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
1425 vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
1426 int advice = ((struct vop_getpages_args *)v)->a_advice;
1427 int flags = ((struct vop_getpages_args *)v)->a_flags;
1429 int error;
1430 int i;
1431 struct tmpfs_node *node;
1432 struct uvm_object *uobj;
1433 int npages = *count;
1435 KASSERT(vp->v_type == VREG);
1436 KASSERT(mutex_owned(&vp->v_interlock));
1438 node = VP_TO_TMPFS_NODE(vp);
1439 uobj = node->tn_spec.tn_reg.tn_aobj;
1441 /* We currently don't rely on PGO_PASTEOF. */
1443 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1444 if ((flags & PGO_LOCKED) == 0)
1445 mutex_exit(&vp->v_interlock);
1446 return EINVAL;
1449 if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1450 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1453 if ((flags & PGO_LOCKED) != 0)
1454 return EBUSY;
1456 if ((flags & PGO_NOTIMESTAMP) == 0) {
1457 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1458 node->tn_status |= TMPFS_NODE_ACCESSED;
1460 if ((access_type & VM_PROT_WRITE) != 0)
1461 node->tn_status |= TMPFS_NODE_MODIFIED;
1464 mutex_exit(&vp->v_interlock);
1467 * Make sure that the array on which we will store the
1468 * gotten pages is clean. Otherwise uao_get (pointed to by
1469 * the pgo_get below) gets confused and does not return the
1470 * appropriate pages.
1472 * XXX This shall be revisited when kern/32166 is addressed
1473 * because the loop to clean m[i] will most likely be redundant
1474 * as well as the PGO_ALLPAGES flag.
1476 if (m != NULL)
1477 for (i = 0; i < npages; i++)
1478 m[i] = NULL;
1479 mutex_enter(&uobj->vmobjlock);
1480 error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
1481 access_type, advice, flags | PGO_ALLPAGES);
1482 #if defined(DEBUG)
1484 /* Make sure that all the pages we return are valid. */
1485 int dbgi;
1486 if (error == 0 && m != NULL)
1487 for (dbgi = 0; dbgi < npages; dbgi++)
1488 KASSERT(m[dbgi] != NULL);
1490 #endif
1492 return error;
1495 /* --------------------------------------------------------------------- */
1498 tmpfs_putpages(void *v)
1500 struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
1501 voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
1502 voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
1503 int flags = ((struct vop_putpages_args *)v)->a_flags;
1505 int error;
1506 struct tmpfs_node *node;
1507 struct uvm_object *uobj;
1509 KASSERT(mutex_owned(&vp->v_interlock));
1511 node = VP_TO_TMPFS_NODE(vp);
1513 if (vp->v_type != VREG) {
1514 mutex_exit(&vp->v_interlock);
1515 return 0;
1518 uobj = node->tn_spec.tn_reg.tn_aobj;
1519 mutex_exit(&vp->v_interlock);
1521 mutex_enter(&uobj->vmobjlock);
1522 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1524 /* XXX mtime */
1526 return error;