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