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