tmpfs_vnops.c revision 1.98.2.1 1 /* $NetBSD: tmpfs_vnops.c,v 1.98.2.1 2012/11/20 03:02:41 tls 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.98.2.1 2012/11/20 03:02:41 tls 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 int error;
536
537 KASSERT(VOP_ISLOCKED(vp));
538
539 if (vp->v_type != VREG) {
540 return EISDIR;
541 }
542 if (uio->uio_offset < 0) {
543 return EINVAL;
544 }
545
546 node = VP_TO_TMPFS_NODE(vp);
547 node->tn_status |= TMPFS_NODE_ACCESSED;
548 uobj = node->tn_spec.tn_reg.tn_aobj;
549 error = 0;
550
551 while (error == 0 && uio->uio_resid > 0) {
552 vsize_t len;
553
554 if (node->tn_size <= uio->uio_offset) {
555 break;
556 }
557 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
558 if (len == 0) {
559 break;
560 }
561 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
562 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
563 }
564 return error;
565 }
566
567 int
568 tmpfs_write(void *v)
569 {
570 struct vop_write_args /* {
571 struct vnode *a_vp;
572 struct uio *a_uio;
573 int a_ioflag;
574 kauth_cred_t a_cred;
575 } */ *ap = v;
576 vnode_t *vp = ap->a_vp;
577 struct uio *uio = ap->a_uio;
578 const int ioflag = ap->a_ioflag;
579 tmpfs_node_t *node;
580 struct uvm_object *uobj;
581 off_t oldsize;
582 bool extended;
583 int error;
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 if (uio->uio_resid == 0) {
595 error = 0;
596 goto out;
597 }
598 if (ioflag & IO_APPEND) {
599 uio->uio_offset = node->tn_size;
600 }
601
602 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
603 if (extended) {
604 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
605 if (error)
606 goto out;
607 }
608
609 uobj = node->tn_spec.tn_reg.tn_aobj;
610 error = 0;
611 while (error == 0 && uio->uio_resid > 0) {
612 vsize_t len;
613
614 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
615 if (len == 0) {
616 break;
617 }
618 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
619 UBC_WRITE | UBC_UNMAP_FLAG(vp));
620 }
621 if (error) {
622 (void)tmpfs_reg_resize(vp, oldsize);
623 }
624
625 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
626 (extended ? TMPFS_NODE_CHANGED : 0);
627 VN_KNOTE(vp, NOTE_WRITE);
628 out:
629 if (error) {
630 KASSERT(oldsize == node->tn_size);
631 } else {
632 KASSERT(uio->uio_resid == 0);
633 }
634 return error;
635 }
636
637 int
638 tmpfs_fsync(void *v)
639 {
640 struct vop_fsync_args /* {
641 struct vnode *a_vp;
642 kauth_cred_t a_cred;
643 int a_flags;
644 off_t a_offlo;
645 off_t a_offhi;
646 struct lwp *a_l;
647 } */ *ap = v;
648 vnode_t *vp = ap->a_vp;
649
650 /* Nothing to do. Just update. */
651 KASSERT(VOP_ISLOCKED(vp));
652 tmpfs_update(vp, NULL, NULL, NULL, 0);
653 return 0;
654 }
655
656 /*
657 * tmpfs_remove: unlink a file.
658 *
659 * => Both directory (dvp) and file (vp) are locked.
660 * => We unlock and drop the reference on both.
661 */
662 int
663 tmpfs_remove(void *v)
664 {
665 struct vop_remove_args /* {
666 struct vnode *a_dvp;
667 struct vnode *a_vp;
668 struct componentname *a_cnp;
669 } */ *ap = v;
670 vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
671 tmpfs_node_t *node;
672 tmpfs_dirent_t *de;
673 int error;
674
675 KASSERT(VOP_ISLOCKED(dvp));
676 KASSERT(VOP_ISLOCKED(vp));
677
678 if (vp->v_type == VDIR) {
679 error = EPERM;
680 goto out;
681 }
682 node = VP_TO_TMPFS_NODE(vp);
683
684 /* Files marked as immutable or append-only cannot be deleted. */
685 if (node->tn_flags & (IMMUTABLE | APPEND)) {
686 error = EPERM;
687 goto out;
688 }
689
690 /* Lookup the directory entry (check the cached hint first). */
691 de = tmpfs_dir_cached(node);
692 if (de == NULL) {
693 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
694 struct componentname *cnp = ap->a_cnp;
695 de = tmpfs_dir_lookup(dnode, cnp);
696 }
697 KASSERT(de && de->td_node == node);
698
699 /*
700 * Remove the entry from the directory (drops the link count) and
701 * destroy it or replace it with a whiteout.
702 * Note: the inode referred by it will not be destroyed
703 * until the vnode is reclaimed/recycled.
704 */
705 tmpfs_dir_detach(dvp, de);
706 if (ap->a_cnp->cn_flags & DOWHITEOUT)
707 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
708 else
709 tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
710 error = 0;
711 out:
712 /* Drop the references and unlock the vnodes. */
713 vput(vp);
714 if (dvp == vp) {
715 vrele(dvp);
716 } else {
717 vput(dvp);
718 }
719 return error;
720 }
721
722 /*
723 * tmpfs_link: create a hard link.
724 */
725 int
726 tmpfs_link(void *v)
727 {
728 struct vop_link_args /* {
729 struct vnode *a_dvp;
730 struct vnode *a_vp;
731 struct componentname *a_cnp;
732 } */ *ap = v;
733 vnode_t *dvp = ap->a_dvp;
734 vnode_t *vp = ap->a_vp;
735 struct componentname *cnp = ap->a_cnp;
736 tmpfs_node_t *dnode, *node;
737 tmpfs_dirent_t *de;
738 int error;
739
740 KASSERT(dvp != vp);
741 KASSERT(VOP_ISLOCKED(dvp));
742 KASSERT(vp->v_type != VDIR);
743 KASSERT(dvp->v_mount == vp->v_mount);
744
745 dnode = VP_TO_TMPFS_DIR(dvp);
746 node = VP_TO_TMPFS_NODE(vp);
747
748 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
749
750 /* Check for maximum number of links limit. */
751 if (node->tn_links == LINK_MAX) {
752 error = EMLINK;
753 goto out;
754 }
755 KASSERT(node->tn_links < LINK_MAX);
756
757 /* We cannot create links of files marked immutable or append-only. */
758 if (node->tn_flags & (IMMUTABLE | APPEND)) {
759 error = EPERM;
760 goto out;
761 }
762
763 /* Allocate a new directory entry to represent the inode. */
764 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
765 cnp->cn_nameptr, cnp->cn_namelen, &de);
766 if (error) {
767 goto out;
768 }
769
770 /*
771 * Insert the entry into the directory.
772 * It will increase the inode link count.
773 */
774 tmpfs_dir_attach(dvp, de, node);
775
776 /* Update the timestamps and trigger the event. */
777 if (node->tn_vnode) {
778 VN_KNOTE(node->tn_vnode, NOTE_LINK);
779 }
780 node->tn_status |= TMPFS_NODE_CHANGED;
781 tmpfs_update(vp, NULL, NULL, NULL, 0);
782 error = 0;
783 out:
784 VOP_UNLOCK(vp);
785 vput(dvp);
786 return error;
787 }
788
789 int
790 tmpfs_mkdir(void *v)
791 {
792 struct vop_mkdir_args /* {
793 struct vnode *a_dvp;
794 struct vnode **a_vpp;
795 struct componentname *a_cnp;
796 struct vattr *a_vap;
797 } */ *ap = v;
798 vnode_t *dvp = ap->a_dvp;
799 vnode_t **vpp = ap->a_vpp;
800 struct componentname *cnp = ap->a_cnp;
801 struct vattr *vap = ap->a_vap;
802
803 KASSERT(vap->va_type == VDIR);
804 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
805 }
806
807 int
808 tmpfs_rmdir(void *v)
809 {
810 struct vop_rmdir_args /* {
811 struct vnode *a_dvp;
812 struct vnode *a_vp;
813 struct componentname *a_cnp;
814 } */ *ap = v;
815 vnode_t *dvp = ap->a_dvp;
816 vnode_t *vp = ap->a_vp;
817 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
818 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
819 tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
820 tmpfs_dirent_t *de;
821 int error = 0;
822
823 KASSERT(VOP_ISLOCKED(dvp));
824 KASSERT(VOP_ISLOCKED(vp));
825 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
826
827 /*
828 * Directories with more than two non-whiteout
829 * entries ('.' and '..') cannot be removed.
830 */
831 if (node->tn_size > 0) {
832 KASSERT(error == 0);
833 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
834 if (de->td_node != TMPFS_NODE_WHITEOUT) {
835 error = ENOTEMPTY;
836 break;
837 }
838 }
839 if (error)
840 goto out;
841 }
842
843 /* Lookup the directory entry (check the cached hint first). */
844 de = tmpfs_dir_cached(node);
845 if (de == NULL) {
846 struct componentname *cnp = ap->a_cnp;
847 de = tmpfs_dir_lookup(dnode, cnp);
848 }
849 KASSERT(de && de->td_node == node);
850
851 /* Check flags to see if we are allowed to remove the directory. */
852 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
853 error = EPERM;
854 goto out;
855 }
856
857 /* Decrement the link count for the virtual '.' entry. */
858 node->tn_links--;
859 node->tn_status |= TMPFS_NODE_STATUSALL;
860
861 /* Detach the directory entry from the directory. */
862 tmpfs_dir_detach(dvp, de);
863
864 /* Purge the cache for parent. */
865 cache_purge(dvp);
866
867 /*
868 * Destroy the directory entry or replace it with a whiteout.
869 * Note: the inode referred by it will not be destroyed
870 * until the vnode is reclaimed.
871 */
872 if (ap->a_cnp->cn_flags & DOWHITEOUT)
873 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
874 else
875 tmpfs_free_dirent(tmp, de);
876
877 /* Destroy the whiteout entries from the node. */
878 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
879 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
880 tmpfs_dir_detach(vp, de);
881 tmpfs_free_dirent(tmp, de);
882 }
883
884 KASSERT(node->tn_links == 0);
885 out:
886 /* Release the nodes. */
887 vput(dvp);
888 vput(vp);
889 return error;
890 }
891
892 int
893 tmpfs_symlink(void *v)
894 {
895 struct vop_symlink_args /* {
896 struct vnode *a_dvp;
897 struct vnode **a_vpp;
898 struct componentname *a_cnp;
899 struct vattr *a_vap;
900 char *a_target;
901 } */ *ap = v;
902 vnode_t *dvp = ap->a_dvp;
903 vnode_t **vpp = ap->a_vpp;
904 struct componentname *cnp = ap->a_cnp;
905 struct vattr *vap = ap->a_vap;
906 char *target = ap->a_target;
907
908 KASSERT(vap->va_type == VLNK);
909 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
910 }
911
912 int
913 tmpfs_readdir(void *v)
914 {
915 struct vop_readdir_args /* {
916 struct vnode *a_vp;
917 struct uio *a_uio;
918 kauth_cred_t a_cred;
919 int *a_eofflag;
920 off_t **a_cookies;
921 int *ncookies;
922 } */ *ap = v;
923 vnode_t *vp = ap->a_vp;
924 struct uio *uio = ap->a_uio;
925 int *eofflag = ap->a_eofflag;
926 off_t **cookies = ap->a_cookies;
927 int *ncookies = ap->a_ncookies;
928 off_t startoff, cnt;
929 tmpfs_node_t *node;
930 int error;
931
932 KASSERT(VOP_ISLOCKED(vp));
933
934 /* This operation only makes sense on directory nodes. */
935 if (vp->v_type != VDIR) {
936 return ENOTDIR;
937 }
938 node = VP_TO_TMPFS_DIR(vp);
939 startoff = uio->uio_offset;
940 cnt = 0;
941 if (node->tn_links == 0) {
942 error = 0;
943 goto out;
944 }
945
946 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
947 error = tmpfs_dir_getdotdent(node, uio);
948 if (error != 0) {
949 if (error == -1)
950 error = 0;
951 goto out;
952 }
953 cnt++;
954 }
955 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
956 error = tmpfs_dir_getdotdotdent(node, uio);
957 if (error != 0) {
958 if (error == -1)
959 error = 0;
960 goto out;
961 }
962 cnt++;
963 }
964 error = tmpfs_dir_getdents(node, uio, &cnt);
965 if (error == -1) {
966 error = 0;
967 }
968 KASSERT(error >= 0);
969 out:
970 if (eofflag != NULL) {
971 *eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
972 }
973 if (error || cookies == NULL || ncookies == NULL) {
974 return error;
975 }
976
977 /* Update NFS-related variables, if any. */
978 off_t i, off = startoff;
979 tmpfs_dirent_t *de = NULL;
980
981 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
982 *ncookies = cnt;
983
984 for (i = 0; i < cnt; i++) {
985 KASSERT(off != TMPFS_DIRCOOKIE_EOF);
986 if (off != TMPFS_DIRCOOKIE_DOT) {
987 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
988 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
989 } else if (de != NULL) {
990 de = TAILQ_NEXT(de, td_entries);
991 } else {
992 de = tmpfs_dir_lookupbycookie(node, off);
993 KASSERT(de != NULL);
994 de = TAILQ_NEXT(de, td_entries);
995 }
996 if (de == NULL) {
997 off = TMPFS_DIRCOOKIE_EOF;
998 } else {
999 off = tmpfs_dircookie(de);
1000 }
1001 } else {
1002 off = TMPFS_DIRCOOKIE_DOTDOT;
1003 }
1004 (*cookies)[i] = off;
1005 }
1006 KASSERT(uio->uio_offset == off);
1007 return error;
1008 }
1009
1010 int
1011 tmpfs_readlink(void *v)
1012 {
1013 struct vop_readlink_args /* {
1014 struct vnode *a_vp;
1015 struct uio *a_uio;
1016 kauth_cred_t a_cred;
1017 } */ *ap = v;
1018 vnode_t *vp = ap->a_vp;
1019 struct uio *uio = ap->a_uio;
1020 tmpfs_node_t *node;
1021 int error;
1022
1023 KASSERT(VOP_ISLOCKED(vp));
1024 KASSERT(uio->uio_offset == 0);
1025 KASSERT(vp->v_type == VLNK);
1026
1027 node = VP_TO_TMPFS_NODE(vp);
1028 error = uiomove(node->tn_spec.tn_lnk.tn_link,
1029 MIN(node->tn_size, uio->uio_resid), uio);
1030 node->tn_status |= TMPFS_NODE_ACCESSED;
1031
1032 return error;
1033 }
1034
1035 int
1036 tmpfs_inactive(void *v)
1037 {
1038 struct vop_inactive_args /* {
1039 struct vnode *a_vp;
1040 bool *a_recycle;
1041 } */ *ap = v;
1042 vnode_t *vp = ap->a_vp;
1043 tmpfs_node_t *node;
1044
1045 KASSERT(VOP_ISLOCKED(vp));
1046
1047 node = VP_TO_TMPFS_NODE(vp);
1048 *ap->a_recycle = (node->tn_links == 0);
1049 VOP_UNLOCK(vp);
1050
1051 return 0;
1052 }
1053
1054 int
1055 tmpfs_reclaim(void *v)
1056 {
1057 struct vop_reclaim_args /* {
1058 struct vnode *a_vp;
1059 } */ *ap = v;
1060 vnode_t *vp = ap->a_vp;
1061 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
1062 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1063 bool racing;
1064
1065 /* Disassociate inode from vnode. */
1066 mutex_enter(&node->tn_vlock);
1067 node->tn_vnode = NULL;
1068 vp->v_data = NULL;
1069 /* Check if tmpfs_vnode_get() is racing with us. */
1070 racing = TMPFS_NODE_RECLAIMING(node);
1071 mutex_exit(&node->tn_vlock);
1072
1073 /*
1074 * If inode is not referenced, i.e. no links, then destroy it.
1075 * Note: if racing - inode is about to get a new vnode, leave it.
1076 */
1077 if (node->tn_links == 0 && !racing) {
1078 tmpfs_free_node(tmp, node);
1079 }
1080 return 0;
1081 }
1082
1083 int
1084 tmpfs_pathconf(void *v)
1085 {
1086 struct vop_pathconf_args /* {
1087 struct vnode *a_vp;
1088 int a_name;
1089 register_t *a_retval;
1090 } */ *ap = v;
1091 const int name = ap->a_name;
1092 register_t *retval = ap->a_retval;
1093 int error = 0;
1094
1095 switch (name) {
1096 case _PC_LINK_MAX:
1097 *retval = LINK_MAX;
1098 break;
1099 case _PC_NAME_MAX:
1100 *retval = TMPFS_MAXNAMLEN;
1101 break;
1102 case _PC_PATH_MAX:
1103 *retval = PATH_MAX;
1104 break;
1105 case _PC_PIPE_BUF:
1106 *retval = PIPE_BUF;
1107 break;
1108 case _PC_CHOWN_RESTRICTED:
1109 *retval = 1;
1110 break;
1111 case _PC_NO_TRUNC:
1112 *retval = 1;
1113 break;
1114 case _PC_SYNC_IO:
1115 *retval = 1;
1116 break;
1117 case _PC_FILESIZEBITS:
1118 *retval = sizeof(off_t) * CHAR_BIT;
1119 break;
1120 default:
1121 error = EINVAL;
1122 }
1123 return error;
1124 }
1125
1126 int
1127 tmpfs_advlock(void *v)
1128 {
1129 struct vop_advlock_args /* {
1130 struct vnode *a_vp;
1131 void * a_id;
1132 int a_op;
1133 struct flock *a_fl;
1134 int a_flags;
1135 } */ *ap = v;
1136 vnode_t *vp = ap->a_vp;
1137 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1138
1139 return lf_advlock(v, &node->tn_lockf, node->tn_size);
1140 }
1141
1142 int
1143 tmpfs_getpages(void *v)
1144 {
1145 struct vop_getpages_args /* {
1146 struct vnode *a_vp;
1147 voff_t a_offset;
1148 struct vm_page **a_m;
1149 int *a_count;
1150 int a_centeridx;
1151 vm_prot_t a_access_type;
1152 int a_advice;
1153 int a_flags;
1154 } */ * const ap = v;
1155 vnode_t *vp = ap->a_vp;
1156 const voff_t offset = ap->a_offset;
1157 struct vm_page **pgs = ap->a_m;
1158 const int centeridx = ap->a_centeridx;
1159 const vm_prot_t access_type = ap->a_access_type;
1160 const int advice = ap->a_advice;
1161 const int flags = ap->a_flags;
1162 int error, npages = *ap->a_count;
1163 tmpfs_node_t *node;
1164 struct uvm_object *uobj;
1165
1166 KASSERT(vp->v_type == VREG);
1167 KASSERT(mutex_owned(vp->v_interlock));
1168
1169 node = VP_TO_TMPFS_NODE(vp);
1170 uobj = node->tn_spec.tn_reg.tn_aobj;
1171
1172 /*
1173 * Currently, PGO_PASTEOF is not supported.
1174 */
1175 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1176 if ((flags & PGO_LOCKED) == 0)
1177 mutex_exit(vp->v_interlock);
1178 return EINVAL;
1179 }
1180
1181 if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1182 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1183 }
1184
1185 if ((flags & PGO_LOCKED) != 0)
1186 return EBUSY;
1187
1188 if ((flags & PGO_NOTIMESTAMP) == 0) {
1189 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1190 node->tn_status |= TMPFS_NODE_ACCESSED;
1191
1192 if ((access_type & VM_PROT_WRITE) != 0) {
1193 node->tn_status |= TMPFS_NODE_MODIFIED;
1194 if (vp->v_mount->mnt_flag & MNT_RELATIME)
1195 node->tn_status |= TMPFS_NODE_ACCESSED;
1196 }
1197 }
1198
1199 /*
1200 * Invoke the pager.
1201 *
1202 * Clean the array of pages before. XXX: PR/32166
1203 * Note that vnode lock is shared with underlying UVM object.
1204 */
1205 if (pgs) {
1206 memset(pgs, 0, sizeof(struct vm_pages *) * npages);
1207 }
1208 KASSERT(vp->v_interlock == uobj->vmobjlock);
1209
1210 error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
1211 access_type, advice, flags | PGO_ALLPAGES);
1212
1213 #if defined(DEBUG)
1214 if (!error && pgs) {
1215 for (int i = 0; i < npages; i++) {
1216 KASSERT(pgs[i] != NULL);
1217 }
1218 }
1219 #endif
1220 return error;
1221 }
1222
1223 int
1224 tmpfs_putpages(void *v)
1225 {
1226 struct vop_putpages_args /* {
1227 struct vnode *a_vp;
1228 voff_t a_offlo;
1229 voff_t a_offhi;
1230 int a_flags;
1231 } */ * const ap = v;
1232 vnode_t *vp = ap->a_vp;
1233 const voff_t offlo = ap->a_offlo;
1234 const voff_t offhi = ap->a_offhi;
1235 const int flags = ap->a_flags;
1236 tmpfs_node_t *node;
1237 struct uvm_object *uobj;
1238 int error;
1239
1240 KASSERT(mutex_owned(vp->v_interlock));
1241
1242 if (vp->v_type != VREG) {
1243 mutex_exit(vp->v_interlock);
1244 return 0;
1245 }
1246
1247 node = VP_TO_TMPFS_NODE(vp);
1248 uobj = node->tn_spec.tn_reg.tn_aobj;
1249
1250 KASSERT(vp->v_interlock == uobj->vmobjlock);
1251 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1252
1253 /* XXX mtime */
1254
1255 return error;
1256 }
1257
1258 int
1259 tmpfs_whiteout(void *v)
1260 {
1261 struct vop_whiteout_args /* {
1262 struct vnode *a_dvp;
1263 struct componentname *a_cnp;
1264 int a_flags;
1265 } */ *ap = v;
1266 vnode_t *dvp = ap->a_dvp;
1267 struct componentname *cnp = ap->a_cnp;
1268 const int flags = ap->a_flags;
1269 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
1270 tmpfs_dirent_t *de;
1271 int error;
1272
1273 switch (flags) {
1274 case LOOKUP:
1275 break;
1276 case CREATE:
1277 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
1278 cnp->cn_namelen, &de);
1279 if (error)
1280 return error;
1281 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
1282 break;
1283 case DELETE:
1284 cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
1285 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
1286 if (de == NULL)
1287 return ENOENT;
1288 tmpfs_dir_detach(dvp, de);
1289 tmpfs_free_dirent(tmp, de);
1290 break;
1291 }
1292 return 0;
1293 }
1294
1295 int
1296 tmpfs_print(void *v)
1297 {
1298 struct vop_print_args /* {
1299 struct vnode *a_vp;
1300 } */ *ap = v;
1301 vnode_t *vp = ap->a_vp;
1302 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1303
1304 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
1305 "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
1306 node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
1307 node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
1308 if (vp->v_type == VFIFO) {
1309 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
1310 }
1311 printf("\n");
1312 return 0;
1313 }
1314