tmpfs_vnops.c revision 1.44.2.2 1 /* $NetBSD: tmpfs_vnops.c,v 1.44.2.2 2007/12/12 17:33:15 ad 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.44.2.2 2007/12/12 17:33:15 ad 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);
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 if (error != 0)
190 goto out;
191
192 /* Keep the component name in the buffer for
193 * future uses. */
194 cnp->cn_flags |= SAVENAME;
195
196 error = EJUSTRETURN;
197 } else
198 error = ENOENT;
199 } else {
200 struct tmpfs_node *tnode;
201
202 /* The entry was found, so get its associated
203 * tmpfs_node. */
204 tnode = de->td_node;
205
206 /* If we are not at the last path component and
207 * found a non-directory or non-link entry (which
208 * may itself be pointing to a directory), raise
209 * an error. */
210 if ((tnode->tn_type != VDIR &&
211 tnode->tn_type != VLNK) &&
212 !(cnp->cn_flags & ISLASTCN)) {
213 error = ENOTDIR;
214 goto out;
215 }
216
217 /* If we are deleting or renaming the entry, keep
218 * track of its tmpfs_dirent so that it can be
219 * easily deleted later. */
220 if ((cnp->cn_flags & ISLASTCN) &&
221 (cnp->cn_nameiop == DELETE ||
222 cnp->cn_nameiop == RENAME)) {
223 if ((dnode->tn_mode & S_ISTXT) != 0 &&
224 kauth_authorize_generic(cnp->cn_cred,
225 KAUTH_GENERIC_ISSUSER, NULL) != 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 if (error != 0)
231 goto out;
232 } else
233 de = NULL;
234
235 /* Allocate a new vnode on the matching entry. */
236 error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
237 tnode->tn_lookup_dirent = de;
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 = curlwp;
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 bool 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 return error;
701 }
702
703 /* --------------------------------------------------------------------- */
704
705 int
706 tmpfs_link(void *v)
707 {
708 struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
709 struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
710 struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
711
712 int error;
713 struct tmpfs_dirent *de;
714 struct tmpfs_node *dnode;
715 struct tmpfs_node *node;
716
717 KASSERT(VOP_ISLOCKED(dvp));
718 KASSERT(cnp->cn_flags & HASBUF);
719 KASSERT(dvp != vp); /* XXX When can this be false? */
720
721 dnode = VP_TO_TMPFS_DIR(dvp);
722 node = VP_TO_TMPFS_NODE(vp);
723
724 /* Lock vp because we will need to run tmpfs_update over it, which
725 * needs the vnode to be locked. */
726 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
727 if (error != 0)
728 goto out1;
729
730 /* XXX: Why aren't the following two tests done by the caller? */
731
732 /* Hard links of directories are forbidden. */
733 if (vp->v_type == VDIR) {
734 error = EPERM;
735 goto out;
736 }
737
738 /* Cannot create cross-device links. */
739 if (dvp->v_mount != vp->v_mount) {
740 error = EXDEV;
741 goto out;
742 }
743
744 /* Ensure that we do not overflow the maximum number of links imposed
745 * by the system. */
746 KASSERT(node->tn_links <= LINK_MAX);
747 if (node->tn_links == LINK_MAX) {
748 error = EMLINK;
749 goto out;
750 }
751
752 /* We cannot create links of files marked immutable or append-only. */
753 if (node->tn_flags & (IMMUTABLE | APPEND)) {
754 error = EPERM;
755 goto out;
756 }
757
758 /* Allocate a new directory entry to represent the node. */
759 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
760 cnp->cn_nameptr, cnp->cn_namelen, &de);
761 if (error != 0)
762 goto out;
763
764 /* Insert the new directory entry into the appropriate directory. */
765 tmpfs_dir_attach(dvp, de);
766
767 /* vp link count has changed, so update node times. */
768 node->tn_status |= TMPFS_NODE_CHANGED;
769 tmpfs_update(vp, NULL, NULL, 0);
770
771 error = 0;
772
773 out:
774 VOP_UNLOCK(vp, 0);
775 out1:
776 PNBUF_PUT(cnp->cn_pnbuf);
777
778 vput(dvp);
779
780 return error;
781 }
782
783 /* --------------------------------------------------------------------- */
784
785 int
786 tmpfs_rename(void *v)
787 {
788 struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
789 struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
790 struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
791 struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
792 struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
793 struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
794
795 char *newname;
796 int error;
797 struct tmpfs_dirent *de;
798 struct tmpfs_mount *tmp;
799 struct tmpfs_node *fdnode;
800 struct tmpfs_node *fnode;
801 struct tmpfs_node *tnode;
802 struct tmpfs_node *tdnode;
803
804 KASSERT(VOP_ISLOCKED(tdvp));
805 KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
806 KASSERT(fcnp->cn_flags & HASBUF);
807 KASSERT(tcnp->cn_flags & HASBUF);
808
809 fdnode = VP_TO_TMPFS_DIR(fdvp);
810 fnode = VP_TO_TMPFS_NODE(fvp);
811 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
812 de = fnode->tn_lookup_dirent;
813
814 /* Disallow cross-device renames.
815 * XXX Why isn't this done by the caller? */
816 if (fvp->v_mount != tdvp->v_mount ||
817 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
818 error = EXDEV;
819 goto out;
820 }
821
822 tmp = VFS_TO_TMPFS(tdvp->v_mount);
823 tdnode = VP_TO_TMPFS_DIR(tdvp);
824
825 /* If source and target are the same file, there is nothing to do. */
826 if (fvp == tvp) {
827 error = 0;
828 goto out;
829 }
830
831 /* Avoid manipulating '.' and '..' entries. */
832 if (de == NULL) {
833 KASSERT(fvp->v_type == VDIR);
834 error = EINVAL;
835 goto out;
836 }
837 KASSERT(de->td_node == fnode);
838
839 /* If replacing an existing entry, ensure we can do the operation. */
840 if (tvp != NULL) {
841 KASSERT(tnode != NULL);
842 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
843 if (tnode->tn_size > 0) {
844 error = ENOTEMPTY;
845 goto out;
846 }
847 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
848 error = ENOTDIR;
849 goto out;
850 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
851 error = EISDIR;
852 goto out;
853 } else {
854 KASSERT(fnode->tn_type != VDIR &&
855 tnode->tn_type != VDIR);
856 }
857 }
858
859 /* If we need to move the directory between entries, lock the
860 * source so that we can safely operate on it. */
861
862 /* XXX: this is a potential locking order violation! */
863 if (fdnode != tdnode) {
864 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
865 if (error != 0)
866 goto out;
867 }
868
869 /* Make sure we have the correct cached dirent */
870 fcnp->cn_flags &= ~(MODMASK | SAVESTART);
871 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
872 if ((error = relookup(fdvp, &fvp, fcnp))) {
873 goto out_locked;
874 }
875 KASSERT(fvp != NULL);
876 /* Relookup always returns with vpp locked and 1UP referenced */
877 VOP_UNLOCK(fvp, 0);
878 vrele(((struct vop_rename_args *)v)->a_fvp);
879
880 /* Reacquire values. fvp might have changed. Since we only
881 * used fvp to sanitycheck fcnp values above, we can do this. */
882 fnode = VP_TO_TMPFS_NODE(fvp);
883 de = fnode->tn_lookup_dirent;
884
885 /* Ensure that we have enough memory to hold the new name, if it
886 * has to be changed. */
887 if (fcnp->cn_namelen != tcnp->cn_namelen ||
888 memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
889 newname = tmpfs_str_pool_get(&tmp->tm_str_pool,
890 tcnp->cn_namelen, 0);
891 if (newname == NULL) {
892 error = ENOSPC;
893 goto out_locked;
894 }
895 } else
896 newname = NULL;
897
898 /* If the node is being moved to another directory, we have to do
899 * the move. */
900 if (fdnode != tdnode) {
901 /* In case we are moving a directory, we have to adjust its
902 * parent to point to the new parent. */
903 if (de->td_node->tn_type == VDIR) {
904 struct tmpfs_node *n;
905
906 /* Ensure the target directory is not a child of the
907 * directory being moved. Otherwise, we'd end up
908 * with stale nodes. */
909 n = tdnode;
910 while (n != n->tn_spec.tn_dir.tn_parent) {
911 if (n == fnode) {
912 error = EINVAL;
913 goto out_locked;
914 }
915 n = n->tn_spec.tn_dir.tn_parent;
916 }
917
918 /* Adjust the parent pointer. */
919 TMPFS_VALIDATE_DIR(fnode);
920 de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
921
922 /* As a result of changing the target of the '..'
923 * entry, the link count of the source and target
924 * directories has to be adjusted. */
925 fdnode->tn_links--;
926 tdnode->tn_links++;
927 }
928
929 /* Do the move: just remove the entry from the source directory
930 * and insert it into the target one. */
931 tmpfs_dir_detach(fdvp, de);
932 tmpfs_dir_attach(tdvp, de);
933
934 /* Notify listeners of fdvp about the change in the directory.
935 * We can do it at this point because we aren't touching fdvp
936 * any more below. */
937 VN_KNOTE(fdvp, NOTE_WRITE);
938 }
939
940 /* If the name has changed, we need to make it effective by changing
941 * it in the directory entry. */
942 if (newname != NULL) {
943 KASSERT(tcnp->cn_namelen < MAXNAMLEN);
944 KASSERT(tcnp->cn_namelen < 0xffff);
945
946 tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
947 de->td_namelen);
948 de->td_namelen = (uint16_t)tcnp->cn_namelen;
949 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
950 de->td_name = newname;
951
952 fnode->tn_status |= TMPFS_NODE_CHANGED;
953 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
954 }
955
956 /* If we are overwriting an entry, we have to remove the old one
957 * from the target directory. */
958 if (tvp != NULL) {
959 KASSERT(tnode != NULL);
960
961 /* Remove the old entry from the target directory. */
962 de = tnode->tn_lookup_dirent;
963 tmpfs_dir_detach(tdvp, de);
964
965 /* Free the directory entry we just deleted. Note that the
966 * node referred by it will not be removed until the vnode is
967 * really reclaimed. */
968 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, true);
969 }
970
971 /* Notify listeners of tdvp about the change in the directory (either
972 * because a new entry was added or because one was removed) and
973 * listeners of fvp about the rename. */
974 VN_KNOTE(tdvp, NOTE_WRITE);
975 VN_KNOTE(fvp, NOTE_RENAME);
976
977 error = 0;
978
979 out_locked:
980 if (fdnode != tdnode)
981 VOP_UNLOCK(fdvp, 0);
982
983 out:
984 /* Release target nodes. */
985 if (tdvp == tvp)
986 vrele(tdvp);
987 else
988 vput(tdvp);
989 if (tvp != NULL)
990 vput(tvp);
991
992 /* Release source nodes. */
993 vrele(fdvp);
994 vrele(fvp);
995
996 return error;
997 }
998
999 /* --------------------------------------------------------------------- */
1000
1001 int
1002 tmpfs_mkdir(void *v)
1003 {
1004 struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
1005 struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1006 struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
1007 struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
1008
1009 KASSERT(vap->va_type == VDIR);
1010
1011 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1012 }
1013
1014 /* --------------------------------------------------------------------- */
1015
1016 int
1017 tmpfs_rmdir(void *v)
1018 {
1019 struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
1020 struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
1021
1022 int error;
1023 struct tmpfs_dirent *de;
1024 struct tmpfs_mount *tmp;
1025 struct tmpfs_node *dnode;
1026 struct tmpfs_node *node;
1027
1028 KASSERT(VOP_ISLOCKED(dvp));
1029 KASSERT(VOP_ISLOCKED(vp));
1030
1031 tmp = VFS_TO_TMPFS(dvp->v_mount);
1032 dnode = VP_TO_TMPFS_DIR(dvp);
1033 node = VP_TO_TMPFS_DIR(vp);
1034 error = 0;
1035
1036 /* Directories with more than two entries ('.' and '..') cannot be
1037 * removed. */
1038 if (node->tn_size > 0) {
1039 error = ENOTEMPTY;
1040 goto out;
1041 }
1042
1043 /* This invariant holds only if we are not trying to remove "..".
1044 * We checked for that above so this is safe now. */
1045 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
1046
1047 /* Get the directory entry associated with node (vp). This was
1048 * filled by tmpfs_lookup while looking up the entry. */
1049 de = node->tn_lookup_dirent;
1050 KASSERT(TMPFS_DIRENT_MATCHES(de,
1051 ((struct vop_rmdir_args *)v)->a_cnp->cn_nameptr,
1052 ((struct vop_rmdir_args *)v)->a_cnp->cn_namelen));
1053
1054 /* Check flags to see if we are allowed to remove the directory. */
1055 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
1056 error = EPERM;
1057 goto out;
1058 }
1059
1060 /* Detach the directory entry from the directory (dnode). */
1061 tmpfs_dir_detach(dvp, de);
1062
1063 node->tn_links--;
1064 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1065 TMPFS_NODE_MODIFIED;
1066 node->tn_spec.tn_dir.tn_parent->tn_links--;
1067 node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1068 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1069
1070 /* Release the parent. */
1071 cache_purge(dvp); /* XXX Is this needed? */
1072
1073 /* Free the directory entry we just deleted. Note that the node
1074 * referred by it will not be removed until the vnode is really
1075 * reclaimed. */
1076 tmpfs_free_dirent(tmp, de, true);
1077
1078 KASSERT(node->tn_links == 1);
1079 out:
1080 /* Release the nodes. */
1081 vput(dvp);
1082 vput(vp);
1083
1084 return error;
1085 }
1086
1087 /* --------------------------------------------------------------------- */
1088
1089 int
1090 tmpfs_symlink(void *v)
1091 {
1092 struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
1093 struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
1094 struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
1095 struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
1096 char *target = ((struct vop_symlink_args *)v)->a_target;
1097
1098 KASSERT(vap->va_type == VLNK);
1099
1100 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1101 }
1102
1103 /* --------------------------------------------------------------------- */
1104
1105 int
1106 tmpfs_readdir(void *v)
1107 {
1108 struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
1109 struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
1110 int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
1111 off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
1112 int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
1113
1114 int error;
1115 off_t startoff;
1116 off_t cnt;
1117 struct tmpfs_node *node;
1118
1119 KASSERT(VOP_ISLOCKED(vp));
1120
1121 /* This operation only makes sense on directory nodes. */
1122 if (vp->v_type != VDIR) {
1123 error = ENOTDIR;
1124 goto out;
1125 }
1126
1127 node = VP_TO_TMPFS_DIR(vp);
1128
1129 startoff = uio->uio_offset;
1130
1131 cnt = 0;
1132 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1133 error = tmpfs_dir_getdotdent(node, uio);
1134 if (error == -1) {
1135 error = 0;
1136 goto outok;
1137 } else if (error != 0)
1138 goto outok;
1139 cnt++;
1140 }
1141
1142 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1143 error = tmpfs_dir_getdotdotdent(node, uio);
1144 if (error == -1) {
1145 error = 0;
1146 goto outok;
1147 } else if (error != 0)
1148 goto outok;
1149 cnt++;
1150 }
1151
1152 error = tmpfs_dir_getdents(node, uio, &cnt);
1153 if (error == -1)
1154 error = 0;
1155 KASSERT(error >= 0);
1156
1157 outok:
1158 /* This label assumes that startoff has been
1159 * initialized. If the compiler didn't spit out warnings, we'd
1160 * simply make this one be 'out' and drop 'outok'. */
1161
1162 if (eofflag != NULL)
1163 *eofflag =
1164 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1165
1166 /* Update NFS-related variables. */
1167 if (error == 0 && cookies != NULL && ncookies != NULL) {
1168 off_t i;
1169 off_t off = startoff;
1170 struct tmpfs_dirent *de = NULL;
1171
1172 *ncookies = cnt;
1173 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1174
1175 for (i = 0; i < cnt; i++) {
1176 KASSERT(off != TMPFS_DIRCOOKIE_EOF);
1177 if (off == TMPFS_DIRCOOKIE_DOT) {
1178 off = TMPFS_DIRCOOKIE_DOTDOT;
1179 } else {
1180 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1181 de = TAILQ_FIRST(&node->tn_spec.
1182 tn_dir.tn_dir);
1183 } else if (de != NULL) {
1184 de = TAILQ_NEXT(de, td_entries);
1185 } else {
1186 de = tmpfs_dir_lookupbycookie(node,
1187 off);
1188 KASSERT(de != NULL);
1189 de = TAILQ_NEXT(de, td_entries);
1190 }
1191 if (de == NULL) {
1192 off = TMPFS_DIRCOOKIE_EOF;
1193 } else {
1194 off = tmpfs_dircookie(de);
1195 }
1196 }
1197
1198 (*cookies)[i] = off;
1199 }
1200 KASSERT(uio->uio_offset == off);
1201 }
1202
1203 out:
1204 KASSERT(VOP_ISLOCKED(vp));
1205
1206 return error;
1207 }
1208
1209 /* --------------------------------------------------------------------- */
1210
1211 int
1212 tmpfs_readlink(void *v)
1213 {
1214 struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
1215 struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
1216
1217 int error;
1218 struct tmpfs_node *node;
1219
1220 KASSERT(VOP_ISLOCKED(vp));
1221 KASSERT(uio->uio_offset == 0);
1222 KASSERT(vp->v_type == VLNK);
1223
1224 node = VP_TO_TMPFS_NODE(vp);
1225
1226 error = uiomove(node->tn_spec.tn_lnk.tn_link,
1227 MIN(node->tn_size, uio->uio_resid), uio);
1228 node->tn_status |= TMPFS_NODE_ACCESSED;
1229
1230 KASSERT(VOP_ISLOCKED(vp));
1231
1232 return error;
1233 }
1234
1235 /* --------------------------------------------------------------------- */
1236
1237 int
1238 tmpfs_inactive(void *v)
1239 {
1240 struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
1241
1242 struct tmpfs_node *node;
1243
1244 KASSERT(VOP_ISLOCKED(vp));
1245
1246 node = VP_TO_TMPFS_NODE(vp);
1247 *((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
1248 VOP_UNLOCK(vp, 0);
1249
1250 return 0;
1251 }
1252
1253 /* --------------------------------------------------------------------- */
1254
1255 int
1256 tmpfs_reclaim(void *v)
1257 {
1258 struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
1259
1260 struct tmpfs_mount *tmp;
1261 struct tmpfs_node *node;
1262
1263 node = VP_TO_TMPFS_NODE(vp);
1264 tmp = VFS_TO_TMPFS(vp->v_mount);
1265
1266 cache_purge(vp);
1267 tmpfs_free_vp(vp);
1268
1269 /* If the node referenced by this vnode was deleted by the user,
1270 * we must free its associated data structures (now that the vnode
1271 * is being reclaimed). */
1272 if (node->tn_links == 0)
1273 tmpfs_free_node(tmp, node);
1274
1275 KASSERT(vp->v_data == NULL);
1276
1277 return 0;
1278 }
1279
1280 /* --------------------------------------------------------------------- */
1281
1282 int
1283 tmpfs_print(void *v)
1284 {
1285 struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
1286
1287 struct tmpfs_node *node;
1288
1289 node = VP_TO_TMPFS_NODE(vp);
1290
1291 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1292 node, node->tn_flags, node->tn_links);
1293 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1294 ", status 0x%x\n",
1295 node->tn_mode, node->tn_uid, node->tn_gid,
1296 (uintmax_t)node->tn_size, node->tn_status);
1297
1298 if (vp->v_type == VFIFO)
1299 fifo_printinfo(vp);
1300 lockmgr_printinfo(&vp->v_lock);
1301
1302 printf("\n");
1303
1304 return 0;
1305 }
1306
1307 /* --------------------------------------------------------------------- */
1308
1309 int
1310 tmpfs_pathconf(void *v)
1311 {
1312 int name = ((struct vop_pathconf_args *)v)->a_name;
1313 register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
1314
1315 int error;
1316
1317 error = 0;
1318
1319 switch (name) {
1320 case _PC_LINK_MAX:
1321 *retval = LINK_MAX;
1322 break;
1323
1324 case _PC_NAME_MAX:
1325 *retval = NAME_MAX;
1326 break;
1327
1328 case _PC_PATH_MAX:
1329 *retval = PATH_MAX;
1330 break;
1331
1332 case _PC_PIPE_BUF:
1333 *retval = PIPE_BUF;
1334 break;
1335
1336 case _PC_CHOWN_RESTRICTED:
1337 *retval = 1;
1338 break;
1339
1340 case _PC_NO_TRUNC:
1341 *retval = 1;
1342 break;
1343
1344 case _PC_SYNC_IO:
1345 *retval = 1;
1346 break;
1347
1348 case _PC_FILESIZEBITS:
1349 *retval = 0; /* XXX Don't know which value should I return. */
1350 break;
1351
1352 default:
1353 error = EINVAL;
1354 }
1355
1356 return error;
1357 }
1358
1359 /* --------------------------------------------------------------------- */
1360
1361 int
1362 tmpfs_advlock(void *v)
1363 {
1364 struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
1365
1366 struct tmpfs_node *node;
1367
1368 node = VP_TO_TMPFS_NODE(vp);
1369
1370 return lf_advlock(v, &node->tn_lockf, node->tn_size);
1371 }
1372
1373 /* --------------------------------------------------------------------- */
1374
1375 int
1376 tmpfs_getpages(void *v)
1377 {
1378 struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
1379 voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
1380 struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
1381 int *count = ((struct vop_getpages_args *)v)->a_count;
1382 int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
1383 vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
1384 int advice = ((struct vop_getpages_args *)v)->a_advice;
1385 int flags = ((struct vop_getpages_args *)v)->a_flags;
1386
1387 int error;
1388 int i;
1389 struct tmpfs_node *node;
1390 struct uvm_object *uobj;
1391 int npages = *count;
1392
1393 KASSERT(vp->v_type == VREG);
1394 KASSERT(mutex_owned(&vp->v_interlock));
1395
1396 node = VP_TO_TMPFS_NODE(vp);
1397 uobj = node->tn_spec.tn_reg.tn_aobj;
1398
1399 /* We currently don't rely on PGO_PASTEOF. */
1400
1401 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1402 if ((flags & PGO_LOCKED) == 0)
1403 mutex_exit(&vp->v_interlock);
1404 return EINVAL;
1405 }
1406
1407 if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1408 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1409 }
1410
1411 if ((flags & PGO_LOCKED) != 0)
1412 return EBUSY;
1413
1414 if ((flags & PGO_NOTIMESTAMP) == 0) {
1415 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1416 node->tn_status |= TMPFS_NODE_ACCESSED;
1417
1418 if ((access_type & VM_PROT_WRITE) != 0)
1419 node->tn_status |= TMPFS_NODE_MODIFIED;
1420 }
1421
1422 mutex_exit(&vp->v_interlock);
1423
1424 /*
1425 * Make sure that the array on which we will store the
1426 * gotten pages is clean. Otherwise uao_get (pointed to by
1427 * the pgo_get below) gets confused and does not return the
1428 * appropriate pages.
1429 *
1430 * XXX This shall be revisited when kern/32166 is addressed
1431 * because the loop to clean m[i] will most likely be redundant
1432 * as well as the PGO_ALLPAGES flag.
1433 */
1434 if (m != NULL)
1435 for (i = 0; i < npages; i++)
1436 m[i] = NULL;
1437 mutex_enter(&uobj->vmobjlock);
1438 error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
1439 access_type, advice, flags | PGO_ALLPAGES);
1440 #if defined(DEBUG)
1441 {
1442 /* Make sure that all the pages we return are valid. */
1443 int dbgi;
1444 if (error == 0 && m != NULL)
1445 for (dbgi = 0; dbgi < npages; dbgi++)
1446 KASSERT(m[dbgi] != NULL);
1447 }
1448 #endif
1449
1450 return error;
1451 }
1452
1453 /* --------------------------------------------------------------------- */
1454
1455 int
1456 tmpfs_putpages(void *v)
1457 {
1458 struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
1459 voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
1460 voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
1461 int flags = ((struct vop_putpages_args *)v)->a_flags;
1462
1463 int error;
1464 struct tmpfs_node *node;
1465 struct uvm_object *uobj;
1466
1467 KASSERT(mutex_owned(&vp->v_interlock));
1468
1469 node = VP_TO_TMPFS_NODE(vp);
1470
1471 if (vp->v_type != VREG) {
1472 mutex_exit(&vp->v_interlock);
1473 return 0;
1474 }
1475
1476 uobj = node->tn_spec.tn_reg.tn_aobj;
1477 mutex_exit(&vp->v_interlock);
1478
1479 mutex_enter(&uobj->vmobjlock);
1480 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1481
1482 /* XXX mtime */
1483
1484 return error;
1485 }
1486