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