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