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