tmpfs_vnops.c revision 1.92.2.6 1 /* $NetBSD: tmpfs_vnops.c,v 1.92.2.6 2013/01/16 05:33:41 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * tmpfs vnode interface.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.92.2.6 2013/01/16 05:33:41 yamt Exp $");
39
40 #include <sys/param.h>
41 #include <sys/dirent.h>
42 #include <sys/fcntl.h>
43 #include <sys/event.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/stat.h>
47 #include <sys/uio.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
50 #include <sys/lockf.h>
51 #include <sys/kauth.h>
52
53 #include <uvm/uvm.h>
54
55 #include <miscfs/fifofs/fifo.h>
56 #include <miscfs/genfs/genfs.h>
57 #include <fs/tmpfs/tmpfs_vnops.h>
58 #include <fs/tmpfs/tmpfs.h>
59
60 /*
61 * vnode operations vector used for files stored in a tmpfs file system.
62 */
63 int (**tmpfs_vnodeop_p)(void *);
64 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
65 { &vop_default_desc, vn_default_error },
66 { &vop_lookup_desc, tmpfs_lookup },
67 { &vop_create_desc, tmpfs_create },
68 { &vop_mknod_desc, tmpfs_mknod },
69 { &vop_open_desc, tmpfs_open },
70 { &vop_close_desc, tmpfs_close },
71 { &vop_access_desc, tmpfs_access },
72 { &vop_getattr_desc, tmpfs_getattr },
73 { &vop_setattr_desc, tmpfs_setattr },
74 { &vop_read_desc, tmpfs_read },
75 { &vop_write_desc, tmpfs_write },
76 { &vop_ioctl_desc, tmpfs_ioctl },
77 { &vop_fcntl_desc, tmpfs_fcntl },
78 { &vop_poll_desc, tmpfs_poll },
79 { &vop_kqfilter_desc, tmpfs_kqfilter },
80 { &vop_revoke_desc, tmpfs_revoke },
81 { &vop_mmap_desc, tmpfs_mmap },
82 { &vop_fsync_desc, tmpfs_fsync },
83 { &vop_seek_desc, tmpfs_seek },
84 { &vop_remove_desc, tmpfs_remove },
85 { &vop_link_desc, tmpfs_link },
86 { &vop_rename_desc, tmpfs_rename },
87 { &vop_mkdir_desc, tmpfs_mkdir },
88 { &vop_rmdir_desc, tmpfs_rmdir },
89 { &vop_symlink_desc, tmpfs_symlink },
90 { &vop_readdir_desc, tmpfs_readdir },
91 { &vop_readlink_desc, tmpfs_readlink },
92 { &vop_abortop_desc, tmpfs_abortop },
93 { &vop_inactive_desc, tmpfs_inactive },
94 { &vop_reclaim_desc, tmpfs_reclaim },
95 { &vop_lock_desc, tmpfs_lock },
96 { &vop_unlock_desc, tmpfs_unlock },
97 { &vop_bmap_desc, tmpfs_bmap },
98 { &vop_strategy_desc, tmpfs_strategy },
99 { &vop_print_desc, tmpfs_print },
100 { &vop_pathconf_desc, tmpfs_pathconf },
101 { &vop_islocked_desc, tmpfs_islocked },
102 { &vop_advlock_desc, tmpfs_advlock },
103 { &vop_bwrite_desc, tmpfs_bwrite },
104 { &vop_getpages_desc, tmpfs_getpages },
105 { &vop_putpages_desc, tmpfs_putpages },
106 { &vop_whiteout_desc, tmpfs_whiteout },
107 { NULL, NULL }
108 };
109
110 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc = {
111 &tmpfs_vnodeop_p, tmpfs_vnodeop_entries
112 };
113
114 /*
115 * tmpfs_lookup: path name traversal routine.
116 *
117 * Arguments: dvp (directory being searched), vpp (result),
118 * cnp (component name - path).
119 *
120 * => Caller holds a reference and lock on dvp.
121 * => We return looked-up vnode (vpp) locked, with a reference held.
122 */
123 int
124 tmpfs_lookup(void *v)
125 {
126 struct vop_lookup_args /* {
127 struct vnode *a_dvp;
128 struct vnode **a_vpp;
129 struct componentname *a_cnp;
130 } */ *ap = v;
131 vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
132 struct componentname *cnp = ap->a_cnp;
133 const bool lastcn = (cnp->cn_flags & ISLASTCN) != 0;
134 tmpfs_node_t *dnode, *tnode;
135 tmpfs_dirent_t *de;
136 int cachefound, iswhiteout;
137 int error;
138
139 KASSERT(VOP_ISLOCKED(dvp));
140
141 dnode = VP_TO_TMPFS_DIR(dvp);
142 *vpp = NULL;
143
144 /* Check accessibility of directory. */
145 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
146 if (error) {
147 goto out;
148 }
149
150 /*
151 * If requesting the last path component on a read-only file system
152 * with a write operation, deny it.
153 */
154 if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
155 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
156 error = EROFS;
157 goto out;
158 }
159
160 /*
161 * Avoid doing a linear scan of the directory if the requested
162 * directory/name couple is already in the cache.
163 */
164 cachefound = cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
165 cnp->cn_nameiop, cnp->cn_flags,
166 &iswhiteout, vpp);
167 if (iswhiteout) {
168 cnp->cn_flags |= ISWHITEOUT;
169 }
170 if (cachefound && *vpp == NULLVP) {
171 /* Negative cache hit. */
172 error = ENOENT;
173 goto out;
174 } else if (cachefound) {
175 error = 0;
176 goto out;
177 }
178
179 if (cnp->cn_flags & ISDOTDOT) {
180 tmpfs_node_t *pnode;
181
182 /*
183 * Lookup of ".." case.
184 */
185 if (lastcn && cnp->cn_nameiop == RENAME) {
186 error = EINVAL;
187 goto out;
188 }
189 KASSERT(dnode->tn_type == VDIR);
190 pnode = dnode->tn_spec.tn_dir.tn_parent;
191 if (pnode == NULL) {
192 error = ENOENT;
193 goto out;
194 }
195
196 /*
197 * Lock the parent tn_vlock before releasing the vnode lock,
198 * and thus prevents parent from disappearing.
199 */
200 mutex_enter(&pnode->tn_vlock);
201 VOP_UNLOCK(dvp);
202
203 /*
204 * Get a vnode of the '..' entry and re-acquire the lock.
205 * Release the tn_vlock.
206 */
207 error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
208 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
209 goto out;
210
211 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
212 /*
213 * Lookup of "." case.
214 */
215 if (lastcn && cnp->cn_nameiop == RENAME) {
216 error = EISDIR;
217 goto out;
218 }
219 vref(dvp);
220 *vpp = dvp;
221 error = 0;
222 goto done;
223 }
224
225 /*
226 * Other lookup cases: perform directory scan.
227 */
228 de = tmpfs_dir_lookup(dnode, cnp);
229 if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
230 /*
231 * The entry was not found in the directory. This is valid
232 * if we are creating or renaming an entry and are working
233 * on the last component of the path name.
234 */
235 if (lastcn && (cnp->cn_nameiop == CREATE ||
236 cnp->cn_nameiop == RENAME)) {
237 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
238 if (error) {
239 goto out;
240 }
241 error = EJUSTRETURN;
242 } else {
243 error = ENOENT;
244 }
245 if (de) {
246 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
247 cnp->cn_flags |= ISWHITEOUT;
248 }
249 goto done;
250 }
251
252 tnode = de->td_node;
253
254 /*
255 * If it is not the last path component and found a non-directory
256 * or non-link entry (which may itself be pointing to a directory),
257 * raise an error.
258 */
259 if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
260 error = ENOTDIR;
261 goto out;
262 }
263
264 /* Check the permissions. */
265 if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
266 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
267 if (error)
268 goto out;
269
270 if ((dnode->tn_mode & S_ISTXT) != 0) {
271 error = kauth_authorize_vnode(cnp->cn_cred,
272 KAUTH_VNODE_DELETE, tnode->tn_vnode,
273 dnode->tn_vnode, genfs_can_sticky(cnp->cn_cred,
274 dnode->tn_uid, tnode->tn_uid));
275 if (error) {
276 error = EPERM;
277 goto out;
278 }
279 }
280 }
281
282 /* Get a vnode for the matching entry. */
283 mutex_enter(&tnode->tn_vlock);
284 error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
285 done:
286 /*
287 * Cache the result, unless request was for creation (as it does
288 * not improve the performance).
289 */
290 if (cnp->cn_nameiop != CREATE) {
291 cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
292 cnp->cn_flags);
293 }
294 out:
295 KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
296 KASSERT(VOP_ISLOCKED(dvp));
297
298 return error;
299 }
300
301 int
302 tmpfs_create(void *v)
303 {
304 struct vop_create_args /* {
305 struct vnode *a_dvp;
306 struct vnode **a_vpp;
307 struct componentname *a_cnp;
308 struct vattr *a_vap;
309 } */ *ap = v;
310 vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
311 struct componentname *cnp = ap->a_cnp;
312 struct vattr *vap = ap->a_vap;
313
314 KASSERT(VOP_ISLOCKED(dvp));
315 KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
316 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
317 }
318
319 int
320 tmpfs_mknod(void *v)
321 {
322 struct vop_mknod_args /* {
323 struct vnode *a_dvp;
324 struct vnode **a_vpp;
325 struct componentname *a_cnp;
326 struct vattr *a_vap;
327 } */ *ap = v;
328 vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
329 struct componentname *cnp = ap->a_cnp;
330 struct vattr *vap = ap->a_vap;
331 enum vtype vt = vap->va_type;
332
333 if (vt != VBLK && vt != VCHR && vt != VFIFO) {
334 vput(dvp);
335 return EINVAL;
336 }
337 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
338 }
339
340 int
341 tmpfs_open(void *v)
342 {
343 struct vop_open_args /* {
344 struct vnode *a_vp;
345 int a_mode;
346 kauth_cred_t a_cred;
347 } */ *ap = v;
348 vnode_t *vp = ap->a_vp;
349 mode_t mode = ap->a_mode;
350 tmpfs_node_t *node;
351
352 KASSERT(VOP_ISLOCKED(vp));
353
354 node = VP_TO_TMPFS_NODE(vp);
355 if (node->tn_links < 1) {
356 /*
357 * The file is still active, but all its names have been
358 * removed (e.g. by a "rmdir $(pwd)"). It cannot be opened
359 * any more, as it is about to be destroyed.
360 */
361 return ENOENT;
362 }
363
364 /* If the file is marked append-only, deny write requests. */
365 if ((node->tn_flags & APPEND) != 0 &&
366 (mode & (FWRITE | O_APPEND)) == FWRITE) {
367 return EPERM;
368 }
369 return 0;
370 }
371
372 int
373 tmpfs_close(void *v)
374 {
375 struct vop_close_args /* {
376 struct vnode *a_vp;
377 int a_fflag;
378 kauth_cred_t a_cred;
379 } */ *ap = v;
380 vnode_t *vp = ap->a_vp;
381
382 KASSERT(VOP_ISLOCKED(vp));
383
384 tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
385 return 0;
386 }
387
388 int
389 tmpfs_access(void *v)
390 {
391 struct vop_access_args /* {
392 struct vnode *a_vp;
393 int a_mode;
394 kauth_cred_t a_cred;
395 } */ *ap = v;
396 vnode_t *vp = ap->a_vp;
397 mode_t mode = ap->a_mode;
398 kauth_cred_t cred = ap->a_cred;
399 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
400 const bool writing = (mode & VWRITE) != 0;
401
402 KASSERT(VOP_ISLOCKED(vp));
403
404 /* Possible? */
405 switch (vp->v_type) {
406 case VDIR:
407 case VLNK:
408 case VREG:
409 if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
410 return EROFS;
411 }
412 break;
413 case VBLK:
414 case VCHR:
415 case VSOCK:
416 case VFIFO:
417 break;
418 default:
419 return EINVAL;
420 }
421 if (writing && (node->tn_flags & IMMUTABLE) != 0) {
422 return EPERM;
423 }
424
425 return kauth_authorize_vnode(cred, kauth_access_action(mode,
426 vp->v_type, node->tn_mode), vp, NULL, genfs_can_access(vp->v_type,
427 node->tn_mode, node->tn_uid, node->tn_gid, mode, cred));
428 }
429
430 int
431 tmpfs_getattr(void *v)
432 {
433 struct vop_getattr_args /* {
434 struct vnode *a_vp;
435 struct vattr *a_vap;
436 kauth_cred_t a_cred;
437 } */ *ap = v;
438 vnode_t *vp = ap->a_vp;
439 struct vattr *vap = ap->a_vap;
440 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
441
442 vattr_null(vap);
443
444 tmpfs_update(vp, NULL, NULL, NULL, 0);
445
446 vap->va_type = vp->v_type;
447 vap->va_mode = node->tn_mode;
448 vap->va_nlink = node->tn_links;
449 vap->va_uid = node->tn_uid;
450 vap->va_gid = node->tn_gid;
451 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
452 vap->va_fileid = node->tn_id;
453 vap->va_size = node->tn_size;
454 vap->va_blocksize = PAGE_SIZE;
455 vap->va_atime = node->tn_atime;
456 vap->va_mtime = node->tn_mtime;
457 vap->va_ctime = node->tn_ctime;
458 vap->va_birthtime = node->tn_birthtime;
459 vap->va_gen = TMPFS_NODE_GEN(node);
460 vap->va_flags = node->tn_flags;
461 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
462 node->tn_spec.tn_dev.tn_rdev : VNOVAL;
463 vap->va_bytes = round_page(node->tn_size);
464 vap->va_filerev = VNOVAL;
465 vap->va_vaflags = 0;
466 vap->va_spare = VNOVAL; /* XXX */
467
468 return 0;
469 }
470
471 #define GOODTIME(tv) ((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
472 /* XXX Should this operation be atomic? I think it should, but code in
473 * XXX other places (e.g., ufs) doesn't seem to be... */
474 int
475 tmpfs_setattr(void *v)
476 {
477 struct vop_setattr_args /* {
478 struct vnode *a_vp;
479 struct vattr *a_vap;
480 kauth_cred_t a_cred;
481 } */ *ap = v;
482 vnode_t *vp = ap->a_vp;
483 struct vattr *vap = ap->a_vap;
484 kauth_cred_t cred = ap->a_cred;
485 lwp_t *l = curlwp;
486 int error = 0;
487
488 KASSERT(VOP_ISLOCKED(vp));
489
490 /* Abort if any unsettable attribute is given. */
491 if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
492 vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
493 vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
494 vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
495 vap->va_bytes != VNOVAL) {
496 return EINVAL;
497 }
498 if (error == 0 && (vap->va_flags != VNOVAL))
499 error = tmpfs_chflags(vp, vap->va_flags, cred, l);
500
501 if (error == 0 && (vap->va_size != VNOVAL))
502 error = tmpfs_chsize(vp, vap->va_size, cred, l);
503
504 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
505 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
506
507 if (error == 0 && (vap->va_mode != VNOVAL))
508 error = tmpfs_chmod(vp, vap->va_mode, cred, l);
509
510 if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
511 || GOODTIME(&vap->va_birthtime))) {
512 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
513 &vap->va_birthtime, vap->va_vaflags, cred, l);
514 if (error == 0)
515 return 0;
516 }
517 tmpfs_update(vp, NULL, NULL, NULL, 0);
518 return error;
519 }
520
521 int
522 tmpfs_read(void *v)
523 {
524 struct vop_read_args /* {
525 struct vnode *a_vp;
526 struct uio *a_uio;
527 int a_ioflag;
528 kauth_cred_t a_cred;
529 } */ *ap = v;
530 vnode_t *vp = ap->a_vp;
531 struct uio *uio = ap->a_uio;
532 const int ioflag = ap->a_ioflag;
533 tmpfs_node_t *node;
534 struct uvm_object *uobj;
535 const int advice = IO_ADV_DECODE(ioflag);
536 int error;
537
538 KASSERT(VOP_ISLOCKED(vp));
539
540 if (vp->v_type != VREG) {
541 return EISDIR;
542 }
543 if (uio->uio_offset < 0) {
544 return EINVAL;
545 }
546
547 node = VP_TO_TMPFS_NODE(vp);
548 node->tn_status |= TMPFS_NODE_ACCESSED;
549 uobj = node->tn_spec.tn_reg.tn_aobj;
550 error = 0;
551
552 if (uio->uio_offset + uio->uio_resid <= node->tn_size) {
553 uvm_loanobj(&vp->v_uobj, uio, advice);
554 }
555 while (error == 0 && uio->uio_resid > 0) {
556 vsize_t len;
557
558 if (node->tn_size <= uio->uio_offset) {
559 break;
560 }
561 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
562 if (len == 0) {
563 break;
564 }
565 error = ubc_uiomove(uobj, uio, len, advice,
566 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
567 }
568 return error;
569 }
570
571 int
572 tmpfs_write(void *v)
573 {
574 struct vop_write_args /* {
575 struct vnode *a_vp;
576 struct uio *a_uio;
577 int a_ioflag;
578 kauth_cred_t a_cred;
579 } */ *ap = v;
580 vnode_t *vp = ap->a_vp;
581 struct uio *uio = ap->a_uio;
582 const int ioflag = ap->a_ioflag;
583 tmpfs_node_t *node;
584 struct uvm_object *uobj;
585 off_t oldsize;
586 bool extended;
587 int error;
588
589 KASSERT(VOP_ISLOCKED(vp));
590
591 node = VP_TO_TMPFS_NODE(vp);
592 oldsize = node->tn_size;
593
594 if (uio->uio_offset < 0 || vp->v_type != VREG) {
595 error = EINVAL;
596 goto out;
597 }
598 if (uio->uio_resid == 0) {
599 error = 0;
600 goto out;
601 }
602 if (ioflag & IO_APPEND) {
603 uio->uio_offset = node->tn_size;
604 }
605
606 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
607 if (extended) {
608 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
609 if (error)
610 goto out;
611 }
612
613 uobj = node->tn_spec.tn_reg.tn_aobj;
614 error = 0;
615 while (error == 0 && uio->uio_resid > 0) {
616 vsize_t len;
617
618 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
619 if (len == 0) {
620 break;
621 }
622 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
623 UBC_WRITE | UBC_UNMAP_FLAG(vp));
624 }
625 if (error) {
626 (void)tmpfs_reg_resize(vp, oldsize);
627 }
628
629 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
630 (extended ? TMPFS_NODE_CHANGED : 0);
631 VN_KNOTE(vp, NOTE_WRITE);
632 out:
633 if (error) {
634 KASSERT(oldsize == node->tn_size);
635 } else {
636 KASSERT(uio->uio_resid == 0);
637 }
638 return error;
639 }
640
641 int
642 tmpfs_fsync(void *v)
643 {
644 struct vop_fsync_args /* {
645 struct vnode *a_vp;
646 kauth_cred_t a_cred;
647 int a_flags;
648 off_t a_offlo;
649 off_t a_offhi;
650 struct lwp *a_l;
651 } */ *ap = v;
652 vnode_t *vp = ap->a_vp;
653
654 /* Nothing to do. Just update. */
655 KASSERT(VOP_ISLOCKED(vp));
656 tmpfs_update(vp, NULL, NULL, NULL, 0);
657 return 0;
658 }
659
660 /*
661 * tmpfs_remove: unlink a file.
662 *
663 * => Both directory (dvp) and file (vp) are locked.
664 * => We unlock and drop the reference on both.
665 */
666 int
667 tmpfs_remove(void *v)
668 {
669 struct vop_remove_args /* {
670 struct vnode *a_dvp;
671 struct vnode *a_vp;
672 struct componentname *a_cnp;
673 } */ *ap = v;
674 vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
675 tmpfs_node_t *node;
676 tmpfs_dirent_t *de;
677 int error;
678
679 KASSERT(VOP_ISLOCKED(dvp));
680 KASSERT(VOP_ISLOCKED(vp));
681
682 if (vp->v_type == VDIR) {
683 error = EPERM;
684 goto out;
685 }
686 node = VP_TO_TMPFS_NODE(vp);
687
688 /* Files marked as immutable or append-only cannot be deleted. */
689 if (node->tn_flags & (IMMUTABLE | APPEND)) {
690 error = EPERM;
691 goto out;
692 }
693
694 /* Lookup the directory entry (check the cached hint first). */
695 de = tmpfs_dir_cached(node);
696 if (de == NULL) {
697 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
698 struct componentname *cnp = ap->a_cnp;
699 de = tmpfs_dir_lookup(dnode, cnp);
700 }
701 KASSERT(de && de->td_node == node);
702
703 /*
704 * Remove the entry from the directory (drops the link count) and
705 * destroy it or replace it with a whiteout.
706 * Note: the inode referred by it will not be destroyed
707 * until the vnode is reclaimed/recycled.
708 */
709 tmpfs_dir_detach(dvp, de);
710 if (ap->a_cnp->cn_flags & DOWHITEOUT)
711 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
712 else
713 tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
714 error = 0;
715 out:
716 /* Drop the references and unlock the vnodes. */
717 vput(vp);
718 if (dvp == vp) {
719 vrele(dvp);
720 } else {
721 vput(dvp);
722 }
723 return error;
724 }
725
726 /*
727 * tmpfs_link: create a hard link.
728 */
729 int
730 tmpfs_link(void *v)
731 {
732 struct vop_link_args /* {
733 struct vnode *a_dvp;
734 struct vnode *a_vp;
735 struct componentname *a_cnp;
736 } */ *ap = v;
737 vnode_t *dvp = ap->a_dvp;
738 vnode_t *vp = ap->a_vp;
739 struct componentname *cnp = ap->a_cnp;
740 tmpfs_node_t *dnode, *node;
741 tmpfs_dirent_t *de;
742 int error;
743
744 KASSERT(dvp != vp);
745 KASSERT(VOP_ISLOCKED(dvp));
746 KASSERT(vp->v_type != VDIR);
747 KASSERT(dvp->v_mount == vp->v_mount);
748
749 dnode = VP_TO_TMPFS_DIR(dvp);
750 node = VP_TO_TMPFS_NODE(vp);
751
752 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
753
754 /* Check for maximum number of links limit. */
755 if (node->tn_links == LINK_MAX) {
756 error = EMLINK;
757 goto out;
758 }
759 KASSERT(node->tn_links < LINK_MAX);
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 inode. */
768 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
769 cnp->cn_nameptr, cnp->cn_namelen, &de);
770 if (error) {
771 goto out;
772 }
773
774 /*
775 * Insert the entry into the directory.
776 * It will increase the inode link count.
777 */
778 tmpfs_dir_attach(dvp, de, node);
779
780 /* Update the timestamps and trigger the event. */
781 if (node->tn_vnode) {
782 VN_KNOTE(node->tn_vnode, NOTE_LINK);
783 }
784 node->tn_status |= TMPFS_NODE_CHANGED;
785 tmpfs_update(vp, NULL, NULL, NULL, 0);
786 error = 0;
787 out:
788 VOP_UNLOCK(vp);
789 vput(dvp);
790 return error;
791 }
792
793 int
794 tmpfs_mkdir(void *v)
795 {
796 struct vop_mkdir_args /* {
797 struct vnode *a_dvp;
798 struct vnode **a_vpp;
799 struct componentname *a_cnp;
800 struct vattr *a_vap;
801 } */ *ap = v;
802 vnode_t *dvp = ap->a_dvp;
803 vnode_t **vpp = ap->a_vpp;
804 struct componentname *cnp = ap->a_cnp;
805 struct vattr *vap = ap->a_vap;
806
807 KASSERT(vap->va_type == VDIR);
808 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
809 }
810
811 int
812 tmpfs_rmdir(void *v)
813 {
814 struct vop_rmdir_args /* {
815 struct vnode *a_dvp;
816 struct vnode *a_vp;
817 struct componentname *a_cnp;
818 } */ *ap = v;
819 vnode_t *dvp = ap->a_dvp;
820 vnode_t *vp = ap->a_vp;
821 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
822 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
823 tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
824 tmpfs_dirent_t *de;
825 int error = 0;
826
827 KASSERT(VOP_ISLOCKED(dvp));
828 KASSERT(VOP_ISLOCKED(vp));
829 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
830
831 /*
832 * Directories with more than two non-whiteout
833 * entries ('.' and '..') cannot be removed.
834 */
835 if (node->tn_size > 0) {
836 KASSERT(error == 0);
837 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
838 if (de->td_node != TMPFS_NODE_WHITEOUT) {
839 error = ENOTEMPTY;
840 break;
841 }
842 }
843 if (error)
844 goto out;
845 }
846
847 /* Lookup the directory entry (check the cached hint first). */
848 de = tmpfs_dir_cached(node);
849 if (de == NULL) {
850 struct componentname *cnp = ap->a_cnp;
851 de = tmpfs_dir_lookup(dnode, cnp);
852 }
853 KASSERT(de && de->td_node == node);
854
855 /* Check flags to see if we are allowed to remove the directory. */
856 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
857 error = EPERM;
858 goto out;
859 }
860
861 /* Decrement the link count for the virtual '.' entry. */
862 node->tn_links--;
863 node->tn_status |= TMPFS_NODE_STATUSALL;
864
865 /* Detach the directory entry from the directory. */
866 tmpfs_dir_detach(dvp, de);
867
868 /* Purge the cache for parent. */
869 cache_purge(dvp);
870
871 /*
872 * Destroy the directory entry or replace it with a whiteout.
873 * Note: the inode referred by it will not be destroyed
874 * until the vnode is reclaimed.
875 */
876 if (ap->a_cnp->cn_flags & DOWHITEOUT)
877 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
878 else
879 tmpfs_free_dirent(tmp, de);
880
881 /* Destroy the whiteout entries from the node. */
882 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
883 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
884 tmpfs_dir_detach(vp, de);
885 tmpfs_free_dirent(tmp, de);
886 }
887
888 KASSERT(node->tn_links == 0);
889 out:
890 /* Release the nodes. */
891 vput(dvp);
892 vput(vp);
893 return error;
894 }
895
896 int
897 tmpfs_symlink(void *v)
898 {
899 struct vop_symlink_args /* {
900 struct vnode *a_dvp;
901 struct vnode **a_vpp;
902 struct componentname *a_cnp;
903 struct vattr *a_vap;
904 char *a_target;
905 } */ *ap = v;
906 vnode_t *dvp = ap->a_dvp;
907 vnode_t **vpp = ap->a_vpp;
908 struct componentname *cnp = ap->a_cnp;
909 struct vattr *vap = ap->a_vap;
910 char *target = ap->a_target;
911
912 KASSERT(vap->va_type == VLNK);
913 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
914 }
915
916 int
917 tmpfs_readdir(void *v)
918 {
919 struct vop_readdir_args /* {
920 struct vnode *a_vp;
921 struct uio *a_uio;
922 kauth_cred_t a_cred;
923 int *a_eofflag;
924 off_t **a_cookies;
925 int *ncookies;
926 } */ *ap = v;
927 vnode_t *vp = ap->a_vp;
928 struct uio *uio = ap->a_uio;
929 int *eofflag = ap->a_eofflag;
930 off_t **cookies = ap->a_cookies;
931 int *ncookies = ap->a_ncookies;
932 off_t startoff, cnt;
933 tmpfs_node_t *node;
934 int error;
935
936 KASSERT(VOP_ISLOCKED(vp));
937
938 /* This operation only makes sense on directory nodes. */
939 if (vp->v_type != VDIR) {
940 return ENOTDIR;
941 }
942 node = VP_TO_TMPFS_DIR(vp);
943 startoff = uio->uio_offset;
944 cnt = 0;
945 if (node->tn_links == 0) {
946 error = 0;
947 goto out;
948 }
949
950 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
951 error = tmpfs_dir_getdotdent(node, uio);
952 if (error != 0) {
953 if (error == -1)
954 error = 0;
955 goto out;
956 }
957 cnt++;
958 }
959 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
960 error = tmpfs_dir_getdotdotdent(node, uio);
961 if (error != 0) {
962 if (error == -1)
963 error = 0;
964 goto out;
965 }
966 cnt++;
967 }
968 error = tmpfs_dir_getdents(node, uio, &cnt);
969 if (error == -1) {
970 error = 0;
971 }
972 KASSERT(error >= 0);
973 out:
974 if (eofflag != NULL) {
975 *eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
976 }
977 if (error || cookies == NULL || ncookies == NULL) {
978 return error;
979 }
980
981 /* Update NFS-related variables, if any. */
982 off_t i, off = startoff;
983 tmpfs_dirent_t *de = NULL;
984
985 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
986 *ncookies = cnt;
987
988 for (i = 0; i < cnt; i++) {
989 KASSERT(off != TMPFS_DIRCOOKIE_EOF);
990 if (off != TMPFS_DIRCOOKIE_DOT) {
991 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
992 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
993 } else if (de != NULL) {
994 de = TAILQ_NEXT(de, td_entries);
995 } else {
996 de = tmpfs_dir_lookupbycookie(node, off);
997 KASSERT(de != NULL);
998 de = TAILQ_NEXT(de, td_entries);
999 }
1000 if (de == NULL) {
1001 off = TMPFS_DIRCOOKIE_EOF;
1002 } else {
1003 off = tmpfs_dircookie(de);
1004 }
1005 } else {
1006 off = TMPFS_DIRCOOKIE_DOTDOT;
1007 }
1008 (*cookies)[i] = off;
1009 }
1010 KASSERT(uio->uio_offset == off);
1011 return error;
1012 }
1013
1014 int
1015 tmpfs_readlink(void *v)
1016 {
1017 struct vop_readlink_args /* {
1018 struct vnode *a_vp;
1019 struct uio *a_uio;
1020 kauth_cred_t a_cred;
1021 } */ *ap = v;
1022 vnode_t *vp = ap->a_vp;
1023 struct uio *uio = ap->a_uio;
1024 tmpfs_node_t *node;
1025 int error;
1026
1027 KASSERT(VOP_ISLOCKED(vp));
1028 KASSERT(uio->uio_offset == 0);
1029 KASSERT(vp->v_type == VLNK);
1030
1031 node = VP_TO_TMPFS_NODE(vp);
1032 error = uiomove(node->tn_spec.tn_lnk.tn_link,
1033 MIN(node->tn_size, uio->uio_resid), uio);
1034 node->tn_status |= TMPFS_NODE_ACCESSED;
1035
1036 return error;
1037 }
1038
1039 int
1040 tmpfs_inactive(void *v)
1041 {
1042 struct vop_inactive_args /* {
1043 struct vnode *a_vp;
1044 bool *a_recycle;
1045 } */ *ap = v;
1046 vnode_t *vp = ap->a_vp;
1047 tmpfs_node_t *node;
1048
1049 KASSERT(VOP_ISLOCKED(vp));
1050
1051 node = VP_TO_TMPFS_NODE(vp);
1052 *ap->a_recycle = (node->tn_links == 0);
1053 VOP_UNLOCK(vp);
1054
1055 return 0;
1056 }
1057
1058 int
1059 tmpfs_reclaim(void *v)
1060 {
1061 struct vop_reclaim_args /* {
1062 struct vnode *a_vp;
1063 } */ *ap = v;
1064 vnode_t *vp = ap->a_vp;
1065 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
1066 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1067 bool racing;
1068
1069 /* Disassociate inode from vnode. */
1070 mutex_enter(&node->tn_vlock);
1071 node->tn_vnode = NULL;
1072 vp->v_data = NULL;
1073 /* Check if tmpfs_vnode_get() is racing with us. */
1074 racing = TMPFS_NODE_RECLAIMING(node);
1075 mutex_exit(&node->tn_vlock);
1076
1077 /*
1078 * If inode is not referenced, i.e. no links, then destroy it.
1079 * Note: if racing - inode is about to get a new vnode, leave it.
1080 */
1081 if (node->tn_links == 0 && !racing) {
1082 tmpfs_free_node(tmp, node);
1083 }
1084 return 0;
1085 }
1086
1087 int
1088 tmpfs_pathconf(void *v)
1089 {
1090 struct vop_pathconf_args /* {
1091 struct vnode *a_vp;
1092 int a_name;
1093 register_t *a_retval;
1094 } */ *ap = v;
1095 const int name = ap->a_name;
1096 register_t *retval = ap->a_retval;
1097 int error = 0;
1098
1099 switch (name) {
1100 case _PC_LINK_MAX:
1101 *retval = LINK_MAX;
1102 break;
1103 case _PC_NAME_MAX:
1104 *retval = TMPFS_MAXNAMLEN;
1105 break;
1106 case _PC_PATH_MAX:
1107 *retval = PATH_MAX;
1108 break;
1109 case _PC_PIPE_BUF:
1110 *retval = PIPE_BUF;
1111 break;
1112 case _PC_CHOWN_RESTRICTED:
1113 *retval = 1;
1114 break;
1115 case _PC_NO_TRUNC:
1116 *retval = 1;
1117 break;
1118 case _PC_SYNC_IO:
1119 *retval = 1;
1120 break;
1121 case _PC_FILESIZEBITS:
1122 *retval = sizeof(off_t) * CHAR_BIT;
1123 break;
1124 default:
1125 error = EINVAL;
1126 }
1127 return error;
1128 }
1129
1130 int
1131 tmpfs_advlock(void *v)
1132 {
1133 struct vop_advlock_args /* {
1134 struct vnode *a_vp;
1135 void * a_id;
1136 int a_op;
1137 struct flock *a_fl;
1138 int a_flags;
1139 } */ *ap = v;
1140 vnode_t *vp = ap->a_vp;
1141 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1142
1143 return lf_advlock(v, &node->tn_lockf, node->tn_size);
1144 }
1145
1146 int
1147 tmpfs_getpages(void *v)
1148 {
1149 struct vop_getpages_args /* {
1150 struct vnode *a_vp;
1151 voff_t a_offset;
1152 struct vm_page **a_m;
1153 int *a_count;
1154 int a_centeridx;
1155 vm_prot_t a_access_type;
1156 int a_advice;
1157 int a_flags;
1158 } */ * const ap = v;
1159 vnode_t *vp = ap->a_vp;
1160 const voff_t offset = ap->a_offset;
1161 struct vm_page **pgs = ap->a_m;
1162 const int centeridx = ap->a_centeridx;
1163 const vm_prot_t access_type = ap->a_access_type;
1164 const int advice = ap->a_advice;
1165 const int flags = ap->a_flags;
1166 int error, npages = *ap->a_count;
1167 tmpfs_node_t *node;
1168 struct uvm_object *uobj;
1169
1170 KASSERT(vp->v_type == VREG);
1171 KASSERT(mutex_owned(vp->v_interlock));
1172
1173 node = VP_TO_TMPFS_NODE(vp);
1174 uobj = node->tn_spec.tn_reg.tn_aobj;
1175
1176 /*
1177 * Currently, PGO_PASTEOF is not supported.
1178 */
1179 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1180 if ((flags & PGO_LOCKED) == 0)
1181 mutex_exit(vp->v_interlock);
1182 return EINVAL;
1183 }
1184
1185 if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1186 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1187 }
1188
1189 if ((flags & PGO_LOCKED) != 0)
1190 return EBUSY;
1191
1192 if ((flags & PGO_NOTIMESTAMP) == 0) {
1193 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1194 node->tn_status |= TMPFS_NODE_ACCESSED;
1195
1196 if ((access_type & VM_PROT_WRITE) != 0) {
1197 node->tn_status |= TMPFS_NODE_MODIFIED;
1198 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1199 node->tn_status |= TMPFS_NODE_ACCESSED;
1200 }
1201 }
1202
1203 /*
1204 * Invoke the pager.
1205 *
1206 * Clean the array of pages before. XXX: PR/32166
1207 * Note that vnode lock is shared with underlying UVM object.
1208 */
1209 if (pgs) {
1210 memset(pgs, 0, sizeof(struct vm_pages *) * npages);
1211 }
1212 KASSERT(vp->v_interlock == uobj->vmobjlock);
1213
1214 error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
1215 access_type, advice, flags | PGO_ALLPAGES);
1216
1217 #if defined(DEBUG)
1218 if (!error && pgs) {
1219 for (int i = 0; i < npages; i++) {
1220 KASSERT(pgs[i] != NULL);
1221 }
1222 }
1223 #endif
1224 return error;
1225 }
1226
1227 int
1228 tmpfs_putpages(void *v)
1229 {
1230 struct vop_putpages_args /* {
1231 struct vnode *a_vp;
1232 voff_t a_offlo;
1233 voff_t a_offhi;
1234 int a_flags;
1235 } */ * const ap = v;
1236 vnode_t *vp = ap->a_vp;
1237 const voff_t offlo = ap->a_offlo;
1238 const voff_t offhi = ap->a_offhi;
1239 const int flags = ap->a_flags;
1240 tmpfs_node_t *node;
1241 struct uvm_object *uobj;
1242 int error;
1243
1244 KASSERT(mutex_owned(vp->v_interlock));
1245
1246 if (vp->v_type != VREG) {
1247 mutex_exit(vp->v_interlock);
1248 return 0;
1249 }
1250
1251 node = VP_TO_TMPFS_NODE(vp);
1252 uobj = node->tn_spec.tn_reg.tn_aobj;
1253
1254 KASSERT(vp->v_interlock == uobj->vmobjlock);
1255 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1256
1257 /* XXX mtime */
1258
1259 return error;
1260 }
1261
1262 int
1263 tmpfs_whiteout(void *v)
1264 {
1265 struct vop_whiteout_args /* {
1266 struct vnode *a_dvp;
1267 struct componentname *a_cnp;
1268 int a_flags;
1269 } */ *ap = v;
1270 vnode_t *dvp = ap->a_dvp;
1271 struct componentname *cnp = ap->a_cnp;
1272 const int flags = ap->a_flags;
1273 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
1274 tmpfs_dirent_t *de;
1275 int error;
1276
1277 switch (flags) {
1278 case LOOKUP:
1279 break;
1280 case CREATE:
1281 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
1282 cnp->cn_namelen, &de);
1283 if (error)
1284 return error;
1285 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
1286 break;
1287 case DELETE:
1288 cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
1289 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
1290 if (de == NULL)
1291 return ENOENT;
1292 tmpfs_dir_detach(dvp, de);
1293 tmpfs_free_dirent(tmp, de);
1294 break;
1295 }
1296 return 0;
1297 }
1298
1299 int
1300 tmpfs_print(void *v)
1301 {
1302 struct vop_print_args /* {
1303 struct vnode *a_vp;
1304 } */ *ap = v;
1305 vnode_t *vp = ap->a_vp;
1306 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1307
1308 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
1309 "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
1310 node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
1311 node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
1312 if (vp->v_type == VFIFO) {
1313 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
1314 }
1315 printf("\n");
1316 return 0;
1317 }
1318