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