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