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